blob: 6ce2ae593b44a3207a9e4d576f504507d4d738da [file] [log] [blame]
Ronald Oussoren143cefb2006-06-15 08:14:18 +00001# 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
4from test import test_support
Neal Norwitz217046f2006-06-16 04:30:34 +00005# 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.
Ronald Oussoren143cefb2006-06-15 08:14:18 +00008test_support.requires(
Neal Norwitz217046f2006-06-16 04:30:34 +00009 'extralargefile',
Ronald Oussoren143cefb2006-06-15 08:14:18 +000010 '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.
14try:
15 import zlib
16except ImportError:
17 zlib = None
18
19import zipfile, os, unittest
Neal Norwitz643ad192006-06-15 09:57:03 +000020import time
21import sys
Ronald Oussoren143cefb2006-06-15 08:14:18 +000022
Ronald Oussoren143cefb2006-06-15 08:14:18 +000023from tempfile import TemporaryFile
24
25from test.test_support import TESTFN, run_unittest
26
27TESTFN2 = TESTFN + "2"
28
Neal Norwitz643ad192006-06-15 09:57:03 +000029# How much time in seconds can pass before we print a 'Still working' message.
30_PRINT_WORKING_MSG_INTERVAL = 5 * 60
31
Ronald Oussoren143cefb2006-06-15 08:14:18 +000032class TestsWithSourceFile(unittest.TestCase):
33 def setUp(self):
Tim Peters84b0f582006-06-15 18:04:40 +000034 # Create test data.
35 # xrange() is important here -- don't want to create immortal space
36 # for a million ints.
37 line_gen = ("Test of zipfile line %d." % i for i in xrange(1000000))
Ronald Oussoren143cefb2006-06-15 08:14:18 +000038 self.data = '\n'.join(line_gen)
39
Tim Peters84b0f582006-06-15 18:04:40 +000040 # And write it to a file.
Ronald Oussoren143cefb2006-06-15 08:14:18 +000041 fp = open(TESTFN, "wb")
42 fp.write(self.data)
43 fp.close()
44
45 def zipTest(self, f, compression):
Tim Peters84b0f582006-06-15 18:04:40 +000046 # Create the ZIP archive.
Ronald Oussoren143cefb2006-06-15 08:14:18 +000047 zipfp = zipfile.ZipFile(f, "w", compression, allowZip64=True)
48
Tim Peters84b0f582006-06-15 18:04:40 +000049 # It will contain enough copies of self.data to reach about 6GB of
50 # raw data to store.
Tim Petersda4b84a2006-06-15 18:38:19 +000051 filecount = 6*1024**3 // len(self.data)
Tim Peters84b0f582006-06-15 18:04:40 +000052
Neal Norwitz643ad192006-06-15 09:57:03 +000053 next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL
Ronald Oussoren143cefb2006-06-15 08:14:18 +000054 for num in range(filecount):
Tim Peters84b0f582006-06-15 18:04:40 +000055 zipfp.writestr("testfn%d" % num, self.data)
Neal Norwitz643ad192006-06-15 09:57:03 +000056 # 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
Neal Norwitzbda14182006-06-15 10:24:49 +000059 print >>sys.__stdout__, (
60 ' zipTest still writing %d of %d, be patient...' %
61 (num, filecount))
Neal Norwitz643ad192006-06-15 09:57:03 +000062 sys.__stdout__.flush()
Ronald Oussoren143cefb2006-06-15 08:14:18 +000063 zipfp.close()
64
65 # Read the ZIP archive
66 zipfp = zipfile.ZipFile(f, "r", compression)
67 for num in range(filecount):
Tim Peters84b0f582006-06-15 18:04:40 +000068 self.assertEqual(zipfp.read("testfn%d" % num), self.data)
Neal Norwitz643ad192006-06-15 09:57:03 +000069 # 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
Neal Norwitzbda14182006-06-15 10:24:49 +000072 print >>sys.__stdout__, (
73 ' zipTest still reading %d of %d, be patient...' %
74 (num, filecount))
Neal Norwitz643ad192006-06-15 09:57:03 +000075 sys.__stdout__.flush()
Ronald Oussoren143cefb2006-06-15 08:14:18 +000076 zipfp.close()
77
78 def testStored(self):
Tim Peters84b0f582006-06-15 18:04:40 +000079 # 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:
Ronald Oussoren143cefb2006-06-15 08:14:18 +000082 self.zipTest(f, zipfile.ZIP_STORED)
83
84 if zlib:
85 def testDeflated(self):
Tim Peters84b0f582006-06-15 18:04:40 +000086 # 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:
Ronald Oussoren143cefb2006-06-15 08:14:18 +000089 self.zipTest(f, zipfile.ZIP_DEFLATED)
90
91 def tearDown(self):
Tim Peters84b0f582006-06-15 18:04:40 +000092 for fname in TESTFN, TESTFN2:
93 if os.path.exists(fname):
94 os.remove(fname)
Ronald Oussoren143cefb2006-06-15 08:14:18 +000095
96def test_main():
97 run_unittest(TestsWithSourceFile)
98
99if __name__ == "__main__":
100 test_main()