summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_bz2.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2013-01-22 15:13:26 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2013-01-22 15:13:26 (GMT)
commit353e54edc42168e0f830a3f346040652831b8b85 (patch)
tree67b8a5a75763d5b1ecc2e5ae87afd54b4bb30247 /Lib/test/test_bz2.py
parent0f2bd027a82ef1a5885059cde3a5ef1ae502d106 (diff)
downloadcpython-353e54edc42168e0f830a3f346040652831b8b85.zip
cpython-353e54edc42168e0f830a3f346040652831b8b85.tar.gz
cpython-353e54edc42168e0f830a3f346040652831b8b85.tar.bz2
Issue #1159051: GzipFile now raises EOFError when reading a corrupted file
with truncated header or footer. Added tests for reading truncated gzip and bzip2 files.
Diffstat (limited to 'Lib/test/test_bz2.py')
-rw-r--r--Lib/test/test_bz2.py18
1 files changed, 18 insertions, 0 deletions
diff --git a/Lib/test/test_bz2.py b/Lib/test/test_bz2.py
index d41ce4d..fdab4e2 100644
--- a/Lib/test/test_bz2.py
+++ b/Lib/test/test_bz2.py
@@ -328,6 +328,24 @@ class BZ2FileTest(BaseTest):
self.assertRaises(ValueError, f.readline)
self.assertRaises(ValueError, f.readlines)
+ def test_read_truncated(self):
+ # Drop the eos_magic field (6 bytes) and CRC (4 bytes).
+ truncated = self.DATA[:-10]
+ with open(self.filename, 'wb') as f:
+ f.write(truncated)
+ with BZ2File(self.filename) as f:
+ self.assertRaises(EOFError, f.read)
+ with BZ2File(self.filename) 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 open(self.filename, 'wb') as f:
+ f.write(truncated[:i])
+ with BZ2File(self.filename) as f:
+ self.assertRaises(EOFError, f.read, 1)
+
+
class BZ2CompressorTest(BaseTest):
def testCompress(self):
# "Test BZ2Compressor.compress()/flush()"