From 156fc8fba28227b455d8f6bf916b6d3a522ac228 Mon Sep 17 00:00:00 2001 From: Aleksey Nogin Date: Sat, 7 Feb 2026 19:12:15 -0800 Subject: [PATCH] Skip tests requiring root privileges when running as non-root user - Skip TestComplexTarWithSpecialFiles (device nodes require root) - Handle testtar.tar unpack failure in CLI test fixture Co-Authored-By: Claude Opus 4.6 --- ofrak_core/CHANGELOG.md | 1 + ofrak_core/tests/components/test_tar_component.py | 3 +++ ofrak_core/tests/unit/test_ofrak_cli.py | 15 +++++++++++++-- ofrak_core/version.py | 2 +- 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/ofrak_core/CHANGELOG.md b/ofrak_core/CHANGELOG.md index 02e5141d1..6f4cba541 100644 --- a/ofrak_core/CHANGELOG.md +++ b/ofrak_core/CHANGELOG.md @@ -14,6 +14,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) ### Changed - Remove test dependencies that are already in the global `requirements-dev.txt` ([#695](https://github.com/redballoonsecurity/ofrak/pull/695)) +- Skip tests requiring root privileges when running as non-root user ([#704](https://github.com/redballoonsecurity/ofrak/pull/704)) ### Fixed - Fix `Resource.get_attributes` docstring to match implementation ([#692](https://github.com/redballoonsecurity/ofrak/pull/692)) diff --git a/ofrak_core/tests/components/test_tar_component.py b/ofrak_core/tests/components/test_tar_component.py index 3dc84323d..c0731f4ec 100644 --- a/ofrak_core/tests/components/test_tar_component.py +++ b/ofrak_core/tests/components/test_tar_component.py @@ -234,6 +234,9 @@ async def verify(self, repacked_root_resource: Resource) -> None: assert self.EXPECTED_DATA == f.read() +@pytest.mark.skipif( + os.geteuid() != 0, reason="Extracting device nodes from testtar.tar requires root" +) class TestComplexTarWithSpecialFiles(FilesystemPackUnpackVerifyPattern): """ Test unpacking and repacking a tar archive containing special file types. diff --git a/ofrak_core/tests/unit/test_ofrak_cli.py b/ofrak_core/tests/unit/test_ofrak_cli.py index be602bfe6..4235ef09c 100644 --- a/ofrak_core/tests/unit/test_ofrak_cli.py +++ b/ofrak_core/tests/unit/test_ofrak_cli.py @@ -10,6 +10,7 @@ from dataclasses import fields from ofrak.cli.command.gui import GUICommand +from ofrak.component.abstract import ComponentSubprocessError from ofrak.ofrak_context import OFRAKContext from .. import components @@ -251,7 +252,14 @@ async def all_expected_hashes(ofrak_context: OFRAKContext): expected_hashes = set() file_path = os.path.join(os.path.dirname(components.__file__), "assets", filename) res = await ofrak_context.create_root_resource_from_file(file_path) - await res.unpack() + try: + await res.unpack() + except ComponentSubprocessError: + # testtar.tar contains device nodes requiring root to extract + if filename == "testtar.tar" and os.geteuid() != 0: + all_expected_hashes[filename] = None + continue + raise for child in await res.get_descendants(): if child.get_data_id() is not None: data = await child.get_data() @@ -272,6 +280,10 @@ def test_unpack(ofrak_cli_parser, capsys, filename, tmpdir, ofrak_context, all_e - The unpack command generates correct hash values for extracted files - The unpack command creates an info dump file with proper content """ + expected_hashes = all_expected_hashes[filename] + if expected_hashes is None: + pytest.skip(f"Unpacking {filename} requires root") + file_path = os.path.join(os.path.dirname(components.__file__), "assets", filename) ofrak_cli_parser.parse_and_run(["unpack", "-o", str(tmpdir), file_path]) @@ -284,7 +296,6 @@ def test_unpack(ofrak_cli_parser, capsys, filename, tmpdir, ofrak_context, all_e path = os.path.join(dirpath, unpacked_file) with open(path, "rb") as file: unpacked_hashes.add(hashlib.sha256(file.read()).hexdigest()) - expected_hashes = all_expected_hashes[filename] assert unpacked_hashes == expected_hashes diff --git a/ofrak_core/version.py b/ofrak_core/version.py index ed1177993..ba047d0bd 100644 --- a/ofrak_core/version.py +++ b/ofrak_core/version.py @@ -1 +1 @@ -VERSION = "3.4.0rc5" +VERSION = "3.4.0rc6"