1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
|
#include "../syncthingconfig.h"
#include "../syncthingconnection.h"
#include "../syncthingconnectionsettings.h"
#include "../syncthingprocess.h"
#include "../syncthingservice.h"
#include "../utils.h"
#include <c++utilities/chrono/datetime.h>
#include <c++utilities/chrono/format.h>
#include <c++utilities/chrono/timespan.h>
#include <c++utilities/tests/testutils.h>
#include "../../testhelper/helper.h"
#include <cppunit/TestFixture.h>
#include <QFile>
#include <QUrl>
#include <iostream>
using namespace std;
using namespace Data;
using namespace CppUtilities;
using namespace CppUtilities::Literals;
using namespace CPPUNIT_NS;
/*!
* \brief The MiscTests class tests various features of the connector library.
*/
class MiscTests : public TestFixture {
CPPUNIT_TEST_SUITE(MiscTests);
CPPUNIT_TEST(testParsingConfig);
CPPUNIT_TEST(testParsingConfigWithDetails);
CPPUNIT_TEST(testSplittingArguments);
CPPUNIT_TEST(testUtils);
#ifdef LIB_SYNCTHING_CONNECTOR_SUPPORT_SYSTEMD
CPPUNIT_TEST(testService);
#endif
CPPUNIT_TEST(testConnectionSettingsAndLoadingSelfSignedCert);
CPPUNIT_TEST(testSyncthingDir);
CPPUNIT_TEST_SUITE_END();
public:
MiscTests();
void testParsingConfig();
void testParsingConfigWithDetails();
void testSplittingArguments();
void testUtils();
#ifdef LIB_SYNCTHING_CONNECTOR_SUPPORT_SYSTEMD
void testService();
#endif
#ifndef QT_NO_SSL
void testConnectionSettingsAndLoadingSelfSignedCert();
#endif
void testSyncthingDir();
void setUp() override;
void tearDown() override;
private:
};
CPPUNIT_TEST_SUITE_REGISTRATION(MiscTests);
MiscTests::MiscTests()
{
}
//
// test setup
//
void MiscTests::setUp()
{
}
void MiscTests::tearDown()
{
}
//
// actual test
//
/*!
* \brief Tests basic behaviour of the SyncthingConnection class.
*/
void MiscTests::testParsingConfig()
{
auto config = SyncthingConfig();
CPPUNIT_ASSERT(!config.restore(QStringLiteral("non-existant-file")));
CPPUNIT_ASSERT(config.restore(QString::fromLocal8Bit(testFilePath("testconfig/config.xml").data())));
CPPUNIT_ASSERT_EQUAL_MESSAGE("address", QStringLiteral("127.0.0.1:4001"), config.guiAddress);
CPPUNIT_ASSERT_EQUAL_MESSAGE("API key", QStringLiteral("syncthingconnectortest"), config.guiApiKey);
CPPUNIT_ASSERT_EQUAL_MESSAGE("user", QStringLiteral("nobody"), config.guiUser);
CPPUNIT_ASSERT_EQUAL_MESSAGE("password", QStringLiteral("$2a$12$35MnbsQgQNn1hzPYK/lWXOaP.U5D2TO0nuuQy2M4gsqJB4ff4q2RK"), config.guiPasswordHash);
CPPUNIT_ASSERT_MESSAGE("TLS", !config.guiEnforcesSecureConnection);
CPPUNIT_ASSERT_EQUAL_MESSAGE("url", QStringLiteral("http://127.0.0.1:4001"), config.syncthingUrl());
config.guiEnforcesSecureConnection = true;
CPPUNIT_ASSERT_EQUAL_MESSAGE("url", QStringLiteral("https://127.0.0.1:4001"), config.syncthingUrl());
const QString configFile(SyncthingConfig::locateConfigFile());
CPPUNIT_ASSERT(configFile.isEmpty() || QFile::exists(configFile));
const QString httpsCert(SyncthingConfig::locateHttpsCertificate());
CPPUNIT_ASSERT(httpsCert.isEmpty() || QFile::exists(httpsCert));
}
/*!
* \brief Tests basic behaviour of the SyncthingConnection class including parsing of folders and devices.
*/
void MiscTests::testParsingConfigWithDetails()
{
auto config = SyncthingConfig();
CPPUNIT_ASSERT(config.restore(QString::fromLocal8Bit(testFilePath("testconfig/config.xml").data()), true));
CPPUNIT_ASSERT(config.details.has_value());
const auto folders = config.details.value().folders;
CPPUNIT_ASSERT_EQUAL(static_cast<QJsonArray::size_type>(2), folders.size());
const auto folder1 = folders.at(0).toObject();
CPPUNIT_ASSERT_EQUAL(QStringLiteral("test1"), folder1.value(QLatin1String("id")).toString());
CPPUNIT_ASSERT_EQUAL(QStringLiteral("/tmp/some/path/1/"), folder1.value(QLatin1String("path")).toString());
CPPUNIT_ASSERT_EQUAL(7200.0, folder1.value(QLatin1String("rescanIntervalS")).toDouble());
CPPUNIT_ASSERT_EQUAL(QJsonValue::Bool, folder1.value(QLatin1String("fsWatcherEnabled")).type());
CPPUNIT_ASSERT_EQUAL(false, folder1.value(QLatin1String("fsWatcherEnabled")).toBool());
CPPUNIT_ASSERT_EQUAL(QJsonValue::Bool, folder1.value(QLatin1String("autoNormalize")).type());
CPPUNIT_ASSERT_EQUAL(true, folder1.value(QLatin1String("autoNormalize")).toBool());
CPPUNIT_ASSERT_EQUAL(QStringLiteral("basic"), folder1.value(QLatin1String("filesystemType")).toString());
const auto folder1Devs = folder1.value(QStringLiteral("devices")).toArray();
CPPUNIT_ASSERT_EQUAL(static_cast<QJsonArray::size_type>(2), folder1Devs.size());
CPPUNIT_ASSERT_EQUAL(QStringLiteral("MMGUI6U-WUEZQCP-XZZ6VYB-LCT4TVC-ER2HAVX-QYT6X7D-S6ZSG2B-323KLQ7"),
folder1Devs.at(0).toObject().value(QLatin1String("deviceID")).toString());
CPPUNIT_ASSERT_EQUAL(QStringLiteral("6EIS2PN-J2IHWGS-AXS3YUL-HC5FT3K-77ZXTLL-AKQLJ4C-7SWVPUS-AZW4RQ4"),
folder1Devs.at(1).toObject().value(QLatin1String("deviceID")).toString());
const auto folder2 = folders.at(1).toObject();
CPPUNIT_ASSERT_EQUAL(QStringLiteral("test2"), folder2.value(QLatin1String("id")).toString());
const auto folder2Devs = folder2.value(QStringLiteral("devices")).toArray();
CPPUNIT_ASSERT_EQUAL(static_cast<QJsonArray::size_type>(1), folder2Devs.size());
CPPUNIT_ASSERT_EQUAL(QStringLiteral("MMGUI6U-WUEZQCP-XZZ6VYB-LCT4TVC-ER2HAVX-QYT6X7D-S6ZSG2B-323KLQ7"),
folder2Devs.at(0).toObject().value(QLatin1String("deviceID")).toString());
const auto devices = config.details.value().devices;
CPPUNIT_ASSERT_EQUAL(static_cast<QJsonArray::size_type>(2), devices.size());
const auto device1 = devices.at(0).toObject();
const auto device1Addresses = device1.value(QLatin1String("addresses")).toArray();
CPPUNIT_ASSERT_EQUAL(
QStringLiteral("MMGUI6U-WUEZQCP-XZZ6VYB-LCT4TVC-ER2HAVX-QYT6X7D-S6ZSG2B-323KLQ7"), device1.value(QLatin1String("deviceID")).toString());
CPPUNIT_ASSERT_EQUAL(static_cast<QJsonArray::size_type>(2), device1Addresses.size());
CPPUNIT_ASSERT_EQUAL(QStringLiteral("tcp://192.168.2.2:22001"), device1Addresses.first().toString());
CPPUNIT_ASSERT_EQUAL(QStringLiteral("tcp://192.168.2.2:22002"), device1Addresses.last().toString());
CPPUNIT_ASSERT_EQUAL(false, device1.contains(QStringLiteral("address")));
CPPUNIT_ASSERT_EQUAL(true, device1.value(QLatin1String("paused")).toBool());
const auto device2 = devices.at(1).toObject();
const auto device2Addresses = device2.value(QLatin1String("addresses")).toArray();
CPPUNIT_ASSERT_EQUAL(
QStringLiteral("6EIS2PN-J2IHWGS-AXS3YUL-HC5FT3K-77ZXTLL-AKQLJ4C-7SWVPUS-AZW4RQ4"), device2.value(QLatin1String("deviceID")).toString());
CPPUNIT_ASSERT_EQUAL(static_cast<QJsonArray::size_type>(1), device2Addresses.size());
CPPUNIT_ASSERT_EQUAL(QStringLiteral("dynamic"), device2Addresses.first().toString());
CPPUNIT_ASSERT_EQUAL(false, device2.contains(QStringLiteral("address")));
CPPUNIT_ASSERT_EQUAL(false, device2.value(QLatin1String("paused")).toBool());
}
/*!
* \brief Test splitting arguments via SyncthingProcess::splitArguments().
*/
void MiscTests::testSplittingArguments()
{
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") }),
SyncthingProcess::splitArguments(QStringLiteral("-home \"some dir\" -no-restart")));
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") }),
SyncthingProcess::splitArguments(QStringLiteral("-home \\\"some dir\\\" -no-restart")));
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")));
}
/*!
* \brief Tests utils.
*/
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"))));
CPPUNIT_ASSERT_EQUAL(
QStringLiteral("/some/path"), substituteTilde(QStringLiteral("/some/path"), QStringLiteral("/home/foo"), QStringLiteral("/")));
CPPUNIT_ASSERT_EQUAL(
QStringLiteral("/home/foo/some/path"), substituteTilde(QStringLiteral("~/some/path"), QStringLiteral("/home/foo"), QStringLiteral("/")));
CPPUNIT_ASSERT_EQUAL(
QStringLiteral("~bar/some/path"), substituteTilde(QStringLiteral("~bar/some/path"), QStringLiteral("/home/foo"), QStringLiteral("/")));
CPPUNIT_ASSERT_EQUAL(QStringLiteral("/home/foobar/some/path"),
substituteTilde(QStringLiteral("~bar/some/path"), QStringLiteral("/home/foo"), QStringLiteral("bar/")));
CPPUNIT_ASSERT_EQUAL(QStringLiteral("/home/foo"), substituteTilde(QStringLiteral("~"), QStringLiteral("/home/foo"), QStringLiteral("\\")));
}
#ifdef LIB_SYNCTHING_CONNECTOR_SUPPORT_SYSTEMD
/*!
* \brief Tests SyncthingService class, but only error cases with a non-existent service so far.
*/
void MiscTests::testService()
{
SyncthingService service;
service.isSystemdAvailable();
service.setUnitName(QStringLiteral("non-existent.service"));
CPPUNIT_ASSERT(!service.isUnitAvailable());
CPPUNIT_ASSERT_EQUAL(QString(), service.description());
CPPUNIT_ASSERT(!service.isRunning());
CPPUNIT_ASSERT(!service.isEnabled());
service.toggleRunning();
service.setEnabled(true);
}
#endif
#ifndef QT_NO_SSL
void MiscTests::testConnectionSettingsAndLoadingSelfSignedCert()
{
SyncthingConnectionSettings settings;
settings.syncthingUrl = QStringLiteral("http://localhost:8080");
settings.apiKey = QByteArrayLiteral("foo");
settings.httpsCertPath = SyncthingConfig::locateHttpsCertificate();
if (!settings.httpsCertPath.isEmpty() && settings.loadHttpsCert()) {
CPPUNIT_ASSERT_GREATER(static_cast<decltype(settings.expectedSslErrors.size())>(0), settings.expectedSslErrors.size());
} else {
CPPUNIT_ASSERT_EQUAL(static_cast<decltype(settings.expectedSslErrors.size())>(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");
connection.clearSelfSignedCertificate();
const auto expectedErrorMessage = QStringLiteral("Unable to load certificate used by Syncthing.");
auto expectedErrorOccured = false;
const function<void(const QString &)> errorHandler = [&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));
}
#endif
void MiscTests::testSyncthingDir()
{
SyncthingDir dir;
dir.status = SyncthingDirStatus::Unknown;
auto updateEvent = static_cast<SyncthingEventId>(42);
auto updateTime = DateTime(DateTime::fromDate(2005, 2, 3));
CPPUNIT_ASSERT(dir.assignStatus(SyncthingDirStatus::Idle, updateEvent, updateTime));
CPPUNIT_ASSERT_EQUAL_MESSAGE("status updated", QStringLiteral("Unshared"), dir.statusString());
CPPUNIT_ASSERT_EQUAL_MESSAGE("event updated", updateEvent, dir.lastStatusUpdateEvent);
CPPUNIT_ASSERT_EQUAL_MESSAGE("time updated", updateTime, dir.lastStatusUpdateTime);
dir.deviceIds << QStringLiteral("dev1") << QStringLiteral("dev2");
CPPUNIT_ASSERT(!dir.assignStatus(SyncthingDirStatus::Scanning, updateEvent - 1, updateTime + TimeSpan::fromDays(1.0)));
CPPUNIT_ASSERT_EQUAL_MESSAGE("status not updated", QStringLiteral("Up to Date"), dir.statusString());
CPPUNIT_ASSERT_EQUAL_MESSAGE("event not updated", updateEvent, dir.lastStatusUpdateEvent);
CPPUNIT_ASSERT_EQUAL_MESSAGE("time not updated", updateTime, dir.lastStatusUpdateTime);
const auto lastScanTime = DateTime(DateTime::now());
CPPUNIT_ASSERT(dir.assignStatus(SyncthingDirStatus::WaitingToScan, updateEvent += 1, updateTime += TimeSpan::fromSeconds(5)));
CPPUNIT_ASSERT(dir.lastScanTime.isNull());
CPPUNIT_ASSERT_EQUAL(QStringLiteral("Waiting to Scan"), dir.statusString());
CPPUNIT_ASSERT(dir.assignStatus(SyncthingDirStatus::Scanning, updateEvent += 1, updateTime += TimeSpan::fromSeconds(5)));
CPPUNIT_ASSERT(dir.lastScanTime.isNull());
CPPUNIT_ASSERT_EQUAL(QStringLiteral("Scanning"), dir.statusString());
CPPUNIT_ASSERT(dir.assignStatus(SyncthingDirStatus::Idle, updateEvent += 1, updateTime += TimeSpan::fromSeconds(2)));
CPPUNIT_ASSERT_EQUAL_MESSAGE("event updated", updateEvent, dir.lastStatusUpdateEvent);
CPPUNIT_ASSERT_EQUAL_MESSAGE("time updated", updateTime, dir.lastStatusUpdateTime);
CPPUNIT_ASSERT(dir.lastScanTime >= lastScanTime);
dir.status = SyncthingDirStatus::Unknown;
dir.lastSyncStartedTime = DateTime(1);
dir.itemErrors.emplace_back(QStringLiteral("message"), QStringLiteral("path"));
CPPUNIT_ASSERT(dir.assignStatus(SyncthingDirStatus::Idle, updateEvent += 1, updateTime += TimeSpan::fromMinutes(1.5)));
CPPUNIT_ASSERT_EQUAL(QStringLiteral("Up to Date"), dir.statusString());
CPPUNIT_ASSERT_EQUAL(1_st, dir.itemErrors.size());
dir.lastSyncStartedTime = DateTime();
CPPUNIT_ASSERT(!dir.assignStatus(SyncthingDirStatus::Idle, updateEvent += 1, updateTime += TimeSpan::fromMinutes(1.5)));
CPPUNIT_ASSERT_EQUAL(updateTime, dir.lastSyncStartedTime);
const auto lastSyncTime = updateTime += TimeSpan::fromMinutes(1.5);
dir.itemErrors.emplace_back();
CPPUNIT_ASSERT(dir.assignStatus(SyncthingDirStatus::Synchronizing, updateEvent += 1, lastSyncTime));
CPPUNIT_ASSERT_EQUAL(QStringLiteral("Syncing"), dir.statusString());
CPPUNIT_ASSERT_EQUAL(0_st, dir.itemErrors.size());
CPPUNIT_ASSERT_EQUAL(lastSyncTime, dir.lastSyncStartedTime);
const auto lastSyncTime2 = updateTime += TimeSpan::fromMinutes(2.0);
dir.itemErrors.emplace_back();
CPPUNIT_ASSERT(dir.assignStatus(SyncthingDirStatus::PreparingToSync, updateEvent += 1, lastSyncTime2));
CPPUNIT_ASSERT_EQUAL(QStringLiteral("Preparing to Sync"), dir.statusString());
CPPUNIT_ASSERT_EQUAL(0_st, dir.itemErrors.size());
CPPUNIT_ASSERT_EQUAL(lastSyncTime2, dir.lastSyncStartedTime);
CPPUNIT_ASSERT(dir.assignStatus(SyncthingDirStatus::Idle, updateEvent += 1, updateTime += TimeSpan::fromMinutes(1.5)));
CPPUNIT_ASSERT_EQUAL(lastSyncTime2, dir.lastSyncStartedTime);
CPPUNIT_ASSERT(dir.assignStatus(QStringLiteral("syncing"), updateEvent += 1, updateTime += TimeSpan::fromMinutes(1.5)));
CPPUNIT_ASSERT_EQUAL(updateTime, dir.lastSyncStartedTime);
dir.itemErrors.clear();
CPPUNIT_ASSERT(dir.assignStatus(QStringLiteral("error"), updateEvent += 1, updateTime += TimeSpan::fromMinutes(1.5)));
CPPUNIT_ASSERT_EQUAL(QStringLiteral("Out of Sync"), dir.statusString());
CPPUNIT_ASSERT_MESSAGE("older status discarded", !dir.assignStatus(QStringLiteral("scanning"), updateEvent - 1, updateTime));
CPPUNIT_ASSERT_EQUAL(QStringLiteral("Out of Sync"), dir.statusString());
dir.deviceIds.clear();
CPPUNIT_ASSERT(dir.assignStatus(QStringLiteral("idle"), updateEvent += 1, updateTime += TimeSpan::fromMinutes(1.5)));
CPPUNIT_ASSERT_EQUAL_MESSAGE("dir considered unshared when no devs present", QStringLiteral("Unshared"), dir.statusString());
CPPUNIT_ASSERT(!dir.assignStatus(SyncthingDirStatus::Idle, updateEvent += 1, updateTime += TimeSpan::fromMinutes(1.5)));
CPPUNIT_ASSERT_EQUAL_MESSAGE("dir considered unshared when no devs present", QStringLiteral("Unshared"), dir.statusString());
CPPUNIT_ASSERT_MESSAGE("same status again not considered an update",
!dir.assignStatus(QStringLiteral("idle"), updateEvent += 1, updateTime += TimeSpan::fromMinutes(1.5)));
}
|