Back to index

python3.2  3.2.2
Public Member Functions | Static Public Attributes
test.test_zipfile.OtherTests Class Reference

List of all members.

Public Member Functions

def test_empty_file_raises_BadZipFile
def test_closed_zip_raises_RuntimeError
def test_bad_constructor_mode
def test_bad_open_mode
def test_read0
def test_open_non_existent_item
def test_bad_compression_mode
def test_null_byte_in_filename
def test_struct_sizes
def test_comments
def check_testzip_with_bad_crc
def test_testzip_with_bad_crc_stored
def test_testzip_with_bad_crc_deflated
def check_read_with_bad_crc
def test_read_with_bad_crc_stored
def test_read_with_bad_crc_deflated
def check_read_return_size
def test_read_return_size_stored
def test_read_return_size_deflated
def test_empty_zipfile
def test_open_empty_file
def tearDown

Static Public Attributes

dictionary zips_with_bad_crc

Detailed Description

Definition at line 732 of file test_zipfile.py.


Member Function Documentation

def test.test_zipfile.OtherTests.check_read_return_size (   self,
  compression 
)

Definition at line 1011 of file test_zipfile.py.

01011 
01012     def check_read_return_size(self, compression):
01013         # Issue #9837: ZipExtFile.read() shouldn't return more bytes
01014         # than requested.
01015         for test_size in (1, 4095, 4096, 4097, 16384):
01016             file_size = test_size + 1
01017             junk = b''.join(struct.pack('B', randint(0, 255))
01018                             for x in range(file_size))
01019             with zipfile.ZipFile(io.BytesIO(), "w", compression) as zipf:
01020                 zipf.writestr('foo', junk)
01021                 with zipf.open('foo', 'r') as fp:
01022                     buf = fp.read(test_size)
01023                     self.assertEqual(len(buf), test_size)

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_zipfile.OtherTests.check_read_with_bad_crc (   self,
  compression 
)
Tests that files with bad CRCs raise a BadZipFile exception when read.

Definition at line 983 of file test_zipfile.py.

00983 
00984     def check_read_with_bad_crc(self, compression):
00985         """Tests that files with bad CRCs raise a BadZipFile exception when read."""
00986         zipdata = self.zips_with_bad_crc[compression]
00987 
00988         # Using ZipFile.read()
00989         with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
00990             self.assertRaises(zipfile.BadZipFile, zipf.read, 'afile')
00991 
00992         # Using ZipExtFile.read()
00993         with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
00994             with zipf.open('afile', 'r') as corrupt_file:
00995                 self.assertRaises(zipfile.BadZipFile, corrupt_file.read)
00996 
00997         # Same with small reads (in order to exercise the buffering logic)
00998         with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
00999             with zipf.open('afile', 'r') as corrupt_file:
01000                 corrupt_file.MIN_READ_SIZE = 2
01001                 with self.assertRaises(zipfile.BadZipFile):
01002                     while corrupt_file.read(2):
01003                         pass

Here is the call graph for this function:

Here is the caller graph for this function:

Tests that files with bad CRCs return their name from testzip.

Definition at line 968 of file test_zipfile.py.

00968 
00969     def check_testzip_with_bad_crc(self, compression):
00970         """Tests that files with bad CRCs return their name from testzip."""
00971         zipdata = self.zips_with_bad_crc[compression]
00972 
00973         with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
00974             # testzip returns the name of the first corrupt file, or None
00975             self.assertEqual('afile', zipf.testzip())

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 1056 of file test_zipfile.py.

01056 
01057     def tearDown(self):
01058         unlink(TESTFN)
01059         unlink(TESTFN2)
01060 

Here is the call graph for this function:

Here is the caller graph for this function:

Check that bad compression methods passed to ZipFile.open are
caught.

Definition at line 913 of file test_zipfile.py.

00913 
00914     def test_bad_compression_mode(self):
00915         """Check that bad compression methods passed to ZipFile.open are
00916         caught."""
00917         self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "w", -1)

Here is the call graph for this function:

Check that bad modes passed to ZipFile constructor are caught.

Definition at line 881 of file test_zipfile.py.

00881 
00882     def test_bad_constructor_mode(self):
00883         """Check that bad modes passed to ZipFile constructor are caught."""
00884         self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "q")

Here is the call graph for this function:

Check that bad modes passed to ZipFile.open are caught.

Definition at line 885 of file test_zipfile.py.

00885 
00886     def test_bad_open_mode(self):
00887         """Check that bad modes passed to ZipFile.open are caught."""
00888         with zipfile.ZipFile(TESTFN, mode="w") as zipf:
00889             zipf.writestr("foo.txt", "O, for a Muse of Fire!")
00890 
00891         with zipfile.ZipFile(TESTFN, mode="r") as zipf:
00892         # read the data to make sure the file is there
00893             zipf.read("foo.txt")
00894             self.assertRaises(RuntimeError, zipf.open, "foo.txt", "q")

Here is the call graph for this function:

Verify that testzip() doesn't swallow inappropriate exceptions.

Definition at line 863 of file test_zipfile.py.

00863 
00864     def test_closed_zip_raises_RuntimeError(self):
00865         """Verify that testzip() doesn't swallow inappropriate exceptions."""
00866         data = io.BytesIO()
00867         with zipfile.ZipFile(data, mode="w") as zipf:
00868             zipf.writestr("foo.txt", "O, for a Muse of Fire!")
00869 
00870         # This is correct; calling .read on a closed ZipFile should throw
00871         # a RuntimeError, and so should calling .testzip.  An earlier
00872         # version of .testzip would swallow this exception (and any other)
00873         # and report that the first file in the archive was corrupt.
00874         self.assertRaises(RuntimeError, zipf.read, "foo.txt")
00875         self.assertRaises(RuntimeError, zipf.open, "foo.txt")
00876         self.assertRaises(RuntimeError, zipf.testzip)
00877         self.assertRaises(RuntimeError, zipf.writestr, "bogus.txt", "bogus")
00878         with open(TESTFN, 'w') as f:
00879             f.write('zipfile test data')
00880         self.assertRaises(RuntimeError, zipf.write, TESTFN)

Here is the call graph for this function:

Check that comments on the archive are handled properly.

Definition at line 932 of file test_zipfile.py.

00932 
00933     def test_comments(self):
00934         """Check that comments on the archive are handled properly."""
00935 
00936         # check default comment is empty
00937         with zipfile.ZipFile(TESTFN, mode="w") as zipf:
00938             self.assertEqual(zipf.comment, b'')
00939             zipf.writestr("foo.txt", "O, for a Muse of Fire!")
00940 
00941         with zipfile.ZipFile(TESTFN, mode="r") as zipfr:
00942             self.assertEqual(zipfr.comment, b'')
00943 
00944         # check a simple short comment
00945         comment = b'Bravely taking to his feet, he beat a very brave retreat.'
00946         with zipfile.ZipFile(TESTFN, mode="w") as zipf:
00947             zipf.comment = comment
00948             zipf.writestr("foo.txt", "O, for a Muse of Fire!")
00949         with zipfile.ZipFile(TESTFN, mode="r") as zipfr:
00950             self.assertEqual(zipf.comment, comment)
00951 
00952         # check a comment of max length
00953         comment2 = ''.join(['%d' % (i**3 % 10) for i in range((1 << 16)-1)])
00954         comment2 = comment2.encode("ascii")
00955         with zipfile.ZipFile(TESTFN, mode="w") as zipf:
00956             zipf.comment = comment2
00957             zipf.writestr("foo.txt", "O, for a Muse of Fire!")
00958 
00959         with zipfile.ZipFile(TESTFN, mode="r") as zipfr:
00960             self.assertEqual(zipfr.comment, comment2)
00961 
00962         # check a comment that is too long is truncated
00963         with zipfile.ZipFile(TESTFN, mode="w") as zipf:
00964             zipf.comment = comment2 + b'oops'
00965             zipf.writestr("foo.txt", "O, for a Muse of Fire!")
00966         with zipfile.ZipFile(TESTFN, mode="r") as zipfr:
00967             self.assertEqual(zipfr.comment, comment2)

Here is the call graph for this function:

Definition at line 854 of file test_zipfile.py.

00854 
00855     def test_empty_file_raises_BadZipFile(self):
00856         f = open(TESTFN, 'w')
00857         f.close()
00858         self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, TESTFN)
00859 
00860         with open(TESTFN, 'w') as fp:
00861             fp.write("short file")
00862         self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, TESTFN)

Here is the call graph for this function:

Definition at line 1031 of file test_zipfile.py.

01031 
01032     def test_empty_zipfile(self):
01033         # Check that creating a file in 'w' or 'a' mode and closing without
01034         # adding any files to the archives creates a valid empty ZIP file
01035         zipf = zipfile.ZipFile(TESTFN, mode="w")
01036         zipf.close()
01037         try:
01038             zipf = zipfile.ZipFile(TESTFN, mode="r")
01039         except zipfile.BadZipFile:
01040             self.fail("Unable to create empty ZIP file in 'w' mode")
01041 
01042         zipf = zipfile.ZipFile(TESTFN, mode="a")
01043         zipf.close()
01044         try:
01045             zipf = zipfile.ZipFile(TESTFN, mode="r")
01046         except:
01047             self.fail("Unable to create empty ZIP file in 'a' mode")

Here is the call graph for this function:

Check that a filename containing a null byte is properly
terminated.

Definition at line 918 of file test_zipfile.py.

00918 
00919     def test_null_byte_in_filename(self):
00920         """Check that a filename containing a null byte is properly
00921         terminated."""
00922         with zipfile.ZipFile(TESTFN, mode="w") as zipf:
00923             zipf.writestr("foo.txt\x00qqq", b"O, for a Muse of Fire!")
00924             self.assertEqual(zipf.namelist(), ['foo.txt'])

Here is the call graph for this function:

Definition at line 1048 of file test_zipfile.py.

01048 
01049     def test_open_empty_file(self):
01050         # Issue 1710703: Check that opening a file with less than 22 bytes
01051         # raises a BadZipFile exception (rather than the previously unhelpful
01052         # IOError)
01053         f = open(TESTFN, 'w')
01054         f.close()
01055         self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, TESTFN, 'r')

Here is the call graph for this function:

Check that attempting to call open() for an item that doesn't
exist in the archive raises a RuntimeError.

Definition at line 907 of file test_zipfile.py.

00907 
00908     def test_open_non_existent_item(self):
00909         """Check that attempting to call open() for an item that doesn't
00910         exist in the archive raises a RuntimeError."""
00911         with zipfile.ZipFile(TESTFN, mode="w") as zipf:
00912             self.assertRaises(KeyError, zipf.open, "foo.txt", "r")

Here is the call graph for this function:

Check that calling read(0) on a ZipExtFile object returns an empty
string and doesn't advance file pointer.

Definition at line 895 of file test_zipfile.py.

00895 
00896     def test_read0(self):
00897         """Check that calling read(0) on a ZipExtFile object returns an empty
00898         string and doesn't advance file pointer."""
00899         with zipfile.ZipFile(TESTFN, mode="w") as zipf:
00900             zipf.writestr("foo.txt", "O, for a Muse of Fire!")
00901             # read the data to make sure the file is there
00902             with zipf.open("foo.txt") as f:
00903                 for i in range(FIXEDTEST_SIZE):
00904                     self.assertEqual(f.read(0), b'')
00905 
00906                 self.assertEqual(f.read(), b"O, for a Muse of Fire!")

Here is the call graph for this function:

Definition at line 1028 of file test_zipfile.py.

01028 
01029     def test_read_return_size_deflated(self):
01030         self.check_read_return_size(zipfile.ZIP_DEFLATED)

Here is the call graph for this function:

Definition at line 1024 of file test_zipfile.py.

01024 
01025     def test_read_return_size_stored(self):
01026         self.check_read_return_size(zipfile.ZIP_STORED)

Here is the call graph for this function:

Definition at line 1008 of file test_zipfile.py.

01008 
01009     def test_read_with_bad_crc_deflated(self):
01010         self.check_read_with_bad_crc(zipfile.ZIP_DEFLATED)

Here is the call graph for this function:

Definition at line 1004 of file test_zipfile.py.

01004 
01005     def test_read_with_bad_crc_stored(self):
01006         self.check_read_with_bad_crc(zipfile.ZIP_STORED)

Here is the call graph for this function:

Check that ZIP internal structure sizes are calculated correctly.

Definition at line 925 of file test_zipfile.py.

00925 
00926     def test_struct_sizes(self):
00927         """Check that ZIP internal structure sizes are calculated correctly."""
00928         self.assertEqual(zipfile.sizeEndCentDir, 22)
00929         self.assertEqual(zipfile.sizeCentralDir, 46)
00930         self.assertEqual(zipfile.sizeEndCentDir64, 56)
00931         self.assertEqual(zipfile.sizeEndCentDir64Locator, 20)

Here is the call graph for this function:

Definition at line 980 of file test_zipfile.py.

00980 
00981     def test_testzip_with_bad_crc_deflated(self):
00982         self.check_testzip_with_bad_crc(zipfile.ZIP_DEFLATED)

Here is the call graph for this function:

Definition at line 976 of file test_zipfile.py.

00976 
00977     def test_testzip_with_bad_crc_stored(self):
00978         self.check_testzip_with_bad_crc(zipfile.ZIP_STORED)

Here is the call graph for this function:


Member Data Documentation

Initial value:
{
        zipfile.ZIP_STORED: (
            b'PK\003\004\024\0\0\0\0\0 \213\212;:r'
            b'\253\377\f\0\0\0\f\0\0\0\005\0\0\000af'
            b'ilehello,AworldP'
            b'K\001\002\024\003\024\0\0\0\0\0 \213\212;:'
            b'r\253\377\f\0\0\0\f\0\0\0\005\0\0\0\0'
            b'\0\0\0\0\0\0\0\200\001\0\0\0\000afi'
            b'lePK\005\006\0\0\0\0\001\0\001\0003\000'
            b'\0\0/\0\0\0\0\0'),
        zipfile.ZIP_DEFLATED: (
            b'PK\x03\x04\x14\x00\x00\x00\x08\x00n}

Definition at line 733 of file test_zipfile.py.


The documentation for this class was generated from the following file: