PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/mongodb/mongo-csharp-driver
C# | 113 lines | 82 code | 17 blank | 14 comment | 2 complexity | 7171693cf92a5bf8962683b643635438 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 MongoDB.Bson.TestHelpers.XunitExtensions;
  28. namespace MongoDB.Driver.Core.Authentication
  29. {
  30. public class PlainAuthenticatorTests
  31. {
  32. private static readonly UsernamePasswordCredential __credential = new UsernamePasswordCredential("source", "user", "pencil");
  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. [Fact]
  40. public void Constructor_should_throw_an_ArgumentNullException_when_credential_is_null()
  41. {
  42. Action act = () => new PlainAuthenticator(null);
  43. act.ShouldThrow<ArgumentNullException>();
  44. }
  45. [Theory]
  46. [ParameterAttributeData]
  47. public void Authenticate_should_throw_an_AuthenticationException_when_authentication_fails(
  48. [Values(false, true)]
  49. bool async)
  50. {
  51. var subject = new PlainAuthenticator(__credential);
  52. var reply = MessageHelper.BuildNoDocumentsReturnedReply<RawBsonDocument>();
  53. var connection = new MockConnection(__serverId);
  54. connection.EnqueueReplyMessage(reply);
  55. Action act;
  56. if (async)
  57. {
  58. act = () => subject.AuthenticateAsync(connection, __description, CancellationToken.None).GetAwaiter().GetResult();
  59. }
  60. else
  61. {
  62. act = () => subject.Authenticate(connection, __description, CancellationToken.None);
  63. }
  64. act.ShouldThrow<MongoAuthenticationException>();
  65. }
  66. [Theory]
  67. [ParameterAttributeData]
  68. public void Authenticate_should_not_throw_when_authentication_succeeds(
  69. [Values(false, true)]
  70. bool async)
  71. {
  72. var subject = new PlainAuthenticator(__credential);
  73. var saslStartReply = MessageHelper.BuildReply<RawBsonDocument>(
  74. RawBsonDocumentHelper.FromJson("{conversationId: 0, payload: BinData(0,\"\"), done: true, ok: 1}"));
  75. var connection = new MockConnection(__serverId);
  76. connection.EnqueueReplyMessage(saslStartReply);
  77. var expectedRequestId = RequestMessage.CurrentGlobalRequestId + 1;
  78. Action act;
  79. if (async)
  80. {
  81. act = () => subject.AuthenticateAsync(connection, __description, CancellationToken.None).GetAwaiter().GetResult();
  82. }
  83. else
  84. {
  85. act = () => subject.Authenticate(connection, __description, CancellationToken.None);
  86. }
  87. act.ShouldNotThrow();
  88. SpinWait.SpinUntil(() => connection.GetSentMessages().Count >= 1, TimeSpan.FromSeconds(5)).Should().BeTrue();
  89. var sentMessages = MessageHelper.TranslateMessagesToBsonDocuments(connection.GetSentMessages());
  90. sentMessages.Count.Should().Be(1);
  91. var actualRequestId = sentMessages[0]["requestId"].AsInt32;
  92. actualRequestId.Should().BeInRange(expectedRequestId, expectedRequestId + 10);
  93. sentMessages[0].Should().Be("{opcode: \"query\", requestId: " + actualRequestId + ", database: \"source\", collection: \"$cmd\", batchSize: -1, slaveOk: true, query: {saslStart: 1, mechanism: \"PLAIN\", payload: new BinData(0, \"AHVzZXIAcGVuY2ls\")}}");
  94. }
  95. }
  96. }