2009-11-25 22:10:32 +00:00
|
|
|
import unittest
|
|
|
|
import onetimepad as OT
|
|
|
|
|
|
|
|
|
|
|
|
class TestOneTimePad(unittest.TestCase):
|
2009-11-26 04:12:55 +00:00
|
|
|
msg = ('HOWMUCHCHUCKCOULDAWOODCHUCKCHUCKIFA'
|
|
|
|
'WOODCHUCKCOULDCHUCKWOOD' * 50)
|
2009-11-26 13:44:05 +00:00
|
|
|
_padsize = 2000
|
2009-11-25 22:10:32 +00:00
|
|
|
|
2009-11-26 01:22:41 +00:00
|
|
|
def test_mk_as_alpha_excludes_j(self):
|
|
|
|
self.assertTrue('J' not in OT._AS_ALPHA.values())
|
2009-11-25 22:10:32 +00:00
|
|
|
|
2009-11-26 01:22:41 +00:00
|
|
|
def test_mk_as_alpha_dict_has_25_members(self):
|
|
|
|
self.assertEqual(25, len(OT._AS_ALPHA.items()))
|
2009-11-25 22:10:32 +00:00
|
|
|
|
2009-11-26 01:22:41 +00:00
|
|
|
def test_creates_pad_of_desired_length(self):
|
|
|
|
for width in (72, 33, 99, 111):
|
2009-11-26 13:44:05 +00:00
|
|
|
pad = OT.create_pad(self._padsize, width=width)
|
|
|
|
lines = [line.strip('.') for line in pad.split()]
|
|
|
|
actual = len(''.join(lines))
|
|
|
|
self.assertEqual(self._padsize, len(''.join(lines)),
|
|
|
|
'pad of {0} chars created at width '
|
|
|
|
'{1}, actual={2}'.format(self._padsize,
|
|
|
|
width, actual))
|
2009-11-26 01:53:04 +00:00
|
|
|
|
2009-11-28 04:52:06 +00:00
|
|
|
def test_get_randint_on_nonexistent_randomness_file_fails_ioerror(self):
|
|
|
|
OT._DEV_RANDOM['fp'] = None
|
|
|
|
self.assertRaises(IOError, OT._get_randint,
|
|
|
|
randomness_file='/foo/bar/busted/borken',
|
|
|
|
fallback_to_fake=False)
|
|
|
|
|
|
|
|
def test_get_randint_on_nonexistent_randomness_file_uses_fake(self):
|
|
|
|
OT._DEV_RANDOM['fp'] = None
|
|
|
|
random_int = OT._get_randint(randomness_file='/broke/as/joke')
|
|
|
|
self.assertTrue(random_int in range(0, 255))
|
|
|
|
|
2009-11-27 18:53:24 +00:00
|
|
|
def test_is_padline(self):
|
|
|
|
for lineno in PADLINES:
|
|
|
|
self.assertTrue(OT._is_padline(lineno),
|
|
|
|
'line {0} is padline'.format(lineno))
|
|
|
|
for lineno in TEXTLINES:
|
|
|
|
self.assertFalse(OT._is_padline(lineno),
|
|
|
|
'line {0} is not padline'.format(lineno))
|
|
|
|
for lineno in CIPHERLINES:
|
|
|
|
self.assertFalse(OT._is_padline(lineno),
|
|
|
|
'line {0} is not padline'.format(lineno))
|
|
|
|
|
|
|
|
def test_is_textline(self):
|
|
|
|
for lineno in TEXTLINES:
|
|
|
|
self.assertTrue(OT._is_textline(lineno),
|
|
|
|
'line {0} is textline'.format(lineno))
|
|
|
|
for lineno in PADLINES:
|
|
|
|
self.assertFalse(OT._is_textline(lineno),
|
|
|
|
'line {0} is not textline'.format(lineno))
|
|
|
|
for lineno in CIPHERLINES:
|
|
|
|
self.assertFalse(OT._is_textline(lineno),
|
|
|
|
'line {0} is not textline'.format(lineno))
|
|
|
|
|
|
|
|
def test_is_cipherline(self):
|
|
|
|
for lineno in CIPHERLINES:
|
|
|
|
self.assertTrue(OT._is_cipherline(lineno),
|
|
|
|
'line {0} is cipherline'.format(lineno))
|
|
|
|
for lineno in PADLINES:
|
|
|
|
self.assertFalse(OT._is_cipherline(lineno),
|
|
|
|
'line {0} is not cipherline'.format(lineno))
|
|
|
|
for lineno in TEXTLINES:
|
|
|
|
self.assertFalse(OT._is_cipherline(lineno),
|
|
|
|
'line {0} is not cipherline'.format(lineno))
|
|
|
|
|
2009-11-27 19:40:54 +00:00
|
|
|
def test_make_cipherline_from_padline_and_textline(self):
|
|
|
|
actual = OT._cipherline_from_padline_and_textline(TEST_PADLINE,
|
|
|
|
TEST_TEXTLINE)
|
|
|
|
self.assertEqual(TEST_CIPHERLINE, actual)
|
|
|
|
|
|
|
|
def test_make_textline_from_cipherline_and_padline(self):
|
|
|
|
actual = OT._textline_from_cipherline_and_padline(TEST_CIPHERLINE,
|
|
|
|
TEST_PADLINE)
|
|
|
|
self.assertEqual(TEST_TEXTLINE, actual)
|
|
|
|
|
2009-11-27 20:33:02 +00:00
|
|
|
def test_encode_equals_expected(self):
|
|
|
|
ciphertext = OT.encode(TEST_MSG, TEST_PAD)
|
|
|
|
self.assertEqual(TEST_CIPHER, ciphertext)
|
|
|
|
|
|
|
|
def test_decode_equals_expected(self):
|
|
|
|
text = OT.decode(TEST_CIPHER, TEST_PAD)
|
|
|
|
self.assertEqual(TEST_MSG, text)
|
|
|
|
|
2009-11-27 18:53:24 +00:00
|
|
|
|
|
|
|
PADLINES = (1, 4, 7, 10, 13, 16, 19, 22, 25)
|
|
|
|
TEXTLINES = (2, 5, 8, 11, 14, 17, 20, 23, 26)
|
|
|
|
CIPHERLINES = (3, 6, 9, 12, 15, 18, 21, 24, 27)
|
2009-11-26 01:54:37 +00:00
|
|
|
|
2009-11-27 19:40:54 +00:00
|
|
|
TEST_PADLINE = 'HUCHUGGHOBUXADDHOHPGQBKXQKAOLRL'
|
|
|
|
TEST_TEXTLINE = 'THISCAKEISUNSATISFACTORYTOMEYES'
|
|
|
|
TEST_CIPHERLINE = 'BCMAXHRNXUPLTEXRGOQKKQBWKYNTKWD'
|
2009-11-26 01:54:37 +00:00
|
|
|
|
2009-11-27 20:33:02 +00:00
|
|
|
TEST_MSG = \
|
|
|
|
'HOWMUCHCHUCKCOULDAWOODCHUCKCHUCKIFAWOODCHUCKCOULDCHUCKWOOD' \
|
|
|
|
'HOWMUCHCHUCKCOULDAWOODCHUCKCHUCKIFAWOODCHUCKCOULDCHUCKWOOD' \
|
|
|
|
'HOWMUCHCHUCKCOULDAWOODCHUCKCHUCKIFAWOODCHUCKCOULDCHUCKWOOD' \
|
|
|
|
'HOWMUCHCHUCKCOULDAWOODCHUCKCHUCKIFAWOODCHUCKCOULDCHUCKWOOD' \
|
|
|
|
'HOWMUCHCH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
|
|
|
|
|
|
|
|
|
|
|
|
XYLLVFETIDCYAXGTIHUFUSFMMWTLKZARVQOFGEBKIGWHFFFFKTHPQOMVAVILFLWMRLWPBWVW
|
|
|
|
|
|
|
|
|
|
|
|
PFKSLFFWKYQVPHVPQTTVFRLQXHFMGVXVHTNSVZQSETKHXAPPSLHAIAVXWGDFVKOBQLEWPAZY
|
|
|
|
|
|
|
|
|
|
|
|
WELQRIFCGSOVFSLYXMSETCTROLNKMDWZSGKREFEPEHCOKSMRTNCDSTHCSQKMKCBYEWZSQHPK
|
|
|
|
|
|
|
|
|
|
|
|
UNWWUBKICDYYOSIZWKWRLGGIMYWKMDXOUKYHCUYCACKSFRWWXADQUZTCNYEYCSQEBTCZMSCV
|
|
|
|
|
|
|
|
|
|
|
|
NTGCDGFUUCTQWINV........................................................
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2009-11-26 01:54:37 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|