File indexing completed on 2025-03-09 04:54:26

0001 /*
0002    SPDX-FileCopyrightText: 2018-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "dkimauthenticationstatusinfotest.h"
0008 #include "dkim-verify/dkimauthenticationstatusinfo.h"
0009 #include <QTest>
0010 
0011 QTEST_GUILESS_MAIN(DKIMAuthenticationStatusInfoTest)
0012 
0013 DKIMAuthenticationStatusInfoTest::DKIMAuthenticationStatusInfoTest(QObject *parent)
0014     : QObject(parent)
0015 {
0016 }
0017 
0018 void DKIMAuthenticationStatusInfoTest::shouldHaveDefaultValue()
0019 {
0020     MessageViewer::DKIMAuthenticationStatusInfo info;
0021     QVERIFY(info.authservId().isEmpty());
0022     QCOMPARE(info.authVersion(), -1);
0023     QVERIFY(info.reasonSpec().isEmpty());
0024     QVERIFY(info.listAuthStatusInfo().isEmpty());
0025 }
0026 
0027 void DKIMAuthenticationStatusInfoTest::shouldParseKey()
0028 {
0029     QFETCH(QString, key);
0030     QFETCH(MessageViewer::DKIMAuthenticationStatusInfo, result);
0031     QFETCH(bool, relaxingParsing);
0032     QFETCH(bool, success);
0033     QBENCHMARK {
0034         MessageViewer::DKIMAuthenticationStatusInfo info;
0035         const bool val = info.parseAuthenticationStatus(key, relaxingParsing);
0036         QCOMPARE(val, success);
0037         const bool compareResult = result == info;
0038         if (!compareResult) {
0039             qDebug() << "parse info: " << info;
0040             qDebug() << "expected: " << result;
0041         }
0042         QVERIFY(compareResult);
0043     }
0044     //    Before
0045     //        PASS   : DKIMAuthenticationStatusInfoTest::initTestCase()
0046     //        PASS   : DKIMAuthenticationStatusInfoTest::shouldHaveDefaultValue()
0047     //        PASS   : DKIMAuthenticationStatusInfoTest::shouldParseKey(empty)
0048     //        RESULT : DKIMAuthenticationStatusInfoTest::shouldParseKey():"empty":
0049     //             0.81 msecs per iteration (total: 52, iterations: 64)
0050     //        PASS   : DKIMAuthenticationStatusInfoTest::shouldParseKey(test1)
0051     //        RESULT : DKIMAuthenticationStatusInfoTest::shouldParseKey():"test1":
0052     //             18 msecs per iteration (total: 72, iterations: 4)
0053     //        PASS   : DKIMAuthenticationStatusInfoTest::shouldParseKey(none)
0054     //        RESULT : DKIMAuthenticationStatusInfoTest::shouldParseKey():"none":
0055     //             2.0 msecs per iteration (total: 65, iterations: 32)
0056     //        PASS   : DKIMAuthenticationStatusInfoTest::shouldParseKey(none2)
0057     //        RESULT : DKIMAuthenticationStatusInfoTest::shouldParseKey():"none2":
0058     //             2.0 msecs per iteration (total: 66, iterations: 32)
0059     //        PASS   : DKIMAuthenticationStatusInfoTest::shouldParseKey(reason)
0060     //        RESULT : DKIMAuthenticationStatusInfoTest::shouldParseKey():"reason":
0061     //             13 msecs per iteration (total: 52, iterations: 4)
0062     //        PASS   : DKIMAuthenticationStatusInfoTest::shouldParseKey(reason2)
0063     //        RESULT : DKIMAuthenticationStatusInfoTest::shouldParseKey():"reason2":
0064     //             12 msecs per iteration (total: 97, iterations: 8)
0065     //        PASS   : DKIMAuthenticationStatusInfoTest::shouldParseKey(gmails)
0066     //        RESULT : DKIMAuthenticationStatusInfoTest::shouldParseKey():"gmails":
0067     //             7.0 msecs per iteration (total: 56, iterations: 8)
0068     //        PASS   : DKIMAuthenticationStatusInfoTest::cleanupTestCase()
0069 
0070     //    AFTER
0071     // RESULT : DKIMAuthenticationStatusInfoTest::shouldParseKey():"empty":
0072     //     0.0089 msecs per iteration (total: 73, iterations: 8192)
0073     // PASS   : DKIMAuthenticationStatusInfoTest::shouldParseKey(test1)
0074     // RESULT : DKIMAuthenticationStatusInfoTest::shouldParseKey():"test1":
0075     //     0.23 msecs per iteration (total: 59, iterations: 256)
0076     // PASS   : DKIMAuthenticationStatusInfoTest::shouldParseKey(none)
0077     // RESULT : DKIMAuthenticationStatusInfoTest::shouldParseKey():"none":
0078     //     0.023 msecs per iteration (total: 96, iterations: 4096)
0079     // PASS   : DKIMAuthenticationStatusInfoTest::shouldParseKey(none2)
0080     // RESULT : DKIMAuthenticationStatusInfoTest::shouldParseKey():"none2":
0081     //     0.023 msecs per iteration (total: 96, iterations: 4096)
0082     // PASS   : DKIMAuthenticationStatusInfoTest::shouldParseKey(reason)
0083     // RESULT : DKIMAuthenticationStatusInfoTest::shouldParseKey():"reason":
0084     //     0.16 msecs per iteration (total: 83, iterations: 512)
0085     // PASS   : DKIMAuthenticationStatusInfoTest::shouldParseKey(reason2)
0086     // RESULT : DKIMAuthenticationStatusInfoTest::shouldParseKey():"reason2":
0087     //     0.14 msecs per iteration (total: 75, iterations: 512)
0088     // PASS   : DKIMAuthenticationStatusInfoTest::shouldParseKey(gmails)
0089     // RESULT : DKIMAuthenticationStatusInfoTest::shouldParseKey():"gmails":
0090     //     0.085 msecs per iteration (total: 88, iterations: 1024)
0091     // PASS   : DKIMAuthenticationStatusInfoTest::cleanupTestCase()
0092 }
0093 
0094 void DKIMAuthenticationStatusInfoTest::shouldParseKey_data()
0095 {
0096     QTest::addColumn<QString>("key");
0097     QTest::addColumn<MessageViewer::DKIMAuthenticationStatusInfo>("result");
0098     QTest::addColumn<bool>("relaxingParsing");
0099     QTest::addColumn<bool>("success");
0100 
0101     QTest::addRow("empty") << QString() << MessageViewer::DKIMAuthenticationStatusInfo() << false << false;
0102     {
0103         MessageViewer::DKIMAuthenticationStatusInfo info;
0104         info.setAuthVersion(1);
0105         info.setAuthservId(QStringLiteral("in68.mail.ovh.net"));
0106         QList<MessageViewer::DKIMAuthenticationStatusInfo::AuthStatusInfo> lst;
0107         MessageViewer::DKIMAuthenticationStatusInfo::AuthStatusInfo property;
0108         property.method = QStringLiteral("dkim");
0109         property.result = QStringLiteral("pass");
0110         property.methodVersion = 1;
0111         {
0112             MessageViewer::DKIMAuthenticationStatusInfo::AuthStatusInfo::Property prop;
0113             prop.type = QStringLiteral("d");
0114             prop.value = QStringLiteral("kde.org");
0115             property.header.append(prop);
0116         }
0117         {
0118             MessageViewer::DKIMAuthenticationStatusInfo::AuthStatusInfo::Property prop;
0119             prop.type = QStringLiteral("i");
0120             prop.value = QStringLiteral("@kde.org");
0121             property.header.append(prop);
0122         }
0123         {
0124             MessageViewer::DKIMAuthenticationStatusInfo::AuthStatusInfo::Property prop;
0125             prop.type = QStringLiteral("b");
0126             prop.value = QStringLiteral("\"GMG2ucPx\"");
0127             property.header.append(prop);
0128         }
0129         lst.append(property);
0130 
0131         MessageViewer::DKIMAuthenticationStatusInfo::AuthStatusInfo property2;
0132         property2.method = QStringLiteral("dkim");
0133         property2.result = QStringLiteral("pass");
0134         property2.methodVersion = 1;
0135 
0136         {
0137             MessageViewer::DKIMAuthenticationStatusInfo::AuthStatusInfo::Property prop;
0138             prop.type = QStringLiteral("d");
0139             prop.value = QStringLiteral("kde.org");
0140             property2.header.append(prop);
0141         }
0142         {
0143             MessageViewer::DKIMAuthenticationStatusInfo::AuthStatusInfo::Property prop;
0144             prop.type = QStringLiteral("i");
0145             prop.value = QStringLiteral("@kde.org");
0146             property2.header.append(prop);
0147         }
0148         {
0149             MessageViewer::DKIMAuthenticationStatusInfo::AuthStatusInfo::Property prop;
0150             prop.type = QStringLiteral("b");
0151             prop.value = QStringLiteral("\"I3t3p7Up\"");
0152             property2.header.append(prop);
0153         }
0154         lst.append(property2);
0155 
0156         MessageViewer::DKIMAuthenticationStatusInfo::AuthStatusInfo property3;
0157         property3.method = QStringLiteral("dkim-atps");
0158         property3.result = QStringLiteral("neutral");
0159         property3.methodVersion = 1;
0160         lst.append(property3);
0161 
0162         info.setListAuthStatusInfo(lst);
0163         QTest::addRow("test1") << QStringLiteral(
0164             "in68.mail.ovh.net; dkim=pass (2048-bit key; unprotected) header.d=kde.org header.i=@kde.org header.b=\"GMG2ucPx\"; dkim=pass (2048-bit key; "
0165             "unprotected) header.d=kde.org header.i=@kde.org header.b=\"I3t3p7Up\"; dkim-atps=neutral")
0166                                << info << false << true;
0167     }
0168     {
0169         MessageViewer::DKIMAuthenticationStatusInfo info;
0170         info.setAuthVersion(1);
0171         info.setAuthservId(QStringLiteral("example.org"));
0172 
0173         QTest::addRow("none") << QStringLiteral("example.org 1; none;") << info << false << false;
0174     }
0175     {
0176         MessageViewer::DKIMAuthenticationStatusInfo info;
0177         info.setAuthVersion(1);
0178         info.setAuthservId(QStringLiteral("example.org"));
0179 
0180         QTest::addRow("none2") << QStringLiteral("example.org 1; none") << info << false << false;
0181     }
0182     {
0183         MessageViewer::DKIMAuthenticationStatusInfo info;
0184         info.setAuthVersion(1);
0185         info.setAuthservId(QStringLiteral("example.com"));
0186 
0187         QList<MessageViewer::DKIMAuthenticationStatusInfo::AuthStatusInfo> lst;
0188         {
0189             MessageViewer::DKIMAuthenticationStatusInfo::AuthStatusInfo property;
0190             property.method = QStringLiteral("dkim");
0191             property.result = QStringLiteral("pass");
0192             property.reason = QStringLiteral("good signature");
0193             property.methodVersion = 1;
0194             {
0195                 MessageViewer::DKIMAuthenticationStatusInfo::AuthStatusInfo::Property prop;
0196                 prop.type = QStringLiteral("i");
0197                 prop.value = QStringLiteral("@mail-router.example.net");
0198                 property.header.append(prop);
0199             }
0200             lst.append(property);
0201         }
0202         {
0203             MessageViewer::DKIMAuthenticationStatusInfo::AuthStatusInfo property2;
0204             property2.method = QStringLiteral("dkim");
0205             property2.result = QStringLiteral("fail");
0206             property2.reason = QStringLiteral("bad signature");
0207             property2.methodVersion = 1;
0208 
0209             {
0210                 MessageViewer::DKIMAuthenticationStatusInfo::AuthStatusInfo::Property prop;
0211                 prop.type = QStringLiteral("i");
0212                 prop.value = QStringLiteral("@newyork.example.com");
0213                 property2.header.append(prop);
0214             }
0215             lst.append(property2);
0216         }
0217         info.setListAuthStatusInfo(lst);
0218 
0219         QTest::addRow("reason") << QStringLiteral(
0220             "example.com; dkim=pass reason=\"good signature\" header.i=@mail-router.example.net; dkim=fail reason=\"bad signature\" "
0221             "header.i=@newyork.example.com;")
0222                                 << info << false << true;
0223     }
0224 
0225     // It will failed. Fix it
0226     {
0227         MessageViewer::DKIMAuthenticationStatusInfo info;
0228         info.setAuthVersion(1);
0229         info.setAuthservId(QStringLiteral("example.com"));
0230 
0231         QList<MessageViewer::DKIMAuthenticationStatusInfo::AuthStatusInfo> lst;
0232         {
0233             MessageViewer::DKIMAuthenticationStatusInfo::AuthStatusInfo property;
0234             property.method = QStringLiteral("dkim");
0235             property.result = QStringLiteral("pass");
0236             property.reason = QStringLiteral("good signature");
0237             property.methodVersion = 1;
0238             {
0239                 MessageViewer::DKIMAuthenticationStatusInfo::AuthStatusInfo::Property prop;
0240                 prop.type = QStringLiteral("i");
0241                 prop.value = QStringLiteral("@mail-router.example.net");
0242                 property.header.append(prop);
0243             }
0244             lst.append(property);
0245         }
0246         {
0247             MessageViewer::DKIMAuthenticationStatusInfo::AuthStatusInfo property2;
0248             property2.method = QStringLiteral("dkim");
0249             property2.result = QStringLiteral("fail");
0250             property2.reason = QStringLiteral("bad signature");
0251             property2.methodVersion = 1;
0252 
0253             {
0254                 MessageViewer::DKIMAuthenticationStatusInfo::AuthStatusInfo::Property prop;
0255                 prop.type = QStringLiteral("i");
0256                 prop.value = QStringLiteral("@newyork.example.com");
0257                 property2.header.append(prop);
0258             }
0259             lst.append(property2);
0260         }
0261         info.setListAuthStatusInfo(lst);
0262 
0263         QTest::addRow("reason2") << QStringLiteral(
0264             "example.com; dkim=pass reason=\"good signature\" header.i=@mail-router.example.net; dkim=fail reason=\"bad signature\" "
0265             "header.i=@newyork.example.com")
0266                                  << info << true << true;
0267     }
0268     {
0269         MessageViewer::DKIMAuthenticationStatusInfo info;
0270         info.setAuthVersion(1);
0271         info.setAuthservId(QStringLiteral("letterbox.kde.org"));
0272         QList<MessageViewer::DKIMAuthenticationStatusInfo::AuthStatusInfo> lst;
0273         {
0274             MessageViewer::DKIMAuthenticationStatusInfo::AuthStatusInfo property;
0275             property.method = QStringLiteral("dmarc");
0276             property.result = QStringLiteral("pass");
0277             property.methodVersion = 1;
0278             {
0279                 MessageViewer::DKIMAuthenticationStatusInfo::AuthStatusInfo::Property prop;
0280                 prop.type = QStringLiteral("from");
0281                 prop.value = QStringLiteral("gmail.com");
0282                 property.header.append(prop);
0283             }
0284             lst.append(property);
0285         }
0286         info.setListAuthStatusInfo(lst);
0287 
0288         QTest::addRow("gmails") << QStringLiteral("letterbox.kde.org; dmarc=pass (p=none dis=none) header.from=gmail.com\r\n") << info << true << true;
0289     }
0290 }
0291 
0292 #include "moc_dkimauthenticationstatusinfotest.cpp"