summaryrefslogtreecommitdiff
path: root/tests/test_server.py
diff options
context:
space:
mode:
authorSimeon Simeonov2020-03-30 21:57:53 +0200
committerSimeon Simeonov2020-03-30 21:57:53 +0200
commit2a6299c32e0baf3df06f8e6d6bc8695b951d630d (patch)
treeb4bd18092822db0fd10411e49dc6e05d1a60e697 /tests/test_server.py
parenta4c050e34ce462f477bc3671e272e244e2792e00 (diff)
Implement OTPState in the server module
Diffstat (limited to 'tests/test_server.py')
-rw-r--r--tests/test_server.py84
1 files changed, 84 insertions, 0 deletions
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