示例#1
0
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
    }
示例#2
0
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'
示例#3
0
def test_constants_Alphabet():
    assert ALPHABET == 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

    enig = Enigma('ZAP')
    encoding = enig.encipher('VNLAZTKMCJQCNTTZMOXQ')

    assert ALPHABET == 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

    decipher = enig.decipher(encoding)

    assert ALPHABET == 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
示例#4
0
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
示例#5
0
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'
示例#6
0
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'
示例#7
0
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'
示例#8
0
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
示例#9
0
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'
示例#10
0
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'
示例#11
0
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'
示例#12
0
def test_Enigma_set_plugs_none():
    enig = Enigma()
    with pytest.raises(TypeError):
        ret = enig.set_plugs()
示例#13
0
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
示例#14
0
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.'
示例#15
0
def test_Enigma_encipher_message():
    enig = Enigma()
    ret = enig.encipher('where in the world is carmen sandiago')
    assert ret == 'KPCURLRRTCCHIYPKUYPVAPZPFGBVCPG'
示例#16
0
def test_Enigma_decipher_message():
    enig = Enigma()
    ret = enig.decipher('KPCURLRRTCCHIYPKUYPVAPZPFGBVCPG')
    assert ret == 'WHEREINTHEWORLDISCARMENSANDIAGO'
示例#17
0
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'
        }
    }
示例#18
0
def test_Enigma_set_plugs():
    enig = Enigma()
    ret = enig.set_plugs(['EF', 'GH'], True)
    assert ret == None
示例#19
0
def test_Enigma_init_short_key():
    with pytest.raises(ValueError):
        enig = Enigma('AA')
        enig = Enigma('AAAAAA')
示例#20
0
 def valid_enigma(self):
     return Enigma(key='AAA',
                   swaps=['AB', 'CD'],
                   rotor_order=['I', 'II', 'III'])
示例#21
0
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.'
示例#22
0
def test_Enigma_encode_decode_letter_final_in_Plug():
    enig = Enigma(swaps=[('I', 'N')])
    ret = enig.encode_decode_letter('H')
    assert ret == 'N'
示例#23
0
 def test_invalid_key(self):
     with pytest.raises(ValueError) as err:
         Enigma(key='AA', swaps=None, rotor_order=['I', 'II', 'III'])
         print(err)
示例#24
0
def test_Enigma_init():
    enig = Enigma()
    assert enig.key == 'AAA'
    assert enig.rotor_order == ['I', 'II', 'III']