summaryrefslogtreecommitdiff
path: root/tests/test_generator.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_generator.py')
-rw-r--r--tests/test_generator.py71
1 files changed, 61 insertions, 10 deletions
diff --git a/tests/test_generator.py b/tests/test_generator.py
index 14258da..a4af3ba 100644
--- a/tests/test_generator.py
+++ b/tests/test_generator.py
@@ -5,9 +5,38 @@ import pytest
5import otp2289 5import otp2289
6 6
7 7
8def test_exceptions_and_constraints(): 8def test_caller_exceptions():
9 """Tests general exceptions and constraints""" 9 """Tests the exceptions when calling an initialized object"""
10 # test the otp2289.OTPGenerator __init__ 10 gen = otp2289.OTPGenerator('This is a test.'.encode(),
11 'TeSt',
12 otp2289.OTP_ALGO_MD5)
13 with pytest.raises(otp2289.OTPGeneratorException) as exc_info:
14 gen.generate_otp_words('3')
15 assert exc_info.type is otp2289.OTPGeneratorException
16 assert exc_info.value.args[0] == 'Step value MUST be an int'
17 with pytest.raises(otp2289.OTPGeneratorException) as exc_info:
18 gen.generate_otp_hexdigest(-1)
19 assert exc_info.type is otp2289.OTPGeneratorException
20 assert exc_info.value.args[0] == 'Step value MUST be >= 0'
21 with pytest.raises(otp2289.OTPChallengeException) as exc_info:
22 gen.generate_otp_hexdigest_from_challenge(b'md5 fbd TeSt')
23 assert exc_info.type is otp2289.OTPChallengeException
24 assert exc_info.value.args[0] == 'Challenge must be str'
25 with pytest.raises(otp2289.OTPChallengeException) as exc_info:
26 gen.generate_otp_hexdigest_from_challenge('md5 fbd TeSt')
27 assert exc_info.type is otp2289.OTPChallengeException
28 assert exc_info.value.args[0] == 'Invalid challenge'
29 with pytest.raises(otp2289.generator.OTPChallengeException) as exc_info:
30 gen.generate_otp_hexdigest_from_challenge('otp-md5 fbd TeSt')
31 assert exc_info.type is otp2289.generator.OTPChallengeException
32 assert exc_info.value.args[0] == 'Invalid challenge'
33
34
35def test_constructor_exceptions():
36 """
37 Tests the exceptions when initializing a new object (in the constructor)
38 """
39 # test the otp2289.OTPGenerator __init__ and validators
11 with pytest.raises(otp2289.OTPGeneratorException) as exc_info: 40 with pytest.raises(otp2289.OTPGeneratorException) as exc_info:
12 otp2289.OTPGenerator('1234567', 'TeStø'.encode(), otp2289.OTP_ALGO_MD5) 41 otp2289.OTPGenerator('1234567', 'TeStø'.encode(), otp2289.OTP_ALGO_MD5)
13 assert exc_info.type is otp2289.OTPGeneratorException 42 assert exc_info.type is otp2289.OTPGeneratorException
@@ -38,17 +67,23 @@ def test_exceptions_and_constraints():
38 assert exc_info.type is otp2289.OTPGeneratorException 67 assert exc_info.type is otp2289.OTPGeneratorException
39 assert exc_info.value.args[0] == ('The seed MUST consist of purely ' 68 assert exc_info.value.args[0] == ('The seed MUST consist of purely '
40 'alphanumeric characters') 69 'alphanumeric characters')
41 gen = otp2289.OTPGenerator('This is a test.'.encode(),
42 'TeSt',
43 otp2289.OTP_ALGO_MD5)
44 with pytest.raises(otp2289.OTPGeneratorException) as exc_info: 70 with pytest.raises(otp2289.OTPGeneratorException) as exc_info:
45 gen.generate_otp_words('3') 71 otp2289.OTPGenerator('This is a test.'.encode(), 'TeSt', 9)
46 assert exc_info.type is otp2289.OTPGeneratorException 72 assert exc_info.type is otp2289.OTPGeneratorException
47 assert exc_info.value.args[0] == 'Step value MUST be an int' 73 assert exc_info.value.args[0] == (
74 'hash_algo is not among the known algorithms')
48 with pytest.raises(otp2289.OTPGeneratorException) as exc_info: 75 with pytest.raises(otp2289.OTPGeneratorException) as exc_info:
49 gen.generate_otp_hexdigest(-1) 76 otp2289.OTPGenerator('This is a test.'.encode(), 'TeSt', b'md5')
50 assert exc_info.type is otp2289.OTPGeneratorException 77 assert exc_info.type is otp2289.OTPGeneratorException
51 assert exc_info.value.args[0] == 'Step value MUST be >= 0' 78 assert exc_info.value.args[0] == 'hash_algo must be an int or a str'
79 # test the package structure as well
80 with pytest.raises(otp2289.generator.OTPGeneratorException) as exc_info:
81 otp2289.generator.OTPGenerator('This is a test.'.encode(),
82 'TeSt',
83 'foo')
84 assert exc_info.type is otp2289.generator.OTPGeneratorException
85 assert exc_info.value.args[0] == ('foo is not supported by this version '
86 'of the hashlib module')
52 87
53 88
54def test_md5(): 89def test_md5():
@@ -71,9 +106,17 @@ def test_md5():
71 # step 1 106 # step 1
72 assert gen.generate_otp_hexdigest(1) == '0x7965e05436f5029f' 107 assert gen.generate_otp_hexdigest(1) == '0x7965e05436f5029f'
73 assert gen.generate_otp_words(1) == 'EASE OIL FUM CURE AWRY AVIS' 108 assert gen.generate_otp_words(1) == 'EASE OIL FUM CURE AWRY AVIS'
109 assert gen.generate_otp_hexdigest_from_challenge('otp-md5 1 TeSt') == (
110 '0x7965e05436f5029f')
111 assert gen.generate_otp_words_from_challenge('otp-md5 1 TeSt') == (
112 'EASE OIL FUM CURE AWRY AVIS')
74 # step 99 113 # step 99
75 assert gen.generate_otp_hexdigest(99) == '0x50fe1962c4965880' 114 assert gen.generate_otp_hexdigest(99) == '0x50fe1962c4965880'
76 assert gen.generate_otp_words(99) == 'BAIL TUFT BITS GANG CHEF THY' 115 assert gen.generate_otp_words(99) == 'BAIL TUFT BITS GANG CHEF THY'
116 assert gen.generate_otp_hexdigest_from_challenge('otp-md5 99 TeSt') == (
117 '0x50fe1962c4965880')
118 assert gen.generate_otp_words_from_challenge('otp-md5 99 TeSt') == (
119 'BAIL TUFT BITS GANG CHEF THY')
77 # iterator test 120 # iterator test
78 hexdigests = list(gen.hexdigest_range(105)) # testing the range itself 121 hexdigests = list(gen.hexdigest_range(105)) # testing the range itself
79 words = list(gen.words_range(99)) 122 words = list(gen.words_range(99))
@@ -126,8 +169,16 @@ def test_sha1():
126 assert res_words == 'MILT VARY MAST OK SEES WENT' 169 assert res_words == 'MILT VARY MAST OK SEES WENT'
127 assert gen.generate_otp_hexdigest(1) == '0x63d936639734385b' 170 assert gen.generate_otp_hexdigest(1) == '0x63d936639734385b'
128 assert gen.generate_otp_words(1) == 'CART OTTO HIVE ODE VAT NUT' 171 assert gen.generate_otp_words(1) == 'CART OTTO HIVE ODE VAT NUT'
172 assert gen.generate_otp_hexdigest_from_challenge('otp-sha1 1 TeSt') == (
173 '0x63d936639734385b')
174 assert gen.generate_otp_words_from_challenge('otp-sha1 1 TeSt') == (
175 'CART OTTO HIVE ODE VAT NUT')
129 assert gen.generate_otp_hexdigest(99) == '0x87fec7768b73ccf9' 176 assert gen.generate_otp_hexdigest(99) == '0x87fec7768b73ccf9'
130 assert gen.generate_otp_words(99) == 'GAFF WAIT SKID GIG SKY EYED' 177 assert gen.generate_otp_words(99) == 'GAFF WAIT SKID GIG SKY EYED'
178 assert gen.generate_otp_hexdigest_from_challenge('otp-sha1 99 TeSt') == (
179 '0x87fec7768b73ccf9')
180 assert gen.generate_otp_words_from_challenge('otp-sha1 99 TeSt') == (
181 'GAFF WAIT SKID GIG SKY EYED')
131 # iterator test 182 # iterator test
132 hexdigests = list(gen.hexdigest_range(105)) 183 hexdigests = list(gen.hexdigest_range(105))
133 words = list(gen.words_range(99)) 184 words = list(gen.words_range(99))