blob: 0947013afbc6ed117578e217b0dff22f2fe8ec87 [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
Hai Shic6f282f2020-08-08 19:05:24 +080020from test.support import os_helper
Nikita Soboleva6a08852022-01-20 04:44:21 +030021from test.support import requires_zlib
Thomas Wouters0e3f5912006-08-11 14:57:12 +000022
Nikita Soboleva6a08852022-01-20 04:44:21 +030023TESTFN = os_helper.TESTFN
Thomas Wouters0e3f5912006-08-11 14:57:12 +000024TESTFN2 = TESTFN + "2"
25
26# How much time in seconds can pass before we print a 'Still working' message.
Victor Stinner2cf4c202018-12-17 09:36:36 +010027_PRINT_WORKING_MSG_INTERVAL = 60
Thomas Wouters0e3f5912006-08-11 14:57:12 +000028
29class TestsWithSourceFile(unittest.TestCase):
30 def setUp(self):
31 # Create test data.
Guido van Rossum805365e2007-05-07 22:24:25 +000032 line_gen = ("Test of zipfile line %d." % i for i in range(1000000))
Amaury Forgeot d'Arc3be2f042008-11-12 01:57:36 +000033 self.data = '\n'.join(line_gen).encode('ascii')
Thomas Wouters0e3f5912006-08-11 14:57:12 +000034
35 # And write it to a file.
Serhiy Storchaka9e4861f2019-03-05 10:05:57 +020036 with open(TESTFN, "wb") as fp:
37 fp.write(self.data)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000038
39 def zipTest(self, f, compression):
40 # Create the ZIP archive.
Serhiy Storchaka9e4861f2019-03-05 10:05:57 +020041 with zipfile.ZipFile(f, "w", compression) as zipfp:
Thomas Wouters0e3f5912006-08-11 14:57:12 +000042
Serhiy Storchaka9e4861f2019-03-05 10:05:57 +020043 # It will contain enough copies of self.data to reach about 6 GiB of
44 # raw data to store.
45 filecount = 6*1024**3 // len(self.data)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000046
Serhiy Storchaka9e4861f2019-03-05 10:05:57 +020047 next_time = time.monotonic() + _PRINT_WORKING_MSG_INTERVAL
48 for num in range(filecount):
49 zipfp.writestr("testfn%d" % num, self.data)
50 # Print still working message since this test can be really slow
51 if next_time <= time.monotonic():
52 next_time = time.monotonic() + _PRINT_WORKING_MSG_INTERVAL
53 print((
54 ' zipTest still writing %d of %d, be patient...' %
55 (num, filecount)), file=sys.__stdout__)
56 sys.__stdout__.flush()
Thomas Wouters0e3f5912006-08-11 14:57:12 +000057
58 # Read the ZIP archive
Serhiy Storchaka9e4861f2019-03-05 10:05:57 +020059 with zipfile.ZipFile(f, "r", compression) as zipfp:
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.monotonic():
64 next_time = time.monotonic() + _PRINT_WORKING_MSG_INTERVAL
65 print((
66 ' zipTest still reading %d of %d, be patient...' %
67 (num, filecount)), file=sys.__stdout__)
68 sys.__stdout__.flush()
Thomas Wouters0e3f5912006-08-11 14:57:12 +000069
70 def testStored(self):
71 # Try the temp file first. If we do TESTFN2 first, then it hogs
72 # gigabytes of disk space for the duration of the test.
Serhiy Storchakaf8282182016-02-25 12:55:19 +020073 with TemporaryFile() as f:
Thomas Wouters0e3f5912006-08-11 14:57:12 +000074 self.zipTest(f, zipfile.ZIP_STORED)
Serhiy Storchakaf8282182016-02-25 12:55:19 +020075 self.assertFalse(f.closed)
76 self.zipTest(TESTFN2, zipfile.ZIP_STORED)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000077
Hai Shia3ec3ad2020-05-19 06:02:57 +080078 @requires_zlib()
Ezio Melotti975077a2011-05-19 22:03:22 +030079 def testDeflated(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.
Serhiy Storchakaf8282182016-02-25 12:55:19 +020082 with TemporaryFile() as f:
Ezio Melotti975077a2011-05-19 22:03:22 +030083 self.zipTest(f, zipfile.ZIP_DEFLATED)
Serhiy Storchakaf8282182016-02-25 12:55:19 +020084 self.assertFalse(f.closed)
85 self.zipTest(TESTFN2, zipfile.ZIP_DEFLATED)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000086
87 def tearDown(self):
88 for fname in TESTFN, TESTFN2:
89 if os.path.exists(fname):
90 os.remove(fname)
91
Martin v. Löwisb09b8442008-07-03 14:13:42 +000092
93class OtherTests(unittest.TestCase):
94 def testMoreThan64kFiles(self):
95 # This test checks that more than 64k files can be added to an archive,
96 # and that the resulting archive can be read properly by ZipFile
Serhiy Storchaka9e4861f2019-03-05 10:05:57 +020097 with zipfile.ZipFile(TESTFN, mode="w", allowZip64=True) as zipf:
98 zipf.debug = 100
99 numfiles = (1 << 16) * 3//2
100 for i in range(numfiles):
101 zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57))
102 self.assertEqual(len(zipf.namelist()), numfiles)
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000103
Serhiy Storchaka9e4861f2019-03-05 10:05:57 +0200104 with zipfile.ZipFile(TESTFN, mode="r") as zipf2:
105 self.assertEqual(len(zipf2.namelist()), numfiles)
106 for i in range(numfiles):
107 content = zipf2.read("foo%08d" % i).decode('ascii')
108 self.assertEqual(content, "%d" % (i**3 % 57))
Serhiy Storchakacfbb3942014-09-23 21:34:24 +0300109
110 def testMoreThan64kFilesAppend(self):
Serhiy Storchaka9e4861f2019-03-05 10:05:57 +0200111 with zipfile.ZipFile(TESTFN, mode="w", allowZip64=False) as zipf:
112 zipf.debug = 100
113 numfiles = (1 << 16) - 1
114 for i in range(numfiles):
115 zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57))
116 self.assertEqual(len(zipf.namelist()), numfiles)
117 with self.assertRaises(zipfile.LargeZipFile):
118 zipf.writestr("foo%08d" % numfiles, b'')
119 self.assertEqual(len(zipf.namelist()), numfiles)
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000120
Serhiy Storchaka9e4861f2019-03-05 10:05:57 +0200121 with zipfile.ZipFile(TESTFN, mode="a", allowZip64=False) as zipf:
122 zipf.debug = 100
123 self.assertEqual(len(zipf.namelist()), numfiles)
124 with self.assertRaises(zipfile.LargeZipFile):
125 zipf.writestr("foo%08d" % numfiles, b'')
126 self.assertEqual(len(zipf.namelist()), numfiles)
Serhiy Storchakacfbb3942014-09-23 21:34:24 +0300127
Serhiy Storchaka9e4861f2019-03-05 10:05:57 +0200128 with zipfile.ZipFile(TESTFN, mode="a", allowZip64=True) as zipf:
129 zipf.debug = 100
130 self.assertEqual(len(zipf.namelist()), numfiles)
131 numfiles2 = (1 << 16) * 3//2
132 for i in range(numfiles, numfiles2):
133 zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57))
134 self.assertEqual(len(zipf.namelist()), numfiles2)
Serhiy Storchakacfbb3942014-09-23 21:34:24 +0300135
Serhiy Storchaka9e4861f2019-03-05 10:05:57 +0200136 with zipfile.ZipFile(TESTFN, mode="r") as zipf2:
137 self.assertEqual(len(zipf2.namelist()), numfiles2)
138 for i in range(numfiles2):
139 content = zipf2.read("foo%08d" % i).decode('ascii')
140 self.assertEqual(content, "%d" % (i**3 % 57))
Serhiy Storchakacfbb3942014-09-23 21:34:24 +0300141
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000142 def tearDown(self):
Hai Shic6f282f2020-08-08 19:05:24 +0800143 os_helper.unlink(TESTFN)
144 os_helper.unlink(TESTFN2)
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000145
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000146if __name__ == "__main__":
Serhiy Storchakacfbb3942014-09-23 21:34:24 +0300147 unittest.main()