ReactOS 0.4.15-dev-7131-ge4d03f4
Example_drv.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS kernel-mode tests
3 * LICENSE: LGPL-2.1+ (https://spdx.org/licenses/LGPL-2.1+)
4 * PURPOSE: Kernel-Mode Test Suite Example test driver
5 * COPYRIGHT: Copyright 2011-2018 Thomas Faber <thomas.faber@reactos.org>
6 */
7
8#include <kmt_test.h>
9
10//#define NDEBUG
11#include <debug.h>
12
13#include "Example.h"
14
15/* prototypes */
16static KMT_MESSAGE_HANDLER TestMessageHandler;
17static KMT_IRP_HANDLER TestIrpHandler;
18
19/* globals */
21
52 IN OUT INT *Flags)
53{
55
56 PAGED_CODE();
57
60
61 DPRINT("Entry!\n");
62
63 ok_irql(PASSIVE_LEVEL);
65
66 *DeviceName = L"Example";
67
68 trace("Hi, this is the example driver\n");
69
73
74 return Status;
75}
76
92VOID
95{
96 PAGED_CODE();
97
98 DPRINT("Unload!\n");
99
100 ok_irql(PASSIVE_LEVEL);
102
103 trace("Unloading example driver\n");
104}
105
127static
133 IN SIZE_T InLength,
134 IN OUT PSIZE_T OutLength)
135{
137
138 switch (ControlCode)
139 {
140 case IOCTL_NOTIFY:
141 {
142 static int TimesReceived = 0;
143
144 ++TimesReceived;
145 ok(TimesReceived == 1, "Received control code 1 %d times\n", TimesReceived);
147 ok_eq_ulong((ULONG)InLength, 0LU);
148 ok_eq_ulong((ULONG)*OutLength, 0LU);
149 break;
150 }
152 {
153 static int TimesReceived = 0;
154 ANSI_STRING ExpectedString = RTL_CONSTANT_STRING("yay");
155 ANSI_STRING ReceivedString;
156
157 ++TimesReceived;
158 ok(TimesReceived == 1, "Received control code 2 %d times\n", TimesReceived);
159 ok(Buffer != NULL, "Buffer is NULL\n");
160 ok_eq_ulong((ULONG)InLength, (ULONG)ExpectedString.Length);
161 ok_eq_ulong((ULONG)*OutLength, 0LU);
162 ReceivedString.MaximumLength = ReceivedString.Length = (USHORT)InLength;
163 ReceivedString.Buffer = Buffer;
164 ok(RtlCompareString(&ExpectedString, &ReceivedString, FALSE) == 0, "Received string: %Z\n", &ReceivedString);
165 break;
166 }
168 {
169 static int TimesReceived = 0;
170 MY_STRUCT ExpectedStruct = { 123, ":D" };
171 MY_STRUCT ResultStruct = { 456, "!!!" };
172
173 ++TimesReceived;
174 ok(TimesReceived == 1, "Received control code 3 %d times\n", TimesReceived);
175 ok(Buffer != NULL, "Buffer is NULL\n");
176 ok_eq_ulong((ULONG)InLength, (ULONG)sizeof ExpectedStruct);
177 ok_eq_ulong((ULONG)*OutLength, 2LU * sizeof ExpectedStruct);
178 if (!skip(Buffer && InLength >= sizeof ExpectedStruct, "Cannot read from buffer!\n"))
179 ok(RtlCompareMemory(&ExpectedStruct, Buffer, sizeof ExpectedStruct) == sizeof ExpectedStruct, "Buffer does not contain expected values\n");
180
181 if (!skip(Buffer && *OutLength >= 2 * sizeof ExpectedStruct, "Cannot write to buffer!\n"))
182 {
183 RtlCopyMemory((PCHAR)Buffer + sizeof ExpectedStruct, &ResultStruct, sizeof ResultStruct);
184 *OutLength = 2 * sizeof ExpectedStruct;
185 }
186 break;
187 }
188 default:
189 ok(0, "Got an unknown message! DeviceObject=%p, ControlCode=%lu, Buffer=%p, In=%lu, Out=%lu bytes\n",
190 DeviceObject, ControlCode, Buffer, InLength, *OutLength);
191 break;
192 }
193
194 return Status;
195}
196
218static
222 IN PIRP Irp,
223 IN PIO_STACK_LOCATION IoStackLocation)
224{
226
227 DPRINT("IRP!\n");
228
229 ok_irql(PASSIVE_LEVEL);
231
232 if (IoStackLocation->MajorFunction == IRP_MJ_CREATE)
233 trace("Got IRP_MJ_CREATE!\n");
234 else if (IoStackLocation->MajorFunction == IRP_MJ_CLOSE)
235 trace("Got IRP_MJ_CLOSE!\n");
236 else
237 trace("Got an IRP!\n");
238
239 Irp->IoStatus.Status = Status;
240 Irp->IoStatus.Information = 0;
241
243
244 return Status;
245}
#define PAGED_CODE()
#define IOCTL_NOTIFY
Definition: Example.h:17
#define IOCTL_SEND_MYSTRUCT
Definition: Example.h:19
#define IOCTL_SEND_STRING
Definition: Example.h:18
static KMT_MESSAGE_HANDLER TestMessageHandler
Definition: Example_drv.c:16
VOID TestUnload(IN PDRIVER_OBJECT DriverObject)
Definition: Example_drv.c:93
static PDRIVER_OBJECT TestDriverObject
Definition: Example_drv.c:20
NTSTATUS TestEntry(IN PDRIVER_OBJECT DriverObject, IN PCUNICODE_STRING RegistryPath, OUT PCWSTR *DeviceName, IN OUT INT *Flags)
Definition: Example_drv.c:48
static KMT_IRP_HANDLER TestIrpHandler
Definition: Example_drv.c:17
#define ok_eq_pointer(value, expected)
Definition: apitest.h:58
#define ok_eq_ulong(value, expected)
Definition: apitest.h:62
#define trace
Definition: atltest.h:70
#define ok(value,...)
Definition: atltest.h:57
#define skip(...)
Definition: atltest.h:64
LONG NTSTATUS
Definition: precomp.h:26
#define U(x)
Definition: wordpad.c:45
Definition: bufpool.h:45
_In_ PIRP Irp
Definition: csq.h:116
#define NULL
Definition: types.h:112
#define FALSE
Definition: types.h:117
#define PASSIVE_LEVEL
Definition: env_spec_w32.h:693
#define RtlCompareMemory(s1, s2, l)
Definition: env_spec_w32.h:465
Status
Definition: gdiplustypes.h:25
NTSTATUS KmtRegisterMessageHandler(IN ULONG ControlCode OPTIONAL, IN PDEVICE_OBJECT DeviceObject OPTIONAL, IN PKMT_MESSAGE_HANDLER MessageHandler)
NTSTATUS KmtRegisterIrpHandler(IN UCHAR MajorFunction, IN PDEVICE_OBJECT DeviceObject OPTIONAL, IN PKMT_IRP_HANDLER IrpHandler)
NTSYSAPI LONG NTAPI RtlCompareString(PSTRING String1, PSTRING String2, BOOLEAN CaseInSensitive)
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:317
#define IoCompleteRequest
Definition: irp.c:1240
#define L(x)
Definition: ntvdm.h:50
unsigned short USHORT
Definition: pedump.c:61
#define IRP_MJ_CLOSE
Definition: rdpdr.c:45
#define IRP_MJ_CREATE
Definition: rdpdr.c:44
#define STATUS_SUCCESS
Definition: shellext.h:65
#define DPRINT
Definition: sndvol32.h:71
PULONG MinorVersion OPTIONAL
Definition: CrossNt.h:68
USHORT MaximumLength
Definition: env_spec_w32.h:377
#define RTL_CONSTANT_STRING(s)
Definition: tunneltest.c:14
ULONG_PTR * PSIZE_T
Definition: typedefs.h:80
const uint16_t * PCWSTR
Definition: typedefs.h:57
ULONG_PTR SIZE_T
Definition: typedefs.h:80
int32_t INT
Definition: typedefs.h:58
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
#define IN
Definition: typedefs.h:39
uint32_t ULONG
Definition: typedefs.h:59
#define OUT
Definition: typedefs.h:40
char * PCHAR
Definition: typedefs.h:51
_In_ PDEVICE_OBJECT DeviceObject
Definition: wdfdevice.h:2055
_Must_inspect_result_ _In_ PWDFDEVICE_INIT _In_opt_ PCUNICODE_STRING DeviceName
Definition: wdfdevice.h:3275
_Must_inspect_result_ _In_ PDRIVER_OBJECT _In_ PCUNICODE_STRING RegistryPath
Definition: wdfdriver.h:215
_Must_inspect_result_ _In_ PDRIVER_OBJECT DriverObject
Definition: wdfdriver.h:213
_IRQL_requires_same_ typedef _In_ ULONG ControlCode
Definition: wmitypes.h:55
_Must_inspect_result_ _In_ ULONG Flags
Definition: wsk.h:170
#define IO_NO_INCREMENT
Definition: iotypes.h:598