|
10 | 10 | from . import assert_dirs_equal, _extract_hardlinks_setup, cmd, requires_hardlinks, RK_ENCRYPTION |
11 | 11 | from . import create_test_files, create_regular_file |
12 | 12 | from . import generate_archiver_tests |
| 13 | +from ...platform import acl_get, acl_set |
| 14 | +from ..platform.platform_test import skipif_not_linux, skipif_acls_not_working |
13 | 15 |
|
14 | 16 | pytest_generate_tests = lambda metafunc: generate_archiver_tests(metafunc, kinds="local,remote,binary") # NOQA |
15 | 17 |
|
@@ -269,3 +271,89 @@ def test_roundtrip_pax_xattrs(archivers, request): |
269 | 271 | extracted_path = os.path.abspath("input/file") |
270 | 272 | xa_value_extracted = xattr.getxattr(extracted_path.encode(), xa_key) |
271 | 273 | assert xa_value_extracted == xa_value |
| 274 | + |
| 275 | + |
| 276 | +@skipif_not_linux |
| 277 | +@skipif_acls_not_working |
| 278 | +def test_acl_roundtrip(archivers, request): |
| 279 | + """Test the complete workflow for POSIX ACLs with export-tar and import-tar. |
| 280 | +
|
| 281 | + This test follows the workflow: |
| 282 | + 1. set filesystem ACLs |
| 283 | + 2. create a Borg archive |
| 284 | + 3. export-tar this archive |
| 285 | + 4. import-tar the resulting tar file |
| 286 | + 5. extract the imported archive |
| 287 | + 6. check the expected ACLs in the filesystem |
| 288 | + """ |
| 289 | + archiver = request.getfixturevalue(archivers) |
| 290 | + |
| 291 | + # Define helper functions for working with ACLs |
| 292 | + def get_acl(path): |
| 293 | + item = {} |
| 294 | + acl_get(path, item, os.stat(path)) |
| 295 | + return item |
| 296 | + |
| 297 | + def set_acl(path, access=None, default=None): |
| 298 | + item = {"acl_access": access, "acl_default": default} |
| 299 | + acl_set(path, item) |
| 300 | + |
| 301 | + # Define example ACLs |
| 302 | + ACCESS_ACL = b"user::rw-\nuser:root:rw-:0\ngroup::r--\ngroup:root:r--:0\nmask::rw-\nother::r--" |
| 303 | + DEFAULT_ACL = b"user::rw-\nuser:root:r--:0\ngroup::r--\ngroup:root:r--:0\nmask::rw-\nother::r--" |
| 304 | + |
| 305 | + # 1. Set filesystem ACLs |
| 306 | + # Create test files with ACLs |
| 307 | + create_regular_file(archiver.input_path, "file") |
| 308 | + os.mkdir(os.path.join(archiver.input_path, "dir")) |
| 309 | + |
| 310 | + file_path = os.path.join(archiver.input_path, "file") |
| 311 | + dir_path = os.path.join(archiver.input_path, "dir") |
| 312 | + |
| 313 | + # Set ACLs on the test files |
| 314 | + try: |
| 315 | + set_acl(file_path, access=ACCESS_ACL) |
| 316 | + set_acl(dir_path, access=ACCESS_ACL, default=DEFAULT_ACL) |
| 317 | + except OSError as e: |
| 318 | + pytest.skip(f"Failed to set ACLs: {e}") |
| 319 | + |
| 320 | + file_acl = get_acl(file_path) |
| 321 | + dir_acl = get_acl(dir_path) |
| 322 | + |
| 323 | + if not file_acl.get("acl_access") or not dir_acl.get("acl_access") or not dir_acl.get("acl_default"): |
| 324 | + pytest.skip("ACLs not supported or not working correctly") |
| 325 | + |
| 326 | + # 2. Create a Borg archive |
| 327 | + cmd(archiver, "repo-create", "--encryption=none") |
| 328 | + cmd(archiver, "create", "original", "input") |
| 329 | + |
| 330 | + # 3. export-tar this archive to a tar file |
| 331 | + cmd(archiver, "export-tar", "original", "acls.tar", "--tar-format=PAX") |
| 332 | + |
| 333 | + # 4. import-tar the resulting tar file |
| 334 | + cmd(archiver, "import-tar", "imported", "acls.tar") |
| 335 | + |
| 336 | + # 5. Extract the imported archive |
| 337 | + with changedir(archiver.output_path): |
| 338 | + cmd(archiver, "extract", "imported") |
| 339 | + |
| 340 | + # 6. Check the expected ACLs in the filesystem |
| 341 | + extracted_file_path = os.path.abspath("input/file") |
| 342 | + extracted_dir_path = os.path.abspath("input/dir") |
| 343 | + |
| 344 | + extracted_file_acl = get_acl(extracted_file_path) |
| 345 | + extracted_dir_acl = get_acl(extracted_dir_path) |
| 346 | + |
| 347 | + # Check that access ACLs were preserved |
| 348 | + assert "acl_access" in extracted_file_acl |
| 349 | + assert extracted_file_acl["acl_access"] == file_acl["acl_access"] |
| 350 | + assert b"user:root:rw-" in file_acl["acl_access"] |
| 351 | + |
| 352 | + assert "acl_access" in extracted_dir_acl |
| 353 | + assert extracted_dir_acl["acl_access"] == dir_acl["acl_access"] |
| 354 | + assert b"user:root:rw-" in dir_acl["acl_access"] |
| 355 | + |
| 356 | + # Check that default ACLs were preserved for directories |
| 357 | + assert "acl_default" in extracted_dir_acl |
| 358 | + assert extracted_dir_acl["acl_default"] == dir_acl["acl_default"] |
| 359 | + assert b"user:root:r--" in dir_acl["acl_default"] |
0 commit comments