| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 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 |
|
| 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 |
|
| 84 |
|
| 85 |
|
| 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).") |