Debian Codegraph @

testsuite/misc-coverage_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """Coverage: cheap one-off paths no other test reaches.
3
4 usage.c daemon_usage + help-rsyncd.h -- rsync --daemon --help
5 main.c show_malloc_stats /
6 flist.c show_flist_stats -- --info=stats3
7 util2.c sum_as_hex /
8 checksum.c canonical_checksum -- -c --out-format='%C %n'
9 (always_checksum branch) and
10 --out-format='%C %n' alone
11 (ITEM_TRANSFER branch)
12 generator.c start_delete_delay_temp /
13 flush_delete_delay -- --delete-delay with enough
14 queued deletions to overflow
15 the BIGPATHBUFLEN*4 in-memory
16 buffer into the temp-file path
17
18 Each section asserts the observable behaviour, not just exit==0, so the test
19 also guards against regressions in those paths.
20 """
21
22 import re
23 import subprocess
24
25 from rsyncfns import (
26 SCRATCHDIR, makepath, rmtree, rsync_argv, test_fail,
27 )
28
29
30 def run(*argv, ok=(0,)):
31 r = subprocess.run(rsync_argv(*argv), capture_output=True, text=True)
32 if r.returncode not in ok:
33 test_fail(f"rsync {' '.join(argv)} -> rc={r.returncode}\n{r.stderr}")
34 return r
35
36
37 # --- 1. daemon_usage + help-rsyncd.h ----------------------------------------
38 r = run('--daemon', '--help')
39 if '--no-detach' not in r.stdout or 'rsync --daemon' not in r.stdout:
40 test_fail(f"--daemon --help did not emit the daemon usage text:\n{r.stdout}")
41
42
43 # --- 2. show_malloc_stats + show_flist_stats (INFO_GTE(STATS,3)) ------------
44 src = SCRATCHDIR / 'stats-src'
45 dst = SCRATCHDIR / 'stats-dst'
46 rmtree(src); rmtree(dst); makepath(src, dst)
47 for i in range(3):
48 (src / f'f{i}').write_bytes(b'x' * (64 + i))
49 r = run('-r', '--info=stats3', f'{src}/', f'{dst}/')
50 out = r.stdout + r.stderr
51 # show_malloc_stats prints an arena/heap summary (one block per process), but
52 # only on a glibc/mallinfo build -- on the BSDs/macOS show_malloc_stats() is a
53 # no-op (still called, and thus covered, at INFO_GTE(STATS,3), just no output).
54 # show_flist_stats() is currently an empty stub.
55 import platform
56 if platform.system() == 'Linux':
57 if 'heap statistics' not in out:
58 test_fail(f"--info=stats3 did not emit malloc stats:\n{out}")
59 # One block per process: sender + server receiver + server generator.
60 if out.count('heap statistics') < 3:
61 test_fail(f"--info=stats3 emitted fewer than 3 heap-stat blocks:\n{out}")
62
63
64 # --- 3. sum_as_hex + canonical_checksum (--out-format=%C) --------------------
65 csrc = SCRATCHDIR / 'csum-src'
66 cdst = SCRATCHDIR / 'csum-dst'
67 rmtree(csrc); rmtree(cdst); makepath(csrc, cdst)
68 payload = b'hello, checksum coverage\n'
69 (csrc / 'a').write_bytes(payload)
70 (csrc / 'b').write_bytes(payload + b'!')
71
72 # The file/transfer checksum algorithm is negotiated (xxh128/xxh3/md5/...
73 # depending on build + protocol), so don't assert which -- just that %C emits
74 # a per-file hex digest of plausible length, and that DIFFERENT file content
75 # produces a DIFFERENT digest (i.e. it's the actual sum, not a placeholder).
76 HEX = re.compile(r'^([0-9a-f]{16,128}) (\S+)$', re.M)
77
78 def check_C(out, label):
79 # Two valid outcomes: hex digests (canonical csum -> sum_as_hex returns the
80 # hex string) or a run of spaces (non-canonical, e.g. md4 at --protocol=29
81 # -> sum_as_hex returns NULL and log.c emits sum_len*2 spaces). Either way
82 # sum_as_hex/canonical_checksum were CALLED, which is the coverage goal.
83 sums = dict((m[2], m[1]) for m in HEX.finditer(out))
84 if sums:
85 if 'a' not in sums or 'b' not in sums or sums['a'] == sums['b']:
86 test_fail(f"{label}: per-file digests not distinct:\n{out!r}")
87 return 'hex'
88 if not re.search(r'^ {8,} a$', out, re.M):
89 test_fail(f"{label}: emitted neither a hex digest nor the "
90 f"non-canonical-csum spaces fallback:\n{out!r}")
91 return 'spaces'
92
93 # 3a. always_checksum branch (log.c:707): with -c, %C encodes F_SUM(file).
94 r = run('-rc', '--out-format=%C %n', f'{csrc}/', f'{cdst}/')
95 kind_a = check_C(r.stdout, '-c %C')
96
97 # 3b. ITEM_TRANSFER branch (log.c:709): no -c, %C encodes sender_file_sum.
98 rmtree(cdst); makepath(cdst)
99 r = run('-r', '--out-format=%C %n', f'{csrc}/', f'{cdst}/')
100 kind_b = check_C(r.stdout, '%C (no -c)')
101
102
103 # --- 4. start_delete_delay_temp + flush_delete_delay ------------------------
104 # deldelay_size = BIGPATHBUFLEN*4 = 20480. Each remember_delete() entry is
105 # "%x %s\0" -> ~6 + len(name). 300 files with 80-char names -> ~25 KB,
106 # guaranteed to overflow the in-memory buffer and spill to the temp file.
107 ddsrc = SCRATCHDIR / 'dd-src'
108 dddst = SCRATCHDIR / 'dd-dst'
109 rmtree(ddsrc); rmtree(dddst); makepath(ddsrc, dddst)
110 (ddsrc / 'keep').write_bytes(b'k')
111 NAME = 'D' * 80
112 N = 300
113 for i in range(N):
114 (dddst / f'{NAME}{i:04d}').write_bytes(b'x')
115 r = run('-r', '--delete-delay', f'{ddsrc}/', f'{dddst}/')
116 remaining = [p for p in dddst.iterdir() if p.name.startswith(NAME)]
117 if remaining:
118 test_fail(f"--delete-delay left {len(remaining)}/{N} files "
119 f"after the temp-file spill path")
120 if not (dddst / 'keep').is_file():
121 test_fail("--delete-delay lost the kept file")
122
123
124 print(f"misc-coverage: --daemon --help, --info=stats3, "
125 f"%C ({kind_a}/{kind_b}), --delete-delay temp-file spill ({N} entries) ok")