-
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 3 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,8 @@ | |
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
using Newtonsoft.Json.Linq; | ||
using Xunit.Sdk; | ||
|
||
namespace InMemory.FunctionalTests; | ||
|
||
|
@@ -66,4 +68,98 @@ 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; | ||
|
||
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => sslStream.AuthenticateAsClientAsync(new SslClientAuthenticationOptions | ||
{ | ||
TargetHost = "localhost", | ||
EnabledSslProtocols = SslProtocols.None | ||
}, token)); | ||
} | ||
} | ||
} | ||
|
||
Assert.False(tlsClientHelloCallbackInvoked); | ||
} | ||
|
||
[Fact] | ||
public async Task TlsClientHelloBytesCallback_UsesOptionsTimeout() | ||
{ | ||
var testContext = new TestServiceContext(LoggerFactory); | ||
await using (var server = new TestServer(context => Task.CompletedTask, | ||
testContext, | ||
listenOptions => | ||
{ | ||
listenOptions.UseHttps(_x509Certificate2, httpsOptions => | ||
{ | ||
httpsOptions.HandshakeTimeout = TimeSpan.FromMilliseconds(1); | ||
|
||
httpsOptions.TlsClientHelloBytesCallback = (connection, clientHelloBytes) => | ||
{ | ||
Logger.LogDebug("[Received TlsClientHelloBytesCallback] Connection: {0}; TLS client hello buffer: {1}", connection.ConnectionId, clientHelloBytes.Length); | ||
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)) | ||
{ | ||
try | ||
{ | ||
await sslStream.AuthenticateAsClientAsync(new SslClientAuthenticationOptions | ||
{ | ||
TargetHost = "localhost", | ||
EnabledSslProtocols = SslProtocols.None | ||
}); | ||
|
||
var request = Encoding.ASCII.GetBytes("GET / HTTP/1.1\r\nHost:\r\n\r\n"); | ||
await sslStream.WriteAsync(request, 0, request.Length); | ||
await sslStream.ReadAsync(new Memory<byte>(new byte[1024])); | ||
} | ||
catch (Exception ex) | ||
when (ex is OperationCanceledException or TaskCanceledException // when cancellation comes from tls listener | ||
or IOException // when the underlying stream is closed due to timeout | ||
) | ||
{ | ||
// expected | ||
} | ||
catch (Exception ex) | ||
{ | ||
ThrowsException.ForIncorrectExceptionType(typeof(OperationCanceledException), ex); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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.
There still isn't a test using the HandshakeTimeout
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.
added