From 078479ca82c4dc407efe0d6718e444af911cbef3 Mon Sep 17 00:00:00 2001 From: Micah Scott Date: Fri, 4 Apr 2025 15:21:19 -0700 Subject: [PATCH 1/4] Update libmongoc for DRIVERS-3123 proposed change --- .../json/bson_binary_vector/packed_bit.json | 26 +++++++++----- src/libbson/tests/test-bson-vector.c | 34 ++++++++----------- 2 files changed, 33 insertions(+), 27 deletions(-) diff --git a/src/libbson/tests/json/bson_binary_vector/packed_bit.json b/src/libbson/tests/json/bson_binary_vector/packed_bit.json index a220e7e318e..aaa0a6b3dd7 100644 --- a/src/libbson/tests/json/bson_binary_vector/packed_bit.json +++ b/src/libbson/tests/json/bson_binary_vector/packed_bit.json @@ -14,30 +14,39 @@ { "description": "Simple Vector PACKED_BIT", "valid": true, - "vector": [127, 7], + "vector": [127, 8], "dtype_hex": "0x10", "dtype_alias": "PACKED_BIT", "padding": 0, - "canonical_bson": "1600000005766563746F7200040000000910007F0700" + "canonical_bson": "1600000005766563746F7200040000000910007F0800" }, { - "description": "Empty Vector PACKED_BIT", + "description": "PACKED_BIT with padding", "valid": true, - "vector": [], + "vector": [127, 8], "dtype_hex": "0x10", "dtype_alias": "PACKED_BIT", - "padding": 0, - "canonical_bson": "1400000005766563746F72000200000009100000" + "padding": 3, + "canonical_bson": "1600000005766563746F7200040000000910037F0800" }, { - "description": "PACKED_BIT with padding", - "valid": true, + "description": "PACKED_BIT with inconsistent padding", + "valid": false, "vector": [127, 7], "dtype_hex": "0x10", "dtype_alias": "PACKED_BIT", "padding": 3, "canonical_bson": "1600000005766563746F7200040000000910037F0700" }, + { + "description": "Empty Vector PACKED_BIT", + "valid": true, + "vector": [], + "dtype_hex": "0x10", + "dtype_alias": "PACKED_BIT", + "padding": 0, + "canonical_bson": "1400000005766563746F72000200000009100000" + }, { "description": "Overflow Vector PACKED_BIT", "valid": false, @@ -81,3 +90,4 @@ } ] } + diff --git a/src/libbson/tests/test-bson-vector.c b/src/libbson/tests/test-bson-vector.c index 81254810b04..d9e182a4d33 100644 --- a/src/libbson/tests/test-bson-vector.c +++ b/src/libbson/tests/test-bson-vector.c @@ -123,6 +123,21 @@ append_vector_packed_bit_from_packed_array ( ASSERT (bson_iter_next (©_iter)); uint8_t packed_byte = (uint8_t) bson_iter_as_int64 (©_iter); ASSERT (bson_vector_packed_bit_view_write_packed (view, &packed_byte, 1, i)); + + // Read back the packed byte, interpret any masking as a conversion failure. + uint8_t packed_byte_check; + ASSERT (bson_vector_packed_bit_view_read_packed (view, &packed_byte_check, 1, i)); + if (packed_byte != packed_byte_check) { + bson_set_error (error, + TEST_ERROR_DOMAIN, + TEST_ERROR_CODE, + "byte at index %" PRId64 + " with value 0x%02X included write to masked bits (reads as 0x%02X)", + (int64_t) i, + packed_byte, + packed_byte_check); + return false; + } } return true; } else { @@ -159,17 +174,6 @@ test_bson_vector_json_case (vector_json_test_case_t *test_case) bson_error_t vector_from_array_error; bool vector_from_array_ok; - // (Spec test improvement TODO) Patch test cases that have unused bits set to '1' when '0' is required. - if (0 == strcmp ("PACKED_BIT with padding", test_case->test_description)) { - bson_iter_t iter; - ASSERT (bson_iter_init_find (&iter, &expected_bson, test_case->scenario_test_key)); - uint32_t binary_len; - uint8_t *binary; - bson_iter_overwrite_binary (&iter, BSON_SUBTYPE_VECTOR, &binary_len, &binary); - ASSERT (binary_len > BSON_VECTOR_HEADER_LEN); - binary[binary_len - 1] &= (uint8_t) 0xFF << bson_vector_padding_from_header_byte_1 (binary[1]); - } - // Try a format conversion from array to the indicated vector format. // The spec calls the first header byte "dtype" (combining the element type and element size fields) if (0 == strcmp ("0x03", test_case->test_dtype_hex_str)) { @@ -336,14 +340,6 @@ test_bson_vector_json_case (vector_json_test_case_t *test_case) test_error ("test-vector array element %d has unexpected type, should be int.", (int) byte_count); } - // (Spec test improvement TODO) Packed writes can't set unused bits to '1' in libbson, but the spec - // tests allow padding bits to take on undefined values. Modify the expected values to keep padding bits - // zeroed. - if (0 == strcmp ("PACKED_BIT with padding", test_case->test_description) && - byte_count == bson_vector_packed_bit_const_view_length_bytes (actual_view) - 1u) { - expected_byte &= ((int64_t) 0xFF << *test_case->test_padding) & 0xFF; - } - // Note, the zero initializer is only needed due to a false positive -Wmaybe-uninitialized warning in // uncommon configurations where the compiler does not have visibility into memcpy(). uint8_t actual_byte = 0; From 663f28985997133ca33e37d3aeb126d2c6b8955b Mon Sep 17 00:00:00 2001 From: Micah Scott Date: Fri, 4 Apr 2025 17:48:28 -0700 Subject: [PATCH 2/4] Extra blank line --- src/libbson/tests/json/bson_binary_vector/packed_bit.json | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libbson/tests/json/bson_binary_vector/packed_bit.json b/src/libbson/tests/json/bson_binary_vector/packed_bit.json index aaa0a6b3dd7..88230956320 100644 --- a/src/libbson/tests/json/bson_binary_vector/packed_bit.json +++ b/src/libbson/tests/json/bson_binary_vector/packed_bit.json @@ -90,4 +90,3 @@ } ] } - From a5395bed614002f64b43d3327de19f696cd785bc Mon Sep 17 00:00:00 2001 From: mdbmes Date: Mon, 7 Apr 2025 09:30:07 -0700 Subject: [PATCH 3/4] Update src/libbson/tests/test-bson-vector.c Co-authored-by: Ezra Chung <88335979+eramongodb@users.noreply.github.com> --- src/libbson/tests/test-bson-vector.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/libbson/tests/test-bson-vector.c b/src/libbson/tests/test-bson-vector.c index d9e182a4d33..c355ada92c7 100644 --- a/src/libbson/tests/test-bson-vector.c +++ b/src/libbson/tests/test-bson-vector.c @@ -131,9 +131,8 @@ append_vector_packed_bit_from_packed_array ( bson_set_error (error, TEST_ERROR_DOMAIN, TEST_ERROR_CODE, - "byte at index %" PRId64 - " with value 0x%02X included write to masked bits (reads as 0x%02X)", - (int64_t) i, + "byte at index %zu with value 0x%02X included write to masked bits (reads as 0x%02X)", + i, packed_byte, packed_byte_check); return false; From cb99a37c31e169f9eb91d86c81e78698f239516d Mon Sep 17 00:00:00 2001 From: Micah Scott Date: Wed, 9 Apr 2025 10:01:31 -0700 Subject: [PATCH 4/4] Sync bson_binary_vector tests from commit 43d2c7bacd62249de8d2173bf8ee39e6fd7a686e --- src/libbson/tests/json/bson_binary_vector/packed_bit.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libbson/tests/json/bson_binary_vector/packed_bit.json b/src/libbson/tests/json/bson_binary_vector/packed_bit.json index 88230956320..3015acba660 100644 --- a/src/libbson/tests/json/bson_binary_vector/packed_bit.json +++ b/src/libbson/tests/json/bson_binary_vector/packed_bit.json @@ -14,11 +14,11 @@ { "description": "Simple Vector PACKED_BIT", "valid": true, - "vector": [127, 8], + "vector": [127, 7], "dtype_hex": "0x10", "dtype_alias": "PACKED_BIT", "padding": 0, - "canonical_bson": "1600000005766563746F7200040000000910007F0800" + "canonical_bson": "1600000005766563746F7200040000000910007F0700" }, { "description": "PACKED_BIT with padding",