PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/src/MongoDB.Driver.Core.Tests/Core/Configuration/SslStreamSettingsTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 200 lines | 150 code | 36 blank | 14 comment | 0 complexity | f41434e06ae6ea6f555143481b6ae07b MD5 | raw file
Possible License(s): Apache-2.0
  1. /* Copyright 2013-2014 MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. using System.Net.Security;
  17. using System.Security.Authentication;
  18. using System.Security.Cryptography.X509Certificates;
  19. using FluentAssertions;
  20. using NUnit.Framework;
  21. namespace MongoDB.Driver.Core.Configuration
  22. {
  23. [TestFixture]
  24. public class SslStreamSettingsTests
  25. {
  26. private static readonly SslStreamSettings __defaults = new SslStreamSettings();
  27. [Test]
  28. public void constructor_should_initialize_instance()
  29. {
  30. var subject = new SslStreamSettings();
  31. subject.CheckCertificateRevocation.Should().BeTrue();
  32. subject.ClientCertificates.Should().BeEmpty();
  33. subject.ClientCertificateSelectionCallback.Should().BeNull();
  34. subject.EnabledSslProtocols.Should().Be(SslProtocols.Default);
  35. subject.ServerCertificateValidationCallback.Should().BeNull();
  36. }
  37. [Test]
  38. public void constructor_should_throw_when_clientCertificates_is_null()
  39. {
  40. Action action = () => new SslStreamSettings(clientCertificates: null);
  41. action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("clientCertificates");
  42. }
  43. [Test]
  44. public void constructor_with_checkCertificateRevocation_should_initialize_instance()
  45. {
  46. var checkCertificateRevocation = !__defaults.CheckCertificateRevocation;
  47. var subject = new SslStreamSettings(checkCertificateRevocation: checkCertificateRevocation);
  48. subject.CheckCertificateRevocation.Should().Be(checkCertificateRevocation);
  49. subject.ClientCertificates.Should().Equal(__defaults.ClientCertificates);
  50. subject.ClientCertificateSelectionCallback.Should().Be(__defaults.ClientCertificateSelectionCallback);
  51. subject.EnabledSslProtocols.Should().Be(__defaults.EnabledSslProtocols);
  52. subject.ServerCertificateValidationCallback.Should().Be(__defaults.ServerCertificateValidationCallback);
  53. }
  54. [Test]
  55. public void constructor_with_clientCertificates_should_initialize_instance()
  56. {
  57. var clientCertificates = new[] { new X509Certificate() };
  58. var subject = new SslStreamSettings(clientCertificates: clientCertificates);
  59. subject.CheckCertificateRevocation.Should().Be(__defaults.CheckCertificateRevocation);
  60. subject.ClientCertificates.Should().Equal(clientCertificates);
  61. subject.ClientCertificateSelectionCallback.Should().Be(__defaults.ClientCertificateSelectionCallback);
  62. subject.EnabledSslProtocols.Should().Be(__defaults.EnabledSslProtocols);
  63. subject.ServerCertificateValidationCallback.Should().Be(__defaults.ServerCertificateValidationCallback);
  64. }
  65. [Test]
  66. public void constructor_with_clientCertificateSelectionCallback_should_initialize_instance()
  67. {
  68. LocalCertificateSelectionCallback clientCertificateSelectionCallback = (s, t, l, r, a) => null;
  69. var subject = new SslStreamSettings(clientCertificateSelectionCallback: clientCertificateSelectionCallback);
  70. subject.CheckCertificateRevocation.Should().Be(__defaults.CheckCertificateRevocation);
  71. subject.ClientCertificates.Should().Equal(__defaults.ClientCertificates);
  72. subject.ClientCertificateSelectionCallback.Should().Be(clientCertificateSelectionCallback);
  73. subject.EnabledSslProtocols.Should().Be(__defaults.EnabledSslProtocols);
  74. subject.ServerCertificateValidationCallback.Should().Be(__defaults.ServerCertificateValidationCallback);
  75. }
  76. [Test]
  77. public void constructor_with_enabledProtocols_should_initialize_instance()
  78. {
  79. var enabledProtocols = SslProtocols.Tls12;
  80. var subject = new SslStreamSettings(enabledProtocols: enabledProtocols);
  81. subject.CheckCertificateRevocation.Should().Be(__defaults.CheckCertificateRevocation);
  82. subject.ClientCertificates.Should().Equal(__defaults.ClientCertificates);
  83. subject.ClientCertificateSelectionCallback.Should().Be(__defaults.ClientCertificateSelectionCallback);
  84. subject.EnabledSslProtocols.Should().Be(enabledProtocols);
  85. subject.ServerCertificateValidationCallback.Should().Be(__defaults.ServerCertificateValidationCallback);
  86. }
  87. [Test]
  88. public void constructor_with_serverCertificateValidationCallback_should_initialize_instance()
  89. {
  90. RemoteCertificateValidationCallback serverCertificateValidationCallback = (s, ce, ch, e) => false;
  91. var subject = new SslStreamSettings(serverCertificateValidationCallback: serverCertificateValidationCallback);
  92. subject.CheckCertificateRevocation.Should().Be(__defaults.CheckCertificateRevocation);
  93. subject.ClientCertificates.Should().Equal(__defaults.ClientCertificates);
  94. subject.ClientCertificateSelectionCallback.Should().Be(__defaults.ClientCertificateSelectionCallback);
  95. subject.EnabledSslProtocols.Should().Be(__defaults.EnabledSslProtocols);
  96. subject.ServerCertificateValidationCallback.Should().Be(serverCertificateValidationCallback);
  97. }
  98. [Test]
  99. public void With_checkCertificateRevocation_should_return_expected_result()
  100. {
  101. var oldCheckCertificateRevocation = false;
  102. var newCheckCertificateRevocation = true;
  103. var subject = new SslStreamSettings(checkCertificateRevocation: oldCheckCertificateRevocation);
  104. var result = subject.With(checkCertificateRevocation: newCheckCertificateRevocation);
  105. result.CheckCertificateRevocation.Should().Be(newCheckCertificateRevocation);
  106. result.ClientCertificates.Should().Equal(subject.ClientCertificates);
  107. result.ClientCertificateSelectionCallback.Should().Be(subject.ClientCertificateSelectionCallback);
  108. result.EnabledSslProtocols.Should().Be(subject.EnabledSslProtocols);
  109. result.ServerCertificateValidationCallback.Should().Be(subject.ServerCertificateValidationCallback);
  110. }
  111. [Test]
  112. public void With_clientCertificates_should_return_expected_result()
  113. {
  114. var oldClientCertificates = new[] { new X509Certificate() };
  115. var newClientCertificates = new[] { new X509Certificate() };
  116. var subject = new SslStreamSettings(clientCertificates: oldClientCertificates);
  117. var result = subject.With(clientCertificates: newClientCertificates);
  118. result.CheckCertificateRevocation.Should().Be(subject.CheckCertificateRevocation);
  119. result.ClientCertificates.Should().Equal(newClientCertificates);
  120. result.ClientCertificateSelectionCallback.Should().Be(subject.ClientCertificateSelectionCallback);
  121. result.EnabledSslProtocols.Should().Be(subject.EnabledSslProtocols);
  122. result.ServerCertificateValidationCallback.Should().Be(subject.ServerCertificateValidationCallback);
  123. }
  124. [Test]
  125. public void With_clientCertificateSelectionCallback_should_return_expected_result()
  126. {
  127. LocalCertificateSelectionCallback oldClientCertificateSelectionCallback = (s, t, l, r, a) => null;
  128. LocalCertificateSelectionCallback newClientCertificateSelectionCallback = (s, t, l, r, a) => null;
  129. var subject = new SslStreamSettings(clientCertificateSelectionCallback: oldClientCertificateSelectionCallback);
  130. var result = subject.With(clientCertificateSelectionCallback: newClientCertificateSelectionCallback);
  131. result.CheckCertificateRevocation.Should().Be(subject.CheckCertificateRevocation);
  132. result.ClientCertificates.Should().Equal(subject.ClientCertificates);
  133. result.ClientCertificateSelectionCallback.Should().Be(newClientCertificateSelectionCallback);
  134. result.EnabledSslProtocols.Should().Be(subject.EnabledSslProtocols);
  135. result.ServerCertificateValidationCallback.Should().Be(subject.ServerCertificateValidationCallback);
  136. }
  137. [Test]
  138. public void With_enabledProtocols_should_return_expected_result()
  139. {
  140. var oldEnabledProtocols = SslProtocols.Default;
  141. var newEnabledProtocols = SslProtocols.Tls12;
  142. var subject = new SslStreamSettings(enabledProtocols: oldEnabledProtocols);
  143. var result = subject.With(enabledProtocols: newEnabledProtocols);
  144. result.CheckCertificateRevocation.Should().Be(subject.CheckCertificateRevocation);
  145. result.ClientCertificates.Should().Equal(subject.ClientCertificates);
  146. result.ClientCertificateSelectionCallback.Should().Be(subject.ClientCertificateSelectionCallback);
  147. result.EnabledSslProtocols.Should().Be(newEnabledProtocols);
  148. result.ServerCertificateValidationCallback.Should().Be(subject.ServerCertificateValidationCallback);
  149. }
  150. [Test]
  151. public void With_serverCertificateValidationCallback_should_return_expected_result()
  152. {
  153. RemoteCertificateValidationCallback oldServerCertificateValidationCallback = (s, ce, ch, e) => false;
  154. RemoteCertificateValidationCallback newServerCertificateValidationCallback = (s, ce, ch, e) => false;
  155. var subject = new SslStreamSettings(serverCertificateValidationCallback: oldServerCertificateValidationCallback);
  156. var result = subject.With(serverCertificateValidationCallback: newServerCertificateValidationCallback);
  157. result.CheckCertificateRevocation.Should().Be(subject.CheckCertificateRevocation);
  158. result.ClientCertificates.Should().Equal(subject.ClientCertificates);
  159. result.ClientCertificateSelectionCallback.Should().Be(subject.ClientCertificateSelectionCallback);
  160. result.EnabledSslProtocols.Should().Be(subject.EnabledSslProtocols);
  161. result.ServerCertificateValidationCallback.Should().Be(newServerCertificateValidationCallback);
  162. }
  163. }
  164. }