Debian Codegraph @

testsuite/daemon-module-options_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """Daemon coverage: assorted rsyncd.conf module/global options whose code
3 paths the suite never exercised. One test, many small option-gates --
4 each is a handful of lines but together they were a measurable gap.
5
6 Covers (all in clientserver.c rsync_module() / send_listing() unless noted):
7 motd file global -> read+echo via open_no_attacker_symlinks
8 socket options global -> set_socket_options() (socket.c) on the
9 daemon's per-connection fd
10 incoming chmod module -> parse_chmod into daemon_chmod_modes
11 outgoing chmod module -> same, am_sender branch
12 dont compress module -> token.c init_set_compression's
13 lp_dont_compress() / `dont compress`
14 daemon-side suffix list under -z
15 list = no module -> the "(list=no)" skip in send_listing()
16 comment module -> printed in send_listing()
17 --sockopts client -> set_socket_options() at connect() time
18
19 `motd file` and `socket options` need a real TCP daemon (the
20 RSYNC_CONNECT_PROG pipe transport bypasses send_listing/motd and there's
21 no socket fd to setsockopt on), so this test is require_tcp()-gated.
22 """
23
24 import os
25 import subprocess
26
27 from rsyncfns import (
28 SCRATCHDIR, FROMDIR,
29 claim_ports, make_tree, makepath, require_tcp, rmtree, rsync_argv,
30 start_test_daemon, test_fail, write_daemon_conf,
31 )
32
33 require_tcp("motd/socket-options need a real socket")
34
35 PORT = 19884
36 claim_ports(PORT)
37
38 src = FROMDIR
39 dst_in = SCRATCHDIR / 'dest-modopt-in'
40 dst_out = SCRATCHDIR / 'dest-modopt-out'
41 dst_hidden = SCRATCHDIR / 'dest-modopt-hidden'
42 for d in (src, dst_in, dst_out, dst_hidden):
43 rmtree(d)
44 make_tree(src, depth=1)
45 makepath(dst_in, dst_out, dst_hidden)
46 # Seed the outgoing module so a PULL has content for `outgoing chmod`.
47 (dst_out / 'o0').write_text('out\n')
48 (dst_out / 'o0').chmod(0o644)
49
50 motd = SCRATCHDIR / 'motd.txt'
51 motd.write_text('** TEST MOTD BANNER **\n')
52
53 mods = [
54 ('inmod', {
55 'path': str(dst_in), 'read only': 'no',
56 'incoming chmod': 'Fu+x,g-w,o=r',
57 # daemon-side suffix list (token.c lp_dont_compress(), distinct from
58 # the client --skip-compress path the existing tests already cover)
59 'dont compress': '*.gz *.bz2',
60 'comment': 'incoming-chmod test module',
61 }),
62 ('outmod', {
63 'path': str(dst_out), 'read only': 'yes',
64 'outgoing chmod': 'F-x,o-rwx',
65 'comment': 'outgoing-chmod test module',
66 }),
67 ('hidden', {
68 'path': str(dst_hidden), 'read only': 'no',
69 'list': 'no',
70 }),
71 ]
72 conf = write_daemon_conf(
73 mods,
74 globals={
75 'motd file': str(motd),
76 # OPT_BOOL + OPT_INT= + OPT_ON, plus an unknown to hit the
77 # "Unknown socket option" branch.
78 'socket options': 'SO_KEEPALIVE SO_RCVBUF=8192 SO_BROADCAST NOSUCHOPT',
79 },
80 name='module-options.conf',
81 )
82 url = start_test_daemon(conf, PORT)
83
84
85 # --- module listing: motd echoed, [hidden] absent, comments shown ----------
86 # A bare daemon URL (ends in '/') with no destination = module listing.
87 r = subprocess.run(rsync_argv(f'{url}'), capture_output=True, text=True)
88 if r.returncode != 0:
89 test_fail(f"module listing failed (rc={r.returncode}):\n{r.stderr}")
90 if 'TEST MOTD BANNER' not in r.stdout:
91 test_fail(f"motd file content not echoed to client:\n{r.stdout!r}")
92 if 'hidden' in r.stdout:
93 test_fail(f"list=no module 'hidden' leaked into listing:\n{r.stdout}")
94 if 'inmod' not in r.stdout or 'outmod' not in r.stdout:
95 test_fail(f"expected modules missing from listing:\n{r.stdout}")
96 # The comment column: send_listing() prints "name\tcomment\n" per module.
97 if 'incoming-chmod test module' not in r.stdout:
98 test_fail(f"module comment missing from listing:\n{r.stdout!r}")
99
100 # --- incoming chmod: push, then check Fu+x,g-w,o=r applied -----------------
101 r = subprocess.run(
102 rsync_argv('-rzp', '--sockopts=SO_KEEPALIVE,SO_SNDBUF=8192',
103 f'{src}/', f'{url}inmod/'),
104 capture_output=True, text=True,
105 )
106 if r.returncode != 0:
107 test_fail(f"push to [inmod] failed (rc={r.returncode}):\n{r.stderr}")
108 # Find any regular file in dst_in and check the mode bits.
109 for f in dst_in.rglob('*'):
110 if f.is_file():
111 m = f.stat().st_mode & 0o777
112 # Fu+x -> u has x; g-w -> g has no w; o=r -> other == r exactly
113 if not (m & 0o100):
114 test_fail(f"incoming chmod Fu+x not applied to {f.name}: {oct(m)}")
115 if m & 0o020:
116 test_fail(f"incoming chmod g-w not applied to {f.name}: {oct(m)}")
117 if (m & 0o007) != 0o004:
118 test_fail(f"incoming chmod o=r not applied to {f.name}: {oct(m)}")
119 break
120 else:
121 test_fail("push to [inmod] produced no regular files")
122
123 # --- outgoing chmod: pull, then check F-x,o-rwx applied to received perms --
124 pull_dst = SCRATCHDIR / 'pull-outmod'
125 rmtree(pull_dst); makepath(pull_dst)
126 r = subprocess.run(
127 rsync_argv('-rp', f'{url}outmod/', f'{pull_dst}/'),
128 capture_output=True, text=True,
129 )
130 if r.returncode != 0:
131 test_fail(f"pull from [outmod] failed (rc={r.returncode}):\n{r.stderr}")
132 m = (pull_dst / 'o0').stat().st_mode & 0o777
133 if m & 0o111:
134 test_fail(f"outgoing chmod F-x not applied: {oct(m)}")
135 if m & 0o007:
136 test_fail(f"outgoing chmod o-rwx not applied: {oct(m)}")
137
138 # --- list=no module is still REACHABLE by name -----------------------------
139 r = subprocess.run(rsync_argv('-r', f'{src}/', f'{url}hidden/'),
140 capture_output=True, text=True)
141 if r.returncode != 0:
142 test_fail(f"push to list=no [hidden] should still work by name "
143 f"(rc={r.returncode}):\n{r.stderr}")
144
145 print("daemon-module-options: motd, socket options, --sockopts, "
146 "incoming/outgoing chmod, dont compress, list=no, comment ok")