summaryrefslogtreecommitdiff
path: root/test/test_main.py
diff options
context:
space:
mode:
Diffstat (limited to 'test/test_main.py')
-rw-r--r--test/test_main.py264
1 files changed, 0 insertions, 264 deletions
diff --git a/test/test_main.py b/test/test_main.py
deleted file mode 100644
index 593bf80..0000000
--- a/test/test_main.py
+++ /dev/null
@@ -1,264 +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.__main__"""
26import os
27import unittest.mock
28
29import pytest
30
31from otp2289.__main__ import main
32
33
34def test_main_generate_otp_response(capsys):
35 """tests main"""
36 args = [
37 '--generate-otp-response',
38 '-a',
39 'sha1',
40 '-i',
41 '99',
42 '-s',
43 'TesT',
44 '-p',
45 'This is a test.',
46 ]
47 with pytest.raises(SystemExit) as exit_info:
48 main(args)
49 captured = capsys.readouterr()
50 assert captured.out == (
51 f'Seed: TesT, Step: 99, Hash: sha1{os.linesep}'
52 f'0x87fec7768b73ccf9{os.linesep}'
53 )
54 assert exit_info.type == SystemExit
55 assert exit_info.value.code == 0
56 args.extend(['-f', 'token'])
57 with pytest.raises(SystemExit) as exit_info:
58 main(args)
59 captured = capsys.readouterr()
60 assert captured.out == (
61 f'Seed: TesT, Step: 99, Hash: sha1{os.linesep}'
62 f'GAFF WAIT SKID GIG SKY EYED{os.linesep}'
63 )
64 assert exit_info.type == SystemExit
65 assert exit_info.value.code == 0
66 args.append('-q')
67 with pytest.raises(SystemExit) as exit_info:
68 main(args)
69 captured = capsys.readouterr()
70 assert captured.out == f'GAFF WAIT SKID GIG SKY EYED{os.linesep}'
71 assert exit_info.type == SystemExit
72 assert exit_info.value.code == 0
73
74
75def test_main_generate_otp_response_env_passwd(capsys):
76 """tests main by fetching password from the env. var. 'OTP2289_PASSWORD'"""
77 args = [
78 '--generate-otp-response',
79 '-a',
80 'sha1',
81 '-i',
82 '99',
83 '-s',
84 'TesT',
85 ]
86 with unittest.mock.patch.dict(
87 os.environ, {'OTP2289_PASSWORD': 'This is a test.'}
88 ):
89 with pytest.raises(SystemExit) as exit_info:
90 main(args)
91 captured = capsys.readouterr()
92 assert captured.out == (
93 f'Seed: TesT, Step: 99, Hash: sha1{os.linesep}'
94 f'0x87fec7768b73ccf9{os.linesep}'
95 )
96 assert exit_info.type == SystemExit
97 assert exit_info.value.code == 0
98 args.extend(['-f', 'token'])
99 with pytest.raises(SystemExit) as exit_info:
100 main(args)
101 captured = capsys.readouterr()
102 assert captured.out == (
103 f'Seed: TesT, Step: 99, Hash: sha1{os.linesep}'
104 f'GAFF WAIT SKID GIG SKY EYED{os.linesep}'
105 )
106 assert exit_info.type == SystemExit
107 assert exit_info.value.code == 0
108 args.append('-q')
109 with pytest.raises(SystemExit) as exit_info:
110 main(args)
111 captured = capsys.readouterr()
112 assert captured.out == f'GAFF WAIT SKID GIG SKY EYED{os.linesep}'
113 assert exit_info.type == SystemExit
114 assert exit_info.value.code == 0
115
116
117def test_main_generate_otp_range(capsys):
118 """tests main"""
119 args = [
120 '--generate-otp-range',
121 '-i',
122 '2',
123 '-s',
124 'TesT',
125 '-r',
126 '5',
127 '-p',
128 'This is a test.',
129 ]
130 with pytest.raises(SystemExit) as exit_info:
131 main(args)
132 captured = capsys.readouterr()
133 assert captured.out == (
134 f'Seed: TesT, Step: 2, Hash: md5, Range: 3'
135 f'{os.linesep}'
136 f'2: 0x4049f8b161669b7b{os.linesep}'
137 f'1: 0x7965e05436f5029f{os.linesep}'
138 f'0: 0x9e876134d90499dd{os.linesep}'
139 )
140 assert exit_info.type == SystemExit
141 assert exit_info.value.code == 0
142 args.append('-q')
143 with pytest.raises(SystemExit) as exit_info:
144 main(args)
145 captured = capsys.readouterr()
146 assert captured.out == (
147 f'2: 0x4049f8b161669b7b{os.linesep}'
148 f'1: 0x7965e05436f5029f{os.linesep}'
149 f'0: 0x9e876134d90499dd{os.linesep}'
150 )
151 assert exit_info.type == SystemExit
152 assert exit_info.value.code == 0
153 args.extend(['-f', 'token'])
154 with pytest.raises(SystemExit) as exit_info:
155 main(args)
156 captured = capsys.readouterr()
157 assert captured.out == (
158 f'2: THY AVON NO NECK COKE MOLL{os.linesep}'
159 f'1: EASE OIL FUM CURE AWRY AVIS{os.linesep}'
160 f'0: INCH SEA ANNE LONG AHEM TOUR{os.linesep}'
161 )
162 assert exit_info.type == SystemExit
163 assert exit_info.value.code == 0
164
165
166@pytest.mark.parametrize(
167 'args',
168 [
169 [
170 '--generate-otp-range',
171 '-i',
172 '2',
173 '-s',
174 'TesT',
175 '-r',
176 '5',
177 ],
178 [
179 '--generate-otp-range',
180 '-i',
181 '2',
182 '-s',
183 'TesT',
184 '-r',
185 '5',
186 '-P',
187 ],
188 ],
189)
190@unittest.mock.patch('getpass.getpass', lambda *args: 'This is a test.')
191def test_main_generate_otp_range_passwd_prompt(capsys, args):
192 """tests main by prompting for password (with or without -P)"""
193 args = [
194 '--generate-otp-range',
195 '-i',
196 '2',
197 '-s',
198 'TesT',
199 '-r',
200 '5',
201 ]
202 with pytest.raises(SystemExit) as exit_info:
203 main(args)
204 captured = capsys.readouterr()
205 assert captured.out == (
206 f'Seed: TesT, Step: 2, Hash: md5, Range: 3'
207 f'{os.linesep}'
208 f'2: 0x4049f8b161669b7b{os.linesep}'
209 f'1: 0x7965e05436f5029f{os.linesep}'
210 f'0: 0x9e876134d90499dd{os.linesep}'
211 )
212 assert exit_info.type == SystemExit
213 assert exit_info.value.code == 0
214 args.append('-q')
215 with pytest.raises(SystemExit) as exit_info:
216 main(args)
217 captured = capsys.readouterr()
218 assert captured.out == (
219 f'2: 0x4049f8b161669b7b{os.linesep}'
220 f'1: 0x7965e05436f5029f{os.linesep}'
221 f'0: 0x9e876134d90499dd{os.linesep}'
222 )
223 assert exit_info.type == SystemExit
224 assert exit_info.value.code == 0
225 args.extend(['-f', 'token'])
226 with pytest.raises(SystemExit) as exit_info:
227 main(args)
228 captured = capsys.readouterr()
229 assert captured.out == (
230 f'2: THY AVON NO NECK COKE MOLL{os.linesep}'
231 f'1: EASE OIL FUM CURE AWRY AVIS{os.linesep}'
232 f'0: INCH SEA ANNE LONG AHEM TOUR{os.linesep}'
233 )
234 assert exit_info.type == SystemExit
235 assert exit_info.value.code == 0
236
237
238def test_main_initiate(capsys):
239 """tests main"""
240 args = [
241 '--initiate-new-sequence',
242 '-i',
243 '500',
244 '-s',
245 'TesT',
246 '-p',
247 'This is a test.',
248 ]
249 with pytest.raises(SystemExit) as exit_info:
250 main(args)
251 captured = capsys.readouterr()
252 assert captured.out == (
253 f'Seed: TesT, Step: 500, Hash: md5{os.linesep}'
254 f'0x2b8d82b6ac14346c{os.linesep}'
255 )
256 assert exit_info.type == SystemExit
257 assert exit_info.value.code == 0
258 args.append('-q')
259 with pytest.raises(SystemExit) as exit_info:
260 main(args)
261 captured = capsys.readouterr()
262 assert captured.out == f'0x2b8d82b6ac14346c{os.linesep}'
263 assert exit_info.type == SystemExit
264 assert exit_info.value.code == 0