1 from openid.test import datadriven
2 import time
3 import unittest
4 import re
5
6 from openid.store.nonce import \
7 mkNonce, \
8 split as splitNonce, \
9 checkTimestamp
10
11 nonce_re = re.compile(r'\A\d{4}-\d\d-\d\dT\d\d:\d\d:\d\dZ')
12
18
24
26 s = '1970-01-01T00:00:00Z'
27 expected_t = 0
28 expected_salt = ''
29 actual_t, actual_salt = splitNonce(s)
30 self.failUnlessEqual(expected_t, actual_t)
31 self.failUnlessEqual(expected_salt, actual_salt)
32
34 t = 42
35 nonce_str = mkNonce(t)
36 self.failUnless(nonce_re.match(nonce_str))
37 et, salt = splitNonce(nonce_str)
38 self.failUnlessEqual(len(salt), 6)
39 self.failUnlessEqual(et, t)
40
42 cases = [
43 '',
44 '1970-01-01T00:00:00+1:00',
45 '1969-01-01T00:00:00Z',
46 '1970-00-01T00:00:00Z',
47 '1970.01-01T00:00:00Z',
48 'Thu Sep 7 13:29:31 PDT 2006',
49 'monkeys',
50 ]
51
55
57 self.failUnlessRaises(ValueError, splitNonce, self.nonce_str)
58
60 cases = [
61
62 ('1970-01-01T00:00:00Z', 0, 0, True),
63
64
65 ('1970-01-01T00:00:00Z', 1000, 0, True),
66
67
68 ('1970-01-01T00:00:00Z', 0, 1, False),
69
70
71 ('1970-01-01T00:00:00Z', 10, 50, False),
72
73
74 ('1970-01-01T00:00:00Z', 1, 1, True),
75
76
77 ('1970-01-01T00:00:02Z', 1, 1, True),
78
79
80 ('1970-01-01T00:00:02Z', 1, 0, False),
81
82
83 ('monkeys', 0, 0, False),
84 ]
85
86 - def __init__(self, nonce_string, allowed_skew, now, expected):
87 datadriven.DataDrivenTestCase.__init__(
88 self, repr((nonce_string, allowed_skew, now)))
89 self.nonce_string = nonce_string
90 self.allowed_skew = allowed_skew
91 self.now = now
92 self.expected = expected
93
95 actual = checkTimestamp(self.nonce_string, self.allowed_skew, self.now)
96 self.failUnlessEqual(bool(self.expected), bool(actual))
97
100
101 if __name__ == '__main__':
102 suite = pyUnitTests()
103 runner = unittest.TextTestRunner()
104 runner.run(suite)
105