Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1 | # Tests of the full ZIP64 functionality of zipfile |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 2 | # The support.requires call is the only reason for keeping this separate |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3 | # from test_zipfile |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 4 | from test import support |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 5 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +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. |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 9 | support.requires( |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 10 | 'extralargefile', |
| 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 |
| 21 | import time |
| 22 | import sys |
| 23 | |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 24 | from io import StringIO |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 25 | from tempfile import TemporaryFile |
| 26 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 27 | from test.support import TESTFN, run_unittest |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 28 | |
| 29 | TESTFN2 = TESTFN + "2" |
| 30 | |
| 31 | # How much time in seconds can pass before we print a 'Still working' message. |
| 32 | _PRINT_WORKING_MSG_INTERVAL = 5 * 60 |
| 33 | |
| 34 | class TestsWithSourceFile(unittest.TestCase): |
| 35 | def setUp(self): |
| 36 | # Create test data. |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 37 | line_gen = ("Test of zipfile line %d." % i for i in range(1000000)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 38 | self.data = '\n'.join(line_gen) |
| 39 | |
| 40 | # And write it to a file. |
| 41 | fp = open(TESTFN, "wb") |
| 42 | fp.write(self.data) |
| 43 | fp.close() |
| 44 | |
| 45 | def zipTest(self, f, compression): |
| 46 | # Create the ZIP archive. |
| 47 | zipfp = zipfile.ZipFile(f, "w", compression, allowZip64=True) |
| 48 | |
| 49 | # It will contain enough copies of self.data to reach about 6GB of |
| 50 | # raw data to store. |
| 51 | filecount = 6*1024**3 // len(self.data) |
| 52 | |
| 53 | next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL |
| 54 | for num in range(filecount): |
| 55 | zipfp.writestr("testfn%d" % num, self.data) |
| 56 | # Print still working message since this test can be really slow |
| 57 | if next_time <= time.time(): |
| 58 | next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 59 | print(( |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 60 | ' zipTest still writing %d of %d, be patient...' % |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 61 | (num, filecount)), file=sys.__stdout__) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 62 | sys.__stdout__.flush() |
| 63 | zipfp.close() |
| 64 | |
| 65 | # Read the ZIP archive |
| 66 | zipfp = zipfile.ZipFile(f, "r", compression) |
| 67 | for num in range(filecount): |
| 68 | self.assertEqual(zipfp.read("testfn%d" % num), self.data) |
| 69 | # Print still working message since this test can be really slow |
| 70 | if next_time <= time.time(): |
| 71 | next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 72 | print(( |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 73 | ' zipTest still reading %d of %d, be patient...' % |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 74 | (num, filecount)), file=sys.__stdout__) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 75 | sys.__stdout__.flush() |
| 76 | zipfp.close() |
| 77 | |
| 78 | def testStored(self): |
| 79 | # Try the temp file first. If we do TESTFN2 first, then it hogs |
| 80 | # gigabytes of disk space for the duration of the test. |
| 81 | for f in TemporaryFile(), TESTFN2: |
| 82 | self.zipTest(f, zipfile.ZIP_STORED) |
| 83 | |
| 84 | if zlib: |
| 85 | def testDeflated(self): |
| 86 | # Try the temp file first. If we do TESTFN2 first, then it hogs |
| 87 | # gigabytes of disk space for the duration of the test. |
| 88 | for f in TemporaryFile(), TESTFN2: |
| 89 | self.zipTest(f, zipfile.ZIP_DEFLATED) |
| 90 | |
| 91 | def tearDown(self): |
| 92 | for fname in TESTFN, TESTFN2: |
| 93 | if os.path.exists(fname): |
| 94 | os.remove(fname) |
| 95 | |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 96 | |
| 97 | class OtherTests(unittest.TestCase): |
| 98 | def testMoreThan64kFiles(self): |
| 99 | # This test checks that more than 64k files can be added to an archive, |
| 100 | # and that the resulting archive can be read properly by ZipFile |
| 101 | zipf = zipfile.ZipFile(TESTFN, mode="w") |
| 102 | zipf.debug = 100 |
| 103 | numfiles = (1 << 16) * 3/2 |
| 104 | for i in xrange(numfiles): |
| 105 | zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57)) |
| 106 | self.assertEqual(len(zipf.namelist()), numfiles) |
| 107 | zipf.close() |
| 108 | |
| 109 | zipf2 = zipfile.ZipFile(TESTFN, mode="r") |
| 110 | self.assertEqual(len(zipf2.namelist()), numfiles) |
| 111 | for i in xrange(numfiles): |
| 112 | self.assertEqual(zipf2.read("foo%08d" % i), "%d" % (i**3 % 57)) |
| 113 | zipf.close() |
| 114 | |
| 115 | def tearDown(self): |
| 116 | test_support.unlink(TESTFN) |
| 117 | test_support.unlink(TESTFN2) |
| 118 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 119 | def test_main(): |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 120 | run_unittest(TestsWithSourceFile, OtherTests) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 121 | |
| 122 | if __name__ == "__main__": |
| 123 | test_main() |