ยปCore Development>Code coverage>Lib/test/test_zipfile64.py

Python code coverage for Lib/test/test_zipfile64.py

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