forked from microsoft/Detours
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_module_api.cpp
More file actions
756 lines (578 loc) · 22.5 KB
/
Copy pathtest_module_api.cpp
File metadata and controls
756 lines (578 loc) · 22.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
//////////////////////////////////////////////////////////////////////////////
//
// Unit Tests for Detours Module API (test_module_api.cpp of unittests.exe)
//
// Microsoft Research Detours Package
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
#include "catch.hpp"
#include "windows.h"
#define DETOURS_INTERNAL
#include "detours.h"
#include "corruptor.h"
#include "payload.h"
#include "process_helpers.h"
// Expose the image base of the current module for test assertions.
//
extern "C" IMAGE_DOS_HEADER __ImageBase;
// Expose default module entry point for test assertions.
//
extern "C" int mainCRTStartup();
// Dummy function pointer used for tests.
//
void NoopFunction() { }
TEST_CASE("DetourLoadImageHlp", "[module]")
{
SECTION("Passing own function, results in own HMODULE")
{
auto info = DetourLoadImageHlp();
REQUIRE( info != nullptr );
REQUIRE( info->hDbgHelp != NULL);
REQUIRE( info->pfImagehlpApiVersionEx != nullptr );
REQUIRE( info->pfSymInitialize != nullptr );
REQUIRE( info->pfSymSetOptions != nullptr );
REQUIRE( info->pfSymGetOptions != nullptr );
REQUIRE( info->pfSymLoadModule64 != nullptr );
REQUIRE( info->pfSymGetModuleInfo64 != nullptr );
REQUIRE( info->pfSymFromName != nullptr );
}
}
TEST_CASE("DetourFindFunction", "[module]")
{
SECTION("Passing nullptr for all parameters, results in nullptr")
{
SetLastError(NO_ERROR);
auto func = DetourFindFunction(nullptr, nullptr);
REQUIRE( GetLastError() == ERROR_INVALID_PARAMETER );
REQUIRE( func == nullptr );
}
SECTION("Passing nullptr for function, results in nullptr")
{
SetLastError(NO_ERROR);
auto func = DetourFindFunction("ntdll.dll", nullptr);
REQUIRE( GetLastError() == ERROR_INVALID_PARAMETER );
REQUIRE( func == nullptr );
}
SECTION("Passing nullptr for module, results in nullptr")
{
SetLastError(NO_ERROR);
auto func = DetourFindFunction(nullptr, "FunctionThatDoesntExist");
REQUIRE( GetLastError() == ERROR_INVALID_PARAMETER );
REQUIRE( func == nullptr );
}
SECTION("Finding ntdll export is successful")
{
SetLastError(NO_ERROR);
auto func = DetourFindFunction("ntdll.dll", "NtDeviceIoControlFile");
REQUIRE( GetLastError() == NO_ERROR );
REQUIRE( func != nullptr );
}
}
TEST_CASE("DetourGetContainingModule", "[module]")
{
SECTION("Passing nullptr, results in nullptr")
{
SetLastError(NO_ERROR);
auto mod = DetourGetContainingModule(nullptr);
REQUIRE( GetLastError() == ERROR_BAD_EXE_FORMAT );
REQUIRE( mod == nullptr );
}
SECTION("Passing GetCommandLineW, results in kernel32 HMODULE")
{
SetLastError(ERROR_INVALID_HANDLE);
auto mod = DetourGetContainingModule(GetCommandLineW);
REQUIRE( GetLastError() == NO_ERROR );
REQUIRE( mod == LoadLibraryW(L"kernel32.dll") );
}
SECTION("Passing own function, results in own HMODULE")
{
SetLastError(ERROR_INVALID_HANDLE);
auto mod = DetourGetContainingModule(NoopFunction);
REQUIRE( GetLastError() == NO_ERROR );
REQUIRE( mod == reinterpret_cast<HMODULE>(&__ImageBase) );
}
}
TEST_CASE("DetourGetEntyPoint", "[module]")
{
SECTION("Passing nullptr, results in CRT entrypoint")
{
SetLastError(ERROR_INVALID_HANDLE);
auto entry = DetourGetEntryPoint(nullptr);
REQUIRE( GetLastError() == NO_ERROR );
REQUIRE( entry == mainCRTStartup );
}
SECTION("Passing nullptr, equals executing image")
{
REQUIRE( DetourGetEntryPoint(nullptr) ==
DetourGetEntryPoint(reinterpret_cast<HMODULE>(&__ImageBase)) );
}
SECTION("Passing ImageBase, results in CRT main")
{
SetLastError(ERROR_INVALID_HANDLE);
auto entry = DetourGetEntryPoint(reinterpret_cast<HMODULE>(&__ImageBase));
REQUIRE( GetLastError() == NO_ERROR );
REQUIRE( entry == mainCRTStartup );
}
SECTION("Corrupt image DOS header magic, results in bad exe format error")
{
ImageCorruptor corruptor(&__ImageBase);
corruptor.ModifyDosMagic(0xDEAD);
SetLastError(NO_ERROR);
auto entry = DetourGetEntryPoint(reinterpret_cast<HMODULE>(&__ImageBase));
REQUIRE( GetLastError() == ERROR_BAD_EXE_FORMAT );
REQUIRE( entry == nullptr );
}
SECTION("Corrupt image NT header signature, results in invalid signature error")
{
ImageCorruptor corruptor(&__ImageBase);
corruptor.ModifyNtSignature(0xDEADBEEF);
SetLastError(NO_ERROR);
auto entry = DetourGetEntryPoint(reinterpret_cast<HMODULE>(&__ImageBase));
REQUIRE( GetLastError() == ERROR_INVALID_EXE_SIGNATURE );
REQUIRE( entry == nullptr );
}
}
TEST_CASE("DetourGetModuleSize", "[module]")
{
SECTION("Passing nullptr, results in current module size")
{
SetLastError(ERROR_INVALID_HANDLE);
auto size = DetourGetModuleSize(nullptr);
REQUIRE( GetLastError() == NO_ERROR );
REQUIRE( size > 0 );
}
SECTION("Passing stack, results in error")
{
SetLastError(NO_ERROR);
int value;
auto size = DetourGetModuleSize(reinterpret_cast<HMODULE>(&value));
REQUIRE( GetLastError() == ERROR_BAD_EXE_FORMAT);
REQUIRE( size == 0 );
}
SECTION("Passing nullptr, equals executing image")
{
REQUIRE( DetourGetModuleSize(nullptr) ==
DetourGetModuleSize(reinterpret_cast<HMODULE>(&__ImageBase)) );
}
SECTION("Corrupt image DOS header magic, results in bad exe format error")
{
ImageCorruptor corruptor(&__ImageBase);
corruptor.ModifyDosMagic(0xDEAD);
SetLastError(NO_ERROR);
auto size = DetourGetModuleSize(reinterpret_cast<HMODULE>(&__ImageBase));
REQUIRE( GetLastError() == ERROR_BAD_EXE_FORMAT );
REQUIRE( size == 0 );
}
SECTION("Corrupt image NT header signature, results in invalid signature error")
{
ImageCorruptor corruptor(&__ImageBase);
corruptor.ModifyNtSignature(0xDEADBEEF);
SetLastError(NO_ERROR);
auto size = DetourGetModuleSize(reinterpret_cast<HMODULE>(&__ImageBase));
REQUIRE( GetLastError() == ERROR_INVALID_EXE_SIGNATURE );
REQUIRE( size == 0 );
}
}
TEST_CASE("DetourEnumerateModules", "[module]")
{
SECTION("Passing nullptr, results in current module being returned")
{
SetLastError(ERROR_INVALID_HANDLE);
auto mod = DetourEnumerateModules(nullptr);
REQUIRE( GetLastError() == NO_ERROR );
REQUIRE( mod != NULL );
}
SECTION("Passing stack, results in module")
{
SetLastError(NO_ERROR);
int value;
auto mod = DetourEnumerateModules(reinterpret_cast<HMODULE>(&value));
REQUIRE( GetLastError() == NO_ERROR );
REQUIRE( mod != NULL );
}
}
// Export test function, only used for test assertions.
//
__declspec(dllexport) void TestFunctionExport() { }
// Context object passed to DetourEnumerateExport(..)
//
struct EnumerateExportsTestContext
{
// Number of exports
//
int ExportCount { 0 };
// If the 'TestFunctionExport' export exists in the module.
//
bool ExportFound { false };
};
// Callback for each modue enumerated with DetourEnumerateExport(..)
//
BOOL CALLBACK ExportCallback(
_In_opt_ PVOID pContext,
_In_ ULONG nOrdinal,
_In_opt_ LPCSTR pszSymbol,
_In_opt_ PVOID pbTarget)
{
(void)pContext;
(void)pbTarget;
(void)nOrdinal;
EnumerateExportsTestContext* context =
reinterpret_cast<EnumerateExportsTestContext*>(pContext);
context->ExportCount++;
context->ExportFound |= Catch::contains(pszSymbol, "TestFunctionExport");
return TRUE;
}
TEST_CASE("DetourEnumerateExports", "[module]")
{
SECTION("Passing nullptr all, results in failure.")
{
SetLastError(NO_ERROR);
auto success = DetourEnumerateExports(nullptr, nullptr, nullptr);
REQUIRE( GetLastError() == ERROR_INVALID_PARAMETER );
REQUIRE_FALSE( success );
}
SECTION("Passing nullptr for just the module, resolves export in current modulee.")
{
SetLastError(ERROR_INVALID_HANDLE);
EnumerateExportsTestContext context {};
auto success = DetourEnumerateExports(nullptr, &context, ExportCallback);
REQUIRE( GetLastError() == NO_ERROR );
REQUIRE( success );
REQUIRE( context.ExportCount == 1 );
REQUIRE( context.ExportFound );
}
SECTION("Passing current module, resolves export correctly.")
{
SetLastError(ERROR_INVALID_HANDLE);
EnumerateExportsTestContext context {};
auto mod = reinterpret_cast<HMODULE>(&__ImageBase);
auto success = DetourEnumerateExports(mod, &context, ExportCallback);
REQUIRE( GetLastError() == NO_ERROR );
REQUIRE( success );
REQUIRE( context.ExportCount == 1 );
REQUIRE( context.ExportFound );
}
SECTION("Passing stack, results in error")
{
SetLastError(NO_ERROR);
int value;
auto mod = reinterpret_cast<HMODULE>(&value);
EnumerateExportsTestContext context {};
auto success = DetourEnumerateExports(mod, &context, ExportCallback);
REQUIRE( GetLastError() == ERROR_BAD_EXE_FORMAT);
REQUIRE_FALSE( success );
}
SECTION("Corrupt image DOS header magic, results in bad exe format error")
{
ImageCorruptor corruptor(&__ImageBase);
corruptor.ModifyDosMagic(0xDEAD);
SetLastError(NO_ERROR);
EnumerateExportsTestContext context {};
auto mod = reinterpret_cast<HMODULE>(&__ImageBase);
auto success = DetourEnumerateExports(mod, &context, ExportCallback);
REQUIRE( GetLastError() == ERROR_BAD_EXE_FORMAT );
REQUIRE_FALSE( success );
}
SECTION("Corrupt image NT header signature, results in invalid signature error")
{
ImageCorruptor corruptor(&__ImageBase);
corruptor.ModifyNtSignature(0xDEADBEEF);
SetLastError(NO_ERROR);
EnumerateExportsTestContext context {};
auto mod = reinterpret_cast<HMODULE>(&__ImageBase);
auto success = DetourEnumerateExports(mod, &context, ExportCallback);
REQUIRE( GetLastError() == ERROR_INVALID_EXE_SIGNATURE );
REQUIRE_FALSE( success );
}
}
// Context object passed to DetourEnumerateimportsExport(..)
//
struct EnumerateImportsTestContext
{
// Number of imports
//
int ImportCount { 0 };
// If the 'TestFunctionExport' export exists in the module.
//
bool ImportModuleFound { false };
// Number of imports
//
int ImportFuncCount { 0 };
// If the 'TestFunctionExport' export exists in the module.
//
bool ImportFuncFound { false };
};
// Callback for each module enumerated with DetourEnumerateImports(..)
//
BOOL WINAPI ImportFileCallback(PVOID pContext, HMODULE, PCSTR pszFile)
{
EnumerateImportsTestContext* context =
reinterpret_cast<EnumerateImportsTestContext*>(pContext);
context->ImportCount++;
context->ImportModuleFound |= Catch::contains(pszFile, "ntdll");
return TRUE;
}
// Callback for each function enumerated with DetourEnumerateImports(..)
//
BOOL WINAPI ImportFuncCallback(_In_opt_ PVOID pContext,
_In_ DWORD nOrdinal,
_In_opt_ LPCSTR pszFunc,
_In_opt_ PVOID pvFunc)
{
UNREFERENCED_PARAMETER(nOrdinal);
UNREFERENCED_PARAMETER(pszFunc);
UNREFERENCED_PARAMETER(pvFunc);
EnumerateImportsTestContext* context =
reinterpret_cast<EnumerateImportsTestContext*>(pContext);
context->ImportFuncCount++;
return TRUE;
}
TEST_CASE("DetourEnumerateImports", "[module]")
{
SECTION("Passing nullptr all, results in invalid parameter.")
{
SetLastError(NO_ERROR);
auto success = DetourEnumerateImports(nullptr, nullptr, nullptr, nullptr);
REQUIRE( GetLastError() == ERROR_INVALID_PARAMETER );
REQUIRE_FALSE( success );
}
SECTION("Passing nullptr for module callback, results in invalid parameter.")
{
SetLastError(NO_ERROR);
EnumerateImportsTestContext context {};
auto success = DetourEnumerateImports(nullptr, &context, ImportFileCallback, nullptr);
REQUIRE( GetLastError() == ERROR_INVALID_PARAMETER );
REQUIRE_FALSE( success );
REQUIRE( context.ImportCount == 0 );
REQUIRE_FALSE( context.ImportModuleFound );
}
SECTION("Passing nullptr for function callback, resolves in invalid parameter.")
{
SetLastError(ERROR_INVALID_HANDLE);
EnumerateImportsTestContext context {};
auto success = DetourEnumerateImports(nullptr, &context, nullptr, ImportFuncCallback);
REQUIRE( GetLastError() == ERROR_INVALID_PARAMETER );
REQUIRE_FALSE( success );
REQUIRE( context.ImportFuncCount == 0 );
REQUIRE_FALSE( context.ImportFuncFound );
}
}
TEST_CASE("DetourGetSizeOfPayloads", "[module]")
{
SECTION("Passing nullptr for module, is successful.")
{
SetLastError(ERROR_INVALID_HANDLE);
auto size = DetourGetSizeOfPayloads(nullptr);
REQUIRE( GetLastError() == NO_ERROR );
REQUIRE( size == sizeof(CPrivateStuff) );
}
SECTION("Passing nullptr is the same as current module.")
{
SetLastError(ERROR_INVALID_HANDLE);
auto mod = reinterpret_cast<HMODULE>(&__ImageBase);
auto nullSize = DetourGetSizeOfPayloads(nullptr);
auto modSize = DetourGetSizeOfPayloads(mod);
REQUIRE( modSize == nullSize );
}
SECTION("Passing a module with no payload, results in exe marked invalid.")
{
auto mod = GetModuleHandleW(L"ntdll.dll");
SetLastError(NO_ERROR);
auto size = DetourGetSizeOfPayloads(mod);
REQUIRE( GetLastError() == ERROR_EXE_MARKED_INVALID );
REQUIRE( size == 0 );
}
SECTION("Passing stack, results in error")
{
SetLastError(NO_ERROR);
int value;
auto mod = reinterpret_cast<HMODULE>(&value);
auto size = DetourGetSizeOfPayloads(mod);
REQUIRE( GetLastError() == ERROR_BAD_EXE_FORMAT );
REQUIRE( size == 0 );
}
SECTION("Corrupt image DOS header magic, results in bad exe format error")
{
ImageCorruptor corruptor(&__ImageBase);
corruptor.ModifyDosMagic(0xDEAD);
SetLastError(NO_ERROR);
auto mod = reinterpret_cast<HMODULE>(&__ImageBase);
auto size = DetourGetSizeOfPayloads(mod);
REQUIRE( GetLastError() == ERROR_BAD_EXE_FORMAT );
REQUIRE( size == 0 );
}
SECTION("Corrupt image NT header signature, results in invalid signature error")
{
ImageCorruptor corruptor(&__ImageBase);
corruptor.ModifyNtSignature(0xDEADBEEF);
SetLastError(NO_ERROR);
auto mod = reinterpret_cast<HMODULE>(&__ImageBase);
auto size = DetourGetSizeOfPayloads(mod);
REQUIRE( GetLastError() == ERROR_INVALID_EXE_SIGNATURE );
REQUIRE( size == 0 );
}
}
TEST_CASE("DetourFindPayload", "[module]")
{
SECTION("Passing empty guid, fails.")
{
SetLastError(NO_ERROR);
HMODULE module {};
GUID guid {};
DWORD data {};
auto payload = DetourFindPayload(module, guid, &data);
REQUIRE( payload == nullptr );
REQUIRE( data == 0 );
REQUIRE( GetLastError() == ERROR_INVALID_HANDLE );
}
SECTION("Passing nullptr for module with correct GUID, is successful.")
{
SetLastError(ERROR_INVALID_HANDLE);
HMODULE module {};
DWORD data {};
auto payload = DetourFindPayload(module, TEST_PAYLOAD_GUID, &data);
REQUIRE( GetLastError() == NO_ERROR );
REQUIRE( payload != nullptr );
REQUIRE( data == TEST_PAYLOAD_SIZE );
char* szPayloadMessage = reinterpret_cast<char*>(payload);
REQUIRE_THAT( szPayloadMessage, Catch::Matchers::Contains("123") );
}
}
TEST_CASE("DetourFindPayloadEx", "[module]")
{
SECTION("Passing empty guid, fails.")
{
SetLastError(NO_ERROR);
GUID guid {};
DWORD data {};
auto payload = DetourFindPayloadEx(guid, &data);
REQUIRE( payload == nullptr );
REQUIRE( data == 0 );
// This returns different values on different versions of windows.
//
REQUIRE( (GetLastError() == ERROR_MOD_NOT_FOUND || GetLastError() == ERROR_INVALID_HANDLE) );
}
SECTION("Finding module with correct GUID, is successful.")
{
SetLastError(ERROR_INVALID_HANDLE);
DWORD data {};
auto payload = DetourFindPayloadEx(TEST_PAYLOAD_GUID, &data);
REQUIRE( GetLastError() == NO_ERROR );
REQUIRE( payload != nullptr );
REQUIRE( data == TEST_PAYLOAD_SIZE );
char* szPayloadMessage = reinterpret_cast<char*>(payload);
REQUIRE_THAT( szPayloadMessage, Catch::Matchers::Contains("123") );
}
}
TEST_CASE("DetourCopyPayloadToProcessEx", "[module]")
{
// {44FA1CE0-1DA5-4AFC-946E-F96890C38673}
static constexpr GUID guid = { 0x44fa1ce0, 0x1da5, 0x4afc, { 0x94, 0x6e, 0xf9, 0x68, 0x90, 0xc3, 0x86, 0x73 } };
static constexpr std::uint32_t data = 0xDEADBEEF;
SECTION("Passing NULL process handle, results in error")
{
const auto ptr = DetourCopyPayloadToProcessEx(NULL, guid, &data, sizeof(data));
REQUIRE(GetLastError() == ERROR_INVALID_HANDLE);
REQUIRE(ptr == nullptr);
}
SECTION("Writing to own process, results in valid pointer")
{
const auto ptr = reinterpret_cast<std::uint32_t*>(DetourCopyPayloadToProcessEx(GetCurrentProcess(), guid, &data, sizeof(data)));
REQUIRE(GetLastError() == NO_ERROR);
REQUIRE(*ptr == data);
}
SECTION("Writing to different process, can be read with ReadProcessMemory")
{
// create a suspended copy of ourself to do things with.
TerminateOnScopeExit process{};
REQUIRE(SUCCEEDED(CreateSuspendedCopy(process)));
const auto ptr = DetourCopyPayloadToProcessEx(process.information.hProcess, guid, &data, sizeof(data));
REQUIRE(GetLastError() == NO_ERROR);
REQUIRE(ptr != nullptr);
std::uint32_t retrieved_data{};
REQUIRE(ReadProcessMemory(process.information.hProcess, ptr, &retrieved_data, sizeof(retrieved_data), nullptr));
REQUIRE(retrieved_data == data);
}
}
TEST_CASE("DetourFindRemotePayload", "[module]")
{
SECTION("Passing NULL process handle, results in error")
{
const auto ptr = DetourFindRemotePayload(NULL, TEST_PAYLOAD_GUID, nullptr);
REQUIRE(GetLastError() == ERROR_INVALID_HANDLE);
REQUIRE(ptr == nullptr);
}
SECTION("Finding null GUID from own process, results in error")
{
const GUID guid{};
const auto ptr = DetourFindRemotePayload(GetCurrentProcess(), guid, nullptr);
REQUIRE(GetLastError() == ERROR_MOD_NOT_FOUND);
REQUIRE(ptr == nullptr);
}
SECTION("Finding null GUID from different process, results in error")
{
// create a suspended copy of ourself to do things with.
TerminateOnScopeExit process{};
REQUIRE(SUCCEEDED(CreateSuspendedCopy(process)));
const GUID guid{};
const auto ptr = DetourFindRemotePayload(process.information.hProcess, guid, nullptr);
REQUIRE(GetLastError() == ERROR_MOD_NOT_FOUND);
REQUIRE(ptr == nullptr);
}
SECTION("Finding valid GUID from own process, results in valid pointer")
{
DWORD size = 0;
const auto ptr = reinterpret_cast<std::uint32_t*>(DetourFindRemotePayload(GetCurrentProcess(), TEST_PAYLOAD_GUID, &size));
REQUIRE(GetLastError() == NO_ERROR);
REQUIRE(ptr != nullptr);
REQUIRE(size == TEST_PAYLOAD_SIZE);
char* szPayloadMessage = reinterpret_cast<char*>(ptr);
REQUIRE_THAT(szPayloadMessage, Catch::Matchers::Contains("123"));
}
SECTION("Finding valid GUID from different process, can be read with ReadProcessMemory")
{
// create a suspended copy of ourself to do things with.
TerminateOnScopeExit process{};
REQUIRE(SUCCEEDED(CreateSuspendedCopy(process)));
DWORD size = 0;
const auto ptr = DetourFindRemotePayload(process.information.hProcess, TEST_PAYLOAD_GUID, &size);
REQUIRE(GetLastError() == NO_ERROR);
REQUIRE(ptr != nullptr);
REQUIRE(size == TEST_PAYLOAD_SIZE);
SIZE_T bytesRead = 0;
char szPayloadMessage[TEST_PAYLOAD_SIZE];
REQUIRE(ReadProcessMemory(process.information.hProcess, ptr, &szPayloadMessage, TEST_PAYLOAD_SIZE, &bytesRead));
REQUIRE(bytesRead == TEST_PAYLOAD_SIZE);
REQUIRE_THAT(szPayloadMessage, Catch::Matchers::Contains("123"));
}
}
TEST_CASE("DetourRestoreAfterWith", "[module]")
{
// TODO: Needs to be written.
}
TEST_CASE("DetourRestoreAfterWithEx", "[module]")
{
// TODO: Needs to be written.
}
// Define the import symbol so that we can get the address of the IAT entry for a static import
#ifdef _X86_
#pragma warning(disable:4483) // disable warning/error about __identifier(<string>)
#define __imp_SetLastError __identifier("_imp__SetLastError@4")
#endif
extern "C" extern void *__imp_SetLastError;
TEST_CASE("DetourIsFunctionImported", "[module]")
{
SECTION("Passing NULL code pointer, results in false")
{
REQUIRE(!DetourIsFunctionImported(NULL, reinterpret_cast<PBYTE>(&__imp_SetLastError)));
}
SECTION("Passing NULL target, results in false")
{
REQUIRE(!DetourIsFunctionImported(reinterpret_cast<PBYTE>(&__ImageBase), NULL));
}
SECTION("Passing imported function, results in true")
{
REQUIRE(DetourIsFunctionImported(reinterpret_cast<PBYTE>(&__ImageBase), reinterpret_cast<PBYTE>(&__imp_SetLastError)));
}
}