From a66c472b09015c4310519bc71eeef925d9041de6 Mon Sep 17 00:00:00 2001 From: drebs Date: Wed, 1 May 2013 16:47:02 -0300 Subject: Use common test method to assert documents with encrypted content. --- src/leap/soledad/tests/test_leap_backend.py | 94 ++++++++++------------------- src/leap/soledad/tests/test_sqlcipher.py | 61 ++++--------------- 2 files changed, 45 insertions(+), 110 deletions(-) (limited to 'src/leap/soledad') diff --git a/src/leap/soledad/tests/test_leap_backend.py b/src/leap/soledad/tests/test_leap_backend.py index abd0d9ee..2053bb33 100644 --- a/src/leap/soledad/tests/test_leap_backend.py +++ b/src/leap/soledad/tests/test_leap_backend.py @@ -405,13 +405,8 @@ class TestLeapSyncTarget( [(doc, 10, 'T-sid')], 'replica', last_known_generation=0, last_known_trans_id=None, return_doc_cb=receive_doc) self.assertEqual(1, new_gen) - # (possibly) decrypt and compare - doc2 = db.get_doc('doc-here') - if leap_backend.ENC_SCHEME_KEY in doc2.content: - doc2.set_json( - leap_backend.decrypt_doc_json( - self._soledad._crypto, doc2.doc_id, doc2.get_json())) - self.assertEqual(doc, doc2) + self.assertGetEncryptedDoc( + db, 'doc-here', 'replica:1', '{"value": "here"}', False) def test_sync_exchange_send_failure_and_retry_scenario(self): """ @@ -419,6 +414,7 @@ class TestLeapSyncTarget( This test was adapted to decrypt remote content before assert. """ + self.startServer() def blackhole_getstderr(inst): @@ -456,14 +452,9 @@ class TestLeapSyncTarget( [(doc1, 10, 'T-sid'), (doc2, 11, 'T-sud')], 'replica', last_known_generation=0, last_known_trans_id=None, return_doc_cb=receive_doc) - # -- (possibly) decrypt and compare - tmpdoc = db.get_doc('doc-here') - if leap_backend.ENC_SCHEME_KEY in tmpdoc.content: - tmpdoc.set_json( - leap_backend.decrypt_doc_json( - self._soledad._crypto, tmpdoc.doc_id, tmpdoc.get_json())) - self.assertEqual(doc1, tmpdoc) - # -- end of decrypt and compare + self.assertGetEncryptedDoc( + db, 'doc-here', 'replica:1', '{"value": "here"}', + False) self.assertEqual( (10, 'T-sid'), db._get_replica_gen_and_trans_id('replica')) self.assertEqual([], other_changes) @@ -472,14 +463,9 @@ class TestLeapSyncTarget( new_gen, trans_id = remote_target.sync_exchange( [(doc2, 11, 'T-sud')], 'replica', last_known_generation=0, last_known_trans_id=None, return_doc_cb=receive_doc) - # -- (possibly) decrypt and compare - tmpdoc = db.get_doc('doc-here2') - if leap_backend.ENC_SCHEME_KEY in tmpdoc.content: - tmpdoc.set_json( - leap_backend.decrypt_doc_json( - self._soledad._crypto, tmpdoc.doc_id, tmpdoc.get_json())) - self.assertEqual(doc2, tmpdoc) - # -- end of decrypt and compare + self.assertGetEncryptedDoc( + db, 'doc-here2', 'replica:1', '{"value": "here2"}', + False) self.assertEqual( (11, 'T-sud'), db._get_replica_gen_and_trans_id('replica')) self.assertEqual(2, new_gen) @@ -514,14 +500,9 @@ class TestLeapSyncTarget( db = self.request_state.open_database('test') self.assertEqual(1, len(replica_uid_box)) self.assertEqual(db._replica_uid, replica_uid_box[0]) - # -- (possibly) decrypt and compare - tmpdoc = db.get_doc('doc-here') - if leap_backend.ENC_SCHEME_KEY in tmpdoc.content: - tmpdoc.set_json( - leap_backend.decrypt_doc_json( - self._soledad._crypto, tmpdoc.doc_id, tmpdoc.get_json())) - self.assertEqual(doc, tmpdoc) - # -- end of decrypt and compare + self.assertGetEncryptedDoc( + db, 'doc-here', 'replica:1', '{"value": "here"}', False) + #----------------------------------------------------------------------------- # The following tests come from `u1db.tests.test_https`. @@ -635,28 +616,27 @@ class LeapDatabaseSyncTargetTests( This test was adapted to decrypt remote content before assert. """ - doc = self.make_document('doc-id', 'replica:1', tests.simple_doc) docs_by_gen = [ - (doc, 10, + (self.make_document('doc-id', 'replica:1', tests.simple_doc), 10, 'T-sid')] new_gen, trans_id = self.st.sync_exchange( docs_by_gen, 'replica', last_known_generation=0, last_known_trans_id=None, return_doc_cb=self.receive_doc) - # -- (possibly) decrypt and compare - tmpdoc = self.db.get_doc('doc-id') - if leap_backend.ENC_SCHEME_KEY in tmpdoc.content: - tmpdoc.set_json( - leap_backend.decrypt_doc_json( - self._soledad._crypto, tmpdoc.doc_id, tmpdoc.get_json())) - self.assertEqual(doc, tmpdoc) - # -- end of decrypt and compare + self.assertGetEncryptedDoc( + self.db, 'doc-id', 'replica:1', tests.simple_doc, False) self.assertTransactionLog(['doc-id'], self.db) last_trans_id = self.getLastTransId(self.db) self.assertEqual(([], 1, last_trans_id), (self.other_changes, new_gen, last_trans_id)) self.assertEqual(10, self.st.get_sync_info('replica')[3]) + def test_sync_exchange_push_many(self): + """ + Test sync exchange. + + This test was adapted to decrypt remote content before assert. + """ docs_by_gen = [ (self.make_document('doc-id', 'replica:1', tests.simple_doc), 10, 'T-1'), (self.make_document('doc-id2', 'replica:1', tests.nested_doc), 11, @@ -664,33 +644,25 @@ class LeapDatabaseSyncTargetTests( new_gen, trans_id = self.st.sync_exchange( docs_by_gen, 'replica', last_known_generation=0, last_known_trans_id=None, return_doc_cb=self.receive_doc) - # -- (possibly) decrypt and compare - tmpdoc = self.db.get_doc('doc-id') - if leap_backend.ENC_SCHEME_KEY in tmpdoc.content: - tmpdoc.set_json( - leap_backend.decrypt_doc_json( - self._soledad._crypto, tmpdoc.doc_id, tmpdoc.get_json())) - self.assertEqual( - self.make_document('doc-id', 'replica:1', tests.simple_doc), - tmpdoc) - # -- end of decrypt and compare - # -- (possibly) decrypt and compare - tmpdoc = self.db.get_doc('doc-id2') - if leap_backend.ENC_SCHEME_KEY in tmpdoc.content: - tmpdoc.set_json( - leap_backend.decrypt_doc_json( - self._soledad._crypto, tmpdoc.doc_id, tmpdoc.get_json())) - self.assertEqual( - self.make_document('doc-id2', 'replica:1', tests.nested_doc), - tmpdoc) - # -- end of decrypt and compare + self.assertGetEncryptedDoc( + self.db, 'doc-id', 'replica:1', tests.simple_doc, False) + self.assertGetEncryptedDoc( + self.db, 'doc-id2', 'replica:1', tests.nested_doc, False) self.assertTransactionLog(['doc-id', 'doc-id2'], self.db) last_trans_id = self.getLastTransId(self.db) self.assertEqual(([], 2, last_trans_id), (self.other_changes, new_gen, trans_id)) self.assertEqual(11, self.st.get_sync_info('replica')[3]) + def test_sync_exchange_returns_many_new_docs(self): + """ + Test sync exchange. + + This test was adapted to avoid JSON serialization comparison as local + and remote representations might differ. It looks directly at the + doc's contents instead. + """ doc = self.db.create_doc_from_json(tests.simple_doc) doc2 = self.db.create_doc_from_json(tests.nested_doc) self.assertTransactionLog([doc.doc_id, doc2.doc_id], self.db) diff --git a/src/leap/soledad/tests/test_sqlcipher.py b/src/leap/soledad/tests/test_sqlcipher.py index b16b991d..c4282c0f 100644 --- a/src/leap/soledad/tests/test_sqlcipher.py +++ b/src/leap/soledad/tests/test_sqlcipher.py @@ -53,7 +53,6 @@ from leap.soledad.backends.leap_backend import ( LeapDocument, EncryptionSchemes, decrypt_doc_json, - encrypt_doc_json, ENC_JSON_KEY, ENC_SCHEME_KEY, MAC_KEY, @@ -562,12 +561,9 @@ class SQLCipherDatabaseSyncTests( self.sync(self.db1, self.db2) # make sure db2 now has the exact same thing doc1 = self.db1.get_doc('doc') - doc2 = self.db2.get_doc('doc') - if ENC_SCHEME_KEY in doc2.content: - doc2.set_json( - decrypt_doc_json( - self._soledad._crypto, doc2.doc_id, doc2.get_json())) - self.assertEqual(doc1, doc2) + self.assertGetEncryptedDoc( + self.db2, + doc1.doc_id, doc1.rev, doc1.get_json(), False) def test_sync_autoresolves_moar_backwards(self): # here we would test that when a database that has a conflicted @@ -654,16 +650,8 @@ class SQLCipherDatabaseSyncTests( self.db2 = self.create_database('test2', 'target') doc = self.db1.create_doc_from_json(tests.simple_doc) self.assertEqual(1, self.sync(self.db1, self.db2)) - exp_doc = self.make_document( - doc.doc_id, doc.rev, tests.simple_doc, False) - doc2 = self.db2.get_doc(doc.doc_id) - # decrypt to compare it it is the case - if ENC_SCHEME_KEY in doc2.content: - doc2 = self.db2.get_doc(doc.doc_id) - doc2.set_json( - decrypt_doc_json( - self._soledad._crypto, doc.doc_id, doc2.get_json())) - self.assertEqual(exp_doc, doc2) + self.assertGetEncryptedDoc( + self.db2, doc.doc_id, doc.rev, tests.simple_doc, False) self.assertEqual(1, self.db1._get_replica_gen_and_trans_id('test2')[0]) self.assertEqual(1, self.db2._get_replica_gen_and_trans_id('test1')[0]) self.assertLastExchangeLog( @@ -713,16 +701,8 @@ class SQLCipherSyncTargetTests( new_gen, trans_id = self.st.sync_exchange( docs_by_gen, 'replica', last_known_generation=0, last_known_trans_id=None, return_doc_cb=self.receive_doc) - # decrypt doc1 for comparison if needed - tmpdoc = self.db.get_doc('doc-id') - if ENC_SCHEME_KEY in tmpdoc.content: - tmpdoc.set_json( - decrypt_doc_json( - self._soledad._crypto, tmpdoc.doc_id, - tmpdoc.get_json())) - self.assertEqual(tmpdoc.rev, 'replica:1') - self.assertEqual(tmpdoc.content, json.loads(tests.simple_doc)) - self.assertFalse(tmpdoc.has_conflicts) + self.assertGetEncryptedDoc( + self.db, 'doc-id', 'replica:1', tests.simple_doc, False) self.assertTransactionLog(['doc-id'], self.db) last_trans_id = self.getLastTransId(self.db) self.assertEqual(([], 1, last_trans_id), @@ -735,33 +715,16 @@ class SQLCipherSyncTargetTests( sever-side. """ docs_by_gen = [ - (self.make_document('doc-id', 'replica:1', tests.simple_doc), 10, - 'T-1'), + (self.make_document('doc-id', 'replica:1', tests.simple_doc), 10, 'T-1'), (self.make_document('doc-id2', 'replica:1', tests.nested_doc), 11, 'T-2')] new_gen, trans_id = self.st.sync_exchange( docs_by_gen, 'replica', last_known_generation=0, last_known_trans_id=None, return_doc_cb=self.receive_doc) - # decrypt doc1 for comparison if needed - tmpdoc1 = self.db.get_doc('doc-id') - if ENC_SCHEME_KEY in tmpdoc1.content: - tmpdoc1.set_json( - decrypt_doc_json( - self._soledad._crypto, tmpdoc1.doc_id, - tmpdoc1.get_json())) - self.assertEqual(tmpdoc1.rev, 'replica:1') - self.assertEqual(tmpdoc1.content, json.loads(tests.simple_doc)) - self.assertFalse(tmpdoc1.has_conflicts) - # decrypt doc2 for comparison if needed - tmpdoc2 = self.db.get_doc('doc-id2') - if ENC_SCHEME_KEY in tmpdoc2.content: - tmpdoc2.set_json( - decrypt_doc_json( - self._soledad._crypto, tmpdoc2.doc_id, - tmpdoc2.get_json())) - self.assertEqual(tmpdoc2.rev, 'replica:1') - self.assertEqual(tmpdoc2.content, json.loads(tests.nested_doc)) - self.assertFalse(tmpdoc2.has_conflicts) + self.assertGetEncryptedDoc( + self.db, 'doc-id', 'replica:1', tests.simple_doc, False) + self.assertGetEncryptedDoc( + self.db, 'doc-id2', 'replica:1', tests.nested_doc, False) self.assertTransactionLog(['doc-id', 'doc-id2'], self.db) last_trans_id = self.getLastTransId(self.db) self.assertEqual(([], 2, last_trans_id), -- cgit v1.2.3