Skip to content

Commit 2e9b7db

Browse files
committed
Fix tests
1 parent 2837991 commit 2e9b7db

File tree

3 files changed

+31
-37
lines changed

3 files changed

+31
-37
lines changed

src/Servers/Kestrel/Core/src/Middleware/HttpsConnectionMiddleware.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,7 @@ public async Task OnConnectionAsync(ConnectionContext context)
220220
}
221221
catch (AuthenticationException ex)
222222
{
223-
if (_serverCertificate == null)
224-
{
225-
_logger.LogDebug(1, ex, CoreStrings.AuthenticationFailed);
226-
}
223+
_logger.LogDebug(1, ex, CoreStrings.AuthenticationFailed);
227224

228225
await sslStream.DisposeAsync();
229226
return;

src/Servers/Kestrel/Kestrel/test/KestrelConfigurationBuilderTests.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.IO;
77
using System.Linq;
88
using System.Security.Cryptography.X509Certificates;
9+
using System.Threading.Tasks;
910
using Microsoft.AspNetCore.Hosting;
1011
using Microsoft.AspNetCore.Server.Kestrel.Core;
1112
using Microsoft.AspNetCore.Server.Kestrel.Https;
@@ -14,6 +15,8 @@
1415
using Microsoft.Extensions.DependencyInjection;
1516
using Microsoft.Extensions.FileProviders;
1617
using Microsoft.Extensions.Hosting;
18+
using Microsoft.Extensions.Logging;
19+
using Microsoft.Extensions.Logging.Testing;
1720
using Xunit;
1821

1922
namespace Microsoft.AspNetCore.Server.Kestrel.Tests
@@ -22,15 +25,41 @@ public class KestrelConfigurationBuilderTests
2225
{
2326
private KestrelServerOptions CreateServerOptions()
2427
{
28+
var testSink = new TestSink();
2529
var serverOptions = new KestrelServerOptions();
2630
var env = new MockHostingEnvironment { ApplicationName = "TestApplication" };
2731
serverOptions.ApplicationServices = new ServiceCollection()
28-
.AddLogging()
32+
.AddLogging(l => l.AddProvider(new TestLoggerProvider(testSink)))
2933
.AddSingleton<IHostEnvironment>(env)
34+
.AddSingleton(testSink)
3035
.BuildServiceProvider();
3136
return serverOptions;
3237
}
3338

39+
[Fact]
40+
[SkipOnHelix("(none)", Queues = "Ubuntu.1604.Amd64.Open;Windows.10.Amd64.Open")]
41+
[OSSkipCondition(OperatingSystems.Linux, SkipReason = "This check only applies to Mac OS as it is the one that checks the key for validity.")]
42+
[OSSkipCondition(OperatingSystems.Windows, SkipReason = "This check only applies to Mac OS as it is the one that checks the key for validity.")]
43+
public void DevCertWithInvalidPrivateKeyProducesCustomWarning()
44+
{
45+
var serverOptions = CreateServerOptions();
46+
var sink = serverOptions.ApplicationServices.GetRequiredService<TestSink>();
47+
var messages = new List<WriteContext>();
48+
sink.MessageLogged += wc => messages.Add(wc);
49+
serverOptions.Configure()
50+
.LocalhostEndpoint(5001, endpointOptions => { });
51+
52+
Assert.Empty(serverOptions.ListenOptions);
53+
54+
serverOptions.ConfigurationLoader.Load();
55+
56+
Assert.Single(serverOptions.ListenOptions);
57+
Assert.Equal(5001, serverOptions.ListenOptions[0].IPEndPoint.Port);
58+
59+
Assert.Equal(5, messages[^1].EventId);
60+
Assert.Equal(LogLevel.Warning, messages[^1].LogLevel);
61+
}
62+
3463
[Fact]
3564
public void ConfigureNamedEndpoint_OnlyRunForMatchingConfig()
3665
{

src/Servers/Kestrel/test/InMemory.FunctionalTests/HttpsTests.cs

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -385,38 +385,6 @@ await Assert.ThrowsAnyAsync<Exception>(() =>
385385
Assert.Equal(LogLevel.Debug, loggerProvider.FilterLogger.LastLogLevel);
386386
}
387387

388-
[Fact]
389-
[SkipOnHelix("", Queues = "Ubuntu.1604.Amd64.Open;Windows.10.Amd64.Open")]
390-
[OSSkipCondition(OperatingSystems.Linux, SkipReason = "This check only applies to Mac OS as it is the one that checks the key for validity.")]
391-
[OSSkipCondition(OperatingSystems.Windows, SkipReason = "This check only applies to Mac OS as it is the one that checks the key for validity.")]
392-
public async Task DevCertWithInvalidPrivateKeyProducesCustomWarning()
393-
{
394-
var loggerProvider = new HandshakeErrorLoggerProvider();
395-
LoggerFactory.AddProvider(loggerProvider);
396-
397-
await using (var server = new TestServer(context => Task.CompletedTask,
398-
new TestServiceContext(LoggerFactory),
399-
listenOptions =>
400-
{
401-
listenOptions.UseHttps(TestResources.GetTestCertificate("aspnetdevcert.pfx", "testPassword"));
402-
}))
403-
{
404-
using (var connection = server.CreateConnection())
405-
using (var sslStream = new SslStream(connection.Stream, true, (sender, certificate, chain, errors) => true))
406-
{
407-
// SslProtocols.Tls is TLS 1.0 which isn't supported by Kestrel by default.
408-
await Assert.ThrowsAnyAsync<Exception>(() =>
409-
sslStream.AuthenticateAsClientAsync("127.0.0.1", clientCertificates: null,
410-
enabledSslProtocols: SslProtocols.Tls,
411-
checkCertificateRevocation: false));
412-
}
413-
}
414-
415-
await loggerProvider.FilterLogger.LogTcs.Task.DefaultTimeout();
416-
Assert.Equal(3, loggerProvider.FilterLogger.LastEventId);
417-
Assert.Equal(LogLevel.Error, loggerProvider.FilterLogger.LastLogLevel);
418-
}
419-
420388
[Fact]
421389
public async Task OnAuthenticate_SeesOtherSettings()
422390
{

0 commit comments

Comments
 (0)