summaryrefslogtreecommitdiff
path: root/tests/integration/keymanager/test_migrator.py
blob: 52c9c1cf35aaa25a59b7a30f80df395a10b718b8 (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
# -*- coding: utf-8 -*-
# test_migrator.py
# Copyright (C) 2015-2016 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 the migrator.
"""
from collections import namedtuple
from mock import Mock
from twisted.internet.defer import succeed, inlineCallbacks

from leap.bitmask.keymanager.migrator import KeyDocumentsMigrator, KEY_ID_KEY
from leap.bitmask.keymanager.documents import (
    TAGS_PRIVATE_INDEX,
    KEYMANAGER_ACTIVE_TAG,
    KEYMANAGER_KEY_TAG,
    KEYMANAGER_DOC_VERSION,

    KEY_ADDRESS_KEY,
    KEY_UIDS_KEY,
    KEY_VERSION_KEY,
    KEY_FINGERPRINT_KEY,
    KEY_VALIDATION_KEY,
    KEY_LAST_AUDITED_AT_KEY,
    KEY_ENCR_USED_KEY,
    KEY_SIGN_USED_KEY,
)
from leap.bitmask.keymanager.validation import ValidationLevels
from leap.bitmask.keymanager.testing import KeyManagerWithSoledadTestCase
from leap.bitmask.keymanager.testing import (
    ADDRESS,
    ADDRESS_2,
    KEY_FINGERPRINT,
)


class OpenPGPCryptoTestCase(KeyManagerWithSoledadTestCase):
    @inlineCallbacks
    def test_simple_migration(self):
        get_from_index = self._soledad.get_from_index
        delete_doc = self._soledad.delete_doc
        put_doc = self._soledad.put_doc

        def my_get_from_index(*args):
            docs = []
            if (args[0] == TAGS_PRIVATE_INDEX and
                    args[2] == '0'):
                SoledadDocument = namedtuple("SoledadDocument", ["content"])
                if args[1] == KEYMANAGER_KEY_TAG:
                    docs = [SoledadDocument({
                        KEY_ADDRESS_KEY: [ADDRESS],
                        KEY_ID_KEY: KEY_FINGERPRINT[-16:],
                        KEY_FINGERPRINT_KEY: KEY_FINGERPRINT,
                        KEY_VALIDATION_KEY: str(ValidationLevels.Weak_Chain),
                        KEY_LAST_AUDITED_AT_KEY: 0,
                        KEY_ENCR_USED_KEY: True,
                        KEY_SIGN_USED_KEY: False,
                    })]
                if args[1] == KEYMANAGER_ACTIVE_TAG:
                    docs = [SoledadDocument({
                        KEY_ID_KEY: KEY_FINGERPRINT[-16:],
                    })]
            return succeed(docs)

        self._soledad.get_from_index = my_get_from_index
        self._soledad.delete_doc = Mock(return_value=succeed(None))
        self._soledad.put_doc = Mock(return_value=succeed(None))

        try:
            migrator = KeyDocumentsMigrator(self._soledad)
            yield migrator.migrate()
            call_list = self._soledad.put_doc.call_args_list
        finally:
            self._soledad.get_from_index = get_from_index
            self._soledad.delete_doc = delete_doc
            self._soledad.put_doc = put_doc

        self.assertEqual(len(call_list), 2)
        active = call_list[0][0][0]
        key = call_list[1][0][0]

        self.assertTrue(KEY_ID_KEY not in active.content)
        self.assertEqual(active.content[KEY_VERSION_KEY],
                         KEYMANAGER_DOC_VERSION)
        self.assertEqual(active.content[KEY_FINGERPRINT_KEY], KEY_FINGERPRINT)
        self.assertEqual(active.content[KEY_VALIDATION_KEY],
                         str(ValidationLevels.Weak_Chain))
        self.assertEqual(active.content[KEY_LAST_AUDITED_AT_KEY], 0)
        self.assertEqual(active.content[KEY_ENCR_USED_KEY], True)
        self.assertEqual(active.content[KEY_SIGN_USED_KEY], False)

        self.assertTrue(KEY_ID_KEY not in key.content)
        self.assertTrue(KEY_ADDRESS_KEY not in key.content)
        self.assertTrue(KEY_VALIDATION_KEY not in key.content)
        self.assertTrue(KEY_LAST_AUDITED_AT_KEY not in key.content)
        self.assertTrue(KEY_ENCR_USED_KEY not in key.content)
        self.assertTrue(KEY_SIGN_USED_KEY not in key.content)
        self.assertEqual(key.content[KEY_UIDS_KEY], [ADDRESS])

    @inlineCallbacks
    def test_two_active_docs(self):
        get_from_index = self._soledad.get_from_index
        delete_doc = self._soledad.delete_doc
        put_doc = self._soledad.put_doc

        def my_get_from_index(*args):
            docs = []
            if (args[0] == TAGS_PRIVATE_INDEX and
                    args[2] == '0'):
                SoledadDocument = namedtuple("SoledadDocument", ["content"])
                if args[1] == KEYMANAGER_KEY_TAG:
                    validation = str(ValidationLevels.Provider_Trust)
                    docs = [SoledadDocument({
                        KEY_ADDRESS_KEY: [ADDRESS, ADDRESS_2],
                        KEY_ID_KEY: KEY_FINGERPRINT[-16:],
                        KEY_FINGERPRINT_KEY: KEY_FINGERPRINT,
                        KEY_VALIDATION_KEY: validation,
                        KEY_LAST_AUDITED_AT_KEY: 1984,
                        KEY_ENCR_USED_KEY: True,
                        KEY_SIGN_USED_KEY: False,
                    })]
                if args[1] == KEYMANAGER_ACTIVE_TAG:
                    docs = [
                        SoledadDocument({
                            KEY_ADDRESS_KEY: ADDRESS,
                            KEY_ID_KEY: KEY_FINGERPRINT[-16:],
                        }),
                        SoledadDocument({
                            KEY_ADDRESS_KEY: ADDRESS_2,
                            KEY_ID_KEY: KEY_FINGERPRINT[-16:],
                        }),
                    ]
            return succeed(docs)

        self._soledad.get_from_index = my_get_from_index
        self._soledad.delete_doc = Mock(return_value=succeed(None))
        self._soledad.put_doc = Mock(return_value=succeed(None))

        try:
            migrator = KeyDocumentsMigrator(self._soledad)
            yield migrator.migrate()
            call_list = self._soledad.put_doc.call_args_list
        finally:
            self._soledad.get_from_index = get_from_index
            self._soledad.delete_doc = delete_doc
            self._soledad.put_doc = put_doc

        self.assertEqual(len(call_list), 3)
        for active in [call[0][0] for call in call_list][:2]:
            self.assertTrue(KEY_ID_KEY not in active.content)
            self.assertEqual(active.content[KEY_VERSION_KEY],
                             KEYMANAGER_DOC_VERSION)
            self.assertEqual(active.content[KEY_FINGERPRINT_KEY],
                             KEY_FINGERPRINT)
            self.assertEqual(active.content[KEY_VALIDATION_KEY],
                             str(ValidationLevels.Weak_Chain))
            self.assertEqual(active.content[KEY_LAST_AUDITED_AT_KEY], 0)
            self.assertEqual(active.content[KEY_ENCR_USED_KEY], False)
            self.assertEqual(active.content[KEY_SIGN_USED_KEY], False)