2017-03-09 23:06:03 +01:00
|
|
|
#include "../syncthingconfig.h"
|
2017-07-09 23:34:43 +02:00
|
|
|
#include "../syncthingconnection.h"
|
|
|
|
#include "../syncthingconnectionsettings.h"
|
2018-04-18 23:27:45 +02:00
|
|
|
#include "../syncthingprocess.h"
|
2017-07-09 23:34:43 +02:00
|
|
|
#include "../syncthingservice.h"
|
2017-03-09 23:06:03 +01:00
|
|
|
#include "../utils.h"
|
|
|
|
|
|
|
|
#include <c++utilities/chrono/datetime.h>
|
2017-07-09 23:34:43 +02:00
|
|
|
#include <c++utilities/chrono/format.h>
|
2017-03-09 23:06:03 +01:00
|
|
|
#include <c++utilities/chrono/timespan.h>
|
2017-05-04 22:48:45 +02:00
|
|
|
#include <c++utilities/tests/testutils.h>
|
2017-03-09 23:06:03 +01:00
|
|
|
|
2018-05-01 01:10:46 +02:00
|
|
|
#include "../../testhelper/helper.h"
|
2017-07-09 23:34:43 +02:00
|
|
|
|
2017-03-09 23:06:03 +01:00
|
|
|
#include <cppunit/TestFixture.h>
|
|
|
|
|
2017-07-09 23:34:43 +02:00
|
|
|
#include <QFile>
|
2017-03-09 23:06:03 +01:00
|
|
|
#include <QUrl>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace Data;
|
2019-06-10 22:48:26 +02:00
|
|
|
using namespace CppUtilities;
|
|
|
|
using namespace CppUtilities::Literals;
|
2017-03-09 23:06:03 +01:00
|
|
|
|
|
|
|
using namespace CPPUNIT_NS;
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief The MiscTests class tests various features of the connector library.
|
|
|
|
*/
|
2017-05-04 22:48:45 +02:00
|
|
|
class MiscTests : public TestFixture {
|
2017-03-09 23:06:03 +01:00
|
|
|
CPPUNIT_TEST_SUITE(MiscTests);
|
|
|
|
CPPUNIT_TEST(testParsingConfig);
|
2018-04-18 23:27:45 +02:00
|
|
|
CPPUNIT_TEST(testSplittingArguments);
|
2017-03-09 23:06:03 +01:00
|
|
|
CPPUNIT_TEST(testUtils);
|
2019-07-13 12:57:35 +02:00
|
|
|
#ifdef LIB_SYNCTHING_CONNECTOR_SUPPORT_SYSTEMD
|
2017-07-09 23:34:43 +02:00
|
|
|
CPPUNIT_TEST(testService);
|
2019-07-13 12:57:35 +02:00
|
|
|
#endif
|
2017-07-09 23:34:43 +02:00
|
|
|
CPPUNIT_TEST(testConnectionSettingsAndLoadingSelfSignedCert);
|
|
|
|
CPPUNIT_TEST(testSyncthingDir);
|
2017-03-09 23:06:03 +01:00
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
|
|
|
|
public:
|
|
|
|
MiscTests();
|
|
|
|
|
|
|
|
void testParsingConfig();
|
2018-04-18 23:27:45 +02:00
|
|
|
void testSplittingArguments();
|
2017-03-09 23:06:03 +01:00
|
|
|
void testUtils();
|
2019-07-13 12:57:35 +02:00
|
|
|
#ifdef LIB_SYNCTHING_CONNECTOR_SUPPORT_SYSTEMD
|
2017-07-09 23:34:43 +02:00
|
|
|
void testService();
|
2019-07-13 12:57:35 +02:00
|
|
|
#endif
|
2017-07-09 23:34:43 +02:00
|
|
|
void testConnectionSettingsAndLoadingSelfSignedCert();
|
|
|
|
void testSyncthingDir();
|
2017-03-09 23:06:03 +01:00
|
|
|
|
|
|
|
void setUp();
|
|
|
|
void tearDown();
|
|
|
|
|
|
|
|
private:
|
|
|
|
};
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(MiscTests);
|
|
|
|
|
|
|
|
MiscTests::MiscTests()
|
2017-05-04 22:48:45 +02:00
|
|
|
{
|
|
|
|
}
|
2017-03-09 23:06:03 +01:00
|
|
|
|
|
|
|
//
|
|
|
|
// test setup
|
|
|
|
//
|
|
|
|
|
|
|
|
void MiscTests::setUp()
|
2017-05-04 22:48:45 +02:00
|
|
|
{
|
|
|
|
}
|
2017-03-09 23:06:03 +01:00
|
|
|
|
|
|
|
void MiscTests::tearDown()
|
2017-05-04 22:48:45 +02:00
|
|
|
{
|
|
|
|
}
|
2017-03-09 23:06:03 +01:00
|
|
|
|
|
|
|
//
|
|
|
|
// actual test
|
|
|
|
//
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Tests basic behaviour of the SyncthingConnection class.
|
|
|
|
*/
|
|
|
|
void MiscTests::testParsingConfig()
|
|
|
|
{
|
|
|
|
SyncthingConfig config;
|
2017-07-09 23:34:43 +02:00
|
|
|
CPPUNIT_ASSERT(!config.restore(QStringLiteral("non-existant-file")));
|
|
|
|
CPPUNIT_ASSERT(config.restore(QString::fromLocal8Bit(testFilePath("testconfig/config.xml").data())));
|
2018-10-10 22:45:49 +02:00
|
|
|
CPPUNIT_ASSERT_EQUAL_MESSAGE("address", QStringLiteral("localhost:4001"), config.guiAddress);
|
2017-03-09 23:06:03 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL_MESSAGE("API key", QStringLiteral("syncthingconnectortest"), config.guiApiKey);
|
2017-07-09 23:34:43 +02:00
|
|
|
CPPUNIT_ASSERT_EQUAL_MESSAGE("user", QStringLiteral("nobody"), config.guiUser);
|
|
|
|
CPPUNIT_ASSERT_EQUAL_MESSAGE("password", QStringLiteral("$2a$12$35MnbsQgQNn1hzPYK/lWXOaP.U5D2TO0nuuQy2M4gsqJB4ff4q2RK"), config.guiPasswordHash);
|
2017-03-09 23:06:03 +01:00
|
|
|
CPPUNIT_ASSERT_MESSAGE("TLS", !config.guiEnforcesSecureConnection);
|
2018-10-10 22:45:49 +02:00
|
|
|
CPPUNIT_ASSERT_EQUAL_MESSAGE("url", QStringLiteral("http://localhost:4001"), config.syncthingUrl());
|
2017-03-09 23:06:03 +01:00
|
|
|
config.guiEnforcesSecureConnection = true;
|
2018-10-10 22:45:49 +02:00
|
|
|
CPPUNIT_ASSERT_EQUAL_MESSAGE("url", QStringLiteral("https://localhost:4001"), config.syncthingUrl());
|
2017-07-09 23:34:43 +02:00
|
|
|
const QString configFile(SyncthingConfig::locateConfigFile());
|
|
|
|
CPPUNIT_ASSERT(configFile.isEmpty() || QFile::exists(configFile));
|
|
|
|
const QString httpsCert(SyncthingConfig::locateHttpsCertificate());
|
|
|
|
CPPUNIT_ASSERT(httpsCert.isEmpty() || QFile::exists(httpsCert));
|
2017-03-09 23:06:03 +01:00
|
|
|
}
|
|
|
|
|
2018-04-18 23:27:45 +02:00
|
|
|
/*!
|
|
|
|
* \brief Test splitting arguments via SyncthingProcess::splitArguments().
|
|
|
|
*/
|
|
|
|
void MiscTests::testSplittingArguments()
|
|
|
|
{
|
2018-04-18 23:46:17 +02:00
|
|
|
CPPUNIT_ASSERT_EQUAL_MESSAGE("empty arguments", QStringList(), SyncthingProcess::splitArguments(QString()));
|
|
|
|
CPPUNIT_ASSERT_EQUAL_MESSAGE("one argument without special characters", QStringList({ QStringLiteral("-simple") }),
|
|
|
|
SyncthingProcess::splitArguments(QStringLiteral("-simple")));
|
|
|
|
CPPUNIT_ASSERT_EQUAL_MESSAGE("multiple arguments without special characters",
|
|
|
|
QStringList({ QStringLiteral("-home"), QStringLiteral("some dir"), QStringLiteral("-no-restart") }),
|
2018-04-18 23:27:45 +02:00
|
|
|
SyncthingProcess::splitArguments(QStringLiteral("-home \"some dir\" -no-restart")));
|
2018-04-18 23:46:17 +02:00
|
|
|
CPPUNIT_ASSERT_EQUAL_MESSAGE("quotation", QStringList({ QStringLiteral("-home"), QStringLiteral("some dir"), QStringLiteral("-no-restart") }),
|
|
|
|
SyncthingProcess::splitArguments(QStringLiteral(" -home \"some dir\" -no-restart ")));
|
|
|
|
CPPUNIT_ASSERT_EQUAL_MESSAGE("escaped quotation",
|
|
|
|
QStringList({ QStringLiteral("-home"), QStringLiteral("\"some"), QStringLiteral("dir\""), QStringLiteral("-no-restart") }),
|
2018-04-18 23:27:45 +02:00
|
|
|
SyncthingProcess::splitArguments(QStringLiteral("-home \\\"some dir\\\" -no-restart")));
|
2018-04-18 23:46:17 +02:00
|
|
|
CPPUNIT_ASSERT_EQUAL_MESSAGE("escaped spaces",
|
|
|
|
QStringList({ QStringLiteral("-home"), QStringLiteral("some dir"), QStringLiteral("-no-restart") }),
|
|
|
|
SyncthingProcess::splitArguments(QStringLiteral("-home \\ some\\ dir -no-restart")));
|
|
|
|
CPPUNIT_ASSERT_EQUAL_MESSAGE("spaces at the beginning through quotes", QStringList({ QStringLiteral("foo"), QStringLiteral(" bar") }),
|
|
|
|
SyncthingProcess::splitArguments(QStringLiteral("foo \" bar\"")));
|
|
|
|
CPPUNIT_ASSERT_EQUAL_MESSAGE("spaces at the end through quotes", QStringList({ QStringLiteral("-home"), QStringLiteral("-no-restart ") }),
|
|
|
|
SyncthingProcess::splitArguments(QStringLiteral("-home \"-no-restart \"")));
|
|
|
|
CPPUNIT_ASSERT_EQUAL_MESSAGE("don't care about missing quote at the end", QStringList({ QStringLiteral("foo"), QStringLiteral(" bar") }),
|
|
|
|
SyncthingProcess::splitArguments(QStringLiteral("foo \" bar")));
|
2018-04-18 23:27:45 +02:00
|
|
|
}
|
|
|
|
|
2017-07-09 23:34:43 +02:00
|
|
|
/*!
|
|
|
|
* \brief Tests utils.
|
|
|
|
*/
|
2017-03-09 23:06:03 +01:00
|
|
|
void MiscTests::testUtils()
|
|
|
|
{
|
|
|
|
CPPUNIT_ASSERT_EQUAL(QStringLiteral("right now"), agoString(DateTime::now()));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(QStringLiteral("5 h ago"), agoString(DateTime::now() - TimeSpan::fromHours(5.0)));
|
|
|
|
CPPUNIT_ASSERT(isLocal(QUrl(QStringLiteral("http://127.0.0.1"))));
|
|
|
|
CPPUNIT_ASSERT(isLocal(QUrl(QStringLiteral("http://[::1]"))));
|
|
|
|
CPPUNIT_ASSERT(isLocal(QUrl(QStringLiteral("http://localhost/"))));
|
|
|
|
CPPUNIT_ASSERT(!isLocal(QUrl(QStringLiteral("http://157.3.52.34"))));
|
|
|
|
}
|
2017-07-09 23:34:43 +02:00
|
|
|
|
2019-07-13 12:57:35 +02:00
|
|
|
#ifdef LIB_SYNCTHING_CONNECTOR_SUPPORT_SYSTEMD
|
2017-07-09 23:34:43 +02:00
|
|
|
/*!
|
|
|
|
* \brief Tests SyncthingService class, but error case with non-existant service so far.
|
|
|
|
*/
|
|
|
|
void MiscTests::testService()
|
|
|
|
{
|
|
|
|
SyncthingService service;
|
|
|
|
service.isSystemdAvailable();
|
|
|
|
service.setUnitName(QStringLiteral("non-existant.service"));
|
|
|
|
CPPUNIT_ASSERT(!service.isUnitAvailable());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(QString(), service.description());
|
|
|
|
CPPUNIT_ASSERT(!service.isRunning());
|
|
|
|
CPPUNIT_ASSERT(!service.isEnabled());
|
|
|
|
service.toggleRunning();
|
|
|
|
service.setEnabled(true);
|
|
|
|
}
|
2019-07-13 12:57:35 +02:00
|
|
|
#endif
|
2017-07-09 23:34:43 +02:00
|
|
|
|
|
|
|
void MiscTests::testConnectionSettingsAndLoadingSelfSignedCert()
|
|
|
|
{
|
|
|
|
SyncthingConnectionSettings settings;
|
|
|
|
settings.syncthingUrl = QStringLiteral("http://localhost:8080");
|
|
|
|
settings.apiKey = QByteArray("foo");
|
|
|
|
settings.httpsCertPath = SyncthingConfig::locateHttpsCertificate();
|
2017-07-11 23:04:56 +02:00
|
|
|
if (!settings.httpsCertPath.isEmpty() && settings.loadHttpsCert()) {
|
2017-07-09 23:34:43 +02:00
|
|
|
CPPUNIT_ASSERT_EQUAL(4, settings.expectedSslErrors.size());
|
|
|
|
} else {
|
|
|
|
CPPUNIT_ASSERT_EQUAL(0, settings.expectedSslErrors.size());
|
|
|
|
}
|
|
|
|
SyncthingConnection connection;
|
|
|
|
CPPUNIT_ASSERT(connection.applySettings(settings));
|
|
|
|
CPPUNIT_ASSERT(!connection.loadSelfSignedCertificate());
|
|
|
|
settings.syncthingUrl = QStringLiteral("https://localhost:8080");
|
|
|
|
CPPUNIT_ASSERT(connection.applySettings(settings));
|
|
|
|
connection.m_configDir = QStringLiteral("some-non/existent-dir");
|
|
|
|
bool errorOccured = false;
|
|
|
|
const function<void(const QString &)> errorHandler
|
|
|
|
= [&errorOccured](const QString &message) { errorOccured |= message == QStringLiteral("Unable to load certificate used by Syncthing."); };
|
|
|
|
waitForSignals(bind(&SyncthingConnection::loadSelfSignedCertificate, &connection), 1,
|
|
|
|
signalInfo(&connection, &SyncthingConnection::error, errorHandler, &errorOccured));
|
|
|
|
settings.expectedSslErrors.clear();
|
|
|
|
CPPUNIT_ASSERT(!connection.applySettings(settings));
|
|
|
|
}
|
|
|
|
|
|
|
|
void MiscTests::testSyncthingDir()
|
|
|
|
{
|
|
|
|
SyncthingDir dir;
|
2018-07-22 22:07:41 +02:00
|
|
|
dir.status = SyncthingDirStatus::Unknown;
|
2017-07-09 23:34:43 +02:00
|
|
|
|
|
|
|
DateTime updateTime(DateTime::fromDate(2005, 2, 3));
|
2018-07-22 22:07:41 +02:00
|
|
|
CPPUNIT_ASSERT(dir.assignStatus(SyncthingDirStatus::Idle, updateTime));
|
2017-07-09 23:34:43 +02:00
|
|
|
CPPUNIT_ASSERT_EQUAL(QStringLiteral("unshared"), dir.statusString());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(updateTime, dir.lastStatusUpdate);
|
|
|
|
|
2018-07-22 22:07:41 +02:00
|
|
|
dir.deviceIds << QStringLiteral("dev1") << QStringLiteral("dev2");
|
|
|
|
|
2017-07-09 23:34:43 +02:00
|
|
|
CPPUNIT_ASSERT(!dir.assignStatus(SyncthingDirStatus::Scanning, DateTime::fromDate(2003, 6, 7)));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(updateTime, dir.lastStatusUpdate);
|
2018-07-22 22:07:41 +02:00
|
|
|
CPPUNIT_ASSERT_EQUAL(QStringLiteral("idle"), dir.statusString());
|
2017-07-09 23:34:43 +02:00
|
|
|
|
|
|
|
const DateTime lastScanTime(DateTime::now());
|
|
|
|
updateTime += TimeSpan::fromSeconds(5);
|
|
|
|
CPPUNIT_ASSERT(dir.assignStatus(SyncthingDirStatus::Scanning, updateTime));
|
|
|
|
CPPUNIT_ASSERT(dir.lastScanTime.isNull());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(QStringLiteral("scanning"), dir.statusString());
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT(dir.assignStatus(SyncthingDirStatus::Idle, updateTime += TimeSpan::fromSeconds(2)));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(updateTime, dir.lastStatusUpdate);
|
|
|
|
CPPUNIT_ASSERT(dir.lastScanTime >= lastScanTime);
|
|
|
|
|
2018-11-02 23:39:15 +01:00
|
|
|
dir.status = SyncthingDirStatus::Unknown;
|
|
|
|
dir.lastSyncStarted = DateTime(1);
|
2017-07-14 20:14:15 +02:00
|
|
|
dir.itemErrors.emplace_back(QStringLiteral("message"), QStringLiteral("path"));
|
2017-07-09 23:34:43 +02:00
|
|
|
CPPUNIT_ASSERT(dir.assignStatus(SyncthingDirStatus::Idle, updateTime += TimeSpan::fromMinutes(1.5)));
|
2018-11-02 23:39:15 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(QStringLiteral("idle"), dir.statusString());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(1_st, dir.itemErrors.size());
|
|
|
|
dir.lastSyncStarted = DateTime();
|
2017-07-09 23:34:43 +02:00
|
|
|
CPPUNIT_ASSERT(!dir.assignStatus(SyncthingDirStatus::Idle, updateTime += TimeSpan::fromMinutes(1.5)));
|
2018-11-03 21:55:31 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(updateTime, dir.lastSyncStarted);
|
2018-11-02 23:39:15 +01:00
|
|
|
const auto lastSyncTime(updateTime += TimeSpan::fromMinutes(1.5));
|
|
|
|
CPPUNIT_ASSERT(dir.assignStatus(SyncthingDirStatus::Synchronizing, lastSyncTime));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(QStringLiteral("synchronizing"), dir.statusString());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(0_st, dir.itemErrors.size());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(lastSyncTime, dir.lastSyncStarted);
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT(dir.assignStatus(SyncthingDirStatus::Idle, updateTime += TimeSpan::fromMinutes(1.5)));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(lastSyncTime, dir.lastSyncStarted);
|
2017-07-09 23:34:43 +02:00
|
|
|
CPPUNIT_ASSERT(dir.assignStatus(QStringLiteral("syncing"), updateTime += TimeSpan::fromMinutes(1.5)));
|
2018-11-02 23:39:15 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(updateTime, dir.lastSyncStarted);
|
2017-07-09 23:34:43 +02:00
|
|
|
|
2017-07-14 20:14:15 +02:00
|
|
|
dir.itemErrors.clear();
|
2017-07-09 23:34:43 +02:00
|
|
|
CPPUNIT_ASSERT(dir.assignStatus(QStringLiteral("error"), updateTime += TimeSpan::fromMinutes(1.5)));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(QStringLiteral("out of sync"), dir.statusString());
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT(dir.assignStatus(QStringLiteral("wrong status"), updateTime += TimeSpan::fromMinutes(1.5)));
|
|
|
|
CPPUNIT_ASSERT_EQUAL_MESSAGE("wrong status treated as idle", QStringLiteral("idle"), dir.statusString());
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_MESSAGE("older status discarded", !dir.assignStatus(QStringLiteral("scanning"), updateTime - TimeSpan::fromSeconds(1)));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(QStringLiteral("idle"), dir.statusString());
|
|
|
|
|
2017-08-22 20:44:49 +02:00
|
|
|
dir.deviceIds.clear();
|
2018-11-02 23:39:15 +01:00
|
|
|
CPPUNIT_ASSERT(!dir.assignStatus(QStringLiteral("idle"), updateTime += TimeSpan::fromMinutes(1.5)));
|
2017-08-22 20:44:49 +02:00
|
|
|
CPPUNIT_ASSERT_EQUAL_MESSAGE("dir considered unshared when no devs present", QStringLiteral("unshared"), dir.statusString());
|
2017-07-09 23:34:43 +02:00
|
|
|
CPPUNIT_ASSERT(!dir.assignStatus(SyncthingDirStatus::Idle, updateTime += TimeSpan::fromMinutes(1.5)));
|
2017-08-22 20:44:49 +02:00
|
|
|
CPPUNIT_ASSERT_EQUAL_MESSAGE("dir considered unshared when no devs present", QStringLiteral("unshared"), dir.statusString());
|
2017-07-09 23:34:43 +02:00
|
|
|
|
|
|
|
updateTime += TimeSpan::fromMinutes(1.5);
|
|
|
|
CPPUNIT_ASSERT_MESSAGE("same status again not considered an update", !dir.assignStatus(QStringLiteral("idle"), updateTime));
|
|
|
|
}
|