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