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