| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
import io |
| 14 |
import os |
| 15 |
from contextlib import redirect_stdout |
| 16 |
|
| 17 |
from rsyncfns import ( |
| 18 |
SCRATCHDIR, claim_ports, makepath, require_tcp, rmtree, start_test_daemon, |
| 19 |
test_fail, write_daemon_conf, |
| 20 |
) |
| 21 |
import rsync_proto as rp |
| 22 |
import xrsync |
| 23 |
|
| 24 |
PORT = 12968 |
| 25 |
require_tcp("xrsync speaks the daemon protocol over a raw TCP socket; run with --use-tcp") |
| 26 |
claim_ports(PORT) |
| 27 |
|
| 28 |
base = SCRATCHDIR / 'xrsync' |
| 29 |
rmtree(base) |
| 30 |
mod = base / 'mod' |
| 31 |
makepath(mod / 'sub') |
| 32 |
A = b'A' * 16 |
| 33 |
B = b'B' * 47 + b'\n' |
| 34 |
NOTE = b'just a note\n' |
| 35 |
CSUB = b'nested pdf\n' |
| 36 |
(mod / 'a.pdf').write_bytes(A) |
| 37 |
(mod / 'b.pdf').write_bytes(B) |
| 38 |
(mod / 'note.txt').write_bytes(NOTE) |
| 39 |
(mod / 'sub' / 'c.pdf').write_bytes(CSUB) |
| 40 |
os.symlink('a.pdf', mod / 'link.pdf') |
| 41 |
MTIME = 1609556645 |
| 42 |
os.utime(mod / 'a.pdf', (MTIME, MTIME)) |
| 43 |
os.utime(mod / 'b.pdf', (MTIME, MTIME)) |
| 44 |
|
| 45 |
conf = write_daemon_conf([ |
| 46 |
('mod', {'path': str(mod), 'read only': 'no', 'use chroot': 'no'}), |
| 47 |
], name='xrsync.conf') |
| 48 |
start_test_daemon(conf, PORT) |
| 49 |
|
| 50 |
COMMON = ['--port', str(PORT)] |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
buf = io.StringIO() |
| 55 |
with redirect_stdout(buf): |
| 56 |
rc = xrsync.main(COMMON + ['-l', 'localhost::mod/']) |
| 57 |
listing = buf.getvalue() |
| 58 |
if rc != 0: |
| 59 |
test_fail(f"xrsync list returned {rc}\n{listing}") |
| 60 |
for name in ('a.pdf', 'b.pdf', 'note.txt', 'link.pdf', 'sub'): |
| 61 |
if name not in listing: |
| 62 |
test_fail(f"xrsync list missing {name!r}:\n{listing}") |
| 63 |
if 'link.pdf -> a.pdf' not in listing: |
| 64 |
test_fail(f"xrsync list did not resolve the symlink target:\n{listing}") |
| 65 |
|
| 66 |
names = [ln.split()[2] for ln in listing.splitlines() if ln.strip()] |
| 67 |
if names != sorted(names): |
| 68 |
test_fail(f"xrsync list not in sorted order: {names}") |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
pull_dest = base / 'pull' |
| 73 |
makepath(pull_dest) |
| 74 |
rc = xrsync.main(COMMON + ['-a', 'localhost::mod/*.pdf', str(pull_dest)]) |
| 75 |
if rc != 0: |
| 76 |
test_fail(f"xrsync pull returned {rc}") |
| 77 |
if (pull_dest / 'a.pdf').read_bytes() != A: |
| 78 |
test_fail("pulled a.pdf content mismatch") |
| 79 |
if (pull_dest / 'b.pdf').read_bytes() != B: |
| 80 |
test_fail("pulled b.pdf content mismatch") |
| 81 |
if not (pull_dest / 'link.pdf').is_symlink() \ |
| 82 |
or os.readlink(pull_dest / 'link.pdf') != 'a.pdf': |
| 83 |
test_fail("pulled link.pdf is not a symlink to a.pdf") |
| 84 |
if int((pull_dest / 'a.pdf').stat().st_mtime) != MTIME: |
| 85 |
test_fail(f"pulled a.pdf mtime not preserved " |
| 86 |
f"({int((pull_dest / 'a.pdf').stat().st_mtime)} != {MTIME})") |
| 87 |
if (pull_dest / 'note.txt').exists(): |
| 88 |
test_fail("pull of *.pdf should not have fetched note.txt") |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
push_src = base / 'push' |
| 93 |
makepath(push_src) |
| 94 |
U1 = b'uploaded one\n' |
| 95 |
U2 = os.urandom(5000) |
| 96 |
(push_src / 'u1.txt').write_bytes(U1) |
| 97 |
(push_src / 'u2.bin').write_bytes(U2) |
| 98 |
rc = xrsync.main(COMMON + [str(push_src / 'u1.txt'), str(push_src / 'u2.bin'), |
| 99 |
'localhost::mod/']) |
| 100 |
if rc != 0: |
| 101 |
test_fail(f"xrsync push returned {rc}") |
| 102 |
if (mod / 'u1.txt').read_bytes() != U1: |
| 103 |
test_fail("pushed u1.txt content mismatch") |
| 104 |
if (mod / 'u2.bin').read_bytes() != U2: |
| 105 |
test_fail("pushed u2.bin (random) content mismatch") |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
class CorruptingClient(rp.DaemonClient): |
| 110 |
"""Replaces just the file token stream with corrupted bytes; everything |
| 111 |
else (handshake, flist, request loop, md5) is xrsync's. The daemon's |
| 112 |
whole-file md5 must then reject the transfer.""" |
| 113 |
|
| 114 |
def make_file_token_stream(self, content): |
| 115 |
return super().make_file_token_stream(b'X' * len(content)) |
| 116 |
|
| 117 |
|
| 118 |
rc = xrsync.main(COMMON + [str(push_src / 'u1.txt'), 'localhost::mod/hooked.txt'], |
| 119 |
client_factory=CorruptingClient) |
| 120 |
landed = mod / 'hooked.txt' |
| 121 |
if landed.exists() and landed.read_bytes() == U1: |
| 122 |
test_fail("the corrupting hook did not take effect -- the daemon accepted " |
| 123 |
"the original content, so make_file_token_stream was not used") |
| 124 |
|
| 125 |
if landed.exists() and landed.read_bytes() not in (b'X' * len(U1),): |
| 126 |
|
| 127 |
pass |
| 128 |
|
| 129 |
print("xrsync: list + pull + push round-trip against rsyncd, and the " |
| 130 |
"make_file_token_stream hook is honoured (corrupt push rejected by md5).") |