1 | n/a | # Tests of the full ZIP64 functionality of zipfile |
---|
2 | n/a | # The support.requires call is the only reason for keeping this separate |
---|
3 | n/a | # from test_zipfile |
---|
4 | n/a | from test import support |
---|
5 | n/a | |
---|
6 | n/a | # XXX(nnorwitz): disable this test by looking for extralargefile resource, |
---|
7 | n/a | # which doesn't exist. This test takes over 30 minutes to run in general |
---|
8 | n/a | # and requires more disk space than most of the buildbots. |
---|
9 | n/a | support.requires( |
---|
10 | n/a | 'extralargefile', |
---|
11 | n/a | 'test requires loads of disk-space bytes and a long time to run' |
---|
12 | n/a | ) |
---|
13 | n/a | |
---|
14 | n/a | import zipfile, os, unittest |
---|
15 | n/a | import time |
---|
16 | n/a | import sys |
---|
17 | n/a | |
---|
18 | n/a | from io import StringIO |
---|
19 | n/a | from tempfile import TemporaryFile |
---|
20 | n/a | |
---|
21 | n/a | from test.support import TESTFN, requires_zlib |
---|
22 | n/a | |
---|
23 | n/a | TESTFN2 = TESTFN + "2" |
---|
24 | n/a | |
---|
25 | n/a | # How much time in seconds can pass before we print a 'Still working' message. |
---|
26 | n/a | _PRINT_WORKING_MSG_INTERVAL = 5 * 60 |
---|
27 | n/a | |
---|
28 | n/a | class TestsWithSourceFile(unittest.TestCase): |
---|
29 | n/a | def setUp(self): |
---|
30 | n/a | # Create test data. |
---|
31 | n/a | line_gen = ("Test of zipfile line %d." % i for i in range(1000000)) |
---|
32 | n/a | self.data = '\n'.join(line_gen).encode('ascii') |
---|
33 | n/a | |
---|
34 | n/a | # And write it to a file. |
---|
35 | n/a | fp = open(TESTFN, "wb") |
---|
36 | n/a | fp.write(self.data) |
---|
37 | n/a | fp.close() |
---|
38 | n/a | |
---|
39 | n/a | def zipTest(self, f, compression): |
---|
40 | n/a | # Create the ZIP archive. |
---|
41 | n/a | zipfp = zipfile.ZipFile(f, "w", compression) |
---|
42 | n/a | |
---|
43 | n/a | # It will contain enough copies of self.data to reach about 6GB of |
---|
44 | n/a | # raw data to store. |
---|
45 | n/a | filecount = 6*1024**3 // len(self.data) |
---|
46 | n/a | |
---|
47 | n/a | next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL |
---|
48 | n/a | for num in range(filecount): |
---|
49 | n/a | zipfp.writestr("testfn%d" % num, self.data) |
---|
50 | n/a | # Print still working message since this test can be really slow |
---|
51 | n/a | if next_time <= time.time(): |
---|
52 | n/a | next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL |
---|
53 | n/a | print(( |
---|
54 | n/a | ' zipTest still writing %d of %d, be patient...' % |
---|
55 | n/a | (num, filecount)), file=sys.__stdout__) |
---|
56 | n/a | sys.__stdout__.flush() |
---|
57 | n/a | zipfp.close() |
---|
58 | n/a | |
---|
59 | n/a | # Read the ZIP archive |
---|
60 | n/a | zipfp = zipfile.ZipFile(f, "r", compression) |
---|
61 | n/a | for num in range(filecount): |
---|
62 | n/a | self.assertEqual(zipfp.read("testfn%d" % num), self.data) |
---|
63 | n/a | # Print still working message since this test can be really slow |
---|
64 | n/a | if next_time <= time.time(): |
---|
65 | n/a | next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL |
---|
66 | n/a | print(( |
---|
67 | n/a | ' zipTest still reading %d of %d, be patient...' % |
---|
68 | n/a | (num, filecount)), file=sys.__stdout__) |
---|
69 | n/a | sys.__stdout__.flush() |
---|
70 | n/a | zipfp.close() |
---|
71 | n/a | |
---|
72 | n/a | def testStored(self): |
---|
73 | n/a | # Try the temp file first. If we do TESTFN2 first, then it hogs |
---|
74 | n/a | # gigabytes of disk space for the duration of the test. |
---|
75 | n/a | with TemporaryFile() as f: |
---|
76 | n/a | self.zipTest(f, zipfile.ZIP_STORED) |
---|
77 | n/a | self.assertFalse(f.closed) |
---|
78 | n/a | self.zipTest(TESTFN2, zipfile.ZIP_STORED) |
---|
79 | n/a | |
---|
80 | n/a | @requires_zlib |
---|
81 | n/a | def testDeflated(self): |
---|
82 | n/a | # Try the temp file first. If we do TESTFN2 first, then it hogs |
---|
83 | n/a | # gigabytes of disk space for the duration of the test. |
---|
84 | n/a | with TemporaryFile() as f: |
---|
85 | n/a | self.zipTest(f, zipfile.ZIP_DEFLATED) |
---|
86 | n/a | self.assertFalse(f.closed) |
---|
87 | n/a | self.zipTest(TESTFN2, zipfile.ZIP_DEFLATED) |
---|
88 | n/a | |
---|
89 | n/a | def tearDown(self): |
---|
90 | n/a | for fname in TESTFN, TESTFN2: |
---|
91 | n/a | if os.path.exists(fname): |
---|
92 | n/a | os.remove(fname) |
---|
93 | n/a | |
---|
94 | n/a | |
---|
95 | n/a | class OtherTests(unittest.TestCase): |
---|
96 | n/a | def testMoreThan64kFiles(self): |
---|
97 | n/a | # This test checks that more than 64k files can be added to an archive, |
---|
98 | n/a | # and that the resulting archive can be read properly by ZipFile |
---|
99 | n/a | zipf = zipfile.ZipFile(TESTFN, mode="w", allowZip64=True) |
---|
100 | n/a | zipf.debug = 100 |
---|
101 | n/a | numfiles = (1 << 16) * 3//2 |
---|
102 | n/a | for i in range(numfiles): |
---|
103 | n/a | zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57)) |
---|
104 | n/a | self.assertEqual(len(zipf.namelist()), numfiles) |
---|
105 | n/a | zipf.close() |
---|
106 | n/a | |
---|
107 | n/a | zipf2 = zipfile.ZipFile(TESTFN, mode="r") |
---|
108 | n/a | self.assertEqual(len(zipf2.namelist()), numfiles) |
---|
109 | n/a | for i in range(numfiles): |
---|
110 | n/a | content = zipf2.read("foo%08d" % i).decode('ascii') |
---|
111 | n/a | self.assertEqual(content, "%d" % (i**3 % 57)) |
---|
112 | n/a | zipf2.close() |
---|
113 | n/a | |
---|
114 | n/a | def testMoreThan64kFilesAppend(self): |
---|
115 | n/a | zipf = zipfile.ZipFile(TESTFN, mode="w", allowZip64=False) |
---|
116 | n/a | zipf.debug = 100 |
---|
117 | n/a | numfiles = (1 << 16) - 1 |
---|
118 | n/a | for i in range(numfiles): |
---|
119 | n/a | zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57)) |
---|
120 | n/a | self.assertEqual(len(zipf.namelist()), numfiles) |
---|
121 | n/a | with self.assertRaises(zipfile.LargeZipFile): |
---|
122 | n/a | zipf.writestr("foo%08d" % numfiles, b'') |
---|
123 | n/a | self.assertEqual(len(zipf.namelist()), numfiles) |
---|
124 | n/a | zipf.close() |
---|
125 | n/a | |
---|
126 | n/a | zipf = zipfile.ZipFile(TESTFN, mode="a", allowZip64=False) |
---|
127 | n/a | zipf.debug = 100 |
---|
128 | n/a | self.assertEqual(len(zipf.namelist()), numfiles) |
---|
129 | n/a | with self.assertRaises(zipfile.LargeZipFile): |
---|
130 | n/a | zipf.writestr("foo%08d" % numfiles, b'') |
---|
131 | n/a | self.assertEqual(len(zipf.namelist()), numfiles) |
---|
132 | n/a | zipf.close() |
---|
133 | n/a | |
---|
134 | n/a | zipf = zipfile.ZipFile(TESTFN, mode="a", allowZip64=True) |
---|
135 | n/a | zipf.debug = 100 |
---|
136 | n/a | self.assertEqual(len(zipf.namelist()), numfiles) |
---|
137 | n/a | numfiles2 = (1 << 16) * 3//2 |
---|
138 | n/a | for i in range(numfiles, numfiles2): |
---|
139 | n/a | zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57)) |
---|
140 | n/a | self.assertEqual(len(zipf.namelist()), numfiles2) |
---|
141 | n/a | zipf.close() |
---|
142 | n/a | |
---|
143 | n/a | zipf2 = zipfile.ZipFile(TESTFN, mode="r") |
---|
144 | n/a | self.assertEqual(len(zipf2.namelist()), numfiles2) |
---|
145 | n/a | for i in range(numfiles2): |
---|
146 | n/a | content = zipf2.read("foo%08d" % i).decode('ascii') |
---|
147 | n/a | self.assertEqual(content, "%d" % (i**3 % 57)) |
---|
148 | n/a | zipf2.close() |
---|
149 | n/a | |
---|
150 | n/a | def tearDown(self): |
---|
151 | n/a | support.unlink(TESTFN) |
---|
152 | n/a | support.unlink(TESTFN2) |
---|
153 | n/a | |
---|
154 | n/a | if __name__ == "__main__": |
---|
155 | n/a | unittest.main() |
---|