1818using Microsoft . Extensions . DependencyInjection ;
1919using Microsoft . Extensions . Hosting ;
2020using Microsoft . Extensions . Logging ;
21+ using Newtonsoft . Json . Linq ;
2122using Xunit . Sdk ;
2223
2324namespace 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 \n Host:\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