Files
desktop/test/testupdater.cpp
Camila Ayres 0958c8ec85 fix(updater): allow a user initiated update check when auto updates are disabled.
The Sparkle delegate returned the background gate for every update check, so a
manual check for updates was blocked whenever autoUpdateCheck was disabled.
Background checks still require autoUpdateCheck.

Assisted-by: ClaudeCode:claude-opus-4-8
Signed-off-by: Camila Ayres <[email protected]>
2026-09-30 15:58:55 +02:00

194 lines
7.9 KiB
C++

/*
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2014 ownCloud, Inc.
* SPDX-License-Identifier: CC0-1.0
*
* This software is in the public domain, furnished "as is", without technical
* support, and with no warranty, express or implied, as to its usefulness for
* any purpose.
*/
#include <QtTest>
#include "common/filesystembase.h"
#include "updater/updater.h"
#include "updater/ocupdater.h"
#include "configfile.h"
#include "logger.h"
#include "filesystem.h"
using namespace Qt::StringLiterals;
#ifdef HAVE_QHTTPSERVER
#include <QHttpServer>
#include <QTcpServer>
#endif
using namespace OCC;
class TestUpdater : public QObject
{
Q_OBJECT
private Q_SLOTS:
void initTestCase()
{
OCC::Logger::instance()->setLogFlush(true);
OCC::Logger::instance()->setLogDebug(true);
QStandardPaths::setTestModeEnabled(true);
}
void testVersionToInt()
{
qint64 lowVersion = Updater::Helper::versionToInt(1,2,80,3000);
QCOMPARE(Updater::Helper::stringVersionToInt("1.2.80.3000"), lowVersion);
qint64 highVersion = Updater::Helper::versionToInt(99,2,80,3000);
qint64 currVersion = Updater::Helper::currentVersionToInt();
QVERIFY(currVersion > 0);
QVERIFY(currVersion > lowVersion);
QVERIFY(currVersion < highVersion);
}
void testUpdateCheckGating_data()
{
QTest::addColumn<bool>("userInitiated");
QTest::addColumn<bool>("skipUpdateCheck");
QTest::addColumn<bool>("autoUpdateCheck");
QTest::addColumn<bool>("allowed");
QTest::newRow("skip blocks a user check") << true << true << true << false;
QTest::newRow("skip blocks a background check") << false << true << true << false;
QTest::newRow("user check allowed with auto off") << true << false << false << true;
QTest::newRow("user check allowed with auto on") << true << false << true << true;
QTest::newRow("background allowed with auto on") << false << false << true << true;
QTest::newRow("background blocked with auto off") << false << false << false << false;
}
void testUpdateCheckGating()
{
QFETCH(bool, userInitiated);
QFETCH(bool, skipUpdateCheck);
QFETCH(bool, autoUpdateCheck);
QFETCH(bool, allowed);
QCOMPARE(Updater::updateCheckAllowed(userInitiated, skipUpdateCheck, autoUpdateCheck), allowed);
}
// Reproduces #7009: on the first start of a newly installed version the
// leftover installer must be gone. Application::configVersionMigration()
// (src/gui/application.cpp:161-164) drops the Updater/* keys before
// main() calls handleStartup() (src/gui/main.cpp:104 vs 142), so the
// cleanup in NSISUpdater::handleStartup() never sees the artifact.
void testLeftoverInstallerIsRemovedAfterVersionChange()
{
QTemporaryDir tempDir;
QVERIFY(tempDir.isValid());
QVERIFY(ConfigFile::setConfDir(tempDir.path()));
ConfigFile cfg;
const auto writeFile = [](const QString &path, const QByteArray &contents) {
QFile f(path);
return f.open(QIODevice::WriteOnly) && f.write(contents) == contents.size();
};
// a previous run downloaded an installer and msiexec left its log behind
const auto installer = FileSystem::joinPath(cfg.configPath(), "Nextcloud-1.0.0-x64.msi"_L1);
const auto msiLog = FileSystem::joinPath(cfg.configPath(), "msi.log"_L1);
QVERIFY(writeFile(installer, "this would be the installer"_ba));
QVERIFY(writeFile(msiLog, "this would be the msiexec log"_ba));
{
QSettings settings(cfg.configFile(), QSettings::IniFormat);
settings.setValue("Updater/updateAvailable"_L1, installer);
settings.setValue("Updater/updateTargetVersion"_L1, "1.0.0"_L1);
settings.setValue("Updater/updateTargetVersionString"_L1, "1.0.0"_L1);
settings.setValue("Updater/autoUpdateAttempted"_L1, true);
settings.sync();
}
// the update was installed: the running binary is newer than the target
QVERIFY(Updater::Helper::currentVersionToInt() > Updater::Helper::stringVersionToInt("1.0.0"_L1));
// what Application::configVersionMigration() does on that very first
// start, before the updater gets a chance to look at its own state
cfg.setClientVersionString("1.0.0"_L1);
cfg.cleanUpdaterConfiguration();
NSISUpdater updater(QUrl("http://localhost:1/updateinfo.xml"_L1));
updater.handleStartup();
QVERIFY(!QFileInfo::exists(installer));
QVERIFY(!QFileInfo::exists(msiLog));
}
#ifdef HAVE_QHTTPSERVER
void testUpdaterDownloadRedirect()
{
QTemporaryDir tempDir;
ConfigFile::setConfDir(tempDir.path()); // we don't want to pollute the user's config file
QVERIFY(tempDir.isValid());
QDir dir(tempDir.path());
// set up a download server that provides the version info and redirects the download request to e.g. some object storage
QHttpServer httpServer;
httpServer.route("/updateinfo.xml", [](const QHttpServerRequest &request, QHttpServerResponder &responder) -> void {
auto downloadTarget = request.url();
downloadTarget.setPath("/Nextcloud.msi");
qInfo() << "redirecting to" << downloadTarget;
QHttpHeaders headers;
headers.append(QHttpHeaders::WellKnownHeader::ContentType, "application/xml"_ba);
auto xmlResponse = "<?xml version=\"1.0\"?>\n<owncloudclient><version>600.0.0</version><versionstring>Nextcloud Client 600.0.0</versionstring><downloadurl>"_ba;
xmlResponse.append(downloadTarget.toEncoded());
xmlResponse.append("</downloadurl><web>https://nextcloud.com/install</web></owncloudclient>"_ba);
responder.write(xmlResponse, headers);
});
httpServer.route("/Nextcloud.msi", [](QHttpServerResponder &responder) -> void {
QHttpHeaders headers;
headers.append(QHttpHeaders::WellKnownHeader::Location, "/storage/blob/42?signature=1234abcd&signatureVersion=2026-01-21"_ba);
responder.write(""_ba, headers, QHttpServerResponder::StatusCode::Found);
});
bool redirectHit = false;
httpServer.route("/storage/blob/42", [&redirectHit](QHttpServerResponder &responder) -> void {
QHttpHeaders headers;
headers.append(QHttpHeaders::WellKnownHeader::ContentType, "application/octet-stream"_ba);
redirectHit = true;
responder.write("This would be the installer"_ba, headers);
});
QTcpServer tcpServer;
QVERIFY(tcpServer.listen(QHostAddress::LocalHost));
QVERIFY(httpServer.bind(&tcpServer));
const QString baseUrl = "http://%1:%2"_L1.arg(tcpServer.serverAddress().toString(), QString::number(tcpServer.serverPort()));
qInfo() << "Listening on" << baseUrl;
NSISUpdater updater(QUrl("%1/updateinfo.xml"_L1.arg(baseUrl)));
QSignalSpy downloadAvailableSpy(&updater, &OCUpdater::newUpdateAvailable);
updater.checkForUpdate();
downloadAvailableSpy.wait();
QCOMPARE(downloadAvailableSpy.size(), 1);
QVERIFY(redirectHit);
ConfigFile cfg;
QSettings settings(cfg.configFile(), QSettings::IniFormat);
const auto downloadedUpdateFilePath = settings.value("Updater/updateAvailable"_L1).toString(); // anonymous const in ocupdater.cpp
const auto expectedUpdateFilePath = FileSystem::joinPath(cfg.configPath(), "Nextcloud.msi");
QCOMPARE(downloadedUpdateFilePath, expectedUpdateFilePath);
QFile updateFile(expectedUpdateFilePath);
QVERIFY(updateFile.open(QIODevice::ReadOnly));
const auto updateContents = updateFile.readAll();
updateFile.close();
QCOMPARE(updateContents, "This would be the installer"_ba);
}
#endif
};
QTEST_GUILESS_MAIN(TestUpdater)
#include "testupdater.moc"