MCPcopy Index your code
hub / github.com/mongodb/mongo-python-driver / test_invalid_decodes

Method test_invalid_decodes

test/test_bson.py:349–395  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

347 self.assertEqual(doc, decode(mm))
348
349 def test_invalid_decodes(self):
350 # Invalid object size (not enough bytes in document for even
351 # an object size of first object.
352 # NOTE: decode_all and decode_iter don't care, not sure if they should?
353 self.assertRaises(InvalidBSON, list, decode_file_iter(BytesIO(b"\x1B")))
354
355 bad_bsons = [
356 # An object size that's too small to even include the object size,
357 # but is correctly encoded, along with a correct EOO (and no data).
358 b"\x01\x00\x00\x00\x00",
359 # One object, but with object size listed smaller than it is in the
360 # data.
361 (
362 b"\x1A\x00\x00\x00\x0E\x74\x65\x73\x74"
363 b"\x00\x0C\x00\x00\x00\x68\x65\x6C\x6C"
364 b"\x6f\x20\x77\x6F\x72\x6C\x64\x00\x00"
365 b"\x05\x00\x00\x00\x00"
366 ),
367 # One object, missing the EOO at the end.
368 (
369 b"\x1B\x00\x00\x00\x0E\x74\x65\x73\x74"
370 b"\x00\x0C\x00\x00\x00\x68\x65\x6C\x6C"
371 b"\x6f\x20\x77\x6F\x72\x6C\x64\x00\x00"
372 b"\x05\x00\x00\x00"
373 ),
374 # One object, sized correctly, with a spot for an EOO, but the EOO
375 # isn't 0x00.
376 (
377 b"\x1B\x00\x00\x00\x0E\x74\x65\x73\x74"
378 b"\x00\x0C\x00\x00\x00\x68\x65\x6C\x6C"
379 b"\x6f\x20\x77\x6F\x72\x6C\x64\x00\x00"
380 b"\x05\x00\x00\x00\xFF"
381 ),
382 ]
383 for i, data in enumerate(bad_bsons):
384 msg = f"bad_bson[{i}]"
385 with self.assertRaises(InvalidBSON, msg=msg):
386 decode_all(data)
387 with self.assertRaises(InvalidBSON, msg=msg):
388 list(decode_iter(data))
389 with self.assertRaises(InvalidBSON, msg=msg):
390 list(decode_file_iter(BytesIO(data)))
391 with tempfile.TemporaryFile() as scratch:
392 scratch.write(data)
393 scratch.seek(0, os.SEEK_SET)
394 with self.assertRaises(InvalidBSON, msg=msg):
395 list(decode_file_iter(scratch))
396
397 def test_invalid_field_name(self):
398 # Decode a truncated field

Callers

nothing calls this directly

Calls 5

decode_file_iterFunction · 0.90
decode_allFunction · 0.90
decode_iterFunction · 0.90
writeMethod · 0.45
seekMethod · 0.45

Tested by

no test coverage detected