Skip to content

Commit

Permalink
Expand unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dshikashio committed Mar 13, 2022
1 parent 0c33994 commit 5ae110a
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 8 deletions.
11 changes: 9 additions & 2 deletions run_tests.bat
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
python -m tests.test_idebugclient
python -m tests.test_target
REM discovery doesn't work well due to:
REM need to split dbgeng instances into different processe
REM until its clear how to properly release all references to objects

REM python -m unittest discover

python -m tests.test_idebugclient
python -m tests.test_target TestBasic
python -m tests.test_target TestTargetCreate
python -m tests.test_target TestTargetAttach1
python -m tests.test_target TestTargetAttach2
36 changes: 36 additions & 0 deletions tests/target/target2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <Windows.h>
#include <stdio.h>

PVOID ADDRESS = (PVOID)0x222000;

int wmain(int argc, wchar_t **argv)
{
PVOID ptr = NULL;

ptr = VirtualAlloc(ADDRESS, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
if (ptr == NULL)
{
printf("VirtualAlloc Failed: %08x\n", GetLastError());
return -1;
}

printf("%S: Hello : %p\n", argv[0], ptr);

PDWORD stop = (PDWORD)ptr;

while (*stop == 0)
{
printf("Sleep\n");
Sleep(2000);
}

//
// set registers
// end with 0xcc
//
// execute buffer


return 0;
}

7 changes: 6 additions & 1 deletion tests/test_idebugclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
class TestIDebugClient(unittest.TestCase):
def test_init(self):
cli = DebugClient()

adv = cli.IDebugAdvanced()
ctr = cli.IDebugControl()
dat = cli.IDebugDataSpaces()
reg = cli.IDebugRegisters()
sym = cli.IDebugSymbols()
sys = cli.IDebugSystemObjects()

if __name__ == '__main__':
unittest.main()
75 changes: 70 additions & 5 deletions tests/test_target.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,81 @@
import unittest
import os
import pprint
import subprocess

from pybag import DbgEng, UserDbg

target = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'target', 'target.exe')
target1 = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'target', 'target.exe')
target2 = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'target', 'target2.exe')

class TestTarget(unittest.TestCase):
def test_init(self):
class TestTargetCreate(unittest.TestCase):
def setUp(self):
dbg = UserDbg()
dbg.create(target)
dbg.r()
dbg.create(target1)
self.dbg = dbg

def tearDown(self):
self.dbg.terminate()
self.dbg = None

def test_base_functions(self):
self.dbg.r()
self.dbg.address()
self.dbg.disasm()
self.dbg.handles()
self.dbg.threads()
self.dbg.teb()
self.dbg.peb()
self.dbg.backtrace()
self.dbg.lm()
self.dbg.imports('target')
self.dbg.exports('kernel32')
self.assertEqual(self.dbg.bitness(), '64')

class TestTargetAttach1(unittest.TestCase):
def setUp(self):
self.dbg = UserDbg()
self.proc = subprocess.Popen([target2])

def tearDown(self):
self.proc.terminate()
self.dbg = None

def test_attach_detach(self):
self.dbg.attach(self.proc.pid)
self.assertEqual(self.dbg.pid, self.proc.pid)
self.assertEqual(self.dbg.exec_status(), 'BREAK')
self.dbg.detach()

class TestTargetAttach2(unittest.TestCase):
def setUp(self):
self.dbg = UserDbg()
self.proc = subprocess.Popen([target2])

def tearDown(self):
self.proc.terminate()
self.dbg = None

def test_attach_terminate(self):
self.dbg.attach(self.proc.pid)
self.dbg.terminate()

class TestBasic(unittest.TestCase):
def setUp(self):
self.dbg = UserDbg()

def tearDown(self):
self.dbg = None

def test_ps(self):
self.dbg.ps()

def test_pids_by_name(self):
pids = self.dbg.pids_by_name("svchost.exe")
pprint.pprint(pids)

def test_exec_status(self):
self.assertEqual(self.dbg.exec_status(), 'NO_DEBUGGEE')

if __name__ == '__main__':
unittest.main()

0 comments on commit 5ae110a

Please sign in to comment.