aboutsummaryrefslogtreecommitdiff
blob: 8a8f043fc0824cc509fdc9e65a8fb36a1a5c10fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# vim:fileencoding=utf8:et:ts=4:sts=4:sw=4:ft=python

from Crypto import Random
from unittest import TestCase

from ...common.crypto import cipher


class OkupyCipherTests(TestCase):
    def setUp(self):
        self._random_string = '123456abcdef' * int(cipher.block_size / 2)

    def test_verify_password_less_than_block_size(self):
        data = self._random_string[:cipher.block_size-3]
        hash = cipher.encrypt(data)
        self.assertEqual(cipher.decrypt(hash, len(data)), data)

    def test_verify_password_exact_block_size(self):
        data = self._random_string[:cipher.block_size]
        hash = cipher.encrypt(data)
        self.assertEqual(cipher.decrypt(hash, len(data)), data)

    def test_verify_password_more_than_block_size(self):
        data = self._random_string[:cipher.block_size+3]
        hash = cipher.encrypt(data)
        self.assertEqual(cipher.decrypt(hash, len(data)), data)

    def test_verify_password_more_than_twice_block_size(self):
        data = self._random_string[:cipher.block_size*2+3]
        hash = cipher.encrypt(data)
        self.assertEqual(cipher.decrypt(hash, len(data)), data)

    def test_encrypt_random_bytes(self):
        data = Random.get_random_bytes(45)
        hash = cipher.encrypt(data)
        self.assertEqual(cipher.decrypt(hash, len(data)), data)

    def test_ciphertext_shorter_than_req_output_raises_valueerror(self):
        data = self._random_string[:cipher.block_size*2]
        hash = cipher.encrypt(data)[:cipher.block_size]
        self.assertRaises(ValueError, cipher.decrypt, hash, len(data))

    def test_ciphertext_not_multiple_of_block_size_raises_valueerror(self):
        data = self._random_string[:cipher.block_size/2]
        hash = cipher.encrypt(data)[:cipher.block_size/2]
        self.assertRaises(ValueError, cipher.decrypt, hash, len(data))