getting file size for all dict files to be downloaded. coming to be 400mb or so.
[worddb.git] / libs / openid / test / test_nonce.py
blob2138305c4feae9086159a59b84b9ac060df09020
1 from openid.test import datadriven
2 import time
3 import unittest
4 import re
6 from openid.store.nonce import \
7 mkNonce, \
8 split as splitNonce, \
9 checkTimestamp
11 nonce_re = re.compile(r'\A\d{4}-\d\d-\d\dT\d\d:\d\d:\d\dZ')
13 class NonceTest(unittest.TestCase):
14 def test_mkNonce(self):
15 nonce = mkNonce()
16 self.failUnless(nonce_re.match(nonce))
17 self.failUnless(len(nonce) == 26)
19 def test_mkNonce_when(self):
20 nonce = mkNonce(0)
21 self.failUnless(nonce_re.match(nonce))
22 self.failUnless(nonce.startswith('1970-01-01T00:00:00Z'))
23 self.failUnless(len(nonce) == 26)
25 def test_splitNonce(self):
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)
33 def test_mkSplit(self):
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)
41 class BadSplitTest(datadriven.DataDrivenTestCase):
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',
52 def __init__(self, nonce_str):
53 datadriven.DataDrivenTestCase.__init__(self, nonce_str)
54 self.nonce_str = nonce_str
56 def runOneTest(self):
57 self.failUnlessRaises(ValueError, splitNonce, self.nonce_str)
59 class CheckTimestampTest(datadriven.DataDrivenTestCase):
60 cases = [
61 # exact, no allowed skew
62 ('1970-01-01T00:00:00Z', 0, 0, True),
64 # exact, large skew
65 ('1970-01-01T00:00:00Z', 1000, 0, True),
67 # no allowed skew, one second old
68 ('1970-01-01T00:00:00Z', 0, 1, False),
70 # many seconds old, outside of skew
71 ('1970-01-01T00:00:00Z', 10, 50, False),
73 # one second old, one second skew allowed
74 ('1970-01-01T00:00:00Z', 1, 1, True),
76 # One second in the future, one second skew allowed
77 ('1970-01-01T00:00:02Z', 1, 1, True),
79 # two seconds in the future, one second skew allowed
80 ('1970-01-01T00:00:02Z', 1, 0, False),
82 # malformed nonce string
83 ('monkeys', 0, 0, False),
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
94 def runOneTest(self):
95 actual = checkTimestamp(self.nonce_string, self.allowed_skew, self.now)
96 self.failUnlessEqual(bool(self.expected), bool(actual))
98 def pyUnitTests():
99 return datadriven.loadTests(__name__)
101 if __name__ == '__main__':
102 suite = pyUnitTests()
103 runner = unittest.TextTestRunner()
104 runner.run(suite)