A minimal Arch system has no cmake, no compiler and no qt6-base, so the self-update used to fail or fall back to the releases page. The update now asks pacman what is missing, installs it through sudo with the password typed in the update dialog, then resumes the build. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
418 lines
17 KiB
C++
418 lines
17 KiB
C++
// Unit tests for UpdateManager: version comparison, the update-strategy choice
|
|
// (pure logic), and the staged install script (run for real against a running
|
|
// binary, no network or toolchain needed).
|
|
#include <cassert>
|
|
#include <iostream>
|
|
|
|
#include <QDir>
|
|
#include <QFile>
|
|
#include <QFileInfo>
|
|
#include <QProcess>
|
|
#include <QProcessEnvironment>
|
|
#include <QString>
|
|
#include <QTemporaryDir>
|
|
#include <QtGlobal>
|
|
|
|
#include "update-manager.h"
|
|
|
|
// Defined by CMake from the repo-root `version` file; empty when the test is
|
|
// built without it.
|
|
#ifndef FWSPACMANAGE_VERSION
|
|
#define FWSPACMANAGE_VERSION ""
|
|
#endif
|
|
|
|
static int failures = 0;
|
|
|
|
static void check(bool cond, const char* name) {
|
|
if (cond) {
|
|
std::cout << "PASS " << name << std::endl;
|
|
} else {
|
|
std::cout << "FAIL " << name << std::endl;
|
|
++failures;
|
|
}
|
|
}
|
|
|
|
static bool writeFile(const QString& path, const QByteArray& content) {
|
|
QFile f(path);
|
|
if (!f.open(QIODevice::WriteOnly)) return false;
|
|
return f.write(content) == content.size();
|
|
}
|
|
|
|
// Byte-for-byte comparison: enough to tell a replaced file from the original.
|
|
static bool sameFileContent(const QString& a, const QString& b) {
|
|
QFile fa(a);
|
|
QFile fb(b);
|
|
if (!fa.open(QIODevice::ReadOnly) || !fb.open(QIODevice::ReadOnly)) return false;
|
|
return fa.readAll() == fb.readAll();
|
|
}
|
|
|
|
using Caps = UpdateManager::UpdateCapabilities;
|
|
using Method = UpdateManager::UpdateMethod;
|
|
|
|
static void testChooseMethod_checkoutWinsOverEverything() {
|
|
Caps caps;
|
|
caps.checkoutUsable = true;
|
|
caps.hasGit = caps.hasCmake = caps.hasCompiler = true;
|
|
caps.hasPrebuilt = caps.hasTarball = true;
|
|
check(UpdateManager::chooseUpdateMethod(caps) == Method::Checkout,
|
|
"method_checkout_wins");
|
|
}
|
|
|
|
static void testChooseMethod_cloneWhenToolchainPresent() {
|
|
Caps caps;
|
|
caps.hasGit = caps.hasCmake = caps.hasCompiler = true;
|
|
caps.hasPrebuilt = caps.hasTarball = true;
|
|
// The clone path is deliberately preferred over the prebuilt asset.
|
|
check(UpdateManager::chooseUpdateMethod(caps) == Method::Clone,
|
|
"method_clone_first");
|
|
}
|
|
|
|
static void testChooseMethod_cloneRequiresCompiler() {
|
|
Caps caps;
|
|
caps.hasGit = caps.hasCmake = true; // no compiler -> cannot rebuild
|
|
check(UpdateManager::chooseUpdateMethod(caps) == Method::None,
|
|
"method_clone_needs_compiler");
|
|
}
|
|
|
|
static void testChooseMethod_prebuiltWithoutToolchain() {
|
|
Caps caps;
|
|
caps.hasPrebuilt = caps.hasTarball = true;
|
|
check(UpdateManager::chooseUpdateMethod(caps) == Method::Prebuilt,
|
|
"method_prebuilt_without_tools");
|
|
}
|
|
|
|
static void testChooseMethod_prebuiltWithGitOnly() {
|
|
Caps caps;
|
|
caps.hasGit = true; // git alone cannot build
|
|
caps.hasPrebuilt = true;
|
|
check(UpdateManager::chooseUpdateMethod(caps) == Method::Prebuilt,
|
|
"method_prebuilt_git_only");
|
|
}
|
|
|
|
static void testChooseMethod_prebuiltPreferredOverTarball() {
|
|
Caps caps;
|
|
caps.hasCmake = caps.hasCompiler = true; // no git
|
|
caps.hasPrebuilt = caps.hasTarball = true;
|
|
check(UpdateManager::chooseUpdateMethod(caps) == Method::Prebuilt,
|
|
"method_prebuilt_over_tarball");
|
|
}
|
|
|
|
static void testChooseMethod_tarballWithoutGit() {
|
|
Caps caps;
|
|
caps.hasCmake = caps.hasCompiler = true; // no git
|
|
caps.hasTarball = true;
|
|
check(UpdateManager::chooseUpdateMethod(caps) == Method::Tarball,
|
|
"method_tarball_without_git");
|
|
}
|
|
|
|
static void testChooseMethod_tarballRequiresCompiler() {
|
|
Caps caps;
|
|
caps.hasCmake = true; // no compiler
|
|
caps.hasTarball = true;
|
|
check(UpdateManager::chooseUpdateMethod(caps) == Method::None,
|
|
"method_tarball_needs_compiler");
|
|
}
|
|
|
|
static void testChooseMethod_noneWhenNothingAvailable() {
|
|
Caps caps;
|
|
check(UpdateManager::chooseUpdateMethod(caps) == Method::None,
|
|
"method_none_without_anything");
|
|
}
|
|
|
|
// pacman can install whatever build tool is missing, so a minimal Arch system
|
|
// (no cmake, no compiler, not even git) still reaches the rebuilding methods:
|
|
// the update script installs the dependencies first, with the sudo password the
|
|
// user typed in the update dialog.
|
|
static void testChooseMethod_pacmanUnlocksClone() {
|
|
Caps caps;
|
|
caps.canInstallPackages = true; // nothing else installed
|
|
caps.hasGit = true; // git is the only tool the clone path
|
|
// strictly needs up front (deps install
|
|
// brings cmake + the compiler)
|
|
check(UpdateManager::chooseUpdateMethod(caps) == Method::Clone,
|
|
"method_clone_with_pacman_only");
|
|
}
|
|
|
|
static void testChooseMethod_pacmanWithoutGit_stillTarball() {
|
|
Caps caps;
|
|
caps.canInstallPackages = true;
|
|
caps.hasTarball = true; // no git: the tarball path also needs no tool now
|
|
check(UpdateManager::chooseUpdateMethod(caps) == Method::Tarball,
|
|
"method_tarball_with_pacman_only");
|
|
}
|
|
|
|
// Without pacman there is no way to fill a missing toolchain: the prebuilt
|
|
// archive (when the release ships one) stays the only option.
|
|
static void testChooseMethod_noPacman_prebuiltStillFallback() {
|
|
Caps caps;
|
|
caps.hasGit = true; // git alone cannot build, and pacman cannot fix it
|
|
caps.hasPrebuilt = true;
|
|
check(UpdateManager::chooseUpdateMethod(caps) == Method::Prebuilt,
|
|
"method_prebuilt_without_pacman");
|
|
}
|
|
|
|
// The dependency list and its `pacman -T` probe are pure data/IO: the list is
|
|
// asserted literally so a rename cannot silently break the install script, and
|
|
// the missing-dependency lookup must agree with pacman itself (empty on this
|
|
// machine means "nothing to install", not "unknown").
|
|
static void testBuildDependencies() {
|
|
const QStringList deps = UpdateManager::buildDependencies();
|
|
check(deps.contains("base-devel"), "deps_base_devel");
|
|
check(deps.contains("cmake"), "deps_cmake");
|
|
check(deps.contains("gcc"), "deps_gcc");
|
|
check(deps.contains("qt6-base"), "deps_qt6_base");
|
|
check(deps.contains("git"), "deps_git");
|
|
|
|
// On a machine without pacman this is empty and the update simply keeps
|
|
// its current behaviour; with pacman it must never report an installed
|
|
// package as missing.
|
|
const QStringList missing = UpdateManager::missingBuildDependencies();
|
|
for (const QString& pkg : missing) {
|
|
check(deps.contains(pkg), "missing_dependency_is_from_the_list");
|
|
}
|
|
}
|
|
|
|
// The generated snippet must install exactly what pacman -T reports, through
|
|
// sudo (with the password piped to -S), and skip everything on a complete
|
|
// system. Quoted here is the password safety: the quoting is the same as the
|
|
// one used for the install step.
|
|
static void testDependencyInstallScript() {
|
|
const QString script = UpdateManager::dependencyInstallScript("'secret'");
|
|
check(script.contains("pacman -T"), "script_asks_pacman_what_is_missing");
|
|
check(script.contains("base-devel"), "script_installs_base_devel");
|
|
check(script.contains("qt6-base"), "script_installs_qt6_base");
|
|
check(script.contains("--needed --noconfirm"), "script_pacman_flags");
|
|
check(script.contains("| sudo -S"), "script_pipes_password_to_sudo");
|
|
check(script.contains("'secret'"), "script_uses_the_given_password");
|
|
|
|
// No password: sudo -n fails fast on a password-requiring setup instead of
|
|
// hanging the background update on an unanswerable prompt.
|
|
const QString passwordless = UpdateManager::dependencyInstallScript(QString());
|
|
check(passwordless.contains("sudo -n"), "script_passwordless_uses_sudo_n");
|
|
check(!passwordless.contains("sudo -S"), "script_passwordless_has_no_pipe");
|
|
}
|
|
|
|
// Reconciliation of the two sources of truth: whichever is ahead wins, an empty
|
|
// side never does. This is what stops a release from being offered again when the
|
|
// binary and the installed marker disagree.
|
|
static void testNewestVersion() {
|
|
check(UpdateManager::newestVersion("1.3.1", "1.4.1") == "1.4.1",
|
|
"newest_installed_ahead");
|
|
check(UpdateManager::newestVersion("1.4.1", "1.3.1") == "1.4.1",
|
|
"newest_compiled_ahead");
|
|
check(UpdateManager::newestVersion("1.4.1", "1.4.1") == "1.4.1", "newest_equal");
|
|
check(UpdateManager::newestVersion("v1.4.1", "1.3.1") == "v1.4.1",
|
|
"newest_tag_prefix");
|
|
check(UpdateManager::newestVersion("", "1.3.1") == "1.3.1", "newest_empty_compiled");
|
|
check(UpdateManager::newestVersion("1.3.1", "") == "1.3.1", "newest_empty_installed");
|
|
check(UpdateManager::newestVersion("1.10.0", "1.9.9") == "1.10.0",
|
|
"newest_minor_beats_patch");
|
|
}
|
|
|
|
// Whatever sits next to the test binary, the reported version never goes
|
|
// backwards relative to the one compiled into it.
|
|
static void testCurrentVersion_neverOlderThanCompiled() {
|
|
const QString reported = UpdateManager::currentVersion();
|
|
check(!reported.isEmpty(), "current_version_not_empty");
|
|
check(!UpdateManager::isNewerVersion(QStringLiteral(FWSPACMANAGE_VERSION), reported),
|
|
"current_version_not_older_than_compiled");
|
|
}
|
|
|
|
// The staged install must replace an executable that is currently running,
|
|
// which is exactly the situation of the app updating itself: opening the busy
|
|
// file for writing fails with ETXTBSY ("Text file busy"), so the script copies
|
|
// to a sibling temp file and renames it over the target instead.
|
|
// Unix-only: it needs /bin/sh and exec semantics, and /bin/sleep as a known
|
|
// long-lived program.
|
|
#if !defined(Q_OS_WIN)
|
|
static void testStagedInstall_replacesRunningExecutable() {
|
|
QTemporaryDir tmp;
|
|
check(tmp.isValid(), "staged_install_tmpdir");
|
|
if (!tmp.isValid()) return;
|
|
|
|
const QString binDir = tmp.path() + "/bin";
|
|
const QString stage = tmp.path() + "/stage";
|
|
QDir().mkpath(binDir + "/plugins");
|
|
QDir().mkpath(stage + "/bin/plugins");
|
|
|
|
// Seed the target with a long-lived executable and start it, so the
|
|
// destination really is "text busy" for the whole duration of the test.
|
|
const QString target = binDir + "/PacManager";
|
|
check(QFile::copy("/bin/sleep", target), "staged_install_seed_binary");
|
|
|
|
QProcess running;
|
|
running.start(target, {"30"});
|
|
check(running.waitForStarted(5000), "staged_install_binary_running");
|
|
|
|
// Staged tree: a different binary, a plugin and the version file.
|
|
check(QFile::copy("/bin/echo", stage + "/bin/PacManager"),
|
|
"staged_install_stage_binary");
|
|
check(writeFile(stage + "/bin/version", "9.9.9"),
|
|
"staged_install_stage_version");
|
|
check(writeFile(stage + "/bin/plugins/fake-provider.so", "plugin-bytes"),
|
|
"staged_install_stage_plugin");
|
|
|
|
QProcess sh;
|
|
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
|
|
env.insert("BIN_DIR", binDir);
|
|
env.insert("STAGE", stage);
|
|
sh.setProcessEnvironment(env);
|
|
sh.start("/bin/sh", {"-c", UpdateManager::stagedInstallScript()});
|
|
const bool finished = sh.waitForFinished(15000);
|
|
const int exitCode = sh.exitCode();
|
|
if (!finished || exitCode != 0) {
|
|
std::cout << " sh output: " << sh.readAllStandardOutput().constData()
|
|
<< sh.readAllStandardError().constData() << std::endl;
|
|
}
|
|
check(finished && sh.exitStatus() == QProcess::NormalExit && exitCode == 0,
|
|
"staged_install_exit_zero");
|
|
|
|
check(sameFileContent(target, stage + "/bin/PacManager"),
|
|
"staged_install_binary_replaced");
|
|
check(sameFileContent(binDir + "/version", stage + "/bin/version"),
|
|
"staged_install_version_replaced");
|
|
check(sameFileContent(binDir + "/plugins/fake-provider.so",
|
|
stage + "/bin/plugins/fake-provider.so"),
|
|
"staged_install_plugin_replaced");
|
|
|
|
// The temporary copy must not survive in the target directory.
|
|
check(QDir(binDir).entryList(QStringList() << "*.new", QDir::Files).isEmpty(),
|
|
"staged_install_no_leftover_temp");
|
|
|
|
// The process started before the install still runs on its old inode, which
|
|
// is what makes the replacement safe for the running app.
|
|
check(running.state() == QProcess::Running, "staged_install_old_process_alive");
|
|
|
|
running.kill();
|
|
running.waitForFinished(2000);
|
|
}
|
|
|
|
// The version marker has to end up describing the release that was installed:
|
|
// it is what the app reports at launch and what the next update check compares
|
|
// against, so a stale marker makes the app offer the same update again.
|
|
static void testStagedInstall_writesReleaseVersionMarker() {
|
|
QTemporaryDir tmp;
|
|
check(tmp.isValid(), "version_marker_tmpdir");
|
|
if (!tmp.isValid()) return;
|
|
|
|
const QString binDir = tmp.path() + "/bin";
|
|
const QString stage = tmp.path() + "/stage";
|
|
QDir().mkpath(binDir);
|
|
QDir().mkpath(stage + "/bin");
|
|
|
|
// The target holds an outdated marker, and the staged tree has none at all
|
|
// (a prebuilt archive built without the file, or an install script that
|
|
// staged only the binary).
|
|
check(writeFile(binDir + "/version", "1.3.1"), "version_marker_seed");
|
|
check(QFile::copy("/bin/echo", stage + "/bin/PacManager"),
|
|
"version_marker_stage_binary");
|
|
|
|
QProcess sh;
|
|
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
|
|
env.insert("BIN_DIR", binDir);
|
|
env.insert("STAGE", stage);
|
|
env.insert("APP_VERSION", "9.9.9");
|
|
sh.setProcessEnvironment(env);
|
|
sh.start("/bin/sh", {"-c", UpdateManager::stagedInstallScript()});
|
|
const bool finished = sh.waitForFinished(15000);
|
|
check(finished && sh.exitCode() == 0, "version_marker_exit_zero");
|
|
|
|
QFile marker(binDir + "/version");
|
|
const bool readable = marker.open(QIODevice::ReadOnly);
|
|
check(readable, "version_marker_readable");
|
|
if (readable) {
|
|
check(QString::fromUtf8(marker.readAll()).trimmed() == "9.9.9",
|
|
"version_marker_updated");
|
|
}
|
|
check(QDir(binDir).entryList(QStringList() << "*.new", QDir::Files).isEmpty(),
|
|
"version_marker_no_leftover_temp");
|
|
}
|
|
#endif // !Q_OS_WIN
|
|
|
|
// The restart after a successful update must point at the binary the update
|
|
// just installed, and must refuse to launch anything that is missing or not
|
|
// runnable (the caller then asks for a manual restart instead).
|
|
static void testRestartProgram_findsInstalledBinary() {
|
|
QTemporaryDir tmp;
|
|
check(tmp.isValid(), "restart_program_tmpdir");
|
|
if (!tmp.isValid()) return;
|
|
|
|
// Nothing installed yet, or no directory at all: nowhere to hand over to.
|
|
check(UpdateManager::restartProgram(tmp.path()).isEmpty(),
|
|
"restart_program_absent");
|
|
check(UpdateManager::restartProgram(QString()).isEmpty(),
|
|
"restart_program_empty_bindir");
|
|
|
|
#ifdef Q_OS_WIN
|
|
const QString name = QStringLiteral("PacManager.exe");
|
|
#else
|
|
const QString name = QStringLiteral("PacManager");
|
|
#endif
|
|
const QString target = tmp.path() + "/" + name;
|
|
check(writeFile(target, "#!/bin/sh\nexit 0\n"), "restart_program_write");
|
|
|
|
// Present but not runnable: still not something to launch. Windows derives
|
|
// executability from the .exe suffix, hence the guard.
|
|
#if !defined(Q_OS_WIN)
|
|
QFile::setPermissions(target, QFileDevice::ReadOwner | QFileDevice::WriteOwner);
|
|
check(UpdateManager::restartProgram(tmp.path()).isEmpty(),
|
|
"restart_program_not_executable");
|
|
#endif
|
|
|
|
QFile::setPermissions(target, QFileDevice::ReadOwner | QFileDevice::WriteOwner
|
|
| QFileDevice::ExeOwner);
|
|
check(UpdateManager::restartProgram(tmp.path())
|
|
== QFileInfo(target).absoluteFilePath(),
|
|
"restart_program_found");
|
|
}
|
|
|
|
int main() {
|
|
// Basic ordering
|
|
check(UpdateManager::isNewerVersion("1.3.2", "1.3.1") == true, "newer_patch");
|
|
check(UpdateManager::isNewerVersion("1.3.1", "1.3.1") == false, "same_version");
|
|
check(UpdateManager::isNewerVersion("1.3.0", "1.3.1") == false, "older_patch");
|
|
check(UpdateManager::isNewerVersion("1.4.0", "1.3.9") == true, "newer_minor");
|
|
check(UpdateManager::isNewerVersion("2.0.0", "1.9.9") == true, "newer_major");
|
|
|
|
// Leading v/V (git tags) is normalized
|
|
check(UpdateManager::isNewerVersion("v1.3.2", "1.3.1") == true, "tag_v_prefix");
|
|
check(UpdateManager::isNewerVersion("V1.3.2", "1.3.1") == true, "tag_V_prefix");
|
|
check(UpdateManager::isNewerVersion("v1.3.1", "1.3.1") == false, "tag_same");
|
|
|
|
// Different component counts: 1.3 == 1.3.0
|
|
check(UpdateManager::isNewerVersion("1.3.1", "1.3") == true, "longer_newer");
|
|
check(UpdateManager::isNewerVersion("1.3", "1.3.0") == false, "equal_implicit_zero");
|
|
|
|
// Empty remote never triggers an update prompt
|
|
check(UpdateManager::isNewerVersion("", "1.3.1") == false, "empty_remote");
|
|
|
|
// Reported version
|
|
testNewestVersion();
|
|
testCurrentVersion_neverOlderThanCompiled();
|
|
|
|
// Update-strategy choice
|
|
testChooseMethod_checkoutWinsOverEverything();
|
|
testChooseMethod_cloneWhenToolchainPresent();
|
|
testChooseMethod_cloneRequiresCompiler();
|
|
testChooseMethod_prebuiltWithoutToolchain();
|
|
testChooseMethod_prebuiltWithGitOnly();
|
|
testChooseMethod_prebuiltPreferredOverTarball();
|
|
testChooseMethod_tarballWithoutGit();
|
|
testChooseMethod_tarballRequiresCompiler();
|
|
testChooseMethod_noneWhenNothingAvailable();
|
|
testChooseMethod_pacmanUnlocksClone();
|
|
testChooseMethod_pacmanWithoutGit_stillTarball();
|
|
testChooseMethod_noPacman_prebuiltStillFallback();
|
|
testBuildDependencies();
|
|
testDependencyInstallScript();
|
|
|
|
#if !defined(Q_OS_WIN)
|
|
testStagedInstall_replacesRunningExecutable();
|
|
testStagedInstall_writesReleaseVersionMarker();
|
|
#endif
|
|
testRestartProgram_findsInstalledBinary();
|
|
|
|
if (failures == 0) std::cout << "All update-manager tests passed" << std::endl;
|
|
return failures == 0 ? 0 : 1;
|
|
}
|