Ejemplo n.º 1
0
 def _setup_pool(self, sync_db=None):
     sync_db = sync_db or self._soledad._sync_db
     return SyncDecrypterPool(
         self._soledad._crypto,
         sync_db,
         source_replica_uid=self._soledad._dbpool.replica_uid,
         insert_doc_cb=self._insert_doc_cb)
Ejemplo n.º 2
0
 def setup():
     pool = SyncDecrypterPool(
         client._crypto,
         client._sync_db,
         source_replica_uid=client._dbpool.replica_uid,
         insert_doc_cb=lambda x, y, z: False)  # ignored
     pool.start(amount)
     request.addfinalizer(pool.stop)
     crypto = client._crypto
     docs = []
     for _ in xrange(amount):
         doc = SoledadDocument(doc_id=uuid4().hex,
                               rev='rev',
                               json=json.dumps(DOC_CONTENT))
         encrypted_content = json.loads(crypto.encrypt_doc(doc))
         docs.append((doc.doc_id, encrypted_content))
     return pool, docs
Ejemplo n.º 3
0
 def setUp(self):
     BaseSoledadTest.setUp(self)
     # setup the pool
     self._pool = SyncDecrypterPool(
         self._soledad._crypto,
         self._soledad._sync_db,
         source_replica_uid=self._soledad._dbpool.replica_uid,
         insert_doc_cb=self._insert_doc_cb)
     # reset the inserted docs mock
     self._inserted_docs = []
Ejemplo n.º 4
0
 def _setup_sync_decr_pool(self):
     """
     Set up the SyncDecrypterPool for deferred decryption.
     """
     if self._sync_decr_pool is None and self._sync_db is not None:
         # initialize syncing queue decryption pool
         self._sync_decr_pool = SyncDecrypterPool(
             self._crypto,
             self._sync_db,
             insert_doc_cb=self._insert_doc_cb,
             source_replica_uid=self.source_replica_uid)
Ejemplo n.º 5
0
class TestSyncDecrypterPool(BaseSoledadTest):

    def _insert_doc_cb(self, doc, gen, trans_id):
        """
        Method used to mock the sync's return_doc_cb callback.
        """
        self._inserted_docs.append((doc, gen, trans_id))

    def setUp(self):
        BaseSoledadTest.setUp(self)
        # setup the pool
        self._pool = SyncDecrypterPool(
            self._soledad._crypto,
            self._soledad._sync_db,
            source_replica_uid=self._soledad._dbpool.replica_uid,
            insert_doc_cb=self._insert_doc_cb)
        # reset the inserted docs mock
        self._inserted_docs = []

    def tearDown(self):
        if self._pool.running:
            self._pool.stop()
        BaseSoledadTest.tearDown(self)

    def test_insert_received_doc(self):
        """
        Test that one document added to the pool is inserted using the
        callback.
        """
        self._pool.start(1)
        self._pool.insert_received_doc(
            DOC_ID, DOC_REV, "{}", 1, "trans_id", 1)

        def _assert_doc_was_inserted(_):
            self.assertEqual(
                self._inserted_docs,
                [(SoledadDocument(DOC_ID, DOC_REV, "{}"), 1, u"trans_id")])

        self._pool.deferred.addCallback(_assert_doc_was_inserted)
        return self._pool.deferred

    def test_insert_received_doc_many(self):
        """
        Test that many documents added to the pool are inserted using the
        callback.
        """
        many = 100
        self._pool.start(many)

        # insert many docs in the pool
        for i in xrange(many):
            gen = idx = i + 1
            doc_id = "doc_id: %d" % idx
            rev = "rev: %d" % idx
            content = {'idx': idx}
            trans_id = "trans_id: %d" % idx
            self._pool.insert_received_doc(
                doc_id, rev, content, gen, trans_id, idx)

        def _assert_doc_was_inserted(_):
            self.assertEqual(many, len(self._inserted_docs))
            idx = 1
            for doc, gen, trans_id in self._inserted_docs:
                expected_gen = idx
                expected_doc_id = "doc_id: %d" % idx
                expected_rev = "rev: %d" % idx
                expected_content = json.dumps({'idx': idx})
                expected_trans_id = "trans_id: %d" % idx

                self.assertEqual(expected_doc_id, doc.doc_id)
                self.assertEqual(expected_rev, doc.rev)
                self.assertEqual(expected_content, json.dumps(doc.content))
                self.assertEqual(expected_gen, gen)
                self.assertEqual(expected_trans_id, trans_id)

                idx += 1

        self._pool.deferred.addCallback(_assert_doc_was_inserted)
        return self._pool.deferred

    def test_insert_encrypted_received_doc(self):
        """
        Test that one encrypted document added to the pool is decrypted and
        inserted using the callback.
        """
        crypto = self._soledad._crypto
        doc = SoledadDocument(
            doc_id=DOC_ID, rev=DOC_REV, json=json.dumps(DOC_CONTENT))
        encrypted_content = json.loads(crypto.encrypt_doc(doc))

        # insert the encrypted document in the pool
        self._pool.start(1)
        self._pool.insert_encrypted_received_doc(
            DOC_ID, DOC_REV, encrypted_content, 1, "trans_id", 1)

        def _assert_doc_was_decrypted_and_inserted(_):
            self.assertEqual(self._inserted_docs, [(doc, 1, u"trans_id")])

        self._pool.deferred.addCallback(
            _assert_doc_was_decrypted_and_inserted)
        return self._pool.deferred

    def test_insert_encrypted_received_doc_many(self):
        """
        Test that many encrypted documents added to the pool are decrypted and
        inserted using the callback.
        """
        crypto = self._soledad._crypto
        many = 100
        self._pool.start(many)

        # insert many encrypted docs in the pool
        for i in xrange(many):
            gen = idx = i + 1
            doc_id = "doc_id: %d" % idx
            rev = "rev: %d" % idx
            content = {'idx': idx}
            trans_id = "trans_id: %d" % idx

            doc = SoledadDocument(
                doc_id=doc_id, rev=rev, json=json.dumps(content))

            encrypted_content = json.loads(crypto.encrypt_doc(doc))

            self._pool.insert_encrypted_received_doc(
                doc_id, rev, encrypted_content, gen, trans_id, idx)

        def _assert_docs_were_decrypted_and_inserted(_):
            self.assertEqual(many, len(self._inserted_docs))
            idx = 1
            for doc, gen, trans_id in self._inserted_docs:
                expected_gen = idx
                expected_doc_id = "doc_id: %d" % idx
                expected_rev = "rev: %d" % idx
                expected_content = json.dumps({'idx': idx})
                expected_trans_id = "trans_id: %d" % idx

                self.assertEqual(expected_doc_id, doc.doc_id)
                self.assertEqual(expected_rev, doc.rev)
                self.assertEqual(expected_content, json.dumps(doc.content))
                self.assertEqual(expected_gen, gen)
                self.assertEqual(expected_trans_id, trans_id)

                idx += 1

        self._pool.deferred.addCallback(
            _assert_docs_were_decrypted_and_inserted)
        return self._pool.deferred