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_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)) 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)) 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) 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) 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) TEST_PADLINE = 'HUCHUGGHOBUXADDHOHPGQBKXQKAOLRL' TEST_TEXTLINE = 'THISCAKEISUNSATISFACTORYTOMEYES' TEST_CIPHERLINE = 'BCMAXHRNXUPLTEXRGOQKKQBWKYNTKWD' TEST_MSG = \ 'HOWMUCHCHUCKCOULDAWOODCHUCKCHUCKIFAWOODCHUCKCOULDCHUCKWOOD' \ 'HOWMUCHCHUCKCOULDAWOODCHUCKCHUCKIFAWOODCHUCKCOULDCHUCKWOOD' \ 'HOWMUCHCHUCKCOULDAWOODCHUCKCHUCKIFAWOODCHUCKCOULDCHUCKWOOD' \ 'HOWMUCHCHUCKCOULDAWOODCHUCKCHUCKIFAWOODCHUCKCOULDCHUCKWOOD' \ 'HOWMUCHCHUCKCOULDAWOODCHUCKCHUCKIFAWOODCHUCKCOULDCHUCKWOOD' \ 'HOWMUCHCHUCKCOULDAWOODCHUCKCHUCKIFAWOODCHUCKCOULDCHUCKWOOD' \ 'HOWMUCHCHUCKCOULDAWOODCHUCKCHUCKIFAWOODCHUCKCOULDCHUCKWOOD' \ 'HOWMUCHCHUCKCOULDAWOODCHUCKCHUC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YCFMVZKUTVKIPXBKGLFATSDCMYLPYLSTPBARVELNSOUFPZMUTRIRNZLYMDITNXMKLGE QXYRYEAOHGETEQMKTUMARPYMPKONUKYZIHTFGNHIKVOLTLRPMPNFSDHWQGVHBNDCWZCLUHQW GIFBFZKZOFZRRYCBOSBWSNVFXXPSAGOUOXDNBNSOZADYYSKWQUTYNCDNGVLSZNDBCTSIYOUX NNUGLZNKNDSYGRUCXTKLBIMXZMMZAEHVRFYMBFKVDPAEHOYCBXKADZOTRRMULOPAIKCIHKFY BQKKMEXUPLMTKGFIMPMTDVRZEBFEFARDEVQADVKIVNBHNZLEAAGLFIKXHTQWXBPNLXZHFUDZ XYLLVFETIDCYAXGTIHUFUSFMMWTLKZARVQOFGEBKIGWHFFFFKTHPQOMVAVILFLWMRLWPBWVW PFKSLFFWKYQVPHVPQTTVFRLQXHFMGVXVHTNSVZQSETKHXAPPSLHAIAVXWGDFVKOBQLEWPAZY WELQRIFCGSOVFSLYXMSETCTROLNKMDWZSGKREFEPEHCOKSMRTNCDSTHCSQKMKCBYEWZSQHPK UNWWUBKICDYYOSIZWKWRLGGIMYWKMDXOUKYHCUYCACKSFRWWXADQUZTCNYEYCSQEBTCZMSCV NTGCDGFUUCTQWINV........................................................ """ if __name__ == '__main__': unittest.main()