Sfoglia il codice sorgente

Add testing to new crypto_engine

oz123 10 anni fa
parent
commit
1f2daf3104
2 ha cambiato i file con 13 aggiunte e 8 eliminazioni
  1. 6 1
      pwman/tests/test_crypto_engine.py
  2. 7 7
      pwman/util/crypto_engine.py

+ 6 - 1
pwman/tests/test_crypto_engine.py

@@ -2,7 +2,8 @@ import unittest
 import pwman.util.config as config
 import os
 from pwman.util.crypto_engine import (write_password, save_a_secret_message,
-                                      read_a_secret_message)
+                                      read_a_secret_message,
+                                      CryptoEngine)
 
 # set cls_timout to negative number (e.g. -1) to disable
 default_config = {'Global': {'umask': '0100', 'colors': 'yes',
@@ -30,3 +31,7 @@ class CryptoEngineTest(unittest.TestCase):
 
     def test_c_read_secret(self):
         read_a_secret_message(reader=give_key)
+
+    def test_d_get_crypto(self):
+        ce = CryptoEngine.get()
+        secret = ce.changepassword(reader=give_key)

+ 7 - 7
pwman/util/crypto_engine.py

@@ -139,7 +139,7 @@ def read_a_secret_message(reader=raw_input):
         print(decoded)
 
 
-class CryptoEngine(object):  # pragma: no cover
+class CryptoEngine(object):  # pagma: no cover
     _timeoutcount = 0
     _instance = None
     _instance_new = None
@@ -186,19 +186,19 @@ class CryptoEngine(object):  # pragma: no cover
         else:
             raise Exception("callback must be an instance of Callback!")
 
-    def _create_password(self):
+    def _create_password(self, reader=raw_input):
         """
-        Write a secret password as a hash and the salt used for this hash
-        to a file
+        Create a secret password as a hash and the salt used for this hash.
+        Change reader to manipulate how input is given.
         """
         salt = base64.b64encode(os.urandom(32))
-        passwd = raw_input("Please type in the secret key:")
+        passwd = reader("Please type in the secret key:")
         key = get_digest(passwd, salt)
         hpk = salt+'$6$'.encode('utf8')+binascii.hexlify(key)
         return hpk.decode('utf-8')
 
-    def changepassword(self):
-        self._keycrypted = self._create_password()
+    def changepassword(self, reader=raw_input):
+        self._keycrypted = self._create_password(reader=reader)
         return self._keycrypted
 
     def __init__(self, keycrypted=None, algorithm='AES', timeout=-1):