]> arthur.barton.de Git - bup.git/blob - lib/bup/t/tmetadata.py
metadata: don't die if Linux attr (not xattr) support is missing.
[bup.git] / lib / bup / t / tmetadata.py
1 import glob, grp, pwd, stat, tempfile, subprocess
2 import bup.helpers as helpers
3 from bup import metadata
4 from bup.helpers import clear_errors, detect_fakeroot
5 from wvtest import *
6
7
8 top_dir = os.getcwd()
9
10
11 def ex(*cmd):
12     try:
13         cmd_str = ' '.join(cmd)
14         print >> sys.stderr, cmd_str
15         rc = subprocess.call(cmd)
16         if rc < 0:
17             print >> sys.stderr, 'terminated by signal', - rc
18             sys.exit(1)
19         elif rc > 0:
20             print >> sys.stderr, 'returned exit status', rc
21             sys.exit(1)
22     except OSError, e:
23         print >> sys.stderr, 'subprocess call failed:', e
24         sys.exit(1)
25
26
27 def setup_testfs():
28     # Set up testfs with user_xattr, etc.
29     subprocess.call(['umount', 'testfs'])
30     ex('dd', 'if=/dev/zero', 'of=testfs.img', 'bs=1M', 'count=32')
31     ex('mke2fs', '-F', '-j', '-m', '0', 'testfs.img')
32     ex('rm', '-rf', 'testfs')
33     os.mkdir('testfs')
34     ex('mount', '-o', 'loop,acl,user_xattr', 'testfs.img', 'testfs')
35     # Hide, so that tests can't create risks.
36     ex('chown', 'root:root', 'testfs')
37     os.chmod('testfs', 0700)
38
39
40 def cleanup_testfs():
41     subprocess.call(['umount', 'testfs'])
42     subprocess.call(['rm', '-f', 'testfs.img'])
43
44
45 @wvtest
46 def test_clean_up_archive_path():
47     cleanup = metadata._clean_up_path_for_archive
48     WVPASSEQ(cleanup('foo'), 'foo')
49     WVPASSEQ(cleanup('/foo'), 'foo')
50     WVPASSEQ(cleanup('///foo'), 'foo')
51     WVPASSEQ(cleanup('/foo/bar'), 'foo/bar')
52     WVPASSEQ(cleanup('foo/./bar'), 'foo/bar')
53     WVPASSEQ(cleanup('/foo/./bar'), 'foo/bar')
54     WVPASSEQ(cleanup('/foo/./bar/././baz'), 'foo/bar/baz')
55     WVPASSEQ(cleanup('/foo/./bar///././baz'), 'foo/bar/baz')
56     WVPASSEQ(cleanup('//./foo/./bar///././baz/.///'), 'foo/bar/baz/')
57     WVPASSEQ(cleanup('./foo/./.bar'), 'foo/.bar')
58     WVPASSEQ(cleanup('./foo/.'), 'foo')
59     WVPASSEQ(cleanup('./foo/..'), '.')
60     WVPASSEQ(cleanup('//./..//.../..//.'), '.')
61     WVPASSEQ(cleanup('//./..//..././/.'), '...')
62     WVPASSEQ(cleanup('/////.'), '.')
63     WVPASSEQ(cleanup('/../'), '.')
64     WVPASSEQ(cleanup(''), '.')
65
66
67 @wvtest
68 def test_risky_path():
69     risky = metadata._risky_path
70     WVPASS(risky('/foo'))
71     WVPASS(risky('///foo'))
72     WVPASS(risky('/../foo'))
73     WVPASS(risky('../foo'))
74     WVPASS(risky('foo/..'))
75     WVPASS(risky('foo/../'))
76     WVPASS(risky('foo/../bar'))
77     WVFAIL(risky('foo'))
78     WVFAIL(risky('foo/'))
79     WVFAIL(risky('foo///'))
80     WVFAIL(risky('./foo'))
81     WVFAIL(risky('foo/.'))
82     WVFAIL(risky('./foo/.'))
83     WVFAIL(risky('foo/bar'))
84     WVFAIL(risky('foo/./bar'))
85
86
87 @wvtest
88 def test_clean_up_extract_path():
89     cleanup = metadata._clean_up_extract_path
90     WVPASSEQ(cleanup('/foo'), 'foo')
91     WVPASSEQ(cleanup('///foo'), 'foo')
92     WVFAIL(cleanup('/../foo'))
93     WVFAIL(cleanup('../foo'))
94     WVFAIL(cleanup('foo/..'))
95     WVFAIL(cleanup('foo/../'))
96     WVFAIL(cleanup('foo/../bar'))
97     WVPASSEQ(cleanup('foo'), 'foo')
98     WVPASSEQ(cleanup('foo/'), 'foo/')
99     WVPASSEQ(cleanup('foo///'), 'foo///')
100     WVPASSEQ(cleanup('./foo'), './foo')
101     WVPASSEQ(cleanup('foo/.'), 'foo/.')
102     WVPASSEQ(cleanup('./foo/.'), './foo/.')
103     WVPASSEQ(cleanup('foo/bar'), 'foo/bar')
104     WVPASSEQ(cleanup('foo/./bar'), 'foo/./bar')
105     WVPASSEQ(cleanup('/'), '.')
106     WVPASSEQ(cleanup('./'), './')
107     WVPASSEQ(cleanup('///foo/bar'), 'foo/bar')
108     WVPASSEQ(cleanup('///foo/bar'), 'foo/bar')
109
110
111 @wvtest
112 def test_from_path_error():
113     if os.geteuid() == 0 or detect_fakeroot():
114         return
115     tmpdir = tempfile.mkdtemp(prefix='bup-tmetadata-')
116     try:
117         path = tmpdir + '/foo'
118         subprocess.call(['mkdir', path])
119         m = metadata.from_path(path, archive_path=path, save_symlinks=True)
120         WVPASSEQ(m.path, path)
121         subprocess.call(['chmod', '000', path])
122         metadata.from_path(path, archive_path=path, save_symlinks=True)
123         if metadata.get_linux_file_attr:
124             errmsg = helpers.saved_errors[0] if helpers.saved_errors else ''
125             WVPASS(errmsg.startswith('read Linux attr'))
126             clear_errors()
127     finally:
128         subprocess.call(['rm', '-rf', tmpdir])
129
130
131 @wvtest
132 def test_apply_to_path_restricted_access():
133     if os.geteuid() == 0 or detect_fakeroot():
134         return
135     tmpdir = tempfile.mkdtemp(prefix='bup-tmetadata-')
136     try:
137         path = tmpdir + '/foo'
138         subprocess.call(['mkdir', path])
139         clear_errors()
140         m = metadata.from_path(path, archive_path=path, save_symlinks=True)
141         WVPASSEQ(m.path, path)
142         subprocess.call(['chmod', '000', tmpdir])
143         m.apply_to_path(path)
144         errmsg = str(helpers.saved_errors[0]) if helpers.saved_errors else ''
145         WVPASS(errmsg.startswith('utime: '))
146         clear_errors()
147     finally:
148         subprocess.call(['rm', '-rf', tmpdir])
149
150
151 @wvtest
152 def test_restore_restricted_user_group():
153     if os.geteuid() == 0 or detect_fakeroot():
154         return
155     tmpdir = tempfile.mkdtemp(prefix='bup-tmetadata-')
156     try:
157         path = tmpdir + '/foo'
158         subprocess.call(['mkdir', path])
159         m = metadata.from_path(path, archive_path=path, save_symlinks=True)
160         WVPASSEQ(m.path, path)
161         WVPASSEQ(m.apply_to_path(path), None)
162         orig_uid = m.uid
163         m.uid = 0;
164         m.apply_to_path(path, restore_numeric_ids=True)
165         errmsg = str(helpers.saved_errors[0]) if helpers.saved_errors else ''
166         WVPASS(errmsg.startswith('lchown: '))
167         clear_errors()
168         m.uid = orig_uid
169         m.gid = 0;
170         m.apply_to_path(path, restore_numeric_ids=True)
171         errmsg = str(helpers.saved_errors[0]) if helpers.saved_errors else ''
172         WVPASS(errmsg.startswith('lchown: ') or os.stat(path).st_gid == m.gid)
173         clear_errors()
174     finally:
175         subprocess.call(['rm', '-rf', tmpdir])
176
177
178 @wvtest
179 def test_restore_nonexistent_user_group():
180     tmpdir = tempfile.mkdtemp(prefix='bup-tmetadata-')
181     try:
182         path = tmpdir + '/foo'
183         subprocess.call(['mkdir', path])
184         m = metadata.from_path(path, archive_path=path, save_symlinks=True)
185         WVPASSEQ(m.path, path)
186         m.owner = max([x.pw_name for x in pwd.getpwall()], key=len) + 'x'
187         m.group = max([x.gr_name for x in grp.getgrall()], key=len) + 'x'
188         WVPASSEQ(m.apply_to_path(path, restore_numeric_ids=True), None)
189         WVPASSEQ(os.stat(path).st_uid, m.uid)
190         WVPASSEQ(os.stat(path).st_gid, m.gid)
191         WVPASSEQ(m.apply_to_path(path, restore_numeric_ids=False), None)
192         WVPASSEQ(os.stat(path).st_uid, os.geteuid())
193         WVPASSEQ(os.stat(path).st_gid, os.getgid())
194     finally:
195         subprocess.call(['rm', '-rf', tmpdir])
196
197
198 @wvtest
199 def test_restore_over_existing_target():
200     tmpdir = tempfile.mkdtemp(prefix='bup-tmetadata-')
201     try:
202         path = tmpdir + '/foo'
203         os.mkdir(path)
204         dir_m = metadata.from_path(path, archive_path=path, save_symlinks=True)
205         os.rmdir(path)
206         open(path, 'w').close()
207         file_m = metadata.from_path(path, archive_path=path, save_symlinks=True)
208         # Restore dir over file.
209         WVPASSEQ(dir_m.create_path(path, create_symlinks=True), None)
210         WVPASS(stat.S_ISDIR(os.stat(path).st_mode))
211         # Restore dir over dir.
212         WVPASSEQ(dir_m.create_path(path, create_symlinks=True), None)
213         WVPASS(stat.S_ISDIR(os.stat(path).st_mode))
214         # Restore file over dir.
215         WVPASSEQ(file_m.create_path(path, create_symlinks=True), None)
216         WVPASS(stat.S_ISREG(os.stat(path).st_mode))
217         # Restore file over file.
218         WVPASSEQ(file_m.create_path(path, create_symlinks=True), None)
219         WVPASS(stat.S_ISREG(os.stat(path).st_mode))
220         # Restore file over non-empty dir.
221         os.remove(path)
222         os.mkdir(path)
223         open(path + '/bar', 'w').close()
224         WVEXCEPT(Exception, file_m.create_path, path, create_symlinks=True)
225         # Restore dir over non-empty dir.
226         os.remove(path + '/bar')
227         os.mkdir(path + '/bar')
228         WVEXCEPT(Exception, dir_m.create_path, path, create_symlinks=True)
229     finally:
230         subprocess.call(['rm', '-rf', tmpdir])
231
232
233 from bup.metadata import posix1e
234 if not posix1e:
235     @wvtest
236     def POSIX1E_ACL_SUPPORT_IS_MISSING():
237         pass
238
239
240 from bup.metadata import xattr
241 if not xattr:
242     @wvtest
243     def LINUX_XATTR_SUPPORT_IS_MISSING():
244         pass
245 else:
246     @wvtest
247     def test_handling_of_incorrect_existing_linux_xattrs():
248         if os.geteuid() != 0 or detect_fakeroot():
249             return
250         setup_testfs()
251         subprocess.check_call('rm -rf testfs/*', shell=True)
252         path = 'testfs/foo'
253         open(path, 'w').close()
254         xattr.set(path, 'foo', 'bar', namespace=xattr.NS_USER)
255         m = metadata.from_path(path, archive_path=path, save_symlinks=True)
256         xattr.set(path, 'baz', 'bax', namespace=xattr.NS_USER)
257         m.apply_to_path(path, restore_numeric_ids=False)
258         WVPASSEQ(xattr.list(path), ['user.foo'])
259         WVPASSEQ(xattr.get(path, 'user.foo'), 'bar')
260         xattr.set(path, 'foo', 'baz', namespace=xattr.NS_USER)
261         m.apply_to_path(path, restore_numeric_ids=False)
262         WVPASSEQ(xattr.list(path), ['user.foo'])
263         WVPASSEQ(xattr.get(path, 'user.foo'), 'bar')
264         xattr.remove(path, 'foo', namespace=xattr.NS_USER)
265         m.apply_to_path(path, restore_numeric_ids=False)
266         WVPASSEQ(xattr.list(path), ['user.foo'])
267         WVPASSEQ(xattr.get(path, 'user.foo'), 'bar')
268         os.chdir(top_dir)
269         cleanup_testfs()