-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_pwmlib_compare.py
313 lines (277 loc) · 10.9 KB
/
test_pwmlib_compare.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/usr/bin/env python
# coding=utf-8
"""
PasswordMaker - Python unit tests in comparison with cli pwm tool
=================================================================
Create and manage passwords.
Copyright (C):
2018 Martin Manns
This file is part of PasswordMaker.
PasswordMaker is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Foobar is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Foobar. If not, see <https://www.gnu.org/licenses/>.
This version should work with Python > 2.3 including Python 3.x.
The pycrypto module enables additional algorithms.
It can be used both on the command-line and with a GUI based on TKinter.
"""
import unittest
import subprocess
from pwmlib import generatepassword, ALGORITHMS, FULL_CHARSET
class TestGeneratepassword(unittest.TestCase):
"""Unit test class for generatepassword"""
# The parameters that are tested in a full factorial way
parameter_set = {
"hashAlgorithm": ALGORITHMS,
"key": ["asdf", "sdfmnklk3", "21289,.3"],
"data": ['passwordmaker.org', 'abcdefghijklmnopqrstuvwxyz.com'],
"passwordLength": [1, 19, 64, 127],
}
def _gen_params(self):
"""Generator of parameters from self.parameter_set"""
for hashAlgorithm in self.parameter_set["hashAlgorithm"]:
for key in self.parameter_set["key"]:
for data in self.parameter_set["data"]:
for passwordLength in self.parameter_set["passwordLength"]:
yield {
"hashAlgorithm": hashAlgorithm,
"key": key,
"data": data,
"passwordLength": passwordLength,
}
def _generatepassword(self,
hashAlgorithm="md5",
key="asdf",
data='passwordmaker.org',
passwordLength=19,
charset=FULL_CHARSET,
prefix="",
suffix=""):
return generatepassword(hashAlgorithm, key, data, passwordLength,
charset, prefix, suffix)
def _get_cmd_args(self, params):
"""Returns command line arguments for given paramters"""
args = ["passwordmaker"]
halg = params["hashAlgorithm"].upper().replace("RMD160", "RIPEMD160")
if "HMAC-" in halg:
args.append("--HMAC")
halg = halg[5:]
args.append("--alg")
args.append(halg)
args.append("--mpw " + params["key"])
args.append("--url " + params["data"])
args.append("--length " + str(params["passwordLength"]))
return args
def test_generatepassword(self):
"""The main test
Requires Python 3.4+
"""
for params in self._gen_params():
with self.subTest(params=params):
res = self._generatepassword(**params)
args = self._get_cmd_args(params)
completed_proc = subprocess.run(args, stdout=subprocess.PIPE)
__res = completed_proc.stdout.strip().decode("utf-8")
if __res.startswith("WARNING"):
__res = __res.split("\n")[-1]
self.assertEqual(res, __res)
# def test_generatepassword_2chars(self):
# res = self._generatepassword(passwordLength=2)
# self.assertEqual(res, 'FR')
#
# def test_generatepassword_19chars(self):
# res = self._generatepassword(passwordLength=19)
# self.assertEqual(res, 'FRRHm)k+UyQiY~%Dj;h')
#
# def test_generatepassword_20chars(self):
# res = self._generatepassword(passwordLength=20)
# self.assertEqual(res, 'FRRHm)k+UyQiY~%Dj;h*')
#
# def test_32chars(self):
# res = self._generatepassword(passwordLength=32)
# r = 'FRRHm)k+UyQiY~%Dj;h*FV[{:5X@EN5k'
# self.assertEqual(res, r)
#
# def test_64chars(self):
# res = self._generatepassword(passwordLength=64)
# r = 'FRRHm)k+UyQiY~%Dj;h*FV[{:[email protected]=-]pF}UyDtCZ9#'
# self.assertEqual(res, r)
#
# # Vary algorithm
#
# def test_generatepassword_sha256(self):
# alg = "sha256"
# if alg not in ALGORITHMS:
# raise Warning("Algorithm {} unavailable.".format(alg))
# res = self._generatepassword(hashAlgorithm=alg)
# r = '7d,Hgx:o&+&}h;=*>5r'
# self.assertEqual(res, r)
#
# def test_generatepassword_hmac_sha256(self):
# alg = "hmac-sha256"
# if alg not in ALGORITHMS:
# raise Warning("Algorithm {} unavailable.".format(alg))
# res = self._generatepassword(hashAlgorithm=alg)
# r = '~A6{!<Y4UGo$%7x;alX'
# self.assertEqual(res, r)
#
# def test_generatepassword_sha1(self):
# alg = "sha1"
# if alg not in ALGORITHMS:
# raise Warning("Algorithm {} unavailable.".format(alg))
# res = self._generatepassword(hashAlgorithm=alg)
# r = "D)k{Gq\\\\'7]-/3\\=m4p"
# self.assertEqual(res, r)
#
# def test_generatepassword_hmac_sha1(self):
# alg = "hmac-sha1"
# if alg not in ALGORITHMS:
# raise Warning("Algorithm {} unavailable.".format(alg))
# res = self._generatepassword(hashAlgorithm=alg)
# r = "E~|ym$>s:gp'cx-}Y.|"
# self.assertEqual(res, r)
#
# def test_generatepassword_md4(self):
# alg = "md4"
# if alg not in ALGORITHMS:
# raise Warning("Algorithm {} unavailable.".format(alg))
# res = self._generatepassword(hashAlgorithm=alg)
# r = 'BE?3q<(S"!(Hyr(dUmr'
# self.assertEqual(res, r)
#
# def test_generatepassword_hmac_md4(self):
# alg = "hmac-md4"
# if alg not in ALGORITHMS:
# raise Warning("Algorithm {} unavailable.".format(alg))
# res = self._generatepassword(hashAlgorithm=alg)
# r = 'B^z!H_Nx\\p0=iVV<>X,'
# self.assertEqual(res, r)
#
# def test_generatepassword_hmac_md5(self):
# alg = "hmac-md5"
# if alg not in ALGORITHMS:
# raise Warning("Algorithm {} unavailable.".format(alg))
# res = self._generatepassword(hashAlgorithm=alg)
# r = 'IGf<=RsU3qvE"hBFmG}'
# self.assertEqual(res, r)
#
# def test_generatepassword_rmd160(self):
# alg = "rmd160"
# if alg not in ALGORITHMS:
# raise Warning("Algorithm {} unavailable.".format(alg))
# res = self._generatepassword(hashAlgorithm=alg)
# r = "CmAQg:hV'<~Vz.:NnDV"
# self.assertEqual(res, r)
#
# def test_generatepassword_hmac_rmd160(self):
# alg = "hmac-rmd160"
# if alg not in ALGORITHMS:
# raise Warning("Algorithm {} unavailable.".format(alg))
# res = self._generatepassword(hashAlgorithm=alg)
# r = 'DBgLK[hHK{[e8nfH8/S'
# self.assertEqual(res, r)
#
# # Vary multiple parameters
#
# def test_generatepassword_20chars_hmac(self):
# res = self._generatepassword(hashAlgorithm="hmac-rmd160",
# passwordLength=20)
# self.assertEqual(res, 'DBgLK[hHK{[e8nfH8/SI')
#
# def test_64chars_hmac(self):
# res = self._generatepassword(hashAlgorithm="hmac-rmd160",
# passwordLength=64)
# r = 'DBgLK[hHK{[e8nfH8/SI.Kz.(E}10$O-U2#f{jBWYT]b:&]vjDC3bBPQE*XN)\'5y'
# self.assertEqual(res, r)
#
# def test_128chars(self):
# res = self._generatepassword(passwordLength=128)
# r = "FRRHm)k+UyQiY~%Dj;h*FV[{:[email protected]=-]pF}UyDtC" +\
# "Z9#e/Gs\\l?0QX2P*gBvHTp" + '"' + "t#h^Knv{l\'G" + r'"' + \
# "AK-qG/DWhEk9l-c%tqH}&ttsK\\<Yl4&{"
# self.assertEqual(res, r)
#
#
#class TestLeet(unittest.TestCase):
# """Unit test class for leet"""
#
# def test_leet_0(self):
# message = "The quick, brown fox jumps over the lazy dog"
# leet_level = 0
# res = leet(leet_level, message)
# r = "the quick, brown fox jumps over the lazy dog"
# self.assertEqual(res, r)
#
# def test_leet_1(self):
# message = "The quick, brown fox jumps over the lazy dog"
# leet_level = 1
# res = leet(leet_level, message)
# r = "7h3 9uick, br0wn f0x jumps 0v3r 7h3 14zy d0g"
# self.assertEqual(res, r)
#
# def test_leet_2(self):
# message = "The quick, brown fox jumps over the lazy dog"
# leet_level = 2
# res = leet(leet_level, message)
# r = "7h3 9ulck, br0wn f0x jump5 0v3r 7h3 142y d0g"
# self.assertEqual(res, r)
#
# def test_leet_3(self):
# message = "The quick, brown fox jumps over the lazy dog"
# leet_level = 3
# res = leet(leet_level, message)
# r = "7h3 9u'ck, 8r0wn f0x jump5 0v3r 7h3 142'/ d06"
# self.assertEqual(res, r)
#
# def test_leet_4(self):
# message = "The quick, brown fox jumps over the lazy dog"
# leet_level = 4
# res = leet(leet_level, message)
# r = "7h3 9u'ck, 8r0wn f0x jump5 0v3r 7h3 1@2'/ d06"
# self.assertEqual(res, r)
#
# def test_leet_5(self):
# message = "The quick, brown fox jumps over the lazy dog"
# leet_level = 5
# res = leet(leet_level, message)
# r = "7#3 9u!c|<, |3|20wn f0x 7um|>$ 0\/3|2 7#3 1@2'/ d06"
# self.assertEqual(res, r)
#
# def test_leet_6(self):
# message = "The quick, brown fox jumps over the lazy dog"
# leet_level = 6
# res = leet(leet_level, message)
# r = "7#& 9u!c|<, |3|20wn |=0x ,|um|>$ 0\/&|2 7#& 1@2'/ |)06"
# self.assertEqual(res, r)
#
# def test_leet_7(self):
# message = "The quick, brown fox jumps over the lazy dog"
# leet_level = 7
# res = leet(leet_level, message)
# r = "7#& 9(_)![|<, |3|20\/\/^/ |=0>< ,|(_)^^|*5 0\/&|2 7#& 1@2'/ |)06"
# self.assertEqual(res, r)
#
# def test_leet_8(self):
# message = "The quick, brown fox jumps over the lazy dog"
# leet_level = 8
# res = leet(leet_level, message)
# r = '||-|& (,)|_|!(|(, 8|2()\\^/|\\| |=())( _||_||\\/||>$ ()\\/&|2 ' +\
# '||-|& 1@"/_\'/ |)()6'
# self.assertEqual(res, r)
#
# def test_leet_9(self):
# message = "The quick, brown fox jumps over the lazy dog"
# leet_level = 9
# res = leet(leet_level, message)
# r = '||-|& (,)|_|!(|{, 8|2()\\^/|\\| |=())( _||_|/\\/\\|>$ ()\\/&|2' +\
# ' ||-|& |_@"/_\'/ |)()6'
# self.assertEqual(res, r)
if __name__ == '__main__':
unittest.main()