You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
2.5 KiB
64 lines
2.5 KiB
import unittest
|
|
import onetimepad as OT
|
|
|
|
|
|
class TestOneTimePad(unittest.TestCase):
|
|
msg = ('HOWMUCHCHUCKCOULDAWOODCHUCKCHUCKIFA'
|
|
'WOODCHUCKCOULDCHUCKWOOD' * 50)
|
|
_padsize = 2000
|
|
|
|
def test_mk_as_alpha_excludes_j(self):
|
|
self.assertTrue('J' not in OT._AS_ALPHA.values())
|
|
|
|
def test_mk_as_alpha_dict_has_25_members(self):
|
|
self.assertEqual(25, len(OT._AS_ALPHA.items()))
|
|
|
|
def test_creates_pad_of_desired_length(self):
|
|
for width in (72, 33, 99, 111):
|
|
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))
|
|
|
|
def test_two_out_of_every_three_lines_are_empty_on_new_pad(self):
|
|
pad = OT.create_pad_lines(2000)
|
|
for lineno, line in enumerate(pad):
|
|
line = line.strip()
|
|
if OT._is_padline(lineno):
|
|
self.assertTrue(bool(len(line)),
|
|
'pad line {0} is non-empty'.format(lineno))
|
|
elif OT._is_txtline(lineno):
|
|
self.assertFalse(bool(len(line)),
|
|
'text line {0} is empty'.format(lineno))
|
|
elif OT._is_cipherline(lineno):
|
|
self.assertFalse(bool(len(line)),
|
|
'cipher line {0} is empty'.format(lineno))
|
|
else:
|
|
raise NotImplementedError(lineno)
|
|
|
|
def test_padfill_leaves_every_third_line_empty(self):
|
|
msg = self.msg[:]
|
|
pad = OT.create_pad(len(msg))
|
|
filled = OT._padfill(msg, pad)
|
|
self.assertTrue(bool(filled))
|
|
for lineno, line in enumerate(filled):
|
|
line = line.strip()
|
|
if OT._is_cipherline(lineno):
|
|
self.assertFalse(bool(len(line)),
|
|
'line {0} is empty'.format(lineno))
|
|
elif OT._is_txtline(lineno):
|
|
self.assertTrue(bool(len(line)),
|
|
'text line {0} is non-empty'.format(lineno))
|
|
elif OT._is_padline(lineno):
|
|
self.assertTrue(bool(len(line)),
|
|
'pad line {0} is non-empty'.format(lineno))
|
|
else:
|
|
raise NotImplementedError(lineno)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|