Package openid :: Package test :: Module test_nonce
[frames] | no frames]

Source Code for Module openid.test.test_nonce

  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   
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)
18
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)
24
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)
32
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)
40
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', 50 ] 51
52 - def __init__(self, nonce_str):
53 datadriven.DataDrivenTestCase.__init__(self, nonce_str) 54 self.nonce_str = nonce_str
55
56 - def runOneTest(self):
57 self.failUnlessRaises(ValueError, splitNonce, self.nonce_str)
58
59 -class CheckTimestampTest(datadriven.DataDrivenTestCase):
60 cases = [ 61 # exact, no allowed skew 62 ('1970-01-01T00:00:00Z', 0, 0, True), 63 64 # exact, large skew 65 ('1970-01-01T00:00:00Z', 1000, 0, True), 66 67 # no allowed skew, one second old 68 ('1970-01-01T00:00:00Z', 0, 1, False), 69 70 # many seconds old, outside of skew 71 ('1970-01-01T00:00:00Z', 10, 50, False), 72 73 # one second old, one second skew allowed 74 ('1970-01-01T00:00:00Z', 1, 1, True), 75 76 # One second in the future, one second skew allowed 77 ('1970-01-01T00:00:02Z', 1, 1, True), 78 79 # two seconds in the future, one second skew allowed 80 ('1970-01-01T00:00:02Z', 1, 0, False), 81 82 # malformed nonce string 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
94 - def runOneTest(self):
95 actual = checkTimestamp(self.nonce_string, self.allowed_skew, self.now) 96 self.failUnlessEqual(bool(self.expected), bool(actual))
97
98 -def pyUnitTests():
99 return datadriven.loadTests(__name__)
100 101 if __name__ == '__main__': 102 suite = pyUnitTests() 103 runner = unittest.TextTestRunner() 104 runner.run(suite) 105