@@ -50,21 +50,25 @@ public class BinaryConnection_CommandEventTests : IDisposable
5050
5151 public static IEnumerable < object [ ] > GetPotentiallyRedactedCommandTestCases ( )
5252 {
53- var potentiallyRedactedCommands = new [ ]
53+ return new object [ ] [ ]
5454 {
55- "authenticate" ,
56- "saslStart" ,
57- "saslContinue" ,
58- "getnonce" ,
59- "createUser" ,
60- "updateUser" ,
61- "copydbgetnonce" ,
62- "copydbsaslstart" ,
63- "copydb" ,
64- "isMaster" ,
55+ // string commandJson, bool shouldBeRedacted
56+ new object [ ] { "{ xyz : 1 }" , false } ,
57+ new object [ ] { "{ authenticate : 1 }" , true } ,
58+ new object [ ] { "{ saslStart : 1 }" , true } ,
59+ new object [ ] { "{ saslContinue : 1 }" , true } ,
60+ new object [ ] { "{ getnonce : 1 }" , true } ,
61+ new object [ ] { "{ createUser : 1 }" , true } ,
62+ new object [ ] { "{ updateUser : 1 }" , true } ,
63+ new object [ ] { "{ copydbgetnonce : 1 }" , true } ,
64+ new object [ ] { "{ copydbsaslstart : 1 }" , true } ,
65+ new object [ ] { "{ copydb : 1 }" , true } ,
66+ new object [ ] { "{ authenticate : 1 }" , true } ,
67+ new object [ ] { "{ isMaster : 1 }" , false } ,
68+ new object [ ] { "{ isMaster : 1, speculativeAuthenticate : { } }" , true } ,
6569 } ;
66- return potentiallyRedactedCommands . Select ( c => new object [ ] { c } ) ;
6770 }
71+
6872 public BinaryConnection_CommandEventTests ( )
6973 {
7074 _capturedEvents = new EventCapturer ( )
@@ -144,13 +148,10 @@ public void Should_process_a_command()
144148
145149 [ Theory ]
146150 [ MemberData ( nameof ( GetPotentiallyRedactedCommandTestCases ) ) ]
147- public void Should_process_a_redacted_command ( string commandName )
151+ public void Should_process_a_redacted_command ( string commandJson , bool shouldBeRedacted )
148152 {
149- var command = BsonDocument . Parse ( $ "{{ { commandName } : 1, extra: true }}") ;
150- command = ModifyMessageToTriggerConditionToRedact ( commandName , command ) ;
151-
153+ var command = BsonDocument . Parse ( commandJson ) ;
152154 var reply = BsonDocument . Parse ( "{ ok: 1, extra: true }" ) ;
153- reply = ModifyMessageToTriggerConditionToRedact ( commandName , reply ) ;
154155
155156 var requestMessage = MessageHelper . BuildCommand (
156157 command ,
@@ -163,17 +164,11 @@ public void Should_process_a_redacted_command(string commandName)
163164 responseTo : requestMessage . RequestId ) ;
164165 ReceiveMessages ( replyMessage ) ;
165166
166-
167- var commandStartedCommandShouldBeRedacted = CommandEventHelperReflector . ShouldRedactMessage ( commandName , command ) ;
168- var commandSucceededCommandShouldBeRedacted = CommandEventHelperReflector . ShouldRedactMessage ( commandName , replyMessage . Documents [ 0 ] ) ;
169- var commandStartedCommandExpectedElementCount = commandStartedCommandShouldBeRedacted ? 0 : command . ElementCount ;
170- var commandSucceededCommandExpectedElementCount = commandSucceededCommandShouldBeRedacted ? 0 : reply . ElementCount ;
171-
172167 var commandStartedEvent = ( CommandStartedEvent ) _capturedEvents . Next ( ) ;
173168 var commandSucceededEvent = ( CommandSucceededEvent ) _capturedEvents . Next ( ) ;
174169
175170 commandStartedEvent . CommandName . Should ( ) . Be ( command . GetElement ( 0 ) . Name ) ;
176- commandStartedEvent . Command . ElementCount . Should ( ) . Be ( commandStartedCommandExpectedElementCount ) ;
171+ commandStartedEvent . Command . Should ( ) . Be ( shouldBeRedacted ? new BsonDocument ( ) : command ) ;
177172 commandStartedEvent . ConnectionId . Should ( ) . Be ( _subject . ConnectionId ) ;
178173 commandStartedEvent . DatabaseNamespace . Should ( ) . Be ( MessageHelper . DefaultDatabaseNamespace ) ;
179174 commandStartedEvent . OperationId . Should ( ) . Be ( EventContext . OperationId ) ;
@@ -183,7 +178,7 @@ public void Should_process_a_redacted_command(string commandName)
183178 commandSucceededEvent . ConnectionId . Should ( ) . Be ( commandStartedEvent . ConnectionId ) ;
184179 commandSucceededEvent . Duration . Should ( ) . BeGreaterThan ( TimeSpan . Zero ) ;
185180 commandSucceededEvent . OperationId . Should ( ) . Be ( commandStartedEvent . OperationId ) ;
186- commandSucceededEvent . Reply . ElementCount . Should ( ) . Be ( commandSucceededCommandExpectedElementCount ) ;
181+ commandSucceededEvent . Reply . Should ( ) . Be ( shouldBeRedacted ? new BsonDocument ( ) : reply ) ;
187182 commandSucceededEvent . RequestId . Should ( ) . Be ( commandStartedEvent . RequestId ) ;
188183 }
189184
@@ -224,13 +219,10 @@ public void Should_process_a_failed_command()
224219
225220 [ Theory ]
226221 [ MemberData ( nameof ( GetPotentiallyRedactedCommandTestCases ) ) ]
227- public void Should_process_a_redacted_failed_command ( string commandName )
222+ public void Should_process_a_redacted_failed_command ( string commandJson , bool shouldBeRedacted )
228223 {
229- var command = BsonDocument . Parse ( $ "{{ { commandName } : 1, extra: true }}") ;
230- command = ModifyMessageToTriggerConditionToRedact ( commandName , command ) ;
231-
224+ var command = BsonDocument . Parse ( commandJson ) ;
232225 var reply = BsonDocument . Parse ( "{ ok: 0, extra: true }" ) ;
233- reply = ModifyMessageToTriggerConditionToRedact ( commandName , reply ) ;
234226
235227 var requestMessage = MessageHelper . BuildCommand (
236228 command ,
@@ -243,16 +235,11 @@ public void Should_process_a_redacted_failed_command(string commandName)
243235 responseTo : requestMessage . RequestId ) ;
244236 ReceiveMessages ( replyMessage ) ;
245237
246- var commandStartedCommandShouldBeRedacted = CommandEventHelperReflector . ShouldRedactMessage ( commandName , command ) ;
247- var commandSucceededCommandShouldBeRedacted = CommandEventHelperReflector . ShouldRedactMessage ( commandName , replyMessage . Documents [ 0 ] ) ;
248- var commandStartedCommandExpectedElementCount = commandStartedCommandShouldBeRedacted ? 0 : command . ElementCount ;
249- var commandSucceededCommandExpectedElementCount = commandSucceededCommandShouldBeRedacted ? 0 : reply . ElementCount ;
250-
251238 var commandStartedEvent = ( CommandStartedEvent ) _capturedEvents . Next ( ) ;
252239 var commandFailedEvent = ( CommandFailedEvent ) _capturedEvents . Next ( ) ;
253240
254241 commandStartedEvent . CommandName . Should ( ) . Be ( command . GetElement ( 0 ) . Name ) ;
255- commandStartedEvent . Command . ElementCount . Should ( ) . Be ( commandStartedCommandExpectedElementCount ) ;
242+ commandStartedEvent . Command . Should ( ) . Be ( shouldBeRedacted ? new BsonDocument ( ) : command ) ;
256243 commandStartedEvent . ConnectionId . Should ( ) . Be ( _subject . ConnectionId ) ;
257244 commandStartedEvent . DatabaseNamespace . Should ( ) . Be ( MessageHelper . DefaultDatabaseNamespace ) ;
258245 commandStartedEvent . OperationId . Should ( ) . Be ( EventContext . OperationId ) ;
@@ -265,7 +252,7 @@ public void Should_process_a_redacted_failed_command(string commandName)
265252 commandFailedEvent . RequestId . Should ( ) . Be ( commandStartedEvent . RequestId ) ;
266253 commandFailedEvent . Failure . Should ( ) . BeOfType < MongoCommandException > ( ) ;
267254 var exception = ( MongoCommandException ) commandFailedEvent . Failure ;
268- exception . Result . ElementCount . Should ( ) . Be ( commandSucceededCommandExpectedElementCount ) ;
255+ exception . Result . Should ( ) . Be ( shouldBeRedacted ? new BsonDocument ( ) : reply ) ;
269256 }
270257
271258 [ Fact ]
@@ -982,24 +969,5 @@ private void ReceiveMessages(params ReplyMessage<BsonDocument>[] messages)
982969 _subject . ReceiveMessageAsync ( message . ResponseTo , encoderSelector , _messageEncoderSettings , CancellationToken . None ) . Wait ( ) ;
983970 }
984971 }
985-
986- private static BsonDocument ModifyMessageToTriggerConditionToRedact ( string commandName , BsonDocument command )
987- {
988- switch ( commandName )
989- {
990- case "isMaster" :
991- command . Add ( "speculativeAuthenticate" , new BsonDocument ( "db" , "authSource" ) ) ;
992- break ;
993- }
994-
995- return command ;
996- }
997- }
998-
999- internal static class CommandEventHelperReflector
1000- {
1001- public static bool ShouldRedactMessage ( string commandName , BsonDocument command ) =>
1002- ( bool ) Reflector . InvokeStatic ( typeof ( CommandEventHelper ) , nameof ( ShouldRedactMessage ) , commandName , command ) ;
1003-
1004972 }
1005973}
0 commit comments