Files
Chronos/tests/Titano.Core.Tests/Packaging/PayloadRoundTripTests.cs
T
Claude d79ab1e41b feat(packaging): add TPK1 self-extracting payload format
Installer payload appended to the setup program: JSON manifest (components,
files with size/SHA-256/offsets/mtime, shortcuts, file types) plus deflate or
stored entries and a 64-byte footer with the manifest hash. The reader finds
the footer before an Authenticode table, validates every offset, rejects
unsafe paths and bounds inflation by the declared size; the writer is
deterministic, deduplicates identical content and strips a signed stub.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-02 09:56:37 +00:00

173 lines
8.6 KiB
C#

using Titano.Core.Packaging;
using Titano.Testing;
namespace Titano.Core.Tests.Packaging;
public sealed class PayloadRoundTripTests(TestContext context)
{
private readonly PackagingFixture _fx = new(context);
[Test]
public void ExtractsNestedEmptyUnicodeAndIncompressibleFilesExactly()
{
var files = new (string Path, byte[] Content)[]
{
("Titano.exe", PackagingFixture.Text("MZ program body ", 4000)),
("lib/deep/er/still/config.json", PackagingFixture.Text("{\"a\":1}", 50)),
("empty.txt", []),
("città/日本語 ✓.txt", PackagingFixture.Text("ciao 世界 ", 300)),
("media/noise.bin", PackagingFixture.Random(3 << 20, seed: 11)),
};
var app = _fx.Folder("app", files);
var installer = _fx.Pack(_fx.Stub(), PackagingFixture.Source(new PayloadComponentSource("Titano", "Titano", true, app)));
using var reader = PayloadReader.Open(installer);
var manifest = reader.Manifest;
Assert.Equal("Titano", manifest.Product);
Assert.Equal("1.2.3", manifest.Version);
Assert.Count(files.Length, manifest.Files);
Assert.Equal(files.Sum(f => (long)f.Content.Length), manifest.Components[0].EstimatedSize);
Assert.Equal(PayloadCompression.Stored, manifest.Files.Single(f => f.Path == "media/noise.bin").Compression);
Assert.Equal(PayloadCompression.Deflate, manifest.Files.Single(f => f.Path == "Titano.exe").Compression);
var empty = manifest.Files.Single(f => f.Path == "empty.txt");
Assert.Equal(0L, empty.Length);
var target = context.CreateTempDirectory("extract");
reader.Extract(manifest.FilesFor(["Titano"]), target);
foreach (var (relative, content) in files)
{
var path = Path.Combine(target, relative.Replace('/', Path.DirectorySeparatorChar));
Assert.BytesEqual(content, File.ReadAllBytes(path), relative);
Assert.Equal(PackagingFixture.FixedTime, File.GetLastWriteTimeUtc(path), relative);
}
}
[Test]
public void ReportsProgressUpToTheTotalSize()
{
var app = _fx.Folder("app", ("a.bin", PackagingFixture.Random(9 << 20, 1)), ("b.txt", PackagingFixture.Text("b", 10)));
var installer = _fx.Pack(_fx.Stub(), PackagingFixture.Source(new PayloadComponentSource("Titano", "Titano", true, app)));
using var reader = PayloadReader.Open(installer);
var reports = new List<PayloadProgress>();
reader.Extract(reader.Manifest.Files, context.CreateTempDirectory(), new SyncProgress<PayloadProgress>(reports.Add));
Assert.True(reports.Count >= 3);
Assert.Monotonic(reports.Select(r => (double)r.Done));
Assert.Equal(reports[^1].Total, reports[^1].Done);
Assert.Equal((9L << 20) + 10, reports[^1].Total);
}
[Test]
public void IdenticalInputsProduceIdenticalInstallers()
{
var app = _fx.Folder("app", ("Titano.exe", PackagingFixture.Text("app", 999)), ("sub/z.txt", PackagingFixture.Text("z")), ("sub/a.txt", PackagingFixture.Text("a")));
var up = _fx.Folder("uploader", ("Titano.Uploader.exe", PackagingFixture.Text("uploader", 500)));
var stub = _fx.Stub();
var source = PackagingFixture.Source(
new PayloadComponentSource("Titano", "Titano", true, app),
new PayloadComponentSource("Uploader", "Titano Uploader", false, up));
var first = _fx.Pack(stub, source, "first.exe");
var second = _fx.Pack(stub, source, "second.exe");
Assert.BytesEqual(File.ReadAllBytes(first), File.ReadAllBytes(second));
Assert.Equal(0L, new FileInfo(first).Length % PayloadFormat.Alignment);
}
[Test]
public void SharedFilesAreStoredOnceAndInstalledOnce()
{
var shared = PackagingFixture.Text("shared runtime ", 2000);
var app = _fx.Folder("app", ("Titano.exe", PackagingFixture.Text("app", 100)), ("runtime.dll", shared));
var up = _fx.Folder("uploader", ("Titano.Uploader.exe", PackagingFixture.Text("up", 100)), ("runtime.dll", shared));
var installer = _fx.Pack(_fx.Stub(), PackagingFixture.Source(
new PayloadComponentSource("Titano", "Titano", true, app),
new PayloadComponentSource("Uploader", "Uploader", true, up)));
using var reader = PayloadReader.Open(installer);
var copies = reader.Manifest.Files.Where(f => f.Path == "runtime.dll").ToList();
Assert.Count(2, copies);
Assert.Equal(copies[0].Offset, copies[1].Offset);
var both = reader.Manifest.FilesFor(["Titano", "Uploader"]);
Assert.Count(3, both);
Assert.Count(2, reader.Manifest.FilesFor(["uploader"]));
Assert.Throws<ArgumentException>(() => reader.Manifest.FilesFor(["Server"]));
}
[Test]
public void DifferentContentAtTheSamePathAcrossComponentsIsRejected()
{
var app = _fx.Folder("app", ("same.dll", PackagingFixture.Text("one")));
var up = _fx.Folder("uploader", ("same.dll", PackagingFixture.Text("two")));
var ex = Assert.Throws<PayloadException>(() => _fx.Pack(_fx.Stub(), PackagingFixture.Source(
new PayloadComponentSource("Titano", "Titano", true, app),
new PayloadComponentSource("Uploader", "Uploader", true, up))));
Assert.Equal(PayloadError.Corrupt, ex.Error);
}
[Test]
public void ShortcutsAndAssociationsSurviveTheRoundTrip()
{
var app = _fx.Folder("app", ("Titano.exe", PackagingFixture.Text("app")));
var source = new PayloadSource
{
Product = "Titano",
Version = "2.0.0-beta.1",
Components = [new PayloadComponentSource("Titano", "Titano", true, app)],
Shortcuts = [new PayloadShortcut { Component = "Titano", Name = "Titano", Target = "Titano.exe", StartMenu = true, Desktop = true }],
Associations = [new PayloadAssociation { Component = "Titano", Extension = ".titano", ProgId = "Titano.Project", Description = "Titano project", Target = "Titano.exe" }],
};
using var reader = PayloadReader.Open(_fx.Pack(_fx.Stub(), source));
var shortcut = reader.Manifest.Shortcuts.Single();
Assert.True(shortcut.Desktop && shortcut.StartMenu);
Assert.Equal("Titano.exe", shortcut.Target);
Assert.Equal("Titano.Project", reader.Manifest.Associations.Single().ProgId);
}
[Test]
public void ShortcutToAFileOutsideItsComponentIsRejected()
{
var app = _fx.Folder("app", ("Titano.exe", PackagingFixture.Text("app")));
var source = new PayloadSource
{
Product = "Titano",
Version = "1.0.0",
Components = [new PayloadComponentSource("Titano", "Titano", true, app)],
Shortcuts = [new PayloadShortcut { Component = "Titano", Name = "Missing", Target = "Missing.exe", StartMenu = true }],
};
Assert.Throws<PayloadException>(() => _fx.Pack(_fx.Stub(), source));
}
[Test]
public void StubCopyEndsAtThePayload()
{
var stub = _fx.Stub(10_000);
var app = _fx.Folder("app", ("Titano.exe", PackagingFixture.Text("app", 10)));
using var reader = PayloadReader.Open(_fx.Pack(stub, PackagingFixture.Source(new PayloadComponentSource("Titano", "Titano", true, app))));
Assert.Equal(10_000L, reader.PayloadOffset);
var copy = Path.Combine(context.CreateTempDirectory(), "Uninstall Titano.exe");
reader.WriteStub(copy);
Assert.BytesEqual(File.ReadAllBytes(stub), File.ReadAllBytes(copy));
Assert.Equal(PayloadError.NotFound, Assert.Throws<PayloadException>(() => PayloadReader.Open(copy)).Error);
}
[Test]
public void OutputInsideAComponentFolderIsRefused()
{
var app = _fx.Folder("app", ("Titano.exe", PackagingFixture.Text("app")));
Assert.Throws<ArgumentException>(() => PayloadWriter.Write(_fx.Stub(), PackagingFixture.Source(new PayloadComponentSource("Titano", "Titano", true, app)), Path.Combine(app, "setup.exe")));
}
[Test]
public void AnInstallerCannotBeUsedAsStub()
{
var app = _fx.Folder("app", ("Titano.exe", PackagingFixture.Text("app")));
var source = PackagingFixture.Source(new PayloadComponentSource("Titano", "Titano", true, app));
var installer = _fx.Pack(_fx.Stub(), source);
Assert.Throws<ArgumentException>(() => _fx.Pack(installer, source, "nested.exe"));
}
/// <summary>Synchronous <see cref="IProgress{T}"/> (Progress&lt;T&gt; posts to the thread pool).</summary>
private sealed class SyncProgress<T>(Action<T> report) : IProgress<T>
{
public void Report(T value) => report(value);
}
}