From 8141275b49f2d13b606093e12e03619c80f78df5 Mon Sep 17 00:00:00 2001 From: Aleksey Nogin Date: Sat, 7 Feb 2026 22:25:35 -0800 Subject: [PATCH] Use tmp_path for ELF test output artifacts instead of source tree (#705) Co-Authored-By: Claude Opus 4.6 --- ofrak_core/CHANGELOG.md | 1 + .../tests/components/test_elf_modifiers.py | 23 ++++++++++--------- .../components/test_patch_from_source.py | 5 ++-- ofrak_core/version.py | 2 +- 4 files changed, 17 insertions(+), 14 deletions(-) diff --git a/ofrak_core/CHANGELOG.md b/ofrak_core/CHANGELOG.md index 02e5141d1..2748c8c6e 100644 --- a/ofrak_core/CHANGELOG.md +++ b/ofrak_core/CHANGELOG.md @@ -16,6 +16,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - Remove test dependencies that are already in the global `requirements-dev.txt` ([#695](https://github.com/redballoonsecurity/ofrak/pull/695)) ### Fixed +- Fix ELF modifier tests writing generated artifacts into source tree instead of tmp_path ([#705](https://github.com/redballoonsecurity/ofrak/pull/705)) - Fix `Resource.get_attributes` docstring to match implementation ([#692](https://github.com/redballoonsecurity/ofrak/pull/692)) - Fix GUI serialization of enum values and script creator generating invalid Python syntax for enum values - `build_image.py` uses `OFRAK_DIR` from `extra_build_args` to identify `pytest_ofrak` location for develop builds ([#657](https://github.com/redballoonsecurity/ofrak/pull/657/)) diff --git a/ofrak_core/tests/components/test_elf_modifiers.py b/ofrak_core/tests/components/test_elf_modifiers.py index e5d57e99f..5de92938f 100644 --- a/ofrak_core/tests/components/test_elf_modifiers.py +++ b/ofrak_core/tests/components/test_elf_modifiers.py @@ -5,6 +5,7 @@ - REQ3.4 """ import os +import shutil import subprocess from typing import Optional @@ -49,9 +50,7 @@ from ofrak import OFRAKContext, Resource -async def test_elf_add_symbols( - ofrak_context: OFRAKContext, elf_executable_file, elf_test_directory -): +async def test_elf_add_symbols(ofrak_context: OFRAKContext, elf_executable_file, tmp_path): """ Tests insertion of new strings into an ELF executable. @@ -76,20 +75,20 @@ async def test_elf_add_symbols( await original_elf.run(ElfAddStringModifier, ElfAddStringModifierConfig(strings_to_add)) - output_path = os.path.join(elf_test_directory, "program_with_newstrings") + output_path = str(tmp_path / "program_with_newstrings") await original_elf.flush_data_to_disk(output_path) strings_result = subprocess.run(["strings", output_path], capture_output=True) new_strings = set(strings_result.stdout.decode().split("\n")) assert new_strings.difference(original_strings) == set(strings_to_add) - result = subprocess.run([os.path.join(elf_test_directory, "program")]) + result = subprocess.run([elf_executable_file]) assert result.returncode == 12 @pytest.mark.skipif_windows async def test_elf_force_relocation( - ofrak_context: OFRAKContext, elf_object_file, elf_test_directory + ofrak_context: OFRAKContext, elf_object_file, elf_test_directory, tmp_path ): """ Tests symbol relocation modification to redirect function calls. @@ -122,9 +121,11 @@ async def test_elf_force_relocation( ElfRelocateSymbolsModifierConfig({foo_vaddr: bar_vaddr, bar_vaddr: foo_vaddr}), ) - await elf.resource.flush_data_to_disk(os.path.join(elf_test_directory, "program_relocated.o")) - subprocess.run(["make", "-C", elf_test_directory, "program_relocated"]) - result = subprocess.run([os.path.join(elf_test_directory, "program_relocated")]) + relocated_obj = str(tmp_path / "program_relocated.o") + await elf.resource.flush_data_to_disk(relocated_obj) + shutil.copy(os.path.join(elf_test_directory, "Makefile"), tmp_path) + subprocess.run(["make", "-C", str(tmp_path), "program_relocated"]) + result = subprocess.run([str(tmp_path / "program_relocated")]) assert result.returncode == 24 @@ -156,7 +157,7 @@ async def test_elf_force_relocation( async def test_modifier( ofrak_context: OFRAKContext, elf_executable_file, - elf_test_directory, + tmp_path, modifier, modifier_config, test_view, @@ -183,7 +184,7 @@ async def test_modifier( for view in views: for entry in await view.get_entries(): await entry.resource.run(modifier, modifier_config) - mod_path = elf_executable_file + "_mod" + mod_path = str(tmp_path / "program_mod") await elf.resource.flush_data_to_disk(mod_path) mod_elf = await ofrak_context.create_root_resource_from_file(mod_path) await mod_elf.unpack() diff --git a/ofrak_core/tests/components/test_patch_from_source.py b/ofrak_core/tests/components/test_patch_from_source.py index d3e77a465..06265b259 100644 --- a/ofrak_core/tests/components/test_patch_from_source.py +++ b/ofrak_core/tests/components/test_patch_from_source.py @@ -50,6 +50,7 @@ async def test_patch_from_source_modifier( ofrak_context: OFRAKContext, large_elf_file, patch_file, + tmp_path, ) -> None: """ Tests the patch from source modifier functionality (REQ6.2). @@ -152,9 +153,9 @@ async def apply_patch(resource: Resource, source_dir: str, new_segment: ElfProgr resource = await ofrak_context.create_root_resource_from_file(large_elf_file) new_segment = await add_and_return_segment(resource, 0x440000, 0x2000) - output_file_name = os.path.join(os.path.dirname(patch_file), "test_patch") + output_file_name = str(tmp_path / "test_patch") - source_dir = os.path.join(os.path.dirname(patch_file)) + source_dir = os.path.dirname(patch_file) await apply_patch(resource, source_dir, new_segment) await call_new_segment_instead(resource, new_segment) 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"