blob: 56746bc08f63d878f4ef9242621d45f204051c78 [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.
Victor Stinner2cf4c202018-12-17 09:36:36 +010025_PRINT_WORKING_MSG_INTERVAL = 60
Thomas Wouters0e3f5912006-08-11 14:57:12 +000026
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.
Serhiy Storchaka9e4861f2019-03-05 10:05:57 +020034 with open(TESTFN, "wb") as fp:
35 fp.write(self.data)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000036
37 def zipTest(self, f, compression):
38 # Create the ZIP archive.
Serhiy Storchaka9e4861f2019-03-05 10:05:57 +020039 with zipfile.ZipFile(f, "w", compression) as zipfp:
Thomas Wouters0e3f5912006-08-11 14:57:12 +000040
Serhiy Storchaka9e4861f2019-03-05 10:05:57 +020041 # It will contain enough copies of self.data to reach about 6 GiB of
42 # raw data to store.
43 filecount = 6*1024**3 // len(self.data)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000044
Serhiy Storchaka9e4861f2019-03-05 10:05:57 +020045 next_time = time.monotonic() + _PRINT_WORKING_MSG_INTERVAL
46 for num in range(filecount):
47 zipfp.writestr("testfn%d" % num, self.data)
48 # Print still working message since this test can be really slow
49 if next_time <= time.monotonic():
50 next_time = time.monotonic() + _PRINT_WORKING_MSG_INTERVAL
51 print((
52 ' zipTest still writing %d of %d, be patient...' %
53 (num, filecount)), file=sys.__stdout__)
54 sys.__stdout__.flush()
Thomas Wouters0e3f5912006-08-11 14:57:12 +000055
56 # Read the ZIP archive
Serhiy Storchaka9e4861f2019-03-05 10:05:57 +020057 with zipfile.ZipFile(f, "r", compression) as zipfp:
58 for num in range(filecount):
59 self.assertEqual(zipfp.read("testfn%d" % num), self.data)
60 # Print still working message since this test can be really slow
61 if next_time <= time.monotonic():
62 next_time = time.monotonic() + _PRINT_WORKING_MSG_INTERVAL
63 print((
64 ' zipTest still reading %d of %d, be patient...' %
65 (num, filecount)), file=sys.__stdout__)
66 sys.__stdout__.flush()
Thomas Wouters0e3f5912006-08-11 14:57:12 +000067
68 def testStored(self):
69 # Try the temp file first. If we do TESTFN2 first, then it hogs
70 # gigabytes of disk space for the duration of the test.
Serhiy Storchakaf8282182016-02-25 12:55:19 +020071 with TemporaryFile() as f:
Thomas Wouters0e3f5912006-08-11 14:57:12 +000072 self.zipTest(f, zipfile.ZIP_STORED)
Serhiy Storchakaf8282182016-02-25 12:55:19 +020073 self.assertFalse(f.closed)
74 self.zipTest(TESTFN2, zipfile.ZIP_STORED)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000075
Ezio Melotti975077a2011-05-19 22:03:22 +030076 @requires_zlib
77 def testDeflated(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.
Serhiy Storchakaf8282182016-02-25 12:55:19 +020080 with TemporaryFile() as f:
Ezio Melotti975077a2011-05-19 22:03:22 +030081 self.zipTest(f, zipfile.ZIP_DEFLATED)
Serhiy Storchakaf8282182016-02-25 12:55:19 +020082 self.assertFalse(f.closed)
83 self.zipTest(TESTFN2, zipfile.ZIP_DEFLATED)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000084
85 def tearDown(self):
86 for fname in TESTFN, TESTFN2:
87 if os.path.exists(fname):
88 os.remove(fname)
89
Martin v. Löwisb09b8442008-07-03 14:13:42 +000090
91class OtherTests(unittest.TestCase):
92 def testMoreThan64kFiles(self):
93 # This test checks that more than 64k files can be added to an archive,
94 # and that the resulting archive can be read properly by ZipFile
Serhiy Storchaka9e4861f2019-03-05 10:05:57 +020095 with zipfile.ZipFile(TESTFN, mode="w", allowZip64=True) as zipf:
96 zipf.debug = 100
97 numfiles = (1 << 16) * 3//2
98 for i in range(numfiles):
99 zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57))
100 self.assertEqual(len(zipf.namelist()), numfiles)
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000101
Serhiy Storchaka9e4861f2019-03-05 10:05:57 +0200102 with zipfile.ZipFile(TESTFN, mode="r") as zipf2:
103 self.assertEqual(len(zipf2.namelist()), numfiles)
104 for i in range(numfiles):
105 content = zipf2.read("foo%08d" % i).decode('ascii')
106 self.assertEqual(content, "%d" % (i**3 % 57))
Serhiy Storchakacfbb3942014-09-23 21:34:24 +0300107
108 def testMoreThan64kFilesAppend(self):
Serhiy Storchaka9e4861f2019-03-05 10:05:57 +0200109 with zipfile.ZipFile(TESTFN, mode="w", allowZip64=False) as zipf:
110 zipf.debug = 100
111 numfiles = (1 << 16) - 1
112 for i in range(numfiles):
113 zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57))
114 self.assertEqual(len(zipf.namelist()), numfiles)
115 with self.assertRaises(zipfile.LargeZipFile):
116 zipf.writestr("foo%08d" % numfiles, b'')
117 self.assertEqual(len(zipf.namelist()), numfiles)
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000118
Serhiy Storchaka9e4861f2019-03-05 10:05:57 +0200119 with zipfile.ZipFile(TESTFN, mode="a", allowZip64=False) as zipf:
120 zipf.debug = 100
121 self.assertEqual(len(zipf.namelist()), numfiles)
122 with self.assertRaises(zipfile.LargeZipFile):
123 zipf.writestr("foo%08d" % numfiles, b'')
124 self.assertEqual(len(zipf.namelist()), numfiles)
Serhiy Storchakacfbb3942014-09-23 21:34:24 +0300125
Serhiy Storchaka9e4861f2019-03-05 10:05:57 +0200126 with zipfile.ZipFile(TESTFN, mode="a", allowZip64=True) as zipf:
127 zipf.debug = 100
128 self.assertEqual(len(zipf.namelist()), numfiles)
129 numfiles2 = (1 << 16) * 3//2
130 for i in range(numfiles, numfiles2):
131 zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57))
132 self.assertEqual(len(zipf.namelist()), numfiles2)
Serhiy Storchakacfbb3942014-09-23 21:34:24 +0300133
Serhiy Storchaka9e4861f2019-03-05 10:05:57 +0200134 with zipfile.ZipFile(TESTFN, mode="r") as zipf2:
135 self.assertEqual(len(zipf2.namelist()), numfiles2)
136 for i in range(numfiles2):
137 content = zipf2.read("foo%08d" % i).decode('ascii')
138 self.assertEqual(content, "%d" % (i**3 % 57))
Serhiy Storchakacfbb3942014-09-23 21:34:24 +0300139
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000140 def tearDown(self):
Amaury Forgeot d'Arc3be2f042008-11-12 01:57:36 +0000141 support.unlink(TESTFN)
142 support.unlink(TESTFN2)
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000143
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000144if __name__ == "__main__":
Serhiy Storchakacfbb3942014-09-23 21:34:24 +0300145 unittest.main()