1 import os.path
2 from openid.dh import DiffieHellman, strxor
3
5 NUL = '\x00'
6
7 cases = [
8 (NUL, NUL, NUL),
9 ('\x01', NUL, '\x01'),
10 ('a', 'a', NUL),
11 ('a', NUL, 'a'),
12 ('abc', NUL * 3, 'abc'),
13 ('x' * 10, NUL * 10, 'x' * 10),
14 ('\x01', '\x02', '\x03'),
15 ('\xf0', '\x0f', '\xff'),
16 ('\xff', '\x0f', '\xf0'),
17 ]
18
19 for aa, bb, expected in cases:
20 actual = strxor(aa, bb)
21 assert actual == expected, (aa, bb, expected, actual)
22
23 exc_cases = [
24 ('', 'a'),
25 ('foo', 'ba'),
26 (NUL * 3, NUL * 4),
27 (''.join(map(chr, xrange(256))),
28 ''.join(map(chr, xrange(128)))),
29 ]
30
31 for aa, bb in exc_cases:
32 try:
33 unexpected = strxor(aa, bb)
34 except ValueError:
35 pass
36 else:
37 assert False, 'Expected ValueError, got %r' % (unexpected,)
38
46
48 s1 = test1()
49 s2 = test1()
50 assert s1 != s2
51
53 f = file(os.path.join(os.path.dirname(__file__), 'dhpriv'))
54 dh = DiffieHellman.fromDefaults()
55 try:
56 for line in f:
57 parts = line.strip().split(' ')
58 dh._setPrivate(long(parts[0]))
59
60 assert dh.public == long(parts[1])
61 finally:
62 f.close()
63
68
69 if __name__ == '__main__':
70 test()
71