Files
rsync/testsuite/protected-regular_test.py
Andrew Tridgell 1f689ec0c2 testsuite: rewrite the shell testsuite in Python
Replace the entire shell-based testsuite with Python. runtests.py
already drove the suite (it had replaced runtests.sh earlier); this
converts all 60 test scripts from *.test shell to *_test.py and adds
testsuite/rsyncfns.py as the shared helper module -- the Python
counterpart of the now-removed rsync.fns.

runtests.py:
  * Discovers and runs both *.test and *_test.py; dispatches the
    Python tests via the same python3 that runs the harness.
  * Extends PYTHONPATH so tests can `import rsyncfns`.

testsuite/rsyncfns.py provides everything the ports need:
  * environment wiring (scratchdir / srcdir / TOOLDIR / RSYNC /
    TLS_ARGS, and HOME pointed at the per-test scratch dir);
  * result reporting -- test_fail / test_skipped / test_xfail mapping
    to the 0 / 1 / 77 / 78 exit-code convention;
  * the transfer-and-verify helpers checkit, checkdiff, verify_dirs,
    rsync_ls_lR, check_perms and the v_filt output filter;
  * fixture builders hands_setup, build_symlinks, build_rsyncd_conf,
    make_data_file, cp_p / cp_touch, makepath / rmtree.

All 60 tests are converted, including the four split-variant tests
that share one source via a Makefile-built symlink (chown/chown-fake,
devices/devices-fake, xattrs/xattrs-hlink, exclude/exclude-lsh);
Makefile.in's CHECK_SYMLINKS now points at the *_test.py names.

The dead rsync.fns shell library is removed.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-22 14:34:52 +10:00

74 lines
2.3 KiB
Python

#!/usr/bin/env python3
# Python rewrite of testsuite/protected-regular.test.
#
# Modern Linux kernels can set fs.protected_regular = {1,2}, which
# blocks O_CREAT|O_WRONLY opens of files in world-writable sticky
# directories that the opener doesn't own. rsync --inplace must still
# be able to write into these files; this test guards that path.
import os
import shutil
import subprocess
import sys
from pathlib import Path
from rsyncfns import TMPDIR, run_rsync, test_skipped
pr_path = Path('/proc/sys/fs/protected_regular')
if not pr_path.is_file():
test_skipped("Can't find protected_regular setting (only available on Linux)")
try:
pr_lvl = pr_path.read_text().strip()
except OSError:
test_skipped("Can't check if fs.protected_regular is enabled")
if pr_lvl == '0':
test_skipped("fs.protected_regular is not enabled")
workdir = TMPDIR / 'files'
workdir.mkdir(parents=True, exist_ok=True)
os.chmod(workdir, 0o1777)
(workdir / 'src').write_text("Source\n")
(workdir / 'dst').write_text("")
def _chown_5001(path: Path) -> bool:
"""Try to chown(2) `path` to uid 5001. Returns True on success."""
try:
os.chown(path, 5001, -1)
return True
except PermissionError:
return False
if not _chown_5001(workdir / 'dst'):
# Not root: fall back to re-running ourselves under unshare with a
# uid mapping (Linux user-namespace trick). Only attempt once.
if not os.environ.get('RSYNC_UNSHARED'):
unshare = shutil.which('unshare')
if unshare is not None:
probe = subprocess.run(
[unshare, '--user', '--map-root-user',
'--map-users', '5001:100000:1', 'true'],
capture_output=True,
)
if probe.returncode == 0:
print("Re-running under unshare with UID mapping...")
env = os.environ.copy()
env['RSYNC_UNSHARED'] = '1'
os.execvpe(
unshare,
[unshare, '--user', '--map-root-user',
'--map-users', '5001:100000:1',
sys.executable, __file__],
env,
)
test_skipped("Can't chown (need root or unshare with uidmap)")
print(f"Contents of {workdir}:")
subprocess.run(['ls', '-al', str(workdir)])
run_rsync('--inplace', str(workdir / 'src'), str(workdir / 'dst'))