Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix default injection path #54

Merged
merged 6 commits into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion python/binharness/common/busybox.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class BusyboxInjection(ExecutableInjection):

def __init__(self: BusyboxInjection) -> None:
"""Create a BusyboxInjection."""
super().__init__(Path("busybox"), self.BUSYBOX_PATH, None)
super().__init__(self.BUSYBOX_PATH)

def install(self: BusyboxInjection, environment: Environment) -> None:
"""Install the injection into an environment."""
Expand Down
4 changes: 1 addition & 3 deletions python/binharness/common/qemu.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ class QemuInjection(ExecutableInjection):
def __init__(self: QemuInjection, arch: str | None = None) -> None:
"""Create a QemuInjection."""
self.arch = arch if arch is not None else _get_qemu_host_arch()
super().__init__(
Path(f"qemu-{self.arch}-static"), Path(f"/usr/bin/qemu-{self.arch}-static")
)
super().__init__(Path(f"/usr/bin/qemu-{self.arch}-static"))

def run_with_log(
self: QemuInjection,
Expand Down
21 changes: 14 additions & 7 deletions python/binharness/types/injection.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ def install(self: Injection, environment: Environment) -> None:
f"{self.host_path.name}-{generate_random_suffix()}.bh-inj"
)

environment.inject_files(
[(self.host_path, self.env_path / self.host_path.name)]
)
environment.inject_files([(self.host_path, self.env_path)])
self._environment = environment

def is_installed(self: Injection) -> bool:
Expand All @@ -76,17 +74,26 @@ class ExecutableInjection(Injection):
environment.
"""

executable: Path
_executable: Path | None

def __init__(
self: ExecutableInjection,
executable: Path,
host_path: Path,
env_path: Path | None = None,
executable: Path | None = None,
) -> None:
"""Create an ExecutableInjection."""
super().__init__(host_path, env_path)
self.executable = executable
self._executable = executable

@property
def executable(self: ExecutableInjection) -> Path:
"""Return the executable path."""
if self.host_path is not None:
if self._executable is None:
return self.host_path
return self.host_path / self._executable
raise InjectionNotInstalledError

def run(
self: ExecutableInjection,
Expand All @@ -98,7 +105,7 @@ def run(
if self._environment is None or self.env_path is None:
raise InjectionNotInstalledError
return self._environment.run_command(
[self.env_path / self.executable, *args],
[self.executable, *args],
env=env,
cwd=cwd,
)
17 changes: 8 additions & 9 deletions python/tests/test_inject.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,19 @@
@pytest.mark.linux()
def test_inject_true() -> None:
env = LocalEnvironment()
true_injection = Injection(Path("/usr/bin/true"), None)
true_injection = Injection(Path("/usr/bin/true"))
true_injection.install(env)
assert true_injection.env_path is not None
assert (true_injection.env_path / "true").is_file()
assert true_injection.env_path.is_file()


@pytest.mark.linux()
def test_inject_true_executable() -> None:
env = LocalEnvironment()
true_injection = ExecutableInjection(Path("true"), Path("/usr/bin/true"))
true_injection = ExecutableInjection(Path("/usr/bin/true"))
true_injection.install(env)
assert true_injection.env_path is not None
assert true_injection.env_path.is_dir()
assert (true_injection.env_path / "true").is_file()
assert true_injection.env_path.is_file()
assert true_injection.is_installed()
assert true_injection.env_path.stat().st_mode & 0o111 != 0
assert true_injection.run().wait() == 0
Expand All @@ -38,7 +37,7 @@ def test_inject_true_executable() -> None:
@pytest.mark.linux()
def test_inject_true_executable_twice() -> None:
env = LocalEnvironment()
true_injection = ExecutableInjection(Path("true"), Path("/usr/bin/true"))
true_injection = ExecutableInjection(Path("/usr/bin/true"))
true_injection.install(env)
with pytest.raises(InjectionAlreadyInstalledError):
true_injection.install(env)
Expand All @@ -47,13 +46,13 @@ def test_inject_true_executable_twice() -> None:
@pytest.mark.linux()
def test_inject_two_true_executables() -> None:
env = LocalEnvironment()
true_injection_1 = ExecutableInjection(Path("true"), Path("/usr/bin/true"))
true_injection_1 = ExecutableInjection(Path("/usr/bin/true"))
true_injection_1.install(env)
true_injection_2 = ExecutableInjection(Path("true"), Path("/usr/bin/true"))
true_injection_2 = ExecutableInjection(Path("/usr/bin/true"))
true_injection_2.install(env)


def test_executable_injection_no_install() -> None:
true_injection = ExecutableInjection(Path("true"), Path("/usr/bin/true"))
true_injection = ExecutableInjection(Path("/usr/bin/true"))
with pytest.raises(InjectionNotInstalledError):
true_injection.run()