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

0001 /*
0002    SPDX-FileCopyrightText: 2019-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "dkimutiltest.h"
0008 #include "dkim-verify/dkimutil.h"
0009 #include <QTest>
0010 
0011 QTEST_GUILESS_MAIN(DKIMUtilTest)
0012 DKIMUtilTest::DKIMUtilTest(QObject *parent)
0013     : QObject(parent)
0014 {
0015 }
0016 
0017 void DKIMUtilTest::shouldTestBodyCanonizationRelaxed()
0018 {
0019     QBENCHMARK {
0020         {
0021             QString ba = QStringLiteral(
0022                 "-- \nLaurent Montel | laurent.montel@kdab.com | KDE/Qt Senior Software Engineer \nKDAB (France) S.A.S., a KDAB Group company\nTel: France +33 "
0023                 "(0)4 90 84 08 53, http://www.kdab.fr\nKDAB - The Qt, C++ and OpenGL Experts\n\n\n");
0024             QString result = MessageViewer::DKIMUtil::bodyCanonizationRelaxed(ba);
0025 
0026             QCOMPARE(MessageViewer::DKIMUtil::generateHash(result.toUtf8(), QCryptographicHash::Sha256), "jnEyWN7LwPIBgES0mElYDek3lmyrRtSwUjDR2Ge08Xw=");
0027         }
0028         {
0029             QString ba = QStringLiteral("Bla bla\n\nbli\t\tblo\nTest\n\n\n\n\n");
0030             QString result = MessageViewer::DKIMUtil::bodyCanonizationRelaxed(ba);
0031 
0032             QCOMPARE(MessageViewer::DKIMUtil::generateHash(result.toUtf8(), QCryptographicHash::Sha256), "DrwZwEC82qsIhJtHlq76T00vAUcrSrHbJh8wY5GTAws=");
0033         }
0034     }
0035     //    BEFORE
0036     //    RESULT : DKIMUtilTest::shouldTestBodyCanonizationRelaxed():
0037     //      0.087 msecs per iteration (total: 90, iterations: 1024)
0038 
0039     //    AFTER
0040     //            RESULT : DKIMUtilTest::shouldTestBodyCanonizationRelaxed():
0041     //      0.014 msecs per iteration (total: 59, iterations: 4096)
0042 }
0043 
0044 void DKIMUtilTest::shouldVerifyEmailDomain()
0045 {
0046     QCOMPARE(MessageViewer::DKIMUtil::emailDomain(QStringLiteral("foo@kde.org")), QStringLiteral("kde.org"));
0047     QCOMPARE(MessageViewer::DKIMUtil::emailDomain(QStringLiteral("foo@blo.bli.kde.org")), QStringLiteral("blo.bli.kde.org"));
0048 }
0049 
0050 void DKIMUtilTest::shouldVerifySubEmailDomain()
0051 {
0052     {
0053         const QString email = QStringLiteral("goo@kde.org");
0054         const QString domainName = MessageViewer::DKIMUtil::emailDomain(email);
0055         QCOMPARE(MessageViewer::DKIMUtil::emailSubDomain(domainName), QStringLiteral("kde.org"));
0056     }
0057     {
0058         const QString email = QStringLiteral("goo@bla.bli.kde.org");
0059         const QString domainName = MessageViewer::DKIMUtil::emailDomain(email);
0060         QCOMPARE(MessageViewer::DKIMUtil::emailSubDomain(domainName), QStringLiteral("kde.org"));
0061     }
0062     {
0063         const QString email = QStringLiteral("goo@bli.kde.org");
0064         const QString domainName = MessageViewer::DKIMUtil::emailDomain(email);
0065         QCOMPARE(MessageViewer::DKIMUtil::emailSubDomain(domainName), QStringLiteral("kde.org"));
0066     }
0067 }
0068 
0069 void DKIMUtilTest::shouldConvertAuthenticationMethodEnumToString()
0070 {
0071     QCOMPARE(MessageViewer::DKIMUtil::convertAuthenticationMethodEnumToString(MessageViewer::DKIMCheckSignatureJob::AuthenticationMethod::Unknown), QString());
0072     QCOMPARE(MessageViewer::DKIMUtil::convertAuthenticationMethodEnumToString(MessageViewer::DKIMCheckSignatureJob::AuthenticationMethod::Dkim),
0073              QStringLiteral("dkim"));
0074     QCOMPARE(MessageViewer::DKIMUtil::convertAuthenticationMethodEnumToString(MessageViewer::DKIMCheckSignatureJob::AuthenticationMethod::Spf),
0075              QStringLiteral("spf"));
0076     QCOMPARE(MessageViewer::DKIMUtil::convertAuthenticationMethodEnumToString(MessageViewer::DKIMCheckSignatureJob::AuthenticationMethod::Dmarc),
0077              QStringLiteral("dmarc"));
0078     QCOMPARE(MessageViewer::DKIMUtil::convertAuthenticationMethodEnumToString(MessageViewer::DKIMCheckSignatureJob::AuthenticationMethod::Dkimatps),
0079              QStringLiteral("dkim-atps"));
0080     QCOMPARE(MessageViewer::DKIMUtil::convertAuthenticationMethodEnumToString(MessageViewer::DKIMCheckSignatureJob::AuthenticationMethod::Auth),
0081              QStringLiteral("auth"));
0082 }
0083 
0084 void DKIMUtilTest::shouldConvertAuthenticationMethodToString()
0085 {
0086     QCOMPARE(MessageViewer::DKIMUtil::convertAuthenticationMethodStringToEnum(QStringLiteral("dkim")),
0087              MessageViewer::DKIMCheckSignatureJob::AuthenticationMethod::Dkim);
0088     QCOMPARE(MessageViewer::DKIMUtil::convertAuthenticationMethodStringToEnum(QStringLiteral("spf")),
0089              MessageViewer::DKIMCheckSignatureJob::AuthenticationMethod::Spf);
0090     QCOMPARE(MessageViewer::DKIMUtil::convertAuthenticationMethodStringToEnum(QStringLiteral("dmarc")),
0091              MessageViewer::DKIMCheckSignatureJob::AuthenticationMethod::Dmarc);
0092     QCOMPARE(MessageViewer::DKIMUtil::convertAuthenticationMethodStringToEnum(QStringLiteral("dkim-atps")),
0093              MessageViewer::DKIMCheckSignatureJob::AuthenticationMethod::Dkimatps);
0094     QCOMPARE(MessageViewer::DKIMUtil::convertAuthenticationMethodStringToEnum(QStringLiteral("auth")),
0095              MessageViewer::DKIMCheckSignatureJob::AuthenticationMethod::Auth);
0096     QCOMPARE(MessageViewer::DKIMUtil::convertAuthenticationMethodStringToEnum(QStringLiteral("sdfsdf")),
0097              MessageViewer::DKIMCheckSignatureJob::AuthenticationMethod::Unknown);
0098     QCOMPARE(MessageViewer::DKIMUtil::convertAuthenticationMethodStringToEnum(QString()), MessageViewer::DKIMCheckSignatureJob::AuthenticationMethod::Unknown);
0099 }
0100 
0101 #include "moc_dkimutiltest.cpp"