2 from __future__ import absolute_import, print_function
3 import errno, glob, stat, subprocess
9 from bup import git, metadata
11 from bup.helpers import clear_errors, detect_fakeroot, is_superuser, resolve_parent
12 from bup.repo import LocalRepo
13 from bup.xstat import utime, lutime
14 import bup.helpers as helpers
15 from bup.compat import fsencode
17 lib_t_dir = os.path.dirname(fsencode(__file__))
19 top_dir = os.path.realpath(os.path.join(lib_t_dir, b'..', b'..'))
21 bup_path = top_dir + b'/bup'
26 cmd_str = b' '.join(cmd)
27 print(cmd_str, file=sys.stderr)
28 rc = subprocess.call(cmd)
30 print('terminated by signal', - rc, file=sys.stderr)
33 print('returned exit status', rc, file=sys.stderr)
36 print('subprocess call failed:', e, file=sys.stderr)
41 assert(sys.platform.startswith('linux'))
42 # Set up testfs with user_xattr, etc.
43 if subprocess.call([b'modprobe', b'loop']) != 0:
45 subprocess.call([b'umount', b'testfs'])
46 ex(b'dd', b'if=/dev/zero', b'of=testfs.img', b'bs=1M', b'count=32')
47 ex(b'mke2fs', b'-F', b'-j', b'-m', b'0', b'testfs.img')
48 ex(b'rm', b'-rf', b'testfs')
50 ex(b'mount', b'-o', b'loop,acl,user_xattr', b'testfs.img', b'testfs')
51 # Hide, so that tests can't create risks.
52 os.chown(b'testfs', 0, 0)
53 os.chmod(b'testfs', 0o700)
58 subprocess.call([b'umount', b'testfs'])
59 helpers.unlink(b'testfs.img')
62 def test_clean_up_archive_path():
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'.')
83 def test_risky_path():
84 risky = metadata._risky_path
85 WVPASS(risky(b'/foo'))
86 WVPASS(risky(b'///foo'))
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/../bar'))
93 WVFAIL(risky(b'foo/'))
94 WVFAIL(risky(b'foo///'))
95 WVFAIL(risky(b'./foo'))
96 WVFAIL(risky(b'foo/.'))
97 WVFAIL(risky(b'./foo/.'))
98 WVFAIL(risky(b'foo/bar'))
99 WVFAIL(risky(b'foo/./bar'))
102 def test_clean_up_extract_path():
103 cleanup = metadata._clean_up_extract_path
104 WVPASSEQ(cleanup(b'/foo'), b'foo')
105 WVPASSEQ(cleanup(b'///foo'), b'foo')
106 WVFAIL(cleanup(b'/../foo'))
107 WVFAIL(cleanup(b'../foo'))
108 WVFAIL(cleanup(b'foo/..'))
109 WVFAIL(cleanup(b'foo/../'))
110 WVFAIL(cleanup(b'foo/../bar'))
111 WVPASSEQ(cleanup(b'foo'), b'foo')
112 WVPASSEQ(cleanup(b'foo/'), b'foo/')
113 WVPASSEQ(cleanup(b'foo///'), b'foo///')
114 WVPASSEQ(cleanup(b'./foo'), b'./foo')
115 WVPASSEQ(cleanup(b'foo/.'), b'foo/.')
116 WVPASSEQ(cleanup(b'./foo/.'), b'./foo/.')
117 WVPASSEQ(cleanup(b'foo/bar'), b'foo/bar')
118 WVPASSEQ(cleanup(b'foo/./bar'), b'foo/./bar')
119 WVPASSEQ(cleanup(b'/'), b'.')
120 WVPASSEQ(cleanup(b'./'), b'./')
121 WVPASSEQ(cleanup(b'///foo/bar'), b'foo/bar')
122 WVPASSEQ(cleanup(b'///foo/bar'), b'foo/bar')
125 def test_metadata_method(tmpdir):
126 bup_dir = tmpdir + b'/bup'
127 data_path = tmpdir + b'/foo'
129 ex(b'touch', data_path + b'/file')
130 ex(b'ln', b'-s', b'file', data_path + b'/symlink')
131 test_time1 = 13 * 1000000000
132 test_time2 = 42 * 1000000000
133 utime(data_path + b'/file', (0, test_time1))
134 lutime(data_path + b'/symlink', (0, 0))
135 utime(data_path, (0, test_time2))
136 ex(bup_path, b'-d', bup_dir, b'init')
137 ex(bup_path, b'-d', bup_dir, b'index', b'-v', data_path)
138 ex(bup_path, b'-d', bup_dir, b'save', b'-tvvn', b'test', data_path)
139 git.check_repo_or_die(bup_dir)
140 with LocalRepo() as repo:
141 resolved = vfs.resolve(repo,
142 b'/test/latest' + resolve_parent(data_path),
144 leaf_name, leaf_item = resolved[-1]
146 WVPASS(m.mtime == test_time2)
147 WVPASS(leaf_name == b'foo')
148 contents = tuple(vfs.contents(repo, leaf_item))
149 WVPASS(len(contents) == 3)
150 WVPASSEQ(frozenset(name for name, item in contents),
151 frozenset((b'.', b'file', b'symlink')))
152 for name, item in contents:
155 WVPASS(m.mtime == test_time1)
156 elif name == b'symlink':
158 WVPASSEQ(m.symlink_target, b'file')
164 if helpers.saved_errors:
165 return str(helpers.saved_errors[0])
169 def test_from_path_error(tmpdir):
170 if is_superuser() or detect_fakeroot():
172 path = tmpdir + b'/foo'
174 m = metadata.from_path(path, archive_path=path, save_symlinks=True)
175 WVPASSEQ(m.path, path)
176 os.chmod(path, 0o000)
177 metadata.from_path(path, archive_path=path, save_symlinks=True)
178 if metadata.get_linux_file_attr:
179 print('saved_errors:', helpers.saved_errors, file=sys.stderr)
180 WVPASS(len(helpers.saved_errors) == 1)
181 errmsg = _first_err()
182 WVPASS(errmsg.startswith('read Linux attr'))
186 def _linux_attr_supported(path):
187 # Expects path to denote a regular file or a directory.
188 if not metadata.get_linux_file_attr:
191 metadata.get_linux_file_attr(path)
193 if e.errno in (errno.ENOTTY, errno.ENOSYS, errno.EOPNOTSUPP):
200 def test_apply_to_path_restricted_access(tmpdir):
201 if is_superuser() or detect_fakeroot():
203 if sys.platform.startswith('cygwin'):
204 return # chmod 000 isn't effective.
206 parent = tmpdir + b'/foo'
207 path = parent + b'/bar'
213 metadata.xattr.set(path, b'user.buptest', b'bup')
215 print("failed to set test xattr")
216 # ignore any failures here - maybe FS cannot do it
218 m = metadata.from_path(path, archive_path=path, save_symlinks=True)
219 WVPASSEQ(m.path, path)
220 os.chmod(parent, 0o000)
221 m.apply_to_path(path)
222 print(b'saved_errors:', helpers.saved_errors, file=sys.stderr)
223 expected_errors = ['utime: ']
224 if m.linux_attr and _linux_attr_supported(tmpdir):
225 expected_errors.append('Linux chattr: ')
226 if metadata.xattr and m.linux_xattr:
227 expected_errors.append("xattr.set ")
228 WVPASS(len(helpers.saved_errors) == len(expected_errors))
229 for i in range(len(expected_errors)):
230 assert str(helpers.saved_errors[i]).startswith(expected_errors[i])
235 def test_restore_over_existing_target(tmpdir):
236 path = tmpdir + b'/foo'
238 dir_m = metadata.from_path(path, archive_path=path, save_symlinks=True)
240 open(path, 'w').close()
241 file_m = metadata.from_path(path, archive_path=path, save_symlinks=True)
242 # Restore dir over file.
243 WVPASSEQ(dir_m.create_path(path, create_symlinks=True), None)
244 WVPASS(stat.S_ISDIR(os.stat(path).st_mode))
245 # Restore dir over dir.
246 WVPASSEQ(dir_m.create_path(path, create_symlinks=True), None)
247 WVPASS(stat.S_ISDIR(os.stat(path).st_mode))
248 # Restore file over dir.
249 WVPASSEQ(file_m.create_path(path, create_symlinks=True), None)
250 WVPASS(stat.S_ISREG(os.stat(path).st_mode))
251 # Restore file over file.
252 WVPASSEQ(file_m.create_path(path, create_symlinks=True), None)
253 WVPASS(stat.S_ISREG(os.stat(path).st_mode))
254 # Restore file over non-empty dir.
257 open(path + b'/bar', 'w').close()
258 WVEXCEPT(Exception, file_m.create_path, path, create_symlinks=True)
259 # Restore dir over non-empty dir.
260 os.remove(path + b'/bar')
261 os.mkdir(path + b'/bar')
262 WVEXCEPT(Exception, dir_m.create_path, path, create_symlinks=True)
265 from bup.metadata import xattr
267 def remove_selinux(attrs):
268 return list(filter(lambda i: not i in (b'security.selinux', ),
271 def test_handling_of_incorrect_existing_linux_xattrs():
272 if not is_superuser() or detect_fakeroot():
273 pytest.skip('skipping test -- not superuser')
275 if not setup_testfs():
276 pytest.skip('unable to load loop module; skipping dependent tests')
278 for f in glob.glob(b'testfs/*'):
281 open(path, 'w').close()
282 xattr.set(path, b'foo', b'bar', namespace=xattr.NS_USER)
283 m = metadata.from_path(path, archive_path=path, save_symlinks=True)
284 xattr.set(path, b'baz', b'bax', namespace=xattr.NS_USER)
285 m.apply_to_path(path, restore_numeric_ids=False)
286 WVPASSEQ(remove_selinux(xattr.list(path)), [b'user.foo'])
287 WVPASSEQ(xattr.get(path, b'user.foo'), b'bar')
288 xattr.set(path, b'foo', b'baz', namespace=xattr.NS_USER)
289 m.apply_to_path(path, restore_numeric_ids=False)
290 WVPASSEQ(remove_selinux(xattr.list(path)), [b'user.foo'])
291 WVPASSEQ(xattr.get(path, b'user.foo'), b'bar')
292 xattr.remove(path, b'foo', namespace=xattr.NS_USER)
293 m.apply_to_path(path, restore_numeric_ids=False)
294 WVPASSEQ(remove_selinux(xattr.list(path)), [b'user.foo'])
295 WVPASSEQ(xattr.get(path, b'user.foo'), b'bar')