Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 1 | # Tests of the full ZIP64 functionality of zipfile |
| 2 | # The test_support.requires call is the only reason for keeping this separate |
| 3 | # from test_zipfile |
| 4 | from test import test_support |
Martin v. Löwis | 8c43641 | 2008-07-03 12:51:14 +0000 | [diff] [blame] | 5 | |
Neal Norwitz | 217046f | 2006-06-16 04:30:34 +0000 | [diff] [blame] | 6 | # XXX(nnorwitz): disable this test by looking for extra largfile resource |
| 7 | # which doesn't exist. This test takes over 30 minutes to run in general |
| 8 | # and requires more disk space than most of the buildbots. |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 9 | test_support.requires( |
Neal Norwitz | 217046f | 2006-06-16 04:30:34 +0000 | [diff] [blame] | 10 | 'extralargefile', |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 11 | 'test requires loads of disk-space bytes and a long time to run' |
| 12 | ) |
| 13 | |
| 14 | # We can test part of the module without zlib. |
| 15 | try: |
| 16 | import zlib |
| 17 | except ImportError: |
| 18 | zlib = None |
| 19 | |
| 20 | import zipfile, os, unittest |
Neal Norwitz | 643ad19 | 2006-06-15 09:57:03 +0000 | [diff] [blame] | 21 | import time |
| 22 | import sys |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 23 | |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 24 | from tempfile import TemporaryFile |
| 25 | |
| 26 | from test.test_support import TESTFN, run_unittest |
| 27 | |
| 28 | TESTFN2 = TESTFN + "2" |
| 29 | |
Neal Norwitz | 643ad19 | 2006-06-15 09:57:03 +0000 | [diff] [blame] | 30 | # How much time in seconds can pass before we print a 'Still working' message. |
| 31 | _PRINT_WORKING_MSG_INTERVAL = 5 * 60 |
| 32 | |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 33 | class TestsWithSourceFile(unittest.TestCase): |
| 34 | def setUp(self): |
Tim Peters | 84b0f58 | 2006-06-15 18:04:40 +0000 | [diff] [blame] | 35 | # Create test data. |
| 36 | # xrange() is important here -- don't want to create immortal space |
| 37 | # for a million ints. |
| 38 | line_gen = ("Test of zipfile line %d." % i for i in xrange(1000000)) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 39 | self.data = '\n'.join(line_gen) |
| 40 | |
Tim Peters | 84b0f58 | 2006-06-15 18:04:40 +0000 | [diff] [blame] | 41 | # And write it to a file. |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 42 | fp = open(TESTFN, "wb") |
| 43 | fp.write(self.data) |
| 44 | fp.close() |
| 45 | |
| 46 | def zipTest(self, f, compression): |
Tim Peters | 84b0f58 | 2006-06-15 18:04:40 +0000 | [diff] [blame] | 47 | # Create the ZIP archive. |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 48 | zipfp = zipfile.ZipFile(f, "w", compression, allowZip64=True) |
| 49 | |
Tim Peters | 84b0f58 | 2006-06-15 18:04:40 +0000 | [diff] [blame] | 50 | # It will contain enough copies of self.data to reach about 6GB of |
| 51 | # raw data to store. |
Tim Peters | da4b84a | 2006-06-15 18:38:19 +0000 | [diff] [blame] | 52 | filecount = 6*1024**3 // len(self.data) |
Tim Peters | 84b0f58 | 2006-06-15 18:04:40 +0000 | [diff] [blame] | 53 | |
Neal Norwitz | 643ad19 | 2006-06-15 09:57:03 +0000 | [diff] [blame] | 54 | next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 55 | for num in range(filecount): |
Tim Peters | 84b0f58 | 2006-06-15 18:04:40 +0000 | [diff] [blame] | 56 | zipfp.writestr("testfn%d" % num, self.data) |
Neal Norwitz | 643ad19 | 2006-06-15 09:57:03 +0000 | [diff] [blame] | 57 | # Print still working message since this test can be really slow |
| 58 | if next_time <= time.time(): |
| 59 | next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL |
Neal Norwitz | bda1418 | 2006-06-15 10:24:49 +0000 | [diff] [blame] | 60 | print >>sys.__stdout__, ( |
| 61 | ' zipTest still writing %d of %d, be patient...' % |
| 62 | (num, filecount)) |
Neal Norwitz | 643ad19 | 2006-06-15 09:57:03 +0000 | [diff] [blame] | 63 | sys.__stdout__.flush() |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 64 | zipfp.close() |
| 65 | |
| 66 | # Read the ZIP archive |
| 67 | zipfp = zipfile.ZipFile(f, "r", compression) |
| 68 | for num in range(filecount): |
Tim Peters | 84b0f58 | 2006-06-15 18:04:40 +0000 | [diff] [blame] | 69 | self.assertEqual(zipfp.read("testfn%d" % num), self.data) |
Neal Norwitz | 643ad19 | 2006-06-15 09:57:03 +0000 | [diff] [blame] | 70 | # Print still working message since this test can be really slow |
| 71 | if next_time <= time.time(): |
| 72 | next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL |
Neal Norwitz | bda1418 | 2006-06-15 10:24:49 +0000 | [diff] [blame] | 73 | print >>sys.__stdout__, ( |
| 74 | ' zipTest still reading %d of %d, be patient...' % |
| 75 | (num, filecount)) |
Neal Norwitz | 643ad19 | 2006-06-15 09:57:03 +0000 | [diff] [blame] | 76 | sys.__stdout__.flush() |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 77 | zipfp.close() |
| 78 | |
| 79 | def testStored(self): |
Tim Peters | 84b0f58 | 2006-06-15 18:04:40 +0000 | [diff] [blame] | 80 | # Try the temp file first. If we do TESTFN2 first, then it hogs |
| 81 | # gigabytes of disk space for the duration of the test. |
| 82 | for f in TemporaryFile(), TESTFN2: |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 83 | self.zipTest(f, zipfile.ZIP_STORED) |
| 84 | |
| 85 | if zlib: |
| 86 | def testDeflated(self): |
Tim Peters | 84b0f58 | 2006-06-15 18:04:40 +0000 | [diff] [blame] | 87 | # Try the temp file first. If we do TESTFN2 first, then it hogs |
| 88 | # gigabytes of disk space for the duration of the test. |
| 89 | for f in TemporaryFile(), TESTFN2: |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 90 | self.zipTest(f, zipfile.ZIP_DEFLATED) |
| 91 | |
| 92 | def tearDown(self): |
Tim Peters | 84b0f58 | 2006-06-15 18:04:40 +0000 | [diff] [blame] | 93 | for fname in TESTFN, TESTFN2: |
| 94 | if os.path.exists(fname): |
| 95 | os.remove(fname) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 96 | |
Martin v. Löwis | 8c43641 | 2008-07-03 12:51:14 +0000 | [diff] [blame] | 97 | |
| 98 | class OtherTests(unittest.TestCase): |
| 99 | def testMoreThan64kFiles(self): |
| 100 | # This test checks that more than 64k files can be added to an archive, |
| 101 | # and that the resulting archive can be read properly by ZipFile |
| 102 | zipf = zipfile.ZipFile(TESTFN, mode="w") |
| 103 | zipf.debug = 100 |
| 104 | numfiles = (1 << 16) * 3/2 |
| 105 | for i in xrange(numfiles): |
| 106 | zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57)) |
| 107 | self.assertEqual(len(zipf.namelist()), numfiles) |
| 108 | zipf.close() |
| 109 | |
| 110 | zipf2 = zipfile.ZipFile(TESTFN, mode="r") |
| 111 | self.assertEqual(len(zipf2.namelist()), numfiles) |
| 112 | for i in xrange(numfiles): |
| 113 | self.assertEqual(zipf2.read("foo%08d" % i), "%d" % (i**3 % 57)) |
| 114 | zipf.close() |
| 115 | |
| 116 | def tearDown(self): |
| 117 | test_support.unlink(TESTFN) |
| 118 | test_support.unlink(TESTFN2) |
| 119 | |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 120 | def test_main(): |
Martin v. Löwis | 8c43641 | 2008-07-03 12:51:14 +0000 | [diff] [blame] | 121 | run_unittest(TestsWithSourceFile, OtherTests) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 122 | |
| 123 | if __name__ == "__main__": |
| 124 | test_main() |