|
1 | 1 | using System.Collections.Concurrent; |
2 | 2 | using System.Reflection; |
| 3 | +using System.Runtime.CompilerServices; |
3 | 4 | using System.Threading.Channels; |
4 | 5 | using System.Threading.Tasks.Dataflow; |
5 | 6 | using System.Threading.Tasks; |
6 | 7 |
|
7 | 8 | namespace FastCloner.Tests; |
8 | 9 | public class FailureHypothesisTests |
9 | 10 | { |
| 11 | + /// <summary> |
| 12 | + /// Demonstrates a weakness: <see cref="FastClonerSafeTypes"/> assumes any class that overrides |
| 13 | + /// <c>GetHashCode</c> has value-based hashing (<c>HasStableHashSemantics == true</c>). This drives |
| 14 | + /// hash-based collections through a memberwise (raw field) clone path that copies the internal |
| 15 | + /// <c>_slots</c>/<c>_buckets</c> arrays verbatim. When the override actually returns an identity-based |
| 16 | + /// hash (e.g. <c>RuntimeHelpers.GetHashCode(this)</c>), the cloned bucket entries store the *original* |
| 17 | + /// object's identity hash, but the elements inside are themselves deep-cloned and therefore have a |
| 18 | + /// brand-new identity hash. The cloned set/dictionary is structurally corrupt: lookups by the |
| 19 | + /// cloned key miss, even though the key is the very element stored in the clone. |
| 20 | + /// </summary> |
| 21 | + private sealed class IdentityHashedKey |
| 22 | + { |
| 23 | + public string Tag { get; set; } = ""; |
| 24 | + public override int GetHashCode() => RuntimeHelpers.GetHashCode(this); |
| 25 | + public override bool Equals(object? obj) => ReferenceEquals(this, obj); |
| 26 | + } |
| 27 | + |
| 28 | + [Test] |
| 29 | + public async Task HashSet_With_IdentityBased_OverriddenGetHashCode_Should_Be_Lookupable_After_Clone() |
| 30 | + { |
| 31 | + IdentityHashedKey item = new IdentityHashedKey { Tag = "a" }; |
| 32 | + HashSet<IdentityHashedKey> original = [item]; |
| 33 | + |
| 34 | + HashSet<IdentityHashedKey> clone = original.DeepClone(); |
| 35 | + |
| 36 | + await Assert.That(clone).IsNotSameReferenceAs(original); |
| 37 | + await Assert.That(clone.Count).IsEqualTo(1); |
| 38 | + |
| 39 | + IdentityHashedKey cloneItem = clone.Single(); |
| 40 | + await Assert.That(cloneItem).IsNotSameReferenceAs(item) |
| 41 | + .Because("Element is a reference type and should be deep-cloned"); |
| 42 | + |
| 43 | + await Assert.That(clone.Contains(cloneItem)).IsTrue() |
| 44 | + .Because("Looking up the actual element of the cloned set must succeed; " + |
| 45 | + "FastCloner copies the original identity-based hash into the cloned bucket, " + |
| 46 | + "while the cloned element has a new identity hash, so lookup misses."); |
| 47 | + } |
| 48 | + |
| 49 | + [Test] |
| 50 | + public async Task Dictionary_With_IdentityBased_OverriddenGetHashCode_Key_Should_Be_Lookupable_After_Clone() |
| 51 | + { |
| 52 | + IdentityHashedKey key = new IdentityHashedKey { Tag = "k" }; |
| 53 | + Dictionary<IdentityHashedKey, int> original = new Dictionary<IdentityHashedKey, int> { [key] = 42 }; |
| 54 | + |
| 55 | + Dictionary<IdentityHashedKey, int> clone = original.DeepClone(); |
| 56 | + |
| 57 | + await Assert.That(clone).IsNotSameReferenceAs(original); |
| 58 | + await Assert.That(clone.Count).IsEqualTo(1); |
| 59 | + |
| 60 | + IdentityHashedKey cloneKey = clone.Keys.Single(); |
| 61 | + await Assert.That(cloneKey).IsNotSameReferenceAs(key); |
| 62 | + |
| 63 | + await Assert.That(clone.TryGetValue(cloneKey, out int value)).IsTrue() |
| 64 | + .Because("The cloned dictionary must be able to find its own key. " + |
| 65 | + "FastCloner stores stale identity hashes from the original key in the cloned bucket."); |
| 66 | + await Assert.That(value).IsEqualTo(42); |
| 67 | + } |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// Type whose override would normally throw on a default-state probe instance (Tag is null, ToUpper NREs). |
| 71 | + /// Without an opt-in, the probe catches the throw and conservatively rebuilds the collection. With |
| 72 | + /// <see cref="FastClonerStableHashAttribute"/> the type author asserts the override is value-based, so |
| 73 | + /// FastCloner skips the probe and uses the fast memberwise path. Lookups in the cloned set must still work. |
| 74 | + /// </summary> |
| 75 | + [FastClonerStableHash] |
| 76 | + private sealed class ProbeUnfriendlyButStableKey |
| 77 | + { |
| 78 | + public string Tag { get; set; } = ""; |
| 79 | + public override int GetHashCode() => Tag.ToUpperInvariant().GetHashCode(); |
| 80 | + public override bool Equals(object? obj) |
| 81 | + => obj is ProbeUnfriendlyButStableKey other |
| 82 | + && string.Equals(Tag, other.Tag, StringComparison.OrdinalIgnoreCase); |
| 83 | + } |
| 84 | + |
| 85 | + /// <summary> |
| 86 | + /// Same hash semantics as <see cref="ProbeUnfriendlyButStableKey"/> but without the attribute. Used to |
| 87 | + /// assert that the attribute really is what changes the verdict (not some unrelated probe success). |
| 88 | + /// </summary> |
| 89 | + private sealed class ProbeUnfriendlyKeyNoAttribute |
| 90 | + { |
| 91 | + public string Tag { get; set; } = ""; |
| 92 | + public override int GetHashCode() => Tag.ToUpperInvariant().GetHashCode(); |
| 93 | + public override bool Equals(object? obj) |
| 94 | + => obj is ProbeUnfriendlyKeyNoAttribute other |
| 95 | + && string.Equals(Tag, other.Tag, StringComparison.OrdinalIgnoreCase); |
| 96 | + } |
| 97 | + |
| 98 | + [Test] |
| 99 | + public async Task FastClonerStableHashAttribute_Marks_Type_As_Stable() |
| 100 | + { |
| 101 | + await Assert.That(global::FastCloner.Code.FastClonerSafeTypes.HasStableHashSemantics(typeof(ProbeUnfriendlyButStableKey))) |
| 102 | + .IsTrue() |
| 103 | + .Because("[FastClonerStableHash] must short-circuit the probe and declare stable semantics, " + |
| 104 | + "even when GetHashCode would throw on default-state instances."); |
| 105 | + |
| 106 | + // Unchanged behavior for the attribute-less twin: probe throws on null Tag, conservative rebuild. |
| 107 | + await Assert.That(global::FastCloner.Code.FastClonerSafeTypes.HasStableHashSemantics(typeof(ProbeUnfriendlyKeyNoAttribute))) |
| 108 | + .IsFalse() |
| 109 | + .Because("Without the opt-in, a probe that NREs on default state must fall back to rebuild."); |
| 110 | + } |
| 111 | + |
| 112 | + [Test] |
| 113 | + public async Task FastClonerStableHashAttribute_Allows_FastPath_With_Correct_Lookup() |
| 114 | + { |
| 115 | + ProbeUnfriendlyButStableKey key = new ProbeUnfriendlyButStableKey { Tag = "Alpha" }; |
| 116 | + HashSet<ProbeUnfriendlyButStableKey> original = [key]; |
| 117 | + |
| 118 | + HashSet<ProbeUnfriendlyButStableKey> clone = original.DeepClone(); |
| 119 | + |
| 120 | + await Assert.That(clone).IsNotSameReferenceAs(original); |
| 121 | + await Assert.That(clone.Count).IsEqualTo(1); |
| 122 | + |
| 123 | + ProbeUnfriendlyButStableKey cloneKey = clone.Single(); |
| 124 | + await Assert.That(cloneKey).IsNotSameReferenceAs(key); |
| 125 | + await Assert.That(clone.Contains(cloneKey)).IsTrue(); |
| 126 | + |
| 127 | + // Equality is case-insensitive, so a fresh key with different casing must also resolve. |
| 128 | + await Assert.That(clone.Contains(new ProbeUnfriendlyButStableKey { Tag = "alpha" })).IsTrue() |
| 129 | + .Because("Hash is value-based on Tag (case-insensitive) and survives the clone unchanged."); |
| 130 | + } |
| 131 | + |
10 | 132 | [Test] |
11 | 133 | public async Task BufferBlock_Should_Be_Deep_Cloned_Independently() |
12 | 134 | { |
|
0 commit comments