[FLTMGR] Latest from my branch (#135)
[reactos.git] / modules / rostests / kmtests / kmtest / filter.c
1 /*
2 * PROJECT: ReactOS kernel-mode tests
3 * LICENSE: GPLv2+ - See COPYING in the top level directory
4 * PURPOSE: File system filter implementation of the original service.c file
5 * PROGRAMMER: Thomas Faber <thomas.faber@reactos.org>
6 * Ged Murphy <gedmurphy@reactos.org>
7 */
8
9 //#include <fltuser.h>
10 #include <kmt_test.h>
11 #include "kmtest.h"
12
13 #include <assert.h>
14
15 #define SERVICE_ACCESS (SERVICE_START | SERVICE_STOP | DELETE)
16
17
18
19 /**
20 * @name KmtFltLoad
21 *
22 * Start the specified filter driver by name
23 *
24 * @param ServiceName
25 * The name of the filter to start
26 *
27 * @return Win32 error code
28 */
29 DWORD
30 KmtFltLoad(
31 _In_z_ PCWSTR ServiceName)
32 {
33 HRESULT hResult;
34 DWORD Error = ERROR_SUCCESS;
35
36 assert(ServiceName);
37
38 hResult = FilterLoad(ServiceName);
39 Error = SCODE_CODE(hResult);
40
41 return Error;
42 }
43 #if 0
44 /**
45 * @name KmtFltCreateAndStartService
46 *
47 * Create and load the specified filter driver and return a handle to it
48 *
49 * @param ServiceName
50 * Name of the service to create
51 * @param ServicePath
52 * File name of the driver, relative to the current directory
53 * @param DisplayName
54 * Service display name
55 * @param ServiceHandle
56 * Pointer to a variable to receive the handle to the service
57 * @param RestartIfRunning
58 * TRUE to stop and restart the service if it is already running
59 *
60 * @return Win32 error code
61 */
62 DWORD
63 KmtFltCreateAndStartService(
64 _In_z_ PCWSTR ServiceName,
65 _In_z_ PCWSTR ServicePath,
66 _In_z_ PCWSTR DisplayName OPTIONAL,
67 _Out_ SC_HANDLE *ServiceHandle,
68 _In_ BOOLEAN RestartIfRunning)
69 {
70 DWORD Error = ERROR_SUCCESS;
71
72 assert(ServiceHandle);
73
74 Error = KmtFltCreateService(ServiceName, ServicePath, DisplayName, ServiceHandle);
75
76 if (Error == ERROR_SERVICE_EXISTS)
77 *ServiceHandle = OpenService(ScmHandle, ServiceName, SERVICE_ACCESS);
78
79 if (Error && Error != ERROR_SERVICE_EXISTS)
80 goto cleanup;
81
82 Error = KmtFltLoad(ServiceName);
83
84 if (Error != ERROR_SERVICE_ALREADY_RUNNING)
85 goto cleanup;
86
87 Error = ERROR_SUCCESS;
88
89 if (!RestartIfRunning)
90 goto cleanup;
91
92 Error = KmtFltUnload(ServiceName);
93 if (Error)
94 goto cleanup;
95
96 Error = KmtFltLoad(ServiceName);
97 if (Error)
98 goto cleanup;
99
100 cleanup:
101 assert(Error);
102 return Error;
103 }
104 #endif
105
106 /**
107 * @name KmtFltConnect
108 *
109 * Create a comms connection to the specified filter
110 *
111 * @param ServiceName
112 * Name of the filter to connect to
113 * @param hPort
114 * Handle to the filter's comms port
115 *
116 * @return Win32 error code
117 */
118 DWORD
119 KmtFltConnect(
120 _In_z_ PCWSTR ServiceName,
121 _Out_ HANDLE *hPort)
122 {
123 HRESULT hResult;
124 DWORD Error;
125
126 assert(ServiceName);
127 assert(hPort);
128
129 hResult = FilterConnectCommunicationPort(ServiceName,
130 0,
131 NULL,
132 0,
133 NULL,
134 hPort);
135 Error = SCODE_CODE(hResult);
136
137 return Error;
138 }
139
140 /**
141 * @name KmtFltDisconnect
142 *
143 * Disconenct from the comms port
144 *
145 * @param hPort
146 * Handle to the filter's comms port
147 *
148 * @return Win32 error code
149 */
150 DWORD
151 KmtFltDisconnect(
152 _In_ HANDLE hPort)
153 {
154 DWORD Error = ERROR_SUCCESS;
155
156 assert(hPort);
157
158 if (!CloseHandle(hPort))
159 {
160 Error = GetLastError();
161 }
162
163 return Error;
164 }
165
166 /**
167 * @name KmtFltSendMessage
168 *
169 * Sneds a message to a filter driver
170 *
171 * @param hPort
172 * Handle to the filter's comms port
173 * @InBuffer
174 * Pointer to a buffer to send to the filter
175 * @InBufferSize
176 * Size of the buffer pointed to by InBuffer
177 * @OutBuffer
178 * Pointer to a buffer to receive reply data from the filter
179 * @OutBufferSize
180 * Size of the buffer pointed to by OutBuffer
181 * @BytesReturned
182 * Number of bytes written in the reply buffer
183 *
184 * @return Win32 error code
185 */
186 DWORD
187 KmtFltSendMessage(
188 _In_ HANDLE hPort,
189 _In_reads_bytes_(dwInBufferSize) LPVOID InBuffer,
190 _In_ DWORD InBufferSize,
191 _Out_writes_bytes_to_opt_(dutBufferSize, *BytesReturned) LPVOID OutBuffer,
192 _In_ DWORD OutBufferSize,
193 _Out_opt_ LPDWORD BytesReturned)
194 {
195 DWORD BytesRet;
196 HRESULT hResult;
197 DWORD Error;
198
199 assert(hPort);
200 assert(InBuffer);
201 assert(InBufferSize);
202
203 if (BytesReturned) *BytesReturned = 0;
204
205 hResult = FilterSendMessage(hPort,
206 InBuffer,
207 InBufferSize,
208 OutBuffer,
209 OutBufferSize,
210 &BytesRet);
211
212 Error = SCODE_CODE(hResult);
213 if (Error == ERROR_SUCCESS)
214 {
215 if (BytesRet)
216 {
217 *BytesReturned = BytesRet;
218 }
219 }
220
221 return Error;
222 }
223
224 /**
225 * @name KmtFltGetMessage
226 *
227 * Gets a message from a filter driver
228 *
229 * @param hPort
230 * Handle to the filter's comms port
231 * @MessageBuffer
232 * Pointer to a buffer to receive the data from the filter
233 * @MessageBufferSize
234 * Size of the buffer pointed to by MessageBuffer
235 * @Overlapped
236 * Pointer to an overlapped structure
237 *
238 * @return Win32 error code
239 */
240 DWORD
241 KmtFltGetMessage(
242 _In_ HANDLE hPort,
243 _Out_writes_bytes_(MessageBufferSize) PFILTER_MESSAGE_HEADER MessageBuffer,
244 _In_ DWORD MessageBufferSize,
245 _In_opt_ LPOVERLAPPED Overlapped)
246 {
247 HRESULT hResult;
248 DWORD Error;
249
250 assert(hPort);
251 assert(MessageBuffer);
252
253 hResult = FilterGetMessage(hPort,
254 MessageBuffer,
255 MessageBufferSize,
256 Overlapped);
257 Error = SCODE_CODE(hResult);
258 return Error;
259 }
260
261 /**
262 * @name KmtFltReplyMessage
263 *
264 * Replies to a message from a filter driver
265 *
266 * @param hPort
267 * Handle to the filter's comms port
268 * @ReplyBuffer
269 * Pointer to a buffer to return to the filter
270 * @ReplyBufferSize
271 * Size of the buffer pointed to by ReplyBuffer
272 *
273 * @return Win32 error code
274 */
275 DWORD
276 KmtFltReplyMessage(
277 _In_ HANDLE hPort,
278 _In_reads_bytes_(ReplyBufferSize) PFILTER_REPLY_HEADER ReplyBuffer,
279 _In_ DWORD ReplyBufferSize)
280 {
281 HRESULT hResult;
282 DWORD Error;
283
284 hResult = FilterReplyMessage(hPort,
285 ReplyBuffer,
286 ReplyBufferSize);
287 Error = SCODE_CODE(hResult);
288 return Error;
289 }
290
291 /**
292 * @name KmtFltGetMessageResult
293 *
294 * Gets the overlapped result from the IO
295 *
296 * @param hPort
297 * Handle to the filter's comms port
298 * @Overlapped
299 * Pointer to the overlapped structure usdd in the IO
300 * @BytesTransferred
301 * Number of bytes transferred in the IO
302 *
303 * @return Win32 error code
304 */
305 DWORD
306 KmtFltGetMessageResult(
307 _In_ HANDLE hPort,
308 _In_ LPOVERLAPPED Overlapped,
309 _Out_ LPDWORD BytesTransferred)
310 {
311 BOOL Success;
312 DWORD Error = ERROR_SUCCESS;
313
314 *BytesTransferred = 0;
315
316 Success = GetOverlappedResult(hPort, Overlapped, BytesTransferred, TRUE);
317 if (!Success)
318 {
319 Error = GetLastError();
320 }
321
322 return Error;
323 }
324
325 /**
326 * @name KmtFltUnload
327 *
328 * Unload the specified filter driver
329 *
330 * @param ServiceName
331 * The name of the filter to unload
332 *
333 * @return Win32 error code
334 */
335 DWORD
336 KmtFltUnload(
337 _In_z_ PCWSTR ServiceName)
338 {
339 HRESULT hResult;
340 DWORD Error = ERROR_SUCCESS;
341
342 assert(ServiceName);
343
344 hResult = FilterUnload(ServiceName);
345 Error = SCODE_CODE(hResult);
346
347 return Error;
348 }