blob: cba909f6bce2772430b09643c4239440d3f44540 [file] [log] [blame]
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001# Tests of the full ZIP64 functionality of zipfile
Benjamin Petersonee8712c2008-05-20 21:35:26 +00002# The support.requires call is the only reason for keeping this separate
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003# from test_zipfile
Benjamin Petersonee8712c2008-05-20 21:35:26 +00004from test import support
Martin v. Löwisb09b8442008-07-03 14:13:42 +00005
Martin Panter6f9b0102015-12-17 10:18:28 +00006# XXX(nnorwitz): disable this test by looking for extralargefile resource,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007# 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 Petersonee8712c2008-05-20 21:35:26 +00009support.requires(
Thomas Wouters0e3f5912006-08-11 14:57:12 +000010 'extralargefile',
11 'test requires loads of disk-space bytes and a long time to run'
12 )
13
Thomas Wouters0e3f5912006-08-11 14:57:12 +000014import zipfile, os, unittest
15import time
16import sys
17
Thomas Wouters0e3f5912006-08-11 14:57:12 +000018from tempfile import TemporaryFile
19
Serhiy Storchakacfbb3942014-09-23 21:34:24 +030020from test.support import TESTFN, requires_zlib
Thomas Wouters0e3f5912006-08-11 14:57:12 +000021
22TESTFN2 = TESTFN + "2"
23
24# How much time in seconds can pass before we print a 'Still working' message.
25_PRINT_WORKING_MSG_INTERVAL = 5 * 60
26
27class TestsWithSourceFile(unittest.TestCase):
28 def setUp(self):
29 # Create test data.
Guido van Rossum805365e2007-05-07 22:24:25 +000030 line_gen = ("Test of zipfile line %d." % i for i in range(1000000))
Amaury Forgeot d'Arc3be2f042008-11-12 01:57:36 +000031 self.data = '\n'.join(line_gen).encode('ascii')
Thomas Wouters0e3f5912006-08-11 14:57:12 +000032
33 # And write it to a file.
34 fp = open(TESTFN, "wb")
35 fp.write(self.data)
36 fp.close()
37
38 def zipTest(self, f, compression):
39 # Create the ZIP archive.
Serhiy Storchaka235c5e02013-11-23 15:55:38 +020040 zipfp = zipfile.ZipFile(f, "w", compression)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000041
Victor Stinner8c663fd2017-11-08 14:44:44 -080042 # It will contain enough copies of self.data to reach about 6 GiB of
Thomas Wouters0e3f5912006-08-11 14:57:12 +000043 # raw data to store.
44 filecount = 6*1024**3 // len(self.data)
45
46 next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL
47 for num in range(filecount):
48 zipfp.writestr("testfn%d" % num, self.data)
49 # Print still working message since this test can be really slow
50 if next_time <= time.time():
51 next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL
Guido van Rossumbe19ed72007-02-09 05:37:30 +000052 print((
Thomas Wouters0e3f5912006-08-11 14:57:12 +000053 ' zipTest still writing %d of %d, be patient...' %
Guido van Rossumbe19ed72007-02-09 05:37:30 +000054 (num, filecount)), file=sys.__stdout__)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000055 sys.__stdout__.flush()
56 zipfp.close()
57
58 # Read the ZIP archive
59 zipfp = zipfile.ZipFile(f, "r", compression)
60 for num in range(filecount):
61 self.assertEqual(zipfp.read("testfn%d" % num), self.data)
62 # Print still working message since this test can be really slow
63 if next_time <= time.time():
64 next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL
Guido van Rossumbe19ed72007-02-09 05:37:30 +000065 print((
Thomas Wouters0e3f5912006-08-11 14:57:12 +000066 ' zipTest still reading %d of %d, be patient...' %
Guido van Rossumbe19ed72007-02-09 05:37:30 +000067 (num, filecount)), file=sys.__stdout__)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000068 sys.__stdout__.flush()
69 zipfp.close()
70
71 def testStored(self):
72 # Try the temp file first. If we do TESTFN2 first, then it hogs
73 # gigabytes of disk space for the duration of the test.
Serhiy Storchakaf8282182016-02-25 12:55:19 +020074 with TemporaryFile() as f:
Thomas Wouters0e3f5912006-08-11 14:57:12 +000075 self.zipTest(f, zipfile.ZIP_STORED)
Serhiy Storchakaf8282182016-02-25 12:55:19 +020076 self.assertFalse(f.closed)
77 self.zipTest(TESTFN2, zipfile.ZIP_STORED)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000078
Ezio Melotti975077a2011-05-19 22:03:22 +030079 @requires_zlib
80 def testDeflated(self):
81 # Try the temp file first. If we do TESTFN2 first, then it hogs
82 # gigabytes of disk space for the duration of the test.
Serhiy Storchakaf8282182016-02-25 12:55:19 +020083 with TemporaryFile() as f:
Ezio Melotti975077a2011-05-19 22:03:22 +030084 self.zipTest(f, zipfile.ZIP_DEFLATED)
Serhiy Storchakaf8282182016-02-25 12:55:19 +020085 self.assertFalse(f.closed)
86 self.zipTest(TESTFN2, zipfile.ZIP_DEFLATED)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000087
88 def tearDown(self):
89 for fname in TESTFN, TESTFN2:
90 if os.path.exists(fname):
91 os.remove(fname)
92
Martin v. Löwisb09b8442008-07-03 14:13:42 +000093
94class OtherTests(unittest.TestCase):
95 def testMoreThan64kFiles(self):
96 # This test checks that more than 64k files can be added to an archive,
97 # and that the resulting archive can be read properly by ZipFile
Serhiy Storchakacfbb3942014-09-23 21:34:24 +030098 zipf = zipfile.ZipFile(TESTFN, mode="w", allowZip64=True)
Martin v. Löwisb09b8442008-07-03 14:13:42 +000099 zipf.debug = 100
Amaury Forgeot d'Arc3be2f042008-11-12 01:57:36 +0000100 numfiles = (1 << 16) * 3//2
101 for i in range(numfiles):
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000102 zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57))
103 self.assertEqual(len(zipf.namelist()), numfiles)
104 zipf.close()
105
106 zipf2 = zipfile.ZipFile(TESTFN, mode="r")
107 self.assertEqual(len(zipf2.namelist()), numfiles)
Amaury Forgeot d'Arc3be2f042008-11-12 01:57:36 +0000108 for i in range(numfiles):
109 content = zipf2.read("foo%08d" % i).decode('ascii')
110 self.assertEqual(content, "%d" % (i**3 % 57))
Serhiy Storchakacfbb3942014-09-23 21:34:24 +0300111 zipf2.close()
112
113 def testMoreThan64kFilesAppend(self):
114 zipf = zipfile.ZipFile(TESTFN, mode="w", allowZip64=False)
115 zipf.debug = 100
116 numfiles = (1 << 16) - 1
117 for i in range(numfiles):
118 zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57))
119 self.assertEqual(len(zipf.namelist()), numfiles)
120 with self.assertRaises(zipfile.LargeZipFile):
121 zipf.writestr("foo%08d" % numfiles, b'')
122 self.assertEqual(len(zipf.namelist()), numfiles)
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000123 zipf.close()
124
Serhiy Storchakacfbb3942014-09-23 21:34:24 +0300125 zipf = zipfile.ZipFile(TESTFN, mode="a", allowZip64=False)
126 zipf.debug = 100
127 self.assertEqual(len(zipf.namelist()), numfiles)
128 with self.assertRaises(zipfile.LargeZipFile):
129 zipf.writestr("foo%08d" % numfiles, b'')
130 self.assertEqual(len(zipf.namelist()), numfiles)
131 zipf.close()
132
133 zipf = zipfile.ZipFile(TESTFN, mode="a", allowZip64=True)
134 zipf.debug = 100
135 self.assertEqual(len(zipf.namelist()), numfiles)
136 numfiles2 = (1 << 16) * 3//2
137 for i in range(numfiles, numfiles2):
138 zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57))
139 self.assertEqual(len(zipf.namelist()), numfiles2)
140 zipf.close()
141
142 zipf2 = zipfile.ZipFile(TESTFN, mode="r")
143 self.assertEqual(len(zipf2.namelist()), numfiles2)
144 for i in range(numfiles2):
145 content = zipf2.read("foo%08d" % i).decode('ascii')
146 self.assertEqual(content, "%d" % (i**3 % 57))
147 zipf2.close()
148
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000149 def tearDown(self):
Amaury Forgeot d'Arc3be2f042008-11-12 01:57:36 +0000150 support.unlink(TESTFN)
151 support.unlink(TESTFN2)
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000152
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000153if __name__ == "__main__":
Serhiy Storchakacfbb3942014-09-23 21:34:24 +0300154 unittest.main()