using Microsoft.Extensions.Configuration; using ZB.MOM.WW.MxGateway.Server.Security.Tls; using Xunit; namespace ZB.MOM.WW.MxGateway.Tests.Security.Tls; public sealed class KestrelTlsInspectorTests { private static IConfiguration Config(params (string Key, string Value)[] entries) => new ConfigurationBuilder() .AddInMemoryCollection(entries.ToDictionary(e => e.Key, e => (string?)e.Value)) .Build(); [Fact] public void RequiresGeneratedCertificate_True_WhenHttpsEndpointHasNoCertificate() => Assert.True(KestrelTlsInspector.RequiresGeneratedCertificate( Config(("Kestrel:Endpoints:Http:Url", "https://0.0.0.0:5120")))); [Fact] public void RequiresGeneratedCertificate_False_WhenAllEndpointsPlaintext() => Assert.False(KestrelTlsInspector.RequiresGeneratedCertificate( Config(("Kestrel:Endpoints:Http:Url", "http://0.0.0.0:5120")))); [Fact] public void RequiresGeneratedCertificate_False_WhenHttpsEndpointHasOwnCertificate() => Assert.False(KestrelTlsInspector.RequiresGeneratedCertificate( Config( ("Kestrel:Endpoints:Http:Url", "https://0.0.0.0:5120"), ("Kestrel:Endpoints:Http:Certificate:Path", @"C:\certs\real.pfx")))); [Fact] public void RequiresGeneratedCertificate_False_WhenNoEndpointsConfigured() => Assert.False(KestrelTlsInspector.RequiresGeneratedCertificate(Config())); [Fact] public void RequiresGeneratedCertificate_False_WhenHttpsEndpointHasThumbprintOnly() => Assert.False(KestrelTlsInspector.RequiresGeneratedCertificate( Config( ("Kestrel:Endpoints:Https:Url", "https://0.0.0.0:5120"), ("Kestrel:Endpoints:Https:Certificate:Thumbprint", "AABBCCDDEEFF00112233445566778899AABBCCDD")))); [Fact] public void RequiresGeneratedCertificate_False_WhenHttpsEndpointHasSubjectOnly() => Assert.False(KestrelTlsInspector.RequiresGeneratedCertificate( Config( ("Kestrel:Endpoints:Https:Url", "https://0.0.0.0:5120"), ("Kestrel:Endpoints:Https:Certificate:Subject", "CN=myserver")))); [Fact] public void RequiresGeneratedCertificate_True_WhenHttpsUrlIsUppercase() => Assert.True(KestrelTlsInspector.RequiresGeneratedCertificate( Config(("Kestrel:Endpoints:Https:Url", "HTTPS://0.0.0.0:5120")))); [Fact] public void RequiresGeneratedCertificate_False_WhenKestrelDefaultCertificateConfigured() => Assert.False(KestrelTlsInspector.RequiresGeneratedCertificate( Config( ("Kestrel:Endpoints:Https:Url", "https://0.0.0.0:5120"), ("Kestrel:Certificates:Default:Path", @"C:\certs\default.pfx")))); [Fact] public void RequiresGeneratedCertificate_True_WhenMixedEndpointsAndOneHttpsHasNoCert() => Assert.True(KestrelTlsInspector.RequiresGeneratedCertificate( Config( ("Kestrel:Endpoints:Grpc:Url", "https://0.0.0.0:5120"), ("Kestrel:Endpoints:Grpc:Certificate:Thumbprint", "AABBCCDDEEFF00112233445566778899AABBCCDD"), ("Kestrel:Endpoints:Dashboard:Url", "https://0.0.0.0:5130")))); }