def test_enigma_different_grundstellung(self): self.rotors = ( Walzen(wiring='EKMFLGDQVZNTOWYHXUSPAIBRCJ', notch='Q'), Walzen(wiring='AJDKSIRUXBLHWTMCQGZNPYFVOE', notch='E'), Walzen(wiring='BDFHJLCPRTXVZNYEIWGAKMUSQO', notch='V'), ) machine = Enigma(rotors=self.rotors[::-1], reflector=self.reflector, grundstellung='BBB') self.assertEqual('PGQPW', machine.cipher('AAAAA'))
def test_default_encode_decode(self): ''' Creates a 20 char long word. Cipher and decipher this message with default parameters. If ciphered and original word different and deciphered and original word is same as deciphered word, test is passed. ''' all_letters = string.ascii_letters + '''0 123/45\t6\n789.,?!:"';''' cipher = Enigma() for _ in range(50): word = random.choices(all_letters, k=20) word = ''.join(word) encoded = cipher.cipher(word) decoded = cipher.decipher(encoded) self.assertNotEqual(encoded, word) self.assertEqual(word, decoded)
def test_offsets_encode_decode(self): ''' Creates a 20 char long word. Cipher and decipher this message with random offset selections. If ciphered and original word different and deciphered and original word is same as deciphered word, test is passed. ''' all_letters = string.ascii_letters + '''0 123/45\t6\n789.,?!:"';''' for _ in range(50): offsets = random.choices(range(len(all_letters)), k=3) cipher = Enigma(offsets=offsets) word = random.choices(all_letters, k=20) word = ''.join(word) encoded = cipher.cipher(word) decoded = cipher.decipher(encoded) self.assertNotEqual(encoded, word) self.assertEqual(word, decoded)
def test_enigma_uses_plugboard(self): machine = Enigma(rotors=self.rotors[::-1], reflector=self.reflector, plugboard=self.plugboard) self.assertEqual('GCZBP', machine.cipher('AAAAA'))
def test_enigma_full_cycle(self): machine = Enigma(rotors=self.rotors[::-1], reflector=self.reflector) machine.cipher('A' * 16900) # this should do a full cycle on rotors self.assertEqual('BDZGO', machine.cipher('AAAAA'))
def test_enigma_decipher(self): machine = Enigma(rotors=self.rotors[::-1], reflector=self.reflector) self.assertEqual('AAAAA', machine.cipher('BDZGO'))