summaryrefslogtreecommitdiff
path: root/src/leap/bitmask/mail/incoming/tests/test_incoming_mail.py
blob: 29422eccb74f74000325286a81bf30db7c5fce06 (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
# -*- coding: utf-8 -*-
# test_incoming_mail.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/>.
"""
Test case for leap.mail.incoming.service

@authors: Ruben Pollan, <meskio@sindominio.net>

@license: GPLv3, see included LICENSE file
"""

import json
import os
import tempfile
import uuid

from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.parser import Parser
from mock import Mock

from twisted.internet import defer
from twisted.python import log

from leap.keymanager.errors import KeyAddressMismatch
from leap.mail.adaptors import soledad_indexes as fields
from leap.mail.adaptors.soledad import cleanup_deferred_locks
from leap.mail.adaptors.soledad import SoledadMailAdaptor
from leap.mail.mail import MessageCollection
from leap.mail.mailbox_indexer import MailboxIndexer

from leap.mail.incoming.service import IncomingMail
from leap.mail.rfc3156 import MultipartEncrypted, PGPEncrypted
from leap.mail.testing import KeyManagerWithSoledadTestCase
from leap.mail.testing import ADDRESS, ADDRESS_2
from leap.soledad.common.document import SoledadDocument
from leap.soledad.common.crypto import (
    EncryptionSchemes,
    ENC_JSON_KEY,
    ENC_SCHEME_KEY,
)

HERE = os.path.split(os.path.abspath(__file__))[0]

# TODO: add some tests for encrypted, unencrypted, signed and unsgined messages


class IncomingMailTestCase(KeyManagerWithSoledadTestCase):
    """
    Tests for the incoming mail parser
    """
    NICKSERVER = "http://domain"
    BODY = """
Governments of the Industrial World, you weary giants of flesh and steel, I
come from Cyberspace, the new home of Mind. On behalf of the future, I ask
you of the past to leave us alone. You are not welcome among us. You have
no sovereignty where we gather.
    """
    EMAIL = """from: Test from SomeDomain <%(from)s>
to: %(to)s
subject: independence of cyberspace

%(body)s
    """ % {
        "from": ADDRESS_2,
        "to": ADDRESS,
        "body": BODY
    }

    def setUp(self):
        cleanup_deferred_locks()
        try:
            del self._soledad
            del self.km
        except AttributeError:
            pass

        # pytest handles correctly the setupEnv for the class,
        # but trial ignores it.
        if not getattr(self, 'tempdir', None):
            self.tempdir = tempfile.mkdtemp()

        def getCollection(_):
            adaptor = SoledadMailAdaptor()
            store = self._soledad
            adaptor.store = store
            mbox_indexer = MailboxIndexer(store)
            mbox_name = "INBOX"
            mbox_uuid = str(uuid.uuid4())

            def get_collection_from_mbox_wrapper(wrapper):
                wrapper.uuid = mbox_uuid
                return MessageCollection(
                    adaptor, store,
                    mbox_indexer=mbox_indexer, mbox_wrapper=wrapper)

            d = adaptor.initialize_store(store)
            d.addCallback(lambda _: mbox_indexer.create_table(mbox_uuid))
            d.addCallback(
                lambda _: adaptor.get_or_create_mbox(store, mbox_name))
            d.addCallback(get_collection_from_mbox_wrapper)
            return d

        def setUpFetcher(inbox_collection):
            self.fetcher = IncomingMail(
                self.km,
                self._soledad,
                inbox_collection,
                ADDRESS)

            # The messages don't exist on soledad will fail on deletion
            self.fetcher._delete_incoming_message = Mock(
                return_value=defer.succeed(None))

        d = KeyManagerWithSoledadTestCase.setUp(self)
        d.addCallback(getCollection)
        d.addCallback(setUpFetcher)
        d.addErrback(log.err)
        return d

    def tearDown(self):
        d = KeyManagerWithSoledadTestCase.tearDown(self)
        return d

    def testExtractOpenPGPHeader(self):
        """
        Test the OpenPGP header key extraction
        """
        KEYURL = "https://leap.se/key.txt"
        OpenPGP = "id=12345678; url=\"%s\"; preference=signencrypt" % (KEYURL,)

        message = Parser().parsestr(self.EMAIL)
        message.add_header("OpenPGP", OpenPGP)
        self.fetcher._keymanager.fetch_key = Mock(
            return_value=defer.succeed(None))

        def fetch_key_called(ret):
            self.fetcher._keymanager.fetch_key.assert_called_once_with(
                ADDRESS_2, KEYURL)

        d = self._create_incoming_email(message.as_string())
        d.addCallback(
            lambda email:
            self._mock_soledad_get_from_index(fields.JUST_MAIL_IDX, [email]))
        d.addCallback(lambda _: self.fetcher.fetch())
        d.addCallback(fetch_key_called)
        return d

    def testExtractOpenPGPHeaderInvalidUrl(self):
        """
        Test the OpenPGP header key extraction
        """
        KEYURL = "https://someotherdomain.com/key.txt"
        OpenPGP = "id=12345678; url=\"%s\"; preference=signencrypt" % (KEYURL,)

        message = Parser().parsestr(self.EMAIL)
        message.add_header("OpenPGP", OpenPGP)
        self.fetcher._keymanager.fetch_key = Mock()

        def fetch_key_called(ret):
            self.assertFalse(self.fetcher._keymanager.fetch_key.called)

        d = self._create_incoming_email(message.as_string())
        d.addCallback(
            lambda email:
            self._mock_soledad_get_from_index(fields.JUST_MAIL_IDX, [email]))
        d.addCallback(lambda _: self.fetcher.fetch())
        d.addCallback(fetch_key_called)
        return d

    def testExtractAttachedKey(self):
        KEY = "-----BEGIN PGP PUBLIC KEY BLOCK-----\n..."

        message = MIMEMultipart()
        message.add_header("from", ADDRESS_2)
        key = MIMEApplication("", "pgp-keys")
        key.set_payload(KEY)
        message.attach(key)
        self.fetcher._keymanager.put_raw_key = Mock(
            return_value=defer.succeed(None))

        def put_raw_key_called(_):
            self.fetcher._keymanager.put_raw_key.assert_called_once_with(
                KEY, address=ADDRESS_2)

        d = self._do_fetch(message.as_string())
        d.addCallback(put_raw_key_called)
        return d

    def testExtractInvalidAttachedKey(self):
        KEY = "-----BEGIN PGP PUBLIC KEY BLOCK-----\n..."

        message = MIMEMultipart()
        message.add_header("from", ADDRESS_2)
        key = MIMEApplication("", "pgp-keys")
        key.set_payload(KEY)
        message.attach(key)
        self.fetcher._keymanager.put_raw_key = Mock(
            return_value=defer.fail(KeyAddressMismatch()))

        def put_raw_key_called(_):
            self.fetcher._keymanager.put_raw_key.assert_called_once_with(
                KEY, address=ADDRESS_2)

        d = self._do_fetch(message.as_string())
        d.addCallback(put_raw_key_called)
        d.addErrback(log.err)
        return d

    def testExtractAttachedKeyAndNotOpenPGPHeader(self):
        KEY = "-----BEGIN PGP PUBLIC KEY BLOCK-----\n..."
        KEYURL = "https://leap.se/key.txt"
        OpenPGP = "id=12345678; url=\"%s\"; preference=signencrypt" % (KEYURL,)

        message = MIMEMultipart()
        message.add_header("from", ADDRESS_2)
        message.add_header("OpenPGP", OpenPGP)
        key = MIMEApplication("", "pgp-keys")
        key.set_payload(KEY)
        message.attach(key)

        self.fetcher._keymanager.put_raw_key = Mock(
            return_value=defer.succeed(None))
        self.fetcher._keymanager.fetch_key = Mock()

        def put_raw_key_called(_):
            self.fetcher._keymanager.put_raw_key.assert_called_once_with(
                KEY, address=ADDRESS_2)
            self.assertFalse(self.fetcher._keymanager.fetch_key.called)

        d = self._do_fetch(message.as_string())
        d.addCallback(put_raw_key_called)
        return d

    def testExtractOpenPGPHeaderIfInvalidAttachedKey(self):
        KEY = "-----BEGIN PGP PUBLIC KEY BLOCK-----\n..."
        KEYURL = "https://leap.se/key.txt"
        OpenPGP = "id=12345678; url=\"%s\"; preference=signencrypt" % (KEYURL,)

        message = MIMEMultipart()
        message.add_header("from", ADDRESS_2)
        message.add_header("OpenPGP", OpenPGP)
        key = MIMEApplication("", "pgp-keys")
        key.set_payload(KEY)
        message.attach(key)

        self.fetcher._keymanager.put_raw_key = Mock(
            return_value=defer.fail(KeyAddressMismatch()))
        self.fetcher._keymanager.fetch_key = Mock()

        def put_raw_key_called(_):
            self.fetcher._keymanager.put_raw_key.assert_called_once_with(
                KEY, address=ADDRESS_2)
            self.fetcher._keymanager.fetch_key.assert_called_once_with(
                ADDRESS_2, KEYURL)

        d = self._do_fetch(message.as_string())
        d.addCallback(put_raw_key_called)
        return d

    def testAddDecryptedHeader(self):
        class DummyMsg():

            def __init__(self):
                self.headers = {}

            def add_header(self, k, v):
                self.headers[k] = v

        msg = DummyMsg()
        self.fetcher._add_decrypted_header(msg)

        self.assertEquals(msg.headers['X-Leap-Encryption'], 'decrypted')

    def testDecryptEmail(self):

        self.fetcher._decryption_error = Mock()
        self.fetcher._add_decrypted_header = Mock()

        def create_encrypted_message(encstr):
            message = Parser().parsestr(self.EMAIL)
            newmsg = MultipartEncrypted('application/pgp-encrypted')
            for hkey, hval in message.items():
                newmsg.add_header(hkey, hval)

            encmsg = MIMEApplication(
                encstr, _subtype='octet-stream', _encoder=lambda x: x)
            encmsg.add_header('content-disposition', 'attachment',
                              filename='msg.asc')
            # create meta message
            metamsg = PGPEncrypted()
            metamsg.add_header('Content-Disposition', 'attachment')
            # attach pgp message parts to new message
            newmsg.attach(metamsg)
            newmsg.attach(encmsg)
            return newmsg

        def decryption_error_not_called(_):
            self.assertFalse(self.fetcher._decryption_error.called,
                             "There was some errors with decryption")

        def add_decrypted_header_called(_):
            self.assertTrue(self.fetcher._add_decrypted_header.called,
                            "There was some errors with decryption")

        d = self.km.encrypt(self.EMAIL, ADDRESS, sign=ADDRESS_2)
        d.addCallback(create_encrypted_message)
        d.addCallback(
            lambda message:
            self._do_fetch(message.as_string()))
        d.addCallback(decryption_error_not_called)
        d.addCallback(add_decrypted_header_called)
        return d

    def testValidateSignatureFromEncryptedEmailFromAppleMail(self):
        enc_signed_file = os.path.join(
            HERE, 'rfc822.multi-encrypt-signed.message')
        self.fetcher._add_verified_signature_header = Mock()

        def add_verified_signature_header_called(_):
            self.assertTrue(self.fetcher._add_verified_signature_header.called,
                            "There was some errors verifying signature")

        with open(enc_signed_file) as f:
            enc_signed_raw = f.read()

        d = self._do_fetch(enc_signed_raw)
        d.addCallback(add_verified_signature_header_called)
        return d

    def testListener(self):
        self.called = False

        def listener(uid):
            self.called = True

        def listener_called(_):
            self.assertTrue(self.called)

        self.fetcher.add_listener(listener)
        d = self._do_fetch(self.EMAIL)
        d.addCallback(listener_called)
        return d

    def _do_fetch(self, message):
        d = self._create_incoming_email(message)
        d.addCallback(
            lambda email:
            self._mock_soledad_get_from_index(fields.JUST_MAIL_IDX, [email]))
        d.addCallback(lambda _: self.fetcher.fetch())
        return d

    def _create_incoming_email(self, email_str):
        email = SoledadDocument()
        data = json.dumps(
            {"incoming": True, "content": email_str},
            ensure_ascii=False)

        def set_email_content(encr_data):
            email.content = {
                fields.INCOMING_KEY: True,
                fields.ERROR_DECRYPTING_KEY: False,
                ENC_SCHEME_KEY: EncryptionSchemes.PUBKEY,
                ENC_JSON_KEY: encr_data
            }
            return email
        d = self.km.encrypt(data, ADDRESS, fetch_remote=False)
        d.addCallback(set_email_content)
        return d

    def _mock_soledad_get_from_index(self, index_name, value):
        get_from_index = self._soledad.get_from_index

        def soledad_mock(idx_name, *key_values):
            if index_name == idx_name:
                return defer.succeed(value)
            return get_from_index(idx_name, *key_values)
        self.fetcher._soledad.get_from_index = Mock(side_effect=soledad_mock)