summaryrefslogtreecommitdiff
path: root/testing/tests/sync/test_encdecpool.py
blob: 0aa17682bb0e6032e657fe0553b218d36ec0c271 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# -*- coding: utf-8 -*-
# test_encdecpool.py
# Copyright (C) 2015 LEAP
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Tests for encryption and decryption pool.
"""
import json
from random import shuffle

from mock import MagicMock
from twisted.internet.defer import inlineCallbacks

from leap.soledad.client.encdecpool import SyncEncrypterPool
from leap.soledad.client.encdecpool import SyncDecrypterPool

from leap.soledad.common.document import SoledadDocument
from test_soledad.util import BaseSoledadTest
from twisted.internet import defer

DOC_ID = "mydoc"
DOC_REV = "rev"
DOC_CONTENT = {'simple': 'document'}


class TestSyncEncrypterPool(BaseSoledadTest):

    def setUp(self):
        BaseSoledadTest.setUp(self)
        crypto = self._soledad._crypto
        sync_db = self._soledad._sync_db
        self._pool = SyncEncrypterPool(crypto, sync_db)
        self._pool.start()

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

    @inlineCallbacks
    def test_get_encrypted_doc_returns_none(self):
        """
        Test that trying to get an encrypted doc from the pool returns None if
        the document was never added for encryption.
        """
        doc = yield self._pool.get_encrypted_doc(DOC_ID, DOC_REV)
        self.assertIsNone(doc)

    @inlineCallbacks
    def test_encrypt_doc_and_get_it_back(self):
        """
        Test that the pool actually encrypts a document added to the queue.
        """
        doc = SoledadDocument(
            doc_id=DOC_ID, rev=DOC_REV, json=json.dumps(DOC_CONTENT))
        self._pool.encrypt_doc(doc)

        # exhaustivelly attempt to get the encrypted document
        encrypted = None
        attempts = 0
        while encrypted is None and attempts < 10:
            encrypted = yield self._pool.get_encrypted_doc(DOC_ID, DOC_REV)
            attempts += 1

        self.assertIsNotNone(encrypted)
        self.assertTrue(attempts < 10)


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_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)

    def setUp(self):
        BaseSoledadTest.setUp(self)
        # setup the pool
        self._pool = self._setup_pool()
        # 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_looping_control(self):
        """
        Start and stop cleanly.
        """
        self._pool.start(10)
        self.assertTrue(self._pool.running)
        self._pool.stop()
        self.assertFalse(self._pool.running)
        self.assertTrue(self._pool.deferred.called)

    def test_sync_id_col_is_created_if_non_existing_in_docs_recvd_table(self):
        """
        Test that docs_received table is migrated, and has the sync_id column
        """
        mock_run_query = MagicMock(return_value=defer.succeed(None))
        mock_sync_db = MagicMock()
        mock_sync_db.runQuery = mock_run_query
        pool = self._setup_pool(mock_sync_db)
        d = pool.start(10)
        pool.stop()

        def assert_trial_to_create_sync_id_column(_):
            mock_run_query.assert_called_once_with(
                "ALTER TABLE docs_received ADD COLUMN sync_id")

        d.addCallback(assert_trial_to_create_sync_id_column)
        return d

    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(1, len(self._inserted_docs))
            self.assertEqual(self._inserted_docs, [(doc, 1, u"trans_id")])

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

    @inlineCallbacks
    def test_processing_order(self):
        """
        This test ensures that processing of documents only occur if there is
        a sequence in place.
        """
        crypto = self._soledad._crypto

        docs = []
        for i in xrange(1, 10):
            i = str(i)
            doc = SoledadDocument(
                doc_id=DOC_ID + i, rev=DOC_REV + i,
                json=json.dumps(DOC_CONTENT))
            encrypted_content = json.loads(crypto.encrypt_doc(doc))
            docs.append((doc, encrypted_content))

        # insert the encrypted document in the pool
        yield self._pool.start(10)  # pool is expecting to process 10 docs
        self._pool._loop.stop()  # we are processing manually
        # first three arrives, forming a sequence
        for i, (doc, encrypted_content) in enumerate(docs[:3]):
            gen = idx = i + 1
            yield self._pool.insert_encrypted_received_doc(
                doc.doc_id, doc.rev, encrypted_content, gen, "trans_id", idx)

        # last one arrives alone, so it can't be processed
        doc, encrypted_content = docs[-1]
        yield self._pool.insert_encrypted_received_doc(
            doc.doc_id, doc.rev, encrypted_content, 10, "trans_id", 10)

        yield self._pool._decrypt_and_recurse()

        self.assertEqual(3, self._pool._processed_docs)

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

        # 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))
            docs.append((doc_id, rev, encrypted_content, gen,
                         trans_id, idx))
        shuffle(docs)

        for doc in docs:
            self._pool.insert_encrypted_received_doc(*doc)

        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

    @inlineCallbacks
    def test_pool_reuse(self):
        """
        The pool is reused between syncs, this test verifies that
        reusing is fine.
        """
        for i in xrange(3):
            yield self.test_insert_encrypted_received_doc_many(5)
            self._inserted_docs = []
            decrypted_docs = yield self._pool._get_docs(encrypted=False)
            # check that decrypted docs staging is clean
            self.assertEquals([], decrypted_docs)
            self._pool.stop()