blob: b0d447de7fa96fc3bc91b0cc9f0ad3f13d54beff [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
5test_support.requires(
6 'largefile',
7 'test requires loads of disk-space bytes and a long time to run'
8 )
9
10# We can test part of the module without zlib.
11try:
12 import zlib
13except ImportError:
14 zlib = None
15
16import zipfile, os, unittest
Neal Norwitz643ad192006-06-15 09:57:03 +000017import time
18import sys
Ronald Oussoren143cefb2006-06-15 08:14:18 +000019
20from StringIO import StringIO
21from tempfile import TemporaryFile
22
23from test.test_support import TESTFN, run_unittest
24
25TESTFN2 = TESTFN + "2"
26
Neal Norwitz643ad192006-06-15 09:57:03 +000027# How much time in seconds can pass before we print a 'Still working' message.
28_PRINT_WORKING_MSG_INTERVAL = 5 * 60
29
Ronald Oussoren143cefb2006-06-15 08:14:18 +000030class TestsWithSourceFile(unittest.TestCase):
31 def setUp(self):
32 line_gen = ("Test of zipfile line %d." % i for i in range(0, 1000000))
33 self.data = '\n'.join(line_gen)
34
35 # Make a source file with some lines
36 fp = open(TESTFN, "wb")
37 fp.write(self.data)
38 fp.close()
39
40 def zipTest(self, f, compression):
41 # Create the ZIP archive
42 filecount = int(((1 << 32) / len(self.data)) * 1.5)
43 zipfp = zipfile.ZipFile(f, "w", compression, allowZip64=True)
44
Neal Norwitz643ad192006-06-15 09:57:03 +000045 next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL
Ronald Oussoren143cefb2006-06-15 08:14:18 +000046 for num in range(filecount):
47 zipfp.writestr("testfn%d"%(num,), self.data)
Neal Norwitz643ad192006-06-15 09:57:03 +000048 # Print still working message since this test can be really slow
49 if next_time <= time.time():
50 next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL
51 print >>sys.__stdout__, \
52 ' zipTest still working, be patient...'
53 sys.__stdout__.flush()
Ronald Oussoren143cefb2006-06-15 08:14:18 +000054 zipfp.close()
55
56 # Read the ZIP archive
57 zipfp = zipfile.ZipFile(f, "r", compression)
58 for num in range(filecount):
59 self.assertEqual(zipfp.read("testfn%d"%(num,)), self.data)
Neal Norwitz643ad192006-06-15 09:57:03 +000060 # Print still working message since this test can be really slow
61 if next_time <= time.time():
62 next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL
63 print >>sys.__stdout__, \
64 ' zipTest still working, be patient...'
65 sys.__stdout__.flush()
Ronald Oussoren143cefb2006-06-15 08:14:18 +000066 zipfp.close()
67
68 def testStored(self):
69 for f in (TESTFN2, TemporaryFile()):
70 self.zipTest(f, zipfile.ZIP_STORED)
71
72 if zlib:
73 def testDeflated(self):
74 for f in (TESTFN2, TemporaryFile()):
75 self.zipTest(f, zipfile.ZIP_DEFLATED)
76
77 def tearDown(self):
78 os.remove(TESTFN)
79 os.remove(TESTFN2)
80
81def test_main():
82 run_unittest(TestsWithSourceFile)
83
84if __name__ == "__main__":
85 test_main()