Skip to content

Commit def61aa

Browse files
authored
Merge pull request #42 from multiformats/rand-test-cases
generate random test cases
2 parents 416e75e + 074f237 commit def61aa

File tree

5 files changed

+399
-0
lines changed

5 files changed

+399
-0
lines changed

tests/values/Makefile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
main: test_cases.csv
3+
4+
clean:
5+
rm -f random_vals.csv test_cases.csv
6+
7+
# multihash random_vals with all hash functions
8+
test_cases.csv: random_vals.csv
9+
./gen_multihashes.py <$^ >$@
10+
11+
# make some random values
12+
random_vals.csv:
13+
./gen_random.py 20 42 >$@
14+
15+
# make sure a multihash is installed
16+
deps:
17+
@multihash <gen_random.py >/dev/null || (echo "a multihash binary must be installed" && exit 1)

tests/values/gen_multihashes.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/usr/bin/env python
2+
3+
import os
4+
import sys
5+
import subprocess
6+
7+
# modify these to add more test cases
8+
algs = ['sha1', 'sha2-256', 'sha2-512', 'sha3']
9+
sizes = [80, 160, 256, 512]
10+
11+
def die(arg):
12+
print "error:", arg
13+
sys.exit(1)
14+
15+
def require(tool):
16+
if os.system("which " + tool + ">/dev/null") is not 0:
17+
die("requires %s tool" % tool)
18+
19+
def usage(code):
20+
print "usage:", sys.argv[0], "<input_strings >test_cases"
21+
print "output multihash test cases for every string in stdin"
22+
sys.exit(code)
23+
24+
def multihash(val, alg, size):
25+
cmd = "multihash -a %s -l %d -e hex -q" % (alg, size)
26+
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
27+
(out, err) = proc.communicate(val)
28+
29+
# failed to process multihash
30+
if proc.returncode != 0:
31+
raise ValueError("multihash failed")
32+
33+
# incorrect length (truncated shorter than desired length)
34+
shdbe = ((size / 8) + 2) * 2
35+
if shdbe != len(out):
36+
raise ValueError("incorrect multihash length: %s %s" % (len(out), shdbe))
37+
38+
return out
39+
40+
def produce(val, alg, size):
41+
m = multihash(val, alg, size)
42+
return "%s,%s,%s,%s" % (alg, size, val, m)
43+
44+
def gen_testcases(strings):
45+
for l in strings:
46+
l = l.strip()
47+
for a in algs:
48+
for s in sizes:
49+
try:
50+
yield produce(l, a, s)
51+
except ValueError, e:
52+
pass
53+
54+
def main():
55+
if '-h' in sys.argv or '--help' in sys.argv:
56+
usage(0)
57+
if len(sys.argv) != 1:
58+
usage(1)
59+
60+
require("multihash")
61+
print "algorithm,bits,input,multihash"
62+
for t in gen_testcases(sys.stdin):
63+
print t
64+
65+
if __name__ == "__main__":
66+
main()

tests/values/gen_random.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env python
2+
3+
import os
4+
import sys
5+
import random
6+
7+
def usage(code):
8+
print "usage:", sys.argv[0], "<number> <size>"
9+
print "output <number> random hex-encoded strings of <size> bytes"
10+
sys.exit(code)
11+
12+
hexcode = '0123456789abcdef'
13+
def randstr(length):
14+
return ''.join(random.choice(hexcode) for i in range(length))
15+
16+
def gen_strings(num, length):
17+
for i in range(0, num):
18+
yield randstr(length)
19+
20+
def main():
21+
if '-h' in sys.argv or '--help' in sys.argv:
22+
usage(0)
23+
if len(sys.argv) != 3:
24+
usage(1)
25+
26+
num = int(sys.argv[1])
27+
length = int(sys.argv[2])
28+
if not (num > 0 and length > 0):
29+
usage(1)
30+
31+
for s in gen_strings(num, length):
32+
print s
33+
34+
if __name__ == "__main__":
35+
main()

tests/values/random_vals.csv

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
431fb5d4c9b735ba1a34d0df045118806ae2336f2c
2+
2d6db2d7882fa8b7d56e74b8e24036deb475de8c94
3+
6c00022ba29d15926c4580332ded091e666f0ec5d9
4+
1391551dc8f15110d256c493ed485bca8cfed07241
5+
557f47c855a5ca40daa3c0904a1e43647b4021ce0c
6+
4c44254356730838195a32cbb1b8be3bed4c6c05c0
7+
3647da9e56b1afffa9921ce9a9caaa51ac8a166706
8+
f7f96cf1e683e6f7e752dab4613a1b1225889bb15d
9+
8fc7992293335bb2e2f6d962f66734a652e495022e
10+
a244e61f124cd3590173e2809b1177ca611ff15e9a
11+
8a461139f4d5078f6ff77af4fbc1befc775c0e0cb0
12+
eaf89d6d7fa2ee40c7357e3d627cbd1338009570f5
13+
4c98758072933abd55fef35782cf37386db7ce4dce
14+
79f28aba59f256868f8b7f868e75aedcab8b068be0
15+
fe2bd8cb6ad52767eae6fbd354ceb3e72e8f55669b
16+
7f0bb0ac4dd2404c097dade4c8b2292ae2419b7a70
17+
b07cb1832e7e253f3eb7cf4029a82d881217d3c02f
18+
29a53e335621740c0fbc8dd85970fa0311b0d58e25
19+
f6896cafdd752d9615f22017dfce1a281fb0598038
20+
444a8ca03cce5b47991b3baf807f4eaa1d8175a72e

0 commit comments

Comments
 (0)