PageRenderTime 1600ms CodeModel.GetById 11ms RepoModel.GetById 2ms app.codeStats 0ms

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

http://github.com/mongodb/mongo-csharp-driver
C# | 199 lines | 149 code | 36 blank | 14 comment | 0 complexity | 4fe8ca0ae0bf94b449d0307b1c05d20f MD5 | raw file
Possible License(s): Apache-2.0
  1. /* Copyright 2013-present 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 Xunit;
  21. namespace MongoDB.Driver.Core.Configuration
  22. {
  23. public class SslStreamSettingsTests
  24. {
  25. private static readonly SslStreamSettings __defaults = new SslStreamSettings();
  26. [Fact]
  27. public void constructor_should_initialize_instance()
  28. {
  29. var subject = new SslStreamSettings();
  30. subject.CheckCertificateRevocation.Should().BeTrue();
  31. subject.ClientCertificates.Should().BeEmpty();
  32. subject.ClientCertificateSelectionCallback.Should().BeNull();
  33. subject.EnabledSslProtocols.Should().Be(SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls);
  34. subject.ServerCertificateValidationCallback.Should().BeNull();
  35. }
  36. [Fact]
  37. public void constructor_should_throw_when_clientCertificates_is_null()
  38. {
  39. Action action = () => new SslStreamSettings(clientCertificates: null);
  40. action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("clientCertificates");
  41. }
  42. [Fact]
  43. public void constructor_with_checkCertificateRevocation_should_initialize_instance()
  44. {
  45. var checkCertificateRevocation = !__defaults.CheckCertificateRevocation;
  46. var subject = new SslStreamSettings(checkCertificateRevocation: checkCertificateRevocation);
  47. subject.CheckCertificateRevocation.Should().Be(checkCertificateRevocation);
  48. subject.ClientCertificates.Should().Equal(__defaults.ClientCertificates);
  49. subject.ClientCertificateSelectionCallback.Should().Be(__defaults.ClientCertificateSelectionCallback);
  50. subject.EnabledSslProtocols.Should().Be(__defaults.EnabledSslProtocols);
  51. subject.ServerCertificateValidationCallback.Should().Be(__defaults.ServerCertificateValidationCallback);
  52. }
  53. [Fact]
  54. public void constructor_with_clientCertificates_should_initialize_instance()
  55. {
  56. var clientCertificates = new[] { new X509Certificate() };
  57. var subject = new SslStreamSettings(clientCertificates: clientCertificates);
  58. subject.CheckCertificateRevocation.Should().Be(__defaults.CheckCertificateRevocation);
  59. subject.ClientCertificates.Should().Equal(clientCertificates);
  60. subject.ClientCertificateSelectionCallback.Should().Be(__defaults.ClientCertificateSelectionCallback);
  61. subject.EnabledSslProtocols.Should().Be(__defaults.EnabledSslProtocols);
  62. subject.ServerCertificateValidationCallback.Should().Be(__defaults.ServerCertificateValidationCallback);
  63. }
  64. [Fact]
  65. public void constructor_with_clientCertificateSelectionCallback_should_initialize_instance()
  66. {
  67. LocalCertificateSelectionCallback clientCertificateSelectionCallback = (s, t, l, r, a) => null;
  68. var subject = new SslStreamSettings(clientCertificateSelectionCallback: clientCertificateSelectionCallback);
  69. subject.CheckCertificateRevocation.Should().Be(__defaults.CheckCertificateRevocation);
  70. subject.ClientCertificates.Should().Equal(__defaults.ClientCertificates);
  71. subject.ClientCertificateSelectionCallback.Should().Be(clientCertificateSelectionCallback);
  72. subject.EnabledSslProtocols.Should().Be(__defaults.EnabledSslProtocols);
  73. subject.ServerCertificateValidationCallback.Should().Be(__defaults.ServerCertificateValidationCallback);
  74. }
  75. [Fact]
  76. public void constructor_with_enabledProtocols_should_initialize_instance()
  77. {
  78. var enabledProtocols = SslProtocols.Tls12;
  79. var subject = new SslStreamSettings(enabledProtocols: enabledProtocols);
  80. subject.CheckCertificateRevocation.Should().Be(__defaults.CheckCertificateRevocation);
  81. subject.ClientCertificates.Should().Equal(__defaults.ClientCertificates);
  82. subject.ClientCertificateSelectionCallback.Should().Be(__defaults.ClientCertificateSelectionCallback);
  83. subject.EnabledSslProtocols.Should().Be(enabledProtocols);
  84. subject.ServerCertificateValidationCallback.Should().Be(__defaults.ServerCertificateValidationCallback);
  85. }
  86. [Fact]
  87. public void constructor_with_serverCertificateValidationCallback_should_initialize_instance()
  88. {
  89. RemoteCertificateValidationCallback serverCertificateValidationCallback = (s, ce, ch, e) => false;
  90. var subject = new SslStreamSettings(serverCertificateValidationCallback: serverCertificateValidationCallback);
  91. subject.CheckCertificateRevocation.Should().Be(__defaults.CheckCertificateRevocation);
  92. subject.ClientCertificates.Should().Equal(__defaults.ClientCertificates);
  93. subject.ClientCertificateSelectionCallback.Should().Be(__defaults.ClientCertificateSelectionCallback);
  94. subject.EnabledSslProtocols.Should().Be(__defaults.EnabledSslProtocols);
  95. subject.ServerCertificateValidationCallback.Should().Be(serverCertificateValidationCallback);
  96. }
  97. [Fact]
  98. public void With_checkCertificateRevocation_should_return_expected_result()
  99. {
  100. var oldCheckCertificateRevocation = false;
  101. var newCheckCertificateRevocation = true;
  102. var subject = new SslStreamSettings(checkCertificateRevocation: oldCheckCertificateRevocation);
  103. var result = subject.With(checkCertificateRevocation: newCheckCertificateRevocation);
  104. result.CheckCertificateRevocation.Should().Be(newCheckCertificateRevocation);
  105. result.ClientCertificates.Should().Equal(subject.ClientCertificates);
  106. result.ClientCertificateSelectionCallback.Should().Be(subject.ClientCertificateSelectionCallback);
  107. result.EnabledSslProtocols.Should().Be(subject.EnabledSslProtocols);
  108. result.ServerCertificateValidationCallback.Should().Be(subject.ServerCertificateValidationCallback);
  109. }
  110. [Fact]
  111. public void With_clientCertificates_should_return_expected_result()
  112. {
  113. var oldClientCertificates = new[] { new X509Certificate() };
  114. var newClientCertificates = new[] { new X509Certificate() };
  115. var subject = new SslStreamSettings(clientCertificates: oldClientCertificates);
  116. var result = subject.With(clientCertificates: newClientCertificates);
  117. result.CheckCertificateRevocation.Should().Be(subject.CheckCertificateRevocation);
  118. result.ClientCertificates.Should().Equal(newClientCertificates);
  119. result.ClientCertificateSelectionCallback.Should().Be(subject.ClientCertificateSelectionCallback);
  120. result.EnabledSslProtocols.Should().Be(subject.EnabledSslProtocols);
  121. result.ServerCertificateValidationCallback.Should().Be(subject.ServerCertificateValidationCallback);
  122. }
  123. [Fact]
  124. public void With_clientCertificateSelectionCallback_should_return_expected_result()
  125. {
  126. LocalCertificateSelectionCallback oldClientCertificateSelectionCallback = (s, t, l, r, a) => null;
  127. LocalCertificateSelectionCallback newClientCertificateSelectionCallback = (s, t, l, r, a) => null;
  128. var subject = new SslStreamSettings(clientCertificateSelectionCallback: oldClientCertificateSelectionCallback);
  129. var result = subject.With(clientCertificateSelectionCallback: newClientCertificateSelectionCallback);
  130. result.CheckCertificateRevocation.Should().Be(subject.CheckCertificateRevocation);
  131. result.ClientCertificates.Should().Equal(subject.ClientCertificates);
  132. result.ClientCertificateSelectionCallback.Should().Be(newClientCertificateSelectionCallback);
  133. result.EnabledSslProtocols.Should().Be(subject.EnabledSslProtocols);
  134. result.ServerCertificateValidationCallback.Should().Be(subject.ServerCertificateValidationCallback);
  135. }
  136. [Fact]
  137. public void With_enabledProtocols_should_return_expected_result()
  138. {
  139. var oldEnabledProtocols = SslProtocols.Tls;
  140. var newEnabledProtocols = SslProtocols.Tls12;
  141. var subject = new SslStreamSettings(enabledProtocols: oldEnabledProtocols);
  142. var result = subject.With(enabledProtocols: newEnabledProtocols);
  143. result.CheckCertificateRevocation.Should().Be(subject.CheckCertificateRevocation);
  144. result.ClientCertificates.Should().Equal(subject.ClientCertificates);
  145. result.ClientCertificateSelectionCallback.Should().Be(subject.ClientCertificateSelectionCallback);
  146. result.EnabledSslProtocols.Should().Be(newEnabledProtocols);
  147. result.ServerCertificateValidationCallback.Should().Be(subject.ServerCertificateValidationCallback);
  148. }
  149. [Fact]
  150. public void With_serverCertificateValidationCallback_should_return_expected_result()
  151. {
  152. RemoteCertificateValidationCallback oldServerCertificateValidationCallback = (s, ce, ch, e) => false;
  153. RemoteCertificateValidationCallback newServerCertificateValidationCallback = (s, ce, ch, e) => false;
  154. var subject = new SslStreamSettings(serverCertificateValidationCallback: oldServerCertificateValidationCallback);
  155. var result = subject.With(serverCertificateValidationCallback: newServerCertificateValidationCallback);
  156. result.CheckCertificateRevocation.Should().Be(subject.CheckCertificateRevocation);
  157. result.ClientCertificates.Should().Equal(subject.ClientCertificates);
  158. result.ClientCertificateSelectionCallback.Should().Be(subject.ClientCertificateSelectionCallback);
  159. result.EnabledSslProtocols.Should().Be(subject.EnabledSslProtocols);
  160. result.ServerCertificateValidationCallback.Should().Be(newServerCertificateValidationCallback);
  161. }
  162. }
  163. }