ReactOS 0.4.15-dev-7842-g558ab78
fltsupport.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS kernel-mode tests
3 * LICENSE: GPLv2+ - See COPYING in the top level directory
4 * PURPOSE: File system mini-filter support routines
5 * PROGRAMMER: Ged Murphy <gedmurphy@reactos.org>
6 */
7
8#include <kmt_test.h>
9
10#define KMT_FLT_USER_MODE
11#include "kmtest.h"
12#include <kmt_public.h>
13
14#include <assert.h>
15#include <debug.h>
16
17/*
18 * We need to call the internal function in the service.c file
19 */
23 IN PCWSTR ServicePath,
24 IN PCWSTR DisplayName OPTIONAL,
25 IN DWORD ServiceType,
26 OUT SC_HANDLE *ServiceHandle);
27
29 _In_z_ LPWSTR lpPrivName,
31
32// move to a shared location
34{
38
40
41extern HANDLE KmtestHandle;
43
44
45
65 _In_z_ PCWSTR DisplayName,
66 _Out_ SC_HANDLE *ServiceHandle)
67{
68 WCHAR ServicePath[MAX_PATH];
69
70 StringCbCopyW(ServicePath, sizeof(ServicePath), ServiceName);
71 StringCbCatW(ServicePath, sizeof(ServicePath), L"_drv.sys");
72
75
77 ServicePath,
78 DisplayName,
80 ServiceHandle);
81}
82
99 _Inout_ SC_HANDLE *ServiceHandle)
100{
101 return KmtDeleteService(ServiceName, ServiceHandle);
102}
103
111DWORD
113 _In_ BOOLEAN EnableDriverLoadPrivlege,
114 _In_ BOOLEAN RestartIfRunning,
115 _In_ BOOLEAN ConnectComms,
116 _Out_ HANDLE *hPort
117)
118{
119 DWORD Error;
120
121 if (EnableDriverLoadPrivlege)
122 {
124 if (Error)
125 {
126 return Error;
127 }
128 }
129
131 if ((Error == ERROR_SERVICE_ALREADY_RUNNING) && RestartIfRunning)
132 {
134 if (Error)
135 {
136 // TODO
137 __debugbreak();
138 }
139
141 }
142
143 if (Error)
144 {
145 return Error;
146 }
147
148 if (ConnectComms)
149 {
150 Error = KmtFltConnectComms(hPort);
151 }
152
153 return Error;
154}
155
168DWORD
170 _In_ HANDLE *hPort,
171 _In_ BOOLEAN DisonnectComms)
172{
174
175 if (DisonnectComms)
176 {
177 Error = KmtFltDisconnect(hPort);
178
179 if (Error)
180 {
181 return Error;
182 }
183 }
184
186
187 if (Error)
188 {
189 // TODO
190 __debugbreak();
191 }
192
193 return Error;
194}
195
206DWORD
208 _Out_ HANDLE *hPort)
209{
210 return KmtFltConnect(TestServiceName, hPort);
211}
212
223DWORD
225 _In_ HANDLE hPort)
226{
227 return KmtFltDisconnect(hPort);
228}
229
230
243 _Inout_ SC_HANDLE *ServiceHandle)
244{
245 return KmtCloseService(ServiceHandle);
246}
247
260DWORD
262 _In_ HANDLE hPort,
263 _In_z_ PCSTR TestName)
264{
265 return KmtFltSendStringToDriver(hPort, KMTFLT_RUN_TEST, TestName);
266}
267
280DWORD
282 _In_ HANDLE hPort,
284{
285 assert(hPort);
286 return KmtFltSendBufferToDriver(hPort, Message, NULL, 0, NULL, 0, NULL);
287}
288
304DWORD
306 _In_ HANDLE hPort,
309{
310 assert(hPort);
311 assert(String);
313}
314
329DWORD
331 _In_ HANDLE hPort,
334{
335 return KmtFltSendBufferToDriver(hPort, Message, (PVOID)String, (DWORD)wcslen(String) * sizeof(WCHAR), NULL, 0, NULL);
336}
337
352DWORD
354 _In_ HANDLE hPort,
357{
358 return KmtFltSendBufferToDriver(hPort, Message, &Value, sizeof(Value), NULL, 0, NULL);
359}
360
383DWORD
385 _In_ HANDLE hPort,
392{
397 DWORD Error;
398
399 assert(hPort);
400
401 if (BufferSize)
402 {
403 assert(InBuffer);
404
407 if (!Ptr)
408 {
410 }
412 }
413 else
414 {
416 Ptr = &Header;
417 }
418
419 Ptr->Message = Message;
420 if (BufferSize)
421 {
422 Ptr->Buffer = (Ptr + 1);
423 StringCbCopy(Ptr->Buffer, BufferSize, InBuffer);
424 Ptr->BufferSize = BufferSize;
425 }
426
428
429 if (FreeMemory)
430 {
432 }
433
434 return Error;
435}
436
447DWORD
450{
451 WCHAR DefaultInstance[128];
452 WCHAR KeyPath[256];
453 HKEY hKey = NULL;
454 HKEY hSubKey = NULL;
455 DWORD Zero = 0;
456 LONG Error;
457
458 StringCbCopyW(KeyPath, sizeof(KeyPath), L"SYSTEM\\CurrentControlSet\\Services\\");
459 StringCbCatW(KeyPath, sizeof(KeyPath), TestServiceName);
460 StringCbCatW(KeyPath, sizeof(KeyPath), L"\\Instances\\");
461
463 KeyPath,
464 0,
465 NULL,
468 NULL,
469 &hKey,
470 NULL);
471 if (Error != ERROR_SUCCESS)
472 {
473 return Error;
474 }
475
476 StringCbCopyW(DefaultInstance, sizeof(DefaultInstance), TestServiceName);
477 StringCbCatW(DefaultInstance, sizeof(DefaultInstance), L" Instance");
478
480 L"DefaultInstance",
481 0,
482 REG_SZ,
483 (LPBYTE)DefaultInstance,
484 (lstrlenW(DefaultInstance) + 1) * sizeof(WCHAR));
485 if (Error != ERROR_SUCCESS)
486 {
487 goto Quit;
488 }
489
490 Error = RegCreateKeyW(hKey, DefaultInstance, &hSubKey);
491 if (Error != ERROR_SUCCESS)
492 {
493 goto Quit;
494 }
495
496 Error = RegSetValueExW(hSubKey,
497 L"Altitude",
498 0,
499 REG_SZ,
501 (lstrlenW(Altitude) + 1) * sizeof(WCHAR));
502 if (Error != ERROR_SUCCESS)
503 {
504 goto Quit;
505 }
506
507 Error = RegSetValueExW(hSubKey,
508 L"Flags",
509 0,
510 REG_DWORD,
511 (LPBYTE)&Zero,
512 sizeof(DWORD));
513
514Quit:
515 if (hSubKey)
516 {
517 RegCloseKey(hSubKey);
518 }
519 if (hKey)
520 {
522 }
523
524 return Error;
525
526}
527
528/*
529* Private functions, not meant for use in kmtests
530*/
531
533 _In_ HANDLE hToken,
534 _In_z_ LPWSTR lpPrivName,
536{
538 LUID luid;
540 DWORD dwError = ERROR_SUCCESS;
541
542 /* Get the luid for this privilege */
543 if (!LookupPrivilegeValueW(NULL, lpPrivName, &luid))
544 return GetLastError();
545
546 /* Setup the struct with the priv info */
547 TokenPrivileges.PrivilegeCount = 1;
548 TokenPrivileges.Privileges[0].Luid = luid;
549 TokenPrivileges.Privileges[0].Attributes = bEnable ? SE_PRIVILEGE_ENABLED : 0;
550
551 /* Enable the privilege info in the token */
553 FALSE,
555 sizeof(TOKEN_PRIVILEGES),
556 NULL,
557 NULL);
558 if (bSuccess == FALSE) dwError = GetLastError();
559
560 /* return status */
561 return dwError;
562}
563
565 _In_z_ LPWSTR lpPrivName,
567{
568 HANDLE hToken;
570 DWORD dwError = ERROR_SUCCESS;
571
572 /* Get a handle to our token */
575 &hToken);
576 if (bSuccess == FALSE) return GetLastError();
577
578 /* Enable the privilege in the agent token */
579 dwError = EnablePrivilege(hToken, lpPrivName, bEnable);
580
581 /* We're done with this now */
582 CloseHandle(hToken);
583
584 /* return status */
585 return dwError;
586}
unsigned char BOOLEAN
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
static VOID FreeMemory(PCREATE_DATA Data)
Definition: create.c:134
static WCHAR ServiceName[]
Definition: browser.c:19
BOOL Error
Definition: chkdsk.c:66
#define RegCloseKey(hKey)
Definition: registry.h:49
Definition: Header.h:9
#define ERROR_NOT_ENOUGH_MEMORY
Definition: dderror.h:7
#define BufferSize
Definition: mmc.h:75
#define ERROR_SUCCESS
Definition: deptool.c:10
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
LONG WINAPI RegSetValueExW(_In_ HKEY hKey, _In_ LPCWSTR lpValueName, _In_ DWORD Reserved, _In_ DWORD dwType, _In_ CONST BYTE *lpData, _In_ DWORD cbData)
Definition: reg.c:4882
LONG WINAPI RegCreateKeyW(HKEY hKey, LPCWSTR lpSubKey, PHKEY phkResult)
Definition: reg.c:1201
BOOL WINAPI LookupPrivilegeValueW(LPCWSTR lpSystemName, LPCWSTR lpPrivilegeName, PLUID lpLuid)
Definition: misc.c:782
BOOL WINAPI AdjustTokenPrivileges(HANDLE TokenHandle, BOOL DisableAllPrivileges, PTOKEN_PRIVILEGES NewState, DWORD BufferLength, PTOKEN_PRIVILEGES PreviousState, PDWORD ReturnLength)
Definition: security.c:374
BOOL WINAPI OpenProcessToken(HANDLE ProcessHandle, DWORD DesiredAccess, PHANDLE TokenHandle)
Definition: security.c:294
#define CloseHandle
Definition: compat.h:739
#define GetProcessHeap()
Definition: compat.h:736
#define HeapAlloc
Definition: compat.h:733
#define GetCurrentProcess()
Definition: compat.h:759
#define MAX_PATH
Definition: compat.h:34
#define HeapFree(x, y, z)
Definition: compat.h:735
#define HEAP_ZERO_MEMORY
Definition: compat.h:134
#define lstrlenW
Definition: compat.h:750
static const WCHAR Message[]
Definition: register.c:74
#define assert(x)
Definition: debug.h:53
static BOOLEAN bSuccess
Definition: drive.cpp:433
IN PVCB IN VBO IN ULONG OUT PBCB OUT PVOID IN BOOLEAN IN BOOLEAN Zero
Definition: fatprocs.h:418
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
_Must_inspect_result_ _Inout_ PFLT_VOLUME _In_ PCUNICODE_STRING Altitude
Definition: fltkernel.h:1173
struct _KMTFLT_MESSAGE_HEADER KMTFLT_MESSAGE_HEADER
static WCHAR TestServiceName[MAX_PATH]
Definition: fltsupport.c:42
DWORD KmtFltCloseService(_Inout_ SC_HANDLE *ServiceHandle)
Definition: fltsupport.c:242
DWORD KmtFltSendToDriver(_In_ HANDLE hPort, _In_ DWORD Message)
Definition: fltsupport.c:281
DWORD EnablePrivilege(_In_ HANDLE hToken, _In_z_ LPWSTR lpPrivName, _In_ BOOL bEnable)
Definition: fltsupport.c:532
DWORD KmtFltCreateService(_In_z_ PCWSTR ServiceName, _In_z_ PCWSTR DisplayName, _Out_ SC_HANDLE *ServiceHandle)
Definition: fltsupport.c:63
DWORD KmtFltRunKernelTest(_In_ HANDLE hPort, _In_z_ PCSTR TestName)
Definition: fltsupport.c:261
DWORD KmtFltSendUlongToDriver(_In_ HANDLE hPort, _In_ DWORD Message, _In_ DWORD Value)
Definition: fltsupport.c:353
DWORD EnablePrivilegeInCurrentProcess(_In_z_ LPWSTR lpPrivName, _In_ BOOL bEnable)
Definition: fltsupport.c:564
struct _KMTFLT_MESSAGE_HEADER * PKMTFLT_MESSAGE_HEADER
DWORD KmtFltDisconnectComms(_In_ HANDLE hPort)
Definition: fltsupport.c:224
DWORD KmtFltSendBufferToDriver(_In_ HANDLE hPort, _In_ DWORD Message, _In_reads_bytes_(BufferSize) LPVOID InBuffer, _In_ DWORD BufferSize, _Out_writes_bytes_to_opt_(OutBufferSize, *BytesReturned) LPVOID OutBuffer, _In_ DWORD OutBufferSize, _Out_opt_ LPDWORD BytesReturned)
Definition: fltsupport.c:384
DWORD KmtFltSendStringToDriver(_In_ HANDLE hPort, _In_ DWORD Message, _In_ PCSTR String)
Definition: fltsupport.c:305
DWORD KmtpCreateService(IN PCWSTR ServiceName, IN PCWSTR ServicePath, IN PCWSTR DisplayName OPTIONAL, IN DWORD ServiceType, OUT SC_HANDLE *ServiceHandle)
Definition: service.c:414
DWORD KmtFltAddAltitude(_In_z_ LPWSTR Altitude)
Definition: fltsupport.c:448
DWORD KmtFltUnloadDriver(_In_ HANDLE *hPort, _In_ BOOLEAN DisonnectComms)
Definition: fltsupport.c:169
HANDLE KmtestHandle
Definition: kmtest.c:32
DWORD KmtFltSendWStringToDriver(_In_ HANDLE hPort, _In_ DWORD Message, _In_ PCWSTR String)
Definition: fltsupport.c:330
DWORD KmtFltConnectComms(_Out_ HANDLE *hPort)
Definition: fltsupport.c:207
DWORD KmtFltLoadDriver(_In_ BOOLEAN EnableDriverLoadPrivlege, _In_ BOOLEAN RestartIfRunning, _In_ BOOLEAN ConnectComms, _Out_ HANDLE *hPort)
Definition: fltsupport.c:112
DWORD KmtFltDeleteService(_In_opt_z_ PCWSTR ServiceName, _Inout_ SC_HANDLE *ServiceHandle)
Definition: fltsupport.c:97
_Must_inspect_result_ _In_ PFSRTL_PER_STREAM_CONTEXT Ptr
Definition: fsrtlfuncs.h:898
FxAutoRegKey hKey
_CRTIMP size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
void __cdecl __debugbreak(void)
Definition: intrin_ppc.h:698
#define KMTFLT_RUN_TEST
Definition: kmt_public.h:30
DWORD KmtCloseService(IN OUT SC_HANDLE *ServiceHandle)
Definition: service.c:392
DWORD KmtDeleteService(IN PCWSTR ServiceName OPTIONAL, IN OUT SC_HANDLE *ServiceHandle)
Definition: service.c:356
#define REG_SZ
Definition: layer.c:22
DWORD KmtFltDisconnect(_In_ HANDLE hPort)
Definition: filter.c:151
DWORD KmtFltLoad(_In_z_ PCWSTR ServiceName)
Definition: filter.c:30
DWORD KmtFltSendMessage(_In_ HANDLE hPort, _In_reads_bytes_(dwInBufferSize) LPVOID InBuffer, _In_ DWORD InBufferSize, _Out_writes_bytes_to_opt_(dutBufferSize, *BytesReturned) LPVOID OutBuffer, _In_ DWORD OutBufferSize, _Out_opt_ LPDWORD BytesReturned)
Definition: filter.c:187
DWORD KmtFltUnload(_In_z_ PCWSTR ServiceName)
Definition: filter.c:336
DWORD KmtFltConnect(_In_z_ PCWSTR ServiceName, _Out_ HANDLE *hPort)
Definition: filter.c:119
#define _Out_opt_
Definition: ms_sal.h:346
#define _In_reads_bytes_(size)
Definition: ms_sal.h:321
#define _Inout_
Definition: ms_sal.h:378
#define _In_z_
Definition: ms_sal.h:313
#define _In_opt_z_
Definition: ms_sal.h:314
#define _Out_
Definition: ms_sal.h:345
#define _Out_writes_bytes_to_opt_(size, count)
Definition: ms_sal.h:361
#define _In_
Definition: ms_sal.h:308
#define KEY_CREATE_SUB_KEY
Definition: nt_native.h:1018
#define REG_OPTION_NON_VOLATILE
Definition: nt_native.h:1057
#define KEY_SET_VALUE
Definition: nt_native.h:1017
#define L(x)
Definition: ntvdm.h:50
long LONG
Definition: pedump.c:60
#define REG_DWORD
Definition: sdbapi.c:596
_In_ UCHAR _In_ ULONG _Out_ PUCHAR _Outptr_result_bytebuffer_ OutBufferLength PVOID * OutBuffer
Definition: scsi.h:4071
PULONG MinorVersion OPTIONAL
Definition: CrossNt.h:68
#define StringCbCopy
Definition: strsafe.h:155
STRSAFEAPI StringCbCopyW(STRSAFE_LPWSTR pszDest, size_t cbDest, STRSAFE_LPCWSTR pszSrc)
Definition: strsafe.h:166
STRSAFEAPI StringCbCatW(STRSAFE_LPWSTR pszDest, size_t cbDest, STRSAFE_LPCWSTR pszSrc)
Definition: strsafe.h:342
const uint16_t * PCWSTR
Definition: typedefs.h:57
unsigned char * LPBYTE
Definition: typedefs.h:53
uint32_t * LPDWORD
Definition: typedefs.h:59
const char * PCSTR
Definition: typedefs.h:52
#define IN
Definition: typedefs.h:39
uint32_t ULONG
Definition: typedefs.h:59
#define OUT
Definition: typedefs.h:40
_Must_inspect_result_ _In_ WDFDEVICE _In_ WDFSTRING String
Definition: wdfdevice.h:2433
_Must_inspect_result_ _In_ WDFIOTARGET _In_opt_ WDFREQUEST _In_ ULONG _In_opt_ PWDF_MEMORY_DESCRIPTOR _In_opt_ PWDF_MEMORY_DESCRIPTOR _In_opt_ PWDF_REQUEST_SEND_OPTIONS _Out_opt_ PULONG_PTR BytesReturned
Definition: wdfiotarget.h:1052
_In_ WDFMEMORY _Out_opt_ size_t * BufferSize
Definition: wdfmemory.h:254
_Must_inspect_result_ _In_ WDFKEY _In_ PCUNICODE_STRING _Out_opt_ PUSHORT _Inout_opt_ PUNICODE_STRING Value
Definition: wdfregistry.h:413
_In_ ULONG OutBufferSize
Definition: wdfwmi.h:87
_In_ ULONG InBufferSize
Definition: wdfwmi.h:106
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
_In_ BOOL bEnable
Definition: winddi.h:3426
#define ERROR_SERVICE_ALREADY_RUNNING
Definition: winerror.h:607
#define SE_LOAD_DRIVER_NAME
Definition: winnt_old.h:375
#define HKEY_LOCAL_MACHINE
Definition: winreg.h:12
#define RegCreateKeyEx
Definition: winreg.h:501
#define SERVICE_FILE_SYSTEM_DRIVER
Definition: cmtypes.h:954
#define TOKEN_ADJUST_PRIVILEGES
Definition: setypes.h:930
#define TOKEN_QUERY
Definition: setypes.h:928
@ TokenPrivileges
Definition: setypes.h:968
#define SE_PRIVILEGE_ENABLED
Definition: setypes.h:63
__wchar_t WCHAR
Definition: xmlstorage.h:180
WCHAR * LPWSTR
Definition: xmlstorage.h:184