File indexing completed on 2024-11-10 04:50:00
0001 /* 0002 * SPDX-FileCopyrightText: 2017 Daniel Vrátil <dvratil@kde.org> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-only 0005 */ 0006 0007 #include "filteractiondecrypttest.h" 0008 #include "../filteractions/filteractiondecrypt.h" 0009 0010 #include <QTest> 0011 0012 void FilterActionDecryptTest::initTestCase() 0013 { 0014 mGpg = new GPGHelper(QLatin1StringView(TEST_PATH) + QStringLiteral("/gpghome")); 0015 QVERIFY(mGpg->isValid()); 0016 } 0017 0018 void FilterActionDecryptTest::cleanupTestCase() 0019 { 0020 delete mGpg; 0021 } 0022 0023 void FilterActionDecryptTest::shouldDecrypt_data() 0024 { 0025 QTest::addColumn<QByteArray>("content"); 0026 QTest::addColumn<QByteArray>("encrypted"); 0027 0028 QDir testDir(QString::fromUtf8(TEST_PATH) + QStringLiteral("/gpgdata")); 0029 const auto tests = testDir.entryInfoList({QStringLiteral("*.msg")}, QDir::Files, QDir::Name); 0030 for (const auto &test : tests) { 0031 QFile plain(test.absoluteFilePath()); 0032 QVERIFY(plain.open(QIODevice::ReadOnly)); 0033 const auto plainData = plain.readAll(); 0034 0035 QFile pgp(test.absoluteFilePath() + QStringLiteral(".pgp")); 0036 QVERIFY(pgp.open(QIODevice::ReadOnly)); 0037 QTest::newRow(QStringLiteral("PGP %1").arg(test.baseName()).toUtf8().constData()) << plainData << pgp.readAll(); 0038 0039 QFile smime(test.absoluteFilePath() + QStringLiteral(".smime")); 0040 QVERIFY(smime.open(QIODevice::ReadOnly)); 0041 QTest::newRow(QStringLiteral("SMIME %1").arg(test.baseName()).toUtf8().constData()) << plainData << smime.readAll(); 0042 0043 QTest::newRow(QStringLiteral("PLAIN %1").arg(test.baseName()).toUtf8().constData()) << plainData << plainData; 0044 } 0045 } 0046 0047 void FilterActionDecryptTest::shouldDecrypt() 0048 { 0049 QFETCH(QByteArray, content); 0050 QFETCH(QByteArray, encrypted); 0051 0052 MailCommon::FilterActionDecrypt action(this); 0053 0054 auto msg = KMime::Message::Ptr::create(); 0055 msg->setContent(encrypted); 0056 msg->parse(); 0057 msg->assemble(); 0058 0059 Akonadi::Item item; 0060 item.setPayload(msg); 0061 0062 MailCommon::ItemContext context(item, true); 0063 const auto result = action.process(context, false); 0064 QCOMPARE(result, MailCommon::FilterAction::GoOn); 0065 if (content != encrypted) { 0066 QVERIFY(context.needsPayloadStore()); 0067 } else { 0068 // the message is not encrypted, no change is needed 0069 QVERIFY(!context.needsPayloadStore()); 0070 } 0071 0072 auto newMsg = context.item().payload<KMime::Message::Ptr>(); 0073 QCOMPARE(newMsg->from()->asUnicodeString(), msg->from()->asUnicodeString()); 0074 QCOMPARE(newMsg->to()->asUnicodeString(), msg->to()->asUnicodeString()); 0075 QCOMPARE(newMsg->date()->asUnicodeString(), msg->date()->asUnicodeString()); 0076 QCOMPARE(newMsg->subject()->asUnicodeString(), msg->subject()->asUnicodeString()); 0077 0078 auto decrypted = newMsg->encodedContent(); 0079 KMime::Message decryptedContent; 0080 decryptedContent.setContent(decrypted); 0081 decryptedContent.parse(); 0082 KMime::Message expectedContent; 0083 expectedContent.setContent(content); 0084 expectedContent.parse(); 0085 QCOMPARE(decryptedContent.from()->asUnicodeString(), expectedContent.from()->asUnicodeString()); 0086 QCOMPARE(decryptedContent.to()->asUnicodeString(), expectedContent.to()->asUnicodeString()); 0087 QCOMPARE(decryptedContent.date()->asUnicodeString(), expectedContent.date()->asUnicodeString()); 0088 QCOMPARE(decryptedContent.subject()->asUnicodeString(), expectedContent.subject()->asUnicodeString()); 0089 QCOMPARE(decryptedContent.contentType()->asUnicodeString(), expectedContent.contentType()->asUnicodeString()); 0090 QCOMPARE(decryptedContent.encodedBody(), expectedContent.encodedBody()); 0091 } 0092 0093 QTEST_MAIN(FilterActionDecryptTest) 0094 0095 #include "moc_filteractiondecrypttest.cpp"