]> arthur.barton.de Git - bup.git/blob - lib/bup/t/tmetadata.py
Migrate all xrange calls to range in bup.compat
[bup.git] / lib / bup / t / tmetadata.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 = '../../..'
18 bup_tmp = os.path.realpath('../../../t/tmp')
19 bup_path = top_dir + '/bup'
20 start_dir = os.getcwd()
21
22
23 def ex(*cmd):
24     try:
25         cmd_str = ' '.join(cmd)
26         print(cmd_str, file=sys.stderr)
27         rc = subprocess.call(cmd)
28         if rc < 0:
29             print('terminated by signal', - rc, file=sys.stderr)
30             sys.exit(1)
31         elif rc > 0:
32             print('returned exit status', rc, file=sys.stderr)
33             sys.exit(1)
34     except OSError as e:
35         print('subprocess call failed:', e, file=sys.stderr)
36         sys.exit(1)
37
38
39 def setup_testfs():
40     assert(sys.platform.startswith('linux'))
41     # Set up testfs with user_xattr, etc.
42     if subprocess.call(['modprobe', 'loop']) != 0:
43         return False
44     subprocess.call(['umount', 'testfs'])
45     ex('dd', 'if=/dev/zero', 'of=testfs.img', 'bs=1M', 'count=32')
46     ex('mke2fs', '-F', '-j', '-m', '0', 'testfs.img')
47     ex('rm', '-rf', 'testfs')
48     os.mkdir('testfs')
49     ex('mount', '-o', 'loop,acl,user_xattr', 'testfs.img', 'testfs')
50     # Hide, so that tests can't create risks.
51     os.chown('testfs', 0, 0)
52     os.chmod('testfs', 0o700)
53     return True
54
55
56 def cleanup_testfs():
57     subprocess.call(['umount', 'testfs'])
58     helpers.unlink('testfs.img')
59
60
61 @wvtest
62 def test_clean_up_archive_path():
63     with no_lingering_errors():
64         cleanup = metadata._clean_up_path_for_archive
65         WVPASSEQ(cleanup('foo'), 'foo')
66         WVPASSEQ(cleanup('/foo'), 'foo')
67         WVPASSEQ(cleanup('///foo'), 'foo')
68         WVPASSEQ(cleanup('/foo/bar'), 'foo/bar')
69         WVPASSEQ(cleanup('foo/./bar'), 'foo/bar')
70         WVPASSEQ(cleanup('/foo/./bar'), 'foo/bar')
71         WVPASSEQ(cleanup('/foo/./bar/././baz'), 'foo/bar/baz')
72         WVPASSEQ(cleanup('/foo/./bar///././baz'), 'foo/bar/baz')
73         WVPASSEQ(cleanup('//./foo/./bar///././baz/.///'), 'foo/bar/baz/')
74         WVPASSEQ(cleanup('./foo/./.bar'), 'foo/.bar')
75         WVPASSEQ(cleanup('./foo/.'), 'foo')
76         WVPASSEQ(cleanup('./foo/..'), '.')
77         WVPASSEQ(cleanup('//./..//.../..//.'), '.')
78         WVPASSEQ(cleanup('//./..//..././/.'), '...')
79         WVPASSEQ(cleanup('/////.'), '.')
80         WVPASSEQ(cleanup('/../'), '.')
81         WVPASSEQ(cleanup(''), '.')
82
83
84 @wvtest
85 def test_risky_path():
86     with no_lingering_errors():
87         risky = metadata._risky_path
88         WVPASS(risky('/foo'))
89         WVPASS(risky('///foo'))
90         WVPASS(risky('/../foo'))
91         WVPASS(risky('../foo'))
92         WVPASS(risky('foo/..'))
93         WVPASS(risky('foo/../'))
94         WVPASS(risky('foo/../bar'))
95         WVFAIL(risky('foo'))
96         WVFAIL(risky('foo/'))
97         WVFAIL(risky('foo///'))
98         WVFAIL(risky('./foo'))
99         WVFAIL(risky('foo/.'))
100         WVFAIL(risky('./foo/.'))
101         WVFAIL(risky('foo/bar'))
102         WVFAIL(risky('foo/./bar'))
103
104
105 @wvtest
106 def test_clean_up_extract_path():
107     with no_lingering_errors():
108         cleanup = metadata._clean_up_extract_path
109         WVPASSEQ(cleanup('/foo'), 'foo')
110         WVPASSEQ(cleanup('///foo'), 'foo')
111         WVFAIL(cleanup('/../foo'))
112         WVFAIL(cleanup('../foo'))
113         WVFAIL(cleanup('foo/..'))
114         WVFAIL(cleanup('foo/../'))
115         WVFAIL(cleanup('foo/../bar'))
116         WVPASSEQ(cleanup('foo'), 'foo')
117         WVPASSEQ(cleanup('foo/'), 'foo/')
118         WVPASSEQ(cleanup('foo///'), 'foo///')
119         WVPASSEQ(cleanup('./foo'), './foo')
120         WVPASSEQ(cleanup('foo/.'), 'foo/.')
121         WVPASSEQ(cleanup('./foo/.'), './foo/.')
122         WVPASSEQ(cleanup('foo/bar'), 'foo/bar')
123         WVPASSEQ(cleanup('foo/./bar'), 'foo/./bar')
124         WVPASSEQ(cleanup('/'), '.')
125         WVPASSEQ(cleanup('./'), './')
126         WVPASSEQ(cleanup('///foo/bar'), 'foo/bar')
127         WVPASSEQ(cleanup('///foo/bar'), 'foo/bar')
128
129
130 @wvtest
131 def test_metadata_method():
132     with no_lingering_errors():
133         with test_tempdir('bup-tmetadata-') as tmpdir:
134             bup_dir = tmpdir + '/bup'
135             data_path = tmpdir + '/foo'
136             os.mkdir(data_path)
137             ex('touch', data_path + '/file')
138             ex('ln', '-s', 'file', data_path + '/symlink')
139             test_time1 = 13 * 1000000000
140             test_time2 = 42 * 1000000000
141             utime(data_path + '/file', (0, test_time1))
142             lutime(data_path + '/symlink', (0, 0))
143             utime(data_path, (0, test_time2))
144             ex(bup_path, '-d', bup_dir, 'init')
145             ex(bup_path, '-d', bup_dir, 'index', '-v', data_path)
146             ex(bup_path, '-d', bup_dir, 'save', '-tvvn', 'test', data_path)
147             git.check_repo_or_die(bup_dir)
148             repo = LocalRepo()
149             resolved = vfs.lresolve(repo,
150                                     '/test/latest' + resolve_parent(data_path))
151             leaf_name, leaf_item = resolved[-1]
152             m = leaf_item.meta
153             WVPASS(m.mtime == test_time2)
154             WVPASS(leaf_name == '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(('.', 'file', 'symlink')))
159             for name, item in contents:
160                 if name == 'file':
161                     m = item.meta
162                     WVPASS(m.mtime == test_time1)
163                 elif name == 'symlink':
164                     m = item.meta
165                     WVPASSEQ(m.symlink_target, '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('bup-tmetadata-') as tmpdir:
182             path = tmpdir + '/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('bup-tmetadata-') as tmpdir:
218             parent = tmpdir + '/foo'
219             path = parent + '/bar'
220             os.mkdir(parent)
221             os.mkdir(path)
222             clear_errors()
223             m = metadata.from_path(path, archive_path=path, save_symlinks=True)
224             WVPASSEQ(m.path, path)
225             os.chmod(parent, 0o000)
226             m.apply_to_path(path)
227             print('saved_errors:', helpers.saved_errors, file=sys.stderr)
228             expected_errors = ['utime: ']
229             if m.linux_attr and _linux_attr_supported(tmpdir):
230                 expected_errors.append('Linux chattr: ')
231             if metadata.xattr and m.linux_xattr:
232                 expected_errors.append("xattr.set '")
233             WVPASS(len(helpers.saved_errors) == len(expected_errors))
234             for i in range(len(expected_errors)):
235                 WVPASS(str(helpers.saved_errors[i]).startswith(expected_errors[i]))
236             clear_errors()
237
238
239 @wvtest
240 def test_restore_over_existing_target():
241     with no_lingering_errors():
242         with test_tempdir('bup-tmetadata-') as tmpdir:
243             path = tmpdir + '/foo'
244             os.mkdir(path)
245             dir_m = metadata.from_path(path, archive_path=path, save_symlinks=True)
246             os.rmdir(path)
247             open(path, 'w').close()
248             file_m = metadata.from_path(path, archive_path=path, save_symlinks=True)
249             # Restore dir over file.
250             WVPASSEQ(dir_m.create_path(path, create_symlinks=True), None)
251             WVPASS(stat.S_ISDIR(os.stat(path).st_mode))
252             # Restore dir over dir.
253             WVPASSEQ(dir_m.create_path(path, create_symlinks=True), None)
254             WVPASS(stat.S_ISDIR(os.stat(path).st_mode))
255             # Restore file over dir.
256             WVPASSEQ(file_m.create_path(path, create_symlinks=True), None)
257             WVPASS(stat.S_ISREG(os.stat(path).st_mode))
258             # Restore file over file.
259             WVPASSEQ(file_m.create_path(path, create_symlinks=True), None)
260             WVPASS(stat.S_ISREG(os.stat(path).st_mode))
261             # Restore file over non-empty dir.
262             os.remove(path)
263             os.mkdir(path)
264             open(path + '/bar', 'w').close()
265             WVEXCEPT(Exception, file_m.create_path, path, create_symlinks=True)
266             # Restore dir over non-empty dir.
267             os.remove(path + '/bar')
268             os.mkdir(path + '/bar')
269             WVEXCEPT(Exception, dir_m.create_path, path, create_symlinks=True)
270
271
272 from bup.metadata import posix1e
273 if not posix1e:
274     @wvtest
275     def POSIX1E_ACL_SUPPORT_IS_MISSING():
276         pass
277
278
279 from bup.metadata import xattr
280 if xattr:
281     @wvtest
282     def test_handling_of_incorrect_existing_linux_xattrs():
283         if not is_superuser() or detect_fakeroot():
284             WVMSG('skipping test -- not superuser')
285             return
286         if not setup_testfs():
287             WVMSG('unable to load loop module; skipping dependent tests')
288             return
289         for f in glob.glob('testfs/*'):
290             ex('rm', '-rf', f)
291         path = 'testfs/foo'
292         open(path, 'w').close()
293         xattr.set(path, 'foo', 'bar', namespace=xattr.NS_USER)
294         m = metadata.from_path(path, archive_path=path, save_symlinks=True)
295         xattr.set(path, 'baz', 'bax', namespace=xattr.NS_USER)
296         m.apply_to_path(path, restore_numeric_ids=False)
297         WVPASSEQ(xattr.list(path), ['user.foo'])
298         WVPASSEQ(xattr.get(path, 'user.foo'), 'bar')
299         xattr.set(path, 'foo', 'baz', namespace=xattr.NS_USER)
300         m.apply_to_path(path, restore_numeric_ids=False)
301         WVPASSEQ(xattr.list(path), ['user.foo'])
302         WVPASSEQ(xattr.get(path, 'user.foo'), 'bar')
303         xattr.remove(path, 'foo', namespace=xattr.NS_USER)
304         m.apply_to_path(path, restore_numeric_ids=False)
305         WVPASSEQ(xattr.list(path), ['user.foo'])
306         WVPASSEQ(xattr.get(path, 'user.foo'), 'bar')
307         os.chdir(start_dir)
308         cleanup_testfs()