浏览代码

Fix corrupted db ... thus fixes failing test

oz123 10 年之前
父节点
当前提交
28d287f08a
共有 3 个文件被更改,包括 13 次插入20 次删除
  1. 1 0
      pwman/tests/db_tests.py
  2. 二进制
      pwman/tests/pwman.v0.0.8.db
  3. 12 20
      pwman/util/crypto.py

+ 1 - 0
pwman/tests/db_tests.py

@@ -166,6 +166,7 @@ class DBTests(unittest.TestCase):
         self.db.addnodes([node])
         idx_created = node._id
         new_node = self.db.getnodes([idx_created])[0]
+
         for key, attr in {'password': password, 'username': username,
                           'url': url, 'notes': notes}.iteritems():
             self.assertEquals(attr, getattr(new_node, key))

二进制
pwman/tests/pwman.v0.0.8.db


+ 12 - 20
pwman/util/crypto.py

@@ -342,8 +342,12 @@ password again")
         if not key:
             raise Exception("Wrong password entered %s times; giving up"
                             % max_tries)
+        try:
+            key = str(key).decode('base64')
+        except Exception:
+            key = cPickle.loads(key)
+            key = str(key).decode('base64')
 
-        key = str(key).decode('base64')
         self._cipher = self._getcipher_real(key,
                                             self._algo)
 
@@ -384,29 +388,17 @@ password again")
         prepare data before encrypting
         """
         plaintext = obj
-        return plaintext
+        numblocks = (len(plaintext)/blocksize) + 1
+        newdatasize = blocksize*numblocks
+        return plaintext.ljust(newdatasize)
 
     def _retrievedata(self, plaintext):
         """
         retrieve encrypted data
         """
+        if (plaintext.startswith(_TAG)):
+            plaintext = plaintext[len(_TAG):]
         return plaintext
-        #if (plaintext.startswith(_TAG)):
-        #    plaintext = plaintext[len(_TAG):]
-        #else:
-        #    raise CryptoBadKeyException("Error decrypting, bad key")
-
-        #try:
-            # old db version used to write stuff to db with
-            # plaintext = cPickle.dumps(obj)
-            # TODO: completely remove this block, and convert
-            # the DB to a completely plain text ...
-
-            # This implies that the coversion from OLD DATABASE FORMAT has
-            # to plain strings too ...
-        #    return cPickle.loads(plaintext)
-        #except (TypeError, ValueError, cPickle.UnpicklingError, EOFError):
-        #    return plaintext
 
 
 class DummyCryptoEngine(CryptoEngine):
@@ -488,8 +480,8 @@ class CryptoEngineOld(CryptoEngine):
         """
         if (plaintext.startswith(_TAG)):
             plaintext = plaintext[len(_TAG):]
-        else:
-            raise CryptoBadKeyException("Error decrypting, bad key")
+        #else:
+        #    raise CryptoBadKeyException("Error decrypting, bad key")
 
         try:
             # old db version used to write stuff to db with