ReactOS  0.4.13-dev-645-g69454b4
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 */
16 static KMT_MESSAGE_HANDLER TestMessageHandler;
17 static 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 
92 VOID
95 {
96  PAGED_CODE();
97 
98  DPRINT("Unload!\n");
99 
100  ok_irql(PASSIVE_LEVEL);
102 
103  trace("Unloading example driver\n");
104 }
105 
127 static
128 NTSTATUS
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  }
151  case IOCTL_SEND_STRING:
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  }
167  case IOCTL_SEND_MYSTRUCT:
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 
218 static
219 NTSTATUS
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 }
signed char * PCHAR
Definition: retypes.h:7
#define ok_eq_ulong(value, expected)
const uint16_t * PCWSTR
Definition: typedefs.h:55
#define trace(...)
Definition: kmt_test.h:217
#define IN
Definition: typedefs.h:38
#define IOCTL_SEND_STRING
Definition: Example.h:18
NTSYSAPI VOID NTAPI RtlCopyMemory(VOID UNALIGNED *Destination, CONST VOID UNALIGNED *Source, ULONG Length)
#define IRP_MJ_CREATE
Definition: rdpdr.c:44
NTSYSAPI LONG NTAPI RtlCompareString(PSTRING String1, PSTRING String2, BOOLEAN CaseInSensitive)
static KMT_IRP_HANDLER TestIrpHandler
Definition: Example_drv.c:17
_In_ PIRP Irp
Definition: csq.h:116
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:323
#define ok_eq_pointer(value, expected)
LONG NTSTATUS
Definition: precomp.h:26
#define IOCTL_SEND_MYSTRUCT
Definition: Example.h:19
static KMT_MESSAGE_HANDLER TestMessageHandler
Definition: Example_drv.c:16
#define IOCTL_NOTIFY
Definition: Example.h:17
WCHAR DeviceName[]
Definition: adapter.cpp:21
int32_t INT
Definition: typedefs.h:56
#define PAGED_CODE()
Definition: video.h:57
_Must_inspect_result_ _In_ ULONG Flags
Definition: wsk.h:170
ULONG_PTR * PSIZE_T
Definition: typedefs.h:78
#define ok(value,...)
smooth NULL
Definition: ftsmooth.c:416
static PDRIVER_OBJECT DriverObject
Definition: template.c:42
#define IoCompleteRequest
Definition: irp.c:1240
void DPRINT(...)
Definition: polytest.cpp:61
_IRQL_requires_same_ typedef _In_ ULONG ControlCode
Definition: wmitypes.h:55
Definition: bufpool.h:45
static PDRIVER_OBJECT TestDriverObject
Definition: Example_drv.c:20
NTSTATUS KmtRegisterIrpHandler(IN UCHAR MajorFunction, IN PDEVICE_OBJECT DeviceObject OPTIONAL, IN PKMT_IRP_HANDLER IrpHandler)
USHORT MaximumLength
Definition: env_spec_w32.h:377
NTSTATUS KmtRegisterMessageHandler(IN ULONG ControlCode OPTIONAL, IN PDEVICE_OBJECT DeviceObject OPTIONAL, IN PKMT_MESSAGE_HANDLER MessageHandler)
NTSTATUS TestEntry(IN PDRIVER_OBJECT DriverObject, IN PCUNICODE_STRING RegistryPath, OUT PCWSTR *DeviceName, IN OUT INT *Flags)
Definition: Example_drv.c:48
#define IRP_MJ_CLOSE
Definition: rdpdr.c:45
static const WCHAR L[]
Definition: oid.c:1250
#define PASSIVE_LEVEL
Definition: env_spec_w32.h:693
Status
Definition: gdiplustypes.h:24
IN PDEVICE_OBJECT DeviceObject
Definition: fatprocs.h:1560
ULONG_PTR SIZE_T
Definition: typedefs.h:78
VOID TestUnload(IN PDRIVER_OBJECT DriverObject)
Definition: Example_drv.c:93
unsigned short USHORT
Definition: pedump.c:61
#define skip(...)
#define OUT
Definition: typedefs.h:39
unsigned int ULONG
Definition: retypes.h:1
#define IO_NO_INCREMENT
Definition: iotypes.h:565
_In_ PUNICODE_STRING RegistryPath
Definition: wmip.h:27
IN BOOLEAN OUT PSTR Buffer
Definition: progress.h:34
return STATUS_SUCCESS
Definition: btrfs.c:2777
#define RtlCompareMemory(s1, s2, l)
Definition: env_spec_w32.h:465
#define RTL_CONSTANT_STRING(s)
Definition: tunneltest.c:14
PULONG MinorVersion OPTIONAL
Definition: CrossNt.h:68