diff options
| author | Simeon Simeonov | 2022-03-29 20:44:03 +0200 |
|---|---|---|
| committer | Simeon Simeonov | 2022-03-29 20:44:03 +0200 |
| commit | f19fde48c6b3f65c164c931286e72a0d254c1fb3 (patch) | |
| tree | 5b6c1bf6cef51c852c49abff3a0e4626e8a33146 /tests/test_generator.py | |
| parent | 12c5e38691b24d12a8fc9605e3b93fac86d461ae (diff) | |
Add the -P param and improve tests
Diffstat (limited to 'tests/test_generator.py')
| -rw-r--r-- | tests/test_generator.py | 152 |
1 files changed, 98 insertions, 54 deletions
diff --git a/tests/test_generator.py b/tests/test_generator.py index 50274e2..aa795a3 100644 --- a/tests/test_generator.py +++ b/tests/test_generator.py | |||
| @@ -1,7 +1,7 @@ | |||
| 1 | # -*- coding: utf-8 -*- | 1 | # -*- coding: utf-8 -*- |
| 2 | # SPDX-License-Identifier: BSD-2-Clause-FreeBSD | 2 | # SPDX-License-Identifier: BSD-2-Clause-FreeBSD |
| 3 | # | 3 | # |
| 4 | # Copyright (c) 2020, Simeon Simeonov | 4 | # Copyright (c) 2020-2022, Simeon Simeonov |
| 5 | # All rights reserved. | 5 | # All rights reserved. |
| 6 | # | 6 | # |
| 7 | # Redistribution and use in source and binary forms, with or without | 7 | # Redistribution and use in source and binary forms, with or without |
| @@ -31,9 +31,11 @@ import otp2289 | |||
| 31 | 31 | ||
| 32 | def test_caller_exceptions(): | 32 | def test_caller_exceptions(): |
| 33 | """Tests the exceptions when calling an initialized object""" | 33 | """Tests the exceptions when calling an initialized object""" |
| 34 | gen = otp2289.OTPGenerator('This is a test.'.encode(), | 34 | gen = otp2289.OTPGenerator( |
| 35 | 'TeSt', | 35 | 'This is a test.'.encode(), |
| 36 | otp2289.OTP_ALGO_MD5) | 36 | 'TeSt', |
| 37 | otp2289.OTP_ALGO_MD5, | ||
| 38 | ) | ||
| 37 | with pytest.raises(otp2289.OTPGeneratorException) as exc_info: | 39 | with pytest.raises(otp2289.OTPGeneratorException) as exc_info: |
| 38 | gen.generate_otp_words('3') | 40 | gen.generate_otp_words('3') |
| 39 | assert exc_info.type is otp2289.OTPGeneratorException | 41 | assert exc_info.type is otp2289.OTPGeneratorException |
| @@ -62,50 +64,72 @@ def test_constructor_exceptions(): | |||
| 62 | """ | 64 | """ |
| 63 | # test the otp2289.OTPGenerator __init__ and validators | 65 | # test the otp2289.OTPGenerator __init__ and validators |
| 64 | with pytest.raises(otp2289.OTPGeneratorException) as exc_info: | 66 | with pytest.raises(otp2289.OTPGeneratorException) as exc_info: |
| 65 | otp2289.OTPGenerator('This is a test.'.encode(), | 67 | otp2289.OTPGenerator( |
| 66 | 'TeStø'.encode(), | 68 | 'This is a test.'.encode(), |
| 67 | otp2289.OTP_ALGO_MD5) | 69 | 'TeStø'.encode(), |
| 70 | otp2289.OTP_ALGO_MD5, | ||
| 71 | ) | ||
| 68 | assert exc_info.type is otp2289.OTPGeneratorException | 72 | assert exc_info.type is otp2289.OTPGeneratorException |
| 69 | assert exc_info.value.args[0] == 'Seed must be a string' | 73 | assert exc_info.value.args[0] == 'Seed must be a string' |
| 70 | with pytest.raises(otp2289.OTPGeneratorException) as exc_info: | 74 | with pytest.raises(otp2289.OTPGeneratorException) as exc_info: |
| 71 | otp2289.OTPGenerator('This is a test.'.encode(), | 75 | otp2289.OTPGenerator( |
| 72 | 'TeStøtEsTteSTteStTest', | 76 | 'This is a test.'.encode(), |
| 73 | otp2289.OTP_ALGO_SHA1) | 77 | 'TeStøtEsTteSTteStTest', |
| 78 | otp2289.OTP_ALGO_SHA1, | ||
| 79 | ) | ||
| 74 | assert exc_info.type is otp2289.OTPGeneratorException | 80 | assert exc_info.type is otp2289.OTPGeneratorException |
| 75 | assert exc_info.value.args[0] == ('The seed MUST be of 1 to 16 ' | 81 | assert exc_info.value.args[0] == ( |
| 76 | 'characters in length') | 82 | 'The seed MUST be of 1 to 16 characters in length' |
| 83 | ) | ||
| 77 | with pytest.raises(otp2289.OTPGeneratorException) as exc_info: | 84 | with pytest.raises(otp2289.OTPGeneratorException) as exc_info: |
| 78 | otp2289.OTPGenerator('This is a test.'.encode(), | 85 | otp2289.OTPGenerator( |
| 79 | 'TeStø', | 86 | 'This is a test.'.encode(), |
| 80 | otp2289.OTP_ALGO_SHA1) | 87 | 'TeStø', |
| 88 | otp2289.OTP_ALGO_SHA1, | ||
| 89 | ) | ||
| 81 | assert exc_info.type is otp2289.OTPGeneratorException | 90 | assert exc_info.type is otp2289.OTPGeneratorException |
| 82 | assert exc_info.value.args[0] == ('The seed MUST consist of purely ' | 91 | assert exc_info.value.args[0] == ( |
| 83 | 'alphanumeric characters') | 92 | 'The seed MUST consist of purely alphanumeric characters' |
| 93 | ) | ||
| 84 | with pytest.raises(otp2289.OTPGeneratorException) as exc_info: | 94 | with pytest.raises(otp2289.OTPGeneratorException) as exc_info: |
| 85 | otp2289.OTPGenerator('This is a test.'.encode(), 'TeSt', 9) | 95 | otp2289.OTPGenerator( |
| 96 | 'This is a test.'.encode(), | ||
| 97 | 'TeSt', | ||
| 98 | 9, | ||
| 99 | ) | ||
| 86 | assert exc_info.type is otp2289.OTPGeneratorException | 100 | assert exc_info.type is otp2289.OTPGeneratorException |
| 87 | assert exc_info.value.args[0] == ( | 101 | assert exc_info.value.args[0] == ( |
| 88 | 'hash_algo is not among the known algorithms') | 102 | 'hash_algo is not among the known algorithms' |
| 103 | ) | ||
| 89 | with pytest.raises(otp2289.OTPGeneratorException) as exc_info: | 104 | with pytest.raises(otp2289.OTPGeneratorException) as exc_info: |
| 90 | otp2289.OTPGenerator('This is a test.'.encode(), 'TeSt', b'md5') | 105 | otp2289.OTPGenerator( |
| 106 | 'This is a test.'.encode(), | ||
| 107 | 'TeSt', | ||
| 108 | b'md5', | ||
| 109 | ) | ||
| 91 | assert exc_info.type is otp2289.OTPGeneratorException | 110 | assert exc_info.type is otp2289.OTPGeneratorException |
| 92 | assert exc_info.value.args[0] == 'hash_algo must be an int or a str' | 111 | assert exc_info.value.args[0] == 'hash_algo must be an int or a str' |
| 93 | # test the package structure as well | 112 | # test the package structure as well |
| 94 | with pytest.raises(otp2289.generator.OTPGeneratorException) as exc_info: | 113 | with pytest.raises(otp2289.generator.OTPGeneratorException) as exc_info: |
| 95 | otp2289.generator.OTPGenerator('This is a test.'.encode(), | 114 | otp2289.generator.OTPGenerator( |
| 96 | 'TeSt', | 115 | 'This is a test.'.encode(), |
| 97 | 'foo') | 116 | 'TeSt', |
| 117 | 'foo', | ||
| 118 | ) | ||
| 98 | assert exc_info.type is otp2289.generator.OTPGeneratorException | 119 | assert exc_info.type is otp2289.generator.OTPGeneratorException |
| 99 | assert exc_info.value.args[0] == ('foo is not supported by this version ' | 120 | assert exc_info.value.args[0] == ( |
| 100 | 'of the hashlib module') | 121 | 'foo is not supported by this version of the hashlib module' |
| 122 | ) | ||
| 101 | with pytest.raises(otp2289.OTPGeneratorException) as exc_info: | 123 | with pytest.raises(otp2289.OTPGeneratorException) as exc_info: |
| 102 | otp2289.OTPGenerator('1234567', 'TeSt', otp2289.OTP_ALGO_MD5) | 124 | otp2289.OTPGenerator('1234567', 'TeSt', otp2289.OTP_ALGO_MD5) |
| 103 | assert exc_info.type is otp2289.OTPGeneratorException | 125 | assert exc_info.type is otp2289.OTPGeneratorException |
| 104 | assert exc_info.value.args[0] == 'Password must be a byte-string' | 126 | assert exc_info.value.args[0] == 'Password must be a byte-string' |
| 105 | with pytest.raises(otp2289.OTPGeneratorException) as exc_info: | 127 | with pytest.raises(otp2289.OTPGeneratorException) as exc_info: |
| 106 | otp2289.OTPGenerator('1234567'.encode(), | 128 | otp2289.OTPGenerator( |
| 107 | 'TeSt', | 129 | '1234567'.encode(), |
| 108 | otp2289.OTP_ALGO_MD5) | 130 | 'TeSt', |
| 131 | otp2289.OTP_ALGO_MD5, | ||
| 132 | ) | ||
| 109 | assert exc_info.type is otp2289.OTPGeneratorException | 133 | assert exc_info.type is otp2289.OTPGeneratorException |
| 110 | assert exc_info.value.args[0] == 'Password must be longer than 10 bytes' | 134 | assert exc_info.value.args[0] == 'Password must be longer than 10 bytes' |
| 111 | 135 | ||
| @@ -118,9 +142,11 @@ def test_md5(): | |||
| 118 | """ | 142 | """ |
| 119 | # We could run this in a loop, but I guess "Readability counts." | 143 | # We could run this in a loop, but I guess "Readability counts." |
| 120 | # pass='This is a test.', seed='TeSt' | 144 | # pass='This is a test.', seed='TeSt' |
| 121 | gen = otp2289.OTPGenerator('This is a test.'.encode(), | 145 | gen = otp2289.OTPGenerator( |
| 122 | 'TeSt', | 146 | 'This is a test.'.encode(), |
| 123 | otp2289.OTP_ALGO_MD5) | 147 | 'TeSt', |
| 148 | otp2289.OTP_ALGO_MD5, | ||
| 149 | ) | ||
| 124 | res_words = gen.generate_otp_words(0) | 150 | res_words = gen.generate_otp_words(0) |
| 125 | res_hex = gen.generate_otp_hexdigest(0) | 151 | res_hex = gen.generate_otp_hexdigest(0) |
| 126 | assert isinstance(res_words, str) | 152 | assert isinstance(res_words, str) |
| @@ -131,16 +157,20 @@ def test_md5(): | |||
| 131 | assert gen.generate_otp_hexdigest(1) == '0x7965e05436f5029f' | 157 | assert gen.generate_otp_hexdigest(1) == '0x7965e05436f5029f' |
| 132 | assert gen.generate_otp_words(1) == 'EASE OIL FUM CURE AWRY AVIS' | 158 | assert gen.generate_otp_words(1) == 'EASE OIL FUM CURE AWRY AVIS' |
| 133 | assert gen.generate_otp_hexdigest_from_challenge('otp-md5 1 TeSt') == ( | 159 | assert gen.generate_otp_hexdigest_from_challenge('otp-md5 1 TeSt') == ( |
| 134 | '0x7965e05436f5029f') | 160 | '0x7965e05436f5029f' |
| 161 | ) | ||
| 135 | assert gen.generate_otp_words_from_challenge('otp-md5 1 TeSt') == ( | 162 | assert gen.generate_otp_words_from_challenge('otp-md5 1 TeSt') == ( |
| 136 | 'EASE OIL FUM CURE AWRY AVIS') | 163 | 'EASE OIL FUM CURE AWRY AVIS' |
| 164 | ) | ||
| 137 | # step 99 | 165 | # step 99 |
| 138 | assert gen.generate_otp_hexdigest(99) == '0x50fe1962c4965880' | 166 | assert gen.generate_otp_hexdigest(99) == '0x50fe1962c4965880' |
| 139 | assert gen.generate_otp_words(99) == 'BAIL TUFT BITS GANG CHEF THY' | 167 | assert gen.generate_otp_words(99) == 'BAIL TUFT BITS GANG CHEF THY' |
| 140 | assert gen.generate_otp_hexdigest_from_challenge('otp-md5 99 TeSt') == ( | 168 | assert gen.generate_otp_hexdigest_from_challenge('otp-md5 99 TeSt') == ( |
| 141 | '0x50fe1962c4965880') | 169 | '0x50fe1962c4965880' |
| 170 | ) | ||
| 142 | assert gen.generate_otp_words_from_challenge('otp-md5 99 TeSt') == ( | 171 | assert gen.generate_otp_words_from_challenge('otp-md5 99 TeSt') == ( |
| 143 | 'BAIL TUFT BITS GANG CHEF THY') | 172 | 'BAIL TUFT BITS GANG CHEF THY' |
| 173 | ) | ||
| 144 | # iterator test | 174 | # iterator test |
| 145 | hexdigests = list(gen.hexdigest_range(105)) # testing the range itself | 175 | hexdigests = list(gen.hexdigest_range(105)) # testing the range itself |
| 146 | words = list(gen.words_range(99)) | 176 | words = list(gen.words_range(99)) |
| @@ -153,9 +183,11 @@ def test_md5(): | |||
| 153 | assert words[1] == 'EASE OIL FUM CURE AWRY AVIS' | 183 | assert words[1] == 'EASE OIL FUM CURE AWRY AVIS' |
| 154 | assert words[99] == 'BAIL TUFT BITS GANG CHEF THY' | 184 | assert words[99] == 'BAIL TUFT BITS GANG CHEF THY' |
| 155 | # pass='AbCdEfGhIjK', seed='alpha1' | 185 | # pass='AbCdEfGhIjK', seed='alpha1' |
| 156 | gen = otp2289.OTPGenerator('AbCdEfGhIjK'.encode(), | 186 | gen = otp2289.OTPGenerator( |
| 157 | 'alpha1', | 187 | 'AbCdEfGhIjK'.encode(), |
| 158 | otp2289.OTP_ALGO_MD5) | 188 | 'alpha1', |
| 189 | otp2289.OTP_ALGO_MD5, | ||
| 190 | ) | ||
| 159 | assert gen.generate_otp_hexdigest(0) == '0x87066dd9644bf206' | 191 | assert gen.generate_otp_hexdigest(0) == '0x87066dd9644bf206' |
| 160 | assert gen.generate_otp_words(0) == 'FULL PEW DOWN ONCE MORT ARC' | 192 | assert gen.generate_otp_words(0) == 'FULL PEW DOWN ONCE MORT ARC' |
| 161 | assert gen.generate_otp_hexdigest(1) == '0x7cd34c1040add14b' | 193 | assert gen.generate_otp_hexdigest(1) == '0x7cd34c1040add14b' |
| @@ -163,9 +195,11 @@ def test_md5(): | |||
| 163 | assert gen.generate_otp_hexdigest(99) == '0x5aa37a81f212146c' | 195 | assert gen.generate_otp_hexdigest(99) == '0x5aa37a81f212146c' |
| 164 | assert gen.generate_otp_words(99) == 'BODE HOP JAKE STOW JUT RAP' | 196 | assert gen.generate_otp_words(99) == 'BODE HOP JAKE STOW JUT RAP' |
| 165 | # pass="OTP's are good", seed='correct' | 197 | # pass="OTP's are good", seed='correct' |
| 166 | gen = otp2289.OTPGenerator("OTP's are good".encode(), | 198 | gen = otp2289.OTPGenerator( |
| 167 | 'correct', | 199 | "OTP's are good".encode(), |
| 168 | otp2289.OTP_ALGO_MD5) | 200 | 'correct', |
| 201 | otp2289.OTP_ALGO_MD5, | ||
| 202 | ) | ||
| 169 | assert gen.generate_otp_hexdigest(0) == '0xf205753943de4cf9' | 203 | assert gen.generate_otp_hexdigest(0) == '0xf205753943de4cf9' |
| 170 | assert gen.generate_otp_words(0) == 'ULAN NEW ARMY FUSE SUIT EYED' | 204 | assert gen.generate_otp_words(0) == 'ULAN NEW ARMY FUSE SUIT EYED' |
| 171 | assert gen.generate_otp_hexdigest(1) == '0xddcdac956f234937' | 205 | assert gen.generate_otp_hexdigest(1) == '0xddcdac956f234937' |
| @@ -181,9 +215,11 @@ def test_sha1(): | |||
| 181 | Those are the tests from 'RFC-2289 Appendix C - OTP Verification Examples' | 215 | Those are the tests from 'RFC-2289 Appendix C - OTP Verification Examples' |
| 182 | """ | 216 | """ |
| 183 | # pass='This is a test.', seed='TeSt' | 217 | # pass='This is a test.', seed='TeSt' |
| 184 | gen = otp2289.OTPGenerator('This is a test.'.encode(), | 218 | gen = otp2289.OTPGenerator( |
| 185 | 'TeSt', | 219 | 'This is a test.'.encode(), |
| 186 | otp2289.OTP_ALGO_SHA1) | 220 | 'TeSt', |
| 221 | otp2289.OTP_ALGO_SHA1, | ||
| 222 | ) | ||
| 187 | # step=0 | 223 | # step=0 |
| 188 | res_hex = gen.generate_otp_hexdigest(0) | 224 | res_hex = gen.generate_otp_hexdigest(0) |
| 189 | res_words = gen.generate_otp_words(0) | 225 | res_words = gen.generate_otp_words(0) |
| @@ -194,15 +230,19 @@ def test_sha1(): | |||
| 194 | assert gen.generate_otp_hexdigest(1) == '0x63d936639734385b' | 230 | assert gen.generate_otp_hexdigest(1) == '0x63d936639734385b' |
| 195 | assert gen.generate_otp_words(1) == 'CART OTTO HIVE ODE VAT NUT' | 231 | assert gen.generate_otp_words(1) == 'CART OTTO HIVE ODE VAT NUT' |
| 196 | assert gen.generate_otp_hexdigest_from_challenge('otp-sha1 1 TeSt') == ( | 232 | assert gen.generate_otp_hexdigest_from_challenge('otp-sha1 1 TeSt') == ( |
| 197 | '0x63d936639734385b') | 233 | '0x63d936639734385b' |
| 234 | ) | ||
| 198 | assert gen.generate_otp_words_from_challenge('otp-sha1 1 TeSt') == ( | 235 | assert gen.generate_otp_words_from_challenge('otp-sha1 1 TeSt') == ( |
| 199 | 'CART OTTO HIVE ODE VAT NUT') | 236 | 'CART OTTO HIVE ODE VAT NUT' |
| 237 | ) | ||
| 200 | assert gen.generate_otp_hexdigest(99) == '0x87fec7768b73ccf9' | 238 | assert gen.generate_otp_hexdigest(99) == '0x87fec7768b73ccf9' |
| 201 | assert gen.generate_otp_words(99) == 'GAFF WAIT SKID GIG SKY EYED' | 239 | assert gen.generate_otp_words(99) == 'GAFF WAIT SKID GIG SKY EYED' |
| 202 | assert gen.generate_otp_hexdigest_from_challenge('otp-sha1 99 TeSt') == ( | 240 | assert gen.generate_otp_hexdigest_from_challenge('otp-sha1 99 TeSt') == ( |
| 203 | '0x87fec7768b73ccf9') | 241 | '0x87fec7768b73ccf9' |
| 242 | ) | ||
| 204 | assert gen.generate_otp_words_from_challenge('otp-sha1 99 TeSt') == ( | 243 | assert gen.generate_otp_words_from_challenge('otp-sha1 99 TeSt') == ( |
| 205 | 'GAFF WAIT SKID GIG SKY EYED') | 244 | 'GAFF WAIT SKID GIG SKY EYED' |
| 245 | ) | ||
| 206 | # iterator test | 246 | # iterator test |
| 207 | hexdigests = list(gen.hexdigest_range(105)) | 247 | hexdigests = list(gen.hexdigest_range(105)) |
| 208 | words = list(gen.words_range(99)) | 248 | words = list(gen.words_range(99)) |
| @@ -215,9 +255,11 @@ def test_sha1(): | |||
| 215 | assert words[1] == 'CART OTTO HIVE ODE VAT NUT' | 255 | assert words[1] == 'CART OTTO HIVE ODE VAT NUT' |
| 216 | assert words[99] == 'GAFF WAIT SKID GIG SKY EYED' | 256 | assert words[99] == 'GAFF WAIT SKID GIG SKY EYED' |
| 217 | # pass='AbCdEfGhIjK', seed='alpha1' | 257 | # pass='AbCdEfGhIjK', seed='alpha1' |
| 218 | gen = otp2289.OTPGenerator('AbCdEfGhIjK'.encode(), | 258 | gen = otp2289.OTPGenerator( |
| 219 | 'alpha1', | 259 | 'AbCdEfGhIjK'.encode(), |
| 220 | otp2289.OTP_ALGO_SHA1) | 260 | 'alpha1', |
| 261 | otp2289.OTP_ALGO_SHA1, | ||
| 262 | ) | ||
| 221 | assert gen.generate_otp_hexdigest(0) == '0xad85f658ebe383c9' | 263 | assert gen.generate_otp_hexdigest(0) == '0xad85f658ebe383c9' |
| 222 | assert gen.generate_otp_words(0) == 'LEST OR HEEL SCOT ROB SUIT' | 264 | assert gen.generate_otp_words(0) == 'LEST OR HEEL SCOT ROB SUIT' |
| 223 | assert gen.generate_otp_hexdigest(1) == '0xd07ce229b5cf119b' | 265 | assert gen.generate_otp_hexdigest(1) == '0xd07ce229b5cf119b' |
| @@ -225,9 +267,11 @@ def test_sha1(): | |||
| 225 | assert gen.generate_otp_hexdigest(99) == '0x27bc71035aaf3dc6' | 267 | assert gen.generate_otp_hexdigest(99) == '0x27bc71035aaf3dc6' |
| 226 | assert gen.generate_otp_words(99) == 'MAY STAR TIN LYON VEDA STAN' | 268 | assert gen.generate_otp_words(99) == 'MAY STAR TIN LYON VEDA STAN' |
| 227 | # pass="OTP's are good", seed='correct' | 269 | # pass="OTP's are good", seed='correct' |
| 228 | gen = otp2289.OTPGenerator("OTP's are good".encode(), | 270 | gen = otp2289.OTPGenerator( |
| 229 | 'correct', | 271 | "OTP's are good".encode(), |
| 230 | otp2289.OTP_ALGO_SHA1) | 272 | 'correct', |
| 273 | otp2289.OTP_ALGO_SHA1, | ||
| 274 | ) | ||
| 231 | assert gen.generate_otp_hexdigest(0) == '0xd51f3e99bf8e6f0b' | 275 | assert gen.generate_otp_hexdigest(0) == '0xd51f3e99bf8e6f0b' |
| 232 | assert gen.generate_otp_words(0) == 'RUST WELT KICK FELL TAIL FRAU' | 276 | assert gen.generate_otp_words(0) == 'RUST WELT KICK FELL TAIL FRAU' |
| 233 | assert gen.generate_otp_hexdigest(1) == '0x82aeb52d943774e4' | 277 | assert gen.generate_otp_hexdigest(1) == '0x82aeb52d943774e4' |
