Debian Codegraph @

testsuite/write-batch-quoting_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Regression test: the generated --write-batch replay script must single-quote
3 # every argument -- including an arg that starts with '-' and contains '=' (the
4 # write_arg "--opt=" split) -- so shell metacharacters in a path (backtick or
5 # $(...) command substitution, redirection, newline, ...) cannot execute when
6 # the .sh is run.
7
8 import os
9 import subprocess
10
11 from rsyncfns import SCRATCHDIR, rmtree, run_rsync, test_fail
12
13 base = SCRATCHDIR / 'wb-quoting'
14 rmtree(base)
15 src = base / 'src'
16 src.mkdir(parents=True)
17 (src / 'f').write_text("hello\n")
18
19 # Relative paths so a fired substitution writes its sentinel here, where we run
20 # the generated replay script.
21 os.chdir(base)
22
23
24 def check(label, dest_arg, sentinel_name, *extra):
25 sentinel = base / sentinel_name
26 if sentinel.exists():
27 sentinel.unlink()
28 # check=False: a '-'-leading dest after '--' makes the transfer itself fail,
29 # but write-batch still emits the .sh (the artifact we care about).
30 run_rsync('-a', '--write-batch=B', 'src/', *extra, dest_arg, check=False)
31 batch_sh = base / 'B.sh'
32 if not batch_sh.exists():
33 test_fail(f"{label}: write-batch did not generate the replay script")
34 script = batch_sh.read_text()
35 # Command substitution in any unquoted token fires during shell word
36 # expansion, independent of whether rsync itself runs.
37 subprocess.run(['sh', 'B.sh'],
38 stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
39 cwd=str(base), env=os.environ.copy())
40 if sentinel.exists():
41 test_fail(f"{label}: metacharacters executed when the replay script ran:\n{script}")
42
43
44 # Backtick command substitution in a bare path token (the value-quoting path).
45 # The body uses only `backtick` and `>` -- neither in the old quote-trigger set.
46 check("bare-path", 'd`>PWNED`x/', 'PWNED')
47
48 # Command substitution in the prefix before '=' of a '-'-leading destination,
49 # passed after '--' (exercises the write_arg "--opt=" split). Here `$(` and `)`
50 # would be caught by value-quoting, but the prefix bytes bypass it unless the
51 # whole arg is quoted.
52 check("dash-eq-prefix", '-x$(>PWNED2)=y/', 'PWNED2', '--')
53
54 print("write-batch-quoting: shell metacharacters in batch paths stay quoted")