From 2a9b02ff6d2279f7cd301ae1f397d275afa66922 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Tue, 28 Nov 2017 14:13:03 +0100 Subject: Fixed imap tests Adjust to cyrus imap settings and use explicit encryption setting. --- examples/imapresource/imapresource.cpp | 26 +++++++++++++++----- examples/imapresource/imapserverproxy.cpp | 15 ++++++++---- examples/imapresource/imapserverproxy.h | 9 ++++++- examples/imapresource/tests/imapmailsynctest.cpp | 16 ++++++------- examples/imapresource/tests/imapmailtest.cpp | 4 ++-- .../imapresource/tests/imapserverproxytest.cpp | 28 ++++++++++++---------- examples/imapresource/tests/populatemailbox.sh | 2 +- examples/imapresource/tests/resetmailbox.sh | 5 ++-- 8 files changed, 68 insertions(+), 37 deletions(-) diff --git a/examples/imapresource/imapresource.cpp b/examples/imapresource/imapresource.cpp index 14c3a3e..cd3b69f 100644 --- a/examples/imapresource/imapresource.cpp +++ b/examples/imapresource/imapresource.cpp @@ -526,7 +526,7 @@ public: KAsync::Job synchronizeWithSource(const Sink::QueryBase &query) Q_DECL_OVERRIDE { - auto imap = QSharedPointer::create(mServer, mPort, &mSessionCache); + auto imap = QSharedPointer::create(mServer, mPort, mEncryptionMode, &mSessionCache); if (query.type() == ApplicationDomain::getTypeName()) { return login(imap) .then([=] { @@ -634,7 +634,7 @@ public: return KAsync::null(); } } - auto imap = QSharedPointer::create(mServer, mPort, &mSessionCache); + auto imap = QSharedPointer::create(mServer, mPort, mEncryptionMode, &mSessionCache); auto login = imap->login(mUser, secret()); KAsync::Job job = KAsync::null(); if (operation == Sink::Operation_Creation) { @@ -719,7 +719,7 @@ public: return KAsync::error("Tried to replay modification without old remoteId."); } } - auto imap = QSharedPointer::create(mServer, mPort, &mSessionCache); + auto imap = QSharedPointer::create(mServer, mPort, mEncryptionMode, &mSessionCache); auto login = imap->login(mUser, secret()); if (operation == Sink::Operation_Creation) { QString parentFolder; @@ -793,6 +793,7 @@ public: public: QString mServer; int mPort; + Imap::EncryptionMode mEncryptionMode = Imap::NoEncryption; QString mUser; int mDaysToSync = 0; QByteArray mResourceInstanceIdentifier; @@ -839,7 +840,7 @@ protected: } KIMAP2::FetchJob::FetchScope scope; scope.mode = KIMAP2::FetchJob::FetchScope::Full; - auto imap = QSharedPointer::create(mServer, mPort); + auto imap = QSharedPointer::create(mServer, mPort, mEncryptionMode); auto messageByUid = QSharedPointer>::create(); SinkTrace() << "Connecting to:" << mServer << mPort; SinkTrace() << "as:" << mUser; @@ -907,7 +908,7 @@ protected: auto set = KIMAP2::ImapSet::fromImapSequenceSet("1:*"); KIMAP2::FetchJob::FetchScope scope; scope.mode = KIMAP2::FetchJob::FetchScope::Headers; - auto imap = QSharedPointer::create(mServer, mPort); + auto imap = QSharedPointer::create(mServer, mPort, mEncryptionMode); auto messageByUid = QSharedPointer>::create(); return imap->login(mUser, secret()) .then(imap->select(remoteId)) @@ -925,7 +926,7 @@ protected: auto folderByPath = QSharedPointer>::create(); auto folderByName = QSharedPointer>::create(); - auto imap = QSharedPointer::create(mServer, mPort); + auto imap = QSharedPointer::create(mServer, mPort, mEncryptionMode); auto inspectionJob = imap->login(mUser, secret()) .then(imap->fetchFolders([=](const Imap::Folder &f) { *folderByPath << f.path(); @@ -950,6 +951,7 @@ protected: public: QString mServer; int mPort; + Imap::EncryptionMode mEncryptionMode = Imap::NoEncryption; QString mUser; }; @@ -962,6 +964,16 @@ ImapResource::ImapResource(const ResourceContext &resourceContext) auto port = config.value("port").toInt(); auto user = config.value("username").toString(); auto daysToSync = config.value("daysToSync", 14).toInt(); + auto starttls = config.value("starttls", false).toBool(); + + auto encryption = Imap::NoEncryption; + if (server.startsWith("imaps")) { + encryption = Imap::Tls; + } + if (starttls) { + encryption = Imap::Starttls; + } + if (server.startsWith("imap")) { server.remove("imap://"); server.remove("imaps://"); @@ -975,6 +987,7 @@ ImapResource::ImapResource(const ResourceContext &resourceContext) auto synchronizer = QSharedPointer::create(resourceContext); synchronizer->mServer = server; synchronizer->mPort = port; + synchronizer->mEncryptionMode = encryption; synchronizer->mUser = user; synchronizer->mDaysToSync = daysToSync; setupSynchronizer(synchronizer); @@ -982,6 +995,7 @@ ImapResource::ImapResource(const ResourceContext &resourceContext) auto inspector = QSharedPointer::create(resourceContext); inspector->mServer = server; inspector->mPort = port; + inspector->mEncryptionMode = encryption; inspector->mUser = user; setupInspector(inspector); diff --git a/examples/imapresource/imapserverproxy.cpp b/examples/imapresource/imapserverproxy.cpp index 9a84327..2dd2d46 100644 --- a/examples/imapresource/imapserverproxy.cpp +++ b/examples/imapresource/imapserverproxy.cpp @@ -136,13 +136,20 @@ KIMAP2::Session *createNewSession(const QString &serverUrl, int port) return newSession; } -ImapServerProxy::ImapServerProxy(const QString &serverUrl, int port, SessionCache *sessionCache) : mSessionCache(sessionCache), mSession(nullptr) +ImapServerProxy::ImapServerProxy(const QString &serverUrl, int port, EncryptionMode encryptionMode, SessionCache *sessionCache) : mSessionCache(sessionCache), mSession(nullptr), mEncryptionMode(encryptionMode) { if (!mSessionCache || mSessionCache->isEmpty()) { mSession = createNewSession(serverUrl, port); } } +QDebug operator<<(QDebug debug, const KIMAP2::MailBoxDescriptor &c) +{ + QDebugStateSaver saver(debug); + debug.nospace() << c.name; + return debug; +} + KAsync::Job ImapServerProxy::login(const QString &username, const QString &password) { if (password.isEmpty()) { @@ -164,12 +171,12 @@ KAsync::Job ImapServerProxy::login(const QString &username, const QString auto loginJob = new KIMAP2::LoginJob(mSession); loginJob->setUserName(username); loginJob->setPassword(password); - loginJob->setAuthenticationMode(KIMAP2::LoginJob::Plain); - if (mSession->port() == 143) { + if (mEncryptionMode == Starttls) { loginJob->setEncryptionMode(QSsl::TlsV1_0OrLater, true); - } else { + } else if (mEncryptionMode == Tls) { loginJob->setEncryptionMode(QSsl::AnyProtocol, false); } + loginJob->setAuthenticationMode(KIMAP2::LoginJob::Plain); auto capabilitiesJob = new KIMAP2::CapabilitiesJob(mSession); QObject::connect(capabilitiesJob, &KIMAP2::CapabilitiesJob::capabilitiesReceived, &mGuard, [this](const QStringList &capabilities) { diff --git a/examples/imapresource/imapserverproxy.h b/examples/imapresource/imapserverproxy.h index 7044a5e..0dc6e55 100644 --- a/examples/imapresource/imapserverproxy.h +++ b/examples/imapresource/imapserverproxy.h @@ -246,9 +246,15 @@ private: QList mSessions; }; +enum EncryptionMode { + NoEncryption, + Tls, + Starttls +}; + class ImapServerProxy { public: - ImapServerProxy(const QString &serverUrl, int port, SessionCache *sessionCache = nullptr); + ImapServerProxy(const QString &serverUrl, int port, EncryptionMode encryption, SessionCache *sessionCache = nullptr); //Standard IMAP calls KAsync::Job login(const QString &username, const QString &password); @@ -305,6 +311,7 @@ private: KIMAP2::Session *mSession; QStringList mCapabilities; Namespaces mNamespaces; + EncryptionMode mEncryptionMode; }; } diff --git a/examples/imapresource/tests/imapmailsynctest.cpp b/examples/imapresource/tests/imapmailsynctest.cpp index 23bb4e5..cdb85dd 100644 --- a/examples/imapresource/tests/imapmailsynctest.cpp +++ b/examples/imapresource/tests/imapmailsynctest.cpp @@ -43,7 +43,7 @@ protected: bool isBackendAvailable() Q_DECL_OVERRIDE { QTcpSocket socket; - socket.connectToHost("localhost", 993); + socket.connectToHost("localhost", 143); return socket.waitForConnected(200); } @@ -56,7 +56,7 @@ protected: { auto resource = ApplicationDomain::ImapResource::create("account1"); resource.setProperty("server", "localhost"); - resource.setProperty("port", 993); + resource.setProperty("port", 143); resource.setProperty("username", "doe"); Sink::SecretStore::instance().insert(resource.identifier(), "doe"); return resource; @@ -67,7 +67,7 @@ protected: auto resource = ApplicationDomain::ImapResource::create("account1"); //Using a bogus ip instead of a bogus hostname avoids getting stuck in the hostname lookup resource.setProperty("server", "111.111.1.1"); - resource.setProperty("port", 993); + resource.setProperty("port", 143); resource.setProperty("username", "doe"); Sink::SecretStore::instance().insert(resource.identifier(), "doe"); return resource; @@ -80,21 +80,21 @@ protected: void createFolder(const QStringList &folderPath) Q_DECL_OVERRIDE { - Imap::ImapServerProxy imap("localhost", 993); + Imap::ImapServerProxy imap("localhost", 143, Imap::NoEncryption); VERIFYEXEC(imap.login("doe", "doe")); VERIFYEXEC(imap.create("INBOX." + folderPath.join('.'))); } void removeFolder(const QStringList &folderPath) Q_DECL_OVERRIDE { - Imap::ImapServerProxy imap("localhost", 993); + Imap::ImapServerProxy imap("localhost", 143, Imap::NoEncryption); VERIFYEXEC(imap.login("doe", "doe")); VERIFYEXEC(imap.remove("INBOX." + folderPath.join('.'))); } QByteArray createMessage(const QStringList &folderPath, const QByteArray &message) Q_DECL_OVERRIDE { - Imap::ImapServerProxy imap("localhost", 993); + Imap::ImapServerProxy imap("localhost", 143, Imap::NoEncryption); VERIFYEXEC_RET(imap.login("doe", "doe"), {}); VERIFYEXEC_RET(imap.append("INBOX." + folderPath.join('.'), message), {}); return "2:*"; @@ -102,14 +102,14 @@ protected: void removeMessage(const QStringList &folderPath, const QByteArray &messages) Q_DECL_OVERRIDE { - Imap::ImapServerProxy imap("localhost", 993); + Imap::ImapServerProxy imap("localhost", 143, Imap::NoEncryption); VERIFYEXEC(imap.login("doe", "doe")); VERIFYEXEC(imap.remove("INBOX." + folderPath.join('.'), messages)); } void markAsImportant(const QStringList &folderPath, const QByteArray &messageIdentifier) Q_DECL_OVERRIDE { - Imap::ImapServerProxy imap("localhost", 993); + Imap::ImapServerProxy imap("localhost", 143, Imap::NoEncryption); VERIFYEXEC(imap.login("doe", "doe")); VERIFYEXEC(imap.select("INBOX." + folderPath.join('.'))); VERIFYEXEC(imap.addFlags(KIMAP2::ImapSet::fromImapSequenceSet(messageIdentifier), QByteArrayList() << Imap::Flags::Flagged)); diff --git a/examples/imapresource/tests/imapmailtest.cpp b/examples/imapresource/tests/imapmailtest.cpp index 60ec1bf..6616811 100644 --- a/examples/imapresource/tests/imapmailtest.cpp +++ b/examples/imapresource/tests/imapmailtest.cpp @@ -23,7 +23,7 @@ protected: bool isBackendAvailable() Q_DECL_OVERRIDE { QTcpSocket socket; - socket.connectToHost("localhost", 993); + socket.connectToHost("localhost", 143); return socket.waitForConnected(200); } @@ -36,7 +36,7 @@ protected: { auto resource = ApplicationDomain::ImapResource::create("account1"); resource.setProperty("server", "localhost"); - resource.setProperty("port", 993); + resource.setProperty("port", 143); resource.setProperty("username", "doe"); Sink::SecretStore::instance().insert(resource.identifier(), "doe"); return resource; diff --git a/examples/imapresource/tests/imapserverproxytest.cpp b/examples/imapresource/tests/imapserverproxytest.cpp index 271b3d9..999665f 100644 --- a/examples/imapresource/tests/imapserverproxytest.cpp +++ b/examples/imapresource/tests/imapserverproxytest.cpp @@ -25,7 +25,7 @@ private slots: { Sink::Test::initTest(); QTcpSocket socket; - socket.connectToHost("localhost", 993); + socket.connectToHost("localhost", 143); QVERIFY(socket.waitForConnected(200)); system("resetmailbox.sh"); } @@ -43,45 +43,47 @@ private slots: void testLogin() { - ImapServerProxy imap("localhost", 993); + ImapServerProxy imap("localhost", 143, Imap::EncryptionMode::NoEncryption); VERIFYEXEC(imap.login("doe", "doe")); } void testLoginFailure() { //Using a bogus ip instead of a bogus hostname avoids getting stuck in the hostname lookup - ImapServerProxy imap("111.111.1.1", 993); + ImapServerProxy imap("111.111.1.1", 143, Imap::EncryptionMode::NoEncryption); VERIFYEXEC_FAIL(imap.login("doe", "doe")); } void testFetchFolders() { - QMap expectedFolderAndParent; - expectedFolderAndParent.insert("INBOX", ""); - expectedFolderAndParent.insert("Drafts", ""); - expectedFolderAndParent.insert("Trash", ""); - expectedFolderAndParent.insert("test", ""); - ImapServerProxy imap("localhost", 993); + QMap expectedFolderAndParent { + {"INBOX", ""}, + {"Drafts", ""}, + {"Trash", ""}, + {"test", ""} + }; + ImapServerProxy imap("localhost", 143, Imap::EncryptionMode::NoEncryption); VERIFYEXEC(imap.login("doe", "doe")); QVector list; VERIFYEXEC(imap.fetchFolders([&](const Folder &f){ list << f;})); for (const auto &f : list) { - QVERIFY(expectedFolderAndParent.contains(f.name())); + QVERIFY2(expectedFolderAndParent.contains(f.name()), QString{"Didn't expect folder %1"}.arg(f.name()).toUtf8()); QCOMPARE(expectedFolderAndParent.value(f.name()), f.parentPath()); expectedFolderAndParent.remove(f.name()); } QVERIFY(expectedFolderAndParent.isEmpty()); +//examples/imapresource/tests/imapserverproxytest testFetchFolders } void testFetchFoldersFailure() { - ImapServerProxy imap("foobar", 993); + ImapServerProxy imap("foobar", 143, Imap::EncryptionMode::NoEncryption); VERIFYEXEC_FAIL(imap.fetchFolders([](const Folder &){})); } void testFetchMail() { - ImapServerProxy imap("localhost", 993); + ImapServerProxy imap("localhost", 143, Imap::EncryptionMode::NoEncryption); VERIFYEXEC(imap.login("doe", "doe")); KIMAP2::FetchJob::FetchScope scope; @@ -98,7 +100,7 @@ private slots: void testRemoveMail() { - ImapServerProxy imap("localhost", 993); + ImapServerProxy imap("localhost", 143, Imap::EncryptionMode::NoEncryption); VERIFYEXEC(imap.login("doe", "doe")); VERIFYEXEC(imap.remove("INBOX.test", "1:*")); diff --git a/examples/imapresource/tests/populatemailbox.sh b/examples/imapresource/tests/populatemailbox.sh index 0889790..a5949fc 100644 --- a/examples/imapresource/tests/populatemailbox.sh +++ b/examples/imapresource/tests/populatemailbox.sh @@ -23,7 +23,7 @@ sudo echo "sub INBOX.Trash" | cyradm --auth PLAIN -u doe -w doe localhost # # sudo cp /src/sink/examples/imapresource/tests/data/1365777830.R28.localhost.localdomain\:2\,S /var/spool/imap/d/user/doe/test/$i. # done # Because this is way faster than a loop -FOLDERPATH=/var/spool/imap/u/'user^doe^test' +FOLDERPATH=/var/spool/imap/d/user/doe/test sudo tee /dev/null $FOLDERPATH/{1..1000}. sudo tee /dev/null $FOLDERPATH/{1001..2000}. sudo tee /dev/null $FOLDERPATH/{2001..3000}. diff --git a/examples/imapresource/tests/resetmailbox.sh b/examples/imapresource/tests/resetmailbox.sh index f3a9391..8e6b1b1 100644 --- a/examples/imapresource/tests/resetmailbox.sh +++ b/examples/imapresource/tests/resetmailbox.sh @@ -9,6 +9,7 @@ sudo echo "subscribe INBOX.Drafts" | cyradm --auth PLAIN -u doe -w doe localhost sudo echo "cm user.doe.Trash" | cyradm --auth PLAIN -u cyrus -w admin localhost sudo echo "subscribe INBOX.Trash" | cyradm --auth PLAIN -u doe -w doe localhost sudo echo "sam user.doe cyrus c" | cyradm --auth PLAIN -u cyrus -w admin localhost -sudo cp /src/sink/examples/imapresource/tests/data/1365777830.R28.localhost.localdomain\:2\,S /var/spool/imap/u/'user^doe^test'/1. -sudo chown cyrus:mail /var/spool/imap/u/'user^doe^test'/1. +FOLDERPATH=/var/spool/imap/d/user/doe/test +sudo cp /src/sink/examples/imapresource/tests/data/1365777830.R28.localhost.localdomain\:2\,S $FOLDERPATH/1. +sudo chown cyrus:mail $FOLDERPATH/1. sudo reconstruct "user.doe.test" -- cgit v1.2.3