5469ece012
TrustVerdictCache keyed on the code hash alone while the verdict also depends on the analysis reference set (and its degraded-fallback mode). The key is now (code SHA-256, degraded flag, base-reference fingerprint, extra-reference digest), length-prefixed. File-backed extra references are digested (path/size/mtime/kind/aliases/embed) so the Central UI run gate's calls are cached under their own key; an extra reference with no stable identity stays uncacheable. The old extras test passed a PortableExecutableReference[] positionally, which binds to the explicit-base overload, so it never exercised the extras path; the tests now use the named argument.
120 lines
6.0 KiB
C#
120 lines
6.0 KiB
C#
using ZB.MOM.WW.ScadaBridge.ScriptAnalysis;
|
|
|
|
namespace ZB.MOM.WW.ScadaBridge.ScriptAnalysis.Tests;
|
|
|
|
/// <summary>
|
|
/// Review 2026-10-21 04/P3: <see cref="ScriptTrustValidator.FindViolations(string, IEnumerable{Microsoft.CodeAnalysis.MetadataReference})"/>
|
|
/// is memoised by code hash for the default (policy) reference set, so the bundle
|
|
/// importer's preview and apply passes — which call it with no change at their call
|
|
/// sites — do not redo a full-framework semantic analysis per script. A cache hit
|
|
/// returns the very same verdict instance, which is what these tests observe.
|
|
/// </summary>
|
|
public class ScriptTrustValidatorCacheTests
|
|
{
|
|
/// <summary>Verifies that the same violating code analysed twice is served from the cache.</summary>
|
|
[Fact]
|
|
public void FindViolations_SameCodeTwice_SecondCallIsCacheHit()
|
|
{
|
|
var code = $"var x = System.IO.File.Exists(\"{Guid.NewGuid()}\");";
|
|
var first = ScriptTrustValidator.FindViolations(code);
|
|
var second = ScriptTrustValidator.FindViolations(code);
|
|
|
|
Assert.NotEmpty(second);
|
|
Assert.Same(first, second); // pre-fix: a fresh list per call
|
|
}
|
|
|
|
/// <summary>Verifies that a clean verdict is cached too.</summary>
|
|
[Fact]
|
|
public void FindViolations_CleanCodeTwice_SecondCallIsCacheHit()
|
|
{
|
|
var code = $"var x = \"{Guid.NewGuid()}\".Length;";
|
|
var first = ScriptTrustValidator.FindViolations(code);
|
|
var second = ScriptTrustValidator.FindViolations(code);
|
|
|
|
Assert.Empty(second);
|
|
Assert.Same(first, second);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Code review 2026-12-02 CMN-5: the extra references are part of the cache key, so a
|
|
/// call with extras is never served the no-extras verdict (or vice versa), while a
|
|
/// repeat call with the same file-backed extras is a cache hit.
|
|
/// </summary>
|
|
[Fact]
|
|
public void FindViolations_WithExtraReferences_KeyedSeparatelyFromNoExtras()
|
|
{
|
|
var code = $"var x = System.IO.File.Exists(\"{Guid.NewGuid()}\");";
|
|
var extra = new[] { Microsoft.CodeAnalysis.MetadataReference.CreateFromFile(typeof(ScriptCompileSurface).Assembly.Location) };
|
|
// Named argument: a PortableExecutableReference[] positional argument binds to the
|
|
// explicit-base-references overload instead (the pre-CMN-5 version of this test did
|
|
// exactly that, so it never exercised the extras path).
|
|
var a = ScriptTrustValidator.FindViolations(code, extraReferences: extra);
|
|
var b = ScriptTrustValidator.FindViolations(code, extraReferences: extra);
|
|
var cached = ScriptTrustValidator.FindViolations(code);
|
|
|
|
Assert.Same(a, b); // same extras → same key
|
|
Assert.NotSame(a, cached); // extras vs none → different keys
|
|
Assert.NotEmpty(a);
|
|
Assert.NotEmpty(cached);
|
|
}
|
|
|
|
/// <summary>An extra reference with no stable identity (an in-memory image) is never cached.</summary>
|
|
[Fact]
|
|
public void FindViolations_WithInMemoryExtraReference_BypassesCache()
|
|
{
|
|
var code = $"var x = System.IO.File.Exists(\"{Guid.NewGuid()}\");";
|
|
var image = File.ReadAllBytes(typeof(ScriptCompileSurface).Assembly.Location);
|
|
var extra = new[] { Microsoft.CodeAnalysis.MetadataReference.CreateFromImage(image) };
|
|
|
|
Assert.Null(ScriptTrustValidator.DigestExtraReferences(extra));
|
|
var a = ScriptTrustValidator.FindViolations(code, extraReferences: extra);
|
|
var b = ScriptTrustValidator.FindViolations(code, extraReferences: extra);
|
|
Assert.NotSame(a, b);
|
|
}
|
|
|
|
/// <summary>Distinct extra-reference sets digest differently; the same set digests the same.</summary>
|
|
[Fact]
|
|
public void DigestExtraReferences_DistinguishesSets()
|
|
{
|
|
var surface = Microsoft.CodeAnalysis.MetadataReference.CreateFromFile(typeof(ScriptCompileSurface).Assembly.Location);
|
|
var surfaceAgain = Microsoft.CodeAnalysis.MetadataReference.CreateFromFile(typeof(ScriptCompileSurface).Assembly.Location);
|
|
var linq = Microsoft.CodeAnalysis.MetadataReference.CreateFromFile(typeof(System.Linq.Enumerable).Assembly.Location);
|
|
var aliased = surface.WithAliases(["other"]);
|
|
|
|
var one = ScriptTrustValidator.DigestExtraReferences([surface]);
|
|
Assert.NotNull(one);
|
|
Assert.Equal(one, ScriptTrustValidator.DigestExtraReferences([surfaceAgain]));
|
|
Assert.NotEqual(one, ScriptTrustValidator.DigestExtraReferences([surface, linq]));
|
|
Assert.NotEqual(one, ScriptTrustValidator.DigestExtraReferences([aliased]));
|
|
}
|
|
|
|
/// <summary>
|
|
/// The key carries every verdict input: code, the degraded flag, the base reference
|
|
/// fingerprint and the extras digest. Changing any one of them changes the key.
|
|
/// </summary>
|
|
[Fact]
|
|
public void BuildKey_ChangesWithEveryInput()
|
|
{
|
|
var baseKey = TrustVerdictCache.BuildKey("code", degraded: false, "fp", "");
|
|
Assert.Equal(baseKey, TrustVerdictCache.BuildKey("code", degraded: false, "fp", ""));
|
|
Assert.NotEqual(baseKey, TrustVerdictCache.BuildKey("code2", degraded: false, "fp", ""));
|
|
Assert.NotEqual(baseKey, TrustVerdictCache.BuildKey("code", degraded: true, "fp", ""));
|
|
Assert.NotEqual(baseKey, TrustVerdictCache.BuildKey("code", degraded: false, "fp2", ""));
|
|
Assert.NotEqual(baseKey, TrustVerdictCache.BuildKey("code", degraded: false, "fp", "x"));
|
|
// Length-prefixing: moving characters between components cannot collide.
|
|
Assert.NotEqual(
|
|
TrustVerdictCache.BuildKey("code", degraded: false, "ab", "c"),
|
|
TrustVerdictCache.BuildKey("code", degraded: false, "a", "bc"));
|
|
}
|
|
|
|
/// <summary>Verifies that the shared verdict cannot be mutated by a caller to poison the cache.</summary>
|
|
[Fact]
|
|
public void FindViolations_ReturnedList_IsReadOnly()
|
|
{
|
|
var code = $"var x = System.IO.File.Exists(\"{Guid.NewGuid()}\");";
|
|
var verdict = ScriptTrustValidator.FindViolations(code);
|
|
Assert.False(verdict is List<string>);
|
|
Assert.True(verdict is System.Collections.Generic.ICollection<string> { IsReadOnly: true });
|
|
}
|
|
}
|