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.
box-o-sand/test_onetimepad.py

59 lines
2.3 KiB

import unittest
import onetimepad as OT
class TestOneTimePad(unittest.TestCase):
msg = ('HOWMUCHCHUCKCOULDAWOODCHUCKCHUCKIFA'
'WOODCHUCKCOULDCHUCKWOOD' * 50)
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(2000, width=width)
lines = [line.strip('.') for line in pad.split('\n\n\n')]
self.assertEqual(2000, len(''.join(lines)))
def test_two_out_of_every_three_lines_are_empty_on_new_pad(self):
pad = OT.create_pad(2000)
for lineno, line in enumerate(pad.splitlines()):
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_cypherline(lineno):
self.assertFalse(bool(len(line)),
'cypher 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_cypherline(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()