diff options
Diffstat (limited to 'src/leap/soledad/tests/test_sqlcipher.py')
-rw-r--r-- | src/leap/soledad/tests/test_sqlcipher.py | 51 |
1 files changed, 50 insertions, 1 deletions
diff --git a/src/leap/soledad/tests/test_sqlcipher.py b/src/leap/soledad/tests/test_sqlcipher.py index a7ba5284..f203dc66 100644 --- a/src/leap/soledad/tests/test_sqlcipher.py +++ b/src/leap/soledad/tests/test_sqlcipher.py @@ -13,18 +13,23 @@ from u1db import ( ) from leap.soledad.backends import sqlcipher as sqlite_backend from leap.soledad.backends.leap_backend import LeapDocument -from leap.soledad.tests import u1db_tests +from leap.soledad.tests import u1db_tests as tests from leap.soledad.tests.u1db_tests.test_sqlite_backend import ( TestSQLiteDatabase, TestSQLitePartialExpandDatabase, ) from leap.soledad.tests.u1db_tests.test_backends import TestAlternativeDocument +from leap.soledad.tests.u1db_tests.test_backends import AllDatabaseTests PASSWORD = '123456' class TestSQLCipherDatabase(TestSQLitePartialExpandDatabase): + # The following tests had to be cloned from u1db because they all + # instantiate the backend directly, so we need to change that in order to + # our backend be instantiated in place. + def setUp(self): super(TestSQLitePartialExpandDatabase, self).setUp() self.db = sqlite_backend.SQLCipherDatabase(':memory:', PASSWORD) @@ -97,9 +102,53 @@ class TestSQLCipherDatabase(TestSQLitePartialExpandDatabase): self.assertEqual(TestAlternativeDocument, db2._factory) def test_create_database_initializes_schema(self): + # This test had to be cloned because our implementation of SQLCipher + # backend is referenced with an index_storage_value that includes the + # word "encrypted". See u1db's sqlite_backend and our + # sqlcipher_backend for reference. raw_db = self.db._get_sqlite_handle() c = raw_db.cursor() c.execute("SELECT * FROM u1db_config") config = dict([(r[0], r[1]) for r in c.fetchall()]) self.assertEqual({'sql_schema': '0', 'replica_uid': 'test', 'index_storage': 'expand referenced encrypted'}, config) + + +# The following tests come from u1db test_backends. + +def make_sqlcipher_database_for_test(test, replica_uid): + db = sqlite_backend.SQLCipherDatabase(':memory:', PASSWORD) + db._set_replica_uid(replica_uid) + return db + + +def copy_sqlcipher_database_for_test(test, db): + # DO NOT COPY OR REUSE THIS CODE OUTSIDE TESTS: COPYING U1DB DATABASES IS + # THE WRONG THING TO DO, THE ONLY REASON WE DO SO HERE IS TO TEST THAT WE + # CORRECTLY DETECT IT HAPPENING SO THAT WE CAN RAISE ERRORS RATHER THAN + # CORRUPT USER DATA. USE SYNC INSTEAD, OR WE WILL SEND NINJA TO YOUR + # HOUSE. + new_db = sqlite_backend.SQLCipherDatabase(':memory:', PASSWORD) + tmpfile = StringIO() + for line in db._db_handle.iterdump(): + if not 'sqlite_sequence' in line: # work around bug in iterdump + tmpfile.write('%s\n' % line) + tmpfile.seek(0) + new_db._db_handle = dbapi2.connect(':memory:') + new_db._db_handle.cursor().executescript(tmpfile.read()) + new_db._db_handle.commit() + new_db._set_replica_uid(db._replica_uid) + new_db._factory = db._factory + return new_db + + +class SQLCipherTests(AllDatabaseTests): + + scenarios = [ + ('sqlcipher', {'make_database_for_test': make_sqlcipher_database_for_test, + 'copy_database_for_test': copy_sqlcipher_database_for_test, + 'make_document_for_test': tests.make_document_for_test,}), + ] + + +load_tests = tests.load_with_scenarios |