blob: 7dea8a32120a629fc21de34c505f651f0df699ce [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
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006# XXX(nnorwitz): disable this test by looking for extra largfile resource
7# 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
Guido van Rossum34d19282007-08-09 01:03:29 +000018from io import StringIO
Thomas Wouters0e3f5912006-08-11 14:57:12 +000019from tempfile import TemporaryFile
20
Serhiy Storchakacfbb3942014-09-23 21:34:24 +030021from test.support import TESTFN, requires_zlib
Thomas Wouters0e3f5912006-08-11 14:57:12 +000022
23TESTFN2 = TESTFN + "2"
24
25# How much time in seconds can pass before we print a 'Still working' message.
26_PRINT_WORKING_MSG_INTERVAL = 5 * 60
27
28class TestsWithSourceFile(unittest.TestCase):
29 def setUp(self):
30 # Create test data.
Guido van Rossum805365e2007-05-07 22:24:25 +000031 line_gen = ("Test of zipfile line %d." % i for i in range(1000000))
Amaury Forgeot d'Arc3be2f042008-11-12 01:57:36 +000032 self.data = '\n'.join(line_gen).encode('ascii')
Thomas Wouters0e3f5912006-08-11 14:57:12 +000033
34 # And write it to a file.
35 fp = open(TESTFN, "wb")
36 fp.write(self.data)
37 fp.close()
38
39 def zipTest(self, f, compression):
40 # Create the ZIP archive.
Serhiy Storchaka235c5e02013-11-23 15:55:38 +020041 zipfp = zipfile.ZipFile(f, "w", compression)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000042
43 # It will contain enough copies of self.data to reach about 6GB of
44 # raw data to store.
45 filecount = 6*1024**3 // len(self.data)
46
47 next_time = time.time() + _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.time():
52 next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL
Guido van Rossumbe19ed72007-02-09 05:37:30 +000053 print((
Thomas Wouters0e3f5912006-08-11 14:57:12 +000054 ' zipTest still writing %d of %d, be patient...' %
Guido van Rossumbe19ed72007-02-09 05:37:30 +000055 (num, filecount)), file=sys.__stdout__)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000056 sys.__stdout__.flush()
57 zipfp.close()
58
59 # Read the ZIP archive
60 zipfp = zipfile.ZipFile(f, "r", compression)
61 for num in range(filecount):
62 self.assertEqual(zipfp.read("testfn%d" % num), self.data)
63 # Print still working message since this test can be really slow
64 if next_time <= time.time():
65 next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL
Guido van Rossumbe19ed72007-02-09 05:37:30 +000066 print((
Thomas Wouters0e3f5912006-08-11 14:57:12 +000067 ' zipTest still reading %d of %d, be patient...' %
Guido van Rossumbe19ed72007-02-09 05:37:30 +000068 (num, filecount)), file=sys.__stdout__)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000069 sys.__stdout__.flush()
70 zipfp.close()
71
72 def testStored(self):
73 # Try the temp file first. If we do TESTFN2 first, then it hogs
74 # gigabytes of disk space for the duration of the test.
75 for f in TemporaryFile(), TESTFN2:
76 self.zipTest(f, zipfile.ZIP_STORED)
77
Ezio Melotti975077a2011-05-19 22:03:22 +030078 @requires_zlib
79 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.
82 for f in TemporaryFile(), TESTFN2:
83 self.zipTest(f, 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 Storchakacfbb3942014-09-23 21:34:24 +030095 zipf = zipfile.ZipFile(TESTFN, mode="w", allowZip64=True)
Martin v. Löwisb09b8442008-07-03 14:13:42 +000096 zipf.debug = 100
Amaury Forgeot d'Arc3be2f042008-11-12 01:57:36 +000097 numfiles = (1 << 16) * 3//2
98 for i in range(numfiles):
Martin v. Löwisb09b8442008-07-03 14:13:42 +000099 zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57))
100 self.assertEqual(len(zipf.namelist()), numfiles)
101 zipf.close()
102
103 zipf2 = zipfile.ZipFile(TESTFN, mode="r")
104 self.assertEqual(len(zipf2.namelist()), numfiles)
Amaury Forgeot d'Arc3be2f042008-11-12 01:57:36 +0000105 for i in range(numfiles):
106 content = zipf2.read("foo%08d" % i).decode('ascii')
107 self.assertEqual(content, "%d" % (i**3 % 57))
Serhiy Storchakacfbb3942014-09-23 21:34:24 +0300108 zipf2.close()
109
110 def testMoreThan64kFilesAppend(self):
111 zipf = zipfile.ZipFile(TESTFN, mode="w", allowZip64=False)
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 zipf.close()
121
Serhiy Storchakacfbb3942014-09-23 21:34:24 +0300122 zipf = zipfile.ZipFile(TESTFN, mode="a", allowZip64=False)
123 zipf.debug = 100
124 self.assertEqual(len(zipf.namelist()), numfiles)
125 with self.assertRaises(zipfile.LargeZipFile):
126 zipf.writestr("foo%08d" % numfiles, b'')
127 self.assertEqual(len(zipf.namelist()), numfiles)
128 zipf.close()
129
130 zipf = zipfile.ZipFile(TESTFN, mode="a", allowZip64=True)
131 zipf.debug = 100
132 self.assertEqual(len(zipf.namelist()), numfiles)
133 numfiles2 = (1 << 16) * 3//2
134 for i in range(numfiles, numfiles2):
135 zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57))
136 self.assertEqual(len(zipf.namelist()), numfiles2)
137 zipf.close()
138
139 zipf2 = zipfile.ZipFile(TESTFN, mode="r")
140 self.assertEqual(len(zipf2.namelist()), numfiles2)
141 for i in range(numfiles2):
142 content = zipf2.read("foo%08d" % i).decode('ascii')
143 self.assertEqual(content, "%d" % (i**3 % 57))
144 zipf2.close()
145
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000146 def tearDown(self):
Amaury Forgeot d'Arc3be2f042008-11-12 01:57:36 +0000147 support.unlink(TESTFN)
148 support.unlink(TESTFN2)
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000149
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000150if __name__ == "__main__":
Serhiy Storchakacfbb3942014-09-23 21:34:24 +0300151 unittest.main()