diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2013-01-22 17:01:59 +0200 |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2013-01-22 17:01:59 +0200 |
commit | b1f4837430ad582db4a59b1a9a4ee7bc96280eac (patch) | |
tree | 6d796c8882c5d293a77ebb7a4221bb82e1b79a67 /Lib/test/test_gzip.py | |
parent | 3163f1e944df1a16621341c5948f50caf3f1ec89 (diff) | |
download | cpython-b1f4837430ad582db4a59b1a9a4ee7bc96280eac.tar.gz |
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_gzip.py')
-rw-r--r-- | Lib/test/test_gzip.py | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/Lib/test/test_gzip.py b/Lib/test/test_gzip.py index ced226f3dc..ba9d7dac8b 100644 --- a/Lib/test/test_gzip.py +++ b/Lib/test/test_gzip.py @@ -365,6 +365,21 @@ class TestGzip(unittest.TestCase): datac = gzip.compress(data) self.assertEqual(gzip.decompress(datac), data) + def test_read_truncated(self): + data = data1*50 + # Drop the CRC (4 bytes) and file size (4 bytes). + truncated = gzip.compress(data)[:-8] + with gzip.GzipFile(fileobj=io.BytesIO(truncated)) as f: + self.assertRaises(EOFError, f.read) + with gzip.GzipFile(fileobj=io.BytesIO(truncated)) as f: + self.assertEqual(f.read(len(data)), data) + self.assertRaises(EOFError, f.read, 1) + # Incomplete 10-byte header. + for i in range(2, 10): + with gzip.GzipFile(fileobj=io.BytesIO(truncated[:i])) as f: + self.assertRaises(EOFError, f.read, 1) + + def test_main(verbose=None): support.run_unittest(TestGzip) |