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