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 shutil |
| 12 | import struct |
| 13 | import zipfile |
| 14 | import unittest |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 15 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 16 | from StringIO import StringIO |
| 17 | from tempfile import TemporaryFile |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 18 | from random import randint, random |
Ezio Melotti | e7a0cc2 | 2009-07-04 14:58:27 +0000 | [diff] [blame] | 19 | from unittest import skipUnless |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 20 | |
Ezio Melotti | 6cbfc12 | 2009-07-10 20:25:56 +0000 | [diff] [blame] | 21 | from test.test_support import TESTFN, run_unittest, findfile, unlink |
Guido van Rossum | 368f04a | 2000-04-10 13:23:04 +0000 | [diff] [blame] | 22 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 23 | TESTFN2 = TESTFN + "2" |
Martin v. Löwis | 0dfcfc8 | 2009-01-24 14:00:33 +0000 | [diff] [blame] | 24 | TESTFNDIR = TESTFN + "d" |
Georg Brandl | 4b3ab6f | 2007-07-12 09:59:22 +0000 | [diff] [blame] | 25 | FIXEDTEST_SIZE = 1000 |
Guido van Rossum | 368f04a | 2000-04-10 13:23:04 +0000 | [diff] [blame] | 26 | |
Georg Brandl | 62416bc | 2008-01-07 18:47:44 +0000 | [diff] [blame] | 27 | SMALL_TEST_DATA = [('_ziptest1', '1q2w3e4r5t'), |
| 28 | ('ziptest2dir/_ziptest2', 'qawsedrftg'), |
| 29 | ('/ziptest2dir/ziptest3dir/_ziptest3', 'azsxdcfvgb'), |
| 30 | ('ziptest2dir/ziptest3dir/ziptest4dir/_ziptest3', '6y7u8i9o0p')] |
| 31 | |
Ezio Melotti | 6cbfc12 | 2009-07-10 20:25:56 +0000 | [diff] [blame] | 32 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 33 | class TestsWithSourceFile(unittest.TestCase): |
| 34 | def setUp(self): |
Georg Brandl | 4b3ab6f | 2007-07-12 09:59:22 +0000 | [diff] [blame] | 35 | self.line_gen = ["Zipfile test line %d. random float: %f" % (i, random()) |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 36 | for i in xrange(FIXEDTEST_SIZE)] |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 37 | self.data = '\n'.join(self.line_gen) + '\n' |
Fred Drake | 6e7e485 | 2001-02-28 05:34:16 +0000 | [diff] [blame] | 38 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 39 | # Make a source file with some lines |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 40 | with open(TESTFN, "wb") as fp: |
| 41 | fp.write(self.data) |
Tim Peters | 7d3bad6 | 2001-04-04 18:56:49 +0000 | [diff] [blame] | 42 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 43 | def make_test_archive(self, f, compression): |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 44 | # Create the ZIP archive |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 45 | with zipfile.ZipFile(f, "w", compression) as zipfp: |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 46 | zipfp.write(TESTFN, "another.name") |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 47 | zipfp.write(TESTFN, TESTFN) |
| 48 | zipfp.writestr("strfile", self.data) |
Tim Peters | 7d3bad6 | 2001-04-04 18:56:49 +0000 | [diff] [blame] | 49 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 50 | def zip_test(self, f, compression): |
| 51 | self.make_test_archive(f, compression) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 52 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 53 | # Read the ZIP archive |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 54 | with zipfile.ZipFile(f, "r", compression) as zipfp: |
| 55 | self.assertEqual(zipfp.read(TESTFN), self.data) |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 56 | self.assertEqual(zipfp.read("another.name"), self.data) |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 57 | self.assertEqual(zipfp.read("strfile"), self.data) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 58 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 59 | # Print the ZIP directory |
| 60 | fp = StringIO() |
| 61 | stdout = sys.stdout |
| 62 | try: |
| 63 | sys.stdout = fp |
| 64 | zipfp.printdir() |
| 65 | finally: |
| 66 | sys.stdout = stdout |
Tim Peters | a608bb2 | 2006-06-15 18:06:29 +0000 | [diff] [blame] | 67 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 68 | directory = fp.getvalue() |
| 69 | lines = directory.splitlines() |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 70 | self.assertEqual(len(lines), 4) # Number of files + header |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 71 | |
Ezio Melotti | aa98058 | 2010-01-23 23:04:36 +0000 | [diff] [blame] | 72 | self.assertIn('File Name', lines[0]) |
| 73 | self.assertIn('Modified', lines[0]) |
| 74 | self.assertIn('Size', lines[0]) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 75 | |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 76 | fn, date, time_, size = lines[1].split() |
| 77 | self.assertEqual(fn, 'another.name') |
| 78 | self.assertTrue(time.strptime(date, '%Y-%m-%d')) |
| 79 | self.assertTrue(time.strptime(time_, '%H:%M:%S')) |
| 80 | self.assertEqual(size, str(len(self.data))) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 81 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 82 | # Check the namelist |
| 83 | names = zipfp.namelist() |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 84 | self.assertEqual(len(names), 3) |
Ezio Melotti | aa98058 | 2010-01-23 23:04:36 +0000 | [diff] [blame] | 85 | self.assertIn(TESTFN, names) |
| 86 | self.assertIn("another.name", names) |
| 87 | self.assertIn("strfile", names) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 88 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 89 | # Check infolist |
| 90 | infos = zipfp.infolist() |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 91 | names = [i.filename for i in infos] |
| 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) |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 96 | for i in infos: |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 97 | self.assertEqual(i.file_size, len(self.data)) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 98 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 99 | # check getinfo |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 100 | for nm in (TESTFN, "another.name", "strfile"): |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 101 | info = zipfp.getinfo(nm) |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 102 | self.assertEqual(info.filename, nm) |
| 103 | self.assertEqual(info.file_size, len(self.data)) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 104 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 105 | # Check that testzip doesn't raise an exception |
| 106 | zipfp.testzip() |
Tim Peters | 7d3bad6 | 2001-04-04 18:56:49 +0000 | [diff] [blame] | 107 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 108 | def test_stored(self): |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 109 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 110 | self.zip_test(f, zipfile.ZIP_STORED) |
Raymond Hettinger | c0fac96 | 2003-06-27 22:25:03 +0000 | [diff] [blame] | 111 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 112 | def zip_open_test(self, f, compression): |
| 113 | self.make_test_archive(f, compression) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 114 | |
| 115 | # Read the ZIP archive |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 116 | with zipfile.ZipFile(f, "r", compression) as zipfp: |
| 117 | zipdata1 = [] |
| 118 | zipopen1 = zipfp.open(TESTFN) |
| 119 | while True: |
| 120 | read_data = zipopen1.read(256) |
| 121 | if not read_data: |
| 122 | break |
| 123 | zipdata1.append(read_data) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 124 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 125 | zipdata2 = [] |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 126 | zipopen2 = zipfp.open("another.name") |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 127 | while True: |
| 128 | read_data = zipopen2.read(256) |
| 129 | if not read_data: |
| 130 | break |
| 131 | zipdata2.append(read_data) |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 132 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 133 | self.assertEqual(''.join(zipdata1), self.data) |
| 134 | self.assertEqual(''.join(zipdata2), self.data) |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 135 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 136 | def test_open_stored(self): |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 137 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 138 | self.zip_open_test(f, zipfile.ZIP_STORED) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 139 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 140 | def test_open_via_zip_info(self): |
Georg Brandl | 112aa50 | 2008-05-20 08:25:48 +0000 | [diff] [blame] | 141 | # Create the ZIP archive |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 142 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: |
| 143 | zipfp.writestr("name", "foo") |
| 144 | zipfp.writestr("name", "bar") |
Georg Brandl | 112aa50 | 2008-05-20 08:25:48 +0000 | [diff] [blame] | 145 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 146 | with zipfile.ZipFile(TESTFN2, "r") as zipfp: |
| 147 | infos = zipfp.infolist() |
| 148 | data = "" |
| 149 | for info in infos: |
| 150 | data += zipfp.open(info).read() |
| 151 | self.assertTrue(data == "foobar" or data == "barfoo") |
| 152 | data = "" |
| 153 | for info in infos: |
| 154 | data += zipfp.read(info) |
| 155 | self.assertTrue(data == "foobar" or data == "barfoo") |
Georg Brandl | 112aa50 | 2008-05-20 08:25:48 +0000 | [diff] [blame] | 156 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 157 | def zip_random_open_test(self, f, compression): |
| 158 | self.make_test_archive(f, compression) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 159 | |
| 160 | # Read the ZIP archive |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 161 | with zipfile.ZipFile(f, "r", compression) as zipfp: |
| 162 | zipdata1 = [] |
| 163 | zipopen1 = zipfp.open(TESTFN) |
| 164 | while True: |
| 165 | read_data = zipopen1.read(randint(1, 1024)) |
| 166 | if not read_data: |
| 167 | break |
| 168 | zipdata1.append(read_data) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 169 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 170 | self.assertEqual(''.join(zipdata1), self.data) |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 171 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 172 | def test_random_open_stored(self): |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 173 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 174 | self.zip_random_open_test(f, zipfile.ZIP_STORED) |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 175 | |
Antoine Pitrou | 94c33eb | 2010-01-27 20:59:50 +0000 | [diff] [blame] | 176 | def test_univeral_readaheads(self): |
| 177 | f = StringIO() |
| 178 | |
| 179 | data = 'a\r\n' * 16 * 1024 |
| 180 | zipfp = zipfile.ZipFile(f, 'w', zipfile.ZIP_STORED) |
| 181 | zipfp.writestr(TESTFN, data) |
| 182 | zipfp.close() |
| 183 | |
| 184 | data2 = '' |
| 185 | zipfp = zipfile.ZipFile(f, 'r') |
| 186 | zipopen = zipfp.open(TESTFN, 'rU') |
| 187 | for line in zipopen: |
| 188 | data2 += line |
| 189 | zipfp.close() |
| 190 | |
| 191 | self.assertEqual(data, data2.replace('\n', '\r\n')) |
| 192 | |
| 193 | def zip_readline_read_test(self, f, compression): |
| 194 | self.make_test_archive(f, compression) |
| 195 | |
| 196 | # Read the ZIP archive |
| 197 | zipfp = zipfile.ZipFile(f, "r") |
| 198 | zipopen = zipfp.open(TESTFN) |
| 199 | |
| 200 | data = '' |
| 201 | while True: |
| 202 | read = zipopen.readline() |
| 203 | if not read: |
| 204 | break |
| 205 | data += read |
| 206 | |
| 207 | read = zipopen.read(100) |
| 208 | if not read: |
| 209 | break |
| 210 | data += read |
| 211 | |
| 212 | self.assertEqual(data, self.data) |
| 213 | zipfp.close() |
| 214 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 215 | def zip_readline_test(self, f, compression): |
| 216 | self.make_test_archive(f, compression) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 217 | |
| 218 | # Read the ZIP archive |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 219 | with zipfile.ZipFile(f, "r") as zipfp: |
| 220 | zipopen = zipfp.open(TESTFN) |
| 221 | for line in self.line_gen: |
| 222 | linedata = zipopen.readline() |
| 223 | self.assertEqual(linedata, line + '\n') |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 224 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 225 | def zip_readlines_test(self, f, compression): |
| 226 | self.make_test_archive(f, compression) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 227 | |
| 228 | # Read the ZIP archive |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 229 | with zipfile.ZipFile(f, "r") as zipfp: |
| 230 | ziplines = zipfp.open(TESTFN).readlines() |
| 231 | for line, zipline in zip(self.line_gen, ziplines): |
| 232 | self.assertEqual(zipline, line + '\n') |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 233 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 234 | def zip_iterlines_test(self, f, compression): |
| 235 | self.make_test_archive(f, compression) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 236 | |
| 237 | # Read the ZIP archive |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 238 | with zipfile.ZipFile(f, "r") as zipfp: |
| 239 | for line, zipline in zip(self.line_gen, zipfp.open(TESTFN)): |
| 240 | self.assertEqual(zipline, line + '\n') |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 241 | |
Antoine Pitrou | 94c33eb | 2010-01-27 20:59:50 +0000 | [diff] [blame] | 242 | def test_readline_read_stored(self): |
| 243 | # Issue #7610: calls to readline() interleaved with calls to read(). |
| 244 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
| 245 | self.zip_readline_read_test(f, zipfile.ZIP_STORED) |
| 246 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 247 | def test_readline_stored(self): |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 248 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 249 | self.zip_readline_test(f, zipfile.ZIP_STORED) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 250 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 251 | def test_readlines_stored(self): |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 252 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 253 | self.zip_readlines_test(f, zipfile.ZIP_STORED) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 254 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 255 | def test_iterlines_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_iterlines_test(f, zipfile.ZIP_STORED) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 258 | |
Ezio Melotti | e7a0cc2 | 2009-07-04 14:58:27 +0000 | [diff] [blame] | 259 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 260 | def test_deflated(self): |
Ezio Melotti | e7a0cc2 | 2009-07-04 14:58:27 +0000 | [diff] [blame] | 261 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 262 | self.zip_test(f, zipfile.ZIP_DEFLATED) |
Raymond Hettinger | c0fac96 | 2003-06-27 22:25:03 +0000 | [diff] [blame] | 263 | |
Ezio Melotti | e7a0cc2 | 2009-07-04 14:58:27 +0000 | [diff] [blame] | 264 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 265 | def test_open_deflated(self): |
Ezio Melotti | e7a0cc2 | 2009-07-04 14:58:27 +0000 | [diff] [blame] | 266 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 267 | self.zip_open_test(f, zipfile.ZIP_DEFLATED) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 268 | |
Ezio Melotti | e7a0cc2 | 2009-07-04 14:58:27 +0000 | [diff] [blame] | 269 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 270 | def test_random_open_deflated(self): |
Ezio Melotti | e7a0cc2 | 2009-07-04 14:58:27 +0000 | [diff] [blame] | 271 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 272 | self.zip_random_open_test(f, zipfile.ZIP_DEFLATED) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 273 | |
Ezio Melotti | e7a0cc2 | 2009-07-04 14:58:27 +0000 | [diff] [blame] | 274 | @skipUnless(zlib, "requires zlib") |
Antoine Pitrou | 94c33eb | 2010-01-27 20:59:50 +0000 | [diff] [blame] | 275 | def test_readline_read_deflated(self): |
| 276 | # Issue #7610: calls to readline() interleaved with calls to read(). |
| 277 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
| 278 | self.zip_readline_read_test(f, zipfile.ZIP_DEFLATED) |
| 279 | |
| 280 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 281 | def test_readline_deflated(self): |
Ezio Melotti | e7a0cc2 | 2009-07-04 14:58:27 +0000 | [diff] [blame] | 282 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 283 | self.zip_readline_test(f, zipfile.ZIP_DEFLATED) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 284 | |
Ezio Melotti | e7a0cc2 | 2009-07-04 14:58:27 +0000 | [diff] [blame] | 285 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 286 | def test_readlines_deflated(self): |
Ezio Melotti | e7a0cc2 | 2009-07-04 14:58:27 +0000 | [diff] [blame] | 287 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 288 | self.zip_readlines_test(f, zipfile.ZIP_DEFLATED) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 289 | |
Ezio Melotti | e7a0cc2 | 2009-07-04 14:58:27 +0000 | [diff] [blame] | 290 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 291 | def test_iterlines_deflated(self): |
Ezio Melotti | e7a0cc2 | 2009-07-04 14:58:27 +0000 | [diff] [blame] | 292 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 293 | self.zip_iterlines_test(f, zipfile.ZIP_DEFLATED) |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 294 | |
Ezio Melotti | e7a0cc2 | 2009-07-04 14:58:27 +0000 | [diff] [blame] | 295 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 296 | def test_low_compression(self): |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 297 | """Check for cases where compressed data is larger than original.""" |
Ezio Melotti | e7a0cc2 | 2009-07-04 14:58:27 +0000 | [diff] [blame] | 298 | # Create the ZIP archive |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 299 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED) as zipfp: |
| 300 | zipfp.writestr("strfile", '12') |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 301 | |
Ezio Melotti | e7a0cc2 | 2009-07-04 14:58:27 +0000 | [diff] [blame] | 302 | # Get an open object for strfile |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 303 | with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_DEFLATED) as zipfp: |
| 304 | openobj = zipfp.open("strfile") |
| 305 | self.assertEqual(openobj.read(1), '1') |
| 306 | self.assertEqual(openobj.read(1), '2') |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 307 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 308 | def test_absolute_arcnames(self): |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 309 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: |
| 310 | zipfp.write(TESTFN, "/absolute") |
Georg Brandl | 8f7c54e | 2006-02-20 08:40:38 +0000 | [diff] [blame] | 311 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 312 | with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) as zipfp: |
| 313 | self.assertEqual(zipfp.namelist(), ["absolute"]) |
Tim Peters | 32cbc96 | 2006-02-20 21:42:18 +0000 | [diff] [blame] | 314 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 315 | def test_append_to_zip_file(self): |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 316 | """Test appending to an existing zipfile.""" |
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, TESTFN) |
| 319 | |
| 320 | with zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) as zipfp: |
| 321 | zipfp.writestr("strfile", self.data) |
| 322 | self.assertEqual(zipfp.namelist(), [TESTFN, "strfile"]) |
Georg Brandl | 4b3ab6f | 2007-07-12 09:59:22 +0000 | [diff] [blame] | 323 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 324 | def test_append_to_non_zip_file(self): |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 325 | """Test appending to an existing file that is not a zipfile.""" |
Georg Brandl | 4b3ab6f | 2007-07-12 09:59:22 +0000 | [diff] [blame] | 326 | # NOTE: this test fails if len(d) < 22 because of the first |
| 327 | # line "fpin.seek(-22, 2)" in _EndRecData |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 328 | data = 'I am not a ZipFile!'*10 |
| 329 | with open(TESTFN2, 'wb') as f: |
| 330 | f.write(data) |
| 331 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 332 | with zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) as zipfp: |
| 333 | zipfp.write(TESTFN, TESTFN) |
Georg Brandl | 4b3ab6f | 2007-07-12 09:59:22 +0000 | [diff] [blame] | 334 | |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 335 | with open(TESTFN2, 'rb') as f: |
| 336 | f.seek(len(data)) |
| 337 | with zipfile.ZipFile(f, "r") as zipfp: |
| 338 | self.assertEqual(zipfp.namelist(), [TESTFN]) |
Georg Brandl | 4b3ab6f | 2007-07-12 09:59:22 +0000 | [diff] [blame] | 339 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 340 | def test_write_default_name(self): |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 341 | """Check that calling ZipFile.write without arcname specified |
| 342 | produces the expected result.""" |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 343 | with zipfile.ZipFile(TESTFN2, "w") as zipfp: |
| 344 | zipfp.write(TESTFN) |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 345 | self.assertEqual(zipfp.read(TESTFN), open(TESTFN).read()) |
Georg Brandl | 4b3ab6f | 2007-07-12 09:59:22 +0000 | [diff] [blame] | 346 | |
Ezio Melotti | 1036a7f | 2009-09-12 14:43:43 +0000 | [diff] [blame] | 347 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 348 | def test_per_file_compression(self): |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 349 | """Check that files within a Zip archive can have different |
| 350 | compression options.""" |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 351 | with zipfile.ZipFile(TESTFN2, "w") as zipfp: |
| 352 | zipfp.write(TESTFN, 'storeme', zipfile.ZIP_STORED) |
| 353 | zipfp.write(TESTFN, 'deflateme', zipfile.ZIP_DEFLATED) |
| 354 | sinfo = zipfp.getinfo('storeme') |
| 355 | dinfo = zipfp.getinfo('deflateme') |
| 356 | self.assertEqual(sinfo.compress_type, zipfile.ZIP_STORED) |
| 357 | self.assertEqual(dinfo.compress_type, zipfile.ZIP_DEFLATED) |
Georg Brandl | 4b3ab6f | 2007-07-12 09:59:22 +0000 | [diff] [blame] | 358 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 359 | def test_write_to_readonly(self): |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 360 | """Check that trying to call write() on a readonly ZipFile object |
| 361 | raises a RuntimeError.""" |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 362 | with zipfile.ZipFile(TESTFN2, mode="w") as zipfp: |
| 363 | zipfp.writestr("somefile.txt", "bogus") |
| 364 | |
| 365 | with zipfile.ZipFile(TESTFN2, mode="r") as zipfp: |
| 366 | self.assertRaises(RuntimeError, zipfp.write, TESTFN) |
Georg Brandl | 4b3ab6f | 2007-07-12 09:59:22 +0000 | [diff] [blame] | 367 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 368 | def test_extract(self): |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 369 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: |
| 370 | for fpath, fdata in SMALL_TEST_DATA: |
| 371 | zipfp.writestr(fpath, fdata) |
Georg Brandl | 62416bc | 2008-01-07 18:47:44 +0000 | [diff] [blame] | 372 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 373 | with zipfile.ZipFile(TESTFN2, "r") as zipfp: |
| 374 | for fpath, fdata in SMALL_TEST_DATA: |
| 375 | writtenfile = zipfp.extract(fpath) |
Georg Brandl | 62416bc | 2008-01-07 18:47:44 +0000 | [diff] [blame] | 376 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 377 | # make sure it was written to the right place |
| 378 | if os.path.isabs(fpath): |
| 379 | correctfile = os.path.join(os.getcwd(), fpath[1:]) |
| 380 | else: |
| 381 | correctfile = os.path.join(os.getcwd(), fpath) |
| 382 | correctfile = os.path.normpath(correctfile) |
Georg Brandl | 62416bc | 2008-01-07 18:47:44 +0000 | [diff] [blame] | 383 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 384 | self.assertEqual(writtenfile, correctfile) |
Georg Brandl | 62416bc | 2008-01-07 18:47:44 +0000 | [diff] [blame] | 385 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 386 | # make sure correct data is in correct file |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 387 | self.assertEqual(fdata, open(writtenfile, "rb").read()) |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 388 | os.remove(writtenfile) |
Georg Brandl | 62416bc | 2008-01-07 18:47:44 +0000 | [diff] [blame] | 389 | |
| 390 | # remove the test file subdirectories |
| 391 | shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir')) |
| 392 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 393 | def test_extract_all(self): |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 394 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: |
| 395 | for fpath, fdata in SMALL_TEST_DATA: |
| 396 | zipfp.writestr(fpath, fdata) |
Georg Brandl | 62416bc | 2008-01-07 18:47:44 +0000 | [diff] [blame] | 397 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 398 | with zipfile.ZipFile(TESTFN2, "r") as zipfp: |
| 399 | zipfp.extractall() |
| 400 | for fpath, fdata in SMALL_TEST_DATA: |
| 401 | if os.path.isabs(fpath): |
| 402 | outfile = os.path.join(os.getcwd(), fpath[1:]) |
| 403 | else: |
| 404 | outfile = os.path.join(os.getcwd(), fpath) |
Georg Brandl | 62416bc | 2008-01-07 18:47:44 +0000 | [diff] [blame] | 405 | |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 406 | self.assertEqual(fdata, open(outfile, "rb").read()) |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 407 | os.remove(outfile) |
Georg Brandl | 62416bc | 2008-01-07 18:47:44 +0000 | [diff] [blame] | 408 | |
| 409 | # remove the test file subdirectories |
| 410 | shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir')) |
| 411 | |
Ronald Oussoren | dd25e86 | 2010-02-07 20:18:02 +0000 | [diff] [blame] | 412 | def test_writestr_compression(self): |
| 413 | zipfp = zipfile.ZipFile(TESTFN2, "w") |
| 414 | zipfp.writestr("a.txt", "hello world", compress_type=zipfile.ZIP_STORED) |
| 415 | if zlib: |
| 416 | zipfp.writestr("b.txt", "hello world", compress_type=zipfile.ZIP_DEFLATED) |
| 417 | |
| 418 | info = zipfp.getinfo('a.txt') |
| 419 | self.assertEqual(info.compress_type, zipfile.ZIP_STORED) |
| 420 | |
| 421 | if zlib: |
| 422 | info = zipfp.getinfo('b.txt') |
| 423 | self.assertEqual(info.compress_type, zipfile.ZIP_DEFLATED) |
| 424 | |
| 425 | |
Antoine Pitrou | 5fdfa3e | 2008-07-25 19:42:26 +0000 | [diff] [blame] | 426 | def zip_test_writestr_permissions(self, f, compression): |
| 427 | # Make sure that writestr creates files with mode 0600, |
| 428 | # when it is passed a name rather than a ZipInfo instance. |
| 429 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 430 | self.make_test_archive(f, compression) |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 431 | with zipfile.ZipFile(f, "r") as zipfp: |
| 432 | zinfo = zipfp.getinfo('strfile') |
| 433 | self.assertEqual(zinfo.external_attr, 0600 << 16) |
Antoine Pitrou | 5fdfa3e | 2008-07-25 19:42:26 +0000 | [diff] [blame] | 434 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 435 | def test_writestr_permissions(self): |
Antoine Pitrou | 5fdfa3e | 2008-07-25 19:42:26 +0000 | [diff] [blame] | 436 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
| 437 | self.zip_test_writestr_permissions(f, zipfile.ZIP_STORED) |
| 438 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 439 | def test_close(self): |
| 440 | """Check that the zipfile is closed after the 'with' block.""" |
| 441 | with zipfile.ZipFile(TESTFN2, "w") as zipfp: |
| 442 | for fpath, fdata in SMALL_TEST_DATA: |
| 443 | zipfp.writestr(fpath, fdata) |
| 444 | self.assertTrue(zipfp.fp is not None, 'zipfp is not open') |
| 445 | self.assertTrue(zipfp.fp is None, 'zipfp is not closed') |
| 446 | |
| 447 | with zipfile.ZipFile(TESTFN2, "r") as zipfp: |
| 448 | self.assertTrue(zipfp.fp is not None, 'zipfp is not open') |
| 449 | self.assertTrue(zipfp.fp is None, 'zipfp is not closed') |
| 450 | |
| 451 | def test_close_on_exception(self): |
| 452 | """Check that the zipfile is closed if an exception is raised in the |
| 453 | 'with' block.""" |
| 454 | with zipfile.ZipFile(TESTFN2, "w") as zipfp: |
| 455 | for fpath, fdata in SMALL_TEST_DATA: |
| 456 | zipfp.writestr(fpath, fdata) |
| 457 | |
| 458 | try: |
| 459 | with zipfile.ZipFile(TESTFN2, "r") as zipfp2: |
| 460 | raise zipfile.BadZipfile() |
| 461 | except zipfile.BadZipfile: |
| 462 | self.assertTrue(zipfp2.fp is None, 'zipfp is not closed') |
| 463 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 464 | def tearDown(self): |
Ezio Melotti | 6cbfc12 | 2009-07-10 20:25:56 +0000 | [diff] [blame] | 465 | unlink(TESTFN) |
| 466 | unlink(TESTFN2) |
| 467 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 468 | |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 469 | class TestZip64InSmallFiles(unittest.TestCase): |
| 470 | # These tests test the ZIP64 functionality without using large files, |
| 471 | # see test_zipfile64 for proper tests. |
| 472 | |
| 473 | def setUp(self): |
| 474 | self._limit = zipfile.ZIP64_LIMIT |
| 475 | zipfile.ZIP64_LIMIT = 5 |
| 476 | |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 477 | line_gen = ("Test of zipfile line %d." % i |
| 478 | for i in range(0, FIXEDTEST_SIZE)) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 479 | self.data = '\n'.join(line_gen) |
| 480 | |
| 481 | # Make a source file with some lines |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 482 | with open(TESTFN, "wb") as fp: |
| 483 | fp.write(self.data) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 484 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 485 | def large_file_exception_test(self, f, compression): |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 486 | with zipfile.ZipFile(f, "w", compression) as zipfp: |
| 487 | self.assertRaises(zipfile.LargeZipFile, |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 488 | zipfp.write, TESTFN, "another.name") |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 489 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 490 | def large_file_exception_test2(self, f, compression): |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 491 | with zipfile.ZipFile(f, "w", compression) as zipfp: |
| 492 | self.assertRaises(zipfile.LargeZipFile, |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 493 | zipfp.writestr, "another.name", self.data) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 494 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 495 | def test_large_file_exception(self): |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 496 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 497 | self.large_file_exception_test(f, zipfile.ZIP_STORED) |
| 498 | self.large_file_exception_test2(f, zipfile.ZIP_STORED) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 499 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 500 | def zip_test(self, f, compression): |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 501 | # Create the ZIP archive |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 502 | with zipfile.ZipFile(f, "w", compression, allowZip64=True) as zipfp: |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 503 | zipfp.write(TESTFN, "another.name") |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 504 | zipfp.write(TESTFN, TESTFN) |
| 505 | zipfp.writestr("strfile", self.data) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 506 | |
| 507 | # Read the ZIP archive |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 508 | with zipfile.ZipFile(f, "r", compression) as zipfp: |
| 509 | self.assertEqual(zipfp.read(TESTFN), self.data) |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 510 | self.assertEqual(zipfp.read("another.name"), self.data) |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 511 | self.assertEqual(zipfp.read("strfile"), self.data) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 512 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 513 | # Print the ZIP directory |
| 514 | fp = StringIO() |
| 515 | stdout = sys.stdout |
| 516 | try: |
| 517 | sys.stdout = fp |
| 518 | zipfp.printdir() |
| 519 | finally: |
| 520 | sys.stdout = stdout |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 521 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 522 | directory = fp.getvalue() |
| 523 | lines = directory.splitlines() |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 524 | self.assertEqual(len(lines), 4) # Number of files + header |
Tim Peters | a608bb2 | 2006-06-15 18:06:29 +0000 | [diff] [blame] | 525 | |
Ezio Melotti | aa98058 | 2010-01-23 23:04:36 +0000 | [diff] [blame] | 526 | self.assertIn('File Name', lines[0]) |
| 527 | self.assertIn('Modified', lines[0]) |
| 528 | self.assertIn('Size', lines[0]) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 529 | |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 530 | fn, date, time_, size = lines[1].split() |
| 531 | self.assertEqual(fn, 'another.name') |
| 532 | self.assertTrue(time.strptime(date, '%Y-%m-%d')) |
| 533 | self.assertTrue(time.strptime(time_, '%H:%M:%S')) |
| 534 | self.assertEqual(size, str(len(self.data))) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 535 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 536 | # Check the namelist |
| 537 | names = zipfp.namelist() |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 538 | self.assertEqual(len(names), 3) |
Ezio Melotti | aa98058 | 2010-01-23 23:04:36 +0000 | [diff] [blame] | 539 | self.assertIn(TESTFN, names) |
| 540 | self.assertIn("another.name", names) |
| 541 | self.assertIn("strfile", names) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 542 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 543 | # Check infolist |
| 544 | infos = zipfp.infolist() |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 545 | names = [i.filename for i in infos] |
| 546 | self.assertEqual(len(names), 3) |
Ezio Melotti | aa98058 | 2010-01-23 23:04:36 +0000 | [diff] [blame] | 547 | self.assertIn(TESTFN, names) |
| 548 | self.assertIn("another.name", names) |
| 549 | self.assertIn("strfile", names) |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 550 | for i in infos: |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 551 | self.assertEqual(i.file_size, len(self.data)) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 552 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 553 | # check getinfo |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 554 | for nm in (TESTFN, "another.name", "strfile"): |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 555 | info = zipfp.getinfo(nm) |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 556 | self.assertEqual(info.filename, nm) |
| 557 | self.assertEqual(info.file_size, len(self.data)) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 558 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 559 | # Check that testzip doesn't raise an exception |
| 560 | zipfp.testzip() |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 561 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 562 | def test_stored(self): |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 563 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 564 | self.zip_test(f, zipfile.ZIP_STORED) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 565 | |
Ezio Melotti | 6cbfc12 | 2009-07-10 20:25:56 +0000 | [diff] [blame] | 566 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 567 | def test_deflated(self): |
Ezio Melotti | 6cbfc12 | 2009-07-10 20:25:56 +0000 | [diff] [blame] | 568 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 569 | self.zip_test(f, zipfile.ZIP_DEFLATED) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 570 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 571 | def test_absolute_arcnames(self): |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 572 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED, |
| 573 | allowZip64=True) as zipfp: |
| 574 | zipfp.write(TESTFN, "/absolute") |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 575 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 576 | with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) as zipfp: |
| 577 | self.assertEqual(zipfp.namelist(), ["absolute"]) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 578 | |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 579 | def tearDown(self): |
| 580 | zipfile.ZIP64_LIMIT = self._limit |
Ezio Melotti | 6cbfc12 | 2009-07-10 20:25:56 +0000 | [diff] [blame] | 581 | unlink(TESTFN) |
| 582 | unlink(TESTFN2) |
| 583 | |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 584 | |
| 585 | class PyZipFileTests(unittest.TestCase): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 586 | def test_write_pyfile(self): |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 587 | with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp: |
| 588 | fn = __file__ |
| 589 | if fn.endswith('.pyc') or fn.endswith('.pyo'): |
| 590 | fn = fn[:-1] |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 591 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 592 | zipfp.writepy(fn) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 593 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 594 | bn = os.path.basename(fn) |
Ezio Melotti | aa98058 | 2010-01-23 23:04:36 +0000 | [diff] [blame] | 595 | self.assertNotIn(bn, zipfp.namelist()) |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 596 | self.assertTrue(bn + 'o' in zipfp.namelist() or |
| 597 | bn + 'c' in zipfp.namelist()) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 598 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 599 | with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp: |
| 600 | fn = __file__ |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 601 | if fn.endswith(('.pyc', '.pyo')): |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 602 | fn = fn[:-1] |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 603 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 604 | zipfp.writepy(fn, "testpackage") |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 605 | |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 606 | bn = "%s/%s" % ("testpackage", os.path.basename(fn)) |
Ezio Melotti | aa98058 | 2010-01-23 23:04:36 +0000 | [diff] [blame] | 607 | self.assertNotIn(bn, zipfp.namelist()) |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 608 | self.assertTrue(bn + 'o' in zipfp.namelist() or |
| 609 | bn + 'c' in zipfp.namelist()) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 610 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 611 | def test_write_python_package(self): |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 612 | import email |
| 613 | packagedir = os.path.dirname(email.__file__) |
| 614 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 615 | with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp: |
| 616 | zipfp.writepy(packagedir) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 617 | |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 618 | # Check for a couple of modules at different levels of the |
| 619 | # hierarchy |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 620 | names = zipfp.namelist() |
| 621 | self.assertTrue('email/__init__.pyo' in names or |
| 622 | 'email/__init__.pyc' in names) |
| 623 | self.assertTrue('email/mime/text.pyo' in names or |
| 624 | 'email/mime/text.pyc' in names) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 625 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 626 | def test_write_python_directory(self): |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 627 | os.mkdir(TESTFN2) |
| 628 | try: |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 629 | with open(os.path.join(TESTFN2, "mod1.py"), "w") as fp: |
Ezio Melotti | 763f1e8 | 2009-12-31 13:27:41 +0000 | [diff] [blame] | 630 | fp.write("print(42)\n") |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 631 | |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 632 | with open(os.path.join(TESTFN2, "mod2.py"), "w") as fp: |
Ezio Melotti | 763f1e8 | 2009-12-31 13:27:41 +0000 | [diff] [blame] | 633 | fp.write("print(42 * 42)\n") |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 634 | |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 635 | with open(os.path.join(TESTFN2, "mod2.txt"), "w") as fp: |
| 636 | fp.write("bla bla bla\n") |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 637 | |
| 638 | zipfp = zipfile.PyZipFile(TemporaryFile(), "w") |
| 639 | zipfp.writepy(TESTFN2) |
| 640 | |
| 641 | names = zipfp.namelist() |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 642 | self.assertTrue('mod1.pyc' in names or 'mod1.pyo' in names) |
| 643 | self.assertTrue('mod2.pyc' in names or 'mod2.pyo' in names) |
Ezio Melotti | aa98058 | 2010-01-23 23:04:36 +0000 | [diff] [blame] | 644 | self.assertNotIn('mod2.txt', names) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 645 | |
| 646 | finally: |
| 647 | shutil.rmtree(TESTFN2) |
| 648 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 649 | def test_write_non_pyfile(self): |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 650 | with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp: |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 651 | open(TESTFN, 'w').write('most definitely not a python file') |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 652 | self.assertRaises(RuntimeError, zipfp.writepy, TESTFN) |
| 653 | os.remove(TESTFN) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 654 | |
| 655 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 656 | class OtherTests(unittest.TestCase): |
Antoine Pitrou | e1436d1 | 2010-08-12 15:25:51 +0000 | [diff] [blame] | 657 | zips_with_bad_crc = { |
| 658 | zipfile.ZIP_STORED: ( |
| 659 | b'PK\003\004\024\0\0\0\0\0 \213\212;:r' |
| 660 | b'\253\377\f\0\0\0\f\0\0\0\005\0\0\000af' |
| 661 | b'ilehello,AworldP' |
| 662 | b'K\001\002\024\003\024\0\0\0\0\0 \213\212;:' |
| 663 | b'r\253\377\f\0\0\0\f\0\0\0\005\0\0\0\0' |
| 664 | b'\0\0\0\0\0\0\0\200\001\0\0\0\000afi' |
| 665 | b'lePK\005\006\0\0\0\0\001\0\001\0003\000' |
| 666 | b'\0\0/\0\0\0\0\0'), |
| 667 | zipfile.ZIP_DEFLATED: ( |
| 668 | b'PK\x03\x04\x14\x00\x00\x00\x08\x00n}\x0c=FA' |
| 669 | b'KE\x10\x00\x00\x00n\x00\x00\x00\x05\x00\x00\x00af' |
| 670 | b'ile\xcbH\xcd\xc9\xc9W(\xcf/\xcaI\xc9\xa0' |
| 671 | b'=\x13\x00PK\x01\x02\x14\x03\x14\x00\x00\x00\x08\x00n' |
| 672 | b'}\x0c=FAKE\x10\x00\x00\x00n\x00\x00\x00\x05' |
| 673 | b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x01\x00\x00\x00' |
| 674 | b'\x00afilePK\x05\x06\x00\x00\x00\x00\x01\x00' |
| 675 | b'\x01\x003\x00\x00\x003\x00\x00\x00\x00\x00'), |
| 676 | } |
| 677 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 678 | def test_unicode_filenames(self): |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 679 | with zipfile.ZipFile(TESTFN, "w") as zf: |
| 680 | zf.writestr(u"foo.txt", "Test for unicode filename") |
| 681 | zf.writestr(u"\xf6.txt", "Test for unicode filename") |
Ezio Melotti | b0f5adc | 2010-01-24 16:58:36 +0000 | [diff] [blame] | 682 | self.assertIsInstance(zf.infolist()[0].filename, unicode) |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 683 | |
| 684 | with zipfile.ZipFile(TESTFN, "r") as zf: |
| 685 | self.assertEqual(zf.filelist[0].filename, "foo.txt") |
| 686 | self.assertEqual(zf.filelist[1].filename, u"\xf6.txt") |
Martin v. Löwis | 471617d | 2008-05-05 17:16:58 +0000 | [diff] [blame] | 687 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 688 | def test_create_non_existent_file_for_append(self): |
Martin v. Löwis | 84f6de9 | 2007-02-13 10:10:39 +0000 | [diff] [blame] | 689 | if os.path.exists(TESTFN): |
| 690 | os.unlink(TESTFN) |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 691 | |
Martin v. Löwis | 84f6de9 | 2007-02-13 10:10:39 +0000 | [diff] [blame] | 692 | filename = 'testfile.txt' |
| 693 | content = 'hello, world. this is some content.' |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 694 | |
Martin v. Löwis | 84f6de9 | 2007-02-13 10:10:39 +0000 | [diff] [blame] | 695 | try: |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 696 | with zipfile.ZipFile(TESTFN, 'a') as zf: |
| 697 | zf.writestr(filename, content) |
Ezio Melotti | 6cbfc12 | 2009-07-10 20:25:56 +0000 | [diff] [blame] | 698 | except IOError: |
Martin v. Löwis | 84f6de9 | 2007-02-13 10:10:39 +0000 | [diff] [blame] | 699 | self.fail('Could not append data to a non-existent zip file.') |
| 700 | |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 701 | self.assertTrue(os.path.exists(TESTFN)) |
Martin v. Löwis | 84f6de9 | 2007-02-13 10:10:39 +0000 | [diff] [blame] | 702 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 703 | with zipfile.ZipFile(TESTFN, 'r') as zf: |
| 704 | self.assertEqual(zf.read(filename), content) |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 705 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 706 | def test_close_erroneous_file(self): |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 707 | # This test checks that the ZipFile constructor closes the file object |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 708 | # it opens if there's an error in the file. If it doesn't, the |
| 709 | # traceback holds a reference to the ZipFile object and, indirectly, |
| 710 | # the file object. |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 711 | # On Windows, this causes the os.unlink() call to fail because the |
| 712 | # underlying file is still open. This is SF bug #412214. |
| 713 | # |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 714 | with open(TESTFN, "w") as fp: |
| 715 | fp.write("this is not a legal zip file\n") |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 716 | try: |
| 717 | zf = zipfile.ZipFile(TESTFN) |
| 718 | except zipfile.BadZipfile: |
Collin Winter | 04a51ec | 2007-03-29 02:28:16 +0000 | [diff] [blame] | 719 | pass |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 720 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 721 | def test_is_zip_erroneous_file(self): |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 722 | """Check that is_zipfile() correctly identifies non-zip files.""" |
Antoine Pitrou | 6f193e0 | 2008-12-27 15:43:12 +0000 | [diff] [blame] | 723 | # - passing a filename |
| 724 | with open(TESTFN, "w") as fp: |
| 725 | fp.write("this is not a legal zip file\n") |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 726 | chk = zipfile.is_zipfile(TESTFN) |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 727 | self.assertFalse(chk) |
Antoine Pitrou | 6f193e0 | 2008-12-27 15:43:12 +0000 | [diff] [blame] | 728 | # - passing a file object |
| 729 | with open(TESTFN, "rb") as fp: |
| 730 | chk = zipfile.is_zipfile(fp) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 731 | self.assertTrue(not chk) |
Antoine Pitrou | 6f193e0 | 2008-12-27 15:43:12 +0000 | [diff] [blame] | 732 | # - passing a file-like object |
| 733 | fp = StringIO() |
| 734 | fp.write("this is not a legal zip file\n") |
| 735 | chk = zipfile.is_zipfile(fp) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 736 | self.assertTrue(not chk) |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 737 | fp.seek(0, 0) |
Antoine Pitrou | 6f193e0 | 2008-12-27 15:43:12 +0000 | [diff] [blame] | 738 | chk = zipfile.is_zipfile(fp) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 739 | self.assertTrue(not chk) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 740 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 741 | def test_is_zip_valid_file(self): |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 742 | """Check that is_zipfile() correctly identifies zip files.""" |
Antoine Pitrou | 6f193e0 | 2008-12-27 15:43:12 +0000 | [diff] [blame] | 743 | # - passing a filename |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 744 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 745 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 746 | chk = zipfile.is_zipfile(TESTFN) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 747 | self.assertTrue(chk) |
Antoine Pitrou | 6f193e0 | 2008-12-27 15:43:12 +0000 | [diff] [blame] | 748 | # - passing a file object |
| 749 | with open(TESTFN, "rb") as fp: |
| 750 | chk = zipfile.is_zipfile(fp) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 751 | self.assertTrue(chk) |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 752 | fp.seek(0, 0) |
Antoine Pitrou | 6f193e0 | 2008-12-27 15:43:12 +0000 | [diff] [blame] | 753 | zip_contents = fp.read() |
| 754 | # - passing a file-like object |
| 755 | fp = StringIO() |
| 756 | fp.write(zip_contents) |
| 757 | chk = zipfile.is_zipfile(fp) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 758 | self.assertTrue(chk) |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 759 | fp.seek(0, 0) |
Antoine Pitrou | 6f193e0 | 2008-12-27 15:43:12 +0000 | [diff] [blame] | 760 | chk = zipfile.is_zipfile(fp) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 761 | self.assertTrue(chk) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 762 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 763 | def test_non_existent_file_raises_IOError(self): |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 764 | # make sure we don't raise an AttributeError when a partially-constructed |
| 765 | # ZipFile instance is finalized; this tests for regression on SF tracker |
| 766 | # bug #403871. |
| 767 | |
| 768 | # The bug we're testing for caused an AttributeError to be raised |
| 769 | # when a ZipFile instance was created for a file that did not |
| 770 | # exist; the .fp member was not initialized but was needed by the |
| 771 | # __del__() method. Since the AttributeError is in the __del__(), |
| 772 | # it is ignored, but the user should be sufficiently annoyed by |
| 773 | # the message on the output that regression will be noticed |
| 774 | # quickly. |
| 775 | self.assertRaises(IOError, zipfile.ZipFile, TESTFN) |
| 776 | |
Amaury Forgeot d'Arc | 3e5b027 | 2009-07-28 22:15:30 +0000 | [diff] [blame] | 777 | def test_empty_file_raises_BadZipFile(self): |
| 778 | f = open(TESTFN, 'w') |
| 779 | f.close() |
| 780 | self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN) |
| 781 | |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 782 | with open(TESTFN, 'w') as fp: |
| 783 | fp.write("short file") |
Amaury Forgeot d'Arc | 3e5b027 | 2009-07-28 22:15:30 +0000 | [diff] [blame] | 784 | self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN) |
| 785 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 786 | def test_closed_zip_raises_RuntimeError(self): |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 787 | """Verify that testzip() doesn't swallow inappropriate exceptions.""" |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 788 | data = StringIO() |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 789 | with zipfile.ZipFile(data, mode="w") as zipf: |
| 790 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 791 | |
| 792 | # This is correct; calling .read on a closed ZipFile should throw |
| 793 | # a RuntimeError, and so should calling .testzip. An earlier |
| 794 | # version of .testzip would swallow this exception (and any other) |
| 795 | # and report that the first file in the archive was corrupt. |
Georg Brandl | 4b3ab6f | 2007-07-12 09:59:22 +0000 | [diff] [blame] | 796 | self.assertRaises(RuntimeError, zipf.read, "foo.txt") |
| 797 | self.assertRaises(RuntimeError, zipf.open, "foo.txt") |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 798 | self.assertRaises(RuntimeError, zipf.testzip) |
Georg Brandl | 4b3ab6f | 2007-07-12 09:59:22 +0000 | [diff] [blame] | 799 | self.assertRaises(RuntimeError, zipf.writestr, "bogus.txt", "bogus") |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 800 | open(TESTFN, 'w').write('zipfile test data') |
Georg Brandl | 4b3ab6f | 2007-07-12 09:59:22 +0000 | [diff] [blame] | 801 | self.assertRaises(RuntimeError, zipf.write, TESTFN) |
| 802 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 803 | def test_bad_constructor_mode(self): |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 804 | """Check that bad modes passed to ZipFile constructor are caught.""" |
Georg Brandl | 4b3ab6f | 2007-07-12 09:59:22 +0000 | [diff] [blame] | 805 | self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "q") |
| 806 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 807 | def test_bad_open_mode(self): |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 808 | """Check that bad modes passed to ZipFile.open are caught.""" |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 809 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 810 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 811 | |
| 812 | with zipfile.ZipFile(TESTFN, mode="r") as zipf: |
Georg Brandl | 4b3ab6f | 2007-07-12 09:59:22 +0000 | [diff] [blame] | 813 | # read the data to make sure the file is there |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 814 | zipf.read("foo.txt") |
| 815 | self.assertRaises(RuntimeError, zipf.open, "foo.txt", "q") |
Georg Brandl | 4b3ab6f | 2007-07-12 09:59:22 +0000 | [diff] [blame] | 816 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 817 | def test_read0(self): |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 818 | """Check that calling read(0) on a ZipExtFile object returns an empty |
| 819 | string and doesn't advance file pointer.""" |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 820 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 821 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 822 | # read the data to make sure the file is there |
| 823 | f = zipf.open("foo.txt") |
| 824 | for i in xrange(FIXEDTEST_SIZE): |
| 825 | self.assertEqual(f.read(0), '') |
Georg Brandl | 4b3ab6f | 2007-07-12 09:59:22 +0000 | [diff] [blame] | 826 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 827 | self.assertEqual(f.read(), "O, for a Muse of Fire!") |
Georg Brandl | 4b3ab6f | 2007-07-12 09:59:22 +0000 | [diff] [blame] | 828 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 829 | def test_open_non_existent_item(self): |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 830 | """Check that attempting to call open() for an item that doesn't |
| 831 | exist in the archive raises a RuntimeError.""" |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 832 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 833 | self.assertRaises(KeyError, zipf.open, "foo.txt", "r") |
Georg Brandl | 4b3ab6f | 2007-07-12 09:59:22 +0000 | [diff] [blame] | 834 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 835 | def test_bad_compression_mode(self): |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 836 | """Check that bad compression methods passed to ZipFile.open are |
| 837 | caught.""" |
Georg Brandl | 4b3ab6f | 2007-07-12 09:59:22 +0000 | [diff] [blame] | 838 | self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "w", -1) |
| 839 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 840 | def test_null_byte_in_filename(self): |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 841 | """Check that a filename containing a null byte is properly |
| 842 | terminated.""" |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 843 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 844 | zipf.writestr("foo.txt\x00qqq", "O, for a Muse of Fire!") |
| 845 | self.assertEqual(zipf.namelist(), ['foo.txt']) |
Neal Norwitz | 0d4c06e | 2007-04-25 06:30:05 +0000 | [diff] [blame] | 846 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 847 | def test_struct_sizes(self): |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 848 | """Check that ZIP internal structure sizes are calculated correctly.""" |
Martin v. Löwis | 8c43641 | 2008-07-03 12:51:14 +0000 | [diff] [blame] | 849 | self.assertEqual(zipfile.sizeEndCentDir, 22) |
| 850 | self.assertEqual(zipfile.sizeCentralDir, 46) |
| 851 | self.assertEqual(zipfile.sizeEndCentDir64, 56) |
| 852 | self.assertEqual(zipfile.sizeEndCentDir64Locator, 20) |
| 853 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 854 | def test_comments(self): |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 855 | """Check that comments on the archive are handled properly.""" |
Martin v. Löwis | 8c43641 | 2008-07-03 12:51:14 +0000 | [diff] [blame] | 856 | |
| 857 | # check default comment is empty |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 858 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 859 | self.assertEqual(zipf.comment, '') |
| 860 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 861 | |
| 862 | with zipfile.ZipFile(TESTFN, mode="r") as zipf: |
| 863 | self.assertEqual(zipf.comment, '') |
Martin v. Löwis | 8c43641 | 2008-07-03 12:51:14 +0000 | [diff] [blame] | 864 | |
| 865 | # check a simple short comment |
| 866 | comment = 'Bravely taking to his feet, he beat a very brave retreat.' |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 867 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 868 | zipf.comment = comment |
| 869 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 870 | with zipfile.ZipFile(TESTFN, mode="r") as zipf: |
| 871 | self.assertEqual(zipf.comment, comment) |
Martin v. Löwis | 8c43641 | 2008-07-03 12:51:14 +0000 | [diff] [blame] | 872 | |
| 873 | # check a comment of max length |
| 874 | 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] | 875 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 876 | zipf.comment = comment2 |
| 877 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 878 | |
| 879 | with zipfile.ZipFile(TESTFN, mode="r") as zipf: |
| 880 | self.assertEqual(zipf.comment, comment2) |
Martin v. Löwis | 8c43641 | 2008-07-03 12:51:14 +0000 | [diff] [blame] | 881 | |
| 882 | # check a comment that is too long is truncated |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 883 | with zipfile.ZipFile(TESTFN, mode="w") as zipf: |
| 884 | zipf.comment = comment2 + 'oops' |
| 885 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 886 | with zipfile.ZipFile(TESTFN, mode="r") as zipf: |
| 887 | self.assertEqual(zipf.comment, comment2) |
Martin v. Löwis | 8c43641 | 2008-07-03 12:51:14 +0000 | [diff] [blame] | 888 | |
Antoine Pitrou | e1436d1 | 2010-08-12 15:25:51 +0000 | [diff] [blame] | 889 | def check_testzip_with_bad_crc(self, compression): |
| 890 | """Tests that files with bad CRCs return their name from testzip.""" |
| 891 | zipdata = self.zips_with_bad_crc[compression] |
| 892 | |
| 893 | with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf: |
| 894 | # testzip returns the name of the first corrupt file, or None |
| 895 | self.assertEqual('afile', zipf.testzip()) |
| 896 | |
| 897 | def test_testzip_with_bad_crc_stored(self): |
| 898 | self.check_testzip_with_bad_crc(zipfile.ZIP_STORED) |
| 899 | |
| 900 | @skipUnless(zlib, "requires zlib") |
| 901 | def test_testzip_with_bad_crc_deflated(self): |
| 902 | self.check_testzip_with_bad_crc(zipfile.ZIP_DEFLATED) |
| 903 | |
| 904 | def check_read_with_bad_crc(self, compression): |
| 905 | """Tests that files with bad CRCs raise a BadZipfile exception when read.""" |
| 906 | zipdata = self.zips_with_bad_crc[compression] |
| 907 | |
| 908 | # Using ZipFile.read() |
| 909 | with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf: |
| 910 | self.assertRaises(zipfile.BadZipfile, zipf.read, 'afile') |
| 911 | |
| 912 | # Using ZipExtFile.read() |
| 913 | with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf: |
| 914 | with zipf.open('afile', 'r') as corrupt_file: |
| 915 | self.assertRaises(zipfile.BadZipfile, corrupt_file.read) |
| 916 | |
| 917 | # Same with small reads (in order to exercise the buffering logic) |
| 918 | with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf: |
| 919 | with zipf.open('afile', 'r') as corrupt_file: |
| 920 | corrupt_file.MIN_READ_SIZE = 2 |
| 921 | with self.assertRaises(zipfile.BadZipfile): |
| 922 | while corrupt_file.read(2): |
| 923 | pass |
| 924 | |
| 925 | def test_read_with_bad_crc_stored(self): |
| 926 | self.check_read_with_bad_crc(zipfile.ZIP_STORED) |
| 927 | |
| 928 | @skipUnless(zlib, "requires zlib") |
| 929 | def test_read_with_bad_crc_deflated(self): |
| 930 | self.check_read_with_bad_crc(zipfile.ZIP_DEFLATED) |
| 931 | |
Antoine Pitrou | e4195e8 | 2010-09-12 14:56:27 +0000 | [diff] [blame] | 932 | def check_read_return_size(self, compression): |
| 933 | # Issue #9837: ZipExtFile.read() shouldn't return more bytes |
| 934 | # than requested. |
| 935 | for test_size in (1, 4095, 4096, 4097, 16384): |
| 936 | file_size = test_size + 1 |
| 937 | junk = b''.join(struct.pack('B', randint(0, 255)) |
| 938 | for x in range(file_size)) |
| 939 | with zipfile.ZipFile(io.BytesIO(), "w", compression) as zipf: |
| 940 | zipf.writestr('foo', junk) |
| 941 | with zipf.open('foo', 'r') as fp: |
| 942 | buf = fp.read(test_size) |
| 943 | self.assertEqual(len(buf), test_size) |
| 944 | |
| 945 | def test_read_return_size_stored(self): |
| 946 | self.check_read_return_size(zipfile.ZIP_STORED) |
| 947 | |
| 948 | @skipUnless(zlib, "requires zlib") |
| 949 | def test_read_return_size_deflated(self): |
| 950 | self.check_read_return_size(zipfile.ZIP_DEFLATED) |
| 951 | |
Collin Winter | 04a51ec | 2007-03-29 02:28:16 +0000 | [diff] [blame] | 952 | def tearDown(self): |
Ezio Melotti | 6cbfc12 | 2009-07-10 20:25:56 +0000 | [diff] [blame] | 953 | unlink(TESTFN) |
| 954 | unlink(TESTFN2) |
| 955 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 956 | |
Martin v. Löwis | c6d626e | 2007-02-13 09:49:38 +0000 | [diff] [blame] | 957 | class DecryptionTests(unittest.TestCase): |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 958 | """Check that ZIP decryption works. Since the library does not |
| 959 | support encryption at the moment, we use a pre-generated encrypted |
| 960 | ZIP file.""" |
Martin v. Löwis | c6d626e | 2007-02-13 09:49:38 +0000 | [diff] [blame] | 961 | |
| 962 | data = ( |
| 963 | 'PK\x03\x04\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00\x1a\x00' |
| 964 | '\x00\x00\x08\x00\x00\x00test.txt\xfa\x10\xa0gly|\xfa-\xc5\xc0=\xf9y' |
| 965 | '\x18\xe0\xa8r\xb3Z}Lg\xbc\xae\xf9|\x9b\x19\xe4\x8b\xba\xbb)\x8c\xb0\xdbl' |
| 966 | 'PK\x01\x02\x14\x00\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00' |
| 967 | '\x1a\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x01\x00 \x00\xb6\x81' |
| 968 | '\x00\x00\x00\x00test.txtPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x006\x00' |
| 969 | '\x00\x00L\x00\x00\x00\x00\x00' ) |
Gregory P. Smith | 0c63fc2 | 2008-01-20 01:21:03 +0000 | [diff] [blame] | 970 | data2 = ( |
| 971 | 'PK\x03\x04\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02' |
| 972 | '\x00\x00\x04\x00\x15\x00zeroUT\t\x00\x03\xd6\x8b\x92G\xda\x8b\x92GUx\x04' |
| 973 | '\x00\xe8\x03\xe8\x03\xc7<M\xb5a\xceX\xa3Y&\x8b{oE\xd7\x9d\x8c\x98\x02\xc0' |
| 974 | 'PK\x07\x08xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00PK\x01\x02\x17\x03' |
| 975 | '\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00' |
| 976 | '\x04\x00\r\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa4\x81\x00\x00\x00\x00ze' |
| 977 | 'roUT\x05\x00\x03\xd6\x8b\x92GUx\x00\x00PK\x05\x06\x00\x00\x00\x00\x01' |
| 978 | '\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] | 979 | |
| 980 | plain = 'zipfile.py encryption test' |
Gregory P. Smith | 0c63fc2 | 2008-01-20 01:21:03 +0000 | [diff] [blame] | 981 | plain2 = '\x00'*512 |
Martin v. Löwis | c6d626e | 2007-02-13 09:49:38 +0000 | [diff] [blame] | 982 | |
| 983 | def setUp(self): |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 984 | with open(TESTFN, "wb") as fp: |
| 985 | fp.write(self.data) |
Martin v. Löwis | c6d626e | 2007-02-13 09:49:38 +0000 | [diff] [blame] | 986 | self.zip = zipfile.ZipFile(TESTFN, "r") |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 987 | with open(TESTFN2, "wb") as fp: |
| 988 | fp.write(self.data2) |
Gregory P. Smith | 0c63fc2 | 2008-01-20 01:21:03 +0000 | [diff] [blame] | 989 | self.zip2 = zipfile.ZipFile(TESTFN2, "r") |
Martin v. Löwis | c6d626e | 2007-02-13 09:49:38 +0000 | [diff] [blame] | 990 | |
| 991 | def tearDown(self): |
| 992 | self.zip.close() |
| 993 | os.unlink(TESTFN) |
Gregory P. Smith | 0c63fc2 | 2008-01-20 01:21:03 +0000 | [diff] [blame] | 994 | self.zip2.close() |
| 995 | os.unlink(TESTFN2) |
Martin v. Löwis | c6d626e | 2007-02-13 09:49:38 +0000 | [diff] [blame] | 996 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 997 | def test_no_password(self): |
Martin v. Löwis | c6d626e | 2007-02-13 09:49:38 +0000 | [diff] [blame] | 998 | # Reading the encrypted file without password |
| 999 | # must generate a RunTime exception |
| 1000 | self.assertRaises(RuntimeError, self.zip.read, "test.txt") |
Gregory P. Smith | 0c63fc2 | 2008-01-20 01:21:03 +0000 | [diff] [blame] | 1001 | self.assertRaises(RuntimeError, self.zip2.read, "zero") |
Martin v. Löwis | c6d626e | 2007-02-13 09:49:38 +0000 | [diff] [blame] | 1002 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1003 | def test_bad_password(self): |
Martin v. Löwis | c6d626e | 2007-02-13 09:49:38 +0000 | [diff] [blame] | 1004 | self.zip.setpassword("perl") |
| 1005 | self.assertRaises(RuntimeError, self.zip.read, "test.txt") |
Gregory P. Smith | 0c63fc2 | 2008-01-20 01:21:03 +0000 | [diff] [blame] | 1006 | self.zip2.setpassword("perl") |
| 1007 | self.assertRaises(RuntimeError, self.zip2.read, "zero") |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1008 | |
Ezio Melotti | 1036a7f | 2009-09-12 14:43:43 +0000 | [diff] [blame] | 1009 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1010 | def test_good_password(self): |
Martin v. Löwis | c6d626e | 2007-02-13 09:49:38 +0000 | [diff] [blame] | 1011 | self.zip.setpassword("python") |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 1012 | self.assertEqual(self.zip.read("test.txt"), self.plain) |
Gregory P. Smith | 0c63fc2 | 2008-01-20 01:21:03 +0000 | [diff] [blame] | 1013 | self.zip2.setpassword("12345") |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 1014 | self.assertEqual(self.zip2.read("zero"), self.plain2) |
Martin v. Löwis | c6d626e | 2007-02-13 09:49:38 +0000 | [diff] [blame] | 1015 | |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1016 | |
| 1017 | class TestsWithRandomBinaryFiles(unittest.TestCase): |
| 1018 | def setUp(self): |
| 1019 | datacount = randint(16, 64)*1024 + randint(1, 1024) |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 1020 | self.data = ''.join(struct.pack('<f', random()*randint(-1000, 1000)) |
Ezio Melotti | 763f1e8 | 2009-12-31 13:27:41 +0000 | [diff] [blame] | 1021 | for i in xrange(datacount)) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1022 | |
| 1023 | # Make a source file with some lines |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 1024 | with open(TESTFN, "wb") as fp: |
| 1025 | fp.write(self.data) |
Neal Norwitz | 0d4c06e | 2007-04-25 06:30:05 +0000 | [diff] [blame] | 1026 | |
Collin Winter | 04a51ec | 2007-03-29 02:28:16 +0000 | [diff] [blame] | 1027 | def tearDown(self): |
Ezio Melotti | 6cbfc12 | 2009-07-10 20:25:56 +0000 | [diff] [blame] | 1028 | unlink(TESTFN) |
| 1029 | unlink(TESTFN2) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1030 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1031 | def make_test_archive(self, f, compression): |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1032 | # Create the ZIP archive |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 1033 | with zipfile.ZipFile(f, "w", compression) as zipfp: |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 1034 | zipfp.write(TESTFN, "another.name") |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 1035 | zipfp.write(TESTFN, TESTFN) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1036 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1037 | def zip_test(self, f, compression): |
| 1038 | self.make_test_archive(f, compression) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1039 | |
| 1040 | # Read the ZIP archive |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 1041 | with zipfile.ZipFile(f, "r", compression) as zipfp: |
| 1042 | testdata = zipfp.read(TESTFN) |
| 1043 | self.assertEqual(len(testdata), len(self.data)) |
| 1044 | self.assertEqual(testdata, self.data) |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 1045 | self.assertEqual(zipfp.read("another.name"), self.data) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1046 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1047 | def test_stored(self): |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1048 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1049 | self.zip_test(f, zipfile.ZIP_STORED) |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1050 | |
Antoine Pitrou | e1436d1 | 2010-08-12 15:25:51 +0000 | [diff] [blame] | 1051 | @skipUnless(zlib, "requires zlib") |
| 1052 | def test_deflated(self): |
| 1053 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 1054 | self.zip_test(f, zipfile.ZIP_DEFLATED) |
| 1055 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1056 | def zip_open_test(self, f, compression): |
| 1057 | self.make_test_archive(f, compression) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1058 | |
| 1059 | # Read the ZIP archive |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 1060 | with zipfile.ZipFile(f, "r", compression) as zipfp: |
| 1061 | zipdata1 = [] |
| 1062 | zipopen1 = zipfp.open(TESTFN) |
| 1063 | while True: |
| 1064 | read_data = zipopen1.read(256) |
| 1065 | if not read_data: |
| 1066 | break |
| 1067 | zipdata1.append(read_data) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1068 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 1069 | zipdata2 = [] |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 1070 | zipopen2 = zipfp.open("another.name") |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 1071 | while True: |
| 1072 | read_data = zipopen2.read(256) |
| 1073 | if not read_data: |
| 1074 | break |
| 1075 | zipdata2.append(read_data) |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1076 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 1077 | testdata1 = ''.join(zipdata1) |
| 1078 | self.assertEqual(len(testdata1), len(self.data)) |
| 1079 | self.assertEqual(testdata1, self.data) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1080 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 1081 | testdata2 = ''.join(zipdata2) |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 1082 | self.assertEqual(len(testdata2), len(self.data)) |
| 1083 | self.assertEqual(testdata2, self.data) |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1084 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1085 | def test_open_stored(self): |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1086 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1087 | self.zip_open_test(f, zipfile.ZIP_STORED) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1088 | |
Antoine Pitrou | e1436d1 | 2010-08-12 15:25:51 +0000 | [diff] [blame] | 1089 | @skipUnless(zlib, "requires zlib") |
| 1090 | def test_open_deflated(self): |
| 1091 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 1092 | self.zip_open_test(f, zipfile.ZIP_DEFLATED) |
| 1093 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1094 | def zip_random_open_test(self, f, compression): |
| 1095 | self.make_test_archive(f, compression) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1096 | |
| 1097 | # Read the ZIP archive |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 1098 | with zipfile.ZipFile(f, "r", compression) as zipfp: |
| 1099 | zipdata1 = [] |
| 1100 | zipopen1 = zipfp.open(TESTFN) |
| 1101 | while True: |
| 1102 | read_data = zipopen1.read(randint(1, 1024)) |
| 1103 | if not read_data: |
| 1104 | break |
| 1105 | zipdata1.append(read_data) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1106 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 1107 | testdata = ''.join(zipdata1) |
| 1108 | self.assertEqual(len(testdata), len(self.data)) |
| 1109 | self.assertEqual(testdata, self.data) |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1110 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1111 | def test_random_open_stored(self): |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1112 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1113 | self.zip_random_open_test(f, zipfile.ZIP_STORED) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1114 | |
Antoine Pitrou | e1436d1 | 2010-08-12 15:25:51 +0000 | [diff] [blame] | 1115 | @skipUnless(zlib, "requires zlib") |
| 1116 | def test_random_open_deflated(self): |
| 1117 | for f in (TESTFN2, TemporaryFile(), io.BytesIO()): |
| 1118 | self.zip_random_open_test(f, zipfile.ZIP_DEFLATED) |
| 1119 | |
Ezio Melotti | 6cbfc12 | 2009-07-10 20:25:56 +0000 | [diff] [blame] | 1120 | |
Ezio Melotti | 1036a7f | 2009-09-12 14:43:43 +0000 | [diff] [blame] | 1121 | @skipUnless(zlib, "requires zlib") |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1122 | class TestsWithMultipleOpens(unittest.TestCase): |
| 1123 | def setUp(self): |
| 1124 | # Create the ZIP archive |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 1125 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED) as zipfp: |
| 1126 | zipfp.writestr('ones', '1'*FIXEDTEST_SIZE) |
| 1127 | zipfp.writestr('twos', '2'*FIXEDTEST_SIZE) |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1128 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1129 | def test_same_file(self): |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1130 | # Verify that (when the ZipFile is in control of creating file objects) |
| 1131 | # multiple open() calls can be made without interfering with each other. |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 1132 | with zipfile.ZipFile(TESTFN2, mode="r") as zipf: |
| 1133 | zopen1 = zipf.open('ones') |
| 1134 | zopen2 = zipf.open('ones') |
| 1135 | data1 = zopen1.read(500) |
| 1136 | data2 = zopen2.read(500) |
| 1137 | data1 += zopen1.read(500) |
| 1138 | data2 += zopen2.read(500) |
| 1139 | self.assertEqual(data1, data2) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1140 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1141 | def test_different_file(self): |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1142 | # Verify that (when the ZipFile is in control of creating file objects) |
| 1143 | # multiple open() calls can be made without interfering with each other. |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 1144 | with zipfile.ZipFile(TESTFN2, mode="r") as zipf: |
| 1145 | zopen1 = zipf.open('ones') |
| 1146 | zopen2 = zipf.open('twos') |
| 1147 | data1 = zopen1.read(500) |
| 1148 | data2 = zopen2.read(500) |
| 1149 | data1 += zopen1.read(500) |
| 1150 | data2 += zopen2.read(500) |
| 1151 | self.assertEqual(data1, '1'*FIXEDTEST_SIZE) |
| 1152 | self.assertEqual(data2, '2'*FIXEDTEST_SIZE) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1153 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1154 | def test_interleaved(self): |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1155 | # Verify that (when the ZipFile is in control of creating file objects) |
| 1156 | # multiple open() calls can be made without interfering with each other. |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 1157 | with zipfile.ZipFile(TESTFN2, mode="r") as zipf: |
| 1158 | zopen1 = zipf.open('ones') |
| 1159 | data1 = zopen1.read(500) |
| 1160 | zopen2 = zipf.open('twos') |
| 1161 | data2 = zopen2.read(500) |
| 1162 | data1 += zopen1.read(500) |
| 1163 | data2 += zopen2.read(500) |
| 1164 | self.assertEqual(data1, '1'*FIXEDTEST_SIZE) |
| 1165 | self.assertEqual(data2, '2'*FIXEDTEST_SIZE) |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1166 | |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1167 | def tearDown(self): |
Ezio Melotti | 6cbfc12 | 2009-07-10 20:25:56 +0000 | [diff] [blame] | 1168 | unlink(TESTFN2) |
| 1169 | |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1170 | |
Martin v. Löwis | 0dfcfc8 | 2009-01-24 14:00:33 +0000 | [diff] [blame] | 1171 | class TestWithDirectory(unittest.TestCase): |
| 1172 | def setUp(self): |
| 1173 | os.mkdir(TESTFN2) |
| 1174 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1175 | def test_extract_dir(self): |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 1176 | with zipfile.ZipFile(findfile("zipdir.zip")) as zipf: |
| 1177 | zipf.extractall(TESTFN2) |
Martin v. Löwis | 0dfcfc8 | 2009-01-24 14:00:33 +0000 | [diff] [blame] | 1178 | self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a"))) |
| 1179 | self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a", "b"))) |
| 1180 | self.assertTrue(os.path.exists(os.path.join(TESTFN2, "a", "b", "c"))) |
| 1181 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1182 | def test_bug_6050(self): |
Martin v. Löwis | 0b09c42 | 2009-05-24 19:30:52 +0000 | [diff] [blame] | 1183 | # Extraction should succeed if directories already exist |
| 1184 | os.mkdir(os.path.join(TESTFN2, "a")) |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1185 | self.test_extract_dir() |
Martin v. Löwis | 0b09c42 | 2009-05-24 19:30:52 +0000 | [diff] [blame] | 1186 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1187 | def test_store_dir(self): |
Martin v. Löwis | 0dfcfc8 | 2009-01-24 14:00:33 +0000 | [diff] [blame] | 1188 | os.mkdir(os.path.join(TESTFN2, "x")) |
| 1189 | zipf = zipfile.ZipFile(TESTFN, "w") |
| 1190 | zipf.write(os.path.join(TESTFN2, "x"), "x") |
| 1191 | self.assertTrue(zipf.filelist[0].filename.endswith("x/")) |
| 1192 | |
| 1193 | def tearDown(self): |
| 1194 | shutil.rmtree(TESTFN2) |
| 1195 | if os.path.exists(TESTFN): |
Ezio Melotti | 6cbfc12 | 2009-07-10 20:25:56 +0000 | [diff] [blame] | 1196 | unlink(TESTFN) |
Martin v. Löwis | 0dfcfc8 | 2009-01-24 14:00:33 +0000 | [diff] [blame] | 1197 | |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1198 | |
| 1199 | class UniversalNewlineTests(unittest.TestCase): |
| 1200 | def setUp(self): |
Ezio Melotti | 6d6b53c | 2009-12-31 13:00:43 +0000 | [diff] [blame] | 1201 | self.line_gen = ["Test of zipfile line %d." % i |
| 1202 | for i in xrange(FIXEDTEST_SIZE)] |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1203 | self.seps = ('\r', '\r\n', '\n') |
| 1204 | self.arcdata, self.arcfiles = {}, {} |
| 1205 | for n, s in enumerate(self.seps): |
| 1206 | self.arcdata[s] = s.join(self.line_gen) + s |
| 1207 | self.arcfiles[s] = '%s-%d' % (TESTFN, n) |
Brett Cannon | 6cef076 | 2007-05-25 20:17:15 +0000 | [diff] [blame] | 1208 | open(self.arcfiles[s], "wb").write(self.arcdata[s]) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1209 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1210 | def make_test_archive(self, f, compression): |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1211 | # Create the ZIP archive |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 1212 | with zipfile.ZipFile(f, "w", compression) as zipfp: |
| 1213 | for fn in self.arcfiles.values(): |
| 1214 | zipfp.write(fn, fn) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1215 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1216 | def read_test(self, f, compression): |
| 1217 | self.make_test_archive(f, compression) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1218 | |
| 1219 | # Read the ZIP archive |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 1220 | with zipfile.ZipFile(f, "r") as zipfp: |
| 1221 | for sep, fn in self.arcfiles.items(): |
| 1222 | zipdata = zipfp.open(fn, "rU").read() |
| 1223 | self.assertEqual(self.arcdata[sep], zipdata) |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1224 | |
Antoine Pitrou | 94c33eb | 2010-01-27 20:59:50 +0000 | [diff] [blame] | 1225 | def readline_read_test(self, f, compression): |
| 1226 | self.make_test_archive(f, compression) |
| 1227 | |
| 1228 | # Read the ZIP archive |
| 1229 | zipfp = zipfile.ZipFile(f, "r") |
| 1230 | for sep, fn in self.arcfiles.items(): |
| 1231 | zipopen = zipfp.open(fn, "rU") |
| 1232 | data = '' |
| 1233 | while True: |
| 1234 | read = zipopen.readline() |
| 1235 | if not read: |
| 1236 | break |
| 1237 | data += read |
| 1238 | |
| 1239 | read = zipopen.read(5) |
| 1240 | if not read: |
| 1241 | break |
| 1242 | data += read |
| 1243 | |
| 1244 | self.assertEqual(data, self.arcdata['\n']) |
| 1245 | |
| 1246 | zipfp.close() |
| 1247 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1248 | def readline_test(self, f, compression): |
| 1249 | self.make_test_archive(f, compression) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1250 | |
| 1251 | # Read the ZIP archive |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 1252 | with zipfile.ZipFile(f, "r") as zipfp: |
| 1253 | for sep, fn in self.arcfiles.items(): |
| 1254 | zipopen = zipfp.open(fn, "rU") |
| 1255 | for line in self.line_gen: |
| 1256 | linedata = zipopen.readline() |
| 1257 | self.assertEqual(linedata, line + '\n') |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1258 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1259 | def readlines_test(self, f, compression): |
| 1260 | self.make_test_archive(f, compression) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1261 | |
| 1262 | # Read the ZIP archive |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 1263 | with zipfile.ZipFile(f, "r") as zipfp: |
| 1264 | for sep, fn in self.arcfiles.items(): |
| 1265 | ziplines = zipfp.open(fn, "rU").readlines() |
| 1266 | for line, zipline in zip(self.line_gen, ziplines): |
| 1267 | self.assertEqual(zipline, line + '\n') |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1268 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1269 | def iterlines_test(self, f, compression): |
| 1270 | self.make_test_archive(f, compression) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1271 | |
| 1272 | # Read the ZIP archive |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 1273 | with zipfile.ZipFile(f, "r") as zipfp: |
| 1274 | for sep, fn in self.arcfiles.items(): |
| 1275 | for line, zipline in zip(self.line_gen, zipfp.open(fn, "rU")): |
| 1276 | self.assertEqual(zipline, line + '\n') |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1277 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1278 | def test_read_stored(self): |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1279 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1280 | self.read_test(f, zipfile.ZIP_STORED) |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1281 | |
Antoine Pitrou | 94c33eb | 2010-01-27 20:59:50 +0000 | [diff] [blame] | 1282 | def test_readline_read_stored(self): |
| 1283 | # Issue #7610: calls to readline() interleaved with calls to read(). |
| 1284 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
| 1285 | self.readline_read_test(f, zipfile.ZIP_STORED) |
| 1286 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1287 | def test_readline_stored(self): |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1288 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1289 | self.readline_test(f, zipfile.ZIP_STORED) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1290 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1291 | def test_readlines_stored(self): |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1292 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1293 | self.readlines_test(f, zipfile.ZIP_STORED) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1294 | |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1295 | def test_iterlines_stored(self): |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1296 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1297 | self.iterlines_test(f, zipfile.ZIP_STORED) |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1298 | |
Ezio Melotti | 6cbfc12 | 2009-07-10 20:25:56 +0000 | [diff] [blame] | 1299 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1300 | def test_read_deflated(self): |
Ezio Melotti | 6cbfc12 | 2009-07-10 20:25:56 +0000 | [diff] [blame] | 1301 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1302 | self.read_test(f, zipfile.ZIP_DEFLATED) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1303 | |
Ezio Melotti | 6cbfc12 | 2009-07-10 20:25:56 +0000 | [diff] [blame] | 1304 | @skipUnless(zlib, "requires zlib") |
Antoine Pitrou | 94c33eb | 2010-01-27 20:59:50 +0000 | [diff] [blame] | 1305 | def test_readline_read_deflated(self): |
| 1306 | # Issue #7610: calls to readline() interleaved with calls to read(). |
| 1307 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
| 1308 | self.readline_read_test(f, zipfile.ZIP_DEFLATED) |
| 1309 | |
| 1310 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1311 | def test_readline_deflated(self): |
Ezio Melotti | 6cbfc12 | 2009-07-10 20:25:56 +0000 | [diff] [blame] | 1312 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1313 | self.readline_test(f, zipfile.ZIP_DEFLATED) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1314 | |
Ezio Melotti | 6cbfc12 | 2009-07-10 20:25:56 +0000 | [diff] [blame] | 1315 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1316 | def test_readlines_deflated(self): |
Ezio Melotti | 6cbfc12 | 2009-07-10 20:25:56 +0000 | [diff] [blame] | 1317 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1318 | self.readlines_test(f, zipfile.ZIP_DEFLATED) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1319 | |
Ezio Melotti | 6cbfc12 | 2009-07-10 20:25:56 +0000 | [diff] [blame] | 1320 | @skipUnless(zlib, "requires zlib") |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1321 | def test_iterlines_deflated(self): |
Ezio Melotti | 6cbfc12 | 2009-07-10 20:25:56 +0000 | [diff] [blame] | 1322 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
Ezio Melotti | d5a23e3 | 2009-07-15 17:07:04 +0000 | [diff] [blame] | 1323 | self.iterlines_test(f, zipfile.ZIP_DEFLATED) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1324 | |
| 1325 | def tearDown(self): |
| 1326 | for sep, fn in self.arcfiles.items(): |
| 1327 | os.remove(fn) |
Ezio Melotti | 6cbfc12 | 2009-07-10 20:25:56 +0000 | [diff] [blame] | 1328 | unlink(TESTFN) |
| 1329 | unlink(TESTFN2) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1330 | |
| 1331 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 1332 | def test_main(): |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1333 | run_unittest(TestsWithSourceFile, TestZip64InSmallFiles, OtherTests, |
| 1334 | PyZipFileTests, DecryptionTests, TestsWithMultipleOpens, |
Ezio Melotti | 6cbfc12 | 2009-07-10 20:25:56 +0000 | [diff] [blame] | 1335 | TestWithDirectory, UniversalNewlineTests, |
| 1336 | TestsWithRandomBinaryFiles) |
Martin v. Löwis | 3eb7648 | 2007-03-06 10:41:24 +0000 | [diff] [blame] | 1337 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 1338 | if __name__ == "__main__": |
| 1339 | test_main() |