forked from pyauth/pyotp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
124 lines (98 loc) · 4.66 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
from __future__ import with_statement
import datetime
import os
import sys
import unittest
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))
import pyotp
class HOTPExampleValuesFromTheRFC(unittest.TestCase):
def testMatchTheRFC(self):
# 12345678901234567890 in Bas32
# GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ
hotp = pyotp.HOTP('GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ')
self.assertEqual(hotp.at(0), 755224)
self.assertEqual(hotp.at(1), 287082)
self.assertEqual(hotp.at(2), 359152)
self.assertEqual(hotp.at(3), 969429)
self.assertEqual(hotp.at(4), 338314)
self.assertEqual(hotp.at(5), 254676)
self.assertEqual(hotp.at(6), 287922)
self.assertEqual(hotp.at(7), 162583)
self.assertEqual(hotp.at(8), 399871)
self.assertEqual(hotp.at(9), 520489)
def testVerifyAnOTPAndNowAllowReuse(self):
hotp = pyotp.HOTP('GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ')
self.assertTrue(hotp.verify(520489, 9))
self.assertFalse(hotp.verify(520489, 10))
self.assertFalse(hotp.verify("520489", 10))
def testProvisioningURI(self):
hotp = pyotp.HOTP('wrn3pqx5uqxqvnqr')
self.assertEqual(
hotp.provisioning_uri('mark@percival'),
'otpauth://hotp/mark@percival?secret=wrn3pqx5uqxqvnqr&counter=0')
self.assertEqual(
hotp.provisioning_uri('mark@percival', initial_count=12),
'otpauth://hotp/mark@percival?secret=wrn3pqx5uqxqvnqr&counter=12')
self.assertEqual(
hotp.provisioning_uri('mark@percival', issuer_name='FooCorp!'),
'otpauth://hotp/FooCorp%21:mark@percival?secret=wrn3pqx5uqxqvnqr&counter=0&issuer=FooCorp%21')
class TOTPExampleValuesFromTheRFC(unittest.TestCase):
def testMatchTheRFC(self):
totp = pyotp.TOTP('GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ')
self.assertEqual(totp.at(1111111111), 50471)
self.assertEqual(totp.at(1234567890), 5924)
self.assertEqual(totp.at(2000000000), 279037)
def testMatchTheGoogleAuthenticatorOutput(self):
totp = pyotp.TOTP('wrn3pqx5uqxqvnqr')
with Timecop(1297553958):
self.assertEqual(totp.now(), 102705)
def testValidateATimeBasedOTP(self):
totp = pyotp.TOTP('wrn3pqx5uqxqvnqr')
with Timecop(1297553958):
self.assertTrue(totp.verify(102705))
self.assertTrue(totp.verify("102705"))
with Timecop(1297553958 + 30):
self.assertFalse(totp.verify(102705))
def testProvisioningURI(self):
totp = pyotp.TOTP('wrn3pqx5uqxqvnqr')
self.assertEqual(
totp.provisioning_uri('mark@percival'),
'otpauth://totp/mark@percival?secret=wrn3pqx5uqxqvnqr')
self.assertEqual(
totp.provisioning_uri('mark@percival', issuer_name='FooCorp!'),
'otpauth://totp/FooCorp%21:mark@percival?secret=wrn3pqx5uqxqvnqr&issuer=FooCorp%21')
class StringComparisonTest(unittest.TestCase):
def testComparisons(self):
self.assertTrue(pyotp.utils.strings_equal("", ""))
self.assertTrue(pyotp.utils.strings_equal(u"", u""))
self.assertTrue(pyotp.utils.strings_equal("a", "a"))
self.assertTrue(pyotp.utils.strings_equal(u"a", u"a"))
self.assertTrue(pyotp.utils.strings_equal(u"a", u"a"))
self.assertTrue(pyotp.utils.strings_equal("a" * 1000, "a" * 1000))
self.assertTrue(pyotp.utils.strings_equal(u"a" * 1000, u"a" * 1000))
self.assertFalse(pyotp.utils.strings_equal("", "a"))
self.assertFalse(pyotp.utils.strings_equal(u"", u"a"))
self.assertFalse(pyotp.utils.strings_equal("a", ""))
self.assertFalse(pyotp.utils.strings_equal(u"a", u""))
self.assertFalse(pyotp.utils.strings_equal("a" * 999 + "b", "a" * 1000))
self.assertFalse(pyotp.utils.strings_equal(u"a" * 999 + u"b", u"a" * 1000))
class Timecop(object):
"""
Half-assed clone of timecop.rb, just enough to pass our tests.
"""
def __init__(self, freeze_timestamp):
self.freeze_timestamp = freeze_timestamp
def __enter__(self):
self.real_datetime = datetime.datetime
datetime.datetime = self.frozen_datetime()
def __exit__(self, type, value, traceback):
datetime.datetime = self.real_datetime
def frozen_datetime(self):
class FrozenDateTime(datetime.datetime):
@classmethod
def now(cls):
return cls.fromtimestamp(timecop.freeze_timestamp)
timecop = self
return FrozenDateTime
if __name__ == '__main__':
unittest.main()