~ubuntu-branches/ubuntu/trusty/python3.4/trusty-proposed

« back to all changes in this revision

Viewing changes to Lib/test/test_zipfile64.py

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2013-11-25 09:44:27 UTC
  • Revision ID: package-import@ubuntu.com-20131125094427-lzxj8ap5w01lmo7f
Tags: upstream-3.4~b1
ImportĀ upstreamĀ versionĀ 3.4~b1

Show diffs side-by-side

added added

removed removed

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