Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +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 |
| 5 | # XXX(nnorwitz): disable this test by looking for extra largfile resource |
| 6 | # which doesn't exist. This test takes over 30 minutes to run in general |
| 7 | # and requires more disk space than most of the buildbots. |
| 8 | test_support.requires( |
| 9 | 'extralargefile', |
| 10 | 'test requires loads of disk-space bytes and a long time to run' |
| 11 | ) |
| 12 | |
| 13 | # We can test part of the module without zlib. |
| 14 | try: |
| 15 | import zlib |
| 16 | except ImportError: |
| 17 | zlib = None |
| 18 | |
| 19 | import zipfile, os, unittest |
| 20 | import time |
| 21 | import sys |
| 22 | |
| 23 | from StringIO import StringIO |
| 24 | from tempfile import TemporaryFile |
| 25 | |
| 26 | from test.test_support import TESTFN, run_unittest |
| 27 | |
| 28 | TESTFN2 = TESTFN + "2" |
| 29 | |
| 30 | # How much time in seconds can pass before we print a 'Still working' message. |
| 31 | _PRINT_WORKING_MSG_INTERVAL = 5 * 60 |
| 32 | |
| 33 | class TestsWithSourceFile(unittest.TestCase): |
| 34 | def setUp(self): |
| 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)) |
| 39 | self.data = '\n'.join(line_gen) |
| 40 | |
| 41 | # And write it to a file. |
| 42 | fp = open(TESTFN, "wb") |
| 43 | fp.write(self.data) |
| 44 | fp.close() |
| 45 | |
| 46 | def zipTest(self, f, compression): |
| 47 | # Create the ZIP archive. |
| 48 | zipfp = zipfile.ZipFile(f, "w", compression, allowZip64=True) |
| 49 | |
| 50 | # It will contain enough copies of self.data to reach about 6GB of |
| 51 | # raw data to store. |
| 52 | filecount = 6*1024**3 // len(self.data) |
| 53 | |
| 54 | next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL |
| 55 | for num in range(filecount): |
| 56 | zipfp.writestr("testfn%d" % num, self.data) |
| 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 |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 60 | print(( |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 61 | ' zipTest still writing %d of %d, be patient...' % |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 62 | (num, filecount)), file=sys.__stdout__) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 63 | sys.__stdout__.flush() |
| 64 | zipfp.close() |
| 65 | |
| 66 | # Read the ZIP archive |
| 67 | zipfp = zipfile.ZipFile(f, "r", compression) |
| 68 | for num in range(filecount): |
| 69 | self.assertEqual(zipfp.read("testfn%d" % num), self.data) |
| 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 |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 73 | print(( |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 74 | ' zipTest still reading %d of %d, be patient...' % |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 75 | (num, filecount)), file=sys.__stdout__) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 76 | sys.__stdout__.flush() |
| 77 | zipfp.close() |
| 78 | |
| 79 | def testStored(self): |
| 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: |
| 83 | self.zipTest(f, zipfile.ZIP_STORED) |
| 84 | |
| 85 | if zlib: |
| 86 | def testDeflated(self): |
| 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: |
| 90 | self.zipTest(f, zipfile.ZIP_DEFLATED) |
| 91 | |
| 92 | def tearDown(self): |
| 93 | for fname in TESTFN, TESTFN2: |
| 94 | if os.path.exists(fname): |
| 95 | os.remove(fname) |
| 96 | |
| 97 | def test_main(): |
| 98 | run_unittest(TestsWithSourceFile) |
| 99 | |
| 100 | if __name__ == "__main__": |
| 101 | test_main() |