Debian Codegraph @

testsuite/devices_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Python rewrite of testsuite/devices.test (and, via a Makefile-built
3 # symlink, of devices-fake.test).
4 #
5 # Test rsync's handling of device nodes (char/block/fifo) plus the
6 # fake-super variant that encodes device numbers in the
7 # user.rsync.%stat xattr instead of mknod-ing real devices.
8
9 import os
10 import sys
11
12 import rsyncfns
13 from rsyncfns import (
14 CHKDIR, CHKFILE, FROMDIR, OUTFILE, RSYNC_PREFIX, TMPDIR, TODIR,
15 all_plus, allspace, dots,
16 checkdiff, hands_setup, makepath, rsync_ls_lR, run_rsync,
17 test_fail, test_skipped, v_filt, xattr_set, xattrs_supported,
18 )
19
20
21 script_name = os.path.basename(sys.argv[0] if sys.argv[0] else __file__)
22 fake_variant = 'fake' in script_name
23
24 if fake_variant:
25 if not xattrs_supported():
26 test_skipped("Rsync needs xattrs for fake device tests")
27
28 rsyncfns.RSYNC = rsyncfns.RSYNC + ' --fake-super'
29 rsyncfns.TLS_ARGS = (rsyncfns.TLS_ARGS + ' --fake-super').strip()
30
31 def make_special(path, kind: str, major: int = 0, minor: int = 0) -> bool:
32 """Pretend to mknod `path` as kind {'p','c','b'} via rsync's
33 fake-super "%stat" xattr (name/namespace handled per-OS by
34 xattr_set). Returns True on success, False if the FS rejects it.
35 """
36 mode = {'p': 0o10644, 'c': 0o20644, 'b': 0o60644}[kind]
37 try:
38 with open(path, 'w'):
39 pass
40 xattr_set(f'{RSYNC_PREFIX}.%stat',
41 f"{mode:o} {major},{minor} 0:0", path)
42 return True
43 except OSError:
44 return False
45 else:
46 my_uid = os.getuid()
47 if my_uid != 0:
48 # Try fakeroot, mirroring the shell test.
49 fakeroot_path = os.environ.get('FAKEROOT_PATH')
50 if fakeroot_path and os.access(fakeroot_path, os.X_OK):
51 print("Let's try re-running the script under fakeroot...")
52 os.execv(fakeroot_path, [fakeroot_path, sys.executable, __file__])
53 test_skipped("Rsync needs root/fakeroot for device tests")
54
55 def make_special(path, kind: str, major: int = 0, minor: int = 0) -> bool:
56 try:
57 if kind == 'p':
58 os.mkfifo(path)
59 else:
60 mode = 0o644 | (0o020000 if kind == 'c' else 0o060000)
61 os.mknod(path, mode, os.makedev(major, minor))
62 return True
63 except OSError:
64 return False
65
66
67 # Does this build of rsync support hard-linking specials?
68 vv = run_rsync('-VV', check=True, capture_output=True)
69 can_hlink_special = '"hardlink_specials": true' in vv.stdout
70
71 FROMDIR.mkdir(parents=True, exist_ok=True)
72 TODIR.mkdir(parents=True, exist_ok=True)
73
74 if not make_special(FROMDIR / 'char', 'c', 41, 67):
75 test_skipped("Can't create char device node")
76 if not make_special(FROMDIR / 'char2', 'c', 42, 68):
77 test_skipped("Can't create char device node")
78 if not make_special(FROMDIR / 'char3', 'c', 42, 69):
79 test_skipped("Can't create char device node")
80 if not make_special(FROMDIR / 'block', 'b', 42, 69):
81 test_skipped("Can't create block device node")
82 if not make_special(FROMDIR / 'block2', 'b', 42, 73):
83 test_skipped("Can't create block device node")
84 if not make_special(FROMDIR / 'block3', 'b', 105, 73):
85 test_skipped("Can't create block device node")
86
87 if can_hlink_special:
88 try:
89 os.link(FROMDIR / 'block3', FROMDIR / 'block3.5')
90 except OSError:
91 # The shell test prints a "Skipping hard-linked device test..." line
92 # when it can't link the device; let it slide here, too.
93 print("Skipping hard-linked device test... (link failed)")
94 can_hlink_special = False
95 else:
96 print("Skipping hard-linked device test...")
97
98 if not make_special(FROMDIR / 'fifo', 'p'):
99 test_skipped("Can't run mkfifo")
100
101 # Match block/block2 timestamps so the diff doesn't drift.
102 ref = (FROMDIR / 'block').stat()
103 os.utime(FROMDIR / 'block', (ref.st_atime, ref.st_mtime), follow_symlinks=False)
104 os.utime(FROMDIR / 'block2', (ref.st_atime, ref.st_mtime), follow_symlinks=False)
105
106 checkdiff(['-ai', f'{FROMDIR}/block', f'{TODIR}/block2'],
107 f"cD{all_plus} block\n")
108
109 checkdiff(['-ai', f'{FROMDIR}/block2', f'{TODIR}/block'],
110 f"cD{all_plus} block2\n")
111
112 import time
113 time.sleep(1)
114
115 checkdiff(['-Di', f'{FROMDIR}/block3', f'{TODIR}/block'],
116 f"cDc.T.{dots} block3\n")
117
118 # Build the expected -aiHvv listing.
119 chkfile_lines = [
120 f".d..t.{dots} ./",
121 f"cDc.t.{dots} block",
122 f"cDc...{dots} block2",
123 f"cD{all_plus} block3",
124 ]
125 if can_hlink_special:
126 chkfile_lines.append(f"hD{all_plus} block3.5 => block3")
127 chkfile_lines += [
128 f"cD{all_plus} char",
129 f"cD{all_plus} char2",
130 f"cD{all_plus} char3",
131 f"cS{all_plus} fifo",
132 ]
133 expected = '\n'.join(chkfile_lines) + '\n'
134
135 checkdiff(['-aiHvv', f'{FROMDIR}/', f'{TODIR}/'], expected, filter=v_filt)
136
137 print("check how the directory listings compare with diff:\n")
138 ls_from = rsync_ls_lR(FROMDIR)
139 ls_to = rsync_ls_lR(TODIR)
140 if ls_from != ls_to:
141 from difflib import unified_diff
142 sys.stdout.write(''.join(unified_diff(
143 ls_from.splitlines(keepends=True),
144 ls_to.splitlines(keepends=True),
145 fromfile='from', tofile='to',
146 )))
147 test_fail("from/to listings differ after device transfer")
148
149 if can_hlink_special:
150 expected = (
151 f"created directory {CHKDIR}\n"
152 f"cd{allspace} ./\n"
153 f"hD{allspace} block\n"
154 f"hD{allspace} block2\n"
155 f"hD{allspace} block3\n"
156 f"hD{allspace} block3.5\n"
157 f"hD{allspace} char\n"
158 f"hD{allspace} char2\n"
159 f"hD{allspace} char3\n"
160 f"hS{allspace} fifo\n"
161 )
162 checkdiff(['-aii', f'--link-dest={TODIR}',
163 f'{FROMDIR}/', f'{CHKDIR}/'], expected)