1 import glob, grp, pwd, stat, tempfile, subprocess, xattr
2 import bup.helpers as helpers
3 from bup import metadata
4 from bup.helpers import clear_errors, detect_fakeroot
13 cmd_str = ' '.join(cmd)
14 print >> sys.stderr, cmd_str
15 rc = subprocess.call(cmd)
17 print >> sys.stderr, 'terminated by signal', - rc
20 print >> sys.stderr, 'returned exit status', rc
23 print >> sys.stderr, 'subprocess call failed:', e
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')
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)
41 subprocess.call(['umount', 'testfs'])
42 subprocess.call(['rm', '-f', 'testfs.img'])
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(''), '.')
68 def test_risky_path():
69 risky = metadata._risky_path
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'))
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'))
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')
112 def test_from_path_error():
113 if os.geteuid() == 0 or detect_fakeroot():
115 tmpdir = tempfile.mkdtemp(prefix='bup-tmetadata-')
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 errmsg = helpers.saved_errors[0] if helpers.saved_errors else ''
124 WVPASS(errmsg.startswith('read Linux attr'))
127 subprocess.call(['rm', '-rf', tmpdir])
131 def test_apply_to_path_restricted_access():
132 if os.geteuid() == 0 or detect_fakeroot():
134 tmpdir = tempfile.mkdtemp(prefix='bup-tmetadata-')
136 path = tmpdir + '/foo'
137 subprocess.call(['mkdir', path])
139 m = metadata.from_path(path, archive_path=path, save_symlinks=True)
140 WVPASSEQ(m.path, path)
141 subprocess.call(['chmod', '000', tmpdir])
142 m.apply_to_path(path)
143 errmsg = str(helpers.saved_errors[0]) if helpers.saved_errors else ''
144 WVPASS(errmsg.startswith('utime: '))
147 subprocess.call(['rm', '-rf', tmpdir])
151 def test_restore_restricted_user_group():
152 if os.geteuid() == 0 or detect_fakeroot():
154 tmpdir = tempfile.mkdtemp(prefix='bup-tmetadata-')
156 path = tmpdir + '/foo'
157 subprocess.call(['mkdir', path])
158 m = metadata.from_path(path, archive_path=path, save_symlinks=True)
159 WVPASSEQ(m.path, path)
160 WVPASSEQ(m.apply_to_path(path), None)
163 m.apply_to_path(path, restore_numeric_ids=True)
164 errmsg = str(helpers.saved_errors[0]) if helpers.saved_errors else ''
165 WVPASS(errmsg.startswith('lchown: '))
169 m.apply_to_path(path, restore_numeric_ids=True)
170 errmsg = str(helpers.saved_errors[0]) if helpers.saved_errors else ''
171 WVPASS(errmsg.startswith('lchown: ') or os.stat(path).st_gid == m.gid)
174 subprocess.call(['rm', '-rf', tmpdir])
178 def test_restore_nonexistent_user_group():
179 tmpdir = tempfile.mkdtemp(prefix='bup-tmetadata-')
181 path = tmpdir + '/foo'
182 subprocess.call(['mkdir', path])
183 m = metadata.from_path(path, archive_path=path, save_symlinks=True)
184 WVPASSEQ(m.path, path)
185 m.owner = max([x.pw_name for x in pwd.getpwall()], key=len) + 'x'
186 m.group = max([x.gr_name for x in grp.getgrall()], key=len) + 'x'
187 WVPASSEQ(m.apply_to_path(path, restore_numeric_ids=True), None)
188 WVPASSEQ(os.stat(path).st_uid, m.uid)
189 WVPASSEQ(os.stat(path).st_gid, m.gid)
190 WVPASSEQ(m.apply_to_path(path, restore_numeric_ids=False), None)
191 WVPASSEQ(os.stat(path).st_uid, os.geteuid())
192 WVPASSEQ(os.stat(path).st_gid, os.getgid())
194 subprocess.call(['rm', '-rf', tmpdir])
198 def test_restore_over_existing_target():
199 tmpdir = tempfile.mkdtemp(prefix='bup-tmetadata-')
201 path = tmpdir + '/foo'
203 dir_m = metadata.from_path(path, archive_path=path, save_symlinks=True)
205 open(path, 'w').close()
206 file_m = metadata.from_path(path, archive_path=path, save_symlinks=True)
207 # Restore dir over file.
208 WVPASSEQ(dir_m.create_path(path, create_symlinks=True), None)
209 WVPASS(stat.S_ISDIR(os.stat(path).st_mode))
210 # Restore dir over dir.
211 WVPASSEQ(dir_m.create_path(path, create_symlinks=True), None)
212 WVPASS(stat.S_ISDIR(os.stat(path).st_mode))
213 # Restore file over dir.
214 WVPASSEQ(file_m.create_path(path, create_symlinks=True), None)
215 WVPASS(stat.S_ISREG(os.stat(path).st_mode))
216 # Restore file over file.
217 WVPASSEQ(file_m.create_path(path, create_symlinks=True), None)
218 WVPASS(stat.S_ISREG(os.stat(path).st_mode))
219 # Restore file over non-empty dir.
222 open(path + '/bar', 'w').close()
223 WVEXCEPT(Exception, file_m.create_path, path, create_symlinks=True)
224 # Restore dir over non-empty dir.
225 os.remove(path + '/bar')
226 os.mkdir(path + '/bar')
227 WVEXCEPT(Exception, dir_m.create_path, path, create_symlinks=True)
229 subprocess.call(['rm', '-rf', tmpdir])
233 def test_handling_of_incorrect_existing_linux_xattrs():
234 if os.geteuid() != 0 or detect_fakeroot():
237 subprocess.check_call('rm -rf testfs/*', shell=True)
239 open(path, 'w').close()
240 xattr.set(path, 'foo', 'bar', namespace=xattr.NS_USER)
241 m = metadata.from_path(path, archive_path=path, save_symlinks=True)
242 xattr.set(path, 'baz', 'bax', namespace=xattr.NS_USER)
243 m.apply_to_path(path, restore_numeric_ids=False)
244 WVPASSEQ(xattr.list(path), ['user.foo'])
245 WVPASSEQ(xattr.get(path, 'user.foo'), 'bar')
246 xattr.set(path, 'foo', 'baz', namespace=xattr.NS_USER)
247 m.apply_to_path(path, restore_numeric_ids=False)
248 WVPASSEQ(xattr.list(path), ['user.foo'])
249 WVPASSEQ(xattr.get(path, 'user.foo'), 'bar')
250 xattr.remove(path, 'foo', namespace=xattr.NS_USER)
251 m.apply_to_path(path, restore_numeric_ids=False)
252 WVPASSEQ(xattr.list(path), ['user.foo'])
253 WVPASSEQ(xattr.get(path, 'user.foo'), 'bar')