@@ -313,6 +313,8 @@ def test_decompress_incomplete_input(self):
313313 format = lzma .FORMAT_RAW , filters = FILTERS_RAW_4 )
314314
315315 def test_decompress_bad_input (self ):
316+ with self .assertRaises (LZMAError ):
317+ lzma .decompress (COMPRESSED_BOGUS )
316318 with self .assertRaises (LZMAError ):
317319 lzma .decompress (COMPRESSED_RAW_1 )
318320 with self .assertRaises (LZMAError ):
@@ -348,6 +350,16 @@ def test_decompress_multistream(self):
348350 ddata = lzma .decompress (COMPRESSED_XZ + COMPRESSED_ALONE )
349351 self .assertEqual (ddata , INPUT * 2 )
350352
353+ # Test robust handling of non-LZMA data following the compressed stream(s).
354+
355+ def test_decompress_trailing_junk (self ):
356+ ddata = lzma .decompress (COMPRESSED_XZ + COMPRESSED_BOGUS )
357+ self .assertEqual (ddata , INPUT )
358+
359+ def test_decompress_multistream_trailing_junk (self ):
360+ ddata = lzma .decompress (COMPRESSED_XZ * 3 + COMPRESSED_BOGUS )
361+ self .assertEqual (ddata , INPUT * 3 )
362+
351363
352364class TempFile :
353365 """Context manager - creates a file, and deletes it on __exit__."""
@@ -658,6 +670,14 @@ def test_read_multistream_buffer_size_aligned(self):
658670 finally :
659671 lzma ._BUFFER_SIZE = saved_buffer_size
660672
673+ def test_read_trailing_junk (self ):
674+ with LZMAFile (BytesIO (COMPRESSED_XZ + COMPRESSED_BOGUS )) as f :
675+ self .assertEqual (f .read (), INPUT )
676+
677+ def test_read_multistream_trailing_junk (self ):
678+ with LZMAFile (BytesIO (COMPRESSED_XZ * 5 + COMPRESSED_BOGUS )) as f :
679+ self .assertEqual (f .read (), INPUT * 5 )
680+
661681 def test_read_from_file (self ):
662682 with TempFile (TESTFN , COMPRESSED_XZ ):
663683 with LZMAFile (TESTFN ) as f :
@@ -687,6 +707,10 @@ def test_read_bad_args(self):
687707 with LZMAFile (BytesIO (COMPRESSED_XZ )) as f :
688708 self .assertRaises (TypeError , f .read , None )
689709
710+ def test_read_bad_data (self ):
711+ with LZMAFile (BytesIO (COMPRESSED_BOGUS )) as f :
712+ self .assertRaises (LZMAError , f .read )
713+
690714 def test_read1 (self ):
691715 with LZMAFile (BytesIO (COMPRESSED_XZ )) as f :
692716 blocks = []
@@ -1192,6 +1216,8 @@ def test_filter_properties_roundtrip(self):
11921216 Farewell.
11931217"""
11941218
1219+ COMPRESSED_BOGUS = b"this is not a valid lzma stream"
1220+
11951221COMPRESSED_XZ = (
11961222 b"\xfd 7zXZ\x00 \x00 \x04 \xe6 \xd6 \xb4 F\x02 \x00 !\x01 \x16 \x00 \x00 \x00 t/\xe5 \xa3 "
11971223 b"\xe0 \x07 \x80 \x03 \xdf ]\x00 \x05 \x14 \x07 bX\x19 \xcd \xdd n\x98 \x15 \xe4 \xb4 \x9d "
0 commit comments