Files
Chronos/tests/Titano.App.Tests/Windows/DiagnosticsBundleTests.cs
T
Claude 03da77277c fix(diagnostics): scrub whole passwords; unique bundle names; no temporary files left behind
- A secret value runs to the next ';' or the end of the line unless quoted
  (backslash and doubled-quote escapes), so passwords with spaces, commas or
  quotes never leak in part; URL passwords run to the last '@' before the path.
- Desktop bundles get " (2)", " (3)"… instead of overwriting one made in the
  same minute, and a bundle that fails half-way deletes its .tmp file.
- The server builds its bundle in a temporary file that no crash leaves behind
  (DeleteOnClose on Windows, unlinked at once elsewhere).
- Tests: passwords with spaces, commas, quotes and '@'; a DbConnectionString in
  the server bundle; bundle name collisions and failure cleanup.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-03 10:05:45 +00:00

98 lines
4.5 KiB
C#

using System.IO;
using System.IO.Compression;
using Titano.App.Infrastructure;
using Titano.App.Settings;
using Titano.Core.Video;
using Titano.Testing;
namespace Titano.App.Tests.Windows;
/// <summary>The desktop diagnostics bundle: every entry present, the saved server token and passwords never inside.</summary>
[WindowsOnly]
public sealed class DiagnosticsBundleTests(TestContext context)
{
private const string Token = "AQAAANCMnd8BFdERjHoAwE-saved-token";
[Test]
public async Task TheBundleCarriesReportsAndLogsButNoSecrets()
{
var environment = AppEnvironment.ForCapture();
try
{
var settings = new AppSettings { Language = "en", ServerUrl = "https://nas.local:8443", ServerUsername = "alice", ProtectedToken = Token };
var services = new AppServices(environment, settings, () => { }, new NoDialogs()) { Encoders = new OneEncoder() };
await File.WriteAllTextAsync(Path.Combine(environment.LogDirectory, "titano-20261002.log"), $"signed in with password={Token}\n");
var path = await DiagnosticsBundle.CreateAsync(services, context.CreateTempDirectory(), ["Self-test H.264 Test: passed"]);
using var zip = ZipFile.OpenRead(path);
var texts = zip.Entries.ToDictionary(e => e.FullName, e =>
{
using var reader = new StreamReader(e.Open());
return reader.ReadToEnd();
});
Assert.SequenceEqual(new[] { "codecs.txt", "environment.txt", "jobs-failed.json", "logs/titano-20261002.log", "settings.json" },
texts.Keys.Order(StringComparer.Ordinal));
foreach (var (name, text) in texts) Assert.False(text.Contains(Token, StringComparison.Ordinal), $"{name} leaks the token");
Assert.Contains("Self-test H.264 Test: passed", texts["environment.txt"]);
Assert.Contains("Test encoder", texts["codecs.txt"]);
Assert.Contains("\"ServerUsername\": \"alice\"", texts["settings.json"]);
Assert.Contains("\"ProtectedToken\": \"[removed]\"", texts["settings.json"]);
}
finally
{
environment.DeleteCaptureData();
}
}
[Test]
public async Task BundlesNeverOverwriteEachOtherAndAFailedOneLeavesNothing()
{
var environment = AppEnvironment.ForCapture();
try
{
var services = new AppServices(environment, new AppSettings { Language = "en" }, () => { }, new NoDialogs()) { Encoders = new OneEncoder() };
await File.WriteAllTextAsync(Path.Combine(environment.LogDirectory, "titano-20261002.log"), "started\n");
var folder = context.CreateTempDirectory();
var first = await DiagnosticsBundle.CreateAsync(services, folder);
var second = await DiagnosticsBundle.CreateAsync(services, folder);
Assert.NotEqual(first, second);
Assert.SequenceEqual(new[] { first, second }.Order(StringComparer.Ordinal), Directory.GetFiles(folder).Order(StringComparer.Ordinal));
// The encoder inventory runs after the job query and before the zip is written: cancelling there fails the
// bundle half-way, once its temporary file exists.
using var cts = new CancellationTokenSource();
services.Encoders = new OneEncoder(cts.Cancel);
var empty = context.CreateTempDirectory();
await Assert.ThrowsAsync<OperationCanceledException>(() => DiagnosticsBundle.CreateAsync(services, empty, ct: cts.Token));
Assert.Empty(Directory.GetFiles(empty));
}
finally
{
environment.DeleteCaptureData();
}
}
private sealed class OneEncoder(Action? onDetect = null) : IEncoderInventory
{
public string? Adapter() => "Test adapter";
public IReadOnlyList<EncoderRow> Detect()
{
onDetect?.Invoke();
return [new EncoderRow("H.264", "Test encoder", null, EncoderSource.MediaFoundation, true, false, 4096, 2304, VideoCodec.H264)];
}
public Task<SelfTestResult> SelfTestAsync(EncoderRow row, CancellationToken ct) => Task.FromResult(new SelfTestResult(true, 100, null));
}
private sealed class NoDialogs : IAppDialogs
{
public string? PickFolder(string title, string? initialFolder = null) => null;
public string? PickJsonFile(string title) => null;
public bool Confirm(ConfirmRequest request) => false;
public bool SignIn(string server, string? username, Func<string, string, Task<string?>> signIn) => false;
}
}