| 515 | ) |
| 516 | |
| 517 | def test_bad_code(self): |
| 518 | # Assert that decoding invalid Code with scope does not include a field name. |
| 519 | def generate_payload(length: int) -> bytes: |
| 520 | string_size = length - 0x1E |
| 521 | |
| 522 | return bytes.fromhex( |
| 523 | struct.pack("<I", length).hex() # payload size |
| 524 | + "0f" # type "code with scope" |
| 525 | + "3100" # key (cstring) |
| 526 | + "0a000000" # c_w_s_size |
| 527 | + "04000000" # code_size |
| 528 | + "41004200" # code (cstring) |
| 529 | + "feffffff" # scope_size |
| 530 | + "02" # type "string" |
| 531 | + "3200" # key (cstring) |
| 532 | + struct.pack("<I", string_size).hex() # string size |
| 533 | + "00" * string_size # value (cstring) |
| 534 | # next bytes is a field name for type \x00 |
| 535 | # type \x00 is invalid so bson throws an exception |
| 536 | ) |
| 537 | |
| 538 | for i in range(100): |
| 539 | payload = generate_payload(0x54F + i) |
| 540 | with self.assertRaisesRegex(InvalidBSON, "invalid") as ctx: |
| 541 | bson.decode(payload) |
| 542 | self.assertNotIn("fieldname", str(ctx.exception)) |
| 543 | |
| 544 | def test_unknown_type(self): |
| 545 | # Repr value differs with major python version |