From 76b406f4e13adfdc77372773e647c3e016f6ea1c Mon Sep 17 00:00:00 2001 From: Martchus Date: Sun, 28 Jan 2024 21:35:54 +0100 Subject: [PATCH] Adapt tests to recent changes in certificate handling --- syncthingconnector/syncthingconnection.cpp | 12 ++++++++++++ syncthingconnector/syncthingconnection.h | 1 + syncthingconnector/tests/misctests.cpp | 16 ++++++++++++---- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/syncthingconnector/syncthingconnection.cpp b/syncthingconnector/syncthingconnection.cpp index 2a0385c..c52bc6f 100644 --- a/syncthingconnector/syncthingconnection.cpp +++ b/syncthingconnector/syncthingconnection.cpp @@ -822,6 +822,18 @@ bool SyncthingConnection::loadSelfSignedCertificate(const QUrl &url) return true; } +/*! + * \brief Clears the self-signed certificate that might be loaded via loadSelfSignedCertificate(). + * \remarks This function mainly exists to ease testing; one normally doesn't need to invoke it explicitly. + */ +void SyncthingConnection::clearSelfSignedCertificate() +{ + m_expectedSslErrors.clear(); + m_certificatePath.clear(); + m_dynamicallyDeterminedCertificatePath.clear(); + m_certificateLastModified = QDateTime(); +} + /*! * \brief Applies the specified configuration. * \remarks diff --git a/syncthingconnector/syncthingconnection.h b/syncthingconnector/syncthingconnection.h index ff8f5a9..2622cbb 100644 --- a/syncthingconnector/syncthingconnection.h +++ b/syncthingconnector/syncthingconnection.h @@ -182,6 +182,7 @@ public: public Q_SLOTS: bool loadSelfSignedCertificate(const QUrl &url = QUrl()); + void clearSelfSignedCertificate(); bool applySettings(Data::SyncthingConnectionSettings &connectionSettings); // methods to initiate/close connection diff --git a/syncthingconnector/tests/misctests.cpp b/syncthingconnector/tests/misctests.cpp index e875cc2..04ac811 100644 --- a/syncthingconnector/tests/misctests.cpp +++ b/syncthingconnector/tests/misctests.cpp @@ -17,6 +17,8 @@ #include #include +#include + using namespace std; using namespace Data; using namespace CppUtilities; @@ -185,12 +187,18 @@ void MiscTests::testConnectionSettingsAndLoadingSelfSignedCert() settings.syncthingUrl = QStringLiteral("https://localhost:8080"); CPPUNIT_ASSERT(connection.applySettings(settings)); connection.m_configDir = QStringLiteral("some-non/existent-dir"); - bool errorOccured = false; + connection.clearSelfSignedCertificate(); + const auto expectedErrorMessage = QStringLiteral("Unable to load certificate used by Syncthing."); + auto expectedErrorOccured = false; const function errorHandler - = [&errorOccured](const QString &message) { errorOccured |= message == QStringLiteral("Unable to load certificate used by Syncthing."); }; - waitForSignals(bind(&SyncthingConnection::loadSelfSignedCertificate, &connection, QUrl()), 1, - signalInfo(&connection, &SyncthingConnection::error, errorHandler, &errorOccured)); + = [&expectedErrorOccured, &expectedErrorMessage](const QString &message) { + std::cout << "\n - error: " << message.toLocal8Bit().data() << '\n'; + expectedErrorOccured |= message == expectedErrorMessage; + }; + waitForSignals(bind(&SyncthingConnection::loadSelfSignedCertificate, &connection, QUrl()), 1000, + signalInfo(&connection, &SyncthingConnection::error, errorHandler, &expectedErrorOccured)); settings.expectedSslErrors.clear(); + settings.httpsCertPath.clear(); CPPUNIT_ASSERT(!connection.applySettings(settings)); }