In the Flatpak, HostFlatpak makes two projects in the sandbox's /tmp, one there and one through a link in the fixture's build/ that leads there, and asks whether the host sees them. They had fixed names, /tmp/crafter-build-host-flatpak and crafter-build-host-flatpak-linked beside it, and every sandbox of the app has the same /tmp, so two runs at once asked about, and removed, the same projects. Both are now in a directory the test makes there, a PrivateDirectory, which the link leads to, and which goes afterwards with the link, or, when the test is killed, when a later one is made, once it has not changed for a minute. The test still names /tmp rather than the TMPDIR it is given, since the sandbox's /tmp is what it asks the host about, and AGENTS.template.md's entry for it says it makes a directory of its own there now.
149 lines
7.7 KiB
C++
149 lines
7.7 KiB
C++
// SPDX-License-Identifier: LGPL-3.0-only
|
|
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
|
|
|
import std;
|
|
import Crafter.Build;
|
|
namespace fs = std::filesystem;
|
|
using namespace Crafter;
|
|
|
|
namespace {
|
|
std::int32_t Failures = 0;
|
|
|
|
void Check(bool cond, std::string_view msg) {
|
|
if (!cond) {
|
|
std::println(std::cerr, "FAIL: {}", msg);
|
|
++Failures;
|
|
}
|
|
}
|
|
|
|
std::string ReadFile(const fs::path& path) {
|
|
std::ifstream in(path, std::ios::binary);
|
|
return std::string(std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>());
|
|
}
|
|
}
|
|
|
|
// crafter-build reaches the host's flatpak through one helper, and says why
|
|
// when it cannot. Inside its Flatpak that needs the org.freedesktop.Flatpak
|
|
// permission and a session bus, which flatpak-builder's build sandbox, where
|
|
// CI runs this suite, has neither of; and a host that sees the project's
|
|
// bin/ and build/ where crafter-build does, which the host is asked: it sees
|
|
// the fixture, in the home, and not a project in the sandbox's own /tmp, nor
|
|
// one a link in the home leads there. Every host command starts in the
|
|
// host's /: one started in crafter-build's working directory failed
|
|
// wherever the host has none such, /app for one. Natively the flatpak on
|
|
// PATH is the host's. Installing or bundling an executable that is not
|
|
// packaged as a Flatpak is refused, naming what to set, before the host is
|
|
// involved.
|
|
int main() {
|
|
const fs::path fixture = fs::current_path() / "tests" / "HostFlatpak" / "fixture";
|
|
// Whatever the question makes in the fixture, it removes again.
|
|
const auto probesLeft = [&] {
|
|
std::error_code ec;
|
|
for (const std::string_view sub : {"bin", "build"}) {
|
|
if (!fs::is_directory(fixture / sub, ec)) continue;
|
|
for (const fs::directory_entry& entry : fs::directory_iterator(fixture / sub)) {
|
|
if (entry.path().filename().string().starts_with(".crafter-build-host-probe-")) return true;
|
|
}
|
|
}
|
|
return false;
|
|
};
|
|
std::error_code ec;
|
|
fs::remove_all(fixture / "bin", ec);
|
|
fs::remove_all(fixture / "build", ec);
|
|
const std::string why = HostFlatpakUnavailable(fixture);
|
|
if (fs::exists("/.flatpak-info")) {
|
|
const std::string info = ReadFile("/.flatpak-info");
|
|
const bool permitted = info.contains("org.freedesktop.Flatpak=talk");
|
|
if (!permitted) {
|
|
Check(why.contains("org.freedesktop.Flatpak"), std::format("without the permission, the reason names it: {}", why));
|
|
} else {
|
|
Check(why.empty(), std::format("the host sees the fixture, in the home, where crafter-build does: {}", why));
|
|
Check(!fs::exists(fixture / "bin") && !fs::exists(fixture / "build"), "and asking leaves nothing in the fixture, which had no bin/ or build/");
|
|
}
|
|
// A project in the sandbox's own /tmp, a tmpfs nothing outside the
|
|
// sandbox sees, which goes with it; and one reached through a link
|
|
// in the fixture's build/, in the home, that leads there, which the
|
|
// host follows to its own /tmp. Both are made for the question and
|
|
// removed again. Where an override shares the host's /tmp with the
|
|
// sandbox, the host sees both, so they are left out.
|
|
bool tmpShared = false;
|
|
for (std::size_t at = info.find("filesystems="); at != std::string::npos && !tmpShared;) {
|
|
const std::size_t end = info.find('\n', at);
|
|
const std::string line = info.substr(at, end == std::string::npos ? std::string::npos : end - at);
|
|
for (std::size_t begin = std::string_view("filesystems=").size(); begin < line.size();) {
|
|
std::size_t next = line.find(';', begin);
|
|
if (next == std::string::npos) next = line.size();
|
|
const std::string entry = line.substr(begin, next - begin);
|
|
if (entry == "/tmp" || entry.starts_with("/tmp:")) tmpShared = true;
|
|
begin = next + 1;
|
|
}
|
|
at = info.find("filesystems=", at + 1);
|
|
}
|
|
// Both in a directory of this run's own there, a PrivateDirectory,
|
|
// removed at the end of this block: every sandbox of the app has the
|
|
// same /tmp, so two runs at once would ask about, and remove, the
|
|
// same projects, and what a run that is killed leaves goes when a
|
|
// later one is made, once it has not changed for a minute.
|
|
const PrivateDirectory own("/tmp", "crafter-build-host-flatpak-");
|
|
const fs::path& scratch = own.path;
|
|
const fs::path link = fixture / "build" / "to-tmp";
|
|
fs::create_directories(link.parent_path(), ec);
|
|
fs::remove(link, ec);
|
|
fs::create_directory_symlink(scratch, link, ec);
|
|
const std::array<fs::path, 2> unseen = {scratch / "project", link / "linked"};
|
|
for (const fs::path& project : unseen) {
|
|
if (tmpShared) {
|
|
std::println("an override shares the host's /tmp with the sandbox, so the host sees {}: left out", project.string());
|
|
continue;
|
|
}
|
|
if (!permitted) {
|
|
const std::string refused = HostFlatpakUnavailable(project);
|
|
Check(refused.contains("org.freedesktop.Flatpak"), std::format("and so is one at {}, for the same reason: {}", project.string(), refused));
|
|
continue;
|
|
}
|
|
fs::create_directories(project, ec);
|
|
const std::string refused = HostFlatpakUnavailable(project);
|
|
Check(refused.contains("the host does not see") && refused.contains((project / "bin").string()), std::format("inside the Flatpak, a project at {} is refused, saying why: {}", project.string(), refused));
|
|
Check(fs::is_empty(project, ec), std::format("and asking leaves nothing in {}", project.string()));
|
|
fs::remove_all(fs::weakly_canonical(project, ec), ec);
|
|
}
|
|
// The link goes with the directory it leads to.
|
|
fs::remove(link, ec);
|
|
// From a working directory only the sandbox has, the host is reached
|
|
// all the same, and sees the fixture.
|
|
if (permitted) {
|
|
const fs::path before = fs::current_path();
|
|
fs::current_path("/app");
|
|
const std::string fromApp = HostFlatpakUnavailable(fixture);
|
|
Check(fromApp.empty(), std::format("from /app, which only the sandbox has, the host sees the fixture: {}", fromApp));
|
|
Check(RunHostFlatpak("--version") == 0, "and the host's flatpak runs from there");
|
|
fs::current_path(before);
|
|
}
|
|
Check(!probesLeft(), "asking leaves no directory of its own in the fixture's bin/ or build/");
|
|
} else if (std::system("command -v flatpak > /dev/null 2>&1") == 0) {
|
|
Check(why.empty(), std::format("natively, the flatpak on PATH is reachable: {}", why));
|
|
Check(RunHostFlatpak("--version") == 0, "and runs");
|
|
} else {
|
|
Check(why.contains("flatpak"), std::format("without flatpak, the reason says so: {}", why));
|
|
}
|
|
|
|
Configuration raw;
|
|
raw.path = fixture;
|
|
raw.name = "raw-app";
|
|
raw.outputName = "raw";
|
|
raw.target = HostTarget();
|
|
raw.packaging = Packaging::None;
|
|
{
|
|
std::array<fs::path, 0> ifaces = {};
|
|
std::array<fs::path, 1> impls = { "main" };
|
|
raw.GetInterfacesAndImplementations(ifaces, impls);
|
|
}
|
|
Check(InstallFlatpakApp(raw).contains("cfg.packaging = Packaging::Flatpak"), std::format("installing an executable that is no Flatpak says what to set: {}", InstallFlatpakApp(raw)));
|
|
Check(BundleFlatpakApp(raw).contains("cfg.packaging = Packaging::Flatpak"), std::format("bundling one says the same: {}", BundleFlatpakApp(raw)));
|
|
|
|
if (Failures > 0) {
|
|
std::println(std::cerr, "{} assertions failed", Failures);
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|