Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 1 | # We can test part of the module without zlib. |
Guido van Rossum | 368f04a | 2000-04-10 13:23:04 +0000 | [diff] [blame] | 2 | try: |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 3 | import zlib |
| 4 | except ImportError: |
| 5 | zlib = None |
Tim Peters | a45cacf | 2004-08-20 03:47:14 +0000 | [diff] [blame] | 6 | |
Ezio Melotti | e7a0cc2 | 2009-07-04 14:58:27 +0000 | [diff] [blame] | 7 | import os |
Antoine Pitrou | e1436d1 | 2010-08-12 15:25:51 +0000 | [diff] [blame] | 8 | import io |
Ezio Melotti | e7a0cc2 | 2009-07-04 14:58:27 +0000 | [diff] [blame] | 9 | import sys |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 10 | import time |
Ezio Melotti | e7a0cc2 | 2009-07-04 14:58:27 +0000 | [diff] [blame] | 11 | import struct |
| 12 | import zipfile |
| 13 | import unittest |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 14 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 15 | from StringIO import StringIO |
| 16 | from tempfile import TemporaryFile |
Serhiy Storchaka | 45aa771 | 2014-12-03 09:11:12 +0200 | [diff] [blame] | 17 | from random import randint, random, getrandbits |
Ezio Melotti | e7a0cc2 | 2009-07-04 14:58:27 +0000 | [diff] [blame] | 18 | from unittest import skipUnless |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 19 | |
Serhiy Storchaka | db03e6b | 2013-05-08 21:52:31 +0300 | [diff] [blame] | 20 | from test.test_support import TESTFN, TESTFN_UNICODE, TESTFN_ENCODING, \ |
Benjamin Peterson | 352eb4f | 2014-04-03 10:31:25 -0400 | [diff] [blame] | 21 | run_unittest, findfile, unlink, rmtree, check_warnings |
Serhiy Storchaka | db03e6b | 2013-05-08 21:52:31 +0300 | [diff] [blame] | 22 | try: |
| 23 | TESTFN_UNICODE.encode(TESTFN_ENCODING) |
| 24 | except (UnicodeError, TypeError): |
| 25 | # Either the file system encoding is None, or the file name |
| 26 | # cannot be encoded in the file system encoding. |
| 27 | TESTFN_UNICODE = None |
Guido van Rossum | 368f04a | 2000-04-10 13:23:04 +0000 | [diff] [blame] | 28 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 29 | TESTFN2 = TESTFN + "2" |
Martin v. Löwis | 0dfcfc8 | 2009-01-24 14:00:33 +0000 | [diff] [blame] | 30 | TESTFNDIR = TESTFN + "d" |
Georg Brandl | 4b3ab6f | 2007-07-12 09:59:22 +0000 | [diff] [blame] | 31 | FIXEDTEST_SIZE = 1000 |
Guido van Rossum | 368f04a | 2000-04-10 13:23:04 +0000 | [diff] [blame] | 32 | |
Georg Brandl | 62416bc | 2008-01-07 18:47:44 +0000 | [diff] [blame] | 33 | SMALL_TEST_DATA = [('_ziptest1', '1q2w3e4r5t'), |
| 34 | ('ziptest2dir/_ziptest2', 'qawsedrftg'), |
Gregory P. Smith | 608cc45 | 2013-02-01 11:40:18 -0800 | [diff] [blame] | 35 | ('ziptest2dir/ziptest3dir/_ziptest3', 'azsxdcfvgb'), |
Georg Brandl | 62416bc | 2008-01-07 18:47:44 +0000 | [diff] [blame] | 36 | ('ziptest2dir/ziptest3dir/ziptest4dir/_ziptest3', '6y7u8i9o0p')] |
| 37 | |
Serhiy Storchaka | 45aa771 | 2014-12-03 09:11:12 +0200 | [diff] [blame] | 38 | def getrandbytes(size): |
Serhiy Storchaka | 45aa771 | 2014-12-03 09:11:12 +0200 | [diff] [blame] | 39 | return bytes(bytearray.fromhex('%0*x' % (2 * size, getrandbits(8 * size)))) |
| 40 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 41 | class TestsWithSourceFile(unittest.TestCase): |
| 42 | def setUp(self): |
Georg Brandl | 4b3ab6f | 2007-07-12 09:59:22 +0000 | [diff] [blame] | 43 | self.line_gen = ["Zipfile test line %d. random float: %f" % (i, random()) |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 44 | for i in xrange(FIXEDTEST_SIZE)] |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 45 | self.data = '\n'.join(self.line_gen) + '\n' |
Fred Drake | 6e7e485 | 2001-02-28 05:34:16 +0000 | [diff] [blame] | 46 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 47 | # Make a source file with some lines |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 48 | with open(TESTFN, "wb") as fp: |
| 49 | fp.write(self.data) |
Tim Peters | 7d3bad6 | 2001-04-04 18:56:49 +0000 | [diff] [blame] | 50 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 51 | def make_test_archive(self, f, compression): |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 52 | # Create the ZIP archive |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 53 | with zipfile.ZipFile(f, "w", compression) as zipfp: |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 54 | zipfp.write(TESTFN, "another.name") |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 55 | zipfp.write(TESTFN, TESTFN) |
| 56 | zipfp.writestr("strfile", self.data) |
Tim Peters | 7d3bad6 | 2001-04-04 18:56:49 +0000 | [diff] [blame] | 57 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 58 | def zip_test(self, f, compression): |
| 59 | self.make_test_archive(f, compression) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 60 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 61 | # Read the ZIP archive |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 62 | with zipfile.ZipFile(f, "r", compression) as zipfp: |
| 63 | self.assertEqual(zipfp.read(TESTFN), self.data) |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 64 | self.assertEqual(zipfp.read("another.name"), self.data) |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 65 | self.assertEqual(zipfp.read("strfile"), self.data) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 66 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 67 | # Print the ZIP directory |
| 68 | fp = StringIO() |
| 69 | stdout = sys.stdout |
| 70 | try: |
| 71 | sys.stdout = fp |
| 72 | zipfp.printdir() |
| 73 | finally: |
| 74 | sys.stdout = stdout |
Tim Peters | a608bb2 | 2006-06-15 18:06:29 +0000 | [diff] [blame] | 75 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 76 | directory = fp.getvalue() |
| 77 | lines = directory.splitlines() |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 78 | self.assertEqual(len(lines), 4) # Number of files + header |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 79 | |
Ezio Melotti | aa98058 | 2010-01-23 23:04:36 +0000 | [diff] [blame] | 80 | self.assertIn('File Name', lines[0]) |
| 81 | self.assertIn('Modified', lines[0]) |
| 82 | self.assertIn('Size', lines[0]) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 83 | |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 84 | fn, date, time_, size = lines[1].split() |
| 85 | self.assertEqual(fn, 'another.name') |
| 86 | self.assertTrue(time.strptime(date, '%Y-%m-%d')) |
| 87 | self.assertTrue(time.strptime(time_, '%H:%M:%S')) |
| 88 | self.assertEqual(size, str(len(self.data))) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 89 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 90 | # Check the namelist |
| 91 | names = zipfp.namelist() |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 92 | self.assertEqual(len(names), 3) |
Ezio Melotti | aa98058 | 2010-01-23 23:04:36 +0000 | [diff] [blame] | 93 | self.assertIn(TESTFN, names) |
| 94 | self.assertIn("another.name", names) |
| 95 | self.assertIn("strfile", names) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 96 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 97 | # Check infolist |
| 98 | infos = zipfp.infolist() |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 99 | names = [i.filename for i in infos] |
| 100 | self.assertEqual(len(names), 3) |
Ezio Melotti | aa98058 | 2010-01-23 23:04:36 +0000 | [diff] [blame] | 101 | self.assertIn(TESTFN, names) |
| 102 | self.assertIn("another.name", names) |
| 103 | self.assertIn("strfile", names) |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 104 | for i in infos: |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 105 | self.assertEqual(i.file_size, len(self.data)) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 106 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 107 | # check getinfo |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 108 | for nm in (TESTFN, "another.name", "strfile"): |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 109 | info = zipfp.getinfo(nm) |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 110 | self.assertEqual(info.filename, nm) |
| 111 | self.assertEqual(info.file_size, len(self.data)) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 112 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 113 | # Check that testzip doesn't raise an exception |
| 114 | zipfp.testzip() |
Tim Peters | 7d3bad6 | 2001-04-04 18:56:49 +0000 | [diff] [blame] | 115 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 116 | def test_stored(self): |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 117 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 118 | self.zip_test(f, zipfile.ZIP_STORED) |
Raymond Hettinger | c0fac96 | 2003-06-27 22:25:03 +0000 | [diff] [blame] | 119 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 120 | def zip_open_test(self, f, compression): |
| 121 | self.make_test_archive(f, compression) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 122 | |
| 123 | # Read the ZIP archive |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 124 | with zipfile.ZipFile(f, "r", compression) as zipfp: |
| 125 | zipdata1 = [] |
Brian Curtin | 0d65433 | 2011-04-19 21:15:55 -0500 | [diff] [blame] | 126 | with zipfp.open(TESTFN) as zipopen1: |
| 127 | while True: |
| 128 | read_data = zipopen1.read(256) |
| 129 | if not read_data: |
| 130 | break |
| 131 | zipdata1.append(read_data) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 132 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 133 | zipdata2 = [] |
Brian Curtin | 0d65433 | 2011-04-19 21:15:55 -0500 | [diff] [blame] | 134 | with zipfp.open("another.name") as zipopen2: |
| 135 | while True: |
| 136 | read_data = zipopen2.read(256) |
| 137 | if not read_data: |
| 138 | break |
| 139 | zipdata2.append(read_data) |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 140 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 141 | self.assertEqual(''.join(zipdata1), self.data) |
| 142 | self.assertEqual(''.join(zipdata2), self.data) |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 143 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 144 | def test_open_stored(self): |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 145 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 146 | self.zip_open_test(f, zipfile.ZIP_STORED) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 147 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 148 | def test_open_via_zip_info(self): |
Georg Brandl | 112aa50 | 2008-05-20 08:25:48 +0000 | [diff] [blame] | 149 | # Create the ZIP archive |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 150 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: |
| 151 | zipfp.writestr("name", "foo") |
Serhiy Storchaka | 4925935 | 2014-01-20 21:57:09 +0200 | [diff] [blame] | 152 | with check_warnings(('', UserWarning)): |
| 153 | zipfp.writestr("name", "bar") |
| 154 | self.assertEqual(zipfp.namelist(), ["name"] * 2) |
Georg Brandl | 112aa50 | 2008-05-20 08:25:48 +0000 | [diff] [blame] | 155 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 156 | with zipfile.ZipFile(TESTFN2, "r") as zipfp: |
| 157 | infos = zipfp.infolist() |
| 158 | data = "" |
| 159 | for info in infos: |
Brian Curtin | 0d65433 | 2011-04-19 21:15:55 -0500 | [diff] [blame] | 160 | with zipfp.open(info) as f: |
| 161 | data += f.read() |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 162 | self.assertTrue(data == "foobar" or data == "barfoo") |
| 163 | data = "" |
| 164 | for info in infos: |
| 165 | data += zipfp.read(info) |
| 166 | self.assertTrue(data == "foobar" or data == "barfoo") |
Georg Brandl | 112aa50 | 2008-05-20 08:25:48 +0000 | [diff] [blame] | 167 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 168 | def zip_random_open_test(self, f, compression): |
| 169 | self.make_test_archive(f, compression) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 170 | |
| 171 | # Read the ZIP archive |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 172 | with zipfile.ZipFile(f, "r", compression) as zipfp: |
| 173 | zipdata1 = [] |
Brian Curtin | 0d65433 | 2011-04-19 21:15:55 -0500 | [diff] [blame] | 174 | with zipfp.open(TESTFN) as zipopen1: |
| 175 | while True: |
| 176 | read_data = zipopen1.read(randint(1, 1024)) |
| 177 | if not read_data: |
| 178 | break |
| 179 | zipdata1.append(read_data) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 180 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 181 | self.assertEqual(''.join(zipdata1), self.data) |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 182 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 183 | def test_random_open_stored(self): |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 184 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 185 | self.zip_random_open_test(f, zipfile.ZIP_STORED) |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 186 | |
Antoine Pitrou | 94c33eb | 2010-01-27 20:59:50 +0000 | [diff] [blame] | 187 | def test_univeral_readaheads(self): |
| 188 | f = StringIO() |
| 189 | |
| 190 | data = 'a\r\n' * 16 * 1024 |
Brian Curtin | 0d65433 | 2011-04-19 21:15:55 -0500 | [diff] [blame] | 191 | with zipfile.ZipFile(f, 'w', zipfile.ZIP_STORED) as zipfp: |
| 192 | zipfp.writestr(TESTFN, data) |
Antoine Pitrou | 94c33eb | 2010-01-27 20:59:50 +0000 | [diff] [blame] | 193 | |
| 194 | data2 = '' |
Brian Curtin | 0d65433 | 2011-04-19 21:15:55 -0500 | [diff] [blame] | 195 | with zipfile.ZipFile(f, 'r') as zipfp: |
| 196 | with zipfp.open(TESTFN, 'rU') as zipopen: |
| 197 | for line in zipopen: |
| 198 | data2 += line |
Antoine Pitrou | 94c33eb | 2010-01-27 20:59:50 +0000 | [diff] [blame] | 199 | |
| 200 | self.assertEqual(data, data2.replace('\n', '\r\n')) |
| 201 | |
| 202 | def zip_readline_read_test(self, f, compression): |
| 203 | self.make_test_archive(f, compression) |
| 204 | |
| 205 | # Read the ZIP archive |
Brian Curtin | 0d65433 | 2011-04-19 21:15:55 -0500 | [diff] [blame] | 206 | with zipfile.ZipFile(f, "r") as zipfp: |
| 207 | with zipfp.open(TESTFN) as zipopen: |
| 208 | data = '' |
| 209 | while True: |
| 210 | read = zipopen.readline() |
| 211 | if not read: |
| 212 | break |
| 213 | data += read |
Antoine Pitrou | 94c33eb | 2010-01-27 20:59:50 +0000 | [diff] [blame] | 214 | |
Brian Curtin | 0d65433 | 2011-04-19 21:15:55 -0500 | [diff] [blame] | 215 | read = zipopen.read(100) |
| 216 | if not read: |
| 217 | break |
| 218 | data += read |
Antoine Pitrou | 94c33eb | 2010-01-27 20:59:50 +0000 | [diff] [blame] | 219 | |
| 220 | self.assertEqual(data, self.data) |
Antoine Pitrou | 94c33eb | 2010-01-27 20:59:50 +0000 | [diff] [blame] | 221 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 222 | def zip_readline_test(self, f, compression): |
| 223 | self.make_test_archive(f, compression) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 224 | |
| 225 | # Read the ZIP archive |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 226 | with zipfile.ZipFile(f, "r") as zipfp: |
Brian Curtin | 0d65433 | 2011-04-19 21:15:55 -0500 | [diff] [blame] | 227 | with zipfp.open(TESTFN) as zipopen: |
| 228 | for line in self.line_gen: |
| 229 | linedata = zipopen.readline() |
| 230 | self.assertEqual(linedata, line + '\n') |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 231 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 232 | def zip_readlines_test(self, f, compression): |
| 233 | self.make_test_archive(f, compression) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 234 | |
| 235 | # Read the ZIP archive |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 236 | with zipfile.ZipFile(f, "r") as zipfp: |
Brian Curtin | 0d65433 | 2011-04-19 21:15:55 -0500 | [diff] [blame] | 237 | with zipfp.open(TESTFN) as zo: |
| 238 | ziplines = zo.readlines() |
| 239 | for line, zipline in zip(self.line_gen, ziplines): |
| 240 | self.assertEqual(zipline, line + '\n') |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 241 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 242 | def zip_iterlines_test(self, f, compression): |
| 243 | self.make_test_archive(f, compression) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 244 | |
| 245 | # Read the ZIP archive |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 246 | with zipfile.ZipFile(f, "r") as zipfp: |
| 247 | for line, zipline in zip(self.line_gen, zipfp.open(TESTFN)): |
| 248 | self.assertEqual(zipline, line + '\n') |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 249 | |
Antoine Pitrou | 94c33eb | 2010-01-27 20:59:50 +0000 | [diff] [blame] | 250 | def test_readline_read_stored(self): |
| 251 | # Issue #7610: calls to readline() interleaved with calls to read(). |
| 252 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
| 253 | self.zip_readline_read_test(f, zipfile.ZIP_STORED) |
| 254 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 255 | def test_readline_stored(self): |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 256 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 257 | self.zip_readline_test(f, zipfile.ZIP_STORED) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 258 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 259 | def test_readlines_stored(self): |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 260 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 261 | self.zip_readlines_test(f, zipfile.ZIP_STORED) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 262 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 263 | def test_iterlines_stored(self): |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 264 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 265 | self.zip_iterlines_test(f, zipfile.ZIP_STORED) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 266 | |
Ezio Melotti | e7a0cc2 | 2009-07-04 14:58:27 +0000 | [diff] [blame] | 267 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 268 | def test_deflated(self): |
Ezio Melotti | e7a0cc2 | 2009-07-04 14:58:27 +0000 | [diff] [blame] | 269 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 270 | self.zip_test(f, zipfile.ZIP_DEFLATED) |
Raymond Hettinger | c0fac96 | 2003-06-27 22:25:03 +0000 | [diff] [blame] | 271 | |
Ezio Melotti | e7a0cc2 | 2009-07-04 14:58:27 +0000 | [diff] [blame] | 272 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 273 | def test_open_deflated(self): |
Ezio Melotti | e7a0cc2 | 2009-07-04 14:58:27 +0000 | [diff] [blame] | 274 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 275 | self.zip_open_test(f, zipfile.ZIP_DEFLATED) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 276 | |
Ezio Melotti | e7a0cc2 | 2009-07-04 14:58:27 +0000 | [diff] [blame] | 277 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 278 | def test_random_open_deflated(self): |
Ezio Melotti | e7a0cc2 | 2009-07-04 14:58:27 +0000 | [diff] [blame] | 279 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 280 | self.zip_random_open_test(f, zipfile.ZIP_DEFLATED) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 281 | |
Ezio Melotti | e7a0cc2 | 2009-07-04 14:58:27 +0000 | [diff] [blame] | 282 | @skipUnless(zlib, "requires zlib") |
Antoine Pitrou | 94c33eb | 2010-01-27 20:59:50 +0000 | [diff] [blame] | 283 | def test_readline_read_deflated(self): |
| 284 | # Issue #7610: calls to readline() interleaved with calls to read(). |
| 285 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
| 286 | self.zip_readline_read_test(f, zipfile.ZIP_DEFLATED) |
| 287 | |
| 288 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 289 | def test_readline_deflated(self): |
Ezio Melotti | e7a0cc2 | 2009-07-04 14:58:27 +0000 | [diff] [blame] | 290 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 291 | self.zip_readline_test(f, zipfile.ZIP_DEFLATED) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 292 | |
Ezio Melotti | e7a0cc2 | 2009-07-04 14:58:27 +0000 | [diff] [blame] | 293 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 294 | def test_readlines_deflated(self): |
Ezio Melotti | e7a0cc2 | 2009-07-04 14:58:27 +0000 | [diff] [blame] | 295 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 296 | self.zip_readlines_test(f, zipfile.ZIP_DEFLATED) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 297 | |
Ezio Melotti | e7a0cc2 | 2009-07-04 14:58:27 +0000 | [diff] [blame] | 298 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 299 | def test_iterlines_deflated(self): |
Ezio Melotti | e7a0cc2 | 2009-07-04 14:58:27 +0000 | [diff] [blame] | 300 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 301 | self.zip_iterlines_test(f, zipfile.ZIP_DEFLATED) |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 302 | |
Ezio Melotti | e7a0cc2 | 2009-07-04 14:58:27 +0000 | [diff] [blame] | 303 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 304 | def test_low_compression(self): |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 305 | """Check for cases where compressed data is larger than original.""" |
Ezio Melotti | e7a0cc2 | 2009-07-04 14:58:27 +0000 | [diff] [blame] | 306 | # Create the ZIP archive |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 307 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED) as zipfp: |
| 308 | zipfp.writestr("strfile", '12') |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 309 | |
Ezio Melotti | e7a0cc2 | 2009-07-04 14:58:27 +0000 | [diff] [blame] | 310 | # Get an open object for strfile |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 311 | with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_DEFLATED) as zipfp: |
Brian Curtin | 0d65433 | 2011-04-19 21:15:55 -0500 | [diff] [blame] | 312 | with zipfp.open("strfile") as openobj: |
| 313 | self.assertEqual(openobj.read(1), '1') |
| 314 | self.assertEqual(openobj.read(1), '2') |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 315 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 316 | def test_absolute_arcnames(self): |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 317 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: |
| 318 | zipfp.write(TESTFN, "/absolute") |
Georg Brandl | 8f7c54e | 2006-02-20 08:40:38 +0000 | [diff] [blame] | 319 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 320 | with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) as zipfp: |
| 321 | self.assertEqual(zipfp.namelist(), ["absolute"]) |
Tim Peters | 32cbc96 | 2006-02-20 21:42:18 +0000 | [diff] [blame] | 322 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 323 | def test_append_to_zip_file(self): |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 324 | """Test appending to an existing zipfile.""" |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 325 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: |
| 326 | zipfp.write(TESTFN, TESTFN) |
| 327 | |
| 328 | with zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) as zipfp: |
| 329 | zipfp.writestr("strfile", self.data) |
| 330 | self.assertEqual(zipfp.namelist(), [TESTFN, "strfile"]) |
Georg Brandl | 4b3ab6f | 2007-07-12 09:59:22 +0000 | [diff] [blame] | 331 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 332 | def test_append_to_non_zip_file(self): |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 333 | """Test appending to an existing file that is not a zipfile.""" |
Georg Brandl | 4b3ab6f | 2007-07-12 09:59:22 +0000 | [diff] [blame] | 334 | # NOTE: this test fails if len(d) < 22 because of the first |
| 335 | # line "fpin.seek(-22, 2)" in _EndRecData |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 336 | data = 'I am not a ZipFile!'*10 |
| 337 | with open(TESTFN2, 'wb') as f: |
| 338 | f.write(data) |
| 339 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 340 | with zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) as zipfp: |
| 341 | zipfp.write(TESTFN, TESTFN) |
Georg Brandl | 4b3ab6f | 2007-07-12 09:59:22 +0000 | [diff] [blame] | 342 | |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 343 | with open(TESTFN2, 'rb') as f: |
| 344 | f.seek(len(data)) |
| 345 | with zipfile.ZipFile(f, "r") as zipfp: |
| 346 | self.assertEqual(zipfp.namelist(), [TESTFN]) |
Georg Brandl | 4b3ab6f | 2007-07-12 09:59:22 +0000 | [diff] [blame] | 347 | |
R David Murray | 873c583 | 2011-06-09 16:01:09 -0400 | [diff] [blame] | 348 | def test_ignores_newline_at_end(self): |
| 349 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: |
| 350 | zipfp.write(TESTFN, TESTFN) |
| 351 | with open(TESTFN2, 'a') as f: |
| 352 | f.write("\r\n\00\00\00") |
| 353 | with zipfile.ZipFile(TESTFN2, "r") as zipfp: |
| 354 | self.assertIsInstance(zipfp, zipfile.ZipFile) |
| 355 | |
| 356 | def test_ignores_stuff_appended_past_comments(self): |
| 357 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: |
| 358 | zipfp.comment = b"this is a comment" |
| 359 | zipfp.write(TESTFN, TESTFN) |
| 360 | with open(TESTFN2, 'a') as f: |
| 361 | f.write("abcdef\r\n") |
| 362 | with zipfile.ZipFile(TESTFN2, "r") as zipfp: |
| 363 | self.assertIsInstance(zipfp, zipfile.ZipFile) |
| 364 | self.assertEqual(zipfp.comment, b"this is a comment") |
| 365 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 366 | def test_write_default_name(self): |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 367 | """Check that calling ZipFile.write without arcname specified |
| 368 | produces the expected result.""" |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 369 | with zipfile.ZipFile(TESTFN2, "w") as zipfp: |
| 370 | zipfp.write(TESTFN) |
Benjamin Peterson | 352eb4f | 2014-04-03 10:31:25 -0400 | [diff] [blame] | 371 | with open(TESTFN,'r') as fid: |
| 372 | self.assertEqual(zipfp.read(TESTFN), fid.read()) |
Georg Brandl | 4b3ab6f | 2007-07-12 09:59:22 +0000 | [diff] [blame] | 373 | |
Ezio Melotti | 1036a7f | 2009-09-12 14:43:43 +0000 | [diff] [blame] | 374 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 375 | def test_per_file_compression(self): |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 376 | """Check that files within a Zip archive can have different |
| 377 | compression options.""" |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 378 | with zipfile.ZipFile(TESTFN2, "w") as zipfp: |
| 379 | zipfp.write(TESTFN, 'storeme', zipfile.ZIP_STORED) |
| 380 | zipfp.write(TESTFN, 'deflateme', zipfile.ZIP_DEFLATED) |
| 381 | sinfo = zipfp.getinfo('storeme') |
| 382 | dinfo = zipfp.getinfo('deflateme') |
| 383 | self.assertEqual(sinfo.compress_type, zipfile.ZIP_STORED) |
| 384 | self.assertEqual(dinfo.compress_type, zipfile.ZIP_DEFLATED) |
Georg Brandl | 4b3ab6f | 2007-07-12 09:59:22 +0000 | [diff] [blame] | 385 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 386 | def test_write_to_readonly(self): |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 387 | """Check that trying to call write() on a readonly ZipFile object |
| 388 | raises a RuntimeError.""" |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 389 | with zipfile.ZipFile(TESTFN2, mode="w") as zipfp: |
| 390 | zipfp.writestr("somefile.txt", "bogus") |
| 391 | |
| 392 | with zipfile.ZipFile(TESTFN2, mode="r") as zipfp: |
| 393 | self.assertRaises(RuntimeError, zipfp.write, TESTFN) |
Georg Brandl | 4b3ab6f | 2007-07-12 09:59:22 +0000 | [diff] [blame] | 394 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 395 | def test_extract(self): |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 396 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: |
| 397 | for fpath, fdata in SMALL_TEST_DATA: |
| 398 | zipfp.writestr(fpath, fdata) |
Georg Brandl | 62416bc | 2008-01-07 18:47:44 +0000 | [diff] [blame] | 399 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 400 | with zipfile.ZipFile(TESTFN2, "r") as zipfp: |
| 401 | for fpath, fdata in SMALL_TEST_DATA: |
| 402 | writtenfile = zipfp.extract(fpath) |
Georg Brandl | 62416bc | 2008-01-07 18:47:44 +0000 | [diff] [blame] | 403 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 404 | # make sure it was written to the right place |
Gregory P. Smith | 608cc45 | 2013-02-01 11:40:18 -0800 | [diff] [blame] | 405 | correctfile = os.path.join(os.getcwd(), fpath) |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 406 | correctfile = os.path.normpath(correctfile) |
Georg Brandl | 62416bc | 2008-01-07 18:47:44 +0000 | [diff] [blame] | 407 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 408 | self.assertEqual(writtenfile, correctfile) |
Georg Brandl | 62416bc | 2008-01-07 18:47:44 +0000 | [diff] [blame] | 409 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 410 | # make sure correct data is in correct file |
Benjamin Peterson | 352eb4f | 2014-04-03 10:31:25 -0400 | [diff] [blame] | 411 | with open(writtenfile, "rb") as fid: |
| 412 | self.assertEqual(fdata, fid.read()) |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 413 | os.remove(writtenfile) |
Georg Brandl | 62416bc | 2008-01-07 18:47:44 +0000 | [diff] [blame] | 414 | |
| 415 | # remove the test file subdirectories |
Benjamin Peterson | 352eb4f | 2014-04-03 10:31:25 -0400 | [diff] [blame] | 416 | rmtree(os.path.join(os.getcwd(), 'ziptest2dir')) |
Georg Brandl | 62416bc | 2008-01-07 18:47:44 +0000 | [diff] [blame] | 417 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 418 | def test_extract_all(self): |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 419 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: |
| 420 | for fpath, fdata in SMALL_TEST_DATA: |
| 421 | zipfp.writestr(fpath, fdata) |
Georg Brandl | 62416bc | 2008-01-07 18:47:44 +0000 | [diff] [blame] | 422 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 423 | with zipfile.ZipFile(TESTFN2, "r") as zipfp: |
| 424 | zipfp.extractall() |
| 425 | for fpath, fdata in SMALL_TEST_DATA: |
Gregory P. Smith | 608cc45 | 2013-02-01 11:40:18 -0800 | [diff] [blame] | 426 | outfile = os.path.join(os.getcwd(), fpath) |
Georg Brandl | 62416bc | 2008-01-07 18:47:44 +0000 | [diff] [blame] | 427 | |
Benjamin Peterson | 352eb4f | 2014-04-03 10:31:25 -0400 | [diff] [blame] | 428 | with open(outfile, "rb") as fid: |
| 429 | self.assertEqual(fdata, fid.read()) |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 430 | os.remove(outfile) |
Georg Brandl | 62416bc | 2008-01-07 18:47:44 +0000 | [diff] [blame] | 431 | |
| 432 | # remove the test file subdirectories |
Benjamin Peterson | 352eb4f | 2014-04-03 10:31:25 -0400 | [diff] [blame] | 433 | rmtree(os.path.join(os.getcwd(), 'ziptest2dir')) |
Georg Brandl | 62416bc | 2008-01-07 18:47:44 +0000 | [diff] [blame] | 434 | |
Gregory P. Smith | 608cc45 | 2013-02-01 11:40:18 -0800 | [diff] [blame] | 435 | def check_file(self, filename, content): |
| 436 | self.assertTrue(os.path.isfile(filename)) |
| 437 | with open(filename, 'rb') as f: |
| 438 | self.assertEqual(f.read(), content) |
| 439 | |
Serhiy Storchaka | db03e6b | 2013-05-08 21:52:31 +0300 | [diff] [blame] | 440 | @skipUnless(TESTFN_UNICODE, "No Unicode filesystem semantics on this platform.") |
Serhiy Storchaka | 6fa83f9 | 2013-04-13 12:28:17 +0300 | [diff] [blame] | 441 | def test_extract_unicode_filenames(self): |
| 442 | fnames = [u'foo.txt', os.path.basename(TESTFN_UNICODE)] |
| 443 | content = 'Test for unicode filename' |
| 444 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: |
| 445 | for fname in fnames: |
| 446 | zipfp.writestr(fname, content) |
| 447 | |
| 448 | with zipfile.ZipFile(TESTFN2, "r") as zipfp: |
| 449 | for fname in fnames: |
| 450 | writtenfile = zipfp.extract(fname) |
| 451 | |
| 452 | # make sure it was written to the right place |
| 453 | correctfile = os.path.join(os.getcwd(), fname) |
| 454 | correctfile = os.path.normpath(correctfile) |
| 455 | self.assertEqual(writtenfile, correctfile) |
| 456 | |
| 457 | self.check_file(writtenfile, content) |
| 458 | os.remove(writtenfile) |
| 459 | |
Gregory P. Smith | 608cc45 | 2013-02-01 11:40:18 -0800 | [diff] [blame] | 460 | def test_extract_hackers_arcnames(self): |
| 461 | hacknames = [ |
| 462 | ('../foo/bar', 'foo/bar'), |
| 463 | ('foo/../bar', 'foo/bar'), |
| 464 | ('foo/../../bar', 'foo/bar'), |
| 465 | ('foo/bar/..', 'foo/bar'), |
| 466 | ('./../foo/bar', 'foo/bar'), |
| 467 | ('/foo/bar', 'foo/bar'), |
| 468 | ('/foo/../bar', 'foo/bar'), |
| 469 | ('/foo/../../bar', 'foo/bar'), |
Gregory P. Smith | 608cc45 | 2013-02-01 11:40:18 -0800 | [diff] [blame] | 470 | ] |
| 471 | if os.path.sep == '\\': |
| 472 | hacknames.extend([ |
| 473 | (r'..\foo\bar', 'foo/bar'), |
| 474 | (r'..\/foo\/bar', 'foo/bar'), |
| 475 | (r'foo/\..\/bar', 'foo/bar'), |
| 476 | (r'foo\/../\bar', 'foo/bar'), |
| 477 | (r'C:foo/bar', 'foo/bar'), |
| 478 | (r'C:/foo/bar', 'foo/bar'), |
| 479 | (r'C://foo/bar', 'foo/bar'), |
| 480 | (r'C:\foo\bar', 'foo/bar'), |
Benjamin Peterson | c459755 | 2014-06-22 20:26:07 -0700 | [diff] [blame] | 481 | (r'//conky/mountpoint/foo/bar', 'foo/bar'), |
| 482 | (r'\\conky\mountpoint\foo\bar', 'foo/bar'), |
Gregory P. Smith | 608cc45 | 2013-02-01 11:40:18 -0800 | [diff] [blame] | 483 | (r'///conky/mountpoint/foo/bar', 'conky/mountpoint/foo/bar'), |
| 484 | (r'\\\conky\mountpoint\foo\bar', 'conky/mountpoint/foo/bar'), |
| 485 | (r'//conky//mountpoint/foo/bar', 'conky/mountpoint/foo/bar'), |
| 486 | (r'\\conky\\mountpoint\foo\bar', 'conky/mountpoint/foo/bar'), |
Benjamin Peterson | c459755 | 2014-06-22 20:26:07 -0700 | [diff] [blame] | 487 | (r'//?/C:/foo/bar', 'foo/bar'), |
| 488 | (r'\\?\C:\foo\bar', 'foo/bar'), |
Gregory P. Smith | 608cc45 | 2013-02-01 11:40:18 -0800 | [diff] [blame] | 489 | (r'C:/../C:/foo/bar', 'C_/foo/bar'), |
| 490 | (r'a:b\c<d>e|f"g?h*i', 'b/c_d_e_f_g_h_i'), |
Serhiy Storchaka | 13e56c7 | 2013-02-02 17:46:33 +0200 | [diff] [blame] | 491 | ('../../foo../../ba..r', 'foo/ba..r'), |
| 492 | ]) |
| 493 | else: # Unix |
| 494 | hacknames.extend([ |
| 495 | ('//foo/bar', 'foo/bar'), |
| 496 | ('../../foo../../ba..r', 'foo../ba..r'), |
Serhiy Storchaka | 05fd744 | 2013-02-02 18:34:57 +0200 | [diff] [blame] | 497 | (r'foo/..\bar', r'foo/..\bar'), |
Gregory P. Smith | 608cc45 | 2013-02-01 11:40:18 -0800 | [diff] [blame] | 498 | ]) |
| 499 | |
| 500 | for arcname, fixedname in hacknames: |
| 501 | content = b'foobar' + arcname.encode() |
| 502 | with zipfile.ZipFile(TESTFN2, 'w', zipfile.ZIP_STORED) as zipfp: |
Serhiy Storchaka | 05fd744 | 2013-02-02 18:34:57 +0200 | [diff] [blame] | 503 | zinfo = zipfile.ZipInfo() |
| 504 | # preserve backslashes |
| 505 | zinfo.filename = arcname |
| 506 | zinfo.external_attr = 0o600 << 16 |
| 507 | zipfp.writestr(zinfo, content) |
Gregory P. Smith | 608cc45 | 2013-02-01 11:40:18 -0800 | [diff] [blame] | 508 | |
Serhiy Storchaka | 2a051fa | 2013-02-02 19:25:57 +0200 | [diff] [blame] | 509 | arcname = arcname.replace(os.sep, "/") |
Gregory P. Smith | 608cc45 | 2013-02-01 11:40:18 -0800 | [diff] [blame] | 510 | targetpath = os.path.join('target', 'subdir', 'subsub') |
| 511 | correctfile = os.path.join(targetpath, *fixedname.split('/')) |
| 512 | |
| 513 | with zipfile.ZipFile(TESTFN2, 'r') as zipfp: |
| 514 | writtenfile = zipfp.extract(arcname, targetpath) |
Serhiy Storchaka | 13e56c7 | 2013-02-02 17:46:33 +0200 | [diff] [blame] | 515 | self.assertEqual(writtenfile, correctfile, |
| 516 | msg="extract %r" % arcname) |
Gregory P. Smith | 608cc45 | 2013-02-01 11:40:18 -0800 | [diff] [blame] | 517 | self.check_file(correctfile, content) |
Benjamin Peterson | 352eb4f | 2014-04-03 10:31:25 -0400 | [diff] [blame] | 518 | rmtree('target') |
Gregory P. Smith | 608cc45 | 2013-02-01 11:40:18 -0800 | [diff] [blame] | 519 | |
| 520 | with zipfile.ZipFile(TESTFN2, 'r') as zipfp: |
| 521 | zipfp.extractall(targetpath) |
| 522 | self.check_file(correctfile, content) |
Benjamin Peterson | 352eb4f | 2014-04-03 10:31:25 -0400 | [diff] [blame] | 523 | rmtree('target') |
Gregory P. Smith | 608cc45 | 2013-02-01 11:40:18 -0800 | [diff] [blame] | 524 | |
| 525 | correctfile = os.path.join(os.getcwd(), *fixedname.split('/')) |
| 526 | |
| 527 | with zipfile.ZipFile(TESTFN2, 'r') as zipfp: |
| 528 | writtenfile = zipfp.extract(arcname) |
Serhiy Storchaka | 13e56c7 | 2013-02-02 17:46:33 +0200 | [diff] [blame] | 529 | self.assertEqual(writtenfile, correctfile, |
| 530 | msg="extract %r" % arcname) |
Gregory P. Smith | 608cc45 | 2013-02-01 11:40:18 -0800 | [diff] [blame] | 531 | self.check_file(correctfile, content) |
Benjamin Peterson | 352eb4f | 2014-04-03 10:31:25 -0400 | [diff] [blame] | 532 | rmtree(fixedname.split('/')[0]) |
Gregory P. Smith | 608cc45 | 2013-02-01 11:40:18 -0800 | [diff] [blame] | 533 | |
| 534 | with zipfile.ZipFile(TESTFN2, 'r') as zipfp: |
| 535 | zipfp.extractall() |
| 536 | self.check_file(correctfile, content) |
Benjamin Peterson | 352eb4f | 2014-04-03 10:31:25 -0400 | [diff] [blame] | 537 | rmtree(fixedname.split('/')[0]) |
Gregory P. Smith | 608cc45 | 2013-02-01 11:40:18 -0800 | [diff] [blame] | 538 | |
| 539 | os.remove(TESTFN2) |
| 540 | |
Ronald Oussoren | dd25e86 | 2010-02-07 20:18:02 +0000 | [diff] [blame] | 541 | def test_writestr_compression(self): |
| 542 | zipfp = zipfile.ZipFile(TESTFN2, "w") |
| 543 | zipfp.writestr("a.txt", "hello world", compress_type=zipfile.ZIP_STORED) |
| 544 | if zlib: |
| 545 | zipfp.writestr("b.txt", "hello world", compress_type=zipfile.ZIP_DEFLATED) |
| 546 | |
| 547 | info = zipfp.getinfo('a.txt') |
| 548 | self.assertEqual(info.compress_type, zipfile.ZIP_STORED) |
| 549 | |
| 550 | if zlib: |
| 551 | info = zipfp.getinfo('b.txt') |
| 552 | self.assertEqual(info.compress_type, zipfile.ZIP_DEFLATED) |
| 553 | |
| 554 | |
Antoine Pitrou | 5fdfa3e | 2008-07-25 19:42:26 +0000 | [diff] [blame] | 555 | def zip_test_writestr_permissions(self, f, compression): |
| 556 | # Make sure that writestr creates files with mode 0600, |
| 557 | # when it is passed a name rather than a ZipInfo instance. |
| 558 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 559 | self.make_test_archive(f, compression) |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 560 | with zipfile.ZipFile(f, "r") as zipfp: |
| 561 | zinfo = zipfp.getinfo('strfile') |
| 562 | self.assertEqual(zinfo.external_attr, 0600 << 16) |
Antoine Pitrou | 5fdfa3e | 2008-07-25 19:42:26 +0000 | [diff] [blame] | 563 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 564 | def test_writestr_permissions(self): |
Antoine Pitrou | 5fdfa3e | 2008-07-25 19:42:26 +0000 | [diff] [blame] | 565 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
| 566 | self.zip_test_writestr_permissions(f, zipfile.ZIP_STORED) |
| 567 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 568 | def test_close(self): |
| 569 | """Check that the zipfile is closed after the 'with' block.""" |
| 570 | with zipfile.ZipFile(TESTFN2, "w") as zipfp: |
| 571 | for fpath, fdata in SMALL_TEST_DATA: |
| 572 | zipfp.writestr(fpath, fdata) |
| 573 | self.assertTrue(zipfp.fp is not None, 'zipfp is not open') |
| 574 | self.assertTrue(zipfp.fp is None, 'zipfp is not closed') |
| 575 | |
| 576 | with zipfile.ZipFile(TESTFN2, "r") as zipfp: |
| 577 | self.assertTrue(zipfp.fp is not None, 'zipfp is not open') |
| 578 | self.assertTrue(zipfp.fp is None, 'zipfp is not closed') |
| 579 | |
| 580 | def test_close_on_exception(self): |
| 581 | """Check that the zipfile is closed if an exception is raised in the |
| 582 | 'with' block.""" |
| 583 | with zipfile.ZipFile(TESTFN2, "w") as zipfp: |
| 584 | for fpath, fdata in SMALL_TEST_DATA: |
| 585 | zipfp.writestr(fpath, fdata) |
| 586 | |
| 587 | try: |
| 588 | with zipfile.ZipFile(TESTFN2, "r") as zipfp2: |
| 589 | raise zipfile.BadZipfile() |
| 590 | except zipfile.BadZipfile: |
| 591 | self.assertTrue(zipfp2.fp is None, 'zipfp is not closed') |
| 592 | |
Senthil Kumaran | ddd4031 | 2011-10-20 01:38:35 +0800 | [diff] [blame] | 593 | def test_add_file_before_1980(self): |
| 594 | # Set atime and mtime to 1970-01-01 |
| 595 | os.utime(TESTFN, (0, 0)) |
| 596 | with zipfile.ZipFile(TESTFN2, "w") as zipfp: |
| 597 | self.assertRaises(ValueError, zipfp.write, TESTFN) |
| 598 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 599 | def tearDown(self): |
Ezio Melotti | 6cbfc12 | 2009-07-10 20:25:56 +0000 | [diff] [blame] | 600 | unlink(TESTFN) |
| 601 | unlink(TESTFN2) |
| 602 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 603 | |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 604 | class TestZip64InSmallFiles(unittest.TestCase): |
| 605 | # These tests test the ZIP64 functionality without using large files, |
| 606 | # see test_zipfile64 for proper tests. |
| 607 | |
| 608 | def setUp(self): |
| 609 | self._limit = zipfile.ZIP64_LIMIT |
Serhiy Storchaka | 1af262c | 2014-09-23 22:26:45 +0300 | [diff] [blame] | 610 | self._filecount_limit = zipfile.ZIP_FILECOUNT_LIMIT |
| 611 | zipfile.ZIP64_LIMIT = 1000 |
| 612 | zipfile.ZIP_FILECOUNT_LIMIT = 9 |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 613 | |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 614 | line_gen = ("Test of zipfile line %d." % i |
| 615 | for i in range(0, FIXEDTEST_SIZE)) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 616 | self.data = '\n'.join(line_gen) |
| 617 | |
| 618 | # Make a source file with some lines |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 619 | with open(TESTFN, "wb") as fp: |
| 620 | fp.write(self.data) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 621 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 622 | def large_file_exception_test(self, f, compression): |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 623 | with zipfile.ZipFile(f, "w", compression) as zipfp: |
| 624 | self.assertRaises(zipfile.LargeZipFile, |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 625 | zipfp.write, TESTFN, "another.name") |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 626 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 627 | def large_file_exception_test2(self, f, compression): |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 628 | with zipfile.ZipFile(f, "w", compression) as zipfp: |
| 629 | self.assertRaises(zipfile.LargeZipFile, |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 630 | zipfp.writestr, "another.name", self.data) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 631 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 632 | def test_large_file_exception(self): |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 633 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 634 | self.large_file_exception_test(f, zipfile.ZIP_STORED) |
| 635 | self.large_file_exception_test2(f, zipfile.ZIP_STORED) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 636 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 637 | def zip_test(self, f, compression): |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 638 | # Create the ZIP archive |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 639 | with zipfile.ZipFile(f, "w", compression, allowZip64=True) as zipfp: |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 640 | zipfp.write(TESTFN, "another.name") |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 641 | zipfp.write(TESTFN, TESTFN) |
| 642 | zipfp.writestr("strfile", self.data) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 643 | |
| 644 | # Read the ZIP archive |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 645 | with zipfile.ZipFile(f, "r", compression) as zipfp: |
| 646 | self.assertEqual(zipfp.read(TESTFN), self.data) |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 647 | self.assertEqual(zipfp.read("another.name"), self.data) |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 648 | self.assertEqual(zipfp.read("strfile"), self.data) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 649 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 650 | # Print the ZIP directory |
| 651 | fp = StringIO() |
| 652 | stdout = sys.stdout |
| 653 | try: |
| 654 | sys.stdout = fp |
| 655 | zipfp.printdir() |
| 656 | finally: |
| 657 | sys.stdout = stdout |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 658 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 659 | directory = fp.getvalue() |
| 660 | lines = directory.splitlines() |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 661 | self.assertEqual(len(lines), 4) # Number of files + header |
Tim Peters | a608bb2 | 2006-06-15 18:06:29 +0000 | [diff] [blame] | 662 | |
Ezio Melotti | aa98058 | 2010-01-23 23:04:36 +0000 | [diff] [blame] | 663 | self.assertIn('File Name', lines[0]) |
| 664 | self.assertIn('Modified', lines[0]) |
| 665 | self.assertIn('Size', lines[0]) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 666 | |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 667 | fn, date, time_, size = lines[1].split() |
| 668 | self.assertEqual(fn, 'another.name') |
| 669 | self.assertTrue(time.strptime(date, '%Y-%m-%d')) |
| 670 | self.assertTrue(time.strptime(time_, '%H:%M:%S')) |
| 671 | self.assertEqual(size, str(len(self.data))) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 672 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 673 | # Check the namelist |
| 674 | names = zipfp.namelist() |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 675 | self.assertEqual(len(names), 3) |
Ezio Melotti | aa98058 | 2010-01-23 23:04:36 +0000 | [diff] [blame] | 676 | self.assertIn(TESTFN, names) |
| 677 | self.assertIn("another.name", names) |
| 678 | self.assertIn("strfile", names) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 679 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 680 | # Check infolist |
| 681 | infos = zipfp.infolist() |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 682 | names = [i.filename for i in infos] |
| 683 | self.assertEqual(len(names), 3) |
Ezio Melotti | aa98058 | 2010-01-23 23:04:36 +0000 | [diff] [blame] | 684 | self.assertIn(TESTFN, names) |
| 685 | self.assertIn("another.name", names) |
| 686 | self.assertIn("strfile", names) |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 687 | for i in infos: |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 688 | self.assertEqual(i.file_size, len(self.data)) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 689 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 690 | # check getinfo |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 691 | for nm in (TESTFN, "another.name", "strfile"): |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 692 | info = zipfp.getinfo(nm) |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 693 | self.assertEqual(info.filename, nm) |
| 694 | self.assertEqual(info.file_size, len(self.data)) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 695 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 696 | # Check that testzip doesn't raise an exception |
| 697 | zipfp.testzip() |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 698 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 699 | def test_stored(self): |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 700 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 701 | self.zip_test(f, zipfile.ZIP_STORED) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 702 | |
Ezio Melotti | 6cbfc12 | 2009-07-10 20:25:56 +0000 | [diff] [blame] | 703 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 704 | def test_deflated(self): |
Ezio Melotti | 6cbfc12 | 2009-07-10 20:25:56 +0000 | [diff] [blame] | 705 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 706 | self.zip_test(f, zipfile.ZIP_DEFLATED) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 707 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 708 | def test_absolute_arcnames(self): |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 709 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED, |
| 710 | allowZip64=True) as zipfp: |
| 711 | zipfp.write(TESTFN, "/absolute") |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 712 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 713 | with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) as zipfp: |
| 714 | self.assertEqual(zipfp.namelist(), ["absolute"]) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 715 | |
Serhiy Storchaka | 1af262c | 2014-09-23 22:26:45 +0300 | [diff] [blame] | 716 | def test_too_many_files(self): |
| 717 | # This test checks that more than 64k files can be added to an archive, |
| 718 | # and that the resulting archive can be read properly by ZipFile |
| 719 | zipf = zipfile.ZipFile(TESTFN, mode="w", allowZip64=True) |
| 720 | zipf.debug = 100 |
| 721 | numfiles = 15 |
| 722 | for i in range(numfiles): |
| 723 | zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57)) |
| 724 | self.assertEqual(len(zipf.namelist()), numfiles) |
| 725 | zipf.close() |
| 726 | |
| 727 | zipf2 = zipfile.ZipFile(TESTFN, mode="r") |
| 728 | self.assertEqual(len(zipf2.namelist()), numfiles) |
| 729 | for i in range(numfiles): |
| 730 | content = zipf2.read("foo%08d" % i) |
| 731 | self.assertEqual(content, "%d" % (i**3 % 57)) |
| 732 | zipf2.close() |
| 733 | |
| 734 | def test_too_many_files_append(self): |
| 735 | zipf = zipfile.ZipFile(TESTFN, mode="w", allowZip64=False) |
| 736 | zipf.debug = 100 |
| 737 | numfiles = 9 |
| 738 | for i in range(numfiles): |
| 739 | zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57)) |
| 740 | self.assertEqual(len(zipf.namelist()), numfiles) |
| 741 | with self.assertRaises(zipfile.LargeZipFile): |
| 742 | zipf.writestr("foo%08d" % numfiles, b'') |
| 743 | self.assertEqual(len(zipf.namelist()), numfiles) |
| 744 | zipf.close() |
| 745 | |
| 746 | zipf = zipfile.ZipFile(TESTFN, mode="a", allowZip64=False) |
| 747 | zipf.debug = 100 |
| 748 | self.assertEqual(len(zipf.namelist()), numfiles) |
| 749 | with self.assertRaises(zipfile.LargeZipFile): |
| 750 | zipf.writestr("foo%08d" % numfiles, b'') |
| 751 | self.assertEqual(len(zipf.namelist()), numfiles) |
| 752 | zipf.close() |
| 753 | |
| 754 | zipf = zipfile.ZipFile(TESTFN, mode="a", allowZip64=True) |
| 755 | zipf.debug = 100 |
| 756 | self.assertEqual(len(zipf.namelist()), numfiles) |
| 757 | numfiles2 = 15 |
| 758 | for i in range(numfiles, numfiles2): |
| 759 | zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57)) |
| 760 | self.assertEqual(len(zipf.namelist()), numfiles2) |
| 761 | zipf.close() |
| 762 | |
| 763 | zipf2 = zipfile.ZipFile(TESTFN, mode="r") |
| 764 | self.assertEqual(len(zipf2.namelist()), numfiles2) |
| 765 | for i in range(numfiles2): |
| 766 | content = zipf2.read("foo%08d" % i) |
| 767 | self.assertEqual(content, "%d" % (i**3 % 57)) |
| 768 | zipf2.close() |
| 769 | |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 770 | def tearDown(self): |
| 771 | zipfile.ZIP64_LIMIT = self._limit |
Serhiy Storchaka | 1af262c | 2014-09-23 22:26:45 +0300 | [diff] [blame] | 772 | zipfile.ZIP_FILECOUNT_LIMIT = self._filecount_limit |
Ezio Melotti | 6cbfc12 | 2009-07-10 20:25:56 +0000 | [diff] [blame] | 773 | unlink(TESTFN) |
| 774 | unlink(TESTFN2) |
| 775 | |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 776 | |
| 777 | class PyZipFileTests(unittest.TestCase): |
Serhiy Storchaka | 4c2bada | 2015-02-14 23:17:13 +0200 | [diff] [blame] | 778 | def requiresWriteAccess(self, path): |
| 779 | if not os.access(path, os.W_OK): |
| 780 | self.skipTest('requires write access to the installed location') |
Serhiy Storchaka | 90f5f51 | 2015-09-19 10:55:20 +0300 | [diff] [blame] | 781 | filename = os.path.join(path, 'test_zipfile.try') |
| 782 | try: |
| 783 | fd = os.open(filename, os.O_WRONLY | os.O_CREAT) |
| 784 | os.close(fd) |
| 785 | except Exception: |
| 786 | self.skipTest('requires write access to the installed location') |
| 787 | unlink(filename) |
Serhiy Storchaka | 4c2bada | 2015-02-14 23:17:13 +0200 | [diff] [blame] | 788 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 789 | def test_write_pyfile(self): |
Serhiy Storchaka | 4c2bada | 2015-02-14 23:17:13 +0200 | [diff] [blame] | 790 | self.requiresWriteAccess(os.path.dirname(__file__)) |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 791 | with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp: |
| 792 | fn = __file__ |
| 793 | if fn.endswith('.pyc') or fn.endswith('.pyo'): |
| 794 | fn = fn[:-1] |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 795 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 796 | zipfp.writepy(fn) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 797 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 798 | bn = os.path.basename(fn) |
Ezio Melotti | aa98058 | 2010-01-23 23:04:36 +0000 | [diff] [blame] | 799 | self.assertNotIn(bn, zipfp.namelist()) |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 800 | self.assertTrue(bn + 'o' in zipfp.namelist() or |
| 801 | bn + 'c' in zipfp.namelist()) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 802 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 803 | with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp: |
| 804 | fn = __file__ |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 805 | if fn.endswith(('.pyc', '.pyo')): |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 806 | fn = fn[:-1] |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 807 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 808 | zipfp.writepy(fn, "testpackage") |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 809 | |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 810 | bn = "%s/%s" % ("testpackage", os.path.basename(fn)) |
Ezio Melotti | aa98058 | 2010-01-23 23:04:36 +0000 | [diff] [blame] | 811 | self.assertNotIn(bn, zipfp.namelist()) |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 812 | self.assertTrue(bn + 'o' in zipfp.namelist() or |
| 813 | bn + 'c' in zipfp.namelist()) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 814 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 815 | def test_write_python_package(self): |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 816 | import email |
| 817 | packagedir = os.path.dirname(email.__file__) |
Serhiy Storchaka | 4c2bada | 2015-02-14 23:17:13 +0200 | [diff] [blame] | 818 | self.requiresWriteAccess(packagedir) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 819 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 820 | with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp: |
| 821 | zipfp.writepy(packagedir) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 822 | |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 823 | # Check for a couple of modules at different levels of the |
| 824 | # hierarchy |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 825 | names = zipfp.namelist() |
| 826 | self.assertTrue('email/__init__.pyo' in names or |
| 827 | 'email/__init__.pyc' in names) |
| 828 | self.assertTrue('email/mime/text.pyo' in names or |
| 829 | 'email/mime/text.pyc' in names) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 830 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 831 | def test_write_python_directory(self): |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 832 | os.mkdir(TESTFN2) |
| 833 | try: |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 834 | with open(os.path.join(TESTFN2, "mod1.py"), "w") as fp: |
Ezio Melotti | 763f1e8 | 2009-12-31 13:27:41 +0000 | [diff] [blame] | 835 | fp.write("print(42)\n") |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 836 | |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 837 | with open(os.path.join(TESTFN2, "mod2.py"), "w") as fp: |
Ezio Melotti | 763f1e8 | 2009-12-31 13:27:41 +0000 | [diff] [blame] | 838 | fp.write("print(42 * 42)\n") |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 839 | |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 840 | with open(os.path.join(TESTFN2, "mod2.txt"), "w") as fp: |
| 841 | fp.write("bla bla bla\n") |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 842 | |
| 843 | zipfp = zipfile.PyZipFile(TemporaryFile(), "w") |
| 844 | zipfp.writepy(TESTFN2) |
| 845 | |
| 846 | names = zipfp.namelist() |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 847 | self.assertTrue('mod1.pyc' in names or 'mod1.pyo' in names) |
| 848 | self.assertTrue('mod2.pyc' in names or 'mod2.pyo' in names) |
Ezio Melotti | aa98058 | 2010-01-23 23:04:36 +0000 | [diff] [blame] | 849 | self.assertNotIn('mod2.txt', names) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 850 | |
| 851 | finally: |
Benjamin Peterson | 352eb4f | 2014-04-03 10:31:25 -0400 | [diff] [blame] | 852 | rmtree(TESTFN2) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 853 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 854 | def test_write_non_pyfile(self): |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 855 | with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp: |
Benjamin Peterson | 352eb4f | 2014-04-03 10:31:25 -0400 | [diff] [blame] | 856 | with open(TESTFN, 'w') as fid: |
| 857 | fid.write('most definitely not a python file') |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 858 | self.assertRaises(RuntimeError, zipfp.writepy, TESTFN) |
| 859 | os.remove(TESTFN) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 860 | |
| 861 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 862 | class OtherTests(unittest.TestCase): |
Antoine Pitrou | e1436d1 | 2010-08-12 15:25:51 +0000 | [diff] [blame] | 863 | zips_with_bad_crc = { |
| 864 | zipfile.ZIP_STORED: ( |
| 865 | b'PK\003\004\024\0\0\0\0\0 \213\212;:r' |
| 866 | b'\253\377\f\0\0\0\f\0\0\0\005\0\0\000af' |
| 867 | b'ilehello,AworldP' |
| 868 | b'K\001\002\024\003\024\0\0\0\0\0 \213\212;:' |
| 869 | b'r\253\377\f\0\0\0\f\0\0\0\005\0\0\0\0' |
| 870 | b'\0\0\0\0\0\0\0\200\001\0\0\0\000afi' |
| 871 | b'lePK\005\006\0\0\0\0\001\0\001\0003\000' |
| 872 | b'\0\0/\0\0\0\0\0'), |
| 873 | zipfile.ZIP_DEFLATED: ( |
| 874 | b'PK\x03\x04\x14\x00\x00\x00\x08\x00n}\x0c=FA' |
| 875 | b'KE\x10\x00\x00\x00n\x00\x00\x00\x05\x00\x00\x00af' |
| 876 | b'ile\xcbH\xcd\xc9\xc9W(\xcf/\xcaI\xc9\xa0' |
| 877 | b'=\x13\x00PK\x01\x02\x14\x03\x14\x00\x00\x00\x08\x00n' |
| 878 | b'}\x0c=FAKE\x10\x00\x00\x00n\x00\x00\x00\x05' |
| 879 | b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x01\x00\x00\x00' |
| 880 | b'\x00afilePK\x05\x06\x00\x00\x00\x00\x01\x00' |
| 881 | b'\x01\x003\x00\x00\x003\x00\x00\x00\x00\x00'), |
| 882 | } |
| 883 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 884 | def test_unicode_filenames(self): |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 885 | with zipfile.ZipFile(TESTFN, "w") as zf: |
| 886 | zf.writestr(u"foo.txt", "Test for unicode filename") |
| 887 | zf.writestr(u"\xf6.txt", "Test for unicode filename") |
Ezio Melotti | b0f5adc | 2010-01-24 16:58:36 +0000 | [diff] [blame] | 888 | self.assertIsInstance(zf.infolist()[0].filename, unicode) |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 889 | |
| 890 | with zipfile.ZipFile(TESTFN, "r") as zf: |
| 891 | self.assertEqual(zf.filelist[0].filename, "foo.txt") |
| 892 | self.assertEqual(zf.filelist[1].filename, u"\xf6.txt") |
Martin v. Löwis | 471617d | 2008-05-05 17:16:58 +0000 | [diff] [blame] | 893 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 894 | def test_create_non_existent_file_for_append(self): |
Martin v. Löwis | 84f6de9 | 2007-02-13 10:10:39 +0000 | [diff] [blame] | 895 | if os.path.exists(TESTFN): |
| 896 | os.unlink(TESTFN) |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 897 | |
Martin v. Löwis | 84f6de9 | 2007-02-13 10:10:39 +0000 | [diff] [blame] | 898 | filename = 'testfile.txt' |
| 899 | content = 'hello, world. this is some content.' |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 900 | |
Martin v. Löwis | 84f6de9 | 2007-02-13 10:10:39 +0000 | [diff] [blame] | 901 | try: |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 902 | with zipfile.ZipFile(TESTFN, 'a') as zf: |
| 903 | zf.writestr(filename, content) |
Ezio Melotti | 6cbfc12 | 2009-07-10 20:25:56 +0000 | [diff] [blame] | 904 | except IOError: |
Martin v. Löwis | 84f6de9 | 2007-02-13 10:10:39 +0000 | [diff] [blame] | 905 | self.fail('Could not append data to a non-existent zip file.') |
| 906 | |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 907 | self.assertTrue(os.path.exists(TESTFN)) |
Martin v. Löwis | 84f6de9 | 2007-02-13 10:10:39 +0000 | [diff] [blame] | 908 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 909 | with zipfile.ZipFile(TESTFN, 'r') as zf: |
| 910 | self.assertEqual(zf.read(filename), content) |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 911 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 912 | def test_close_erroneous_file(self): |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 913 | # This test checks that the ZipFile constructor closes the file object |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 914 | # it opens if there's an error in the file. If it doesn't, the |
| 915 | # traceback holds a reference to the ZipFile object and, indirectly, |
| 916 | # the file object. |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 917 | # On Windows, this causes the os.unlink() call to fail because the |
| 918 | # underlying file is still open. This is SF bug #412214. |
| 919 | # |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 920 | with open(TESTFN, "w") as fp: |
| 921 | fp.write("this is not a legal zip file\n") |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 922 | try: |
| 923 | zf = zipfile.ZipFile(TESTFN) |
| 924 | except zipfile.BadZipfile: |
Collin Winter | 04a51ec | 2007-03-29 02:28:16 +0000 | [diff] [blame] | 925 | pass |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 926 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 927 | def test_is_zip_erroneous_file(self): |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 928 | """Check that is_zipfile() correctly identifies non-zip files.""" |
Antoine Pitrou | 6f193e0 | 2008-12-27 15:43:12 +0000 | [diff] [blame] | 929 | # - passing a filename |
| 930 | with open(TESTFN, "w") as fp: |
| 931 | fp.write("this is not a legal zip file\n") |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 932 | chk = zipfile.is_zipfile(TESTFN) |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 933 | self.assertFalse(chk) |
Antoine Pitrou | 6f193e0 | 2008-12-27 15:43:12 +0000 | [diff] [blame] | 934 | # - passing a file object |
| 935 | with open(TESTFN, "rb") as fp: |
| 936 | chk = zipfile.is_zipfile(fp) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 937 | self.assertTrue(not chk) |
Antoine Pitrou | 6f193e0 | 2008-12-27 15:43:12 +0000 | [diff] [blame] | 938 | # - passing a file-like object |
| 939 | fp = StringIO() |
| 940 | fp.write("this is not a legal zip file\n") |
| 941 | chk = zipfile.is_zipfile(fp) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 942 | self.assertTrue(not chk) |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 943 | fp.seek(0, 0) |
Antoine Pitrou | 6f193e0 | 2008-12-27 15:43:12 +0000 | [diff] [blame] | 944 | chk = zipfile.is_zipfile(fp) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 945 | self.assertTrue(not chk) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 946 | |
Serhiy Storchaka | 0be506a | 2013-01-31 15:26:55 +0200 | [diff] [blame] | 947 | def test_damaged_zipfile(self): |
| 948 | """Check that zipfiles with missing bytes at the end raise BadZipFile.""" |
| 949 | # - Create a valid zip file |
| 950 | fp = io.BytesIO() |
| 951 | with zipfile.ZipFile(fp, mode="w") as zipf: |
| 952 | zipf.writestr("foo.txt", b"O, for a Muse of Fire!") |
| 953 | zipfiledata = fp.getvalue() |
| 954 | |
| 955 | # - Now create copies of it missing the last N bytes and make sure |
| 956 | # a BadZipFile exception is raised when we try to open it |
| 957 | for N in range(len(zipfiledata)): |
| 958 | fp = io.BytesIO(zipfiledata[:N]) |
| 959 | self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, fp) |
| 960 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 961 | def test_is_zip_valid_file(self): |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 962 | """Check that is_zipfile() correctly identifies zip files.""" |
Antoine Pitrou | 6f193e0 | 2008-12-27 15:43:12 +0000 | [diff] [blame] | 963 | # - passing a filename |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 964 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 965 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 966 | chk = zipfile.is_zipfile(TESTFN) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 967 | self.assertTrue(chk) |
Antoine Pitrou | 6f193e0 | 2008-12-27 15:43:12 +0000 | [diff] [blame] | 968 | # - passing a file object |
| 969 | with open(TESTFN, "rb") as fp: |
| 970 | chk = zipfile.is_zipfile(fp) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 971 | self.assertTrue(chk) |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 972 | fp.seek(0, 0) |
Antoine Pitrou | 6f193e0 | 2008-12-27 15:43:12 +0000 | [diff] [blame] | 973 | zip_contents = fp.read() |
| 974 | # - passing a file-like object |
| 975 | fp = StringIO() |
| 976 | fp.write(zip_contents) |
| 977 | chk = zipfile.is_zipfile(fp) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 978 | self.assertTrue(chk) |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 979 | fp.seek(0, 0) |
Antoine Pitrou | 6f193e0 | 2008-12-27 15:43:12 +0000 | [diff] [blame] | 980 | chk = zipfile.is_zipfile(fp) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 981 | self.assertTrue(chk) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 982 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 983 | def test_non_existent_file_raises_IOError(self): |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 984 | # make sure we don't raise an AttributeError when a partially-constructed |
| 985 | # ZipFile instance is finalized; this tests for regression on SF tracker |
| 986 | # bug #403871. |
| 987 | |
| 988 | # The bug we're testing for caused an AttributeError to be raised |
| 989 | # when a ZipFile instance was created for a file that did not |
| 990 | # exist; the .fp member was not initialized but was needed by the |
| 991 | # __del__() method. Since the AttributeError is in the __del__(), |
| 992 | # it is ignored, but the user should be sufficiently annoyed by |
| 993 | # the message on the output that regression will be noticed |
| 994 | # quickly. |
| 995 | self.assertRaises(IOError, zipfile.ZipFile, TESTFN) |
| 996 | |
Amaury Forgeot d'Arc | 3e5b027 | 2009-07-28 22:15:30 +0000 | [diff] [blame] | 997 | def test_empty_file_raises_BadZipFile(self): |
Brian Curtin | 0d65433 | 2011-04-19 21:15:55 -0500 | [diff] [blame] | 998 | with open(TESTFN, 'w') as f: |
| 999 | pass |
Amaury Forgeot d'Arc | 3e5b027 | 2009-07-28 22:15:30 +0000 | [diff] [blame] | 1000 | self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN) |
| 1001 | |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 1002 | with open(TESTFN, 'w') as fp: |
| 1003 | fp.write("short file") |
Amaury Forgeot d'Arc | 3e5b027 | 2009-07-28 22:15:30 +0000 | [diff] [blame] | 1004 | self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN) |
| 1005 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1006 | def test_closed_zip_raises_RuntimeError(self): |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 1007 | """Verify that testzip() doesn't swallow inappropriate exceptions.""" |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 1008 | data = StringIO() |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 1009 | with zipfile.ZipFile(data, mode="w") as zipf: |
| 1010 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 1011 | |
Andrew Svetlov | 4bb142b | 2012-12-18 21:27:37 +0200 | [diff] [blame] | 1012 | # This is correct; calling .read on a closed ZipFile should raise |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 1013 | # a RuntimeError, and so should calling .testzip. An earlier |
| 1014 | # version of .testzip would swallow this exception (and any other) |
| 1015 | # and report that the first file in the archive was corrupt. |
Georg Brandl | 4b3ab6f | 2007-07-12 09:59:22 +0000 | [diff] [blame] | 1016 | self.assertRaises(RuntimeError, zipf.read, "foo.txt") |
| 1017 | self.assertRaises(RuntimeError, zipf.open, "foo.txt") |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 1018 | self.assertRaises(RuntimeError, zipf.testzip) |
Georg Brandl | 4b3ab6f | 2007-07-12 09:59:22 +0000 | [diff] [blame] | 1019 | self.assertRaises(RuntimeError, zipf.writestr, "bogus.txt", "bogus") |
Benjamin Peterson | 352eb4f | 2014-04-03 10:31:25 -0400 | [diff] [blame] | 1020 | with open(TESTFN, 'w') as fid: |
| 1021 | fid.write('zipfile test data') |
| 1022 | self.assertRaises(RuntimeError, zipf.write, TESTFN) |
Georg Brandl | 4b3ab6f | 2007-07-12 09:59:22 +0000 | [diff] [blame] | 1023 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1024 | def test_bad_constructor_mode(self): |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 1025 | """Check that bad modes passed to ZipFile constructor are caught.""" |
Georg Brandl | 4b3ab6f | 2007-07-12 09:59:22 +0000 | [diff] [blame] | 1026 | self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "q") |
| 1027 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1028 | def test_bad_open_mode(self): |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 1029 | """Check that bad modes passed to ZipFile.open are caught.""" |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 1030 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 1031 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 1032 | |
| 1033 | with zipfile.ZipFile(TESTFN, mode="r") as zipf: |
Georg Brandl | 4b3ab6f | 2007-07-12 09:59:22 +0000 | [diff] [blame] | 1034 | # read the data to make sure the file is there |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 1035 | zipf.read("foo.txt") |
| 1036 | self.assertRaises(RuntimeError, zipf.open, "foo.txt", "q") |
Georg Brandl | 4b3ab6f | 2007-07-12 09:59:22 +0000 | [diff] [blame] | 1037 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1038 | def test_read0(self): |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 1039 | """Check that calling read(0) on a ZipExtFile object returns an empty |
| 1040 | string and doesn't advance file pointer.""" |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 1041 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 1042 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 1043 | # read the data to make sure the file is there |
Brian Curtin | 0d65433 | 2011-04-19 21:15:55 -0500 | [diff] [blame] | 1044 | with zipf.open("foo.txt") as f: |
| 1045 | for i in xrange(FIXEDTEST_SIZE): |
| 1046 | self.assertEqual(f.read(0), '') |
Georg Brandl | 4b3ab6f | 2007-07-12 09:59:22 +0000 | [diff] [blame] | 1047 | |
Brian Curtin | 0d65433 | 2011-04-19 21:15:55 -0500 | [diff] [blame] | 1048 | self.assertEqual(f.read(), "O, for a Muse of Fire!") |
Georg Brandl | 4b3ab6f | 2007-07-12 09:59:22 +0000 | [diff] [blame] | 1049 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1050 | def test_open_non_existent_item(self): |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 1051 | """Check that attempting to call open() for an item that doesn't |
| 1052 | exist in the archive raises a RuntimeError.""" |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 1053 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 1054 | self.assertRaises(KeyError, zipf.open, "foo.txt", "r") |
Georg Brandl | 4b3ab6f | 2007-07-12 09:59:22 +0000 | [diff] [blame] | 1055 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1056 | def test_bad_compression_mode(self): |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 1057 | """Check that bad compression methods passed to ZipFile.open are |
| 1058 | caught.""" |
Georg Brandl | 4b3ab6f | 2007-07-12 09:59:22 +0000 | [diff] [blame] | 1059 | self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "w", -1) |
| 1060 | |
Ezio Melotti | 9e94972 | 2012-11-18 13:18:06 +0200 | [diff] [blame] | 1061 | def test_unsupported_compression(self): |
| 1062 | # data is declared as shrunk, but actually deflated |
| 1063 | data = (b'PK\x03\x04.\x00\x00\x00\x01\x00\xe4C\xa1@\x00\x00\x00' |
| 1064 | b'\x00\x02\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00x\x03\x00PK\x01' |
| 1065 | b'\x02.\x03.\x00\x00\x00\x01\x00\xe4C\xa1@\x00\x00\x00\x00\x02\x00\x00' |
| 1066 | b'\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' |
| 1067 | b'\x80\x01\x00\x00\x00\x00xPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x00' |
| 1068 | b'/\x00\x00\x00!\x00\x00\x00\x00\x00') |
| 1069 | with zipfile.ZipFile(io.BytesIO(data), 'r') as zipf: |
| 1070 | self.assertRaises(NotImplementedError, zipf.open, 'x') |
| 1071 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1072 | def test_null_byte_in_filename(self): |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 1073 | """Check that a filename containing a null byte is properly |
| 1074 | terminated.""" |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 1075 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 1076 | zipf.writestr("foo.txt\x00qqq", "O, for a Muse of Fire!") |
| 1077 | self.assertEqual(zipf.namelist(), ['foo.txt']) |
Neal Norwitz | 0d4c06e | 2007-04-25 06:30:05 +0000 | [diff] [blame] | 1078 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1079 | def test_struct_sizes(self): |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 1080 | """Check that ZIP internal structure sizes are calculated correctly.""" |
Martin v. Löwis | 8c43641 | 2008-07-03 12:51:14 +0000 | [diff] [blame] | 1081 | self.assertEqual(zipfile.sizeEndCentDir, 22) |
| 1082 | self.assertEqual(zipfile.sizeCentralDir, 46) |
| 1083 | self.assertEqual(zipfile.sizeEndCentDir64, 56) |
| 1084 | self.assertEqual(zipfile.sizeEndCentDir64Locator, 20) |
| 1085 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1086 | def test_comments(self): |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 1087 | """Check that comments on the archive are handled properly.""" |
Martin v. Löwis | 8c43641 | 2008-07-03 12:51:14 +0000 | [diff] [blame] | 1088 | |
| 1089 | # check default comment is empty |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 1090 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 1091 | self.assertEqual(zipf.comment, '') |
| 1092 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 1093 | |
| 1094 | with zipfile.ZipFile(TESTFN, mode="r") as zipf: |
| 1095 | self.assertEqual(zipf.comment, '') |
Martin v. Löwis | 8c43641 | 2008-07-03 12:51:14 +0000 | [diff] [blame] | 1096 | |
| 1097 | # check a simple short comment |
| 1098 | comment = 'Bravely taking to his feet, he beat a very brave retreat.' |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 1099 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 1100 | zipf.comment = comment |
| 1101 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 1102 | with zipfile.ZipFile(TESTFN, mode="r") as zipf: |
| 1103 | self.assertEqual(zipf.comment, comment) |
Martin v. Löwis | 8c43641 | 2008-07-03 12:51:14 +0000 | [diff] [blame] | 1104 | |
| 1105 | # check a comment of max length |
| 1106 | comment2 = ''.join(['%d' % (i**3 % 10) for i in xrange((1 << 16)-1)]) |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 1107 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 1108 | zipf.comment = comment2 |
| 1109 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 1110 | |
| 1111 | with zipfile.ZipFile(TESTFN, mode="r") as zipf: |
| 1112 | self.assertEqual(zipf.comment, comment2) |
Martin v. Löwis | 8c43641 | 2008-07-03 12:51:14 +0000 | [diff] [blame] | 1113 | |
| 1114 | # check a comment that is too long is truncated |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 1115 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
Serhiy Storchaka | 4925935 | 2014-01-20 21:57:09 +0200 | [diff] [blame] | 1116 | with check_warnings(('', UserWarning)): |
| 1117 | zipf.comment = comment2 + 'oops' |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 1118 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 1119 | with zipfile.ZipFile(TESTFN, mode="r") as zipf: |
| 1120 | self.assertEqual(zipf.comment, comment2) |
Martin v. Löwis | 8c43641 | 2008-07-03 12:51:14 +0000 | [diff] [blame] | 1121 | |
R David Murray | 3f4ccba | 2012-04-12 18:42:47 -0400 | [diff] [blame] | 1122 | def test_change_comment_in_empty_archive(self): |
| 1123 | with zipfile.ZipFile(TESTFN, "a", zipfile.ZIP_STORED) as zipf: |
| 1124 | self.assertFalse(zipf.filelist) |
| 1125 | zipf.comment = b"this is a comment" |
| 1126 | with zipfile.ZipFile(TESTFN, "r") as zipf: |
| 1127 | self.assertEqual(zipf.comment, b"this is a comment") |
| 1128 | |
| 1129 | def test_change_comment_in_nonempty_archive(self): |
| 1130 | with zipfile.ZipFile(TESTFN, "w", zipfile.ZIP_STORED) as zipf: |
| 1131 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 1132 | with zipfile.ZipFile(TESTFN, "a", zipfile.ZIP_STORED) as zipf: |
| 1133 | self.assertTrue(zipf.filelist) |
| 1134 | zipf.comment = b"this is a comment" |
| 1135 | with zipfile.ZipFile(TESTFN, "r") as zipf: |
| 1136 | self.assertEqual(zipf.comment, b"this is a comment") |
| 1137 | |
Antoine Pitrou | e1436d1 | 2010-08-12 15:25:51 +0000 | [diff] [blame] | 1138 | def check_testzip_with_bad_crc(self, compression): |
| 1139 | """Tests that files with bad CRCs return their name from testzip.""" |
| 1140 | zipdata = self.zips_with_bad_crc[compression] |
| 1141 | |
| 1142 | with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf: |
| 1143 | # testzip returns the name of the first corrupt file, or None |
| 1144 | self.assertEqual('afile', zipf.testzip()) |
| 1145 | |
| 1146 | def test_testzip_with_bad_crc_stored(self): |
| 1147 | self.check_testzip_with_bad_crc(zipfile.ZIP_STORED) |
| 1148 | |
| 1149 | @skipUnless(zlib, "requires zlib") |
| 1150 | def test_testzip_with_bad_crc_deflated(self): |
| 1151 | self.check_testzip_with_bad_crc(zipfile.ZIP_DEFLATED) |
| 1152 | |
| 1153 | def check_read_with_bad_crc(self, compression): |
| 1154 | """Tests that files with bad CRCs raise a BadZipfile exception when read.""" |
| 1155 | zipdata = self.zips_with_bad_crc[compression] |
| 1156 | |
| 1157 | # Using ZipFile.read() |
| 1158 | with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf: |
| 1159 | self.assertRaises(zipfile.BadZipfile, zipf.read, 'afile') |
| 1160 | |
| 1161 | # Using ZipExtFile.read() |
| 1162 | with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf: |
| 1163 | with zipf.open('afile', 'r') as corrupt_file: |
| 1164 | self.assertRaises(zipfile.BadZipfile, corrupt_file.read) |
| 1165 | |
| 1166 | # Same with small reads (in order to exercise the buffering logic) |
| 1167 | with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf: |
| 1168 | with zipf.open('afile', 'r') as corrupt_file: |
| 1169 | corrupt_file.MIN_READ_SIZE = 2 |
| 1170 | with self.assertRaises(zipfile.BadZipfile): |
| 1171 | while corrupt_file.read(2): |
| 1172 | pass |
| 1173 | |
| 1174 | def test_read_with_bad_crc_stored(self): |
| 1175 | self.check_read_with_bad_crc(zipfile.ZIP_STORED) |
| 1176 | |
| 1177 | @skipUnless(zlib, "requires zlib") |
| 1178 | def test_read_with_bad_crc_deflated(self): |
| 1179 | self.check_read_with_bad_crc(zipfile.ZIP_DEFLATED) |
| 1180 | |
Antoine Pitrou | e4195e8 | 2010-09-12 14:56:27 +0000 | [diff] [blame] | 1181 | def check_read_return_size(self, compression): |
| 1182 | # Issue #9837: ZipExtFile.read() shouldn't return more bytes |
| 1183 | # than requested. |
| 1184 | for test_size in (1, 4095, 4096, 4097, 16384): |
| 1185 | file_size = test_size + 1 |
Serhiy Storchaka | 45aa771 | 2014-12-03 09:11:12 +0200 | [diff] [blame] | 1186 | junk = getrandbytes(file_size) |
Antoine Pitrou | e4195e8 | 2010-09-12 14:56:27 +0000 | [diff] [blame] | 1187 | with zipfile.ZipFile(io.BytesIO(), "w", compression) as zipf: |
| 1188 | zipf.writestr('foo', junk) |
| 1189 | with zipf.open('foo', 'r') as fp: |
| 1190 | buf = fp.read(test_size) |
| 1191 | self.assertEqual(len(buf), test_size) |
| 1192 | |
| 1193 | def test_read_return_size_stored(self): |
| 1194 | self.check_read_return_size(zipfile.ZIP_STORED) |
| 1195 | |
| 1196 | @skipUnless(zlib, "requires zlib") |
| 1197 | def test_read_return_size_deflated(self): |
| 1198 | self.check_read_return_size(zipfile.ZIP_DEFLATED) |
| 1199 | |
Georg Brandl | 86e0c89 | 2010-11-26 07:22:28 +0000 | [diff] [blame] | 1200 | def test_empty_zipfile(self): |
| 1201 | # Check that creating a file in 'w' or 'a' mode and closing without |
| 1202 | # adding any files to the archives creates a valid empty ZIP file |
Brian Curtin | 0d65433 | 2011-04-19 21:15:55 -0500 | [diff] [blame] | 1203 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 1204 | pass |
Georg Brandl | 86e0c89 | 2010-11-26 07:22:28 +0000 | [diff] [blame] | 1205 | try: |
| 1206 | zipf = zipfile.ZipFile(TESTFN, mode="r") |
Benjamin Peterson | 352eb4f | 2014-04-03 10:31:25 -0400 | [diff] [blame] | 1207 | zipf.close() |
Éric Araujo | 67843b3 | 2011-02-02 17:03:38 +0000 | [diff] [blame] | 1208 | except zipfile.BadZipfile: |
Georg Brandl | 86e0c89 | 2010-11-26 07:22:28 +0000 | [diff] [blame] | 1209 | self.fail("Unable to create empty ZIP file in 'w' mode") |
| 1210 | |
Brian Curtin | 0d65433 | 2011-04-19 21:15:55 -0500 | [diff] [blame] | 1211 | with zipfile.ZipFile(TESTFN, mode="a") as zipf: |
| 1212 | pass |
Georg Brandl | 86e0c89 | 2010-11-26 07:22:28 +0000 | [diff] [blame] | 1213 | try: |
| 1214 | zipf = zipfile.ZipFile(TESTFN, mode="r") |
Benjamin Peterson | 352eb4f | 2014-04-03 10:31:25 -0400 | [diff] [blame] | 1215 | zipf.close() |
Georg Brandl | 86e0c89 | 2010-11-26 07:22:28 +0000 | [diff] [blame] | 1216 | except: |
| 1217 | self.fail("Unable to create empty ZIP file in 'a' mode") |
| 1218 | |
| 1219 | def test_open_empty_file(self): |
| 1220 | # Issue 1710703: Check that opening a file with less than 22 bytes |
| 1221 | # raises a BadZipfile exception (rather than the previously unhelpful |
| 1222 | # IOError) |
Brian Curtin | 0d65433 | 2011-04-19 21:15:55 -0500 | [diff] [blame] | 1223 | with open(TESTFN, 'w') as f: |
| 1224 | pass |
Georg Brandl | 86e0c89 | 2010-11-26 07:22:28 +0000 | [diff] [blame] | 1225 | self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN, 'r') |
| 1226 | |
Senthil Kumaran | ddd4031 | 2011-10-20 01:38:35 +0800 | [diff] [blame] | 1227 | def test_create_zipinfo_before_1980(self): |
| 1228 | self.assertRaises(ValueError, |
| 1229 | zipfile.ZipInfo, 'seventies', (1979, 1, 1, 0, 0, 0)) |
| 1230 | |
Gregory P. Smith | 0344a06 | 2014-05-29 23:41:52 -0700 | [diff] [blame] | 1231 | def test_zipfile_with_short_extra_field(self): |
| 1232 | """If an extra field in the header is less than 4 bytes, skip it.""" |
| 1233 | zipdata = ( |
| 1234 | b'PK\x03\x04\x14\x00\x00\x00\x00\x00\x93\x9b\xad@\x8b\x9e' |
| 1235 | b'\xd9\xd3\x01\x00\x00\x00\x01\x00\x00\x00\x03\x00\x03\x00ab' |
| 1236 | b'c\x00\x00\x00APK\x01\x02\x14\x03\x14\x00\x00\x00\x00' |
| 1237 | b'\x00\x93\x9b\xad@\x8b\x9e\xd9\xd3\x01\x00\x00\x00\x01\x00\x00' |
| 1238 | b'\x00\x03\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa4\x81\x00' |
| 1239 | b'\x00\x00\x00abc\x00\x00PK\x05\x06\x00\x00\x00\x00' |
| 1240 | b'\x01\x00\x01\x003\x00\x00\x00%\x00\x00\x00\x00\x00' |
| 1241 | ) |
| 1242 | with zipfile.ZipFile(io.BytesIO(zipdata), 'r') as zipf: |
| 1243 | # testzip returns the name of the first corrupt file, or None |
| 1244 | self.assertIsNone(zipf.testzip()) |
| 1245 | |
Collin Winter | 04a51ec | 2007-03-29 02:28:16 +0000 | [diff] [blame] | 1246 | def tearDown(self): |
Ezio Melotti | 6cbfc12 | 2009-07-10 20:25:56 +0000 | [diff] [blame] | 1247 | unlink(TESTFN) |
| 1248 | unlink(TESTFN2) |
| 1249 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 1250 | |
Martin v. Löwis | c6d626e | 2007-02-13 09:49:38 +0000 | [diff] [blame] | 1251 | class DecryptionTests(unittest.TestCase): |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 1252 | """Check that ZIP decryption works. Since the library does not |
| 1253 | support encryption at the moment, we use a pre-generated encrypted |
| 1254 | ZIP file.""" |
Martin v. Löwis | c6d626e | 2007-02-13 09:49:38 +0000 | [diff] [blame] | 1255 | |
| 1256 | data = ( |
| 1257 | 'PK\x03\x04\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00\x1a\x00' |
| 1258 | '\x00\x00\x08\x00\x00\x00test.txt\xfa\x10\xa0gly|\xfa-\xc5\xc0=\xf9y' |
| 1259 | '\x18\xe0\xa8r\xb3Z}Lg\xbc\xae\xf9|\x9b\x19\xe4\x8b\xba\xbb)\x8c\xb0\xdbl' |
| 1260 | 'PK\x01\x02\x14\x00\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00' |
| 1261 | '\x1a\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x01\x00 \x00\xb6\x81' |
| 1262 | '\x00\x00\x00\x00test.txtPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x006\x00' |
| 1263 | '\x00\x00L\x00\x00\x00\x00\x00' ) |
Gregory P. Smith | 0c63fc2 | 2008-01-20 01:21:03 +0000 | [diff] [blame] | 1264 | data2 = ( |
| 1265 | 'PK\x03\x04\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02' |
| 1266 | '\x00\x00\x04\x00\x15\x00zeroUT\t\x00\x03\xd6\x8b\x92G\xda\x8b\x92GUx\x04' |
| 1267 | '\x00\xe8\x03\xe8\x03\xc7<M\xb5a\xceX\xa3Y&\x8b{oE\xd7\x9d\x8c\x98\x02\xc0' |
| 1268 | 'PK\x07\x08xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00PK\x01\x02\x17\x03' |
| 1269 | '\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00' |
| 1270 | '\x04\x00\r\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa4\x81\x00\x00\x00\x00ze' |
| 1271 | 'roUT\x05\x00\x03\xd6\x8b\x92GUx\x00\x00PK\x05\x06\x00\x00\x00\x00\x01' |
| 1272 | '\x00\x01\x00?\x00\x00\x00[\x00\x00\x00\x00\x00' ) |
Martin v. Löwis | c6d626e | 2007-02-13 09:49:38 +0000 | [diff] [blame] | 1273 | |
| 1274 | plain = 'zipfile.py encryption test' |
Gregory P. Smith | 0c63fc2 | 2008-01-20 01:21:03 +0000 | [diff] [blame] | 1275 | plain2 = '\x00'*512 |
Martin v. Löwis | c6d626e | 2007-02-13 09:49:38 +0000 | [diff] [blame] | 1276 | |
| 1277 | def setUp(self): |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 1278 | with open(TESTFN, "wb") as fp: |
| 1279 | fp.write(self.data) |
Martin v. Löwis | c6d626e | 2007-02-13 09:49:38 +0000 | [diff] [blame] | 1280 | self.zip = zipfile.ZipFile(TESTFN, "r") |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 1281 | with open(TESTFN2, "wb") as fp: |
| 1282 | fp.write(self.data2) |
Gregory P. Smith | 0c63fc2 | 2008-01-20 01:21:03 +0000 | [diff] [blame] | 1283 | self.zip2 = zipfile.ZipFile(TESTFN2, "r") |
Martin v. Löwis | c6d626e | 2007-02-13 09:49:38 +0000 | [diff] [blame] | 1284 | |
| 1285 | def tearDown(self): |
| 1286 | self.zip.close() |
| 1287 | os.unlink(TESTFN) |
Gregory P. Smith | 0c63fc2 | 2008-01-20 01:21:03 +0000 | [diff] [blame] | 1288 | self.zip2.close() |
| 1289 | os.unlink(TESTFN2) |
Martin v. Löwis | c6d626e | 2007-02-13 09:49:38 +0000 | [diff] [blame] | 1290 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1291 | def test_no_password(self): |
Martin v. Löwis | c6d626e | 2007-02-13 09:49:38 +0000 | [diff] [blame] | 1292 | # Reading the encrypted file without password |
| 1293 | # must generate a RunTime exception |
| 1294 | self.assertRaises(RuntimeError, self.zip.read, "test.txt") |
Gregory P. Smith | 0c63fc2 | 2008-01-20 01:21:03 +0000 | [diff] [blame] | 1295 | self.assertRaises(RuntimeError, self.zip2.read, "zero") |
Martin v. Löwis | c6d626e | 2007-02-13 09:49:38 +0000 | [diff] [blame] | 1296 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1297 | def test_bad_password(self): |
Martin v. Löwis | c6d626e | 2007-02-13 09:49:38 +0000 | [diff] [blame] | 1298 | self.zip.setpassword("perl") |
| 1299 | self.assertRaises(RuntimeError, self.zip.read, "test.txt") |
Gregory P. Smith | 0c63fc2 | 2008-01-20 01:21:03 +0000 | [diff] [blame] | 1300 | self.zip2.setpassword("perl") |
| 1301 | self.assertRaises(RuntimeError, self.zip2.read, "zero") |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1302 | |
Ezio Melotti | 1036a7f | 2009-09-12 14:43:43 +0000 | [diff] [blame] | 1303 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1304 | def test_good_password(self): |
Martin v. Löwis | c6d626e | 2007-02-13 09:49:38 +0000 | [diff] [blame] | 1305 | self.zip.setpassword("python") |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 1306 | self.assertEqual(self.zip.read("test.txt"), self.plain) |
Gregory P. Smith | 0c63fc2 | 2008-01-20 01:21:03 +0000 | [diff] [blame] | 1307 | self.zip2.setpassword("12345") |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 1308 | self.assertEqual(self.zip2.read("zero"), self.plain2) |
Martin v. Löwis | c6d626e | 2007-02-13 09:49:38 +0000 | [diff] [blame] | 1309 | |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1310 | |
| 1311 | class TestsWithRandomBinaryFiles(unittest.TestCase): |
| 1312 | def setUp(self): |
| 1313 | datacount = randint(16, 64)*1024 + randint(1, 1024) |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 1314 | self.data = ''.join(struct.pack('<f', random()*randint(-1000, 1000)) |
Ezio Melotti | 763f1e8 | 2009-12-31 13:27:41 +0000 | [diff] [blame] | 1315 | for i in xrange(datacount)) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1316 | |
| 1317 | # Make a source file with some lines |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 1318 | with open(TESTFN, "wb") as fp: |
| 1319 | fp.write(self.data) |
Neal Norwitz | 0d4c06e | 2007-04-25 06:30:05 +0000 | [diff] [blame] | 1320 | |
Collin Winter | 04a51ec | 2007-03-29 02:28:16 +0000 | [diff] [blame] | 1321 | def tearDown(self): |
Ezio Melotti | 6cbfc12 | 2009-07-10 20:25:56 +0000 | [diff] [blame] | 1322 | unlink(TESTFN) |
| 1323 | unlink(TESTFN2) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1324 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1325 | def make_test_archive(self, f, compression): |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1326 | # Create the ZIP archive |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 1327 | with zipfile.ZipFile(f, "w", compression) as zipfp: |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 1328 | zipfp.write(TESTFN, "another.name") |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 1329 | zipfp.write(TESTFN, TESTFN) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1330 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1331 | def zip_test(self, f, compression): |
| 1332 | self.make_test_archive(f, compression) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1333 | |
| 1334 | # Read the ZIP archive |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 1335 | with zipfile.ZipFile(f, "r", compression) as zipfp: |
| 1336 | testdata = zipfp.read(TESTFN) |
| 1337 | self.assertEqual(len(testdata), len(self.data)) |
| 1338 | self.assertEqual(testdata, self.data) |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 1339 | self.assertEqual(zipfp.read("another.name"), self.data) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1340 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1341 | def test_stored(self): |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1342 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1343 | self.zip_test(f, zipfile.ZIP_STORED) |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1344 | |
Antoine Pitrou | e1436d1 | 2010-08-12 15:25:51 +0000 | [diff] [blame] | 1345 | @skipUnless(zlib, "requires zlib") |
| 1346 | def test_deflated(self): |
| 1347 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 1348 | self.zip_test(f, zipfile.ZIP_DEFLATED) |
| 1349 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1350 | def zip_open_test(self, f, compression): |
| 1351 | self.make_test_archive(f, compression) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1352 | |
| 1353 | # Read the ZIP archive |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 1354 | with zipfile.ZipFile(f, "r", compression) as zipfp: |
| 1355 | zipdata1 = [] |
Brian Curtin | 0d65433 | 2011-04-19 21:15:55 -0500 | [diff] [blame] | 1356 | with zipfp.open(TESTFN) as zipopen1: |
| 1357 | while True: |
| 1358 | read_data = zipopen1.read(256) |
| 1359 | if not read_data: |
| 1360 | break |
| 1361 | zipdata1.append(read_data) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1362 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 1363 | zipdata2 = [] |
Brian Curtin | 0d65433 | 2011-04-19 21:15:55 -0500 | [diff] [blame] | 1364 | with zipfp.open("another.name") as zipopen2: |
| 1365 | while True: |
| 1366 | read_data = zipopen2.read(256) |
| 1367 | if not read_data: |
| 1368 | break |
| 1369 | zipdata2.append(read_data) |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1370 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 1371 | testdata1 = ''.join(zipdata1) |
| 1372 | self.assertEqual(len(testdata1), len(self.data)) |
| 1373 | self.assertEqual(testdata1, self.data) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1374 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 1375 | testdata2 = ''.join(zipdata2) |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 1376 | self.assertEqual(len(testdata2), len(self.data)) |
| 1377 | self.assertEqual(testdata2, self.data) |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1378 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1379 | def test_open_stored(self): |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1380 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1381 | self.zip_open_test(f, zipfile.ZIP_STORED) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1382 | |
Antoine Pitrou | e1436d1 | 2010-08-12 15:25:51 +0000 | [diff] [blame] | 1383 | @skipUnless(zlib, "requires zlib") |
| 1384 | def test_open_deflated(self): |
| 1385 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 1386 | self.zip_open_test(f, zipfile.ZIP_DEFLATED) |
| 1387 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1388 | def zip_random_open_test(self, f, compression): |
| 1389 | self.make_test_archive(f, compression) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1390 | |
| 1391 | # Read the ZIP archive |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 1392 | with zipfile.ZipFile(f, "r", compression) as zipfp: |
| 1393 | zipdata1 = [] |
Brian Curtin | 0d65433 | 2011-04-19 21:15:55 -0500 | [diff] [blame] | 1394 | with zipfp.open(TESTFN) as zipopen1: |
| 1395 | while True: |
| 1396 | read_data = zipopen1.read(randint(1, 1024)) |
| 1397 | if not read_data: |
| 1398 | break |
| 1399 | zipdata1.append(read_data) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1400 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 1401 | testdata = ''.join(zipdata1) |
| 1402 | self.assertEqual(len(testdata), len(self.data)) |
| 1403 | self.assertEqual(testdata, self.data) |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1404 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1405 | def test_random_open_stored(self): |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1406 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1407 | self.zip_random_open_test(f, zipfile.ZIP_STORED) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1408 | |
Antoine Pitrou | e1436d1 | 2010-08-12 15:25:51 +0000 | [diff] [blame] | 1409 | @skipUnless(zlib, "requires zlib") |
| 1410 | def test_random_open_deflated(self): |
| 1411 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 1412 | self.zip_random_open_test(f, zipfile.ZIP_DEFLATED) |
| 1413 | |
Ezio Melotti | 6cbfc12 | 2009-07-10 20:25:56 +0000 | [diff] [blame] | 1414 | |
Ezio Melotti | 1036a7f | 2009-09-12 14:43:43 +0000 | [diff] [blame] | 1415 | @skipUnless(zlib, "requires zlib") |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1416 | class TestsWithMultipleOpens(unittest.TestCase): |
Serhiy Storchaka | 45aa771 | 2014-12-03 09:11:12 +0200 | [diff] [blame] | 1417 | @classmethod |
| 1418 | def setUpClass(cls): |
| 1419 | cls.data1 = b'111' + getrandbytes(10000) |
| 1420 | cls.data2 = b'222' + getrandbytes(10000) |
| 1421 | |
| 1422 | def make_test_archive(self, f): |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1423 | # Create the ZIP archive |
Serhiy Storchaka | 45aa771 | 2014-12-03 09:11:12 +0200 | [diff] [blame] | 1424 | with zipfile.ZipFile(f, "w", zipfile.ZIP_DEFLATED) as zipfp: |
| 1425 | zipfp.writestr('ones', self.data1) |
| 1426 | zipfp.writestr('twos', self.data2) |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1427 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1428 | def test_same_file(self): |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1429 | # Verify that (when the ZipFile is in control of creating file objects) |
| 1430 | # multiple open() calls can be made without interfering with each other. |
Serhiy Storchaka | c328d11 | 2015-01-26 13:45:04 +0200 | [diff] [blame] | 1431 | self.make_test_archive(TESTFN2) |
| 1432 | with zipfile.ZipFile(TESTFN2, mode="r") as zipf: |
| 1433 | with zipf.open('ones') as zopen1, zipf.open('ones') as zopen2: |
Brian Curtin | 0d65433 | 2011-04-19 21:15:55 -0500 | [diff] [blame] | 1434 | data1 = zopen1.read(500) |
| 1435 | data2 = zopen2.read(500) |
Serhiy Storchaka | 45aa771 | 2014-12-03 09:11:12 +0200 | [diff] [blame] | 1436 | data1 += zopen1.read() |
| 1437 | data2 += zopen2.read() |
Serhiy Storchaka | c328d11 | 2015-01-26 13:45:04 +0200 | [diff] [blame] | 1438 | self.assertEqual(data1, data2) |
| 1439 | self.assertEqual(data1, self.data1) |
| 1440 | |
| 1441 | def test_different_file(self): |
| 1442 | # Verify that (when the ZipFile is in control of creating file objects) |
| 1443 | # multiple open() calls can be made without interfering with each other. |
| 1444 | self.make_test_archive(TESTFN2) |
| 1445 | with zipfile.ZipFile(TESTFN2, mode="r") as zipf: |
| 1446 | with zipf.open('ones') as zopen1, zipf.open('twos') as zopen2: |
| 1447 | data1 = zopen1.read(500) |
| 1448 | data2 = zopen2.read(500) |
| 1449 | data1 += zopen1.read() |
| 1450 | data2 += zopen2.read() |
Serhiy Storchaka | 45aa771 | 2014-12-03 09:11:12 +0200 | [diff] [blame] | 1451 | self.assertEqual(data1, self.data1) |
| 1452 | self.assertEqual(data2, self.data2) |
| 1453 | |
Serhiy Storchaka | c328d11 | 2015-01-26 13:45:04 +0200 | [diff] [blame] | 1454 | def test_interleaved(self): |
| 1455 | # Verify that (when the ZipFile is in control of creating file objects) |
| 1456 | # multiple open() calls can be made without interfering with each other. |
| 1457 | self.make_test_archive(TESTFN2) |
| 1458 | with zipfile.ZipFile(TESTFN2, mode="r") as zipf: |
Serhiy Storchaka | 0e1c5f7 | 2016-05-13 21:19:44 +0300 | [diff] [blame^] | 1459 | with zipf.open('ones') as zopen1: |
Serhiy Storchaka | c328d11 | 2015-01-26 13:45:04 +0200 | [diff] [blame] | 1460 | data1 = zopen1.read(500) |
Serhiy Storchaka | 0e1c5f7 | 2016-05-13 21:19:44 +0300 | [diff] [blame^] | 1461 | with zipf.open('twos') as zopen2: |
| 1462 | data2 = zopen2.read(500) |
| 1463 | data1 += zopen1.read() |
| 1464 | data2 += zopen2.read() |
Serhiy Storchaka | c328d11 | 2015-01-26 13:45:04 +0200 | [diff] [blame] | 1465 | self.assertEqual(data1, self.data1) |
| 1466 | self.assertEqual(data2, self.data2) |
| 1467 | |
| 1468 | def test_read_after_close(self): |
| 1469 | self.make_test_archive(TESTFN2) |
| 1470 | zopen1 = zopen2 = None |
| 1471 | try: |
| 1472 | with zipfile.ZipFile(TESTFN2, 'r') as zipf: |
| 1473 | zopen1 = zipf.open('ones') |
| 1474 | zopen2 = zipf.open('twos') |
| 1475 | data1 = zopen1.read(500) |
| 1476 | data2 = zopen2.read(500) |
| 1477 | data1 += zopen1.read() |
| 1478 | data2 += zopen2.read() |
| 1479 | finally: |
| 1480 | if zopen1: |
| 1481 | zopen1.close() |
| 1482 | if zopen2: |
| 1483 | zopen2.close() |
| 1484 | self.assertEqual(data1, self.data1) |
| 1485 | self.assertEqual(data2, self.data2) |
| 1486 | |
Serhiy Storchaka | 45aa771 | 2014-12-03 09:11:12 +0200 | [diff] [blame] | 1487 | def test_read_after_write(self): |
Serhiy Storchaka | c328d11 | 2015-01-26 13:45:04 +0200 | [diff] [blame] | 1488 | with zipfile.ZipFile(TESTFN2, 'w', zipfile.ZIP_DEFLATED) as zipf: |
| 1489 | zipf.writestr('ones', self.data1) |
| 1490 | zipf.writestr('twos', self.data2) |
| 1491 | with zipf.open('ones') as zopen1: |
| 1492 | data1 = zopen1.read(500) |
| 1493 | self.assertEqual(data1, self.data1[:500]) |
| 1494 | with zipfile.ZipFile(TESTFN2, 'r') as zipf: |
| 1495 | data1 = zipf.read('ones') |
| 1496 | data2 = zipf.read('twos') |
| 1497 | self.assertEqual(data1, self.data1) |
| 1498 | self.assertEqual(data2, self.data2) |
Serhiy Storchaka | 45aa771 | 2014-12-03 09:11:12 +0200 | [diff] [blame] | 1499 | |
| 1500 | def test_write_after_read(self): |
Serhiy Storchaka | c328d11 | 2015-01-26 13:45:04 +0200 | [diff] [blame] | 1501 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED) as zipf: |
| 1502 | zipf.writestr('ones', self.data1) |
| 1503 | with zipf.open('ones') as zopen1: |
| 1504 | zopen1.read(500) |
| 1505 | zipf.writestr('twos', self.data2) |
| 1506 | with zipfile.ZipFile(TESTFN2, 'r') as zipf: |
| 1507 | data1 = zipf.read('ones') |
| 1508 | data2 = zipf.read('twos') |
| 1509 | self.assertEqual(data1, self.data1) |
| 1510 | self.assertEqual(data2, self.data2) |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1511 | |
Benjamin Peterson | 352eb4f | 2014-04-03 10:31:25 -0400 | [diff] [blame] | 1512 | def test_many_opens(self): |
| 1513 | # Verify that read() and open() promptly close the file descriptor, |
| 1514 | # and don't rely on the garbage collector to free resources. |
Serhiy Storchaka | 45aa771 | 2014-12-03 09:11:12 +0200 | [diff] [blame] | 1515 | self.make_test_archive(TESTFN2) |
Benjamin Peterson | 352eb4f | 2014-04-03 10:31:25 -0400 | [diff] [blame] | 1516 | with zipfile.ZipFile(TESTFN2, mode="r") as zipf: |
| 1517 | for x in range(100): |
| 1518 | zipf.read('ones') |
| 1519 | with zipf.open('ones') as zopen1: |
| 1520 | pass |
| 1521 | with open(os.devnull) as f: |
| 1522 | self.assertLess(f.fileno(), 100) |
| 1523 | |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1524 | def tearDown(self): |
Ezio Melotti | 6cbfc12 | 2009-07-10 20:25:56 +0000 | [diff] [blame] | 1525 | unlink(TESTFN2) |
| 1526 | |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1527 | |
Martin v. Löwis | 0dfcfc8 | 2009-01-24 14:00:33 +0000 | [diff] [blame] | 1528 | class TestWithDirectory(unittest.TestCase): |
| 1529 | def setUp(self): |
| 1530 | os.mkdir(TESTFN2) |
| 1531 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1532 | def test_extract_dir(self): |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 1533 | with zipfile.ZipFile(findfile("zipdir.zip")) as zipf: |
| 1534 | zipf.extractall(TESTFN2) |
Martin v. Löwis | 0dfcfc8 | 2009-01-24 14:00:33 +0000 | [diff] [blame] | 1535 | self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a"))) |
| 1536 | self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a", "b"))) |
| 1537 | self.assertTrue(os.path.exists(os.path.join(TESTFN2, "a", "b", "c"))) |
| 1538 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1539 | def test_bug_6050(self): |
Martin v. Löwis | 0b09c42 | 2009-05-24 19:30:52 +0000 | [diff] [blame] | 1540 | # Extraction should succeed if directories already exist |
| 1541 | os.mkdir(os.path.join(TESTFN2, "a")) |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1542 | self.test_extract_dir() |
Martin v. Löwis | 0b09c42 | 2009-05-24 19:30:52 +0000 | [diff] [blame] | 1543 | |
Serhiy Storchaka | 6d343e7 | 2014-09-23 22:39:59 +0300 | [diff] [blame] | 1544 | def test_write_dir(self): |
| 1545 | dirpath = os.path.join(TESTFN2, "x") |
| 1546 | os.mkdir(dirpath) |
| 1547 | mode = os.stat(dirpath).st_mode & 0xFFFF |
| 1548 | with zipfile.ZipFile(TESTFN, "w") as zipf: |
| 1549 | zipf.write(dirpath) |
| 1550 | zinfo = zipf.filelist[0] |
| 1551 | self.assertTrue(zinfo.filename.endswith("/x/")) |
| 1552 | self.assertEqual(zinfo.external_attr, (mode << 16) | 0x10) |
| 1553 | zipf.write(dirpath, "y") |
| 1554 | zinfo = zipf.filelist[1] |
| 1555 | self.assertTrue(zinfo.filename, "y/") |
| 1556 | self.assertEqual(zinfo.external_attr, (mode << 16) | 0x10) |
| 1557 | with zipfile.ZipFile(TESTFN, "r") as zipf: |
| 1558 | zinfo = zipf.filelist[0] |
| 1559 | self.assertTrue(zinfo.filename.endswith("/x/")) |
| 1560 | self.assertEqual(zinfo.external_attr, (mode << 16) | 0x10) |
| 1561 | zinfo = zipf.filelist[1] |
| 1562 | self.assertTrue(zinfo.filename, "y/") |
| 1563 | self.assertEqual(zinfo.external_attr, (mode << 16) | 0x10) |
| 1564 | target = os.path.join(TESTFN2, "target") |
| 1565 | os.mkdir(target) |
| 1566 | zipf.extractall(target) |
| 1567 | self.assertTrue(os.path.isdir(os.path.join(target, "y"))) |
| 1568 | self.assertEqual(len(os.listdir(target)), 2) |
| 1569 | |
| 1570 | def test_writestr_dir(self): |
Martin v. Löwis | 0dfcfc8 | 2009-01-24 14:00:33 +0000 | [diff] [blame] | 1571 | os.mkdir(os.path.join(TESTFN2, "x")) |
Benjamin Peterson | 352eb4f | 2014-04-03 10:31:25 -0400 | [diff] [blame] | 1572 | with zipfile.ZipFile(TESTFN, "w") as zipf: |
Serhiy Storchaka | 6d343e7 | 2014-09-23 22:39:59 +0300 | [diff] [blame] | 1573 | zipf.writestr("x/", b'') |
| 1574 | zinfo = zipf.filelist[0] |
| 1575 | self.assertEqual(zinfo.filename, "x/") |
| 1576 | self.assertEqual(zinfo.external_attr, (0o40775 << 16) | 0x10) |
| 1577 | with zipfile.ZipFile(TESTFN, "r") as zipf: |
| 1578 | zinfo = zipf.filelist[0] |
| 1579 | self.assertTrue(zinfo.filename.endswith("x/")) |
| 1580 | self.assertEqual(zinfo.external_attr, (0o40775 << 16) | 0x10) |
| 1581 | target = os.path.join(TESTFN2, "target") |
| 1582 | os.mkdir(target) |
| 1583 | zipf.extractall(target) |
| 1584 | self.assertTrue(os.path.isdir(os.path.join(target, "x"))) |
| 1585 | self.assertEqual(os.listdir(target), ["x"]) |
Martin v. Löwis | 0dfcfc8 | 2009-01-24 14:00:33 +0000 | [diff] [blame] | 1586 | |
| 1587 | def tearDown(self): |
Benjamin Peterson | 352eb4f | 2014-04-03 10:31:25 -0400 | [diff] [blame] | 1588 | rmtree(TESTFN2) |
Martin v. Löwis | 0dfcfc8 | 2009-01-24 14:00:33 +0000 | [diff] [blame] | 1589 | if os.path.exists(TESTFN): |
Ezio Melotti | 6cbfc12 | 2009-07-10 20:25:56 +0000 | [diff] [blame] | 1590 | unlink(TESTFN) |
Martin v. Löwis | 0dfcfc8 | 2009-01-24 14:00:33 +0000 | [diff] [blame] | 1591 | |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1592 | |
| 1593 | class UniversalNewlineTests(unittest.TestCase): |
| 1594 | def setUp(self): |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 1595 | self.line_gen = ["Test of zipfile line %d." % i |
| 1596 | for i in xrange(FIXEDTEST_SIZE)] |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1597 | self.seps = ('\r', '\r\n', '\n') |
| 1598 | self.arcdata, self.arcfiles = {}, {} |
| 1599 | for n, s in enumerate(self.seps): |
| 1600 | self.arcdata[s] = s.join(self.line_gen) + s |
| 1601 | self.arcfiles[s] = '%s-%d' % (TESTFN, n) |
Benjamin Peterson | 352eb4f | 2014-04-03 10:31:25 -0400 | [diff] [blame] | 1602 | with open(self.arcfiles[s], "wb") as fid: |
| 1603 | fid.write(self.arcdata[s]) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1604 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1605 | def make_test_archive(self, f, compression): |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1606 | # Create the ZIP archive |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 1607 | with zipfile.ZipFile(f, "w", compression) as zipfp: |
| 1608 | for fn in self.arcfiles.values(): |
| 1609 | zipfp.write(fn, fn) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1610 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1611 | def read_test(self, f, compression): |
| 1612 | self.make_test_archive(f, compression) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1613 | |
| 1614 | # Read the ZIP archive |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 1615 | with zipfile.ZipFile(f, "r") as zipfp: |
| 1616 | for sep, fn in self.arcfiles.items(): |
Brian Curtin | 0d65433 | 2011-04-19 21:15:55 -0500 | [diff] [blame] | 1617 | with zipfp.open(fn, "rU") as fp: |
| 1618 | zipdata = fp.read() |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 1619 | self.assertEqual(self.arcdata[sep], zipdata) |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1620 | |
Antoine Pitrou | 94c33eb | 2010-01-27 20:59:50 +0000 | [diff] [blame] | 1621 | def readline_read_test(self, f, compression): |
| 1622 | self.make_test_archive(f, compression) |
| 1623 | |
| 1624 | # Read the ZIP archive |
| 1625 | zipfp = zipfile.ZipFile(f, "r") |
| 1626 | for sep, fn in self.arcfiles.items(): |
Brian Curtin | 0d65433 | 2011-04-19 21:15:55 -0500 | [diff] [blame] | 1627 | with zipfp.open(fn, "rU") as zipopen: |
| 1628 | data = '' |
| 1629 | while True: |
| 1630 | read = zipopen.readline() |
| 1631 | if not read: |
| 1632 | break |
| 1633 | data += read |
Antoine Pitrou | 94c33eb | 2010-01-27 20:59:50 +0000 | [diff] [blame] | 1634 | |
Brian Curtin | 0d65433 | 2011-04-19 21:15:55 -0500 | [diff] [blame] | 1635 | read = zipopen.read(5) |
| 1636 | if not read: |
| 1637 | break |
| 1638 | data += read |
Antoine Pitrou | 94c33eb | 2010-01-27 20:59:50 +0000 | [diff] [blame] | 1639 | |
| 1640 | self.assertEqual(data, self.arcdata['\n']) |
| 1641 | |
| 1642 | zipfp.close() |
| 1643 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1644 | def readline_test(self, f, compression): |
| 1645 | self.make_test_archive(f, compression) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1646 | |
| 1647 | # Read the ZIP archive |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 1648 | with zipfile.ZipFile(f, "r") as zipfp: |
| 1649 | for sep, fn in self.arcfiles.items(): |
Brian Curtin | 0d65433 | 2011-04-19 21:15:55 -0500 | [diff] [blame] | 1650 | with zipfp.open(fn, "rU") as zipopen: |
| 1651 | for line in self.line_gen: |
| 1652 | linedata = zipopen.readline() |
| 1653 | self.assertEqual(linedata, line + '\n') |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1654 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1655 | def readlines_test(self, f, compression): |
| 1656 | self.make_test_archive(f, compression) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1657 | |
| 1658 | # Read the ZIP archive |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 1659 | with zipfile.ZipFile(f, "r") as zipfp: |
| 1660 | for sep, fn in self.arcfiles.items(): |
Brian Curtin | 0d65433 | 2011-04-19 21:15:55 -0500 | [diff] [blame] | 1661 | with zipfp.open(fn, "rU") as fp: |
| 1662 | ziplines = fp.readlines() |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 1663 | for line, zipline in zip(self.line_gen, ziplines): |
| 1664 | self.assertEqual(zipline, line + '\n') |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1665 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1666 | def iterlines_test(self, f, compression): |
| 1667 | self.make_test_archive(f, compression) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1668 | |
| 1669 | # Read the ZIP archive |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 1670 | with zipfile.ZipFile(f, "r") as zipfp: |
| 1671 | for sep, fn in self.arcfiles.items(): |
Benjamin Peterson | 352eb4f | 2014-04-03 10:31:25 -0400 | [diff] [blame] | 1672 | with zipfp.open(fn, "rU") as fid: |
| 1673 | for line, zipline in zip(self.line_gen, fid): |
| 1674 | self.assertEqual(zipline, line + '\n') |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1675 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1676 | def test_read_stored(self): |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1677 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1678 | self.read_test(f, zipfile.ZIP_STORED) |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1679 | |
Antoine Pitrou | 94c33eb | 2010-01-27 20:59:50 +0000 | [diff] [blame] | 1680 | def test_readline_read_stored(self): |
| 1681 | # Issue #7610: calls to readline() interleaved with calls to read(). |
| 1682 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
| 1683 | self.readline_read_test(f, zipfile.ZIP_STORED) |
| 1684 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1685 | def test_readline_stored(self): |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1686 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1687 | self.readline_test(f, zipfile.ZIP_STORED) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1688 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1689 | def test_readlines_stored(self): |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1690 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1691 | self.readlines_test(f, zipfile.ZIP_STORED) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1692 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1693 | def test_iterlines_stored(self): |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1694 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1695 | self.iterlines_test(f, zipfile.ZIP_STORED) |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1696 | |
Ezio Melotti | 6cbfc12 | 2009-07-10 20:25:56 +0000 | [diff] [blame] | 1697 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1698 | def test_read_deflated(self): |
Ezio Melotti | 6cbfc12 | 2009-07-10 20:25:56 +0000 | [diff] [blame] | 1699 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1700 | self.read_test(f, zipfile.ZIP_DEFLATED) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1701 | |
Ezio Melotti | 6cbfc12 | 2009-07-10 20:25:56 +0000 | [diff] [blame] | 1702 | @skipUnless(zlib, "requires zlib") |
Antoine Pitrou | 94c33eb | 2010-01-27 20:59:50 +0000 | [diff] [blame] | 1703 | def test_readline_read_deflated(self): |
| 1704 | # Issue #7610: calls to readline() interleaved with calls to read(). |
| 1705 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
| 1706 | self.readline_read_test(f, zipfile.ZIP_DEFLATED) |
| 1707 | |
| 1708 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1709 | def test_readline_deflated(self): |
Ezio Melotti | 6cbfc12 | 2009-07-10 20:25:56 +0000 | [diff] [blame] | 1710 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1711 | self.readline_test(f, zipfile.ZIP_DEFLATED) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1712 | |
Ezio Melotti | 6cbfc12 | 2009-07-10 20:25:56 +0000 | [diff] [blame] | 1713 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1714 | def test_readlines_deflated(self): |
Ezio Melotti | 6cbfc12 | 2009-07-10 20:25:56 +0000 | [diff] [blame] | 1715 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1716 | self.readlines_test(f, zipfile.ZIP_DEFLATED) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1717 | |
Ezio Melotti | 6cbfc12 | 2009-07-10 20:25:56 +0000 | [diff] [blame] | 1718 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1719 | def test_iterlines_deflated(self): |
Ezio Melotti | 6cbfc12 | 2009-07-10 20:25:56 +0000 | [diff] [blame] | 1720 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1721 | self.iterlines_test(f, zipfile.ZIP_DEFLATED) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1722 | |
| 1723 | def tearDown(self): |
| 1724 | for sep, fn in self.arcfiles.items(): |
| 1725 | os.remove(fn) |
Ezio Melotti | 6cbfc12 | 2009-07-10 20:25:56 +0000 | [diff] [blame] | 1726 | unlink(TESTFN) |
| 1727 | unlink(TESTFN2) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1728 | |
| 1729 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 1730 | def test_main(): |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1731 | run_unittest(TestsWithSourceFile, TestZip64InSmallFiles, OtherTests, |
| 1732 | PyZipFileTests, DecryptionTests, TestsWithMultipleOpens, |
Ezio Melotti | 6cbfc12 | 2009-07-10 20:25:56 +0000 | [diff] [blame] | 1733 | TestWithDirectory, UniversalNewlineTests, |
| 1734 | TestsWithRandomBinaryFiles) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1735 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 1736 | if __name__ == "__main__": |
| 1737 | test_main() |