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