Pārlūkot izejas kodu

Add more testing for crypto_engine

oz123 10 gadi atpakaļ
vecāks
revīzija
787d03fb6c
2 mainītis faili ar 28 papildinājumiem un 7 dzēšanām
  1. 25 3
      pwman/tests/test_crypto_engine.py
  2. 3 4
      pwman/util/crypto_engine.py

+ 25 - 3
pwman/tests/test_crypto_engine.py

@@ -1,8 +1,10 @@
 import unittest
 import unittest
-from pwman.util.callback import Callback
 import os
 import os
-from pwman.util.crypto_engine import (CryptoEngine, CryptoException)
 import time
 import time
+import string
+from pwman.util.callback import Callback
+from pwman.util.crypto_engine import (CryptoEngine, CryptoException,
+                                      generate_password)
 
 
 # set cls_timout to negative number (e.g. -1) to disable
 # set cls_timout to negative number (e.g. -1) to disable
 default_config = {'Global': {'umask': '0100', 'colors': 'yes',
 default_config = {'Global': {'umask': '0100', 'colors': 'yes',
@@ -36,6 +38,26 @@ class DummyCallback(Callback):
         return u'12345'
         return u'12345'
 
 
 
 
+class TestPassGenerator(unittest.TestCase):
+
+    def test_len(self):
+        self.assertEqual(13, len(generate_password(pass_len=13)))
+
+    def test_has_upper(self):
+        password = generate_password(uppercase=True, lowercase=False)
+        lower = set(string.ascii_lowercase)
+        it = lower.intersection(set(password))
+        print(it)
+        self.assertTrue(len(it) == 0)
+
+    def test_has_digits(self):
+        password = generate_password(uppercase=True, lowercase=False)
+        digits = set(string.digits)
+        it = digits.intersection(password)
+        print(it)
+        self.assertTrue(len(it) > 0)
+
+
 class CryptoEngineTest(unittest.TestCase):
 class CryptoEngineTest(unittest.TestCase):
 
 
     def test4_d_get_crypto(self):
     def test4_d_get_crypto(self):
@@ -89,4 +111,4 @@ class CryptoEngineTest(unittest.TestCase):
         self.assertEqual(decrypt.decode(), "topsecret")
         self.assertEqual(decrypt.decode(), "topsecret")
 
 
 if __name__ == '__main__':
 if __name__ == '__main__':
-    unittest.main(verbosity=1, failfast=True)
+    unittest.main(verbosity=2, failfast=True)

+ 3 - 4
pwman/util/crypto_engine.py

@@ -156,8 +156,8 @@ class CryptoEngine(object):  # pagma: no cover
         if not self._is_authenticated():
         if not self._is_authenticated():
             p, s = self._auth()
             p, s = self._auth()
             cipher = get_cipher(p, s)
             cipher = get_cipher(p, s)
+            self._cipher = cipher
             del(p)
             del(p)
-            return EncodeAES(cipher, prepare_data(text, AES.block_size))
 
 
         return EncodeAES(self._cipher, prepare_data(text, AES.block_size))
         return EncodeAES(self._cipher, prepare_data(text, AES.block_size))
 
 
@@ -165,8 +165,8 @@ class CryptoEngine(object):  # pagma: no cover
         if not self._is_authenticated():
         if not self._is_authenticated():
             p, s = self._auth()
             p, s = self._auth()
             cipher = get_cipher(p, s)
             cipher = get_cipher(p, s)
+            self._cipher = cipher
             del(p)
             del(p)
-            return DecodeAES(cipher, prepare_data(cipher_text, AES.block_size))
 
 
         return DecodeAES(self._cipher, prepare_data(cipher_text,
         return DecodeAES(self._cipher, prepare_data(cipher_text,
                                                     AES.block_size))
                                                     AES.block_size))
@@ -194,8 +194,7 @@ class CryptoEngine(object):  # pagma: no cover
     def changepassword(self, reader=raw_input):
     def changepassword(self, reader=raw_input):
         if self._callback is None:
         if self._callback is None:
             raise CryptoException("No callback class has been specified")
             raise CryptoException("No callback class has been specified")
-        # if not self._is_authenticated():
-        #    p, s = self._auth()
+
         # if you change the password of the database you have to Change
         # if you change the password of the database you have to Change
         # all the cipher texts in the databse!!!
         # all the cipher texts in the databse!!!
         self._keycrypted = self._create_password()
         self._keycrypted = self._create_password()