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
|
# Copyright (C) 2017 Jan Jancar
#
# This file is a part of the Mailman PGP plugin.
#
# 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/>.
""""""
import unittest
from mailman.app.lifecycle import create_list
from mailman.email.message import Message
from mailman.interfaces.subscriptions import ISubscriptionManager
from mailman.interfaces.usermanager import IUserManager
from mailman.runners.command import CommandRunner
from mailman.testing.helpers import get_queue_messages, make_testable_runner
from mailman.utilities.datetime import now
from zope.component import getUtility
from mailman_pgp.config import mm_config
from mailman_pgp.database import transaction
from mailman_pgp.model.address import PGPAddress
from mailman_pgp.model.list import PGPMailingList
from mailman_pgp.pgp.mime import MIMEWrapper
from mailman_pgp.pgp.tests.base import load_key
from mailman_pgp.pgp.wrapper import PGPWrapper
from mailman_pgp.testing.layers import PGPConfigLayer
from mailman_pgp.workflows.base import CONFIRM_REQUEST
from mailman_pgp.workflows.subscription import OpenSubscriptionPolicy
def _create_plain(from_hdr, to_hdr, subject_hdr, payload):
message = Message()
message['From'] = from_hdr
message['To'] = to_hdr
message['Subject'] = subject_hdr
message.set_payload(payload)
return message
def _create_mixed(from_hdr, to_hdr, subject_hdr):
message = Message()
message['From'] = from_hdr
message['To'] = to_hdr
message['Subject'] = subject_hdr
message.set_type('multipart/mixed')
return message
class TestPreDispatch(unittest.TestCase):
layer = PGPConfigLayer
def setUp(self):
self.mlist = create_list('test@example.com')
def test_no_arguments(self):
message = _create_plain('bart@example.com', 'test@example.com',
'key', '')
mm_config.switchboards['command'].enqueue(message,
listid='test.example.com')
make_testable_runner(CommandRunner, 'command').run()
items = get_queue_messages('virgin', expected_count=1)
results_msg = items[0].msg
self.assertIn('No sub-command specified', results_msg.get_payload())
def test_wrong_subcommand(self):
message = _create_plain('bart@example.com', 'test@example.com',
'key wrooooooong', '')
mm_config.switchboards['command'].enqueue(message,
listid='test.example.com')
make_testable_runner(CommandRunner, 'command').run()
items = get_queue_messages('virgin', expected_count=1)
results_msg = items[0].msg
self.assertIn('Wrong sub-command specified', results_msg.get_payload())
def test_no_pgp_list(self):
message = _create_plain('bart@example.com', 'test@example.com',
'key set', '')
mm_config.switchboards['command'].enqueue(message,
listid='test.example.com')
make_testable_runner(CommandRunner, 'command').run()
items = get_queue_messages('virgin', expected_count=1)
results_msg = items[0].msg
self.assertIn("This mailing list doesn't have pgp enabled.",
results_msg.get_payload())
class TestPreSubscription(unittest.TestCase):
layer = PGPConfigLayer
def setUp(self):
self.mlist = create_list('test@example.com', style_name='pgp-default')
self.pgp_list = PGPMailingList.for_list(self.mlist)
self.pgp_list.key = load_key('ecc_p256.priv.asc')
def test_set(self):
self.mlist.subscription_policy = OpenSubscriptionPolicy
bart = getUtility(IUserManager).create_address('bart@example.com',
'Bart Person')
bart.verified_on = now()
token, token_owner, member = ISubscriptionManager(self.mlist).register(
bart)
get_queue_messages('virgin')
bart_key = load_key('rsa_1024.priv.asc')
set_message = _create_mixed('bart@example.com', 'test@example.com',
'Re: key set {}'.format(token))
wrapped_set_message = MIMEWrapper(set_message)
set_message = wrapped_set_message.attach_key(bart_key.pubkey)
mm_config.switchboards['command'].enqueue(set_message,
listid='test.example.com')
make_testable_runner(CommandRunner, 'command').run()
pgp_address = PGPAddress.for_address(bart)
self.assertIsNotNone(pgp_address)
self.assertEqual(pgp_address.key.fingerprint, bart_key.fingerprint)
self.assertEqual(pgp_address.key_fingerprint, bart_key.fingerprint)
self.assertFalse(pgp_address.key_confirmed)
items = get_queue_messages('virgin', expected_count=2)
if items[0].msg['Subject'] == 'The results of your email commands':
results = items[0].msg
confirm_request = items[1].msg
else:
results = items[1].msg
confirm_request = items[0].msg
self.assertIn('Key succesfully set.', results.get_payload())
self.assertIn('Key fingerprint: {}'.format(bart_key.fingerprint),
results.get_payload())
confirm_wrapped = PGPWrapper(confirm_request)
self.assertTrue(confirm_wrapped.is_encrypted())
def test_set_no_token(self):
message = _create_plain('bart@example.com', 'test@example.com',
'key set', '')
mm_config.switchboards['command'].enqueue(message,
listid='test.example.com')
make_testable_runner(CommandRunner, 'command').run()
items = get_queue_messages('virgin', expected_count=1)
results_msg = items[0].msg
self.assertIn('Missing token.', results_msg.get_payload())
def test_set_no_key(self):
message = _create_plain('bart@example.com', 'test@example.com',
'key set token', '')
mm_config.switchboards['command'].enqueue(message,
listid='test.example.com')
make_testable_runner(CommandRunner, 'command').run()
items = get_queue_messages('virgin', expected_count=1)
results_msg = items[0].msg
self.assertIn('No keys attached? Send a key.',
results_msg.get_payload())
def test_set_multiple_keys(self):
bart_key = load_key('rsa_1024.priv.asc')
anne_key = load_key('ecc_p256.priv.asc')
set_message = _create_mixed('bart@example.com', 'test@example.com',
'Re: key set token')
wrapped_set_message = MIMEWrapper(set_message)
set_message = wrapped_set_message.attach_key(bart_key.pubkey)
wrapped_set_message = MIMEWrapper(set_message)
set_message = wrapped_set_message.attach_key(anne_key.pubkey)
mm_config.switchboards['command'].enqueue(set_message,
listid='test.example.com')
make_testable_runner(CommandRunner, 'command').run()
items = get_queue_messages('virgin', expected_count=1)
results_msg = items[0].msg
self.assertIn('More than one key! Send only one key.',
results_msg.get_payload())
def test_set_no_email(self):
bart_key = load_key('rsa_1024.priv.asc')
message = _create_mixed('', 'test@example.com', 'key set token')
wrapped_message = MIMEWrapper(message)
message = wrapped_message.attach_key(bart_key.pubkey)
mm_config.switchboards['command'].enqueue(message,
listid='test.example.com')
make_testable_runner(CommandRunner, 'command').run()
items = get_queue_messages('virgin', expected_count=1)
results_msg = items[0].msg
self.assertIn('No email to subscribe with.', results_msg.get_payload())
def test_set_no_address(self):
bart_key = load_key('rsa_1024.priv.asc')
set_message = _create_mixed('bart@example.com', 'test@example.com',
'key set token')
wrapped_set_message = MIMEWrapper(set_message)
set_message = wrapped_set_message.attach_key(bart_key.pubkey)
mm_config.switchboards['command'].enqueue(set_message,
listid='test.example.com')
make_testable_runner(CommandRunner, 'command').run()
items = get_queue_messages('virgin', expected_count=1)
results_msg = items[0].msg
self.assertIn('No adddress to subscribe with.',
results_msg.get_payload())
def test_confirm(self):
self.mlist.subscription_policy = OpenSubscriptionPolicy
bart = getUtility(IUserManager).create_address('bart@example.com',
'Bart Person')
bart.verified_on = now()
bart_key = load_key('rsa_1024.priv.asc')
token, token_owner, member = ISubscriptionManager(self.mlist).register(
bart, pubkey=bart_key.pubkey)
get_queue_messages('virgin')
with transaction() as t:
pgp_address = PGPAddress(bart)
pgp_address.key = bart_key.pubkey
t.add(pgp_address)
message = _create_plain('bart@example.com', 'test@example.com',
'Re: key confirm {}'.format(token),
CONFIRM_REQUEST.format(bart_key.fingerprint,
token))
wrapped_message = MIMEWrapper(message)
message = wrapped_message.sign(bart_key)
mm_config.switchboards['command'].enqueue(message,
listid='test.example.com')
make_testable_runner(CommandRunner, 'command').run()
pgp_address = PGPAddress.for_address(bart)
self.assertTrue(pgp_address.key_confirmed)
self.assertTrue(self.mlist.is_subscribed(bart))
def test_confirm_encrypted(self):
self.mlist.subscription_policy = OpenSubscriptionPolicy
bart = getUtility(IUserManager).create_address('bart@example.com',
'Bart Person')
bart.verified_on = now()
bart_key = load_key('rsa_1024.priv.asc')
token, token_owner, member = ISubscriptionManager(self.mlist).register(
bart, pubkey=bart_key.pubkey)
get_queue_messages('virgin')
with transaction() as t:
pgp_address = PGPAddress(bart)
pgp_address.key = bart_key.pubkey
t.add(pgp_address)
message = _create_plain('bart@example.com', 'test@example.com',
'Re: key confirm {}'.format(token),
CONFIRM_REQUEST.format(bart_key.fingerprint,
token))
wrapped_message = MIMEWrapper(message)
message = wrapped_message.sign_encrypt(bart_key, self.pgp_list.pubkey,
bart_key.pubkey)
mm_config.switchboards['command'].enqueue(message,
listid='test.example.com')
make_testable_runner(CommandRunner, 'command').run()
pgp_address = PGPAddress.for_address(bart)
self.assertTrue(pgp_address.key_confirmed)
self.assertTrue(self.mlist.is_subscribed(bart))
def test_confirm_no_token(self):
message = _create_plain('bart@example.com', 'test@example.com',
'key confirm', '')
mm_config.switchboards['command'].enqueue(message,
listid='test.example.com')
make_testable_runner(CommandRunner, 'command').run()
items = get_queue_messages('virgin', expected_count=1)
results_msg = items[0].msg
self.assertIn('Missing token.', results_msg.get_payload())
def test_confirm_no_email(self):
message = _create_plain('', 'test@example.com',
'key confirm token', '')
mm_config.switchboards['command'].enqueue(message,
listid='test.example.com')
make_testable_runner(CommandRunner, 'command').run()
items = get_queue_messages('virgin', expected_count=1)
results_msg = items[0].msg
self.assertIn('No email to subscribe with.', results_msg.get_payload())
def test_confirm_no_pgp_address(self):
message = _create_plain('bart@example.com', 'test@example.com',
'key confirm token', '')
mm_config.switchboards['command'].enqueue(message,
listid='test.example.com')
make_testable_runner(CommandRunner, 'command').run()
items = get_queue_messages('virgin', expected_count=1)
results_msg = items[0].msg
self.assertIn('A pgp enabled address not found.',
results_msg.get_payload())
def test_confirm_not_signed(self):
self.mlist.subscription_policy = OpenSubscriptionPolicy
bart = getUtility(IUserManager).create_address('bart@example.com',
'Bart Person')
bart.verified_on = now()
bart_key = load_key('rsa_1024.priv.asc')
token, token_owner, member = ISubscriptionManager(self.mlist).register(
bart, pubkey=bart_key.pubkey)
get_queue_messages('virgin')
with transaction() as t:
pgp_address = PGPAddress(bart)
pgp_address.key = bart_key.pubkey
t.add(pgp_address)
message = _create_plain('bart@example.com', 'test@example.com',
'Re: key confirm {}'.format(token),
CONFIRM_REQUEST.format(bart_key.fingerprint,
token))
mm_config.switchboards['command'].enqueue(message,
listid='test.example.com')
make_testable_runner(CommandRunner, 'command').run()
items = get_queue_messages('virgin', expected_count=1)
results_msg = items[0].msg
self.assertIn('Message not signed, ignoring.',
results_msg.get_payload())
|