-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add unit test and found a few bugs and problems.
- Loading branch information
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# -*- coding:utf-8 -*- | ||
|
||
import unittest | ||
import tempfile | ||
|
||
from handler import AESHandler | ||
|
||
|
||
class TestAESHandler(unittest.TestCase): | ||
|
||
def setUp(self): | ||
self.tmpfile = tempfile.NamedTemporaryFile() | ||
self.handler = AESHandler() | ||
self.handler.initialize('provide a key', self.tmpfile.name) | ||
|
||
def tearDown(self): | ||
self.tmpfile.close() | ||
|
||
def test_data_persistance(self): | ||
self.assertTrue(self.handler.add_record(u'web', u'facebook', u'lol2012')) | ||
self.assertTrue(self.handler.add_record(u'web', u'google', u'answer42')) | ||
self.assertTrue(self.handler.add_record(u'bank', u'BOA', u'money888')) | ||
|
||
print self.handler.data | ||
self.assertEqual(len(self.handler.data['records']), 3) | ||
self.assertEqual(self.handler.data['currentID'], 3) | ||
self.assertEqual(self.handler.data['currentGID'], 2) | ||
|
||
handler2 = AESHandler() | ||
handler2.initialize('provide a key', self.tmpfile.name) | ||
|
||
self.assertDictEqual(self.handler.data, handler2.data) | ||
self.assertListEqual(self.handler.data['records'], | ||
handler2.data['records']) | ||
|
||
def test_32byte_key_generate(self): | ||
key1 = AESHandler.figure_32Byte_key('not enough 32 bytes') | ||
key2 = AESHandler.figure_32Byte_key('exceed 32 bytes' * 3) | ||
self.assertTrue(key1.startswith('not enough 32 bytes')) | ||
self.assertEqual(len(key1), 32) | ||
self.assertEqual(len(key2), 32) | ||
|
||
def test_encrypt_and_decrypt(self): | ||
key = AESHandler.figure_32Byte_key('provide a key') | ||
text = 'test encrypt and decrypt.' | ||
ciphertext = AESHandler.encrypt(text, key) | ||
plaintext = AESHandler.decrypt(ciphertext, key) | ||
self.assertEqual(text, plaintext) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |