def sig_file_test_helper(self, blocksize, iterations, file_len=None): """Compare SigFile output to rdiff output at given blocksize""" for i in range(iterations): MakeRandomFile(self.basis.path, file_len) self._clean_file(self.sig) rdiff_help_text = subprocess.check_output(["rdiff", "--help"]) if b'-R' in rdiff_help_text: self.assertEqual(os_system( b"rdiff -b %i -R rollsum -S 8 -H md4 signature %b %b" % (blocksize, self.basis.path, self.sig.path)), 0) elif b'-H' in rdiff_help_text: self.assertEqual(os_system( b"rdiff -b %i -H md4 signature %b %b" % (blocksize, self.basis.path, self.sig.path)), 0) else: self.assertEqual(os_system( b"rdiff -b %i signature %b %b" % (blocksize, self.basis.path, self.sig.path)), 0) with self.sig.open("rb") as fp: rdiff_sig = fp.read() sf = librsync.SigFile(self.basis.open("rb"), blocksize) librsync_sig = sf.read() sf.close() self.assertEqual(rdiff_sig, librsync_sig)
def testDelta(self): """Test delta generation by making sure rdiff can process output There appears to be some indeterminism so we can't just byte-compare the deltas produced by rdiff and DeltaFile. """ MakeRandomFile(self.basis.path) self._clean_file(self.sig) self.assertEqual(os_system( b"rdiff signature %s %s" % (self.basis.path, self.sig.path)), 0) for i in range(5): MakeRandomFile(self.new.path) df = librsync.DeltaFile(self.sig.open("rb"), self.new.open("rb")) librsync_delta = df.read() df.close() fp = self.delta.open("wb") fp.write(librsync_delta) fp.close() self._clean_file(self.new2) self.assertEqual(os_system( b"rdiff patch %s %s %s" % (self.basis.path, self.delta.path, self.new2.path)), 0) new_fp = self.new.open("rb") new = new_fp.read() new_fp.close() new2_fp = self.new2.open("rb") new2 = new2_fp.read() new2_fp.close() self.assertEqual(new, new2)
def testRdiffDeltaPatchGzip(self): """Same as above by try gzipping patches""" rplist = [ self.basis, self.new, self.delta, self.signature, self.output ] for rp in rplist: if rp.lstat(): rp.delete() MakeRandomFile(self.basis.path) MakeRandomFile(self.new.path) list(map(rpath.RPath.setdata, [self.basis, self.new])) self.assertTrue(self.basis.lstat() and self.new.lstat()) self.signature.write_from_fileobj(Rdiff.get_signature(self.basis)) self.assertTrue(self.signature.lstat()) self.delta.write_from_fileobj( Rdiff.get_delta_sigrp_hash(self.signature, self.new)) self.assertTrue(self.delta.lstat()) gzip_path = self.delta.path + b".gz" if os.name == "nt": # simulate gzip using 7z on Windows os_system("7z a -tgzip -sdel -bb0 -y %s %s" % (os.fsdecode(gzip_path), os.fsdecode(self.delta.path))) else: os_system(b"gzip %s" % self.delta.path) os.rename(gzip_path, self.delta.path) self.delta.setdata() Rdiff.patch_local(self.basis, self.delta, self.output, delta_compressed=1) self.assertTrue(rpath.cmp(self.new, self.output)) list(map(rpath.RPath.delete, rplist))
def testWriteDeltaGzip(self): """Same as above but delta is written gzipped""" rplist = [self.basis, self.new, self.delta, self.output] MakeRandomFile(self.basis.path) MakeRandomFile(self.new.path) list(map(rpath.RPath.setdata, [self.basis, self.new])) self.assertTrue(self.basis.lstat() and self.new.lstat()) delta_gz = rpath.RPath(self.delta.conn, self.delta.path + b".gz") if delta_gz.lstat(): delta_gz.delete() Rdiff.write_delta(self.basis, self.new, delta_gz, 1) self.assertTrue(delta_gz.lstat()) if os.name == "nt": # simulate gunzip using 7z on Windows os_system( "7z e -tgzip -bb0 -y -o%s %s" % (os.fsdecode(delta_gz.get_parent_rp()), os.fsdecode(delta_gz))) os.unlink(delta_gz.path) else: os_system(b"gunzip %s" % delta_gz.path) delta_gz.setdata() self.delta.setdata() Rdiff.patch_local(self.basis, self.delta, self.output) self.assertTrue(rpath.cmp(self.new, self.output)) list(map(rpath.RPath.delete, rplist))
def testQuote(self): """See if filename quoting works""" wtf = rpath.RPath(self.lc, self.prefix, (self.weirdfilename, )) reg = rpath.RPath(self.lc, self.prefix, (b"regular_file", )) self.assertTrue(wtf.lstat()) self.assertTrue(reg.lstat()) self.assertEqual(os_system(b"ls %s >/dev/null 2>&1" % wtf.quote()), 0) self.assertEqual(os_system(b"ls %s >/dev/null 2>&1" % reg.quote()), 0)
def secure_rdiff_backup(self, in_dir, out_dir, in_local, restrict_args, extra_args=b"", success=1, current_time=None): """Run rdiff-backup locally, with given restrict settings""" if not current_time: current_time = int(time.time()) # escape the %s of the remote schema with double % prefix = (b'%b --current-time %i --remote-schema {h} ' % (RBBin, current_time)) if in_local: out_dir = (b'"%b %b --server::%b"' % (RBBin, restrict_args, out_dir)) else: in_dir = (b'"%b %b --server::%b"' % (RBBin, restrict_args, in_dir)) cmdline = b"%b %b %b %b" % (prefix, extra_args, in_dir, out_dir) print("Executing:", cmdline) exit_val = os_system(cmdline) if success: self.assertEqual(exit_val, 0) else: self.assertNotEqual(exit_val, 0)
def testGzipRead(self): """Test reading of gzipped files""" try: os.mkdir(abs_output_dir) except OSError: pass file_nogz = os.path.join(abs_output_dir, b"file") file_gz = file_nogz + b".gz" rp_gz = rpath.RPath(self.lc, file_gz) if rp_gz.lstat(): rp_gz.delete() rp_nogz = rpath.RPath(self.lc, file_nogz) s = "Hello, world!" with rp_nogz.open("w") as fp_out: fp_out.write(s) rp_nogz.setdata() self.assertTrue(rp_nogz.lstat()) self.assertEqual(os_system(b"gzip %s" % file_nogz), 0) rp_nogz.setdata() rp_gz.setdata() self.assertFalse(rp_nogz.lstat()) self.assertTrue(rp_gz.lstat()) with rp_gz.open("r", compress=1) as fp_in: read_s = fp_in.read().decode() # zip is always binary hence bytes self.assertEqual(read_s, s)
def OldtestDelta(self): """Test delta generation against Rdiff""" MakeRandomFile(self.basis.path) self.assertEqual(os_system( b"rdiff signature %s %s" % (self.basis.path, self.sig.path)), 0) for i in range(5): MakeRandomFile(self.new.path) self.assertEqual(os_system( b"rdiff delta %b %b %b" % (self.sig.path, self.new.path, self.delta.path)), 0) fp = self.delta.open("rb") rdiff_delta = fp.read() fp.close() df = librsync.DeltaFile(self.sig.open("rb"), self.new.open("rb")) librsync_delta = df.read() df.close() print(len(rdiff_delta), len(librsync_delta)) print(repr(rdiff_delta[:100])) print(repr(librsync_delta[:100])) self.assertEqual(rdiff_delta, librsync_delta)
def testPatch(self): """Test patching against Rdiff""" MakeRandomFile(self.basis.path) self._clean_file(self.sig) self.assertEqual(os_system( b"rdiff signature %s %s" % (self.basis.path, self.sig.path)), 0) for i in range(5): MakeRandomFile(self.new.path) self._clean_file(self.delta) self.assertEqual(os_system( b"rdiff delta %s %s %s" % (self.sig.path, self.new.path, self.delta.path)), 0) fp = self.new.open("rb") real_new = fp.read() fp.close() pf = librsync.PatchedFile(self.basis.open("rb"), self.delta.open("rb")) librsync_new = pf.read() pf.close() self.assertEqual(real_new, librsync_new)
def run_cmd(cmd): """Run the given cmd, return the amount of time it took""" time.sleep(1) # just to be sure to not have the infamous message # Fatal Error: Time of Last backup is not in the past. This is probably caused # by running two backups in less than a second. Wait a second and try again. if new_pythonpath: full_cmd = b"PYTHONPATH=%b %b" % (new_pythonpath, cmd) else: full_cmd = cmd print("Running command '%s'" % (full_cmd, )) t = time.time() rc = os_system(full_cmd) if rc != 0: raise RuntimeError("Return code of '{cmd}' is '{rc}'".format( cmd=cmd, rc=rc)) return time.time() - t
def cause_regress(self, rp): """Change some of the above to trigger regress""" rp1_1 = rp.append('foo') rp1_1.chmod(0o4) rp_new = rp.append('lala') rp_new.write_string('asoentuh') rp_new.chmod(0) self.assertEqual( os_system(b'chown %s %s' % (user.encode(), rp_new.path)), 0) rp1_3 = rp.append('unreadable_dir') rp1_3.chmod(0o700) rp1_3_1 = rp1_3.append('file_inside') rp1_3_1.chmod(0o1) rp1_3.chmod(0) rbdir = rp.append('rdiff-backup-data') rbdir.append('current_mirror.2000-12-31T21:33:20-07:00.data').touch()
def testGzipWrite(self): """Test writing of gzipped files""" try: os.mkdir(abs_output_dir) except OSError: pass file_nogz = os.path.join(abs_output_dir, b"file") file_gz = file_nogz + b".gz" rp_gz = rpath.RPath(self.lc, file_gz) rp_nogz = rpath.RPath(self.lc, file_nogz) if rp_nogz.lstat(): rp_nogz.delete() s = b"Hello, world!" with rp_gz.open("wb", compress=1) as fp_out: fp_out.write(s) self.assertEqual(os_system(b"gunzip %s" % file_gz), 0) with rp_nogz.open("rb") as fp_in: self.assertEqual(fp_in.read(), s)
def test_unreadable(self): """Run regress test when regular file is unreadable""" self.output_rp.setdata() if self.output_rp.lstat(): Myrm(self.output_rp.path) unreadable_rp = self.make_unreadable() rdiff_backup(1, 1, unreadable_rp.path, self.output_rp.path, current_time=1) rbdir = self.output_rp.append('rdiff-backup-data') marker = rbdir.append('current_mirror.2000-12-31T21:33:20-07:00.data') marker.touch() self.change_unreadable() cmd = b"rdiff-backup --check-destination-dir %s" % self.output_rp.path print("Executing:", cmd) self.assertEqual(os_system(cmd), 0)
def _run_cmd(self, cmd): print("Running: ", cmd) rc = os_system(cmd) self.assertEqual( rc, 0, "Command '{cmd}' failed with rc={rc}".format(cmd=cmd, rc=rc))