-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathmain.cpp
120 lines (96 loc) · 3.12 KB
/
main.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
#define _SILENCE_CXX23_ALIGNED_STORAGE_DEPRECATION_WARNING
#include <wdm.h>
EXTERN_C DRIVER_INITIALIZE DriverEntry;
EXTERN_C DRIVER_UNLOAD DriverUnload;
#ifdef ALLOC_PRAGMA
#pragma alloc_text(INIT, DriverEntry)
#pragma alloc_text(PAGE, DriverUnload)
#endif
void test_all();
#include <gtest/gtest.h>
#if CRTSYS_USE_NTL_MAIN
#include <iostream>
#include <ntl/driver>
#include <ntl/rpc/server>
// rpc server stub code
#include "common/rpc.hpp"
#include "common/test_device.h"
ntl::status ntl::main(ntl::driver &driver, const std::wstring ®istry_path) {
KdBreakPoint();
std::wcout << "load (registry_path :" << registry_path << ")\n";
test_all();
struct test_extension {
test_extension() : val(0) {
std::cout << "constructor - val : " << val << '\n';
}
~test_extension() { std::cout << "destroctor - val : " << val << '\n'; }
void inc() { val++; }
int val;
};
auto test_dev =
driver.create_device<test_extension>(ntl::device_options()
.name(TEST_DEVICE_NAME)
.type(FILE_DEVICE_UNKNOWN)
.exclusive());
if (test_dev) {
test_dev->extension().val = 100;
test_dev->extension().inc();
std::weak_ptr test_dev_weak = test_dev;
test_dev->on_device_control([test_dev_weak](
const ntl::device_control::code &code,
const ntl::device_control::in_buffer &in,
ntl::device_control::out_buffer &out) {
if (code == TEST_DEVICE_CTL) {
if (auto test_dev = test_dev_weak.lock())
test_dev->extension().val--;
std::string actual(reinterpret_cast<const char *>(in.ptr), in.size);
if (actual != "hello")
std::cout << "[FAILED] expect : hello, actual : " << actual << '\n';
if (out.ptr) {
strcpy_s(reinterpret_cast<char *>(out.ptr), out.size, "world");
} else {
std::cout << "[FAILED] out_buffer == null\n";
}
}
});
}
driver.on_unload([registry_path, test_dev,
rpc_svr = test_rpc::init(driver)]() mutable {
if (test_dev)
std::wcout << L"delete device :" << test_dev->name() << " - "
<< test_dev->extension().val << L'\n';
std::wcout << L"unload driver (registry_path :" << registry_path << L")\n";
});
testing::InitGoogleTest();
return RUN_ALL_TESTS() == 0 ? status::ok() : status(STATUS_UNSUCCESSFUL);
}
#else // !CRTSYS_USE_NTL_MAIN
// clang-format off
EXTERN_C
NTSTATUS
DriverEntry (
_In_ PDRIVER_OBJECT DriverObject,
_In_ PUNICODE_STRING RegistryPath
)
{
// clang-format on
PAGED_CODE();
UNREFERENCED_PARAMETER(RegistryPath);
KdBreakPoint();
test_all();
DriverObject->DriverUnload = DriverUnload;
testing::InitGoogleTest();
return RUN_ALL_TESTS() == 0 ? STATUS_SUCCESS : STATUS_UNSUCCESSFUL;
}
// clang-format off
EXTERN_C
VOID
DriverUnload (
_In_ PDRIVER_OBJECT DriverObject
)
{
// clang-format on
PAGED_CODE();
UNREFERENCED_PARAMETER(DriverObject);
}
#endif // !CRTSYS_USE_NTL_MAIN