| 1 |
|
| 2 |
"""Daemon coverage: exclude, incoming chmod, outgoing chmod (at depth). |
| 3 |
|
| 4 |
A daemon-side exclude must hide matching files everywhere in the module tree; |
| 5 |
incoming/outgoing chmod must rewrite the permissions of every transferred file, |
| 6 |
including ones several levels deep. |
| 7 |
""" |
| 8 |
|
| 9 |
import os |
| 10 |
import subprocess |
| 11 |
|
| 12 |
from rsyncfns import ( |
| 13 |
FROMDIR, SCRATCHDIR, |
| 14 |
assert_mode, assert_not_exists, assert_same, make_tree, makepath, rmtree, |
| 15 |
rsync_argv, start_test_daemon, test_fail, walk_files, write_daemon_conf, |
| 16 |
) |
| 17 |
|
| 18 |
DAEMON_PORT = 12887 |
| 19 |
|
| 20 |
src = FROMDIR |
| 21 |
rmtree(src) |
| 22 |
make_tree(src, depth=3) |
| 23 |
(src / 'a.secret').write_text('s\n') |
| 24 |
(src / 'd1' / 'd2' / 'b.secret').write_text('s\n') |
| 25 |
rels = [p.relative_to(src) for p in walk_files(src)] |
| 26 |
|
| 27 |
incdir = SCRATCHDIR / 'incdest' |
| 28 |
for d in (incdir,): |
| 29 |
rmtree(d) |
| 30 |
makepath(incdir) |
| 31 |
|
| 32 |
conf = write_daemon_conf([ |
| 33 |
('filt', {'path': src, 'read only': 'yes', 'exclude': '*.secret'}), |
| 34 |
('inc', {'path': incdir, 'read only': 'no', 'incoming chmod': 'F600'}), |
| 35 |
('out', {'path': src, 'read only': 'yes', 'outgoing chmod': 'Fg-r,Fo-r'}), |
| 36 |
]) |
| 37 |
url = start_test_daemon(conf, DAEMON_PORT) |
| 38 |
|
| 39 |
|
| 40 |
def pull(mod, dest): |
| 41 |
rmtree(dest) |
| 42 |
makepath(dest) |
| 43 |
proc = subprocess.run(rsync_argv('-a', f'{url}{mod}/', f'{dest}/'), |
| 44 |
stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, |
| 45 |
text=True) |
| 46 |
if proc.returncode not in (0, 23): |
| 47 |
test_fail(f"pull from {mod} failed (rc={proc.returncode}): {proc.stderr}") |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
fp = SCRATCHDIR / 'filtpull' |
| 52 |
pull('filt', fp) |
| 53 |
assert_not_exists(fp / 'a.secret', label='daemon exclude top') |
| 54 |
assert_not_exists(fp / 'd1' / 'd2' / 'b.secret', label='daemon exclude deep') |
| 55 |
assert_same(fp / 'd1' / 'd2' / 'f2', src / 'd1' / 'd2' / 'f2', |
| 56 |
label='daemon exclude kept others') |
| 57 |
|
| 58 |
|
| 59 |
proc = subprocess.run(rsync_argv('-a', f'{src}/', f'{url}inc/'), |
| 60 |
stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, |
| 61 |
text=True) |
| 62 |
if proc.returncode not in (0, 23): |
| 63 |
test_fail(f"incoming push failed (rc={proc.returncode}): {proc.stderr}") |
| 64 |
checked = 0 |
| 65 |
for rel in rels: |
| 66 |
p = incdir / rel |
| 67 |
if p.is_file(): |
| 68 |
assert_mode(p, 0o600, label=f'incoming chmod {rel}') |
| 69 |
checked += 1 |
| 70 |
if checked == 0: |
| 71 |
test_fail("incoming chmod test transferred no files") |
| 72 |
|
| 73 |
|
| 74 |
op = SCRATCHDIR / 'outpull' |
| 75 |
pull('out', op) |
| 76 |
checked = 0 |
| 77 |
for rel in rels: |
| 78 |
p = op / rel |
| 79 |
if not p.is_file(): |
| 80 |
continue |
| 81 |
checked += 1 |
| 82 |
if os.stat(p).st_mode & 0o044: |
| 83 |
test_fail(f"outgoing chmod did not clear group/other read on {rel}: " |
| 84 |
f"{oct(os.stat(p).st_mode & 0o777)}") |
| 85 |
if checked == 0: |
| 86 |
test_fail("outgoing chmod test pulled no files (loop was vacuous)") |
| 87 |
|
| 88 |
print("daemon-filter: exclude / incoming chmod / outgoing chmod verified at depth") |