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
|
#
# Copyright (c) 2014 ThoughtWorks, Inc.
#
# Pixelated is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pixelated 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Pixelated. If not, see <http://www.gnu.org/licenses/>.
from mock import patch, MagicMock
from mockito import when
from unittest import TestCase
from pixelated.bitmask_libraries.keymanager import Keymanager
from pixelated.bitmask_libraries.keymanager import UploadKeyError
from pixelated.config import leap_config
from twisted.internet import defer
class KeymanagerTest(TestCase):
def setUp(self):
self.provider = MagicMock()
self.soledad = MagicMock()
self.auth = MagicMock(token='token', auth='auth')
self.leap_keymanager = MagicMock()
with patch('pixelated.bitmask_libraries.keymanager.KeyManager', return_value=self.leap_keymanager):
self.keymanager = Keymanager(self.provider,
self.soledad,
'test_user@some-server.test',
self.auth.token,
self.auth.uuid)
def tearDown(self):
reload(leap_config)
@patch('pixelated.bitmask_libraries.keymanager.KeyManager')
def test_keymanager_is_created(self, keymanager_mock):
when(self.provider)._discover_nicknym_server().thenReturn('nicknym_server')
self.provider.provider_api_cert = 'ca_cert_path'
self.provider.api_uri = 'api_uri'
self.provider.api_version = '1'
self.provider.combined_cerfificates_path = 'combined_ca_bundle'
leap_config.gpg_binary = '/path/to/gpg'
Keymanager(self.provider,
self.soledad,
'test_user@some-server.test',
self.auth.token,
self.auth.uuid)
keymanager_mock.assert_called_with(
'test_user@some-server.test',
'nicknym_server',
self.soledad,
token=self.auth.token,
ca_cert_path='ca_cert_path',
api_uri='api_uri',
api_version='1',
uid=self.auth.uuid,
gpgbinary='/path/to/gpg',
combined_ca_bundle='combined_ca_bundle')
def test_keymanager_generate_openpgp_key_generates_key_correctly(self):
self.keymanager._synchronize_remote_key = MagicMock()
when(self.keymanager)._key_exists('test_user@some-server.test').thenReturn(None)
self.leap_keymanager.gen_key = MagicMock()
self.leap_keymanager.send_key = MagicMock()
self.keymanager.generate_openpgp_key()
self.leap_keymanager.gen_key.assert_called_once()
self.leap_keymanager.send_key.assert_called_once()
def test_keymanager_generate_openpgp_key_doesnt_regenerate_preexisting_key(self):
mock_open_pgp_key = MagicMock()
mock_open_pgp_key.needs_renewal = MagicMock(return_value=False)
when(self.keymanager)._key_exists('test_user@some-server.test').thenReturn(mock_open_pgp_key)
self.leap_keymanager.gen_key = MagicMock()
self.keymanager.generate_openpgp_key()
self.leap_keymanager.gen_key.assert_not_called()
def test_keymanager_generate_openpgp_key_doesnt_upload_preexisting_key(self):
self.keymanager._synchronize_remote_key = MagicMock()
mock_open_pgp_key = MagicMock()
mock_open_pgp_key.needs_renewal = MagicMock(return_value=False)
when(self.keymanager)._key_exists('test_user@some-server.test').thenReturn(mock_open_pgp_key)
self.leap_keymanager.send_key = MagicMock()
self.keymanager.generate_openpgp_key()
self.leap_keymanager.send_key.assert_not_called()
@defer.inlineCallbacks
def test_keymanager_generate_openpgp_key_deletes_key_when_upload_fails(self):
when(self.keymanager)._key_exists('test_user@some-server.test').thenReturn(None)
self.keymanager.delete_key_pair = MagicMock()
when(self.leap_keymanager).send_key().thenRaise(Exception('Could not upload key'))
with self.assertRaises(UploadKeyError):
yield self.keymanager.generate_openpgp_key()
self.keymanager.delete_key_pair.assert_called_once()
@defer.inlineCallbacks
def test_keymanager_regenerate_key_pair_if_current_key_is_about_to_expire(self):
self.keymanager._synchronize_remote_key = MagicMock()
mock_open_pgp_key = MagicMock()
mock_open_pgp_key.needs_renewal = MagicMock(return_value=True)
when(self.keymanager)._key_exists('test_user@some-server.test').thenReturn(mock_open_pgp_key)
yield self.keymanager.generate_openpgp_key()
self.leap_keymanager.regenerate_key.assert_called_once()
self.leap_keymanager.send_key.assert_called_once()
@defer.inlineCallbacks
def test_key_regeneration_does_not_delete_key_when_upload_fails(self):
mock_open_pgp_key = MagicMock()
mock_open_pgp_key.needs_renewal = MagicMock(return_value=True)
when(self.keymanager)._key_exists('test_user@some-server.test').thenReturn(mock_open_pgp_key)
when(self.leap_keymanager).send_key().thenRaise(Exception('Could not upload key'))
self.keymanager.delete_key_pair = MagicMock()
with self.assertRaises(UploadKeyError):
yield self.keymanager.generate_openpgp_key()
self.leap_keymanager.regenerate_key.assert_called_once()
self.keymanager.delete_key_pair.assert_not_called()
@defer.inlineCallbacks
def test_key_is_syncronized_with_server(self):
self.keymanager._is_key_synchronized_with_server = MagicMock(return_value=True)
mock_open_pgp_key = MagicMock()
mock_open_pgp_key.needs_renewal = MagicMock(return_value=False)
when(self.keymanager)._key_exists('test_user@some-server.test').thenReturn(mock_open_pgp_key)
yield self.keymanager.generate_openpgp_key()
self.leap_keymanager.send_key.assert_not_called()
@defer.inlineCallbacks
def test_key_is_not_syncronized_with_server(self):
self.keymanager._is_key_synchronized_with_server = MagicMock(return_value=False)
mock_open_pgp_key = MagicMock()
mock_open_pgp_key.needs_renewal = MagicMock(return_value=False)
when(self.keymanager)._key_exists('test_user@some-server.test').thenReturn(mock_open_pgp_key)
yield self.keymanager.generate_openpgp_key()
self.leap_keymanager.send_key.assert_called_once()
@defer.inlineCallbacks
def test_local_and_remote_keys_are_the_same(self):
mock_current_key = MagicMock()
mock_current_key.fingerprint = 'ABC'
mock_remote_key = MagicMock()
mock_remote_key.fingerprint = 'ABC'
self.keymanager.get_key = MagicMock(return_value=mock_remote_key)
result = yield self.keymanager._is_key_synchronized_with_server(mock_current_key)
self.assertTrue(result)
@defer.inlineCallbacks
def test_local_and_remote_keys_are_not_the_same(self):
mock_current_key = MagicMock()
mock_current_key.fingerprint = 'ABC'
mock_remote_key = MagicMock()
mock_remote_key.fingerprint = '123'
self.keymanager.get_key = MagicMock(return_value=mock_remote_key)
result = yield self.keymanager._is_key_synchronized_with_server(mock_current_key)
self.assertFalse(result)
|