Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions tests/MongoDB.Driver.Tests/Core/Connections/HelloResultTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,18 @@ public void ElectionId_should_parse_document_correctly(string json, string expec
subject.ElectionId.Should().Be(expected);
}

[Theory]
[InlineData("{ }", false)]
[InlineData("{ saslSupportedMechs : [] }", true)]
[InlineData("{ saslSupportedMechs : ['SCRAM-SHA-128'] }", true)]
[InlineData("{ saslSupportedMechs : ['unknown'] }", true)]
public void HasSaslSupportedMechs_should_parse_document_correctly(string json, bool expected)
{
var subject = new HelloResult(BsonDocument.Parse(json));

subject.HasSaslSupportedMechs.Should().Be(expected);
}

[Theory]
[InlineData("{ lastWrite : { lastWriteDate : ISODate(\"2015-01-01T00:00:00Z\") } }", 2015)]
[InlineData("{ lastWrite : { lastWriteDate : ISODate(\"2016-01-01T00:00:00Z\") } }", 2016)]
Expand Down Expand Up @@ -205,6 +217,18 @@ public void Me_should_parse_document_correctly(string json, string expectedEndPo
subject.Me.Should().Be(endPoint);
}

[Theory]
[InlineData("{ }", new string[0])]
[InlineData("{ saslSupportedMechs : ['SCRAM-SHA-128'] }", new[] { "SCRAM-SHA-128" })]
[InlineData("{ saslSupportedMechs : ['SCRAM-SHA-128', 'SCRAM-SHA-256'] }", new[] { "SCRAM-SHA-128", "SCRAM-SHA-256" })]
[InlineData("{ saslSupportedMechs : ['unknown'] }", new[] { "unknown" })]
public void SaslSupportedMechs_should_parse_document_correctly(string json, string[] expectedMechs)
{
var subject = new HelloResult(BsonDocument.Parse(json));

subject.SaslSupportedMechs.Should().BeEquivalentTo(expectedMechs);
}

[Theory]
[InlineData("{ ok: 1, isreplicaset: true, setName: \"awesome\", isWritablePrimary: true }", ServerType.ReplicaSetGhost)]
[InlineData("{ ok: 1, setName: \"awesome\", " + OppressiveLanguageConstants.LegacyHelloResponseIsWritablePrimaryFieldName + ": true }", ServerType.ReplicaSetPrimary)]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/* Copyright 2010-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System.Net;
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
using MongoDB.Bson;
using MongoDB.Bson.TestHelpers;
using MongoDB.Driver.Core;
using MongoDB.Driver.Core.Clusters;
using MongoDB.Driver.Core.Configuration;
using MongoDB.Driver.Core.Connections;
using MongoDB.Driver.Core.Misc;
using MongoDB.Driver.Core.Servers;
using MongoDB.Driver.Core.TestHelpers.Logging;
using MongoDB.TestHelpers.XunitExtensions;
using Moq;
using Xunit;
using Xunit.Abstractions;

namespace MongoDB.Driver.Tests.Specifications.mongodb_handshake
{
public class MongoDbHandshakeProseTests : LoggableTestClass
{
// https://github.com/mongodb/specifications/blob/75027a8e91ff50778aed2ad5a67c005f2694705f/source/mongodb-handshake/tests/README.md?plain=1#L77
public MongoDbHandshakeProseTests(ITestOutputHelper output) : base(output)
{
}

[Theory]
[ParameterAttributeData]
public async Task DriverAcceptsArbitraryAuthMechanism([Values(false, true)] bool async)
{
var capturedEvents = new EventCapturer();
var mockStreamFactory = new Mock<IStreamFactory>();
var endPoint = new DnsEndPoint("localhost", 27017);
var serverId = new ServerId(new ClusterId(), endPoint);
var connectionId = new ConnectionId(serverId);
var helloResult = new HelloResult(BsonDocument.Parse("{ ok: 1, saslSupportedMechs : ['arbitrary string'] }"));
var connectionDescription = new ConnectionDescription(connectionId, helloResult);
var connectionInitializerContext = new ConnectionInitializerContext(connectionDescription, null);
var connectionInitializerContextAfterAuthentication = new ConnectionInitializerContext(connectionDescription, null);

var mockConnectionInitializer = new Mock<IConnectionInitializer>();
mockConnectionInitializer
.Setup(i => i.SendHello(It.IsAny<IConnection>(), CancellationToken.None))
.Returns(connectionInitializerContext);
mockConnectionInitializer
.Setup(i => i.Authenticate(It.IsAny<IConnection>(), It.IsAny<ConnectionInitializerContext>(), CancellationToken.None))
.Returns(connectionInitializerContextAfterAuthentication);
mockConnectionInitializer
.Setup(i => i.SendHelloAsync(It.IsAny<IConnection>(), CancellationToken.None))
.ReturnsAsync(connectionInitializerContext);
mockConnectionInitializer
.Setup(i => i.AuthenticateAsync(It.IsAny<IConnection>(), It.IsAny<ConnectionInitializerContext>(), CancellationToken.None))
.ReturnsAsync(connectionInitializerContextAfterAuthentication);

using var subject = new BinaryConnection(
serverId: serverId,
endPoint: endPoint,
settings: new ConnectionSettings(),
streamFactory: mockStreamFactory.Object,
connectionInitializer: mockConnectionInitializer.Object,
eventSubscriber: capturedEvents,
LoggerFactory);

if (async)
{
await subject.OpenAsync(CancellationToken.None);
}
else
{
subject.Open(CancellationToken.None);
}

subject._state().Should().Be(3); // 3 - open.
}
}

internal static class BinaryConnectionReflector
{
public static int _state(this BinaryConnection subject)
=> ((InterlockedInt32)Reflector.GetFieldValue(subject, nameof(_state))).Value;
}
}