File indexing completed on 2024-10-27 04:50:59

0001 /*
0002   SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org>
0003 
0004   SPDX-License-Identifier: LGPL-2.0-or-later
0005 
0006 */
0007 
0008 #include "potentialphishingemailjobtest.h"
0009 #include "../potentialphishingemailjob.h"
0010 #include <QSignalSpy>
0011 #include <QStringList>
0012 #include <QTest>
0013 
0014 PotentialPhishingEmailJobTest::PotentialPhishingEmailJobTest(QObject *parent)
0015     : QObject(parent)
0016 {
0017 }
0018 
0019 PotentialPhishingEmailJobTest::~PotentialPhishingEmailJobTest() = default;
0020 
0021 void PotentialPhishingEmailJobTest::shouldNotStartIfNoEmails()
0022 {
0023     auto job = new PotentialPhishingEmailJob;
0024     QVERIFY(!job->start());
0025     QVERIFY(job->potentialPhisingEmails().isEmpty());
0026 }
0027 
0028 void PotentialPhishingEmailJobTest::shouldReturnPotentialPhishingEmails_data()
0029 {
0030     QTest::addColumn<QStringList>("listEmails");
0031     QTest::addColumn<QStringList>("whiteListEmail");
0032     QTest::addColumn<bool>("hasPotentialPhishing");
0033     QTest::newRow("NoPotentialPhishing") << (QStringList() << QStringLiteral("foo@kde.org")) << QStringList() << false;
0034     QTest::newRow("HasPotentialPhishing") << (QStringList() << QStringLiteral("\"bla@kde.org\" <foo@kde.org>")) << QStringList() << true;
0035     const QString email = QStringLiteral("\"bla@kde.org\" <foo@kde.org>");
0036     QTest::newRow("EmailInWhiteList") << (QStringList() << email) << (QStringList() << email) << false;
0037     QTest::newRow("NotAllEmailInWhiteList") << (QStringList() << email << QStringLiteral("\"c@kde.org\" <dd@kde.org>")) << (QStringList() << email) << true;
0038     QTest::newRow("EmailInWhiteListWithSpace") << (QStringList() << QStringLiteral(" \"bla@kde.org\" <foo@kde.org> ")) << (QStringList() << email) << false;
0039     QTest::newRow("EmailWithSameNameAndDisplayName") << (QStringList() << QStringLiteral("\"<foo@kde.com>\" <foo@kde.com>")) << (QStringList() << email)
0040                                                      << false;
0041     QTest::newRow("EmailWithSameNameAndDisplayNameWithSpace")
0042         << (QStringList() << QStringLiteral(" \"<foo@kde.com>\" <foo@kde.com> ")) << (QStringList() << email) << false;
0043 
0044     QTest::newRow("notsamecase") << (QStringList() << QStringLiteral("\"Foo@kde.org\" <foo@kde.org>")) << QStringList() << false;
0045     QTest::newRow("notsamecaseaddress") << (QStringList() << QStringLiteral("\"Foo@kde.org\" <FOO@kde.ORG>")) << QStringList() << false;
0046 
0047     QTest::newRow("emailinparenthese") << (QStringList() << QStringLiteral("\"bla (Foo@kde.org)\" <FOO@kde.ORG>")) << QStringList() << false;
0048     QTest::newRow("notemailinparenthese") << (QStringList() << QStringLiteral("\"bla (bli@kde.org)\" <FOO@kde.ORG>")) << QStringList() << true;
0049     QTest::newRow("erroremailinparenthese") << (QStringList() << QStringLiteral("\"bla Foo@kde.org\" <FOO@kde.ORG>")) << QStringList() << true;
0050 
0051     QTest::newRow("WithMultiSameEmail") << (QStringList() << QStringLiteral("\"foo@kde.org foo@kde.org\" <foo@kde.org>")) << QStringList() << false;
0052     QTest::newRow("WithMultiSameEmailWithSpace") << (QStringList() << QStringLiteral("\"  foo@kde.org   foo@kde.org  \" <foo@kde.org>")) << QStringList()
0053                                                  << false;
0054     QTest::newRow("WithMultiNotSameEmail") << (QStringList() << QStringLiteral("\"  bla@kde.org   foo@kde.org  \" <foo@kde.org>")) << QStringList() << true;
0055 
0056     QTest::newRow("EmailWithSimpleQuote") << (QStringList() << QStringLiteral("\"\'foo@kde.org\'\" <foo@kde.org>")) << QStringList() << false;
0057 
0058     QTest::newRow("BadCompletion") << (QStringList() << QStringLiteral("@kde.org <foo@kde.org>")) << QStringList() << false;
0059 }
0060 
0061 void PotentialPhishingEmailJobTest::shouldReturnPotentialPhishingEmails()
0062 {
0063     QFETCH(QStringList, listEmails);
0064     QFETCH(QStringList, whiteListEmail);
0065     QFETCH(bool, hasPotentialPhishing);
0066 
0067     auto job = new PotentialPhishingEmailJob;
0068     job->setEmailWhiteList(whiteListEmail);
0069     job->setPotentialPhishingEmails(listEmails);
0070     QVERIFY(job->start());
0071     QCOMPARE(job->potentialPhisingEmails().isEmpty(), !hasPotentialPhishing);
0072 }
0073 
0074 void PotentialPhishingEmailJobTest::shouldEmitSignal()
0075 {
0076     auto job = new PotentialPhishingEmailJob;
0077     QSignalSpy spy(job, &PotentialPhishingEmailJob::potentialPhishingEmailsFound);
0078     job->setPotentialPhishingEmails((QStringList() << QStringLiteral("\"bla@kde.org\" <foo@kde.org>")));
0079     QVERIFY(job->start());
0080     QCOMPARE(spy.count(), 1);
0081 }
0082 
0083 void PotentialPhishingEmailJobTest::shouldCreateCorrectListOfEmails_data()
0084 {
0085     QTest::addColumn<QStringList>("emails");
0086     QTest::addColumn<QStringList>("createdListOfEmails");
0087     QTest::newRow("emptylist") << QStringList() << QStringList();
0088     QStringList emails{QStringLiteral("foo@kde.org"), QStringLiteral("bla@kde.org")};
0089     QStringList createdList{QStringLiteral("foo@kde.org"), QStringLiteral("bla@kde.org")};
0090     QTest::newRow("nonempty") << emails << createdList;
0091     emails = QStringList{QStringLiteral("\"bla\" <foo@kde.org>"), QStringLiteral("bla@kde.org")};
0092     QTest::newRow("potentialerrors") << emails << emails;
0093 
0094     emails = QStringList{QStringLiteral("\"bla, foo\" <foo@kde.org>"), QStringLiteral("bla@kde.org")};
0095     QTest::newRow("emailswithquote") << emails << emails;
0096 
0097     emails = QStringList{QStringLiteral("\"bla, foo\" <foo@kde.org>"), QStringLiteral("bla@kde.org"), QStringLiteral(" ")};
0098     createdList = QStringList{QStringLiteral("\"bla, foo\" <foo@kde.org>"), QStringLiteral("bla@kde.org")};
0099     QTest::newRow("emailswithemptystr") << emails << createdList;
0100 }
0101 
0102 void PotentialPhishingEmailJobTest::shouldCreateCorrectListOfEmails()
0103 {
0104     QFETCH(QStringList, emails);
0105     QFETCH(QStringList, createdListOfEmails);
0106     auto job = new PotentialPhishingEmailJob;
0107     job->setPotentialPhishingEmails(emails);
0108     QCOMPARE(job->checkEmails(), createdListOfEmails);
0109     delete job;
0110 }
0111 
0112 QTEST_MAIN(PotentialPhishingEmailJobTest)
0113 
0114 #include "moc_potentialphishingemailjobtest.cpp"