PageRenderTime 959ms CodeModel.GetById 402ms app.highlight 454ms RepoModel.GetById 89ms app.codeStats 1ms

/Lib/test/test_zipfile.py

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