PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/MongoDB.Driver.Core.Tests/Core/Authentication/MongoDBX509AuthenticatorTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 175 lines | 136 code | 24 blank | 15 comment | 4 complexity | b85da119ffa6d22eabcd3068770df261 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;
  17. using System.Threading;
  18. using FluentAssertions;
  19. using MongoDB.Bson;
  20. using MongoDB.Driver.Core.Authentication;
  21. using MongoDB.Driver.Core.Clusters;
  22. using MongoDB.Driver.Core.Servers;
  23. using MongoDB.Driver.Core.Helpers;
  24. using Xunit;
  25. using MongoDB.Driver.Core.Connections;
  26. using System.Threading.Tasks;
  27. using MongoDB.Bson.TestHelpers.XunitExtensions;
  28. using MongoDB.Driver.Core.Misc;
  29. namespace MongoDB.Driver.Core.Authentication
  30. {
  31. public class MongoDBX509AuthenticatorTests
  32. {
  33. private static readonly ClusterId __clusterId = new ClusterId();
  34. private static readonly ServerId __serverId = new ServerId(__clusterId, new DnsEndPoint("localhost", 27017));
  35. private static readonly ConnectionDescription __description = new ConnectionDescription(
  36. new ConnectionId(__serverId),
  37. new IsMasterResult(new BsonDocument("ok", 1).Add("ismaster", 1)),
  38. new BuildInfoResult(new BsonDocument("version", "2.6.0")));
  39. [Theory]
  40. [InlineData("")]
  41. public void Constructor_should_throw_an_ArgumentException_when_username_is_empty(string username)
  42. {
  43. Action act = () => new MongoDBX509Authenticator(username);
  44. act.ShouldThrow<ArgumentException>();
  45. }
  46. [Theory]
  47. [ParameterAttributeData]
  48. public void Authenticate_should_throw_an_AuthenticationException_when_authentication_fails(
  49. [Values(false, true)]
  50. bool async)
  51. {
  52. var subject = new MongoDBX509Authenticator("CN=client,OU=kerneluser,O=10Gen,L=New York City,ST=New York,C=US");
  53. var reply = MessageHelper.BuildNoDocumentsReturnedReply<RawBsonDocument>();
  54. var connection = new MockConnection(__serverId);
  55. connection.Description = CreateConnectionDescription(new SemanticVersion(3, 2, 0));
  56. connection.EnqueueReplyMessage(reply);
  57. Action act;
  58. if (async)
  59. {
  60. act = () => subject.AuthenticateAsync(connection, __description, CancellationToken.None).GetAwaiter().GetResult();
  61. }
  62. else
  63. {
  64. act = () => subject.Authenticate(connection, __description, CancellationToken.None);
  65. }
  66. act.ShouldThrow<MongoAuthenticationException>();
  67. }
  68. [Theory]
  69. [ParameterAttributeData]
  70. public void Authenticate_should_not_throw_when_authentication_succeeds(
  71. [Values(false, true)]
  72. bool async)
  73. {
  74. var subject = new MongoDBX509Authenticator("CN=client,OU=kerneluser,O=10Gen,L=New York City,ST=New York,C=US");
  75. var reply = MessageHelper.BuildReply<RawBsonDocument>(
  76. RawBsonDocumentHelper.FromJson("{ok: 1}"));
  77. var connection = new MockConnection(__serverId);
  78. connection.Description = CreateConnectionDescription(new SemanticVersion(3, 2, 0));
  79. connection.EnqueueReplyMessage(reply);
  80. Action act;
  81. if (async)
  82. {
  83. act = () => subject.AuthenticateAsync(connection, __description, CancellationToken.None).GetAwaiter().GetResult();
  84. }
  85. else
  86. {
  87. act = () => subject.Authenticate(connection, __description, CancellationToken.None);
  88. }
  89. act.ShouldNotThrow();
  90. }
  91. [Theory]
  92. [ParameterAttributeData]
  93. public void Authenticate_should_throw_when_username_is_null_and_server_does_not_support_null_username(
  94. [Values(false, true)]
  95. bool async)
  96. {
  97. var subject = new MongoDBX509Authenticator(null);
  98. var reply = MessageHelper.BuildReply<RawBsonDocument>(
  99. RawBsonDocumentHelper.FromJson("{ok: 1}"));
  100. var connection = new MockConnection(__serverId);
  101. connection.Description = CreateConnectionDescription(new SemanticVersion(3, 2, 0));
  102. connection.EnqueueReplyMessage(reply);
  103. Exception exception;
  104. if (async)
  105. {
  106. exception = Record.Exception(() => subject.AuthenticateAsync(connection, __description, CancellationToken.None).GetAwaiter().GetResult());
  107. }
  108. else
  109. {
  110. exception = Record.Exception(() => subject.Authenticate(connection, __description, CancellationToken.None));
  111. }
  112. exception.Should().BeOfType<MongoConnectionException>();
  113. }
  114. [Theory]
  115. [ParameterAttributeData]
  116. public void Authenticate_should_not_throw_when_username_is_null_and_server_support_null_username(
  117. [Values(false, true)]
  118. bool async)
  119. {
  120. var subject = new MongoDBX509Authenticator(null);
  121. var reply = MessageHelper.BuildReply<RawBsonDocument>(
  122. RawBsonDocumentHelper.FromJson("{ok: 1}"));
  123. var connection = new MockConnection(__serverId);
  124. connection.EnqueueReplyMessage(reply);
  125. var description = CreateConnectionDescription(new SemanticVersion(3, 4, 0));
  126. Exception exception;
  127. if (async)
  128. {
  129. exception = Record.Exception(() => subject.AuthenticateAsync(connection, description, CancellationToken.None).GetAwaiter().GetResult());
  130. }
  131. else
  132. {
  133. exception = Record.Exception(() => subject.Authenticate(connection, description, CancellationToken.None));
  134. }
  135. exception.Should().BeNull();
  136. }
  137. // private methods
  138. private ConnectionDescription CreateConnectionDescription(SemanticVersion serverVersion)
  139. {
  140. var clusterId = new ClusterId(1);
  141. var serverId = new ServerId(clusterId, new DnsEndPoint("localhost", 27017));
  142. var connectionId = new ConnectionId(serverId, 1);
  143. var isMasterResult = new IsMasterResult(new BsonDocument());
  144. var buildInfoResult = new BuildInfoResult(new BsonDocument
  145. {
  146. { "version", serverVersion.ToString() }
  147. });
  148. return new ConnectionDescription(connectionId, isMasterResult, buildInfoResult);
  149. }
  150. }
  151. }