1// Copyright (C) MongoDB, Inc. 2017-present.
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may
4// not use this file except in compliance with the License. You may obtain
5// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
7package driver
8
9import (
10	"testing"
11
12	"go.mongodb.org/mongo-driver/internal/testutil/assert"
13	"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
14)
15
16func TestCommandMonitoring(t *testing.T) {
17	t.Run("redactCommand", func(t *testing.T) {
18		emptyDoc := bsoncore.BuildDocumentFromElements(nil)
19		isMaster := bsoncore.BuildDocumentFromElements(nil,
20			bsoncore.AppendInt32Element(nil, "isMaster", 1),
21		)
22		isMasterLowercase := bsoncore.BuildDocumentFromElements(nil,
23			bsoncore.AppendInt32Element(nil, "ismaster", 1),
24		)
25		isMasterSpeculative := bsoncore.BuildDocumentFromElements(nil,
26			bsoncore.AppendInt32Element(nil, "isMaster", 1),
27			bsoncore.AppendDocumentElement(nil, "speculativeAuthenticate", emptyDoc),
28		)
29		isMasterSpeculativeLowercase := bsoncore.BuildDocumentFromElements(nil,
30			bsoncore.AppendInt32Element(nil, "ismaster", 1),
31			bsoncore.AppendDocumentElement(nil, "speculativeAuthenticate", emptyDoc),
32		)
33
34		testCases := []struct {
35			name        string
36			commandName string
37			command     bsoncore.Document
38			redacted    bool
39		}{
40			{"isMaster", "isMaster", isMaster, false},
41			{"isMaster lowercase", "ismaster", isMasterLowercase, false},
42			{"isMaster speculative auth", "isMaster", isMasterSpeculative, true},
43			{"isMaster speculative auth lowercase", "isMaster", isMasterSpeculativeLowercase, true},
44		}
45		for _, tc := range testCases {
46			t.Run(tc.name, func(t *testing.T) {
47				canMonitor := (&Operation{}).redactCommand(tc.commandName, tc.command)
48				assert.Equal(t, tc.redacted, canMonitor, "expected redacted %v, got %v", tc.redacted, canMonitor)
49			})
50		}
51	})
52}
53