diff --git a/messageviewer/src/messagepartthemes/default/autotests/objecttreeparsertest.h b/messageviewer/src/messagepartthemes/default/autotests/objecttreeparsertest.h --- a/messageviewer/src/messagepartthemes/default/autotests/objecttreeparsertest.h +++ b/messageviewer/src/messagepartthemes/default/autotests/objecttreeparsertest.h @@ -29,14 +29,6 @@ void initTestCase(); private Q_SLOTS: - void test_parseEncapsulatedMessage(); - void test_parsePlainMessage(); - void test_missingContentTypeHeader(); - void test_inlinePGPDecryption(); - void test_inlinePGPSigned(); - void test_HTML(); - void test_HTMLasText(); - void test_HTMLOnly(); void test_HTMLOnlyText(); void test_HTMLExternal(); void test_Alternative(); diff --git a/messageviewer/src/messagepartthemes/default/autotests/objecttreeparsertest.cpp b/messageviewer/src/messagepartthemes/default/autotests/objecttreeparsertest.cpp --- a/messageviewer/src/messagepartthemes/default/autotests/objecttreeparsertest.cpp +++ b/messageviewer/src/messagepartthemes/default/autotests/objecttreeparsertest.cpp @@ -35,217 +35,6 @@ Test::setupEnv(); } -void ObjectTreeParserTester::test_parsePlainMessage() -{ - KMime::Message::Ptr msg(new KMime::Message()); - QByteArray content( - "From: Thomas McGuire \n" - "Subject: Plain Message Test\n" - "Date: Wed, 5 Aug 2009 10:58:27 +0200\n" - "MIME-Version: 1.0\n" - "Content-Type: text/plain;\n" - " charset=\"iso-8859-15\"\n" - "\n" - "This is the message text.\n"); - msg->setContent(content); - msg->parse(); - - QCOMPARE(msg->subject()->as7BitString(false).constData(), "Plain Message Test"); - QCOMPARE(msg->contents().size(), 0); - - // Parse the message - Test::ObjectTreeSource emptySource(nullptr, nullptr); - ObjectTreeParser otp(&emptySource); - otp.parseObjectTree(msg.data()); - - // Check that the textual content and the charset have the expected values - QCOMPARE(otp.plainTextContent(), QStringLiteral("This is the message text.\n")); - QVERIFY(otp.htmlContent().isEmpty()); - QCOMPARE(otp.plainTextContentCharset().toLower(), QByteArray("iso-8859-15")); - - // Check that the message was not modified in any way - QCOMPARE(msg->encodedContent().constData(), content.constData()); - - // Test that the charset of messages without an explicit charset declaration - // is correct - content - = "From: Thomas McGuire \n" - "Subject: Plain Message Test\n" - "Date: Wed, 5 Aug 2009 10:58:27 +0200\n" - "MIME-Version: 1.0\n" - "Content-Type: text/plain;\n" - "\n" - "This is the message text.\n"; - msg->setContent(content); - msg->parse(); - ObjectTreeParser otp2(&emptySource); - otp2.parseObjectTree(msg.data()); - QCOMPARE(otp2.plainTextContentCharset().constData(), msg->defaultCharset().constData()); -} - -void ObjectTreeParserTester::test_parseEncapsulatedMessage() -{ - KMime::Message::Ptr msg - = Test::readAndParseMail(QStringLiteral("encapsulated-with-attachment.mbox")); - QCOMPARE(msg->subject()->as7BitString(false).constData(), "Fwd: Test with attachment"); - QCOMPARE(msg->contents().size(), 2); - - // Parse the message - BufferedHtmlWriter testWriter; - testWriter.begin(); - Test::CSSHelper testCSSHelper; - NodeHelper nodeHelper; - Test::ObjectTreeSource emptySource(&testWriter, &testCSSHelper); - ObjectTreeParser otp(&emptySource, &nodeHelper); - otp.parseObjectTree(msg.data()); - testWriter.end(); - - // Check that the OTP didn't modify the message in weird ways - QCOMPARE(msg->contents().size(), 2); - QCOMPARE(msg->contents().at(0)->contents().size(), 0); - QCOMPARE(msg->contents().at(1)->contents().size(), 1); - QCOMPARE(msg->contents().at(1)->contents().first()->contents().size(), 2); - QCOMPARE(msg->contents().at(1)->contents().first()->contents().at(0)->contents().size(), 0); - QCOMPARE(msg->contents().at(1)->contents().first()->contents().at(1)->contents().size(), 0); - - // Check that the textual content and the charset have the expected values - QCOMPARE(otp.plainTextContent(), QStringLiteral("This is the first encapsulating message.\n")); - QCOMPARE(otp.plainTextContentCharset().toLower(), QByteArray("iso-8859-15")); - QVERIFY(otp.htmlContent().isEmpty()); - - // Check that the objecttreeparser did process the encapsulated message - KMime::Message::Ptr encapsulated = msg->contents().at(1)->bodyAsMessage(); - QVERIFY(encapsulated.data()); - QVERIFY(nodeHelper.nodeProcessed(encapsulated.data())); - QVERIFY(nodeHelper.nodeProcessed(encapsulated->contents().at(0))); - QVERIFY(nodeHelper.nodeProcessed(encapsulated->contents().at(1))); - QVERIFY(nodeHelper.partMetaData(msg->contents().at(1)).isEncapsulatedRfc822Message); -} - -void ObjectTreeParserTester::test_missingContentTypeHeader() -{ - KMime::Message::Ptr msg = Test::readAndParseMail(QStringLiteral("no-content-type.mbox")); - QCOMPARE(msg->subject()->as7BitString( - false).constData(), "Simple Mail Without Content-Type Header"); - QCOMPARE(msg->contents().size(), 0); - - BufferedHtmlWriter testWriter; - testWriter.begin(); - Test::CSSHelper testCSSHelper; - NodeHelper nodeHelper; - Test::ObjectTreeSource emptySource(&testWriter, &testCSSHelper); - ObjectTreeParser otp(&emptySource, &nodeHelper); - otp.parseObjectTree(msg.data()); - testWriter.end(); - - QCOMPARE(otp.plainTextContent().toLatin1().data(), "asdfasdf"); - QVERIFY(otp.htmlContent().isEmpty()); -} - -void ObjectTreeParserTester::test_inlinePGPDecryption() -{ - KMime::Message::Ptr msg = Test::readAndParseMail(QStringLiteral("inlinepgpencrypted.mbox")); - - QCOMPARE(msg->subject()->as7BitString(false).constData(), "inlinepgpencrypted"); - QCOMPARE(msg->contents().size(), 0); - - BufferedHtmlWriter testWriter; - testWriter.begin(); - Test::CSSHelper testCSSHelper; - NodeHelper nodeHelper; - Test::ObjectTreeSource emptySource(&testWriter, &testCSSHelper); - ObjectTreeParser otp(&emptySource, &nodeHelper); - - emptySource.setAllowDecryption(true); - otp.parseObjectTree(msg.data()); - testWriter.end(); - - QCOMPARE(otp.plainTextContent().toLatin1().data(), "some random text"); - - // This test is only a workaround, till we can set the memento to the proper node of the mail. - KMime::Content *content = new KMime::Content; - QVERIFY(nodeHelper.bodyPartMemento(content, "decryptverify-attachment:?place=0")); - - QVERIFY(otp.htmlContent().isEmpty()); -} - -void ObjectTreeParserTester::test_inlinePGPSigned() -{ - KMime::Message::Ptr msg = Test::readAndParseMail(QStringLiteral("openpgp-inline-signed.mbox")); - - QCOMPARE(msg->subject()->as7BitString(false).constData(), "test"); - QCOMPARE(msg->contents().size(), 0); - - BufferedHtmlWriter testWriter; - testWriter.begin(); - Test::CSSHelper testCSSHelper; - NodeHelper nodeHelper; - Test::ObjectTreeSource emptySource(&testWriter, &testCSSHelper); - ObjectTreeParser otp(&emptySource, &nodeHelper); - - emptySource.setAllowDecryption(true); - otp.parseObjectTree(msg.data()); - testWriter.end(); - - // This test is only a workaround, till we can set the memento to the proper node of the mail. - QVERIFY(nodeHelper.bodyPartMemento(nullptr, "verification-attachment:?place=0")); -} - -void ObjectTreeParserTester::test_HTML() -{ - KMime::Message::Ptr msg = Test::readAndParseMail(QStringLiteral("html.mbox")); - - QCOMPARE(msg->subject()->as7BitString(false).constData(), "HTML test"); - QCOMPARE(msg->contents().size(), 2); - - Test::ObjectTreeSource emptySource(nullptr, nullptr); - ObjectTreeParser otp(&emptySource); - - otp.parseObjectTree(msg.data()); - - QCOMPARE(otp.plainTextContent().toLatin1().data(), "Some HTML text"); - QVERIFY(otp.htmlContent().contains(QLatin1String( - "Some HTML text"))); - QCOMPARE(otp.htmlContentCharset().data(), "windows-1252"); -} - -void ObjectTreeParserTester::test_HTMLasText() -{ - KMime::Message::Ptr msg = Test::readAndParseMail(QStringLiteral("html.mbox")); - - QCOMPARE(msg->subject()->as7BitString(false).constData(), "HTML test"); - QCOMPARE(msg->contents().size(), 2); - - BufferedHtmlWriter testWriter; - testWriter.begin(); - Test::CSSHelper testCSSHelper; - Test::ObjectTreeSource emptySource(&testWriter, &testCSSHelper); - ObjectTreeParser otp(&emptySource); - emptySource.setPreferredMode(MimeTreeParser::Util::MultipartPlain); - otp.parseObjectTree(msg.data()); - testWriter.end(); - - QCOMPARE(otp.htmlContent().toLatin1().constData(), ""); - QCOMPARE(otp.htmlContentCharset().constData(), ""); - QCOMPARE(otp.plainTextContent().toLatin1().constData(), "Some HTML text"); - QCOMPARE(otp.plainTextContentCharset().constData(), "windows-1252"); -} - -void ObjectTreeParserTester::test_HTMLOnly() -{ - KMime::Message::Ptr msg = Test::readAndParseMail(QStringLiteral("htmlonly.mbox")); - - QCOMPARE(msg->subject()->as7BitString(false).constData(), "HTML test"); - QCOMPARE(msg->contents().size(), 0); - - Test::ObjectTreeSource emptySource(nullptr, nullptr); - ObjectTreeParser otp(&emptySource); - - otp.parseObjectTree(msg.data()); - - QVERIFY(otp.plainTextContent().isEmpty()); - QVERIFY(otp.htmlContent().contains(QLatin1String("SOME HTML text."))); -} void ObjectTreeParserTester::test_HTMLOnlyText() { diff --git a/mimetreeparser/autotests/basicobjecttreeparsertest.h b/mimetreeparser/autotests/basicobjecttreeparsertest.h --- a/mimetreeparser/autotests/basicobjecttreeparsertest.h +++ b/mimetreeparser/autotests/basicobjecttreeparsertest.h @@ -43,6 +43,14 @@ void testMemoryHole(); void testRenderedTree(); void testRenderedTree_data(); + void testParsePlainMessage(); + void testParseEncapsulatedMessage(); + void testMissingContentTypeHeader(); + void testInlinePGPDecryption(); + void testInlinePGPSigned(); + void testHTML(); + void testHTMLasText(); + void testHTMLOnly(); }; #endif // MIMETREEPARSER_TESTS_OBJECTTREEPARSERTEST_H diff --git a/mimetreeparser/autotests/basicobjecttreeparsertest.cpp b/mimetreeparser/autotests/basicobjecttreeparsertest.cpp --- a/mimetreeparser/autotests/basicobjecttreeparsertest.cpp +++ b/mimetreeparser/autotests/basicobjecttreeparsertest.cpp @@ -397,5 +397,199 @@ testSource.setDecryptMessage(true); otp.parseObjectTree(originalMessage.data()); + testMessagePartTree(otp.parsedPart(), mailFileName); } + +void ObjectTreeParserTest::testParsePlainMessage() +{ + KMime::Message::Ptr msg(new KMime::Message()); + QByteArray content( + "From: Thomas McGuire \n" + "Subject: Plain Message Test\n" + "Date: Wed, 5 Aug 2009 10:58:27 +0200\n" + "MIME-Version: 1.0\n" + "Content-Type: text/plain;\n" + " charset=\"iso-8859-15\"\n" + "\n" + "This is the message text.\n"); + msg->setContent(content); + msg->parse(); + + QCOMPARE(msg->subject()->as7BitString(false).constData(), "Plain Message Test"); + QCOMPARE(msg->contents().size(), 0); + + // Parse the message + SimpleObjectTreeSource testSource; + ObjectTreeParser otp(&testSource); + otp.parseObjectTree(msg.data()); + + // Check that the textual content and the charset have the expected values + QCOMPARE(otp.plainTextContent(), QStringLiteral("This is the message text.\n")); + QVERIFY(otp.htmlContent().isEmpty()); + QCOMPARE(otp.plainTextContentCharset().toLower(), QByteArray("iso-8859-15")); + + // Check that the message was not modified in any way + QCOMPARE(msg->encodedContent().constData(), content.constData()); + + // Test that the charset of messages without an explicit charset declaration + // is correct + content + = "From: Thomas McGuire \n" + "Subject: Plain Message Test\n" + "Date: Wed, 5 Aug 2009 10:58:27 +0200\n" + "MIME-Version: 1.0\n" + "Content-Type: text/plain;\n" + "\n" + "This is the message text.\n"; + msg->setContent(content); + msg->parse(); + ObjectTreeParser otp2(&testSource); + otp2.parseObjectTree(msg.data()); + QCOMPARE(otp2.plainTextContentCharset().constData(), msg->defaultCharset().constData()); +} + +void ObjectTreeParserTest::testParseEncapsulatedMessage() +{ + KMime::Message::Ptr msg + = readAndParseMail(QStringLiteral("encapsulated-with-attachment.mbox")); + QCOMPARE(msg->subject()->as7BitString(false).constData(), "Fwd: Test with attachment"); + QCOMPARE(msg->contents().size(), 2); + + // Parse the message + SimpleObjectTreeSource testSource; + NodeHelper nodeHelper; + ObjectTreeParser otp(&testSource, &nodeHelper); + otp.parseObjectTree(msg.data()); + + // Check that the OTP didn't modify the message in weird ways + QCOMPARE(msg->contents().size(), 2); + QCOMPARE(msg->contents().at(0)->contents().size(), 0); + QCOMPARE(msg->contents().at(1)->contents().size(), 1); + QCOMPARE(msg->contents().at(1)->contents().first()->contents().size(), 2); + QCOMPARE(msg->contents().at(1)->contents().first()->contents().at(0)->contents().size(), 0); + QCOMPARE(msg->contents().at(1)->contents().first()->contents().at(1)->contents().size(), 0); + + // Check that the textual content and the charset have the expected values + QCOMPARE(otp.plainTextContent(), QStringLiteral("This is the first encapsulating message.\n")); + QCOMPARE(otp.plainTextContentCharset().toLower(), QByteArray("iso-8859-15")); + QVERIFY(otp.htmlContent().isEmpty()); + + // Check that the objecttreeparser did process the encapsulated message + KMime::Message::Ptr encapsulated = msg->contents().at(1)->bodyAsMessage(); + QVERIFY(encapsulated.data()); + QVERIFY(nodeHelper.nodeProcessed(encapsulated.data())); + QVERIFY(nodeHelper.nodeProcessed(encapsulated->contents().at(0))); + QVERIFY(nodeHelper.nodeProcessed(encapsulated->contents().at(1))); + QVERIFY(nodeHelper.partMetaData(msg->contents().at(1)).isEncapsulatedRfc822Message); +} + +void ObjectTreeParserTest::testMissingContentTypeHeader() +{ + KMime::Message::Ptr msg = readAndParseMail(QStringLiteral("no-content-type.mbox")); + QCOMPARE(msg->subject()->as7BitString( + false).constData(), "Simple Mail Without Content-Type Header"); + QCOMPARE(msg->contents().size(), 0); + + NodeHelper nodeHelper; + SimpleObjectTreeSource testSource; + ObjectTreeParser otp(&testSource, &nodeHelper); + otp.parseObjectTree(msg.data()); + + QCOMPARE(otp.plainTextContent().toLatin1().data(), "asdfasdf"); + QVERIFY(otp.htmlContent().isEmpty()); +} + +void ObjectTreeParserTest::testInlinePGPDecryption() +{ + KMime::Message::Ptr msg = readAndParseMail(QStringLiteral("inlinepgpencrypted.mbox")); + + QCOMPARE(msg->subject()->as7BitString(false).constData(), "inlinepgpencrypted"); + QCOMPARE(msg->contents().size(), 0); + + NodeHelper nodeHelper; + SimpleObjectTreeSource testSource; + ObjectTreeParser otp(&testSource, &nodeHelper); + + testSource.setDecryptMessage(true); + otp.parseObjectTree(msg.data()); + + QCOMPARE(otp.plainTextContent().toLatin1().data(), "some random text"); + + // This test is only a workaround, till we can set the memento to the proper node of the mail. + KMime::Content *content = new KMime::Content; + QVERIFY(nodeHelper.bodyPartMemento(content, "decryptverify-attachment:?place=0")); + + QVERIFY(otp.htmlContent().isEmpty()); +} + +void ObjectTreeParserTest::testInlinePGPSigned() +{ + KMime::Message::Ptr msg = readAndParseMail(QStringLiteral("openpgp-inline-signed.mbox")); + + QCOMPARE(msg->subject()->as7BitString(false).constData(), "test"); + QCOMPARE(msg->contents().size(), 0); + + NodeHelper nodeHelper; + SimpleObjectTreeSource testSource; + ObjectTreeParser otp(&testSource, &nodeHelper); + + testSource.setDecryptMessage(true); + otp.parseObjectTree(msg.data()); + + // This test is only a workaround, till we can set the memento to the proper node of the mail. + QVERIFY(nodeHelper.bodyPartMemento(nullptr, "verification-attachment:?place=0")); +} + +void ObjectTreeParserTest::testHTML() +{ + KMime::Message::Ptr msg = readAndParseMail(QStringLiteral("html.mbox")); + + QCOMPARE(msg->subject()->as7BitString(false).constData(), "HTML test"); + QCOMPARE(msg->contents().size(), 2); + + SimpleObjectTreeSource testSource; + ObjectTreeParser otp(&testSource); + + otp.parseObjectTree(msg.data()); + + QCOMPARE(otp.plainTextContent().toLatin1().data(), "Some HTML text"); + QVERIFY(otp.htmlContent().contains(QLatin1String( + "Some HTML text"))); + QCOMPARE(otp.htmlContentCharset().data(), "windows-1252"); +} + +void ObjectTreeParserTest::testHTMLasText() +{ + KMime::Message::Ptr msg = readAndParseMail(QStringLiteral("html.mbox")); + + QCOMPARE(msg->subject()->as7BitString(false).constData(), "HTML test"); + QCOMPARE(msg->contents().size(), 2); + + SimpleObjectTreeSource testSource; + ObjectTreeParser otp(&testSource); + + testSource.setPreferredMode(MimeTreeParser::Util::MultipartPlain); + otp.parseObjectTree(msg.data()); + + QCOMPARE(otp.htmlContent().toLatin1().constData(), ""); + QCOMPARE(otp.htmlContentCharset().constData(), ""); + QCOMPARE(otp.plainTextContent().toLatin1().constData(), "Some HTML text"); + QCOMPARE(otp.plainTextContentCharset().constData(), "windows-1252"); +} + +void ObjectTreeParserTest::testHTMLOnly() +{ + KMime::Message::Ptr msg = readAndParseMail(QStringLiteral("htmlonly.mbox")); + + QCOMPARE(msg->subject()->as7BitString(false).constData(), "HTML test"); + QCOMPARE(msg->contents().size(), 0); + + SimpleObjectTreeSource testSource; + ObjectTreeParser otp(&testSource); + + otp.parseObjectTree(msg.data()); + + QVERIFY(otp.plainTextContent().isEmpty()); + QVERIFY(otp.htmlContent().contains(QLatin1String("SOME HTML text."))); +} diff --git a/mimetreeparser/src/simpleobjecttreesource.h b/mimetreeparser/src/simpleobjecttreesource.h --- a/mimetreeparser/src/simpleobjecttreesource.h +++ b/mimetreeparser/src/simpleobjecttreesource.h @@ -44,6 +44,8 @@ void setDecryptMessage(bool decryptMessage); void setHtmlMode(MimeTreeParser::Util::HtmlMode mode, const QList &availableModes) override; + + void setPreferredMode(MimeTreeParser::Util::HtmlMode mode); MimeTreeParser::Util::HtmlMode preferredMode() const override; const QTextCodec *overrideCodec() override; diff --git a/mimetreeparser/src/simpleobjecttreesource.cpp b/mimetreeparser/src/simpleobjecttreesource.cpp --- a/mimetreeparser/src/simpleobjecttreesource.cpp +++ b/mimetreeparser/src/simpleobjecttreesource.cpp @@ -29,6 +29,7 @@ { public: bool mDecryptMessage = false; + Util::HtmlMode mPreferredMode = Util::Html; }; } @@ -59,9 +60,15 @@ d->mDecryptMessage = decryptMessage; } +void SimpleObjectTreeSource::setPreferredMode(MimeTreeParser::Util::HtmlMode mode) +{ + d->mPreferredMode = mode; +} + + MimeTreeParser::Util::HtmlMode SimpleObjectTreeSource::preferredMode() const { - return Util::Html; + return d->mPreferredMode; } const QTextCodec *SimpleObjectTreeSource::overrideCodec()