Skip to content

Commit

Permalink
Fix default injection path (#54)
Browse files Browse the repository at this point in the history
* Fix default injection path

* Fix test_inject.py

* Whoops

* Clean up executable injection

* Fix more issues

* Fix double path in qemu injection
  • Loading branch information
twizmwazin authored Jan 20, 2024
1 parent e0282f9 commit f4c8bcf
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 20 deletions.
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()

0 comments on commit f4c8bcf

Please sign in to comment.