comparison src/test/test_memory_crypto.py @ 1098:77cd312d32c4

memory: fixes encoding issues during encryption
author souliane <souliane@mailoo.org>
date Sun, 06 Jul 2014 21:13:47 +0200
parents cbf917a90784
children 069ad98b360d
comparison
equal deleted inserted replaced
1097:dace0ede919c 1098:77cd312d32c4
21 21
22 """ Tests for the plugin radiocol """ 22 """ Tests for the plugin radiocol """
23 23
24 from sat.test import helpers 24 from sat.test import helpers
25 from sat.memory.crypto import BlockCipher, PasswordHasher 25 from sat.memory.crypto import BlockCipher, PasswordHasher
26 from os import urandom 26 import random
27 import string
27 from twisted.internet import defer 28 from twisted.internet import defer
29
30
31 def getRandomUnicode(len):
32 """Return a random unicode string"""
33 return u''.join(random.choice(string.letters + u"éáúóâêûôßüöä") for i in xrange(len))
28 34
29 35
30 class CryptoTest(helpers.SatTestCase): 36 class CryptoTest(helpers.SatTestCase):
31 37
32 def setUp(self): 38 def setUp(self):
40 d.addCallback(lambda ciphertext: BlockCipher.decrypt(key, ciphertext)) 46 d.addCallback(lambda ciphertext: BlockCipher.decrypt(key, ciphertext))
41 d.addCallback(lambda decrypted: self.assertEqual(message, decrypted)) 47 d.addCallback(lambda decrypted: self.assertEqual(message, decrypted))
42 d_list.append(d) 48 d_list.append(d)
43 49
44 for key_len in (0, 2, 8, 10, 16, 24, 30, 32, 40): 50 for key_len in (0, 2, 8, 10, 16, 24, 30, 32, 40):
45 key = urandom(key_len) 51 key = getRandomUnicode(key_len)
46 for message_len in (0, 2, 16, 24, 32, 100): 52 for message_len in (0, 2, 16, 24, 32, 100):
47 message = urandom(message_len) 53 message = getRandomUnicode(message_len)
48 test(key, message) 54 test(key, message)
49 return defer.DeferredList(d_list) 55 return defer.DeferredList(d_list)
50 56
51 def test_hash_verify(self): 57 def test_hash_verify(self):
52 d_list = [] 58 d_list = []
55 61
56 def cb(hashed): 62 def cb(hashed):
57 d1 = PasswordHasher.verify(password, hashed) 63 d1 = PasswordHasher.verify(password, hashed)
58 d1.addCallback(lambda result: self.assertTrue(result)) 64 d1.addCallback(lambda result: self.assertTrue(result))
59 d_list.append(d1) 65 d_list.append(d1)
60 attempt = urandom(10) 66 attempt = getRandomUnicode(10)
61 d2 = PasswordHasher.verify(attempt, hashed) 67 d2 = PasswordHasher.verify(attempt, hashed)
62 d2.addCallback(lambda result: self.assertFalse(result)) 68 d2.addCallback(lambda result: self.assertFalse(result))
63 d_list.append(d2) 69 d_list.append(d2)
64 70
65 d.addCallback(cb) 71 d.addCallback(cb)