# HG changeset patch # Parent dfe62f685538b7d9a895ed33c775b8b9cf312ef8 diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py --- a/Lib/test/test_tarfile.py +++ b/Lib/test/test_tarfile.py @@ -364,6 +364,33 @@ finally: tar.close() + def test_premature_eof1(self): + for size in (512, 600, 1024, 1200, 1536): + with tarfile.open(tmpname, "w:") as tar: + t = tarfile.TarInfo("foo") + t.size = 1024 + tar.addfile(t, io.BytesIO(b"a" * 1024)) + + os.truncate(tmpname, size) + + with tarfile.open(tmpname) as tar: + with self.assertRaises(Exception): + for t in tar: + pass + + def test_premature_eof2(self): + for size in (512, 600, 1024, 1200): + with tarfile.open(tmpname, "w:") as tar: + t = tarfile.TarInfo("foo") + t.size = 1024 + tar.addfile(t, io.BytesIO(b"a" * 1024)) + + os.truncate(tmpname, size) + + with tarfile.open(tmpname) as tar: + with self.assertRaises(Exception): + tar.extractfile(tar.next()).read() + class MiscReadTestBase(CommonReadTest): def requires_name_attribute(self):