using Xunit; namespace ZB.MOM.WW.OtOpcUa.Driver.AbLegacy.IntegrationTests; /// Serializes environment mutation against every other test collection. [CollectionDefinition(Name, DisableParallelization = true)] public sealed class AbLegacyFixtureEnvironmentCollection { /// The isolated collection used by pure fixture endpoint guards. public const string Name = "AbLegacyFixtureEnvironment"; } /// Verifies dynamic per-family fixture mapping without probing any endpoint. [Collection(AbLegacyFixtureEnvironmentCollection.Name)] [Trait("Category", "Unit")] [Trait("Feature", "GrpcData"), Trait("Phase", "P12")] public sealed class AbLegacyFixtureEndpointGuardTests : IDisposable { private static readonly string[] Variables = [ "AB_LEGACY_ENDPOINT", "AB_LEGACY_CIP_PATH", "AB_LEGACY_COMPOSE_PROFILE", "AB_LEGACY_SLC500_ENDPOINT", "AB_LEGACY_MICROLOGIX_ENDPOINT", "AB_LEGACY_PLC5_ENDPOINT", "OTOPCUA_LOCAL_FIXTURE_MODE", ]; private readonly Dictionary _previous; /// Isolates this test's fixture inputs and retains every original value. public AbLegacyFixtureEndpointGuardTests() { _previous = Variables.ToDictionary(name => name, Environment.GetEnvironmentVariable); foreach (var name in Variables) Environment.SetEnvironmentVariable(name, null); } /// Restores the complete parent environment after each guard test. public void Dispose() { foreach (var (name, value) in _previous) Environment.SetEnvironmentVariable(name, value); } private static void OwnedEndpoints() { Environment.SetEnvironmentVariable("OTOPCUA_LOCAL_FIXTURE_MODE", "1"); Environment.SetEnvironmentVariable("AB_LEGACY_ENDPOINT", "127.0.0.1:41001"); Environment.SetEnvironmentVariable("AB_LEGACY_SLC500_ENDPOINT", "127.0.0.1:41001"); Environment.SetEnvironmentVariable("AB_LEGACY_MICROLOGIX_ENDPOINT", "127.0.0.1:41002"); Environment.SetEnvironmentVariable("AB_LEGACY_PLC5_ENDPOINT", "127.0.0.1:41003"); } /// All three explicit ports retain the complete profile-by-transport matrix. [Fact] public void Complete_family_endpoints_select_distinct_ports_and_all_transports() { OwnedEndpoints(); var fixture = new AbLegacyServerFixture(); // constructor performs no network I/O Assert.Equal("127.0.0.1", fixture.Host); Assert.Equal(41001, fixture.PortFor(KnownProfiles.Slc500)); Assert.Equal(41002, fixture.PortFor(KnownProfiles.MicroLogix)); Assert.Equal(41003, fixture.PortFor(KnownProfiles.Plc5)); Assert.True(AbLegacyServerFixture.HasFamilyEndpoints); var rows = TransportMatrix.Profiles.ToArray(); Assert.Equal(6, rows.Length); foreach (var profile in KnownProfiles.All) Assert.Equal(2, rows.Count(row => ReferenceEquals(profile, row[0]))); } /// A single pinned target preserves the existing SLC500 default matrix. [Fact] public void Single_pinned_endpoint_preserves_the_existing_default_profile() { Environment.SetEnvironmentVariable("AB_LEGACY_ENDPOINT", "127.0.0.1:42000"); var fixture = new AbLegacyServerFixture(); Assert.False(AbLegacyServerFixture.HasFamilyEndpoints); foreach (var profile in KnownProfiles.All) Assert.Equal(42000, fixture.PortFor(profile)); var rows = TransportMatrix.Profiles.ToArray(); Assert.Equal(2, rows.Length); Assert.All(rows, row => Assert.Same(KnownProfiles.Slc500, row[0])); } /// An explicit operator profile selection still controls the matrix. [Fact] public void Explicit_profile_selection_is_preserved_with_family_endpoints() { OwnedEndpoints(); Environment.SetEnvironmentVariable("AB_LEGACY_COMPOSE_PROFILE", "plc5"); var rows = TransportMatrix.Profiles.ToArray(); Assert.Equal(2, rows.Length); Assert.All(rows, row => Assert.Same(KnownProfiles.Plc5, row[0])); } /// Malformed or foreign family endpoints fail before any reachability probe. [Theory] [InlineData("10.100.0.35:41002")] [InlineData("127.0.0.1")] [InlineData("127.0.0.1:0")] [InlineData("127.0.0.1:65536")] [InlineData("127.0.0.1:not-a-port")] public void Invalid_family_endpoint_is_rejected_without_network(string endpoint) { OwnedEndpoints(); Environment.SetEnvironmentVariable("AB_LEGACY_MICROLOGIX_ENDPOINT", endpoint); var fixture = new AbLegacyServerFixture(); Assert.Throws(() => fixture.PortFor(KnownProfiles.MicroLogix)); } /// Partial mappings cannot accidentally opt into the full family matrix. [Fact] public void Missing_family_override_does_not_claim_a_complete_mapping() { OwnedEndpoints(); Environment.SetEnvironmentVariable("AB_LEGACY_PLC5_ENDPOINT", null); Assert.False(AbLegacyServerFixture.HasFamilyEndpoints); Assert.Equal(2, TransportMatrix.Profiles.Count()); } /// The original nonlocal port map remains available without opening sockets. [Fact] public void Unpinned_CI_defaults_preserve_the_original_port_mapping() { var fixture = new AbLegacyServerFixture(); Assert.False(fixture.EndpointPinned); foreach (var profile in KnownProfiles.All) Assert.Equal(profile.Port, fixture.PortFor(profile)); Assert.Equal(6, TransportMatrix.Profiles.Count()); } }