Skip to content

Commit 23b1786

Browse files
authored
Refactor tests to be pytest-friendly (#61)
* Refactor test_techniques.py to be pytest-friendly * Fixes
1 parent aa20996 commit 23b1786

File tree

1 file changed

+95
-40
lines changed

1 file changed

+95
-40
lines changed

tests/test_techniques.py

Lines changed: 95 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@
66
import logging
77
import shutil
88
from functools import wraps
9-
import tempfile
109
import random
1110
import unittest
1211

1312
import claripy
1413

1514
import patcherex
16-
from patcherex.patch_master import PatchMaster
1715
from patcherex.backends.detourbackend import DetourBackend
1816
from patcherex.backends.reassembler_backend import ReassemblerBackend
1917
from patcherex.patches import *
@@ -123,8 +121,7 @@ def wrapper():
123121
f(DetourBackend, None, True)
124122
return wrapper
125123

126-
@add_fallback_strategy
127-
def test_shadowstack(BackendClass, data_fallback, try_pdf_removal):
124+
def _test_shadowstack(BackendClass, data_fallback, try_pdf_removal):
128125
filepath = os.path.join(bin_location, "CADET_00003")
129126
pipe = subprocess.PIPE
130127

@@ -147,8 +144,11 @@ def test_shadowstack(BackendClass, data_fallback, try_pdf_removal):
147144
assert p.returncode == 68
148145

149146

150-
@add_fallback_strategy
151-
def test_packer(BackendClass, data_fallback, try_pdf_removal):
147+
def test_shadowstack():
148+
add_fallback_strategy(_test_shadowstack)
149+
150+
151+
def _test_packer(BackendClass, data_fallback, try_pdf_removal):
152152
filepath = os.path.join(bin_location, "CADET_00003")
153153
pipe = subprocess.PIPE
154154

@@ -167,8 +167,11 @@ def test_packer(BackendClass, data_fallback, try_pdf_removal):
167167
assert res[0] == expected and p.returncode == 0
168168

169169

170-
@add_fallback_strategy
171-
def test_simplecfi(BackendClass, data_fallback, try_pdf_removal):
170+
def test_packer():
171+
add_fallback_strategy(_test_packer)
172+
173+
174+
def _test_simplecfi(BackendClass, data_fallback, try_pdf_removal):
172175
filepath = os.path.join(bin_location, "0b32aa01_01_2")
173176
pipe = subprocess.PIPE
174177

@@ -207,8 +210,11 @@ def test_simplecfi(BackendClass, data_fallback, try_pdf_removal):
207210
assert res[0] == expected3 and p.returncode == 0x45
208211

209212

210-
@add_fallback_strategy
211-
def test_qemudetection(BackendClass, data_fallback, try_pdf_removal):
213+
def test_simplecfi():
214+
add_fallback_strategy(_test_simplecfi)
215+
216+
217+
def _test_qemudetection(BackendClass, data_fallback, try_pdf_removal):
212218
filepath = os.path.join(bin_location, "0b32aa01_01_2")
213219
pipe = subprocess.PIPE
214220

@@ -237,8 +243,11 @@ def test_qemudetection(BackendClass, data_fallback, try_pdf_removal):
237243
assert res[0] == expected and p.returncode == 0
238244

239245

240-
@add_fallback_strategy
241-
def test_randomsyscallloop(BackendClass, data_fallback, try_pdf_removal):
246+
def test_qemudetection():
247+
add_fallback_strategy(_test_qemudetection)
248+
249+
250+
def _test_randomsyscallloop(BackendClass, data_fallback, try_pdf_removal):
242251
filepath = os.path.join(bin_location, "CADET_00003")
243252
pipe = subprocess.PIPE
244253

@@ -262,8 +271,11 @@ def test_randomsyscallloop(BackendClass, data_fallback, try_pdf_removal):
262271
assert p.returncode == -11
263272

264273

265-
@add_fallback_strategy
266-
def test_cpuid(BackendClass, data_fallback, try_pdf_removal):
274+
def test_randomsyscallloop():
275+
add_fallback_strategy(_test_randomsyscallloop)
276+
277+
278+
def _test_cpuid(BackendClass, data_fallback, try_pdf_removal):
267279
filepath = os.path.join(bin_location, "CADET_00003")
268280
pipe = subprocess.PIPE
269281

@@ -288,8 +300,11 @@ def test_cpuid(BackendClass, data_fallback, try_pdf_removal):
288300
assert p.returncode == -11
289301

290302

291-
@reassembler_only
292-
def test_stackretencryption(BackendClass, data_fallback, try_pdf_removal):
303+
def test_cpuid():
304+
add_fallback_strategy(_test_cpuid)
305+
306+
307+
def _test_stackretencryption(BackendClass, data_fallback, try_pdf_removal):
293308
filepath1 = os.path.join(bin_location, "0b32aa01_01_2")
294309
filepath2 = os.path.join(bin_location, "CROMU_00070")
295310
filepath3 = os.path.join(bin_location, "original/CROMU_00008")
@@ -421,8 +436,11 @@ def test_stackretencryption(BackendClass, data_fallback, try_pdf_removal):
421436
'''
422437

423438

424-
@reassembler_only
425-
def test_indirectcfi(BackendClass, data_fallback, try_pdf_removal):
439+
def test_stackretencryption():
440+
reassembler_only(_test_stackretencryption)
441+
442+
443+
def _test_indirectcfi(BackendClass, data_fallback, try_pdf_removal):
426444
tests = [
427445
("patchrex/indirect_call_test_O0", b"b7fff000"),
428446
#("patchrex/indirect_call_test_fullmem_O0", b"78000000"),
@@ -647,6 +665,10 @@ def test_indirectcfi(BackendClass, data_fallback, try_pdf_removal):
647665
'''
648666

649667

668+
def test_indirectcfi():
669+
reassembler_only(_test_indirectcfi)
670+
671+
650672
def test_freeregs():
651673
def bin_str(name,btype="original"):
652674
return "%s/%s" % (btype,name)
@@ -684,8 +706,7 @@ def bin_str(name,btype="original"):
684706
#import IPython; IPython.embed()
685707

686708

687-
@reassembler_only
688-
def test_transmitprotection(BackendClass, data_fallback, try_pdf_removal):
709+
def _test_transmitprotection(BackendClass, data_fallback, try_pdf_removal):
689710
def check_test(test):
690711
values,expected_crash = test
691712
tinput = b"08048000\n00000005\n"
@@ -804,8 +825,11 @@ def check_test(test):
804825
check_test(test)
805826

806827

807-
@reassembler_only
808-
def test_shiftstack(BackendClass, data_fallback, try_pdf_removal):
828+
def test_transmitprotection():
829+
reassembler_only(_test_transmitprotection)
830+
831+
832+
def _test_shiftstack(BackendClass, data_fallback, try_pdf_removal):
809833
filepath = os.path.join(bin_location, "CROMU_00044")
810834
tinput = b"1\n" * 50 + b"2\n" * 50
811835

@@ -858,9 +882,11 @@ def test_shiftstack(BackendClass, data_fallback, try_pdf_removal):
858882
print(map(hex,random_stack_pos))
859883
assert len(random_stack_pos)>=2
860884

885+
def test_shiftstack():
886+
reassembler_only(_test_shiftstack)
887+
861888

862-
@try_reassembler_and_detour_full # this changes the headers, let't test it in all 4 cases
863-
def test_nxstack(BackendClass, data_fallback, try_pdf_removal):
889+
def _test_nxstack(BackendClass, data_fallback, try_pdf_removal):
864890
filepath = os.path.join(bin_location, "CROMU_00044")
865891
tinput = b"login\n" * 50 + b"2\n"*50
866892
res = QEMURunner(filepath,tinput,record_stdout=True)
@@ -968,8 +994,12 @@ def test_nxstack(BackendClass, data_fallback, try_pdf_removal):
968994
'''
969995

970996

971-
@try_reassembler_and_detour
972-
def test_adversarial(BackendClass, data_fallback, try_pdf_removal):
997+
def test_nxstack():
998+
# this changes the headers, let's test it in all 4 cases
999+
try_reassembler_and_detour_full(_test_nxstack)
1000+
1001+
1002+
def _test_adversarial(BackendClass, data_fallback, try_pdf_removal):
9731003
pipe = subprocess.PIPE
9741004
tinput = b"1\n" * 50 + b"2\n"*50
9751005
filepath = os.path.join(bin_location, "CROMU_00044")
@@ -994,8 +1024,11 @@ def test_adversarial(BackendClass, data_fallback, try_pdf_removal):
9941024
assert original_res[1] != patched_res[1]
9951025

9961026

997-
@reassembler_only
998-
def test_backdoor(BackendClass, data_fallback, try_pdf_removal):
1027+
def test_adversarial():
1028+
try_reassembler_and_detour_full(_test_adversarial)
1029+
1030+
1031+
def _test_backdoor(BackendClass, data_fallback, try_pdf_removal):
9991032
def solution_to_bytearray(l):
10001033
# deal with endianness craziness
10011034
return bytes([ l[3],l[2],l[1],l[0],0,0,0,l[4] ])
@@ -1104,8 +1137,11 @@ def solution_to_bytearray(l):
11041137
'''
11051138

11061139

1107-
@reassembler_only
1108-
def test_bitflip(BackendClass, data_fallback, try_pdf_removal):
1140+
def test_backdoor():
1141+
reassembler_only(_test_backdoor)
1142+
1143+
1144+
def _test_bitflip(BackendClass, data_fallback, try_pdf_removal):
11091145
all_chars = [bytes([c]) for c in range(256)]
11101146
pipe = subprocess.PIPE
11111147
tests = []
@@ -1162,8 +1198,10 @@ def test_bitflip(BackendClass, data_fallback, try_pdf_removal):
11621198
print(test, tlen)
11631199
assert expected == patched
11641200

1165-
@reassembler_only
1166-
def test_uninitialized(BackendClass, data_fallback, try_pdf_removal):
1201+
def test_bitflip():
1202+
reassembler_only(_test_bitflip)
1203+
1204+
def _test_uninitialized(BackendClass, data_fallback, try_pdf_removal):
11671205
filepath = os.path.join(bin_location, "CROMU_00070")
11681206

11691207
with patcherex.utils.tempdir() as td:
@@ -1204,8 +1242,12 @@ def test_uninitialized(BackendClass, data_fallback, try_pdf_removal):
12041242
res = p.communicate(poll_input)
12051243
assert expected_output == res[0]
12061244

1207-
@reassembler_only
1208-
def test_malloc_patcher(BackendClass, data_fallback, try_pdf_removal):
1245+
1246+
def test_uninitialized():
1247+
reassembler_only(_test_uninitialized)
1248+
1249+
1250+
def _test_malloc_patcher(BackendClass, data_fallback, try_pdf_removal):
12091251
filepath = os.path.join(bin_location, "NRFIN_00078")
12101252

12111253
with patcherex.utils.tempdir() as td:
@@ -1239,6 +1281,10 @@ def test_malloc_patcher(BackendClass, data_fallback, try_pdf_removal):
12391281
'''
12401282

12411283

1284+
def test_malloc_patcher():
1285+
reassembler_only(_test_malloc_patcher)
1286+
1287+
12421288
@reassembler_only
12431289
def disable_no_flag_printf(BackendClass, data_fallback, try_pdf_removal):
12441290

@@ -1304,8 +1350,8 @@ def disable_no_flag_printf(BackendClass, data_fallback, try_pdf_removal):
13041350
assert expected_stdout == actual_stdout
13051351
'''
13061352

1307-
@detour_only
1308-
def test_countdown_1(BackendClass, data_fallback, try_pdf_removal):
1353+
1354+
def _test_countdown_1(BackendClass, data_fallback, try_pdf_removal):
13091355
filepath = os.path.join(bin_location, "countdown_test")
13101356

13111357
with patcherex.utils.tempdir() as td:
@@ -1329,8 +1375,11 @@ def test_countdown_1(BackendClass, data_fallback, try_pdf_removal):
13291375
assert expected_output == res[0]
13301376

13311377

1332-
@detour_only
1333-
def test_countdown_2(BackendClass, data_fallback, try_pdf_removal):
1378+
def test_countdown_1():
1379+
reassembler_only(_test_countdown_1)
1380+
1381+
1382+
def _test_countdown_2(BackendClass, data_fallback, try_pdf_removal):
13341383
filepath = os.path.join(bin_location, "countdown_test")
13351384

13361385
with patcherex.utils.tempdir() as td:
@@ -1351,9 +1400,11 @@ def test_countdown_2(BackendClass, data_fallback, try_pdf_removal):
13511400
res = p.communicate(b"foo\n")
13521401
assert expected_output == res[0]
13531402

1403+
def test_countdown_2():
1404+
reassembler_only(_test_countdown_2)
1405+
13541406

1355-
@detour_only
1356-
def test_countdown_3(BackendClass, data_fallback, try_pdf_removal):
1407+
def _test_countdown_3(BackendClass, data_fallback, try_pdf_removal):
13571408
filepath = os.path.join(bin_location, "countdown_test")
13581409

13591410
with patcherex.utils.tempdir() as td:
@@ -1377,6 +1428,10 @@ def test_countdown_3(BackendClass, data_fallback, try_pdf_removal):
13771428
assert expected_output == res[0]
13781429

13791430

1431+
def test_countdown_3():
1432+
detour_only(_test_countdown_3)
1433+
1434+
13801435
def run_all():
13811436
functions = globals()
13821437
all_functions = dict(filter((lambda x: x[0].startswith('test_')), functions.items()))

0 commit comments

Comments
 (0)