fix(testing): escape XML-invalid characters in JUnit reports

A theory case containing a control character crashed the JUnit writer
after every test had passed, failing the Data test step on all runners.

Also clears a stale rejected certificate fingerprint once a later
handshake is accepted or the fingerprint is trusted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-02 10:43:36 +00:00
1 parent 95dbf53569
commit 3223c8af39
2 files changed
+48 -12

No files matched your search

@@ -6,25 +6,39 @@ namespace Titano.Protocol.Client;
/// <summary>
/// Trust-on-first-use certificate validation: a certificate is accepted when it chains to a trusted root or when
/// its SHA-256 fingerprint is in the caller's trusted set. The last unknown fingerprint is remembered so the UI can
/// ask the user once and add it.
/// its SHA-256 fingerprint is in the caller's trusted set. The fingerprint rejected by the latest handshake is
/// remembered (and cleared by the next accepted one) so the UI can ask the user once and add it.
/// </summary>
public sealed class CertificatePinning(IEnumerable<string> trustedFingerprints)
{
private readonly HashSet<string> _trusted = new(trustedFingerprints.Select(Normalize), StringComparer.Ordinal);
private readonly Lock _gate = new();
private string? _lastRejected;
public string? LastRejectedFingerprint { get; private set; }
public string? LastRejectedFingerprint
{
get { lock (_gate) return _lastRejected; }
}
public void Trust(string fingerprint) => _trusted.Add(Normalize(fingerprint));
public void Trust(string fingerprint)
{
lock (_gate)
{
_trusted.Add(Normalize(fingerprint));
_lastRejected = null;
}
}
public bool Validate(object sender, X509Certificate? certificate, X509Chain? chain, SslPolicyErrors errors)
{
if (certificate is null) return false;
if (errors == SslPolicyErrors.None) return true;
var fingerprint = Fingerprint(certificate);
if (_trusted.Contains(fingerprint)) return true;
LastRejectedFingerprint = fingerprint;
return false;
var fingerprint = errors == SslPolicyErrors.None ? null : Fingerprint(certificate);
lock (_gate)
{
var accepted = fingerprint is null || _trusted.Contains(fingerprint);
_lastRejected = accepted ? null : fingerprint;
return accepted;
}
}
public static string Fingerprint(X509Certificate certificate) =>
+25 -3
View File
@@ -253,8 +253,8 @@ public static class TestRunner
foreach (var r in results.OrderBy(r => r.Test.FullName, StringComparer.Ordinal))
{
w.WriteStartElement("testcase");
w.WriteAttributeString("classname", r.Test.Type.FullName);
w.WriteAttributeString("name", r.Test.FullName[(r.Test.Type.FullName!.Length + 1)..]);
w.WriteAttributeString("classname", Clean(r.Test.Type.FullName!));
w.WriteAttributeString("name", Clean(r.Test.FullName[(r.Test.Type.FullName!.Length + 1)..]));
w.WriteAttributeString("time", r.Duration.TotalSeconds.ToString("F3", CultureInfo.InvariantCulture));
if (r.Outcome == TestOutcome.Failed)
{
@@ -276,7 +276,29 @@ public static class TestRunner
w.WriteEndDocument();
}
private static string Clean(string s) => new(s.Where(XmlConvert.IsXmlChar).ToArray());
/// <summary>Escapes characters XML cannot carry (theory cases may contain control characters) as <c>\uXXXX</c>.</summary>
private static string Clean(string s)
{
StringBuilder? sb = null;
for (var i = 0; i < s.Length; i++)
{
var c = s[i];
if (XmlConvert.IsXmlChar(c))
{
sb?.Append(c);
continue;
}
if (char.IsHighSurrogate(c) && i + 1 < s.Length && XmlConvert.IsXmlSurrogatePair(s[i + 1], c))
{
sb?.Append(c).Append(s[i + 1]);
i++;
continue;
}
sb ??= new StringBuilder(s, 0, i, s.Length + 8);
sb.Append(CultureInfo.InvariantCulture, $"\\u{(int)c:X4}");
}
return sb?.ToString() ?? s;
}
private sealed class Options
{