diff options
Diffstat (limited to 'framework/domain/mimetreeparser/tests/interfacetest.cpp')
-rw-r--r-- | framework/domain/mimetreeparser/tests/interfacetest.cpp | 310 |
1 files changed, 310 insertions, 0 deletions
diff --git a/framework/domain/mimetreeparser/tests/interfacetest.cpp b/framework/domain/mimetreeparser/tests/interfacetest.cpp new file mode 100644 index 00000000..3ae32a4a --- /dev/null +++ b/framework/domain/mimetreeparser/tests/interfacetest.cpp | |||
@@ -0,0 +1,310 @@ | |||
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 | |||
20 | #include "interface.h" | ||
21 | #include "interface_p.h" | ||
22 | |||
23 | #include <QTest> | ||
24 | |||
25 | QByteArray readMailFromFile(const QString &mailFile) | ||
26 | { | ||
27 | QFile file(QLatin1String(MAIL_DATA_DIR) + QLatin1Char('/') + mailFile); | ||
28 | file.open(QIODevice::ReadOnly); | ||
29 | Q_ASSERT(file.isOpen()); | ||
30 | return file.readAll(); | ||
31 | } | ||
32 | |||
33 | QByteArray join(QVector<QByteArray> vec, QByteArray sep) | ||
34 | { | ||
35 | QByteArray ret; | ||
36 | bool bInit = true; | ||
37 | foreach(const auto &entry, vec) { | ||
38 | if (!bInit) { | ||
39 | ret += sep; | ||
40 | } | ||
41 | bInit = false; | ||
42 | ret += entry; | ||
43 | } | ||
44 | return ret; | ||
45 | } | ||
46 | |||
47 | class InterfaceTest : public QObject | ||
48 | { | ||
49 | Q_OBJECT | ||
50 | private: | ||
51 | void printTree(const Part::Ptr &start, QString pre) | ||
52 | { | ||
53 | foreach (const auto &part, start->subParts()) { | ||
54 | qWarning() << QStringLiteral("%1* %2(%3)") | ||
55 | .arg(pre) | ||
56 | .arg(QString::fromLatin1(part->type())) | ||
57 | .arg(QString::fromLatin1(join(part->availableContents(),", "))); | ||
58 | printTree(part,pre + QStringLiteral(" ")); | ||
59 | } | ||
60 | } | ||
61 | |||
62 | private slots: | ||
63 | |||
64 | void testTextMail() | ||
65 | { | ||
66 | Parser parser(readMailFromFile("plaintext.mbox")); | ||
67 | printTree(parser.d->mTree,QString()); | ||
68 | auto contentPartList = parser.collectContentParts(); | ||
69 | QCOMPARE(contentPartList.size(), 1); | ||
70 | auto contentPart = contentPartList[0]; | ||
71 | QVERIFY((bool)contentPart); | ||
72 | QCOMPARE(contentPart->availableContents(), QVector<QByteArray>() << "plaintext"); | ||
73 | auto contentList = contentPart->content("plaintext"); | ||
74 | QCOMPARE(contentList.size(), 1); | ||
75 | QCOMPARE(contentList[0]->content(), 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/").toLocal8Bit()); | ||
76 | QCOMPARE(contentList[0]->charset(), QStringLiteral("utf-8").toLocal8Bit()); | ||
77 | QCOMPARE(contentList[0]->encryptions().size(), 0); | ||
78 | QCOMPARE(contentList[0]->signatures().size(), 0); | ||
79 | |||
80 | contentList = contentPart->content("html"); | ||
81 | QCOMPARE(contentList.size(), 0); | ||
82 | auto contentAttachmentList = parser.collectAttachmentParts(); | ||
83 | QCOMPARE(contentAttachmentList.size(), 0); | ||
84 | } | ||
85 | |||
86 | void testTextAlternative() | ||
87 | { | ||
88 | Parser parser(readMailFromFile("alternative.mbox")); | ||
89 | printTree(parser.d->mTree,QString()); | ||
90 | auto contentPartList = parser.collectContentParts(); | ||
91 | QCOMPARE(contentPartList.size(), 1); | ||
92 | auto contentPart = contentPartList[0]; | ||
93 | QVERIFY((bool)contentPart); | ||
94 | QCOMPARE(contentPart->availableContents(), QVector<QByteArray>() << "html" << "plaintext"); | ||
95 | auto contentList = contentPart->content("plaintext"); | ||
96 | QCOMPARE(contentList.size(), 1); | ||
97 | QCOMPARE(contentList[0]->content(), 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").toLocal8Bit()); | ||
98 | QCOMPARE(contentList[0]->charset(), QStringLiteral("utf-8").toLocal8Bit()); | ||
99 | QCOMPARE(contentList[0]->encryptions().size(), 0); | ||
100 | QCOMPARE(contentList[0]->signatures().size(), 0); | ||
101 | |||
102 | contentList = contentPart->content("html"); | ||
103 | QCOMPARE(contentList.size(), 1); | ||
104 | QCOMPARE(contentList[0]->content(), QStringLiteral("<html><body><p><span>HTML</span> text</p></body></html>\n\n").toLocal8Bit()); | ||
105 | QCOMPARE(contentList[0]->charset(), QStringLiteral("utf-8").toLocal8Bit()); | ||
106 | QCOMPARE(contentList[0]->encryptions().size(), 0); | ||
107 | QCOMPARE(contentList[0]->signatures().size(), 0); | ||
108 | auto contentAttachmentList = parser.collectAttachmentParts(); | ||
109 | QCOMPARE(contentAttachmentList.size(), 0); | ||
110 | } | ||
111 | |||
112 | void testTextHtml() | ||
113 | { | ||
114 | Parser parser(readMailFromFile("html.mbox")); | ||
115 | printTree(parser.d->mTree,QString()); | ||
116 | auto contentPartList = parser.collectContentParts(); | ||
117 | QCOMPARE(contentPartList.size(), 1); | ||
118 | auto contentPart = contentPartList[0]; | ||
119 | QVERIFY((bool)contentPart); | ||
120 | QCOMPARE(contentPart->availableContents(), QVector<QByteArray>() << "html"); | ||
121 | |||
122 | auto contentList = contentPart->content("plaintext"); | ||
123 | QCOMPARE(contentList.size(), 0); | ||
124 | |||
125 | contentList = contentPart->content("html"); | ||
126 | QCOMPARE(contentList.size(), 1); | ||
127 | QCOMPARE(contentList[0]->content(), QStringLiteral("<html><body><p><span>HTML</span> text</p></body></html>").toLocal8Bit()); | ||
128 | QCOMPARE(contentList[0]->charset(), QStringLiteral("utf-8").toLocal8Bit()); | ||
129 | QCOMPARE(contentList[0]->encryptions().size(), 0); | ||
130 | QCOMPARE(contentList[0]->signatures().size(), 0); | ||
131 | auto contentAttachmentList = parser.collectAttachmentParts(); | ||
132 | QCOMPARE(contentAttachmentList.size(), 0); | ||
133 | } | ||
134 | |||
135 | void testSMimeEncrypted() | ||
136 | { | ||
137 | Parser parser(readMailFromFile("smime-encrypted.mbox")); | ||
138 | printTree(parser.d->mTree,QString()); | ||
139 | auto contentPartList = parser.collectContentParts(); | ||
140 | QCOMPARE(contentPartList.size(), 1); | ||
141 | auto contentPart = contentPartList[0]; | ||
142 | QVERIFY((bool)contentPart); | ||
143 | QCOMPARE(contentPart->availableContents(), QVector<QByteArray>() << "plaintext"); | ||
144 | auto contentList = contentPart->content("plaintext"); | ||
145 | QCOMPARE(contentList.size(), 1); | ||
146 | QCOMPARE(contentList[0]->content(), QStringLiteral("The quick brown fox jumped over the lazy dog.").toLocal8Bit()); | ||
147 | QCOMPARE(contentList[0]->charset(), QStringLiteral("us-ascii").toLocal8Bit()); | ||
148 | QCOMPARE(contentList[0]->encryptions().size(), 1); | ||
149 | QCOMPARE(contentList[0]->signatures().size(), 0); | ||
150 | auto contentAttachmentList = parser.collectAttachmentParts(); | ||
151 | QCOMPARE(contentAttachmentList.size(), 0); | ||
152 | } | ||
153 | |||
154 | void testOpenPGPEncryptedAttachment() | ||
155 | { | ||
156 | Parser parser(readMailFromFile("openpgp-encrypted-attachment-and-non-encrypted-attachment.mbox")); | ||
157 | printTree(parser.d->mTree,QString()); | ||
158 | auto contentPartList = parser.collectContentParts(); | ||
159 | QCOMPARE(contentPartList.size(), 1); | ||
160 | auto contentPart = contentPartList[0]; | ||
161 | QVERIFY((bool)contentPart); | ||
162 | QCOMPARE(contentPart->availableContents(), QVector<QByteArray>() << "plaintext"); | ||
163 | auto contentList = contentPart->content("plaintext"); | ||
164 | QCOMPARE(contentList.size(), 1); | ||
165 | QCOMPARE(contentList[0]->content(), QStringLiteral("test text").toLocal8Bit()); | ||
166 | QCOMPARE(contentList[0]->charset(), QStringLiteral("us-ascii").toLocal8Bit()); | ||
167 | QCOMPARE(contentList[0]->encryptions().size(), 1); | ||
168 | QCOMPARE(contentList[0]->signatures().size(), 1); | ||
169 | auto contentAttachmentList = parser.collectAttachmentParts(); | ||
170 | QCOMPARE(contentAttachmentList.size(), 2); | ||
171 | QCOMPARE(contentAttachmentList[0]->availableContents(), QVector<QByteArray>() << "text/plain"); | ||
172 | QCOMPARE(contentAttachmentList[0]->content().size(), 1); | ||
173 | QCOMPARE(contentAttachmentList[0]->encryptions().size(), 1); | ||
174 | QCOMPARE(contentAttachmentList[0]->signatures().size(), 1); | ||
175 | QCOMPARE(contentAttachmentList[1]->availableContents(), QVector<QByteArray>() << "image/png"); | ||
176 | QCOMPARE(contentAttachmentList[1]->content().size(), 1); | ||
177 | QCOMPARE(contentAttachmentList[1]->encryptions().size(), 0); | ||
178 | QCOMPARE(contentAttachmentList[1]->signatures().size(), 0); | ||
179 | } | ||
180 | |||
181 | void testOpenPGPInline() | ||
182 | { | ||
183 | Parser parser(readMailFromFile("openpgp-inline-charset-encrypted.mbox")); | ||
184 | printTree(parser.d->mTree,QString()); | ||
185 | auto contentPartList = parser.collectContentParts(); | ||
186 | QCOMPARE(contentPartList.size(), 1); | ||
187 | auto contentPart = contentPartList[0]; | ||
188 | QVERIFY((bool)contentPart); | ||
189 | QCOMPARE(contentPart->availableContents(), QVector<QByteArray>() << "plaintext"); | ||
190 | QCOMPARE(contentPart->encryptions().size(), 0); | ||
191 | QCOMPARE(contentPart->signatures().size(), 0); | ||
192 | auto contentList = contentPart->content("plaintext"); | ||
193 | QCOMPARE(contentList.size(), 1); | ||
194 | QCOMPARE(contentList[0]->content(), QStringLiteral("asdasd asd asd asdf sadf sdaf sadf äöü").toLocal8Bit()); | ||
195 | QCOMPARE(contentList[0]->charset(), QStringLiteral("ISO-8859-15").toLocal8Bit()); | ||
196 | QCOMPARE(contentList[0]->encryptions().size(), 1); | ||
197 | QCOMPARE(contentList[0]->signatures().size(), 1); | ||
198 | auto contentAttachmentList = parser.collectAttachmentParts(); | ||
199 | QCOMPARE(contentAttachmentList.size(), 0); | ||
200 | } | ||
201 | |||
202 | void testOpenPPGInlineWithNonEncText() | ||
203 | { | ||
204 | Parser parser(readMailFromFile("openpgp-inline-encrypted+nonenc.mbox")); | ||
205 | printTree(parser.d->mTree,QString()); | ||
206 | auto contentPartList = parser.collectContentParts(); | ||
207 | QCOMPARE(contentPartList.size(), 1); | ||
208 | auto contentPart = contentPartList[0]; | ||
209 | QVERIFY((bool)contentPart); | ||
210 | QCOMPARE(contentPart->availableContents(), QVector<QByteArray>() << "plaintext"); | ||
211 | QCOMPARE(contentPart->encryptions().size(), 0); | ||
212 | QCOMPARE(contentPart->signatures().size(), 0); | ||
213 | auto contentList = contentPart->content("plaintext"); | ||
214 | QCOMPARE(contentList.size(), 2); | ||
215 | QCOMPARE(contentList[0]->content(), QStringLiteral("Not encrypted not signed :(\n\n").toLocal8Bit()); | ||
216 | QCOMPARE(contentList[0]->charset(), QStringLiteral("us-ascii").toLocal8Bit()); | ||
217 | QCOMPARE(contentList[0]->encryptions().size(), 0); | ||
218 | QCOMPARE(contentList[0]->signatures().size(), 0); | ||
219 | QCOMPARE(contentList[1]->content(), QStringLiteral("some random text").toLocal8Bit()); | ||
220 | QCOMPARE(contentList[1]->charset(), QStringLiteral("us-ascii").toLocal8Bit()); | ||
221 | QCOMPARE(contentList[1]->encryptions().size(), 1); | ||
222 | QCOMPARE(contentList[1]->signatures().size(), 0); | ||
223 | auto contentAttachmentList = parser.collectAttachmentParts(); | ||
224 | QCOMPARE(contentAttachmentList.size(), 0); | ||
225 | } | ||
226 | |||
227 | void testEncryptionBlock() | ||
228 | { | ||
229 | Parser parser(readMailFromFile("openpgp-encrypted-attachment-and-non-encrypted-attachment.mbox")); | ||
230 | auto contentPartList = parser.collectContentParts(); | ||
231 | auto contentPart = contentPartList[0]; | ||
232 | auto contentList = contentPart->content("plaintext"); | ||
233 | QCOMPARE(contentList.size(), 1); | ||
234 | QCOMPARE(contentList[0]->encryptions().size(), 1); | ||
235 | auto enc = contentList[0]->encryptions()[0]; | ||
236 | QCOMPARE((int) enc->recipients().size(), 2); | ||
237 | |||
238 | auto r = enc->recipients()[0]; | ||
239 | QCOMPARE(r->keyid(),QStringLiteral("14B79E26050467AA")); | ||
240 | QCOMPARE(r->name(),QStringLiteral("kdetest")); | ||
241 | QCOMPARE(r->email(),QStringLiteral("you@you.com")); | ||
242 | QCOMPARE(r->comment(),QStringLiteral("")); | ||
243 | |||
244 | r = enc->recipients()[1]; | ||
245 | QCOMPARE(r->keyid(),QStringLiteral("8D9860C58F246DE6")); | ||
246 | QCOMPARE(r->name(),QStringLiteral("unittest key")); | ||
247 | QCOMPARE(r->email(),QStringLiteral("test@kolab.org")); | ||
248 | QCOMPARE(r->comment(),QStringLiteral("no password")); | ||
249 | } | ||
250 | |||
251 | void testSignatureBlock() | ||
252 | { | ||
253 | Parser parser(readMailFromFile("openpgp-encrypted-attachment-and-non-encrypted-attachment.mbox")); | ||
254 | auto contentPartList = parser.collectContentParts(); | ||
255 | auto contentPart = contentPartList[0]; | ||
256 | auto contentList = contentPart->content("plaintext"); | ||
257 | QCOMPARE(contentList.size(), 1); | ||
258 | QCOMPARE(contentList[0]->signatures().size(), 1); | ||
259 | auto sig = contentList[0]->signatures()[0]; | ||
260 | QCOMPARE(sig->creationDateTime(), QDateTime(QDate(2015,05,01),QTime(15,12,47))); | ||
261 | QCOMPARE(sig->expirationDateTime(), QDateTime()); | ||
262 | QCOMPARE(sig->neverExpires(), true); | ||
263 | |||
264 | auto key = sig->key(); | ||
265 | QCOMPARE(key->keyid(),QStringLiteral("8D9860C58F246DE6")); | ||
266 | QCOMPARE(key->name(),QStringLiteral("unittest key")); | ||
267 | QCOMPARE(key->email(),QStringLiteral("test@kolab.org")); | ||
268 | QCOMPARE(key->comment(),QStringLiteral("no password")); | ||
269 | } | ||
270 | |||
271 | void testRelatedAlternative() | ||
272 | { | ||
273 | Parser parser(readMailFromFile("cid-links.mbox")); | ||
274 | printTree(parser.d->mTree,QString()); | ||
275 | auto contentPartList = parser.collectContentParts(); | ||
276 | QCOMPARE(contentPartList.size(), 1); | ||
277 | auto contentPart = contentPartList[0]; | ||
278 | QVERIFY((bool)contentPart); | ||
279 | QCOMPARE(contentPart->availableContents(), QVector<QByteArray>() << "html" << "plaintext"); | ||
280 | QCOMPARE(contentPart->encryptions().size(), 0); | ||
281 | QCOMPARE(contentPart->signatures().size(), 0); | ||
282 | auto contentList = contentPart->content("plaintext"); | ||
283 | QCOMPARE(contentList.size(), 1); | ||
284 | auto contentAttachmentList = parser.collectAttachmentParts(); | ||
285 | QCOMPARE(contentAttachmentList.size(), 0); | ||
286 | } | ||
287 | |||
288 | void testAttachmentPart() | ||
289 | { | ||
290 | Parser parser(readMailFromFile("cid-links.mbox")); | ||
291 | const auto relatedImage = parser.d->mTree->subParts().at(1); | ||
292 | QCOMPARE(relatedImage->availableContents(), QVector<QByteArray>() << "image/jpeg"); | ||
293 | auto contentList = relatedImage->content(); | ||
294 | QCOMPARE(contentList.size(), 1); | ||
295 | contentList = relatedImage->content("image/jpeg"); | ||
296 | QCOMPARE(contentList.size(), 1); | ||
297 | } | ||
298 | |||
299 | void testCidLink() | ||
300 | { | ||
301 | Parser parser(readMailFromFile("cid-links.mbox")); | ||
302 | printTree(parser.d->mTree,QString()); | ||
303 | QCOMPARE(parser.getPart(QUrl("cid:9359201d15e53f31a68c307b3369b6@info")), parser.d->mTree->subParts().at(1)); | ||
304 | QVERIFY(!parser.getPart(QUrl("cid:"))); | ||
305 | QVERIFY(!parser.getPart(QUrl("cid:unknown"))); | ||
306 | } | ||
307 | }; | ||
308 | |||
309 | QTEST_GUILESS_MAIN(InterfaceTest) | ||
310 | #include "interfacetest.moc" | ||