import glob, grp, pwd, stat, tempfile, subprocess
import bup.helpers as helpers
from bup import metadata
-from bup.helpers import clear_errors, detect_fakeroot
+from bup.helpers import clear_errors, detect_fakeroot, is_superuser
from wvtest import *
def cleanup_testfs():
subprocess.call(['umount', 'testfs'])
- unlink('testfs.img')
+ helpers.unlink('testfs.img')
@wvtest
WVPASSEQ(cleanup('///foo/bar'), 'foo/bar')
+def _first_err():
+ if helpers.saved_errors:
+ return str(helpers.saved_errors[0])
+ return ''
+
+
@wvtest
def test_from_path_error():
- if os.geteuid() == 0 or detect_fakeroot():
+ if is_superuser() or detect_fakeroot():
return
tmpdir = tempfile.mkdtemp(prefix='bup-tmetadata-')
try:
os.chmod(path, 000)
metadata.from_path(path, archive_path=path, save_symlinks=True)
if metadata.get_linux_file_attr:
- errmsg = helpers.saved_errors[0] if helpers.saved_errors else ''
+ WVPASS(len(helpers.saved_errors) == 1)
+ errmsg = _first_err()
WVPASS(errmsg.startswith('read Linux attr'))
clear_errors()
finally:
@wvtest
def test_apply_to_path_restricted_access():
- if os.geteuid() == 0 or detect_fakeroot():
+ if is_superuser() or detect_fakeroot():
return
tmpdir = tempfile.mkdtemp(prefix='bup-tmetadata-')
try:
WVPASSEQ(m.path, path)
os.chmod(tmpdir, 000)
m.apply_to_path(path)
- errmsg = str(helpers.saved_errors[0]) if helpers.saved_errors else ''
+ WVPASS(len(helpers.saved_errors) == 1)
+ errmsg = _first_err()
WVPASS(errmsg.startswith('utime: '))
clear_errors()
finally:
@wvtest
def test_restore_restricted_user_group():
- if os.geteuid() == 0 or detect_fakeroot():
+ if is_superuser() or detect_fakeroot():
return
tmpdir = tempfile.mkdtemp(prefix='bup-tmetadata-')
try:
orig_uid = m.uid
m.uid = 0;
m.apply_to_path(path, restore_numeric_ids=True)
- errmsg = str(helpers.saved_errors[0]) if helpers.saved_errors else ''
+ WVPASS(len(helpers.saved_errors) == 1)
+ errmsg = _first_err()
WVPASS(errmsg.startswith('lchown: '))
clear_errors()
m.uid = orig_uid
m.gid = 0;
m.apply_to_path(path, restore_numeric_ids=True)
- errmsg = str(helpers.saved_errors[0]) if helpers.saved_errors else ''
+ WVPASS(len(helpers.saved_errors) == 1)
+ errmsg = _first_err()
WVPASS(errmsg.startswith('lchown: ') or os.stat(path).st_gid == m.gid)
clear_errors()
finally:
os.mkdir(path)
m = metadata.from_path(path, archive_path=path, save_symlinks=True)
WVPASSEQ(m.path, path)
- m.owner = max([x.pw_name for x in pwd.getpwall()], key=len) + 'x'
- m.group = max([x.gr_name for x in grp.getgrall()], key=len) + 'x'
+ junk,m.user = max([(len(x.pw_name), x.pw_name + 'x')
+ for x in pwd.getpwall()])
+ junk,m.group = max([(len(x.gr_name), x.gr_name + 'x')
+ for x in grp.getgrall()])
WVPASSEQ(m.apply_to_path(path, restore_numeric_ids=True), None)
WVPASSEQ(os.stat(path).st_uid, m.uid)
WVPASSEQ(os.stat(path).st_gid, m.gid)
else:
@wvtest
def test_handling_of_incorrect_existing_linux_xattrs():
- if os.geteuid() != 0 or detect_fakeroot():
+ if not is_superuser():
return
setup_testfs()
for f in glob.glob('testfs/*'):