diff options
Diffstat (limited to 'test/test_server.py')
| -rw-r--r-- | test/test_server.py | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/test/test_server.py b/test/test_server.py new file mode 100644 index 0000000..64e7f67 --- /dev/null +++ b/test/test_server.py | |||
| @@ -0,0 +1,128 @@ | |||
| 1 | # -*- coding: utf-8 -*- | ||
| 2 | # SPDX-License-Identifier: BSD-2-Clause-FreeBSD | ||
| 3 | # | ||
| 4 | # Copyright (c) 2020-2023, 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""" | ||
| 27 | import json | ||
| 28 | |||
| 29 | import pytest | ||
| 30 | |||
| 31 | import otp2289 | ||
| 32 | |||
| 33 | |||
| 34 | def test_state_caller_exceptions(): | ||
| 35 | """Tests the exceptions when calling the OTPState objects""" | ||
| 36 | state = otp2289.OTPState( | ||
| 37 | '0x7965e05436f5029f', | ||
| 38 | 1, | ||
| 39 | 'TeSt', | ||
| 40 | otp2289.OTP_ALGO_MD5, | ||
| 41 | ) | ||
| 42 | with pytest.raises(otp2289.OTPInvalidResponse) as exc_info: | ||
| 43 | state.response_validates('bla') | ||
| 44 | assert exc_info.type is otp2289.OTPInvalidResponse | ||
| 45 | assert exc_info.value.args[0] == ( | ||
| 46 | 'The response is neither a valid token or hex' | ||
| 47 | ) | ||
| 48 | |||
| 49 | |||
| 50 | def test_state_constructor_exceptions(): | ||
| 51 | """Tests the exceptions when initializing new OTPState objects""" | ||
| 52 | with pytest.raises(otp2289.OTPStateException) as exc_info: | ||
| 53 | otp2289.OTPState( | ||
| 54 | '0x7965e05436f5029t', | ||
| 55 | 1, | ||
| 56 | 'TeStø'.encode(), | ||
| 57 | otp2289.OTP_ALGO_MD5, | ||
| 58 | ) | ||
| 59 | assert exc_info.type is otp2289.OTPStateException | ||
| 60 | assert exc_info.value.args[0] == 'Seed must be a string' | ||
| 61 | with pytest.raises(otp2289.OTPStateException) as exc_info: | ||
| 62 | otp2289.OTPState( | ||
| 63 | '0x7965e05436f5029t', | ||
| 64 | '1', | ||
| 65 | 'TeSt', | ||
| 66 | otp2289.OTP_ALGO_MD5, | ||
| 67 | ) | ||
| 68 | assert exc_info.type is otp2289.OTPStateException | ||
| 69 | assert exc_info.value.args[0] == 'Step value MUST be an int' | ||
| 70 | |||
| 71 | |||
| 72 | def test_state_validation_md5(): | ||
| 73 | """Tests the OTPState validation functionality for MD5""" | ||
| 74 | state = otp2289.OTPState( | ||
| 75 | '0x7965e05436f5029f', | ||
| 76 | 1, | ||
| 77 | 'TeSt', | ||
| 78 | otp2289.OTP_ALGO_MD5, | ||
| 79 | ) | ||
| 80 | assert state.validated is False | ||
| 81 | assert state.response_validates('0x9e876134d90499dd') is True | ||
| 82 | assert state.response_validates('INCH SEA ANNE LONG AHEM TOUR') is True | ||
| 83 | assert state.ot_hex == '7965e05436f5029f' | ||
| 84 | assert state.validated is True | ||
| 85 | |||
| 86 | |||
| 87 | def test_state_validation_sha1(): | ||
| 88 | """Tests the OTPState validation functionality for SHA1""" | ||
| 89 | state = otp2289.OTPState( | ||
| 90 | '0x63d936639734385b', | ||
| 91 | 1, | ||
| 92 | 'TeSt', | ||
| 93 | otp2289.OTP_ALGO_SHA1, | ||
| 94 | ) | ||
| 95 | assert state.validated is False | ||
| 96 | assert state.response_validates('0xbb9e6ae1979d8ff4') is True | ||
| 97 | assert state.response_validates('MILT VARY MAST OK SEES WENT') is True | ||
| 98 | assert state.ot_hex == '63d936639734385b' | ||
| 99 | assert state.validated is True | ||
| 100 | |||
| 101 | |||
| 102 | def test_store(): | ||
| 103 | """Tests the OTPStore functionality""" | ||
| 104 | store_data = { | ||
| 105 | 'sgs': { | ||
| 106 | 'ot_hex': '0x7965e05436f5029f', | ||
| 107 | 'current_step': 1, | ||
| 108 | 'seed': 'TeSt', | ||
| 109 | 'hash_algo': 'md5', | ||
| 110 | }, | ||
| 111 | 'blackmore': { | ||
| 112 | 'ot_hex': '0x63d936639734385b', | ||
| 113 | 'current_step': 1, | ||
| 114 | 'seed': 'TeSt', | ||
| 115 | 'hash_algo': 'sha1', | ||
| 116 | }, | ||
| 117 | } | ||
| 118 | store = otp2289.OTPStore(store_data) | ||
| 119 | assert len(store) == 2 | ||
| 120 | assert isinstance(json.dumps(store.to_dict()), str) # serializable? | ||
| 121 | assert store.response_validates('sgs', '0x9e876134d90499dd') is True | ||
| 122 | assert store.response_validates('sgs', '0x9e876134d90499dd') is False | ||
| 123 | sgs_state = store.get('sgs') | ||
| 124 | assert sgs_state in store | ||
| 125 | store.pop_state('sgs') | ||
| 126 | assert bool(store) is True | ||
| 127 | store.pop_state('blackmore') | ||
| 128 | assert bool(store) is False | ||
