Skip to content

Commit 3c1f92e

Browse files
committed
make meaningfull test
1 parent 5d3e42d commit 3c1f92e

File tree

1 file changed

+8
-62
lines changed

1 file changed

+8
-62
lines changed

src/Servers/Kestrel/test/InMemory.FunctionalTests/TlsListenerTests.cs

Lines changed: 8 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.IO.Pipelines;
67
using System.Net;
78
using System.Net.Security;
89
using System.Security.Authentication;
@@ -70,17 +71,18 @@ await sslStream.AuthenticateAsClientAsync(new SslClientAuthenticationOptions
7071
}
7172

7273
[Fact]
73-
public async Task TlsClientHelloBytesCallback_PreCanceledToken()
74+
public async Task TlsClientHelloBytesCallback_UsesOptionsTimeout()
7475
{
7576
var tlsClientHelloCallbackInvoked = false;
76-
7777
var testContext = new TestServiceContext(LoggerFactory);
7878
await using (var server = new TestServer(context => Task.CompletedTask,
7979
testContext,
8080
listenOptions =>
8181
{
8282
listenOptions.UseHttps(_x509Certificate2, httpsOptions =>
8383
{
84+
httpsOptions.HandshakeTimeout = TimeSpan.FromMilliseconds(1);
85+
8486
httpsOptions.TlsClientHelloBytesCallback = (connection, clientHelloBytes) =>
8587
{
8688
Logger.LogDebug("[Received TlsClientHelloBytesCallback] Connection: {0}; TLS client hello buffer: {1}", connection.ConnectionId, clientHelloBytes.Length);
@@ -95,71 +97,15 @@ public async Task TlsClientHelloBytesCallback_PreCanceledToken()
9597
{
9698
using (var sslStream = new SslStream(connection.Stream, false, (sender, cert, chain, errors) => true, null))
9799
{
98-
var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromMilliseconds(1));
99-
var token = cancellationTokenSource.Token;
100+
await connection.TransportConnection.Input.WriteAsync(new byte[] { 0x16 });
101+
var readResult = await connection.TransportConnection.Output.ReadAsync();
100102

101-
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => sslStream.AuthenticateAsClientAsync(new SslClientAuthenticationOptions
102-
{
103-
TargetHost = "localhost",
104-
EnabledSslProtocols = SslProtocols.None
105-
}, token));
103+
// HttpsConnectionMiddleware catches the exception, so we can only check the effects of the timeout here
104+
Assert.True(readResult.IsCompleted);
106105
}
107106
}
108107
}
109108

110109
Assert.False(tlsClientHelloCallbackInvoked);
111110
}
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-
{
138-
try
139-
{
140-
await sslStream.AuthenticateAsClientAsync(new SslClientAuthenticationOptions
141-
{
142-
TargetHost = "localhost",
143-
EnabledSslProtocols = SslProtocols.None
144-
});
145-
146-
var request = Encoding.ASCII.GetBytes("GET / HTTP/1.1\r\nHost:\r\n\r\n");
147-
await sslStream.WriteAsync(request, 0, request.Length);
148-
await sslStream.ReadAsync(new Memory<byte>(new byte[1024]));
149-
}
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-
)
154-
{
155-
// expected
156-
}
157-
catch (Exception ex)
158-
{
159-
ThrowsException.ForIncorrectExceptionType(typeof(OperationCanceledException), ex);
160-
}
161-
}
162-
}
163-
}
164-
}
165111
}

0 commit comments

Comments
 (0)