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