| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
import filecmp |
| 24 |
import os |
| 25 |
import re |
| 26 |
import shlex |
| 27 |
import subprocess |
| 28 |
|
| 29 |
from rsyncfns import ( |
| 30 |
FROMDIR, RSYNC, RSYNC_PEER, TMPDIR, |
| 31 |
build_rsyncd_conf, makepath, make_data_file, start_test_daemon, test_fail, split_rsync_cmd, |
| 32 |
) |
| 33 |
|
| 34 |
DAEMON_PORT = 12894 |
| 35 |
FILESIZE = 512 * 1024 |
| 36 |
|
| 37 |
|
| 38 |
_SUMMARY = re.compile(r'(?:sent|wrote) ([\d,]+) bytes\s+(?:received|read) ([\d,]+) bytes') |
| 39 |
|
| 40 |
TODIR = TMPDIR / 'to' |
| 41 |
|
| 42 |
|
| 43 |
def make_versions(path_old, path_new): |
| 44 |
"""Write an 'old' file and a 'new' file derived from it: same head, a |
| 45 |
changed middle block, and an appended tail. The shared blocks give the |
| 46 |
delta algorithm something to match; the changes give it real literal data |
| 47 |
to send.""" |
| 48 |
make_data_file(path_old, FILESIZE) |
| 49 |
data = bytearray(open(path_old, 'rb').read()) |
| 50 |
data[100000:100050] = bytes(((b + 1) & 0xFF) for b in data[100000:100050]) |
| 51 |
data += b'reverse-delta appended tail\n' * 64 |
| 52 |
with open(path_new, 'wb') as f: |
| 53 |
f.write(data) |
| 54 |
|
| 55 |
|
| 56 |
def peer_client(args, label): |
| 57 |
"""Run the OLD client (RSYNC_PEER) and return (sent, received) wire bytes |
| 58 |
parsed from rsync's summary line. Fails the test on non-zero exit.""" |
| 59 |
argv = split_rsync_cmd(RSYNC_PEER) + args |
| 60 |
|
| 61 |
|
| 62 |
proc = subprocess.run(argv, stdout=subprocess.PIPE, |
| 63 |
stderr=subprocess.STDOUT, text=True, |
| 64 |
env={**os.environ, 'LC_ALL': 'C'}) |
| 65 |
print(proc.stdout, end='') |
| 66 |
if proc.returncode != 0: |
| 67 |
test_fail(f"{label}: old client exited {proc.returncode}") |
| 68 |
m = _SUMMARY.search(proc.stdout) |
| 69 |
if not m: |
| 70 |
test_fail(f"{label}: could not parse sent/received from client output") |
| 71 |
return int(m.group(1).replace(',', '')), int(m.group(2).replace(',', '')) |
| 72 |
|
| 73 |
|
| 74 |
def assert_delta(label, moved): |
| 75 |
"""A delta transfer of FILESIZE moves far less than the whole file; a |
| 76 |
whole-file copy (delta failed to engage) would move ~FILESIZE.""" |
| 77 |
if moved >= FILESIZE // 2: |
| 78 |
test_fail(f"{label}: {moved} bytes crossed the wire -- delta did not " |
| 79 |
f"engage (file is {FILESIZE} bytes)") |
| 80 |
|
| 81 |
|
| 82 |
def run_push(compress): |
| 83 |
"""old client (sender) -> current daemon (receiver), receiver holds the old |
| 84 |
version as the basis. Exercises old->new delta encoding.""" |
| 85 |
tag = "push+z" if compress else "push" |
| 86 |
basis = TODIR / f'{tag}.dat' |
| 87 |
source = src / f'{tag}.dat' |
| 88 |
make_versions(basis, source) |
| 89 |
opts = ['-a', '-v'] + (['-z'] if compress else []) |
| 90 |
sent, _ = peer_client(opts + [str(source), f'{url}test-to/'], tag) |
| 91 |
if not filecmp.cmp(source, basis, shallow=False): |
| 92 |
test_fail(f"{tag}: daemon-side file does not match source after push") |
| 93 |
assert_delta(tag, sent) |
| 94 |
print(f"{tag}: OK (sent {sent} bytes for a {FILESIZE}-byte file)") |
| 95 |
|
| 96 |
|
| 97 |
def run_pull(compress): |
| 98 |
"""current daemon (sender) -> old client (receiver), client holds the old |
| 99 |
version as the basis. Exercises new->old delta encoding.""" |
| 100 |
tag = "pull+z" if compress else "pull" |
| 101 |
served = FROMDIR / f'{tag}.dat' |
| 102 |
local = dst / f'{tag}.dat' |
| 103 |
make_versions(local, served) |
| 104 |
opts = ['-a', '-v'] + (['-z'] if compress else []) |
| 105 |
_, received = peer_client( |
| 106 |
opts + [f'{url}test-from/{tag}.dat', str(dst) + '/'], tag) |
| 107 |
if not filecmp.cmp(served, local, shallow=False): |
| 108 |
test_fail(f"{tag}: client file does not match daemon source after pull") |
| 109 |
assert_delta(tag, received) |
| 110 |
print(f"{tag}: OK (received {received} bytes for a {FILESIZE}-byte file)") |
| 111 |
|
| 112 |
|
| 113 |
os.chdir(TMPDIR) |
| 114 |
makepath(FROMDIR, TODIR) |
| 115 |
|
| 116 |
|
| 117 |
conf = build_rsyncd_conf() |
| 118 |
url = start_test_daemon(conf, DAEMON_PORT, rsync_cmd=RSYNC) |
| 119 |
|
| 120 |
src = TMPDIR / 'client-src' |
| 121 |
dst = TMPDIR / 'client-dst' |
| 122 |
makepath(src, dst) |
| 123 |
|
| 124 |
run_push(compress=False) |
| 125 |
run_push(compress=True) |
| 126 |
run_pull(compress=False) |
| 127 |
run_pull(compress=True) |