summaryrefslogtreecommitdiff
path: root/testing/test_soledad/util.py
blob: 4a705396f44d5998908085c39e341696e9668abb (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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# -*- CODING: UTF-8 -*-
# util.py
# Copyright (C) 2013 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/>.

"""
Utilities used by multiple test suites.
"""

import os
import random
import string
import couchdb
import pytest

from uuid import uuid4
from mock import Mock
from urlparse import urljoin
from StringIO import StringIO
from pysqlcipher import dbapi2

from twisted.trial import unittest

from leap.common.testing.basetest import BaseLeapTest

from leap.soledad.common import l2db
from leap.soledad.common.l2db import sync
from leap.soledad.common.l2db.remote import http_database

from leap.soledad.common.document import SoledadDocument
from leap.soledad.common.couch import CouchDatabase
from leap.soledad.common.couch.state import CouchServerState


from leap.soledad.client import Soledad
from leap.soledad.client import http_target
from leap.soledad.client import auth
from leap.soledad.client.sqlcipher import SQLCipherDatabase
from leap.soledad.client.sqlcipher import SQLCipherOptions
from leap.soledad.client._crypto import is_symmetrically_encrypted

from leap.soledad.server import SoledadApp
from leap.soledad.server.auth import SoledadTokenAuthMiddleware


PASSWORD = '123456'
ADDRESS = 'leap@leap.se'


def make_local_db_and_target(test):
    db = test.create_database('test')
    st = db.get_sync_target()
    return db, st


def make_document_for_test(test, doc_id, rev, content, has_conflicts=False):
    return SoledadDocument(doc_id, rev, content, has_conflicts=has_conflicts)


def make_sqlcipher_database_for_test(test, replica_uid):
    db = SQLCipherDatabase(
        SQLCipherOptions(':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 = make_sqlcipher_database_for_test(test, None)
    tmpfile = StringIO()
    for line in db._db_handle.iterdump():
        if 'sqlite_sequence' not 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


SQLCIPHER_SCENARIOS = [
    ('sqlcipher', {'make_database_for_test': make_sqlcipher_database_for_test,
                   'copy_database_for_test': copy_sqlcipher_database_for_test,
                   'make_document_for_test': make_document_for_test, }),
]


def make_soledad_app(state):
    return SoledadApp(state)


def make_token_soledad_app(state):
    app = SoledadApp(state)

    def _verify_authentication_data(uuid, auth_data):
        if uuid.startswith('user-') and auth_data == 'auth-token':
            return True
        return False

    # we test for action authorization in leap.soledad.common.tests.test_server
    def _verify_authorization(uuid, environ):
        return True

    application = SoledadTokenAuthMiddleware(app)
    application._verify_authentication_data = _verify_authentication_data
    application._verify_authorization = _verify_authorization
    return application


def make_soledad_document_for_test(test, doc_id, rev, content,
                                   has_conflicts=False):
    return SoledadDocument(
        doc_id, rev, content, has_conflicts=has_conflicts)


def make_token_http_database_for_test(test, replica_uid):
    test.startServer()
    test.request_state._create_database(replica_uid)

    class _HTTPDatabaseWithToken(
            http_database.HTTPDatabase, auth.TokenBasedAuth):

        def set_token_credentials(self, uuid, token):
            auth.TokenBasedAuth.set_token_credentials(self, uuid, token)

        def _sign_request(self, method, url_query, params):
            return auth.TokenBasedAuth._sign_request(
                self, method, url_query, params)

    http_db = _HTTPDatabaseWithToken(test.getURL('test'))
    http_db.set_token_credentials('user-uuid', 'auth-token')
    return http_db


def copy_token_http_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.
    http_db = test.request_state._copy_database(db)
    http_db.set_token_credentials(http_db, 'user-uuid', 'auth-token')
    return http_db


def sync_via_synchronizer(test, db_source, db_target, trace_hook=None,
                          trace_hook_shallow=None):
    target = db_target.get_sync_target()
    trace_hook = trace_hook or trace_hook_shallow
    if trace_hook:
        target._set_trace_hook(trace_hook)
    return sync.Synchronizer(db_source, target).sync()


class MockedSharedDBTest(object):

    def get_default_shared_mock(self, put_doc_side_effect=None,
                                get_doc_return_value=None):
        """
        Get a default class for mocking the shared DB
        """
        class defaultMockSharedDB(object):
            get_doc = Mock(return_value=get_doc_return_value)
            put_doc = Mock(side_effect=put_doc_side_effect)
            open = Mock(return_value=None)
            close = Mock(return_value=None)
            syncable = True

            def __call__(self):
                return self
        return defaultMockSharedDB


def soledad_sync_target(
        test, path, source_replica_uid=uuid4().hex):
    creds = {'token': {
        'uuid': 'user-uuid',
        'token': 'auth-token',
    }}
    return http_target.SoledadHTTPSyncTarget(
        test.getURL(path),
        source_replica_uid,
        creds,
        test._soledad._crypto,
        None)  # cert_file


# redefine the base leap test class so it inherits from twisted trial's
# TestCase. This is needed so trial knows that it has to manage a reactor and
# wait for deferreds returned by tests to be fired.

BaseLeapTest = type(
    'BaseLeapTest', (unittest.TestCase,), dict(BaseLeapTest.__dict__))


class BaseSoledadTest(BaseLeapTest, MockedSharedDBTest):

    """
    Instantiates Soledad for usage in tests.
    """

    @pytest.mark.usefixtures("method_tmpdir")
    def setUp(self):
        # The following snippet comes from BaseLeapTest.setUpClass, but we
        # repeat it here because twisted.trial does not work with
        # setUpClass/tearDownClass.

        self.home = self.tempdir

        # config info
        self.db1_file = os.path.join(self.tempdir, "db1.u1db")
        self.db2_file = os.path.join(self.tempdir, "db2.u1db")
        self.email = ADDRESS
        # open test dbs
        self._db1 = l2db.open(self.db1_file, create=True,
                              document_factory=SoledadDocument)
        self._db2 = l2db.open(self.db2_file, create=True,
                              document_factory=SoledadDocument)
        # get a random prefix for each test, so we do not mess with
        # concurrency during initialization and shutting down of
        # each local db.
        self.rand_prefix = ''.join(
            map(lambda x: random.choice(string.ascii_letters), range(6)))

        # initialize soledad by hand so we can control keys
        # XXX check if this soledad is actually used
        self._soledad = self._soledad_instance(
            prefix=self.rand_prefix, user=self.email)

    def tearDown(self):
        self._db1.close()
        self._db2.close()
        self._soledad.close()

        def _delete_temporary_dirs():
            # XXX should not access "private" attrs
            for f in [self._soledad.local_db_path,
                      self._soledad.secrets.secrets_path]:
                if os.path.isfile(f):
                    os.unlink(f)

        from twisted.internet import reactor
        reactor.addSystemEventTrigger(
            "after", "shutdown", _delete_temporary_dirs)

    def _soledad_instance(self, user=ADDRESS, passphrase=u'123',
                          prefix='',
                          secrets_path='secrets.json',
                          local_db_path='soledad.u1db',
                          server_url='https://127.0.0.1/',
                          cert_file=None,
                          shared_db_class=None,
                          auth_token='auth-token'):

        def _put_doc_side_effect(doc):
            self._doc_put = doc

        if shared_db_class is not None:
            MockSharedDB = shared_db_class
        else:
            MockSharedDB = self.get_default_shared_mock(
                _put_doc_side_effect)

        soledad = Soledad(
            user,
            passphrase,
            secrets_path=os.path.join(
                self.tempdir, prefix, secrets_path),
            local_db_path=os.path.join(
                self.tempdir, prefix, local_db_path),
            server_url=server_url,  # Soledad will fail if not given an url
            cert_file=cert_file,
            shared_db=MockSharedDB(),
            auth_token=auth_token)
        self.addCleanup(soledad.close)
        return soledad

    @pytest.inlineCallbacks
    def assertGetEncryptedDoc(
            self, db, doc_id, doc_rev, content, has_conflicts):
        """
        Assert that the document in the database looks correct.
        """
        exp_doc = self.make_document(doc_id, doc_rev, content,
                                     has_conflicts=has_conflicts)
        doc = db.get_doc(doc_id)

        if is_symmetrically_encrypted(doc.content['raw']):
            crypt = self._soledad._crypto
            decrypted = yield crypt.decrypt_doc(doc)
            doc.set_json(decrypted)
        self.assertEqual(exp_doc.doc_id, doc.doc_id)
        self.assertEqual(exp_doc.rev, doc.rev)
        self.assertEqual(exp_doc.has_conflicts, doc.has_conflicts)
        self.assertEqual(exp_doc.content, doc.content)


@pytest.mark.usefixtures("couch_url")
class CouchDBTestCase(unittest.TestCase, MockedSharedDBTest):

    """
    TestCase base class for tests against a real CouchDB server.
    """

    def setUp(self):
        """
        Make sure we have a CouchDB instance for a test.
        """
        self.couch_server = couchdb.Server(self.couch_url)

    def delete_db(self, name):
        try:
            self.couch_server.delete(name)
        except:
            # ignore if already missing
            pass


class CouchServerStateForTests(CouchServerState):

    """
    This is a slightly modified CouchDB server state that allows for creating
    a database.

    Ordinarily, the CouchDB server state does not allow some operations,
    because for security purposes the Soledad Server should not even have
    enough permissions to perform them. For tests, we allow database creation,
    otherwise we'd have to create those databases in setUp/tearDown methods,
    which is less pleasant than allowing the db to be automatically created.
    """

    def __init__(self, *args, **kwargs):
        self.dbs = []
        super(CouchServerStateForTests, self).__init__(*args, **kwargs)

    def _create_database(self, replica_uid=None, dbname=None):
        """
        Create db and append to a list, allowing test to close it later
        """
        dbname = dbname or ('test-%s' % uuid4().hex)
        db = CouchDatabase.open_database(
            urljoin(self.couch_url, dbname),
            True,
            replica_uid=replica_uid or 'test')
        self.dbs.append(db)
        return db

    def ensure_database(self, dbname):
        db = self._create_database(dbname=dbname)
        return db, db.replica_uid


class SoledadWithCouchServerMixin(
        BaseSoledadTest,
        CouchDBTestCase):

    def setUp(self):
        CouchDBTestCase.setUp(self)
        BaseSoledadTest.setUp(self)
        main_test_class = getattr(self, 'main_test_class', None)
        if main_test_class is not None:
            main_test_class.setUp(self)

    def tearDown(self):
        main_test_class = getattr(self, 'main_test_class', None)
        if main_test_class is not None:
            main_test_class.tearDown(self)
        # delete the test database
        BaseSoledadTest.tearDown(self)
        CouchDBTestCase.tearDown(self)

    def make_app(self):
        self.request_state = CouchServerStateForTests(self.couch_url)
        self.addCleanup(self.delete_dbs)
        return self.make_app_with_state(self.request_state)

    def delete_dbs(self):
        for db in self.request_state.dbs:
            self.delete_db(db._dbname)