Debian Codegraph @

testsuite/proto-sender-selftest_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Self-test / bitrot guard for the pure-Python rsync sender (rsync_proto.py).
3 #
4 # rsync_proto.py is a second, hand-written implementation of the rsync
5 # client/sender wire format, used to push (possibly malformed) file lists to a
6 # real daemon receiver for security regression tests without recompiling a
7 # patched rsync. Because it duplicates io.c / flist.c / compat.c on-wire
8 # behaviour, it can silently drift when the protocol changes. This test pins it
9 # against the rsync binary under test by checking that the daemon's
10 # recv_file_entry() actually parses the bytes we encode:
11 #
12 # * the @RSYNCD handshake + protocol-30 setup completes (no exception), and
13 # * a well-formed flat file list is parsed without any flist error, and
14 # * an entry whose name length is encoded as XMIT_LONG_NAME + an absurd
15 # varint trips recv_file_entry()'s "overflow: xflags=..." guard
16 # (flist.c) -- which is only reachable if the daemon read our flags byte
17 # and varint name-length exactly, proving the encoder is correct (a
18 # desync would error elsewhere, or not at all).
19 #
20 # Needs a real TCP daemon (raw socket speaker), so it is skipped unless
21 # --use-tcp is in effect.
22
23 import time
24
25 from rsyncfns import (
26 SCRATCHDIR, claim_ports, require_tcp, start_rsyncd, test_fail,
27 )
28 import rsync_proto as rp
29
30 PORT = 12944
31 require_tcp("the pure-Python sender needs a real TCP daemon; run with --use-tcp")
32 claim_ports(PORT)
33
34 mod = SCRATCHDIR / 'proto-mod'
35 mod.mkdir(parents=True, exist_ok=True)
36 conf = SCRATCHDIR / 'proto-selftest.conf'
37 log = SCRATCHDIR / 'proto-selftest.log'
38 conf.write_text(f"""\
39 pid file = {SCRATCHDIR}/proto-rsyncd.pid
40 use chroot = no
41 log file = {log}
42
43 [mod]
44 path = {mod}
45 read only = no
46 """)
47 start_rsyncd(conf, PORT)
48
49 SERVER_ARGS = ['--server', '-e.LsfxCIu', '.', 'mod/']
50
51
52 def push(raw_entries):
53 """Handshake, send a flat flist built from raw entry byte-strings, and
54 return the daemon log text produced for this connection."""
55 before = log.read_text(errors='replace') if log.exists() else ''
56 s = rp.DaemonSender('127.0.0.1', PORT)
57 s.handshake('mod', SERVER_ARGS, greeting_version=30)
58 buf = bytearray()
59 for e in raw_entries:
60 buf += e
61 buf += rp.end_of_flist(0, s.protocol)
62 s.send_data(bytes(buf))
63 s.drain(timeout=2.0)
64 s.close()
65 for _ in range(50):
66 text = log.read_text(errors='replace')
67 if len(text) > len(before):
68 break
69 time.sleep(0.1)
70 return text[len(before):]
71
72
73 # --- 1. a well-formed entry parses with no flist error ----------------------
74 good = rp.FileEntry('hello.txt', mode=rp.S_IFREG | 0o644, length=0).encode()
75 good_log = push([good])
76 if 'receiving file list' not in good_log:
77 test_fail("daemon never reached 'receiving file list' for a well-formed "
78 f"push -- handshake/setup encoding is wrong.\nlog:\n{good_log}")
79 if 'overflow' in good_log:
80 test_fail("a well-formed entry tripped the recv_file_entry overflow guard; "
81 f"the encoder is mis-aligned.\nlog:\n{good_log}")
82
83 # --- 2. XMIT_LONG_NAME + an absurd varint name length is parsed and rejected -
84 # flags byte 0x58 = XMIT_LONG_NAME|XMIT_SAME_UID|XMIT_SAME_GID (no high bits,
85 # no XMIT_EXTENDED_FLAGS -> single byte); then a huge varint name length.
86 bad = (rp.w_byte(rp.XMIT_LONG_NAME | rp.XMIT_SAME_UID | rp.XMIT_SAME_GID)
87 + rp.w_varint(0x7fffff) + b'xx')
88 bad_log = push([bad])
89 if 'overflow' not in bad_log:
90 test_fail("daemon did NOT hit recv_file_entry's name-length overflow guard "
91 "for an XMIT_LONG_NAME entry with a huge varint length; the flag "
92 f"or varint encoding is wrong (the parser never saw it).\n"
93 f"log:\n{bad_log}")
94
95 print("proto-sender-selftest: daemon's recv_file_entry parses rsync_proto's "
96 "flags + varint name-length exactly (well-formed accepted, "
97 "XMIT_LONG_NAME overflow rejected).")