Many tests done, still more to come.
[szyfrow.git] / tests / test_amsco.py
1 import pytest
2 import string
3
4 from szyfrow.amsco import *
5 from szyfrow.support.utilities import *
6 from szyfrow.support.language_models import transpositions_of
7
8 def test_positions():
9 grid = amsco_positions(string.ascii_lowercase, 'freddy', fillpattern=(1, 2))
10 assert grid == [[AmscoSlice(index=3, start=4, end=6),
11 AmscoSlice(index=2, start=3, end=4),
12 AmscoSlice(index=0, start=0, end=1),
13 AmscoSlice(index=1, start=1, end=3),
14 AmscoSlice(index=4, start=6, end=7)],
15 [AmscoSlice(index=8, start=12, end=13),
16 AmscoSlice(index=7, start=10, end=12),
17 AmscoSlice(index=5, start=7, end=9),
18 AmscoSlice(index=6, start=9, end=10),
19 AmscoSlice(index=9, start=13, end=15)],
20 [AmscoSlice(index=13, start=19, end=21),
21 AmscoSlice(index=12, start=18, end=19),
22 AmscoSlice(index=10, start=15, end=16),
23 AmscoSlice(index=11, start=16, end=18),
24 AmscoSlice(index=14, start=21, end=22)],
25 [AmscoSlice(index=18, start=27, end=28),
26 AmscoSlice(index=17, start=25, end=27),
27 AmscoSlice(index=15, start=22, end=24),
28 AmscoSlice(index=16, start=24, end=25),
29 AmscoSlice(index=19, start=28, end=30)]]
30
31 def test_encipher_message():
32 ciphertext = amsco_encipher('hellothere', 'abc', fillpattern=(1, 2))
33 assert ciphertext == 'hoteelhler'
34
35 ciphertext = amsco_encipher('hellothere', 'abc', fillpattern=(2, 1))
36 assert ciphertext == 'hetelhelor'
37
38 ciphertext = amsco_encipher('hellothere', 'acb', fillpattern=(1, 2))
39 assert ciphertext == 'hotelerelh'
40
41 ciphertext = amsco_encipher('hellothere', 'acb', fillpattern=(2, 1))
42 assert ciphertext == 'hetelorlhe'
43
44 ciphertext = amsco_encipher('hereissometexttoencipher', 'encode')
45 assert ciphertext == 'etecstthhomoerereenisxip'
46
47 ciphertext = amsco_encipher('hereissometexttoencipher', 'cipher', fillpattern=(1, 2))
48 assert ciphertext == 'hetcsoeisterereipexthomn'
49
50 ciphertext = amsco_encipher('hereissometexttoencipher', 'cipher', fillpattern=(1, 2), fillstyle=AmscoFillStyle.continuous)
51 assert ciphertext == 'hecsoisttererteipexhomen'
52
53 ciphertext = amsco_encipher('hereissometexttoencipher', 'cipher', fillpattern=(2, 1))
54 assert ciphertext == 'heecisoosttrrtepeixhemen'
55
56 ciphertext = amsco_encipher('hereissometexttoencipher', 'cipher', fillpattern=(1, 3, 2))
57 assert ciphertext == 'hxtomephescieretoeisnter'
58
59 ciphertext = amsco_encipher('hereissometexttoencipher', 'cipher', fillpattern=(1, 3, 2), fillstyle=AmscoFillStyle.continuous)
60 assert ciphertext == 'hxomeiphscerettoisenteer'
61
62
63 def test_decipher_message():
64 plaintext = 'hereissometexttoencipher'
65 for key in ['bayes', 'samplekey']:
66 for fillpattern in [(1, 2), (2, 1)]:
67 for fillstyle in AmscoFillStyle:
68 enciphered = amsco_encipher(plaintext, key,
69 fillpattern=fillpattern, fillstyle=fillstyle)
70 deciphered = amsco_decipher(enciphered, key,
71 fillpattern=fillpattern, fillstyle=fillstyle)
72 assert deciphered == plaintext
73
74
75 def test_amsco_break():
76 plaintext = sanitise('''It is a truth universally acknowledged, that a single man in
77 possession of a good fortune, must be in want of a wife. However
78 little known the feelings or views of such a man may be on his
79 first entering a neighbourhood, this truth is so well fixed in
80 the minds of the surrounding families, that he is considered the
81 rightful property of some one or other of their daughters.''')
82 expected_key = 'encipher'
83 expected_fillpattern = (1, 2)
84 expected_fillsytle = AmscoFillStyle.continuous
85 expected_score = Pbigrams(plaintext)
86 ciphertext = amsco_encipher(plaintext, expected_key,
87 fillpattern=expected_fillpattern, fillstyle=expected_fillsytle)
88 used_translist = collections.defaultdict(list)
89 for word in 'encipher fourteen keyword'.split():
90 used_translist[transpositions_of(word)] += [word]
91
92 (key, fillpattern, fillstyle), score = amsco_break(ciphertext,
93 translist=used_translist)
94
95 assert key == transpositions_of(expected_key)
96 assert fillpattern == expected_fillpattern
97 assert fillstyle == expected_fillsytle
98 assert score == pytest.approx(expected_score)