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")
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")
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')
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.assertEquals(czf.readfile("0").read(), "the real data")
def test_unsupportedCompression(self): """ A zipfile which describes an unsupported compression mechanism should raise BadZipfile. """ fn = self.mktemp() zf = zipfile.ZipFile(fn, "w") 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 zf.close() czf = zipstream.ChunkingZipFile(fn) self.assertRaises(zipfile.BadZipfile, czf.readfile, "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.assertEquals(czf.readfile("1").read(), "more contents")
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.assertEquals(czf.readfile("1").read(), "more contents")