File indexing completed on 2024-06-16 04:59:05

0001 /*
0002   SPDX-FileCopyrightText: 2017 Daniel Vrátil <dvratil@kde.org>
0003 
0004   SPDX-License-Identifier: GPL-2.0-only
0005 */
0006 
0007 #include "searchruleencryptiontest.h"
0008 #include "searchrule/searchruleencryption.h"
0009 
0010 #include <QTest>
0011 
0012 #include <Akonadi/Item>
0013 #include <KMime/Message>
0014 
0015 Q_DECLARE_METATYPE(MailCommon::SearchRule::Function)
0016 
0017 using namespace MailCommon;
0018 
0019 void SearchRuleEncryptionTest::shouldNotBeEmpty()
0020 {
0021     SearchRuleEncryption rule;
0022     QCOMPARE(rule.isEmpty(), false);
0023 }
0024 
0025 void SearchRuleEncryptionTest::shouldRequiresPart()
0026 {
0027     SearchRuleEncryption rule;
0028     QCOMPARE(rule.requiredPart(), SearchRule::CompleteMessage);
0029 }
0030 
0031 void SearchRuleEncryptionTest::shouldMatchMessage_data()
0032 {
0033     QTest::addColumn<QByteArray>("message");
0034     QTest::addColumn<SearchRule::Function>("function");
0035     QTest::addColumn<bool>("shouldMatch");
0036 
0037     QByteArray msg;
0038 
0039     msg =
0040         "From: Sender <sender@example.com>\n"
0041         "To: Recipient <recipient@example.com>\n"
0042         "Subject: Test Message\n"
0043         "Date: Mon, 18 Sep 2017 16:33:57 +0200\n"
0044         "Content-Type: text/plain; charset=\"us-ascii\"\n"
0045         "\n"
0046         "So long and thanks for all the fish.\n";
0047     QTest::newRow("text/plain equals") << msg << SearchRule::FuncEquals << false;
0048     QTest::newRow("text/plain notequals") << msg << SearchRule::FuncNotEqual << true;
0049 
0050     msg =
0051         "From: Sender <sender@example.com>\n"
0052         "To: Recipient <recipient@example.com>\n"
0053         "Subject: Test Message\n"
0054         "Date: Mon, 18 Sep 2017 16:36:55 +0200\n"
0055         "Content-Type: multipart/alternative; boundary=\"nextPart4248936.HWsL1qCvO2\"\n"
0056         "\n"
0057         "This is a multi-part message in MIME format.\n"
0058         "\n"
0059         "--nextPart4248936.HWsL1qCvO2\n"
0060         "Content-Type: text/plain; charset=\"us-ascii\"\n"
0061         "\n"
0062         "/So long and thanks for all the fish/\n"
0063         "\n"
0064         "--nextPart4248936.HWsL1qCvO2\n"
0065         "Content-Type: text/html; charset=\"us-ascii\"\n"
0066         "\n"
0067         "<!DOCTYPE HTML>\n"
0068         "<html><head><p>So long and thanks for all the fish</p></body></html>\n"
0069         "--nextPart4248936.HWsL1qCvO2--\n";
0070     QTest::newRow("multipart equals") << msg << SearchRule::FuncEquals << false;
0071     QTest::newRow("multipart notequals") << msg << SearchRule::FuncNotEqual << true;
0072 
0073     msg =
0074         "From: Sender <sender@example.com>\n"
0075         "To: Recipient <recipient@example.com>\n"
0076         "Subject: Test Message\n"
0077         "Date: Mon, 18 Sep 2017 16:40:40 +0200\n"
0078         "Content-Type: multipart/encrypted; boundary=\"nextPart1821584.7Og3uilSh7\"; protocol=\"application/pgp-encrypted\"\n"
0079         "\n"
0080         "--nextPart1821584.7Og3uilSh7\n"
0081         "Content-Type: application/pgp-encrypted\n"
0082         "Content-Disposition: attachment\n"
0083         "\n"
0084         "Version: 1\n"
0085         "--nextPart1821584.7Og3uilSh7\n"
0086         "Content-Type: application/octet-stream\n"
0087         "Content-Disposition: inline; filename=\"msg.asc\"\n"
0088         "Content-Transfer-Encoding: 7Bit\n"
0089         "\n"
0090         "-----BEGIN PGP MESSAGE-----\n"
0091         "\n"
0092         "hQEMA923xmsQX9HEAQf9HoQNoqpAcOjPyR2ggO05o33FX3VQueGK6lTgvBYeyshK\n"
0093         "fLOaWvjtQ2ftoH3nd4mBBrKwaYHTrfPL/9ZgDJ8j8YXJtD68wOW150iXyCUBQasO\n"
0094         "9nQ59wd61JPECO6MoHItzJgpkeE+dujK5ULQJYiFinVn5HZEqEizOEqTlFJUN86d\n"
0095         " we will not look into the body, so we don't need the full body \n"
0096         "hJKWywAfRqRJBq/fQmoXMQ+IvsBQx7KZ92eO2NvkGimEzHX3Uiwi6kuORFiLrFqJ\n"
0097         "Uc0mhna4cBU4bt3Zg2i0jQ4AJUZiXKg=\n"
0098         "=96px\n"
0099         "-----END PGP MESSAGE-----\n"
0100         "\n"
0101         "--nextPart1821584.7Og3uilSh7--\n";
0102     QTest::newRow("gpg equals") << msg << SearchRule::FuncEquals << true;
0103     QTest::newRow("gpg notequals") << msg << SearchRule::FuncNotEqual << false;
0104 
0105     msg =
0106         "From: Sender <sender@example.com>\n"
0107         "To: Recipient <recipient@example.com>\n"
0108         "Subject: Test Message\n"
0109         "Date: Mon, 18 Sep 2017 16:40:40 +0200\n"
0110         "Content-Type: text/plain; charset=\"us-ascii\"\n"
0111         "\n"
0112         "-----BEGIN PGP MESSAGE-----\n"
0113         "\n"
0114         "hQEMA923xmsQX9HEAQf/Zu+L2EqkoAhacPlns466Chw4Re5q+zPQgVYhmXW4KkQ3\n"
0115         "bVzkXJ0ZiWamfimc/JBjU3gfXcCBPgnwdPJXxS9U8mIrgzLRIc4xGLL4OKidZibC\n"
0116         "HzmfC1ScJjWz2UE4KkVRSfEeCzVn+7S3V4DrPYjN3XGR4hGpV+gDDfqrw563KqVZ\n"
0117         " we will not look into the body, so we don't need the full body \n"
0118         "URsJzzMVfl2CacwT2GWuthv6g0idzNardrircmefgT2ACMvxGSbTmfw9T5McuiRu\n"
0119         "6nJFyi9rE948y8B5IjdXazaOyf8=\n"
0120         "=KRPN\n"
0121         "-----END PGP MESSAGE-----\n";
0122     QTest::newRow("gpg-inline equals") << msg << SearchRule::FuncEquals << true;
0123     QTest::newRow("gpg-inline notequals") << msg << SearchRule::FuncNotEqual << false;
0124 
0125     msg =
0126         "From: Sender <sender@example.com>\n"
0127         "To: Recipient <recipient@example.com>\n"
0128         "Subject: Test message\n"
0129         "Date: Mon, 18 Sep 2017 16:50:22 +0200\n"
0130         "Content-Type: application/pkcs7-mime; name=\"smime.p7m\"; smime-type=\"enveloped-data\"\n"
0131         "Content-Transfer-Encoding: base64\n"
0132         "Content-Disposition: attachment; filename=\"smime.p7m\"\n"
0133         "\n"
0134         "MIAGCSqGSIb3DQEHA6CAMIACAQAxggHNMIIByQIBADCBsDCBmzELMAkGA1UEBhMCR0IxGzAZBgNV\n"
0135         "BAgTEkdyZWF0ZXIgTWFuY2hlc3RlcjEQMA4GA1UEBxMHU2FsZm9yZDEaMBgGA1UEChMRQ09NT0RP\n"
0136         "a57rVj7FnjHhlBgpsYGpv8cDu2f60JLHWcKJwaJHnqVABWVfE7Dp5LHP1U96tdJjiZ1EsPiWalT4\n"
0137         "Tto8U1MEsyZGJzm0Xfs8oKyZx63mp0WfRLXdfo+PFLF0MaFcd4ITo/KstX2VY8FNtVjqAZ/aE5+f\n"
0138         "...... we will not look into the body, so we don't need the full body ......\n"
0139         "ulxttPf2lx34UjZPPTmhBJT91tPlbN3oiezl5dYwgAYJKoZIhvcNAQcBMB0GCWCGSAFlAwQBAgQQ\n"
0140         "4CBkVjsRMzvAVfnOFJm9KP0ex3pK/t1zotsVUaGXm28OTIL6xRJp1TLdhy6HFIiMGmyKPSMDq//F\n"
0141         "sonU51JpPcqs1G/XFUTgrPZ4R5zBQyclGwQsY3YQR+ik2iBkk4rjiJbI1chVp9kLEY1YxfABY/vo\n"
0142         "LVURWHOzXAlxzbVz8hY0TJ9Nr2wyrkUR52UhBBBvxNjI3yM2XVxUQflmayVkAAAAAAAAAAAAAA==\n";
0143     QTest::newRow("smime equals") << msg << SearchRule::FuncEquals << true;
0144     QTest::newRow("smime notequals") << msg << SearchRule::FuncNotEqual << false;
0145 }
0146 
0147 void SearchRuleEncryptionTest::shouldMatchMessage()
0148 {
0149     QFETCH(QByteArray, message);
0150     QFETCH(SearchRule::Function, function);
0151     QFETCH(bool, shouldMatch);
0152 
0153     auto msg = KMime::Message::Ptr::create();
0154     msg->setContent(message);
0155     msg->parse();
0156 
0157     Akonadi::Item item(KMime::Message::mimeType());
0158     item.setPayload(msg);
0159 
0160     SearchRuleEncryption rule({}, function, {});
0161     QCOMPARE(rule.matches(item), shouldMatch);
0162 }
0163 
0164 QTEST_MAIN(SearchRuleEncryptionTest)
0165 
0166 #include "moc_searchruleencryptiontest.cpp"