diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2010-08-12 15:30:13 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2010-08-12 15:30:13 (GMT) |
commit | 5f2a7bce2ed3dce23b9e167e17746ba3dbab9d58 (patch) | |
tree | 66e3b9a98c8c52f94af8a0fd3d66ff3d26ccdd32 /Lib/test | |
parent | 595c8d34a340fa2f381d9b0e4b6a09e2accc6898 (diff) | |
download | cpython-5f2a7bce2ed3dce23b9e167e17746ba3dbab9d58.zip cpython-5f2a7bce2ed3dce23b9e167e17746ba3dbab9d58.tar.gz cpython-5f2a7bce2ed3dce23b9e167e17746ba3dbab9d58.tar.bz2 |
Merged revisions 83959-83960 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
........
r83959 | antoine.pitrou | 2010-08-12 17:11:50 +0200 (jeu., 12 août 2010) | 5 lines
Issue #7467: when a file from a ZIP archive, its CRC is checked and a
BadZipfile error is raised if it doesn't match (as used to be the
case in Python 2.5 and earlier).
........
r83960 | antoine.pitrou | 2010-08-12 17:15:01 +0200 (jeu., 12 août 2010) | 3 lines
Typo.
........
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_zipfile.py | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py index 55cfaa9..474ec58 100644 --- a/Lib/test/test_zipfile.py +++ b/Lib/test/test_zipfile.py @@ -582,6 +582,27 @@ class PyZipFileTests(unittest.TestCase): class OtherTests(unittest.TestCase): + zips_with_bad_crc = { + zipfile.ZIP_STORED: ( + b'PK\003\004\024\0\0\0\0\0 \213\212;:r' + b'\253\377\f\0\0\0\f\0\0\0\005\0\0\000af' + b'ilehello,AworldP' + b'K\001\002\024\003\024\0\0\0\0\0 \213\212;:' + b'r\253\377\f\0\0\0\f\0\0\0\005\0\0\0\0' + b'\0\0\0\0\0\0\0\200\001\0\0\0\000afi' + b'lePK\005\006\0\0\0\0\001\0\001\0003\000' + b'\0\0/\0\0\0\0\0'), + zipfile.ZIP_DEFLATED: ( + b'PK\x03\x04\x14\x00\x00\x00\x08\x00n}\x0c=FA' + b'KE\x10\x00\x00\x00n\x00\x00\x00\x05\x00\x00\x00af' + b'ile\xcbH\xcd\xc9\xc9W(\xcf/\xcaI\xc9\xa0' + b'=\x13\x00PK\x01\x02\x14\x03\x14\x00\x00\x00\x08\x00n' + b'}\x0c=FAKE\x10\x00\x00\x00n\x00\x00\x00\x05' + b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x01\x00\x00\x00' + b'\x00afilePK\x05\x06\x00\x00\x00\x00\x01\x00' + b'\x01\x003\x00\x00\x003\x00\x00\x00\x00\x00'), + } + def testUnicodeFilenames(self): zf = zipfile.ZipFile(TESTFN, "w") zf.writestr("foo.txt", "Test for unicode filename") @@ -809,6 +830,55 @@ class OtherTests(unittest.TestCase): self.assertEqual(zipfr.comment, comment2) zipfr.close() + def check_testzip_with_bad_crc(self, compression): + """Tests that files with bad CRCs return their name from testzip.""" + zipdata = self.zips_with_bad_crc[compression] + + zipf = zipfile.ZipFile(io.BytesIO(zipdata), mode="r") + # testzip returns the name of the first corrupt file, or None + self.assertEqual('afile', zipf.testzip()) + zipf.close() + + def test_testzip_with_bad_crc_stored(self): + self.check_testzip_with_bad_crc(zipfile.ZIP_STORED) + + if zlib: + def test_testzip_with_bad_crc_deflated(self): + self.check_testzip_with_bad_crc(zipfile.ZIP_DEFLATED) + + def check_read_with_bad_crc(self, compression): + """Tests that files with bad CRCs raise a BadZipfile exception when read.""" + zipdata = self.zips_with_bad_crc[compression] + + # Using ZipFile.read() + zipf = zipfile.ZipFile(io.BytesIO(zipdata), mode="r") + self.assertRaises(zipfile.BadZipfile, zipf.read, 'afile') + zipf.close() + + # Using ZipExtFile.read() + zipf = zipfile.ZipFile(io.BytesIO(zipdata), mode="r") + corrupt_file = zipf.open('afile', 'r') + self.assertRaises(zipfile.BadZipfile, corrupt_file.read) + corrupt_file.close() + zipf.close() + + # Same with small reads (in order to exercise the buffering logic) + zipf = zipfile.ZipFile(io.BytesIO(zipdata), mode="r") + corrupt_file = zipf.open('afile', 'r') + corrupt_file.MIN_READ_SIZE = 2 + with self.assertRaises(zipfile.BadZipfile): + while corrupt_file.read(2): + pass + corrupt_file.close() + zipf.close() + + def test_read_with_bad_crc_stored(self): + self.check_read_with_bad_crc(zipfile.ZIP_STORED) + + if zlib: + def test_read_with_bad_crc_deflated(self): + self.check_read_with_bad_crc(zipfile.ZIP_DEFLATED) + def tearDown(self): support.unlink(TESTFN) support.unlink(TESTFN2) |