1#ifndef DTTR_TEST_SUPPORT_H
2#define DTTR_TEST_SUPPORT_H
17 CMUnitTestFunction
fn;
20#define DTTR_TEST_ARRAY_COUNT(TESTS) (sizeof(TESTS) / sizeof(*(TESTS)))
21#define DTTR_TEST_MAIN(TESTS) \
22 int main(int argc, char **argv) { \
23 return dttr_test_run_cases((TESTS), DTTR_TEST_ARRAY_COUNT(TESTS), argc, argv); \
32 const struct CMUnitTest tests[] = {
33 cmocka_unit_test(test_case->
fn),
36 return cmocka_run_group_tests_name(test_case->
name, tests,
NULL,
NULL);
45 const char *required = getenv(
"DTTR_REQUIRE_PCDOGS_FIXTURES");
46 if (required && required[0] && strcmp(required,
"0") != 0) {
47 fail_msg(
"required PCDOGS fixtures are unavailable");
56 size_t test_case_count,
59 if (!test_cases || !name) {
63 for (
size_t i = 0; i < test_case_count; i++) {
64 if (strcmp(name, test_cases[i].name) == 0) {
65 return &test_cases[i];
75 size_t test_case_count,
79 if (!test_cases || !argv || argc < 1) {
93 fprintf(stderr,
"unknown test case: %s\n", argv[1]);
98 fprintf(stderr,
"usage: %s [test-case]\n", argv[0]);
103 for (
size_t i = 0; i < test_case_count; i++) {
105 if (test_status != 0) {
106 status = test_status;
115#if defined(DTTR_TEST_BINARY_SUPPORT) && !defined(DTTR_TEST_BINARY_SUPPORT_H)
116#define DTTR_TEST_BINARY_SUPPORT_H
122#include <Zydis/Zydis.h>
125#define DTTR_TEST_SIG_NOT_FOUND UINTPTR_MAX
126#define DTTR_TEST_IMPORT_CAP 128u
127#define DTTR_TEST_FIXTURE_BIT(INDEX) (UINT64_C(1) << (INDEX))
128#define DTTR_TEST_FIXTURE_MASK_ALL(COUNT) \
129 ((DTTR_TestFixtureMask)(DTTR_TEST_FIXTURE_BIT(COUNT) - UINT64_C(1)))
131typedef uint64_t DTTR_TestFixtureMask;
135 const char *filename;
138} DTTR_TestBinaryFixture;
145 IMAGE_DATA_DIRECTORY imports_dir;
152 DTTR_TestFixtureMask required;
153} DTTR_TestPatternExpectation;
156 ZydisDecodedInstruction instruction;
157 ZydisDecodedOperand operands[ZYDIS_MAX_OPERAND_COUNT];
158} DTTR_TestDecodedInstruction;
164} DTTR_TestImportEntry;
167 DTTR_TEST_TARGET_RESOLVE,
168 DTTR_TEST_TARGET_JMP_HOOK,
169 DTTR_TEST_TARGET_TRAMPOLINE_HOOK,
170 DTTR_TEST_TARGET_POINTER_FF25_E8_TARGET,
171 DTTR_TEST_TARGET_POINTER_U32_AT_MATCH_PLUS_2,
172 DTTR_TEST_TARGET_BYTE_PATCH,
173} DTTR_TestTargetKind;
177 DTTR_TestTargetKind kind;
180 DTTR_TestFixtureMask required;
182 const uint8_t *patch_bytes;
184 const uint8_t *expected_original;
185 const char *expected_original_mask;
186} DTTR_TestTargetExpectation;
188typedef bool (*DTTR_TestPEFixtureVisitor)(
189 size_t fixture_index,
190 const DTTR_TestBinaryFixture *fixture,
192 const DTTR_TestPEImage *image,
215 const DTTR_TestBinaryFixture *fixtures,
216 size_t fixture_count,
217 const char *fixture_dir
221 const DTTR_TestBinaryFixture *fixtures,
222 size_t fixture_count,
223 const char *fixture_dir,
224 DTTR_TestPEFixtureVisitor visitor,
229 const DTTR_TestPEImage *image,
235 const DTTR_TestPEImage *image,
241 const DTTR_TestPEImage *image,
242 DTTR_TestImportEntry *imports,
248 const DTTR_TestPEImage *image,
250 DTTR_TestDecodedInstruction *out
254 const DTTR_TestBinaryFixture *fixture,
255 const DTTR_TestTargetExpectation *
target,
256 const DTTR_TestPEImage *image
void dttr_test_assert_target_resolved(const DTTR_TestBinaryFixture *fixture, const DTTR_TestTargetExpectation *target, const DTTR_TestPEImage *image)
bool dttr_test_case_equal(const char *a, const char *b)
bool dttr_test_pe_for_each_fixture(const DTTR_TestBinaryFixture *fixtures, size_t fixture_count, const char *fixture_dir, DTTR_TestPEFixtureVisitor visitor, void *userdata)
bool dttr_test_fixtures_available(const DTTR_TestBinaryFixture *fixtures, size_t fixture_count, const char *fixture_dir)
size_t DTTR_TestPE_CollectImports(const DTTR_TestPEImage *image, DTTR_TestImportEntry *imports, size_t imports_cap)
uintptr_t DTTR_TestPE_Sigscan(const DTTR_TestPEImage *image, const uint8_t *sig, const char *mask)
bool dttr_test_signed_range_valid(uintptr_t base, int32_t offset, size_t size, size_t total)
bool dttr_test_fixture_required(DTTR_TestFixtureMask required, size_t fixture_index)
bool dttr_test_zydis_decode32_at(const DTTR_TestPEImage *image, uintptr_t rva, DTTR_TestDecodedInstruction *out)
uintptr_t dttr_test_offset_site(uintptr_t base, int32_t offset)
bool dttr_test_range_valid(size_t offset, size_t size, size_t total)
size_t DTTR_TestPE_SigscanCount(const DTTR_TestPEImage *image, const uint8_t *sig, const char *mask)
DTTR_Graphics_COM_DirectDrawSurface7 DWORD flags void NULL
DTTR_Graphics_COM_DirectDrawSurface7 DWORD DWORD void void DWORD flags DTTR_Graphics_COM_DirectDrawSurface7 void void *cb void * target
static int dttr_test_run_case(const DTTR_TestCase *test_case)
static int dttr_test_run_cases(const DTTR_TestCase *test_cases, size_t test_case_count, int argc, char **argv)
static const DTTR_TestCase * dttr_test_find_case(const DTTR_TestCase *test_cases, size_t test_case_count, const char *name)
static void dttr_test_require_available(bool available)