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, is_superuser
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 os.chown('testfs', 0, 0)
37 os.chmod('testfs', 0700)
41 subprocess.call(['umount', 'testfs'])
42 helpers.unlink('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 if helpers.saved_errors:
113 return str(helpers.saved_errors[0])
118 def test_from_path_error():
119 if is_superuser() or detect_fakeroot():
121 tmpdir = tempfile.mkdtemp(prefix='bup-tmetadata-')
123 path = tmpdir + '/foo'
125 m = metadata.from_path(path, archive_path=path, save_symlinks=True)
126 WVPASSEQ(m.path, path)
128 metadata.from_path(path, archive_path=path, save_symlinks=True)
129 if metadata.get_linux_file_attr:
130 WVPASS(len(helpers.saved_errors) == 1)
131 errmsg = _first_err()
132 WVPASS(errmsg.startswith('read Linux attr'))
135 subprocess.call(['rm', '-rf', tmpdir])
139 def test_apply_to_path_restricted_access():
140 if is_superuser() or detect_fakeroot():
142 tmpdir = tempfile.mkdtemp(prefix='bup-tmetadata-')
144 path = tmpdir + '/foo'
147 m = metadata.from_path(path, archive_path=path, save_symlinks=True)
148 WVPASSEQ(m.path, path)
149 os.chmod(tmpdir, 000)
150 m.apply_to_path(path)
151 WVPASS(len(helpers.saved_errors) == 1)
152 errmsg = _first_err()
153 WVPASS(errmsg.startswith('utime: '))
156 subprocess.call(['rm', '-rf', tmpdir])
160 def test_restore_restricted_user_group():
161 if is_superuser() or detect_fakeroot():
163 tmpdir = tempfile.mkdtemp(prefix='bup-tmetadata-')
165 path = tmpdir + '/foo'
167 m = metadata.from_path(path, archive_path=path, save_symlinks=True)
168 WVPASSEQ(m.path, path)
169 WVPASSEQ(m.apply_to_path(path), None)
172 m.apply_to_path(path, restore_numeric_ids=True)
173 WVPASS(len(helpers.saved_errors) == 1)
174 errmsg = _first_err()
175 WVPASS(errmsg.startswith('lchown: '))
179 m.apply_to_path(path, restore_numeric_ids=True)
180 WVPASS(len(helpers.saved_errors) == 1)
181 errmsg = _first_err()
182 WVPASS(errmsg.startswith('lchown: ') or os.stat(path).st_gid == m.gid)
185 subprocess.call(['rm', '-rf', tmpdir])
189 def test_restore_nonexistent_user_group():
190 tmpdir = tempfile.mkdtemp(prefix='bup-tmetadata-')
192 path = tmpdir + '/foo'
194 m = metadata.from_path(path, archive_path=path, save_symlinks=True)
195 WVPASSEQ(m.path, path)
196 junk,m.owner = max([(len(x.pw_name), x.pw_name + 'x')
197 for x in pwd.getpwall()])
198 junk,m.group = max([(len(x.gr_name), x.gr_name + 'x')
199 for x in grp.getgrall()])
200 WVPASSEQ(m.apply_to_path(path, restore_numeric_ids=True), None)
201 WVPASSEQ(os.stat(path).st_uid, m.uid)
202 WVPASSEQ(os.stat(path).st_gid, m.gid)
203 WVPASSEQ(m.apply_to_path(path, restore_numeric_ids=False), None)
204 WVPASSEQ(os.stat(path).st_uid, m.uid)
205 WVPASSEQ(os.stat(path).st_gid, m.gid)
207 subprocess.call(['rm', '-rf', tmpdir])
211 def test_restore_over_existing_target():
212 tmpdir = tempfile.mkdtemp(prefix='bup-tmetadata-')
214 path = tmpdir + '/foo'
216 dir_m = metadata.from_path(path, archive_path=path, save_symlinks=True)
218 open(path, 'w').close()
219 file_m = metadata.from_path(path, archive_path=path, save_symlinks=True)
220 # Restore dir over file.
221 WVPASSEQ(dir_m.create_path(path, create_symlinks=True), None)
222 WVPASS(stat.S_ISDIR(os.stat(path).st_mode))
223 # Restore dir over dir.
224 WVPASSEQ(dir_m.create_path(path, create_symlinks=True), None)
225 WVPASS(stat.S_ISDIR(os.stat(path).st_mode))
226 # Restore file over dir.
227 WVPASSEQ(file_m.create_path(path, create_symlinks=True), None)
228 WVPASS(stat.S_ISREG(os.stat(path).st_mode))
229 # Restore file over file.
230 WVPASSEQ(file_m.create_path(path, create_symlinks=True), None)
231 WVPASS(stat.S_ISREG(os.stat(path).st_mode))
232 # Restore file over non-empty dir.
235 open(path + '/bar', 'w').close()
236 WVEXCEPT(Exception, file_m.create_path, path, create_symlinks=True)
237 # Restore dir over non-empty dir.
238 os.remove(path + '/bar')
239 os.mkdir(path + '/bar')
240 WVEXCEPT(Exception, dir_m.create_path, path, create_symlinks=True)
242 subprocess.call(['rm', '-rf', tmpdir])
245 from bup.metadata import posix1e
248 def POSIX1E_ACL_SUPPORT_IS_MISSING():
252 from bup.metadata import xattr
255 def LINUX_XATTR_SUPPORT_IS_MISSING():
259 def test_handling_of_incorrect_existing_linux_xattrs():
260 if not is_superuser():
263 for f in glob.glob('testfs/*'):
266 open(path, 'w').close()
267 xattr.set(path, 'foo', 'bar', namespace=xattr.NS_USER)
268 m = metadata.from_path(path, archive_path=path, save_symlinks=True)
269 xattr.set(path, 'baz', 'bax', namespace=xattr.NS_USER)
270 m.apply_to_path(path, restore_numeric_ids=False)
271 WVPASSEQ(xattr.list(path), ['user.foo'])
272 WVPASSEQ(xattr.get(path, 'user.foo'), 'bar')
273 xattr.set(path, 'foo', 'baz', namespace=xattr.NS_USER)
274 m.apply_to_path(path, restore_numeric_ids=False)
275 WVPASSEQ(xattr.list(path), ['user.foo'])
276 WVPASSEQ(xattr.get(path, 'user.foo'), 'bar')
277 xattr.remove(path, 'foo', namespace=xattr.NS_USER)
278 m.apply_to_path(path, restore_numeric_ids=False)
279 WVPASSEQ(xattr.list(path), ['user.foo'])
280 WVPASSEQ(xattr.get(path, 'user.foo'), 'bar')