blob: 151baf2302349ab22ebc51f7d11bdb112a37e855 [file] [log] [blame]
Ronald Oussoren143cefb2006-06-15 08:14:18 +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
Martin v. Löwis8c436412008-07-03 12:51:14 +00005
Neal Norwitz217046f2006-06-16 04:30:34 +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.
Ronald Oussoren143cefb2006-06-15 08:14:18 +00009test_support.requires(
Neal Norwitz217046f2006-06-16 04:30:34 +000010 'extralargefile',
Ronald Oussoren143cefb2006-06-15 08:14:18 +000011 'test requires loads of disk-space bytes and a long time to run'
12 )
13
14# We can test part of the module without zlib.
15try:
16 import zlib
17except ImportError:
18 zlib = None
19
20import zipfile, os, unittest
Neal Norwitz643ad192006-06-15 09:57:03 +000021import time
22import sys
Ronald Oussoren143cefb2006-06-15 08:14:18 +000023
Ronald Oussoren143cefb2006-06-15 08:14:18 +000024from tempfile import TemporaryFile
25
26from test.test_support import TESTFN, run_unittest
27
28TESTFN2 = TESTFN + "2"
29
Neal Norwitz643ad192006-06-15 09:57:03 +000030# How much time in seconds can pass before we print a 'Still working' message.
31_PRINT_WORKING_MSG_INTERVAL = 5 * 60
32
Ronald Oussoren143cefb2006-06-15 08:14:18 +000033class TestsWithSourceFile(unittest.TestCase):
34 def setUp(self):
Tim Peters84b0f582006-06-15 18:04:40 +000035 # Create test data.
36 # xrange() is important here -- don't want to create immortal space
37 # for a million ints.
38 line_gen = ("Test of zipfile line %d." % i for i in xrange(1000000))
Ronald Oussoren143cefb2006-06-15 08:14:18 +000039 self.data = '\n'.join(line_gen)
40
Tim Peters84b0f582006-06-15 18:04:40 +000041 # And write it to a file.
Ronald Oussoren143cefb2006-06-15 08:14:18 +000042 fp = open(TESTFN, "wb")
43 fp.write(self.data)
44 fp.close()
45
46 def zipTest(self, f, compression):
Tim Peters84b0f582006-06-15 18:04:40 +000047 # Create the ZIP archive.
Ronald Oussoren143cefb2006-06-15 08:14:18 +000048 zipfp = zipfile.ZipFile(f, "w", compression, allowZip64=True)
49
Tim Peters84b0f582006-06-15 18:04:40 +000050 # It will contain enough copies of self.data to reach about 6GB of
51 # raw data to store.
Tim Petersda4b84a2006-06-15 18:38:19 +000052 filecount = 6*1024**3 // len(self.data)
Tim Peters84b0f582006-06-15 18:04:40 +000053
Neal Norwitz643ad192006-06-15 09:57:03 +000054 next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL
Ronald Oussoren143cefb2006-06-15 08:14:18 +000055 for num in range(filecount):
Tim Peters84b0f582006-06-15 18:04:40 +000056 zipfp.writestr("testfn%d" % num, self.data)
Neal Norwitz643ad192006-06-15 09:57:03 +000057 # Print still working message since this test can be really slow
58 if next_time <= time.time():
59 next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL
Neal Norwitzbda14182006-06-15 10:24:49 +000060 print >>sys.__stdout__, (
61 ' zipTest still writing %d of %d, be patient...' %
62 (num, filecount))
Neal Norwitz643ad192006-06-15 09:57:03 +000063 sys.__stdout__.flush()
Ronald Oussoren143cefb2006-06-15 08:14:18 +000064 zipfp.close()
65
66 # Read the ZIP archive
67 zipfp = zipfile.ZipFile(f, "r", compression)
68 for num in range(filecount):
Tim Peters84b0f582006-06-15 18:04:40 +000069 self.assertEqual(zipfp.read("testfn%d" % num), self.data)
Neal Norwitz643ad192006-06-15 09:57:03 +000070 # Print still working message since this test can be really slow
71 if next_time <= time.time():
72 next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL
Neal Norwitzbda14182006-06-15 10:24:49 +000073 print >>sys.__stdout__, (
74 ' zipTest still reading %d of %d, be patient...' %
75 (num, filecount))
Neal Norwitz643ad192006-06-15 09:57:03 +000076 sys.__stdout__.flush()
Ronald Oussoren143cefb2006-06-15 08:14:18 +000077 zipfp.close()
78
79 def testStored(self):
Tim Peters84b0f582006-06-15 18:04:40 +000080 # 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 Storchakaee7fe382016-02-25 12:55:19 +020082 with TemporaryFile() as f:
Ronald Oussoren143cefb2006-06-15 08:14:18 +000083 self.zipTest(f, zipfile.ZIP_STORED)
Serhiy Storchakaee7fe382016-02-25 12:55:19 +020084 self.assertFalse(f.closed)
85 self.zipTest(TESTFN2, zipfile.ZIP_STORED)
Ronald Oussoren143cefb2006-06-15 08:14:18 +000086
Serhiy Storchakaee7fe382016-02-25 12:55:19 +020087 @unittest.skipUnless(zlib, "requires zlib")
88 def testDeflated(self):
89 # Try the temp file first. If we do TESTFN2 first, then it hogs
90 # gigabytes of disk space for the duration of the test.
91 with TemporaryFile() as f:
92 self.zipTest(f, zipfile.ZIP_DEFLATED)
93 self.assertFalse(f.closed)
94 self.zipTest(TESTFN2, zipfile.ZIP_DEFLATED)
Ronald Oussoren143cefb2006-06-15 08:14:18 +000095
96 def tearDown(self):
Tim Peters84b0f582006-06-15 18:04:40 +000097 for fname in TESTFN, TESTFN2:
98 if os.path.exists(fname):
99 os.remove(fname)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000100
Martin v. Löwis8c436412008-07-03 12:51:14 +0000101
102class OtherTests(unittest.TestCase):
103 def testMoreThan64kFiles(self):
104 # This test checks that more than 64k files can be added to an archive,
105 # and that the resulting archive can be read properly by ZipFile
Serhiy Storchaka45efb222014-09-23 21:33:52 +0300106 zipf = zipfile.ZipFile(TESTFN, mode="w", allowZip64=True)
Martin v. Löwis8c436412008-07-03 12:51:14 +0000107 zipf.debug = 100
108 numfiles = (1 << 16) * 3/2
109 for i in xrange(numfiles):
110 zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57))
111 self.assertEqual(len(zipf.namelist()), numfiles)
112 zipf.close()
113
114 zipf2 = zipfile.ZipFile(TESTFN, mode="r")
115 self.assertEqual(len(zipf2.namelist()), numfiles)
116 for i in xrange(numfiles):
117 self.assertEqual(zipf2.read("foo%08d" % i), "%d" % (i**3 % 57))
Serhiy Storchaka45efb222014-09-23 21:33:52 +0300118 zipf2.close()
119
120 def testMoreThan64kFilesAppend(self):
121 zipf = zipfile.ZipFile(TESTFN, mode="w", allowZip64=False)
122 zipf.debug = 100
123 numfiles = (1 << 16) - 1
124 for i in range(numfiles):
125 zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57))
126 self.assertEqual(len(zipf.namelist()), numfiles)
127 with self.assertRaises(zipfile.LargeZipFile):
128 zipf.writestr("foo%08d" % numfiles, b'')
129 self.assertEqual(len(zipf.namelist()), numfiles)
Martin v. Löwis8c436412008-07-03 12:51:14 +0000130 zipf.close()
131
Serhiy Storchaka45efb222014-09-23 21:33:52 +0300132 zipf = zipfile.ZipFile(TESTFN, mode="a", allowZip64=False)
133 zipf.debug = 100
134 self.assertEqual(len(zipf.namelist()), numfiles)
135 with self.assertRaises(zipfile.LargeZipFile):
136 zipf.writestr("foo%08d" % numfiles, b'')
137 self.assertEqual(len(zipf.namelist()), numfiles)
138 zipf.close()
139
140 zipf = zipfile.ZipFile(TESTFN, mode="a", allowZip64=True)
141 zipf.debug = 100
142 self.assertEqual(len(zipf.namelist()), numfiles)
143 numfiles2 = (1 << 16) * 3//2
144 for i in range(numfiles, numfiles2):
145 zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57))
146 self.assertEqual(len(zipf.namelist()), numfiles2)
147 zipf.close()
148
149 zipf2 = zipfile.ZipFile(TESTFN, mode="r")
150 self.assertEqual(len(zipf2.namelist()), numfiles2)
151 for i in range(numfiles2):
152 self.assertEqual(zipf2.read("foo%08d" % i), "%d" % (i**3 % 57))
153 zipf2.close()
154
Martin v. Löwis8c436412008-07-03 12:51:14 +0000155 def tearDown(self):
156 test_support.unlink(TESTFN)
157 test_support.unlink(TESTFN2)
158
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000159def test_main():
Martin v. Löwis8c436412008-07-03 12:51:14 +0000160 run_unittest(TestsWithSourceFile, OtherTests)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000161
162if __name__ == "__main__":
163 test_main()