@@ -5477,7 +5477,7 @@ test_auto_datakeys (void *unused)
5477
5477
}
5478
5478
5479
5479
static void
5480
- test_create_encrypted_collection (void * unused )
5480
+ test_create_encrypted_collection_simple (void * unused )
5481
5481
{
5482
5482
BSON_UNUSED (unused );
5483
5483
bson_error_t error = {0 };
@@ -5508,7 +5508,7 @@ test_create_encrypted_collection (void *unused)
5508
5508
mongoc_client_encryption_opts_new ();
5509
5509
mongoc_client_encryption_opts_set_kms_providers (ceOpts , kmsProviders );
5510
5510
mongoc_client_encryption_opts_set_keyvault_namespace (
5511
- ceOpts , "keyvaule " , "datakeys" );
5511
+ ceOpts , "keyvault " , "datakeys" );
5512
5512
mongoc_client_encryption_opts_set_keyvault_client (ceOpts , client );
5513
5513
mongoc_client_encryption_t * const ce =
5514
5514
mongoc_client_encryption_new (ceOpts , & error );
@@ -5550,6 +5550,135 @@ test_create_encrypted_collection (void *unused)
5550
5550
mongoc_client_destroy (client );
5551
5551
}
5552
5552
5553
+ static void
5554
+ test_create_encrypted_collection_no_encryptedFields (void * unused )
5555
+ {
5556
+ BSON_UNUSED (unused );
5557
+ bson_error_t error = {0 };
5558
+ mongoc_client_t * const client = test_framework_new_default_client ();
5559
+ bson_t * const kmsProviders = _make_kms_providers (false, true);
5560
+
5561
+ const char * const dbName = "cec-test-db" ;
5562
+
5563
+ // Drop prior data
5564
+ {
5565
+ mongoc_collection_t * const coll =
5566
+ mongoc_client_get_collection (client , "keyvault" , "datakeys" );
5567
+ if (coll ) {
5568
+ mongoc_collection_drop (coll , & error );
5569
+ bool okay =
5570
+ error .code == 0 || strstr (error .message , "ns not found" ) != NULL ;
5571
+ ASSERT_OR_PRINT (okay , error );
5572
+ }
5573
+ mongoc_collection_destroy (coll );
5574
+
5575
+ mongoc_database_t * const db = mongoc_client_get_database (client , dbName );
5576
+ ASSERT_OR_PRINT (mongoc_database_drop (db , & error ), error );
5577
+ mongoc_database_destroy (db );
5578
+ }
5579
+
5580
+ // Create a CE
5581
+ mongoc_client_encryption_opts_t * const ceOpts =
5582
+ mongoc_client_encryption_opts_new ();
5583
+ mongoc_client_encryption_opts_set_kms_providers (ceOpts , kmsProviders );
5584
+ mongoc_client_encryption_opts_set_keyvault_namespace (
5585
+ ceOpts , "keyvault" , "datakeys" );
5586
+ mongoc_client_encryption_opts_set_keyvault_client (ceOpts , client );
5587
+ mongoc_client_encryption_t * const ce =
5588
+ mongoc_client_encryption_new (ceOpts , & error );
5589
+ mongoc_client_encryption_opts_destroy (ceOpts );
5590
+ ASSERT_OR_PRINT (ce , error );
5591
+
5592
+ // Create the encrypted collection
5593
+ bsonBuildDecl (ccOpts );
5594
+ mongoc_database_t * const db = mongoc_client_get_database (client , dbName );
5595
+ mongoc_client_encryption_datakey_opts_t * const dkOpts =
5596
+ mongoc_client_encryption_datakey_opts_new ();
5597
+ mongoc_collection_t * const coll =
5598
+ mongoc_client_encryption_create_encrypted_collection (
5599
+ ce , db , "test-coll" , & ccOpts , NULL , "local" , dkOpts , & error );
5600
+ ASSERT_ERROR_CONTAINS (error ,
5601
+ MONGOC_ERROR_COMMAND ,
5602
+ MONGOC_ERROR_COMMAND_INVALID_ARG ,
5603
+ "No 'encryptedFields' are defined" );
5604
+ bson_destroy (& ccOpts );
5605
+
5606
+ bson_destroy (kmsProviders );
5607
+ mongoc_client_encryption_datakey_opts_destroy (dkOpts );
5608
+ mongoc_collection_destroy (coll );
5609
+ mongoc_database_drop (db , & error );
5610
+ mongoc_database_destroy (db );
5611
+ mongoc_client_encryption_destroy (ce );
5612
+ mongoc_client_destroy (client );
5613
+ }
5614
+
5615
+ static void
5616
+ test_create_encrypted_collection_bad_keyId (void * unused )
5617
+ {
5618
+ BSON_UNUSED (unused );
5619
+ bson_error_t error = {0 };
5620
+ mongoc_client_t * const client = test_framework_new_default_client ();
5621
+ bson_t * const kmsProviders = _make_kms_providers (false, true);
5622
+
5623
+ const char * const dbName = "cec-test-db" ;
5624
+
5625
+ // Drop prior data
5626
+ {
5627
+ mongoc_collection_t * const coll =
5628
+ mongoc_client_get_collection (client , "keyvault" , "datakeys" );
5629
+ if (coll ) {
5630
+ mongoc_collection_drop (coll , & error );
5631
+ bool okay =
5632
+ error .code == 0 || strstr (error .message , "ns not found" ) != NULL ;
5633
+ ASSERT_OR_PRINT (okay , error );
5634
+ }
5635
+ mongoc_collection_destroy (coll );
5636
+
5637
+ mongoc_database_t * const db = mongoc_client_get_database (client , dbName );
5638
+ ASSERT_OR_PRINT (mongoc_database_drop (db , & error ), error );
5639
+ mongoc_database_destroy (db );
5640
+ }
5641
+
5642
+ // Create a CE
5643
+ mongoc_client_encryption_opts_t * const ceOpts =
5644
+ mongoc_client_encryption_opts_new ();
5645
+ mongoc_client_encryption_opts_set_kms_providers (ceOpts , kmsProviders );
5646
+ mongoc_client_encryption_opts_set_keyvault_namespace (
5647
+ ceOpts , "keyvault" , "datakeys" );
5648
+ mongoc_client_encryption_opts_set_keyvault_client (ceOpts , client );
5649
+ mongoc_client_encryption_t * const ce =
5650
+ mongoc_client_encryption_new (ceOpts , & error );
5651
+ mongoc_client_encryption_opts_destroy (ceOpts );
5652
+ ASSERT_OR_PRINT (ce , error );
5653
+
5654
+ // Create the encrypted collection
5655
+ bsonBuildDecl (ccOpts ,
5656
+ kv ("encryptedFields" ,
5657
+ doc (kv ("fields" ,
5658
+ array (doc (kv ("path" , cstr ("ssn" )),
5659
+ kv ("bsonType" , cstr ("string" )),
5660
+ kv ("keyId" , bool (true))))))));
5661
+ mongoc_database_t * const db = mongoc_client_get_database (client , dbName );
5662
+ mongoc_client_encryption_datakey_opts_t * const dkOpts =
5663
+ mongoc_client_encryption_datakey_opts_new ();
5664
+ mongoc_collection_t * const coll =
5665
+ mongoc_client_encryption_create_encrypted_collection (
5666
+ ce , db , "test-coll" , & ccOpts , NULL , "local" , dkOpts , & error );
5667
+ ASSERT_ERROR_CONTAINS (error ,
5668
+ MONGOC_ERROR_QUERY ,
5669
+ MONGOC_ERROR_PROTOCOL_INVALID_REPLY ,
5670
+ "create.encryptedFields.fields.keyId" );
5671
+ bson_destroy (& ccOpts );
5672
+
5673
+ bson_destroy (kmsProviders );
5674
+ mongoc_client_encryption_datakey_opts_destroy (dkOpts );
5675
+ mongoc_collection_destroy (coll );
5676
+ mongoc_database_drop (db , & error );
5677
+ mongoc_database_destroy (db );
5678
+ mongoc_client_encryption_destroy (ce );
5679
+ mongoc_client_destroy (client );
5680
+ }
5681
+
5553
5682
void
5554
5683
test_client_side_encryption_install (TestSuite * suite )
5555
5684
{
@@ -5858,9 +5987,29 @@ test_client_side_encryption_install (TestSuite *suite)
5858
5987
NULL ,
5859
5988
NULL );
5860
5989
5990
+ TestSuite_AddFull (
5991
+ suite ,
5992
+ "/client_side_encryption/createEncryptedCollection/simple" ,
5993
+ test_create_encrypted_collection_simple ,
5994
+ NULL ,
5995
+ NULL ,
5996
+ test_framework_skip_if_no_client_side_encryption ,
5997
+ test_framework_skip_if_max_wire_version_less_than_17 ,
5998
+ test_framework_skip_if_single );
5999
+
6000
+ TestSuite_AddFull (suite ,
6001
+ "/client_side_encryption/createEncryptedCollection/"
6002
+ "missing-encryptedFields" ,
6003
+ test_create_encrypted_collection_no_encryptedFields ,
6004
+ NULL ,
6005
+ NULL ,
6006
+ test_framework_skip_if_no_client_side_encryption ,
6007
+ test_framework_skip_if_max_wire_version_less_than_17 ,
6008
+ test_framework_skip_if_single );
5861
6009
TestSuite_AddFull (suite ,
5862
- "/client_side_encryption/createEncryptedCollection" ,
5863
- test_create_encrypted_collection ,
6010
+ "/client_side_encryption/createEncryptedCollection/"
6011
+ "bad-keyId" ,
6012
+ test_create_encrypted_collection_bad_keyId ,
5864
6013
NULL ,
5865
6014
NULL ,
5866
6015
test_framework_skip_if_no_client_side_encryption ,
0 commit comments