PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/mongodb/mongo-csharp-driver
C# | 126 lines | 95 code | 17 blank | 14 comment | 2 complexity | c5329917300200292202d88cc3cb9e78 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 MongoDB.Driver.Core.WireProtocol.Messages;
  25. using Xunit;
  26. using MongoDB.Driver.Core.Connections;
  27. using System.Threading.Tasks;
  28. using MongoDB.Bson.TestHelpers.XunitExtensions;
  29. namespace MongoDB.Driver.Core.Authentication
  30. {
  31. public class MongoDBXCRAuthenticatorTests
  32. {
  33. private static readonly UsernamePasswordCredential __credential = new UsernamePasswordCredential("source", "user", "pencil");
  34. private static readonly ClusterId __clusterId = new ClusterId();
  35. private static readonly ServerId __serverId = new ServerId(__clusterId, new DnsEndPoint("localhost", 27017));
  36. private static readonly ConnectionDescription __description = new ConnectionDescription(
  37. new ConnectionId(__serverId),
  38. new IsMasterResult(new BsonDocument("ok", 1).Add("ismaster", 1)),
  39. new BuildInfoResult(new BsonDocument("version", "2.6.0")));
  40. [Fact]
  41. public void Constructor_should_throw_an_ArgumentNullException_when_credential_is_null()
  42. {
  43. #pragma warning disable 618
  44. Action act = () => new MongoDBCRAuthenticator(null);
  45. #pragma warning restore 618
  46. act.ShouldThrow<ArgumentNullException>();
  47. }
  48. [Theory]
  49. [ParameterAttributeData]
  50. public void Authenticate_should_throw_an_AuthenticationException_when_authentication_fails(
  51. [Values(false, true)]
  52. bool async)
  53. {
  54. #pragma warning disable 618
  55. var subject = new MongoDBCRAuthenticator(__credential);
  56. #pragma warning restore 618
  57. var reply = MessageHelper.BuildNoDocumentsReturnedReply<RawBsonDocument>();
  58. var connection = new MockConnection(__serverId);
  59. connection.EnqueueReplyMessage(reply);
  60. Action act;
  61. if (async)
  62. {
  63. act = () => subject.AuthenticateAsync(connection, __description, CancellationToken.None).GetAwaiter().GetResult();
  64. }
  65. else
  66. {
  67. act = () => subject.Authenticate(connection, __description, CancellationToken.None);
  68. }
  69. act.ShouldThrow<MongoAuthenticationException>();
  70. }
  71. [Theory]
  72. [ParameterAttributeData]
  73. public void Authenticate_should_not_throw_when_authentication_succeeds(
  74. [Values(false, true)]
  75. bool async)
  76. {
  77. #pragma warning disable 618
  78. var subject = new MongoDBCRAuthenticator(__credential);
  79. #pragma warning restore 618
  80. var getNonceReply = MessageHelper.BuildReply<RawBsonDocument>(
  81. RawBsonDocumentHelper.FromJson("{nonce: \"2375531c32080ae8\", ok: 1}"));
  82. var authenticateReply = MessageHelper.BuildReply<RawBsonDocument>(
  83. RawBsonDocumentHelper.FromJson("{ok: 1}"));
  84. var connection = new MockConnection(__serverId);
  85. connection.EnqueueReplyMessage(getNonceReply);
  86. connection.EnqueueReplyMessage(authenticateReply);
  87. var expectedRequestId = RequestMessage.CurrentGlobalRequestId + 1;
  88. Action act;
  89. if (async)
  90. {
  91. act = () => subject.AuthenticateAsync(connection, __description, CancellationToken.None).GetAwaiter().GetResult();
  92. }
  93. else
  94. {
  95. act = () => subject.Authenticate(connection, __description, CancellationToken.None);
  96. }
  97. act.ShouldNotThrow();
  98. SpinWait.SpinUntil(() => connection.GetSentMessages().Count >= 2, TimeSpan.FromSeconds(5)).Should().BeTrue();
  99. var sentMessages = MessageHelper.TranslateMessagesToBsonDocuments(connection.GetSentMessages());
  100. sentMessages.Count.Should().Be(2);
  101. var actualRequestId0 = sentMessages[0]["requestId"].AsInt32;
  102. var actualRequestId1 = sentMessages[1]["requestId"].AsInt32;
  103. actualRequestId0.Should().BeInRange(expectedRequestId, expectedRequestId + 10);
  104. actualRequestId1.Should().BeInRange(actualRequestId0 + 1, actualRequestId0 + 11);
  105. sentMessages[0].Should().Be("{opcode: \"query\", requestId: " + actualRequestId0 + ", database: \"source\", collection: \"$cmd\", batchSize: -1, slaveOk: true, query: {getnonce: 1}}");
  106. sentMessages[1].Should().Be("{opcode: \"query\", requestId: " + actualRequestId1 + ", database: \"source\", collection: \"$cmd\", batchSize: -1, slaveOk: true, query: {authenticate: 1, user: \"user\", nonce: \"2375531c32080ae8\", key: \"21742f26431831d5cfca035a08c5bdf6\"}}");
  107. }
  108. }
  109. }