229 lines
10 KiB
C#
229 lines
10 KiB
C#
using System.Reflection;
|
|
using System.Security.Claims;
|
|
using Bunit;
|
|
using Microsoft.AspNetCore.Components.Authorization;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Diagnostics;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Options;
|
|
using ZB.MOM.WW.ScadaBridge.CentralUI.Auth;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Sites;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Services;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Transport;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Types.Transport;
|
|
using ZB.MOM.WW.ScadaBridge.ConfigurationDatabase;
|
|
using ZB.MOM.WW.ScadaBridge.Security;
|
|
using ZB.MOM.WW.ScadaBridge.Transport;
|
|
using ZB.MOM.WW.ScadaBridge.Transport.Import;
|
|
using NSubstitute;
|
|
using TransportImportPage = ZB.MOM.WW.ScadaBridge.CentralUI.Components.Pages.Design.TransportImport;
|
|
|
|
namespace ZB.MOM.WW.ScadaBridge.CentralUI.Tests.Pages.Design;
|
|
|
|
/// <summary>
|
|
/// Regression tests for register row 103 (the P6-6 lesser instance in
|
|
/// <c>TransportImport</c>). <c>TryLoadAsync</c> awaits
|
|
/// <see cref="IBundleImporter.LoadAsync"/>, then used to publish the result into
|
|
/// <c>_session</c> unconditionally. If the circuit was torn down inside that await
|
|
/// (user navigates away mid-upload), <c>Dispose</c> would have already run, found
|
|
/// <c>_session</c> still null, and released nothing — so the freshly-opened server-side
|
|
/// <c>BundleSession</c> stayed registered, stranding one of
|
|
/// <c>TransportOptions.MaxConcurrentImportSessions</c> (register row 43) for the full
|
|
/// session TTL. The fix publishes the session and re-checks disposal under one lock
|
|
/// (<c>_sessionLock</c>), mirroring <c>DebugView</c>'s P6-6 fix, so exactly one side
|
|
/// (this method or <c>Dispose</c>) abandons it.
|
|
/// </summary>
|
|
public class TransportImportSessionDisposeRaceTests : BunitContext
|
|
{
|
|
/// <summary>
|
|
/// A real <see cref="BundleSessionStore"/>-backed <see cref="IBundleImporter"/> whose
|
|
/// <see cref="LoadAsync"/> can be held open on demand, so the dispose-during-load
|
|
/// window is deterministic rather than timing-dependent. Mirrors
|
|
/// <c>DebugViewConnectDisposeRaceTests.GatedDebugStreamService</c>.
|
|
/// </summary>
|
|
private sealed class GatedImporter : IBundleImporter
|
|
{
|
|
private readonly TaskCompletionSource _gate = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
private readonly TaskCompletionSource _entered = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
private readonly List<Guid> _abandoned = new();
|
|
|
|
public BundleSessionStore Store { get; } = new(
|
|
Options.Create(new TransportOptions()), TimeProvider.System);
|
|
|
|
/// <summary>Completes once <see cref="LoadAsync"/> has been entered.</summary>
|
|
public Task Entered => _entered.Task;
|
|
|
|
/// <summary>Session ids passed to <see cref="Abandon"/>, in call order.</summary>
|
|
public IReadOnlyList<Guid> Abandoned
|
|
{
|
|
get { lock (_abandoned) { return _abandoned.ToList(); } }
|
|
}
|
|
|
|
public void ReleaseLoad() => _gate.TrySetResult();
|
|
|
|
public async Task<BundleSession> LoadAsync(
|
|
Stream bundleStream, string? passphrase, CancellationToken ct = default, string? clientKey = null)
|
|
{
|
|
_entered.TrySetResult();
|
|
await _gate.Task;
|
|
|
|
var session = new BundleSession
|
|
{
|
|
SessionId = Guid.NewGuid(),
|
|
Manifest = new BundleManifest(
|
|
BundleFormatVersion: 1,
|
|
SchemaVersion: "1.0",
|
|
CreatedAtUtc: DateTimeOffset.UtcNow,
|
|
SourceEnvironment: "prod-cluster",
|
|
ExportedBy: "bob",
|
|
ScadaBridgeVersion: "1.0.0",
|
|
ContentHash: "sha256:0000",
|
|
Encryption: null,
|
|
Summary: new BundleSummary(0, 0, 0, 0, 0, 0, 0, 0),
|
|
Contents: Array.Empty<ManifestContentEntry>()),
|
|
// Non-zero bytes so a zeroing assertion (if ever added) is meaningful.
|
|
DecryptedContent = new byte[] { 0x11, 0x22, 0x33, 0x44 },
|
|
ExpiresAt = DateTimeOffset.UtcNow.AddMinutes(30),
|
|
};
|
|
return Store.Open(session);
|
|
}
|
|
|
|
public Task<ImportPreview> PreviewAsync(Guid sessionId, CancellationToken ct = default)
|
|
=> throw new NotSupportedException("Not exercised by these tests.");
|
|
|
|
public Task<ImportResult> ApplyAsync(
|
|
Guid sessionId,
|
|
IReadOnlyList<ImportResolution> resolutions,
|
|
string user,
|
|
CancellationToken ct = default,
|
|
BundleNameMap? nameMap = null)
|
|
=> throw new NotSupportedException("Not exercised by these tests.");
|
|
|
|
public void Abandon(Guid sessionId)
|
|
{
|
|
lock (_abandoned) { _abandoned.Add(sessionId); }
|
|
var session = Store.Get(sessionId);
|
|
if (session is { DecryptedContent.Length: > 0 })
|
|
{
|
|
Array.Clear(session.DecryptedContent, 0, session.DecryptedContent.Length);
|
|
}
|
|
Store.Remove(sessionId);
|
|
}
|
|
}
|
|
|
|
private readonly GatedImporter _importer = new();
|
|
private readonly ISiteRepository _siteRepo = Substitute.For<ISiteRepository>();
|
|
private const string TestClientIp = "203.0.113.7";
|
|
|
|
public TransportImportSessionDisposeRaceTests()
|
|
{
|
|
JSInterop.Mode = JSRuntimeMode.Loose;
|
|
|
|
Services.AddSingleton<IBundleImporter>(_importer);
|
|
Services.AddSingleton(Substitute.For<IAuditService>());
|
|
Services.AddSingleton(new CircuitClientAddressProvider(TestClientIp));
|
|
|
|
_siteRepo.GetAllSitesAsync(Arg.Any<CancellationToken>()).Returns(Array.Empty<Site>());
|
|
Services.AddSingleton(_siteRepo);
|
|
Services.AddSingleton<IOptions<TransportOptions>>(
|
|
Options.Create(new TransportOptions
|
|
{
|
|
MaxBundleSizeMb = 10,
|
|
MaxUnlockAttemptsPerSession = 3,
|
|
}));
|
|
|
|
var dbOptions = new DbContextOptionsBuilder<ScadaBridgeDbContext>()
|
|
.UseSqlite("DataSource=:memory:")
|
|
.ConfigureWarnings(w => w.Ignore(RelationalEventId.AmbientTransactionWarning))
|
|
.Options;
|
|
var dbContext = new ScadaBridgeDbContext(dbOptions);
|
|
dbContext.Database.OpenConnection();
|
|
dbContext.Database.EnsureCreated();
|
|
Services.AddSingleton(dbContext);
|
|
|
|
var claims = new List<Claim> { new(JwtTokenService.UsernameClaimType, "alice") };
|
|
var principal = new ClaimsPrincipal(new ClaimsIdentity(claims, "TestAuth"));
|
|
Services.AddSingleton<AuthenticationStateProvider>(new TestAuthStateProvider(principal));
|
|
Services.AddAuthorizationCore();
|
|
Services.AddTestConfigurationMutations();
|
|
}
|
|
|
|
private static void SetField(object obj, string name, object? value) =>
|
|
obj.GetType().GetField(name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)!
|
|
.SetValue(obj, value);
|
|
|
|
private static T GetField<T>(object obj, string name) =>
|
|
(T)obj.GetType().GetField(name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)!
|
|
.GetValue(obj)!;
|
|
|
|
/// <summary>Stages a temp bundle file and points the wizard's <c>_bundleTempPath</c> at it.</summary>
|
|
private static string StageBundleFile()
|
|
{
|
|
var dir = Path.Combine(Path.GetTempPath(), "scadabridge-transport-staging");
|
|
Directory.CreateDirectory(dir);
|
|
var path = Path.Combine(dir, $"test-{Guid.NewGuid():N}.scadabundle");
|
|
File.WriteAllBytes(path, new byte[] { 0x01, 0x02, 0x03 });
|
|
return path;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Starts <c>TryLoadAsync</c> on the renderer's dispatcher (where it really runs) and
|
|
/// hands back its task without awaiting it, so the test can act inside the load window.
|
|
/// Mirrors <c>DebugViewConnectDisposeRaceTests.StartConnectAsync</c>.
|
|
/// </summary>
|
|
private static async Task<Task> StartTryLoadAsync(IRenderedComponent<TransportImportPage> cut)
|
|
{
|
|
var method = typeof(TransportImportPage).GetMethod(
|
|
"TryLoadAsync", BindingFlags.Instance | BindingFlags.NonPublic)!;
|
|
Task? task = null;
|
|
await cut.InvokeAsync(() => { task = (Task)method.Invoke(cut.Instance, new object?[] { null })!; });
|
|
return task!;
|
|
}
|
|
|
|
[Fact]
|
|
public async Task TryLoadAsync_DisposedMidLoad_AbandonsTheJustOpenedSession()
|
|
{
|
|
var cut = Render<TransportImportPage>();
|
|
SetField(cut.Instance, "_bundleTempPath", StageBundleFile());
|
|
|
|
var tryLoad = await StartTryLoadAsync(cut);
|
|
await _importer.Entered.WaitAsync(TimeSpan.FromSeconds(5));
|
|
|
|
// The user navigates away while LoadAsync is still in flight. Dispose runs off
|
|
// the renderer's thread, exactly as a circuit teardown can.
|
|
cut.Instance.Dispose();
|
|
|
|
_importer.ReleaseLoad();
|
|
await tryLoad.WaitAsync(TimeSpan.FromSeconds(10));
|
|
|
|
Assert.Single(_importer.Abandoned);
|
|
Assert.Null(GetField<object?>(cut.Instance, "_session"));
|
|
// The store must not still hold the session either — Abandon actually released it.
|
|
Assert.Null(_importer.Store.Get(_importer.Abandoned[0]));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task TryLoadAsync_ThenDispose_AbandonsTheSessionExactlyOnce()
|
|
{
|
|
var cut = Render<TransportImportPage>();
|
|
SetField(cut.Instance, "_bundleTempPath", StageBundleFile());
|
|
|
|
// Normal path: the load completes while the component is very much alive.
|
|
_importer.ReleaseLoad();
|
|
var tryLoad = await StartTryLoadAsync(cut);
|
|
await tryLoad.WaitAsync(TimeSpan.FromSeconds(10));
|
|
|
|
Assert.Empty(_importer.Abandoned);
|
|
Assert.NotNull(GetField<object?>(cut.Instance, "_session"));
|
|
|
|
cut.Instance.Dispose();
|
|
Assert.Single(_importer.Abandoned);
|
|
|
|
// A second Dispose (bUnit tears the component down again at test end) must not
|
|
// abandon an already-released session a second time.
|
|
cut.Instance.Dispose();
|
|
Assert.Single(_importer.Abandoned);
|
|
}
|
|
}
|