]> arthur.barton.de Git - bup.git/blob - lib/bup/t/tmetadata.py
_apply_linux_xattr_rec(): do nothing if no rec; fix restricted access test.
[bup.git] / lib / bup / t / tmetadata.py
1 import errno, glob, grp, pwd, stat, tempfile, subprocess
2 import bup.helpers as helpers
3 from bup import git, metadata, vfs
4 from bup.helpers import clear_errors, detect_fakeroot, is_superuser, realpath
5 from wvtest import *
6 from bup.xstat import utime, lutime
7
8
9 top_dir = '../../..'
10 bup_path = top_dir + '/bup'
11 start_dir = os.getcwd()
12
13
14 def ex(*cmd):
15     try:
16         cmd_str = ' '.join(cmd)
17         print >> sys.stderr, cmd_str
18         rc = subprocess.call(cmd)
19         if rc < 0:
20             print >> sys.stderr, 'terminated by signal', - rc
21             sys.exit(1)
22         elif rc > 0:
23             print >> sys.stderr, 'returned exit status', rc
24             sys.exit(1)
25     except OSError, e:
26         print >> sys.stderr, 'subprocess call failed:', e
27         sys.exit(1)
28
29
30 def setup_testfs():
31     assert(sys.platform.startswith('linux'))
32     # Set up testfs with user_xattr, etc.
33     subprocess.call(['umount', 'testfs'])
34     ex('dd', 'if=/dev/zero', 'of=testfs.img', 'bs=1M', 'count=32')
35     ex('mke2fs', '-F', '-j', '-m', '0', 'testfs.img')
36     ex('rm', '-rf', 'testfs')
37     os.mkdir('testfs')
38     ex('mount', '-o', 'loop,acl,user_xattr', 'testfs.img', 'testfs')
39     # Hide, so that tests can't create risks.
40     os.chown('testfs', 0, 0)
41     os.chmod('testfs', 0700)
42
43
44 def cleanup_testfs():
45     subprocess.call(['umount', 'testfs'])
46     helpers.unlink('testfs.img')
47
48
49 @wvtest
50 def test_clean_up_archive_path():
51     cleanup = metadata._clean_up_path_for_archive
52     WVPASSEQ(cleanup('foo'), 'foo')
53     WVPASSEQ(cleanup('/foo'), 'foo')
54     WVPASSEQ(cleanup('///foo'), 'foo')
55     WVPASSEQ(cleanup('/foo/bar'), 'foo/bar')
56     WVPASSEQ(cleanup('foo/./bar'), 'foo/bar')
57     WVPASSEQ(cleanup('/foo/./bar'), 'foo/bar')
58     WVPASSEQ(cleanup('/foo/./bar/././baz'), 'foo/bar/baz')
59     WVPASSEQ(cleanup('/foo/./bar///././baz'), 'foo/bar/baz')
60     WVPASSEQ(cleanup('//./foo/./bar///././baz/.///'), 'foo/bar/baz/')
61     WVPASSEQ(cleanup('./foo/./.bar'), 'foo/.bar')
62     WVPASSEQ(cleanup('./foo/.'), 'foo')
63     WVPASSEQ(cleanup('./foo/..'), '.')
64     WVPASSEQ(cleanup('//./..//.../..//.'), '.')
65     WVPASSEQ(cleanup('//./..//..././/.'), '...')
66     WVPASSEQ(cleanup('/////.'), '.')
67     WVPASSEQ(cleanup('/../'), '.')
68     WVPASSEQ(cleanup(''), '.')
69
70
71 @wvtest
72 def test_risky_path():
73     risky = metadata._risky_path
74     WVPASS(risky('/foo'))
75     WVPASS(risky('///foo'))
76     WVPASS(risky('/../foo'))
77     WVPASS(risky('../foo'))
78     WVPASS(risky('foo/..'))
79     WVPASS(risky('foo/../'))
80     WVPASS(risky('foo/../bar'))
81     WVFAIL(risky('foo'))
82     WVFAIL(risky('foo/'))
83     WVFAIL(risky('foo///'))
84     WVFAIL(risky('./foo'))
85     WVFAIL(risky('foo/.'))
86     WVFAIL(risky('./foo/.'))
87     WVFAIL(risky('foo/bar'))
88     WVFAIL(risky('foo/./bar'))
89
90
91 @wvtest
92 def test_clean_up_extract_path():
93     cleanup = metadata._clean_up_extract_path
94     WVPASSEQ(cleanup('/foo'), 'foo')
95     WVPASSEQ(cleanup('///foo'), 'foo')
96     WVFAIL(cleanup('/../foo'))
97     WVFAIL(cleanup('../foo'))
98     WVFAIL(cleanup('foo/..'))
99     WVFAIL(cleanup('foo/../'))
100     WVFAIL(cleanup('foo/../bar'))
101     WVPASSEQ(cleanup('foo'), 'foo')
102     WVPASSEQ(cleanup('foo/'), 'foo/')
103     WVPASSEQ(cleanup('foo///'), 'foo///')
104     WVPASSEQ(cleanup('./foo'), './foo')
105     WVPASSEQ(cleanup('foo/.'), 'foo/.')
106     WVPASSEQ(cleanup('./foo/.'), './foo/.')
107     WVPASSEQ(cleanup('foo/bar'), 'foo/bar')
108     WVPASSEQ(cleanup('foo/./bar'), 'foo/./bar')
109     WVPASSEQ(cleanup('/'), '.')
110     WVPASSEQ(cleanup('./'), './')
111     WVPASSEQ(cleanup('///foo/bar'), 'foo/bar')
112     WVPASSEQ(cleanup('///foo/bar'), 'foo/bar')
113
114
115 @wvtest
116 def test_metadata_method():
117     tmpdir = tempfile.mkdtemp(prefix='bup-tmetadata-')
118     try:
119         bup_dir = tmpdir + '/bup'
120         data_path = tmpdir + '/foo'
121         os.mkdir(data_path)
122         ex('touch', data_path + '/file')
123         ex('ln', '-s', 'file', data_path + '/symlink')
124         test_time1 = 13 * 1000000000
125         test_time2 = 42 * 1000000000
126         utime(data_path + '/file', (0, test_time1))
127         lutime(data_path + '/symlink', (0, 0))
128         utime(data_path, (0, test_time2))
129         ex(bup_path, '-d', bup_dir, 'init')
130         ex(bup_path, '-d', bup_dir, 'index', '-v', data_path)
131         ex(bup_path, '-d', bup_dir, 'save', '-tvvn', 'test', data_path)
132         git.check_repo_or_die(bup_dir)
133         top = vfs.RefList(None)
134         n = top.lresolve('/test/latest' + realpath(data_path))
135         m = n.metadata()
136         WVPASS(m.mtime == test_time2)
137         WVPASS(len(n.subs()) == 2)
138         WVPASS(n.name == 'foo')
139         WVPASS(set([x.name for x in n.subs()]) == set(['file', 'symlink']))
140         for sub in n:
141             if sub.name == 'file':
142                 m = sub.metadata()
143                 WVPASS(m.mtime == test_time1)
144             elif sub.name == 'symlink':
145                 m = sub.metadata()
146                 WVPASS(m.mtime == 0)
147     finally:
148         subprocess.call(['rm', '-rf', tmpdir])
149
150
151 def _first_err():
152     if helpers.saved_errors:
153         return str(helpers.saved_errors[0])
154     return ''
155
156
157 @wvtest
158 def test_from_path_error():
159     if is_superuser() or detect_fakeroot():
160         return
161     tmpdir = tempfile.mkdtemp(prefix='bup-tmetadata-')
162     try:
163         path = tmpdir + '/foo'
164         os.mkdir(path)
165         m = metadata.from_path(path, archive_path=path, save_symlinks=True)
166         WVPASSEQ(m.path, path)
167         os.chmod(path, 000)
168         metadata.from_path(path, archive_path=path, save_symlinks=True)
169         if metadata.get_linux_file_attr:
170             WVPASS(len(helpers.saved_errors) == 1)
171             errmsg = _first_err()
172             WVPASS(errmsg.startswith('read Linux attr'))
173             clear_errors()
174     finally:
175         subprocess.call(['rm', '-rf', tmpdir])
176
177
178 def _linux_attr_supported(path):
179     # Expects path to denote a regular file or a directory.
180     if not metadata.get_linux_file_attr:
181         return False
182     try:
183         metadata.get_linux_file_attr(path)
184     except OSError, e:
185         if e.errno in (errno.ENOTTY, errno.ENOSYS, errno.EOPNOTSUPP):
186             return False
187         else:
188             raise
189     return True
190
191
192 @wvtest
193 def test_apply_to_path_restricted_access():
194     if is_superuser() or detect_fakeroot():
195         return
196     if sys.platform.startswith('cygwin'):
197         return # chmod 000 isn't effective.
198     tmpdir = tempfile.mkdtemp(prefix='bup-tmetadata-')
199     try:
200         parent = tmpdir + '/foo'
201         path = parent + '/bar'
202         os.mkdir(parent)
203         os.mkdir(path)
204         clear_errors()
205         m = metadata.from_path(path, archive_path=path, save_symlinks=True)
206         WVPASSEQ(m.path, path)
207         os.chmod(parent, 000)
208         m.apply_to_path(path)
209         print >> sys.stderr, helpers.saved_errors
210         expected_errors = ['utime: ']
211         if m.linux_attr and _linux_attr_supported(tmpdir):
212             expected_errors.append('Linux chattr: ')
213         if metadata.xattr and m.linux_xattr:
214             expected_errors.append('xattr.set: ')
215         WVPASS(len(helpers.saved_errors) == len(expected_errors))
216         for i in xrange(len(expected_errors)):
217             WVPASS(str(helpers.saved_errors[i]).startswith(expected_errors[i]))
218         clear_errors()
219     finally:
220         subprocess.call(['chmod', '-R', 'u+rwX', tmpdir])
221         subprocess.call(['rm', '-rf', tmpdir])
222
223
224 @wvtest
225 def test_restore_over_existing_target():
226     tmpdir = tempfile.mkdtemp(prefix='bup-tmetadata-')
227     try:
228         path = tmpdir + '/foo'
229         os.mkdir(path)
230         dir_m = metadata.from_path(path, archive_path=path, save_symlinks=True)
231         os.rmdir(path)
232         open(path, 'w').close()
233         file_m = metadata.from_path(path, archive_path=path, save_symlinks=True)
234         # Restore dir over file.
235         WVPASSEQ(dir_m.create_path(path, create_symlinks=True), None)
236         WVPASS(stat.S_ISDIR(os.stat(path).st_mode))
237         # Restore dir over dir.
238         WVPASSEQ(dir_m.create_path(path, create_symlinks=True), None)
239         WVPASS(stat.S_ISDIR(os.stat(path).st_mode))
240         # Restore file over dir.
241         WVPASSEQ(file_m.create_path(path, create_symlinks=True), None)
242         WVPASS(stat.S_ISREG(os.stat(path).st_mode))
243         # Restore file over file.
244         WVPASSEQ(file_m.create_path(path, create_symlinks=True), None)
245         WVPASS(stat.S_ISREG(os.stat(path).st_mode))
246         # Restore file over non-empty dir.
247         os.remove(path)
248         os.mkdir(path)
249         open(path + '/bar', 'w').close()
250         WVEXCEPT(Exception, file_m.create_path, path, create_symlinks=True)
251         # Restore dir over non-empty dir.
252         os.remove(path + '/bar')
253         os.mkdir(path + '/bar')
254         WVEXCEPT(Exception, dir_m.create_path, path, create_symlinks=True)
255     finally:
256         subprocess.call(['rm', '-rf', tmpdir])
257
258
259 from bup.metadata import posix1e
260 if not posix1e:
261     @wvtest
262     def POSIX1E_ACL_SUPPORT_IS_MISSING():
263         pass
264
265
266 from bup.metadata import xattr
267 if xattr:
268     @wvtest
269     def test_handling_of_incorrect_existing_linux_xattrs():
270         if not is_superuser() or detect_fakeroot():
271             WVMSG('skipping test -- not superuser')
272             return
273         setup_testfs()
274         for f in glob.glob('testfs/*'):
275             ex('rm', '-rf', f)
276         path = 'testfs/foo'
277         open(path, 'w').close()
278         xattr.set(path, 'foo', 'bar', namespace=xattr.NS_USER)
279         m = metadata.from_path(path, archive_path=path, save_symlinks=True)
280         xattr.set(path, 'baz', 'bax', namespace=xattr.NS_USER)
281         m.apply_to_path(path, restore_numeric_ids=False)
282         WVPASSEQ(xattr.list(path), ['user.foo'])
283         WVPASSEQ(xattr.get(path, 'user.foo'), 'bar')
284         xattr.set(path, 'foo', 'baz', namespace=xattr.NS_USER)
285         m.apply_to_path(path, restore_numeric_ids=False)
286         WVPASSEQ(xattr.list(path), ['user.foo'])
287         WVPASSEQ(xattr.get(path, 'user.foo'), 'bar')
288         xattr.remove(path, 'foo', namespace=xattr.NS_USER)
289         m.apply_to_path(path, restore_numeric_ids=False)
290         WVPASSEQ(xattr.list(path), ['user.foo'])
291         WVPASSEQ(xattr.get(path, 'user.foo'), 'bar')
292         os.chdir(start_dir)
293         cleanup_testfs()