Handle xattr returning non-UTF-8 bytes.

This commit is contained in:
Shea Craig 2023-04-07 19:07:31 -04:00 committed by Gareth J. Greenaway
parent a1274fc2dd
commit 6313682379
2 changed files with 16 additions and 0 deletions

View file

@ -110,6 +110,9 @@ def read(path, attribute, **kwargs):
try:
ret = salt.utils.mac_utils.execute_return_result(cmd)
except UnicodeDecodeError as exc:
# Mimic the builtin xattr tool by replacing undecodeable bytes.
return exc.object.decode(errors="replace")
except CommandExecutionError as exc:
if "No such file" in exc.strerror:
raise CommandExecutionError("File not found: {}".format(path))

View file

@ -74,6 +74,19 @@ def test_read_missing():
pytest.raises(CommandExecutionError, xattr.read, "/path/to/file", "attribute")
def test_read_not_decodeable():
"""
Test reading an attribute which returns non-UTF-8 bytes
"""
with patch(
"salt.utils.mac_utils.execute_return_result",
MagicMock(
side_effect=UnicodeDecodeError("UTF-8", b"\xd1expected results", 0, 1, "")
),
):
assert xattr.read("/path/to/file", "com.attr") == "<EFBFBD>expected results"
def test_write():
"""
Test writing a specific attribute to a file