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