webfaction and repo.or.cz deployment done
[worddb.git] / libs / openid / test / dh.py
blob16b8d560eef2a82a0f5a783c469eb75505adccca
1 import os.path
2 from openid.dh import DiffieHellman, strxor
4 def test_strxor():
5 NUL = '\x00'
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'),
19 for aa, bb, expected in cases:
20 actual = strxor(aa, bb)
21 assert actual == expected, (aa, bb, expected, actual)
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)))),
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,)
39 def test1():
40 dh1 = DiffieHellman.fromDefaults()
41 dh2 = DiffieHellman.fromDefaults()
42 secret1 = dh1.getSharedSecret(dh2.public)
43 secret2 = dh2.getSharedSecret(dh1.public)
44 assert secret1 == secret2
45 return secret1
47 def test_exchange():
48 s1 = test1()
49 s2 = test1()
50 assert s1 != s2
52 def test_public():
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]))
60 assert dh.public == long(parts[1])
61 finally:
62 f.close()
64 def test():
65 test_exchange()
66 test_public()
67 test_strxor()
69 if __name__ == '__main__':
70 test()