Warning, file /pim/kmailtransport/src/plugins/smtp/autotests/smtpjobtest.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     SPDX-FileCopyrightText: 2017 Daniel Vrátil <dvratil@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "../smtpjob.h"
0008 #include "fakeserver.h"
0009 
0010 #include "transportbase.h"
0011 #include "transportmanager.h"
0012 
0013 #include <QObject>
0014 #include <QStandardPaths>
0015 #include <QTest>
0016 
0017 Q_DECLARE_METATYPE(MailTransport::TransportBase::EnumAuthenticationType)
0018 
0019 class SmtpJobTest : public QObject
0020 {
0021     Q_OBJECT
0022 
0023 private Q_SLOTS:
0024     void initTestCase()
0025     {
0026         QStandardPaths::setTestModeEnabled(true);
0027     }
0028 
0029     void smtpJobTest_data()
0030     {
0031         QTest::addColumn<QList<QByteArray>>("scenario");
0032         QTest::addColumn<MailTransport::TransportBase::EnumAuthenticationType>("authType");
0033         QTest::addColumn<QString>("from");
0034         QTest::addColumn<QStringList>("to");
0035         QTest::addColumn<QStringList>("cc");
0036         QTest::addColumn<QByteArray>("data");
0037         QTest::addColumn<bool>("success");
0038 
0039         QList<QByteArray> scenario;
0040         scenario << FakeServer::greetingAndEhlo() << "S: 250 AUTH PLAIN LOGIN"
0041                  << "C: AUTH LOGIN"
0042                  << "S: 334 VXNlcm5hbWU6"
0043                  << "C: bG9naW4=" // "login".toBase64()
0044                  << "S: 334 UGFzc3dvcmQ6"
0045                  << "C: cGFzc3dvcmQ=" // "password".toBase64()
0046                  << "S: 235 Authenticated."
0047                  << "C: MAIL FROM:<foo@bar.com>"
0048                  << "S: 250 ok"
0049                  << "C: RCPT TO:<bar@foo.com>"
0050                  << "S: 250 ok"
0051                  << "C: RCPT TO:<bar@bar.foo>"
0052                  << "S: 250 ok"
0053                  << "C: DATA"
0054                  << "S: 354 Ok go ahead"
0055                  << "C: Hi Bob"
0056                  << "C: "
0057                  << "C: ."
0058                  << "S: 250 Ok transfer done" << FakeServer::bye();
0059         QTest::newRow("simple") << scenario << MailTransport::TransportBase::EnumAuthenticationType::LOGIN << QStringLiteral("Foo Bar <foo@bar.com>")
0060                                 << QStringList{} << QStringList{QStringLiteral("bar@foo.com"), QStringLiteral("<bar@bar.foo>")} << QByteArray("Hi Bob") << true;
0061     }
0062 
0063     void smtpJobTest()
0064     {
0065         QFETCH(QList<QByteArray>, scenario);
0066         QFETCH(MailTransport::TransportBase::EnumAuthenticationType, authType);
0067         QFETCH(QString, from);
0068         QFETCH(QStringList, to);
0069         QFETCH(QStringList, cc);
0070         QFETCH(QByteArray, data);
0071         QFETCH(bool, success);
0072 
0073         FakeServer server;
0074         server.setScenario(scenario);
0075         server.startAndWait();
0076 
0077         auto transport = MailTransport::TransportManager::self()->createTransport();
0078         transport->setSpecifyHostname(true);
0079         transport->setHost(QStringLiteral("127.0.0.1"));
0080         transport->setLocalHostname(QStringLiteral("127.0.0.1"));
0081         transport->setPort(5989);
0082         transport->setRequiresAuthentication(true);
0083         transport->setAuthenticationType(authType);
0084         transport->setStorePassword(false);
0085         transport->setUserName(QStringLiteral("login"));
0086         transport->setPassword(QStringLiteral("password"));
0087 
0088         {
0089             MailTransport::SmtpJob smtpJob(transport);
0090             smtpJob.setSender(from);
0091             smtpJob.setTo(to);
0092             smtpJob.setCc(cc);
0093             smtpJob.setData(data);
0094 
0095             QVERIFY(smtpJob.exec());
0096             if (success) {
0097                 QCOMPARE(smtpJob.error(), 0);
0098             } else {
0099                 QVERIFY(smtpJob.error() > 0);
0100             }
0101 
0102             // Make sure the smtpJob goes out-of-scope here and thus the
0103             // internal session pool is destroyed
0104         }
0105         // KSMTP time to stop the session
0106         QTest::qWait(10);
0107 
0108         QVERIFY(server.isAllScenarioDone());
0109         server.quit();
0110     }
0111 };
0112 
0113 QTEST_MAIN(SmtpJobTest)
0114 
0115 #include "smtpjobtest.moc"