-
Notifications
You must be signed in to change notification settings - Fork 4
/
gentests.py
43 lines (36 loc) · 1.02 KB
/
gentests.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
import sys
sys.path.append('./fastpbkdf2')
import testdata
hashes = dict(
sha1 = 'pbkdf2_hmac_sha1',
sha256 = 'pbkdf2_hmac_sha256',
sha512 = 'pbkdf2_hmac_sha512'
)
def bb(b):
if b.isalnum():
return 'b"%s"' % b
else:
return 'b"%s"' % ''.join('\\x%02x' % ord(c) for c in b)
print """// generated by gentests.py; do not edit
extern crate fastpbkdf2;
use fastpbkdf2::{pbkdf2_hmac_sha1, pbkdf2_hmac_sha256, pbkdf2_hmac_sha512};
#[cfg(test)]
fn check(a: &[u8], b: &[u8]) {
assert_eq!(a.len(), b.len());
for i in 0..a.len() {
assert_eq!(a[i], b[i]);
}
}
"""
for hash, tests in sorted(testdata.tests.items()):
print '#[test]'
print 'fn test_%s() {' % hash
fn = hashes[hash]
for t in tests:
print ' {'
print ' let mut out = [0u8; %d];' % len(t['output'])
print ' %s(%s, %s, %d, &mut out);' % (fn, bb(t['password']), bb(t['salt']), t['iterations'])
print ' check(&out, %s);' % bb(t['output'])
print ' }'
print '}'
print