mirror of
https://github.com/borgbackup/borg.git
synced 2025-01-01 12:45:34 +00:00
Xattr.py unittest to pytest conversion (#7657)
This commit is contained in:
parent
96076a71d2
commit
a5c4d0d310
1 changed files with 64 additions and 61 deletions
|
@ -1,81 +1,84 @@
|
||||||
import os
|
import os
|
||||||
import tempfile
|
|
||||||
import unittest
|
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from ..platform.xattr import buffer, split_lstring
|
from ..platform.xattr import buffer, split_lstring
|
||||||
from ..xattr import is_enabled, getxattr, setxattr, listxattr
|
from ..xattr import is_enabled, getxattr, setxattr, listxattr
|
||||||
from ..platformflags import is_linux
|
from ..platformflags import is_linux
|
||||||
from . import BaseTestCase
|
|
||||||
|
|
||||||
|
|
||||||
@unittest.skipUnless(is_enabled(), "xattr not enabled on filesystem")
|
@pytest.fixture()
|
||||||
class XattrTestCase(BaseTestCase):
|
def tempfile_symlink(tmp_path):
|
||||||
def setUp(self):
|
if not is_enabled(tmp_path):
|
||||||
self.tmpfile = tempfile.NamedTemporaryFile()
|
pytest.skip("xattr not enabled on filesystem")
|
||||||
self.symlink = self.tmpfile.name + ".symlink"
|
with open(os.fspath(tmp_path / "xattr"), "w") as temp_file:
|
||||||
os.symlink(self.tmpfile.name, self.symlink)
|
symlink = temp_file.name + ".symlink"
|
||||||
|
os.symlink(temp_file.name, symlink)
|
||||||
|
yield temp_file, symlink
|
||||||
|
|
||||||
def tearDown(self):
|
|
||||||
os.unlink(self.symlink)
|
|
||||||
|
|
||||||
def assert_equal_se(self, is_x, want_x):
|
def assert_equal_se(is_x, want_x):
|
||||||
# check 2 xattr lists for equality, but ignore security.selinux attr
|
# check 2 xattr lists for equality, but ignore security.selinux attr
|
||||||
is_x = set(is_x) - {b"security.selinux"}
|
is_x = set(is_x) - {b"security.selinux"}
|
||||||
want_x = set(want_x)
|
want_x = set(want_x)
|
||||||
self.assert_equal(is_x, want_x)
|
assert is_x == want_x
|
||||||
|
|
||||||
def test(self):
|
|
||||||
tmp_fn = os.fsencode(self.tmpfile.name)
|
|
||||||
tmp_lfn = os.fsencode(self.symlink)
|
|
||||||
tmp_fd = self.tmpfile.fileno()
|
|
||||||
self.assert_equal_se(listxattr(tmp_fn), [])
|
|
||||||
self.assert_equal_se(listxattr(tmp_fd), [])
|
|
||||||
self.assert_equal_se(listxattr(tmp_lfn), [])
|
|
||||||
setxattr(tmp_fn, b"user.foo", b"bar")
|
|
||||||
setxattr(tmp_fd, b"user.bar", b"foo")
|
|
||||||
setxattr(tmp_fn, b"user.empty", b"")
|
|
||||||
if not is_linux:
|
|
||||||
# linux does not allow setting user.* xattrs on symlinks
|
|
||||||
setxattr(tmp_lfn, b"user.linkxattr", b"baz")
|
|
||||||
self.assert_equal_se(listxattr(tmp_fn), [b"user.foo", b"user.bar", b"user.empty"])
|
|
||||||
self.assert_equal_se(listxattr(tmp_fd), [b"user.foo", b"user.bar", b"user.empty"])
|
|
||||||
self.assert_equal_se(listxattr(tmp_lfn, follow_symlinks=True), [b"user.foo", b"user.bar", b"user.empty"])
|
|
||||||
if not is_linux:
|
|
||||||
self.assert_equal_se(listxattr(tmp_lfn), [b"user.linkxattr"])
|
|
||||||
self.assert_equal(getxattr(tmp_fn, b"user.foo"), b"bar")
|
|
||||||
self.assert_equal(getxattr(tmp_fd, b"user.foo"), b"bar")
|
|
||||||
self.assert_equal(getxattr(tmp_lfn, b"user.foo", follow_symlinks=True), b"bar")
|
|
||||||
if not is_linux:
|
|
||||||
self.assert_equal(getxattr(tmp_lfn, b"user.linkxattr"), b"baz")
|
|
||||||
self.assert_equal(getxattr(tmp_fn, b"user.empty"), b"")
|
|
||||||
|
|
||||||
def test_listxattr_buffer_growth(self):
|
def test(tempfile_symlink):
|
||||||
tmp_fn = os.fsencode(self.tmpfile.name)
|
temp_file, symlink = tempfile_symlink
|
||||||
# make it work even with ext4, which imposes rather low limits
|
tmp_fn = os.fsencode(temp_file.name)
|
||||||
buffer.resize(size=64, init=True)
|
tmp_lfn = os.fsencode(symlink)
|
||||||
# xattr raw key list will be > 64
|
tmp_fd = temp_file.fileno()
|
||||||
keys = [b"user.attr%d" % i for i in range(20)]
|
assert_equal_se(listxattr(tmp_fn), [])
|
||||||
for key in keys:
|
assert_equal_se(listxattr(tmp_fd), [])
|
||||||
setxattr(tmp_fn, key, b"x")
|
assert_equal_se(listxattr(tmp_lfn), [])
|
||||||
got_keys = listxattr(tmp_fn)
|
setxattr(tmp_fn, b"user.foo", b"bar")
|
||||||
self.assert_equal_se(got_keys, keys)
|
setxattr(tmp_fd, b"user.bar", b"foo")
|
||||||
assert len(buffer) > 64
|
setxattr(tmp_fn, b"user.empty", b"")
|
||||||
|
if not is_linux:
|
||||||
|
# linux does not allow setting user.* xattrs on symlinks
|
||||||
|
setxattr(tmp_lfn, b"user.linkxattr", b"baz")
|
||||||
|
assert_equal_se(listxattr(tmp_fn), [b"user.foo", b"user.bar", b"user.empty"])
|
||||||
|
assert_equal_se(listxattr(tmp_fd), [b"user.foo", b"user.bar", b"user.empty"])
|
||||||
|
assert_equal_se(listxattr(tmp_lfn, follow_symlinks=True), [b"user.foo", b"user.bar", b"user.empty"])
|
||||||
|
if not is_linux:
|
||||||
|
assert_equal_se(listxattr(tmp_lfn), [b"user.linkxattr"])
|
||||||
|
assert getxattr(tmp_fn, b"user.foo") == b"bar"
|
||||||
|
assert getxattr(tmp_fd, b"user.foo") == b"bar"
|
||||||
|
assert getxattr(tmp_lfn, b"user.foo", follow_symlinks=True) == b"bar"
|
||||||
|
if not is_linux:
|
||||||
|
assert getxattr(tmp_lfn, b"user.linkxattr") == b"baz"
|
||||||
|
assert getxattr(tmp_fn, b"user.empty") == b""
|
||||||
|
|
||||||
def test_getxattr_buffer_growth(self):
|
|
||||||
tmp_fn = os.fsencode(self.tmpfile.name)
|
def test_listxattr_buffer_growth(tempfile_symlink):
|
||||||
# make it work even with ext4, which imposes rather low limits
|
temp_file, symlink = tempfile_symlink
|
||||||
buffer.resize(size=64, init=True)
|
tmp_fn = os.fsencode(temp_file.name)
|
||||||
value = b"x" * 126
|
# make it work even with ext4, which imposes rather low limits
|
||||||
setxattr(tmp_fn, b"user.big", value)
|
buffer.resize(size=64, init=True)
|
||||||
got_value = getxattr(tmp_fn, b"user.big")
|
# xattr raw key list will be > 64
|
||||||
self.assert_equal(value, got_value)
|
keys = [b"user.attr%d" % i for i in range(20)]
|
||||||
self.assert_equal(len(buffer), 128)
|
for key in keys:
|
||||||
|
setxattr(tmp_fn, key, b"x")
|
||||||
|
got_keys = listxattr(tmp_fn)
|
||||||
|
assert_equal_se(got_keys, keys)
|
||||||
|
assert len(buffer) > 64
|
||||||
|
|
||||||
|
|
||||||
|
def test_getxattr_buffer_growth(tempfile_symlink):
|
||||||
|
temp_file, symlink = tempfile_symlink
|
||||||
|
tmp_fn = os.fsencode(temp_file.name)
|
||||||
|
# make it work even with ext4, which imposes rather low limits
|
||||||
|
buffer.resize(size=64, init=True)
|
||||||
|
value = b"x" * 126
|
||||||
|
setxattr(tmp_fn, b"user.big", value)
|
||||||
|
got_value = getxattr(tmp_fn, b"user.big")
|
||||||
|
assert value == got_value
|
||||||
|
assert len(buffer) == 128
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"lstring, splitted", ((b"", []), (b"\x00", [b""]), (b"\x01a", [b"a"]), (b"\x01a\x02cd", [b"a", b"cd"]))
|
"lstring, splitted", [(b"", []), (b"\x00", [b""]), (b"\x01a", [b"a"]), (b"\x01a\x02cd", [b"a", b"cd"])]
|
||||||
)
|
)
|
||||||
def test_split_lstring(lstring, splitted):
|
def test_split_lstring(lstring, splitted):
|
||||||
assert split_lstring(lstring) == splitted
|
assert split_lstring(lstring) == splitted
|
||||||
|
|
Loading…
Reference in a new issue