changeset 7:65f72a842650

Added converter tests.
author Brian Neal <bgneal@gmail.com>
date Sun, 02 Jun 2013 19:21:11 -0500
parents c399a23e8f20
children b5e6552394a2
files m209/converter.py m209/tests/test_converter.py
diffstat 2 files changed, 141 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/m209/converter.py	Sun Jun 02 15:44:01 2013 -0500
+++ b/m209/converter.py	Sun Jun 02 19:21:11 2013 -0500
@@ -175,21 +175,3 @@
 
         return CIPHER_TABLE[(ord(c) - ord('A') - count) % 26]
 
-
-if __name__ == '__main__':
-
-    m209 = M209()
-    m209.set_drum_lugs('1-0 2-0*4 0-3 0-4*3 0-5*3 0-6*11 2-5 2-6 3-4 4-5')
-    m209.set_pins(0, 'BFJKLOSTUWXZ')
-    m209.set_pins(1, 'ABDJKLMORTUV')
-    m209.set_pins(2, 'EHJKNPQRSX')
-    m209.set_pins(3, 'ABCHIJLMPQR')
-    m209.set_pins(4, 'BCDGJLNOPQS')
-    m209.set_pins(5, 'AEFHIJP')
-    pt = 'A' * 26
-    print(m209.encrypt(pt))
-
-    ct = 'OZGPK AFVAJ JYRZW LRJEG MOVLU M'
-    m209.set_key_wheels('AAAAAA')
-    print(m209.decrypt(ct))
-
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/m209/tests/test_converter.py	Sun Jun 02 19:21:11 2013 -0500
@@ -0,0 +1,141 @@
+# Copyright (C) 2013 by Brian Neal.
+# This file is part of m209, the M-209 simulation.
+# m209 is released under the MIT License (see LICENSE.txt).
+
+"""test_converter.py - Unit tests for the M209 class for the M-209 simulation."""
+
+import unittest
+
+from .. import M209Error
+from ..converter import M209
+
+
+# Data taken from Mark J. Blair's AA key list
+AA_LUGS = '1-0*5 0-3*3 0-4 0-5*4 0-6*6 1-2 1-5*4 3-4 3-6 5-6'
+
+AA_PIN_LIST = [
+    'FGIKOPRSUVWYZ',
+    'DFGKLMOTUY',
+    'ADEFGIORTUVX',
+    'ACFGHILMRSU',
+    'BCDEFJKLPS',
+    'EFGHIJLMNP'
+]
+
+AA_CHECK = 'QLRRN TPTFU TRPTN MWQTV JLIJE J'
+
+
+class M209TestCase(unittest.TestCase):
+
+    def test_invalid_set_pins(self):
+        """Ensure invalid inputs raise errors."""
+        m = M209()
+        pins = 'BFJKLOSTUWXZ'
+        self.assertRaises(M209Error, m.set_pins, -1, pins)
+        self.assertRaises(M209Error, m.set_pins, -2, pins)
+        self.assertRaises(M209Error, m.set_pins, 6, pins)
+        self.assertRaises(M209Error, m.set_pins, 7, pins)
+        self.assertRaises(M209Error, m.set_pins, 100, pins)
+
+    def letter_check(self, lugs, pin_list, check):
+        """Generic letter check routine"""
+
+        pt = 'A' * 26
+        ct = check
+
+        m = M209()
+        m.set_drum_lugs(lugs)
+
+        for n, pins in enumerate(pin_list):
+            m.set_pins(n, pins)
+
+        result = m.encrypt(pt)
+
+        self.assertEqual(result, ct)
+        self.assertEqual(m.letter_counter, 26)
+
+        m.letter_counter = 0
+        m.set_key_wheels('A' * 6)
+        result = m.decrypt(ct)
+
+        self.assertEqual(result, pt)
+        self.assertEqual(m.letter_counter, 26)
+
+    def test_aa_letter_check(self):
+        """See if we can pass a letter check using Mark J. Blair's AA key list."""
+
+        self.letter_check(AA_LUGS, AA_PIN_LIST, AA_CHECK)
+
+    def test_yl_letter_check(self):
+        """See if we can pass a letter check using Mark J. Blair's YL key list."""
+
+        lugs = '1-0 2-0*4 0-3 0-4*3 0-5*3 0-6*11 2-5 2-6 3-4 4-5'
+
+        pin_list = [
+            'BFJKLOSTUWXZ',
+            'ABDJKLMORTUV',
+            'EHJKNPQRSX',
+            'ABCHIJLMPQR',
+            'BCDGJLNOPQS',
+            'AEFHIJP',
+        ]
+
+        check = 'OZGPK AFVAJ JYRZW LRJEG MOVLU M'
+        self.letter_check(lugs, pin_list, check)
+
+    def test_no_group(self):
+
+        m = M209()
+        m.set_drum_lugs(AA_LUGS)
+        for n, pins in enumerate(AA_PIN_LIST):
+            m.set_pins(n, pins)
+
+        result = m.encrypt('A' * 26, group=False)
+        expected = AA_CHECK.replace(' ', '')
+        self.assertEqual(result, expected)
+
+    def test_encrpyt_no_spaces(self):
+
+        m = M209()
+        self.assertRaises(M209Error, m.encrypt, 'ATTACK AT DAWN', spaces=False)
+
+    def test_encrypt_spaces(self):
+
+        m = M209()
+        m.set_drum_lugs(AA_LUGS)
+        for n, pins in enumerate(AA_PIN_LIST):
+            m.set_pins(n, pins)
+
+        wheels = 'YGXREL'
+        m.set_key_wheels(wheels)
+        result1 = m.encrypt('ATTACK AT DAWN')
+
+        m.set_key_wheels(wheels)
+        result2 = m.encrypt('ATTACKZATZDAWN', spaces=False)
+
+        m.set_key_wheels(wheels)
+        result3 = m.encrypt('ATTACKZATZDAWN', spaces=True)
+
+        self.assertTrue(result1 == result2 == result3)
+
+    def test_decrpyt_no_spaces(self):
+
+        m = M209()
+        self.assertRaises(M209Error, m.decrypt, 'ATTACK AT DAWN', spaces=False)
+
+    def test_decrypt_no_z_sub(self):
+
+        m = M209()
+        m.set_drum_lugs(AA_LUGS)
+        for n, pins in enumerate(AA_PIN_LIST):
+            m.set_pins(n, pins)
+
+        pt = 'ATTACK AT DAWN'
+        wheels = 'YGXREL'
+        m.set_key_wheels(wheels)
+        ct = m.encrypt(pt)
+
+        m.set_key_wheels(wheels)
+        result = m.decrypt(ct, z_sub=False)
+
+        self.assertEqual(pt.replace(' ', 'Z'), result)