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 NUnit . Framework ;
@@ -40,37 +42,25 @@ namespace RabbitMQ.Client.Unit
40
42
[ TestFixture ]
41
43
public class TestSsl
42
44
{
43
- public void SendReceive ( ConnectionFactory cf )
44
- {
45
- using ( IConnection conn = cf . CreateConnection ( ) )
46
- {
47
- IModel ch = conn . CreateModel ( ) ;
48
-
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" , null , msgBytes ) ;
56
-
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 ) ;
45
+ private readonly string _testDisplayName ;
46
+ private readonly string _sslDir ;
47
+ private readonly string _certPassphrase ;
48
+ private readonly bool _sslConfigured ;
61
49
62
- Assert . AreEqual ( message , resultMessage ) ;
63
- }
50
+ public TestSsl ( )
51
+ {
52
+ _sslDir = IntegrationFixture . CertificatesDirectory ( ) ;
53
+ _certPassphrase = Environment . GetEnvironmentVariable ( "PASSWORD" ) ;
54
+ _sslConfigured = Directory . Exists ( _sslDir ) &&
55
+ ( false == string . IsNullOrEmpty ( _certPassphrase ) ) ;
64
56
}
65
57
66
58
[ Test ]
67
59
public void TestServerVerifiedIgnoringNameMismatch ( )
68
60
{
69
- string sslDir = IntegrationFixture . CertificatesDirectory ( ) ;
70
- if ( null == sslDir )
61
+ if ( false == _sslConfigured )
71
62
{
72
- Console . WriteLine ( "SSL_CERTS_DIR is not configured, skipping test" ) ;
73
- return ;
63
+ Assert . Ignore ( "SSL_CERTS_DIR and/or PASSWORD are not configured, skipping test" ) ;
74
64
}
75
65
76
66
ConnectionFactory cf = new ConnectionFactory { Port = 5671 } ;
@@ -83,11 +73,9 @@ public void TestServerVerifiedIgnoringNameMismatch()
83
73
[ Test ]
84
74
public void TestServerVerified ( )
85
75
{
86
- string sslDir = IntegrationFixture . CertificatesDirectory ( ) ;
87
- if ( null == sslDir )
76
+ if ( false == _sslConfigured )
88
77
{
89
- Console . WriteLine ( "SSL_CERTS_DIR is not configured, skipping test" ) ;
90
- return ;
78
+ Assert . Ignore ( "SSL_CERTS_DIR and/or PASSWORD are not configured, skipping test" ) ;
91
79
}
92
80
93
81
ConnectionFactory cf = new ConnectionFactory { Port = 5671 } ;
@@ -99,18 +87,16 @@ public void TestServerVerified()
99
87
[ Test ]
100
88
public void TestClientAndServerVerified ( )
101
89
{
102
- string sslDir = IntegrationFixture . CertificatesDirectory ( ) ;
103
- if ( null == sslDir )
90
+ if ( false == _sslConfigured )
104
91
{
105
- Console . WriteLine ( "SSL_CERTS_DIR is not configured, skipping test" ) ;
106
- return ;
92
+ Assert . Ignore ( "SSL_CERTS_DIR and/or PASSWORD are not configured, skipping test" ) ;
107
93
}
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 . IsNotNull ( 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
}
@@ -119,11 +105,9 @@ public void TestClientAndServerVerified()
119
105
[ Test ]
120
106
public void TestNoClientCertificate ( )
121
107
{
122
- string sslDir = IntegrationFixture . CertificatesDirectory ( ) ;
123
- if ( null == sslDir )
108
+ if ( false == _sslConfigured )
124
109
{
125
- Console . WriteLine ( "SSL_CERTS_DIR is not configured, skipping test" ) ;
126
- return ;
110
+ Assert . Ignore ( "SSL_CERTS_DIR and/or PASSWORD are not configured, skipping test" ) ;
127
111
}
128
112
129
113
ConnectionFactory cf = new ConnectionFactory
@@ -143,5 +127,29 @@ public void TestNoClientCertificate()
143
127
144
128
SendReceive ( cf ) ;
145
129
}
130
+
131
+ private void SendReceive ( ConnectionFactory cf )
132
+ {
133
+ using ( IConnection conn = cf . CreateConnection ( $ "{ _testDisplayName } :{ Guid . NewGuid ( ) } ") )
134
+ {
135
+ using ( IModel ch = conn . CreateModel ( ) )
136
+ {
137
+ ch . ExchangeDeclare ( "Exchange_TestSslEndPoint" , ExchangeType . Direct ) ;
138
+ string qName = ch . QueueDeclare ( ) ;
139
+ ch . QueueBind ( qName , "Exchange_TestSslEndPoint" , "Key_TestSslEndpoint" , null ) ;
140
+
141
+ string message = "Hello C# SSL Client World" ;
142
+ byte [ ] msgBytes = System . Text . Encoding . UTF8 . GetBytes ( message ) ;
143
+ ch . BasicPublish ( "Exchange_TestSslEndPoint" , "Key_TestSslEndpoint" , true , null , msgBytes ) ;
144
+
145
+ bool autoAck = false ;
146
+ BasicGetResult result = ch . BasicGet ( qName , autoAck ) ;
147
+ byte [ ] body = result . Body . ToArray ( ) ;
148
+ string resultMessage = System . Text . Encoding . UTF8 . GetString ( body ) ;
149
+
150
+ Assert . AreEqual ( message , resultMessage ) ;
151
+ }
152
+ }
153
+ }
146
154
}
147
155
}
0 commit comments