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