@@ -3442,6 +3442,133 @@ test_explicit_encryption_case1 (void *unused)
3442
3442
explicit_encryption_destroy (eef );
3443
3443
}
3444
3444
3445
+ static void
3446
+ test_explicit_encryption_case2 (void * unused )
3447
+ {
3448
+ /* Case 2: can insert encrypted indexed and find with non-zero contention */
3449
+ bson_error_t error ;
3450
+ bool ok ;
3451
+ mongoc_client_encryption_encrypt_opts_t * eopts ;
3452
+ bson_value_t plaintext = {0 };
3453
+ int i = 0 ;
3454
+
3455
+ ee_fixture * eef = explicit_encryption_setup ();
3456
+ plaintext .value_type = BSON_TYPE_UTF8 ;
3457
+ plaintext .value .v_utf8 .str = "encrypted indexed value" ;
3458
+ plaintext .value .v_utf8 .len = (uint32_t ) strlen (plaintext .value .v_utf8 .str );
3459
+
3460
+ /* Insert 10 documents ``{ "encryptedIndexed": <insertPayload> }`` with
3461
+ * contention factor 10. */
3462
+ for (i = 0 ; i < 10 ; i ++ ) {
3463
+ bson_value_t insertPayload ;
3464
+ bson_t to_insert = BSON_INITIALIZER ;
3465
+
3466
+ eopts = mongoc_client_encryption_encrypt_opts_new ();
3467
+ mongoc_client_encryption_encrypt_opts_set_keyid (eopts , & eef -> key1ID );
3468
+ mongoc_client_encryption_encrypt_opts_set_algorithm (
3469
+ eopts , MONGOC_ENCRYPT_ALGORITHM_INDEXED );
3470
+ mongoc_client_encryption_encrypt_opts_set_contention_factor (eopts , 10 );
3471
+
3472
+ ok = mongoc_client_encryption_encrypt (
3473
+ eef -> clientEncryption , & plaintext , eopts , & insertPayload , & error );
3474
+ ASSERT_OR_PRINT (ok , error );
3475
+
3476
+ ASSERT (
3477
+ BSON_APPEND_VALUE (& to_insert , "encryptedIndexed" , & insertPayload ));
3478
+
3479
+ ok = mongoc_collection_insert_one (eef -> encryptedColl ,
3480
+ & to_insert ,
3481
+ NULL /* opts */ ,
3482
+ NULL /* reply */ ,
3483
+ & error );
3484
+ ASSERT_OR_PRINT (ok , error );
3485
+
3486
+ bson_value_destroy (& insertPayload );
3487
+ bson_destroy (& to_insert );
3488
+ mongoc_client_encryption_encrypt_opts_destroy (eopts );
3489
+ }
3490
+
3491
+ /* Find with default contention factor of 0. Expect < 10 documents returned.
3492
+ */
3493
+ {
3494
+ bson_value_t findPayload ;
3495
+ mongoc_cursor_t * cursor ;
3496
+ bson_t filter = BSON_INITIALIZER ;
3497
+ const bson_t * got ;
3498
+ int got_count = 0 ;
3499
+
3500
+ eopts = mongoc_client_encryption_encrypt_opts_new ();
3501
+ mongoc_client_encryption_encrypt_opts_set_keyid (eopts , & eef -> key1ID );
3502
+ mongoc_client_encryption_encrypt_opts_set_algorithm (
3503
+ eopts , MONGOC_ENCRYPT_ALGORITHM_INDEXED );
3504
+ mongoc_client_encryption_encrypt_opts_set_query_type (
3505
+ eopts , MONGOC_ENCRYPT_QUERY_TYPE_EQUALITY );
3506
+
3507
+ ok = mongoc_client_encryption_encrypt (
3508
+ eef -> clientEncryption , & plaintext , eopts , & findPayload , & error );
3509
+ ASSERT_OR_PRINT (ok , error );
3510
+
3511
+ ASSERT (BSON_APPEND_VALUE (& filter , "encryptedIndexed" , & findPayload ));
3512
+
3513
+ cursor = mongoc_collection_find_with_opts (
3514
+ eef -> encryptedColl , & filter , NULL /* opts */ , NULL /* read_prefs */ );
3515
+
3516
+ while (mongoc_cursor_next (cursor , & got )) {
3517
+ got_count ++ ;
3518
+ ASSERT_MATCH (got ,
3519
+ "{ 'encryptedIndexed': 'encrypted indexed value' }" );
3520
+ }
3521
+ ASSERT_OR_PRINT (!mongoc_cursor_error (cursor , & error ), error );
3522
+ ASSERT_CMPINT (got_count , < , 10 );
3523
+
3524
+ bson_value_destroy (& findPayload );
3525
+ mongoc_cursor_destroy (cursor );
3526
+ mongoc_client_encryption_encrypt_opts_destroy (eopts );
3527
+ bson_destroy (& filter );
3528
+ }
3529
+
3530
+ /* Find with contention factor of 10. Expect all 10 documents returned. */
3531
+ {
3532
+ bson_value_t findPayload ;
3533
+ mongoc_cursor_t * cursor ;
3534
+ bson_t filter = BSON_INITIALIZER ;
3535
+ const bson_t * got ;
3536
+ int got_count = 0 ;
3537
+
3538
+ eopts = mongoc_client_encryption_encrypt_opts_new ();
3539
+ mongoc_client_encryption_encrypt_opts_set_keyid (eopts , & eef -> key1ID );
3540
+ mongoc_client_encryption_encrypt_opts_set_algorithm (
3541
+ eopts , MONGOC_ENCRYPT_ALGORITHM_INDEXED );
3542
+ mongoc_client_encryption_encrypt_opts_set_query_type (
3543
+ eopts , MONGOC_ENCRYPT_QUERY_TYPE_EQUALITY );
3544
+ mongoc_client_encryption_encrypt_opts_set_contention_factor (eopts , 10 );
3545
+
3546
+ ok = mongoc_client_encryption_encrypt (
3547
+ eef -> clientEncryption , & plaintext , eopts , & findPayload , & error );
3548
+ ASSERT_OR_PRINT (ok , error );
3549
+
3550
+ ASSERT (BSON_APPEND_VALUE (& filter , "encryptedIndexed" , & findPayload ));
3551
+
3552
+ cursor = mongoc_collection_find_with_opts (
3553
+ eef -> encryptedColl , & filter , NULL /* opts */ , NULL /* read_prefs */ );
3554
+
3555
+ while (mongoc_cursor_next (cursor , & got )) {
3556
+ got_count ++ ;
3557
+ ASSERT_MATCH (got ,
3558
+ "{ 'encryptedIndexed': 'encrypted indexed value' }" );
3559
+ }
3560
+ ASSERT_OR_PRINT (!mongoc_cursor_error (cursor , & error ), error );
3561
+ ASSERT_CMPINT (got_count , = = , 10 );
3562
+
3563
+ bson_value_destroy (& findPayload );
3564
+ mongoc_cursor_destroy (cursor );
3565
+ mongoc_client_encryption_encrypt_opts_destroy (eopts );
3566
+ bson_destroy (& filter );
3567
+ }
3568
+
3569
+ explicit_encryption_destroy (eef );
3570
+ }
3571
+
3445
3572
void
3446
3573
test_client_side_encryption_install (TestSuite * suite )
3447
3574
{
@@ -3597,4 +3724,13 @@ test_client_side_encryption_install (TestSuite *suite)
3597
3724
test_framework_skip_if_no_client_side_encryption ,
3598
3725
test_framework_skip_if_max_wire_version_less_than_17 ,
3599
3726
test_framework_skip_if_single );
3727
+
3728
+ TestSuite_AddFull (suite ,
3729
+ "/client_side_encryption/explicit_encryption/case2" ,
3730
+ test_explicit_encryption_case2 ,
3731
+ NULL /* dtor */ ,
3732
+ NULL /* ctx */ ,
3733
+ test_framework_skip_if_no_client_side_encryption ,
3734
+ test_framework_skip_if_max_wire_version_less_than_17 ,
3735
+ test_framework_skip_if_single );
3600
3736
}
0 commit comments