Run each test in a separate GDB session (#498)

* it would be cool to have tests that run within GDB so that we don't have to parse GDB output and deal with weird problems
* we can't run all tests in one GDB session as `file x; entry; <some pwndbg command>; file y; entry; <some wndbg command>;` may have different results - it seems either us or GDB fails to cleanup everything properly
pull/500/head
Disconnect3d 8 years ago committed by GitHub
parent 5b9671f284
commit 87aa167599
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

6
.gitignore vendored

@ -60,4 +60,8 @@ npm-debug.log
.gdb_history
# PyCharm project files
.idea/
.idea/
# PyTest files
.pytest_cache/
tests/.pytest_cache/

@ -0,0 +1,36 @@
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
from __future__ import unicode_literals
import os
import sys
import pytest
TESTS_PATH = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
'tests'
)
class CollectTestFunctionNames:
"""See https://github.com/pytest-dev/pytest/issues/2039#issuecomment-257753269"""
def __init__(self):
self.collected = []
def pytest_collection_modifyitems(self, items):
for item in items:
self.collected.append(item.nodeid)
collector = CollectTestFunctionNames()
pytest.main(['--collect-only', TESTS_PATH], plugins=[collector])
print('Listing collected tests:')
for nodeid in collector.collected:
print('Test:', nodeid)
# easy way to exit GDB session
sys.exit(0)

@ -10,13 +10,14 @@ print(sys.argv)
sys._pwndbg_unittest_run = True
TESTS_PATH = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
'tests'
)
CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
test = os.environ['PWNDBG_LAUNCH_TEST']
test = os.path.join(CURRENT_DIR, test)
# If you want to debug tests locally, add '--pdb' here
args = [TESTS_PATH, '-vvv', '-s', '--showlocals', '--color=yes']
args = [test, '-vvv', '-s', '--showlocals', '--color=yes']
print('Launching pytest with args: %s' % args)

@ -1,3 +1,10 @@
#!/bin/bash
PWNDBG_DISABLE_COLORS=1 gdb --silent --nx --nh --command gdbinit.py --command pytests_launcher.py
# NOTE: We run tests under GDB sessions and because of some cleanup/tests dependencies problems
# we decided to run each test in a separate GDB session
TESTS_LIST=$(gdb --silent --nx --nh --command gdbinit.py --command pytests_collect.py | grep -o "tests/.*::.*")
for test_case in ${TESTS_LIST}; do
PWNDBG_LAUNCH_TEST="${test_case}" PWNDBG_DISABLE_COLORS=1 gdb --silent --nx --nh --command gdbinit.py --command pytests_launcher.py
done

@ -9,7 +9,6 @@ import codecs
import os
import re
import subprocess
import tempfile
import pytest
@ -37,7 +36,6 @@ def run_gdb_with_script(binary='', core='', pybefore=None, pyafter=None):
if core:
command += ['--core', core]
for cmd in pyafter:
command += ['--eval-command', cmd]
@ -55,6 +53,7 @@ def run_gdb_with_script(binary='', core='', pybefore=None, pyafter=None):
return output
HELLO = (
'pwndbg: loaded ### commands. Type pwndbg [filter] for a list.\n'
'pwndbg: created $rebase, $ida gdb functions (can be used with print/break)\n'

@ -3,9 +3,6 @@ from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import gdb
import pytest
import pwndbg.memory
import pwndbg.stack

Loading…
Cancel
Save