This repository has been archived by the owner on Jul 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 97
/
unittest.cpp
368 lines (299 loc) · 8.86 KB
/
unittest.cpp
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
#include <Veil/Veil.h>
#include <kext/kallocator.h>
#include <string>
#include <random>
#include <vector>
#include <functional>
#include <mutex>
#include <unordered_map>
#include <system_error>
#include <thread>
#ifndef ASSERT
# define ASSERT assert
#endif
#define LOG(Format, ...) DbgPrintEx(DPFLTR_DEFAULT_ID, DPFLTR_ERROR_LEVEL, "[ucxxrt] [" __FUNCTION__ ":%u]: " Format "\n", __LINE__, ## __VA_ARGS__)
namespace UnitTest
{
template<typename T>
using Vector = std::vector<T, std::kallocator<T, PagedPool, 'tset'>>;
static Vector<std::function<void()>> TestVec;
#define TEST(name) TEST_ ## name
#define TEST_PUSH(name) ::UnitTest::TestVec.emplace_back(TEST(name))
class TEST(StaticObject)
{
ULONG* mData = nullptr;
public:
TEST(StaticObject)() noexcept
: mData(new ULONG[1]{ 1 })
{
LOG("has called.");
}
~TEST(StaticObject)() noexcept
{
LOG("has called.");
ASSERT(mData[0] == 1);
delete[] mData;
}
};
static TEST(StaticObject) StaticObject;
void TEST(Float2Int)()
{
// On x86, call _ftoui/_ftoui2/_ftoui3
float Val1 = 1.6f;
int Val2 = static_cast<int>(Val1);
ASSERT(Val2 == 1);
LOG("1.6f == %d", Val2);
}
void TEST(ThrowInt)()
{
try {
try {
try {
throw 1;
}
catch (const int& Exception) {
LOG("catch exception: %d", Exception);
}
}
catch (const std::string& Exception) {
ASSERT(false);
LOG("catch exception: %s", Exception.c_str());
}
}
catch (...) {
ASSERT(false);
LOG("catch exception: ...");
}
}
void TEST(ThrowObject)()
{
try {
try {
try {
throw std::string(__FUNCTION__);
}
catch (const int& Exception) {
ASSERT(false);
LOG("catch exception: %d", Exception);
}
}
catch (const std::string& Exception) {
LOG("catch exception: %s", Exception.c_str());
}
}
catch (...) {
ASSERT(false);
LOG("catch exception: ...");
}
}
void TEST(ThrowUnknow)()
{
try {
try {
try {
throw std::wstring(__FUNCTIONW__);
}
catch (const int& Exception) {
ASSERT(false);
LOG("catch exception: %d", Exception);
}
}
catch (const std::string& Exception) {
ASSERT(false);
LOG("catch exception: %s", Exception.c_str());
}
}
catch (...) {
LOG("catch exception: ...");
}
}
void TEST(Map)()
{
auto Sand = LARGE_INTEGER();
KeQueryTickCount(&Sand);
auto Rand = std::mt19937_64(Sand.QuadPart);
auto Map = std::unordered_map<uint32_t, std::string>();
for (auto Idx = 0u; Idx < 10; ++Idx) {
Map[Idx] = std::to_string(Rand());
}
for (const auto& [Idx, Val] : Map) {
LOG("map[%ld] = %s", Idx, Val.c_str());
}
}
template<typename T>
class TEST(InitializerListObject)
{
using iterator = typename std::vector<T>::iterator;
using const_iterator = typename std::vector<T>::const_iterator;
using reverse_iterator = typename std::vector<T>::reverse_iterator;
using const_reverse_iterator = typename std::vector<T>::const_reverse_iterator;
std::vector<T> mVec;
public:
TEST(InitializerListObject)(std::initializer_list<T> List)
: mVec(List)
{
}
[[nodiscard]] const_iterator begin() const noexcept
{
return mVec.begin();
}
[[nodiscard]] iterator end() noexcept
{
return mVec.end();
}
[[nodiscard]] const_iterator cbegin() const noexcept
{
return mVec.cbegin();
}
[[nodiscard]] const_iterator cend() const noexcept
{
return mVec.cend();
}
};
void TEST(InitializerList)()
{
auto Vec = TEST(InitializerListObject) { 0, 1, 2, 3, 4 };
int Idx = 0;
for (const auto Val : Vec) {
LOG("%d", Val);
ASSERT(Idx == Val); ++Idx;
}
}
std::unordered_map<std::string, ULONG_PTR> TEST(StaticObjectInitializer) =
{
{ "1", 1 },
{ "2", 2 },
{ "3", 3 },
{ "4", 4 },
{ "5", 5 },
};
void TEST(SystemErrorCode)()
{
std::error_code Code(STATUS_INVALID_PARAMETER, std::system_category());
LOG("%s", Code.message().c_str());
}
void TEST(Realloc)()
{
const auto Buffer1 = static_cast<int*>(malloc(sizeof(int)));
if (Buffer1) {
*Buffer1 = 123;
const auto Buffer2 = static_cast<int*>(realloc(Buffer1, sizeof(size_t)));
if (Buffer2) {
ASSERT(*Buffer2 == 123);
free(Buffer2);
}
else {
free(Buffer1);
}
}
}
// compile with: /EHa
void TEST(SEH)()
{
try {
constexpr int InvalidAddress = 0;
*reinterpret_cast<void**>(InvalidAddress) = TEST(SEH);
}
catch (...) {
LOG("Caught a C exception");
}
}
// compile with: /EHa
class TEST(StructExceptionTranslator)
{
unsigned int mCode = 0;
public:
TEST(StructExceptionTranslator) () = delete;
TEST(StructExceptionTranslator)(const TEST(StructExceptionTranslator)& Other) noexcept
: mCode(Other.mCode)
{
}
TEST(StructExceptionTranslator)(TEST(StructExceptionTranslator)&& Other) noexcept
: mCode(Other.mCode)
{
Other.mCode = 0;
}
explicit TEST(StructExceptionTranslator)(const unsigned int Code) noexcept
: mCode(Code)
{
}
[[nodiscard]] unsigned int GetCode() const noexcept
{
return mCode;
}
};
void TEST(SETranslate)()
{
_set_se_translator([](const unsigned int Code, _EXCEPTION_POINTERS* /*Exception*/) {
LOG("In SE Translator.");
throw TEST(StructExceptionTranslator)(Code);
});
try {
constexpr int InvalidAddress = 0;
*reinterpret_cast<void**>(InvalidAddress) = TEST(SETranslate);
}
catch (const TEST(StructExceptionTranslator)& Exception) {
LOG("Caught a __try exception, error 0x%08X.", Exception.GetCode());
}
}
std::mutex TEST(Mutex);
std::condition_variable TEST(ConditionVariable);
std::string TEST(ThreadData);
bool TEST(ThreadReady) = false;
bool TEST(ThreadProcessed) = false;
void TEST(Thread)()
{
auto Worker = std::thread([]
{
std::unique_lock Lock(TEST(Mutex));
TEST(ConditionVariable).wait(Lock, [] { return TEST(ThreadReady); });
LOG("Worker thread is processing data");
TEST(ThreadData) += " after processing";
TEST(ThreadProcessed) = true;
LOG("Worker thread signals data processing completed");
Lock.unlock();
TEST(ConditionVariable).notify_one();
});
TEST(ThreadData) = "Example data";
{
std::lock_guard Lock(TEST(Mutex));
TEST(ThreadReady) = true;
LOG("main() signals data ready for processing");
}
TEST(ConditionVariable).notify_one();
{
std::unique_lock Lock(TEST(Mutex));
TEST(ConditionVariable).wait(Lock, [] { return TEST(ThreadProcessed); });
}
LOG("Back in main(), data = %s", TEST(ThreadData).c_str());
Worker.join();
}
}
namespace Main
{
EXTERN_C NTSTATUS DriverMain(const PDRIVER_OBJECT DriverObject, const PUNICODE_STRING Registry)
{
using namespace UnitTest;
UNREFERENCED_PARAMETER(Registry);
LOG("entry.");
TEST_PUSH(Float2Int);
TEST_PUSH(ThrowInt);
TEST_PUSH(ThrowObject);
TEST_PUSH(ThrowUnknow);
TEST_PUSH(Map);
TEST_PUSH(InitializerList);
TEST_PUSH(SystemErrorCode);
TEST_PUSH(Realloc);
TEST_PUSH(SEH);
TEST_PUSH(SETranslate);
TEST_PUSH(Thread);
for (const auto& Test : TestVec) {
Test();
}
DriverObject->DriverUnload = [](auto)
{
LOG("exit.");
};
return 0l;
}
}