]> git.proxmox.com Git - mirror_edk2.git/blame - AppPkg/Applications/Python/Python-2.7.2/Lib/test/test_zipfile64.py
EmbeddedPkg: Extend NvVarStoreFormattedLib LIBRARY_CLASS
[mirror_edk2.git] / AppPkg / Applications / Python / Python-2.7.2 / Lib / test / test_zipfile64.py
CommitLineData
4710c53d 1# Tests of the full ZIP64 functionality of zipfile\r
2# The test_support.requires call is the only reason for keeping this separate\r
3# from test_zipfile\r
4from test import test_support\r
5\r
6# XXX(nnorwitz): disable this test by looking for extra largfile resource\r
7# which doesn't exist. This test takes over 30 minutes to run in general\r
8# and requires more disk space than most of the buildbots.\r
9test_support.requires(\r
10 'extralargefile',\r
11 'test requires loads of disk-space bytes and a long time to run'\r
12 )\r
13\r
14# We can test part of the module without zlib.\r
15try:\r
16 import zlib\r
17except ImportError:\r
18 zlib = None\r
19\r
20import zipfile, os, unittest\r
21import time\r
22import sys\r
23\r
24from tempfile import TemporaryFile\r
25\r
26from test.test_support import TESTFN, run_unittest\r
27\r
28TESTFN2 = TESTFN + "2"\r
29\r
30# How much time in seconds can pass before we print a 'Still working' message.\r
31_PRINT_WORKING_MSG_INTERVAL = 5 * 60\r
32\r
33class TestsWithSourceFile(unittest.TestCase):\r
34 def setUp(self):\r
35 # Create test data.\r
36 # xrange() is important here -- don't want to create immortal space\r
37 # for a million ints.\r
38 line_gen = ("Test of zipfile line %d." % i for i in xrange(1000000))\r
39 self.data = '\n'.join(line_gen)\r
40\r
41 # And write it to a file.\r
42 fp = open(TESTFN, "wb")\r
43 fp.write(self.data)\r
44 fp.close()\r
45\r
46 def zipTest(self, f, compression):\r
47 # Create the ZIP archive.\r
48 zipfp = zipfile.ZipFile(f, "w", compression, allowZip64=True)\r
49\r
50 # It will contain enough copies of self.data to reach about 6GB of\r
51 # raw data to store.\r
52 filecount = 6*1024**3 // len(self.data)\r
53\r
54 next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL\r
55 for num in range(filecount):\r
56 zipfp.writestr("testfn%d" % num, self.data)\r
57 # Print still working message since this test can be really slow\r
58 if next_time <= time.time():\r
59 next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL\r
60 print >>sys.__stdout__, (\r
61 ' zipTest still writing %d of %d, be patient...' %\r
62 (num, filecount))\r
63 sys.__stdout__.flush()\r
64 zipfp.close()\r
65\r
66 # Read the ZIP archive\r
67 zipfp = zipfile.ZipFile(f, "r", compression)\r
68 for num in range(filecount):\r
69 self.assertEqual(zipfp.read("testfn%d" % num), self.data)\r
70 # Print still working message since this test can be really slow\r
71 if next_time <= time.time():\r
72 next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL\r
73 print >>sys.__stdout__, (\r
74 ' zipTest still reading %d of %d, be patient...' %\r
75 (num, filecount))\r
76 sys.__stdout__.flush()\r
77 zipfp.close()\r
78\r
79 def testStored(self):\r
80 # Try the temp file first. If we do TESTFN2 first, then it hogs\r
81 # gigabytes of disk space for the duration of the test.\r
82 for f in TemporaryFile(), TESTFN2:\r
83 self.zipTest(f, zipfile.ZIP_STORED)\r
84\r
85 if zlib:\r
86 def testDeflated(self):\r
87 # Try the temp file first. If we do TESTFN2 first, then it hogs\r
88 # gigabytes of disk space for the duration of the test.\r
89 for f in TemporaryFile(), TESTFN2:\r
90 self.zipTest(f, zipfile.ZIP_DEFLATED)\r
91\r
92 def tearDown(self):\r
93 for fname in TESTFN, TESTFN2:\r
94 if os.path.exists(fname):\r
95 os.remove(fname)\r
96\r
97\r
98class OtherTests(unittest.TestCase):\r
99 def testMoreThan64kFiles(self):\r
100 # This test checks that more than 64k files can be added to an archive,\r
101 # and that the resulting archive can be read properly by ZipFile\r
102 zipf = zipfile.ZipFile(TESTFN, mode="w")\r
103 zipf.debug = 100\r
104 numfiles = (1 << 16) * 3/2\r
105 for i in xrange(numfiles):\r
106 zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57))\r
107 self.assertEqual(len(zipf.namelist()), numfiles)\r
108 zipf.close()\r
109\r
110 zipf2 = zipfile.ZipFile(TESTFN, mode="r")\r
111 self.assertEqual(len(zipf2.namelist()), numfiles)\r
112 for i in xrange(numfiles):\r
113 self.assertEqual(zipf2.read("foo%08d" % i), "%d" % (i**3 % 57))\r
114 zipf.close()\r
115\r
116 def tearDown(self):\r
117 test_support.unlink(TESTFN)\r
118 test_support.unlink(TESTFN2)\r
119\r
120def test_main():\r
121 run_unittest(TestsWithSourceFile, OtherTests)\r
122\r
123if __name__ == "__main__":\r
124 test_main()\r