diff --git a/tests/api/gef_session.py b/tests/api/gef_session.py index b97d759d7..9bde1e277 100644 --- a/tests/api/gef_session.py +++ b/tests/api/gef_session.py @@ -3,9 +3,9 @@ """ -from logging import root -import subprocess import os +import random +import subprocess from tests.utils import ( TMPDIR, gdb_test_python_method, @@ -13,17 +13,15 @@ GefUnitTestGeneric, gdbserver_session, gdb_run_cmd, - qemuuser_session + qemuuser_session, + GDBSERVER_DEFAULT_HOST ) import re -GDBSERVER_PREFERED_HOST = "localhost" -GDBSERVER_PREFERED_PORT = 1234 class GefSessionApi(GefUnitTestGeneric): """`gef.session` test module.""" - def test_func_get_filepath(self): res = gdb_test_python_method("gef.session.file", target=_target("default")) self.assertNoException(res) @@ -49,7 +47,7 @@ def test_func_auxiliary_vector(self): self.assertTrue("'AT_EXECFN':" in res) self.assertFalse("'AT_WHATEVER':" in res) - def test_root_dir(self): + def test_root_dir_local(self): func = "(s.st_dev, s.st_ino)" res = gdb_test_python_method(func, target=_target("default"), before="s=os.stat(gef.session.root)") self.assertNoException(res) @@ -58,20 +56,25 @@ def test_root_dir(self): # Check that the `/` directory and the `session.root` directory are the same assert (stat_root.st_dev == st_dev) and (stat_root.st_ino == st_ino) - port = GDBSERVER_PREFERED_PORT + 1 - before = [f"gef-remote {GDBSERVER_PREFERED_HOST} {port}", - "pi s = os.stat(gef.session.root)"] - with gdbserver_session(port=port) as _: + def test_root_dir_remote(self): + func = "(s.st_dev, s.st_ino)" + stat_root = os.stat("/") + host = GDBSERVER_DEFAULT_HOST + port = random.randint(1025, 65535) + before = [f"gef-remote {host} {port}", "pi s=os.stat(gef.session.root)"] + with gdbserver_session(port=port): res = gdb_run_cmd(f"pi {func}", target=_target("default"), before=before) self.assertNoException(res) st_dev, st_ino = eval(res.split("\n")[-1]) assert (stat_root.st_dev == st_dev) and (stat_root.st_ino == st_ino) - port = GDBSERVER_PREFERED_PORT + 2 - with qemuuser_session(port=port) as _: + def test_root_dir_qemu(self): + host = GDBSERVER_DEFAULT_HOST + port = random.randint(1025, 65535) + with qemuuser_session(port=port): target = _target("default") before = [ - f"gef-remote --qemu-user --qemu-binary {target} {GDBSERVER_PREFERED_HOST} {port}"] + f"gef-remote --qemu-user --qemu-binary {target} {host} {port}"] res = gdb_run_cmd(f"pi gef.session.root", target=_target("default"), before=before) self.assertNoException(res) assert re.search(r"\/proc\/[0-9]+/root", res) diff --git a/tests/utils.py b/tests/utils.py index 6456d53a5..d32483b85 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -29,6 +29,7 @@ GEF_DEFAULT_TEMPDIR = "/tmp/gef" GEF_PATH = pathlib.Path(os.getenv("GEF_PATH", "gef.py")) STRIP_ANSI_DEFAULT = True +GDBSERVER_DEFAULT_HOST = "localhost" GDBSERVER_DEFAULT_PORT = 1234 CommandType = Union[str, Iterable[str]] @@ -231,6 +232,7 @@ def _target(name: str, extension: str = ".out") -> pathlib.Path: def start_gdbserver(exe: Union[str, pathlib.Path] = _target("default"), + host: str = GDBSERVER_DEFAULT_HOST, port: int = GDBSERVER_DEFAULT_PORT) -> subprocess.Popen: """Start a gdbserver on the target binary. @@ -241,7 +243,7 @@ def start_gdbserver(exe: Union[str, pathlib.Path] = _target("default"), Returns: subprocess.Popen: a Popen object for the gdbserver process. """ - return subprocess.Popen(["gdbserver", f":{port}", exe], + return subprocess.Popen(["gdbserver", f"{host}:{port}", exe], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) @@ -260,10 +262,11 @@ def stop_gdbserver(gdbserver: subprocess.Popen) -> None: @contextlib.contextmanager def gdbserver_session(*args, **kwargs): exe = kwargs.get("exe", "") or _target("default") - port = kwargs.get("port", 0) or GDBSERVER_DEFAULT_PORT - sess = start_gdbserver(exe, port) + host = kwargs.get("host", GDBSERVER_DEFAULT_HOST) + port = kwargs.get("port", GDBSERVER_DEFAULT_PORT) + sess = start_gdbserver(exe, host, port) try: - time.sleep(0.5) # forced delay to allow gdbserver to start listening + time.sleep(1) # forced delay to allow gdbserver to start listening yield sess finally: stop_gdbserver(sess)