513fa1aa16
Comment/XML-doc changes only: - add summaries to ~10k test methods, fixtures, fakes and helpers (+ <param> on theories, <returns> on async Task methods, <inheritdoc/> on interface implementations the checker can resolve) - strip ~2.6k project-tracking references (WP*, P*-*, row-NN, Task N, #NN, Component-0NN) from comments, keeping the explanatory text; reword false positives in comments only - real docs on fakes implementing BCL/library interfaces (IDisposable, ILogger, IOptionsMonitor, IAsyncLifetime) - reword the last 5 src/ false positives (CWE-1236, failures-1, year-0001, M365) Only non-comment edits: four one-line nested classes split across lines so members can carry docs. Build 0 errors / 0 warnings.
194 lines
13 KiB
C#
194 lines
13 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Types.Audit;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Types.Debugging;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
|
|
using ZB.MOM.WW.ScadaBridge.ConfigurationDatabase;
|
|
using ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Repositories;
|
|
using ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests.Migrations;
|
|
using Xunit;
|
|
|
|
namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests.Repositories;
|
|
|
|
/// <summary>
|
|
/// Flow Debugger Replay (design 2026-09-05 §7.1): <see cref="AuditLogRepository.GetTraceEventsAsync"/>
|
|
/// returns the whole chain's ScriptTrace rows plus the inbound row, from ANY entry id, ordered,
|
|
/// and excludes unrelated and non-trace rows. Runs against the shared MSSQL fixture.
|
|
/// </summary>
|
|
public class AuditLogRepositoryTraceEventsTests : IClassFixture<MsSqlMigrationFixture>
|
|
{
|
|
private readonly MsSqlMigrationFixture _fixture;
|
|
/// <summary>Initializes the test class with the shared SQL Server migration fixture.</summary>
|
|
/// <param name="fixture">The shared SQL Server migration fixture.</param>
|
|
public AuditLogRepositoryTraceEventsTests(MsSqlMigrationFixture fixture) => _fixture = fixture;
|
|
|
|
private ScadaBridgeDbContext CreateContext() => new(
|
|
new DbContextOptionsBuilder<ScadaBridgeDbContext>().UseSqlServer(_fixture.ConnectionString).Options);
|
|
|
|
/// <summary>Verifies that scoped tree and replay reads exclude other sites' rows and retain central rows.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[SkippableFact]
|
|
public async Task ScopedTreeAndReplay_ExcludeOtherSite_AndRetainCentralRows()
|
|
{
|
|
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
|
|
var root = Guid.NewGuid(); var childA = Guid.NewGuid(); var childB = Guid.NewGuid();
|
|
var central = ScadaBridgeAuditEventFactory.Create(AuditChannel.ApiInbound, AuditKind.InboundRequest, AuditStatus.Delivered, executionId: root);
|
|
var allowed = ScadaBridgeAuditEventFactory.Create(AuditChannel.ScriptTrace, AuditKind.ScriptRun, AuditStatus.Delivered, executionId: childA, parentExecutionId: root, sourceSiteId: "site-a");
|
|
var denied = ScadaBridgeAuditEventFactory.Create(AuditChannel.DbOutbound, AuditKind.DbWrite, AuditStatus.Delivered, executionId: childB, parentExecutionId: root, sourceSiteId: "site-b", responseSummary: "sensitive-sql-values");
|
|
await using var context = CreateContext(); var repo = new AuditLogRepository(context);
|
|
foreach (var row in new[] { central, allowed, denied }) await repo.InsertIfNotExistsAsync(row);
|
|
var tree = await repo.GetExecutionTreeAsync(childA, permittedSiteIds: new[] { "site-a" });
|
|
Assert.Contains(tree, node => node.ExecutionId == root);
|
|
Assert.DoesNotContain(tree, node => node.ExecutionId == childB);
|
|
var replay = await repo.GetTraceEventsAsync(childA, permittedSiteIds: new[] { "site-a" });
|
|
Assert.Equal(new[] { central.EventId, allowed.EventId }.Order(), replay.Select(e => e.EventId).Order());
|
|
Assert.DoesNotContain(replay, row => row.DetailsJson?.Contains("sensitive-sql-values") == true);
|
|
}
|
|
|
|
/// <summary>Verifies that replay bounds the number of rows and their UTF-8 byte size while keeping the root row.</summary>
|
|
/// <param name="count">The number of audit rows to seed.</param>
|
|
/// <param name="payloadLength">The length of each row's details payload.</param>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[SkippableTheory]
|
|
[InlineData(600, 0)]
|
|
[InlineData(200, 10000)]
|
|
[InlineData(1, 1100000)]
|
|
public async Task Replay_BoundsRowsAndUtf8Bytes_AndKeepsRoot(int count, int payloadLength)
|
|
{
|
|
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
|
|
var root = Guid.NewGuid(); var start = DateTime.UtcNow;
|
|
await using var context = CreateContext(); var repo = new AuditLogRepository(context);
|
|
for (var i = 0; i < count; i++)
|
|
await repo.InsertIfNotExistsAsync(ScadaBridgeAuditEventFactory.Create(AuditChannel.ScriptTrace,
|
|
AuditKind.ScriptRun, AuditStatus.Delivered, executionId: root, occurredAtUtc: payloadLength == 0 ? start : start.AddMilliseconds(i),
|
|
responseSummary: new string('界', payloadLength)));
|
|
var rows = await repo.GetTraceEventsAsync(root);
|
|
var batch = Assert.IsType<TraceEventBatch>(rows);
|
|
Assert.True(batch.Truncated);
|
|
Assert.Equal(root, batch.RootExecutionId);
|
|
Assert.True(batch.Count <= 500);
|
|
if (payloadLength == 0) Assert.Equal(500, batch.Count);
|
|
if (payloadLength > 1024 * 1024) Assert.Empty(batch);
|
|
Assert.Equal(batch.Count, batch.Select(e => e.EventId).Distinct().Count());
|
|
Assert.True(batch.Sum(e => System.Text.Json.JsonSerializer.SerializeToUtf8Bytes(e).Length) <= 1024 * 1024);
|
|
}
|
|
|
|
/// <summary>Verifies that the tree read returns the inbound row plus chain trace rows in order from any entry point.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[SkippableFact]
|
|
public async Task ReturnsInboundPlusChainTraceRows_Ordered_FromAnyEntry()
|
|
{
|
|
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
|
|
var siteId = "test-trace-" + Guid.NewGuid().ToString("N")[..8];
|
|
var t0 = new DateTime(2026, 9, 5, 12, 0, 0, DateTimeKind.Utc);
|
|
var root = Guid.NewGuid();
|
|
var siteRun = Guid.NewGuid();
|
|
var cascade = Guid.NewGuid();
|
|
|
|
var inbound = ScadaBridgeAuditEventFactory.Create(AuditChannel.ApiInbound, AuditKind.InboundRequest, AuditStatus.Delivered,
|
|
occurredAtUtc: t0, actor: "key", target: "M", executionId: root, sourceSiteId: siteId, responseSummary: "{}");
|
|
var route = ScadaBridgeAuditEventFactory.Create(AuditChannel.ScriptTrace, AuditKind.RouteCall, AuditStatus.Delivered,
|
|
occurredAtUtc: t0.AddMilliseconds(10), target: "I.S", executionId: root, sourceSiteId: siteId,
|
|
extra: new ScriptTraceExtra { ChildExecutionId = siteRun }.ToJson());
|
|
var run = ScadaBridgeAuditEventFactory.Create(AuditChannel.ScriptTrace, AuditKind.ScriptRun, AuditStatus.Delivered,
|
|
occurredAtUtc: t0.AddMilliseconds(20), target: "S", executionId: siteRun, parentExecutionId: root, sourceSiteId: siteId);
|
|
var write = ScadaBridgeAuditEventFactory.Create(AuditChannel.ScriptTrace, AuditKind.AttributeWrite, AuditStatus.Delivered,
|
|
occurredAtUtc: t0.AddMilliseconds(30), target: "A", executionId: siteRun, parentExecutionId: root, sourceSiteId: siteId);
|
|
var cascadeRun = ScadaBridgeAuditEventFactory.Create(AuditChannel.ScriptTrace, AuditKind.ScriptRun, AuditStatus.Delivered,
|
|
occurredAtUtc: t0.AddMilliseconds(40), target: "C", executionId: cascade, parentExecutionId: siteRun, sourceSiteId: siteId);
|
|
// Same chain but outside the replay channel set: a secured-write row — excluded
|
|
// (outbound-call plan 2026-09-06 D6 widened the set; SecuredWrite stays out).
|
|
var outbound = ScadaBridgeAuditEventFactory.Create(AuditChannel.SecuredWrite, AuditKind.SecuredWriteSubmit, AuditStatus.Delivered,
|
|
occurredAtUtc: t0.AddMilliseconds(25), target: "ERP.x", executionId: siteRun, parentExecutionId: root, sourceSiteId: siteId);
|
|
// Unrelated chain.
|
|
var other = ScadaBridgeAuditEventFactory.Create(AuditChannel.ScriptTrace, AuditKind.ScriptRun, AuditStatus.Delivered,
|
|
occurredAtUtc: t0.AddMilliseconds(5), target: "Z", executionId: Guid.NewGuid(), sourceSiteId: siteId);
|
|
|
|
await using (var ctx = CreateContext())
|
|
{
|
|
var repo = new AuditLogRepository(ctx);
|
|
foreach (var e in new[] { cascadeRun, other, write, outbound, inbound, run, route })
|
|
await repo.InsertIfNotExistsAsync(e);
|
|
}
|
|
|
|
await using (var ctx = CreateContext())
|
|
{
|
|
var repo = new AuditLogRepository(ctx);
|
|
foreach (var entry in new[] { root, siteRun, cascade })
|
|
{
|
|
var events = await repo.GetTraceEventsAsync(entry);
|
|
Assert.Equal(
|
|
new[] { inbound.EventId, route.EventId, run.EventId, write.EventId, cascadeRun.EventId },
|
|
events.Select(e => e.EventId).ToArray());
|
|
Assert.Equal(inbound, events[0]);
|
|
Assert.Equal(cascadeRun, events[4]);
|
|
}
|
|
|
|
Assert.Empty(await repo.GetTraceEventsAsync(Guid.NewGuid()));
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Replay returns the chain's ScriptTrace, ApiOutbound,
|
|
/// DbOutbound and Notification rows plus the InboundRequest row, in (OccurredAtUtc, EventId)
|
|
/// order; SecuredWrite, Cluster and InboundAuthFailure rows in the same tree stay excluded.
|
|
/// </summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[SkippableFact]
|
|
public async Task IncludesOutboundApiDbAndNotificationRows_ExcludesOtherChannels()
|
|
{
|
|
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
|
|
var siteId = "test-trace-" + Guid.NewGuid().ToString("N")[..8];
|
|
var t0 = new DateTime(2026, 9, 6, 12, 0, 0, DateTimeKind.Utc);
|
|
var root = Guid.NewGuid();
|
|
var siteRun = Guid.NewGuid();
|
|
|
|
var inbound = ScadaBridgeAuditEventFactory.Create(AuditChannel.ApiInbound, AuditKind.InboundRequest, AuditStatus.Delivered,
|
|
occurredAtUtc: t0, actor: "key", target: "M", executionId: root, sourceSiteId: siteId, responseSummary: "{}");
|
|
var route = ScadaBridgeAuditEventFactory.Create(AuditChannel.ScriptTrace, AuditKind.RouteCall, AuditStatus.Delivered,
|
|
occurredAtUtc: t0.AddMilliseconds(10), target: "I.S", executionId: root, sourceSiteId: siteId,
|
|
extra: new ScriptTraceExtra { ChildExecutionId = siteRun }.ToJson());
|
|
var run = ScadaBridgeAuditEventFactory.Create(AuditChannel.ScriptTrace, AuditKind.ScriptRun, AuditStatus.Delivered,
|
|
occurredAtUtc: t0.AddMilliseconds(20), target: "S", executionId: siteRun, parentExecutionId: root, sourceSiteId: siteId);
|
|
var apiSubmit = ScadaBridgeAuditEventFactory.Create(AuditChannel.ApiOutbound, AuditKind.CachedSubmit, AuditStatus.Submitted,
|
|
occurredAtUtc: t0.AddMilliseconds(30), target: "Delmia.AckRecipe", executionId: siteRun, parentExecutionId: root,
|
|
sourceSiteId: siteId, correlationId: Guid.NewGuid());
|
|
var dbWrite = ScadaBridgeAuditEventFactory.Create(AuditChannel.DbOutbound, AuditKind.DbWrite, AuditStatus.Delivered,
|
|
occurredAtUtc: t0.AddMilliseconds(40), target: "MesDb", executionId: siteRun, parentExecutionId: root, sourceSiteId: siteId);
|
|
var notify = ScadaBridgeAuditEventFactory.Create(AuditChannel.Notification, AuditKind.NotifySend, AuditStatus.Submitted,
|
|
occurredAtUtc: t0.AddMilliseconds(50), target: "Planners", executionId: siteRun, parentExecutionId: root,
|
|
sourceSiteId: siteId, correlationId: Guid.NewGuid());
|
|
// Same execution ids, but channels outside the replay set — all excluded.
|
|
var secured = ScadaBridgeAuditEventFactory.Create(AuditChannel.SecuredWrite, AuditKind.SecuredWriteSubmit, AuditStatus.Delivered,
|
|
occurredAtUtc: t0.AddMilliseconds(35), target: "Tag", executionId: siteRun, parentExecutionId: root, sourceSiteId: siteId);
|
|
var authFailure = ScadaBridgeAuditEventFactory.Create(AuditChannel.ApiInbound, AuditKind.InboundAuthFailure, AuditStatus.Failed,
|
|
occurredAtUtc: t0.AddMilliseconds(1), actor: "key", target: "M", executionId: root, sourceSiteId: siteId);
|
|
var cluster = ScadaBridgeAuditEventFactory.Create(AuditChannel.Cluster, AuditKind.ManualFailover, AuditStatus.Delivered,
|
|
occurredAtUtc: t0.AddMilliseconds(45), actor: "admin", target: "central-a", executionId: root, sourceSiteId: siteId);
|
|
|
|
await using (var ctx = CreateContext())
|
|
{
|
|
var repo = new AuditLogRepository(ctx);
|
|
foreach (var e in new[] { notify, secured, dbWrite, cluster, run, authFailure, apiSubmit, inbound, route })
|
|
await repo.InsertIfNotExistsAsync(e);
|
|
}
|
|
|
|
await using (var ctx = CreateContext())
|
|
{
|
|
var repo = new AuditLogRepository(ctx);
|
|
foreach (var entry in new[] { root, siteRun })
|
|
{
|
|
var events = await repo.GetTraceEventsAsync(entry);
|
|
Assert.Equal(
|
|
new[] { inbound.EventId, route.EventId, run.EventId, apiSubmit.EventId, dbWrite.EventId, notify.EventId },
|
|
events.Select(e => e.EventId).ToArray());
|
|
Assert.Equal(apiSubmit, events[3]);
|
|
Assert.Equal(dbWrite, events[4]);
|
|
Assert.Equal(notify, events[5]);
|
|
var decomposed = events.Select(AuditRowProjection.Decompose).ToList();
|
|
Assert.DoesNotContain(decomposed, v => v.Channel is AuditChannel.SecuredWrite or AuditChannel.Cluster);
|
|
Assert.DoesNotContain(decomposed, v => v.Kind == AuditKind.InboundAuthFailure);
|
|
}
|
|
}
|
|
}
|
|
}
|