def test_plugboard_raises_for_invalid_argument(self): """Test a plugboard raises for a connection has the wrong number of arguments.""" with self.assertRaises(InvalidPlugboard): Plugboard([("A")]) with self.assertRaises(InvalidPlugboard): Plugboard([("A", "B", "C")]) with self.assertRaises(InvalidPlugboard): Plugboard([("@", "B")])
def test_plugboard_encoding(self): """Test that the plugboard can encode values.""" board = Plugboard([("A", "Q"), ("N", "V")]) self.assertEqual(board.encode("A"), "Q") self.assertEqual(board.encode("Q"), "A") self.assertEqual(board.encode("N"), "V") self.assertEqual(board.encode("V"), "N") self.assertEqual(board.encode("L"), "L")
def test_unknown_chars(self): plugboard = Plugboard() plugboard.add(PlugLead("SZ")) plugboard.add(PlugLead("GT")) plugboard.add(PlugLead("DV")) plugboard.add(PlugLead("KU")) self.assertEqual(plugboard.encode("*"), "*") self.assertEqual(plugboard.encode(" "), " ") self.assertEqual(plugboard.encode("5"), "5")
def test_plug_board(self): plugboard = Plugboard() plugboard.add(PlugLead("SZ")) plugboard.add(PlugLead("GT")) plugboard.add(PlugLead("DV")) plugboard.add(PlugLead("KU")) self.assertEqual(plugboard.encode("K"), "U") self.assertEqual(plugboard.encode("A"), "A")
def get_enigma( self, rotors=[ { "wiring": "EKMFLGDQVZNTOWYHXUSPAIBRCJ", "position": "A", "ring_setting": 1, "turnover_positions": ["R"], }, { "wiring": "AJDKSIRUXBLHWTMCQGZNPYFVOE", "position": "A", "ring_setting": 1, "turnover_positions": ["F"], }, { "wiring": "BDFHJLCPRTXVZNYEIWGAKMUSQO", "position": "A", "ring_setting": 1, "turnover_positions": ["W"], }, ], reflector="YRUHQSLDPXNGOKMIEBFZCWVJAT", plugboard=[], ): """Test enigma object can be created.""" used_rotors = [ Rotor( wiring=_["wiring"], position=_["position"], ring_setting=_["ring_setting"], turnover_positions=_["turnover_positions"], ) for _ in rotors ] used_reflector = Reflector(wiring=reflector) used_plugboard = Plugboard(plugboard) return Enigma(rotors=used_rotors, reflector=used_reflector, plugboard=used_plugboard)
def __init__(self, *, rotors: Sequence[str], positions: Sequence[str], ring_settings: Sequence[str], plugboard_pairs: Sequence[Tuple[str, str]], reflector: str): """ Set up Enigma object. Kwargs: rotors: Iterable containing the names of the rotors to use. E.g. ('I', 'II', 'III'). positions: Iterable containng the initial letter positions of the rotors. E.g. 'AAA'. ring_settings: Iterable containing the ring settings of the rotors as str. E.g ('01', '01', '01') plugboard_pairs: Iterable containing two character strings of letter pairs for the plugboard. E.g. ['AB', 'QZ'] reflector: Name of the reflector to use. """ positions = positions ring_settings = ring_settings used_reflector = self.available_reflectors[reflector]() plugboard_pairs = plugboard_pairs used_rotors = [] for i in range(3): rotor_class = self.available_rotors[rotors[i]] rotor = rotor_class(position=positions[i], ring_setting=int(ring_settings[i])) used_rotors.append(rotor) plugboard = Plugboard(plugboard_pairs) super().__init__(rotors=used_rotors, reflector=used_reflector, plugboard=plugboard)
def test_empty_plug_board(self): plugboard = Plugboard() self.assertEqual(plugboard.encode("K"), "K") self.assertEqual(plugboard.encode("A"), "A")
def test_plugboard_raises_for_invalid_connection(self): """Test a plugboard cannot be created with an invalid setup.""" with self.assertRaises(InvalidPlugboard): Plugboard([("A", "A")])
def test_empty_plugboard(self): """Test empty plugboard does not change encoding.""" board = Plugboard([]) self.assertEqual(board.encode("A"), "A") self.assertEqual(board.encode("Q"), "Q")