Skip to content

Commit

Permalink
[ci] increased delay to appease GHActions gods
Browse files Browse the repository at this point in the history
  • Loading branch information
hugsy committed Nov 17, 2022
1 parent 05b17d0 commit 4e89034
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
31 changes: 17 additions & 14 deletions tests/api/gef_session.py
Expand Up @@ -3,27 +3,25 @@
"""


from logging import root
import subprocess
import os
import random
import subprocess
from tests.utils import (
TMPDIR,
gdb_test_python_method,
_target,
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)
Expand All @@ -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)
Expand All @@ -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)
11 changes: 7 additions & 4 deletions tests/utils.py
Expand Up @@ -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]]
Expand Down Expand Up @@ -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.
Expand All @@ -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)


Expand All @@ -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)
Expand Down

0 comments on commit 4e89034

Please sign in to comment.