2 from __future__ import absolute_import, print_function
3 import errno, glob, grp, pwd, stat, tempfile, subprocess
7 from bup import git, metadata
9 from bup.compat import range
10 from bup.helpers import clear_errors, detect_fakeroot, is_superuser, resolve_parent
11 from bup.repo import LocalRepo
12 from bup.xstat import utime, lutime
13 from buptest import no_lingering_errors, test_tempdir
14 import bup.helpers as helpers
18 bup_path = top_dir + b'/bup'
19 start_dir = os.getcwd()
24 cmd_str = b' '.join(cmd)
25 print(cmd_str, file=sys.stderr)
26 rc = subprocess.call(cmd)
28 print('terminated by signal', - rc, file=sys.stderr)
31 print('returned exit status', rc, file=sys.stderr)
34 print('subprocess call failed:', e, file=sys.stderr)
39 assert(sys.platform.startswith('linux'))
40 # Set up testfs with user_xattr, etc.
41 if subprocess.call([b'modprobe', b'loop']) != 0:
43 subprocess.call([b'umount', b'testfs'])
44 ex(b'dd', b'if=/dev/zero', b'of=testfs.img', b'bs=1M', b'count=32')
45 ex(b'mke2fs', b'-F', b'-j', b'-m', b'0', b'testfs.img')
46 ex(b'rm', b'-rf', b'testfs')
48 ex(b'mount', b'-o', b'loop,acl,user_xattr', b'testfs.img', b'testfs')
49 # Hide, so that tests can't create risks.
50 os.chown(b'testfs', 0, 0)
51 os.chmod(b'testfs', 0o700)
56 subprocess.call([b'umount', b'testfs'])
57 helpers.unlink(b'testfs.img')
61 def test_clean_up_archive_path():
62 with no_lingering_errors():
63 cleanup = metadata._clean_up_path_for_archive
64 WVPASSEQ(cleanup(b'foo'), b'foo')
65 WVPASSEQ(cleanup(b'/foo'), b'foo')
66 WVPASSEQ(cleanup(b'///foo'), b'foo')
67 WVPASSEQ(cleanup(b'/foo/bar'), b'foo/bar')
68 WVPASSEQ(cleanup(b'foo/./bar'), b'foo/bar')
69 WVPASSEQ(cleanup(b'/foo/./bar'), b'foo/bar')
70 WVPASSEQ(cleanup(b'/foo/./bar/././baz'), b'foo/bar/baz')
71 WVPASSEQ(cleanup(b'/foo/./bar///././baz'), b'foo/bar/baz')
72 WVPASSEQ(cleanup(b'//./foo/./bar///././baz/.///'), b'foo/bar/baz/')
73 WVPASSEQ(cleanup(b'./foo/./.bar'), b'foo/.bar')
74 WVPASSEQ(cleanup(b'./foo/.'), b'foo')
75 WVPASSEQ(cleanup(b'./foo/..'), b'.')
76 WVPASSEQ(cleanup(b'//./..//.../..//.'), b'.')
77 WVPASSEQ(cleanup(b'//./..//..././/.'), b'...')
78 WVPASSEQ(cleanup(b'/////.'), b'.')
79 WVPASSEQ(cleanup(b'/../'), b'.')
80 WVPASSEQ(cleanup(b''), b'.')
84 def test_risky_path():
85 with no_lingering_errors():
86 risky = metadata._risky_path
87 WVPASS(risky(b'/foo'))
88 WVPASS(risky(b'///foo'))
89 WVPASS(risky(b'/../foo'))
90 WVPASS(risky(b'../foo'))
91 WVPASS(risky(b'foo/..'))
92 WVPASS(risky(b'foo/../'))
93 WVPASS(risky(b'foo/../bar'))
95 WVFAIL(risky(b'foo/'))
96 WVFAIL(risky(b'foo///'))
97 WVFAIL(risky(b'./foo'))
98 WVFAIL(risky(b'foo/.'))
99 WVFAIL(risky(b'./foo/.'))
100 WVFAIL(risky(b'foo/bar'))
101 WVFAIL(risky(b'foo/./bar'))
105 def test_clean_up_extract_path():
106 with no_lingering_errors():
107 cleanup = metadata._clean_up_extract_path
108 WVPASSEQ(cleanup(b'/foo'), b'foo')
109 WVPASSEQ(cleanup(b'///foo'), b'foo')
110 WVFAIL(cleanup(b'/../foo'))
111 WVFAIL(cleanup(b'../foo'))
112 WVFAIL(cleanup(b'foo/..'))
113 WVFAIL(cleanup(b'foo/../'))
114 WVFAIL(cleanup(b'foo/../bar'))
115 WVPASSEQ(cleanup(b'foo'), b'foo')
116 WVPASSEQ(cleanup(b'foo/'), b'foo/')
117 WVPASSEQ(cleanup(b'foo///'), b'foo///')
118 WVPASSEQ(cleanup(b'./foo'), b'./foo')
119 WVPASSEQ(cleanup(b'foo/.'), b'foo/.')
120 WVPASSEQ(cleanup(b'./foo/.'), b'./foo/.')
121 WVPASSEQ(cleanup(b'foo/bar'), b'foo/bar')
122 WVPASSEQ(cleanup(b'foo/./bar'), b'foo/./bar')
123 WVPASSEQ(cleanup(b'/'), b'.')
124 WVPASSEQ(cleanup(b'./'), b'./')
125 WVPASSEQ(cleanup(b'///foo/bar'), b'foo/bar')
126 WVPASSEQ(cleanup(b'///foo/bar'), b'foo/bar')
130 def test_metadata_method():
131 with no_lingering_errors():
132 with test_tempdir(b'bup-tmetadata-') as tmpdir:
133 bup_dir = tmpdir + b'/bup'
134 data_path = tmpdir + b'/foo'
136 ex(b'touch', data_path + b'/file')
137 ex(b'ln', b'-s', b'file', data_path + b'/symlink')
138 test_time1 = 13 * 1000000000
139 test_time2 = 42 * 1000000000
140 utime(data_path + b'/file', (0, test_time1))
141 lutime(data_path + b'/symlink', (0, 0))
142 utime(data_path, (0, test_time2))
143 ex(bup_path, b'-d', bup_dir, b'init')
144 ex(bup_path, b'-d', bup_dir, b'index', b'-v', data_path)
145 ex(bup_path, b'-d', bup_dir, b'save', b'-tvvn', b'test', data_path)
146 git.check_repo_or_die(bup_dir)
148 resolved = vfs.resolve(repo,
149 b'/test/latest' + resolve_parent(data_path),
151 leaf_name, leaf_item = resolved[-1]
153 WVPASS(m.mtime == test_time2)
154 WVPASS(leaf_name == b'foo')
155 contents = tuple(vfs.contents(repo, leaf_item))
156 WVPASS(len(contents) == 3)
157 WVPASSEQ(frozenset(name for name, item in contents),
158 frozenset((b'.', b'file', b'symlink')))
159 for name, item in contents:
162 WVPASS(m.mtime == test_time1)
163 elif name == b'symlink':
165 WVPASSEQ(m.symlink_target, b'file')
171 if helpers.saved_errors:
172 return str(helpers.saved_errors[0])
177 def test_from_path_error():
178 if is_superuser() or detect_fakeroot():
180 with no_lingering_errors():
181 with test_tempdir(b'bup-tmetadata-') as tmpdir:
182 path = tmpdir + b'/foo'
184 m = metadata.from_path(path, archive_path=path, save_symlinks=True)
185 WVPASSEQ(m.path, path)
186 os.chmod(path, 0o000)
187 metadata.from_path(path, archive_path=path, save_symlinks=True)
188 if metadata.get_linux_file_attr:
189 print('saved_errors:', helpers.saved_errors, file=sys.stderr)
190 WVPASS(len(helpers.saved_errors) == 1)
191 errmsg = _first_err()
192 WVPASS(errmsg.startswith('read Linux attr'))
196 def _linux_attr_supported(path):
197 # Expects path to denote a regular file or a directory.
198 if not metadata.get_linux_file_attr:
201 metadata.get_linux_file_attr(path)
203 if e.errno in (errno.ENOTTY, errno.ENOSYS, errno.EOPNOTSUPP):
211 def test_apply_to_path_restricted_access():
212 if is_superuser() or detect_fakeroot():
214 if sys.platform.startswith('cygwin'):
215 return # chmod 000 isn't effective.
216 with no_lingering_errors():
217 with test_tempdir(b'bup-tmetadata-') as tmpdir:
218 parent = tmpdir + b'/foo'
219 path = parent + b'/bar'
225 metadata.xattr.set(path, b'user.buptest', b'bup')
227 print("failed to set test xattr")
228 # ignore any failures here - maybe FS cannot do it
230 m = metadata.from_path(path, archive_path=path, save_symlinks=True)
231 WVPASSEQ(m.path, path)
232 os.chmod(parent, 0o000)
233 m.apply_to_path(path)
234 print('saved_errors:', helpers.saved_errors, file=sys.stderr)
235 expected_errors = ['utime: ']
236 if m.linux_attr and _linux_attr_supported(tmpdir):
237 expected_errors.append('Linux chattr: ')
238 if metadata.xattr and m.linux_xattr:
239 expected_errors.append("xattr.set ")
240 WVPASS(len(helpers.saved_errors) == len(expected_errors))
241 for i in range(len(expected_errors)):
242 WVPASS(str(helpers.saved_errors[i]).startswith(expected_errors[i]))
247 def test_restore_over_existing_target():
248 with no_lingering_errors():
249 with test_tempdir(b'bup-tmetadata-') as tmpdir:
250 path = tmpdir + b'/foo'
252 dir_m = metadata.from_path(path, archive_path=path, save_symlinks=True)
254 open(path, 'w').close()
255 file_m = metadata.from_path(path, archive_path=path, save_symlinks=True)
256 # Restore dir over file.
257 WVPASSEQ(dir_m.create_path(path, create_symlinks=True), None)
258 WVPASS(stat.S_ISDIR(os.stat(path).st_mode))
259 # Restore dir over dir.
260 WVPASSEQ(dir_m.create_path(path, create_symlinks=True), None)
261 WVPASS(stat.S_ISDIR(os.stat(path).st_mode))
262 # Restore file over dir.
263 WVPASSEQ(file_m.create_path(path, create_symlinks=True), None)
264 WVPASS(stat.S_ISREG(os.stat(path).st_mode))
265 # Restore file over file.
266 WVPASSEQ(file_m.create_path(path, create_symlinks=True), None)
267 WVPASS(stat.S_ISREG(os.stat(path).st_mode))
268 # Restore file over non-empty dir.
271 open(path + b'/bar', 'w').close()
272 WVEXCEPT(Exception, file_m.create_path, path, create_symlinks=True)
273 # Restore dir over non-empty dir.
274 os.remove(path + b'/bar')
275 os.mkdir(path + b'/bar')
276 WVEXCEPT(Exception, dir_m.create_path, path, create_symlinks=True)
279 from bup.metadata import read_acl
282 def POSIX1E_ACL_SUPPORT_IS_MISSING():
286 from bup.metadata import xattr
288 def remove_selinux(attrs):
289 return list(filter(lambda i: not i in (b'security.selinux', ),
293 def test_handling_of_incorrect_existing_linux_xattrs():
294 if not is_superuser() or detect_fakeroot():
295 WVMSG('skipping test -- not superuser')
297 if not setup_testfs():
298 WVMSG('unable to load loop module; skipping dependent tests')
300 for f in glob.glob(b'testfs/*'):
303 open(path, 'w').close()
304 xattr.set(path, b'foo', b'bar', namespace=xattr.NS_USER)
305 m = metadata.from_path(path, archive_path=path, save_symlinks=True)
306 xattr.set(path, b'baz', b'bax', namespace=xattr.NS_USER)
307 m.apply_to_path(path, restore_numeric_ids=False)
308 WVPASSEQ(remove_selinux(xattr.list(path)), [b'user.foo'])
309 WVPASSEQ(xattr.get(path, b'user.foo'), b'bar')
310 xattr.set(path, b'foo', b'baz', namespace=xattr.NS_USER)
311 m.apply_to_path(path, restore_numeric_ids=False)
312 WVPASSEQ(remove_selinux(xattr.list(path)), [b'user.foo'])
313 WVPASSEQ(xattr.get(path, b'user.foo'), b'bar')
314 xattr.remove(path, b'foo', namespace=xattr.NS_USER)
315 m.apply_to_path(path, restore_numeric_ids=False)
316 WVPASSEQ(remove_selinux(xattr.list(path)), [b'user.foo'])
317 WVPASSEQ(xattr.get(path, b'user.foo'), b'bar')