]> arthur.barton.de Git - bup.git/blob - test/int/test_metadata.py
398e0ba45d7a9048497bbbf3ab02d470d9cd3cf5
[bup.git] / test / int / test_metadata.py
1
2 from __future__ import absolute_import, print_function
3 import errno, glob, grp, pwd, stat, tempfile, subprocess
4
5 from wvtest import *
6
7 from bup import git, metadata
8 from bup import vfs
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
15
16
17 top_dir = b'../..'
18 bup_path = top_dir + b'/bup'
19 start_dir = os.getcwd()
20
21
22 def ex(*cmd):
23     try:
24         cmd_str = b' '.join(cmd)
25         print(cmd_str, file=sys.stderr)
26         rc = subprocess.call(cmd)
27         if rc < 0:
28             print('terminated by signal', - rc, file=sys.stderr)
29             sys.exit(1)
30         elif rc > 0:
31             print('returned exit status', rc, file=sys.stderr)
32             sys.exit(1)
33     except OSError as e:
34         print('subprocess call failed:', e, file=sys.stderr)
35         sys.exit(1)
36
37
38 def setup_testfs():
39     assert(sys.platform.startswith('linux'))
40     # Set up testfs with user_xattr, etc.
41     if subprocess.call([b'modprobe', b'loop']) != 0:
42         return False
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')
47     os.mkdir(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)
52     return True
53
54
55 def cleanup_testfs():
56     subprocess.call([b'umount', b'testfs'])
57     helpers.unlink(b'testfs.img')
58
59
60 @wvtest
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'.')
81
82
83 @wvtest
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'))
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/.'))
99         WVFAIL(risky(b'./foo/.'))
100         WVFAIL(risky(b'foo/bar'))
101         WVFAIL(risky(b'foo/./bar'))
102
103
104 @wvtest
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')
127
128
129 @wvtest
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'
135             os.mkdir(data_path)
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)
147             repo = LocalRepo()
148             resolved = vfs.resolve(repo,
149                                    b'/test/latest' + resolve_parent(data_path),
150                                    follow=False)
151             leaf_name, leaf_item = resolved[-1]
152             m = leaf_item.meta
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:
160                 if name == b'file':
161                     m = item.meta
162                     WVPASS(m.mtime == test_time1)
163                 elif name == b'symlink':
164                     m = item.meta
165                     WVPASSEQ(m.symlink_target, b'file')
166                     WVPASSEQ(m.size, 4)
167                     WVPASSEQ(m.mtime, 0)
168
169
170 def _first_err():
171     if helpers.saved_errors:
172         return str(helpers.saved_errors[0])
173     return ''
174
175
176 @wvtest
177 def test_from_path_error():
178     if is_superuser() or detect_fakeroot():
179         return
180     with no_lingering_errors():
181         with test_tempdir(b'bup-tmetadata-') as tmpdir:
182             path = tmpdir + b'/foo'
183             os.mkdir(path)
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'))
193                 clear_errors()
194
195
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:
199         return False
200     try:
201         metadata.get_linux_file_attr(path)
202     except OSError as e:
203         if e.errno in (errno.ENOTTY, errno.ENOSYS, errno.EOPNOTSUPP):
204             return False
205         else:
206             raise
207     return True
208
209
210 @wvtest
211 def test_apply_to_path_restricted_access():
212     if is_superuser() or detect_fakeroot():
213         return
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'
220             os.mkdir(parent)
221             os.mkdir(path)
222             clear_errors()
223             if metadata.xattr:
224                 try:
225                     metadata.xattr.set(path, b'user.buptest', b'bup')
226                 except:
227                     print("failed to set test xattr")
228                     # ignore any failures here - maybe FS cannot do it
229                     pass
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]))
243             clear_errors()
244
245
246 @wvtest
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'
251             os.mkdir(path)
252             dir_m = metadata.from_path(path, archive_path=path, save_symlinks=True)
253             os.rmdir(path)
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.
269             os.remove(path)
270             os.mkdir(path)
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)
277
278
279 from bup.metadata import read_acl
280 if not read_acl:
281     @wvtest
282     def POSIX1E_ACL_SUPPORT_IS_MISSING():
283         pass
284
285
286 from bup.metadata import xattr
287 if xattr:
288     def remove_selinux(attrs):
289         return list(filter(lambda i: not i in (b'security.selinux', ),
290                            attrs))
291
292     @wvtest
293     def test_handling_of_incorrect_existing_linux_xattrs():
294         if not is_superuser() or detect_fakeroot():
295             WVMSG('skipping test -- not superuser')
296             return
297         if not setup_testfs():
298             WVMSG('unable to load loop module; skipping dependent tests')
299             return
300         for f in glob.glob(b'testfs/*'):
301             ex(b'rm', b'-rf', f)
302         path = b'testfs/foo'
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')
318         os.chdir(start_dir)
319         cleanup_testfs()