diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2013-01-22 15:07:49 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2013-01-22 15:07:49 (GMT) |
commit | 57f9b7a12420d461e8ea5cc1ba63f80de778c7d5 (patch) | |
tree | c68dc61b4ff59cb81b20427786c4785c36472613 /Lib/test/test_gzip.py | |
parent | a80f761a6d23f42abc529a338477741ba4973168 (diff) | |
parent | 7c3922f44c226eac29a497648bbc3cc8702905a8 (diff) | |
download | cpython-57f9b7a12420d461e8ea5cc1ba63f80de778c7d5.zip cpython-57f9b7a12420d461e8ea5cc1ba63f80de778c7d5.tar.gz cpython-57f9b7a12420d461e8ea5cc1ba63f80de778c7d5.tar.bz2 |
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.
Diffstat (limited to 'Lib/test/test_gzip.py')
-rw-r--r-- | Lib/test/test_gzip.py | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/Lib/test/test_gzip.py b/Lib/test/test_gzip.py index af73953..ebd4c43 100644 --- a/Lib/test/test_gzip.py +++ b/Lib/test/test_gzip.py @@ -389,6 +389,20 @@ class TestGzip(BaseTest): 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) + class TestOpen(BaseTest): def test_binary_modes(self): |