Debian Codegraph @

testsuite/ki58-log-format-percent_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """Regression test: `%%` in --out-format outputs a literal `%`.
3
4 # Verifies: SW-REQ-166
5
6 The log_formatted() switch in log.c had no `case '%'`, so `%%` left the
7 first `%` in place and re-parsed the second `%` as the start of a new
8 specifier. This test transfers a file with `--out-format='100%% done %f'`
9 and asserts the output contains `100% done <filename>` (a single literal
10 percent, a space, the word "done", and the expanded filename).
11 """
12
13 import subprocess
14
15 from rsyncfns import (
16 FROMDIR, TODIR,
17 forced_protocol, makepath, rmtree, rsync_argv, test_fail,
18 )
19
20 src = FROMDIR
21
22 # Build a single-file source tree.
23 rmtree(src)
24 rmtree(TODIR)
25 makepath(src)
26 (src / 'percentfile').write_text("data\n")
27
28 # Transfer using a format string with both %% (literal percent) and %f (name).
29 p = subprocess.run(
30 rsync_argv('-a', '--out-format=100%% done %f', f'{src}/', f'{TODIR}/'),
31 capture_output=True, text=True,
32 )
33 if p.returncode != 0:
34 test_fail(f"rsync exited {p.returncode}:\n{p.stderr}")
35
36 # %f expands to the transfer-relative path, which includes leading directories
37 # when the source is addressed by an absolute path (as here) -- so don't pin the
38 # exact filename. The point of this test is the escape: "100% done " with a
39 # single literal percent followed by a space (a broken %% consumes the space or
40 # leaves a doubled percent), plus the transferred file name somewhere.
41 if '100% done ' not in p.stdout or 'percentfile' not in p.stdout:
42 test_fail(
43 "expected '100% done ' (a single literal percent) and 'percentfile' in "
44 f"--out-format output, got:\n{p.stdout}"
45 )
46
47 # log_format_has() must not see the 'i' in '%%i' as an itemize escape: the
48 # second '%' of a '%%' is a literal, not the start of a new specifier. A
49 # misdetection turns on itemizing, which logs attribute-only changes that a
50 # plain --out-format run must not mention.
51 (src / 'percentfile').chmod(0o640)
52 p = subprocess.run(
53 rsync_argv('-a', '--out-format=%%i %n', f'{src}/', f'{TODIR}/'),
54 capture_output=True, text=True,
55 )
56 if p.returncode != 0:
57 test_fail(f"rsync exited {p.returncode}:\n{p.stderr}")
58 (src / 'percentfile').chmod(0o600)
59 p = subprocess.run(
60 rsync_argv('-a', '--out-format=%%i %n', f'{src}/', f'{TODIR}/'),
61 capture_output=True, text=True,
62 )
63 if p.returncode != 0:
64 test_fail(f"rsync exited {p.returncode}:\n{p.stderr}")
65 if 'percentfile' in p.stdout:
66 test_fail(
67 "'%%i' misdetected as itemizing: an attribute-only change was logged "
68 f"by --out-format='%%i %n':\n{p.stdout}"
69 )
70
71 # And '%%i' on a really transferred file still renders as literal '%i'.
72 (src / 'percentfile').write_text("data2\n")
73 p = subprocess.run(
74 rsync_argv('-a', '--out-format=%%i %n', f'{src}/', f'{TODIR}/'),
75 capture_output=True, text=True,
76 )
77 if p.returncode != 0:
78 test_fail(f"rsync exited {p.returncode}:\n{p.stderr}")
79 if '%i percentfile' not in p.stdout:
80 test_fail(
81 "expected literal '%i percentfile' for a transferred file with "
82 f"--out-format='%%i %n', got:\n{p.stdout}"
83 )
84
85 # log_format_has() and log_formatted() must agree on where an escape letter is
86 # even for an over-long field width. log_formatted() stops consuming width
87 # digits at its 32-byte fmt buffer, then rescans and expands the trailing %C;
88 # an unbounded log_format_has() instead swallows every digit and the following
89 # '%' as a literal %%, missing the %C. With -c that leaves sender_keeps_checksum
90 # unset while %C still reads F_SUM -- a heap over-read (ASan: use-after-poison in
91 # sum_as_hex), and the rendered digest is garbage.
92 #
93 # Oracle without a sanitizer: the digest %C renders for the over-wide format
94 # must equal the digest a plain '%C' renders for the same file. A parser
95 # divergence leaves the wide-format sender without a retained checksum, so its
96 # %C differs (or is garbage) while the plain %C is correct.
97 import re
98
99 def checksum_for(fmt):
100 (src / 'percentfile').write_text("data3\n")
101 rmtree(TODIR)
102 r = subprocess.run(
103 rsync_argv('-a', '-c', f'--out-format={fmt} %n', f'{src}/', f'{TODIR}/'),
104 capture_output=True, text=True,
105 )
106 if r.returncode != 0:
107 test_fail(f"rsync -c exited {r.returncode} for --out-format={fmt!r}:\n{r.stderr}")
108 m = re.search(r'([0-9a-f]+) percentfile', r.stdout)
109 if not m:
110 test_fail(f"no '<hex> percentfile' in --out-format={fmt!r} output:\n{r.stdout}")
111 return m.group(1)
112
113 # The over-wide %C check needs %C to actually render a hex digest, which only
114 # happens for a canonical checksum: at protocol < 30 the negotiated file checksum
115 # is a non-canonical MD4 variant and %C (via sum_as_hex) renders empty, so there
116 # is no digest to compare and no F_SUM read to over-run. Skip that sub-case
117 # there; at protocol 30+ checksum_for() still fails on a missing digest, so a
118 # real regression is caught. The %% literal checks above are protocol-independent.
119 proto = forced_protocol()
120 if proto is None or proto >= 30:
121 # The over-wide format renders its width digits literally, so the digest is
122 # the trailing run of hex; compare that tail against the plain %C digest.
123 plain = checksum_for('%C')
124 wide = checksum_for('%' + '0' * 30 + '%C')
125 if not wide.endswith(plain):
126 test_fail(
127 f"over-wide %C rendered ...{wide[-len(plain):]!r} but plain %C rendered "
128 f"{plain!r}: log_format_has()/log_formatted() disagree on the escape position"
129 )
130
131 print("ki58-log-format-percent: %% literal-percent escape verified")