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 "filteractionencrypttest.h" 0008 #include "../filteractions/filteractionencrypt.h" 0009 0010 #include <QTest> 0011 0012 void FilterActionEncryptTest::initTestCase() 0013 { 0014 mGpg = new GPGHelper(QStringLiteral(TEST_PATH) + QStringLiteral("/gpghome")); 0015 QVERIFY(mGpg->isValid()); 0016 } 0017 0018 void FilterActionEncryptTest::cleanupTestCase() 0019 { 0020 delete mGpg; 0021 } 0022 0023 void FilterActionEncryptTest::shouldEncrypt_data() 0024 { 0025 QTest::addColumn<QString>("key"); 0026 QTest::addColumn<QByteArray>("content"); // content for decryption 0027 QTest::addColumn<QByteArray>("expected"); // decrypted expected content 0028 0029 const auto smimeKey = QStringLiteral("SMIME:%1:0FDD972BCEFB5735DC7E8EE57DB7BA4E5FDBE218"); 0030 const auto pgpKey = QStringLiteral("PGP:%1:818AE8DA30F81B0CEA4403BA358732559B8659B2"); 0031 0032 QDir testDir(QStringLiteral(TEST_PATH) + QStringLiteral("/gpgdata")); 0033 const auto tests = testDir.entryInfoList({QStringLiteral("*.msg")}, QDir::Files, QDir::Name); 0034 for (const auto &test : tests) { 0035 QFile plain(test.absoluteFilePath()); 0036 QVERIFY(plain.open(QIODevice::ReadOnly)); 0037 const auto plainData = plain.readAll(); 0038 0039 QTest::newRow(QStringLiteral("PGP %1").arg(test.baseName()).toUtf8().constData()) << pgpKey.arg(0) << plainData << plainData; 0040 QTest::newRow(QStringLiteral("SMIME %1").arg(test.baseName()).toUtf8().constData()) << smimeKey.arg(0) << plainData << plainData; 0041 0042 QFile smimeFile(test.absoluteFilePath() + QStringLiteral(".smime")); 0043 QVERIFY(smimeFile.open(QIODevice::ReadOnly)); 0044 const auto smimeData = smimeFile.readAll(); 0045 QFile pgpFile(test.absoluteFilePath() + QStringLiteral(".pgp")); 0046 QVERIFY(pgpFile.open(QIODevice::ReadOnly)); 0047 const auto pgpData = pgpFile.readAll(); 0048 0049 QTest::newRow(QStringLiteral("PGP %1 re-encrypt").arg(test.baseName()).toUtf8().constData()) << pgpKey.arg(1) << smimeData << plainData; 0050 QTest::newRow(QStringLiteral("SMIME %1 re-encrypt").arg(test.baseName()).toUtf8().constData()) << smimeKey.arg(1) << pgpData << plainData; 0051 QTest::newRow(QStringLiteral("PGP %1 re-encrypt same key").arg(test.baseName()).toUtf8().constData()) << pgpKey.arg(1) << pgpData << plainData; 0052 } 0053 } 0054 0055 void FilterActionEncryptTest::shouldEncrypt() 0056 { 0057 QFETCH(QString, key); 0058 QFETCH(QByteArray, content); 0059 QFETCH(QByteArray, expected); 0060 0061 MailCommon::FilterActionEncrypt action(this); 0062 action.argsFromString(key); 0063 QVERIFY(!action.key().isNull()); 0064 QCOMPARE(action.reencrypt(), key.contains(QLatin1StringView(":1:"))); 0065 0066 auto msg = KMime::Message::Ptr::create(); 0067 msg->setContent(content); 0068 msg->parse(); 0069 msg->assemble(); 0070 0071 Akonadi::Item item; 0072 item.setPayload(msg); 0073 0074 MailCommon::ItemContext context(item, true); 0075 const auto result = action.process(context, false); 0076 QCOMPARE(result, MailCommon::FilterAction::GoOn); 0077 QVERIFY(context.needsPayloadStore()); 0078 0079 auto newMsg = context.item().payload<KMime::Message::Ptr>(); 0080 QCOMPARE(newMsg->from()->asUnicodeString(), msg->from()->asUnicodeString()); 0081 QCOMPARE(newMsg->to()->asUnicodeString(), msg->to()->asUnicodeString()); 0082 QCOMPARE(newMsg->date()->asUnicodeString(), msg->date()->asUnicodeString()); 0083 QCOMPARE(newMsg->subject()->asUnicodeString(), msg->subject()->asUnicodeString()); 0084 0085 QByteArray resultContent; 0086 GPGHelper::CryptoType crypto; 0087 if (key.startsWith(QLatin1StringView("PGP"))) { 0088 QCOMPARE(newMsg->contentType()->mimeType(), QByteArray("multipart/encrypted")); 0089 resultContent = newMsg->encodedContent(); 0090 crypto = GPGHelper::OpenPGP; 0091 } else { 0092 QCOMPARE(newMsg->contentType()->mimeType(), QByteArray("application/pkcs7-mime")); 0093 resultContent = QByteArray::fromBase64(newMsg->encodedBody()); 0094 crypto = GPGHelper::SMIME; 0095 } 0096 0097 // Check if the message is encrypted with the right key 0098 const auto usedKey = mGpg->encryptionKeyFp(resultContent, crypto); 0099 QCOMPARE(usedKey, QString::fromLatin1(action.key().primaryFingerprint())); 0100 0101 const auto actual = mGpg->decrypt(resultContent, crypto); 0102 0103 KMime::Message actualContent; 0104 actualContent.setContent(actual); 0105 actualContent.parse(); 0106 KMime::Message expectedContent; 0107 expectedContent.setContent(expected); 0108 expectedContent.parse(); 0109 QCOMPARE(actualContent.from()->asUnicodeString(), expectedContent.from()->asUnicodeString()); 0110 QCOMPARE(actualContent.to()->asUnicodeString(), expectedContent.to()->asUnicodeString()); 0111 QCOMPARE(actualContent.date()->asUnicodeString(), expectedContent.date()->asUnicodeString()); 0112 QCOMPARE(actualContent.subject()->asUnicodeString(), expectedContent.subject()->asUnicodeString()); 0113 QCOMPARE(actualContent.contentType()->asUnicodeString(), expectedContent.contentType()->asUnicodeString()); 0114 QCOMPARE(actualContent.encodedBody(), expectedContent.encodedBody()); 0115 } 0116 0117 QTEST_MAIN(FilterActionEncryptTest) 0118 0119 #include "moc_filteractionencrypttest.cpp"