]> arthur.barton.de Git - bup.git/blob - lib/bup/t/tmetadata.py
ea34f297dcc260839e68811ddfdab39d9b7be578
[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 def _linux_xattr_supported(path):
193     # NOTE: destructive test (tries to write to path).
194     if not metadata.xattr:
195         return False
196     try:
197         xattr.set(path, 'user.bup-test-xattr-support', 'true', nofollow=True)
198     except IOError, e:
199         if e.errno == errno.EOPNOTSUPP:
200             return False
201         else:
202             raise
203     return True
204
205
206 @wvtest
207 def test_apply_to_path_restricted_access():
208     if is_superuser() or detect_fakeroot():
209         return
210     if sys.platform.startswith('cygwin'):
211         return # chmod 000 isn't effective.
212     tmpdir = tempfile.mkdtemp(prefix='bup-tmetadata-')
213     try:
214         parent = tmpdir + '/foo'
215         path = parent + '/bar'
216         os.mkdir(parent)
217         os.mkdir(path)
218         clear_errors()
219         m = metadata.from_path(path, archive_path=path, save_symlinks=True)
220         WVPASSEQ(m.path, path)
221         os.chmod(parent, 000)
222         m.apply_to_path(path)
223         print >> sys.stderr, helpers.saved_errors
224         expected_errors = ['utime: ']
225         if m.linux_attr and _linux_attr_supported(tmpdir):
226             expected_errors.append('Linux chattr: ')
227         if _linux_xattr_supported(tmpdir):
228             expected_errors.append('xattr.set: ')
229         WVPASS(len(helpers.saved_errors) == len(expected_errors))
230         for i in xrange(len(expected_errors)):
231             WVPASS(str(helpers.saved_errors[i]).startswith(expected_errors[i]))
232         clear_errors()
233     finally:
234         subprocess.call(['chmod', '-R', 'u+rwX', tmpdir])
235         subprocess.call(['rm', '-rf', tmpdir])
236
237
238 @wvtest
239 def test_restore_over_existing_target():
240     tmpdir = tempfile.mkdtemp(prefix='bup-tmetadata-')
241     try:
242         path = tmpdir + '/foo'
243         os.mkdir(path)
244         dir_m = metadata.from_path(path, archive_path=path, save_symlinks=True)
245         os.rmdir(path)
246         open(path, 'w').close()
247         file_m = metadata.from_path(path, archive_path=path, save_symlinks=True)
248         # Restore dir over file.
249         WVPASSEQ(dir_m.create_path(path, create_symlinks=True), None)
250         WVPASS(stat.S_ISDIR(os.stat(path).st_mode))
251         # Restore dir over dir.
252         WVPASSEQ(dir_m.create_path(path, create_symlinks=True), None)
253         WVPASS(stat.S_ISDIR(os.stat(path).st_mode))
254         # Restore file over dir.
255         WVPASSEQ(file_m.create_path(path, create_symlinks=True), None)
256         WVPASS(stat.S_ISREG(os.stat(path).st_mode))
257         # Restore file over file.
258         WVPASSEQ(file_m.create_path(path, create_symlinks=True), None)
259         WVPASS(stat.S_ISREG(os.stat(path).st_mode))
260         # Restore file over non-empty dir.
261         os.remove(path)
262         os.mkdir(path)
263         open(path + '/bar', 'w').close()
264         WVEXCEPT(Exception, file_m.create_path, path, create_symlinks=True)
265         # Restore dir over non-empty dir.
266         os.remove(path + '/bar')
267         os.mkdir(path + '/bar')
268         WVEXCEPT(Exception, dir_m.create_path, path, create_symlinks=True)
269     finally:
270         subprocess.call(['rm', '-rf', tmpdir])
271
272
273 from bup.metadata import posix1e
274 if not posix1e:
275     @wvtest
276     def POSIX1E_ACL_SUPPORT_IS_MISSING():
277         pass
278
279
280 from bup.metadata import xattr
281 if xattr:
282     @wvtest
283     def test_handling_of_incorrect_existing_linux_xattrs():
284         if not is_superuser() or detect_fakeroot():
285             WVMSG('skipping test -- not superuser')
286             return
287         setup_testfs()
288         for f in glob.glob('testfs/*'):
289             ex('rm', '-rf', f)
290         path = 'testfs/foo'
291         open(path, 'w').close()
292         xattr.set(path, 'foo', 'bar', namespace=xattr.NS_USER)
293         m = metadata.from_path(path, archive_path=path, save_symlinks=True)
294         xattr.set(path, 'baz', 'bax', namespace=xattr.NS_USER)
295         m.apply_to_path(path, restore_numeric_ids=False)
296         WVPASSEQ(xattr.list(path), ['user.foo'])
297         WVPASSEQ(xattr.get(path, 'user.foo'), 'bar')
298         xattr.set(path, 'foo', 'baz', namespace=xattr.NS_USER)
299         m.apply_to_path(path, restore_numeric_ids=False)
300         WVPASSEQ(xattr.list(path), ['user.foo'])
301         WVPASSEQ(xattr.get(path, 'user.foo'), 'bar')
302         xattr.remove(path, 'foo', namespace=xattr.NS_USER)
303         m.apply_to_path(path, restore_numeric_ids=False)
304         WVPASSEQ(xattr.list(path), ['user.foo'])
305         WVPASSEQ(xattr.get(path, 'user.foo'), 'bar')
306         os.chdir(start_dir)
307         cleanup_testfs()