]> arthur.barton.de Git - bup.git/blob - lib/bup/t/tmetadata.py
Use Python 3 compatible octal notation
[bup.git] / lib / bup / t / tmetadata.py
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
5 from wvtest import *
6 from bup.xstat import utime, lutime
7
8
9 top_dir = '../../..'
10 bup_tmp = os.path.realpath('../../../t/tmp')
11 bup_path = top_dir + '/bup'
12 start_dir = os.getcwd()
13
14
15 def ex(*cmd):
16     try:
17         cmd_str = ' '.join(cmd)
18         print >> sys.stderr, cmd_str
19         rc = subprocess.call(cmd)
20         if rc < 0:
21             print >> sys.stderr, 'terminated by signal', - rc
22             sys.exit(1)
23         elif rc > 0:
24             print >> sys.stderr, 'returned exit status', rc
25             sys.exit(1)
26     except OSError, e:
27         print >> sys.stderr, 'subprocess call failed:', e
28         sys.exit(1)
29
30
31 def setup_testfs():
32     assert(sys.platform.startswith('linux'))
33     # Set up testfs with user_xattr, etc.
34     if subprocess.call(['modprobe', 'loop']) != 0:
35         return False
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')
40     os.mkdir('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)
45     return True
46
47
48 def cleanup_testfs():
49     subprocess.call(['umount', 'testfs'])
50     helpers.unlink('testfs.img')
51
52
53 @wvtest
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(''), '.')
73
74
75 @wvtest
76 def test_risky_path():
77     risky = metadata._risky_path
78     WVPASS(risky('/foo'))
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'))
85     WVFAIL(risky('foo'))
86     WVFAIL(risky('foo/'))
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'))
93
94
95 @wvtest
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')
117
118
119 @wvtest
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'
125     os.mkdir(data_path)
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' + realpath(data_path))
139     m = n.metadata()
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']))
144     for sub in n:
145         if sub.name == 'file':
146             m = sub.metadata()
147             WVPASS(m.mtime == test_time1)
148         elif sub.name == 'symlink':
149             m = sub.metadata()
150             WVPASS(m.mtime == 0)
151     if wvfailure_count() == initial_failures:
152         subprocess.call(['rm', '-rf', tmpdir])
153
154
155 def _first_err():
156     if helpers.saved_errors:
157         return str(helpers.saved_errors[0])
158     return ''
159
160
161 @wvtest
162 def test_from_path_error():
163     initial_failures = wvfailure_count()
164     if is_superuser() or detect_fakeroot():
165         return
166     tmpdir = tempfile.mkdtemp(dir=bup_tmp, prefix='bup-tmetadata-')
167     path = tmpdir + '/foo'
168     os.mkdir(path)
169     m = metadata.from_path(path, archive_path=path, save_symlinks=True)
170     WVPASSEQ(m.path, path)
171     os.chmod(path, 000)
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'))
178         clear_errors()
179     if wvfailure_count() == initial_failures:
180         subprocess.call(['chmod', '-R', 'u+rwX', tmpdir])
181         subprocess.call(['rm', '-rf', tmpdir])
182
183
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:
187         return False
188     try:
189         metadata.get_linux_file_attr(path)
190     except OSError, e:
191         if e.errno in (errno.ENOTTY, errno.ENOSYS, errno.EOPNOTSUPP):
192             return False
193         else:
194             raise
195     return True
196
197
198 @wvtest
199 def test_apply_to_path_restricted_access():
200     initial_failures = wvfailure_count()
201     if is_superuser() or detect_fakeroot():
202         return
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'
208     os.mkdir(parent)
209     os.mkdir(path)
210     clear_errors()
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]))
224     clear_errors()
225     if wvfailure_count() == initial_failures:
226         subprocess.call(['chmod', '-R', 'u+rwX', tmpdir])
227         subprocess.call(['rm', '-rf', tmpdir])
228
229
230 @wvtest
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'
235     os.mkdir(path)
236     dir_m = metadata.from_path(path, archive_path=path, save_symlinks=True)
237     os.rmdir(path)
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.
253     os.remove(path)
254     os.mkdir(path)
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])
263
264
265 from bup.metadata import posix1e
266 if not posix1e:
267     @wvtest
268     def POSIX1E_ACL_SUPPORT_IS_MISSING():
269         pass
270
271
272 from bup.metadata import xattr
273 if xattr:
274     @wvtest
275     def test_handling_of_incorrect_existing_linux_xattrs():
276         if not is_superuser() or detect_fakeroot():
277             WVMSG('skipping test -- not superuser')
278             return
279         if not setup_testfs():
280             WVMSG('unable to load loop module; skipping dependent tests')
281             return
282         for f in glob.glob('testfs/*'):
283             ex('rm', '-rf', f)
284         path = 'testfs/foo'
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')
300         os.chdir(start_dir)
301         cleanup_testfs()