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, resolve_parent
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 if subprocess.call(['modprobe', 'loop']) != 0:
36 subprocess.call(['umount', 'testfs'])
37 ex('dd', 'if=/dev/zero', 'of=testfs.img', 'bs=1M', 'count=32')
38 ex('mke2fs', '-F', '-j', '-m', '0', 'testfs.img')
39 ex('rm', '-rf', 'testfs')
41 ex('mount', '-o', 'loop,acl,user_xattr', 'testfs.img', 'testfs')
42 # Hide, so that tests can't create risks.
43 os.chown('testfs', 0, 0)
44 os.chmod('testfs', 0o700)
49 subprocess.call(['umount', 'testfs'])
50 helpers.unlink('testfs.img')
54 def test_clean_up_archive_path():
55 cleanup = metadata._clean_up_path_for_archive
56 WVPASSEQ(cleanup('foo'), 'foo')
57 WVPASSEQ(cleanup('/foo'), 'foo')
58 WVPASSEQ(cleanup('///foo'), 'foo')
59 WVPASSEQ(cleanup('/foo/bar'), 'foo/bar')
60 WVPASSEQ(cleanup('foo/./bar'), 'foo/bar')
61 WVPASSEQ(cleanup('/foo/./bar'), 'foo/bar')
62 WVPASSEQ(cleanup('/foo/./bar/././baz'), 'foo/bar/baz')
63 WVPASSEQ(cleanup('/foo/./bar///././baz'), 'foo/bar/baz')
64 WVPASSEQ(cleanup('//./foo/./bar///././baz/.///'), 'foo/bar/baz/')
65 WVPASSEQ(cleanup('./foo/./.bar'), 'foo/.bar')
66 WVPASSEQ(cleanup('./foo/.'), 'foo')
67 WVPASSEQ(cleanup('./foo/..'), '.')
68 WVPASSEQ(cleanup('//./..//.../..//.'), '.')
69 WVPASSEQ(cleanup('//./..//..././/.'), '...')
70 WVPASSEQ(cleanup('/////.'), '.')
71 WVPASSEQ(cleanup('/../'), '.')
72 WVPASSEQ(cleanup(''), '.')
76 def test_risky_path():
77 risky = metadata._risky_path
79 WVPASS(risky('///foo'))
80 WVPASS(risky('/../foo'))
81 WVPASS(risky('../foo'))
82 WVPASS(risky('foo/..'))
83 WVPASS(risky('foo/../'))
84 WVPASS(risky('foo/../bar'))
87 WVFAIL(risky('foo///'))
88 WVFAIL(risky('./foo'))
89 WVFAIL(risky('foo/.'))
90 WVFAIL(risky('./foo/.'))
91 WVFAIL(risky('foo/bar'))
92 WVFAIL(risky('foo/./bar'))
96 def test_clean_up_extract_path():
97 cleanup = metadata._clean_up_extract_path
98 WVPASSEQ(cleanup('/foo'), 'foo')
99 WVPASSEQ(cleanup('///foo'), 'foo')
100 WVFAIL(cleanup('/../foo'))
101 WVFAIL(cleanup('../foo'))
102 WVFAIL(cleanup('foo/..'))
103 WVFAIL(cleanup('foo/../'))
104 WVFAIL(cleanup('foo/../bar'))
105 WVPASSEQ(cleanup('foo'), 'foo')
106 WVPASSEQ(cleanup('foo/'), 'foo/')
107 WVPASSEQ(cleanup('foo///'), 'foo///')
108 WVPASSEQ(cleanup('./foo'), './foo')
109 WVPASSEQ(cleanup('foo/.'), 'foo/.')
110 WVPASSEQ(cleanup('./foo/.'), './foo/.')
111 WVPASSEQ(cleanup('foo/bar'), 'foo/bar')
112 WVPASSEQ(cleanup('foo/./bar'), 'foo/./bar')
113 WVPASSEQ(cleanup('/'), '.')
114 WVPASSEQ(cleanup('./'), './')
115 WVPASSEQ(cleanup('///foo/bar'), 'foo/bar')
116 WVPASSEQ(cleanup('///foo/bar'), 'foo/bar')
120 def test_metadata_method():
121 initial_failures = wvfailure_count()
122 tmpdir = tempfile.mkdtemp(dir=bup_tmp, prefix='bup-tmetadata-')
123 bup_dir = tmpdir + '/bup'
124 data_path = tmpdir + '/foo'
126 ex('touch', data_path + '/file')
127 ex('ln', '-s', 'file', data_path + '/symlink')
128 test_time1 = 13 * 1000000000
129 test_time2 = 42 * 1000000000
130 utime(data_path + '/file', (0, test_time1))
131 lutime(data_path + '/symlink', (0, 0))
132 utime(data_path, (0, test_time2))
133 ex(bup_path, '-d', bup_dir, 'init')
134 ex(bup_path, '-d', bup_dir, 'index', '-v', data_path)
135 ex(bup_path, '-d', bup_dir, 'save', '-tvvn', 'test', data_path)
136 git.check_repo_or_die(bup_dir)
137 top = vfs.RefList(None)
138 n = top.lresolve('/test/latest' + resolve_parent(data_path))
140 WVPASS(m.mtime == test_time2)
141 WVPASS(len(n.subs()) == 2)
142 WVPASS(n.name == 'foo')
143 WVPASS(set([x.name for x in n.subs()]) == set(['file', 'symlink']))
145 if sub.name == 'file':
147 WVPASS(m.mtime == test_time1)
148 elif sub.name == 'symlink':
151 if wvfailure_count() == initial_failures:
152 subprocess.call(['rm', '-rf', tmpdir])
156 if helpers.saved_errors:
157 return str(helpers.saved_errors[0])
162 def test_from_path_error():
163 initial_failures = wvfailure_count()
164 if is_superuser() or detect_fakeroot():
166 tmpdir = tempfile.mkdtemp(dir=bup_tmp, prefix='bup-tmetadata-')
167 path = tmpdir + '/foo'
169 m = metadata.from_path(path, archive_path=path, save_symlinks=True)
170 WVPASSEQ(m.path, path)
172 metadata.from_path(path, archive_path=path, save_symlinks=True)
173 if metadata.get_linux_file_attr:
174 print >> sys.stderr, 'saved_errors:', helpers.saved_errors
175 WVPASS(len(helpers.saved_errors) == 1)
176 errmsg = _first_err()
177 WVPASS(errmsg.startswith('read Linux attr'))
179 if wvfailure_count() == initial_failures:
180 subprocess.call(['chmod', '-R', 'u+rwX', tmpdir])
181 subprocess.call(['rm', '-rf', tmpdir])
184 def _linux_attr_supported(path):
185 # Expects path to denote a regular file or a directory.
186 if not metadata.get_linux_file_attr:
189 metadata.get_linux_file_attr(path)
191 if e.errno in (errno.ENOTTY, errno.ENOSYS, errno.EOPNOTSUPP):
199 def test_apply_to_path_restricted_access():
200 initial_failures = wvfailure_count()
201 if is_superuser() or detect_fakeroot():
203 if sys.platform.startswith('cygwin'):
204 return # chmod 000 isn't effective.
205 tmpdir = tempfile.mkdtemp(dir=bup_tmp, prefix='bup-tmetadata-')
206 parent = tmpdir + '/foo'
207 path = parent + '/bar'
211 m = metadata.from_path(path, archive_path=path, save_symlinks=True)
212 WVPASSEQ(m.path, path)
213 os.chmod(parent, 000)
214 m.apply_to_path(path)
215 print >> sys.stderr, 'saved_errors:', helpers.saved_errors
216 expected_errors = ['utime: ']
217 if m.linux_attr and _linux_attr_supported(tmpdir):
218 expected_errors.append('Linux chattr: ')
219 if metadata.xattr and m.linux_xattr:
220 expected_errors.append("xattr.set '")
221 WVPASS(len(helpers.saved_errors) == len(expected_errors))
222 for i in xrange(len(expected_errors)):
223 WVPASS(str(helpers.saved_errors[i]).startswith(expected_errors[i]))
225 if wvfailure_count() == initial_failures:
226 subprocess.call(['chmod', '-R', 'u+rwX', tmpdir])
227 subprocess.call(['rm', '-rf', tmpdir])
231 def test_restore_over_existing_target():
232 initial_failures = wvfailure_count()
233 tmpdir = tempfile.mkdtemp(dir=bup_tmp, prefix='bup-tmetadata-')
234 path = tmpdir + '/foo'
236 dir_m = metadata.from_path(path, archive_path=path, save_symlinks=True)
238 open(path, 'w').close()
239 file_m = metadata.from_path(path, archive_path=path, save_symlinks=True)
240 # Restore dir over file.
241 WVPASSEQ(dir_m.create_path(path, create_symlinks=True), None)
242 WVPASS(stat.S_ISDIR(os.stat(path).st_mode))
243 # Restore dir over dir.
244 WVPASSEQ(dir_m.create_path(path, create_symlinks=True), None)
245 WVPASS(stat.S_ISDIR(os.stat(path).st_mode))
246 # Restore file over dir.
247 WVPASSEQ(file_m.create_path(path, create_symlinks=True), None)
248 WVPASS(stat.S_ISREG(os.stat(path).st_mode))
249 # Restore file over file.
250 WVPASSEQ(file_m.create_path(path, create_symlinks=True), None)
251 WVPASS(stat.S_ISREG(os.stat(path).st_mode))
252 # Restore file over non-empty dir.
255 open(path + '/bar', 'w').close()
256 WVEXCEPT(Exception, file_m.create_path, path, create_symlinks=True)
257 # Restore dir over non-empty dir.
258 os.remove(path + '/bar')
259 os.mkdir(path + '/bar')
260 WVEXCEPT(Exception, dir_m.create_path, path, create_symlinks=True)
261 if wvfailure_count() == initial_failures:
262 subprocess.call(['rm', '-rf', tmpdir])
265 from bup.metadata import posix1e
268 def POSIX1E_ACL_SUPPORT_IS_MISSING():
272 from bup.metadata import xattr
275 def test_handling_of_incorrect_existing_linux_xattrs():
276 if not is_superuser() or detect_fakeroot():
277 WVMSG('skipping test -- not superuser')
279 if not setup_testfs():
280 WVMSG('unable to load loop module; skipping dependent tests')
282 for f in glob.glob('testfs/*'):
285 open(path, 'w').close()
286 xattr.set(path, 'foo', 'bar', namespace=xattr.NS_USER)
287 m = metadata.from_path(path, archive_path=path, save_symlinks=True)
288 xattr.set(path, 'baz', 'bax', namespace=xattr.NS_USER)
289 m.apply_to_path(path, restore_numeric_ids=False)
290 WVPASSEQ(xattr.list(path), ['user.foo'])
291 WVPASSEQ(xattr.get(path, 'user.foo'), 'bar')
292 xattr.set(path, 'foo', 'baz', namespace=xattr.NS_USER)
293 m.apply_to_path(path, restore_numeric_ids=False)
294 WVPASSEQ(xattr.list(path), ['user.foo'])
295 WVPASSEQ(xattr.get(path, 'user.foo'), 'bar')
296 xattr.remove(path, 'foo', namespace=xattr.NS_USER)
297 m.apply_to_path(path, restore_numeric_ids=False)
298 WVPASSEQ(xattr.list(path), ['user.foo'])
299 WVPASSEQ(xattr.get(path, 'user.foo'), 'bar')