public void AddClientDocumentToCommand_with_ConnectionInitializer_client_document_should_return_expected_result()
        {
            var command = HelloHelper.CreateCommand();
            var connectionInitializer = new ConnectionInitializer("test", new CompressorConfiguration[0], serverApi: null);
            var subjectClientDocument = (BsonDocument)Reflector.GetFieldValue(connectionInitializer, "_clientDocument");
            var result = HelloHelper.AddClientDocumentToCommand(command, subjectClientDocument);

            var names = result.Names.ToList();

            names.Count.Should().Be(2);
            names[0].Should().Be("isMaster");
            names[1].Should().Be("client");
            result[0].Should().Be(1);
            var clientDocument      = result[1].AsBsonDocument;
            var clientDocumentNames = clientDocument.Names.ToList();

            clientDocumentNames.Count.Should().Be(4);
            clientDocumentNames[0].Should().Be("application");
            clientDocumentNames[1].Should().Be("driver");
            clientDocumentNames[2].Should().Be("os");
            clientDocumentNames[3].Should().Be("platform");
            clientDocument["application"]["name"].AsString.Should().Be("test");
            clientDocument["driver"]["name"].AsString.Should().Be("mongo-csharp-driver");
            clientDocument["driver"]["version"].BsonType.Should().Be(BsonType.String);
        }
        public void InitializeConnection_with_serverApi_should_send_hello([Values(false, true)] bool async)
        {
            var serverApi = new ServerApi(ServerApiVersion.V1, true, true);

            var connection = new MockConnection(__serverId);
            var helloReply = RawBsonDocumentHelper.FromJson($"{{ ok : 1, connectionId : 1, maxWireVersion : {WireVersion.Server42} }}");

            connection.EnqueueCommandResponseMessage(MessageHelper.BuildCommandResponse(helloReply));

            var subject = new ConnectionInitializer("test", new[] { new CompressorConfiguration(CompressorType.Zlib) }, serverApi);

            var result = InitializeConnection(subject, connection, async, CancellationToken.None);

            result.ConnectionId.ServerValue.Should().Be(1);

            SpinWait.SpinUntil(() => connection.GetSentMessages().Count >= 1, TimeSpan.FromSeconds(5)).Should().BeTrue();

            var sentMessages = MessageHelper.TranslateMessagesToBsonDocuments(connection.GetSentMessages());

            sentMessages.Count.Should().Be(1);

            sentMessages[0]["opcode"].AsString.Should().Be("opmsg");
            var helloRequestDocument = sentMessages[0]["sections"][0]["document"];

            helloRequestDocument["hello"].AsInt32.Should().Be(1);
            helloRequestDocument["apiVersion"].AsString.Should().Be("1");
            helloRequestDocument["apiStrict"].AsBoolean.Should().Be(true);
            helloRequestDocument["apiDeprecationErrors"].AsBoolean.Should().Be(true);
        }
Example #3
0
        public void InitializeConnection_should_send_serverApi_in_isMaster_and_buildInfo(
            [Values(false, true)] bool useServerApi,
            [Values(false, true)] bool async)
        {
            var serverApi = useServerApi ? new ServerApi(ServerApiVersion.V1, true, true) : null;

            var isMasterReply  = MessageHelper.BuildReply(RawBsonDocumentHelper.FromJson("{ ok : 1, connectionId : 1 }"));
            var buildInfoReply = MessageHelper.BuildReply(RawBsonDocumentHelper.FromJson("{ ok : 1, version : \"4.2.0\" }"));

            var connection = new MockConnection(__serverId);

            connection.EnqueueReplyMessage(isMasterReply);
            connection.EnqueueReplyMessage(buildInfoReply);

            var subject = new ConnectionInitializer("test", new[] { new CompressorConfiguration(CompressorType.Zlib) }, serverApi);

            ConnectionDescription result;

            if (async)
            {
                result = subject.InitializeConnectionAsync(connection, CancellationToken.None).GetAwaiter().GetResult();
            }
            else
            {
                result = subject.InitializeConnection(connection, CancellationToken.None);
            }

            result.ConnectionId.ServerValue.Should().Be(1);

            SpinWait.SpinUntil(() => connection.GetSentMessages().Count >= 2, TimeSpan.FromSeconds(5)).Should().BeTrue();

            var sentMessages = MessageHelper.TranslateMessagesToBsonDocuments(connection.GetSentMessages());

            sentMessages.Count.Should().Be(2);

            var actualRequestId1 = sentMessages[1]["requestId"].AsInt32;

            sentMessages[0]["opcode"].AsString.Should().Be("query");
            sentMessages[0]["query"]["isMaster"].AsInt32.Should().Be(1);
            if (useServerApi)
            {
                sentMessages[0]["query"]["apiVersion"].AsString.Should().Be("1");
                sentMessages[0]["query"]["apiStrict"].AsBoolean.Should().Be(true);
                sentMessages[0]["query"]["apiDeprecationErrors"].AsBoolean.Should().Be(true);
            }
            else
            {
                sentMessages[0]["query"].AsBsonDocument.TryGetElement("apiVersion", out _).Should().BeFalse();
                sentMessages[0]["query"].AsBsonDocument.TryGetElement("apiStrict", out _).Should().BeFalse();
                sentMessages[0]["query"].AsBsonDocument.TryGetElement("apiDeprecationErrors", out _).Should().BeFalse();
            }
            var expectedServerApiBuildInfoString = useServerApi ? ", apiVersion : \"1\", apiStrict : false, apiDeprecationErrors : true" : "";

            sentMessages[1].Should().Be($"{{ opcode : \"query\", requestId : {actualRequestId1}, database : \"admin\", collection : \"$cmd\", batchSize : -1, slaveOk : true, query : {{ buildInfo : 1{expectedServerApiBuildInfoString} }}}}");
        }
        private ConnectionDescription InitializeConnection(ConnectionInitializer connectionInitializer, IConnection connection, bool async, CancellationToken cancellationToken)
        {
            ConnectionDescription result;

            if (async)
            {
                result = connectionInitializer.SendHelloAsync(connection, cancellationToken).GetAwaiter().GetResult();
                return(connectionInitializer.AuthenticateAsync(connection, result, cancellationToken).GetAwaiter().GetResult());
            }
            else
            {
                result = connectionInitializer.SendHello(connection, cancellationToken);
                return(connectionInitializer.Authenticate(connection, result, cancellationToken));
            }
        }
        public void CreateInitialHelloCommand_with_server_api_should_return_hello_with_speculativeAuthenticate(
            [Values("default", "SCRAM-SHA-256", "SCRAM-SHA-1")] string authenticatorType,
            [Values(false, true)] bool async)
        {
            var credentials = new UsernamePasswordCredential(
                source: "Pathfinder", username: "******", password: "******");
            var authenticator = CreateAuthenticator(authenticatorType, credentials);

            var subject       = new ConnectionInitializer("test", new[] { new CompressorConfiguration(CompressorType.Zlib) }, serverApi: new ServerApi(ServerApiVersion.V1));
            var helloDocument = subject.CreateInitialHelloCommand(new[] { authenticator }, false);

            helloDocument.Should().Contain("hello");
            helloDocument.Should().Contain("speculativeAuthenticate");
            var speculativeAuthenticateDocument = helloDocument["speculativeAuthenticate"].AsBsonDocument;

            speculativeAuthenticateDocument.Should().Contain("mechanism");
            var expectedMechanism = new BsonString(
                authenticatorType == "default" ? "SCRAM-SHA-256" : authenticatorType);

            speculativeAuthenticateDocument["mechanism"].Should().Be(expectedMechanism);
            speculativeAuthenticateDocument["db"].Should().Be(new BsonString(credentials.Source));
        }
        public void InitializeConnection_with_serverApi_should_send_hello_and_buildInfo([Values(false, true)] bool async)
        {
            var serverApi = new ServerApi(ServerApiVersion.V1, true, true);

            var connection = new MockConnection(__serverId);
            var helloReply = RawBsonDocumentHelper.FromJson("{ ok : 1, connectionId : 1 }");

            connection.EnqueueCommandResponseMessage(MessageHelper.BuildCommandResponse(helloReply));
            var buildInfoReply = RawBsonDocumentHelper.FromJson("{ ok : 1, version : \"4.2.0\" }");

            connection.EnqueueCommandResponseMessage(MessageHelper.BuildCommandResponse(buildInfoReply));

            var subject = new ConnectionInitializer("test", new[] { new CompressorConfiguration(CompressorType.Zlib) }, serverApi);

            var result = InitializeConnection(subject, connection, async, CancellationToken.None);

            result.ConnectionId.ServerValue.Should().Be(1);

            SpinWait.SpinUntil(() => connection.GetSentMessages().Count >= 2, TimeSpan.FromSeconds(5)).Should().BeTrue();

            var sentMessages = MessageHelper.TranslateMessagesToBsonDocuments(connection.GetSentMessages());

            sentMessages.Count.Should().Be(2);

            var actualRequestId1 = sentMessages[1]["requestId"].AsInt32;

            sentMessages[0]["opcode"].AsString.Should().Be("opmsg");
            var helloRequestDocument = sentMessages[0]["sections"][0]["document"];

            helloRequestDocument["hello"].AsInt32.Should().Be(1);
            helloRequestDocument["apiVersion"].AsString.Should().Be("1");
            helloRequestDocument["apiStrict"].AsBoolean.Should().Be(true);
            helloRequestDocument["apiDeprecationErrors"].AsBoolean.Should().Be(true);

            sentMessages[1].Should().Be($"{{ \"opcode\" : \"opmsg\", \"requestId\" : {actualRequestId1}, \"responseTo\" : 0, \"sections\" : [ {{ \"payloadType\" : 0, \"document\" : {{ \"buildInfo\" : 1, \"$db\" : \"admin\", \"$readPreference\" : {{ \"mode\" : \"primaryPreferred\" }}, \"apiVersion\" : \"1\", \"apiStrict\" : false, \"apiDeprecationErrors\" : true }} }}] }}");
        }
Example #7
0
 public ConnectionInitializerTests()
 {
     _subject = new ConnectionInitializer("test", new[] { new CompressorConfiguration(CompressorType.Zlib) }, serverApi: null);
 }
Example #8
0
 public static BsonDocument CreateInitialIsMasterCommand(
     this ConnectionInitializer initializer,
     IReadOnlyList <IAuthenticator> authenticators) =>
 (BsonDocument)Reflector.Invoke(initializer, nameof(CreateInitialIsMasterCommand), authenticators);
 public ConnectionInitializerTests()
 {
     _subject = new ConnectionInitializer();
 }
 public static BsonDocument CreateInitialHelloCommand(
     this ConnectionInitializer initializer,
     IReadOnlyList <IAuthenticator> authenticators,
     bool loadBalanced) =>
 (BsonDocument)Reflector.Invoke(initializer, nameof(CreateInitialHelloCommand), authenticators, loadBalanced);
 public ConnectionInitializerTests()
 {
     _subject = new ConnectionInitializer("test");
 }
 public void Setup()
 {
     _subject = new ConnectionInitializer();
 }
 public void Setup()
 {
     _subject = new ConnectionInitializer();
 }