blob: a008fd03130b46c6ce922eb182b1f250aba31417 [file] [log] [blame]
Thomas Wouters0e3f5912006-08-11 14:57:12 +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
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.
8test_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.
14try:
15 import zlib
16except ImportError:
17 zlib = None
18
19import zipfile, os, unittest
20import time
21import sys
22
Guido van Rossum34d19282007-08-09 01:03:29 +000023from io import StringIO
Thomas Wouters0e3f5912006-08-11 14:57:12 +000024from tempfile import TemporaryFile
25
26from test.test_support import TESTFN, run_unittest
27
28TESTFN2 = 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
33class TestsWithSourceFile(unittest.TestCase):
34 def setUp(self):
35 # Create test data.
Guido van Rossum805365e2007-05-07 22:24:25 +000036 line_gen = ("Test of zipfile line %d." % i for i in range(1000000))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000037 self.data = '\n'.join(line_gen)
38
39 # And write it to a file.
40 fp = open(TESTFN, "wb")
41 fp.write(self.data)
42 fp.close()
43
44 def zipTest(self, f, compression):
45 # Create the ZIP archive.
46 zipfp = zipfile.ZipFile(f, "w", compression, allowZip64=True)
47
48 # It will contain enough copies of self.data to reach about 6GB of
49 # raw data to store.
50 filecount = 6*1024**3 // len(self.data)
51
52 next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL
53 for num in range(filecount):
54 zipfp.writestr("testfn%d" % num, self.data)
55 # Print still working message since this test can be really slow
56 if next_time <= time.time():
57 next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL
Guido van Rossumbe19ed72007-02-09 05:37:30 +000058 print((
Thomas Wouters0e3f5912006-08-11 14:57:12 +000059 ' zipTest still writing %d of %d, be patient...' %
Guido van Rossumbe19ed72007-02-09 05:37:30 +000060 (num, filecount)), file=sys.__stdout__)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000061 sys.__stdout__.flush()
62 zipfp.close()
63
64 # Read the ZIP archive
65 zipfp = zipfile.ZipFile(f, "r", compression)
66 for num in range(filecount):
67 self.assertEqual(zipfp.read("testfn%d" % num), self.data)
68 # Print still working message since this test can be really slow
69 if next_time <= time.time():
70 next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL
Guido van Rossumbe19ed72007-02-09 05:37:30 +000071 print((
Thomas Wouters0e3f5912006-08-11 14:57:12 +000072 ' zipTest still reading %d of %d, be patient...' %
Guido van Rossumbe19ed72007-02-09 05:37:30 +000073 (num, filecount)), file=sys.__stdout__)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000074 sys.__stdout__.flush()
75 zipfp.close()
76
77 def testStored(self):
78 # Try the temp file first. If we do TESTFN2 first, then it hogs
79 # gigabytes of disk space for the duration of the test.
80 for f in TemporaryFile(), TESTFN2:
81 self.zipTest(f, zipfile.ZIP_STORED)
82
83 if zlib:
84 def testDeflated(self):
85 # Try the temp file first. If we do TESTFN2 first, then it hogs
86 # gigabytes of disk space for the duration of the test.
87 for f in TemporaryFile(), TESTFN2:
88 self.zipTest(f, zipfile.ZIP_DEFLATED)
89
90 def tearDown(self):
91 for fname in TESTFN, TESTFN2:
92 if os.path.exists(fname):
93 os.remove(fname)
94
95def test_main():
96 run_unittest(TestsWithSourceFile)
97
98if __name__ == "__main__":
99 test_main()