Skip to content

Commit

Permalink
Move placeholder parsing tests to new test module
Browse files Browse the repository at this point in the history
  • Loading branch information
erwee committed Jan 14, 2025
1 parent 6600f41 commit f6a384c
Show file tree
Hide file tree
Showing 7 changed files with 454 additions and 299 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ set (TEST_MONGOCRYPT_SOURCES
test/test-gcp-auth.c
test/test-mc-cmp.c
test/test-mc-efc.c
test/test-mc-fle2-encryption-placeholder.c
test/test-mc-fle2-find-equality-payload-v2.c
test/test-mc-fle2-find-range-payload-v2.c
test/test-mc-fle2-payload-iev.c
Expand Down
1 change: 1 addition & 0 deletions src/mc-fle2-encryption-placeholder.c
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,7 @@ mc_FLE2PrefixInsertSpec_parse(mc_FLE2PrefixInsertSpec_t *out, const bson_iter_t
CLIENT_ERR_PREFIXED("upper bound cannot be less than the lower bound");
goto fail;
}
return true;
fail:
return false;
}
Expand Down
3 changes: 1 addition & 2 deletions test/test-mc-efc.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ static void _test_efc(_mongocrypt_tester_t *tester) {

{
_load_test_file(tester, "./test/data/efc/efc-textSearchFields.json", &efc_bson);
ASSERT_OK_STATUS(mc_EncryptedFieldConfig_parse(&efc, &efc_bson, status, use_range_v2),
status);
ASSERT_OK_STATUS(mc_EncryptedFieldConfig_parse(&efc, &efc_bson, status, use_range_v2), status);
ptr = efc.fields;
ASSERT(ptr);
ASSERT_STREQUAL(ptr->path, "lastName");
Expand Down
448 changes: 448 additions & 0 deletions test/test-mc-fle2-encryption-placeholder.c

Large diffs are not rendered by default.

297 changes: 0 additions & 297 deletions test/test-mongocrypt-ctx-encrypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -1615,301 +1615,6 @@ static void _test_encrypt_with_bypassqueryanalysis(_mongocrypt_tester_t *tester)
}
}

static void _test_FLE2EncryptionPlaceholder_parse(_mongocrypt_tester_t *tester) {
mc_FLE2EncryptionPlaceholder_t placeholder;
bson_t as_bson;
mongocrypt_status_t *status;
_mongocrypt_buffer_t buf;

status = mongocrypt_status_new();
_mongocrypt_buffer_copy_from_hex(&buf,
"03610000001074000100000010610002000000056b690010000000041234567812349876"
"1234123456789012056b75001000000004abcdefab123498761234123456789012027600"
"0900000076616c75653132330012636d00000000000000000000");
ASSERT(bson_init_static(&as_bson, buf.data + 1, buf.len - 1));
mc_FLE2EncryptionPlaceholder_init(&placeholder);
ASSERT_OK_STATUS(mc_FLE2EncryptionPlaceholder_parse(&placeholder, &as_bson, status), status);

ASSERT(placeholder.type == MONGOCRYPT_FLE2_PLACEHOLDER_TYPE_INSERT);
ASSERT(placeholder.algorithm == MONGOCRYPT_FLE2_ALGORITHM_EQUALITY);
ASSERT(BSON_ITER_HOLDS_UTF8(&placeholder.v_iter));
ASSERT_STREQUAL(bson_iter_utf8(&placeholder.v_iter, NULL), "value123");

_mongocrypt_buffer_t expect_index_key_id;
_mongocrypt_buffer_copy_from_hex(&expect_index_key_id, "12345678123498761234123456789012");
ASSERT_CMPBUF(placeholder.index_key_id, expect_index_key_id);
_mongocrypt_buffer_cleanup(&expect_index_key_id);

_mongocrypt_buffer_t expect_user_key_id;
_mongocrypt_buffer_copy_from_hex(&expect_user_key_id, "abcdefab123498761234123456789012");
ASSERT_CMPBUF(placeholder.user_key_id, expect_user_key_id);
_mongocrypt_buffer_cleanup(&expect_user_key_id);

ASSERT(placeholder.maxContentionFactor == 0);

mc_FLE2EncryptionPlaceholder_cleanup(&placeholder);
_mongocrypt_buffer_cleanup(&buf);
mongocrypt_status_destroy(status);
}

static void _test_FLE2EncryptionPlaceholder_range_parse(_mongocrypt_tester_t *tester) {
// Test type=MONGOCRYPT_FLE2_PLACEHOLDER_TYPE_INSERT.
{
mc_FLE2EncryptionPlaceholder_t placeholder;
bson_t as_bson;
mongocrypt_status_t *status;
_mongocrypt_buffer_t buf;

status = mongocrypt_status_new();
_mongocrypt_buffer_copy_from_hex(&buf,
"037d0000001074000100000010610003000000056b690010000000041234567812349"
"8761234123456789012056b75001000000004abcdefab123498761234123456789012"
"0376001e00000010760040e20100106d696e0000000000106d61780087d6120000126"
"36d000000000000000000127300010000000000000000");
ASSERT(bson_init_static(&as_bson, buf.data + 1, buf.len - 1));
mc_FLE2EncryptionPlaceholder_init(&placeholder);
ASSERT_OK_STATUS(mc_FLE2EncryptionPlaceholder_parse(&placeholder, &as_bson, status), status);

ASSERT(placeholder.type == MONGOCRYPT_FLE2_PLACEHOLDER_TYPE_INSERT);
ASSERT(placeholder.algorithm == MONGOCRYPT_FLE2_ALGORITHM_RANGE);

_mongocrypt_buffer_t expect_index_key_id;
_mongocrypt_buffer_copy_from_hex(&expect_index_key_id, "12345678123498761234123456789012");
ASSERT_CMPBUF(placeholder.index_key_id, expect_index_key_id);
_mongocrypt_buffer_cleanup(&expect_index_key_id);

_mongocrypt_buffer_t expect_user_key_id;
_mongocrypt_buffer_copy_from_hex(&expect_user_key_id, "abcdefab123498761234123456789012");
ASSERT_CMPBUF(placeholder.user_key_id, expect_user_key_id);
_mongocrypt_buffer_cleanup(&expect_user_key_id);

ASSERT_CMPINT64(placeholder.sparsity, ==, 1);

// Parse FLE2RangeInsertSpec.
{
mc_FLE2RangeInsertSpec_t spec;

ASSERT_OK_STATUS(mc_FLE2RangeInsertSpec_parse(&spec, &placeholder.v_iter, false /* use_range_v2 */, status),
status);

ASSERT(BSON_ITER_HOLDS_INT32(&spec.v));
ASSERT_CMPINT32(bson_iter_int32(&spec.v), ==, 123456);

ASSERT(BSON_ITER_HOLDS_INT32(&spec.min));
ASSERT_CMPINT32(bson_iter_int32(&spec.min), ==, 0);

ASSERT(BSON_ITER_HOLDS_INT32(&spec.max));
ASSERT_CMPINT32(bson_iter_int32(&spec.max), ==, 1234567);
}

mc_FLE2EncryptionPlaceholder_cleanup(&placeholder);
_mongocrypt_buffer_cleanup(&buf);
mongocrypt_status_destroy(status);
}

// Test type=MONGOCRYPT_FLE2_PLACEHOLDER_TYPE_FIND.
{
mc_FLE2EncryptionPlaceholder_t placeholder;
bson_t as_bson;
mongocrypt_status_t *status;
_mongocrypt_buffer_t buf;

status = mongocrypt_status_new();
_mongocrypt_buffer_copy_from_hex(&buf,
"03ec0000001074000200000010610003000000056b690010000000041234567812349"
"8761234123456789012056b75001000000004abcdefab123498761234123456789012"
"0376008d000000036564676573496e666f005b000000106c6f776572426f756e64000"
"0000000086c62496e636c756465640001107570706572426f756e640087d612000875"
"62496e636c75646564000110696e6465784d696e000000000010696e6465784d61780"
"087d6120000107061796c6f6164496400d20400001066697273744f70657261746f72"
"00010000000012636d000000000000000000127300010000000000000000");
ASSERT(bson_init_static(&as_bson, buf.data + 1, buf.len - 1));
mc_FLE2EncryptionPlaceholder_init(&placeholder);
ASSERT_OK_STATUS(mc_FLE2EncryptionPlaceholder_parse(&placeholder, &as_bson, status), status);

ASSERT(placeholder.type == MONGOCRYPT_FLE2_PLACEHOLDER_TYPE_FIND);
ASSERT(placeholder.algorithm == MONGOCRYPT_FLE2_ALGORITHM_RANGE);

_mongocrypt_buffer_t expect_index_key_id;
_mongocrypt_buffer_copy_from_hex(&expect_index_key_id, "12345678123498761234123456789012");
ASSERT_CMPBUF(placeholder.index_key_id, expect_index_key_id);
_mongocrypt_buffer_cleanup(&expect_index_key_id);

_mongocrypt_buffer_t expect_user_key_id;
_mongocrypt_buffer_copy_from_hex(&expect_user_key_id, "abcdefab123498761234123456789012");
ASSERT_CMPBUF(placeholder.user_key_id, expect_user_key_id);
_mongocrypt_buffer_cleanup(&expect_user_key_id);

ASSERT_CMPINT64(placeholder.sparsity, ==, 1);

// Parse FLE2RangeFindSpec.
{
mc_FLE2RangeFindSpec_t spec;

ASSERT_OK_STATUS(mc_FLE2RangeFindSpec_parse(&spec, &placeholder.v_iter, false /* use_range_v2 */, status),
status);

ASSERT(spec.edgesInfo.set);

ASSERT(BSON_ITER_HOLDS_INT32(&spec.edgesInfo.value.lowerBound));
ASSERT_CMPINT32(bson_iter_int32(&spec.edgesInfo.value.lowerBound), ==, 0);
ASSERT(spec.edgesInfo.value.lbIncluded);

ASSERT(BSON_ITER_HOLDS_INT32(&spec.edgesInfo.value.upperBound));
ASSERT_CMPINT32(bson_iter_int32(&spec.edgesInfo.value.upperBound), ==, 1234567);
ASSERT(spec.edgesInfo.value.ubIncluded);

ASSERT(BSON_ITER_HOLDS_INT32(&spec.edgesInfo.value.indexMin));
ASSERT_CMPINT32(bson_iter_int32(&spec.edgesInfo.value.indexMin), ==, 0);
ASSERT(spec.edgesInfo.value.ubIncluded);

ASSERT(BSON_ITER_HOLDS_INT32(&spec.edgesInfo.value.indexMax));
ASSERT_CMPINT32(bson_iter_int32(&spec.edgesInfo.value.indexMax), ==, 1234567);
ASSERT(spec.edgesInfo.value.ubIncluded);

ASSERT_CMPINT32(spec.payloadId, ==, 1234);

ASSERT_CMPINT(spec.firstOperator, ==, FLE2RangeOperator_kGt);
ASSERT_CMPINT(spec.secondOperator, ==, FLE2RangeOperator_kNone);
}

mc_FLE2EncryptionPlaceholder_cleanup(&placeholder);
_mongocrypt_buffer_cleanup(&buf);
mongocrypt_status_destroy(status);
}

// Test type=MONGOCRYPT_FLE2_PLACEHOLDER_TYPE_FIND with precision.
{
mc_FLE2EncryptionPlaceholder_t placeholder;
bson_t as_bson;
mongocrypt_status_t *status;
_mongocrypt_buffer_t buf;

status = mongocrypt_status_new();
_mongocrypt_buffer_copy_from_hex(&buf,
"030b0100001074000200000010610003000000056b690010000000041234567812349"
"8761234123456789012056b75001000000004abcdefab123498761234123456789012"
"037600ac000000036564676573496e666f007a000000016c6f776572426f756e64000"
"000000000000000086c62496e636c756465640001017570706572426f756e64000000"
"000000006940087562496e636c75646564000110707265636973696f6e00020000000"
"1696e6465784d696e00000000000000000001696e6465784d61780000000000000069"
"4000107061796c6f6164496400d20400001066697273744f70657261746f720001000"
"0000012636d000000000000000000127300010000000000000000");
ASSERT(bson_init_static(&as_bson, buf.data + 1, buf.len - 1));
mc_FLE2EncryptionPlaceholder_init(&placeholder);
ASSERT_OK_STATUS(mc_FLE2EncryptionPlaceholder_parse(&placeholder, &as_bson, status), status);

ASSERT(placeholder.type == MONGOCRYPT_FLE2_PLACEHOLDER_TYPE_FIND);
ASSERT(placeholder.algorithm == MONGOCRYPT_FLE2_ALGORITHM_RANGE);

_mongocrypt_buffer_t expect_index_key_id;
_mongocrypt_buffer_copy_from_hex(&expect_index_key_id, "12345678123498761234123456789012");
ASSERT_CMPBUF(placeholder.index_key_id, expect_index_key_id);
_mongocrypt_buffer_cleanup(&expect_index_key_id);

_mongocrypt_buffer_t expect_user_key_id;
_mongocrypt_buffer_copy_from_hex(&expect_user_key_id, "abcdefab123498761234123456789012");
ASSERT_CMPBUF(placeholder.user_key_id, expect_user_key_id);
_mongocrypt_buffer_cleanup(&expect_user_key_id);

ASSERT_CMPINT64(placeholder.sparsity, ==, 1);

// Parse FLE2RangeFindSpec.
{
mc_FLE2RangeFindSpec_t spec;

ASSERT_OK_STATUS(mc_FLE2RangeFindSpec_parse(&spec, &placeholder.v_iter, false /* use_range_v2 */, status),
status);

ASSERT(spec.edgesInfo.set);

ASSERT(BSON_ITER_HOLDS_DOUBLE(&spec.edgesInfo.value.lowerBound));
ASSERT_CMPDOUBLE(bson_iter_double(&spec.edgesInfo.value.lowerBound), ==, 0.0);
ASSERT(spec.edgesInfo.value.lbIncluded);

ASSERT(BSON_ITER_HOLDS_DOUBLE(&spec.edgesInfo.value.upperBound));
ASSERT_CMPDOUBLE(bson_iter_double(&spec.edgesInfo.value.upperBound), ==, 200.0);
ASSERT(spec.edgesInfo.value.ubIncluded);

ASSERT(BSON_ITER_HOLDS_DOUBLE(&spec.edgesInfo.value.indexMin));
ASSERT_CMPDOUBLE(bson_iter_double(&spec.edgesInfo.value.indexMin), ==, 0);
ASSERT(spec.edgesInfo.value.ubIncluded);

ASSERT(BSON_ITER_HOLDS_DOUBLE(&spec.edgesInfo.value.indexMax));
ASSERT_CMPDOUBLE(bson_iter_double(&spec.edgesInfo.value.indexMax), ==, 200.0);
ASSERT(spec.edgesInfo.value.ubIncluded);

ASSERT_CMPDOUBLE(spec.payloadId, ==, 1234);

ASSERT_CMPINT(spec.firstOperator, ==, FLE2RangeOperator_kGt);
ASSERT(spec.edgesInfo.value.precision.set);
ASSERT_CMPUINT32(spec.edgesInfo.value.precision.value, ==, 2);
}

mc_FLE2EncryptionPlaceholder_cleanup(&placeholder);
_mongocrypt_buffer_cleanup(&buf);
mongocrypt_status_destroy(status);
}

// Test type=MONGOCRYPT_FLE2_PLACEHOLDER_TYPE_INSERT with precision.
{
mc_FLE2EncryptionPlaceholder_t placeholder;
bson_t as_bson;
mongocrypt_status_t *status;
_mongocrypt_buffer_t buf;

status = mongocrypt_status_new();
_mongocrypt_buffer_copy_from_hex(&buf,
"03980000001074000100000010610003000000056b690010000000041234567812349"
"8761234123456789012056b75001000000004abcdefab123498761234123456789012"
"0376003900000001760077be9f1a2fdd5e40016d696e000000000000000000016d617"
"800000000000000694010707265636973696f6e00020000000012636d000000000000"
"000000127300010000000000000000");
ASSERT(bson_init_static(&as_bson, buf.data + 1, buf.len - 1));
mc_FLE2EncryptionPlaceholder_init(&placeholder);
ASSERT_OK_STATUS(mc_FLE2EncryptionPlaceholder_parse(&placeholder, &as_bson, status), status);

ASSERT(placeholder.type == MONGOCRYPT_FLE2_PLACEHOLDER_TYPE_INSERT);
ASSERT(placeholder.algorithm == MONGOCRYPT_FLE2_ALGORITHM_RANGE);

_mongocrypt_buffer_t expect_index_key_id;
_mongocrypt_buffer_copy_from_hex(&expect_index_key_id, "12345678123498761234123456789012");
ASSERT_CMPBUF(placeholder.index_key_id, expect_index_key_id);
_mongocrypt_buffer_cleanup(&expect_index_key_id);

_mongocrypt_buffer_t expect_user_key_id;
_mongocrypt_buffer_copy_from_hex(&expect_user_key_id, "abcdefab123498761234123456789012");
ASSERT_CMPBUF(placeholder.user_key_id, expect_user_key_id);
_mongocrypt_buffer_cleanup(&expect_user_key_id);

ASSERT_CMPINT64(placeholder.sparsity, ==, 1);

// Parse FLE2RangeInsertSpec.
{
mc_FLE2RangeInsertSpec_t spec;

ASSERT_OK_STATUS(mc_FLE2RangeInsertSpec_parse(&spec, &placeholder.v_iter, false /* use_range_v2 */, status),
status);

ASSERT(BSON_ITER_HOLDS_DOUBLE(&spec.v));
ASSERT_CMPDOUBLE(bson_iter_double(&spec.v), ==, 123.456);

ASSERT(BSON_ITER_HOLDS_DOUBLE(&spec.min));
ASSERT_CMPDOUBLE(bson_iter_double(&spec.min), ==, 0.0);

ASSERT(BSON_ITER_HOLDS_DOUBLE(&spec.max));
ASSERT_CMPDOUBLE(bson_iter_double(&spec.max), ==, 200.0);

ASSERT(spec.precision.set);
ASSERT_CMPUINT32(spec.precision.value, ==, 2);
}

mc_FLE2EncryptionPlaceholder_cleanup(&placeholder);
_mongocrypt_buffer_cleanup(&buf);
mongocrypt_status_destroy(status);
}
}

// Shared implementation for insert and find tests
typedef struct {
_mongocrypt_buffer_t buf;
Expand Down Expand Up @@ -5723,7 +5428,6 @@ void _mongocrypt_tester_install_ctx_encrypt(_mongocrypt_tester_t *tester) {
INSTALL_TEST(_test_encrypt_no_schema);
INSTALL_TEST(_test_encrypt_remote_encryptedfields);
INSTALL_TEST(_test_encrypt_with_bypassqueryanalysis);
INSTALL_TEST(_test_FLE2EncryptionPlaceholder_parse);
INSTALL_TEST(_test_encrypt_fle2_insert_payload);
INSTALL_TEST(_test_encrypt_fle2_find_payload);
INSTALL_TEST(_test_encrypt_fle2_unindexed_encrypted_payload);
Expand All @@ -5749,7 +5453,6 @@ void _mongocrypt_tester_install_ctx_encrypt(_mongocrypt_tester_t *tester) {
INSTALL_TEST(_test_encrypt_macos_no_ctr);
INSTALL_TEST(_test_fle1_collmod_with_jsonSchema);
INSTALL_TEST(_test_fle1_collmod_without_jsonSchema);
INSTALL_TEST(_test_FLE2EncryptionPlaceholder_range_parse);
INSTALL_TEST(_test_encrypt_fle2_insert_range_payload_int32);
INSTALL_TEST(_test_encrypt_fle2_insert_range_payload_int64);
INSTALL_TEST(_test_encrypt_fle2_insert_range_payload_date);
Expand Down
1 change: 1 addition & 0 deletions test/test-mongocrypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,7 @@ int main(int argc, char **argv) {
_mongocrypt_tester_install_efc(&tester);
_mongocrypt_tester_install_cleanup(&tester);
_mongocrypt_tester_install_compact(&tester);
_mongocrypt_tester_install_fle2_encryption_placeholder(&tester);
_mongocrypt_tester_install_fle2_payload_uev(&tester);
_mongocrypt_tester_install_fle2_payload_uev_v2(&tester);
_mongocrypt_tester_install_fle2_payload_iup(&tester);
Expand Down
2 changes: 2 additions & 0 deletions test/test-mongocrypt.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ void _mongocrypt_tester_install_cleanup(_mongocrypt_tester_t *tester);

void _mongocrypt_tester_install_compact(_mongocrypt_tester_t *tester);

void _mongocrypt_tester_install_fle2_encryption_placeholder(_mongocrypt_tester_t *tester);

void _mongocrypt_tester_install_fle2_payload_uev(_mongocrypt_tester_t *tester);

void _mongocrypt_tester_install_fle2_payload_uev_v2(_mongocrypt_tester_t *tester);
Expand Down

0 comments on commit f6a384c

Please sign in to comment.