Mercurial > libervia-backend
comparison src/test/test_memory_crypto.py @ 1028:127c96020022
memory, test: added module crypto to hash passwords and encrypt/decrypt passwords or blocks
author | souliane <souliane@mailoo.org> |
---|---|
date | Wed, 07 May 2014 15:46:43 +0200 |
parents | |
children | cbf917a90784 |
comparison
equal
deleted
inserted
replaced
1027:ee46515a12f2 | 1028:127c96020022 |
---|---|
1 #!/usr/bin/python | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # SAT: a jabber client | |
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Jérôme Poisson (goffi@goffi.org) | |
6 # Copyright (C) 2013, 2014 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 sat.test import helpers | |
25 from sat.memory.crypto import BlockCipher, PasswordHasher | |
26 from os import urandom | |
27 from twisted.internet import defer | |
28 | |
29 | |
30 class CryptoTest(helpers.SatTestCase): | |
31 | |
32 def setUp(self): | |
33 self.host = helpers.FakeSAT() | |
34 | |
35 def test_encrypt_decrypt(self): | |
36 d_list = [] | |
37 for key_len in (0, 2, 8, 10, 16, 24, 30, 32, 40): | |
38 key = urandom(key_len) | |
39 for message_len in (0, 2, 16, 24, 32, 100): | |
40 message = urandom(message_len) | |
41 d = BlockCipher.encrypt(key, message) | |
42 d.addCallback(lambda ciphertext: lambda key, cipher: BlockCipher.decrypt(key, ciphertext)) | |
43 d.addCallback(lambda decrypted: lambda message, decrypted: self.assertEqual(message, decrypted)) | |
44 d_list.append(d) | |
45 return defer.DeferredList(d_list) | |
46 | |
47 def test_hash_verify(self): | |
48 d_list = [] | |
49 for password in (0, 2, 8, 10, 16, 24, 30, 32, 40): | |
50 d = PasswordHasher.hash(password) | |
51 | |
52 def cb(hashed): | |
53 d1 = PasswordHasher.verify(password, hashed) | |
54 d1.addCallback(lambda result: self.assertTrue(result)) | |
55 d_list.append(d1) | |
56 attempt = urandom(10) | |
57 d2 = PasswordHasher.verify(attempt, hashed) | |
58 d2.addCallback(lambda result: self.assertFalse(result)) | |
59 d_list.append(d2) | |
60 | |
61 d.addCallback(cb) | |
62 return defer.DeferredList(d_list) |