Gallopsled / Gallopsled/pwntools-write-ups

run_all_tests.sh and harness.py

Open
#18 0 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Python
Stars
528
Forks
112
PR merge metrics
No merged PRs in 30d

Description

`run_all_tests.sh` should be replaced with a simple scandown for unit tests. For example, `pytest` or `nosetests` scans for all files which start with `test`, and executes all functions whose name start with `test`.

`harness.py` can then be renamed to `test.py` and replaced with a simple wrapper script that does something like what's shown below.
### Example `test.py`

```
#!/usr/bin/env python2
from pwn import *

def run_exploit(**kwargs):
# set up the flag and target file
write('flag', randoms(20, string.ascii_letters))
saveflag = tempfile.NamedTemporaryFile()

# Set up arguments
global args
args['SAVEFLAG'] = saveflag.name
args['FLAG'] = 'flag'
args.update(**kwargs)

exploit = __import__('exploit', level=0)
del sys.modules['exploit']
del exploit

# verify
assert read(saveflag.name) == read('flag')

def test_local():
'Run the exploit locally'
run_exploit()

def test_remote():
l = listen(0)
l.spawn_process('pwnme')
run_exploit(REMOTE='localhost', PORT=l.lport)

if __name__ == '__main__':
test_local()
test_remote()
```
## py.test and nosetest example

Given this input script

``` sh
pip install pytest nose
cat > test_foo.py < raise Exception()
E Exception

test_foo.py:8: Exception
------------------------------------------------------------------------------ Captured stdout call -------------------------------------------------------------------------------
Shucks!
__________________________________________________________________________________ test_failure ___________________________________________________________________________________

def test_failure():
> log.error("Oh no!")

test_foo.py:14:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = , m = 'Oh no!', a = (), kw = {'extra': {'pwnlib_stop': False, 'pwnlib_symbol': 'ERROR'}}

def error(self, m, *a, **kw):
"""error(message)

Logs an error message, and raises an ``Exception``.
"""
self.__log(logging.ERROR, m, a, kw, text.on_red('ERROR'))
> raise Exception(m)
E Exception: Oh no!

/home/riggle/pwntools/pwnlib/log.py:134: Exception
------------------------------------------------------------------------------ Captured stdout call -------------------------------------------------------------------------------
[ERROR] Oh no!
======================================================================= 2 failed, 2 passed in 0.23 seconds ========================================================================
```
### nosetests output

```
.E.E
======================================================================
ERROR: test_foo.test_normal_error
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/riggle/.pyenv/versions/2.7.8/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
self.test(*self.arg)
File "/media/SSD1T/riggle/yyy/test_foo.py", line 8, in test_normal_error
raise Exception()
Exception:
-------------------- >> begin captured stdout << ---------------------
Shucks!

--------------------- >> end captured stdout << ----------------------

======================================================================
ERROR: test_foo.test_failure
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/riggle/.pyenv/versions/2.7.8/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
self.test(*self.arg)
File "/media/SSD1T/riggle/yyy/test_foo.py", line 14, in test_failure
log.error("Oh no!")
File "/home/riggle/pwntools/pwnlib/log.py", line 134, in error
raise Exception(m)
Exception: Oh no!
-------------------- >> begin captured stdout << ---------------------
[ERROR] Oh no!

--------------------- >> end captured stdout << ----------------------
-------------------- >> begin captured logging << --------------------
pwnlib.exploit: ERROR: Oh no!
--------------------- >> end captured logging << ---------------------

----------------------------------------------------------------------
Ran 4 tests in 0.126s

FAILED (errors=2)
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with run_all_tests.sh and harness.py, then compare their current behavior with the pytest and nosetests examples in the issue. Replace the existing harness flow with test.py and ensure test_* functions can be discovered and run for both local and remote cases; done means the documented commands execute the tests and report failures.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, shell
Domain
testing-qa, tooling
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.