|
56 | 56 | //---------------------------------------------------------------------------
|
57 | 57 | using NUnit.Framework;
|
58 | 58 | using System;
|
| 59 | +using System.Net.Security; |
59 | 60 | using RabbitMQ.Client;
|
60 | 61 |
|
61 | 62 | [TestFixture]
|
62 |
| -public class TestSslEndpointUnverified { |
| 63 | +public class TestSsl { |
63 | 64 |
|
64 |
| - public void SendReceive(IConnection conn) { |
65 |
| - string message = "Hello C# SSL Client World"; |
| 65 | + public void SendReceive(ConnectionFactory cf) { |
| 66 | + IProtocol proto = Protocols.DefaultProtocol; |
| 67 | + using (IConnection conn = cf.CreateConnection(proto, "localhost", 5671)) { |
| 68 | + IModel ch = conn.CreateModel(); |
| 69 | + |
| 70 | + ch.ExchangeDeclare("Exchange_TestSslEndPoint", ExchangeType.Direct); |
| 71 | + ch.QueueDeclare("Queue_TestSslEndpoint"); |
| 72 | + ch.QueueBind("Queue_TestSslEndpoint", "Exchange_TestSslEndPoint", "Key_TestSslEndpoint", false, null); |
| 73 | + |
| 74 | + string message = "Hello C# SSL Client World"; |
| 75 | + byte[] msgBytes = System.Text.Encoding.UTF8.GetBytes(message); |
| 76 | + ch.BasicPublish("Exchange_TestSslEndPoint", "Key_TestSslEndpoint", null, msgBytes); |
66 | 77 |
|
67 |
| - IModel ch = conn.CreateModel(); |
| 78 | + bool noAck = false; |
| 79 | + BasicGetResult result = ch.BasicGet("Queue_TestSslEndpoint", noAck); |
| 80 | + byte[] body = result.Body; |
| 81 | + string resultMessage = System.Text.Encoding.UTF8.GetString(body); |
68 | 82 |
|
69 |
| - ch.ExchangeDeclare("Exchange_TestSslEndPoint", ExchangeType.Direct); |
70 |
| - ch.QueueDeclare("Queue_TestSslEndpoint"); |
71 |
| - ch.QueueBind("Queue_TestSslEndpoint", "Exchange_TestSslEndPoint", "Key_TestSslEndpoint", false, null); |
72 |
| - |
73 |
| - byte[] msgBytes = System.Text.Encoding.UTF8.GetBytes(message); |
74 |
| - ch.BasicPublish("Exchange_TestSslEndPoint", "Key_TestSslEndpoint", null, msgBytes); |
75 |
| - |
76 |
| - bool noAck = false; |
| 83 | + Assert.AreEqual(message, resultMessage); |
| 84 | + } |
| 85 | + } |
77 | 86 |
|
78 |
| - BasicGetResult result = ch.BasicGet("Queue_TestSslEndpoint", noAck); |
79 |
| - byte[] body = result.Body; |
| 87 | + [Test] |
| 88 | + public void TestServerVerifiedIgnoringNameMismatch() { |
| 89 | + string sslDir = Environment.GetEnvironmentVariable("SSL_CERTS_DIR"); |
| 90 | + if (null == sslDir) return; |
80 | 91 |
|
81 |
| - string resultMessage = System.Text.Encoding.UTF8.GetString(body); |
| 92 | + ConnectionFactory cf = new ConnectionFactory(); |
| 93 | + cf.Parameters.Ssl.ServerName = "*"; |
| 94 | + cf.Parameters.Ssl.AcceptablePolicyErrors = SslPolicyErrors.RemoteCertificateNameMismatch; |
| 95 | + cf.Parameters.Ssl.Enabled = true; |
| 96 | + SendReceive(cf); |
| 97 | + } |
82 | 98 |
|
83 |
| - ch.Close(200, "Closing the channel"); |
84 |
| - conn.Close(); |
| 99 | + [Test] |
| 100 | + public void TestServerVerified() { |
| 101 | + string sslDir = Environment.GetEnvironmentVariable("SSL_CERTS_DIR"); |
| 102 | + if (null == sslDir) return; |
85 | 103 |
|
86 |
| - Assert.AreEqual(message, resultMessage); |
| 104 | + ConnectionFactory cf = new ConnectionFactory(); |
| 105 | + cf.Parameters.Ssl.ServerName = System.Net.Dns.GetHostName(); |
| 106 | + cf.Parameters.Ssl.Enabled = true; |
| 107 | + SendReceive(cf); |
87 | 108 | }
|
88 | 109 |
|
89 |
| - |
90 | 110 | [Test]
|
91 |
| - public virtual void TestHostWithPort() { |
| 111 | + public void TestClientAndServerVerified() { |
92 | 112 | string sslDir = Environment.GetEnvironmentVariable("SSL_CERTS_DIR");
|
93 |
| - if (null == sslDir) { |
94 |
| - return; |
95 |
| - } else { |
96 |
| - ConnectionFactory cf = new ConnectionFactory(); |
97 |
| - |
98 |
| - cf.Parameters.Ssl.ServerName = System.Net.Dns.GetHostName(); |
99 |
| - cf.Parameters.Ssl.Enabled = true; |
| 113 | + if (null == sslDir) return; |
100 | 114 |
|
101 |
| - IProtocol proto = Protocols.DefaultProtocol; |
102 |
| - IConnection conn = cf.CreateConnection(proto, "localhost", 5671); |
103 |
| - SendReceive(conn); |
104 |
| - } |
| 115 | + ConnectionFactory cf = new ConnectionFactory(); |
| 116 | + cf.Parameters.Ssl.ServerName = System.Net.Dns.GetHostName(); |
| 117 | + Assert.IsNotNull(sslDir); |
| 118 | + cf.Parameters.Ssl.CertPath = sslDir + "/client/keycert.p12"; |
| 119 | + string p12Password = Environment.GetEnvironmentVariable("PASSWORD"); |
| 120 | + Assert.IsNotNull(p12Password, "missing PASSWORD env var"); |
| 121 | + cf.Parameters.Ssl.CertPassphrase = p12Password; |
| 122 | + cf.Parameters.Ssl.Enabled = true; |
| 123 | + SendReceive(cf); |
105 | 124 | }
|
106 | 125 | }
|
0 commit comments