Many tests done, still more to come.
[szyfrow.git] / tests / test_railfence.py
1 import pytest
2 import string
3
4 from szyfrow.railfence import *
5 from szyfrow.support.utilities import *
6
7
8 def test_encipher_message():
9 plaintext = 'hellothereavastmeheartiesthisisalongpieceoftextfortestingrailfenceciphers'
10 ciphertext = railfence_encipher(plaintext, 2, fillvalue='!')
11 expected = 'hlohraateerishsslnpeefetotsigaleccpeselteevsmhatetiiaogicotxfretnrifneihr!'
12 assert ciphertext == expected
13 ciphertext = railfence_encipher(plaintext, 3, fillvalue='!')
14 expected = 'horaersslpeeosglcpselteevsmhatetiiaogicotxfretnrifneihr!!lhateihsnefttiaece!'
15 assert ciphertext == expected
16 ciphertext = railfence_encipher(plaintext, 5, fillvalue='!')
17 expected = 'hresleogcseeemhetaocofrnrner!!lhateihsnefttiaece!!ltvsatiigitxetifih!!oarspeslp!'
18 assert ciphertext == expected
19 ciphertext = railfence_encipher(plaintext, 10, fillvalue='!')
20 expected = 'hepisehagitnr!!lernesge!!lmtocerh!!otiletap!!tseaorii!!hassfolc!!evtitffe!!rahsetec!!eixn!'
21 assert ciphertext == expected
22 ciphertext = railfence_encipher(plaintext, 3)
23 expected = 'horaersslpeeosglcpselteevsmhatetiiaogicotxfretnrifneihrlhateihsnefttiaece'
24 assert ciphertext == expected
25 ciphertext = railfence_encipher(plaintext, 5)
26 expected = 'hresleogcseeemhetaocofrnrnerlhateihsnefttiaeceltvsatiigitxetifihoarspeslp'
27 assert ciphertext == expected
28 ciphertext = railfence_encipher(plaintext, 7)
29 expected = 'haspolsevsetgifrifrlatihnettaeelemtiocxernhorersleesgcptehaiaottneihesfic'
30 assert ciphertext == expected
31
32
33 def test_decipher_message():
34 plaintext = 'hellothereavastmeheartiesthisisalongpieceoftextfortestingrailfenceciphers'
35 for key in range(2, 11):
36 enciphered = railfence_encipher(plaintext, key)
37 deciphered = railfence_decipher(enciphered, key)
38 assert deciphered == plaintext
39
40
41 def test_railfence_break():
42 plaintext = sanitise('''It is a truth universally acknowledged, that a single man in
43 possession of a good fortune, must be in want of a wife. However
44 little known the feelings or views of such a man may be on his
45 first entering a neighbourhood, this truth is so well fixed in
46 the minds of the surrounding families, that he is considered the
47 rightful property of some one or other of their daughters.''')
48 expected_key = 7
49 expected_score = Ptrigrams(plaintext)
50 ciphertext = railfence_encipher(plaintext, expected_key)
51
52 key, score = railfence_break(ciphertext, fitness=Ptrigrams)
53
54 assert key == expected_key
55 assert score == pytest.approx(expected_score)