Skip to content

Commit c90f511

Browse files
committed
tests
1 parent 6a96694 commit c90f511

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ public async Task RunTlsClientHelloCallbackTest_DeterministicallyReads()
158158
Assert.Equal(5, readResult.Buffer.Length);
159159

160160
// ensuring that we have read limited number of times
161-
Assert.True(reader.ReadAsyncCounter is >= 2 && reader.ReadAsyncCounter is <= 4,
162-
$"Expected ReadAsync() to happen about 2-4 times. Actually happened {reader.ReadAsyncCounter} times.");
161+
Assert.True(reader.ReadAsyncCounter is >= 2 && reader.ReadAsyncCounter is <= 5,
162+
$"Expected ReadAsync() to happen about 2-5 times. Actually happened {reader.ReadAsyncCounter} times.");
163163
}
164164

165165
private async Task RunTlsClientHelloCallbackTest_WithMultipleSegments(

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

Lines changed: 57 additions & 0 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 Xunit.Sdk;
2122

2223
namespace InMemory.FunctionalTests;
2324

@@ -66,4 +67,60 @@ await sslStream.AuthenticateAsClientAsync(new SslClientAuthenticationOptions
6667

6768
Assert.True(tlsClientHelloCallbackInvoked);
6869
}
70+
71+
[Fact]
72+
public async Task TlsClientHelloBytesCallback_PreCanceledToken()
73+
{
74+
var tlsClientHelloCallbackInvoked = false;
75+
76+
var testContext = new TestServiceContext(LoggerFactory);
77+
await using (var server = new TestServer(context => Task.CompletedTask,
78+
testContext,
79+
listenOptions =>
80+
{
81+
listenOptions.UseHttps(_x509Certificate2, httpsOptions =>
82+
{
83+
httpsOptions.TlsClientHelloBytesCallback = (connection, clientHelloBytes) =>
84+
{
85+
Logger.LogDebug("[Received TlsClientHelloBytesCallback] Connection: {0}; TLS client hello buffer: {1}", connection.ConnectionId, clientHelloBytes.Length);
86+
tlsClientHelloCallbackInvoked = true;
87+
Assert.True(clientHelloBytes.Length > 32);
88+
Assert.NotNull(connection);
89+
};
90+
});
91+
}))
92+
{
93+
using (var connection = server.CreateConnection())
94+
{
95+
using (var sslStream = new SslStream(connection.Stream, false, (sender, cert, chain, errors) => true, null))
96+
{
97+
var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromMilliseconds(1));
98+
var token = cancellationTokenSource.Token;
99+
100+
try
101+
{
102+
await sslStream.AuthenticateAsClientAsync(new SslClientAuthenticationOptions
103+
{
104+
TargetHost = "localhost",
105+
EnabledSslProtocols = SslProtocols.None
106+
}, token);
107+
108+
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);
111+
}
112+
catch (Exception ex) when (ex is OperationCanceledException or TaskCanceledException)
113+
{
114+
// expected
115+
}
116+
catch (Exception ex)
117+
{
118+
ThrowsException.ForIncorrectExceptionType(typeof(OperationCanceledException), ex);
119+
}
120+
}
121+
}
122+
}
123+
124+
Assert.False(tlsClientHelloCallbackInvoked);
125+
}
69126
}

0 commit comments

Comments
 (0)