summaryrefslogtreecommitdiffstats
path: root/tests/src/com/android/email/LegacyConversionsTests.java
blob: 82f333ae75b5be3bb108cb1e3e7228bde30c5ac2 (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
398
399
400
401
402
403
404
405
/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.email;

import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.test.ProviderTestCase2;
import android.test.suitebuilder.annotation.Suppress;

import com.android.email.provider.EmailProvider;
import com.android.email.provider.ProviderTestUtils;
import com.android.emailcommon.internet.MimeHeader;
import com.android.emailcommon.internet.MimeUtility;
import com.android.emailcommon.mail.Address;
import com.android.emailcommon.mail.BodyPart;
import com.android.emailcommon.mail.Flag;
import com.android.emailcommon.mail.Message;
import com.android.emailcommon.mail.Message.RecipientType;
import com.android.emailcommon.mail.MessageTestUtils;
import com.android.emailcommon.mail.MessageTestUtils.MessageBuilder;
import com.android.emailcommon.mail.MessageTestUtils.MultipartBuilder;
import com.android.emailcommon.mail.MessagingException;
import com.android.emailcommon.mail.Part;
import com.android.emailcommon.provider.EmailContent;
import com.android.emailcommon.provider.EmailContent.Attachment;

import java.io.IOException;
import java.util.ArrayList;

/**
 * Tests of the Legacy Conversions code (used by MessagingController).
 *
 * NOTE:  It would probably make sense to rewrite this using a MockProvider, instead of the
 * ProviderTestCase (which is a real provider running on a temp database).  This would be more of
 * a true "unit test".
 *
 * You can run this entire test case with:
 *   runtest -c com.android.email.LegacyConversionsTests email
 */
@Suppress
public class LegacyConversionsTests extends ProviderTestCase2<EmailProvider> {

    Context mProviderContext;
    Context mContext;

    public LegacyConversionsTests() {
        super(EmailProvider.class, EmailContent.AUTHORITY);
    }

    @Override
    public void setUp() throws Exception {
        super.setUp();
        mProviderContext = getMockContext();
        mContext = getContext();
    }

    /**
     * TODO: basic Legacy -> Provider Message conversions
     * TODO: basic Legacy -> Provider Body conversions
     * TODO: rainy day tests of all kinds
     */

    /**
     * Sunny day test of adding attachments from an IMAP/POP message.
     */
    public void brokentestAddAttachments() throws MessagingException, IOException {
        // Prepare a local message to add the attachments to
        final long accountId = 1;
        final long mailboxId = 1;

        // test 1: legacy message using content-type:name style for name
        final EmailContent.Message localMessage = ProviderTestUtils.setupMessage(
                "local-message", accountId, mailboxId, false, true, mProviderContext);
        final Message legacyMessage = prepareLegacyMessageWithAttachments(2, false);
        convertAndCheckcheckAddedAttachments(localMessage, legacyMessage);

        // test 2: legacy message using content-disposition:filename style for name
        final EmailContent.Message localMessage2 = ProviderTestUtils.setupMessage(
                "local-message", accountId, mailboxId, false, true, mProviderContext);
        final Message legacyMessage2 = prepareLegacyMessageWithAttachments(2, true);
        convertAndCheckcheckAddedAttachments(localMessage2, legacyMessage2);
    }

    /**
     * Helper for testAddAttachments
     */
    private void convertAndCheckcheckAddedAttachments(final EmailContent.Message localMessage,
            final Message legacyMessage) throws MessagingException, IOException {
        // Now, convert from legacy to provider and see what happens
        ArrayList<Part> viewables = new ArrayList<Part>();
        ArrayList<Part> attachments = new ArrayList<Part>();
        MimeUtility.collectParts(legacyMessage, viewables, attachments);
        LegacyConversions.updateAttachments(mProviderContext, localMessage, attachments);

        // Read back all attachments for message and check field values
        Uri uri = ContentUris.withAppendedId(Attachment.MESSAGE_ID_URI, localMessage.mId);
        Cursor c = mProviderContext.getContentResolver().query(uri, Attachment.CONTENT_PROJECTION,
                null, null, null);
        try {
            assertEquals(2, c.getCount());
            while (c.moveToNext()) {
                Attachment attachment =
                        Attachment.getContent(mProviderContext, c, Attachment.class);
                if ("100".equals(attachment.mLocation)) {
                    checkAttachment("attachment1Part", attachments.get(0), attachment,
                            localMessage.mAccountKey);
                } else if ("101".equals(attachment.mLocation)) {
                    checkAttachment("attachment2Part", attachments.get(1), attachment,
                            localMessage.mAccountKey);
                } else {
                    fail("Unexpected attachment with location " + attachment.mLocation);
                }
            }
        } finally {
            c.close();
        }
    }

    /**
     * Test that only "attachment" or "inline" attachments are captured and added.
     * @throws MessagingException
     * @throws IOException
     */
    public void brokentestAttachmentDispositions() throws MessagingException, IOException {
        // Prepare a local message to add the attachments to
        final long accountId = 1;
        final long mailboxId = 1;

        // Prepare the three attachments we want to test
        BodyPart[] sourceAttachments = new BodyPart[3];
        BodyPart attachmentPart;

        // 1. Standard attachment
        attachmentPart = MessageTestUtils.bodyPart("image/jpg", null);
        attachmentPart.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "image/jpg");
        attachmentPart.setHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING, "base64");
        attachmentPart.setHeader(MimeHeader.HEADER_CONTENT_DISPOSITION,
                "attachment;\n filename=\"file-1\";\n size=100");
        attachmentPart.setHeader(MimeHeader.HEADER_ANDROID_ATTACHMENT_STORE_DATA, "100");
        sourceAttachments[0] = attachmentPart;

        // 2. Inline attachment
        attachmentPart = MessageTestUtils.bodyPart("image/gif", null);
        attachmentPart.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "image/gif");
        attachmentPart.setHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING, "base64");
        attachmentPart.setHeader(MimeHeader.HEADER_CONTENT_DISPOSITION,
                "inline;\n filename=\"file-2\";\n size=200");
        attachmentPart.setHeader(MimeHeader.HEADER_ANDROID_ATTACHMENT_STORE_DATA, "101");
        sourceAttachments[1] = attachmentPart;

        // 3. Neither (use VCALENDAR)
        attachmentPart = MessageTestUtils.bodyPart("text/calendar", null);
        attachmentPart.setHeader(MimeHeader.HEADER_CONTENT_TYPE,
                "text/calendar; charset=UTF-8; method=REQUEST");
        attachmentPart.setHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING, "7bit");
        attachmentPart.setHeader(MimeHeader.HEADER_ANDROID_ATTACHMENT_STORE_DATA, "102");
        sourceAttachments[2] = attachmentPart;

        // Prepare local message (destination) and legacy message w/attachments (source)
        final EmailContent.Message localMessage = ProviderTestUtils.setupMessage(
                "local-message", accountId, mailboxId, false, true, mProviderContext);
        final Message legacyMessage = prepareLegacyMessageWithAttachments(sourceAttachments);
        convertAndCheckcheckAddedAttachments(localMessage, legacyMessage);

        // Run the conversion and check for the converted attachments - this test asserts
        // that there are two attachments numbered 100 & 101 (so will fail if it finds 102)
        convertAndCheckcheckAddedAttachments(localMessage, legacyMessage);
    }

    /**
     * Test that attachments aren't re-added in the DB.  This supports the "partial download"
     * nature of POP messages.
     */
    public void brokentestAddDuplicateAttachments() throws MessagingException, IOException {
        // Prepare a local message to add the attachments to
        final long accountId = 1;
        final long mailboxId = 1;
        final EmailContent.Message localMessage = ProviderTestUtils.setupMessage(
                "local-message", accountId, mailboxId, false, true, mProviderContext);

        // Prepare a legacy message with attachments
        Message legacyMessage = prepareLegacyMessageWithAttachments(2, false);

        // Now, convert from legacy to provider and see what happens
        ArrayList<Part> viewables = new ArrayList<Part>();
        ArrayList<Part> attachments = new ArrayList<Part>();
        MimeUtility.collectParts(legacyMessage, viewables, attachments);
        LegacyConversions.updateAttachments(mProviderContext, localMessage, attachments);

        // Confirm two attachment objects created
        Uri uri = ContentUris.withAppendedId(Attachment.MESSAGE_ID_URI, localMessage.mId);
        assertEquals(2, EmailContent.count(mProviderContext, uri, null, null));

        // Now add the attachments again and confirm there are still only two
        LegacyConversions.updateAttachments(mProviderContext, localMessage, attachments);
        assertEquals(2, EmailContent.count(mProviderContext, uri, null, null));

        // Now add a 3rd & 4th attachment and make sure the total is 4, not 2 or 6
        legacyMessage = prepareLegacyMessageWithAttachments(4, false);
        viewables = new ArrayList<Part>();
        attachments = new ArrayList<Part>();
        MimeUtility.collectParts(legacyMessage, viewables, attachments);
        LegacyConversions.updateAttachments(mProviderContext, localMessage, attachments);
        assertEquals(4, EmailContent.count(mProviderContext, uri, null, null));
    }

    /**
     * Prepare a legacy message with 1+ attachments
     * @param numAttachments how many attachments to add
     * @param filenameInDisposition False: attachment names are sent as content-type:name.  True:
     *          attachment names are sent as content-disposition:filename.
     */
    private Message prepareLegacyMessageWithAttachments(int numAttachments,
            boolean filenameInDisposition) throws MessagingException {
        BodyPart[] attachmentParts = new BodyPart[numAttachments];
        for (int i = 0; i < numAttachments; ++i) {
            // construct parameter parts for content-type:name or content-disposition:filename.
            String name = "";
            String filename = "";
            String quotedName = "\"test-attachment-" + i + "\"";
            if (filenameInDisposition) {
                filename = ";\n filename=" + quotedName;
            } else {
                name = ";\n name=" + quotedName;
            }

            // generate an attachment that came from a server
            BodyPart attachmentPart = MessageTestUtils.bodyPart("image/jpg", null);

            // name=attachmentN size=N00 location=10N
            attachmentPart.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "image/jpg" + name);
            attachmentPart.setHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING, "base64");
            attachmentPart.setHeader(MimeHeader.HEADER_CONTENT_DISPOSITION,
                    "attachment" + filename +  ";\n size=" + (i+1) + "00");
            attachmentPart.setHeader(MimeHeader.HEADER_ANDROID_ATTACHMENT_STORE_DATA, "10" + i);

            attachmentParts[i] = attachmentPart;
        }

        return prepareLegacyMessageWithAttachments(attachmentParts);
    }

    /**
     * Prepare a legacy message with 1+ attachments
     * @param attachments array containing one or more attachments
     */
    private Message prepareLegacyMessageWithAttachments(BodyPart[] attachments)
            throws MessagingException {
        // Build the multipart that holds the attachments
        MultipartBuilder mpBuilder = new MultipartBuilder("multipart/mixed");
        for (int i = 0; i < attachments.length; ++i) {
            mpBuilder.addBodyPart(attachments[i]);
        }

        // Now build a message with them
        final Message legacyMessage = new MessageBuilder()
            .setBody(new MultipartBuilder("multipart/mixed")
                     .addBodyPart(MessageTestUtils.bodyPart("text/html", null))
                     .addBodyPart(mpBuilder.buildBodyPart())
                     .build())
                .build();

        return legacyMessage;
    }

    /**
     * Compare attachment that was converted from Part (expected) to Provider Attachment (actual)
     *
     * TODO content URI should only be set if we also saved a file
     * TODO other data encodings
     */
    private void checkAttachment(String tag, Part expected, EmailContent.Attachment actual,
            long accountKey) throws MessagingException {
        String contentType = MimeUtility.unfoldAndDecode(expected.getContentType());
        String contentTypeName = MimeUtility.getHeaderParameter(contentType, "name");
        assertEquals(tag, expected.getMimeType(), actual.mMimeType);
        String disposition = expected.getDisposition();
        String sizeString = MimeUtility.getHeaderParameter(disposition, "size");
        String dispositionFilename = MimeUtility.getHeaderParameter(disposition, "filename");
        long expectedSize = (sizeString != null) ? Long.parseLong(sizeString) : 0;
        assertEquals(tag, expectedSize, actual.mSize);
        assertEquals(tag, expected.getContentId(), actual.mContentId);

        // filename is either content-type:name or content-disposition:filename
        String expectedName = (contentTypeName != null) ? contentTypeName : dispositionFilename;
        assertEquals(tag, expectedName, actual.mFileName);

        // content URI should be null
        assertNull(tag, actual.getContentUri());

        assertTrue(tag, 0 != actual.mMessageKey);

        // location is either both null or both matching
        String expectedPartId = null;
        String[] storeData = expected.getHeader(MimeHeader.HEADER_ANDROID_ATTACHMENT_STORE_DATA);
        if (storeData != null && storeData.length > 0) {
            expectedPartId = storeData[0];
        }
        assertEquals(tag, expectedPartId, actual.mLocation);
        assertEquals(tag, "B", actual.mEncoding);
        assertEquals(tag, accountKey, actual.mAccountKey);
    }

    /**
     * TODO: Sunny day test of adding attachments from a POP message.
     */

    /**
     * Sunny day tests of converting an original message to a legacy message
     */
    public void brokentestMakeLegacyMessage() throws MessagingException {
        // Set up and store a message in the provider
        long account1Id = 1;
        long mailbox1Id = 1;

        // Test message 1: No body
        EmailContent.Message localMessage1 = ProviderTestUtils.setupMessage("make-legacy",
                account1Id, mailbox1Id, false, true, mProviderContext);
        Message getMessage1 = LegacyConversions.makeMessage(mProviderContext, localMessage1);
        checkLegacyMessage("no body", localMessage1, getMessage1);

        // Test message 2: Simple body
        EmailContent.Message localMessage2 = ProviderTestUtils.setupMessage("make-legacy",
                account1Id, mailbox1Id, true, false, mProviderContext);
        localMessage2.mFlags &= ~EmailContent.Message.FLAG_TYPE_MASK;
        localMessage2.save(mProviderContext);
        Message getMessage2 = LegacyConversions.makeMessage(mProviderContext, localMessage2);
        checkLegacyMessage("simple body", localMessage2, getMessage2);

        // Test message 3: Body + replied-to text
        EmailContent.Message localMessage3 = ProviderTestUtils.setupMessage("make-legacy",
                account1Id, mailbox1Id, true, false, mProviderContext);
        localMessage3.mFlags &= ~EmailContent.Message.FLAG_TYPE_MASK;
        localMessage3.mFlags |= EmailContent.Message.FLAG_TYPE_REPLY;
        localMessage3.save(mProviderContext);
        Message getMessage3 = LegacyConversions.makeMessage(mProviderContext, localMessage3);
        checkLegacyMessage("reply-to", localMessage3, getMessage3);

        // Test message 4: Body + forwarded text
        EmailContent.Message localMessage4 = ProviderTestUtils.setupMessage("make-legacy",
                account1Id, mailbox1Id, true, false, mProviderContext);
        localMessage4.mFlags &= ~EmailContent.Message.FLAG_TYPE_MASK;
        localMessage4.mFlags |= EmailContent.Message.FLAG_TYPE_FORWARD;
        localMessage4.save(mProviderContext);
        Message getMessage4 = LegacyConversions.makeMessage(mProviderContext, localMessage4);
        checkLegacyMessage("forwarding", localMessage4, getMessage4);
    }

    /**
     * Check equality of a pair of converted messages
     */
    private void checkLegacyMessage(String tag, EmailContent.Message expect, Message actual)
            throws MessagingException {
        assertEquals(tag, expect.mServerId, actual.getUid());
        assertEquals(tag, expect.mServerTimeStamp, actual.getInternalDate().getTime());
        assertEquals(tag, expect.mSubject, actual.getSubject());
        assertEquals(tag, expect.mFrom, Address.toHeader(actual.getFrom()));
        assertEquals(tag, expect.mTimeStamp, actual.getSentDate().getTime());
        assertEquals(tag, expect.mTo, Address.toHeader(actual.getRecipients(RecipientType.TO)));
        assertEquals(tag, expect.mCc, Address.toHeader(actual.getRecipients(RecipientType.CC)));
        assertEquals(tag, expect.mBcc, Address.toHeader(actual.getRecipients(RecipientType.BCC)));
        assertEquals(tag, expect.mReplyTo, Address.toHeader(actual.getReplyTo()));
        assertEquals(tag, expect.mMessageId, actual.getMessageId());
        // check flags
        assertEquals(tag, expect.mFlagRead, actual.isSet(Flag.SEEN));
        assertEquals(tag, expect.mFlagFavorite, actual.isSet(Flag.FLAGGED));

        // Check the body of the message
        ArrayList<Part> viewables = new ArrayList<Part>();
        ArrayList<Part> attachments = new ArrayList<Part>();
        MimeUtility.collectParts(actual, viewables, attachments);
        String get1Text = null;
        String get1Html = null;
        for (Part viewable : viewables) {
            String text = MimeUtility.getTextFromPart(viewable);
            if (viewable.getMimeType().equalsIgnoreCase("text/html")) {
                get1Html = text;
            } else {
                get1Text = text;
            }
        }
        assertEquals(tag, expect.mText, get1Text);
        assertEquals(tag, expect.mHtml, get1Html);

        // TODO Check the attachments

//      cv.put("attachment_count", attachments.size());
    }
}