mbproxy: replace status page with a live SignalR web dashboard
The single auto-refreshing zero-JS status page gave operators a 25-column wall and no way to drill into one connection. This adds a Bootstrap fleet dashboard (filterable/sortable KPI table) and a per-PLC detail page with a real-time debug view of raw PLC-side BCD vs. decoded client-side values, streamed live over a SignalR feed. The debug view is fed by an on-demand per-tag value capture, armed only while a detail page is open. All assets (Bootstrap, SignalR client, fonts) are embedded so the UI works unchanged on firewalled networks; GET /status.json is untouched for scrapers. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,181 @@
|
||||
using System.Collections.Frozen;
|
||||
using Mbproxy.Bcd;
|
||||
using Mbproxy.Proxy;
|
||||
using Mbproxy.Proxy.Multiplexing;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
|
||||
namespace Mbproxy.Tests.Proxy;
|
||||
|
||||
/// <summary>
|
||||
/// Unit tests for the <see cref="TagValueCapture"/> recording hooks in
|
||||
/// <see cref="BcdPduPipeline"/>. Verifies that an armed capture records raw PLC-side
|
||||
/// and decoded client-side values, and — as a regression guard — that a disarmed or
|
||||
/// absent capture leaves the rewrite behaviour byte-identical.
|
||||
/// </summary>
|
||||
[Trait("Category", "Unit")]
|
||||
public sealed class BcdPduPipelineCaptureTests
|
||||
{
|
||||
private static readonly BcdPduPipeline Pipeline = new();
|
||||
|
||||
private static BcdTagMap BuildMap(params BcdTag[] tags)
|
||||
{
|
||||
var frozen = tags.ToDictionary(t => t.Address).ToFrozenDictionary();
|
||||
return frozen.Count > 0 ? new BcdTagMap(frozen) : BcdTagMap.Empty;
|
||||
}
|
||||
|
||||
private static PerPlcContext MakeContext(TagValueCapture? capture, params BcdTag[] tags)
|
||||
=> new()
|
||||
{
|
||||
PlcName = "TestPLC",
|
||||
TagMap = BuildMap(tags),
|
||||
Counters = new ProxyCounters(),
|
||||
Logger = NullLogger.Instance,
|
||||
Capture = capture,
|
||||
};
|
||||
|
||||
private static InFlightRequest MakeInFlight(byte fc, ushort start, ushort qty)
|
||||
=> new(1, fc, start, qty, Array.Empty<InterestedParty>(), DateTimeOffset.UtcNow);
|
||||
|
||||
private static byte[] Fc03Response(params ushort[] regs)
|
||||
{
|
||||
var pdu = new byte[2 + regs.Length * 2];
|
||||
pdu[0] = 0x03;
|
||||
pdu[1] = (byte)(regs.Length * 2);
|
||||
for (int i = 0; i < regs.Length; i++)
|
||||
{
|
||||
pdu[2 + i * 2] = (byte)(regs[i] >> 8);
|
||||
pdu[2 + i * 2 + 1] = (byte)(regs[i] & 0xFF);
|
||||
}
|
||||
return pdu;
|
||||
}
|
||||
|
||||
private static byte[] Fc06Request(ushort address, ushort value)
|
||||
=> [0x06, (byte)(address >> 8), (byte)(address & 0xFF), (byte)(value >> 8), (byte)(value & 0xFF)];
|
||||
|
||||
private static byte[] Fc16Request(ushort start, params ushort[] regs)
|
||||
{
|
||||
var pdu = new byte[6 + regs.Length * 2];
|
||||
pdu[0] = 0x10;
|
||||
pdu[1] = (byte)(start >> 8);
|
||||
pdu[2] = (byte)(start & 0xFF);
|
||||
pdu[3] = (byte)((ushort)regs.Length >> 8);
|
||||
pdu[4] = (byte)(regs.Length & 0xFF);
|
||||
pdu[5] = (byte)(regs.Length * 2);
|
||||
for (int i = 0; i < regs.Length; i++)
|
||||
{
|
||||
pdu[6 + i * 2] = (byte)(regs[i] >> 8);
|
||||
pdu[6 + i * 2 + 1] = (byte)(regs[i] & 0xFF);
|
||||
}
|
||||
return pdu;
|
||||
}
|
||||
|
||||
private static void ProcessFc03Response(PerPlcContext ctx, ushort start, ushort qty, byte[] response)
|
||||
{
|
||||
var responseCtx = ctx.WithCurrentRequest(MakeInFlight(0x03, start, qty));
|
||||
Pipeline.Process(MbapDirection.ResponseToClient, ReadOnlySpan<byte>.Empty, response.AsSpan(), responseCtx);
|
||||
}
|
||||
|
||||
private static ushort ReadReg(byte[] pdu, int offsetWords)
|
||||
=> (ushort)((pdu[2 + offsetWords * 2] << 8) | pdu[2 + offsetWords * 2 + 1]);
|
||||
|
||||
// ── Read path (FC03/FC04 response) ───────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void FC03_16Bit_ArmedCapture_RecordsRawAndDecoded()
|
||||
{
|
||||
var capture = new TagValueCapture([BcdTag.Create(100, 16)]);
|
||||
capture.Arm();
|
||||
var ctx = MakeContext(capture, BcdTag.Create(100, 16));
|
||||
|
||||
ProcessFc03Response(ctx, 100, 1, Fc03Response(0x1234));
|
||||
|
||||
var slot = capture.Snapshot().ShouldHaveSingleItem();
|
||||
slot.Address.ShouldBe((ushort)100);
|
||||
slot.RawLow.ShouldBe((ushort)0x1234); // BCD nibbles on the PLC wire
|
||||
slot.DecodedValue.ShouldBe(1234); // binary the client receives
|
||||
slot.Direction.ShouldBe(CaptureDirection.Read);
|
||||
slot.UpdatedAtUtc.ShouldNotBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FC03_32Bit_ArmedCapture_RecordsBothRawWords()
|
||||
{
|
||||
var capture = new TagValueCapture([BcdTag.Create(100, 32)]);
|
||||
capture.Arm();
|
||||
var ctx = MakeContext(capture, BcdTag.Create(100, 32));
|
||||
|
||||
// CDAB: low word 0x5678, high word 0x1234 → decoded 1234*10000 + 5678.
|
||||
ProcessFc03Response(ctx, 100, 2, Fc03Response(0x5678, 0x1234));
|
||||
|
||||
var slot = capture.Snapshot().ShouldHaveSingleItem();
|
||||
slot.Width.ShouldBe((byte)32);
|
||||
slot.RawLow.ShouldBe((ushort)0x5678);
|
||||
slot.RawHigh.ShouldBe((ushort)0x1234);
|
||||
slot.DecodedValue.ShouldBe(12345678);
|
||||
slot.Direction.ShouldBe(CaptureDirection.Read);
|
||||
}
|
||||
|
||||
// ── Write path (FC06 / FC16 request) ─────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void FC06_ArmedCapture_RecordsEncodedBcdAndClientValue()
|
||||
{
|
||||
var capture = new TagValueCapture([BcdTag.Create(100, 16)]);
|
||||
capture.Arm();
|
||||
var ctx = MakeContext(capture, BcdTag.Create(100, 16));
|
||||
|
||||
// Client writes binary 1234; proxy encodes to BCD 0x1234 for the PLC.
|
||||
var req = Fc06Request(100, 1234);
|
||||
Pipeline.Process(MbapDirection.RequestToBackend, ReadOnlySpan<byte>.Empty, req.AsSpan(), ctx);
|
||||
|
||||
var slot = capture.Snapshot().ShouldHaveSingleItem();
|
||||
slot.RawLow.ShouldBe((ushort)0x1234); // BCD nibbles sent to the PLC
|
||||
slot.DecodedValue.ShouldBe(1234); // binary the client wrote
|
||||
slot.Direction.ShouldBe(CaptureDirection.Write);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FC16_16Bit_ArmedCapture_RecordsWrite()
|
||||
{
|
||||
var capture = new TagValueCapture([BcdTag.Create(100, 16)]);
|
||||
capture.Arm();
|
||||
var ctx = MakeContext(capture, BcdTag.Create(100, 16));
|
||||
|
||||
var req = Fc16Request(100, 4321);
|
||||
Pipeline.Process(MbapDirection.RequestToBackend, ReadOnlySpan<byte>.Empty, req.AsSpan(), ctx);
|
||||
|
||||
var slot = capture.Snapshot().ShouldHaveSingleItem();
|
||||
slot.RawLow.ShouldBe((ushort)0x4321);
|
||||
slot.DecodedValue.ShouldBe(4321);
|
||||
slot.Direction.ShouldBe(CaptureDirection.Write);
|
||||
}
|
||||
|
||||
// ── Regression guards: disarmed / absent capture ─────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void FC03_DisarmedCapture_StillRewrites_ButCapturesNothing()
|
||||
{
|
||||
var capture = new TagValueCapture([BcdTag.Create(100, 16)]);
|
||||
// Not armed.
|
||||
var ctx = MakeContext(capture, BcdTag.Create(100, 16));
|
||||
|
||||
var rsp = Fc03Response(0x1234);
|
||||
ProcessFc03Response(ctx, 100, 1, rsp);
|
||||
|
||||
ReadReg(rsp, 0).ShouldBe((ushort)1234); // rewrite still happened
|
||||
capture.Snapshot().ShouldHaveSingleItem().UpdatedAtUtc.ShouldBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FC03_NullCapture_DoesNotThrow_AndStillRewrites()
|
||||
{
|
||||
var ctx = MakeContext(capture: null, BcdTag.Create(100, 16));
|
||||
|
||||
var rsp = Fc03Response(0x1234);
|
||||
Should.NotThrow(() => ProcessFc03Response(ctx, 100, 1, rsp));
|
||||
|
||||
ReadReg(rsp, 0).ShouldBe((ushort)1234);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
using System.Collections.Frozen;
|
||||
using Mbproxy.Bcd;
|
||||
using Mbproxy.Proxy;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
|
||||
namespace Mbproxy.Tests.Proxy;
|
||||
|
||||
/// <summary>
|
||||
/// Unit tests for <see cref="TagCaptureRegistry"/> — the shared seam that arms and
|
||||
/// disarms per-PLC <see cref="TagValueCapture"/> instances.
|
||||
/// </summary>
|
||||
[Trait("Category", "Unit")]
|
||||
public sealed class TagCaptureRegistryTests
|
||||
{
|
||||
private static BcdTagMap Map(params (ushort addr, byte width)[] tags)
|
||||
{
|
||||
if (tags.Length == 0)
|
||||
return BcdTagMap.Empty;
|
||||
var frozen = tags
|
||||
.Select(t => BcdTag.Create(t.addr, t.width))
|
||||
.ToDictionary(t => t.Address)
|
||||
.ToFrozenDictionary();
|
||||
return new BcdTagMap(frozen);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetOrCreate_ReturnsSameInstance_OnRepeatCall_WhenTagSetUnchanged()
|
||||
{
|
||||
var registry = new TagCaptureRegistry();
|
||||
var first = registry.GetOrCreate("plc-1", Map((100, 16)));
|
||||
var second = registry.GetOrCreate("plc-1", Map((100, 16)));
|
||||
|
||||
// AddOrUpdate's update path rebuilds; both must be live and consistent.
|
||||
second.TagCount.ShouldBe(1);
|
||||
registry.TryGet("plc-1", out var current).ShouldBeTrue();
|
||||
current.ShouldBeSameAs(second);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetOrCreate_Rebuild_PreservesArmedFlag()
|
||||
{
|
||||
var registry = new TagCaptureRegistry();
|
||||
var capture = registry.GetOrCreate("plc-1", Map((100, 16)));
|
||||
capture.Arm();
|
||||
|
||||
// Hot-reload reseat: same PLC, changed tag set.
|
||||
var rebuilt = registry.GetOrCreate("plc-1", Map((100, 16), (200, 32)));
|
||||
|
||||
rebuilt.ShouldNotBeSameAs(capture);
|
||||
rebuilt.IsArmed.ShouldBeTrue("a rebuilt capture must keep capturing for an open detail page");
|
||||
rebuilt.TagCount.ShouldBe(2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Arm_And_Disarm_ReachTheRightCapture()
|
||||
{
|
||||
var registry = new TagCaptureRegistry();
|
||||
registry.GetOrCreate("plc-1", Map((100, 16)));
|
||||
registry.GetOrCreate("plc-2", Map((100, 16)));
|
||||
|
||||
registry.Arm("plc-1");
|
||||
|
||||
registry.TryGet("plc-1", out var c1).ShouldBeTrue();
|
||||
registry.TryGet("plc-2", out var c2).ShouldBeTrue();
|
||||
c1.IsArmed.ShouldBeTrue();
|
||||
c2.IsArmed.ShouldBeFalse();
|
||||
|
||||
registry.Disarm("plc-1");
|
||||
c1.IsArmed.ShouldBeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DisarmAll_DisarmsEveryCapture()
|
||||
{
|
||||
var registry = new TagCaptureRegistry();
|
||||
registry.GetOrCreate("plc-1", Map((100, 16)));
|
||||
registry.GetOrCreate("plc-2", Map((100, 16)));
|
||||
registry.Arm("plc-1");
|
||||
registry.Arm("plc-2");
|
||||
|
||||
registry.DisarmAll();
|
||||
|
||||
registry.TryGet("plc-1", out var c1).ShouldBeTrue();
|
||||
registry.TryGet("plc-2", out var c2).ShouldBeTrue();
|
||||
c1.IsArmed.ShouldBeFalse();
|
||||
c2.IsArmed.ShouldBeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UnknownPlc_Operations_AreSafeNoOps()
|
||||
{
|
||||
var registry = new TagCaptureRegistry();
|
||||
|
||||
Should.NotThrow(() => registry.Arm("ghost"));
|
||||
Should.NotThrow(() => registry.Disarm("ghost"));
|
||||
Should.NotThrow(() => registry.Remove("ghost"));
|
||||
registry.TryGet("ghost", out _).ShouldBeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Remove_DropsTheCapture()
|
||||
{
|
||||
var registry = new TagCaptureRegistry();
|
||||
registry.GetOrCreate("plc-1", Map((100, 16)));
|
||||
registry.TryGet("plc-1", out _).ShouldBeTrue();
|
||||
|
||||
registry.Remove("plc-1");
|
||||
|
||||
registry.TryGet("plc-1", out _).ShouldBeFalse();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
using Mbproxy.Bcd;
|
||||
using Mbproxy.Proxy;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
|
||||
namespace Mbproxy.Tests.Proxy;
|
||||
|
||||
/// <summary>
|
||||
/// Unit tests for <see cref="TagValueCapture"/> — the on-demand per-tag value store
|
||||
/// behind the connection-detail debug view.
|
||||
/// </summary>
|
||||
[Trait("Category", "Unit")]
|
||||
public sealed class TagValueCaptureTests
|
||||
{
|
||||
private static TagValueCapture Make(params (ushort addr, byte width)[] tags)
|
||||
=> new(tags.Select(t => BcdTag.Create(t.addr, t.width)));
|
||||
|
||||
[Fact]
|
||||
public void Disarmed_Record_IsNoOp()
|
||||
{
|
||||
var capture = Make((100, 16));
|
||||
// No Arm() call — capture starts disarmed.
|
||||
capture.Record(100, 0x1234, 0, 1234, CaptureDirection.Read);
|
||||
|
||||
capture.IsArmed.ShouldBeFalse();
|
||||
var slot = capture.Snapshot().ShouldHaveSingleItem();
|
||||
slot.UpdatedAtUtc.ShouldBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Armed_Record_UpdatesMatchingSlot()
|
||||
{
|
||||
var capture = Make((100, 16));
|
||||
capture.Arm();
|
||||
capture.Record(100, 0x1234, 0, 1234, CaptureDirection.Read);
|
||||
|
||||
var slot = capture.Snapshot().ShouldHaveSingleItem();
|
||||
slot.Address.ShouldBe((ushort)100);
|
||||
slot.Width.ShouldBe((byte)16);
|
||||
slot.RawLow.ShouldBe((ushort)0x1234);
|
||||
slot.DecodedValue.ShouldBe(1234);
|
||||
slot.Direction.ShouldBe(CaptureDirection.Read);
|
||||
slot.UpdatedAtUtc.ShouldNotBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Armed_Record_UnknownAddress_IsIgnored()
|
||||
{
|
||||
var capture = Make((100, 16));
|
||||
capture.Arm();
|
||||
capture.Record(999, 0x1111, 0, 1111, CaptureDirection.Read);
|
||||
|
||||
capture.Snapshot().ShouldAllBe(s => s.UpdatedAtUtc == null);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Disarm_ClearsAllSlots()
|
||||
{
|
||||
var capture = Make((100, 16), (200, 16));
|
||||
capture.Arm();
|
||||
capture.Record(100, 0x0042, 0, 42, CaptureDirection.Read);
|
||||
capture.Record(200, 0x0099, 0, 99, CaptureDirection.Read);
|
||||
|
||||
capture.Disarm();
|
||||
|
||||
capture.IsArmed.ShouldBeFalse();
|
||||
capture.Snapshot().ShouldAllBe(s => s.UpdatedAtUtc == null);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReArm_AfterDisarm_StartsEmpty()
|
||||
{
|
||||
var capture = Make((100, 16));
|
||||
capture.Arm();
|
||||
capture.Record(100, 0x0042, 0, 42, CaptureDirection.Read);
|
||||
capture.Disarm();
|
||||
capture.Arm();
|
||||
|
||||
// No new traffic since re-arm — slot must read as empty, not the pre-disarm value.
|
||||
capture.Snapshot().ShouldHaveSingleItem().UpdatedAtUtc.ShouldBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ThirtyTwoBitTag_RecordsBothRawWords()
|
||||
{
|
||||
var capture = Make((100, 32));
|
||||
capture.Arm();
|
||||
capture.Record(100, 0x5678, 0x1234, 12345678, CaptureDirection.Read);
|
||||
|
||||
var slot = capture.Snapshot().ShouldHaveSingleItem();
|
||||
slot.Width.ShouldBe((byte)32);
|
||||
slot.RawLow.ShouldBe((ushort)0x5678);
|
||||
slot.RawHigh.ShouldBe((ushort)0x1234);
|
||||
slot.DecodedValue.ShouldBe(12345678);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Snapshot_ReturnsOneRowPerTag_OrderedByAddress()
|
||||
{
|
||||
var capture = Make((300, 16), (100, 32), (200, 16));
|
||||
capture.TagCount.ShouldBe(3);
|
||||
|
||||
var snap = capture.Snapshot();
|
||||
snap.Select(s => s.Address).ShouldBe([(ushort)100, (ushort)200, (ushort)300]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WriteDirection_IsPreserved()
|
||||
{
|
||||
var capture = Make((100, 16));
|
||||
capture.Arm();
|
||||
capture.Record(100, 0x0500, 0, 500, CaptureDirection.Write);
|
||||
|
||||
capture.Snapshot().ShouldHaveSingleItem().Direction.ShouldBe(CaptureDirection.Write);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ConcurrentRecordAndSnapshot_NeverYieldsTornSlot()
|
||||
{
|
||||
// Invariant maintained by every Record: DecodedValue == RawLow + RawHigh.
|
||||
// A torn read (fields from two different Record calls) would break it.
|
||||
var capture = Make((100, 32));
|
||||
capture.Arm();
|
||||
|
||||
var ct = TestContext.Current.CancellationToken;
|
||||
bool tornObserved = false;
|
||||
|
||||
var writers = Enumerable.Range(0, 4).Select(seed => Task.Run(() =>
|
||||
{
|
||||
var rng = new Random(seed + 1);
|
||||
for (int i = 0; i < 200_000; i++)
|
||||
{
|
||||
ushort lo = (ushort)rng.Next(0, 60000);
|
||||
ushort hi = (ushort)rng.Next(0, 5000);
|
||||
capture.Record(100, lo, hi, lo + hi, CaptureDirection.Read);
|
||||
}
|
||||
}, ct)).ToArray();
|
||||
|
||||
var reader = Task.Run(() =>
|
||||
{
|
||||
for (int i = 0; i < 200_000; i++)
|
||||
{
|
||||
foreach (var slot in capture.Snapshot())
|
||||
{
|
||||
if (slot.UpdatedAtUtc is null)
|
||||
continue;
|
||||
if (slot.DecodedValue != slot.RawLow + slot.RawHigh)
|
||||
tornObserved = true;
|
||||
}
|
||||
}
|
||||
}, ct);
|
||||
|
||||
await Task.WhenAll([.. writers, reader]);
|
||||
tornObserved.ShouldBeFalse("Snapshot must never observe a torn (half-updated) slot");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user