Skip to content

Commit 24fb3f5

Browse files
authored
MONGOCRYPT-743 implement token _new_from_buffer_copy (#914)
* implement new_from_buffer_copy * add test * initialize data buffer * initialize buffer in test * fix formatting error * remove original_test_input
1 parent 7890557 commit 24fb3f5

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

src/mc-tokens-private.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,10 @@
8989
extern const _mongocrypt_buffer_t *BSON_CONCAT(Prefix, _get)(const T *t); \
9090
/* Destructor */ \
9191
extern void BSON_CONCAT(Prefix, _destroy)(T * t); \
92-
/* Constructor for server to create tokens from raw buffer */ \
92+
/* Constructor for server to shallow copy tokens from raw buffer */ \
9393
extern T *BSON_CONCAT(Prefix, _new_from_buffer)(_mongocrypt_buffer_t * buf); \
94+
/* Constructor for server to deep copy tokens from raw buffer */ \
95+
extern T *BSON_CONCAT(Prefix, _new_from_buffer_copy)(_mongocrypt_buffer_t * buf); \
9496
/* Constructor. Parameter list given as variadic args */ \
9597
extern T *BSON_CONCAT(Prefix, _new)(_mongocrypt_crypto_t * crypto, __VA_ARGS__, mongocrypt_status_t * status)
9698

src/mc-tokens.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,21 @@
3838
_mongocrypt_buffer_cleanup(&self->data); \
3939
bson_free(self); \
4040
} \
41-
/* Constructor. From raw buffer */ \
41+
/* Constructor. Shallow copy from raw buffer */ \
4242
T *BSON_CONCAT(Prefix, _new_from_buffer)(_mongocrypt_buffer_t * buf) { \
4343
BSON_ASSERT(buf->len == MONGOCRYPT_HMAC_SHA256_LEN); \
4444
T *t = bson_malloc(sizeof(T)); \
4545
_mongocrypt_buffer_set_to(buf, &t->data); \
4646
return t; \
4747
} \
48+
/* Constructor. Deep copy from raw buffer */ \
49+
T *BSON_CONCAT(Prefix, _new_from_buffer_copy)(_mongocrypt_buffer_t * buf) { \
50+
BSON_ASSERT(buf->len == MONGOCRYPT_HMAC_SHA256_LEN); \
51+
T *t = bson_malloc(sizeof(T)); \
52+
_mongocrypt_buffer_init(&t->data); \
53+
_mongocrypt_buffer_copy_to(buf, &t->data); \
54+
return t; \
55+
} \
4856
/* Constructor. Parameter list given as variadic args. */ \
4957
T *BSON_CONCAT(Prefix, _new)(_mongocrypt_crypto_t * crypto, __VA_ARGS__, mongocrypt_status_t * status)
5058

test/test-mc-tokens.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -259,31 +259,38 @@ static void _test_mc_tokens_error(_mongocrypt_tester_t *tester) {
259259
}
260260

261261
static void _test_mc_tokens_raw_buffer(_mongocrypt_tester_t *tester) {
262-
mc_ServerDataEncryptionLevel1Token_t *token;
262+
mc_ServerDataEncryptionLevel1Token_t *token1;
263+
mc_ServerDataEncryptionLevel1Token_t *token2;
263264
_mongocrypt_buffer_t test_input;
264265
_mongocrypt_buffer_t expected;
265266

266267
_mongocrypt_buffer_copy_from_hex(&test_input, "6c6a349956c19f9c5e638e612011a71fbb71921edb540310c17cd0208b7f548b");
267268

268269
/* Make a token from a raw buffer */
269-
token = mc_ServerDataEncryptionLevel1Token_new_from_buffer(&test_input);
270+
token1 = mc_ServerDataEncryptionLevel1Token_new_from_buffer(&test_input);
271+
token2 = mc_ServerDataEncryptionLevel1Token_new_from_buffer_copy(&test_input);
270272

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

275277
_mongocrypt_buffer_copy_from_hex(&expected, "6c6a349956c19f9c5e638e612011a71fbb71921edb540310c17cd0208b7f548b");
276278

277-
ASSERT_CMPBUF(*mc_ServerDataEncryptionLevel1Token_get(token), expected);
279+
ASSERT_CMPBUF(*mc_ServerDataEncryptionLevel1Token_get(token1), expected);
280+
ASSERT_CMPBUF(*mc_ServerDataEncryptionLevel1Token_get(token2), expected);
278281

279282
/* Assert new_from_buffer references original buffer instead of a copy. */
280283
test_input.data[0] = '0';
281284
expected.data[0] = '0';
282-
ASSERT_CMPBUF(*mc_ServerDataEncryptionLevel1Token_get(token), expected);
285+
ASSERT_CMPBUF(*mc_ServerDataEncryptionLevel1Token_get(token1), expected);
286+
287+
// Assert new_from_buffer_copy references a new buffer.
288+
ASSERT_CMPUINT8(mc_ServerDataEncryptionLevel1Token_get(token2)->data[0], !=, expected.data[0]);
283289

284290
_mongocrypt_buffer_cleanup(&test_input);
285291
_mongocrypt_buffer_cleanup(&expected);
286-
mc_ServerDataEncryptionLevel1Token_destroy(token);
292+
mc_ServerDataEncryptionLevel1Token_destroy(token1);
293+
mc_ServerDataEncryptionLevel1Token_destroy(token2);
287294
}
288295

289296
void _mongocrypt_tester_install_mc_tokens(_mongocrypt_tester_t *tester) {

0 commit comments

Comments
 (0)