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_path = top_dir + '/bup'
11 start_dir = os.getcwd()
16 cmd_str = ' '.join(cmd)
17 print >> sys.stderr, cmd_str
18 rc = subprocess.call(cmd)
20 print >> sys.stderr, 'terminated by signal', - rc
23 print >> sys.stderr, 'returned exit status', rc
26 print >> sys.stderr, 'subprocess call failed:', e
31 assert(sys.platform.startswith('linux'))
32 # Set up testfs with user_xattr, etc.
33 subprocess.call(['umount', 'testfs'])
34 ex('dd', 'if=/dev/zero', 'of=testfs.img', 'bs=1M', 'count=32')
35 ex('mke2fs', '-F', '-j', '-m', '0', 'testfs.img')
36 ex('rm', '-rf', 'testfs')
38 ex('mount', '-o', 'loop,acl,user_xattr', 'testfs.img', 'testfs')
39 # Hide, so that tests can't create risks.
40 os.chown('testfs', 0, 0)
41 os.chmod('testfs', 0700)
45 subprocess.call(['umount', 'testfs'])
46 helpers.unlink('testfs.img')
50 def test_clean_up_archive_path():
51 cleanup = metadata._clean_up_path_for_archive
52 WVPASSEQ(cleanup('foo'), 'foo')
53 WVPASSEQ(cleanup('/foo'), 'foo')
54 WVPASSEQ(cleanup('///foo'), 'foo')
55 WVPASSEQ(cleanup('/foo/bar'), 'foo/bar')
56 WVPASSEQ(cleanup('foo/./bar'), 'foo/bar')
57 WVPASSEQ(cleanup('/foo/./bar'), 'foo/bar')
58 WVPASSEQ(cleanup('/foo/./bar/././baz'), 'foo/bar/baz')
59 WVPASSEQ(cleanup('/foo/./bar///././baz'), 'foo/bar/baz')
60 WVPASSEQ(cleanup('//./foo/./bar///././baz/.///'), 'foo/bar/baz/')
61 WVPASSEQ(cleanup('./foo/./.bar'), 'foo/.bar')
62 WVPASSEQ(cleanup('./foo/.'), 'foo')
63 WVPASSEQ(cleanup('./foo/..'), '.')
64 WVPASSEQ(cleanup('//./..//.../..//.'), '.')
65 WVPASSEQ(cleanup('//./..//..././/.'), '...')
66 WVPASSEQ(cleanup('/////.'), '.')
67 WVPASSEQ(cleanup('/../'), '.')
68 WVPASSEQ(cleanup(''), '.')
72 def test_risky_path():
73 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/../bar'))
83 WVFAIL(risky('foo///'))
84 WVFAIL(risky('./foo'))
85 WVFAIL(risky('foo/.'))
86 WVFAIL(risky('./foo/.'))
87 WVFAIL(risky('foo/bar'))
88 WVFAIL(risky('foo/./bar'))
92 def test_clean_up_extract_path():
93 cleanup = metadata._clean_up_extract_path
94 WVPASSEQ(cleanup('/foo'), 'foo')
95 WVPASSEQ(cleanup('///foo'), 'foo')
96 WVFAIL(cleanup('/../foo'))
97 WVFAIL(cleanup('../foo'))
98 WVFAIL(cleanup('foo/..'))
99 WVFAIL(cleanup('foo/../'))
100 WVFAIL(cleanup('foo/../bar'))
101 WVPASSEQ(cleanup('foo'), 'foo')
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/bar'), 'foo/bar')
108 WVPASSEQ(cleanup('foo/./bar'), 'foo/./bar')
109 WVPASSEQ(cleanup('/'), '.')
110 WVPASSEQ(cleanup('./'), './')
111 WVPASSEQ(cleanup('///foo/bar'), 'foo/bar')
112 WVPASSEQ(cleanup('///foo/bar'), 'foo/bar')
116 def test_metadata_method():
117 tmpdir = tempfile.mkdtemp(prefix='bup-tmetadata-')
119 bup_dir = tmpdir + '/bup'
120 data_path = tmpdir + '/foo'
122 ex('touch', data_path + '/file')
123 ex('ln', '-s', 'file', data_path + '/symlink')
124 test_time1 = 13 * 1000000000
125 test_time2 = 42 * 1000000000
126 utime(data_path + '/file', (0, test_time1))
127 lutime(data_path + '/symlink', (0, 0))
128 utime(data_path, (0, test_time2))
129 ex(bup_path, '-d', bup_dir, 'init')
130 ex(bup_path, '-d', bup_dir, 'index', '-v', data_path)
131 ex(bup_path, '-d', bup_dir, 'save', '-tvvn', 'test', data_path)
132 git.check_repo_or_die(bup_dir)
133 top = vfs.RefList(None)
134 n = top.lresolve('/test/latest' + realpath(data_path))
136 WVPASS(m.mtime == test_time2)
137 WVPASS(len(n.subs()) == 2)
138 WVPASS(n.name == 'foo')
139 WVPASS(set([x.name for x in n.subs()]) == set(['file', 'symlink']))
141 if sub.name == 'file':
143 WVPASS(m.mtime == test_time1)
144 elif sub.name == 'symlink':
148 subprocess.call(['rm', '-rf', tmpdir])
152 if helpers.saved_errors:
153 return str(helpers.saved_errors[0])
158 def test_from_path_error():
159 if is_superuser() or detect_fakeroot():
161 tmpdir = tempfile.mkdtemp(prefix='bup-tmetadata-')
163 path = tmpdir + '/foo'
165 m = metadata.from_path(path, archive_path=path, save_symlinks=True)
166 WVPASSEQ(m.path, path)
168 metadata.from_path(path, archive_path=path, save_symlinks=True)
169 if metadata.get_linux_file_attr:
170 WVPASS(len(helpers.saved_errors) == 1)
171 errmsg = _first_err()
172 WVPASS(errmsg.startswith('read Linux attr'))
175 subprocess.call(['rm', '-rf', tmpdir])
178 def _linux_attr_supported(path):
179 # Expects path to denote a regular file or a directory.
180 if not metadata.get_linux_file_attr:
183 metadata.get_linux_file_attr(path)
185 if e.errno in (errno.ENOTTY, errno.ENOSYS, errno.EOPNOTSUPP):
192 def _linux_xattr_supported(path):
193 # NOTE: destructive test (tries to write to path).
194 if not metadata.xattr:
197 xattr.set(path, 'user.bup-test-xattr-support', 'true', nofollow=True)
199 if e.errno == errno.EOPNOTSUPP:
207 def test_apply_to_path_restricted_access():
208 if is_superuser() or detect_fakeroot():
210 if sys.platform.startswith('cygwin'):
211 return # chmod 000 isn't effective.
212 tmpdir = tempfile.mkdtemp(prefix='bup-tmetadata-')
214 parent = tmpdir + '/foo'
215 path = parent + '/bar'
219 m = metadata.from_path(path, archive_path=path, save_symlinks=True)
220 WVPASSEQ(m.path, path)
221 os.chmod(parent, 000)
222 m.apply_to_path(path)
223 print >> sys.stderr, helpers.saved_errors
224 expected_errors = ['utime: ']
225 if m.linux_attr and _linux_attr_supported(tmpdir):
226 expected_errors.append('Linux chattr: ')
227 if _linux_xattr_supported(tmpdir):
228 expected_errors.append('xattr.set: ')
229 WVPASS(len(helpers.saved_errors) == len(expected_errors))
230 for i in xrange(len(expected_errors)):
231 WVPASS(str(helpers.saved_errors[i]).startswith(expected_errors[i]))
234 subprocess.call(['chmod', '-R', 'u+rwX', tmpdir])
235 subprocess.call(['rm', '-rf', tmpdir])
239 def test_restore_over_existing_target():
240 tmpdir = tempfile.mkdtemp(prefix='bup-tmetadata-')
242 path = tmpdir + '/foo'
244 dir_m = metadata.from_path(path, archive_path=path, save_symlinks=True)
246 open(path, 'w').close()
247 file_m = metadata.from_path(path, archive_path=path, save_symlinks=True)
248 # Restore dir over file.
249 WVPASSEQ(dir_m.create_path(path, create_symlinks=True), None)
250 WVPASS(stat.S_ISDIR(os.stat(path).st_mode))
251 # Restore dir over dir.
252 WVPASSEQ(dir_m.create_path(path, create_symlinks=True), None)
253 WVPASS(stat.S_ISDIR(os.stat(path).st_mode))
254 # Restore file over dir.
255 WVPASSEQ(file_m.create_path(path, create_symlinks=True), None)
256 WVPASS(stat.S_ISREG(os.stat(path).st_mode))
257 # Restore file over file.
258 WVPASSEQ(file_m.create_path(path, create_symlinks=True), None)
259 WVPASS(stat.S_ISREG(os.stat(path).st_mode))
260 # Restore file over non-empty dir.
263 open(path + '/bar', 'w').close()
264 WVEXCEPT(Exception, file_m.create_path, path, create_symlinks=True)
265 # Restore dir over non-empty dir.
266 os.remove(path + '/bar')
267 os.mkdir(path + '/bar')
268 WVEXCEPT(Exception, dir_m.create_path, path, create_symlinks=True)
270 subprocess.call(['rm', '-rf', tmpdir])
273 from bup.metadata import posix1e
276 def POSIX1E_ACL_SUPPORT_IS_MISSING():
280 from bup.metadata import xattr
283 def test_handling_of_incorrect_existing_linux_xattrs():
284 if not is_superuser() or detect_fakeroot():
285 WVMSG('skipping test -- not superuser')
288 for f in glob.glob('testfs/*'):
291 open(path, 'w').close()
292 xattr.set(path, 'foo', 'bar', namespace=xattr.NS_USER)
293 m = metadata.from_path(path, archive_path=path, save_symlinks=True)
294 xattr.set(path, 'baz', 'bax', namespace=xattr.NS_USER)
295 m.apply_to_path(path, restore_numeric_ids=False)
296 WVPASSEQ(xattr.list(path), ['user.foo'])
297 WVPASSEQ(xattr.get(path, 'user.foo'), 'bar')
298 xattr.set(path, 'foo', 'baz', namespace=xattr.NS_USER)
299 m.apply_to_path(path, restore_numeric_ids=False)
300 WVPASSEQ(xattr.list(path), ['user.foo'])
301 WVPASSEQ(xattr.get(path, 'user.foo'), 'bar')
302 xattr.remove(path, 'foo', namespace=xattr.NS_USER)
303 m.apply_to_path(path, restore_numeric_ids=False)
304 WVPASSEQ(xattr.list(path), ['user.foo'])
305 WVPASSEQ(xattr.get(path, 'user.foo'), 'bar')