comparison libervia/backend/test/test_memory_crypto.py @ 4071:4b842c1fb686

refactoring: renamed `sat` package to `libervia.backend`
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 11:49:51 +0200
parents sat/test/test_memory_crypto.py@524856bd7b19
children
comparison
equal deleted inserted replaced
4070:d10748475025 4071:4b842c1fb686
1 #!/usr/bin/env python3
2
3
4 # SAT: a jabber client
5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org)
6 # Copyright (C) 2013-2016 Adrien Cossa (souliane@mailoo.org)
7
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU Affero General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU Affero General Public License for more details.
17
18 # You should have received a copy of the GNU Affero General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21
22 """ Tests for the plugin radiocol """
23
24 from libervia.backend.test import helpers
25 from libervia.backend.memory.crypto import BlockCipher, PasswordHasher
26 import random
27 import string
28 from twisted.internet import defer
29
30
31 def get_random_unicode(len):
32 """Return a random unicode string"""
33 return "".join(random.choice(string.letters + "éáúóâêûôßüöä") for i in range(len))
34
35
36 class CryptoTest(helpers.SatTestCase):
37 def setUp(self):
38 self.host = helpers.FakeSAT()
39
40 def test_encrypt_decrypt(self):
41 d_list = []
42
43 def test(key, message):
44 d = BlockCipher.encrypt(key, message)
45 d.addCallback(lambda ciphertext: BlockCipher.decrypt(key, ciphertext))
46 d.addCallback(lambda decrypted: self.assertEqual(message, decrypted))
47 d_list.append(d)
48
49 for key_len in (0, 2, 8, 10, 16, 24, 30, 32, 40):
50 key = get_random_unicode(key_len)
51 for message_len in (0, 2, 16, 24, 32, 100):
52 message = get_random_unicode(message_len)
53 test(key, message)
54 return defer.DeferredList(d_list)
55
56 def test_hash_verify(self):
57 d_list = []
58 for password in (0, 2, 8, 10, 16, 24, 30, 32, 40):
59 d = PasswordHasher.hash(password)
60
61 def cb(hashed):
62 d1 = PasswordHasher.verify(password, hashed)
63 d1.addCallback(lambda result: self.assertTrue(result))
64 d_list.append(d1)
65 attempt = get_random_unicode(10)
66 d2 = PasswordHasher.verify(attempt, hashed)
67 d2.addCallback(lambda result: self.assertFalse(result))
68 d_list.append(d2)
69
70 d.addCallback(cb)
71 return defer.DeferredList(d_list)