Skip to content

Commit 247a67f

Browse files
authored
CDRIVER-5979 Update spec tests for validation of unused packed_bit bits (#1984)
* Sync bson_binary_vector tests from commit 43d2c7bacd62249de8d2173bf8ee39e6fd7a686e * tests: Remove obsolete workarounds * tests: Modify append_vector_packed_bit_from_packed_array to treat masked writes as a conversion failure
1 parent 9f891f8 commit 247a67f

File tree

2 files changed

+29
-25
lines changed

2 files changed

+29
-25
lines changed

src/libbson/tests/json/bson_binary_vector/packed_bit.json

+15-6
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,32 @@
2121
"canonical_bson": "1600000005766563746F7200040000000910007F0700"
2222
},
2323
{
24-
"description": "Empty Vector PACKED_BIT",
24+
"description": "PACKED_BIT with padding",
2525
"valid": true,
26-
"vector": [],
26+
"vector": [127, 8],
2727
"dtype_hex": "0x10",
2828
"dtype_alias": "PACKED_BIT",
29-
"padding": 0,
30-
"canonical_bson": "1400000005766563746F72000200000009100000"
29+
"padding": 3,
30+
"canonical_bson": "1600000005766563746F7200040000000910037F0800"
3131
},
3232
{
33-
"description": "PACKED_BIT with padding",
34-
"valid": true,
33+
"description": "PACKED_BIT with inconsistent padding",
34+
"valid": false,
3535
"vector": [127, 7],
3636
"dtype_hex": "0x10",
3737
"dtype_alias": "PACKED_BIT",
3838
"padding": 3,
3939
"canonical_bson": "1600000005766563746F7200040000000910037F0700"
4040
},
41+
{
42+
"description": "Empty Vector PACKED_BIT",
43+
"valid": true,
44+
"vector": [],
45+
"dtype_hex": "0x10",
46+
"dtype_alias": "PACKED_BIT",
47+
"padding": 0,
48+
"canonical_bson": "1400000005766563746F72000200000009100000"
49+
},
4150
{
4251
"description": "Overflow Vector PACKED_BIT",
4352
"valid": false,

src/libbson/tests/test-bson-vector.c

+14-19
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,20 @@ append_vector_packed_bit_from_packed_array (
123123
ASSERT (bson_iter_next (&copy_iter));
124124
uint8_t packed_byte = (uint8_t) bson_iter_as_int64 (&copy_iter);
125125
ASSERT (bson_vector_packed_bit_view_write_packed (view, &packed_byte, 1, i));
126+
127+
// Read back the packed byte, interpret any masking as a conversion failure.
128+
uint8_t packed_byte_check;
129+
ASSERT (bson_vector_packed_bit_view_read_packed (view, &packed_byte_check, 1, i));
130+
if (packed_byte != packed_byte_check) {
131+
bson_set_error (error,
132+
TEST_ERROR_DOMAIN,
133+
TEST_ERROR_CODE,
134+
"byte at index %zu with value 0x%02X included write to masked bits (reads as 0x%02X)",
135+
i,
136+
packed_byte,
137+
packed_byte_check);
138+
return false;
139+
}
126140
}
127141
return true;
128142
} else {
@@ -159,17 +173,6 @@ test_bson_vector_json_case (vector_json_test_case_t *test_case)
159173
bson_error_t vector_from_array_error;
160174
bool vector_from_array_ok;
161175

162-
// (Spec test improvement TODO) Patch test cases that have unused bits set to '1' when '0' is required.
163-
if (0 == strcmp ("PACKED_BIT with padding", test_case->test_description)) {
164-
bson_iter_t iter;
165-
ASSERT (bson_iter_init_find (&iter, &expected_bson, test_case->scenario_test_key));
166-
uint32_t binary_len;
167-
uint8_t *binary;
168-
bson_iter_overwrite_binary (&iter, BSON_SUBTYPE_VECTOR, &binary_len, &binary);
169-
ASSERT (binary_len > BSON_VECTOR_HEADER_LEN);
170-
binary[binary_len - 1] &= (uint8_t) 0xFF << bson_vector_padding_from_header_byte_1 (binary[1]);
171-
}
172-
173176
// Try a format conversion from array to the indicated vector format.
174177
// The spec calls the first header byte "dtype" (combining the element type and element size fields)
175178
if (0 == strcmp ("0x03", test_case->test_dtype_hex_str)) {
@@ -336,14 +339,6 @@ test_bson_vector_json_case (vector_json_test_case_t *test_case)
336339
test_error ("test-vector array element %d has unexpected type, should be int.", (int) byte_count);
337340
}
338341

339-
// (Spec test improvement TODO) Packed writes can't set unused bits to '1' in libbson, but the spec
340-
// tests allow padding bits to take on undefined values. Modify the expected values to keep padding bits
341-
// zeroed.
342-
if (0 == strcmp ("PACKED_BIT with padding", test_case->test_description) &&
343-
byte_count == bson_vector_packed_bit_const_view_length_bytes (actual_view) - 1u) {
344-
expected_byte &= ((int64_t) 0xFF << *test_case->test_padding) & 0xFF;
345-
}
346-
347342
// Note, the zero initializer is only needed due to a false positive -Wmaybe-uninitialized warning in
348343
// uncommon configurations where the compiler does not have visibility into memcpy().
349344
uint8_t actual_byte = 0;

0 commit comments

Comments
 (0)