def compare_tar(tarfile1, tarfile2): """Compare two tarfiles""" tf1 = tarfile.TarFile("none", "r", tarfile1) tf2 = tarfile.TarFile("none", "r", tarfile2) tf2_iter = iter(tf2) for ti1 in tf1: try: ti2 = tf2_iter.next() except StopIteration: assert 0, ("Premature end to second tarfile, " "ti1.name = %s" % ti1.name) # print "Comparing ", ti1.name, ti2.name assert tarinfo_eq(ti1, ti2), "%s %s" % (ti1.name, ti2.name) if ti1.size != 0: fp1 = tf1.extractfile(ti1) buf1 = fp1.read() fp1.close() fp2 = tf2.extractfile(ti2) buf2 = fp2.read() fp2.close() assert buf1 == buf2 try: ti2 = tf2_iter.next() except StopIteration: pass else: assert 0, ("Premature end to first tarfile, " "ti2.name = %s" % ti2.name) tarfile1.close() tarfile2.close()
def set_tarfile( self ): """Set tarfile from next file object, or raise StopIteration""" if self.current_fp: assert not self.current_fp.close() self.current_fp = self.fileobj_iter.next() self.tarfile = tarfile.TarFile( "arbitrary", "r", self.current_fp ) self.tar_iter = iter( self.tarfile )
def test_diff2(self): """Another diff test - this one involves multivol support (requires rdiff to be installed to pass)""" sel1 = selection.Select(Path("testfiles/dir2")) diffdir.write_block_iter(diffdir.SigTarBlockIter(sel1.set_iter()), "testfiles/output/dir2.sigtar") sigtar_fp = open("testfiles/output/dir2.sigtar", "rb") sel2 = selection.Select(Path("testfiles/dir3")) delta_tar = diffdir.DirDelta(sel2.set_iter(), sigtar_fp) diffdir.write_block_iter(delta_tar, "testfiles/output/dir2dir3.difftar") buffer = "" tf = tarfile.TarFile("testfiles/output/dir2dir3.difftar", "r") for tarinfo in tf: if tarinfo.name.startswith("multivol_diff/"): buffer += tf.extractfile(tarinfo).read() assert 3000000 < len(buffer) < 4000000 fout = open("testfiles/output/largefile.delta", "wb") fout.write(buffer) fout.close() assert not os.system("rdiff patch testfiles/dir2/largefile " "testfiles/output/largefile.delta " "testfiles/output/largefile.patched") dir3large = open("testfiles/dir3/largefile", "rb").read() patchedlarge = open("testfiles/output/largefile.patched", "rb").read() assert dir3large == patchedlarge
def test_diff(self): """Test making a diff""" sel1 = selection.Select(Path("testfiles/dir1")) diffdir.write_block_iter(diffdir.SigTarBlockIter(sel1.set_iter()), "testfiles/output/dir1.sigtar") sigtar_fp = open("testfiles/output/dir1.sigtar", "rb") sel2 = selection.Select(Path("testfiles/dir2")) delta_tar = diffdir.DirDelta(sel2.set_iter(), sigtar_fp) diffdir.write_block_iter(delta_tar, "testfiles/output/dir1dir2.difftar") changed_files = ["diff/changeable_permission", "diff/regular_file", "snapshot/symbolic_link/", "deleted/deleted_file", "snapshot/directory_to_file", "snapshot/file_to_directory/"] for tarinfo in tarfile.TarFile("testfiles/output/dir1dir2.difftar", "r"): tiname = util.get_tarinfo_name(tarinfo) if tiname in changed_files: changed_files.remove(tiname) assert not changed_files, ("Following files not found:\n" + "\n".join(changed_files))
def test_iteration(self): """Test iteration through temp2.tar""" self.make_temptar() i = 0 tf = tarfile.TarFile("none", "r", FileLogger(open("temp2.tar", "rb"))) tf.debug = 3 for tarinfo in tf: i += 1 #@UnusedVariable assert i >= 6, i
def testsig(self): """Test producing tar signature of various file types""" select = selection.Select(Path("testfiles/various_file_types")) select.set_iter() sigtar = diffdir.SigTarBlockIter(select) diffdir.write_block_iter(sigtar, "testfiles/output/sigtar") i = 0 for tarinfo in tarfile.TarFile("testfiles/output/sigtar", "r"): # @UnusedVariable i += 1 assert i >= 5, "There should be at least 5 files in sigtar"
def testsig(self): u"""Test producing tar signature of various file types""" select = selection.Select(Path(u"{0}/testfiles/various_file_types".format(_runtest_dir))) select.set_iter() sigtar = diffdir.SigTarBlockIter(select) diffdir.write_block_iter(sigtar, u"{0}/testfiles/output/sigtar".format(_runtest_dir)) i = 0 for tarinfo in tarfile.TarFile(u"{0}/testfiles/output/sigtar".format(_runtest_dir), u"r"): i += 1 assert i >= 5, u"There should be at least 5 files in sigtar"
def make_tarfile(mode, fp): # We often use 'empty' tarfiles for signatures that haven't been filled out # yet. So we want to ignore ReadError exceptions, which are used to signal # this. try: tf = tarfile.TarFile(u"arbitrary", mode, fp) # Now we cause TarFile to not cache TarInfo objects. It would end up # consuming a lot of memory over the lifetime of our long-lasting # signature files otherwise. tf.members = BlackHoleList() return tf except tarfile.ReadError: return FakeTarFile()
def make_bad_tar(filename): u"""Write attack tarfile to filename""" tf = tarfile.TarFile(name=filename, mode=u"w") # file object will be empty, and tarinfo will have path # "snapshot/../warning-security-error" assert not os.system(u"cat /dev/null >testfiles/output/file") path = Path(b"testfiles/output/file") path.index = (b"diff", b"..", b"warning-security-error") ti = path.get_tarinfo() fp = io.StringIO(u"") tf.addfile(ti, fp) tf.close()
def make_temptar(self): """Tar up tempdir, write to "temp2.tar" """ try: os.lstat("temp2.tar") except OSError: pass else: assert not os.system("rm temp2.tar") self.make_tempdir() tf = tarfile.TarFile("temp2.tar", "w") for filename in self.files_in_tempdir: tf.add(filename, filename, 0) tf.close()
def Patch( base_path, difftar_fileobj ): """Patch given base_path and file object containing delta""" diff_tarfile = tarfile.TarFile( "arbitrary", "r", difftar_fileobj ) patch_diff_tarfile( base_path, diff_tarfile ) assert not difftar_fileobj.close()