Many tests done, still more to come.
[szyfrow.git] / tests / test_column_transposition.py
1 import pytest
2 import string
3
4 from szyfrow.column_transposition import *
5 from szyfrow.support.utilities import *
6
7 def test_encipher_message():
8 ciphertext = column_transposition_encipher('hellothere', 'abcdef', fillcolumnwise=True)
9 assert ciphertext == 'hlohr eltee '
10 ciphertext = column_transposition_encipher('hellothere', 'abcdef', fillcolumnwise=True, emptycolumnwise=True)
11 assert ciphertext == 'hellothere '
12 ciphertext = column_transposition_encipher('hellothere', 'abcdef')
13 assert ciphertext == 'hellothere '
14 ciphertext = column_transposition_encipher('hellothere', 'abcde')
15 assert ciphertext == 'hellothere'
16 ciphertext = column_transposition_encipher('hellothere', 'abcde', fillcolumnwise=True, emptycolumnwise=True)
17 assert ciphertext == 'hellothere'
18 ciphertext = column_transposition_encipher('hellothere', 'abcde', fillcolumnwise=True, emptycolumnwise=False)
19 assert ciphertext == 'hlohreltee'
20 ciphertext = column_transposition_encipher('hellothere', 'abcde', fillcolumnwise=False, emptycolumnwise=True)
21 assert ciphertext == 'htehlelroe'
22 ciphertext = column_transposition_encipher('hellothere', 'abcde', fillcolumnwise=False, emptycolumnwise=False)
23 assert ciphertext == 'hellothere'
24 ciphertext = column_transposition_encipher('hellothere', 'clever', fillcolumnwise=True, emptycolumnwise=True)
25 assert ciphertext == 'heotllrehe'
26 ciphertext = column_transposition_encipher('hellothere', 'clever', fillcolumnwise=True, emptycolumnwise=False)
27 assert ciphertext == 'holrhetlee'
28 ciphertext = column_transposition_encipher('hellothere', 'clever', fillcolumnwise=False, emptycolumnwise=True)
29 assert ciphertext == 'htleehoelr'
30 ciphertext = column_transposition_encipher('hellothere', 'clever', fillcolumnwise=False, emptycolumnwise=False)
31 assert ciphertext == 'hleolteher'
32 ciphertext = column_transposition_encipher('hellothere', 'cleverly')
33 assert ciphertext == 'hleolthre e '
34 ciphertext = column_transposition_encipher('hellothere', 'cleverly', fillvalue='!')
35 assert ciphertext == 'hleolthre!e!'
36 ciphertext = column_transposition_encipher('hellothere', 'cleverly', fillvalue=lambda: '*')
37 assert ciphertext == 'hleolthre*e*'
38
39 def test_decipher_message():
40 plaintext = 'hereissometexttoencipher'
41 for key in ['bayes', 'samplekey']:
42 for fillcolumnwise in [True, False]:
43 for emptycolumnwise in [True, False]:
44 enciphered = column_transposition_encipher(plaintext, key,
45 fillcolumnwise=fillcolumnwise, emptycolumnwise=emptycolumnwise)
46 deciphered = column_transposition_decipher(enciphered, key,
47 fillcolumnwise=fillcolumnwise, emptycolumnwise=emptycolumnwise)
48 assert deciphered.strip() == plaintext
49
50
51 def test_encipher_scytale():
52 assert scytale_encipher('thequickbrownfox', 3) == 'tcnhkfeboqrxuo iw '
53 assert scytale_encipher('thequickbrownfox', 4) == 'tubnhirfecooqkwx'
54 assert scytale_encipher('thequickbrownfox', 5) == 'tubn hirf ecoo qkwx '
55 assert scytale_encipher('thequickbrownfox', 6) == 'tqcrnxhukof eibwo '
56 assert scytale_encipher('thequickbrownfox', 7) == 'tqcrnx hukof eibwo '
57
58 def test_decipher_scytale():
59 plaintext = 'hereissometexttoencipher'
60 for key in range(3, 8):
61 enciphered = scytale_encipher(plaintext, key)
62 deciphered = scytale_decipher(enciphered, key)
63 assert deciphered.strip() == plaintext
64
65 def test_column_transposition_break():
66 plaintext = sanitise('''It is a truth universally acknowledged, that a single man in
67 possession of a good fortune, must be in want of a wife. However
68 little known the feelings or views of such a man may be on his
69 first entering a neighbourhood, this truth is so well fixed in
70 the minds of the surrounding families, that he is considered the
71 rightful property of some one or other of their daughters.''')
72 expected_key = 'encipher'
73 expected_fill = False
74 expected_empty = True
75 expected_score = Pbigrams(plaintext)
76 ciphertext = column_transposition_encipher(plaintext, expected_key,
77 fillcolumnwise=expected_fill, emptycolumnwise=expected_empty)
78 used_translist = collections.defaultdict(list)
79 for word in 'encipher fourteen keyword'.split():
80 used_translist[transpositions_of(word)] += [word]
81
82 (key, fill, empty), score = column_transposition_break_mp(ciphertext,
83 translist=used_translist)
84
85 assert key == transpositions_of(expected_key)
86 assert fill == expected_fill
87 assert empty == expected_empty
88 assert score == pytest.approx(expected_score)
89
90 def test_scytale_break():
91 plaintext = sanitise('''It is a truth universally acknowledged, that a single man in
92 possession of a good fortune, must be in want of a wife. However
93 little known the feelings or views of such a man may be on his
94 first entering a neighbourhood, this truth is so well fixed in
95 the minds of the surrounding families, that he is considered the
96 rightful property of some one or other of their daughters.''')
97 expected_key = 5
98 expected_score = Pbigrams(plaintext)
99 ciphertext = scytale_encipher(plaintext, expected_key)
100
101 key, score = scytale_break_mp(ciphertext)
102
103 assert key == expected_key
104 assert score == pytest.approx(expected_score)
105