Debian Codegraph @

testsuite/reverse-daemon-delta_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Reverse-direction version-mixing smoke test: OLD client <-> CURRENT daemon.
3 #
4 # Every other two-sided test drives with the current binary and uses the old
5 # binary only as the server/daemon. That covers new-client -> old-server but
6 # NOT the more important backward-compat direction: a current daemon/server
7 # must keep working for the large installed base of OLD clients. This test
8 # fills that gap by starting the daemon with the CURRENT build (RSYNC) and
9 # running the OLD binary (RSYNC_PEER) as the client.
10 #
11 # It exercises, in BOTH transfer directions and with and without compression:
12 # * push (old client = sender, current daemon = receiver) -> old->new delta
13 # * pull (current daemon = sender, old client = receiver) -> new->old delta
14 # In each case the receiving side already holds an older version of the file,
15 # so the rsync delta algorithm actually runs (block matching + token stream)
16 # rather than a whole-file copy -- verified by asserting the bytes moved over
17 # the wire are far smaller than the file (a whole-file transfer of this random,
18 # incompressible data would be ~filesize even with -z).
19 #
20 # When no second binary was selected (RSYNC_PEER == RSYNC) this still runs as a
21 # current<->current smoke test of delta + compression over a daemon.
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 # big enough that delta savings are unambiguous
36 # Old rsync (2.6.x era) prints "wrote N bytes read M bytes"; 3.0+ prints
37 # "sent N bytes received M bytes". Accept both so old clients parse too.
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 # Force C locale: rsync groups the "sent/received N bytes" numbers per the
61 # locale (e.g. de_DE uses '.' for thousands), which would break parsing.
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' # daemon-side basis (old)
87 source = src / f'{tag}.dat' # client source (new)
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' # daemon module file (new)
102 local = dst / f'{tag}.dat' # client basis (old)
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 # Current build is the daemon; old binary is the client.
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)