Repository: dotnet/aspnetcore
File: src/Servers/Kestrel/test/InMemory.FunctionalTests/TlsListenerTests.cs (C#)

Code before:

    [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 =>

Review comment (suggestion):
I don't think these tests should actually send any bytes over the connection. That creates a race where the timeout doesn't actually occur by the time the tls client hello is received and parsed.

Code after:
    }

    [Fact]
    public async Task TlsClientHelloBytesCallback_UsesOptionsTimeout()
    {
        var tlsClientHelloCallbackInvoked = false;
        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);
                        tlsClientHelloCallbackInvoked = true;
                        Assert.True(clientHelloBytes.Length > 32);
                        Assert.NotNull(connection);
                    };
                });
            }))
        {
            using (var connection = server.CreateConnection())
            {
                await connection.TransportConnection.Input.WriteAsync(new byte[] { 0x16 });
                var readResult = await connection.TransportConnection.Output.ReadAsync();

                // HttpsConnectionMiddleware catches the exception, so we can only check the effects of the timeout here
                Assert.True(readResult.IsCompleted);
            }
        }

        Assert.False(tlsClientHelloCallbackInvoked);
    }
}