using System.Collections; using System.Reflection; using Shouldly; using Xunit; using ZB.MOM.WW.OtOpcUa.Security.Ldap; using LdapBindMode = ZB.MOM.WW.Auth.Abstractions.Ldap.LdapBindMode; using LdapTransport = ZB.MOM.WW.Auth.Abstractions.Ldap.LdapTransport; using LibLdapOptions = ZB.MOM.WW.Auth.Abstractions.Ldap.LdapOptions; namespace ZB.MOM.WW.OtOpcUa.Security.Tests; /// /// lmxopcua#913 — once silently dropped /// FallbackServers, BindMode, UserBindTemplate and /// AcceptDomainQualifiedNames, so the AdminUI had no DC failover and the config keys were /// ignored without error. This guard reflects over the shared library's /// in BOTH directions: /// /// every settable library property is either projected by ToLibraryOptions() /// (a non-default value set on OtOpcUa's options arrives in the library options) or is /// named in ; and /// every entry still exists on the library type (no /// stale allow-list). /// /// A library upgrade that adds a property therefore fails here until it is mapped or allow-listed. /// public sealed class LdapOptionsProjectionCompletenessTests { /// Library properties deliberately not projected, each with its reason. private static readonly IReadOnlyDictionary IntentionallyUnmapped = new Dictionary(StringComparer.Ordinal) { [nameof(LibLdapOptions.ServerCertificateValidationCallback)] = "code-only TLS hook in the library; not bindable from configuration", }; /// /// A non-default value per library property, set on OtOpcUa's same-named option. Every value /// differs from BOTH the library default and OtOpcUa's default, so an unmapped property cannot /// pass by coincidence. /// private static readonly IReadOnlyDictionary NonDefaultValues = new Dictionary(StringComparer.Ordinal) { [nameof(LibLdapOptions.Enabled)] = false, [nameof(LibLdapOptions.Server)] = "dc1.example.test", [nameof(LibLdapOptions.Port)] = 6360, [nameof(LibLdapOptions.Transport)] = LdapTransport.StartTls, [nameof(LibLdapOptions.AllowInsecure)] = true, [nameof(LibLdapOptions.SearchBase)] = "dc=example,dc=test", [nameof(LibLdapOptions.ServiceAccountDn)] = "cn=svc,dc=example,dc=test", [nameof(LibLdapOptions.ServiceAccountPassword)] = "s3cret", [nameof(LibLdapOptions.UserNameAttribute)] = "sAMAccountName", [nameof(LibLdapOptions.DisplayNameAttribute)] = "displayName", [nameof(LibLdapOptions.GroupAttribute)] = "memberOfX", [nameof(LibLdapOptions.ConnectionTimeoutMs)] = 4321, [nameof(LibLdapOptions.BindMode)] = LdapBindMode.User, [nameof(LibLdapOptions.UserBindTemplate)] = "EXAMPLE\\{0}", [nameof(LibLdapOptions.AcceptDomainQualifiedNames)] = false, [nameof(LibLdapOptions.FallbackServers)] = new List { "dc2.example.test", "dc3.example.test:6361" }, }; private static IEnumerable LibrarySettableProperties() => typeof(LibLdapOptions) .GetProperties(BindingFlags.Public | BindingFlags.Instance) .Where(p => p.SetMethod is { IsPublic: true }); // init-only setters are public SetMethods /// Every allow-listed name still exists on the library type (no stale entries). [Fact] public void Allow_list_has_no_stale_entries() { var names = LibrarySettableProperties().Select(p => p.Name).ToHashSet(StringComparer.Ordinal); foreach (var unmapped in IntentionallyUnmapped.Keys) names.ShouldContain(unmapped, $"allow-listed '{unmapped}' no longer exists on the library LdapOptions"); } /// /// Every library property is allow-listed or has a test value AND a same-named OtOpcUa option, /// and no property is both (a mapped property must not hide behind the allow-list). /// [Fact] public void Every_library_property_is_mapped_or_allow_listed() { foreach (var lib in LibrarySettableProperties()) { var allowListed = IntentionallyUnmapped.ContainsKey(lib.Name); var hasValue = NonDefaultValues.ContainsKey(lib.Name); (allowListed ^ hasValue).ShouldBeTrue( $"library LdapOptions.{lib.Name} must be either projected by ToLibraryOptions() (add a " + "non-default value to NonDefaultValues) or allow-listed with a reason — exactly one"); if (allowListed) continue; typeof(LdapOptions).GetProperty(lib.Name, BindingFlags.Public | BindingFlags.Instance) .ShouldNotBeNull($"OtOpcUa LdapOptions has no '{lib.Name}' property to bind it from configuration"); } // Reverse direction for the test data itself: no value for a property the library dropped. var libNames = LibrarySettableProperties().Select(p => p.Name).ToHashSet(StringComparer.Ordinal); foreach (var key in NonDefaultValues.Keys) libNames.ShouldContain(key, $"NonDefaultValues has '{key}', which the library LdapOptions no longer has"); } /// /// Setting each non-default value on OtOpcUa's options makes it arrive in the projected library /// options — compared by content for lists — and each value really differs from the library /// default, so the assertion could fail. /// [Fact] public void ToLibraryOptions_projects_every_mapped_property() { var app = new LdapOptions(); foreach (var (name, value) in NonDefaultValues) typeof(LdapOptions).GetProperty(name)!.SetValue(app, value); var projected = app.ToLibraryOptions(); var libDefaults = new LibLdapOptions(); foreach (var (name, expected) in NonDefaultValues) { var prop = typeof(LibLdapOptions).GetProperty(name)!; var actual = prop.GetValue(projected); var libDefault = prop.GetValue(libDefaults); ValuesEqual(libDefault, expected).ShouldBeFalse( $"test value for {name} equals the library default, so it cannot prove the mapping"); ValuesEqual(actual, expected).ShouldBeTrue( $"ToLibraryOptions() did not carry {name}: expected {Render(expected)}, got {Render(actual)}"); } } /// The projected list is a copy: mutating OtOpcUa's list afterwards does not reach it. [Fact] public void FallbackServers_projection_is_a_snapshot() { var app = new LdapOptions { FallbackServers = ["dc2"] }; var projected = app.ToLibraryOptions(); app.FallbackServers.Add("dc3"); projected.FallbackServers.ShouldBe(["dc2"]); } /// OtOpcUa's defaults for the four lmxopcua#913 properties equal the library's. [Fact] public void New_properties_default_to_the_library_defaults() { var app = new LdapOptions(); var lib = new LibLdapOptions(); app.FallbackServers.ShouldBeEmpty(); lib.FallbackServers.ShouldBeEmpty(); app.BindMode.ShouldBe(lib.BindMode); app.UserBindTemplate.ShouldBe(lib.UserBindTemplate); app.AcceptDomainQualifiedNames.ShouldBe(lib.AcceptDomainQualifiedNames); } private static bool ValuesEqual(object? a, object? b) { if (a is IEnumerable ea && a is not string && b is IEnumerable eb && b is not string) return ea.Cast().SequenceEqual(eb.Cast()); return Equals(a, b); } private static string Render(object? value) => value is IEnumerable e && value is not string ? "[" + string.Join(", ", e.Cast()) + "]" : value?.ToString() ?? "null"; }