Debian Codegraph @

tests/setup.cpp

raw symbols SCIP
1 #include "../setup/updater.h"
2
3 #include "../misc/disablewarningsmoc.h"
4
5 #include "resources/config.h"
6
7 #include <c++utilities/application/argumentparser.h>
8 #include <c++utilities/conversion/conversionexception.h>
9 #include <c++utilities/conversion/stringconversion.h>
10 #include <c++utilities/io/misc.h>
11
12 #include <QEventLoop>
13 #include <QNetworkAccessManager>
14 #include <QNetworkDiskCache>
15 #include <QSettings>
16 #include <QTimer>
17
18 #include <QtTest/QtTest>
19
20 #include <c++utilities/tests/testutils.h>
21
22 using namespace QtUtilities;
23
24 class SetupTests : public QObject {
25 Q_OBJECT
26
27 private Q_SLOTS:
28 void initTestCase();
29 void testUpdateNotifierReleaseChecking();
30 void testUpdateNotifierReleaseDistinction();
31 void testVersionComparison();
32
33 private:
34 CppUtilities::TestApplication m_app;
35 double m_timeoutFactor = 1.0;
36 };
37
38 void SetupTests::initTestCase()
39 {
40 CppUtilities::applicationInfo.url = "https://github.com/Martchus/syncthingtray";
41 CppUtilities::applicationInfo.version = "1.4.0";
42
43 if (!qEnvironmentVariableIsSet(PROJECT_VARNAME_UPPER "_UPDATER_VERBOSE")) {
44 qputenv(PROJECT_VARNAME_UPPER "_UPDATER_VERBOSE", "1");
45 }
46
47 if (const auto timeoutFactorEnv = qgetenv(PROJECT_VARNAME_UPPER "_TEST_TIMEOUT_FACTOR"); !timeoutFactorEnv.isEmpty()) {
48 try {
49 m_timeoutFactor = CppUtilities::stringToNumber<double>(timeoutFactorEnv.data());
50 qDebug() << "using timeout factor: " << m_timeoutFactor;
51 } catch (const CppUtilities::ConversionException &) {
52 qDebug() << "ignoring invalid " PROJECT_VARNAME_UPPER "_TEST_TIMEOUT_FACTOR";
53 }
54 }
55 }
56
57 void SetupTests::testUpdateNotifierReleaseChecking()
58 {
59 auto settings = QSettings();
60 auto networkAccessManager = QNetworkAccessManager();
61 auto *const diskCache = new QNetworkDiskCache(&networkAccessManager);
62 auto cacheDirectory = CppUtilities::testDirPath("setup");
63 diskCache->setCacheDirectory(QString::fromLocal8Bit(cacheDirectory.data(), static_cast<QString::size_type>(cacheDirectory.size())));
64 networkAccessManager.setCache(diskCache);
65 auto updateHandler = UpdateHandler(&settings, &networkAccessManager);
66 auto &updateNotifier = *updateHandler.notifier();
67 if (!updateNotifier.isSupported()) {
68 qDebug() << "skipping: UpdateNotifier() is not supported";
69 return;
70 }
71 if (qEnvironmentVariableIntValue(PROJECT_VARNAME_UPPER "_TEST_ONLINE")) {
72 updateHandler.setCacheLoadControl(QNetworkRequest::AlwaysNetwork);
73 } else {
74 updateHandler.setCacheLoadControl(QNetworkRequest::AlwaysCache);
75 }
76
77 auto eventLoop = QEventLoop();
78 auto timeout = QTimer();
79 timeout.setInterval(static_cast<int>(5000 * m_timeoutFactor));
80 timeout.setSingleShot(true);
81
82 auto newVersionFromSignal = QString();
83 updateNotifier.checkForUpdate();
84
85 QObject::connect(&updateNotifier, &UpdateNotifier::inProgressChanged, &eventLoop, &QEventLoop::quit);
86 QObject::connect(&updateNotifier, &UpdateNotifier::updateAvailable, &updateNotifier,
87 [&newVersionFromSignal](const QString &version, const QString &additionalInfo) {
88 Q_UNUSED(additionalInfo)
89 newVersionFromSignal = version;
90 });
91 QObject::connect(&timeout, &QTimer::timeout, &eventLoop, &QEventLoop::quit);
92
93 timeout.start();
94 eventLoop.exec();
95 timeout.stop();
96
97 QCOMPARE(updateNotifier.error(), QString());
98 qDebug() << "download URL: " << updateNotifier.downloadUrl();
99 QCOMPARE(updateNotifier.downloadUrl().host(), QStringLiteral("github.com"));
100 QVERIFY2(updateNotifier.downloadUrl().path().startsWith(QLatin1String("/Martchus/syncthingtray/releases/download/")),
101 "download URL contains expected path");
102 QCOMPARE(updateNotifier.signatureUrl().path(), updateNotifier.downloadUrl().path() + QStringLiteral(".sig"));
103 QVERIFY2(updateNotifier.isUpdateAvailable(), "update considered available");
104 QVERIFY2(!updateNotifier.isInProgress(), "not timed out");
105 QVERIFY2(!updateNotifier.newVersion().isEmpty(), "new version assigned");
106 QVERIFY2(!newVersionFromSignal.isEmpty(), "updateAvailable() was emitted with non-empty version");
107 QCOMPARE(updateNotifier.newVersion(), newVersionFromSignal);
108 QVERIFY2(!QVersionNumber::fromString(newVersionFromSignal).isNull(), "version is parsable");
109 }
110
111 void SetupTests::testUpdateNotifierReleaseDistinction()
112 {
113 auto settings = QSettings();
114 auto updateNotifier = UpdateNotifier();
115 if (!updateNotifier.isSupported()) {
116 qDebug() << "skipping: UpdateNotifier() is not supported";
117 return;
118 }
119 using namespace CppUtilities;
120 const auto jsonData = QByteArray::fromStdString(readFile(testFilePath("setup/releases.json")));
121
122 // only regular release considered by default
123 updateNotifier.supplyNewReleaseData(jsonData);
124 QCOMPARE(updateNotifier.error(), QString());
125 QCOMPARE(updateNotifier.newVersion(), QStringLiteral("1.7.6"));
126
127 // pre-release considered if flags set acordingly
128 // rc2 wins over rc1 and beta1
129 updateNotifier.setFlags(UpdateCheckFlags::IncludePreReleases);
130 updateNotifier.supplyNewReleaseData(jsonData);
131 QCOMPARE(updateNotifier.error(), QString());
132 QCOMPARE(updateNotifier.newVersion(), QStringLiteral("1.7.7-rc2"));
133
134 // draft release considered if flags set accordingly
135 // regular release tag wins over alpha1
136 updateNotifier.setFlags(UpdateCheckFlags::IncludeDrafts | UpdateCheckFlags::IncludePreReleases);
137 updateNotifier.supplyNewReleaseData(jsonData);
138 QCOMPARE(updateNotifier.error(), QString());
139 QCOMPARE(updateNotifier.newVersion(), QStringLiteral("1.8.0"));
140 }
141
142 void SetupTests::testVersionComparison()
143 {
144 #ifdef QT_UTILITIES_SETUP_TOOLS_ENABLED
145 QVERIFY2(UpdateNotifier::isVersionHigher(QStringLiteral("2.0.8"), QStringLiteral("2.0.7")), "only simple versions");
146 QVERIFY2(!UpdateNotifier::isVersionHigher(QStringLiteral("2.0.7"), QStringLiteral("2.0.8")), "only simple versions (reversed)");
147
148 QVERIFY2(UpdateNotifier::isVersionHigher(QStringLiteral("2.0.8"), QStringLiteral("2.0.7-beta1")), "simple version and suffixed-version");
149 QVERIFY2(UpdateNotifier::isVersionHigher(QStringLiteral("2.0.8-beta1"), QStringLiteral("2.0.7")), "suffixed-version and simple version");
150
151 QVERIFY2(UpdateNotifier::isVersionHigher(QStringLiteral("2.0.8"), QStringLiteral("2.0.8-beta1")), "suffixed-version considered older");
152 QVERIFY2(!UpdateNotifier::isVersionHigher(QStringLiteral("2.0.8"), QStringLiteral("2.0.8-3224.493f60f2")), "invalid suffixed-version ignored");
153 QVERIFY2(
154 !UpdateNotifier::isVersionHigher(QStringLiteral("2.0.8-beta1"), QStringLiteral("2.0.8")), "suffixed-version considered older (reversed)");
155
156 QVERIFY2(!UpdateNotifier::isVersionHigher(QStringLiteral("2.0.8"), QStringLiteral("2.0.8")), "equality with to simple versions");
157 QVERIFY2(!UpdateNotifier::isVersionHigher(QStringLiteral("2.0.8-beta1"), QStringLiteral("2.0.8-beta1")), "equality with two suffixed versions");
158 QVERIFY2(
159 UpdateNotifier::isVersionHigher(QStringLiteral("2.0.8-beta2"), QStringLiteral("2.0.8-beta1")), "higher version in suffix considered newer");
160
161 QVERIFY2(UpdateNotifier::isVersionHigher(QStringLiteral("2.0.8-beta1"), QStringLiteral("2.0.8-alpha2")), "beta considered newer than alpha");
162 QVERIFY2(UpdateNotifier::isVersionHigher(QStringLiteral("2.0.8-rc1"), QStringLiteral("2.0.8-beta2")), "rc concidered newer than beta");
163 #endif
164 }
165
166 QT_UTILITIES_DISABLE_WARNINGS_FOR_MOC_INCLUDE
167 QTEST_MAIN(SetupTests)
168 #include "setup.moc"