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