-
Notifications
You must be signed in to change notification settings - Fork 10.5k
follow-up: kestrel tls listener callback #62266
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
using Xunit.Sdk; | ||
|
||
namespace InMemory.FunctionalTests; | ||
|
||
|
@@ -66,4 +67,60 @@ await sslStream.AuthenticateAsClientAsync(new SslClientAuthenticationOptions | |
|
||
Assert.True(tlsClientHelloCallbackInvoked); | ||
} | ||
|
||
[Fact] | ||
public async Task TlsClientHelloBytesCallback_PreCanceledToken() | ||
|
||
{ | ||
var tlsClientHelloCallbackInvoked = false; | ||
|
||
var testContext = new TestServiceContext(LoggerFactory); | ||
await using (var server = new TestServer(context => Task.CompletedTask, | ||
testContext, | ||
listenOptions => | ||
{ | ||
listenOptions.UseHttps(_x509Certificate2, httpsOptions => | ||
{ | ||
httpsOptions.TlsClientHelloBytesCallback = (connection, clientHelloBytes) => | ||
{ | ||
Logger.LogDebug("[Received TlsClientHelloBytesCallback] Connection: {0}; TLS client hello buffer: {1}", connection.ConnectionId, clientHelloBytes.Length); | ||
tlsClientHelloCallbackInvoked = true; | ||
Assert.True(clientHelloBytes.Length > 32); | ||
Assert.NotNull(connection); | ||
}; | ||
}); | ||
})) | ||
{ | ||
using (var connection = server.CreateConnection()) | ||
{ | ||
using (var sslStream = new SslStream(connection.Stream, false, (sender, cert, chain, errors) => true, null)) | ||
|
||
{ | ||
var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromMilliseconds(1)); | ||
var token = cancellationTokenSource.Token; | ||
|
||
try | ||
{ | ||
await sslStream.AuthenticateAsClientAsync(new SslClientAuthenticationOptions | ||
{ | ||
TargetHost = "localhost", | ||
EnabledSslProtocols = SslProtocols.None | ||
}, token); | ||
|
||
var request = Encoding.ASCII.GetBytes("GET / HTTP/1.1\r\nHost:\r\n\r\n"); | ||
await sslStream.WriteAsync(request, 0, request.Length, token); | ||
await sslStream.ReadAsync(new Memory<byte>(new byte[1024]), token); | ||
} | ||
catch (Exception ex) when (ex is OperationCanceledException or TaskCanceledException) | ||
{ | ||
// expected | ||
} | ||
catch (Exception ex) | ||
{ | ||
ThrowsException.ForIncorrectExceptionType(typeof(OperationCanceledException), ex); | ||
} | ||
} | ||
} | ||
} | ||
|
||
Assert.False(tlsClientHelloCallbackInvoked); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Normally we just use
Assert.ThrowsAnyAsync<OperationCanceledException>(...);
for thisThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks, i did not see that API before. refactored