Files
Chronos/tests/Titano.Core.Tests/Diagnostics/DiagnosticsArchiveTests.cs
T
Claude 2650aa31fb feat(diagnostics): scrubbed diagnostics bundles on the desktop and the server
SecretScrubber removes passwords, tokens, keys, cookies, authorisation
headers, URL credentials and private-key blocks from text and JSON;
DiagnosticsArchive zips reports, settings, failed jobs and log tails through
it. The desktop bundle (Settings > Advanced, About) now adds the codec and
encoder inventory, the settings and recent failed jobs; the server serves
the same as GET /api/v1/server/diagnostics (ManageArchive, audited), linked
from admin > Logs.

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

75 lines
3.4 KiB
C#

using System.IO.Compression;
using System.Text;
using Titano.Core.Diagnostics;
using Titano.Core.Model;
using Titano.Testing;
namespace Titano.Core.Tests.Diagnostics;
public sealed class DiagnosticsArchiveTests(TestContext context)
{
private const string Secret = "correct-horse-battery";
[Test]
public void EveryEntryIsScrubbedAndLogsKeepTheirTail()
{
var logs = context.CreateTempDirectory();
File.WriteAllText(Path.Combine(logs, "titano-20261001.log"), "old line\nPassword=" + Secret + "\n");
var big = new StringBuilder();
for (var i = 0; i < 2_000; i++) big.Append("line ").Append(i).Append(" token: ").Append(Secret).Append('\n');
File.WriteAllText(Path.Combine(logs, "titano-20261002.log"), big.ToString());
File.WriteAllText(Path.Combine(logs, "photo.jpg"), "not a log");
File.SetLastWriteTimeUtc(Path.Combine(logs, "titano-20261001.log"), DateTime.UtcNow.AddDays(-1));
using var zip = new MemoryStream();
using (var bundle = new DiagnosticsArchive(zip))
{
bundle.AddText("environment.txt", DiagnosticsArchive.SystemReport() + "Server https://admin:" + Secret + "@nas:8443\n");
bundle.AddJson("settings.json", $$"""{ "ProtectedToken": "{{Secret}}", "Theme": "Dark" }""");
bundle.AddJobs("jobs-failed.json",
[
new Job
{
Id = Guid.CreateVersion7(), Kind = JobKind.Render, State = JobState.Failed, Attempts = 2, Created = DateTimeOffset.UtcNow,
Error = "Upload refused: Authorization: Bearer " + Secret, LogTail = "retry with password=" + Secret,
},
]);
bundle.AddLogs(logs, tailBytes: 4096);
}
zip.Position = 0;
using var archive = new ZipArchive(zip, ZipArchiveMode.Read);
var names = archive.Entries.Select(e => e.FullName).Order(StringComparer.Ordinal).ToArray();
Assert.SequenceEqual(new[] { "environment.txt", "jobs-failed.json", "logs/titano-20261001.log", "logs/titano-20261002.log", "settings.json" }, names);
foreach (var entry in archive.Entries)
{
var text = Read(entry);
Assert.False(text.Contains(Secret, StringComparison.Ordinal), $"{entry.FullName} leaks the secret");
}
Assert.Contains("Runtime ", Read(archive.GetEntry("environment.txt")!));
Assert.Contains("\"Theme\": \"Dark\"", Read(archive.GetEntry("settings.json")!));
Assert.Contains("\"kind\": \"Render\"", Read(archive.GetEntry("jobs-failed.json")!));
var tail = Read(archive.GetEntry("logs/titano-20261002.log")!);
Assert.True(tail.StartsWith("[first ", StringComparison.Ordinal), "a cut log says so");
Assert.Contains("line 1999 token: [removed]", tail);
Assert.Less(tail.Length, 5_000);
}
[Test]
public void AMissingLogFolderAddsNothing()
{
using var zip = new MemoryStream();
using (var bundle = new DiagnosticsArchive(zip)) bundle.AddLogs(Path.Combine(context.CreateTempDirectory(), "missing"));
zip.Position = 0;
using var archive = new ZipArchive(zip, ZipArchiveMode.Read);
Assert.Empty(archive.Entries);
}
private static string Read(ZipArchiveEntry entry)
{
using var reader = new StreamReader(entry.Open(), Encoding.UTF8);
return reader.ReadToEnd();
}
}