1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
# Copyright (C) 2012 by Brian Neal.
# This file is part of Py-Enigma, the Enigma Machine simulation.
# Py-Enigma is released under the MIT License (see License.txt).
"""test_rotor.py - Unit tests for the Rotor class for the Enigma simulation."""
import unittest
import collections
import string
from ..rotors.rotor import Rotor, ALPHA_LABELS
from ..rotors import RotorError
from ..rotors.data import ROTORS
from ..rotors.factory import create_rotor
WIRING = 'EKMFLGDQVZNTOWYHXUSPAIBRCJ'
class SimpleRotorTestCase(unittest.TestCase):
"""Basic tests to verify Rotor functionality"""
def test_bad_wiring(self):
self.assertRaises(RotorError, Rotor, 'I', '')
self.assertRaises(RotorError, Rotor, 'I', 'ABC')
self.assertRaises(RotorError, Rotor, 'I', '123')
w = string.punctuation[:26]
self.assertRaises(RotorError, Rotor, 'I', w)
w = 'ABCD' * 7
self.assertRaises(RotorError, Rotor, 'III', w[:26])
def test_bad_ring_setting(self):
self.assertRaises(RotorError, Rotor, 'I', WIRING, ring_setting=-1)
self.assertRaises(RotorError, Rotor, 'I', WIRING, ring_setting=26)
self.assertRaises(RotorError, Rotor, 'I', WIRING, ring_setting='A')
self.assertRaises(RotorError, Rotor, 'I', WIRING, ring_setting=None)
def test_bad_stepping(self):
self.assertRaises(RotorError, Rotor, 'I', WIRING, stepping="0")
self.assertRaises(RotorError, Rotor, 'I', WIRING, stepping="A0")
self.assertRaises(RotorError, Rotor, 'I', WIRING, stepping=[1])
self.assertRaises(RotorError, Rotor, 'I', WIRING, stepping=['A', '%', '14'])
self.assertRaises(RotorError, Rotor, 'I', WIRING, stepping=('A', '%', '14'))
def test_display(self):
for r in range(26):
rotor = Rotor('I', WIRING, ring_setting=r)
for s in ALPHA_LABELS:
rotor.set_display(s)
self.assertEqual(s, rotor.get_display())
def test_wiring(self):
"""Loop through all ring settings & rotor positions and test the
wiring.
"""
for r in range(26):
rotor = Rotor('I', WIRING, ring_setting=r)
for n, d in enumerate(ALPHA_LABELS):
rotor.set_display(d)
wiring = collections.deque(WIRING)
wiring.rotate(r - n)
for i in range(26):
output = rotor.signal_in(i)
expected = (ord(wiring[i]) - ord('A') + r - n) % 26
self.assertEqual(output, expected)
output = rotor.signal_out(expected)
self.assertEqual(output, i)
def test_notches(self):
"""For every rotor we simulate, ensure that the notch setting is correct
regardless of the ring setting.
"""
rotor_names = ROTORS.keys()
for rotor_name in rotor_names:
notches = ROTORS[rotor_name]['stepping']
if notches is None:
continue
for r in range(26):
rotor = create_rotor(rotor_name, r)
rotor.set_display('A')
for n in range(26):
over_notch = rotor.get_display() in notches
self.assertEqual(rotor.notch_over_pawl(), over_notch)
rotor.rotate()
def test_rotate(self):
for r in range(26):
rotor1 = Rotor('X', WIRING, ring_setting=r)
rotor2 = Rotor('Y', WIRING, ring_setting=r)
rotor2.set_display('A')
for i, d in enumerate(ALPHA_LABELS):
rotor1.set_display(d)
self.assertEqual(rotor1.get_display(), rotor2.get_display())
rotor2.rotate()
|