158 lines
7.3 KiB
C++
158 lines
7.3 KiB
C++
|
|
// SPDX-License-Identifier: LGPL-3.0-only
|
||
|
|
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||
|
|
|
||
|
|
#include <stdlib.h>
|
||
|
|
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>());
|
||
|
|
}
|
||
|
|
|
||
|
|
// The elements of a list key of `metadata`'s [Context], in any order:
|
||
|
|
// build-finish writes them in an order of its own.
|
||
|
|
std::set<std::string> ContextList(const std::string& metadata, std::string_view key) {
|
||
|
|
std::set<std::string> elements;
|
||
|
|
bool inContext = false;
|
||
|
|
std::istringstream lines(metadata);
|
||
|
|
for (std::string line; std::getline(lines, line);) {
|
||
|
|
if (line.starts_with("[")) {
|
||
|
|
inContext = line == "[Context]";
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
if (!inContext || !line.starts_with(std::format("{}=", key))) continue;
|
||
|
|
std::istringstream values(line.substr(key.size() + 1));
|
||
|
|
for (std::string element; std::getline(values, element, ';');) {
|
||
|
|
if (!element.empty()) elements.insert(element);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return elements;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// The sandbox's permissions are finish-args, as a project spells them, and
|
||
|
|
// flatpak build-finish is what reads them. `crafter-build install` and
|
||
|
|
// `bundle` hand it those of the executable's dependencies first and the
|
||
|
|
// executable's own last, so it can take one back, in the manifest
|
||
|
|
// flatpak-builder takes for an app with modules as for one without. The
|
||
|
|
// build itself does not read them: changing them links nothing and writes
|
||
|
|
// no metadata. Where the host's flatpak can be run, a bundle's metadata
|
||
|
|
// holds what build-finish made of them, and a permission it does not know
|
||
|
|
// fails the bundle rather than install the app without a word; elsewhere,
|
||
|
|
// as in flatpak-builder's build sandbox, where CI runs the suite, that half
|
||
|
|
// is left out.
|
||
|
|
int main() {
|
||
|
|
const fs::path scratch = fs::temp_directory_path() / "crafter-build-flatpak-permissions";
|
||
|
|
std::error_code ec;
|
||
|
|
fs::remove_all(scratch, ec);
|
||
|
|
fs::create_directories(scratch);
|
||
|
|
const fs::path info = scratch / "flatpak-info";
|
||
|
|
std::ofstream(info) << "[Application]\nname=net.catcrafts.CrafterBuild\nruntime=runtime/org.freedesktop.Sdk/x86_64/26.08\n";
|
||
|
|
setenv("CRAFTER_BUILD_FLATPAK_INFO", info.string().c_str(), 1);
|
||
|
|
if (NormalizeTarget(GetToolchain().hostTriple) != "x86_64-linux-gnu") {
|
||
|
|
std::println(std::cerr, "SKIP: the staged runtime is x86_64 and the host is {}", GetToolchain().hostTriple);
|
||
|
|
return 77;
|
||
|
|
}
|
||
|
|
if (!GetToolchain().libcxxDir.empty()) {
|
||
|
|
std::println(std::cerr, "SKIP: CRAFTER_BUILD_LIBCXX_DIR is set, and a build against it is no Flatpak");
|
||
|
|
return 77;
|
||
|
|
}
|
||
|
|
|
||
|
|
const fs::path fixture = fs::current_path() / "tests" / "FlatpakPermissions" / "fixture";
|
||
|
|
fs::remove_all(fixture / "bin", ec);
|
||
|
|
fs::remove_all(fixture / "build", ec);
|
||
|
|
fs::remove_all(fixture / "lib" / "bin", ec);
|
||
|
|
fs::remove_all(fixture / "lib" / "build", ec);
|
||
|
|
auto lib = std::make_unique<Configuration>();
|
||
|
|
lib->path = fixture / "lib";
|
||
|
|
lib->name = "perm-lib";
|
||
|
|
lib->outputName = "perm";
|
||
|
|
lib->target = HostTarget();
|
||
|
|
lib->type = ConfigurationType::LibraryStatic;
|
||
|
|
lib->flatpak.permissions = {"--socket=wayland", "--socket=x11", "--device=dri"};
|
||
|
|
{
|
||
|
|
std::array<fs::path, 1> ifaces = { "Perm" };
|
||
|
|
std::array<fs::path, 0> impls = {};
|
||
|
|
lib->GetInterfacesAndImplementations(ifaces, impls);
|
||
|
|
}
|
||
|
|
Configuration app;
|
||
|
|
app.path = fixture;
|
||
|
|
app.name = "perm-app";
|
||
|
|
app.outputName = "perm-app";
|
||
|
|
app.target = HostTarget();
|
||
|
|
app.packaging = Packaging::Flatpak;
|
||
|
|
app.dependencies = { lib.get() };
|
||
|
|
app.flatpak.appId = "net.example.Permissions";
|
||
|
|
app.flatpak.permissions = {"--nosocket=x11", "--share=network"};
|
||
|
|
{
|
||
|
|
std::array<fs::path, 0> ifaces = {};
|
||
|
|
std::array<fs::path, 1> impls = { "main" };
|
||
|
|
app.GetInterfacesAndImplementations(ifaces, impls);
|
||
|
|
}
|
||
|
|
const auto build = [&]() {
|
||
|
|
std::unordered_map<fs::path, std::shared_future<BuildResult>> depResults;
|
||
|
|
std::mutex depMutex;
|
||
|
|
return Build(app, depResults, depMutex).result;
|
||
|
|
};
|
||
|
|
if (const std::string failed = build(); !failed.empty()) {
|
||
|
|
std::println(std::cerr, "FAIL: build failed: {}", failed);
|
||
|
|
return 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
// In the order build-finish gets them, the library's before the app's.
|
||
|
|
const std::string manifest = FlatpakManifest(app);
|
||
|
|
std::size_t at = 0;
|
||
|
|
for (const std::string_view argument : {"--socket=wayland", "--socket=x11", "--device=dri", "--nosocket=x11", "--share=network"}) {
|
||
|
|
const std::size_t found = manifest.find(std::format("\"{}\"", argument), at);
|
||
|
|
Check(found != std::string::npos, std::format("{} is handed on, after what comes before it:\n{}", argument, manifest));
|
||
|
|
if (found != std::string::npos) at = found;
|
||
|
|
}
|
||
|
|
|
||
|
|
const std::string variant = app.VariantId();
|
||
|
|
const fs::path program = ExpectedOutput(app);
|
||
|
|
const fs::file_time_type linked = fs::last_write_time(program);
|
||
|
|
app.flatpak.permissions.push_back("--socket=pulseaudio");
|
||
|
|
if (const std::string failed = build(); !failed.empty()) {
|
||
|
|
std::println(std::cerr, "FAIL: the second build failed: {}", failed);
|
||
|
|
return 1;
|
||
|
|
}
|
||
|
|
Check(app.VariantId() == variant, "changing a permission does not move the build to another directory");
|
||
|
|
Check(fs::last_write_time(program) == linked, "and links nothing");
|
||
|
|
Check(!fs::exists(app.BinDir() / "metadata"), "and the build writes no metadata, which build-finish does");
|
||
|
|
Check(FlatpakManifest(app).contains("\"--socket=pulseaudio\""), "a permission added since is handed on");
|
||
|
|
|
||
|
|
if (const std::string why = HostFlatpakUnavailable(fixture); !why.empty()) {
|
||
|
|
std::println("what build-finish makes of them is left out: {}", why);
|
||
|
|
} else {
|
||
|
|
const std::string bundled = BundleFlatpakApp(app);
|
||
|
|
Check(bundled.empty(), std::format("the app is bundled: {}", bundled));
|
||
|
|
const std::string metadata = ReadFile(FlatpakAppDir(app) / "build-dir" / "metadata");
|
||
|
|
Check(ContextList(metadata, "sockets") == std::set<std::string>{"wayland", "!x11", "pulseaudio"}, std::format("the library's sockets reach the app, which takes x11 back:\n{}", metadata));
|
||
|
|
Check(ContextList(metadata, "devices") == std::set<std::string>{"dri"} && ContextList(metadata, "shared") == std::set<std::string>{"network"}, std::format("and its device and share:\n{}", metadata));
|
||
|
|
Check(metadata.contains("\ncommand=perm-app\n"), std::format("the program is the command:\n{}", metadata));
|
||
|
|
|
||
|
|
app.flatpak.permissions.push_back("--socket=sound");
|
||
|
|
const std::string refused = BundleFlatpakApp(app);
|
||
|
|
Check(refused.contains("flatpak build-finish failed") && refused.contains("listed above"), std::format("a socket build-finish does not know fails the bundle: {}", refused));
|
||
|
|
}
|
||
|
|
|
||
|
|
fs::remove_all(scratch, ec);
|
||
|
|
if (Failures > 0) {
|
||
|
|
std::println(std::cerr, "{} assertions failed", Failures);
|
||
|
|
return 1;
|
||
|
|
}
|
||
|
|
return 0;
|
||
|
|
}
|