Path: redb/redb.Tests.Integration/Tests/Unit/RedbHashCanonCollisionTests.cs Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Tests.Integration/Tests/Unit/RedbHashCanonCollisionTests.cs ---- using System.Globalization; using System.Security.Cryptography; using System.Text; using redb.Core.Utils; using redb.Tests.Integration.Models; namespace redb.Tests.Integration.Tests.Unit; /// /// The hash canon tells apart values whose plain joined strings coincide: a separator inside a string ('|', ',', '=') /// and a dictionary key that is not a simple value. ChangeTracking skips an object, a collection or a nested class whose /// hash did not move, so a collision is a lost change. A value with no separator and no escape character keeps the /// canon it had before the escaping, so stored hashes of such objects stay valid. /// public sealed class RedbHashCanonCollisionTests { private static Guid HashOf(TProps props) where TProps : class, new() => RedbHash.ComputeForProps(props)!.Value; private static Guid Md5(string canon) => new(MD5.HashData(Encoding.UTF8.GetBytes(canon))); [Fact] public void PipeInsideAString_DoesNotCollide() => HashOf(new HashCanonPair { A = "x|y", B = "" }) .Should().NotBe(HashOf(new HashCanonPair { A = "x", B = "y|" })); [Fact] public void CommaInsideAnElement_DoesNotCollide() => HashOf(new HashCanonProbeProps { Tags = ["a,b"] }) .Should().NotBe(HashOf(new HashCanonProbeProps { Tags = ["a", "b"] })); [Fact] public void EqualsAndCommaInsideADictionaryValue_DoNotCollide() => HashOf(new HashCanonProbeProps { Dict = new() { ["a"] = "1,b=2" } }) .Should().NotBe(HashOf(new HashCanonProbeProps { Dict = new() { ["a"] = "1", ["b"] = "2" } })); [Fact] public void EqualsInsideADictionaryKey_DoesNotCollide() => HashOf(new HashCanonProbeProps { Dict = new() { ["a=b"] = "c" } }) .Should().NotBe(HashOf(new HashCanonProbeProps { Dict = new() { ["a"] = "b=c" } })); [Fact] public void EscapeCharacterItself_DoesNotCollide() => HashOf(new HashCanonPair { A = "x\u0001", B = "|y" }) .Should().NotBe(HashOf(new HashCanonPair { A = "x\u0001|", B = "y" })); /// /// A hash stored before the escaping is the hash of the plain canon. A value that held the escaped form of the new /// value as text must not hash like it, or a node that upgraded skips the change against the stored hash: with a /// backslash as the escape, "a\,b" stored before and "a,b" saved after shared the canon "[a\,b]". /// [Fact] public void EscapedCanon_DoesNotMatchACanonStoredBeforeTheEscaping() { // The canons stored before for Tags = ["a\,b"] and for A = "x\|y", B = "\e". HashOf(new HashCanonProbeProps { Tags = ["a,b"] }).Should().NotBe(Md5("||||||[a\\,b]")); HashOf(new HashCanonPair { A = "x|y", B = "" }).Should().NotBe(Md5("x\\|y|\\e")); } [Fact] public void NullElementAndTheStringNull_DoNotCollide() => HashOf(new HashCanonProbeProps { Tags = [null] }) .Should().NotBe(HashOf(new HashCanonProbeProps { Tags = ["null"] })); [Fact] public void EmptyCollectionAndOneEmptyString_DoNotCollide() => HashOf(new HashCanonProbeProps { Tags = [] }) .Should().NotBe(HashOf(new HashCanonProbeProps { Tags = [""] })); [Fact] public void NullDictionaryValueAndTheStringNull_DoNotCollide() => HashOf(new HashCanonProbeProps { Dict = new() { ["a"] = null! } }) .Should().NotBe(HashOf(new HashCanonProbeProps { Dict = new() { ["a"] = "null" } })); [Fact] public void NullPropertyAndAnEmptyString_DoNotCollide() => HashOf(new HashCanonPair { A = null, B = "b" }) .Should().NotBe(HashOf(new HashCanonPair { A = "", B = "b" })); [Fact] public void ClassKeysWithoutToString_DoNotCollide() => HashOf(new HashCanonClassKeyProps { ByKey = new() { [new HashCanonKey { Code = "a", Rank = 1 }] = "v" } }) .Should().NotBe(HashOf(new HashCanonClassKeyProps { ByKey = new() { [new HashCanonKey { Code = "b", Rank = 2 }] = "v" } })); [Fact] public void TupleKeyOfDoubleAndDate_HashesTheSame_UnderEveryCulture() { var at = new DateTime(2026, 9, 16, 12, 30, 15, DateTimeKind.Utc); HashCanonTupleKeyProps Props() => new() { ByTuple = new() { [(1.5, at)] = "v" } }; var ruRu = Under("ru-RU", () => HashOf(Props())); var enUs = Under("en-US", () => HashOf(Props())); var deDe = Under("de-DE", () => HashOf(Props())); ruRu.Should().Be(enUs); ruRu.Should().Be(deDe); } /// /// The canon of an object with no separator and no escape character in its strings, written out by hand: the /// escaping must leave it as it was, so the hashes stored for such objects stay valid. /// [Fact] public void StringsWithoutSeparators_KeepTheirCanon() { var props = new HashCanonProbeProps { A = "x", B = "y z", Label = null, Tags = ["a", null, "b.c", "c:\\d"], Dict = new() { ["k2"] = "v 2", ["k1"] = "v1" }, Inner = new HashCanonPair { A = "p", B = "q" }, Items = [new HashCanonPair { A = "p", B = "q" }, new HashCanonPair { A = "r", B = null }], }; var pq = Md5("p|q").ToString("N"); var r = Md5("r|").ToString("N"); // Ordinal order of the properties: A, B, Dict, Inner, Items, Label, Tags. var canon = $"x|y z|{{k1=v1,k2=v 2}}|{pq}|[{pq},{r}]||[a,null,b.c,c:\\d]"; HashOf(props).Should().Be(Md5(canon)); } private static Guid Under(string culture, Func compute) { var previous = CultureInfo.CurrentCulture; CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo(culture); try { return compute(); } finally { CultureInfo.CurrentCulture = previous; } } }