summaryrefslogtreecommitdiffstats
path: root/framework/src/domain/mime/mimetreeparser/tests/mimetreeparsertest.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'framework/src/domain/mime/mimetreeparser/tests/mimetreeparsertest.cpp')
-rw-r--r--framework/src/domain/mime/mimetreeparser/tests/mimetreeparsertest.cpp377
1 files changed, 377 insertions, 0 deletions
diff --git a/framework/src/domain/mime/mimetreeparser/tests/mimetreeparsertest.cpp b/framework/src/domain/mime/mimetreeparser/tests/mimetreeparsertest.cpp
new file mode 100644
index 00000000..961dbf9a
--- /dev/null
+++ b/framework/src/domain/mime/mimetreeparser/tests/mimetreeparsertest.cpp
@@ -0,0 +1,377 @@
1/*
2 Copyright (c) 2016 Sandro Knauß <knauss@kolabsystems.com>
3
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
8
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
18*/
19#include <objecttreeparser.h>
20
21#include <QTest>
22#include <QDebug>
23
24QByteArray readMailFromFile(const QString &mailFile)
25{
26 QFile file(QLatin1String(MAIL_DATA_DIR) + QLatin1Char('/') + mailFile);
27 file.open(QIODevice::ReadOnly);
28 Q_ASSERT(file.isOpen());
29 return file.readAll();
30}
31
32class InterfaceTest : public QObject
33{
34 Q_OBJECT
35private slots:
36 void testTextMail()
37 {
38 const auto expectedText = QStringLiteral("If you can see this text it means that your email client couldn't display our newsletter properly.\nPlease visit this link to view the newsletter on our website: http://www.gog.com/newsletter/");
39 MimeTreeParser::ObjectTreeParser otp;
40 otp.parseObjectTree(readMailFromFile("plaintext.mbox"));
41 auto partList = otp.collectContentParts();
42 QCOMPARE(partList.size(), 1);
43 auto part = partList[0].dynamicCast<MimeTreeParser::MessagePart>();
44 QCOMPARE(part->text(), expectedText);
45 QCOMPARE(part->charset(), QStringLiteral("utf-8").toLocal8Bit());
46
47 QCOMPARE(part->encryptions().size(), 0);
48 QCOMPARE(part->signatures().size(), 0);
49
50 QCOMPARE(otp.collectAttachmentParts().size(), 0);
51
52 QCOMPARE(otp.plainTextContent(), expectedText);
53 QVERIFY(otp.htmlContent().isEmpty());
54 }
55
56 void testAlternative()
57 {
58 MimeTreeParser::ObjectTreeParser otp;
59 otp.parseObjectTree(readMailFromFile("alternative.mbox"));
60 auto partList = otp.collectContentParts();
61 QCOMPARE(partList.size(), 1);
62 auto part = partList[0].dynamicCast<MimeTreeParser::AlternativeMessagePart>();
63 QVERIFY(bool(part));
64 QCOMPARE(part->plaintextContent(), QStringLiteral("If you can see this text it means that your email client couldn't display our newsletter properly.\nPlease visit this link to view the newsletter on our website: http://www.gog.com/newsletter/\n"));
65 //FIXME html charset is different from plain, and both are not ISO-8859-1
66 QCOMPARE(part->charset(), QStringLiteral("ISO-8859-1").toLocal8Bit());
67 QCOMPARE(part->htmlContent(), QStringLiteral("<html><body><p><span>HTML</span> text</p></body></html>\n\n"));
68 QCOMPARE(otp.collectAttachmentParts().size(), 0);
69 QCOMPARE(part->encryptions().size(), 0);
70 QCOMPARE(part->signatures().size(), 0);
71 }
72
73 void testTextHtml()
74 {
75 auto expectedText = QStringLiteral("<html><body><p><span>HTML</span> text</p></body></html>");
76 MimeTreeParser::ObjectTreeParser otp;
77 otp.parseObjectTree(readMailFromFile("html.mbox"));
78 otp.print();
79 auto partList = otp.collectContentParts();
80 QCOMPARE(partList.size(), 1);
81 auto part = partList[0].dynamicCast<MimeTreeParser::HtmlMessagePart>();
82 QVERIFY(bool(part));
83 QCOMPARE(part->htmlContent(), expectedText);
84 QCOMPARE(part->charset(), QStringLiteral("windows-1252").toLocal8Bit());
85 QCOMPARE(part->encryptions().size(), 0);
86 QCOMPARE(part->signatures().size(), 0);
87 auto contentAttachmentList = otp.collectAttachmentParts();
88 QCOMPARE(contentAttachmentList.size(), 0);
89
90 QCOMPARE(otp.htmlContent(), expectedText);
91 QVERIFY(otp.plainTextContent().isEmpty());
92 }
93
94 void testSMimeEncrypted()
95 {
96 MimeTreeParser::ObjectTreeParser otp;
97 otp.parseObjectTree(readMailFromFile("smime-encrypted.mbox"));
98 otp.print();
99 otp.decryptParts();
100 otp.print();
101 auto partList = otp.collectContentParts();
102 QCOMPARE(partList.size(), 1);
103 auto part = partList[0].dynamicCast<MimeTreeParser::MessagePart>();
104 QVERIFY(bool(part));
105 QCOMPARE(part->text(), QStringLiteral("The quick brown fox jumped over the lazy dog."));
106 QCOMPARE(part->charset(), QStringLiteral("us-ascii").toLocal8Bit());
107 QCOMPARE(part->encryptions().size(), 1);
108 QCOMPARE(part->signatures().size(), 0);
109 auto contentAttachmentList = otp.collectAttachmentParts();
110 QCOMPARE(contentAttachmentList.size(), 0);
111 }
112
113 void testOpenPGPEncryptedAttachment()
114 {
115 MimeTreeParser::ObjectTreeParser otp;
116 otp.parseObjectTree(readMailFromFile("openpgp-encrypted-attachment-and-non-encrypted-attachment.mbox"));
117 otp.print();
118 otp.decryptParts();
119 otp.print();
120 auto partList = otp.collectContentParts();
121 QCOMPARE(partList.size(), 1);
122 auto part = partList[0].dynamicCast<MimeTreeParser::MessagePart>();
123 QVERIFY(bool(part));
124 QCOMPARE(part->text(), QStringLiteral("test text"));
125 QCOMPARE(part->charset(), QStringLiteral("us-ascii").toLocal8Bit());
126 QCOMPARE(part->encryptions().size(), 1);
127 QCOMPARE(part->signatures().size(), 1);
128 QCOMPARE(part->encryptionState(), MimeTreeParser::KMMsgFullyEncrypted);
129 QCOMPARE(part->signatureState(), MimeTreeParser::KMMsgFullySigned);
130 auto contentAttachmentList = otp.collectAttachmentParts();
131 QCOMPARE(contentAttachmentList.size(), 2);
132 // QCOMPARE(contentAttachmentList[0]->availableContents(), QVector<QByteArray>() << "text/plain");
133 // QCOMPARE(contentAttachmentList[0]->content().size(), 1);
134 QCOMPARE(contentAttachmentList[0]->encryptions().size(), 1);
135 QCOMPARE(contentAttachmentList[0]->signatures().size(), 1);
136 QCOMPARE(contentAttachmentList[0]->encryptionState(), MimeTreeParser::KMMsgFullyEncrypted);
137 QCOMPARE(contentAttachmentList[0]->signatureState(), MimeTreeParser::KMMsgFullySigned);
138 // QCOMPARE(contentAttachmentList[1]->availableContents(), QVector<QByteArray>() << "image/png");
139 // QCOMPARE(contentAttachmentList[1]->content().size(), 1);
140 QCOMPARE(contentAttachmentList[1]->encryptions().size(), 0);
141 QCOMPARE(contentAttachmentList[1]->signatures().size(), 0);
142 QCOMPARE(contentAttachmentList[1]->encryptionState(), MimeTreeParser::KMMsgNotEncrypted);
143 QCOMPARE(contentAttachmentList[1]->signatureState(), MimeTreeParser::KMMsgNotSigned);
144 }
145
146 void testOpenPGPInline()
147 {
148 MimeTreeParser::ObjectTreeParser otp;
149 otp.parseObjectTree(readMailFromFile("openpgp-inline-charset-encrypted.mbox"));
150 otp.print();
151 otp.decryptParts();
152 otp.print();
153 auto partList = otp.collectContentParts();
154 QCOMPARE(partList.size(), 1);
155 auto part = partList[0].dynamicCast<MimeTreeParser::MessagePart>();
156 QVERIFY(bool(part));
157 QCOMPARE(part->charset(), QStringLiteral("ISO-8859-1").toLocal8Bit());
158 QCOMPARE(part->text(), QString::fromUtf8("asdasd asd asd asdf sadf sdaf sadf öäü"));
159
160 QCOMPARE(part->encryptions().size(), 1);
161 QCOMPARE(part->signatures().size(), 1);
162 QCOMPARE(otp.collectAttachmentParts().size(), 0);
163 }
164
165 void testOpenPPGInlineWithNonEncText()
166 {
167 MimeTreeParser::ObjectTreeParser otp;
168 otp.parseObjectTree(readMailFromFile("openpgp-inline-encrypted+nonenc.mbox"));
169 otp.print();
170 otp.decryptParts();
171 otp.print();
172 auto partList = otp.collectContentParts();
173 QCOMPARE(partList.size(), 1);
174 auto part1 = partList[0].dynamicCast<MimeTreeParser::MessagePart>();
175 QVERIFY(bool(part1));
176 QCOMPARE(part1->text(), QStringLiteral("Not encrypted not signed :(\n\nsome random text"));
177 //TODO test if we get the proper subparts with the appropriate encryptions
178 QCOMPARE(part1->charset(), QStringLiteral("us-ascii").toLocal8Bit());
179
180 QCOMPARE(part1->encryptionState(), MimeTreeParser::KMMsgPartiallyEncrypted);
181 QCOMPARE(part1->signatureState(), MimeTreeParser::KMMsgNotSigned);
182
183 // QCOMPARE(part1->text(), QStringLiteral("Not encrypted not signed :(\n\n"));
184 // QCOMPARE(part1->charset(), QStringLiteral("us-ascii").toLocal8Bit());
185 // QCOMPARE(contentList[1]->content(), QStringLiteral("some random text").toLocal8Bit());
186 // QCOMPARE(contentList[1]->charset(), QStringLiteral("us-ascii").toLocal8Bit());
187 // QCOMPARE(contentList[1]->encryptions().size(), 1);
188 // QCOMPARE(contentList[1]->signatures().size(), 0);
189 QCOMPARE(otp.collectAttachmentParts().size(), 0);
190 }
191
192 void testEncryptionBlock()
193 {
194 MimeTreeParser::ObjectTreeParser otp;
195 otp.parseObjectTree(readMailFromFile("openpgp-encrypted-attachment-and-non-encrypted-attachment.mbox"));
196 otp.print();
197 otp.decryptParts();
198 otp.print();
199 auto partList = otp.collectContentParts();
200 QCOMPARE(partList.size(), 1);
201 auto part1 = partList[0].dynamicCast<MimeTreeParser::MessagePart>();
202 QVERIFY(bool(part1));
203 QCOMPARE(part1->encryptions().size(), 1);
204 // auto enc = contentList[0]->encryptions()[0];
205 // QCOMPARE((int) enc->recipients().size(), 2);
206
207 // auto r = enc->recipients()[0];
208 // QCOMPARE(r->keyid(),QStringLiteral("14B79E26050467AA"));
209 // QCOMPARE(r->name(),QStringLiteral("kdetest"));
210 // QCOMPARE(r->email(),QStringLiteral("you@you.com"));
211 // QCOMPARE(r->comment(),QStringLiteral(""));
212
213 // r = enc->recipients()[1];
214 // QCOMPARE(r->keyid(),QStringLiteral("8D9860C58F246DE6"));
215 // QCOMPARE(r->name(),QStringLiteral("unittest key"));
216 // QCOMPARE(r->email(),QStringLiteral("test@kolab.org"));
217 // QCOMPARE(r->comment(),QStringLiteral("no password"));
218 auto attachmentList = otp.collectAttachmentParts();
219 QCOMPARE(attachmentList.size(), 2);
220 auto attachment1 = attachmentList[0];
221 QVERIFY(attachment1->node());
222 QCOMPARE(attachment1->filename(), QStringLiteral("file.txt"));
223 auto attachment2 = attachmentList[1];
224 QVERIFY(attachment2->node());
225 QCOMPARE(attachment2->filename(), QStringLiteral("image.png"));
226 }
227
228 void testSignatureBlock()
229 {
230 MimeTreeParser::ObjectTreeParser otp;
231 otp.parseObjectTree(readMailFromFile("openpgp-encrypted-attachment-and-non-encrypted-attachment.mbox"));
232 otp.print();
233 otp.decryptParts();
234 otp.print();
235 auto partList = otp.collectContentParts();
236 QCOMPARE(partList.size(), 1);
237 auto part = partList[0].dynamicCast<MimeTreeParser::MessagePart>();
238 QVERIFY(bool(part));
239
240 // QCOMPARE(contentList[0]->signatures().size(), 1);
241 // auto sig = contentList[0]->signatures()[0];
242 // QCOMPARE(sig->creationDateTime(), QDateTime(QDate(2015,05,01),QTime(15,12,47)));
243 // QCOMPARE(sig->expirationDateTime(), QDateTime());
244 // QCOMPARE(sig->neverExpires(), true);
245
246 // auto key = sig->key();
247 // QCOMPARE(key->keyid(),QStringLiteral("8D9860C58F246DE6"));
248 // QCOMPARE(key->name(),QStringLiteral("unittest key"));
249 // QCOMPARE(key->email(),QStringLiteral("test@kolab.org"));
250 // QCOMPARE(key->comment(),QStringLiteral("no password"));
251 }
252
253 void testRelatedAlternative()
254 {
255 MimeTreeParser::ObjectTreeParser otp;
256 otp.parseObjectTree(readMailFromFile("cid-links.mbox"));
257 otp.print();
258 auto partList = otp.collectContentParts();
259 QCOMPARE(partList.size(), 1);
260 auto part = partList[0].dynamicCast<MimeTreeParser::MessagePart>();
261 QVERIFY(bool(part));
262 QCOMPARE(part->encryptions().size(), 0);
263 QCOMPARE(part->signatures().size(), 0);
264 QCOMPARE(otp.collectAttachmentParts().size(), 0);
265 }
266
267 void testAttachmentPart()
268 {
269 MimeTreeParser::ObjectTreeParser otp;
270 otp.parseObjectTree(readMailFromFile("attachment.mbox"));
271 otp.print();
272 auto partList = otp.collectAttachmentParts();
273 QCOMPARE(partList.size(), 1);
274 auto part = partList[0].dynamicCast<MimeTreeParser::MessagePart>();
275 QVERIFY(bool(part));
276 auto att = part->node();
277 qWarning() << "Attachment type: " << att->contentType(true)->mimeType();
278 QCOMPARE(part->mimeType(), QByteArray("image/jpeg"));
279 }
280
281 void testCidLink()
282 {
283 MimeTreeParser::ObjectTreeParser otp;
284 otp.parseObjectTree(readMailFromFile("cid-links.mbox"));
285 otp.print();
286 auto partList = otp.collectContentParts();
287 QCOMPARE(partList.size(), 1);
288 auto part = partList[0].dynamicCast<MimeTreeParser::AlternativeMessagePart>();
289 QVERIFY(bool(part));
290 auto resolvedContent = otp.resolveCidLinks(part->htmlContent());
291 QVERIFY(!resolvedContent.contains("cid:"));
292 }
293
294 void testCidLinkInForwardedInline()
295 {
296 MimeTreeParser::ObjectTreeParser otp;
297 otp.parseObjectTree(readMailFromFile("cid-links-forwarded-inline.mbox"));
298 otp.print();
299 auto partList = otp.collectContentParts();
300 QCOMPARE(partList.size(), 1);
301 auto part = partList[0].dynamicCast<MimeTreeParser::AlternativeMessagePart>();
302 QVERIFY(bool(part));
303 auto resolvedContent = otp.resolveCidLinks(part->htmlContent());
304 QVERIFY(!resolvedContent.contains("cid:"));
305 }
306
307 void testOpenPGPInlineError()
308 {
309 MimeTreeParser::ObjectTreeParser otp;
310 otp.parseObjectTree(readMailFromFile("inlinepgpgencrypted-error.mbox"));
311 otp.print();
312 otp.decryptParts();
313 otp.print();
314 auto partList = otp.collectContentParts();
315 QCOMPARE(partList.size(), 1);
316 auto part = partList[0].dynamicCast<MimeTreeParser::EncryptedMessagePart>();
317 QVERIFY(bool(part));
318 QVERIFY(part->error());
319 }
320
321 void testEncapsulated()
322 {
323 MimeTreeParser::ObjectTreeParser otp;
324 otp.parseObjectTree(readMailFromFile("encapsulated-with-attachment.mbox"));
325 otp.decryptParts();
326 auto partList = otp.collectContentParts();
327 QCOMPARE(partList.size(), 2);
328 auto part = partList[1].dynamicCast<MimeTreeParser::EncapsulatedRfc822MessagePart>();
329 QVERIFY(bool(part));
330 QCOMPARE(part->from(), QLatin1String("Thomas McGuire <dontspamme@gmx.net>"));
331 QCOMPARE(part->date().toString(), QLatin1String("Wed Aug 5 10:57:58 2009 GMT+0200"));
332 auto subPartList = otp.collectContentParts(part);
333 QCOMPARE(subPartList.size(), 1);
334 qWarning() << subPartList[0]->metaObject()->className();
335 auto subPart = subPartList[0].dynamicCast<MimeTreeParser::TextMessagePart>();
336 QVERIFY(bool(subPart));
337 }
338
339 void test8bitEncodedInPlaintext()
340 {
341 MimeTreeParser::ObjectTreeParser otp;
342 otp.parseObjectTree(readMailFromFile("8bitencoded.mbox"));
343 QVERIFY(otp.plainTextContent().contains(QString::fromUtf8("Why Pisa’s Tower")));
344 QVERIFY(otp.htmlContent().contains(QString::fromUtf8("Why Pisa’s Tower")));
345 }
346
347 void testInlineSigned()
348 {
349 MimeTreeParser::ObjectTreeParser otp;
350 otp.parseObjectTree(readMailFromFile("openpgp-inline-signed.mbox"));
351 otp.decryptParts();
352 auto partList = otp.collectContentParts();
353 QCOMPARE(partList.size(), 1);
354 auto part = partList[0].dynamicCast<MimeTreeParser::MessagePart>();
355 QCOMPARE(part->signatures().size(), 1);
356 QCOMPARE(part->encryptionState(), MimeTreeParser::KMMsgNotEncrypted);
357 QCOMPARE(part->signatureState(), MimeTreeParser::KMMsgFullySigned);
358 }
359
360 void testEncryptedAndSigned()
361 {
362 MimeTreeParser::ObjectTreeParser otp;
363 otp.parseObjectTree(readMailFromFile("openpgp-encrypted+signed.mbox"));
364 otp.decryptParts();
365 auto partList = otp.collectContentParts();
366 QCOMPARE(partList.size(), 1);
367 auto part = partList[0].dynamicCast<MimeTreeParser::MessagePart>();
368 QCOMPARE(part->signatures().size(), 1);
369 QCOMPARE(part->encryptions().size(), 1);
370 QCOMPARE(part->encryptionState(), MimeTreeParser::KMMsgFullyEncrypted);
371 QCOMPARE(part->signatureState(), MimeTreeParser::KMMsgFullySigned);
372 QVERIFY(otp.plainTextContent().contains(QString::fromUtf8("encrypted message text")));
373 }
374};
375
376QTEST_GUILESS_MAIN(InterfaceTest)
377#include "mimetreeparsertest.moc"