more pytest POC + fixed debugger code + test FunctionHXBP

This commit is contained in:
Clement Rouault
2017-09-07 17:13:59 +02:00
parent 89e486cd95
commit 7db5620210
7 changed files with 57 additions and 53 deletions
+23 -42
View File
@@ -27,23 +27,19 @@ else:
import sys
import weakref
def generate_pop_and_exit_fixtures(proc_popers, ids=[], dwCreationFlags=DEFAULT_CREATION_FLAGS):
@pytest.fixture(params=proc_popers, ids=ids)
def pop_and_exit_process(request):
proc_poper = request.param
proc = proc_poper(dwCreationFlags=dwCreationFlags)
yield proc # provide the fixture value
yield weakref.proxy(proc) # provide the fixture value
try:
print("EXIT PROC <{0}>".format(sys.getrefcount(proc)))
# if sys.getrefcount(proc) > 5:
# import pdb;pdb.set_trace()
proc.exit(0)
except WindowsError as e:
if not proc.is_exit:
raise
# import pdb;pdb.set_trace()
# proc.__del__()
del proc
return pop_and_exit_process
@@ -59,13 +55,7 @@ else:
dwCreationFlags=gdef.CREATE_SUSPENDED)
@pytest.fixture()
def check_for_gc_garbage(request):
garbage_before = set(gc.garbage)
yield
gc.collect()
new_garbage = set(gc.garbage) - garbage_before
assert not new_garbage, "Test generated uncollectable object ({0})".format(new_garbage)
class HandleDebugger(object):
def __init__(self, pid):
@@ -96,38 +86,29 @@ class HandleDebugger(object):
current_process_hdebugger = HandleDebugger(windows.current_process.pid)
current_process_hdebugger.refresh_handles()
# TST = current_process_hdebugger.refresh_handles()
RESULT = {}
@pytest.fixture()
def check_for_handle_leak(request):
# current_process_hdebugger.refresh_handles()
yield
# leaked_handles = current_process_hdebugger.get_new_handle()
# for lh in leaked_handles:
# RESULT[lh.wValue] = request.function.__name__
print("HANDLE LEAK SAVE")
# assert not leaked_handles, "Test Leaked <{0}> handles of types ({1})".format(len(leaked_handles), set(h.type for h in leaked_handles))
x = current_process_hdebugger.refresh_handles()
yield None
leaked_handles = current_process_hdebugger.get_new_handle(x)
try:
leaked_handles_types = set(h.type for h in leaked_handles)
except Exception as e:
leaked_handles = current_process_hdebugger.get_new_handle(x)
leaked_handles_types = set(h.type for h in leaked_handles)
# print("ERROR FOR TYPE, newleaked = {0}".format(current_process_hdebugger.get_new_handle(x)))
leaked_handles_types -= set(['EtwRegistration', 'Key', 'DebugObject', 'Event'])
assert not leaked_handles_types, "Test Leaked <{0}> handles of types ({1})".format(len(leaked_handles), leaked_handles_types)
@pytest.fixture(scope='session')
def check_for_handle_leak_final(request):
# x = current_process_hdebugger.get_handles()
print("CHECK HANDLE FINAL :D")
# current_process_hdebugger.refresh_handles()
yield
# leaked_handles = current_process_hdebugger.get_new_handle(x)
# import pdb;pdb.set_trace()
# print(leaked_handles)
# leaked_handles = current_process_hdebugger.get_new_handle()
# import pdb;pdb.set_trace()
# assert not leaked_handles, "Test Leaked <{0}> handles of types ({1})".format(len(leaked_handles), set(h.type for h in leaked_handles))
def pytest_unconfigure(*args, **kwargs):
import pdb;pdb.set_trace()
print(TST)
# pytestmark = pytest.mark.usefixtures('check_for_handle_leak_final')
@pytest.fixture()
def check_for_gc_garbage(request):
# print("GC CHECK")
garbage_before = set(gc.garbage)
yield None
gc.collect()
new_garbage = set(gc.garbage) - garbage_before
assert not new_garbage, "Test generated uncollectable object ({0})".format(new_garbage)
# print("GC CHECK END")
+6 -3
View File
@@ -11,7 +11,7 @@ import windows.native_exec.simple_x64 as x64
from conftest import generate_pop_and_exit_fixtures, pop_proc_32, pop_proc_64
from pfwtest import *
pytestmark = pytest.mark.usefixtures('check_for_gc_garbage')
# pytestmark = pytest.mark.usefixtures('check_for_gc_garbage', "check_for_handle_leak")
proc32_debug = generate_pop_and_exit_fixtures([pop_proc_32], ids=["proc32dbg"], dwCreationFlags=gdef.DEBUG_PROCESS)
proc64_debug = generate_pop_and_exit_fixtures([pop_proc_64], ids=["proc64dbg"], dwCreationFlags=gdef.DEBUG_PROCESS)
@@ -106,9 +106,11 @@ def test_multiple_hwx_breakpoint(proc32_64_debug):
# Used to verif we actually called the Breakpoints
assert TSTBP.COUNTER == 4
# @check_for_gc_garbage
@check_for_handle_leak
def test_four_hwx_breakpoint_fail(proc32_64_debug):
"""Check that setting 4HXBP in the same thread fails"""
# print("test_four_hwx_breakpoint_fail {0}".format(proc32_64_debug))
class TSTBP(windows.debug.HXBreakpoint):
def __init__(self, addr, expec_before):
self.addr = addr
@@ -165,7 +167,8 @@ def test_hwx_breakpoint_are_on_all_thread(proc32_64_debug):
# Used to verif we actually called the Breakpoints
assert TSTBP.COUNTER == 2
@check_for_handle_leak
# @check_for_handle_leak
# @check_for_gc_garbage
@pytest.mark.parametrize("bptype", [windows.debug.Breakpoint, windows.debug.HXBreakpoint])
def test_simple_breakpoint_name_addr(proc32_64_debug, bptype):
"""Check breakpoint address resolution for format dll!api"""
+5
View File
@@ -63,8 +63,13 @@ class TestCurrentProcessWithCheckGarbage(object):
assert isinstance(token.integrity, (int, long))
assert isinstance(token.is_elevated, (bool))
@check_for_handle_leak
def test_yolo(proc32_64):
print(proc32_64)
print(proc32_64.handle)
@check_for_handle_leak
@check_for_gc_garbage
class TestProcessWithCheckGarbage(object):
def test_pop_proc_32(self, proc32):