The prefix-free structural check did its job finding the bug, but the table is now verified against all 34 revisions in history and is effectively frozen -- it was guarding a constant. The StatLockInfo vector is a strict subset of the all-symbols vector. Keeps the one test covering the risk that's still live: a future rewrite of Compress (hand-unrolled bit packing) silently corrupting the wire format. Pins all 256 table entries plus the terminal code, and runs in ~10ms. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
68 lines
3.5 KiB
C#
68 lines
3.5 KiB
C#
using System;
|
|
using Server.Network;
|
|
using Xunit;
|
|
|
|
namespace Server.Tests.Network;
|
|
|
|
/// <summary>
|
|
/// Known-answer test for outgoing huffman compression.
|
|
/// The table is a contract with the client's hard-coded decompression tree, so a single wrong
|
|
/// entry -- or a subtle bit-packing error in a future rewrite of <see cref="NetworkCompression.Compress"/> --
|
|
/// silently corrupts every affected packet and desyncs the client's framing with no error.
|
|
/// A round-trip test cannot catch that, since encoder and decoder would share the same mistake.
|
|
/// The expected bytes below were generated from the canonical table, not from the implementation.
|
|
/// </summary>
|
|
public class NetworkCompressionTests
|
|
{
|
|
// Every symbol 0x00-0xFF exactly once. Any single wrong table entry changes these bytes, so this
|
|
// pins all 256 entries plus the terminal code, and exercises the encoder end to end.
|
|
private static ReadOnlySpan<byte> AllSymbolsCompressed =>
|
|
[
|
|
0x3F, 0x13, 0x4E, 0xB4, 0x76, 0xCB, 0x81, 0x8A, 0xB3, 0xCE, 0x76, 0x5E,
|
|
0xAA, 0xEE, 0x2B, 0x7C, 0xCA, 0x74, 0xB3, 0xD9, 0x9F, 0x72, 0x3C, 0xE7,
|
|
0xE9, 0x05, 0x97, 0xB9, 0x1C, 0x6B, 0x70, 0xCD, 0xE9, 0x37, 0x31, 0x69,
|
|
0xAE, 0x70, 0x29, 0x14, 0xA4, 0x13, 0x3F, 0x71, 0x90, 0x39, 0xDF, 0x3A,
|
|
0x5C, 0x03, 0x20, 0x9D, 0x86, 0x4B, 0x33, 0xCA, 0xDC, 0xF9, 0x48, 0x6F,
|
|
0x4B, 0x6F, 0x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 0xAB, 0x82, 0x76, 0x94, 0x1F,
|
|
0x1F, 0x2C, 0xC7, 0xC0, 0x90, 0x34, 0x61, 0x32, 0xC2, 0x51, 0xF3, 0xAC,
|
|
0x72, 0x6F, 0x85, 0xDE, 0x7A, 0xD8, 0xEB, 0xE5, 0x7C, 0x90, 0x3B, 0xC7,
|
|
0x9D, 0xDE, 0xA6, 0x02, 0xC5, 0xED, 0x24, 0x8D, 0xA4, 0xC9, 0x9E, 0xBA,
|
|
0x2D, 0x7F, 0x3D, 0x27, 0xB7, 0x39, 0x25, 0xB9, 0x59, 0xE1, 0x08, 0xE9,
|
|
0xB8, 0x75, 0xEC, 0x75, 0xD9, 0x8B, 0x40
|
|
];
|
|
|
|
[Fact]
|
|
public void Compress_EncodesEverySymbol_MatchingCanonicalTable()
|
|
{
|
|
Span<byte> input = stackalloc byte[256];
|
|
for (var i = 0; i < input.Length; i++)
|
|
{
|
|
input[i] = (byte)i;
|
|
}
|
|
|
|
Span<byte> output = stackalloc byte[512];
|
|
var length = NetworkCompression.Compress(input, output);
|
|
|
|
Assert.Equal(AllSymbolsCompressed.Length, length);
|
|
|
|
var expected = AllSymbolsCompressed;
|
|
for (var i = 0; i < expected.Length; i++)
|
|
{
|
|
Assert.True(expected[i] == output[i], $"Byte {i}: expected 0x{expected[i]:X2}, got 0x{output[i]:X2}");
|
|
}
|
|
}
|
|
}
|