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 |
| 5 | test_support.requires( |
Tim Peters | 84b0f58 | 2006-06-15 18:04:40 +0000 | [diff] [blame^] | 6 | 'largefile', |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 7 | 'test requires loads of disk-space bytes and a long time to run' |
| 8 | ) |
| 9 | |
| 10 | # We can test part of the module without zlib. |
| 11 | try: |
| 12 | import zlib |
| 13 | except ImportError: |
| 14 | zlib = None |
| 15 | |
| 16 | import zipfile, os, unittest |
Neal Norwitz | 643ad19 | 2006-06-15 09:57:03 +0000 | [diff] [blame] | 17 | import time |
| 18 | import sys |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 19 | |
| 20 | from StringIO import StringIO |
| 21 | from tempfile import TemporaryFile |
| 22 | |
| 23 | from test.test_support import TESTFN, run_unittest |
| 24 | |
| 25 | TESTFN2 = TESTFN + "2" |
| 26 | |
Neal Norwitz | 643ad19 | 2006-06-15 09:57:03 +0000 | [diff] [blame] | 27 | # How much time in seconds can pass before we print a 'Still working' message. |
| 28 | _PRINT_WORKING_MSG_INTERVAL = 5 * 60 |
| 29 | |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 30 | class TestsWithSourceFile(unittest.TestCase): |
| 31 | def setUp(self): |
Tim Peters | 84b0f58 | 2006-06-15 18:04:40 +0000 | [diff] [blame^] | 32 | # Create test data. |
| 33 | # xrange() is important here -- don't want to create immortal space |
| 34 | # for a million ints. |
| 35 | 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] | 36 | self.data = '\n'.join(line_gen) |
| 37 | |
Tim Peters | 84b0f58 | 2006-06-15 18:04:40 +0000 | [diff] [blame^] | 38 | # And write it to a file. |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 39 | fp = open(TESTFN, "wb") |
| 40 | fp.write(self.data) |
| 41 | fp.close() |
| 42 | |
| 43 | def zipTest(self, f, compression): |
Tim Peters | 84b0f58 | 2006-06-15 18:04:40 +0000 | [diff] [blame^] | 44 | # Create the ZIP archive. |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 45 | zipfp = zipfile.ZipFile(f, "w", compression, allowZip64=True) |
| 46 | |
Tim Peters | 84b0f58 | 2006-06-15 18:04:40 +0000 | [diff] [blame^] | 47 | # It will contain enough copies of self.data to reach about 6GB of |
| 48 | # raw data to store. |
| 49 | filecount = 6*1024**2 // len(self.data) |
| 50 | |
Neal Norwitz | 643ad19 | 2006-06-15 09:57:03 +0000 | [diff] [blame] | 51 | next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 52 | for num in range(filecount): |
Tim Peters | 84b0f58 | 2006-06-15 18:04:40 +0000 | [diff] [blame^] | 53 | zipfp.writestr("testfn%d" % num, self.data) |
Neal Norwitz | 643ad19 | 2006-06-15 09:57:03 +0000 | [diff] [blame] | 54 | # Print still working message since this test can be really slow |
| 55 | if next_time <= time.time(): |
| 56 | next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL |
Neal Norwitz | bda1418 | 2006-06-15 10:24:49 +0000 | [diff] [blame] | 57 | print >>sys.__stdout__, ( |
| 58 | ' zipTest still writing %d of %d, be patient...' % |
| 59 | (num, filecount)) |
Neal Norwitz | 643ad19 | 2006-06-15 09:57:03 +0000 | [diff] [blame] | 60 | sys.__stdout__.flush() |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 61 | zipfp.close() |
| 62 | |
| 63 | # Read the ZIP archive |
| 64 | zipfp = zipfile.ZipFile(f, "r", compression) |
| 65 | for num in range(filecount): |
Tim Peters | 84b0f58 | 2006-06-15 18:04:40 +0000 | [diff] [blame^] | 66 | self.assertEqual(zipfp.read("testfn%d" % num), self.data) |
Neal Norwitz | 643ad19 | 2006-06-15 09:57:03 +0000 | [diff] [blame] | 67 | # Print still working message since this test can be really slow |
| 68 | if next_time <= time.time(): |
| 69 | next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL |
Neal Norwitz | bda1418 | 2006-06-15 10:24:49 +0000 | [diff] [blame] | 70 | print >>sys.__stdout__, ( |
| 71 | ' zipTest still reading %d of %d, be patient...' % |
| 72 | (num, filecount)) |
Neal Norwitz | 643ad19 | 2006-06-15 09:57:03 +0000 | [diff] [blame] | 73 | sys.__stdout__.flush() |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 74 | zipfp.close() |
| 75 | |
| 76 | def testStored(self): |
Tim Peters | 84b0f58 | 2006-06-15 18:04:40 +0000 | [diff] [blame^] | 77 | # Try the temp file first. If we do TESTFN2 first, then it hogs |
| 78 | # gigabytes of disk space for the duration of the test. |
| 79 | for f in TemporaryFile(), TESTFN2: |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 80 | self.zipTest(f, zipfile.ZIP_STORED) |
| 81 | |
| 82 | if zlib: |
| 83 | def testDeflated(self): |
Tim Peters | 84b0f58 | 2006-06-15 18:04:40 +0000 | [diff] [blame^] | 84 | # Try the temp file first. If we do TESTFN2 first, then it hogs |
| 85 | # gigabytes of disk space for the duration of the test. |
| 86 | for f in TemporaryFile(), TESTFN2: |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 87 | self.zipTest(f, zipfile.ZIP_DEFLATED) |
| 88 | |
| 89 | def tearDown(self): |
Tim Peters | 84b0f58 | 2006-06-15 18:04:40 +0000 | [diff] [blame^] | 90 | for fname in TESTFN, TESTFN2: |
| 91 | if os.path.exists(fname): |
| 92 | os.remove(fname) |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 93 | |
| 94 | def test_main(): |
| 95 | run_unittest(TestsWithSourceFile) |
| 96 | |
| 97 | if __name__ == "__main__": |
| 98 | test_main() |