819f1b4665
Each finding is a focused validation guard or upper bound at a trust boundary.
Highlights:
- Commons-015: EncryptionMetadata ctor now validates Algorithm (AES-256-GCM
only), Kdf (PBKDF2-SHA256 only), Iterations ([100k, 10M]), non-null Salt/IV.
- Transport-004: new BundleUnlockRateLimiter (sliding-window, per-key,
singleton) wired into BundleImporter.LoadAsync; over-budget callers see
BundleUnlockRateLimitedException. Per-bundle 3-strike + per-window cap.
- ESG-022: ExternalSystemClient.InvokeHttpAsync allow-lists the documented
GET/POST/PUT/PATCH/DELETE set (case-insensitive); unknown verbs throw.
- SEL-015: SiteEventLogger queue now bounded (10k cap, DropOldest); dropped
events fault their Task and increment FailedWriteCount so the drop is
observable instead of an unbounded memory growth.
- SEL-017: EventLogQueryService clamps caller-supplied PageSize to a new
MaxQueryPageSize cap (default 500) so int.MaxValue can't OOM the host.
- SEL-020: LogEventAsync rejects severities outside {Info, Warning, Error}
(matches SQLite BINARY-collation query filter).
- InboundAPI-020: ContentType "json" check now case-insensitive
(application/JSON no longer slips through as not-json).
- InboundAPI-024: _knownBadMethods capped at 1000 entries (drops new entries
once full); per-request DB lookup remains the correctness path.
- SR-025: HandleSetStaticAttribute validates the attribute name against the
deployed config; unknown names now return Success=false instead of
leaking orphan override rows into the SQLite store.
- TE-021: MoveTemplateAsync runs the sibling-name-collision check at the
destination, mirroring TemplateFolderService.MoveFolderAsync.
- TE-022: LockEnforcer's once-locked-stays-locked rule now also covers
LockedInDerived (was previously only IsLocked).
New regression tests across 8 test projects (EncryptionMetadata, rate
limiter, ESG client allow-list, SEL bounded channel / PageSize clamp /
severity validation, InboundAPI ContentType + bad-methods cap, SiteRT
unknown-attribute, TemplateEngine MoveTemplate + LockedInDerived).
Build clean; affected suites all green. README regenerated: 93 open (was 104).
Note: a separate manual re-run was needed for the SiteEventLogging hunk
because its initial subagent's source edits never landed on disk despite
reporting success (file-collision-style failure mode).
217 lines
8.6 KiB
C#
217 lines
8.6 KiB
C#
using Akka.Actor;
|
|
using Akka.TestKit.Xunit2;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Akka.TestKit;
|
|
using ScadaLink.Commons.Messages.DataConnection;
|
|
using ScadaLink.Commons.Messages.Instance;
|
|
using ScadaLink.Commons.Types.Flattening;
|
|
using ScadaLink.SiteRuntime.Actors;
|
|
using ScadaLink.SiteRuntime.Persistence;
|
|
using ScadaLink.SiteRuntime.Scripts;
|
|
using System.Text.Json;
|
|
|
|
namespace ScadaLink.SiteRuntime.Tests.Actors;
|
|
|
|
/// <summary>
|
|
/// Regression tests for SiteRuntime-001: Instance.SetAttribute must route writes
|
|
/// to the Data Connection Layer for data-sourced attributes instead of persisting
|
|
/// a local static override.
|
|
/// </summary>
|
|
public class InstanceActorSetAttributeTests : TestKit, IDisposable
|
|
{
|
|
private readonly SiteStorageService _storage;
|
|
private readonly ScriptCompilationService _compilationService;
|
|
private readonly SharedScriptLibrary _sharedScriptLibrary;
|
|
private readonly SiteRuntimeOptions _options;
|
|
private readonly string _dbFile;
|
|
|
|
public InstanceActorSetAttributeTests()
|
|
{
|
|
_dbFile = Path.Combine(Path.GetTempPath(), $"instance-setattr-test-{Guid.NewGuid():N}.db");
|
|
_storage = new SiteStorageService(
|
|
$"Data Source={_dbFile}",
|
|
NullLogger<SiteStorageService>.Instance);
|
|
_storage.InitializeAsync().GetAwaiter().GetResult();
|
|
_compilationService = new ScriptCompilationService(
|
|
NullLogger<ScriptCompilationService>.Instance);
|
|
_sharedScriptLibrary = new SharedScriptLibrary(
|
|
_compilationService, NullLogger<SharedScriptLibrary>.Instance);
|
|
_options = new SiteRuntimeOptions();
|
|
}
|
|
|
|
void IDisposable.Dispose()
|
|
{
|
|
Shutdown();
|
|
try { File.Delete(_dbFile); } catch { /* cleanup */ }
|
|
}
|
|
|
|
private IActorRef CreateInstanceActor(string instanceName, FlattenedConfiguration config, IActorRef? dclManager)
|
|
{
|
|
return ActorOf(Props.Create(() => new InstanceActor(
|
|
instanceName,
|
|
JsonSerializer.Serialize(config),
|
|
_storage,
|
|
_compilationService,
|
|
_sharedScriptLibrary,
|
|
null,
|
|
_options,
|
|
NullLogger<InstanceActor>.Instance,
|
|
dclManager)));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Drains the startup <see cref="SubscribeTagsRequest"/> the Instance Actor emits
|
|
/// to the DCL in PreStart, then returns the next <see cref="WriteTagRequest"/>.
|
|
/// </summary>
|
|
private static WriteTagRequest ExpectWriteTag(TestProbe dclProbe)
|
|
=> dclProbe.FishForMessage<WriteTagRequest>(_ => true, TimeSpan.FromSeconds(5));
|
|
|
|
private static FlattenedConfiguration DataSourcedConfig(string instanceName) => new()
|
|
{
|
|
InstanceUniqueName = instanceName,
|
|
Attributes =
|
|
[
|
|
new ResolvedAttribute
|
|
{
|
|
CanonicalName = "Setpoint",
|
|
Value = "10",
|
|
DataType = "Double",
|
|
DataSourceReference = "/Motor/Setpoint",
|
|
BoundDataConnectionName = "OpcServer1"
|
|
}
|
|
]
|
|
};
|
|
|
|
[Fact]
|
|
public async Task SetAttribute_DataSourcedAttribute_IssuesDclWriteAndDoesNotPersistOverride()
|
|
{
|
|
var config = DataSourcedConfig("PumpDcl1");
|
|
var dclProbe = CreateTestProbe();
|
|
var actor = CreateInstanceActor("PumpDcl1", config, dclProbe.Ref);
|
|
|
|
actor.Tell(new SetStaticAttributeCommand(
|
|
"corr-dcl", "PumpDcl1", "Setpoint", "55", DateTimeOffset.UtcNow));
|
|
|
|
// The Instance Actor must forward a WriteTagRequest to the DCL manager.
|
|
var write = ExpectWriteTag(dclProbe);
|
|
Assert.Equal("OpcServer1", write.ConnectionName);
|
|
Assert.Equal("/Motor/Setpoint", write.TagPath);
|
|
Assert.Equal("55", write.Value);
|
|
|
|
// DCL confirms the write.
|
|
dclProbe.Reply(new WriteTagResponse(write.CorrelationId, true, null, DateTimeOffset.UtcNow));
|
|
var response = ExpectMsg<SetStaticAttributeResponse>(TimeSpan.FromSeconds(5));
|
|
Assert.True(response.Success);
|
|
|
|
// No static override should be persisted for a data-sourced attribute.
|
|
await Task.Delay(300);
|
|
var overrides = await _storage.GetStaticOverridesAsync("PumpDcl1");
|
|
Assert.Empty(overrides);
|
|
}
|
|
|
|
[Fact]
|
|
public void SetAttribute_DataSourcedAttribute_DoesNotOptimisticallyUpdateMemory()
|
|
{
|
|
var config = DataSourcedConfig("PumpDcl2");
|
|
var dclProbe = CreateTestProbe();
|
|
var actor = CreateInstanceActor("PumpDcl2", config, dclProbe.Ref);
|
|
|
|
actor.Tell(new SetStaticAttributeCommand(
|
|
"corr-dcl2", "PumpDcl2", "Setpoint", "999", DateTimeOffset.UtcNow));
|
|
|
|
var write = ExpectWriteTag(dclProbe);
|
|
dclProbe.Reply(new WriteTagResponse(write.CorrelationId, true, null, DateTimeOffset.UtcNow));
|
|
ExpectMsg<SetStaticAttributeResponse>(TimeSpan.FromSeconds(5));
|
|
|
|
// In-memory value must still be the original config value — it is only
|
|
// updated when the subscription delivers the confirmed device value.
|
|
actor.Tell(new GetAttributeRequest("corr-get", "PumpDcl2", "Setpoint", DateTimeOffset.UtcNow));
|
|
var get = ExpectMsg<GetAttributeResponse>(TimeSpan.FromSeconds(5));
|
|
Assert.Equal("10", get.Value?.ToString());
|
|
}
|
|
|
|
[Fact]
|
|
public void SetAttribute_DataSourcedAttribute_DclWriteFailure_ReturnedToCaller()
|
|
{
|
|
var config = DataSourcedConfig("PumpDcl3");
|
|
var dclProbe = CreateTestProbe();
|
|
var actor = CreateInstanceActor("PumpDcl3", config, dclProbe.Ref);
|
|
|
|
actor.Tell(new SetStaticAttributeCommand(
|
|
"corr-dcl3", "PumpDcl3", "Setpoint", "42", DateTimeOffset.UtcNow));
|
|
|
|
var write = ExpectWriteTag(dclProbe);
|
|
dclProbe.Reply(new WriteTagResponse(write.CorrelationId, false, "device rejected write", DateTimeOffset.UtcNow));
|
|
|
|
var response = ExpectMsg<SetStaticAttributeResponse>(TimeSpan.FromSeconds(5));
|
|
Assert.False(response.Success);
|
|
Assert.Contains("device rejected write", response.ErrorMessage);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SetAttribute_StaticAttribute_StillPersistsOverrideAndDoesNotCallDcl()
|
|
{
|
|
var config = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "PumpStatic1",
|
|
Attributes =
|
|
[
|
|
new ResolvedAttribute { CanonicalName = "Label", Value = "Main", DataType = "String" }
|
|
]
|
|
};
|
|
var dclProbe = CreateTestProbe();
|
|
var actor = CreateInstanceActor("PumpStatic1", config, dclProbe.Ref);
|
|
|
|
actor.Tell(new SetStaticAttributeCommand(
|
|
"corr-static", "PumpStatic1", "Label", "Backup", DateTimeOffset.UtcNow));
|
|
|
|
var response = ExpectMsg<SetStaticAttributeResponse>(TimeSpan.FromSeconds(5));
|
|
Assert.True(response.Success);
|
|
|
|
// DCL must NOT receive a write for a static attribute.
|
|
dclProbe.ExpectNoMsg(TimeSpan.FromMilliseconds(300));
|
|
|
|
await Task.Delay(300);
|
|
var overrides = await _storage.GetStaticOverridesAsync("PumpStatic1");
|
|
Assert.Single(overrides);
|
|
Assert.Equal("Backup", overrides["Label"]);
|
|
}
|
|
|
|
// SiteRuntime-025: SetAttribute on an unknown attribute name must NOT
|
|
// pollute the in-memory dictionary, NOT publish a synthetic
|
|
// AttributeValueChanged, and NOT persist a durable override row.
|
|
|
|
[Fact]
|
|
public async Task SetAttribute_UnknownAttribute_ReturnsFailureAndDoesNotPersistOverride()
|
|
{
|
|
var config = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "PumpUnknown",
|
|
Attributes =
|
|
[
|
|
new ResolvedAttribute { CanonicalName = "Label", Value = "Main", DataType = "String" }
|
|
]
|
|
};
|
|
var dclProbe = CreateTestProbe();
|
|
var actor = CreateInstanceActor("PumpUnknown", config, dclProbe.Ref);
|
|
|
|
actor.Tell(new SetStaticAttributeCommand(
|
|
"corr-unknown", "PumpUnknown", "notARealAttr", "x", DateTimeOffset.UtcNow));
|
|
|
|
var response = ExpectMsg<SetStaticAttributeResponse>(TimeSpan.FromSeconds(5));
|
|
Assert.False(response.Success);
|
|
Assert.Contains("Unknown attribute", response.ErrorMessage);
|
|
Assert.Contains("notARealAttr", response.ErrorMessage);
|
|
|
|
// The DCL must NOT receive any write — the attribute does not exist.
|
|
dclProbe.ExpectNoMsg(TimeSpan.FromMilliseconds(300));
|
|
|
|
// No durable override row should be persisted for an unknown attribute —
|
|
// otherwise the polluting key resurrects on every restart via
|
|
// HandleOverridesLoaded.
|
|
await Task.Delay(300);
|
|
var overrides = await _storage.GetStaticOverridesAsync("PumpUnknown");
|
|
Assert.DoesNotContain("notARealAttr", overrides.Keys);
|
|
}
|
|
}
|