Skip to content

Commit 5405116

Browse files
committed
add remaining test cases
1 parent 6046006 commit 5405116

File tree

1 file changed

+189
-0
lines changed

1 file changed

+189
-0
lines changed

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

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3569,6 +3569,168 @@ test_explicit_encryption_case2 (void *unused)
35693569
explicit_encryption_destroy (eef);
35703570
}
35713571

3572+
static void
3573+
test_explicit_encryption_case3 (void *unused)
3574+
{
3575+
/* Case 3: can insert encrypted unindexed */
3576+
bson_error_t error;
3577+
bool ok;
3578+
mongoc_client_encryption_encrypt_opts_t *eopts;
3579+
bson_value_t plaintext = {0};
3580+
3581+
ee_fixture *eef = explicit_encryption_setup ();
3582+
plaintext.value_type = BSON_TYPE_UTF8;
3583+
plaintext.value.v_utf8.str = "encrypted unindexed value";
3584+
plaintext.value.v_utf8.len = (uint32_t) strlen (plaintext.value.v_utf8.str);
3585+
3586+
/* Use ``encryptedClient`` to insert the document ``{ "_id": 1,
3587+
* "encryptedUnindexed": <insertPayload> }``. */
3588+
{
3589+
bson_value_t insertPayload;
3590+
bson_t to_insert = BSON_INITIALIZER;
3591+
3592+
eopts = mongoc_client_encryption_encrypt_opts_new ();
3593+
mongoc_client_encryption_encrypt_opts_set_keyid (eopts, &eef->key1ID);
3594+
mongoc_client_encryption_encrypt_opts_set_algorithm (
3595+
eopts, MONGOC_ENCRYPT_ALGORITHM_UNINDEXED);
3596+
3597+
ok = mongoc_client_encryption_encrypt (
3598+
eef->clientEncryption, &plaintext, eopts, &insertPayload, &error);
3599+
ASSERT_OR_PRINT (ok, error);
3600+
3601+
ASSERT (BSON_APPEND_INT32 (&to_insert, "_id", 1));
3602+
ASSERT (
3603+
BSON_APPEND_VALUE (&to_insert, "encryptedUnindexed", &insertPayload));
3604+
3605+
ok = mongoc_collection_insert_one (eef->encryptedColl,
3606+
&to_insert,
3607+
NULL /* opts */,
3608+
NULL /* reply */,
3609+
&error);
3610+
ASSERT_OR_PRINT (ok, error);
3611+
3612+
bson_value_destroy (&insertPayload);
3613+
bson_destroy (&to_insert);
3614+
mongoc_client_encryption_encrypt_opts_destroy (eopts);
3615+
}
3616+
3617+
/* Use ``encryptedClient`` to run a "find" operation on the
3618+
* ``db.explicit_encryption`` collection with the filter ``{ "_id": 1 }``. */
3619+
{
3620+
mongoc_cursor_t *cursor;
3621+
bson_t filter = BSON_INITIALIZER;
3622+
const bson_t *got;
3623+
3624+
ASSERT (BSON_APPEND_INT32 (&filter, "_id", 1));
3625+
3626+
cursor = mongoc_collection_find_with_opts (
3627+
eef->encryptedColl, &filter, NULL /* opts */, NULL /* read_prefs */);
3628+
ASSERT (mongoc_cursor_next (cursor, &got));
3629+
ASSERT_OR_PRINT (!mongoc_cursor_error (cursor, &error), error);
3630+
ASSERT_MATCH (got,
3631+
"{ 'encryptedUnindexed': 'encrypted unindexed value' }");
3632+
ASSERT (!mongoc_cursor_next (cursor, &got) &&
3633+
"expected one document to be returned, got more than one");
3634+
3635+
mongoc_cursor_destroy (cursor);
3636+
bson_destroy (&filter);
3637+
}
3638+
3639+
explicit_encryption_destroy (eef);
3640+
}
3641+
3642+
static void
3643+
test_explicit_encryption_case4 (void *unused)
3644+
{
3645+
/* Case 4: can roundtrip encrypted indexed */
3646+
bson_error_t error;
3647+
bool ok;
3648+
mongoc_client_encryption_encrypt_opts_t *eopts;
3649+
bson_value_t plaintext = {0};
3650+
bson_value_t payload;
3651+
3652+
ee_fixture *eef = explicit_encryption_setup ();
3653+
plaintext.value_type = BSON_TYPE_UTF8;
3654+
plaintext.value.v_utf8.str = "encrypted indexed value";
3655+
plaintext.value.v_utf8.len = (uint32_t) strlen (plaintext.value.v_utf8.str);
3656+
3657+
/* Use ``clientEncryption`` to encrypt the value "encrypted indexed value".
3658+
*/
3659+
{
3660+
eopts = mongoc_client_encryption_encrypt_opts_new ();
3661+
mongoc_client_encryption_encrypt_opts_set_keyid (eopts, &eef->key1ID);
3662+
mongoc_client_encryption_encrypt_opts_set_algorithm (
3663+
eopts, MONGOC_ENCRYPT_ALGORITHM_INDEXED);
3664+
3665+
ok = mongoc_client_encryption_encrypt (
3666+
eef->clientEncryption, &plaintext, eopts, &payload, &error);
3667+
ASSERT_OR_PRINT (ok, error);
3668+
3669+
mongoc_client_encryption_encrypt_opts_destroy (eopts);
3670+
}
3671+
3672+
/* Use ``clientEncryption`` to decrypt ``payload`` */
3673+
{
3674+
bson_value_t got;
3675+
3676+
ok = mongoc_client_encryption_decrypt (
3677+
eef->clientEncryption, &payload, &got, &error);
3678+
ASSERT_OR_PRINT (ok, error);
3679+
ASSERT (got.value_type == BSON_TYPE_UTF8);
3680+
ASSERT_CMPSTR (got.value.v_utf8.str, "encrypted indexed value");
3681+
bson_value_destroy (&got);
3682+
}
3683+
3684+
bson_value_destroy (&payload);
3685+
explicit_encryption_destroy (eef);
3686+
}
3687+
3688+
static void
3689+
test_explicit_encryption_case5 (void *unused)
3690+
{
3691+
/* Case 5: can roundtrip encrypted unindexed */
3692+
bson_error_t error;
3693+
bool ok;
3694+
mongoc_client_encryption_encrypt_opts_t *eopts;
3695+
bson_value_t plaintext = {0};
3696+
bson_value_t payload;
3697+
3698+
ee_fixture *eef = explicit_encryption_setup ();
3699+
plaintext.value_type = BSON_TYPE_UTF8;
3700+
plaintext.value.v_utf8.str = "encrypted unindexed value";
3701+
plaintext.value.v_utf8.len = (uint32_t) strlen (plaintext.value.v_utf8.str);
3702+
3703+
/* Use ``clientEncryption`` to encrypt the value "encrypted unindexed value".
3704+
*/
3705+
{
3706+
eopts = mongoc_client_encryption_encrypt_opts_new ();
3707+
mongoc_client_encryption_encrypt_opts_set_keyid (eopts, &eef->key1ID);
3708+
mongoc_client_encryption_encrypt_opts_set_algorithm (
3709+
eopts, MONGOC_ENCRYPT_ALGORITHM_UNINDEXED);
3710+
3711+
ok = mongoc_client_encryption_encrypt (
3712+
eef->clientEncryption, &plaintext, eopts, &payload, &error);
3713+
ASSERT_OR_PRINT (ok, error);
3714+
3715+
mongoc_client_encryption_encrypt_opts_destroy (eopts);
3716+
}
3717+
3718+
/* Use ``clientEncryption`` to decrypt ``payload`` */
3719+
{
3720+
bson_value_t got;
3721+
3722+
ok = mongoc_client_encryption_decrypt (
3723+
eef->clientEncryption, &payload, &got, &error);
3724+
ASSERT_OR_PRINT (ok, error);
3725+
ASSERT (got.value_type == BSON_TYPE_UTF8);
3726+
ASSERT_CMPSTR (got.value.v_utf8.str, "encrypted unindexed value");
3727+
bson_value_destroy (&got);
3728+
}
3729+
3730+
bson_value_destroy (&payload);
3731+
explicit_encryption_destroy (eef);
3732+
}
3733+
35723734
void
35733735
test_client_side_encryption_install (TestSuite *suite)
35743736
{
@@ -3733,4 +3895,31 @@ test_client_side_encryption_install (TestSuite *suite)
37333895
test_framework_skip_if_no_client_side_encryption,
37343896
test_framework_skip_if_max_wire_version_less_than_17,
37353897
test_framework_skip_if_single);
3898+
3899+
TestSuite_AddFull (suite,
3900+
"/client_side_encryption/explicit_encryption/case3",
3901+
test_explicit_encryption_case3,
3902+
NULL /* dtor */,
3903+
NULL /* ctx */,
3904+
test_framework_skip_if_no_client_side_encryption,
3905+
test_framework_skip_if_max_wire_version_less_than_17,
3906+
test_framework_skip_if_single);
3907+
3908+
TestSuite_AddFull (suite,
3909+
"/client_side_encryption/explicit_encryption/case4",
3910+
test_explicit_encryption_case4,
3911+
NULL /* dtor */,
3912+
NULL /* ctx */,
3913+
test_framework_skip_if_no_client_side_encryption,
3914+
test_framework_skip_if_max_wire_version_less_than_17,
3915+
test_framework_skip_if_single);
3916+
3917+
TestSuite_AddFull (suite,
3918+
"/client_side_encryption/explicit_encryption/case5",
3919+
test_explicit_encryption_case5,
3920+
NULL /* dtor */,
3921+
NULL /* ctx */,
3922+
test_framework_skip_if_no_client_side_encryption,
3923+
test_framework_skip_if_max_wire_version_less_than_17,
3924+
test_framework_skip_if_single);
37363925
}

0 commit comments

Comments
 (0)