Files
Febe/tests/Febe.Tests/Packaging/PackagingTests.cs
T
2026-10-14 00:59:18 +00:00

299 lines
16 KiB
C#

using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using Febe.Tests.Desktop.Support;
using Febe.Tests.Support;
namespace Febe.Tests.Packaging;
/// <summary>
/// What ships Febe (spec §11, §12): the start of the container as root never follows what the unprivileged user can
/// plant in <c>/dati</c> (pkg-2); the seccomp profile Docker reads on the server stays outside the Data volume (pkg-2);
/// the release waits for the tests and for the image, and says when the image was not published (pkg-3); Docker Compose
/// is released with the profile it points to (pkg-4); the image lives on the registry of the Gitea at home, in HTTP.
/// </summary>
[TestClass]
public sealed partial class PackagingTests
{
private const string Registry = "192.168.30.23:3000";
private const int Nobody = 65534;
public TestContext TestContext { get; set; } = null!;
[TestMethod]
[Timeout(60_000, CooperativeCancellation = true)]
public async Task TheStartOfTheContainerNeverFollowsLinksPlantedInTheDataFolder()
{
var reason = EntrypointUnavailableReason();
if (reason is not null)
{
Assert.Inconclusive(reason);
}
using var temp = new TempFolder("avvio-contenitore");
// What a compromised browser (running as PUID) could leave in /dati before a restart: /dati/home pointing to
// a folder of the container, /dati/seccomp.json pointing to one of its files.
var victimFolder = temp.Combine("vittima");
Directory.CreateDirectory(victimFolder);
var victimFile = temp.Combine("vittima.txt");
await File.WriteAllTextAsync(victimFile, "originale", TestContext.CancellationToken);
var data = temp.Combine("dati");
Directory.CreateDirectory(data);
await RunAsync("chown", $"{Nobody}:{Nobody}", data);
File.CreateSymbolicLink(Path.Combine(data, "home"), victimFolder);
File.CreateSymbolicLink(Path.Combine(data, "seccomp.json"), victimFile);
var before = (Folder: await StatAsync(victimFolder), File: await StatAsync(victimFile));
var (code, _, errors) = await RunEntrypointAsync(temp, "true");
Assert.AreEqual(0, code, errors);
Assert.AreEqual(before.Folder, await StatAsync(victimFolder), "a folder of the container never changes owner or mode");
Assert.AreEqual(before.File, await StatAsync(victimFile));
Assert.AreEqual("originale", await File.ReadAllTextAsync(victimFile, TestContext.CancellationToken), "nor is a file written through a link");
StringAssert.Contains(errors, "/dati/seccomp.json non si aggiorna più", "the old profile in /dati is pointed out");
}
[TestMethod]
[Timeout(60_000, CooperativeCancellation = true)]
public async Task TheStartOfTheContainerPreparesTheDataFolderAsTheUserAndKeepsTheProfileOut()
{
var reason = EntrypointUnavailableReason();
if (reason is not null)
{
Assert.Inconclusive(reason);
}
// A first start: /dati and /libreria just created by Docker (root), a folder of photos inside the library.
using var temp = new TempFolder("avvio-contenitore");
Directory.CreateDirectory(temp.Combine("libreria", "foto"));
var whoAmI = temp.Combine("dati", "home", "chi");
var (code, _, errors) = await RunEntrypointAsync(temp, "/bin/sh", "-c", $"id -u > '{whoAmI}'");
Assert.AreEqual(0, code, errors);
Assert.AreEqual($"{Nobody}:{Nobody} 750", await StatAsync(temp.Combine("dati")));
Assert.AreEqual($"{Nobody}:{Nobody} 700", await StatAsync(temp.Combine("dati", "home")), "HOME of Chromium, made by the user");
Assert.AreEqual(Nobody.ToString(System.Globalization.CultureInfo.InvariantCulture), (await File.ReadAllTextAsync(whoAmI, TestContext.CancellationToken)).Trim(), "Febe runs as PUID");
Assert.IsFalse(File.Exists(temp.Combine("dati", "seccomp.json")), "the profile Docker reads stays out of /dati");
Assert.AreEqual($"{Nobody}:{Nobody}", (await StatAsync(temp.Combine("libreria"))).Split(' ')[0]);
Assert.AreEqual("0:0", (await StatAsync(temp.Combine("libreria", "foto"))).Split(' ')[0], "the library is never changed recursively");
// A /dati/home left by a start as root (a real folder): given back to the user.
await RunAsync("chown", "0:0", temp.Combine("dati", "home"));
(code, _, errors) = await RunEntrypointAsync(temp, "true");
Assert.AreEqual(0, code, errors);
Assert.AreEqual($"{Nobody}:{Nobody} 700", await StatAsync(temp.Combine("dati", "home")));
}
[TestMethod]
public void TheUnraidTemplateUsesTheRegistryAtHomeAndKeepsTheSeccompProfileOutOfTheDataFolder()
{
var template = XDocument.Load(Repository.PathOf("docker", "unraid", "febe.xml")).Root!;
string Field(string name) => template.Element(name)?.Value.Trim() ?? string.Empty;
Assert.AreEqual($"{Registry}/alby96/febe:latest", Field("Repository"));
foreach (var name in new[] { "Registry", "Support", "Project", "TemplateURL", "Icon" })
{
StringAssert.StartsWith(Field(name), $"http://{Registry}/Alby96/", name);
}
StringAssert.EndsWith(Field("TemplateURL"), "/raw/branch/main/docker/unraid/febe.xml");
StringAssert.EndsWith(Field("Icon"), "/raw/branch/main/docker/unraid/febe.png");
StringAssert.Contains(Field("Requires"), "insecure-registries");
// pkg-2: Docker reads the profile on the server when it creates the container; inside the Data volume the user
// of the container could loosen it.
var data = template.Elements("Config").Single(c => (string?)c.Attribute("Target") == "/dati").Value.Trim();
var profile = SeccompProfile().Match(Field("ExtraParams"));
Assert.IsTrue(profile.Success, Field("ExtraParams"));
var path = profile.Groups["path"].Value;
Assert.IsFalse(path.StartsWith(data.TrimEnd('/') + "/", StringComparison.Ordinal), $"{path} is inside {data}");
StringAssert.Contains(Field("Requires"), path, "the template says how to put the profile there");
StringAssert.Contains(Field("Requires"), "/usr/share/febe/seccomp.json");
}
[TestMethod]
public void TheComposeFilePointsToTheRegistryAtHomeAndToAProfileOutsideTheDataVolume()
{
var compose = File.ReadAllText(Repository.PathOf("docker", "compose.yml"));
StringAssert.Contains(compose, $"image: {Registry}/alby96/febe:latest");
StringAssert.Contains(compose, "- seccomp=./chromium/seccomp.json");
Assert.IsTrue(File.Exists(Repository.PathOf("docker", "chromium", "seccomp.json")), "the profile is next to compose.yml");
StringAssert.Contains(compose, ":/dati");
Assert.DoesNotContain("seccomp.json:/dati", compose);
}
[TestMethod]
public void TheReleaseWaitsForTheTestsAndTheImageAndShipsComposeWithItsProfile()
{
var workflow = Workflow("rilascio.yml");
var prove = Job(workflow, "prove");
StringAssert.Contains(prove, "dotnet build Febe.slnx -c Release -nologo -warnaserror");
StringAssert.Contains(prove, "dotnet test tests/Febe.Tests -c Release --no-build");
StringAssert.Contains(Job(workflow, "windows"), "needs: prove");
var image = Job(workflow, "immagine");
StringAssert.Contains(image, "needs: prove");
// pkg-3: the release needs the tests, the installer and the image job, which says whether it published.
var release = Job(workflow, "release");
StringAssert.Contains(release, "needs: [prove, windows, immagine]");
Assert.DoesNotContain("if:", release, "no «always()»: a failed build or push stops the release");
StringAssert.Contains(image, "pubblicata: ${{ steps.pubblicazione.outputs.pubblicata }}");
StringAssert.Contains(release, "PUBBLICATA: ${{ needs.immagine.outputs.pubblicata }}");
StringAssert.Contains(release, "non è stata pubblicata");
// Without the secret the image is built and tested, and the job still succeeds: the release says so.
var secret = Step(image, "Segreto per il registro");
Assert.DoesNotContain("exit 1", secret);
StringAssert.Contains(secret, "presente=no");
StringAssert.Contains(Step(image, "Pubblicazione sul registro di Gitea"), "if: steps.segreto.outputs.presente == 'si'");
// The registry at home answers in HTTP: buildx with its own builder, told so, and no «docker login».
StringAssert.Contains(image, "uses: docker/setup-buildx-action@v3");
StringAssert.Contains(image, "http = true");
StringAssert.Contains(image, "insecure = true");
StringAssert.Contains(image, "--push");
Assert.DoesNotContain("docker login", image);
Assert.DoesNotContain("docker push", image);
// pkg-4: compose.yml goes out with chromium/seccomp.json next to it, never alone.
StringAssert.Contains(release, "cp docker/compose.yml \"$COMPOSE/compose.yml\"");
StringAssert.Contains(release, "cp docker/chromium/seccomp.json \"$COMPOSE/chromium/seccomp.json\"");
StringAssert.Contains(release, "\"$COMPOSE.tar.gz\"");
Assert.DoesNotContain("febe-compose-$VERSION.yml", release);
}
/// <summary>Why the entrypoint cannot be run here (it needs Linux, root, setpriv and the user «nobody»); null when it can.</summary>
private static string? EntrypointUnavailableReason()
{
if (!OperatingSystem.IsLinux())
{
return "L'avvio del contenitore si prova solo su Linux.";
}
if (Environment.UserName != "root")
{
return "L'avvio del contenitore gira come root: la prova si salta senza root.";
}
foreach (var tool in new[] { "sh", "setpriv", "stat", "chown", "getent" })
{
if (FindOnPath(tool) is null)
{
return $"Manca {tool}: la prova dell'avvio del contenitore si salta.";
}
}
// The entrypoint creates the user and the group when they are missing: never on the machine of the tests.
return Run("getent", "passwd", Nobody.ToString(System.Globalization.CultureInfo.InvariantCulture)) && Run("getent", "group", Nobody.ToString(System.Globalization.CultureInfo.InvariantCulture))
? null
: $"L'utente o il gruppo {Nobody} non esistono qui: la prova si salta per non crearli.";
}
/// <summary>The real entrypoint, with /dati, /libreria and the profile of the image moved into <paramref name="temp"/>.</summary>
private async Task<(int Code, string Output, string Errors)> RunEntrypointAsync(TempFolder temp, params string[] command)
{
var text = await File.ReadAllTextAsync(Repository.PathOf("docker", "entrypoint.sh"), TestContext.CancellationToken);
var image = temp.Combine("immagine-seccomp.json");
await File.WriteAllTextAsync(image, "{}", TestContext.CancellationToken);
var relocated = text
.Replace("/usr/share/febe/seccomp.json", image, StringComparison.Ordinal)
.Replace("/dati", temp.Combine("dati"), StringComparison.Ordinal)
.Replace("/libreria", temp.Combine("libreria"), StringComparison.Ordinal)
.Replace("groupadd ", ": groupadd ", StringComparison.Ordinal)
.Replace("useradd ", ": useradd ", StringComparison.Ordinal);
var script = temp.Combine("avvio.sh");
await File.WriteAllTextAsync(script, relocated, TestContext.CancellationToken);
var start = new ProcessStartInfo("sh") { RedirectStandardOutput = true, RedirectStandardError = true };
start.ArgumentList.Add(script);
foreach (var part in command)
{
start.ArgumentList.Add(part);
}
start.Environment["PUID"] = Nobody.ToString(System.Globalization.CultureInfo.InvariantCulture);
start.Environment["PGID"] = Nobody.ToString(System.Globalization.CultureInfo.InvariantCulture);
using var process = Process.Start(start)!;
var output = process.StandardOutput.ReadToEndAsync(TestContext.CancellationToken);
var errors = process.StandardError.ReadToEndAsync(TestContext.CancellationToken);
await process.WaitForExitAsync(TestContext.CancellationToken);
return (process.ExitCode, await output, await errors);
}
/// <summary>Owner, group and mode of a path, without following a final link («0:0 755»).</summary>
private async Task<string> StatAsync(string path)
{
var start = new ProcessStartInfo("stat") { RedirectStandardOutput = true };
foreach (var part in new[] { "-c", "%u:%g %a", path })
{
start.ArgumentList.Add(part);
}
using var process = Process.Start(start)!;
var text = await process.StandardOutput.ReadToEndAsync(TestContext.CancellationToken);
await process.WaitForExitAsync(TestContext.CancellationToken);
Assert.AreEqual(0, process.ExitCode, path);
return text.Trim();
}
private async Task RunAsync(string program, params string[] arguments)
{
using var process = Process.Start(new ProcessStartInfo(program, arguments))!;
await process.WaitForExitAsync(TestContext.CancellationToken);
Assert.AreEqual(0, process.ExitCode, $"{program} {string.Join(' ', arguments)}");
}
private static bool Run(string program, params string[] arguments)
{
using var process = Process.Start(new ProcessStartInfo(program, arguments) { RedirectStandardOutput = true, RedirectStandardError = true })!;
process.StandardOutput.ReadToEnd();
process.StandardError.ReadToEnd();
process.WaitForExit();
return process.ExitCode == 0;
}
/// <summary>A workflow of .gitea/workflows; inconclusive in the build of the image, whose context leaves .gitea out.</summary>
private static string Workflow(string name)
{
var path = Repository.PathOf(".gitea", "workflows", name);
var root = Repository.Root()!;
if (!File.Exists(path) && !Directory.Exists(Path.Combine(root, ".git")) && !File.Exists(Path.Combine(root, ".git")))
{
Assert.Inconclusive("Questa copia del repository non ha .gitea/ (costruzione dell'immagine Docker).");
}
return File.ReadAllText(path).Replace("\r\n", "\n", StringComparison.Ordinal);
}
/// <summary>The block of a job: from « name:» to the next job.</summary>
private static string Job(string workflow, string name)
{
var match = Regex.Match(workflow, $@"^ {Regex.Escape(name)}:\n(?<body>(?:(?: .*|\s*)\n)*)", RegexOptions.Multiline);
Assert.IsTrue(match.Success, $"manca il lavoro {name}");
return match.Groups["body"].Value;
}
/// <summary>The block of a step, from its «- name:» to the next step.</summary>
private static string Step(string job, string name)
{
var start = job.IndexOf("- name: " + name, StringComparison.Ordinal);
Assert.IsGreaterThanOrEqualTo(0, start, $"manca il passo «{name}»");
var next = job.IndexOf("\n - ", start + 1, StringComparison.Ordinal);
return next < 0 ? job[start..] : job[start..next];
}
private static string? FindOnPath(string name)
{
foreach (var folder in (Environment.GetEnvironmentVariable("PATH") ?? string.Empty).Split(Path.PathSeparator, StringSplitOptions.RemoveEmptyEntries))
{
var path = Path.Combine(folder, name);
if (File.Exists(path))
{
return path;
}
}
return null;
}
[GeneratedRegex(@"--security-opt seccomp=(?<path>\S+)")]
private static partial Regex SeccompProfile();
}