Skip to content

Commit 654a574

Browse files
authored
retire DummyTcpServer from SslStream tests (#65876)
* retire DummyTcpServer from SslStream test * fix build * feedback from review
1 parent 3b72c25 commit 654a574

12 files changed

+236
-580
lines changed

src/libraries/System.Net.Quic/tests/FunctionalTests/System.Net.Quic.Functional.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<Compile Include="$(CommonTestPath)System\Threading\Tasks\TaskTimeoutExtensions.cs" Link="TestCommon\System\Threading\Tasks\TaskTimeoutExtensions.cs" />
2525
<Compile Include="$(CommonTestPath)TestUtilities\System\DisableParallelization.cs" Link="Common\TestUtilities\System\DisableParallelization.cs" />
2626
<Compile Include="..\..\..\System.Net.Security\tests\FunctionalTests\TestHelper.cs" />
27+
<Compile Include="..\..\..\System.Net.Security\tests\FunctionalTests\TestConfiguration.cs" />
2728
</ItemGroup>
2829
<ItemGroup>
2930
<ProjectReference Include="$(CommonTestPath)StreamConformanceTests\StreamConformanceTests.csproj" />

src/libraries/System.Net.Security/tests/FunctionalTests/ClientAsyncAuthenticateTest.cs

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Collections.Generic;
5-
using System.IO;
6-
using System.Net.Sockets;
75
using System.Net.Test.Common;
86
using System.Security.Authentication;
97
using System.Security.Cryptography.X509Certificates;
@@ -131,25 +129,49 @@ private async Task ClientAsyncSslHelper(
131129
{
132130
_log.WriteLine("Server: " + serverSslProtocols + "; Client: " + clientSslProtocols);
133131

134-
IPEndPoint endPoint = new IPEndPoint(IPAddress.Loopback, 0);
132+
(SslStream client, SslStream server) = TestHelper.GetConnectedSslStreams();
135133

136-
using (var server = new DummyTcpServer(endPoint, encryptionPolicy))
137-
using (var client = new TcpClient())
134+
using (client)
135+
using (server)
138136
{
139-
server.SslProtocols = serverSslProtocols;
140137
// Use a different SNI for each connection to prevent TLS 1.3 renegotiation issue: https://github.com/dotnet/runtime/issues/47378
141138
string serverName = TestHelper.GetTestSNIName(nameof(ClientAsyncSslHelper), clientSslProtocols, serverSslProtocols);
142139

143-
await client.ConnectAsync(server.RemoteEndPoint.Address, server.RemoteEndPoint.Port);
144-
using (SslStream sslStream = new SslStream(client.GetStream(), false, certificateCallback != null ? certificateCallback : AllowAnyServerCertificate, null))
140+
Task serverTask = default;
141+
try
145142
{
146-
Task clientAuthTask = sslStream.AuthenticateAsClientAsync(serverName, null, clientSslProtocols, false);
147-
await clientAuthTask.WaitAsync(TestConfiguration.PassingTestTimeout);
148-
149-
_log.WriteLine("Client authenticated to server({0}) with encryption cipher: {1} {2}-bit strength",
150-
server.RemoteEndPoint, sslStream.CipherAlgorithm, sslStream.CipherStrength);
151-
Assert.True(sslStream.CipherAlgorithm != CipherAlgorithmType.Null, "Cipher algorithm should not be NULL");
152-
Assert.True(sslStream.CipherStrength > 0, "Cipher strength should be greater than 0");
143+
Task clientTask = client.AuthenticateAsClientAsync(new SslClientAuthenticationOptions
144+
{
145+
EnabledSslProtocols = clientSslProtocols,
146+
RemoteCertificateValidationCallback = AllowAnyServerCertificate,
147+
TargetHost = serverName });
148+
serverTask = server.AuthenticateAsServerAsync( new SslServerAuthenticationOptions
149+
{
150+
EncryptionPolicy = encryptionPolicy,
151+
EnabledSslProtocols = serverSslProtocols,
152+
ServerCertificate = TestConfiguration.ServerCertificate,
153+
CertificateRevocationCheckMode = X509RevocationMode.NoCheck });
154+
155+
await clientTask.WaitAsync(TestConfiguration.PassingTestTimeout);
156+
157+
_log.WriteLine("Client authenticated to server with encryption cipher: {0} {1}-bit strength",
158+
client.CipherAlgorithm, client.CipherStrength);
159+
Assert.True(client.CipherAlgorithm != CipherAlgorithmType.Null, "Cipher algorithm should not be NULL");
160+
Assert.True(client.CipherStrength > 0, "Cipher strength should be greater than 0");
161+
}
162+
finally
163+
{
164+
// make sure we signal server in case of client failures
165+
client.Close();
166+
try
167+
{
168+
await serverTask;
169+
}
170+
catch (Exception ex)
171+
{
172+
// We generally don't care about server but can log exception to help diagnose test failures
173+
_log.WriteLine(ex.ToString());
174+
}
153175
}
154176
}
155177
}

src/libraries/System.Net.Security/tests/FunctionalTests/ClientDefaultEncryptionTest.cs

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System.IO;
54
using System.Net.Sockets;
65
using System.Net.Test.Common;
76
using System.Security.Authentication;
8-
using System.Security.Cryptography.X509Certificates;
97
using System.Threading.Tasks;
108

119
using Xunit;
@@ -17,74 +15,78 @@ public class ClientDefaultEncryptionTest
1715
{
1816
private readonly ITestOutputHelper _log;
1917

20-
public ClientDefaultEncryptionTest()
18+
public ClientDefaultEncryptionTest(ITestOutputHelper output)
2119
{
22-
_log = TestLogging.GetInstance();
23-
}
24-
25-
// The following method is invoked by the RemoteCertificateValidationDelegate.
26-
public bool AllowAnyServerCertificate(
27-
object sender,
28-
X509Certificate certificate,
29-
X509Chain chain,
30-
SslPolicyErrors sslPolicyErrors)
31-
{
32-
return true; // allow everything
20+
_log = output;
3321
}
3422

3523
[Fact]
3624
public async Task ClientDefaultEncryption_ServerRequireEncryption_ConnectWithEncryption()
3725
{
38-
using (var serverRequireEncryption = new DummyTcpServer(
39-
new IPEndPoint(IPAddress.Loopback, 0), EncryptionPolicy.RequireEncryption))
40-
using (var client = new TcpClient())
26+
(NetworkStream clientStream, NetworkStream serverStream) = TestHelper.GetConnectedTcpStreams();
27+
using (clientStream)
28+
using (serverStream)
4129
{
42-
await client.ConnectAsync(serverRequireEncryption.RemoteEndPoint.Address, serverRequireEncryption.RemoteEndPoint.Port);
43-
44-
using (var sslStream = new SslStream(client.GetStream(), false, AllowAnyServerCertificate, null))
30+
using (var client = new SslStream(clientStream, false, TestHelper.AllowAnyServerCertificate, null))
31+
using (var server = new SslStream(serverStream))
4532
{
46-
await sslStream.AuthenticateAsClientAsync("localhost", null, SslProtocolSupport.DefaultSslProtocols, false);
33+
await TestConfiguration.WhenAllOrAnyFailedWithTimeout(
34+
client.AuthenticateAsClientAsync("localhost", null, SslProtocolSupport.DefaultSslProtocols, false),
35+
server.AuthenticateAsServerAsync(TestConfiguration.ServerCertificate));
36+
4737
_log.WriteLine("Client authenticated to server({0}) with encryption cipher: {1} {2}-bit strength",
48-
serverRequireEncryption.RemoteEndPoint, sslStream.CipherAlgorithm, sslStream.CipherStrength);
49-
Assert.True(sslStream.CipherAlgorithm != CipherAlgorithmType.Null, "Cipher algorithm should not be NULL");
50-
Assert.True(sslStream.CipherStrength > 0, "Cipher strength should be greater than 0");
38+
clientStream.Socket.RemoteEndPoint, client.CipherAlgorithm, client.CipherStrength) ;
39+
Assert.True(client.CipherAlgorithm != CipherAlgorithmType.Null, "Cipher algorithm should not be NULL");
40+
Assert.True(client.CipherStrength > 0, "Cipher strength should be greater than 0");
5141
}
5242
}
5343
}
5444

5545
[Fact]
5646
public async Task ClientDefaultEncryption_ServerAllowNoEncryption_ConnectWithEncryption()
5747
{
58-
using (var serverAllowNoEncryption = new DummyTcpServer(
59-
new IPEndPoint(IPAddress.Loopback, 0), EncryptionPolicy.AllowNoEncryption))
60-
using (var client = new TcpClient())
48+
(NetworkStream clientStream, NetworkStream serverStream) = TestHelper.GetConnectedTcpStreams();
49+
using (clientStream)
50+
using (serverStream)
6151
{
62-
await client.ConnectAsync(serverAllowNoEncryption.RemoteEndPoint.Address, serverAllowNoEncryption.RemoteEndPoint.Port);
63-
64-
using (var sslStream = new SslStream(client.GetStream(), false, AllowAnyServerCertificate, null))
52+
using (var client = new SslStream(clientStream, false, TestHelper.AllowAnyServerCertificate, null))
53+
using (var server = new SslStream(serverStream))
6554
{
66-
await sslStream.AuthenticateAsClientAsync("localhost", null, SslProtocolSupport.DefaultSslProtocols, false);
55+
await TestConfiguration.WhenAllOrAnyFailedWithTimeout(
56+
client.AuthenticateAsClientAsync("localhost", null, SslProtocolSupport.DefaultSslProtocols, false),
57+
server.AuthenticateAsServerAsync(TestConfiguration.ServerCertificate));
58+
6759
_log.WriteLine("Client authenticated to server({0}) with encryption cipher: {1} {2}-bit strength",
68-
serverAllowNoEncryption.RemoteEndPoint, sslStream.CipherAlgorithm, sslStream.CipherStrength);
69-
Assert.True(sslStream.CipherAlgorithm != CipherAlgorithmType.Null, "Cipher algorithm should not be NULL");
70-
Assert.True(sslStream.CipherStrength > 0, "Cipher strength should be greater than 0");
60+
clientStream.Socket.RemoteEndPoint, client.CipherAlgorithm, client.CipherStrength);
61+
Assert.True(client.CipherAlgorithm != CipherAlgorithmType.Null, "Cipher algorithm should not be NULL");
62+
Assert.True(client.CipherStrength > 0, "Cipher strength should be greater than 0");
7163
}
7264
}
7365
}
7466

7567
[Fact]
7668
public async Task ClientDefaultEncryption_ServerNoEncryption_NoConnect()
7769
{
78-
using (var serverNoEncryption = new DummyTcpServer(
79-
new IPEndPoint(IPAddress.Loopback, 0), EncryptionPolicy.NoEncryption))
80-
using (var client = new TcpClient())
70+
(NetworkStream clientStream, NetworkStream serverStream) = TestHelper.GetConnectedTcpStreams();
71+
using (clientStream)
72+
using (serverStream)
8173
{
82-
await client.ConnectAsync(serverNoEncryption.RemoteEndPoint.Address, serverNoEncryption.RemoteEndPoint.Port);
83-
84-
using (var sslStream = new SslStream(client.GetStream(), false, AllowAnyServerCertificate, null))
74+
using (var client = new SslStream(clientStream, false, TestHelper.AllowAnyServerCertificate, null))
75+
using (var server = new SslStream(serverStream, false, TestHelper.AllowAnyServerCertificate, null, EncryptionPolicy.NoEncryption))
8576
{
77+
Task serverTask = server.AuthenticateAsServerAsync(TestConfiguration.ServerCertificate);
8678
await Assert.ThrowsAsync<AuthenticationException>(() =>
87-
sslStream.AuthenticateAsClientAsync("localhost", null, SslProtocolSupport.DefaultSslProtocols, false));
79+
client.AuthenticateAsClientAsync("localhost", null, SslProtocolSupport.DefaultSslProtocols, false));
80+
try
81+
{
82+
await serverTask.WaitAsync(TestConfiguration.PassingTestTimeout);
83+
}
84+
catch (Exception ex)
85+
{
86+
// serverTask will fail.
87+
// We generally don't care but can log exception to help diagnose test failures
88+
_log.WriteLine(ex.ToString());
89+
}
8890
}
8991
}
9092
}

0 commit comments

Comments
 (0)