PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/mongodb/mongo-csharp-driver
C# | 114 lines | 84 code | 16 blank | 14 comment | 2 complexity | df13ebee469443e4874d528412b28aaf 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.Net;
  16. using System.Security;
  17. using System.Threading;
  18. using System.Threading.Tasks;
  19. using FluentAssertions;
  20. using MongoDB.Bson;
  21. using MongoDB.Bson.TestHelpers.XunitExtensions;
  22. using MongoDB.Driver.Core.Clusters;
  23. using MongoDB.Driver.Core.Configuration;
  24. using MongoDB.Driver.Core.Connections;
  25. using MongoDB.Driver.Core.Servers;
  26. using Moq;
  27. using Xunit;
  28. namespace MongoDB.Driver.Core.Authentication
  29. {
  30. public class AuthenticationHelperTests
  31. {
  32. [Theory]
  33. [InlineData("user", "pencil", "1c33006ec1ffd90f9cadcbcc0e118200")]
  34. public void MongoPasswordDigest_should_create_the_correct_hash(string username, string password, string expected)
  35. {
  36. var securePassword = new SecureString();
  37. foreach (var c in password)
  38. {
  39. securePassword.AppendChar(c);
  40. }
  41. securePassword.MakeReadOnly();
  42. var passwordDigest = AuthenticationHelper.MongoPasswordDigest(username, securePassword);
  43. passwordDigest.Should().BeEquivalentTo(expected);
  44. }
  45. [Theory]
  46. [ParameterAttributeData]
  47. public void Authenticate_should_invoke_authenticators_when_they_exist(
  48. [Values(false, true)]
  49. bool async)
  50. {
  51. var description = new ConnectionDescription(
  52. new ConnectionId(new ServerId(new ClusterId(), new DnsEndPoint("localhost", 27017))),
  53. new IsMasterResult(new BsonDocument("ok", 1)),
  54. new BuildInfoResult(new BsonDocument("version", "2.8.0")));
  55. var mockAuthenticator = new Mock<IAuthenticator>();
  56. var settings = new ConnectionSettings(authenticators: new[] { mockAuthenticator.Object });
  57. var mockConnection = new Mock<IConnection>();
  58. mockConnection.SetupGet(c => c.Description).Returns(description);
  59. mockConnection.SetupGet(c => c.Settings).Returns(settings);
  60. if (async)
  61. {
  62. AuthenticationHelper.AuthenticateAsync(mockConnection.Object, description, CancellationToken.None).GetAwaiter().GetResult();
  63. mockAuthenticator.Verify(a => a.AuthenticateAsync(mockConnection.Object, description, CancellationToken.None), Times.Once);
  64. }
  65. else
  66. {
  67. AuthenticationHelper.Authenticate(mockConnection.Object, description, CancellationToken.None);
  68. mockAuthenticator.Verify(a => a.Authenticate(mockConnection.Object, description, CancellationToken.None), Times.Once);
  69. }
  70. }
  71. [Theory]
  72. [ParameterAttributeData]
  73. public void Authenticate_should_not_invoke_authenticators_when_connected_to_an_arbiter(
  74. [Values(false, true)]
  75. bool async)
  76. {
  77. var description = new ConnectionDescription(
  78. new ConnectionId(new ServerId(new ClusterId(), new DnsEndPoint("localhost", 27017))),
  79. new IsMasterResult(new BsonDocument("ok", 1).Add("setName", "rs").Add("arbiterOnly", true)),
  80. new BuildInfoResult(new BsonDocument("version", "2.8.0")));
  81. var mockAuthenticator = new Mock<IAuthenticator>();
  82. var settings = new ConnectionSettings(authenticators: new[] { mockAuthenticator.Object });
  83. var mockConnection = new Mock<IConnection>();
  84. mockConnection.SetupGet(c => c.Description).Returns(description);
  85. mockConnection.SetupGet(c => c.Settings).Returns(settings);
  86. if (async)
  87. {
  88. AuthenticationHelper.AuthenticateAsync(mockConnection.Object, description, CancellationToken.None).GetAwaiter().GetResult();
  89. mockAuthenticator.Verify(a => a.AuthenticateAsync(It.IsAny<IConnection>(), It.IsAny<ConnectionDescription>(), It.IsAny<CancellationToken>()), Times.Never);
  90. }
  91. else
  92. {
  93. AuthenticationHelper.Authenticate(mockConnection.Object, description, CancellationToken.None);
  94. mockAuthenticator.Verify(a => a.Authenticate(It.IsAny<IConnection>(), It.IsAny<ConnectionDescription>(), It.IsAny<CancellationToken>()), Times.Never);
  95. }
  96. }
  97. }
  98. }