30
30
//---------------------------------------------------------------------------
31
31
32
32
using System ;
33
+ using System . IO ;
33
34
using System . Net . Security ;
35
+ using System . Reflection ;
34
36
using System . Security . Authentication ;
35
37
36
38
using Xunit ;
39
+ using Xunit . Abstractions ;
37
40
38
41
namespace RabbitMQ . Client . Unit
39
42
{
40
-
43
+ [ Collection ( "IntegrationFixture" ) ]
41
44
public class TestSsl
42
45
{
43
- private void SendReceive ( ConnectionFactory cf )
44
- {
45
- using ( IConnection conn = cf . CreateConnection ( ) )
46
- {
47
- IModel ch = conn . CreateModel ( ) ;
46
+ private readonly ITestOutputHelper _output ;
47
+ private readonly string _testDisplayName ;
48
+ private readonly string _sslDir ;
49
+ private readonly string _certPassphrase ;
50
+ private readonly bool _sslConfigured ;
48
51
49
- ch . ExchangeDeclare ( "Exchange_TestSslEndPoint" , ExchangeType . Direct ) ;
50
- string qName = ch . QueueDeclare ( ) ;
51
- ch . QueueBind ( qName , "Exchange_TestSslEndPoint" , "Key_TestSslEndpoint" , null ) ;
52
-
53
- string message = "Hello C# SSL Client World" ;
54
- byte [ ] msgBytes = System . Text . Encoding . UTF8 . GetBytes ( message ) ;
55
- ch . BasicPublish ( "Exchange_TestSslEndPoint" , "Key_TestSslEndpoint" , msgBytes ) ;
52
+ public TestSsl ( ITestOutputHelper output )
53
+ {
54
+ _output = output ;
55
+ var type = _output . GetType ( ) ;
56
+ var testMember = type . GetField ( "test" , BindingFlags . Instance | BindingFlags . NonPublic ) ;
57
+ var test = ( ITest ) testMember . GetValue ( output ) ;
58
+ _testDisplayName = test . DisplayName ;
56
59
57
- bool autoAck = false ;
58
- BasicGetResult result = ch . BasicGet ( qName , autoAck ) ;
59
- byte [ ] body = result . Body . ToArray ( ) ;
60
- string resultMessage = System . Text . Encoding . UTF8 . GetString ( body ) ;
60
+ _sslDir = IntegrationFixture . CertificatesDirectory ( ) ;
61
+ _certPassphrase = Environment . GetEnvironmentVariable ( "PASSWORD" ) ;
61
62
62
- Assert . Equal ( message , resultMessage ) ;
63
- }
63
+ _sslConfigured = Directory . Exists ( _sslDir ) &&
64
+ ( false == string . IsNullOrEmpty ( _certPassphrase ) ) ;
64
65
}
65
66
66
- [ Fact ]
67
+ [ SkippableFact ]
67
68
public void TestServerVerifiedIgnoringNameMismatch ( )
68
69
{
69
- string sslDir = IntegrationFixture . CertificatesDirectory ( ) ;
70
- if ( null == sslDir )
71
- {
72
- Console . WriteLine ( "SSL_CERTS_DIR is not configured, skipping test" ) ;
73
- return ;
74
- }
70
+ Skip . IfNot ( _sslConfigured , "SSL_CERTS_DIR and/or PASSWORD are not configured, skipping test" ) ;
75
71
76
72
ConnectionFactory cf = new ConnectionFactory { Port = 5671 } ;
77
73
cf . Ssl . ServerName = "*" ;
@@ -80,51 +76,36 @@ public void TestServerVerifiedIgnoringNameMismatch()
80
76
SendReceive ( cf ) ;
81
77
}
82
78
83
- [ Fact ]
79
+ [ SkippableFact ]
84
80
public void TestServerVerified ( )
85
81
{
86
- string sslDir = IntegrationFixture . CertificatesDirectory ( ) ;
87
- if ( null == sslDir )
88
- {
89
- Console . WriteLine ( "SSL_CERTS_DIR is not configured, skipping test" ) ;
90
- return ;
91
- }
82
+ Skip . IfNot ( _sslConfigured , "SSL_CERTS_DIR and/or PASSWORD are not configured, skipping test" ) ;
92
83
93
84
ConnectionFactory cf = new ConnectionFactory { Port = 5671 } ;
94
85
cf . Ssl . ServerName = System . Net . Dns . GetHostName ( ) ;
95
86
cf . Ssl . Enabled = true ;
96
87
SendReceive ( cf ) ;
97
88
}
98
89
99
- [ Fact ]
90
+ [ SkippableFact ]
100
91
public void TestClientAndServerVerified ( )
101
92
{
102
- string sslDir = IntegrationFixture . CertificatesDirectory ( ) ;
103
- if ( null == sslDir )
104
- {
105
- Console . WriteLine ( "SSL_CERTS_DIR is not configured, skipping test" ) ;
106
- return ;
107
- }
93
+ Skip . IfNot ( _sslConfigured , "SSL_CERTS_DIR and/or PASSWORD are not configured, skipping test" ) ;
108
94
95
+ string hostName = System . Net . Dns . GetHostName ( ) ;
109
96
ConnectionFactory cf = new ConnectionFactory { Port = 5671 } ;
110
- cf . Ssl . ServerName = System . Net . Dns . GetHostName ( ) ;
111
- Assert . NotNull ( sslDir ) ;
112
- cf . Ssl . CertPath = $ "{ sslDir } /client_key.p12";
113
- cf . Ssl . CertPassphrase = Environment . GetEnvironmentVariable ( "PASSWORD" ) ;
97
+ cf . Ssl . ServerName = hostName ;
98
+ cf . Ssl . CertPath = $ "{ _sslDir } /client_{ hostName } _key.p12";
99
+ cf . Ssl . CertPassphrase = _certPassphrase ;
114
100
cf . Ssl . Enabled = true ;
115
101
SendReceive ( cf ) ;
116
102
}
117
103
118
104
// rabbitmq/rabbitmq-dotnet-client#46, also #44 and #45
119
- [ Fact ]
105
+ [ SkippableFact ]
120
106
public void TestNoClientCertificate ( )
121
107
{
122
- string sslDir = IntegrationFixture . CertificatesDirectory ( ) ;
123
- if ( null == sslDir )
124
- {
125
- Console . WriteLine ( "SSL_CERTS_DIR is not configured, skipping test" ) ;
126
- return ;
127
- }
108
+ Skip . IfNot ( _sslConfigured , "SSL_CERTS_DIR and/or PASSWORD are not configured, skipping test" ) ;
128
109
129
110
ConnectionFactory cf = new ConnectionFactory
130
111
{
@@ -143,5 +124,29 @@ public void TestNoClientCertificate()
143
124
144
125
SendReceive ( cf ) ;
145
126
}
127
+
128
+ private void SendReceive ( ConnectionFactory cf )
129
+ {
130
+ using ( IConnection conn = cf . CreateConnection ( $ "{ _testDisplayName } :{ Guid . NewGuid ( ) } ") )
131
+ {
132
+ using ( IModel ch = conn . CreateModel ( ) )
133
+ {
134
+ ch . ExchangeDeclare ( "Exchange_TestSslEndPoint" , ExchangeType . Direct ) ;
135
+ string qName = ch . QueueDeclare ( ) ;
136
+ ch . QueueBind ( qName , "Exchange_TestSslEndPoint" , "Key_TestSslEndpoint" , null ) ;
137
+
138
+ string message = "Hello C# SSL Client World" ;
139
+ byte [ ] msgBytes = System . Text . Encoding . UTF8 . GetBytes ( message ) ;
140
+ ch . BasicPublish ( "Exchange_TestSslEndPoint" , "Key_TestSslEndpoint" , msgBytes ) ;
141
+
142
+ bool autoAck = false ;
143
+ BasicGetResult result = ch . BasicGet ( qName , autoAck ) ;
144
+ byte [ ] body = result . Body . ToArray ( ) ;
145
+ string resultMessage = System . Text . Encoding . UTF8 . GetString ( body ) ;
146
+
147
+ Assert . Equal ( message , resultMessage ) ;
148
+ }
149
+ }
150
+ }
146
151
}
147
152
}
0 commit comments