changeset 12:7d32247eab5e

Got a test for the paper's example. It is failing. 4 letters are off. Hmm...
author Brian Neal <bgneal@gmail.com>
date Sun, 01 Dec 2013 16:56:45 -0600
parents d2c21b6c5bbc
children 2e3d919383dd
files purple/tests/test_machine.py
diffstat 1 files changed, 52 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/purple/tests/test_machine.py	Sat Nov 30 23:23:01 2013 -0600
+++ b/purple/tests/test_machine.py	Sun Dec 01 16:56:45 2013 -0600
@@ -91,3 +91,55 @@
         self.assertRaises(Purple97Error, Purple97.from_key_sheet, '1-9,2,20-a3')
         self.assertRaises(Purple97Error, Purple97.from_key_sheet, '1-9,2,20-1a')
         self.assertRaises(Purple97Error, Purple97.from_key_sheet, '1-9,2,20-123')
+
+    def test_first_part_of_14_part_message(self):
+
+        # The ciphertext contains garbles, indicated by '-' chars.
+        ciphertext = (
+            "ZTXODNWKCCMAVNZXYWEETUQTCIMN"
+            "VEUVIWBLUAXRRTLVARGNTPCNOIUP"
+            "JLCIVRTPJKAUHVMUDTHKTXYZELQTVWGBUHFAWSHU"
+            "LBFBHEXMYHFLOWD-KWHKKNXEBVPYHHGHEKXIOHQ"
+            "HUHWIKYJYHPPFEALNNAKIBOOZNFRLQCFLJTTSSDDOIOCVT-"
+            "ZCKQTSHXTIJCNWXOKUFNQR-TAOIHWTATWV"
+        )
+
+        ciphertext = """\
+ZTXOD NWKCC MAVNZ XYWEE TUQTC IMNVE UVIWB LUAXR RTLVA
+RGNTP CNOIU PJLCI VRTPJ KAUHV MUDTH KTXYZ ELQTV WGBUH FAWSH
+ULBFB HEXMY HFLOW D-KWH KKNXE BVPYH HGHEK XIOHQ HUHWI KYJYH
+PPFEA LNNAK IBOOZ NFRLQ CFLJT TSSDD OIOCV T-ZCK QTSHX TIJCN
+WXOKU FNQR- TAOIH WTATW V"""
+        ciphertext = ''.join(ciphertext.split())
+        self.assertEqual(216, len(ciphertext))
+
+        # Use 'X' in place of the garbles
+        input_text = ciphertext.replace('-', 'A')
+
+        plaintext = (
+            "FOVTATAKIDASINIMUIMINOMOXIWO"
+            "IRUBESIFYXXFCKZZRDXOOVBTNFYX"
+            "FAEMEMORANDUMFIOFOVOOMOJIBAKARIFYXRAICCY"
+            "LFCBBCFCTHEGOVE-NMENTOFJAPANLFLPROMPTED"
+            "BYAGENUINEDESIRETOCOMETOANAMICABLEUNDERSTANDIN-"
+            "WITHTHEGOVERNMENTOFTHE-NITEDSTATES"
+        )
+
+        # Decrypt
+        purple = Purple97.from_key_sheet(
+                switches='9-1,24,6-23',
+                alphabet='NOKTYUXEQLHBRMPDICJASVWGZF')
+
+        actual = purple.decrypt(input_text)
+        print(actual)
+
+        mismatches = []
+        for n, (a, b) in enumerate(zip(plaintext, actual)):
+            if a != b and a != '-':
+                mismatches.append(n)
+
+        print("There are {} mismatches".format(len(mismatches)))
+        if mismatches:
+            print("mismatches = {}".format(mismatches))
+
+        self.assertTrue(len(mismatches) == 0)