1 import glob, grp, platform, 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
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('Linux' in platform.system())
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' + 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])
179 def test_apply_to_path_restricted_access():
180 if is_superuser() or detect_fakeroot():
182 tmpdir = tempfile.mkdtemp(prefix='bup-tmetadata-')
184 path = tmpdir + '/foo'
187 m = metadata.from_path(path, archive_path=path, save_symlinks=True)
188 WVPASSEQ(m.path, path)
189 os.chmod(tmpdir, 000)
190 m.apply_to_path(path)
191 WVPASS(len(helpers.saved_errors) == 1)
192 errmsg = _first_err()
193 WVPASS(errmsg.startswith('utime: '))
196 subprocess.call(['rm', '-rf', tmpdir])
200 def test_restore_over_existing_target():
201 tmpdir = tempfile.mkdtemp(prefix='bup-tmetadata-')
203 path = tmpdir + '/foo'
205 dir_m = metadata.from_path(path, archive_path=path, save_symlinks=True)
207 open(path, 'w').close()
208 file_m = metadata.from_path(path, archive_path=path, save_symlinks=True)
209 # Restore dir over file.
210 WVPASSEQ(dir_m.create_path(path, create_symlinks=True), None)
211 WVPASS(stat.S_ISDIR(os.stat(path).st_mode))
212 # Restore dir over dir.
213 WVPASSEQ(dir_m.create_path(path, create_symlinks=True), None)
214 WVPASS(stat.S_ISDIR(os.stat(path).st_mode))
215 # Restore file over dir.
216 WVPASSEQ(file_m.create_path(path, create_symlinks=True), None)
217 WVPASS(stat.S_ISREG(os.stat(path).st_mode))
218 # Restore file over file.
219 WVPASSEQ(file_m.create_path(path, create_symlinks=True), None)
220 WVPASS(stat.S_ISREG(os.stat(path).st_mode))
221 # Restore file over non-empty dir.
224 open(path + '/bar', 'w').close()
225 WVEXCEPT(Exception, file_m.create_path, path, create_symlinks=True)
226 # Restore dir over non-empty dir.
227 os.remove(path + '/bar')
228 os.mkdir(path + '/bar')
229 WVEXCEPT(Exception, dir_m.create_path, path, create_symlinks=True)
231 subprocess.call(['rm', '-rf', tmpdir])
234 from bup.metadata import posix1e
237 def POSIX1E_ACL_SUPPORT_IS_MISSING():
241 from bup.metadata import xattr
244 def test_handling_of_incorrect_existing_linux_xattrs():
245 if not is_superuser():
246 WVMSG('skipping test -- not superuser')
249 for f in glob.glob('testfs/*'):
252 open(path, 'w').close()
253 xattr.set(path, 'foo', 'bar', namespace=xattr.NS_USER)
254 m = metadata.from_path(path, archive_path=path, save_symlinks=True)
255 xattr.set(path, 'baz', 'bax', namespace=xattr.NS_USER)
256 m.apply_to_path(path, restore_numeric_ids=False)
257 WVPASSEQ(xattr.list(path), ['user.foo'])
258 WVPASSEQ(xattr.get(path, 'user.foo'), 'bar')
259 xattr.set(path, 'foo', 'baz', namespace=xattr.NS_USER)
260 m.apply_to_path(path, restore_numeric_ids=False)
261 WVPASSEQ(xattr.list(path), ['user.foo'])
262 WVPASSEQ(xattr.get(path, 'user.foo'), 'bar')
263 xattr.remove(path, 'foo', namespace=xattr.NS_USER)
264 m.apply_to_path(path, restore_numeric_ids=False)
265 WVPASSEQ(xattr.list(path), ['user.foo'])
266 WVPASSEQ(xattr.get(path, 'user.foo'), 'bar')