Skip to content

Commit f5e10d6

Browse files
committed
Add more tests
1 parent c34c31a commit f5e10d6

File tree

2 files changed

+100
-12
lines changed

2 files changed

+100
-12
lines changed

test/Core.Test/Core.Test.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1111
<PrivateAssets>all</PrivateAssets>
1212
</PackageReference>
13+
<PackageReference Include="Microsoft.Extensions.Diagnostics.Testing" Version="9.3.0" />
1314
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftNetTestSdkVersion)" />
1415
<PackageReference Include="NSubstitute" Version="$(NSubstituteVersion)" />
1516
<PackageReference Include="xunit" Version="$(XUnitVersion)" />

test/Core.Test/Platform/X509ChainCustomization/X509ChainCustomizationServiceCollectionExtensionsTests.cs

+99-12
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Microsoft.Extensions.Configuration;
66
using Microsoft.Extensions.DependencyInjection;
77
using Microsoft.Extensions.Hosting;
8+
using Microsoft.Extensions.Logging;
89
using Microsoft.Extensions.Options;
910
using NSubstitute;
1011
using Xunit;
@@ -30,10 +31,6 @@ public async Task OptionsPatternReturnsCachedValue()
3031

3132
var services = CreateServices((gs, environment, config) =>
3233
{
33-
gs.SelfHosted = true;
34-
35-
environment.EnvironmentName = "Development";
36-
3734
config["X509ChainOptions:AdditionalCustomTrustCertificatesDirectory"] = tempDir.FullName;
3835
});
3936

@@ -69,8 +66,6 @@ public async Task DoesNotProvideCustomCallbackOnCloud()
6966
{
7067
gs.SelfHosted = false;
7168

72-
environment.EnvironmentName = "Development";
73-
7469
config["X509ChainOptions:AdditionalCustomTrustCertificatesDirectory"] = tempDir.FullName;
7570
});
7671

@@ -85,12 +80,8 @@ public async Task ManuallyAddingOptionsTakesPrecedence()
8580
var tempCert = Path.Combine(tempDir.FullName, "test.crt");
8681
await File.WriteAllBytesAsync(tempCert, CreateSelfSignedCert("localhost").Export(X509ContentType.Cert));
8782

88-
var options = CreateOptions((gs, environment, config) =>
83+
var services = CreateServices((gs, environment, config) =>
8984
{
90-
gs.SelfHosted = false;
91-
92-
environment.EnvironmentName = "Development";
93-
9485
config["X509ChainOptions:AdditionalCustomTrustCertificatesDirectory"] = tempDir.FullName;
9586
}, services =>
9687
{
@@ -100,9 +91,95 @@ public async Task ManuallyAddingOptionsTakesPrecedence()
10091
});
10192
});
10293

94+
var options = services.GetRequiredService<IOptions<X509ChainOptions>>().Value;
95+
10396
Assert.True(options.TryGetCustomRemoteCertificateValidationCallback(out var callback));
10497
var cert = Assert.Single(options.AdditionalCustomTrustCertificates);
10598
Assert.Equal("CN=example.com", cert.Subject);
99+
100+
var fakeLogCollector = services.GetFakeLogCollector();
101+
102+
Assert.Contains(fakeLogCollector.GetSnapshot(),
103+
r => r.Message == $"Additional custom trust certificates were added directly, skipping loading them from '{tempDir}'");
104+
}
105+
106+
[Fact]
107+
public void NullCustomDirectory_SkipsTryingToLoad()
108+
{
109+
var services = CreateServices((gs, environment, config) =>
110+
{
111+
config["X509ChainOptions:AdditionalCustomTrustCertificatesDirectory"] = null;
112+
});
113+
114+
var options = services.GetRequiredService<IOptions<X509ChainOptions>>().Value;
115+
116+
Assert.False(options.TryGetCustomRemoteCertificateValidationCallback(out _));
117+
}
118+
119+
[Theory]
120+
[InlineData("Development", LogLevel.Debug)]
121+
[InlineData("Production", LogLevel.Warning)]
122+
public void CustomDirectoryDoesNotExist_Logs(string environment, LogLevel logLevel)
123+
{
124+
var fakeDir = "/fake/dir/that/does/not/exist";
125+
var services = CreateServices((gs, hostEnvironment, config) =>
126+
{
127+
hostEnvironment.EnvironmentName = environment;
128+
129+
config["X509ChainOptions:AdditionalCustomTrustCertificatesDirectory"] = fakeDir;
130+
});
131+
132+
var options = services.GetRequiredService<IOptions<X509ChainOptions>>().Value;
133+
134+
Assert.False(options.TryGetCustomRemoteCertificateValidationCallback(out _));
135+
136+
var fakeLogCollector = services.GetFakeLogCollector();
137+
138+
Assert.Contains(fakeLogCollector.GetSnapshot(),
139+
r => r.Message == $"An additional custom trust certificate directory was given '{fakeDir}' but that directory does not exist."
140+
&& r.Level == logLevel
141+
);
142+
}
143+
144+
[Fact]
145+
public async Task NamedOptions_NotConfiguredAsync()
146+
{
147+
// To help make sure this fails for the right reason we should add certs to the directory
148+
var tempDir = Directory.CreateTempSubdirectory("certs");
149+
150+
var tempCert = Path.Combine(tempDir.FullName, "test.crt");
151+
await File.WriteAllBytesAsync(tempCert, CreateSelfSignedCert("localhost").Export(X509ContentType.Cert));
152+
153+
var services = CreateServices((gs, environment, config) =>
154+
{
155+
config["X509ChainOptions:AdditionalCustomTrustCertificatesDirectory"] = tempDir.FullName;
156+
});
157+
158+
var options = services.GetRequiredService<IOptionsMonitor<X509ChainOptions>>();
159+
160+
var namedOptions = options.Get("SomeName");
161+
162+
Assert.Null(namedOptions.AdditionalCustomTrustCertificates);
163+
}
164+
165+
[Fact]
166+
public void CustomLocation_NoCertificates_Logs()
167+
{
168+
var tempDir = Directory.CreateTempSubdirectory("certs");
169+
var services = CreateServices((gs, hostEnvironment, config) =>
170+
{
171+
config["X509ChainOptions:AdditionalCustomTrustCertificatesDirectory"] = tempDir.FullName;
172+
});
173+
174+
var options = services.GetRequiredService<IOptions<X509ChainOptions>>().Value;
175+
176+
Assert.False(options.TryGetCustomRemoteCertificateValidationCallback(out _));
177+
178+
var fakeLogCollector = services.GetFakeLogCollector();
179+
180+
Assert.Contains(fakeLogCollector.GetSnapshot(),
181+
r => r.Message == $"No additional custom trust certificates were found in '{tempDir.FullName}'"
182+
);
106183
}
107184

108185
private static X509ChainOptions CreateOptions(Action<GlobalSettings, IHostEnvironment, Dictionary<string, string>> configure, Action<IServiceCollection>? after = null)
@@ -113,13 +190,23 @@ private static X509ChainOptions CreateOptions(Action<GlobalSettings, IHostEnviro
113190

114191
private static IServiceProvider CreateServices(Action<GlobalSettings, IHostEnvironment, Dictionary<string, string>> configure, Action<IServiceCollection>? after = null)
115192
{
116-
var globalSettings = new GlobalSettings();
193+
var globalSettings = new GlobalSettings
194+
{
195+
// A solid default for these tests as these settings aren't allowed to work in cloud.
196+
SelfHosted = true,
197+
};
117198
var hostEnvironment = Substitute.For<IHostEnvironment>();
199+
hostEnvironment.EnvironmentName = "Development";
118200
var config = new Dictionary<string, string>();
119201

120202
configure(globalSettings, hostEnvironment, config);
121203

122204
var services = new ServiceCollection();
205+
services.AddLogging(logging =>
206+
{
207+
logging.SetMinimumLevel(LogLevel.Debug);
208+
logging.AddFakeLogging();
209+
});
123210
services.AddSingleton(globalSettings);
124211
services.AddSingleton(hostEnvironment);
125212
services.AddSingleton<IConfiguration>(

0 commit comments

Comments
 (0)