summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/test_generator.py20
-rw-r--r--tests/test_server.py84
-rw-r--r--tests/test_static.py79
3 files changed, 173 insertions, 10 deletions
diff --git a/tests/test_generator.py b/tests/test_generator.py
index 74fc3a4..50274e2 100644
--- a/tests/test_generator.py
+++ b/tests/test_generator.py
@@ -62,16 +62,6 @@ def test_constructor_exceptions():
62 """ 62 """
63 # test the otp2289.OTPGenerator __init__ and validators 63 # test the otp2289.OTPGenerator __init__ and validators
64 with pytest.raises(otp2289.OTPGeneratorException) as exc_info: 64 with pytest.raises(otp2289.OTPGeneratorException) as exc_info:
65 otp2289.OTPGenerator('1234567', 'TeStø'.encode(), otp2289.OTP_ALGO_MD5)
66 assert exc_info.type is otp2289.OTPGeneratorException
67 assert exc_info.value.args[0] == 'Password must be a byte-string'
68 with pytest.raises(otp2289.OTPGeneratorException) as exc_info:
69 otp2289.OTPGenerator('1234567'.encode(),
70 'TeStø'.encode(),
71 otp2289.OTP_ALGO_MD5)
72 assert exc_info.type is otp2289.OTPGeneratorException
73 assert exc_info.value.args[0] == 'Password must be longer than 10 bytes'
74 with pytest.raises(otp2289.OTPGeneratorException) as exc_info:
75 otp2289.OTPGenerator('This is a test.'.encode(), 65 otp2289.OTPGenerator('This is a test.'.encode(),
76 'TeStø'.encode(), 66 'TeStø'.encode(),
77 otp2289.OTP_ALGO_MD5) 67 otp2289.OTP_ALGO_MD5)
@@ -108,6 +98,16 @@ def test_constructor_exceptions():
108 assert exc_info.type is otp2289.generator.OTPGeneratorException 98 assert exc_info.type is otp2289.generator.OTPGeneratorException
109 assert exc_info.value.args[0] == ('foo is not supported by this version ' 99 assert exc_info.value.args[0] == ('foo is not supported by this version '
110 'of the hashlib module') 100 'of the hashlib module')
101 with pytest.raises(otp2289.OTPGeneratorException) as exc_info:
102 otp2289.OTPGenerator('1234567', 'TeSt', otp2289.OTP_ALGO_MD5)
103 assert exc_info.type is otp2289.OTPGeneratorException
104 assert exc_info.value.args[0] == 'Password must be a byte-string'
105 with pytest.raises(otp2289.OTPGeneratorException) as exc_info:
106 otp2289.OTPGenerator('1234567'.encode(),
107 'TeSt',
108 otp2289.OTP_ALGO_MD5)
109 assert exc_info.type is otp2289.OTPGeneratorException
110 assert exc_info.value.args[0] == 'Password must be longer than 10 bytes'
111 111
112 112
113def test_md5(): 113def test_md5():
diff --git a/tests/test_server.py b/tests/test_server.py
new file mode 100644
index 0000000..c7cdb12
--- /dev/null
+++ b/tests/test_server.py
@@ -0,0 +1,84 @@
1# -*- coding: utf-8 -*-
2# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3#
4# Copyright (c) 2020, Simeon Simeonov
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11# notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright notice,
13# this list of conditions and the following disclaimer in the documentation
14# and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
17# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26"""Tests for otp2289.server"""
27import pytest
28
29import otp2289
30
31
32def test_state_caller_exceptions():
33 """Tests the exceptions when calling the OTPState objects"""
34 state = otp2289.OTPState('0x7965e05436f5029f',
35 1,
36 'TeSt',
37 otp2289.OTP_ALGO_MD5)
38 with pytest.raises(otp2289.OTPInvalidResponse) as exc_info:
39 state.response_validates('bla')
40 assert exc_info.type is otp2289.OTPInvalidResponse
41 assert exc_info.value.args[0] == (
42 'The response is neither a valid token or hex')
43
44
45def test_state_constructor_exceptions():
46 """Tests the exceptions when initializing new OTPState objects"""
47 with pytest.raises(otp2289.OTPStateException) as exc_info:
48 otp2289.OTPState('0x7965e05436f5029t',
49 1,
50 'TeStø'.encode(),
51 otp2289.OTP_ALGO_MD5)
52 assert exc_info.type is otp2289.OTPStateException
53 assert exc_info.value.args[0] == 'Seed must be a string'
54 with pytest.raises(otp2289.OTPStateException) as exc_info:
55 otp2289.OTPState('0x7965e05436f5029t',
56 '1',
57 'TeSt',
58 otp2289.OTP_ALGO_MD5)
59 assert exc_info.type is otp2289.OTPStateException
60 assert exc_info.value.args[0] == 'Step value MUST be an int'
61
62
63def test_state_validation_md5():
64 """Tests the OTPState validation functionality for MD5"""
65 state = otp2289.OTPState('0x7965e05436f5029f',
66 1,
67 'TeSt',
68 otp2289.OTP_ALGO_MD5)
69 assert state.validated is False
70 assert state.response_validates('0x9e876134d90499dd') is True
71 assert state.response_validates('INCH SEA ANNE LONG AHEM TOUR') is True
72 assert state.validated is True
73
74
75def test_state_validation_sha1():
76 """Tests the OTPState validation functionality for SHA1"""
77 state = otp2289.OTPState('0x63d936639734385b',
78 1,
79 'TeSt',
80 otp2289.OTP_ALGO_SHA1)
81 assert state.validated is False
82 assert state.response_validates('0xbb9e6ae1979d8ff4') is True
83 assert state.response_validates('MILT VARY MAST OK SEES WENT') is True
84 assert state.validated is True
diff --git a/tests/test_static.py b/tests/test_static.py
new file mode 100644
index 0000000..dc6e2d7
--- /dev/null
+++ b/tests/test_static.py
@@ -0,0 +1,79 @@
1# -*- coding: utf-8 -*-
2# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3#
4# Copyright (c) 2020, Simeon Simeonov
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11# notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright notice,
13# this list of conditions and the following disclaimer in the documentation
14# and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
17# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26"""Tests for the static methods and basic bit, byte, token functionality"""
27import binascii
28import os
29
30import otp2289
31
32
33def test_bytes_and_tokens():
34 """Tests the official hex and tokens defined in RFC2289"""
35 assert binascii.unhexlify('9e876134d90499dd') == (
36 otp2289.OTPGenerator.tokens_to_bytes('INCH SEA ANNE LONG AHEM TOUR'))
37 assert binascii.unhexlify('7965e05436f5029f') == (
38 otp2289.OTPGenerator.tokens_to_bytes('EASE OIL FUM CURE AWRY AVIS'))
39 assert binascii.unhexlify('50fe1962c4965880') == (
40 otp2289.OTPGenerator.tokens_to_bytes('BAIL TUFT BITS GANG CHEF THY'))
41 assert binascii.unhexlify('87066dd9644bf206') == (
42 otp2289.OTPGenerator.tokens_to_bytes('FULL PEW DOWN ONCE MORT ARC'))
43 assert binascii.unhexlify('7cd34c1040add14b') == (
44 otp2289.OTPGenerator.tokens_to_bytes('FACT HOOF AT FIST SITE KENT'))
45 assert binascii.unhexlify('5aa37a81f212146c') == (
46 otp2289.OTPGenerator.tokens_to_bytes('BODE HOP JAKE STOW JUT RAP'))
47 assert binascii.unhexlify('f205753943de4cf9') == (
48 otp2289.OTPGenerator.tokens_to_bytes('ULAN NEW ARMY FUSE SUIT EYED'))
49 assert binascii.unhexlify('ddcdac956f234937') == (
50 otp2289.OTPGenerator.tokens_to_bytes('SKIM CULT LOB SLAM POE HOWL'))
51 assert binascii.unhexlify('b203e28fa525be47') == (
52 otp2289.OTPGenerator.tokens_to_bytes('LONG IVY JULY AJAR BOND LEE'))
53
54 assert binascii.unhexlify('bb9e6ae1979d8ff4') == (
55 otp2289.OTPGenerator.tokens_to_bytes('MILT VARY MAST OK SEES WENT'))
56 assert binascii.unhexlify('63d936639734385b') == (
57 otp2289.OTPGenerator.tokens_to_bytes('CART OTTO HIVE ODE VAT NUT'))
58 assert binascii.unhexlify('87fec7768b73ccf9') == (
59 otp2289.OTPGenerator.tokens_to_bytes('GAFF WAIT SKID GIG SKY EYED'))
60 assert binascii.unhexlify('ad85f658ebe383c9') == (
61 otp2289.OTPGenerator.tokens_to_bytes('LEST OR HEEL SCOT ROB SUIT'))
62 assert binascii.unhexlify('d07ce229b5cf119b') == (
63 otp2289.OTPGenerator.tokens_to_bytes('RITE TAKE GELD COST TUNE RECK'))
64 assert binascii.unhexlify('27bc71035aaf3dc6') == (
65 otp2289.OTPGenerator.tokens_to_bytes('MAY STAR TIN LYON VEDA STAN'))
66 assert binascii.unhexlify('d51f3e99bf8e6f0b') == (
67 otp2289.OTPGenerator.tokens_to_bytes('RUST WELT KICK FELL TAIL FRAU'))
68 assert binascii.unhexlify('82aeb52d943774e4') == (
69 otp2289.OTPGenerator.tokens_to_bytes('FLIT DOSE ALSO MEW DRUM DEFY'))
70 assert binascii.unhexlify('4f296a74fe1567ec') == (
71 otp2289.OTPGenerator.tokens_to_bytes('AURA ALOE HURL WING BERG WAIT'))
72
73
74def test_random_bytes():
75 """Implement a few tests with random bytes"""
76 for _ in range(10):
77 rnd_bytes = os.urandom(8) # 64 bits
78 tokens = otp2289.OTPGenerator.bytes_to_tokens(rnd_bytes)
79 assert rnd_bytes == otp2289.OTPGenerator.tokens_to_bytes(tokens)