Esempio n. 1
0
 def test_invalidMode(self):
     """
     A ChunkingZipFile opened in write-mode should not allow .readfile(),
     and raise a RuntimeError instead.
     """
     czf = zipstream.ChunkingZipFile(self.mktemp(), "w")
     self.assertRaises(RuntimeError, czf.readfile, "something")
Esempio n. 2
0
 def test_closedArchive(self):
     """
     A closed ChunkingZipFile should raise a L{RuntimeError} when
     .readfile() is invoked.
     """
     czf = zipstream.ChunkingZipFile(self.makeZipFile(["something"]), "r")
     czf.close()
     self.assertRaises(RuntimeError, czf.readfile, "something")
Esempio n. 3
0
 def getFileEntry(self, contents):
     """
     Return an appropriate zip file entry
     """
     filename = self.mktemp()
     with zipfile.ZipFile(filename, "w", self.compression) as z:
         z.writestr("content", contents)
     z = zipstream.ChunkingZipFile(filename, "r")
     return z.readfile("content")
Esempio n. 4
0
 def getFileEntry(self, contents):
     """
     Return an appropriate zip file entry
     """
     filename = self.mktemp()
     z = zipfile.ZipFile(filename, 'w', self.compression)
     z.writestr('content', contents)
     z.close()
     z = zipstream.ChunkingZipFile(filename, 'r')
     return z.readfile('content')
Esempio n. 5
0
 def test_extraData(self):
     """
     readfile() should skip over 'extra' data present in the zip metadata.
     """
     fn = self.mktemp()
     with zipfile.ZipFile(fn, 'w') as zf:
         zi = zipfile.ZipInfo("0")
         zi.extra = b"hello, extra"
         zf.writestr(zi, b"the real data")
     with zipstream.ChunkingZipFile(fn) as czf, czf.readfile("0") as zfe:
         self.assertEqual(zfe.read(), b"the real data")
Esempio n. 6
0
 def test_extraData(self):
     """
     readfile() should skip over 'extra' data present in the zip metadata.
     """
     fn = self.mktemp()
     zf = zipfile.ZipFile(fn, 'w')
     zi = zipfile.ZipInfo("0")
     zi.extra = "hello, extra"
     zf.writestr(zi, "the real data")
     zf.close()
     czf = zipstream.ChunkingZipFile(fn)
     self.assertEqual(czf.readfile("0").read(), "the real data")
Esempio n. 7
0
    def test_unsupportedCompression(self):
        """
        A zipfile which describes an unsupported compression mechanism should
        raise BadZipfile.
        """
        fn = self.mktemp()
        with zipfile.ZipFile(fn, "w") as zf:
            zi = zipfile.ZipInfo("0")
            zf.writestr(zi, "some data")
            # Mangle its compression type in the central directory; can't do
            # this before the writestr call or zipfile will (correctly) tell us
            # not to pass bad compression types :)
            zi.compress_type = 1234

        with zipstream.ChunkingZipFile(fn) as czf:
            self.assertRaises(zipfile.BadZipfile, czf.readfile, "0")
Esempio n. 8
0
 def test_invalidHeader(self):
     """
     A zipfile entry with the wrong magic number should raise BadZipfile for
     readfile(), but that should not affect other files in the archive.
     """
     fn = self.makeZipFile(["test contents", "more contents"])
     with zipfile.ZipFile(fn, "r") as zf:
         zeroOffset = zf.getinfo("0").header_offset
     # Zero out just the one header.
     with open(fn, "r+b") as scribble:
         scribble.seek(zeroOffset, 0)
         scribble.write(b"0" * 4)
     with zipstream.ChunkingZipFile(fn) as czf:
         self.assertRaises(zipfile.BadZipfile, czf.readfile, "0")
         with czf.readfile("1") as zfe:
             self.assertEqual(zfe.read(), b"more contents")
Esempio n. 9
0
 def test_invalidHeader(self):
     """
     A zipfile entry with the wrong magic number should raise BadZipfile for
     readfile(), but that should not affect other files in the archive.
     """
     fn = self.makeZipFile(["test contents", "more contents"])
     zf = zipfile.ZipFile(fn, "r")
     zeroOffset = zf.getinfo("0").header_offset
     zf.close()
     # Zero out just the one header.
     scribble = file(fn, "r+b")
     scribble.seek(zeroOffset, 0)
     scribble.write(chr(0) * 4)
     scribble.close()
     czf = zipstream.ChunkingZipFile(fn)
     self.assertRaises(zipfile.BadZipfile, czf.readfile, "0")
     self.assertEqual(czf.readfile("1").read(), "more contents")
Esempio n. 10
0
    def test_filenameMismatch(self):
        """
        A zipfile entry with a different filename than is found in the central
        directory should raise BadZipfile.
        """
        fn = self.makeZipFile([b"test contents", b"more contents"])
        with zipfile.ZipFile(fn, "r") as zf:
            info = zf.getinfo("0")
            info.filename = "not zero"
        with open(fn, "r+b") as scribble:
            scribble.seek(info.header_offset, 0)
            scribble.write(info.FileHeader())

        with zipstream.ChunkingZipFile(fn) as czf:
            self.assertRaises(zipfile.BadZipfile, czf.readfile, "0")
            with czf.readfile("1") as zfe:
                self.assertEqual(zfe.read(), b"more contents")
Esempio n. 11
0
    def test_filenameMismatch(self):
        """
        A zipfile entry with a different filename than is found in the central
        directory should raise BadZipfile.
        """
        fn = self.makeZipFile(["test contents", "more contents"])
        zf = zipfile.ZipFile(fn, "r")
        info = zf.getinfo("0")
        info.filename = "not zero"
        zf.close()
        scribble = file(fn, "r+b")
        scribble.seek(info.header_offset, 0)
        scribble.write(info.FileHeader())
        scribble.close()

        czf = zipstream.ChunkingZipFile(fn)
        self.assertRaises(zipfile.BadZipfile, czf.readfile, "0")
        self.assertEqual(czf.readfile("1").read(), "more contents")