Skip to content

Commit e2e5f8b

Browse files
authored
MONGOCRYPT-812 add crypt_shared version in errors (#1130)
1 parent 2b2cd11 commit e2e5f8b

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

src/mongocrypt-ctx-encrypt.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,13 +508,23 @@ static bool _try_run_csfle_marking(mongocrypt_ctx_t *ctx) {
508508
goto fail_create_cmd;
509509
}
510510

511+
const char *version_str = ctx->crypt->csfle.get_version_str();
512+
// Examples:
513+
// mongo_crypt_v1-dev-8.0.0
514+
// mongo_crypt_v1-dev-8.1.0-alpha3-245-ge1c2344
515+
// Strip leading prefix.
516+
if (strstr(version_str, "mongo_crypt_v1-dev-") == version_str) {
517+
version_str += strlen("mongo_crypt_v1-dev-");
518+
}
519+
511520
#define CHECK_CSFLE_ERROR(Func, FailLabel) \
512521
if (1) { \
513522
if (csfle.status_get_error(status)) { \
514523
_mongocrypt_set_error(ctx->status, \
515524
MONGOCRYPT_STATUS_ERROR_CRYPT_SHARED, \
516525
MONGOCRYPT_GENERIC_ERROR_CODE, \
517-
"csfle " #Func " failed: %s [Error %d, code %d]", \
526+
"[crypt_shared %s] " #Func " failed: %s [Error %d, code %d]", \
527+
version_str, \
518528
csfle.status_get_explanation(status), \
519529
csfle.status_get_error(status), \
520530
csfle.status_get_code(status)); \

test/test-mongocrypt-csfle-lib.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "mongocrypt.h"
22

33
#include "mongocrypt-util-private.h"
4+
#include "test-mongocrypt-assert.h"
45
#include "test-mongocrypt.h"
56

67
static mongocrypt_t *get_test_mongocrypt(_mongocrypt_tester_t *tester) {
@@ -199,6 +200,31 @@ static void _test_loading_libmongocrypt_fails(_mongocrypt_tester_t *tester) {
199200
mongocrypt_destroy(crypt);
200201
}
201202

203+
static void _test_error_includes_version(_mongocrypt_tester_t *tester) {
204+
if (!TEST_MONGOCRYPT_HAVE_REAL_CRYPT_SHARED_LIB) {
205+
TEST_STDERR_PRINTF("No 'real' csfle library is available. The %s test is a no-op.\n", BSON_FUNC);
206+
return;
207+
}
208+
209+
mongocrypt_t *crypt = _mongocrypt_tester_mongocrypt(TESTER_MONGOCRYPT_WITH_CRYPT_SHARED_LIB);
210+
const char *version_str = crypt->csfle.get_version_str();
211+
// Strip leading mongo_crypt_v1-dev-
212+
if (strstr(version_str, "mongo_crypt_v1-dev-") == version_str) {
213+
version_str += strlen("mongo_crypt_v1-dev-");
214+
}
215+
216+
mongocrypt_ctx_t *ctx = mongocrypt_ctx_new(crypt);
217+
mongocrypt_binary_t *cmd = TEST_BSON(BSON_STR({"insert" : "test", "bad_field" : 1}));
218+
ASSERT_OK(mongocrypt_ctx_encrypt_init(ctx, "db", -1, cmd), ctx);
219+
ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_MONGO_COLLINFO);
220+
ASSERT_OK(mongocrypt_ctx_mongo_feed(ctx, TEST_FILE("./test/example/collection-info.json")), ctx);
221+
char *pattern = bson_strdup_printf("[crypt_shared %s] \"analyze_query\" failed", version_str);
222+
ASSERT_FAILS(mongocrypt_ctx_mongo_done(ctx), ctx, pattern);
223+
bson_free(pattern);
224+
mongocrypt_ctx_destroy(ctx);
225+
mongocrypt_destroy(crypt);
226+
}
227+
202228
void _mongocrypt_tester_install_csfle_lib(_mongocrypt_tester_t *tester) {
203229
INSTALL_TEST(_test_csfle_no_paths);
204230
INSTALL_TEST(_test_csfle_not_found);
@@ -212,4 +238,5 @@ void _mongocrypt_tester_install_csfle_lib(_mongocrypt_tester_t *tester) {
212238
INSTALL_TEST(_test_override_error_includes_reason);
213239
INSTALL_TEST(_test_lookup_version_check);
214240
INSTALL_TEST(_test_loading_libmongocrypt_fails);
241+
INSTALL_TEST(_test_error_includes_version);
215242
}

0 commit comments

Comments
 (0)