Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/mongocrypt-ctx-encrypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -508,13 +508,23 @@ static bool _try_run_csfle_marking(mongocrypt_ctx_t *ctx) {
goto fail_create_cmd;
}

const char *version_str = ctx->crypt->csfle.get_version_str();
// Examples:
// mongo_crypt_v1-dev-8.0.0
// mongo_crypt_v1-dev-8.1.0-alpha3-245-ge1c2344
// Strip leading prefix.
if (strstr(version_str, "mongo_crypt_v1-dev-") == version_str) {
version_str += strlen("mongo_crypt_v1-dev-");
}

#define CHECK_CSFLE_ERROR(Func, FailLabel) \
if (1) { \
if (csfle.status_get_error(status)) { \
_mongocrypt_set_error(ctx->status, \
MONGOCRYPT_STATUS_ERROR_CRYPT_SHARED, \
MONGOCRYPT_GENERIC_ERROR_CODE, \
"csfle " #Func " failed: %s [Error %d, code %d]", \
"[crypt_shared %s] " #Func " failed: %s [Error %d, code %d]", \
version_str, \
csfle.status_get_explanation(status), \
csfle.status_get_error(status), \
csfle.status_get_code(status)); \
Expand Down
27 changes: 27 additions & 0 deletions test/test-mongocrypt-csfle-lib.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "mongocrypt.h"

#include "mongocrypt-util-private.h"
#include "test-mongocrypt-assert.h"
#include "test-mongocrypt.h"

static mongocrypt_t *get_test_mongocrypt(_mongocrypt_tester_t *tester) {
Expand Down Expand Up @@ -199,6 +200,31 @@ static void _test_loading_libmongocrypt_fails(_mongocrypt_tester_t *tester) {
mongocrypt_destroy(crypt);
}

static void _test_error_includes_version(_mongocrypt_tester_t *tester) {
if (!TEST_MONGOCRYPT_HAVE_REAL_CRYPT_SHARED_LIB) {
TEST_STDERR_PRINTF("No 'real' csfle library is available. The %s test is a no-op.\n", BSON_FUNC);
return;
}

mongocrypt_t *crypt = _mongocrypt_tester_mongocrypt(TESTER_MONGOCRYPT_WITH_CRYPT_SHARED_LIB);
const char *version_str = crypt->csfle.get_version_str();
// Strip leading mongo_crypt_v1-dev-
if (strstr(version_str, "mongo_crypt_v1-dev-") == version_str) {
version_str += strlen("mongo_crypt_v1-dev-");
}

mongocrypt_ctx_t *ctx = mongocrypt_ctx_new(crypt);
mongocrypt_binary_t *cmd = TEST_BSON(BSON_STR({"insert" : "test", "bad_field" : 1}));
ASSERT_OK(mongocrypt_ctx_encrypt_init(ctx, "db", -1, cmd), ctx);
ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_MONGO_COLLINFO);
ASSERT_OK(mongocrypt_ctx_mongo_feed(ctx, TEST_FILE("./test/example/collection-info.json")), ctx);
char *pattern = bson_strdup_printf("[crypt_shared %s] \"analyze_query\" failed", version_str);
ASSERT_FAILS(mongocrypt_ctx_mongo_done(ctx), ctx, pattern);
bson_free(pattern);
mongocrypt_ctx_destroy(ctx);
mongocrypt_destroy(crypt);
}

void _mongocrypt_tester_install_csfle_lib(_mongocrypt_tester_t *tester) {
INSTALL_TEST(_test_csfle_no_paths);
INSTALL_TEST(_test_csfle_not_found);
Expand All @@ -212,4 +238,5 @@ void _mongocrypt_tester_install_csfle_lib(_mongocrypt_tester_t *tester) {
INSTALL_TEST(_test_override_error_includes_reason);
INSTALL_TEST(_test_lookup_version_check);
INSTALL_TEST(_test_loading_libmongocrypt_fails);
INSTALL_TEST(_test_error_includes_version);
}