Skip to content

Commit

Permalink
feat: allow tests to be run in other places than /tmp (#739)
Browse files Browse the repository at this point in the history
  • Loading branch information
theguy147 authored Oct 8, 2021
1 parent 467273f commit 078ce33
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 95 deletions.
42 changes: 27 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,35 @@ PYLINT_COMMON_PARAMETERS := --jobs=$(PYLINT_JOBS) --suggestion-mode=$(PYLINT_SUG
PYLINT_GEF_PARAMETERS := --disable=$(PYLINT_DISABLE) --enable=$(PYLINT_ENABLE) $(PYLINT_COMMON_PARAMETERS)
PYLINT_TEST_PARAMETERS := --disable=$(PYLINT_DISABLE) --enable=$(PYLINT_TEST_ENABLE) $(PYLINT_COMMON_PARAMETERS)
TARGET := $(shell lscpu | head -1 | sed -e 's/Architecture:\s*//g')
GEF_PATH ?= $(shell readlink -f gef.py)
TMPDIR ?= /tmp
PYTEST_PARAMETERS := --verbose -n $(NB_CORES)
ifdef DEBUG
PYTEST_PARAMETERS += --pdb
endif

test: testbins
@cp gef.py /tmp/gef.py
python3 -m pytest --verbose --numprocesses=$(NB_CORES) tests/runtests.py
@rm -f /tmp/gef.py
@rm -f /tmp/gef-*
@$(MAKE) -j $(NB_CORES) -C tests/binaries clean
.PHONY: test test_% Test% testbins clean lint

Test%: testbins
@cp gef.py /tmp/gef.py
python3 -m pytest --verbose --numprocesses=$(NB_CORES) tests/runtests.py $@
@rm -f /tmp/gef.py
@rm -f /tmp/gef-*
test: $(TMPDIR) testbins
TMPDIR=$(TMPDIR) python3 -m pytest $(PYTEST_PARAMETERS) tests/runtests.py

testbins: tests/binaries/*.c
@$(MAKE) -j $(NB_CORES) -C tests/binaries TARGET=$(TARGET) all
Test%: $(TMPDIR) testbins
TMPDIR=$(TMPDIR) python3 -m pytest $(PYTEST_PARAMETERS) tests/runtests.py::$@

test_%: $(TMPDIR) testbins
TMPDIR=$(TMPDIR) python3 -m pytest $(PYTEST_PARAMETERS) tests/runtests.py -k $@

testbins: $(TMPDIR) $(wildcard tests/binaries/*.c)
@TMPDIR=$(TMPDIR) $(MAKE) -j $(NB_CORES) -C tests/binaries TARGET=$(TARGET) all

clean:
TMPDIR=$(TMPDIR) $(MAKE) -j $(NB_CORES) -C tests/binaries clean
@rm -rf $(TMPDIR)

lint:
python3 -m pylint $(PYLINT_GEF_PARAMETERS) gef.py
python3 -m pylint $(PYLINT_TEST_PARAMETERS) tests/*.py
python3 -m pylint $(PYLINT_GEF_PARAMETERS) $(GEF_PATH)
python3 -m pylint $(PYLINT_TEST_PARAMETERS) $(wildcard tests/*.py)

$(TMPDIR):
mkdir -p $@

3 changes: 2 additions & 1 deletion gef.py
Original file line number Diff line number Diff line change
Expand Up @@ -7681,7 +7681,8 @@ def get_shellcode(self, sid):
return

ok("Downloaded, written to disk...")
fd, fname = tempfile.mkstemp(suffix=".txt", prefix="sc-", text=True, dir="/tmp")
tempdir = get_gef_setting("gef.tempdir")
fd, fname = tempfile.mkstemp(suffix=".txt", prefix="sc-", text=True, dir=tempdir)
shellcode = res.splitlines()[7:-11]
shellcode = b"\n".join(shellcode).replace(b""", b'"')
os.write(fd, shellcode)
Expand Down
6 changes: 3 additions & 3 deletions tests/binaries/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ SOURCES = $(wildcard *.c)
LINKED = $(SOURCES:.c=.out)
LDFLAGS =
EXTRA_FLAGS =
BINDIR = /tmp
TMPDIR ?= /tmp

ifeq ($(TARGET), i686)
CFLAGS += -m32
Expand All @@ -25,11 +25,11 @@ all: $(LINKED)

%.out : %.c
@echo "[+] Building '$@'"
@$(CC) $(CFLAGS) $(EXTRA_FLAGS) -o $(BINDIR)/$@ $? $(LDFLAGS)
@$(CC) $(CFLAGS) $(EXTRA_FLAGS) -o $(TMPDIR)/$@ $? $(LDFLAGS)

clean :
@echo "[+] Cleaning stuff"
@cd $(BINDIR) && rm -f $(LINKED)
@cd $(TMPDIR) && rm -f $(LINKED)

format-string-helper.out: EXTRA_FLAGS := -Wno-format-security

Expand Down
33 changes: 22 additions & 11 deletions tests/helpers.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
from typing import Iterable, Union, NewType, List
import os
import platform
import re
import subprocess
import os
import sys
import platform
import tempfile
from pathlib import Path
from typing import Iterable, Union, NewType, List

PATH_TO_DEFAULT_BINARY = "/tmp/default.out"
TMPDIR = Path(tempfile.gettempdir())
DEFAULT_TARGET = TMPDIR / "default.out"
GEF_PATH = Path(os.getenv("GEF_PATH", "gef.py"))
STRIP_ANSI_DEFAULT = True
DEFAULT_CONTEXT = "-code -stack"
ARCH = (os.getenv("GEF_CI_ARCH") or platform.machine()).lower()
Expand All @@ -30,12 +34,12 @@ def _add_command(commands: CommandType) -> List[str]:


def gdb_run_cmd(cmd: CommandType, before: CommandType = (), after: CommandType = (),
target: str = PATH_TO_DEFAULT_BINARY, strip_ansi: bool = STRIP_ANSI_DEFAULT) -> str:
target: Path = DEFAULT_TARGET, strip_ansi: bool = STRIP_ANSI_DEFAULT) -> str:
"""Execute a command inside GDB. `before` and `after` are lists of commands to be executed
before (resp. after) the command to test."""
command = [
"gdb", "-q", "-nx",
"-ex", "source /tmp/gef.py",
"-ex", f"source {GEF_PATH}",
"-ex", "gef config gef.debug True"
]

Expand Down Expand Up @@ -69,7 +73,7 @@ def gdb_run_cmd(cmd: CommandType, before: CommandType = (), after: CommandType =


def gdb_run_silent_cmd(cmd: CommandType, before: CommandType = (), after: CommandType = (),
target: str = PATH_TO_DEFAULT_BINARY,
target: Path = DEFAULT_TARGET,
strip_ansi: bool = STRIP_ANSI_DEFAULT) -> str:
"""Disable the output and run entirely the `target` binary."""
before = [*before, "gef config context.clear_screen False",
Expand All @@ -79,14 +83,14 @@ def gdb_run_silent_cmd(cmd: CommandType, before: CommandType = (), after: Comman


def gdb_run_cmd_last_line(cmd: CommandType, before: CommandType = (), after: CommandType = (),
target: str = PATH_TO_DEFAULT_BINARY,
target: Path = DEFAULT_TARGET,
strip_ansi: bool = STRIP_ANSI_DEFAULT) -> str:
"""Execute a command in GDB, and return only the last line of its output."""
return gdb_run_cmd(cmd, before, after, target, strip_ansi).splitlines()[-1]


def gdb_start_silent_cmd(cmd: CommandType, before: CommandType = (), after: CommandType = (),
target: str = PATH_TO_DEFAULT_BINARY,
target: Path = DEFAULT_TARGET,
strip_ansi: bool = STRIP_ANSI_DEFAULT,
context: str = DEFAULT_CONTEXT) -> str:
"""Execute a command in GDB by starting an execution context. This command
Expand All @@ -100,14 +104,14 @@ def gdb_start_silent_cmd(cmd: CommandType, before: CommandType = (), after: Comm

def gdb_start_silent_cmd_last_line(cmd: CommandType, before: CommandType = (),
after: CommandType = (),
target=PATH_TO_DEFAULT_BINARY,
target: Path = DEFAULT_TARGET,
strip_ansi=STRIP_ANSI_DEFAULT) -> str:
"""Execute `gdb_start_silent_cmd()` and return only the last line of its output."""
return gdb_start_silent_cmd(cmd, before, after, target, strip_ansi).splitlines()[-1]


def gdb_test_python_method(meth: str, before: str = "", after: str = "",
target: str = PATH_TO_DEFAULT_BINARY,
target: Path = DEFAULT_TARGET,
strip_ansi: bool = STRIP_ANSI_DEFAULT) -> str:
brk = before + ";" if before else ""
cmd = f"pi {brk}print({meth});{after}"
Expand Down Expand Up @@ -136,3 +140,10 @@ def inner_f(*args, **kwargs):
sys.stderr.flush()
return inner_f
return wrapper


def _target(name: str, extension: str = ".out") -> Path:
target = TMPDIR / f"{name}{extension}"
if not target.exists():
raise FileNotFoundError(f"Could not find file '{target}'")
return target
Loading

0 comments on commit 078ce33

Please sign in to comment.