Skip to content

Commit 01bfaf3

Browse files
authored
Merge pull request #39 from chriscool/add-hash-tests
Add hash tests
2 parents def61aa + 48545fe commit 01bfaf3

File tree

4 files changed

+135
-5
lines changed

4 files changed

+135
-5
lines changed

tests/sharness/lib/test-lib.sh

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Test framework for multihash
2+
#
3+
# Copyright (c) 2016 Christian Couder
4+
# MIT Licensed; see the LICENSE file in this repository.
5+
#
6+
# We are using sharness (https://github.com/chriscool/sharness)
7+
# which was extracted from the Git test framework.
8+
9+
# Test the first 'multihash' tool available in the PATH.
10+
11+
# Add current bin directory to path, in case multihash tool is there.
12+
PATH=$(pwd)/bin:${PATH}
13+
14+
# Set sharness verbosity. we set the env var directly as
15+
# it's too late to pass in --verbose, and --verbose is harder
16+
# to pass through in some cases.
17+
test "$TEST_VERBOSE" = 1 && verbose=t
18+
19+
# Assert a `multihash` tool is available in the PATH.
20+
type multihash >/dev/null || {
21+
echo >&2 "Cannot find any 'multihash' tool in '$PATH'."
22+
echo >&2 "Please make sure 'multihash' is in your PATH."
23+
exit 1
24+
}
25+
26+
SHARNESS_LIB="lib/sharness/sharness.sh"
27+
28+
. "$SHARNESS_LIB" || {
29+
echo >&2 "Cannot source: $SHARNESS_LIB"
30+
echo >&2 "Please check Sharness installation."
31+
exit 1
32+
}
33+
34+
# Please put multihash specific shell functions below
35+
36+
if type shasum >/dev/null; then
37+
export SHA1SUMBIN="shasum" &&
38+
test_set_prereq SHA1SUM &&
39+
export SHA256SUMBIN="shasum -a 256" &&
40+
test_set_prereq SHA256SUM &&
41+
export SHA512SUMBIN="shasum -a 512" &&
42+
test_set_prereq SHA512SUM
43+
else
44+
if type sha1sum >/dev/null; then
45+
export SHA1SUMBIN="sha1sum" &&
46+
test_set_prereq SHA1SUM
47+
fi
48+
if type sha256sum >/dev/null; then
49+
export SHA256SUMBIN="sha256sum" &&
50+
test_set_prereq SHA256SUM
51+
fi
52+
if type sha512sum >/dev/null; then
53+
export SHA512SUMBIN="sha512sum" &&
54+
test_set_prereq SHA512SUM
55+
fi
56+
fi

tests/sharness/t0010-multihash-basics.sh

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,12 @@
22

33
test_description="Test multihash basics"
44

5-
. ./lib/sharness/sharness.sh
5+
. lib/test-lib.sh
66

77
test_expect_success "current dir is writable" '
88
echo "It works!" >test.txt
99
'
1010

11-
test_expect_success "multihash is available" '
12-
type multihash
13-
'
14-
1511
test_expect_success "'multihash --help' looks good" '
1612
multihash --help >actual 2>&1 || true &&
1713
egrep -i "usage" actual >/dev/null

tests/sharness/t0020-sha1.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/sh
2+
#
3+
# Copyright (c) 2015 Christian Couder
4+
# MIT Licensed; see the LICENSE file in this repository.
5+
#
6+
7+
test_description="sha1 tests"
8+
9+
. lib/test-lib.sh
10+
11+
test_expect_success "setup sha1 tests" '
12+
echo "Hash me!" >hash_me.txt &&
13+
SHA1=bc6f2c3cd945bc754789e50b2f68deee2f421810 &&
14+
echo "1114$SHA1" >expected
15+
'
16+
17+
test_expect_success "'multihash -a=sha1 -e=hex' works" '
18+
multihash -a=sha1 -e=hex hash_me.txt >actual
19+
'
20+
21+
test_expect_success "'multihash -a=sha1 -e=hex' output looks good" '
22+
test_cmp expected actual
23+
'
24+
25+
test_expect_success SHA1SUM "check hash using '$SHA1SUMBIN'" '
26+
echo "$SHA1 hash_me.txt" >expected &&
27+
$SHA1SUMBIN hash_me.txt >actual &&
28+
test_cmp expected actual
29+
'
30+
31+
test_done

tests/sharness/t0030-sha2.sh

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/bin/sh
2+
#
3+
# Copyright (c) 2016 Christian Couder
4+
# MIT Licensed; see the LICENSE file in this repository.
5+
#
6+
7+
test_description="sha2 tests"
8+
9+
. lib/test-lib.sh
10+
11+
test_expect_success "setup sha2 tests" '
12+
echo "Hash me!" >hash_me.txt &&
13+
SHA256=c16bdce7e126ff8e241a9893ccd908c292cd0e54c403145326eb0c567071a613 &&
14+
SHA512=4c4a59a7d958cff035eb7d752b319b90337037e352b89d58aa5be29ef051ea0a565133781aa3279384654274c4786893287ea8037f0a1a15d9b40ea0bc4be73c &&
15+
echo "1220$SHA256" >expected256 &&
16+
echo "1340$SHA512" >expected512
17+
'
18+
19+
test_expect_success "'multihash -a=sha2-256 -e=hex' works" '
20+
multihash -a=sha2-256 -e=hex hash_me.txt >actual256
21+
'
22+
23+
test_expect_success "'multihash -a=sha2-256 -e=hex' output looks good" '
24+
test_cmp expected256 actual256
25+
'
26+
27+
test_expect_success "'multihash -a=sha2-512 -e=hex' works" '
28+
multihash -a=sha2-512 -e=hex hash_me.txt >actual512
29+
'
30+
31+
test_expect_success "'multihash -a=sha2-512 -e=hex' output looks good" '
32+
test_cmp expected512 actual512
33+
'
34+
35+
test_expect_success SHA256SUM "check sha2-256 hash using '$SHA256SUMBIN'" '
36+
echo "$SHA256 hash_me.txt" >expected &&
37+
$SHA256SUMBIN hash_me.txt >actual &&
38+
test_cmp expected actual
39+
'
40+
41+
test_expect_success SHA512SUM "check sha2-512 hash using '$SHA512SUMBIN'" '
42+
echo "$SHA512 hash_me.txt" >expected &&
43+
$SHA512SUMBIN hash_me.txt >actual &&
44+
test_cmp expected actual
45+
'
46+
47+
test_done

0 commit comments

Comments
 (0)