File indexing completed on 2025-02-16 04:57:36
0001 /* 0002 SPDX-FileCopyrightText: 2010 Thomas McGuire <thomas.mcguire@kdab.com> 0003 SPDX-FileCopyrightText: 2019 Sandro Knauß <sknauss@kde.org> 0004 0005 SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 #include "basicobjecttreeparsertest.h" 0009 #include "setupenv.h" 0010 #include "util.h" 0011 0012 #include <MimeTreeParser/MessagePart> 0013 #include <MimeTreeParser/NodeHelper> 0014 #include <MimeTreeParser/ObjectTreeParser> 0015 #include <MimeTreeParser/SimpleObjectTreeSource> 0016 0017 #include <QProcess> 0018 #include <QTest> 0019 0020 using namespace MimeTreeParser; 0021 0022 QTEST_MAIN(ObjectTreeParserTest) 0023 0024 void ObjectTreeParserTest::initTestCase() 0025 { 0026 Test::setupEnv(); 0027 qputenv("TZ", "GMT"); 0028 } 0029 0030 QString stringifyMessagePartTree(const MimeTreeParser::MessagePart::Ptr &messagePart, QString indent) 0031 { 0032 const QString line = QStringLiteral("%1 * %3\n").arg(indent, QString::fromUtf8(messagePart->metaObject()->className())); 0033 QString ret = line; 0034 0035 indent += QLatin1Char(' '); 0036 const auto subParts = messagePart->subParts(); 0037 for (const auto &subPart : subParts) { 0038 ret += stringifyMessagePartTree(subPart, indent); 0039 } 0040 return ret; 0041 } 0042 0043 void testMessagePartTree(const MessagePart::Ptr &messagePart, const QString &mailFileName) 0044 { 0045 QString renderedTree = stringifyMessagePartTree(messagePart, QString()); 0046 0047 const QString treeFileName = QLatin1StringView(MAIL_DATA_DIR) + QLatin1Char('/') + mailFileName + QStringLiteral(".tree"); 0048 const QString outTreeFileName = mailFileName + QStringLiteral(".tree"); 0049 0050 { 0051 QFile f(outTreeFileName); 0052 f.open(QIODevice::WriteOnly); 0053 f.write(renderedTree.toUtf8()); 0054 f.close(); 0055 } 0056 // compare to reference file 0057 const QStringList args = QStringList() << QStringLiteral("-u") << treeFileName << outTreeFileName; 0058 QProcess proc; 0059 proc.setProcessChannelMode(QProcess::ForwardedChannels); 0060 proc.start(QStringLiteral("diff"), args); 0061 QVERIFY(proc.waitForFinished()); 0062 0063 QCOMPARE(proc.exitCode(), 0); 0064 } 0065 0066 void ObjectTreeParserTest::testMailWithoutEncryption() 0067 { 0068 KMime::Message::Ptr originalMessage = readAndParseMail(QStringLiteral("encapsulated-with-attachment.mbox")); 0069 NodeHelper nodeHelper; 0070 SimpleObjectTreeSource testSource; 0071 ObjectTreeParser otp(&testSource, &nodeHelper); 0072 otp.parseObjectTree(originalMessage.data()); 0073 QVERIFY(!nodeHelper.unencryptedMessage(originalMessage)); 0074 } 0075 0076 void ObjectTreeParserTest::testBinaryAttachmentNotPGP() 0077 { 0078 KMime::Message::Ptr originalMessage = readAndParseMail(QStringLiteral("binary-attachment-not-pgp.mbox")); 0079 NodeHelper nodeHelper; 0080 SimpleObjectTreeSource testSource; 0081 ObjectTreeParser otp(&testSource, &nodeHelper); 0082 otp.parseObjectTree(originalMessage.data()); 0083 0084 QCOMPARE(originalMessage->contentType()->mimeType().data(), "multipart/mixed"); 0085 QCOMPARE(originalMessage->contents().size(), 2); 0086 QCOMPARE(originalMessage->contents().at(0)->contentType()->mimeType().data(), "text/plain"); 0087 QCOMPARE(originalMessage->contents().at(1)->contentType()->mimeType().data(), "application/msword"); 0088 0089 QCOMPARE(nodeHelper.encryptionState(originalMessage->contents().at(0)), KMMsgNotEncrypted); 0090 QCOMPARE(nodeHelper.encryptionState(originalMessage->contents().at(1)), KMMsgNotEncrypted); 0091 } 0092 0093 void ObjectTreeParserTest::testSignedForwardedOpenPGPSignedEncrypted() 0094 { 0095 KMime::Message::Ptr originalMessage = readAndParseMail(QStringLiteral("signed-forward-openpgp-signed-encrypted.mbox")); 0096 0097 NodeHelper nodeHelper; 0098 SimpleObjectTreeSource testSource; 0099 ObjectTreeParser otp(&testSource, &nodeHelper); 0100 otp.parseObjectTree(originalMessage.data()); 0101 0102 QCOMPARE(otp.plainTextContent().toLatin1().data(), "bla bla bla"); // The textual content doesn't include the encrypted encapsulated message by design 0103 QCOMPARE(nodeHelper.overallEncryptionState(originalMessage.data()), KMMsgPartiallyEncrypted); 0104 QCOMPARE(nodeHelper.overallSignatureState(originalMessage.data()), KMMsgFullySigned); 0105 0106 KMime::Message::Ptr unencryptedMessage = nodeHelper.unencryptedMessage(originalMessage); 0107 QVERIFY(!unencryptedMessage); // We must not invalidate the outer signature 0108 } 0109 0110 void ObjectTreeParserTest::testForwardedOpenPGPSignedEncrypted() 0111 { 0112 KMime::Message::Ptr originalMessage = readAndParseMail(QStringLiteral("forward-openpgp-signed-encrypted.mbox")); 0113 0114 NodeHelper nodeHelper; 0115 SimpleObjectTreeSource testSource; 0116 ObjectTreeParser otp(&testSource, &nodeHelper); 0117 testSource.setDecryptMessage(true); 0118 otp.parseObjectTree(originalMessage.data()); 0119 0120 QCOMPARE(otp.plainTextContent().toLatin1().data(), "bla bla bla"); // The textual content doesn't include the encrypted encapsulated message by design 0121 QCOMPARE(nodeHelper.overallEncryptionState(originalMessage.data()), KMMsgPartiallyEncrypted); 0122 0123 QCOMPARE(nodeHelper.overallSignatureState(originalMessage.data()), KMMsgPartiallySigned); 0124 0125 // Now, test that the unencrypted message is generated correctly 0126 KMime::Message::Ptr unencryptedMessage = nodeHelper.unencryptedMessage(originalMessage); 0127 QVERIFY(unencryptedMessage.data()); 0128 QCOMPARE(unencryptedMessage->contentType()->mimeType().data(), "multipart/mixed"); 0129 QCOMPARE(unencryptedMessage->contents().size(), 2); 0130 QCOMPARE(unencryptedMessage->contents().first()->contentType()->mimeType().data(), "text/plain"); 0131 QCOMPARE(unencryptedMessage->contents().first()->decodedContent().data(), "bla bla bla"); 0132 QCOMPARE(unencryptedMessage->contents().at(1)->contentType()->mimeType().data(), "message/rfc822"); 0133 KMime::Message::Ptr encapsulated = unencryptedMessage->contents().at(1)->bodyAsMessage(); 0134 QCOMPARE(encapsulated->contentType()->mimeType().data(), "multipart/signed"); 0135 QCOMPARE(encapsulated->contents().size(), 2); 0136 QCOMPARE(encapsulated->contents().first()->contentType()->mimeType().data(), "text/plain"); 0137 QCOMPARE(encapsulated->contents().at(1)->contentType()->mimeType().data(), "application/pgp-signature"); 0138 QCOMPARE(encapsulated->contents().first()->decodedContent().data(), "encrypted message text"); 0139 0140 // TODO: Check that the signature is valid 0141 } 0142 0143 void ObjectTreeParserTest::testSMIMESignedEncrypted() 0144 { 0145 KMime::Message::Ptr originalMessage = readAndParseMail(QStringLiteral("smime-signed-encrypted.mbox")); 0146 0147 NodeHelper nodeHelper; 0148 SimpleObjectTreeSource testSource; 0149 ObjectTreeParser otp(&testSource, &nodeHelper); 0150 testSource.setDecryptMessage(true); 0151 otp.parseObjectTree(originalMessage.data()); 0152 0153 QCOMPARE(otp.plainTextContent().toLatin1().data(), "encrypted message text"); 0154 QCOMPARE(nodeHelper.overallEncryptionState(originalMessage.data()), KMMsgFullyEncrypted); 0155 0156 QCOMPARE(nodeHelper.overallSignatureState(originalMessage.data()), KMMsgFullySigned); 0157 0158 // Now, test that the unencrypted message is generated correctly 0159 KMime::Message::Ptr unencryptedMessage = nodeHelper.unencryptedMessage(originalMessage); 0160 QCOMPARE(unencryptedMessage->contentType()->mimeType().data(), "multipart/signed"); 0161 QCOMPARE(unencryptedMessage->contents().size(), 2); 0162 QCOMPARE(unencryptedMessage->contents().first()->contentType()->mimeType().data(), "text/plain"); 0163 QCOMPARE(unencryptedMessage->contents().at(1)->contentType()->mimeType().data(), "application/pkcs7-signature"); 0164 QCOMPARE(unencryptedMessage->contents().first()->decodedContent().data(), "encrypted message text"); 0165 0166 // TODO: Check that the signature is valid 0167 } 0168 0169 void ObjectTreeParserTest::testOpenPGPSignedEncrypted() 0170 { 0171 KMime::Message::Ptr originalMessage = readAndParseMail(QStringLiteral("openpgp-signed-encrypted.mbox")); 0172 0173 NodeHelper nodeHelper; 0174 SimpleObjectTreeSource testSource; 0175 ObjectTreeParser otp(&testSource, &nodeHelper); 0176 testSource.setDecryptMessage(true); 0177 otp.parseObjectTree(originalMessage.data()); 0178 0179 QCOMPARE(otp.plainTextContent().toLatin1().data(), "encrypted message text"); 0180 QCOMPARE(nodeHelper.overallEncryptionState(originalMessage.data()), KMMsgFullyEncrypted); 0181 0182 QCOMPARE(nodeHelper.overallSignatureState(originalMessage.data()), KMMsgFullySigned); 0183 0184 // Now, test that the unencrypted message is generated correctly 0185 KMime::Message::Ptr unencryptedMessage = nodeHelper.unencryptedMessage(originalMessage); 0186 QCOMPARE(unencryptedMessage->contentType()->mimeType().data(), "multipart/signed"); 0187 QCOMPARE(unencryptedMessage->contents().size(), 2); 0188 QCOMPARE(unencryptedMessage->contents().first()->contentType()->mimeType().data(), "text/plain"); 0189 QCOMPARE(unencryptedMessage->contents().at(1)->contentType()->mimeType().data(), "application/pgp-signature"); 0190 QCOMPARE(unencryptedMessage->contents().first()->decodedContent().data(), "encrypted message text"); 0191 0192 // TODO: Check that the signature is valid 0193 } 0194 0195 void ObjectTreeParserTest::testOpenPGPEncryptedAndSigned() 0196 { 0197 KMime::Message::Ptr originalMessage = readAndParseMail(QStringLiteral("openpgp-encrypted+signed.mbox")); 0198 0199 NodeHelper nodeHelper; 0200 SimpleObjectTreeSource testSource; 0201 ObjectTreeParser otp(&testSource, &nodeHelper); 0202 testSource.setDecryptMessage(true); 0203 otp.parseObjectTree(originalMessage.data()); 0204 0205 QCOMPARE(otp.plainTextContent().toLatin1().data(), "encrypted message text"); 0206 QCOMPARE(nodeHelper.overallEncryptionState(originalMessage.data()), KMMsgFullyEncrypted); 0207 0208 QCOMPARE(nodeHelper.overallSignatureState(originalMessage.data()), KMMsgFullySigned); 0209 0210 // Now, test that the unencrypted message is generated correctly 0211 KMime::Message::Ptr unencryptedMessage = nodeHelper.unencryptedMessage(originalMessage); 0212 QCOMPARE(unencryptedMessage->contentType()->mimeType().data(), "text/plain"); 0213 QCOMPARE(unencryptedMessage->contents().size(), 0); 0214 QCOMPARE(unencryptedMessage->decodedContent().data(), "encrypted message text"); 0215 0216 // TODO: Check that the signature is valid 0217 } 0218 0219 void ObjectTreeParserTest::testOpenPGPEncrypted() 0220 { 0221 KMime::Message::Ptr originalMessage = readAndParseMail(QStringLiteral("openpgp-encrypted.mbox")); 0222 0223 NodeHelper nodeHelper; 0224 SimpleObjectTreeSource testSource; 0225 ObjectTreeParser otp(&testSource, &nodeHelper); 0226 testSource.setDecryptMessage(true); 0227 otp.parseObjectTree(originalMessage.data()); 0228 0229 QCOMPARE(otp.plainTextContent().toLatin1().data(), "encrypted message text"); 0230 QCOMPARE(nodeHelper.overallEncryptionState(originalMessage.data()), KMMsgFullyEncrypted); 0231 0232 // Now, test that the unencrypted message is generated correctly 0233 KMime::Message::Ptr unencryptedMessage = nodeHelper.unencryptedMessage(originalMessage); 0234 QCOMPARE(unencryptedMessage->contentType()->mimeType().data(), "text/plain"); 0235 QCOMPARE(unencryptedMessage->decodedContent().data(), "encrypted message text"); 0236 QCOMPARE(unencryptedMessage->contents().size(), 0); 0237 } 0238 0239 void ObjectTreeParserTest::testOpenPGPEncryptedNotDecrypted() 0240 { 0241 KMime::Message::Ptr originalMessage = readAndParseMail(QStringLiteral("openpgp-encrypted.mbox")); 0242 0243 NodeHelper nodeHelper; 0244 SimpleObjectTreeSource testSource; 0245 ObjectTreeParser otp(&testSource, &nodeHelper); 0246 otp.parseObjectTree(originalMessage.data()); 0247 0248 QCOMPARE(nodeHelper.overallEncryptionState(originalMessage.data()), KMMsgFullyEncrypted); 0249 QCOMPARE(otp.plainTextContent().toLatin1().data(), ""); 0250 0251 KMime::Message::Ptr unencryptedMessage = nodeHelper.unencryptedMessage(originalMessage); 0252 QVERIFY(!unencryptedMessage); 0253 } 0254 0255 void ObjectTreeParserTest::testOpenPGPEncryptedOverrideEncoding() 0256 { 0257 KMime::Message::Ptr originalMessage = readAndParseMail(QStringLiteral("openpgp-encrypted-attachment-iso-8859-15-encoded.mbox")); 0258 0259 NodeHelper nodeHelper; 0260 SimpleObjectTreeSource testSource; 0261 testSource.setDecryptMessage(true); 0262 const QString data = QStringLiteral( 0263 "german umlauts ISO-8859-15:\n\u00c3\u20ac - a with two dots - utf-8: U+00C3 U20AC\n\u00c3\u009f - eszett or \"sharp s\" - utf-8: U+00C3 U+009F\n"); 0264 ObjectTreeParser otp(&testSource, &nodeHelper); 0265 otp.parseObjectTree(originalMessage.data()); 0266 0267 QCOMPARE(nodeHelper.overallEncryptionState(originalMessage.data()), KMMsgFullyEncrypted); 0268 0269 QVERIFY(otp.plainTextContent() != data); // it is not utf-8 0270 0271 testSource.setOverrideCodecName("iso-8859-15"); 0272 otp.parseObjectTree(originalMessage.data()); 0273 QCOMPARE(otp.plainTextContent(), data); 0274 } 0275 0276 void ObjectTreeParserTest::testAsync_data() 0277 { 0278 QTest::addColumn<QString>("mailFileName"); 0279 QTest::addColumn<QString>("output"); 0280 0281 QTest::newRow("openpgp-encrypt") << QStringLiteral("openpgp-encrypted.mbox") << QStringLiteral("encrypted message text"); 0282 QTest::newRow("smime-opaque-sign") << QStringLiteral("smime-opaque-sign.mbox") << QStringLiteral("A simple signed only test."); 0283 QTest::newRow("smime-encrypt") << QStringLiteral("smime-encrypted.mbox") << QStringLiteral("The quick brown fox jumped over the lazy dog."); 0284 QTest::newRow("openpgp-inline-encrypt") << QStringLiteral("openpgp-inline-charset-encrypted.mbox") 0285 << QStringLiteral("asdasd asd asd asdf sadf sdaf sadf \u00F6\u00E4\u00FC"); 0286 } 0287 0288 void ObjectTreeParserTest::testAsync() 0289 { 0290 QFETCH(QString, mailFileName); 0291 QFETCH(QString, output); 0292 0293 KMime::Message::Ptr originalMessage = readAndParseMail(mailFileName); 0294 NodeHelper nodeHelper; 0295 SimpleObjectTreeSource testSource; 0296 testSource.setDecryptMessage(true); 0297 { 0298 QEventLoop loop; 0299 ObjectTreeParser otp(&testSource, &nodeHelper); 0300 0301 connect(&nodeHelper, &NodeHelper::update, &loop, &QEventLoop::quit); 0302 otp.setAllowAsync(true); 0303 otp.parseObjectTree(originalMessage.data()); 0304 loop.exec(); 0305 } 0306 // Job ended 0307 { 0308 ObjectTreeParser otp(&testSource, &nodeHelper); 0309 otp.setAllowAsync(true); 0310 otp.parseObjectTree(originalMessage.data()); 0311 QCOMPARE(otp.plainTextContent(), output); 0312 } 0313 } 0314 0315 void ObjectTreeParserTest::testHtmlContent_data() 0316 { 0317 QTest::addColumn<QString>("mailFileName"); 0318 QTest::addColumn<QString>("output"); 0319 0320 QTest::newRow("html-attachments1") << QStringLiteral("html-attachment1.mbox") 0321 << QStringLiteral("<html><head></head><body><p><span style=\"font-family:Arial;\">A Body Text</span></p></body></html>"); 0322 QTest::newRow("html-attachments2") << QStringLiteral("html-attachment2.mbox") << QStringLiteral("<html><head></head><body>HTML Text</body></html>"); 0323 } 0324 0325 void ObjectTreeParserTest::testHtmlContent() 0326 { 0327 QFETCH(QString, mailFileName); 0328 QFETCH(QString, output); 0329 0330 KMime::Message::Ptr originalMessage = readAndParseMail(mailFileName); 0331 NodeHelper nodeHelper; 0332 SimpleObjectTreeSource testSource; 0333 ObjectTreeParser otp(&testSource, &nodeHelper); 0334 testSource.setDecryptMessage(true); 0335 otp.parseObjectTree(originalMessage.data()); 0336 0337 QVERIFY(otp.plainTextContent().isEmpty()); 0338 QCOMPARE(otp.htmlContent(), output); 0339 } 0340 0341 void ObjectTreeParserTest::testRenderedTree_data() 0342 { 0343 QTest::addColumn<QString>("mailFileName"); 0344 0345 QDir dir(QStringLiteral(MAIL_DATA_DIR)); 0346 const auto l = dir.entryList(QStringList(QStringLiteral("*.mbox")), QDir::Files | QDir::Readable | QDir::NoSymLinks); 0347 for (const QString &file : l) { 0348 if (!QFile::exists(dir.path() + QLatin1Char('/') + file + QStringLiteral(".tree"))) { 0349 continue; 0350 } 0351 QTest::newRow(file.toLatin1().constData()) << file; 0352 } 0353 } 0354 0355 void ObjectTreeParserTest::testRenderedTree() 0356 { 0357 QFETCH(QString, mailFileName); 0358 KMime::Message::Ptr originalMessage = readAndParseMail(mailFileName); 0359 NodeHelper nodeHelper; 0360 SimpleObjectTreeSource testSource; 0361 ObjectTreeParser otp(&testSource, &nodeHelper); 0362 testSource.setDecryptMessage(true); 0363 otp.parseObjectTree(originalMessage.data()); 0364 0365 testMessagePartTree(otp.parsedPart(), mailFileName); 0366 } 0367 0368 void ObjectTreeParserTest::testParsePlainMessage() 0369 { 0370 KMime::Message::Ptr msg(new KMime::Message()); 0371 QByteArray content( 0372 "From: Thomas McGuire <dontspamme@gmx.net>\n" 0373 "Subject: Plain Message Test\n" 0374 "Date: Wed, 5 Aug 2009 10:58:27 +0200\n" 0375 "MIME-Version: 1.0\n" 0376 "Content-Type: text/plain;\n" 0377 " charset=\"iso-8859-15\"\n" 0378 "\n" 0379 "This is the message text.\n"); 0380 msg->setContent(content); 0381 msg->parse(); 0382 0383 QCOMPARE(msg->subject()->as7BitString(false).constData(), "Plain Message Test"); 0384 QCOMPARE(msg->contents().size(), 0); 0385 0386 // Parse the message 0387 SimpleObjectTreeSource testSource; 0388 ObjectTreeParser otp(&testSource); 0389 otp.parseObjectTree(msg.data()); 0390 0391 // Check that the textual content and the charset have the expected values 0392 QCOMPARE(otp.plainTextContent(), QStringLiteral("This is the message text.\n")); 0393 QVERIFY(otp.htmlContent().isEmpty()); 0394 QCOMPARE(otp.plainTextContentCharset().toLower(), QByteArray("iso-8859-15")); 0395 0396 // Check that the message was not modified in any way 0397 QCOMPARE(msg->encodedContent().constData(), content.constData()); 0398 0399 // Test that the charset of messages without an explicit charset declaration 0400 // is correct 0401 content = 0402 "From: Thomas McGuire <dontspamme@gmx.net>\n" 0403 "Subject: Plain Message Test\n" 0404 "Date: Wed, 5 Aug 2009 10:58:27 +0200\n" 0405 "MIME-Version: 1.0\n" 0406 "Content-Type: text/plain;\n" 0407 "\n" 0408 "This is the message text.\n"; 0409 msg->setContent(content); 0410 msg->parse(); 0411 ObjectTreeParser otp2(&testSource); 0412 otp2.parseObjectTree(msg.data()); 0413 QCOMPARE(otp2.plainTextContentCharset().constData(), msg->defaultCharset().constData()); 0414 } 0415 0416 void ObjectTreeParserTest::testParseEncapsulatedMessage() 0417 { 0418 KMime::Message::Ptr msg = readAndParseMail(QStringLiteral("encapsulated-with-attachment.mbox")); 0419 QCOMPARE(msg->subject()->as7BitString(false).constData(), "Fwd: Test with attachment"); 0420 QCOMPARE(msg->contents().size(), 2); 0421 0422 // Parse the message 0423 SimpleObjectTreeSource testSource; 0424 NodeHelper nodeHelper; 0425 ObjectTreeParser otp(&testSource, &nodeHelper); 0426 otp.parseObjectTree(msg.data()); 0427 0428 // Check that the OTP didn't modify the message in weird ways 0429 QCOMPARE(msg->contents().size(), 2); 0430 QCOMPARE(msg->contents().at(0)->contents().size(), 0); 0431 QCOMPARE(msg->contents().at(1)->contents().size(), 1); 0432 QCOMPARE(msg->contents().at(1)->contents().first()->contents().size(), 2); 0433 QCOMPARE(msg->contents().at(1)->contents().first()->contents().at(0)->contents().size(), 0); 0434 QCOMPARE(msg->contents().at(1)->contents().first()->contents().at(1)->contents().size(), 0); 0435 0436 // Check that the textual content and the charset have the expected values 0437 QCOMPARE(otp.plainTextContent(), QStringLiteral("This is the first encapsulating message.\n")); 0438 QCOMPARE(otp.plainTextContentCharset().toLower(), QByteArray("iso-8859-15")); 0439 QVERIFY(otp.htmlContent().isEmpty()); 0440 0441 // Check that the objecttreeparser did process the encapsulated message 0442 KMime::Message::Ptr encapsulated = msg->contents().at(1)->bodyAsMessage(); 0443 QVERIFY(encapsulated.data()); 0444 QVERIFY(nodeHelper.nodeProcessed(encapsulated.data())); 0445 QVERIFY(nodeHelper.nodeProcessed(encapsulated->contents().at(0))); 0446 QVERIFY(nodeHelper.nodeProcessed(encapsulated->contents().at(1))); 0447 QVERIFY(nodeHelper.partMetaData(msg->contents().at(1)).isEncapsulatedRfc822Message); 0448 } 0449 0450 void ObjectTreeParserTest::testMissingContentTypeHeader() 0451 { 0452 KMime::Message::Ptr msg = readAndParseMail(QStringLiteral("no-content-type.mbox")); 0453 QCOMPARE(msg->subject()->as7BitString(false).constData(), "Simple Mail Without Content-Type Header"); 0454 QCOMPARE(msg->contents().size(), 0); 0455 0456 NodeHelper nodeHelper; 0457 SimpleObjectTreeSource testSource; 0458 ObjectTreeParser otp(&testSource, &nodeHelper); 0459 otp.parseObjectTree(msg.data()); 0460 0461 QCOMPARE(otp.plainTextContent().toLatin1().data(), "asdfasdf"); 0462 QVERIFY(otp.htmlContent().isEmpty()); 0463 } 0464 0465 void ObjectTreeParserTest::testInlinePGPDecryption() 0466 { 0467 KMime::Message::Ptr msg = readAndParseMail(QStringLiteral("inlinepgpencrypted.mbox")); 0468 0469 QCOMPARE(msg->subject()->as7BitString(false).constData(), "inlinepgpencrypted"); 0470 QCOMPARE(msg->contents().size(), 0); 0471 0472 NodeHelper nodeHelper; 0473 SimpleObjectTreeSource testSource; 0474 ObjectTreeParser otp(&testSource, &nodeHelper); 0475 0476 testSource.setDecryptMessage(true); 0477 otp.parseObjectTree(msg.data()); 0478 0479 QCOMPARE(otp.plainTextContent().toLatin1().data(), "some random text"); 0480 0481 // This test is only a workaround, till we can set the memento to the proper node of the mail. 0482 auto content = new KMime::Content; 0483 QVERIFY(nodeHelper.bodyPartMemento(content, "decryptverify-attachment:?place=0")); 0484 0485 QVERIFY(otp.htmlContent().isEmpty()); 0486 } 0487 0488 void ObjectTreeParserTest::testInlinePGPSigned() 0489 { 0490 KMime::Message::Ptr msg = readAndParseMail(QStringLiteral("openpgp-inline-signed.mbox")); 0491 0492 QCOMPARE(msg->subject()->as7BitString(false).constData(), "test"); 0493 QCOMPARE(msg->contents().size(), 0); 0494 0495 NodeHelper nodeHelper; 0496 SimpleObjectTreeSource testSource; 0497 ObjectTreeParser otp(&testSource, &nodeHelper); 0498 0499 testSource.setDecryptMessage(true); 0500 otp.parseObjectTree(msg.data()); 0501 0502 // This test is only a workaround, till we can set the memento to the proper node of the mail. 0503 QVERIFY(nodeHelper.bodyPartMemento(nullptr, "verification-attachment:?place=0")); 0504 } 0505 0506 void ObjectTreeParserTest::testHTML() 0507 { 0508 KMime::Message::Ptr msg = readAndParseMail(QStringLiteral("html.mbox")); 0509 0510 QCOMPARE(msg->subject()->as7BitString(false).constData(), "HTML test"); 0511 QCOMPARE(msg->contents().size(), 2); 0512 0513 SimpleObjectTreeSource testSource; 0514 ObjectTreeParser otp(&testSource); 0515 0516 otp.parseObjectTree(msg.data()); 0517 0518 QCOMPARE(otp.plainTextContent().toLatin1().data(), "Some HTML text"); 0519 QVERIFY(otp.htmlContent().contains(QLatin1StringView("Some <span style=\" font-weight:600;\">HTML</span> text"))); 0520 QCOMPARE(otp.htmlContentCharset().data(), "windows-1252"); 0521 } 0522 0523 void ObjectTreeParserTest::testHTMLasText() 0524 { 0525 KMime::Message::Ptr msg = readAndParseMail(QStringLiteral("html.mbox")); 0526 0527 QCOMPARE(msg->subject()->as7BitString(false).constData(), "HTML test"); 0528 QCOMPARE(msg->contents().size(), 2); 0529 0530 SimpleObjectTreeSource testSource; 0531 ObjectTreeParser otp(&testSource); 0532 0533 testSource.setPreferredMode(MimeTreeParser::Util::MultipartPlain); 0534 otp.parseObjectTree(msg.data()); 0535 0536 QCOMPARE(otp.htmlContent().toLatin1().constData(), ""); 0537 QCOMPARE(otp.htmlContentCharset().constData(), ""); 0538 QCOMPARE(otp.plainTextContent().toLatin1().constData(), "Some HTML text"); 0539 QCOMPARE(otp.plainTextContentCharset().constData(), "windows-1252"); 0540 } 0541 0542 void ObjectTreeParserTest::testHTMLOnly() 0543 { 0544 KMime::Message::Ptr msg = readAndParseMail(QStringLiteral("htmlonly.mbox")); 0545 0546 QCOMPARE(msg->subject()->as7BitString(false).constData(), "HTML test"); 0547 QCOMPARE(msg->contents().size(), 0); 0548 0549 SimpleObjectTreeSource testSource; 0550 ObjectTreeParser otp(&testSource); 0551 0552 otp.parseObjectTree(msg.data()); 0553 0554 QVERIFY(otp.plainTextContent().isEmpty()); 0555 QVERIFY(otp.htmlContent().contains(QLatin1StringView("<b>SOME</b> HTML text."))); 0556 } 0557 0558 #include "moc_basicobjecttreeparsertest.cpp"