Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1 | # Tests of the full ZIP64 functionality of zipfile |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 2 | # The support.requires call is the only reason for keeping this separate |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3 | # from test_zipfile |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 4 | from test import support |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 5 | |
Martin Panter | 6f9b010 | 2015-12-17 10:18:28 +0000 | [diff] [blame] | 6 | # XXX(nnorwitz): disable this test by looking for extralargefile resource, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 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 Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 9 | support.requires( |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 10 | 'extralargefile', |
| 11 | 'test requires loads of disk-space bytes and a long time to run' |
| 12 | ) |
| 13 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14 | import zipfile, os, unittest |
| 15 | import time |
| 16 | import sys |
| 17 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 18 | from tempfile import TemporaryFile |
| 19 | |
Hai Shi | c6f282f | 2020-08-08 19:05:24 +0800 | [diff] [blame] | 20 | from test.support import os_helper |
Serhiy Storchaka | cfbb394 | 2014-09-23 21:34:24 +0300 | [diff] [blame] | 21 | from test.support import TESTFN, requires_zlib |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 22 | |
| 23 | TESTFN2 = TESTFN + "2" |
| 24 | |
| 25 | # How much time in seconds can pass before we print a 'Still working' message. |
Victor Stinner | 2cf4c20 | 2018-12-17 09:36:36 +0100 | [diff] [blame] | 26 | _PRINT_WORKING_MSG_INTERVAL = 60 |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 27 | |
| 28 | class TestsWithSourceFile(unittest.TestCase): |
| 29 | def setUp(self): |
| 30 | # Create test data. |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 31 | line_gen = ("Test of zipfile line %d." % i for i in range(1000000)) |
Amaury Forgeot d'Arc | 3be2f04 | 2008-11-12 01:57:36 +0000 | [diff] [blame] | 32 | self.data = '\n'.join(line_gen).encode('ascii') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 33 | |
| 34 | # And write it to a file. |
Serhiy Storchaka | 9e4861f | 2019-03-05 10:05:57 +0200 | [diff] [blame] | 35 | with open(TESTFN, "wb") as fp: |
| 36 | fp.write(self.data) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 37 | |
| 38 | def zipTest(self, f, compression): |
| 39 | # Create the ZIP archive. |
Serhiy Storchaka | 9e4861f | 2019-03-05 10:05:57 +0200 | [diff] [blame] | 40 | with zipfile.ZipFile(f, "w", compression) as zipfp: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 41 | |
Serhiy Storchaka | 9e4861f | 2019-03-05 10:05:57 +0200 | [diff] [blame] | 42 | # It will contain enough copies of self.data to reach about 6 GiB of |
| 43 | # raw data to store. |
| 44 | filecount = 6*1024**3 // len(self.data) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 45 | |
Serhiy Storchaka | 9e4861f | 2019-03-05 10:05:57 +0200 | [diff] [blame] | 46 | next_time = time.monotonic() + _PRINT_WORKING_MSG_INTERVAL |
| 47 | for num in range(filecount): |
| 48 | zipfp.writestr("testfn%d" % num, self.data) |
| 49 | # Print still working message since this test can be really slow |
| 50 | if next_time <= time.monotonic(): |
| 51 | next_time = time.monotonic() + _PRINT_WORKING_MSG_INTERVAL |
| 52 | print(( |
| 53 | ' zipTest still writing %d of %d, be patient...' % |
| 54 | (num, filecount)), file=sys.__stdout__) |
| 55 | sys.__stdout__.flush() |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 56 | |
| 57 | # Read the ZIP archive |
Serhiy Storchaka | 9e4861f | 2019-03-05 10:05:57 +0200 | [diff] [blame] | 58 | with zipfile.ZipFile(f, "r", compression) as zipfp: |
| 59 | for num in range(filecount): |
| 60 | self.assertEqual(zipfp.read("testfn%d" % num), self.data) |
| 61 | # Print still working message since this test can be really slow |
| 62 | if next_time <= time.monotonic(): |
| 63 | next_time = time.monotonic() + _PRINT_WORKING_MSG_INTERVAL |
| 64 | print(( |
| 65 | ' zipTest still reading %d of %d, be patient...' % |
| 66 | (num, filecount)), file=sys.__stdout__) |
| 67 | sys.__stdout__.flush() |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 68 | |
| 69 | def testStored(self): |
| 70 | # Try the temp file first. If we do TESTFN2 first, then it hogs |
| 71 | # gigabytes of disk space for the duration of the test. |
Serhiy Storchaka | f828218 | 2016-02-25 12:55:19 +0200 | [diff] [blame] | 72 | with TemporaryFile() as f: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 73 | self.zipTest(f, zipfile.ZIP_STORED) |
Serhiy Storchaka | f828218 | 2016-02-25 12:55:19 +0200 | [diff] [blame] | 74 | self.assertFalse(f.closed) |
| 75 | self.zipTest(TESTFN2, zipfile.ZIP_STORED) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 76 | |
Hai Shi | a3ec3ad | 2020-05-19 06:02:57 +0800 | [diff] [blame] | 77 | @requires_zlib() |
Ezio Melotti | 975077a | 2011-05-19 22:03:22 +0300 | [diff] [blame] | 78 | def testDeflated(self): |
| 79 | # Try the temp file first. If we do TESTFN2 first, then it hogs |
| 80 | # gigabytes of disk space for the duration of the test. |
Serhiy Storchaka | f828218 | 2016-02-25 12:55:19 +0200 | [diff] [blame] | 81 | with TemporaryFile() as f: |
Ezio Melotti | 975077a | 2011-05-19 22:03:22 +0300 | [diff] [blame] | 82 | self.zipTest(f, zipfile.ZIP_DEFLATED) |
Serhiy Storchaka | f828218 | 2016-02-25 12:55:19 +0200 | [diff] [blame] | 83 | self.assertFalse(f.closed) |
| 84 | self.zipTest(TESTFN2, zipfile.ZIP_DEFLATED) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 85 | |
| 86 | def tearDown(self): |
| 87 | for fname in TESTFN, TESTFN2: |
| 88 | if os.path.exists(fname): |
| 89 | os.remove(fname) |
| 90 | |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 91 | |
| 92 | class OtherTests(unittest.TestCase): |
| 93 | def testMoreThan64kFiles(self): |
| 94 | # This test checks that more than 64k files can be added to an archive, |
| 95 | # and that the resulting archive can be read properly by ZipFile |
Serhiy Storchaka | 9e4861f | 2019-03-05 10:05:57 +0200 | [diff] [blame] | 96 | with zipfile.ZipFile(TESTFN, mode="w", allowZip64=True) as zipf: |
| 97 | zipf.debug = 100 |
| 98 | numfiles = (1 << 16) * 3//2 |
| 99 | for i in range(numfiles): |
| 100 | zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57)) |
| 101 | self.assertEqual(len(zipf.namelist()), numfiles) |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 102 | |
Serhiy Storchaka | 9e4861f | 2019-03-05 10:05:57 +0200 | [diff] [blame] | 103 | with zipfile.ZipFile(TESTFN, mode="r") as zipf2: |
| 104 | self.assertEqual(len(zipf2.namelist()), numfiles) |
| 105 | for i in range(numfiles): |
| 106 | content = zipf2.read("foo%08d" % i).decode('ascii') |
| 107 | self.assertEqual(content, "%d" % (i**3 % 57)) |
Serhiy Storchaka | cfbb394 | 2014-09-23 21:34:24 +0300 | [diff] [blame] | 108 | |
| 109 | def testMoreThan64kFilesAppend(self): |
Serhiy Storchaka | 9e4861f | 2019-03-05 10:05:57 +0200 | [diff] [blame] | 110 | with zipfile.ZipFile(TESTFN, mode="w", allowZip64=False) as zipf: |
| 111 | zipf.debug = 100 |
| 112 | numfiles = (1 << 16) - 1 |
| 113 | for i in range(numfiles): |
| 114 | zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57)) |
| 115 | self.assertEqual(len(zipf.namelist()), numfiles) |
| 116 | with self.assertRaises(zipfile.LargeZipFile): |
| 117 | zipf.writestr("foo%08d" % numfiles, b'') |
| 118 | self.assertEqual(len(zipf.namelist()), numfiles) |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 119 | |
Serhiy Storchaka | 9e4861f | 2019-03-05 10:05:57 +0200 | [diff] [blame] | 120 | with zipfile.ZipFile(TESTFN, mode="a", allowZip64=False) as zipf: |
| 121 | zipf.debug = 100 |
| 122 | self.assertEqual(len(zipf.namelist()), numfiles) |
| 123 | with self.assertRaises(zipfile.LargeZipFile): |
| 124 | zipf.writestr("foo%08d" % numfiles, b'') |
| 125 | self.assertEqual(len(zipf.namelist()), numfiles) |
Serhiy Storchaka | cfbb394 | 2014-09-23 21:34:24 +0300 | [diff] [blame] | 126 | |
Serhiy Storchaka | 9e4861f | 2019-03-05 10:05:57 +0200 | [diff] [blame] | 127 | with zipfile.ZipFile(TESTFN, mode="a", allowZip64=True) as zipf: |
| 128 | zipf.debug = 100 |
| 129 | self.assertEqual(len(zipf.namelist()), numfiles) |
| 130 | numfiles2 = (1 << 16) * 3//2 |
| 131 | for i in range(numfiles, numfiles2): |
| 132 | zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57)) |
| 133 | self.assertEqual(len(zipf.namelist()), numfiles2) |
Serhiy Storchaka | cfbb394 | 2014-09-23 21:34:24 +0300 | [diff] [blame] | 134 | |
Serhiy Storchaka | 9e4861f | 2019-03-05 10:05:57 +0200 | [diff] [blame] | 135 | with zipfile.ZipFile(TESTFN, mode="r") as zipf2: |
| 136 | self.assertEqual(len(zipf2.namelist()), numfiles2) |
| 137 | for i in range(numfiles2): |
| 138 | content = zipf2.read("foo%08d" % i).decode('ascii') |
| 139 | self.assertEqual(content, "%d" % (i**3 % 57)) |
Serhiy Storchaka | cfbb394 | 2014-09-23 21:34:24 +0300 | [diff] [blame] | 140 | |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 141 | def tearDown(self): |
Hai Shi | c6f282f | 2020-08-08 19:05:24 +0800 | [diff] [blame] | 142 | os_helper.unlink(TESTFN) |
| 143 | os_helper.unlink(TESTFN2) |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 144 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 145 | if __name__ == "__main__": |
Serhiy Storchaka | cfbb394 | 2014-09-23 21:34:24 +0300 | [diff] [blame] | 146 | unittest.main() |