-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_chi.py
executable file
·594 lines (479 loc) · 21.9 KB
/
test_chi.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
#!/usr/bin/env python
# -*- coding: us-ascii -*-
# vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab
"""Test suite for pytombo
Sample usage:
./test_chi.py -v
./test_chi.py -v TestCompatChiIO
"""
import os
import sys
import string
import codecs
try:
if sys.version_info < (2, 3):
raise ImportError
import unittest2
unittest = unittest2
except ImportError:
import unittest
unittest2 = None
try:
raise ImportError # do not use cStringIO for readwrite_seek test
# AttributeError: 'cStringIO.StringI' object has no attribute 'write'
from cStringIO import StringIO as FakeFile
using_cstring = True
except ImportError:
try:
from StringIO import StringIO as FakeFile
using_cstring = True
except ImportError:
from io import BytesIO as FakeFile # py3
using_cstring = False
import chi_io
"""
Missing tests for:
* chi_io.dumb_unix2dos()
Present are tests that make calls to:
* chi_io.write_encrypted_file()
* chi_io.read_encrypted_file()
* chi_io.ChiAsFile class
* chi_io.CHI_cipher() (explicit and implicit called by read_encrypted_file() and write_encrypted_file())
NOTE this does not mean exhaustive tests ;-)
No explicit tests for:
* ??
"""
class TestChiIOBase(unittest.TestCase):
def skip(self, reason):
"""Skip current test because of `reason`.
NOTE currently expects unittest2, and defaults to "pass" if not available.
unittest2 does NOT work under Python 2.2.
Could potentially use nose or py.test which has (previously) supported Python 2.2
* nose http://python-nose.googlecode.com/svn/wiki/NoseWithPython2_2.wiki
* py.test http://codespeak.net/pipermail/py-dev/2005-February/000203.html
"""
# self.assertEqual(1, 0)
if hasattr(unittest, 'SkipTest'):
raise unittest.SkipTest(reason)
else:
print(reason)
self.fail('SKIP THIS TEST: ' + reason)
# self.assertTrue(False, reason)
# raise Exception(reason)
class TestChiIOUtil(TestChiIOBase):
## test util functions
def test_password_ascii_bytes(self):
test_password = b'mypassword'
result = chi_io.CHI_cipher(test_password)
self.assertTrue(isinstance(result, chi_io.TheBlowfishClass)) # TODO review is this a reasonable self.assert call?
def test_password_ascii_string(self):
test_password = 'mypassword'
result = chi_io.CHI_cipher(test_password)
self.assertTrue(isinstance(result, chi_io.TheBlowfishClass))
def test_password_ascii_unicode(self):
test_password = u'mypassword'
result = chi_io.CHI_cipher(test_password)
self.assertTrue(isinstance(result, chi_io.TheBlowfishClass))
def test_password_nonascii_unicode(self):
test_password = u'YEN SIGN \u00A5'
self.assertRaises(
chi_io.ChiIO,
chi_io.CHI_cipher,
test_password
)
class TestChiIO(TestChiIOBase):
def test_get_what_you_put_in(self):
test_data = b"this is just a small piece of text."
test_password = b'mypassword'
fileptr1 = FakeFile()
chi_io.write_encrypted_file(fileptr1, test_password, test_data)
crypted_data = fileptr1.getvalue()
# print repr(crypted_data)
fileptr2 = FakeFile(crypted_data)
result_data = chi_io.read_encrypted_file(fileptr2, test_password)
# print repr(result_data)
self.assertEqual(test_data, result_data)
def test_same_input_different_crypted_text(self):
test_data = b"this is just a small piece of text."
test_password = b'mypassword'
fileptr1 = FakeFile()
chi_io.write_encrypted_file(fileptr1, test_password, test_data)
crypted_data1 = fileptr1.getvalue()
fileptr2 = FakeFile()
chi_io.write_encrypted_file(fileptr2, test_password, test_data)
crypted_data2 = fileptr2.getvalue()
self.assertNotEqual(crypted_data1, crypted_data2)
fileptr3 = FakeFile(crypted_data1)
result_data = chi_io.read_encrypted_file(fileptr3, test_password)
self.assertEqual(test_data, result_data)
fileptr3 = FakeFile(crypted_data2)
result_data = chi_io.read_encrypted_file(fileptr3, test_password)
self.assertEqual(test_data, result_data)
def test_fileread(self):
test_data = b"this is just a small piece of text."
test_password = b'mypassword'
fileptr1 = FakeFile()
chi_io.write_encrypted_file(fileptr1, test_password, test_data)
crypted_data = fileptr1.getvalue()
# print repr(crypted_data)
fileptr2 = FakeFile(crypted_data)
chi_fileptr = chi_io.ChiAsFile(fileptr2, test_password)
result_data = chi_fileptr.read()
# print repr(result_data)
self.assertEqual(test_data, result_data)
def test_filewrite(self):
test_data = b"this is just a small piece of text."
test_password = b'mypassword'
fileptr1 = FakeFile()
chi_fileptr = chi_io.ChiAsFile(fileptr1, test_password, 'w')
chi_fileptr.write(test_data)
chi_fileptr.close()
crypted_data = fileptr1.getvalue()
# print 'crypted_data ', repr(crypted_data)
fileptr2 = FakeFile(crypted_data)
result_data = chi_io.read_encrypted_file(fileptr2, test_password)
# print repr(result_data)
self.assertEqual(test_data, result_data)
def test_file_bad_read(self):
test_data = b"this is just a small piece of text."
test_password = b'mypassword'
fileptr1 = FakeFile()
chi_fileptr = chi_io.ChiAsFile(fileptr1, test_password, 'w')
chi_fileptr.write(
test_data
) # file is now a write only file, any reads after this point are an error
self.assertRaises(IOError, chi_fileptr.read)
def test_file_bad_write(self):
test_data = b"this is just a small piece of text."
test_password = b'mypassword'
fileptr1 = FakeFile()
chi_io.write_encrypted_file(fileptr1, test_password, test_data)
crypted_data = fileptr1.getvalue()
fileptr1 = FakeFile(crypted_data)
chi_fileptr = chi_io.ChiAsFile(fileptr1, test_password)
result_data = chi_fileptr.read()
self.assertEqual(test_data, result_data)
self.assertRaises(IOError, chi_fileptr.write, test_data)
def test_filewrite_unicode(self):
test_data = u'Bj\N{LATIN SMALL LETTER O WITH DIAERESIS}rk is a great singer!'
test_password = b'mypassword'
output_encoding = 'utf8'
encoder, decoder, streamreader, streamwriter = codecs.lookup(output_encoding)
fileptr1 = FakeFile()
chi_fileptr = chi_io.ChiAsFile(fileptr1, test_password, 'w')
chi_fileptr = streamwriter(chi_fileptr)
chi_fileptr.write(test_data)
chi_fileptr.close()
crypted_data = fileptr1.getvalue()
# print 'crypted_data ', repr(crypted_data)
fileptr2 = FakeFile(crypted_data)
result_data = chi_io.read_encrypted_file(fileptr2, test_password)
# print repr(result_data)
result_data = result_data.decode(output_encoding)
self.assertEqual(test_data, result_data)
def test_fileread_unicode(self):
test_data = u'Bj\N{LATIN SMALL LETTER O WITH DIAERESIS}rk is a great singer!'
test_password = b'mypassword'
output_encoding = 'utf8'
encoder, decoder, streamreader, streamwriter = codecs.lookup(output_encoding)
fileptr1 = FakeFile()
chi_io.write_encrypted_file(
fileptr1, test_password, test_data.encode(output_encoding)
)
crypted_data = fileptr1.getvalue()
# print repr(crypted_data)
fileptr2 = FakeFile(crypted_data)
chi_fileptr = chi_io.ChiAsFile(fileptr2, test_password)
chi_fileptr = streamreader(chi_fileptr)
result_data = chi_fileptr.read()
# print repr(result_data)
self.assertEqual(test_data, result_data)
def test_file_bad_write_after_close(self):
test_data = b"this is just a small piece of text."
test_password = b'mypassword'
fileptr1 = FakeFile()
chi_fileptr = chi_io.ChiAsFile(fileptr1, test_password, 'w')
chi_fileptr.write(test_data)
#
chi_fileptr.close()
self.assertRaises(ValueError, chi_fileptr.write, test_data)
def test_file_bad_read_after_close(self):
test_data = b"this is just a small piece of text."
test_password = b'mypassword'
fileptr1 = FakeFile()
chi_io.write_encrypted_file(fileptr1, test_password, test_data)
crypted_data = fileptr1.getvalue()
fileptr1 = FakeFile(crypted_data)
chi_fileptr = chi_io.ChiAsFile(fileptr1, test_password)
result_data = chi_fileptr.read()
chi_fileptr.close()
self.assertEqual(test_data, result_data)
self.assertRaises(ValueError, chi_fileptr.read)
def test_readwrite_seek_middle(self): # FIXME / TODO seek in name but no seek operation in test
if using_cstring:
self.skip('cString does not support read and write on the same object')
test_data = b"this is just a small piece of text."
test_password = b'mypassword'
fileptr1 = FakeFile()
chi_io.write_encrypted_file(fileptr1, test_password, test_data)
crypted_data = fileptr1.getvalue()
# print repr(crypted_data)
fileptr1 = FakeFile(crypted_data)
chi_fileptr = chi_io.ChiAsFile(
fileptr1, test_password, '+'
) # NOTE + is Read and Write which Cpython 2.x cStringIO does not support, expect AttributeError: 'cStringIO.StringI' object has no attribute 'write'
chi_fileptr.write(b'text')
chi_fileptr.close()
crypted_result = fileptr1.getvalue()
fileptr2 = FakeFile(crypted_result)
chi_fileptr = chi_io.ChiAsFile(fileptr2, test_password)
result_data = chi_fileptr.read()
# print repr(result_data)
self.assertEqual(b"text is just a small piece of text.", result_data)
class TestCompatChiData(TestChiIOBase):
## Test that can read files generated from Windows Tombo
## http://tombo.sourceforge.jp/En/
password = b'password'
## encrypted data taken from Win32 Tombo
## password for below data is: 'password'
binary_data = b'BF01\xe6\x05\x00\x00\xb2\xdfB\xf2\x9d-]\x89c\x92|.\xae\x88\xaa\x96Oi\
\xd6\x1dOA\xb41_b\x9b\x84\xdeSS\x1c\xc1\xd6\x98\xcf\xd9e\x84\xff\xecm\
\n\x8a|\xb4\xc3\xf0\xda\xd2@\xecqDaE_\xc1x\xff!sP+]\xa8F\xe2\n\xd5\xd1ZL\
\x84Wh\x85\x9db\xc2\xf4\x14\x00\xccs+\xac\x0e\xe0a\xfb)\r\x04\x81\x7f\
\xcd#e\xfe_\xca\x90\x95`\xd7\xb2 ,~/q\xc1\xc5\x03\x84*G4\xa6Zf<-\xcc\
\x06MW>\xe7\xa6\xba\xe8\xdfQZdG)qwx\xf3\xd4%\xe7\x93\xccKL_L1\x13\xb6\
\xfa\xb2b\xd2\xcck\x14:9\xa4$\xb6\x06\x92\xccs.\x18V\xecH\x1e\xe7\x12\
\xbe\xe4{\xc1^v\xad\xd9\x99\xa89\xba\xc7\xa1\xfaFl\x82\xb1\x92kB\xf7\
\x86 \x13\xec\x89\xfd\xbfg/{\x82:\xb3\x1f\xf4s\x15}\xeb\x98\xa6\xf1\t\
\x07\xedm\xb8\x03\xff\xd6\x14_\x07\x98:\x88\xf6H\x16\xb5\x00 V\xc5<LV\
\xc89\xd4\xd3\xd8\xee\xec\\\x1c\xf7\xc4\x19\xb4\x1d\xdet\xcf\t\xcb{\n\n\
\xa9T\xd4\xb3R\xbev\xe9A\x98\xe1>yW\x91}s{$\x1c\xcf-h9\xbe\x05bX|\x18\
\xde\xb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c4bM\x9e\x17l\xf9\xb8C\xd2\
\xe0\x17\xb4E\x0e\xae.G\xde\xe2\x9e\xfc~\x1b\x8687\xd2!\xe7w\x95!t\
\xcfXN\xcf\xae\x81\xd7\xc1\xe9\x84(F\xc3i\x12\x12\x178\x06J\xd0G\xa2:\
\xb6\xf5\x94b+\x1c6\x97yvs\xb2\x1a\x93)\xf6V\xaf(\x02Y\xef\x05\x8f)\
\xa5\x1b\xd2y\x10vn\x9d\xf4\x0e\x1d\xff?\x94i\xcb\xc3\x1f!\xd5:\x96\xdb~\xad'
plain_text_data = b'''aesop\r\n\r\nThe Frogs Desiring a King\r\n\r\nThe Frogs were l\
iving as happy as could be in a marshy swamp that just suited the\
m; they went splashing about caring for nobody and nobody troubli\
ng with them. But some of them thought that this was not right, t\
hat they should have a king and a proper constitution, so they de\
termined to send up a petition to Jove to give them what they wan\
ted. "Mighty Jove," they cried, "send unto us a king that will ru\
le over us and keep us in order." Jove laughed at their croaking,\
and threw down into the swamp a huge Log, which came down splash\
ing into the swamp. The Frogs were frightened out of their lives \
by the commotion made in their midst, and all rushed to the bank \
to look at the horrible monster; but after a time, seeing that it\
did not move, one or two of the boldest of them ventured out tow\
ards the Log, and even dared to touch it; still it did not move. \
Then the greatest hero of the Frogs jumped upon the Log and comme\
nced dancing up and down upon it, thereupon all the Frogs came an\
d did the same; and for some time the Frogs went about their busi\
ness every day without taking the slightest notice of their new K\
ing Log lying in their midst. But this did not suit them, so they\
sent another petition to Jove, and said to him, "We want a real \
king; one that will really rule over us." Now this made Jove angr\
y, so he sent among them a big Stork that soon set to work gobbli\
ng them all up. Then the Frogs repented when too late.\r\n\r\nBet\
ter no rule than cruel rule.\r\n'''
class TestCompatChiIO(TestCompatChiData):
## Test file IO
def do_fileread(self, crypted_data, test_password, expected_plain_text_data):
fileptr2 = FakeFile(crypted_data)
chi_fileptr = chi_io.ChiAsFile(fileptr2, test_password)
result_data = chi_fileptr.read()
self.assertEqual(expected_plain_text_data, result_data)
def test_win32_compat_fileread(self):
test_password = self.password
self.do_fileread(self.binary_data, test_password, self.plain_text_data)
def test_seek_fileread(self):
test_password = self.password
crypted_data = self.binary_data
expected_plain_text_data = self.plain_text_data[30:]
fileptr2 = FakeFile(crypted_data)
chi_fileptr = chi_io.ChiAsFile(fileptr2, test_password)
chi_fileptr.seek(30)
result_data = chi_fileptr.read()
self.assertEqual(expected_plain_text_data, result_data)
def test_badpassword(self):
test_password = b'badpassword'
self.assertRaises(
chi_io.BadPassword,
self.do_fileread,
self.binary_data,
test_password,
self.plain_text_data,
)
self.assertRaises(
chi_io.ChiIO,
self.do_fileread,
self.binary_data,
test_password,
self.plain_text_data,
)
class TestCompatChiDecrypt(TestCompatChiData):
## Test file IO
# TODO clone approach in TestCompatChiIO
def test_in_memory_decrypt(self):
test_password = self.password
cipher = chi_io.PEP272LikeCipher(test_password)
result_data = cipher.decrypt(self.binary_data)
self.assertEqual(self.plain_text_data, result_data)
def test_in_memory_decrypt_badpassword(self):
test_password = b'badpassword'
cipher = chi_io.PEP272LikeCipher(test_password)
self.assertRaises(
chi_io.BadPassword,
cipher.decrypt,
self.binary_data
)
self.assertRaises(
chi_io.ChiIO, # base exception for CHI
cipher.decrypt,
self.binary_data
)
def test_in_memory_decrypt_badinput_empty(self):
test_password = b'badpassword'
test_data = b''
cipher = chi_io.PEP272LikeCipher(test_password)
self.assertRaises(
chi_io.UnsupportedFile,
cipher.decrypt,
test_data
)
self.assertRaises(
chi_io.ChiIO, # base exception for CHI
cipher.decrypt,
test_data
)
def test_in_memory_decrypt_badinput_junk(self):
test_password = b'badpassword'
test_data = b'JUNKDATEHEREAS'
cipher = chi_io.PEP272LikeCipher(test_password)
self.assertRaises(
chi_io.UnsupportedFile,
cipher.decrypt,
test_data
)
self.assertRaises(
chi_io.ChiIO, # base exception for CHI
cipher.decrypt,
test_data
)
def test_in_memory_decrypt_badinput_valid_bf01(self):
test_password = b'badpassword'
test_data = b'BF01JUNKDATEHEREAS'
cipher = chi_io.PEP272LikeCipher(test_password)
self.assertRaises(
chi_io.UnsupportedFile,
cipher.decrypt,
test_data
)
self.assertRaises(
chi_io.ChiIO, # base exception for CHI
cipher.decrypt,
test_data
)
def test_in_memory_decrypt_badinput_validstart_extra(self):
test_password = b'badpassword'
test_data = self.binary_data + b'BF01JUNKDATEHEREAS'
cipher = chi_io.PEP272LikeCipher(test_password)
self.assertRaises(
chi_io.UnsupportedFile,
cipher.decrypt,
test_data
)
self.assertRaises(
chi_io.ChiIO, # base exception for CHI
cipher.decrypt,
test_data
)
class TestCompatChiEncryptDecrypt(TestCompatChiData):
## in memory equiv of TestChiIO.test_get_what_you_put_in()
def test_get_what_you_put_in(self):
test_data = b"this is just a small piece of text."
test_password = b'mypassword'
cipher = chi_io.PEP272LikeCipher(test_password)
crypted_data = cipher.encrypt(test_data)
result_data = cipher.decrypt(crypted_data)
self.assertEqual(test_data, result_data)
def test_get_what_you_put_in_larger(self):
test_data = self.plain_text_data
test_password = self.password
cipher = chi_io.PEP272LikeCipher(test_password)
crypted_data = cipher.encrypt(test_data)
result_data = cipher.decrypt(crypted_data)
self.assertEqual(test_data, result_data)
def test_same_input_different_crypted_text(self):
test_data = b"this is just a small piece of text."
test_password = b'mypassword'
cipher = chi_io.PEP272LikeCipher(test_password)
crypted_data1 = cipher.encrypt(test_data)
crypted_data2 = cipher.encrypt(test_data)
self.assertNotEqual(crypted_data1, crypted_data2)
result_data = cipher.decrypt(crypted_data1)
self.assertEqual(test_data, result_data)
result_data = cipher.decrypt(crypted_data2)
self.assertEqual(test_data, result_data)
def test_unicode_strings_rejected(self):
test_data = u"this is just a small piece of text."
test_password = b'mypassword'
cipher = chi_io.PEP272LikeCipher(test_password)
self.assertRaises(
chi_io.ChiIO,
cipher.encrypt,
test_data
)
if __name__ == '__main__':
print(sys.version)
print(chi_io.implementation)
unittest.main()