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