1
1
// Licensed to the .NET Foundation under one or more agreements.
2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
- using System . IO ;
5
4
using System . Net . Sockets ;
6
5
using System . Net . Test . Common ;
7
6
using System . Security . Authentication ;
8
- using System . Security . Cryptography . X509Certificates ;
9
7
using System . Threading . Tasks ;
10
8
11
9
using Xunit ;
@@ -17,74 +15,78 @@ public class ClientDefaultEncryptionTest
17
15
{
18
16
private readonly ITestOutputHelper _log ;
19
17
20
- public ClientDefaultEncryptionTest ( )
18
+ public ClientDefaultEncryptionTest ( ITestOutputHelper output )
21
19
{
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 ;
33
21
}
34
22
35
23
[ Fact ]
36
24
public async Task ClientDefaultEncryption_ServerRequireEncryption_ConnectWithEncryption ( )
37
25
{
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 )
41
29
{
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 ) )
45
32
{
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
+
47
37
_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" ) ;
51
41
}
52
42
}
53
43
}
54
44
55
45
[ Fact ]
56
46
public async Task ClientDefaultEncryption_ServerAllowNoEncryption_ConnectWithEncryption ( )
57
47
{
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 )
61
51
{
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 ) )
65
54
{
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
+
67
59
_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" ) ;
71
63
}
72
64
}
73
65
}
74
66
75
67
[ Fact ]
76
68
public async Task ClientDefaultEncryption_ServerNoEncryption_NoConnect ( )
77
69
{
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 )
81
73
{
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 ) )
85
76
{
77
+ Task serverTask = server . AuthenticateAsServerAsync ( TestConfiguration . ServerCertificate ) ;
86
78
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
+ }
88
90
}
89
91
}
90
92
}
0 commit comments