mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
Issue #1159051: GzipFile now raises EOFError when reading a corrupted file
with truncated header or footer. Added tests for reading truncated gzip, bzip2, and lzma files.
This commit is contained in:
commit
57f9b7a124
5 changed files with 82 additions and 44 deletions
|
@ -577,6 +577,20 @@ class BZ2FileTest(BaseTest):
|
|||
bz2f.seek(-150, 1)
|
||||
self.assertEqual(bz2f.read(), self.TEXT[500-150:])
|
||||
|
||||
def test_read_truncated(self):
|
||||
# Drop the eos_magic field (6 bytes) and CRC (4 bytes).
|
||||
truncated = self.DATA[:-10]
|
||||
with BZ2File(BytesIO(truncated)) as f:
|
||||
self.assertRaises(EOFError, f.read)
|
||||
with BZ2File(BytesIO(truncated)) as f:
|
||||
self.assertEqual(f.read(len(self.TEXT)), self.TEXT)
|
||||
self.assertRaises(EOFError, f.read, 1)
|
||||
# Incomplete 4-byte file header, and block header of at least 146 bits.
|
||||
for i in range(22):
|
||||
with BZ2File(BytesIO(truncated[:i])) as f:
|
||||
self.assertRaises(EOFError, f.read, 1)
|
||||
|
||||
|
||||
class BZ2CompressorTest(BaseTest):
|
||||
def testCompress(self):
|
||||
bz2c = BZ2Compressor()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue