Debian Codegraph @

testsuite/daemon-include-maxconn_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """Daemon coverage: rsyncd.conf `&include`/`&merge` directives + `max connections`.
3
4 Covers:
5 - params.c include_config(): both the file form (`&merge = file.inc`) and
6 the directory form (`&include = dir/` -> readdir + glob *.conf + qsort
7 via name_cmp() when count>1) -> the ]push/]reset/]pop section-stack path
8 in loadparm.c.
9 - connection.c claim_connection() / util1.c lock_range(): a module with
10 `max connections = N` + `lock file = path` opens the lock file via
11 open_no_attacker_symlinks() and lock_range()s a 4-byte slot.
12 """
13
14 import subprocess
15
16 from rsyncfns import (
17 SCRATCHDIR, FROMDIR,
18 claim_ports, get_rootgid, get_rootuid, get_testuid,
19 make_tree, makepath, rmtree, rsync_argv,
20 start_test_daemon, test_fail, write_daemon_conf,
21 )
22
23 # `&include` (manage_globals=1) ]push/]pop's the global section stack, so the
24 # parent conf's root-aware `uid = 0`/`gid = 0` does NOT propagate into the
25 # fragments -- the included modules would default to `nobody` and fail to
26 # write into root-owned dest dirs. Mirror write_daemon_conf()'s handling.
27 if get_testuid() == get_rootuid():
28 UIDGID = f'\tuid = {get_rootuid()}\n\tgid = {get_rootgid()}\n'
29 else:
30 UIDGID = ''
31
32 PORT = 12895
33 claim_ports(PORT)
34
35 src = FROMDIR
36 rmtree(src)
37 make_tree(src, depth=1)
38
39 # A directory of *.conf fragments for `&include = <dir>` (needs >=2 entries
40 # to hit name_cmp() via qsort, and a non-matching file to exercise the
41 # wildmatch("*.conf", ...) skip).
42 conf_d = SCRATCHDIR / 'rsyncd.conf.d'
43 makepath(conf_d)
44
45 dest_a = SCRATCHDIR / 'dest-incA'
46 dest_b = SCRATCHDIR / 'dest-incB'
47 dest_m = SCRATCHDIR / 'dest-merge'
48 makepath(dest_a, dest_b, dest_m)
49
50 lockfile = SCRATCHDIR / 'rsyncd.lock'
51
52 (conf_d / '20-b.conf').write_text(
53 f'[incB]\n\tpath = {dest_b}\n\tread only = no\n{UIDGID}'
54 )
55 (conf_d / '10-a.conf').write_text(
56 '# fragment loaded via &include directory glob\n'
57 f'[incA]\n'
58 f'\tpath = {dest_a}\n'
59 f'\tread only = no\n'
60 f'\tmax connections = 2\n'
61 f'\tlock file = {lockfile}\n'
62 f'{UIDGID}'
63 )
64 (conf_d / 'README').write_text('not matched by *.conf glob\n')
65
66 # A single file for `&merge = <file>` (the manage_globals=0 / *.inc branch).
67 merge_inc = SCRATCHDIR / 'extra.inc'
68 merge_inc.write_text(
69 f'[merged]\n\tpath = {dest_m}\n\tread only = no\n'
70 )
71
72 # Main conf: globals carry both directives. write_daemon_conf() emits
73 # `globals` as `key = value`, which is exactly the &include/&merge syntax.
74 conf = write_daemon_conf(
75 [],
76 globals={
77 '&include': str(conf_d),
78 '&merge': str(merge_inc),
79 },
80 name='include-maxconn.conf',
81 )
82 url = start_test_daemon(conf, PORT)
83
84
85 def push(module):
86 return subprocess.run(
87 rsync_argv('-r', f'{src}/', f'{url}{module}/'),
88 capture_output=True, text=True,
89 )
90
91
92 # --- &include directory: both fragment-defined modules must resolve --------
93 for mod, dest in (('incA', dest_a), ('incB', dest_b)):
94 r = push(mod)
95 if r.returncode != 0:
96 test_fail(f"&include dir: module [{mod}] should exist "
97 f"(rc={r.returncode}):\n{r.stderr}")
98 if not any(dest.iterdir()):
99 test_fail(f"&include dir: push to [{mod}] wrote nothing into {dest}")
100
101 # --- &merge file: the [merged] module must resolve -------------------------
102 r = push('merged')
103 if r.returncode != 0:
104 test_fail(f"&merge file: module [merged] should exist "
105 f"(rc={r.returncode}):\n{r.stderr}")
106
107 # --- max connections: lock file was created and the slot taken+released ----
108 if not lockfile.exists():
109 test_fail(f"max connections=2 should have created lock file {lockfile}")
110 # Second sequential push to [incA] reuses (or re-acquires) a slot; this
111 # exercises claim_connection() twice on the same lock file.
112 r = push('incA')
113 if r.returncode != 0:
114 test_fail(f"second push to [incA] (max connections=2) failed "
115 f"(rc={r.returncode}):\n{r.stderr}")
116
117 print(f"daemon-include-maxconn: &include={conf_d.name}/ (2 *.conf, qsort), "
118 f"&merge={merge_inc.name}, max connections=2 lock file ok")