Skip to content

Commit 5d3e42d

Browse files
committed
use xunit API + use handshake timeout
1 parent da3355f commit 5d3e42d

File tree

2 files changed

+48
-37
lines changed

2 files changed

+48
-37
lines changed

src/Servers/Kestrel/Core/test/TlsListenerTests.cs

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,7 @@ public async Task RunTlsClientHelloCallbackTest_WithExtraShortLastingToken()
7070
var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(3));
7171

7272
await writer.WriteAsync(new byte[1] { 0x16 });
73-
await VerifyThrowsAnyAsync(
74-
async () => await listener.OnTlsClientHelloAsync(transportConnection, cts.Token),
75-
typeof(OperationCanceledException), typeof(TaskCanceledException));
73+
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => listener.OnTlsClientHelloAsync(transportConnection, cts.Token));
7674
Assert.False(tlsClientHelloCallbackInvoked);
7775
}
7876

@@ -95,9 +93,7 @@ public async Task RunTlsClientHelloCallbackTest_WithPreCanceledToken()
9593
cts.Cancel();
9694

9795
await writer.WriteAsync(new byte[1] { 0x16 });
98-
await VerifyThrowsAnyAsync(
99-
async () => await listener.OnTlsClientHelloAsync(transportConnection, cts.Token),
100-
typeof(OperationCanceledException), typeof(TaskCanceledException));
96+
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => listener.OnTlsClientHelloAsync(transportConnection, cts.Token));
10197
Assert.False(tlsClientHelloCallbackInvoked);
10298
}
10399

@@ -122,7 +118,7 @@ public async Task RunTlsClientHelloCallbackTest_WithPendingCancellation()
122118
await writer.WriteAsync(new byte[2] { 0x03, 0x01 });
123119
cts.Cancel();
124120

125-
await VerifyThrowsAnyAsync(() => listenerTask, typeof(OperationCanceledException), typeof(TaskCanceledException));
121+
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => listenerTask);
126122
Assert.False(tlsClientHelloCallbackInvoked);
127123
}
128124

@@ -623,28 +619,4 @@ public static IEnumerable<object[]> InvalidClientHelloData_Segmented()
623619
_invalidTlsClientHelloHeader, _invalid3BytesMessage, _invalid9BytesMessage,
624620
_invalidUnknownProtocolVersion1, _invalidUnknownProtocolVersion2, _invalidIncorrectHandshakeMessageType
625621
};
626-
627-
static async Task VerifyThrowsAnyAsync(Func<Task> code, params Type[] exceptionTypes)
628-
{
629-
if (exceptionTypes == null || exceptionTypes.Length == 0)
630-
{
631-
throw new ArgumentException("At least one exception type must be provided.", nameof(exceptionTypes));
632-
}
633-
634-
try
635-
{
636-
await code();
637-
}
638-
catch (Exception ex)
639-
{
640-
if (exceptionTypes.Any(type => type.IsInstanceOfType(ex)))
641-
{
642-
return;
643-
}
644-
645-
throw ThrowsException.ForIncorrectExceptionType(exceptionTypes.First(), ex);
646-
}
647-
648-
throw ThrowsException.ForNoException(exceptionTypes.First());
649-
}
650622
}

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

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using Microsoft.Extensions.DependencyInjection;
1919
using Microsoft.Extensions.Hosting;
2020
using Microsoft.Extensions.Logging;
21+
using Newtonsoft.Json.Linq;
2122
using Xunit.Sdk;
2223

2324
namespace InMemory.FunctionalTests;
@@ -97,19 +98,59 @@ public async Task TlsClientHelloBytesCallback_PreCanceledToken()
9798
var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromMilliseconds(1));
9899
var token = cancellationTokenSource.Token;
99100

101+
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => sslStream.AuthenticateAsClientAsync(new SslClientAuthenticationOptions
102+
{
103+
TargetHost = "localhost",
104+
EnabledSslProtocols = SslProtocols.None
105+
}, token));
106+
}
107+
}
108+
}
109+
110+
Assert.False(tlsClientHelloCallbackInvoked);
111+
}
112+
113+
[Fact]
114+
public async Task TlsClientHelloBytesCallback_UsesOptionsTimeout()
115+
{
116+
var testContext = new TestServiceContext(LoggerFactory);
117+
await using (var server = new TestServer(context => Task.CompletedTask,
118+
testContext,
119+
listenOptions =>
120+
{
121+
listenOptions.UseHttps(_x509Certificate2, httpsOptions =>
122+
{
123+
httpsOptions.HandshakeTimeout = TimeSpan.FromMilliseconds(1);
124+
125+
httpsOptions.TlsClientHelloBytesCallback = (connection, clientHelloBytes) =>
126+
{
127+
Logger.LogDebug("[Received TlsClientHelloBytesCallback] Connection: {0}; TLS client hello buffer: {1}", connection.ConnectionId, clientHelloBytes.Length);
128+
Assert.True(clientHelloBytes.Length > 32);
129+
Assert.NotNull(connection);
130+
};
131+
});
132+
}))
133+
{
134+
using (var connection = server.CreateConnection())
135+
{
136+
using (var sslStream = new SslStream(connection.Stream, false, (sender, cert, chain, errors) => true, null))
137+
{
100138
try
101139
{
102140
await sslStream.AuthenticateAsClientAsync(new SslClientAuthenticationOptions
103141
{
104142
TargetHost = "localhost",
105143
EnabledSslProtocols = SslProtocols.None
106-
}, token);
144+
});
107145

108146
var request = Encoding.ASCII.GetBytes("GET / HTTP/1.1\r\nHost:\r\n\r\n");
109-
await sslStream.WriteAsync(request, 0, request.Length, token);
110-
await sslStream.ReadAsync(new Memory<byte>(new byte[1024]), token);
147+
await sslStream.WriteAsync(request, 0, request.Length);
148+
await sslStream.ReadAsync(new Memory<byte>(new byte[1024]));
111149
}
112-
catch (Exception ex) when (ex is OperationCanceledException or TaskCanceledException)
150+
catch (Exception ex)
151+
when (ex is OperationCanceledException or TaskCanceledException // when cancellation comes from tls listener
152+
or IOException // when the underlying stream is closed due to timeout
153+
)
113154
{
114155
// expected
115156
}
@@ -120,7 +161,5 @@ await sslStream.AuthenticateAsClientAsync(new SslClientAuthenticationOptions
120161
}
121162
}
122163
}
123-
124-
Assert.False(tlsClientHelloCallbackInvoked);
125164
}
126165
}

0 commit comments

Comments
 (0)