@@ -61,7 +61,8 @@ test_iteration_count (int count, bool should_succeed)
61
61
/* set up the scram state to immediately test step 2. */
62
62
_mongoc_scram_init (& scram , MONGOC_CRYPTO_ALGORITHM_SHA_1 );
63
63
_mongoc_scram_set_pass (& scram , "password" );
64
- bson_strncpy (scram .encoded_nonce , client_nonce , sizeof (scram .encoded_nonce ));
64
+ bson_strncpy (
65
+ scram .encoded_nonce , client_nonce , sizeof (scram .encoded_nonce ));
65
66
scram .encoded_nonce_len = (int32_t ) strlen (client_nonce );
66
67
scram .auth_message = bson_malloc0 (4096 );
67
68
scram .auth_messagemax = 4096 ;
@@ -291,53 +292,41 @@ _check_error (const bson_error_t *error, test_error_t expected_error)
291
292
ASSERT_ERROR_CONTAINS ((* error ), domain , code , message );
292
293
}
293
294
294
- /* if auth is expected to succeed, expected_error is zero'd out. */
295
295
static void
296
- _try_auth (bool pooled ,
297
- const char * user ,
298
- const char * pwd ,
299
- const char * mechanism ,
300
- test_error_t expected_error )
296
+ _try_auth_from_uri (bool pooled , mongoc_uri_t * uri , test_error_t expected_error )
301
297
{
302
- mongoc_uri_t * uri ;
303
298
mongoc_client_pool_t * client_pool = NULL ;
304
299
mongoc_client_t * client = NULL ;
300
+ mongoc_collection_t * coll ;
305
301
bson_error_t error ;
306
302
bson_t reply ;
307
303
bool res ;
308
304
309
- uri = test_framework_get_uri ();
310
- mongoc_uri_set_username (uri , user );
311
- mongoc_uri_set_password (uri , pwd );
312
- if (mechanism ) {
313
- mongoc_uri_set_auth_mechanism (uri , mechanism );
314
- }
315
305
if (pooled ) {
316
306
client_pool = mongoc_client_pool_new (uri );
307
+ test_framework_set_pool_ssl_opts (client_pool );
317
308
mongoc_client_pool_set_error_api (client_pool , 2 );
318
309
client = mongoc_client_pool_pop (client_pool );
319
310
/* suppress the auth failure logs from pooled clients. */
320
311
capture_logs (true);
321
312
} else {
322
313
client = mongoc_client_new_from_uri (uri );
323
314
mongoc_client_set_error_api (client , 2 );
315
+ test_framework_set_ssl_opts (client );
324
316
}
325
- res = mongoc_client_command_simple (client ,
326
- "admin" ,
327
- tmp_bson ("{'dbstats': 1}" ),
328
- NULL /* read_prefs. */ ,
329
- & reply ,
330
- & error );
317
+ coll = get_test_collection (client , "try_auth" );
318
+ res = mongoc_collection_insert_one (
319
+ coll , tmp_bson ("{'x': 1}" ), NULL /* opts */ , & reply , & error );
331
320
332
321
if (expected_error == MONGOC_TEST_NO_ERROR ) {
333
- ASSERT (res );
334
- ASSERT_MATCH (& reply , "{'db ': 'admin', 'ok': 1 }" );
322
+ ASSERT_OR_PRINT (res , error );
323
+ ASSERT_MATCH (& reply , "{'insertedCount ': 1 }" );
335
324
} else {
336
325
ASSERT (!res );
337
326
_check_error (& error , expected_error );
338
327
}
339
328
bson_destroy (& reply );
340
- mongoc_uri_destroy ( uri );
329
+ mongoc_collection_destroy ( coll );
341
330
if (pooled ) {
342
331
mongoc_client_pool_push (client_pool , client );
343
332
mongoc_client_pool_destroy (client_pool );
@@ -347,6 +336,26 @@ _try_auth (bool pooled,
347
336
}
348
337
}
349
338
339
+ /* if auth is expected to succeed, expected_error is zero'd out. */
340
+ static void
341
+ _try_auth (bool pooled ,
342
+ const char * user ,
343
+ const char * pwd ,
344
+ const char * mechanism ,
345
+ test_error_t expected_error )
346
+ {
347
+ mongoc_uri_t * uri ;
348
+
349
+ uri = test_framework_get_uri ();
350
+ mongoc_uri_set_username (uri , user );
351
+ mongoc_uri_set_password (uri , pwd );
352
+ if (mechanism ) {
353
+ mongoc_uri_set_auth_mechanism (uri , mechanism );
354
+ }
355
+ _try_auth_from_uri (pooled , uri , expected_error );
356
+ mongoc_uri_destroy (uri );
357
+ }
358
+
350
359
351
360
static void
352
361
_test_mongoc_scram_auth (bool pooled )
@@ -395,8 +404,7 @@ _test_mongoc_scram_auth (bool pooled)
395
404
/* Auth spec: "For a non-existent username, verify that not specifying a
396
405
* mechanism when connecting fails with the same error type that would occur
397
406
* with a correct username but incorrect password or mechanism." */
398
- _try_auth (
399
- pooled , "unknown_user" , "bad" , NULL , MONGOC_TEST_USER_NOT_FOUND_ERROR );
407
+ _try_auth (pooled , "unknown_user" , "bad" , NULL , MONGOC_TEST_AUTH_ERROR );
400
408
}
401
409
402
410
/* test the auth tests described in the auth spec. */
@@ -414,20 +422,31 @@ test_mongoc_scram_auth (void *ctx)
414
422
static int
415
423
_skip_if_no_sha256 ()
416
424
{
417
- mongoc_uri_t * uri ;
418
425
mongoc_client_t * client ;
419
426
bool res ;
427
+ bson_error_t error ;
428
+
429
+ client = test_framework_client_new ();
430
+
431
+ /* Check if SCRAM-SHA-256 is a supported auth mechanism by attempting to
432
+ * create a new user with it. */
433
+ res = mongoc_client_command_simple (
434
+ client ,
435
+ "admin" ,
436
+ tmp_bson ("{'createUser': 'temp', 'pwd': 'sha256', 'roles': ['root'], "
437
+ "'mechanisms': ['SCRAM-SHA-256']}" ),
438
+ NULL /* read_prefs */ ,
439
+ NULL /* reply */ ,
440
+ & error );
441
+
442
+ if (res ) {
443
+ mongoc_database_t * db ;
444
+
445
+ db = mongoc_client_get_database (client , "admin" );
446
+ ASSERT_OR_PRINT (mongoc_database_remove_user (db , "temp" , & error ), error );
447
+ mongoc_database_destroy (db );
448
+ }
420
449
421
- uri = test_framework_get_uri ();
422
- mongoc_uri_set_auth_mechanism (uri , "SCRAM-SHA-256" );
423
- client = mongoc_client_new_from_uri (uri );
424
- res = mongoc_client_command_simple (client ,
425
- "admin" ,
426
- tmp_bson ("{'dbstats': 1}" ),
427
- NULL /* read_prefs */ ,
428
- NULL /* reply */ ,
429
- NULL /* error */ );
430
- mongoc_uri_destroy (uri );
431
450
mongoc_client_destroy (client );
432
451
return res ? 1 : 0 ;
433
452
}
@@ -497,9 +516,68 @@ _drop_saslprep_users ()
497
516
mongoc_client_destroy (client );
498
517
}
499
518
519
+ static void
520
+ _make_uri (const char * username , const char * password , mongoc_uri_t * * out )
521
+ {
522
+ char * uri_str ;
523
+ char * tmp ;
524
+
525
+ tmp = test_framework_get_uri_str_no_auth ("admin" );
526
+ uri_str = test_framework_add_user_password (tmp , username , password );
527
+ mongoc_uri_destroy (* out );
528
+ * out = mongoc_uri_new (uri_str );
529
+ bson_free (tmp );
530
+ bson_free (uri_str );
531
+ }
532
+
500
533
static void
501
534
_test_mongoc_scram_saslprep_auth (bool pooled )
502
535
{
536
+ mongoc_uri_t * uri = NULL ;
537
+
538
+ /* Test URIs of the form in the auth spec test plan for SASLPrep.
539
+ - mongodb://IX:[email protected] /admin
540
+ - mongodb://IX:I%C2%[email protected] /admin
541
+ - mongodb://%E2%85%A8:[email protected] /admin
542
+ - mongodb://%E2%85%A8:I%C2%[email protected] /admin
543
+
544
+ Test in three ways.
545
+ 1. By embedding the multi-byte UTF-8 characters directly into the
546
+ connection string.
547
+ 2. By percent escaping the multi-byte UTF-8 characters.
548
+ 3. By using the setters, mongoc_uri_set_username/mongoc_uri_set_password
549
+ and embedding the UTF-8 characters (percent unescaping does not occur for
550
+ the setters)
551
+ */
552
+
553
+ /* Way 1: embedding multi-byte UTF-8 characters directly */
554
+ _make_uri ("IX" , "IX" , & uri );
555
+ _try_auth_from_uri (pooled , uri , MONGOC_TEST_NO_ERROR );
556
+
557
+ _make_uri ("IX" , ROMAN_NUMERAL_NINE , & uri );
558
+ _try_auth_from_uri (pooled , uri , MONGOC_TEST_NO_ERROR );
559
+
560
+ _make_uri (ROMAN_NUMERAL_NINE , "IV" , & uri );
561
+ _try_auth_from_uri (pooled , uri , MONGOC_TEST_NO_ERROR );
562
+
563
+ _make_uri (ROMAN_NUMERAL_NINE , ROMAN_NUMERAL_FOUR , & uri );
564
+ _try_auth_from_uri (pooled , uri , MONGOC_TEST_NO_ERROR );
565
+
566
+ /* Way 2: Percent escaping */
567
+ _make_uri ("IX" , "IX" , & uri );
568
+ _try_auth_from_uri (pooled , uri , MONGOC_TEST_NO_ERROR );
569
+
570
+ _make_uri ("IX" , "I%C2%ADX" , & uri );
571
+ _try_auth_from_uri (pooled , uri , MONGOC_TEST_NO_ERROR );
572
+
573
+ _make_uri ("%E2%85%A8" , "IV" , & uri );
574
+ _try_auth_from_uri (pooled , uri , MONGOC_TEST_NO_ERROR );
575
+
576
+ _make_uri ("%E2%85%A8" , "I%C2%ADV" , & uri );
577
+ _try_auth_from_uri (pooled , uri , MONGOC_TEST_NO_ERROR );
578
+ mongoc_uri_destroy (uri );
579
+
580
+ /* Way 3: with username/password setters. */
503
581
_try_auth (pooled , "IX" , "IX" , NULL , MONGOC_TEST_NO_ERROR );
504
582
_try_auth (pooled , "IX" , ROMAN_NUMERAL_NINE , NULL , MONGOC_TEST_NO_ERROR );
505
583
_try_auth (pooled , ROMAN_NUMERAL_NINE , "IV" , NULL , MONGOC_TEST_NO_ERROR );
0 commit comments