Skip to content

Commit

Permalink
MONGOCRYPT-743 implement token _new_from_buffer_copy (#914)
Browse files Browse the repository at this point in the history
* implement new_from_buffer_copy

* add test

* initialize data buffer

* initialize buffer in test

* fix formatting error

* remove original_test_input
  • Loading branch information
joshbsiegel authored Nov 19, 2024
1 parent 7890557 commit 24fb3f5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/mc-tokens-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,10 @@
extern const _mongocrypt_buffer_t *BSON_CONCAT(Prefix, _get)(const T *t); \
/* Destructor */ \
extern void BSON_CONCAT(Prefix, _destroy)(T * t); \
/* Constructor for server to create tokens from raw buffer */ \
/* Constructor for server to shallow copy tokens from raw buffer */ \
extern T *BSON_CONCAT(Prefix, _new_from_buffer)(_mongocrypt_buffer_t * buf); \
/* Constructor for server to deep copy tokens from raw buffer */ \
extern T *BSON_CONCAT(Prefix, _new_from_buffer_copy)(_mongocrypt_buffer_t * buf); \
/* Constructor. Parameter list given as variadic args */ \
extern T *BSON_CONCAT(Prefix, _new)(_mongocrypt_crypto_t * crypto, __VA_ARGS__, mongocrypt_status_t * status)

Expand Down
10 changes: 9 additions & 1 deletion src/mc-tokens.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,21 @@
_mongocrypt_buffer_cleanup(&self->data); \
bson_free(self); \
} \
/* Constructor. From raw buffer */ \
/* Constructor. Shallow copy from raw buffer */ \
T *BSON_CONCAT(Prefix, _new_from_buffer)(_mongocrypt_buffer_t * buf) { \
BSON_ASSERT(buf->len == MONGOCRYPT_HMAC_SHA256_LEN); \
T *t = bson_malloc(sizeof(T)); \
_mongocrypt_buffer_set_to(buf, &t->data); \
return t; \
} \
/* Constructor. Deep copy from raw buffer */ \
T *BSON_CONCAT(Prefix, _new_from_buffer_copy)(_mongocrypt_buffer_t * buf) { \
BSON_ASSERT(buf->len == MONGOCRYPT_HMAC_SHA256_LEN); \
T *t = bson_malloc(sizeof(T)); \
_mongocrypt_buffer_init(&t->data); \
_mongocrypt_buffer_copy_to(buf, &t->data); \
return t; \
} \
/* Constructor. Parameter list given as variadic args. */ \
T *BSON_CONCAT(Prefix, _new)(_mongocrypt_crypto_t * crypto, __VA_ARGS__, mongocrypt_status_t * status)

Expand Down
17 changes: 12 additions & 5 deletions test/test-mc-tokens.c
Original file line number Diff line number Diff line change
Expand Up @@ -259,31 +259,38 @@ static void _test_mc_tokens_error(_mongocrypt_tester_t *tester) {
}

static void _test_mc_tokens_raw_buffer(_mongocrypt_tester_t *tester) {
mc_ServerDataEncryptionLevel1Token_t *token;
mc_ServerDataEncryptionLevel1Token_t *token1;
mc_ServerDataEncryptionLevel1Token_t *token2;
_mongocrypt_buffer_t test_input;
_mongocrypt_buffer_t expected;

_mongocrypt_buffer_copy_from_hex(&test_input, "6c6a349956c19f9c5e638e612011a71fbb71921edb540310c17cd0208b7f548b");

/* Make a token from a raw buffer */
token = mc_ServerDataEncryptionLevel1Token_new_from_buffer(&test_input);
token1 = mc_ServerDataEncryptionLevel1Token_new_from_buffer(&test_input);
token2 = mc_ServerDataEncryptionLevel1Token_new_from_buffer_copy(&test_input);

/* Assert new_from_buffer did not steal ownership. */
ASSERT(test_input.owned);
ASSERT(test_input.len == MONGOCRYPT_HMAC_SHA256_LEN);

_mongocrypt_buffer_copy_from_hex(&expected, "6c6a349956c19f9c5e638e612011a71fbb71921edb540310c17cd0208b7f548b");

ASSERT_CMPBUF(*mc_ServerDataEncryptionLevel1Token_get(token), expected);
ASSERT_CMPBUF(*mc_ServerDataEncryptionLevel1Token_get(token1), expected);
ASSERT_CMPBUF(*mc_ServerDataEncryptionLevel1Token_get(token2), expected);

/* Assert new_from_buffer references original buffer instead of a copy. */
test_input.data[0] = '0';
expected.data[0] = '0';
ASSERT_CMPBUF(*mc_ServerDataEncryptionLevel1Token_get(token), expected);
ASSERT_CMPBUF(*mc_ServerDataEncryptionLevel1Token_get(token1), expected);

// Assert new_from_buffer_copy references a new buffer.
ASSERT_CMPUINT8(mc_ServerDataEncryptionLevel1Token_get(token2)->data[0], !=, expected.data[0]);

_mongocrypt_buffer_cleanup(&test_input);
_mongocrypt_buffer_cleanup(&expected);
mc_ServerDataEncryptionLevel1Token_destroy(token);
mc_ServerDataEncryptionLevel1Token_destroy(token1);
mc_ServerDataEncryptionLevel1Token_destroy(token2);
}

void _mongocrypt_tester_install_mc_tokens(_mongocrypt_tester_t *tester) {
Expand Down

0 comments on commit 24fb3f5

Please sign in to comment.