2014-04-08 00:17:34 -03:00
|
|
|
import unittest
|
|
|
|
|
|
2016-08-17 11:57:29 -07:00
|
|
|
from simple_cipher import Caesar, Cipher
|
2014-06-11 15:06:22 +02:00
|
|
|
|
2014-04-20 19:43:22 +02:00
|
|
|
|
2014-04-08 00:17:34 -03:00
|
|
|
class CipherTest(unittest.TestCase):
|
2014-04-20 19:43:22 +02:00
|
|
|
|
2014-04-08 00:17:34 -03:00
|
|
|
def test_caesar_encode1(self):
|
|
|
|
|
self.assertEqual('lwlvdzhvrphsurjudpplqjlqsbwkrq',
|
|
|
|
|
Caesar().encode('itisawesomeprogramminginpython'))
|
|
|
|
|
|
|
|
|
|
def test_caesar_encode2(self):
|
|
|
|
|
self.assertEqual('yhqlylglylfl', Caesar().encode('venividivici'))
|
|
|
|
|
|
|
|
|
|
def test_caesar_encode3(self):
|
|
|
|
|
self.assertEqual('wzdvwkhqljkwehiruhfkulvwpdv',
|
|
|
|
|
Caesar().encode('\'Twas the night before Christmas'))
|
|
|
|
|
|
|
|
|
|
def test_caesar_encode_with_numbers(self):
|
|
|
|
|
self.assertEqual('jr', Caesar().encode('1, 2, 3, Go!'))
|
|
|
|
|
|
|
|
|
|
def test_caesar_decode(self):
|
|
|
|
|
self.assertEqual('venividivici', Caesar().decode('yhqlylglylfl'))
|
|
|
|
|
|
|
|
|
|
def test_cipher_encode1(self):
|
|
|
|
|
c = Cipher('a')
|
|
|
|
|
self.assertEqual('itisawesomeprogramminginpython',
|
|
|
|
|
c.encode('itisawesomeprogramminginpython'))
|
|
|
|
|
|
|
|
|
|
def test_cipher_encode2(self):
|
|
|
|
|
c = Cipher('aaaaaaaaaaaaaaaaaaaaaa')
|
|
|
|
|
self.assertEqual('itisawesomeprogramminginpython',
|
|
|
|
|
c.encode('itisawesomeprogramminginpython'))
|
|
|
|
|
|
|
|
|
|
def test_cipher_encode3(self):
|
|
|
|
|
c = Cipher('dddddddddddddddddddddd')
|
|
|
|
|
self.assertEqual('yhqlylglylfl', c.encode('venividivici'))
|
|
|
|
|
|
|
|
|
|
def test_cipher_encode4(self):
|
2015-11-09 11:10:22 +01:00
|
|
|
key = ('duxrceqyaimciuucnelkeoxjhdyduucpmrxmaivacmybmsdrzwqxvbxsy'
|
|
|
|
|
'gzsabdjmdjabeorttiwinfrpmpogvabiofqexnohrqu')
|
2014-04-08 00:17:34 -03:00
|
|
|
c = Cipher(key)
|
|
|
|
|
self.assertEqual('gccwkixcltycv', c.encode('diffiehellman'))
|
|
|
|
|
|
2015-09-02 16:23:20 +02:00
|
|
|
def test_cipher_encode_short_key(self):
|
|
|
|
|
c = Cipher('abcd')
|
|
|
|
|
self.assertEqual('abcdabcd', c.encode('aaaaaaaa'))
|
|
|
|
|
|
2014-04-08 00:17:34 -03:00
|
|
|
def test_cipher_compositiion1(self):
|
2015-11-09 11:10:22 +01:00
|
|
|
key = ('duxrceqyaimciuucnelkeoxjhdyduucpmrxmaivacmybmsdrzwqxvbxsy'
|
|
|
|
|
'gzsabdjmdjabeorttiwinfrpmpogvabiofqexnohrqu')
|
2014-04-08 00:17:34 -03:00
|
|
|
plaintext = 'adaywithoutlaughterisadaywasted'
|
|
|
|
|
c = Cipher(key)
|
|
|
|
|
self.assertEqual(plaintext, c.decode(c.encode(plaintext)))
|
|
|
|
|
|
|
|
|
|
def test_cipher_compositiion2(self):
|
|
|
|
|
plaintext = 'adaywithoutlaughterisadaywasted'
|
|
|
|
|
c = Cipher()
|
|
|
|
|
self.assertEqual(plaintext, c.decode(c.encode(plaintext)))
|
|
|
|
|
|
2015-09-02 16:09:40 +02:00
|
|
|
def test_cipher_random_key(self):
|
|
|
|
|
c = Cipher()
|
|
|
|
|
self.assertTrue(len(c.key) >= 100,
|
2015-11-09 11:10:22 +01:00
|
|
|
'A random key must be generated when no key is given!')
|
2015-09-02 16:09:40 +02:00
|
|
|
self.assertTrue(c.key.islower() and c.key.isalpha(),
|
2015-11-09 11:10:22 +01:00
|
|
|
'All items in the key must be chars and lowercase!')
|
2015-09-02 16:09:40 +02:00
|
|
|
|
2015-09-02 16:14:03 +02:00
|
|
|
def test_cipher_wrong_key(self):
|
|
|
|
|
self.assertRaises(ValueError, Cipher, 'a1cde')
|
|
|
|
|
self.assertRaises(ValueError, Cipher, 'aBcde')
|
|
|
|
|
|
2014-04-08 00:17:34 -03:00
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
unittest.main()
|