Changeset View
Changeset View
Standalone View
Standalone View
mimetreeparser/autotests/basicobjecttreeparsertest.cpp
- This file was copied from messageviewer/src/messagepartthemes/default/autotests/unencryptedmessagetest.cpp.
1 | /* | 1 | /* | ||
---|---|---|---|---|---|
2 | Copyright (c) 2010 Thomas McGuire <thomas.mcguire@kdab.com> | 2 | Copyright (c) 2010 Thomas McGuire <thomas.mcguire@kdab.com> | ||
3 | Copyright (c) 2016 Sandro Knauß <sknauss@kde.org> | 3 | Copyright (c) 2019 Sandro Knauß <sknauss@kde.org> | ||
4 | 4 | | |||
5 | This library is free software; you can redistribute it and/or modify it | 5 | This library is free software; you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Library General Public License as published by | 6 | under the terms of the GNU Library General Public License as published by | ||
7 | the Free Software Foundation; either version 2 of the License, or (at your | 7 | the Free Software Foundation; either version 2 of the License, or (at your | ||
8 | option) any later version. | 8 | option) any later version. | ||
9 | 9 | | |||
10 | This library is distributed in the hope that it will be useful, but WITHOUT | 10 | This library is distributed in the hope that it will be useful, but WITHOUT | ||
11 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | 11 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
12 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public | 12 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public | ||
13 | License for more details. | 13 | License for more details. | ||
14 | 14 | | |||
15 | You should have received a copy of the GNU Library General Public License | 15 | You should have received a copy of the GNU Library General Public License | ||
16 | along with this library; see the file COPYING.LIB. If not, write to the | 16 | along with this library; see the file COPYING.LIB. If not, write to the | ||
17 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | 17 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
18 | 02110-1301, USA. | 18 | 02110-1301, USA. | ||
19 | */ | 19 | */ | ||
20 | 20 | | |||
21 | #include "unencryptedmessagetest.h" | 21 | #include "basicobjecttreeparsertest.h" | ||
22 | #include "util.h" | 22 | #include "util.h" | ||
23 | #include "setupenv.h" | 23 | #include "setupenv.h" | ||
24 | 24 | | |||
25 | #include <MimeTreeParser/NodeHelper> | 25 | #include <MimeTreeParser/NodeHelper> | ||
26 | #include <MimeTreeParser/ObjectTreeParser> | 26 | #include <MimeTreeParser/ObjectTreeParser> | ||
27 | #include <MessageViewer/BufferedHtmlWriter> | 27 | #include <MimeTreeParser/SimpleObjectTreeSource> | ||
28 | 28 | | |||
29 | #include <QTest> | 29 | #include <QTest> | ||
30 | 30 | | |||
31 | using namespace MessageViewer; | 31 | using namespace MimeTreeParser; | ||
32 | 32 | | |||
33 | QTEST_MAIN(UnencryptedMessageTest) | 33 | QTEST_MAIN(ObjectTreeParserTest) | ||
34 | 34 | | |||
35 | void UnencryptedMessageTest::initTestCase() | 35 | void ObjectTreeParserTest::initTestCase() | ||
36 | { | 36 | { | ||
37 | Test::setupEnv(); | 37 | Test::setupEnv(); | ||
38 | } | 38 | } | ||
39 | 39 | | |||
40 | void UnencryptedMessageTest::testMailWithoutEncryption() | 40 | void ObjectTreeParserTest::testMailWithoutEncryption() | ||
41 | { | 41 | { | ||
42 | KMime::Message::Ptr originalMessage | 42 | KMime::Message::Ptr originalMessage | ||
43 | = Test::readAndParseMail(QStringLiteral("encapsulated-with-attachment.mbox")); | 43 | = readAndParseMail(QStringLiteral("encapsulated-with-attachment.mbox")); | ||
44 | MimeTreeParser::NodeHelper nodeHelper; | 44 | NodeHelper nodeHelper; | ||
45 | Test::ObjectTreeSource emptySource(nullptr, nullptr); | 45 | SimpleObjectTreeSource testSource; | ||
46 | MimeTreeParser::ObjectTreeParser otp(&emptySource, &nodeHelper); | 46 | ObjectTreeParser otp(&testSource, &nodeHelper); | ||
47 | otp.parseObjectTree(originalMessage.data()); | 47 | otp.parseObjectTree(originalMessage.data()); | ||
48 | QVERIFY(!nodeHelper.unencryptedMessage(originalMessage)); | 48 | QVERIFY(!nodeHelper.unencryptedMessage(originalMessage)); | ||
49 | } | 49 | } | ||
50 | 50 | | |||
51 | void UnencryptedMessageTest::testSignedForwardedOpenPGPSignedEncrypted() | 51 | void ObjectTreeParserTest::testSignedForwardedOpenPGPSignedEncrypted() | ||
52 | { | 52 | { | ||
53 | KMime::Message::Ptr originalMessage | 53 | KMime::Message::Ptr originalMessage | ||
54 | = Test::readAndParseMail(QStringLiteral("signed-forward-openpgp-signed-encrypted.mbox")); | 54 | = readAndParseMail(QStringLiteral("signed-forward-openpgp-signed-encrypted.mbox")); | ||
55 | 55 | | |||
56 | MimeTreeParser::NodeHelper nodeHelper; | 56 | NodeHelper nodeHelper; | ||
57 | BufferedHtmlWriter testWriter; | 57 | SimpleObjectTreeSource testSource; | ||
58 | testWriter.begin(); | 58 | ObjectTreeParser otp(&testSource, &nodeHelper); | ||
59 | Test::CSSHelper testCSSHelper; | | |||
60 | Test::ObjectTreeSource emptySource(&testWriter, &testCSSHelper); | | |||
61 | MimeTreeParser::ObjectTreeParser otp(&emptySource, &nodeHelper); | | |||
62 | otp.parseObjectTree(originalMessage.data()); | 59 | otp.parseObjectTree(originalMessage.data()); | ||
63 | testWriter.end(); | | |||
64 | 60 | | |||
65 | QCOMPARE(otp.plainTextContent().toLatin1().data(), "bla bla bla"); // The textual content doesn't include the encrypted encapsulated message by design | 61 | QCOMPARE(otp.plainTextContent().toLatin1().data(), "bla bla bla"); // The textual content doesn't include the encrypted encapsulated message by design | ||
66 | QCOMPARE(nodeHelper.overallEncryptionState( | 62 | QCOMPARE(nodeHelper.overallEncryptionState( | ||
67 | originalMessage.data()), MimeTreeParser::KMMsgPartiallyEncrypted); | 63 | originalMessage.data()), KMMsgPartiallyEncrypted); | ||
68 | QCOMPARE(nodeHelper.overallSignatureState( | 64 | QCOMPARE(nodeHelper.overallSignatureState( | ||
69 | originalMessage.data()), MimeTreeParser::KMMsgFullySigned); | 65 | originalMessage.data()), KMMsgFullySigned); | ||
70 | 66 | | |||
71 | KMime::Message::Ptr unencryptedMessage = nodeHelper.unencryptedMessage(originalMessage); | 67 | KMime::Message::Ptr unencryptedMessage = nodeHelper.unencryptedMessage(originalMessage); | ||
72 | QVERIFY(!unencryptedMessage); // We must not invalidate the outer signature | 68 | QVERIFY(!unencryptedMessage); // We must not invalidate the outer signature | ||
73 | } | 69 | } | ||
74 | 70 | | |||
75 | void UnencryptedMessageTest::testForwardedOpenPGPSignedEncrypted() | 71 | void ObjectTreeParserTest::testForwardedOpenPGPSignedEncrypted() | ||
76 | { | 72 | { | ||
77 | KMime::Message::Ptr originalMessage | 73 | KMime::Message::Ptr originalMessage | ||
78 | = Test::readAndParseMail(QStringLiteral("forward-openpgp-signed-encrypted.mbox")); | 74 | = readAndParseMail(QStringLiteral("forward-openpgp-signed-encrypted.mbox")); | ||
79 | 75 | | |||
80 | MimeTreeParser::NodeHelper nodeHelper; | 76 | NodeHelper nodeHelper; | ||
81 | BufferedHtmlWriter testWriter; | 77 | SimpleObjectTreeSource testSource; | ||
82 | testWriter.begin(); | 78 | ObjectTreeParser otp(&testSource, &nodeHelper); | ||
83 | Test::CSSHelper testCSSHelper; | 79 | testSource.setDecryptMessage(true); | ||
84 | Test::ObjectTreeSource emptySource(&testWriter, &testCSSHelper); | | |||
85 | emptySource.setAllowDecryption(true); | | |||
86 | MimeTreeParser::ObjectTreeParser otp(&emptySource, &nodeHelper); | | |||
87 | otp.parseObjectTree(originalMessage.data()); | 80 | otp.parseObjectTree(originalMessage.data()); | ||
88 | testWriter.end(); | | |||
89 | 81 | | |||
90 | QCOMPARE(otp.plainTextContent().toLatin1().data(), "bla bla bla"); // The textual content doesn't include the encrypted encapsulated message by design | 82 | QCOMPARE(otp.plainTextContent().toLatin1().data(), "bla bla bla"); // The textual content doesn't include the encrypted encapsulated message by design | ||
91 | QCOMPARE(nodeHelper.overallEncryptionState( | 83 | QCOMPARE(nodeHelper.overallEncryptionState( | ||
92 | originalMessage.data()), MimeTreeParser::KMMsgPartiallyEncrypted); | 84 | originalMessage.data()), KMMsgPartiallyEncrypted); | ||
93 | 85 | | |||
94 | QCOMPARE(nodeHelper.overallSignatureState( | 86 | QCOMPARE(nodeHelper.overallSignatureState( | ||
95 | originalMessage.data()), MimeTreeParser::KMMsgPartiallySigned); | 87 | originalMessage.data()), KMMsgPartiallySigned); | ||
96 | 88 | | |||
97 | // Now, test that the unencrypted message is generated correctly | 89 | // Now, test that the unencrypted message is generated correctly | ||
98 | KMime::Message::Ptr unencryptedMessage = nodeHelper.unencryptedMessage(originalMessage); | 90 | KMime::Message::Ptr unencryptedMessage = nodeHelper.unencryptedMessage(originalMessage); | ||
99 | QVERIFY(unencryptedMessage.data()); | 91 | QVERIFY(unencryptedMessage.data()); | ||
100 | QCOMPARE(unencryptedMessage->contentType()->mimeType().data(), "multipart/mixed"); | 92 | QCOMPARE(unencryptedMessage->contentType()->mimeType().data(), "multipart/mixed"); | ||
101 | QCOMPARE(unencryptedMessage->contents().size(), 2); | 93 | QCOMPARE(unencryptedMessage->contents().size(), 2); | ||
102 | QCOMPARE(unencryptedMessage->contents().first()->contentType()->mimeType().data(), | 94 | QCOMPARE(unencryptedMessage->contents().first()->contentType()->mimeType().data(), | ||
103 | "text/plain"); | 95 | "text/plain"); | ||
104 | QCOMPARE(unencryptedMessage->contents().first()->decodedContent().data(), "bla bla bla"); | 96 | QCOMPARE(unencryptedMessage->contents().first()->decodedContent().data(), "bla bla bla"); | ||
105 | QCOMPARE(unencryptedMessage->contents().at( | 97 | QCOMPARE(unencryptedMessage->contents().at( | ||
106 | 1)->contentType()->mimeType().data(), "message/rfc822"); | 98 | 1)->contentType()->mimeType().data(), "message/rfc822"); | ||
107 | KMime::Message::Ptr encapsulated = unencryptedMessage->contents().at(1)->bodyAsMessage(); | 99 | KMime::Message::Ptr encapsulated = unencryptedMessage->contents().at(1)->bodyAsMessage(); | ||
108 | QCOMPARE(encapsulated->contentType()->mimeType().data(), "multipart/signed"); | 100 | QCOMPARE(encapsulated->contentType()->mimeType().data(), "multipart/signed"); | ||
109 | QCOMPARE(encapsulated->contents().size(), 2); | 101 | QCOMPARE(encapsulated->contents().size(), 2); | ||
110 | QCOMPARE(encapsulated->contents().first()->contentType()->mimeType().data(), "text/plain"); | 102 | QCOMPARE(encapsulated->contents().first()->contentType()->mimeType().data(), "text/plain"); | ||
111 | QCOMPARE(encapsulated->contents().at( | 103 | QCOMPARE(encapsulated->contents().at( | ||
112 | 1)->contentType()->mimeType().data(), "application/pgp-signature"); | 104 | 1)->contentType()->mimeType().data(), "application/pgp-signature"); | ||
113 | QCOMPARE(encapsulated->contents().first()->decodedContent().data(), "encrypted message text"); | 105 | QCOMPARE(encapsulated->contents().first()->decodedContent().data(), "encrypted message text"); | ||
114 | 106 | | |||
115 | // TODO: Check that the signature is valid | 107 | // TODO: Check that the signature is valid | ||
116 | } | 108 | } | ||
117 | 109 | | |||
118 | void UnencryptedMessageTest::testSMIMESignedEncrypted() | 110 | void ObjectTreeParserTest::testSMIMESignedEncrypted() | ||
119 | { | 111 | { | ||
120 | KMime::Message::Ptr originalMessage | 112 | KMime::Message::Ptr originalMessage | ||
121 | = Test::readAndParseMail(QStringLiteral("smime-signed-encrypted.mbox")); | 113 | = readAndParseMail(QStringLiteral("smime-signed-encrypted.mbox")); | ||
122 | 114 | | |||
123 | MimeTreeParser::NodeHelper nodeHelper; | 115 | NodeHelper nodeHelper; | ||
124 | Test::ObjectTreeSource emptySource(nullptr, nullptr); | 116 | SimpleObjectTreeSource testSource; | ||
125 | emptySource.setAllowDecryption(true); | 117 | ObjectTreeParser otp(&testSource, &nodeHelper); | ||
126 | MimeTreeParser::ObjectTreeParser otp(&emptySource, &nodeHelper); | 118 | testSource.setDecryptMessage(true); | ||
127 | otp.parseObjectTree(originalMessage.data()); | 119 | otp.parseObjectTree(originalMessage.data()); | ||
128 | 120 | | |||
129 | QCOMPARE(otp.plainTextContent().toLatin1().data(), "encrypted message text"); | 121 | QCOMPARE(otp.plainTextContent().toLatin1().data(), "encrypted message text"); | ||
130 | QCOMPARE(nodeHelper.overallEncryptionState( | 122 | QCOMPARE(nodeHelper.overallEncryptionState( | ||
131 | originalMessage.data()), MimeTreeParser::KMMsgFullyEncrypted); | 123 | originalMessage.data()), KMMsgFullyEncrypted); | ||
132 | 124 | | |||
133 | QCOMPARE(nodeHelper.overallSignatureState( | 125 | QCOMPARE(nodeHelper.overallSignatureState( | ||
134 | originalMessage.data()), MimeTreeParser::KMMsgFullySigned); | 126 | originalMessage.data()), KMMsgFullySigned); | ||
135 | 127 | | |||
136 | // Now, test that the unencrypted message is generated correctly | 128 | // Now, test that the unencrypted message is generated correctly | ||
137 | KMime::Message::Ptr unencryptedMessage = nodeHelper.unencryptedMessage(originalMessage); | 129 | KMime::Message::Ptr unencryptedMessage = nodeHelper.unencryptedMessage(originalMessage); | ||
138 | QCOMPARE(unencryptedMessage->contentType()->mimeType().data(), "multipart/signed"); | 130 | QCOMPARE(unencryptedMessage->contentType()->mimeType().data(), "multipart/signed"); | ||
139 | QCOMPARE(unencryptedMessage->contents().size(), 2); | 131 | QCOMPARE(unencryptedMessage->contents().size(), 2); | ||
140 | QCOMPARE(unencryptedMessage->contents().first()->contentType()->mimeType().data(), | 132 | QCOMPARE(unencryptedMessage->contents().first()->contentType()->mimeType().data(), | ||
141 | "text/plain"); | 133 | "text/plain"); | ||
142 | QCOMPARE(unencryptedMessage->contents().at( | 134 | QCOMPARE(unencryptedMessage->contents().at( | ||
143 | 1)->contentType()->mimeType().data(), "application/pkcs7-signature"); | 135 | 1)->contentType()->mimeType().data(), "application/pkcs7-signature"); | ||
144 | QCOMPARE( | 136 | QCOMPARE( | ||
145 | unencryptedMessage->contents().first()->decodedContent().data(), "encrypted message text"); | 137 | unencryptedMessage->contents().first()->decodedContent().data(), "encrypted message text"); | ||
146 | 138 | | |||
147 | // TODO: Check that the signature is valid | 139 | // TODO: Check that the signature is valid | ||
148 | } | 140 | } | ||
149 | 141 | | |||
150 | void UnencryptedMessageTest::testOpenPGPSignedEncrypted() | 142 | void ObjectTreeParserTest::testOpenPGPSignedEncrypted() | ||
151 | { | 143 | { | ||
152 | KMime::Message::Ptr originalMessage | 144 | KMime::Message::Ptr originalMessage | ||
153 | = Test::readAndParseMail(QStringLiteral("openpgp-signed-encrypted.mbox")); | 145 | = readAndParseMail(QStringLiteral("openpgp-signed-encrypted.mbox")); | ||
154 | 146 | | |||
155 | MimeTreeParser::NodeHelper nodeHelper; | 147 | NodeHelper nodeHelper; | ||
156 | Test::ObjectTreeSource emptySource(nullptr, nullptr); | 148 | SimpleObjectTreeSource testSource; | ||
157 | emptySource.setAllowDecryption(true); | 149 | ObjectTreeParser otp(&testSource, &nodeHelper); | ||
158 | MimeTreeParser::ObjectTreeParser otp(&emptySource, &nodeHelper); | 150 | testSource.setDecryptMessage(true); | ||
159 | otp.parseObjectTree(originalMessage.data()); | 151 | otp.parseObjectTree(originalMessage.data()); | ||
160 | 152 | | |||
161 | QCOMPARE(otp.plainTextContent().toLatin1().data(), "encrypted message text"); | 153 | QCOMPARE(otp.plainTextContent().toLatin1().data(), "encrypted message text"); | ||
162 | QCOMPARE(nodeHelper.overallEncryptionState( | 154 | QCOMPARE(nodeHelper.overallEncryptionState( | ||
163 | originalMessage.data()), MimeTreeParser::KMMsgFullyEncrypted); | 155 | originalMessage.data()), KMMsgFullyEncrypted); | ||
164 | 156 | | |||
165 | QCOMPARE(nodeHelper.overallSignatureState( | 157 | QCOMPARE(nodeHelper.overallSignatureState( | ||
166 | originalMessage.data()), MimeTreeParser::KMMsgFullySigned); | 158 | originalMessage.data()), KMMsgFullySigned); | ||
167 | 159 | | |||
168 | // Now, test that the unencrypted message is generated correctly | 160 | // Now, test that the unencrypted message is generated correctly | ||
169 | KMime::Message::Ptr unencryptedMessage = nodeHelper.unencryptedMessage(originalMessage); | 161 | KMime::Message::Ptr unencryptedMessage = nodeHelper.unencryptedMessage(originalMessage); | ||
170 | QCOMPARE(unencryptedMessage->contentType()->mimeType().data(), "multipart/signed"); | 162 | QCOMPARE(unencryptedMessage->contentType()->mimeType().data(), "multipart/signed"); | ||
171 | QCOMPARE(unencryptedMessage->contents().size(), 2); | 163 | QCOMPARE(unencryptedMessage->contents().size(), 2); | ||
172 | QCOMPARE(unencryptedMessage->contents().first()->contentType()->mimeType().data(), | 164 | QCOMPARE(unencryptedMessage->contents().first()->contentType()->mimeType().data(), | ||
173 | "text/plain"); | 165 | "text/plain"); | ||
174 | QCOMPARE(unencryptedMessage->contents().at( | 166 | QCOMPARE(unencryptedMessage->contents().at( | ||
175 | 1)->contentType()->mimeType().data(), "application/pgp-signature"); | 167 | 1)->contentType()->mimeType().data(), "application/pgp-signature"); | ||
176 | QCOMPARE( | 168 | QCOMPARE( | ||
177 | unencryptedMessage->contents().first()->decodedContent().data(), "encrypted message text"); | 169 | unencryptedMessage->contents().first()->decodedContent().data(), "encrypted message text"); | ||
178 | 170 | | |||
179 | // TODO: Check that the signature is valid | 171 | // TODO: Check that the signature is valid | ||
180 | } | 172 | } | ||
181 | 173 | | |||
182 | void UnencryptedMessageTest::testOpenPGPEncryptedAndSigned() | 174 | void ObjectTreeParserTest::testOpenPGPEncryptedAndSigned() | ||
183 | { | 175 | { | ||
184 | KMime::Message::Ptr originalMessage | 176 | KMime::Message::Ptr originalMessage | ||
185 | = Test::readAndParseMail(QStringLiteral("openpgp-encrypted+signed.mbox")); | 177 | = readAndParseMail(QStringLiteral("openpgp-encrypted+signed.mbox")); | ||
186 | 178 | | |||
187 | MimeTreeParser::NodeHelper nodeHelper; | 179 | NodeHelper nodeHelper; | ||
188 | Test::ObjectTreeSource emptySource(nullptr, nullptr); | 180 | SimpleObjectTreeSource testSource; | ||
189 | emptySource.setAllowDecryption(true); | 181 | ObjectTreeParser otp(&testSource, &nodeHelper); | ||
190 | MimeTreeParser::ObjectTreeParser otp(&emptySource, &nodeHelper); | 182 | testSource.setDecryptMessage(true); | ||
191 | otp.parseObjectTree(originalMessage.data()); | 183 | otp.parseObjectTree(originalMessage.data()); | ||
192 | 184 | | |||
193 | QCOMPARE(otp.plainTextContent().toLatin1().data(), "encrypted message text"); | 185 | QCOMPARE(otp.plainTextContent().toLatin1().data(), "encrypted message text"); | ||
194 | QCOMPARE(nodeHelper.overallEncryptionState( | 186 | QCOMPARE(nodeHelper.overallEncryptionState( | ||
195 | originalMessage.data()), MimeTreeParser::KMMsgFullyEncrypted); | 187 | originalMessage.data()), KMMsgFullyEncrypted); | ||
196 | 188 | | |||
197 | QCOMPARE(nodeHelper.overallSignatureState( | 189 | QCOMPARE(nodeHelper.overallSignatureState( | ||
198 | originalMessage.data()), MimeTreeParser::KMMsgFullySigned); | 190 | originalMessage.data()), KMMsgFullySigned); | ||
199 | 191 | | |||
200 | // Now, test that the unencrypted message is generated correctly | 192 | // Now, test that the unencrypted message is generated correctly | ||
201 | KMime::Message::Ptr unencryptedMessage = nodeHelper.unencryptedMessage(originalMessage); | 193 | KMime::Message::Ptr unencryptedMessage = nodeHelper.unencryptedMessage(originalMessage); | ||
202 | QCOMPARE(unencryptedMessage->contentType()->mimeType().data(), "text/plain"); | 194 | QCOMPARE(unencryptedMessage->contentType()->mimeType().data(), "text/plain"); | ||
203 | QCOMPARE(unencryptedMessage->contents().size(), 0); | 195 | QCOMPARE(unencryptedMessage->contents().size(), 0); | ||
204 | QCOMPARE(unencryptedMessage->decodedContent().data(), "encrypted message text"); | 196 | QCOMPARE(unencryptedMessage->decodedContent().data(), "encrypted message text"); | ||
205 | 197 | | |||
206 | // TODO: Check that the signature is valid | 198 | // TODO: Check that the signature is valid | ||
207 | } | 199 | } | ||
208 | 200 | | |||
209 | void UnencryptedMessageTest::testOpenPGPEncrypted() | 201 | void ObjectTreeParserTest::testOpenPGPEncrypted() | ||
210 | { | 202 | { | ||
211 | KMime::Message::Ptr originalMessage | 203 | KMime::Message::Ptr originalMessage | ||
212 | = Test::readAndParseMail(QStringLiteral("openpgp-encrypted.mbox")); | 204 | = readAndParseMail(QStringLiteral("openpgp-encrypted.mbox")); | ||
213 | 205 | | |||
214 | MimeTreeParser::NodeHelper nodeHelper; | 206 | NodeHelper nodeHelper; | ||
215 | Test::ObjectTreeSource emptySource(nullptr, nullptr); | 207 | SimpleObjectTreeSource testSource; | ||
216 | emptySource.setAllowDecryption(true); | 208 | ObjectTreeParser otp(&testSource, &nodeHelper); | ||
217 | MimeTreeParser::ObjectTreeParser otp(&emptySource, &nodeHelper); | 209 | testSource.setDecryptMessage(true); | ||
218 | otp.parseObjectTree(originalMessage.data()); | 210 | otp.parseObjectTree(originalMessage.data()); | ||
219 | 211 | | |||
220 | QCOMPARE(otp.plainTextContent().toLatin1().data(), "encrypted message text"); | 212 | QCOMPARE(otp.plainTextContent().toLatin1().data(), "encrypted message text"); | ||
221 | QCOMPARE(nodeHelper.overallEncryptionState( | 213 | QCOMPARE(nodeHelper.overallEncryptionState( | ||
222 | originalMessage.data()), MimeTreeParser::KMMsgFullyEncrypted); | 214 | originalMessage.data()), KMMsgFullyEncrypted); | ||
223 | 215 | | |||
224 | // Now, test that the unencrypted message is generated correctly | 216 | // Now, test that the unencrypted message is generated correctly | ||
225 | KMime::Message::Ptr unencryptedMessage = nodeHelper.unencryptedMessage(originalMessage); | 217 | KMime::Message::Ptr unencryptedMessage = nodeHelper.unencryptedMessage(originalMessage); | ||
226 | QCOMPARE(unencryptedMessage->contentType()->mimeType().data(), "text/plain"); | 218 | QCOMPARE(unencryptedMessage->contentType()->mimeType().data(), "text/plain"); | ||
227 | QCOMPARE(unencryptedMessage->decodedContent().data(), "encrypted message text"); | 219 | QCOMPARE(unencryptedMessage->decodedContent().data(), "encrypted message text"); | ||
228 | QCOMPARE(unencryptedMessage->contents().size(), 0); | 220 | QCOMPARE(unencryptedMessage->contents().size(), 0); | ||
229 | } | 221 | } | ||
230 | 222 | | |||
231 | void UnencryptedMessageTest::testOpenPGPEncryptedNotDecrypted() | 223 | void ObjectTreeParserTest::testOpenPGPEncryptedNotDecrypted() | ||
232 | { | 224 | { | ||
233 | KMime::Message::Ptr originalMessage | 225 | KMime::Message::Ptr originalMessage | ||
234 | = Test::readAndParseMail(QStringLiteral("openpgp-encrypted.mbox")); | 226 | = readAndParseMail(QStringLiteral("openpgp-encrypted.mbox")); | ||
235 | 227 | | |||
236 | MimeTreeParser::NodeHelper nodeHelper; | 228 | NodeHelper nodeHelper; | ||
237 | Test::ObjectTreeSource emptySource(nullptr, nullptr); | 229 | SimpleObjectTreeSource testSource; | ||
238 | emptySource.setAllowDecryption(false); | 230 | ObjectTreeParser otp(&testSource, &nodeHelper); | ||
239 | MimeTreeParser::ObjectTreeParser otp(&emptySource, &nodeHelper); | | |||
240 | otp.parseObjectTree(originalMessage.data()); | 231 | otp.parseObjectTree(originalMessage.data()); | ||
241 | 232 | | |||
242 | QCOMPARE(nodeHelper.overallEncryptionState( | 233 | QCOMPARE(nodeHelper.overallEncryptionState( | ||
243 | originalMessage.data()), MimeTreeParser::KMMsgFullyEncrypted); | 234 | originalMessage.data()), KMMsgFullyEncrypted); | ||
244 | QCOMPARE(otp.plainTextContent().toLatin1().data(), ""); | 235 | QCOMPARE(otp.plainTextContent().toLatin1().data(), ""); | ||
245 | 236 | | |||
246 | KMime::Message::Ptr unencryptedMessage = nodeHelper.unencryptedMessage(originalMessage); | 237 | KMime::Message::Ptr unencryptedMessage = nodeHelper.unencryptedMessage(originalMessage); | ||
247 | QVERIFY(!unencryptedMessage); | 238 | QVERIFY(!unencryptedMessage); | ||
248 | } | 239 | } | ||
249 | 240 | | |||
250 | void UnencryptedMessageTest::testAsync_data() | 241 | void ObjectTreeParserTest::testAsync_data() | ||
251 | { | 242 | { | ||
252 | QTest::addColumn<QString>("mailFileName"); | 243 | QTest::addColumn<QString>("mailFileName"); | ||
253 | QTest::addColumn<QString>("output"); | 244 | QTest::addColumn<QString>("output"); | ||
254 | 245 | | |||
255 | QTest::newRow("openpgp-encrypt") << QStringLiteral("openpgp-encrypted.mbox") << QStringLiteral( | 246 | QTest::newRow("openpgp-encrypt") << QStringLiteral("openpgp-encrypted.mbox") << QStringLiteral( | ||
256 | "encrypted message text"); | 247 | "encrypted message text"); | ||
257 | QTest::newRow("smime-opaque-sign") << QStringLiteral("smime-opaque-sign.mbox") | 248 | QTest::newRow("smime-opaque-sign") << QStringLiteral("smime-opaque-sign.mbox") | ||
258 | << QStringLiteral("A simple signed only test."); | 249 | << QStringLiteral("A simple signed only test."); | ||
259 | QTest::newRow("smime-encrypt") << QStringLiteral("smime-encrypted.mbox") << QStringLiteral( | 250 | QTest::newRow("smime-encrypt") << QStringLiteral("smime-encrypted.mbox") << QStringLiteral( | ||
260 | "The quick brown fox jumped over the lazy dog."); | 251 | "The quick brown fox jumped over the lazy dog."); | ||
261 | QTest::newRow("openpgp-inline-encrypt") << QStringLiteral( | 252 | QTest::newRow("openpgp-inline-encrypt") << QStringLiteral( | ||
262 | "openpgp-inline-charset-encrypted.mbox") << QStringLiteral( | 253 | "openpgp-inline-charset-encrypted.mbox") << QStringLiteral( | ||
263 | "asdasd asd asd asdf sadf sdaf sadf \u00F6\u00E4\u00FC"); | 254 | "asdasd asd asd asdf sadf sdaf sadf \u00F6\u00E4\u00FC"); | ||
264 | } | 255 | } | ||
265 | 256 | | |||
266 | void UnencryptedMessageTest::testAsync() | 257 | void ObjectTreeParserTest::testAsync() | ||
267 | { | 258 | { | ||
268 | QFETCH(QString, mailFileName); | 259 | QFETCH(QString, mailFileName); | ||
269 | QFETCH(QString, output); | 260 | QFETCH(QString, output); | ||
270 | 261 | | |||
271 | KMime::Message::Ptr originalMessage = Test::readAndParseMail(mailFileName); | 262 | KMime::Message::Ptr originalMessage = readAndParseMail(mailFileName); | ||
272 | MimeTreeParser::NodeHelper nodeHelper; | 263 | NodeHelper nodeHelper; | ||
273 | Test::ObjectTreeSource emptySource(nullptr, nullptr); | 264 | SimpleObjectTreeSource testSource; | ||
274 | emptySource.setAllowDecryption(true); | 265 | testSource.setDecryptMessage(true); | ||
275 | { | 266 | { | ||
276 | QEventLoop loop; | 267 | QEventLoop loop; | ||
277 | MimeTreeParser::ObjectTreeParser otp(&emptySource, &nodeHelper); | 268 | ObjectTreeParser otp(&testSource, &nodeHelper); | ||
278 | 269 | | |||
279 | connect(&nodeHelper, &MimeTreeParser::NodeHelper::update, &loop, &QEventLoop::quit); | 270 | connect(&nodeHelper, &NodeHelper::update, &loop, &QEventLoop::quit); | ||
280 | otp.setAllowAsync(true); | 271 | otp.setAllowAsync(true); | ||
281 | otp.parseObjectTree(originalMessage.data()); | 272 | otp.parseObjectTree(originalMessage.data()); | ||
282 | loop.exec(); | 273 | loop.exec(); | ||
283 | } | 274 | } | ||
284 | // Job ended | 275 | // Job ended | ||
285 | { | 276 | { | ||
286 | MimeTreeParser::ObjectTreeParser otp(&emptySource, &nodeHelper); | 277 | ObjectTreeParser otp(&testSource, &nodeHelper); | ||
287 | otp.setAllowAsync(true); | 278 | otp.setAllowAsync(true); | ||
288 | otp.parseObjectTree(originalMessage.data()); | 279 | otp.parseObjectTree(originalMessage.data()); | ||
289 | QCOMPARE(otp.plainTextContent(), output); | 280 | QCOMPARE(otp.plainTextContent(), output); | ||
290 | } | 281 | } | ||
291 | } | 282 | } | ||
292 | | ||||
293 | void UnencryptedMessageTest::testNotDecrypted_data() | | |||
294 | { | | |||
295 | QTest::addColumn<QString>("mailFileName"); | | |||
296 | QTest::addColumn<bool>("decryptMessage"); | | |||
297 | | ||||
298 | QTest::newRow("openpgp-inline") << QStringLiteral("inlinepgpencrypted.mbox") << true; | | |||
299 | QTest::newRow("openpgp-encrypt") << QStringLiteral("openpgp-encrypted.mbox") << true; | | |||
300 | QTest::newRow("smime-encrypt") << QStringLiteral("smime-encrypted.mbox") << true; | | |||
301 | QTest::newRow("openpgp-inline-encrypt") << QStringLiteral( | | |||
302 | "openpgp-inline-charset-encrypted.mbox") << true; | | |||
303 | QTest::newRow("smime-opaque-sign") << QStringLiteral("smime-opaque-sign.mbox") << false; | | |||
304 | QTest::newRow("openpgp-inline-signed") << QStringLiteral("openpgp-inline-signed.mbox") << false; | | |||
305 | QTest::newRow("openpgp-mime-signed") << QStringLiteral("openpgp-signed-mailinglist.mbox") | | |||
306 | << false; | | |||
307 | } | | |||
308 | | ||||
309 | void UnencryptedMessageTest::testNotDecrypted() | | |||
310 | { | | |||
311 | QFETCH(QString, mailFileName); | | |||
312 | QFETCH(bool, decryptMessage); | | |||
313 | KMime::Message::Ptr originalMessage = Test::readAndParseMail(mailFileName); | | |||
314 | | ||||
315 | MimeTreeParser::NodeHelper nodeHelper; | | |||
316 | BufferedHtmlWriter testWriter; | | |||
317 | testWriter.begin(); | | |||
318 | Test::CSSHelper testCSSHelper; | | |||
319 | Test::ObjectTreeSource emptySource(&testWriter, &testCSSHelper); | | |||
320 | emptySource.setAllowDecryption(false); | | |||
321 | MimeTreeParser::ObjectTreeParser otp(&emptySource, &nodeHelper); | | |||
322 | otp.parseObjectTree(originalMessage.data()); | | |||
323 | testWriter.end(); | | |||
324 | | ||||
325 | if (decryptMessage) { | | |||
326 | QCOMPARE(otp.plainTextContent().toLatin1().data(), ""); | | |||
327 | } else { | | |||
328 | QVERIFY(otp.plainTextContent().toLatin1().data()); | | |||
329 | } | | |||
330 | QCOMPARE(testWriter.data().contains("<a href=\"kmail:decryptMessage\">"), decryptMessage); | | |||
331 | | ||||
332 | KMime::Message::Ptr unencryptedMessage = nodeHelper.unencryptedMessage(originalMessage); | | |||
333 | QCOMPARE((bool)unencryptedMessage, false); | | |||
334 | } | | |||
335 | | ||||
336 | void UnencryptedMessageTest::testSMimeAutoCertImport() | | |||
337 | { | | |||
338 | KMime::Message::Ptr originalMessage = Test::readAndParseMail(QStringLiteral("smime-cert.mbox")); | | |||
339 | | ||||
340 | MimeTreeParser::NodeHelper nodeHelper; | | |||
341 | BufferedHtmlWriter testWriter; | | |||
342 | testWriter.begin(); | | |||
343 | Test::CSSHelper testCSSHelper; | | |||
344 | Test::ObjectTreeSource emptySource(&testWriter, &testCSSHelper); | | |||
345 | MimeTreeParser::ObjectTreeParser otp(&emptySource, &nodeHelper); | | |||
346 | otp.parseObjectTree(originalMessage.data()); | | |||
347 | testWriter.end(); | | |||
348 | | ||||
349 | QCOMPARE(otp.plainTextContent().toLatin1().data(), ""); | | |||
350 | QVERIFY(testWriter.data().contains("Sorry, certificate could not be imported.")); | | |||
351 | } | |