def test_constants_Notches(): assert ROTOR_NOTCHES == { 'I': 'Q', # Next rotor steps when I moves from Q -> R 'II': 'E', # Next rotor steps when II moves from E -> F 'III': 'V', # Next rotor steps when III moves from V -> W 'V': 'Z' # Next rotor steps when V moves from Z -> A } enig = Enigma('ZAP') encoding = enig.encipher('VNLAZTKMCJQCNTTZMOXQ') assert ROTOR_NOTCHES == { 'I': 'Q', # Next rotor steps when I moves from Q -> R 'II': 'E', # Next rotor steps when II moves from E -> F 'III': 'V', # Next rotor steps when III moves from V -> W 'V': 'Z' # Next rotor steps when V moves from Z -> A } decipher = enig.decipher(encoding) assert ROTOR_NOTCHES == { 'I': 'Q', # Next rotor steps when I moves from Q -> R 'II': 'E', # Next rotor steps when II moves from E -> F 'III': 'V', # Next rotor steps when III moves from V -> W 'V': 'Z' # Next rotor steps when V moves from Z -> A }
def test_Enigma_rotor_pos_ZAF_no_print(): enig = Enigma() enig.set_rotor_position('ZAF', printIt=False) assert enig.key == 'ZAF' assert enig.l_rotor.window == 'Z' assert enig.m_rotor.window == 'A' assert enig.r_rotor.window == 'F'
def test_constants_Alphabet(): assert ALPHABET == 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' enig = Enigma('ZAP') encoding = enig.encipher('VNLAZTKMCJQCNTTZMOXQ') assert ALPHABET == 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' decipher = enig.decipher(encoding) assert ALPHABET == 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
def test_check_rotors(): enig = Enigma('ZAP') l_rotor = enig.l_rotor m_rotor = enig.m_rotor r_rotor = enig.r_rotor encoding = enig.encipher('VNLAZTKMCJQCNTTZMOXQ') assert l_rotor.wiring == enig.l_rotor.wiring assert m_rotor.wiring == enig.m_rotor.wiring assert r_rotor.wiring == enig.r_rotor.wiring
def test_double_decipher2(): enig = Enigma('ZAP') encoding = enig.encipher('VNLAZTKMCJQCN TTZMOXQ') encoding2 = enig.encipher('SHTMVEUVNYPL SGYBXMWJ') enig = Enigma('ZAP') assert enig.decipher(encoding) == 'VNLAZTKMCJQCNTTZMOXQ' assert enig.decipher(encoding2) == 'SHTMVEUVNYPLSGYBXMWJ'
def test_Enigma_repr(): enig = Enigma() ret = enig.__repr__() assert ret == 'Key: AAA' enig = Enigma() enig.set_rotor_position('ZAF', printIt=True) ret = enig.__repr__() assert ret == 'Key: ZAF'
def test_Enigma_set_rotor_order_scramble2(): enig = Enigma(key='ZAF') enig.set_rotor_order(['II', 'III', 'I']) assert enig.l_rotor.wiring == { 'forward': 'AJDKSIRUXBLHWTMCQGZNPYFVOE', 'backward': 'AJPCZWRLFBDKOTYUQGENHXMIVS' } assert enig.m_rotor.wiring == { 'forward': 'BDFHJLCPRTXVZNYEIWGAKMUSQO', 'backward': 'TAGBPCSDQEUFVNZHYIXJWLRKOM' } assert enig.r_rotor.wiring == { 'forward': 'EKMFLGDQVZNTOWYHXUSPAIBRCJ', 'backward': 'UWYGADFPVZBECKMTHXSLRINQOJ' } assert enig.l_rotor.window == 'Z' assert enig.m_rotor.window == 'A' assert enig.r_rotor.window == 'F'
def test_Enigma_rotor_pos_not_str_or_not_len3(): enig = Enigma() ret = enig.set_rotor_position(123) assert ret == None enig = Enigma() ret = enig.set_rotor_position('A') assert ret == None
def test_Enigma_encode_decode_letter_inornot_plug(): enig = Enigma(swaps=[('A', 'S')]) ret = enig.encode_decode_letter('A') assert ret == 'J' enig = Enigma() ret = enig.encode_decode_letter('H') assert ret == 'I'
def test_stress(): enig = Enigma('ZAP') encoding = enig.encipher('VNLAZTKMCJQCNTTZMOXQ' * 10000) assert enig.l_rotor.window == 'V' assert enig.r_rotor.window == 'X' assert enig.m_rotor.window == 'T' enig = Enigma('ZAP') decipher = enig.decipher(encoding) assert decipher == 'VNLAZTKMCJQCNTTZMOXQ' * 10000 assert enig.l_rotor.window == 'V' assert enig.r_rotor.window == 'X' assert enig.m_rotor.window == 'T'
def test_stress2(): enig = Enigma(key="ZQE", rotor_order=['V', 'I', 'II'], swaps=['AB', 'PO', 'ES']) encoding = enig.encipher('VNLAZTKMCJQCNTTZMOXQ' * 10000) assert enig.l_rotor.window == 'V' assert enig.r_rotor.window == 'M' assert enig.m_rotor.window == 'I' enig = Enigma(key="ZQE", rotor_order=['V', 'I', 'II'], swaps=['AB', 'PO', 'ES']) decipher = enig.decipher(encoding) assert decipher == 'VNLAZTKMCJQCNTTZMOXQ' * 10000 assert enig.l_rotor.window == 'V' assert enig.r_rotor.window == 'M' assert enig.m_rotor.window == 'I'
def test_Enigma_set_plugs_none(): enig = Enigma() with pytest.raises(TypeError): ret = enig.set_plugs()
def test_double_decipher(): enig = Enigma('ZAP') encoding = enig.encipher('VNLAZTKMCJQCNTTZMOXQ') decipher = enig.decipher(encoding) decipher2 = enig.decipher(encoding) assert decipher == 'TCXTTJYXJBSQMJZGQACG' assert decipher2 == 'VOGVFJIRTYWKMXYHDXUN' enig = Enigma('ZAP') assert enig.decipher(encoding) == 'VNLAZTKMCJQCNTTZMOXQ' assert enig.encipher(decipher) == encoding assert enig.encipher(decipher2) == encoding enig = Enigma() encoding = enig.encipher('VNLAZTKMCJQCNTTZMOXQ') decipher = enig.decipher(encoding) decipher2 = enig.decipher(encoding) assert decipher == 'PRMRMJXFVLKDSJCCJUIF' assert decipher2 == 'TJJXBBNBMQWRXQSZFMRU' enig = Enigma() assert enig.decipher(encoding) == 'VNLAZTKMCJQCNTTZMOXQ' assert enig.encipher(decipher) == encoding assert enig.encipher(decipher2) == encoding
def test_Enigma_encipher_ints(): enig = Enigma() ret = enig.encipher('123') assert ret == 'Please provide a string containing only the characters a-zA-Z and spaces.'
def test_Enigma_encipher_message(): enig = Enigma() ret = enig.encipher('where in the world is carmen sandiago') assert ret == 'KPCURLRRTCCHIYPKUYPVAPZPFGBVCPG'
def test_Enigma_decipher_message(): enig = Enigma() ret = enig.decipher('KPCURLRRTCCHIYPKUYPVAPZPFGBVCPG') assert ret == 'WHEREINTHEWORLDISCARMENSANDIAGO'
def test_constants_wirings(): assert ROTOR_WIRINGS == { 'I': { 'forward': 'EKMFLGDQVZNTOWYHXUSPAIBRCJ', 'backward': 'UWYGADFPVZBECKMTHXSLRINQOJ' }, 'II': { 'forward': 'AJDKSIRUXBLHWTMCQGZNPYFVOE', 'backward': 'AJPCZWRLFBDKOTYUQGENHXMIVS' }, 'III': { 'forward': 'BDFHJLCPRTXVZNYEIWGAKMUSQO', 'backward': 'TAGBPCSDQEUFVNZHYIXJWLRKOM' }, 'V': { 'forward': 'VZBRGITYUPSDNHLXAWMJQOFECK', 'backward': 'QCYLXWENFTZOSMVJUDKGIARPHB' } } enig = Enigma('ZAP') encoding = enig.encipher('VNLAZTKMCJQCNTTZMOXQ') assert ROTOR_WIRINGS == { 'I': { 'forward': 'EKMFLGDQVZNTOWYHXUSPAIBRCJ', 'backward': 'UWYGADFPVZBECKMTHXSLRINQOJ' }, 'II': { 'forward': 'AJDKSIRUXBLHWTMCQGZNPYFVOE', 'backward': 'AJPCZWRLFBDKOTYUQGENHXMIVS' }, 'III': { 'forward': 'BDFHJLCPRTXVZNYEIWGAKMUSQO', 'backward': 'TAGBPCSDQEUFVNZHYIXJWLRKOM' }, 'V': { 'forward': 'VZBRGITYUPSDNHLXAWMJQOFECK', 'backward': 'QCYLXWENFTZOSMVJUDKGIARPHB' } } decipher = enig.decipher(encoding) assert ROTOR_WIRINGS == { 'I': { 'forward': 'EKMFLGDQVZNTOWYHXUSPAIBRCJ', 'backward': 'UWYGADFPVZBECKMTHXSLRINQOJ' }, 'II': { 'forward': 'AJDKSIRUXBLHWTMCQGZNPYFVOE', 'backward': 'AJPCZWRLFBDKOTYUQGENHXMIVS' }, 'III': { 'forward': 'BDFHJLCPRTXVZNYEIWGAKMUSQO', 'backward': 'TAGBPCSDQEUFVNZHYIXJWLRKOM' }, 'V': { 'forward': 'VZBRGITYUPSDNHLXAWMJQOFECK', 'backward': 'QCYLXWENFTZOSMVJUDKGIARPHB' } }
def test_Enigma_set_plugs(): enig = Enigma() ret = enig.set_plugs(['EF', 'GH'], True) assert ret == None
def test_Enigma_init_short_key(): with pytest.raises(ValueError): enig = Enigma('AA') enig = Enigma('AAAAAA')
def valid_enigma(self): return Enigma(key='AAA', swaps=['AB', 'CD'], rotor_order=['I', 'II', 'III'])
def test_Enigma_encode_decode_letter_int(): enig = Enigma() ret = enig.encode_decode_letter('1') assert ret == 'Please provide a letter in a-zA-Z.'
def test_Enigma_encode_decode_letter_final_in_Plug(): enig = Enigma(swaps=[('I', 'N')]) ret = enig.encode_decode_letter('H') assert ret == 'N'
def test_invalid_key(self): with pytest.raises(ValueError) as err: Enigma(key='AA', swaps=None, rotor_order=['I', 'II', 'III']) print(err)
def test_Enigma_init(): enig = Enigma() assert enig.key == 'AAA' assert enig.rotor_order == ['I', 'II', 'III']