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
6 from bup.xstat import utime, lutime
10 bup_tmp = os.path.realpath('../../../t/tmp')
11 bup_path = top_dir + '/bup'
12 start_dir = os.getcwd()
17 cmd_str = ' '.join(cmd)
18 print >> sys.stderr, cmd_str
19 rc = subprocess.call(cmd)
21 print >> sys.stderr, 'terminated by signal', - rc
24 print >> sys.stderr, 'returned exit status', rc
27 print >> sys.stderr, 'subprocess call failed:', e
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')
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)
46 subprocess.call(['umount', 'testfs'])
47 helpers.unlink('testfs.img')
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(''), '.')
73 def test_risky_path():
74 risky = metadata._risky_path
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'))
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'))
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')
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'
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))
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']))
142 if sub.name == 'file':
144 WVPASS(m.mtime == test_time1)
145 elif sub.name == 'symlink':
148 if wvfailure_count() == initial_failures:
149 subprocess.call(['rm', '-rf', tmpdir])
153 if helpers.saved_errors:
154 return str(helpers.saved_errors[0])
159 def test_from_path_error():
160 initial_failures = wvfailure_count()
161 if is_superuser() or detect_fakeroot():
163 tmpdir = tempfile.mkdtemp(dir=bup_tmp, prefix='bup-tmetadata-')
164 path = tmpdir + '/foo'
166 m = metadata.from_path(path, archive_path=path, save_symlinks=True)
167 WVPASSEQ(m.path, path)
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'))
175 if wvfailure_count() == initial_failures:
176 subprocess.call(['chmod', '-R', 'u+rwX', tmpdir])
177 subprocess.call(['rm', '-rf', tmpdir])
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:
185 metadata.get_linux_file_attr(path)
187 if e.errno in (errno.ENOTTY, errno.ENOSYS, errno.EOPNOTSUPP):
195 def test_apply_to_path_restricted_access():
196 initial_failures = wvfailure_count()
197 if is_superuser() or detect_fakeroot():
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'
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]))
221 if wvfailure_count() == initial_failures:
222 subprocess.call(['chmod', '-R', 'u+rwX', tmpdir])
223 subprocess.call(['rm', '-rf', tmpdir])
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'
232 dir_m = metadata.from_path(path, archive_path=path, save_symlinks=True)
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.
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])
261 from bup.metadata import posix1e
264 def POSIX1E_ACL_SUPPORT_IS_MISSING():
268 from bup.metadata import xattr
271 def test_handling_of_incorrect_existing_linux_xattrs():
272 if not is_superuser() or detect_fakeroot():
273 WVMSG('skipping test -- not superuser')
276 for f in glob.glob('testfs/*'):
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')