Skip to content

Commit fe9739b

Browse files
committed
add explicit encryption prose test fixture
1 parent 340dbfb commit fe9739b

File tree

3 files changed

+222
-0
lines changed

3 files changed

+222
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"fields": [
3+
{
4+
"keyId": {
5+
"$binary": {
6+
"base64": "EjRWeBI0mHYSNBI0VniQEg==",
7+
"subType": "04"
8+
}
9+
},
10+
"path": "encryptedIndexed",
11+
"bsonType": "string",
12+
"queries": {
13+
"queryType": "equality",
14+
"contention": {
15+
"$numberLong": "0"
16+
}
17+
}
18+
},
19+
{
20+
"keyId": {
21+
"$binary": {
22+
"base64": "q83vqxI0mHYSNBI0VniQEg==",
23+
"subType": "04"
24+
}
25+
},
26+
"path": "encryptedUnindexed",
27+
"bsonType": "string"
28+
}
29+
]
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"_id": {
3+
"$binary": {
4+
"base64": "EjRWeBI0mHYSNBI0VniQEg==",
5+
"subType": "04"
6+
}
7+
},
8+
"keyMaterial": {
9+
"$binary": {
10+
"base64": "sHe0kz57YW7v8g9VP9sf/+K1ex4JqKc5rf/URX3n3p8XdZ6+15uXPaSayC6adWbNxkFskuMCOifDoTT+rkqMtFkDclOy884RuGGtUysq3X7zkAWYTKi8QAfKkajvVbZl2y23UqgVasdQu3OVBQCrH/xY00nNAs/52e958nVjBuzQkSb1T8pKJAyjZsHJ60+FtnfafDZSTAIBJYn7UWBCwQ==",
11+
"subType": "00"
12+
}
13+
},
14+
"creationDate": {
15+
"$date": {
16+
"$numberLong": "1648914851981"
17+
}
18+
},
19+
"updateDate": {
20+
"$date": {
21+
"$numberLong": "1648914851981"
22+
}
23+
},
24+
"status": {
25+
"$numberInt": "0"
26+
},
27+
"masterKey": {
28+
"provider": "local"
29+
}
30+
}

src/libmongoc/tests/test-mongoc-client-side-encryption.c

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
/* _mongoc_host_list_from_string_with_err */
2121
#include "mongoc/mongoc-host-list-private.h"
2222

23+
/* MONGOC_SERVER_ERR_NS_NOT_FOUND */
24+
#include "mongoc/mongoc-error-private.h"
25+
2326
#include "mongoc/mongoc-uri.h"
2427

2528
static void
@@ -3197,6 +3200,156 @@ test_kms_tls_options_extra_rejected (void *unused)
31973200
mongoc_client_destroy (keyvault_client);
31983201
}
31993202

3203+
/* ee_fixture is a fixture for the Explicit Encryption prose test. */
3204+
typedef struct {
3205+
bson_value_t key1ID;
3206+
mongoc_client_t *keyVaultClient;
3207+
mongoc_client_encryption_t *clientEncryption;
3208+
mongoc_client_t *encryptedClient;
3209+
} ee_fixture;
3210+
3211+
static ee_fixture *
3212+
explicit_encryption_setup (void)
3213+
{
3214+
ee_fixture *eef = (ee_fixture *) bson_malloc0 (sizeof (ee_fixture));
3215+
bson_t *encryptedFields = get_bson_from_json_file (
3216+
"./src/libmongoc/tests/client_side_encryption_prose/explicit_encryption/"
3217+
"encryptedFields.json");
3218+
bson_t *key1Document = get_bson_from_json_file (
3219+
"./src/libmongoc/tests/client_side_encryption_prose/explicit_encryption/"
3220+
"key1-document.json");
3221+
mongoc_client_t *setupClient = test_framework_new_default_client ();
3222+
3223+
3224+
/* Read the ``"_id"`` field of ``key1Document`` as ``key1ID``. */
3225+
{
3226+
bson_iter_t iter;
3227+
const bson_value_t *value;
3228+
3229+
ASSERT (bson_iter_init_find (&iter, key1Document, "_id"));
3230+
value = bson_iter_value (&iter);
3231+
bson_value_copy (value, &eef->key1ID);
3232+
}
3233+
3234+
/* Drop and create the collection ``db.explicit_encryption`` using
3235+
* ``encryptedFields`` as an option. */
3236+
{
3237+
mongoc_database_t *db = mongoc_client_get_database (setupClient, "db");
3238+
mongoc_collection_t *coll =
3239+
mongoc_database_get_collection (db, "explicit_encryption");
3240+
bson_error_t error;
3241+
bson_t *opts;
3242+
3243+
opts = BCON_NEW ("encryptedFields", BCON_DOCUMENT (encryptedFields));
3244+
3245+
if (!mongoc_collection_drop_with_opts (coll, opts, &error)) {
3246+
if (error.code != MONGOC_SERVER_ERR_NS_NOT_FOUND) {
3247+
test_error ("unexpected error in drop: %s", error.message);
3248+
}
3249+
}
3250+
mongoc_collection_destroy (coll);
3251+
3252+
coll = mongoc_database_create_collection (
3253+
db, "explicit_encryption", opts, &error);
3254+
ASSERT_OR_PRINT (coll, error);
3255+
3256+
mongoc_collection_destroy (coll);
3257+
bson_destroy (opts);
3258+
mongoc_database_destroy (db);
3259+
}
3260+
3261+
/* Drop and create the collection ``keyvault.datakeys``. */
3262+
{
3263+
mongoc_database_t *db =
3264+
mongoc_client_get_database (setupClient, "keyvault");
3265+
mongoc_collection_t *coll =
3266+
mongoc_database_get_collection (db, "datakeys");
3267+
bson_error_t error;
3268+
3269+
if (!mongoc_collection_drop (coll, &error)) {
3270+
if (error.code != MONGOC_SERVER_ERR_NS_NOT_FOUND) {
3271+
test_error ("unexpected error in drop: %s", error.message);
3272+
}
3273+
}
3274+
mongoc_collection_destroy (coll);
3275+
3276+
coll = mongoc_database_create_collection (
3277+
db, "datakeys", NULL /* opts */, &error);
3278+
ASSERT_OR_PRINT (coll, error);
3279+
3280+
mongoc_collection_destroy (coll);
3281+
mongoc_database_destroy (db);
3282+
}
3283+
3284+
eef->keyVaultClient = test_framework_new_default_client ();
3285+
3286+
/* Create a ClientEncryption object named ``clientEncryption`` */
3287+
{
3288+
mongoc_client_encryption_opts_t *ceOpts =
3289+
mongoc_client_encryption_opts_new ();
3290+
bson_t *kms_providers = _make_local_kms_provider (NULL);
3291+
bson_error_t error;
3292+
3293+
mongoc_client_encryption_opts_set_keyvault_client (ceOpts,
3294+
eef->keyVaultClient);
3295+
mongoc_client_encryption_opts_set_keyvault_namespace (
3296+
ceOpts, "keyvault", "datakeys");
3297+
mongoc_client_encryption_opts_set_kms_providers (ceOpts, kms_providers);
3298+
3299+
eef->clientEncryption = mongoc_client_encryption_new (ceOpts, &error);
3300+
ASSERT_OR_PRINT (eef->clientEncryption, error);
3301+
3302+
bson_destroy (kms_providers);
3303+
mongoc_client_encryption_opts_destroy (ceOpts);
3304+
}
3305+
3306+
/* Create a MongoClient named ``encryptedClient``. */
3307+
{
3308+
mongoc_auto_encryption_opts_t *aeOpts =
3309+
mongoc_auto_encryption_opts_new ();
3310+
bson_t *kms_providers = _make_local_kms_provider (NULL);
3311+
bson_error_t error;
3312+
3313+
mongoc_auto_encryption_opts_set_keyvault_namespace (
3314+
aeOpts, "keyvault", "datakeys");
3315+
mongoc_auto_encryption_opts_set_kms_providers (aeOpts, kms_providers);
3316+
mongoc_auto_encryption_opts_set_bypass_query_analysis (aeOpts, true);
3317+
eef->encryptedClient = test_framework_new_default_client ();
3318+
ASSERT_OR_PRINT (mongoc_client_enable_auto_encryption (
3319+
eef->encryptedClient, aeOpts, &error),
3320+
error);
3321+
3322+
bson_destroy (kms_providers);
3323+
mongoc_auto_encryption_opts_destroy (aeOpts);
3324+
}
3325+
3326+
mongoc_client_destroy (setupClient);
3327+
bson_destroy (key1Document);
3328+
bson_destroy (encryptedFields);
3329+
return eef;
3330+
}
3331+
3332+
static void
3333+
explicit_encryption_destroy (ee_fixture *eef)
3334+
{
3335+
if (!eef) {
3336+
return;
3337+
}
3338+
3339+
mongoc_client_destroy (eef->encryptedClient);
3340+
mongoc_client_encryption_destroy (eef->clientEncryption);
3341+
mongoc_client_destroy (eef->keyVaultClient);
3342+
bson_value_destroy (&eef->key1ID);
3343+
bson_free (eef);
3344+
}
3345+
3346+
static void
3347+
test_explicit_encryption (void *unused)
3348+
{
3349+
ee_fixture *eef = explicit_encryption_setup ();
3350+
explicit_encryption_destroy (eef);
3351+
}
3352+
32003353
void
32013354
test_client_side_encryption_install (TestSuite *suite)
32023355
{
@@ -3343,4 +3496,13 @@ test_client_side_encryption_install (TestSuite *suite)
33433496
NULL,
33443497
NULL,
33453498
test_framework_skip_if_no_client_side_encryption);
3499+
3500+
TestSuite_AddFull (suite,
3501+
"/client_side_encryption/explicit_encryption",
3502+
test_explicit_encryption,
3503+
NULL /* dtor */,
3504+
NULL /* ctx */,
3505+
test_framework_skip_if_no_client_side_encryption,
3506+
test_framework_skip_if_max_wire_version_less_than_17,
3507+
test_framework_skip_if_single);
33463508
}

0 commit comments

Comments
 (0)