Files
FWS-PacManage/tests/flatpak-manager.test.cpp
T
BlackAngelTV 55718064a6 feat(files): confirm dropped files and refuse the ones already installed
A drop — or "Ouvrir avec PacManager" — answered nothing: a Flatpak
install walked off into the background and a plugin package landed
without a sound, so the gesture read as ignored. Both now open a
dialog saying what was taken and what it starts.

A file the machine already has is refused before anything runs, and
named. The application a file would install is read out of the file
itself (the [Flatpak Ref] section of a .flatpakref, the header of a
.flatpak bundle, the manifest of a .fwspacm) and compared with what
the provider has, so nothing is started to find out: Flatpak would
otherwise skip the install and report a success the user never got,
and a plugin package would silently replace the one in place.
2026-10-09 19:22:24 +02:00

454 lines
20 KiB
C++

#include "flatpak-manager.h"
#include <cassert>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
// The parsers below read real files, so the tests write real bytes: a scratch
// directory of our own in the system temp tree, never the user's home.
static std::filesystem::path scratchDir() {
std::error_code ec;
const std::filesystem::path dir =
std::filesystem::temp_directory_path(ec) / "pacmanager-flatpak-manager-test";
std::filesystem::create_directories(dir, ec);
return dir;
}
static std::string writeScratch(const std::string& name, const std::string& bytes) {
const std::filesystem::path path = scratchDir() / name;
std::ofstream out(path, std::ios::binary | std::ios::trunc);
out.write(bytes.data(), static_cast<std::streamsize>(bytes.size()));
out.close();
return path.string();
}
// Bytes encoded the way a hex dump reads, so a fixture can be copied from the
// real file it came from.
static std::string fromHex(const std::string& hex) {
std::string bytes;
bytes.reserve(hex.size() / 2);
for (std::size_t i = 0; i + 1 < hex.size(); i += 2) {
bytes.push_back(static_cast<char>(std::stoi(hex.substr(i, 2), nullptr, 16)));
}
return bytes;
}
static void testParseSearchOutput_parsesAlignedRows() {
std::string output =
"Name Description Application ID Version Branch Remotes\n"
"Firefox Fast and privacy-aware browser org.mozilla.firefox 128.0.2 stable flathub\n"
"Calculator Simple calculator org.gnome.Calculator 46.0 stable flathub\n";
auto pkgs = FlatpakManager::parseSearchOutput(output);
assert(pkgs.size() == 2);
assert(pkgs[0].name == "org.mozilla.firefox");
assert(pkgs[0].version == "128.0.2");
assert(pkgs[0].repository == "flathub");
// The display name is prefixed to the description.
assert(pkgs[0].description == "Firefox — Fast and privacy-aware browser");
assert(pkgs[1].name == "org.gnome.Calculator");
}
static void testParseSearchOutput_keepsMultiWordDescriptions() {
std::string output =
"Name Description Application ID Version Branch Remotes\n"
"Spotify Desktop music player with sync org.spotify.Client 1.2.3 stable flathub\n";
auto pkgs = FlatpakManager::parseSearchOutput(output);
assert(pkgs.size() == 1);
assert(pkgs[0].name == "org.spotify.Client");
assert(pkgs[0].description == "Spotify — Desktop music player with sync");
}
static void testParseSearchOutput_ignoresVersionLikeTokens() {
// Versions such as 128.0.2 must not be mistaken for an application ID.
std::string output =
"Name Description Application ID Version Branch Remotes\n"
"GIMP Image editor org.gimp.GIMP 2.10.36 stable flathub\n";
auto pkgs = FlatpakManager::parseSearchOutput(output);
assert(pkgs.size() == 1);
assert(pkgs[0].name == "org.gimp.GIMP");
assert(pkgs[0].version == "2.10.36");
}
static void testParseSearchOutput_emptyInput() {
assert(FlatpakManager::parseSearchOutput("").empty());
}
static void testParseInstalledOutput_parsesTabSeparated() {
std::string output =
"org.mozilla.firefox\t128.0.2\tflathub\n"
"org.gnome.Calculator\t46.0\tflathub\n";
auto pkgs = FlatpakManager::parseInstalledOutput(output);
assert(pkgs.size() == 2);
assert(pkgs[0].name == "org.mozilla.firefox");
assert(pkgs[0].version == "128.0.2");
assert(pkgs[0].repository == "flathub");
assert(pkgs[0].installed);
assert(!pkgs[0].updateAvailable);
}
static void testParseInstalledOutput_emptyInput() {
assert(FlatpakManager::parseInstalledOutput("").empty());
}
// The same application installed more than once: one copy per installation,
// per remote or per branch. Each row must say which one it is, or the view
// cannot list the copies and the provider cannot act on any of them.
static void testParseInstalledOutput_namesEveryInstallation() {
std::string output =
"com.hoptodesk.HopToDesk\t\thoptodesk-origin\tcom.hoptodesk.HopToDesk/x86_64/master\tmaster\tuser\n"
"com.hoptodesk.HopToDesk\t1.46.38\tflathub\tcom.hoptodesk.HopToDesk/x86_64/stable\tstable\tuser\n"
"com.usebottles.bottles\t67.3\tflathub\tcom.usebottles.bottles/x86_64/stable\tstable\tsystem\n"
"com.usebottles.bottles\t67.3\tflathub\tcom.usebottles.bottles/x86_64/stable\tstable\tuser\n";
auto pkgs = FlatpakManager::parseInstalledOutput(output);
assert(pkgs.size() == 4);
// An empty version is a column, not a gap: dropping it shifted every field
// after it, and the row said nothing about which copy it was.
assert(pkgs[0].version.empty());
assert(pkgs[0].repository == "hoptodesk-origin");
// The target is "<installation>:<ref>": the two halves are what flatpak
// needs to refuse the application id and name this copy instead.
assert(pkgs[0].instances[0].target == "user:com.hoptodesk.HopToDesk/x86_64/master");
assert(pkgs[1].instances[0].target == "user:com.hoptodesk.HopToDesk/x86_64/stable");
assert(pkgs[2].instances[0].target == "system:com.usebottles.bottles/x86_64/stable");
assert(pkgs[3].instances[0].target == "user:com.usebottles.bottles/x86_64/stable");
// A label says the installation, and the remote or branch when it is what
// tells two copies of the same id apart.
assert(pkgs[0].instances[0].label == "utilisateur · hoptodesk-origin · master");
assert(pkgs[1].instances[0].label == "utilisateur");
assert(pkgs[2].instances[0].label == "système");
}
// A listing without the ref/branch/installation columns stays readable: the
// row is the application, with no copy to choose between.
static void testParseInstalledOutput_withoutInstallationColumns() {
auto pkgs = FlatpakManager::parseInstalledOutput("org.gnome.Calculator\t47.0\tflathub\n");
assert(pkgs.size() == 1);
assert(pkgs[0].name == "org.gnome.Calculator");
assert(pkgs[0].instances.empty());
}
// Once merged, a row must keep the copies of every row it absorbed: that is
// what the chooser lists.
static void testMergedRowKeepsEveryCopy() {
std::vector<PackageInfo> rows = FlatpakManager::parseInstalledOutput(
"com.usebottles.bottles\t67.3\tflathub\tcom.usebottles.bottles/x86_64/stable\tstable\tsystem\n"
"com.usebottles.bottles\t67.3\tflathub\tcom.usebottles.bottles/x86_64/stable\tstable\tuser\n");
mergeProviderDuplicates(rows);
assert(rows.size() == 1);
assert(rows[0].instances.size() == 2);
assert(rows[0].instances[0].target == "system:com.usebottles.bottles/x86_64/stable");
assert(rows[0].instances[1].target == "user:com.usebottles.bottles/x86_64/stable");
}
static void testParseUpdatesOutput_parsesRows() {
std::string output =
"org.mozilla.firefox\t130.0\n"
"org.gnome.Calculator\t47.0\n";
auto pkgs = FlatpakManager::parseUpdatesOutput(output);
assert(pkgs.size() == 2);
assert(pkgs[0].name == "org.mozilla.firefox");
assert(pkgs[0].version == "130.0");
assert(pkgs[0].installed);
assert(pkgs[0].updateAvailable);
}
static void testParseUpdatesOutput_emptyInput() {
assert(FlatpakManager::parseUpdatesOutput("").empty());
}
// ---- Installing from a file (drag & drop, "Ouvrir avec PacManager") ----
static void testClassifyFile_recognisesTheThreeFlatpakFormats() {
using FileKind = FlatpakManager::FileKind;
assert(FlatpakManager::classifyFile("/home/jane/Téléchargements/org.gnome.Dictionary.flatpakref")
== FileKind::Ref);
assert(FlatpakManager::classifyFile("flathub.flatpakrepo") == FileKind::Repo);
assert(FlatpakManager::classifyFile("/tmp/app.flatpak") == FileKind::Bundle);
// A bundle's extension starts like a ref one's, so it must not be read as
// a .flatpakref by accident.
assert(FlatpakManager::classifyFile("/tmp/Org.App.flatpak") != FileKind::Ref);
// Case and an URL query string do not change the format.
assert(FlatpakManager::classifyFile("https://flathub.org/repo/appstream/x.FLATPAKREF")
== FileKind::Ref);
assert(FlatpakManager::classifyFile("https://example.org/app.flatpakref?v=2")
== FileKind::Ref);
// Anything else is not ours: a pacman package, a directory named after the
// format, or a plain document.
assert(FlatpakManager::classifyFile("/tmp/firefox.pkg.tar.zst") == FileKind::None);
assert(FlatpakManager::classifyFile("/tmp/flatpak") == FileKind::None);
assert(FlatpakManager::classifyFile("") == FileKind::None);
}
static void testRepoName_comesFromTheFileName() {
assert(FlatpakManager::repoNamespace("flathub.flatpakrepo") == "flathub");
assert(FlatpakManager::repoNamespace("/tmp/My-Repo.FlatpakRepo") == "my-repo");
// A nameless path still has to register a remote under some name.
assert(!FlatpakManager::repoNamespace("").empty());
}
static void testInstallFileCommand_buildsTheFlatpakCalls() {
using FileKind = FlatpakManager::FileKind;
// A ref file or bundle is handed to `flatpak install`, non-interactively so
// a dropped file cannot open a prompt nobody is looking at.
const std::string ref = FlatpakManager::installFileCommand(
FileKind::Ref, "/tmp/org.gnome.Dictionary.flatpakref");
assert(ref.find("flatpak install") == 0);
assert(ref.find("--noninteractive") != std::string::npos);
assert(ref.find("--assumeyes") != std::string::npos);
assert(ref.find("/tmp/org.gnome.Dictionary.flatpakref") != std::string::npos);
assert(ref.find("--user") == std::string::npos);
// The per-user retry (polkit refused, no admin rights) adds --user.
const std::string userRef = FlatpakManager::installFileCommand(
FileKind::Ref, "/tmp/org.gnome.Dictionary.flatpakref", /*userScope=*/true);
assert(userRef.find("--user") != std::string::npos);
// A .flatpakrepo registers the remote under the file's name instead.
const std::string repo = FlatpakManager::installFileCommand(
FileKind::Repo, "/tmp/flathub.flatpakrepo", /*userScope=*/true);
assert(repo.find("flatpak remote-add") == 0);
assert(repo.find("--if-not-exists") != std::string::npos);
assert(repo.find("flathub ") != std::string::npos);
assert(repo.find("/tmp/flathub.flatpakrepo") != std::string::npos);
// A path with spaces stays a single argument.
const std::string quoted = FlatpakManager::installFileCommand(
FileKind::Bundle, "/tmp/mon paquet.flatpak");
assert(quoted.find("'/tmp/mon paquet.flatpak'") != std::string::npos);
}
// Naming one copy is what makes the call unambiguous: the ref alone picks the
// remote and the branch, the scope picks the installation.
static void testUninstallAndUpdateCommands_targetOneCopy() {
const std::string userCopy =
FlatpakManager::uninstallCommand("user:com.example.App/x86_64/stable");
assert(userCopy.find("flatpak uninstall") == 0);
assert(userCopy.find("--user") != std::string::npos);
assert(userCopy.find("com.example.App/x86_64/stable") != std::string::npos);
// The application's own id must not be in there any more: that is exactly
// what flatpak refuses when several copies exist.
assert(userCopy.find("--assumeyes com.example.App ") == std::string::npos);
const std::string systemCopy =
FlatpakManager::uninstallCommand("system:com.example.App/x86_64/stable");
assert(systemCopy.find("flatpak uninstall") == 0);
assert(systemCopy.find("--user") == std::string::npos);
assert(systemCopy.find("com.example.App/x86_64/stable") != std::string::npos);
const std::string update = FlatpakManager::updateCommand("user:com.example.App/x86_64/stable");
assert(update.find("flatpak update") == 0);
assert(update.find("--user") != std::string::npos);
assert(update.find("com.example.App/x86_64/stable") != std::string::npos);
}
// A plain application id keeps working as it always did — the provider only
// needs the target when the id is ambiguous, and finds out by trying.
static void testUninstallCommand_stillAcceptsAnApplicationId() {
const std::string plain = FlatpakManager::uninstallCommand("com.example.App");
assert(plain.find("flatpak uninstall") == 0);
assert(plain.find("com.example.App") != std::string::npos);
assert(plain.find("--user") == std::string::npos);
}
// ---- What a dropped file would install ----
//
// The window asks this before it runs anything, to refuse a file the machine
// already has. Both answers are read out of the file itself: no flatpak call,
// no network, and nothing guessed at.
static void testAppIdFromRefFile_readsTheNameField() {
const std::string ref = writeScratch("org.gnome.Dictionary.flatpakref",
"[Flatpak Ref]\n"
"Title=Dictionary\n"
"Name=org.gnome.Dictionary\n"
"Branch=stable\n"
"Url=https://dl.flathub.org/repo/\n"
"SuggestRemoteName=flathub\n"
"RuntimeRepo=https://dl.flathub.org/repo/flathub.flatpakrepo\n"
"IsRuntime=false\n");
assert(FlatpakManager::appIdFromRefFile(ref) == "org.gnome.Dictionary");
// A hand-written file: comments, blank lines, CRLF endings, spaces around
// the separator, a lower-case section, and another section behind it whose
// `Name` is not the application.
const std::string spoken = writeScratch("spaced.flatpakref",
"; written by hand\r\n"
"\r\n"
"[FLATPAK REF]\r\n"
" Name = org.gnome.Calculator \r\n"
"[Flatpak Repo]\n"
"Name=org.example.NotTheApp\n");
assert(FlatpakManager::appIdFromRefFile(spoken) == "org.gnome.Calculator");
}
static void testAppIdFromRefFile_refusesWhatNamesNoApplication() {
// The name is not in the ref section: a repository definition lists other
// keys there, and reading one of them as the application would refuse an
// install that has every right to run.
const std::string repo = writeScratch("flathub.flatpakref",
"[Flatpak Repo]\n"
"Name=org.example.NotTheApp\n"
"[Flatpak Ref]\n"
"Branch=stable\n");
assert(FlatpakManager::appIdFromRefFile(repo).empty());
// An id that is not one is refused rather than handed to flatpak: this came
// straight out of a file the user dropped.
const std::string injected = writeScratch("injected.flatpakref",
"[Flatpak Ref]\n"
"Name=org.example.App; rm -rf ~\n");
assert(FlatpakManager::appIdFromRefFile(injected).empty());
// A file that is not there, an empty one, and one that is not a ref file.
assert(FlatpakManager::appIdFromRefFile("/tmp/pacmgr-does-not-exist.flatpakref").empty());
assert(FlatpakManager::appIdFromRefFile(writeScratch("empty.flatpakref", "")).empty());
assert(FlatpakManager::appIdFromRefFile(writeScratch("junk.flatpakref", "garbage")).empty());
}
static void testAppIdFromRef_stripsKindArchAndBranch() {
assert(FlatpakManager::appIdFromRef("app/org.gnome.Calculator/x86_64/stable")
== "org.gnome.Calculator");
assert(FlatpakManager::appIdFromRef("runtime/org.freedesktop.Platform/x86_64/50")
== "org.freedesktop.Platform");
// A branch can carry a dash and a version, which is not part of the id.
assert(FlatpakManager::appIdFromRef("app/org.winehq.Wine/x86_64/stable-25.08")
== "org.winehq.Wine");
// Not a ref at all: a bare id, a kind alone, an empty string, a URL.
assert(FlatpakManager::appIdFromRef("com.example.App").empty());
assert(FlatpakManager::appIdFromRef("app/").empty());
assert(FlatpakManager::appIdFromRef("").empty());
assert(FlatpakManager::appIdFromRef("https://flathub.org/repo/app.flatpakref").empty());
// A kind flatpak does not install as an application.
assert(FlatpakManager::appIdFromRef("something/org.example.App/x86_64/stable").empty());
}
// The first bytes of a real bundle, hex-encoded exactly as `flatpak
// build-bundle` wrote them: the magic, the ref it installs, then the metadata.
static std::string bundleHeaderFixture() {
return fromHex(
"666c617470616b00010089e500750800"
"72656600000000006170702f636f6d2e"
"757365626f74746c65732e626f74746c"
"65732f7838365f36342f737461626c65"
"00007304000000006d65746164617461"
"00000000000000005b4170706c696361"
"74696f6e5d0a6e616d653d636f6d2e75"
"7365626f74746c65732e626f74746c65"
"730a72756e74696d653d6f72672e676e"
"6f6d652e506c6174666f726d2f783836"
"5f36342f35300a73");
}
static void testAppIdFromBundle_readsTheRefInTheHeader() {
const std::string bundle = writeScratch("bottles.flatpak", bundleHeaderFixture());
assert(FlatpakManager::appIdFromBundle(bundle) == "com.usebottles.bottles");
// A runtime bundle is named the same way, behind "runtime/" instead of
// "app/", and with the framing flatpak may put around the ref differing.
std::string runtime("flatpak\0", 8);
runtime += std::string("\x01\0\0\0\0\0\0\0", 8);
runtime += std::string("ref\0\0\0\0", 8);
runtime += "runtime/org.freedesktop.Platform/x86_64/50";
runtime += '\0';
runtime += "metadata";
assert(FlatpakManager::appIdFromBundle(writeScratch("platform.flatpak", runtime))
== "org.freedesktop.Platform");
}
static void testAppIdFromBundle_refusesWhatItCannotRead() {
assert(FlatpakManager::appIdFromBundle("/tmp/pacmgr-does-not-exist.flatpak").empty());
assert(FlatpakManager::appIdFromBundle(writeScratch("empty.flatpak", "")).empty());
assert(FlatpakManager::appIdFromBundle(writeScratch("plain.flatpak", "not a bundle")).empty());
// The magic alone, then a header that stops in the middle of the ref.
std::string truncated("flatpak\0", 8);
truncated += "ref\0";
assert(FlatpakManager::appIdFromBundle(writeScratch("truncated.flatpak", truncated)).empty());
// The right magic with something else behind the ref key.
std::string foreign("flatpak\0", 8);
foreign += std::string("ref\0\0\0\0\0", 9);
foreign += "garbage-not-a-ref";
foreign += '\0';
assert(FlatpakManager::appIdFromBundle(writeScratch("foreign.flatpak", foreign)).empty());
}
// A repository definition installs no application, so it answers nothing and
// is installed the ordinary way; the two other formats name what they install.
static void testFileTargetName_onlyNamesApplications() {
FlatpakManager manager;
const std::string repo = writeScratch("flathub.flatpakrepo",
"[Flatpak Repo]\n"
"Name=flathub\n"
"Url=https://dl.flathub.org/repo/\n");
assert(manager.fileTargetName(repo).empty());
const std::string ref = writeScratch("calculator.flatpakref",
"[Flatpak Ref]\n"
"Name=org.gnome.Calculator\n");
assert(manager.fileTargetName(ref) == "org.gnome.Calculator");
assert(manager.fileTargetName(writeScratch("app.flatpak", bundleHeaderFixture()))
== "com.usebottles.bottles");
// A file no format claims stays unknown rather than guessed at.
assert(manager.fileTargetName(writeScratch("documents.txt", "rien à voir")).empty());
}
int main() {
testParseSearchOutput_parsesAlignedRows();
testParseSearchOutput_keepsMultiWordDescriptions();
testParseSearchOutput_ignoresVersionLikeTokens();
testParseSearchOutput_emptyInput();
testParseInstalledOutput_parsesTabSeparated();
testParseInstalledOutput_emptyInput();
testParseInstalledOutput_namesEveryInstallation();
testParseInstalledOutput_withoutInstallationColumns();
testMergedRowKeepsEveryCopy();
testParseUpdatesOutput_parsesRows();
testParseUpdatesOutput_emptyInput();
testClassifyFile_recognisesTheThreeFlatpakFormats();
testRepoName_comesFromTheFileName();
testInstallFileCommand_buildsTheFlatpakCalls();
testUninstallAndUpdateCommands_targetOneCopy();
testUninstallCommand_stillAcceptsAnApplicationId();
testAppIdFromRefFile_readsTheNameField();
testAppIdFromRefFile_refusesWhatNamesNoApplication();
testAppIdFromRef_stripsKindArchAndBranch();
testAppIdFromBundle_readsTheRefInTheHeader();
testAppIdFromBundle_refusesWhatItCannotRead();
testFileTargetName_onlyNamesApplications();
std::cout << "All flatpak-manager tests passed." << std::endl;
return 0;
}