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..3015acba660 100644 --- a/src/libbson/tests/json/bson_binary_vector/packed_bit.json +++ b/src/libbson/tests/json/bson_binary_vector/packed_bit.json @@ -21,23 +21,32 @@ "canonical_bson": "1600000005766563746F7200040000000910007F0700" }, { - "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, diff --git a/src/libbson/tests/test-bson-vector.c b/src/libbson/tests/test-bson-vector.c index 81254810b04..c355ada92c7 100644 --- a/src/libbson/tests/test-bson-vector.c +++ b/src/libbson/tests/test-bson-vector.c @@ -123,6 +123,20 @@ 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 %zu with value 0x%02X included write to masked bits (reads as 0x%02X)", + i, + packed_byte, + packed_byte_check); + return false; + } } return true; } else { @@ -159,17 +173,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 +339,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;