|
18 | 18 | using Microsoft.Extensions.DependencyInjection; |
19 | 19 | using Microsoft.Extensions.Hosting; |
20 | 20 | using Microsoft.Extensions.Logging; |
| 21 | +using Xunit.Sdk; |
21 | 22 |
|
22 | 23 | namespace InMemory.FunctionalTests; |
23 | 24 |
|
@@ -66,4 +67,60 @@ await sslStream.AuthenticateAsClientAsync(new SslClientAuthenticationOptions |
66 | 67 |
|
67 | 68 | Assert.True(tlsClientHelloCallbackInvoked); |
68 | 69 | } |
| 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 | + } |
69 | 126 | } |
0 commit comments