2626using MongoDB . Driver . Core . Connections ;
2727using System . Threading . Tasks ;
2828using MongoDB . Bson . TestHelpers . XunitExtensions ;
29+ using MongoDB . Driver . Core . Misc ;
2930
3031namespace MongoDB . Driver . Core . Authentication
3132{
@@ -39,9 +40,8 @@ public class MongoDBX509AuthenticatorTests
3940 new BuildInfoResult ( new BsonDocument ( "version" , "2.6.0" ) ) ) ;
4041
4142 [ Theory ]
42- [ InlineData ( null ) ]
4343 [ InlineData ( "" ) ]
44- public void Constructor_should_throw_an_ArgumentException_when_username_is_null_or_empty ( string username )
44+ public void Constructor_should_throw_an_ArgumentException_when_username_is_empty ( string username )
4545 {
4646 Action act = ( ) => new MongoDBX509Authenticator ( username ) ;
4747
@@ -58,6 +58,7 @@ public void Authenticate_should_throw_an_AuthenticationException_when_authentica
5858
5959 var reply = MessageHelper . BuildNoDocumentsReturnedReply < RawBsonDocument > ( ) ;
6060 var connection = new MockConnection ( __serverId ) ;
61+ connection . Description = CreateConnectionDescription ( new SemanticVersion ( 3 , 2 , 0 ) ) ;
6162 connection . EnqueueReplyMessage ( reply ) ;
6263
6364 Action act ;
@@ -85,6 +86,7 @@ public void Authenticate_should_not_throw_when_authentication_succeeds(
8586 RawBsonDocumentHelper . FromJson ( "{ok: 1}" ) ) ;
8687
8788 var connection = new MockConnection ( __serverId ) ;
89+ connection . Description = CreateConnectionDescription ( new SemanticVersion ( 3 , 2 , 0 ) ) ;
8890 connection. EnqueueReplyMessage ( reply ) ;
8991
9092 Action act;
@@ -99,5 +101,75 @@ public void Authenticate_should_not_throw_when_authentication_succeeds(
99101
100102 act . ShouldNotThrow ( ) ;
101103 }
104+
105+ [ Theory ]
106+ [ ParameterAttributeData ]
107+ public void Authenticate_should_throw_when_username_is_null_and_server_does_not_support_null_username (
108+ [ Values ( false , true ) ]
109+ bool async)
110+ {
111+ var subject = new MongoDBX509Authenticator ( null ) ;
112+
113+ var reply = MessageHelper. BuildReply< RawBsonDocument> (
114+ RawBsonDocumentHelper . FromJson ( "{ok: 1}" ) ) ;
115+
116+ var connection = new MockConnection( __serverId ) ;
117+ connection. Description = CreateConnectionDescription ( new SemanticVersion ( 3 , 2 , 0 ) ) ;
118+ connection. EnqueueReplyMessage ( reply ) ;
119+
120+ Exception exception;
121+ if ( async)
122+ {
123+ exception = Record . Exception ( ( ) => subject . AuthenticateAsync ( connection , __description , CancellationToken . None ) . GetAwaiter ( ) . GetResult ( ) ) ;
124+ }
125+ else
126+ {
127+ exception = Record . Exception ( ( ) => subject . Authenticate ( connection , __description , CancellationToken . None ) ) ;
128+ }
129+
130+ exception . Should ( ) . BeOfType < MongoConnectionException > ( ) ;
131+ }
132+
133+ [ Theory ]
134+ [ ParameterAttributeData ]
135+ public void Authenticate_should_not_throw_when_username_is_null_and_server_support_null_username (
136+ [ Values ( false , true ) ]
137+ bool async)
138+ {
139+ var subject = new MongoDBX509Authenticator ( null ) ;
140+
141+ var reply = MessageHelper. BuildReply< RawBsonDocument> (
142+ RawBsonDocumentHelper . FromJson ( "{ok: 1}" ) ) ;
143+
144+ var connection = new MockConnection( __serverId ) ;
145+ connection. Description = CreateConnectionDescription ( new SemanticVersion ( 3 , 4 , 0 ) ) ;
146+ connection. EnqueueReplyMessage ( reply ) ;
147+
148+ Exception exception;
149+ if ( async)
150+ {
151+ exception = Record . Exception ( ( ) => subject . AuthenticateAsync ( connection , __description , CancellationToken . None ) . GetAwaiter ( ) . GetResult ( ) ) ;
152+ }
153+ else
154+ {
155+ exception = Record . Exception ( ( ) => subject . Authenticate ( connection , __description , CancellationToken . None ) ) ;
156+ }
157+
158+ exception . Should ( ) . BeNull ( ) ;
159+ }
160+
161+ // private methods
162+ private ConnectionDescription CreateConnectionDescription ( SemanticVersion serverVersion )
163+ {
164+ var clusterId = new ClusterId ( 1 ) ;
165+ var serverId = new ServerId ( clusterId , new DnsEndPoint ( "localhost" , 27017 ) ) ;
166+ var connectionId = new ConnectionId ( serverId , 1 ) ;
167+ var isMasterResult = new IsMasterResult ( new BsonDocument ( ) ) ;
168+ var buildInfoResult = new BuildInfoResult ( new BsonDocument
169+ {
170+ { "version" , serverVersion . ToString ( ) }
171+ } ) ;
172+ return new ConnectionDescription ( connectionId , isMasterResult , buildInfoResult ) ;
173+ }
102174 }
103175}
0 commit comments