Copying over the executable the update is running from fails with ETXTBSY, so the self-update aborted after a successful build and left the user to restart by hand. Installing through a rename, then handing over to the new build, lets the update complete on its own. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
262 lines
10 KiB
C++
262 lines
10 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"
|
|
|
|
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");
|
|
}
|
|
|
|
// 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);
|
|
}
|
|
#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");
|
|
|
|
// Update-strategy choice
|
|
testChooseMethod_checkoutWinsOverEverything();
|
|
testChooseMethod_cloneWhenToolchainPresent();
|
|
testChooseMethod_cloneRequiresCompiler();
|
|
testChooseMethod_prebuiltWithoutToolchain();
|
|
testChooseMethod_prebuiltWithGitOnly();
|
|
testChooseMethod_prebuiltPreferredOverTarball();
|
|
testChooseMethod_tarballWithoutGit();
|
|
testChooseMethod_tarballRequiresCompiler();
|
|
testChooseMethod_noneWhenNothingAvailable();
|
|
|
|
#if !defined(Q_OS_WIN)
|
|
testStagedInstall_replacesRunningExecutable();
|
|
#endif
|
|
testRestartProgram_findsInstalledBinary();
|
|
|
|
if (failures == 0) std::cout << "All update-manager tests passed" << std::endl;
|
|
return failures == 0 ? 0 : 1;
|
|
}
|