summaryrefslogtreecommitdiff
path: root/test/test_server.py
diff options
context:
space:
mode:
Diffstat (limited to 'test/test_server.py')
-rw-r--r--test/test_server.py127
1 files changed, 0 insertions, 127 deletions
diff --git a/test/test_server.py b/test/test_server.py
deleted file mode 100644
index e81532f..0000000
--- a/test/test_server.py
+++ /dev/null
@@ -1,127 +0,0 @@
1# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
2#
3# Copyright (c) 2020-2025, Simeon Simeonov
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright notice,
12# this list of conditions and the following disclaimer in the documentation
13# and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
16# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25"""Tests for otp2289.server"""
26import json
27
28import pytest
29
30import otp2289
31
32
33def test_state_caller_exceptions():
34 """Tests the exceptions when calling the OTPState objects"""
35 state = otp2289.OTPState(
36 '0x7965e05436f5029f',
37 1,
38 'TeSt',
39 otp2289.OTP_ALGO_MD5,
40 )
41 with pytest.raises(otp2289.OTPInvalidResponseError) as exc_info:
42 state.response_validates('bla')
43 assert exc_info.type is otp2289.OTPInvalidResponseError
44 assert exc_info.value.args[0] == (
45 'The response is neither a valid token or hex'
46 )
47
48
49def test_state_constructor_exceptions():
50 """Tests the exceptions when initializing new OTPState objects"""
51 with pytest.raises(otp2289.OTPStateError) as exc_info:
52 otp2289.OTPState(
53 '0x7965e05436f5029t',
54 1,
55 'TeStø'.encode(),
56 otp2289.OTP_ALGO_MD5,
57 )
58 assert exc_info.type is otp2289.OTPStateError
59 assert exc_info.value.args[0] == 'Seed must be a string'
60 with pytest.raises(otp2289.OTPStateError) as exc_info:
61 otp2289.OTPState(
62 '0x7965e05436f5029t',
63 '1',
64 'TeSt',
65 otp2289.OTP_ALGO_MD5,
66 )
67 assert exc_info.type is otp2289.OTPStateError
68 assert exc_info.value.args[0] == 'Step value MUST be an int'
69
70
71def test_state_validation_md5():
72 """Tests the OTPState validation functionality for MD5"""
73 state = otp2289.OTPState(
74 '0x7965e05436f5029f',
75 1,
76 'TeSt',
77 otp2289.OTP_ALGO_MD5,
78 )
79 assert state.validated is False
80 assert state.response_validates('0x9e876134d90499dd') is True
81 assert state.response_validates('INCH SEA ANNE LONG AHEM TOUR') is True
82 assert state.ot_hex == '7965e05436f5029f'
83 assert state.validated is True
84
85
86def test_state_validation_sha1():
87 """Tests the OTPState validation functionality for SHA1"""
88 state = otp2289.OTPState(
89 '0x63d936639734385b',
90 1,
91 'TeSt',
92 otp2289.OTP_ALGO_SHA1,
93 )
94 assert state.validated is False
95 assert state.response_validates('0xbb9e6ae1979d8ff4') is True
96 assert state.response_validates('MILT VARY MAST OK SEES WENT') is True
97 assert state.ot_hex == '63d936639734385b'
98 assert state.validated is True
99
100
101def test_store():
102 """Tests the OTPStore functionality"""
103 store_data = {
104 'sgs': {
105 'ot_hex': '0x7965e05436f5029f',
106 'current_step': 1,
107 'seed': 'TeSt',
108 'hash_algo': 'md5',
109 },
110 'blackmore': {
111 'ot_hex': '0x63d936639734385b',
112 'current_step': 1,
113 'seed': 'TeSt',
114 'hash_algo': 'sha1',
115 },
116 }
117 store = otp2289.OTPStore(store_data)
118 assert len(store) == 2
119 assert isinstance(json.dumps(store.to_dict()), str) # serializable?
120 assert store.response_validates('sgs', '0x9e876134d90499dd') is True
121 assert store.response_validates('sgs', '0x9e876134d90499dd') is False
122 sgs_state = store.get('sgs')
123 assert sgs_state in store
124 store.pop_state('sgs')
125 assert bool(store) is True
126 store.pop_state('blackmore')
127 assert bool(store) is False