summaryrefslogtreecommitdiff
path: root/tests/test_generator.py
blob: 68d3bfe8279c44e12305a35ec7e87966823c2a15 (plain)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# SPDX-License-Identifier: BSD-2-Clause
#
# Copyright (c) 2020-2026, Simeon Simeonov
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""Tests for otp2289.generator"""

import pytest

import otp2289


def test_caller_exceptions() -> None:
    """Tests the exceptions when calling an initialized object"""
    gen = otp2289.OTPGenerator(
        b'This is a test.', 'TeSt', otp2289.OTP_ALGO_MD5
    )
    with pytest.raises(otp2289.OTPGeneratorError) as exc_info:
        gen.generate_otp_response('3')  # ty: ignore[invalid-argument-type]
    assert exc_info.type is otp2289.OTPGeneratorError
    assert exc_info.value.args[0] == 'Step value MUST be an int'
    with pytest.raises(otp2289.OTPGeneratorError) as exc_info:
        gen.generate_otp_response(-1)
    assert exc_info.type is otp2289.OTPGeneratorError
    assert exc_info.value.args[0] == 'Step value MUST be >= 0'
    with pytest.raises(otp2289.OTPChallengeError) as exc_info:
        gen.generate_otp_response_from_challenge(
            b'md5 fbd TeSt'  # ty: ignore[invalid-argument-type]
        )
    assert exc_info.type is otp2289.OTPChallengeError
    assert exc_info.value.args[0] == 'Challenge must be str'
    with pytest.raises(otp2289.OTPChallengeError) as exc_info:
        gen.generate_otp_response_from_challenge('md5 fbd TeSt')
    assert exc_info.type is otp2289.OTPChallengeError
    assert exc_info.value.args[0] == 'Invalid challenge'
    with pytest.raises(otp2289.OTPChallengeError) as exc_info:
        gen.generate_otp_response_from_challenge('otp-md5 fbd TeSt')
    assert exc_info.type is otp2289.OTPChallengeError
    assert exc_info.value.args[0] == 'Invalid challenge'


def test_constructor_exceptions() -> None:
    """
    Tests the exceptions when initializing a new object (in the constructor)
    """
    # test the otp2289.OTPGenerator __init__ and validators
    with pytest.raises(otp2289.OTPGeneratorError) as exc_info:
        otp2289.OTPGenerator(
            b'This is a test.',
            'TeStø'.encode(),  # ty: ignore[invalid-argument-type]
            otp2289.OTP_ALGO_MD5,
        )
    assert exc_info.type is otp2289.OTPGeneratorError
    assert exc_info.value.args[0] == 'Seed must be a string'
    with pytest.raises(otp2289.OTPGeneratorError) as exc_info:
        otp2289.OTPGenerator(
            b'This is a test.', 'TeStøtEsTteSTteStTest', otp2289.OTP_ALGO_SHA1
        )
    assert exc_info.type is otp2289.OTPGeneratorError
    assert exc_info.value.args[0] == (
        'The seed MUST be of 1 to 16 characters in length'
    )
    with pytest.raises(otp2289.OTPGeneratorError) as exc_info:
        otp2289.OTPGenerator(
            b'This is a test.', 'TeStø', otp2289.OTP_ALGO_SHA1
        )
    assert exc_info.type is otp2289.OTPGeneratorError
    assert exc_info.value.args[0] == (
        'The seed MUST consist of purely alphanumeric characters'
    )
    with pytest.raises(otp2289.OTPGeneratorError) as exc_info:
        otp2289.OTPGenerator(b'This is a test.', 'TeSt', 9)
    assert exc_info.type is otp2289.OTPGeneratorError
    assert exc_info.value.args[0] == (
        'hash_algo is not among the known algorithms'
    )
    with pytest.raises(otp2289.OTPGeneratorError) as exc_info:
        otp2289.OTPGenerator(
            b'This is a test.',
            'TeSt',
            b'md5',  # ty: ignore[invalid-argument-type]
        )
    assert exc_info.type is otp2289.OTPGeneratorError
    assert exc_info.value.args[0] == 'hash_algo must be an int or a str'
    # test the package structure as well
    with pytest.raises(otp2289.OTPGeneratorError) as exc_info:
        otp2289.OTPGenerator(b'This is a test.', 'TeSt', 'foo')
    assert exc_info.type is otp2289.OTPGeneratorError
    assert exc_info.value.args[0] == (
        'foo is not supported by this version of the hashlib module'
    )
    with pytest.raises(otp2289.OTPGeneratorError) as exc_info:
        otp2289.OTPGenerator(
            '1234567',  # ty: ignore[invalid-argument-type]
            'TeSt',
            otp2289.OTP_ALGO_MD5,
        )
    assert exc_info.type is otp2289.OTPGeneratorError
    assert exc_info.value.args[0] == 'Password must be a byte-string'
    with pytest.raises(otp2289.OTPGeneratorError) as exc_info:
        otp2289.OTPGenerator(b'1234567', 'TeSt', otp2289.OTP_ALGO_MD5)
    assert exc_info.type is otp2289.OTPGeneratorError
    assert exc_info.value.args[0] == 'Password must be longer than 10 bytes'


def test_md5() -> None:
    """
    Tests the MD5 functionality of the OTPGenerator as described in the RFC

    Those are the tests from 'RFC-2289 Appendix C - OTP Verification Examples'
    """
    # We could run this in a loop, but I guess "Readability counts."
    # pass='This is a test.', seed='TeSt'
    gen = otp2289.OTPGenerator(
        b'This is a test.', 'TeSt', otp2289.OTP_ALGO_MD5
    )
    response = gen.generate_otp_response(0)
    assert isinstance(response, otp2289.OTPResponse)
    assert isinstance(response.words, str)
    assert isinstance(response.hexdigest, str)
    assert response.hexdigest == '0x9e876134d90499dd'
    assert response.words == 'INCH SEA ANNE LONG AHEM TOUR'

    # step 1
    response = gen.generate_otp_response(1)
    assert response.hexdigest == '0x7965e05436f5029f'
    assert response.words == 'EASE OIL FUM CURE AWRY AVIS'
    assert (
        gen.generate_otp_response_from_challenge('otp-md5 1   TeSt').hexdigest
        == '0x7965e05436f5029f'
    )
    assert (
        gen.generate_otp_response_from_challenge('otp-md5 1 TeSt').words
        == 'EASE OIL FUM CURE AWRY AVIS'
    )

    # step 99
    response = gen.generate_otp_response(99)
    assert response.hexdigest == '0x50fe1962c4965880'
    assert response.words == 'BAIL TUFT BITS GANG CHEF THY'
    assert (
        gen.generate_otp_response_from_challenge('otp-md5 99   TeSt').hexdigest
        == '0x50fe1962c4965880'
    )
    assert (
        gen.generate_otp_response_from_challenge('otp-md5 99   TeSt').words
        == 'BAIL TUFT BITS GANG CHEF THY'
    )

    # iterator test
    hexdigests = list(gen.otp_response_range(105))  # testing the range itself
    words = list(gen.otp_response_range(99))
    hexdigests.reverse()
    words.reverse()
    assert hexdigests[0].hexdigest == '0x9e876134d90499dd'
    assert hexdigests[1].hexdigest == '0x7965e05436f5029f'
    assert hexdigests[99].hexdigest == '0x50fe1962c4965880'
    assert words[0].words == 'INCH SEA ANNE LONG AHEM TOUR'
    assert words[1].words == 'EASE OIL FUM CURE AWRY AVIS'
    assert words[99].words == 'BAIL TUFT BITS GANG CHEF THY'

    # pass='AbCdEfGhIjK', seed='alpha1'
    gen = otp2289.OTPGenerator(b'AbCdEfGhIjK', 'alpha1', otp2289.OTP_ALGO_MD5)
    response = gen.generate_otp_response(0)
    assert response.hexdigest == '0x87066dd9644bf206'
    assert response.words == 'FULL PEW DOWN ONCE MORT ARC'
    response = gen.generate_otp_response(1)
    assert response.hexdigest == '0x7cd34c1040add14b'
    assert response.words == 'FACT HOOF AT FIST SITE KENT'
    response = gen.generate_otp_response(99)
    assert response.hexdigest == '0x5aa37a81f212146c'
    assert response.words == 'BODE HOP JAKE STOW JUT RAP'

    # pass="OTP's are good", seed='correct'
    gen = otp2289.OTPGenerator(
        b"OTP's are good", 'correct', otp2289.OTP_ALGO_MD5
    )
    response = gen.generate_otp_response(0)
    assert response.hexdigest == '0xf205753943de4cf9'
    assert response.words == 'ULAN NEW ARMY FUSE SUIT EYED'
    response = gen.generate_otp_response(1)
    assert response.hexdigest == '0xddcdac956f234937'
    assert response.words == 'SKIM CULT LOB SLAM POE HOWL'
    response = gen.generate_otp_response(99)
    assert response.hexdigest == '0xb203e28fa525be47'
    assert response.words == 'LONG IVY JULY AJAR BOND LEE'


def test_otp_response() -> None:
    """Tests OTPResponse"""
    gen = otp2289.OTPGenerator(
        b'This is a test.', 'TeSt', otp2289.OTP_ALGO_MD5
    )
    response = gen.generate_otp_response(0)
    assert response.response_bytes == bytes(response)

    # From 'RFC-2289 Appendix C - OTP Verification Examples'
    # hexdigest: 9e876134d90499dd
    # words: INCH SEA ANNE LONG AHEM TOUR
    response_from_tokens = otp2289.OTPResponse.from_tokens(
        'INCH SEA ANNE LONG AHEM TOUR'
    )
    response_from_hex1 = otp2289.OTPResponse.from_hex('9e876134d90499dd')
    response_from_hex2 = otp2289.OTPResponse.from_hex('0x9e876134d90499dd')
    assert (
        response_from_tokens.response_bytes
        == response_from_hex1.response_bytes
    )
    assert (
        response_from_tokens.response_bytes
        == response_from_hex2.response_bytes
    )

    # same tests, only using __eq__
    assert response_from_tokens == response_from_hex1
    assert response_from_tokens == response_from_hex2


def test_otp_response_exceptions() -> None:
    """Tests OTPResponse exceptions"""
    with pytest.raises(otp2289.OTPResponseError) as exc_info:
        otp2289.OTPResponse.from_hex(
            b'9e876134d90499dd'  # ty: ignore[invalid-argument-type]
        )
    assert exc_info.type is otp2289.OTPResponseError
    assert exc_info.value.args[0] == 'OT-hex must be a str'
    with pytest.raises(otp2289.OTPResponseError) as exc_info:
        otp2289.OTPResponse.from_hex('9e876134d90499d')
    assert exc_info.type is otp2289.OTPResponseError
    assert exc_info.value.args[0] == (
        'The length of the hex should be 16 (representing 64 bits digest)'
    )
    with pytest.raises(otp2289.OTPResponseError) as exc_info:
        otp2289.OTPResponse.from_hex('9e876134d90499dg')
    assert exc_info.type is otp2289.OTPResponseError
    assert exc_info.value.args[0] == 'Invalid OT-hex'

    # .from_tokens
    with pytest.raises(otp2289.OTPResponseError) as exc_info:
        otp2289.OTPResponse.from_tokens(
            b'INCH SEA ANNE LONG AHEM TOUR'  # ty: ignore[invalid-argument-type]
        )
    assert exc_info.type is otp2289.OTPResponseError
    assert exc_info.value.args[0] == 'tokens must be a str'
    with pytest.raises(otp2289.OTPResponseError) as exc_info:
        otp2289.OTPResponse.from_tokens('INCH SEA ANNE LONG AHEM')
    assert exc_info.type is otp2289.OTPResponseError
    assert exc_info.value.args[0] == 'Tokens-string does not contain 6 tokens'
    with pytest.raises(otp2289.OTPResponseError) as exc_info:
        otp2289.OTPResponse.from_tokens('INCH SEA ANNE LONG AHEM SIMEON')
    assert exc_info.type is otp2289.OTPResponseError
    assert exc_info.value.args[0] == (
        'One or more words not present in RFC1760'
    )
    with pytest.raises(otp2289.OTPResponseError) as exc_info:
        otp2289.OTPResponse.from_tokens('INCH SEA ANNE LONG AHEM LEE')
    assert exc_info.type is otp2289.OTPResponseError
    assert exc_info.value.args[0] == 'Invalid bit checksum'


def test_sha1() -> None:
    """
    Tests the SHA-1 functionality of the OTPGenerator as described in the RFC

    Those are the tests from 'RFC-2289 Appendix C - OTP Verification Examples'
    """
    # pass='This is a test.', seed='TeSt'
    gen = otp2289.OTPGenerator(
        b'This is a test.', 'TeSt', otp2289.OTP_ALGO_SHA1
    )
    response = gen.generate_otp_response(step=0)
    assert isinstance(response, otp2289.OTPResponse)
    assert isinstance(response.words, str)
    assert isinstance(response.hexdigest, str)
    assert response.hexdigest == '0xbb9e6ae1979d8ff4'
    assert response.words == 'MILT VARY MAST OK SEES WENT'
    response = gen.generate_otp_response(1)
    assert response.hexdigest == '0x63d936639734385b'
    assert response.words == 'CART OTTO HIVE ODE VAT NUT'
    assert (
        gen.generate_otp_response_from_challenge('otp-sha1 1 TeSt').hexdigest
        == '0x63d936639734385b'
    )
    assert (
        gen.generate_otp_response_from_challenge('otp-sha1 1 TeSt').words
        == 'CART OTTO HIVE ODE VAT NUT'
    )
    response = gen.generate_otp_response(99)
    assert response.hexdigest == '0x87fec7768b73ccf9'
    assert response.words == 'GAFF WAIT SKID GIG SKY EYED'
    assert (
        gen.generate_otp_response_from_challenge(
            'otp-sha1 99   TeSt'
        ).hexdigest
        == '0x87fec7768b73ccf9'
    )
    assert (
        gen.generate_otp_response_from_challenge('otp-sha1 99  TeSt').words
        == 'GAFF WAIT SKID GIG SKY EYED'
    )

    # iterator test
    hexdigests = list(gen.otp_response_range(105))
    words = list(gen.otp_response_range(99))
    hexdigests.reverse()
    words.reverse()
    assert hexdigests[0].hexdigest == '0xbb9e6ae1979d8ff4'
    assert hexdigests[1].hexdigest == '0x63d936639734385b'
    assert hexdigests[99].hexdigest == '0x87fec7768b73ccf9'
    assert words[0].words == 'MILT VARY MAST OK SEES WENT'
    assert words[1].words == 'CART OTTO HIVE ODE VAT NUT'
    assert words[99].words == 'GAFF WAIT SKID GIG SKY EYED'

    # pass='AbCdEfGhIjK', seed='alpha1'
    gen = otp2289.OTPGenerator(b'AbCdEfGhIjK', 'alpha1', otp2289.OTP_ALGO_SHA1)
    response = gen.generate_otp_response(0)
    assert response.hexdigest == '0xad85f658ebe383c9'
    assert response.words == 'LEST OR HEEL SCOT ROB SUIT'
    response = gen.generate_otp_response(1)
    assert response.hexdigest == '0xd07ce229b5cf119b'
    assert response.words == 'RITE TAKE GELD COST TUNE RECK'
    response = gen.generate_otp_response(99)
    assert response.hexdigest == '0x27bc71035aaf3dc6'
    assert response.words == 'MAY STAR TIN LYON VEDA STAN'

    # pass="OTP's are good", seed='correct'
    gen = otp2289.OTPGenerator(
        b"OTP's are good", 'correct', otp2289.OTP_ALGO_SHA1
    )
    response = gen.generate_otp_response(0)
    assert response.hexdigest == '0xd51f3e99bf8e6f0b'
    assert response.words == 'RUST WELT KICK FELL TAIL FRAU'
    response = gen.generate_otp_response(1)
    assert response.hexdigest == '0x82aeb52d943774e4'
    assert response.words == 'FLIT DOSE ALSO MEW DRUM DEFY'
    response = gen.generate_otp_response(99)
    assert response.hexdigest == '0x4f296a74fe1567ec'
    assert response.words == 'AURA ALOE HURL WING BERG WAIT'