-
Notifications
You must be signed in to change notification settings - Fork 18
/
generate_tests.py
69 lines (56 loc) · 2.12 KB
/
generate_tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python
# This will generate a test suite.
def generate():
# SHA-3
FILES = [
('test/data/ShortMsgKAT_SHA3-224.txt', 'sha3.SHA3224'),
('test/data/ShortMsgKAT_SHA3-256.txt', 'sha3.SHA3256'),
('test/data/ShortMsgKAT_SHA3-384.txt', 'sha3.SHA3384'),
('test/data/ShortMsgKAT_SHA3-512.txt', 'sha3.SHA3512'),
]
print """
# This file generated by generate_tests.py
import sha3
import unittest
import binascii
class SHA3Tests(unittest.TestCase):
"""
for path, instance_str in FILES:
contents = file(path).read().split('Len = ')
for test in contents:
lines = test.split('\n')
if lines and len(lines) and not lines[0].startswith('#'):
length = int(lines[0])
if length % 8 == 0 and length != 0:
msg = lines[1].split(' = ')[-1].lower()
md = lines[2].split(' = ')[-1].lower()
print """ def test_%s_%s(self):
inst = %s()
inst.update(binascii.unhexlify(b%r))
assert inst.hexdigest() == b%r
""" % (path.split('/')[-1].split('.')[0].replace('-', '_'), length, instance_str, msg, md)
# SHAKE
FILES = [
('test/data/ShortMsgKAT_SHAKE128.txt', 'sha3.SHAKE128'),
('test/data/ShortMsgKAT_SHAKE256.txt', 'sha3.SHAKE256'),
]
for path, instance_str in FILES:
contents = file(path).read().split('Len = ')
for test in contents:
lines = test.split('\n')
if lines and len(lines) and not lines[0].startswith('#'):
length = int(lines[0])
if length % 8 == 0 and length != 0:
msg = lines[1].split(' = ')[-1].lower()
md = lines[2].split(' = ')[-1].lower()
print """ def test_%s_%s(self):
inst = %s(512*8)
inst.update(binascii.unhexlify(b%r))
assert inst.hexdigest() == b%r
""" % (path.split('/')[-1].split('.')[0].replace('-', '_'), length, instance_str, msg, md)
print """
if __name__ == '__main__':
unittest.main()
"""
if __name__ == '__main__':
generate()