1 from openid
.test
import datadriven
6 from openid
.store
.nonce
import \
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
):
16 self
.failUnless(nonce_re
.match(nonce
))
17 self
.failUnless(len(nonce
) == 26)
19 def test_mkNonce_when(self
):
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'
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
):
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
):
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',
52 def __init__(self
, nonce_str
):
53 datadriven
.DataDrivenTestCase
.__init
__(self
, nonce_str
)
54 self
.nonce_str
= nonce_str
57 self
.failUnlessRaises(ValueError, splitNonce
, self
.nonce_str
)
59 class CheckTimestampTest(datadriven
.DataDrivenTestCase
):
61 # exact, no allowed skew
62 ('1970-01-01T00:00:00Z', 0, 0, True),
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
92 self
.expected
= expected
95 actual
= checkTimestamp(self
.nonce_string
, self
.allowed_skew
, self
.now
)
96 self
.failUnlessEqual(bool(self
.expected
), bool(actual
))
99 return datadriven
.loadTests(__name__
)
101 if __name__
== '__main__':
102 suite
= pyUnitTests()
103 runner
= unittest
.TextTestRunner()