d1bd3d1eeac30e0b94a1abcba3e36fac40021bce
[reactos.git] / rostests / winetests / ntdll / pipe.c
1 /* Unit test suite for Ntdll NamedPipe API functions
2 *
3 * Copyright 2011 Bernhard Loos
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18 */
19
20 #include <stdio.h>
21 #include <stdarg.h>
22
23 #include "ntstatus.h"
24 #define WIN32_NO_STATUS
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "winreg.h"
29 #include "winnls.h"
30 #include "wine/test.h"
31 #include "wine/winternl.h"
32 #include "winioctl.h"
33
34 #ifndef __WINE_WINTERNL_H
35
36 typedef struct {
37 ULONG ReadMode;
38 ULONG CompletionMode;
39 } FILE_PIPE_INFORMATION;
40
41 typedef struct {
42 ULONG NamedPipeType;
43 ULONG NamedPipeConfiguration;
44 ULONG MaximumInstances;
45 ULONG CurrentInstances;
46 ULONG InboundQuota;
47 ULONG ReadDataAvailable;
48 ULONG OutboundQuota;
49 ULONG WriteQuotaAvailable;
50 ULONG NamedPipeState;
51 ULONG NamedPipeEnd;
52 } FILE_PIPE_LOCAL_INFORMATION;
53
54 #ifndef FILE_SYNCHRONOUS_IO_ALERT
55 #define FILE_SYNCHRONOUS_IO_ALERT 0x10
56 #endif
57
58 #ifndef FILE_SYNCHRONOUS_IO_NONALERT
59 #define FILE_SYNCHRONOUS_IO_NONALERT 0x20
60 #endif
61
62 #ifndef FSCTL_PIPE_LISTEN
63 #define FSCTL_PIPE_LISTEN CTL_CODE(FILE_DEVICE_NAMED_PIPE, 2, METHOD_BUFFERED, FILE_ANY_ACCESS)
64 #endif
65 #endif
66
67 static NTSTATUS (WINAPI *pNtFsControlFile) (HANDLE handle, HANDLE event, PIO_APC_ROUTINE apc, PVOID apc_context, PIO_STATUS_BLOCK io, ULONG code, PVOID in_buffer, ULONG in_size, PVOID out_buffer, ULONG out_size);
68 static NTSTATUS (WINAPI *pNtCreateNamedPipeFile) (PHANDLE handle, ULONG access,
69 POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK iosb,
70 ULONG sharing, ULONG dispo, ULONG options,
71 ULONG pipe_type, ULONG read_mode,
72 ULONG completion_mode, ULONG max_inst,
73 ULONG inbound_quota, ULONG outbound_quota,
74 PLARGE_INTEGER timeout);
75 static NTSTATUS (WINAPI *pNtQueryInformationFile) (IN HANDLE FileHandle, OUT PIO_STATUS_BLOCK IoStatusBlock, OUT PVOID FileInformation, IN ULONG Length, IN FILE_INFORMATION_CLASS FileInformationClass);
76 static NTSTATUS (WINAPI *pNtSetInformationFile) (HANDLE handle, PIO_STATUS_BLOCK io, PVOID ptr, ULONG len, FILE_INFORMATION_CLASS class);
77 static NTSTATUS (WINAPI *pNtCancelIoFile) (HANDLE hFile, PIO_STATUS_BLOCK io_status);
78 static void (WINAPI *pRtlInitUnicodeString) (PUNICODE_STRING target, PCWSTR source);
79
80 static HANDLE (WINAPI *pOpenThread)(DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwThreadId);
81 static DWORD (WINAPI *pQueueUserAPC)(PAPCFUNC pfnAPC, HANDLE hThread, ULONG_PTR dwData);
82
83
84 static BOOL init_func_ptrs(void)
85 {
86 HMODULE module = GetModuleHandleA("ntdll.dll");
87
88 #define loadfunc(name) if (!(p##name = (void *)GetProcAddress(module, #name))) { \
89 trace("GetProcAddress(%s) failed\n", #name); \
90 return FALSE; \
91 }
92
93 loadfunc(NtFsControlFile)
94 loadfunc(NtCreateNamedPipeFile)
95 loadfunc(NtQueryInformationFile)
96 loadfunc(NtSetInformationFile)
97 loadfunc(NtCancelIoFile)
98 loadfunc(RtlInitUnicodeString)
99
100 /* not fatal */
101 module = GetModuleHandleA("kernel32.dll");
102 pOpenThread = (void *)GetProcAddress(module, "OpenThread");
103 pQueueUserAPC = (void *)GetProcAddress(module, "QueueUserAPC");
104 return TRUE;
105 }
106
107 static const WCHAR testpipe[] = { '\\', '\\', '.', '\\', 'p', 'i', 'p', 'e', '\\',
108 't', 'e', 's', 't', 'p', 'i', 'p', 'e', 0 };
109 static const WCHAR testpipe_nt[] = { '\\', '?', '?', '\\', 'p', 'i', 'p', 'e', '\\',
110 't', 'e', 's', 't', 'p', 'i', 'p', 'e', 0 };
111
112 static NTSTATUS create_pipe(PHANDLE handle, ULONG sharing, ULONG options)
113 {
114 IO_STATUS_BLOCK iosb;
115 OBJECT_ATTRIBUTES attr;
116 UNICODE_STRING name;
117 LARGE_INTEGER timeout;
118 NTSTATUS res;
119
120 pRtlInitUnicodeString(&name, testpipe_nt);
121
122 attr.Length = sizeof(attr);
123 attr.RootDirectory = 0;
124 attr.ObjectName = &name;
125 attr.Attributes = 0x40; /*case insensitive */
126 attr.SecurityDescriptor = NULL;
127 attr.SecurityQualityOfService = NULL;
128
129 timeout.QuadPart = -100000000000ll;
130
131 res = pNtCreateNamedPipeFile(handle, FILE_READ_ATTRIBUTES | SYNCHRONIZE, &attr, &iosb, sharing, 2 /*FILE_CREATE*/,
132 options, 1, 0, 0, 0xFFFFFFFF, 500, 500, &timeout);
133 return res;
134 }
135
136 static void test_create_invalid(void)
137 {
138 IO_STATUS_BLOCK iosb;
139 OBJECT_ATTRIBUTES attr;
140 UNICODE_STRING name;
141 LARGE_INTEGER timeout;
142 NTSTATUS res;
143 HANDLE handle, handle2;
144 FILE_PIPE_LOCAL_INFORMATION info;
145
146 pRtlInitUnicodeString(&name, testpipe_nt);
147
148 attr.Length = sizeof(attr);
149 attr.RootDirectory = 0;
150 attr.ObjectName = &name;
151 attr.Attributes = 0x40; /*case insensitive */
152 attr.SecurityDescriptor = NULL;
153 attr.SecurityQualityOfService = NULL;
154
155 timeout.QuadPart = -100000000000ll;
156
157 /* create a pipe with FILE_OVERWRITE */
158 res = pNtCreateNamedPipeFile(&handle, FILE_READ_ATTRIBUTES | SYNCHRONIZE, &attr, &iosb, FILE_SHARE_READ, 4 /*FILE_OVERWRITE*/,
159 0, 1, 0, 0, 0xFFFFFFFF, 500, 500, &timeout);
160 todo_wine ok(res == STATUS_INVALID_PARAMETER, "NtCreateNamedPipeFile returned %x\n", res);
161 if (!res)
162 CloseHandle(handle);
163
164 /* create a pipe with FILE_OVERWRITE_IF */
165 res = pNtCreateNamedPipeFile(&handle, FILE_READ_ATTRIBUTES | SYNCHRONIZE, &attr, &iosb, FILE_SHARE_READ, 5 /*FILE_OVERWRITE_IF*/,
166 0, 1, 0, 0, 0xFFFFFFFF, 500, 500, &timeout);
167 todo_wine ok(res == STATUS_INVALID_PARAMETER, "NtCreateNamedPipeFile returned %x\n", res);
168 if (!res)
169 CloseHandle(handle);
170
171 /* create a pipe with sharing = 0 */
172 res = pNtCreateNamedPipeFile(&handle, FILE_READ_ATTRIBUTES | SYNCHRONIZE, &attr, &iosb, 0, 2 /*FILE_CREATE*/,
173 0, 1, 0, 0, 0xFFFFFFFF, 500, 500, &timeout);
174 ok(res == STATUS_INVALID_PARAMETER, "NtCreateNamedPipeFile returned %x\n", res);
175 if (!res)
176 CloseHandle(handle);
177
178 /* create a pipe without r/w access */
179 res = pNtCreateNamedPipeFile(&handle, SYNCHRONIZE, &attr, &iosb, FILE_SHARE_READ | FILE_SHARE_WRITE, 2 /*FILE_CREATE*/,
180 0, 1, 0, 0, 0xFFFFFFFF, 500, 500, &timeout);
181 ok(!res, "NtCreateNamedPipeFile returned %x\n", res);
182
183 res = pNtQueryInformationFile(handle, &iosb, &info, sizeof(info), (FILE_INFORMATION_CLASS)24);
184 ok(res == STATUS_ACCESS_DENIED, "NtQueryInformationFile returned %x\n", res);
185
186 /* test FILE_CREATE creation disposition */
187 res = pNtCreateNamedPipeFile(&handle2, SYNCHRONIZE, &attr, &iosb, FILE_SHARE_READ | FILE_SHARE_WRITE, 2 /*FILE_CREATE*/,
188 0, 1, 0, 0, 0xFFFFFFFF, 500, 500, &timeout);
189 todo_wine ok(res == STATUS_ACCESS_DENIED, "NtCreateNamedPipeFile returned %x\n", res);
190 if (!res)
191 CloseHandle(handle2);
192
193 CloseHandle(handle);
194 }
195
196 static void test_create(void)
197 {
198 HANDLE hserver;
199 NTSTATUS res;
200 int j, k;
201 FILE_PIPE_LOCAL_INFORMATION info;
202 IO_STATUS_BLOCK iosb;
203
204 static const DWORD access[] = { 0, GENERIC_READ, GENERIC_WRITE, GENERIC_READ | GENERIC_WRITE};
205 static const DWORD sharing[] = { FILE_SHARE_READ, FILE_SHARE_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE };
206 static const DWORD pipe_config[]= { 1, 0, 2 };
207
208 for (j = 0; j < sizeof(sharing) / sizeof(DWORD); j++) {
209 for (k = 0; k < sizeof(access) / sizeof(DWORD); k++) {
210 HANDLE hclient;
211 BOOL should_succeed = TRUE;
212
213 res = create_pipe(&hserver, sharing[j], FILE_SYNCHRONOUS_IO_NONALERT);
214 if (res) {
215 ok(0, "NtCreateNamedPipeFile returned %x, sharing: %x\n", res, sharing[j]);
216 continue;
217 }
218
219 res = pNtQueryInformationFile(hserver, &iosb, &info, sizeof(info), (FILE_INFORMATION_CLASS)24);
220 ok(!res, "NtQueryInformationFile for server returned %x, sharing: %x\n", res, sharing[j]);
221 ok(info.NamedPipeConfiguration == pipe_config[j], "wrong duplex status for pipe: %d, expected %d\n",
222 info.NamedPipeConfiguration, pipe_config[j]);
223
224 hclient = CreateFileW(testpipe, access[k], 0, 0, OPEN_EXISTING, 0, 0);
225 if (hclient != INVALID_HANDLE_VALUE) {
226 res = pNtQueryInformationFile(hclient, &iosb, &info, sizeof(info), (FILE_INFORMATION_CLASS)24);
227 ok(!res, "NtQueryInformationFile for client returned %x, access: %x, sharing: %x\n",
228 res, access[k], sharing[j]);
229 ok(info.NamedPipeConfiguration == pipe_config[j], "wrong duplex status for pipe: %d, expected %d\n",
230 info.NamedPipeConfiguration, pipe_config[j]);
231 CloseHandle(hclient);
232 }
233
234 if (access[k] & GENERIC_WRITE)
235 should_succeed &= !!(sharing[j] & FILE_SHARE_WRITE);
236 if (access[k] & GENERIC_READ)
237 should_succeed &= !!(sharing[j] & FILE_SHARE_READ);
238
239 if (should_succeed)
240 ok(hclient != INVALID_HANDLE_VALUE, "CreateFile failed for sharing %x, access: %x, GetLastError: %d\n",
241 sharing[j], access[k], GetLastError());
242 else
243 ok(hclient == INVALID_HANDLE_VALUE, "CreateFile succeeded for sharing %x, access: %x\n", sharing[j], access[k]);
244
245 CloseHandle(hserver);
246 }
247 }
248 }
249
250 static BOOL ioapc_called;
251 static void CALLBACK ioapc(void *arg, PIO_STATUS_BLOCK io, ULONG reserved)
252 {
253 ioapc_called = TRUE;
254 }
255
256 static NTSTATUS listen_pipe(HANDLE hPipe, HANDLE hEvent, PIO_STATUS_BLOCK iosb, BOOL use_apc)
257 {
258 int dummy;
259
260 ioapc_called = FALSE;
261
262 return pNtFsControlFile(hPipe, hEvent, use_apc ? &ioapc: NULL, use_apc ? &dummy: NULL, iosb, FSCTL_PIPE_LISTEN, 0, 0, 0, 0);
263 }
264
265 static void test_overlapped(void)
266 {
267 IO_STATUS_BLOCK iosb;
268 HANDLE hEvent;
269 HANDLE hPipe;
270 HANDLE hClient;
271 NTSTATUS res;
272
273 hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
274 ok(hEvent != INVALID_HANDLE_VALUE, "can't create event, GetLastError: %x\n", GetLastError());
275
276 res = create_pipe(&hPipe, FILE_SHARE_READ | FILE_SHARE_WRITE, 0 /* OVERLAPPED */);
277 ok(!res, "NtCreateNamedPipeFile returned %x\n", res);
278
279 memset(&iosb, 0x55, sizeof(iosb));
280
281 /* try with event and apc */
282 res = listen_pipe(hPipe, hEvent, &iosb, TRUE);
283 ok(res == STATUS_PENDING, "NtFsControlFile returned %x\n", res);
284
285 hClient = CreateFileW(testpipe, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
286 ok(hClient != INVALID_HANDLE_VALUE, "can't open pipe, GetLastError: %x\n", GetLastError());
287
288 ok(U(iosb).Status == 0, "Wrong iostatus %x\n", U(iosb).Status);
289 ok(WaitForSingleObject(hEvent, 0) == 0, "hEvent not signaled\n");
290
291 ok(!ioapc_called, "IOAPC ran too early\n");
292
293 SleepEx(0, TRUE); /* alertable wait state */
294
295 ok(ioapc_called, "IOAPC didn't run\n");
296
297 CloseHandle(hEvent);
298 CloseHandle(hPipe);
299 CloseHandle(hClient);
300 }
301
302 static BOOL userapc_called;
303 static void CALLBACK userapc(ULONG_PTR dwParam)
304 {
305 userapc_called = TRUE;
306 }
307
308 static BOOL open_succeeded;
309 static DWORD WINAPI thread(PVOID main_thread)
310 {
311 HANDLE h;
312
313 Sleep(400);
314
315 if (main_thread) {
316 DWORD ret;
317 userapc_called = FALSE;
318 ret = pQueueUserAPC(&userapc, main_thread, 0);
319 ok(ret, "can't queue user apc, GetLastError: %x\n", GetLastError());
320 CloseHandle(main_thread);
321 }
322
323 Sleep(400);
324
325 h = CreateFileW(testpipe, GENERIC_WRITE | GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0);
326
327 if (h != INVALID_HANDLE_VALUE) {
328 open_succeeded = TRUE;
329 Sleep(100);
330 CloseHandle(h);
331 } else
332 open_succeeded = FALSE;
333
334 return 0;
335 }
336
337 static void test_alertable(void)
338 {
339 IO_STATUS_BLOCK iosb;
340 HANDLE hEvent;
341 HANDLE hPipe;
342 NTSTATUS res;
343 HANDLE hThread;
344 DWORD ret;
345
346 memset(&iosb, 0x55, sizeof(iosb));
347
348 hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
349 ok(hEvent != INVALID_HANDLE_VALUE, "can't create event, GetLastError: %x\n", GetLastError());
350
351 res = create_pipe(&hPipe, FILE_SHARE_READ | FILE_SHARE_WRITE, FILE_SYNCHRONOUS_IO_ALERT);
352 ok(!res, "NtCreateNamedPipeFile returned %x\n", res);
353
354 /* queue an user apc before calling listen */
355 userapc_called = FALSE;
356 ret = pQueueUserAPC(&userapc, GetCurrentThread(), 0);
357 ok(ret, "can't queue user apc, GetLastError: %x\n", GetLastError());
358
359 res = listen_pipe(hPipe, hEvent, &iosb, TRUE);
360 todo_wine ok(res == STATUS_CANCELLED, "NtFsControlFile returned %x\n", res);
361
362 todo_wine ok(userapc_called, "user apc didn't run\n");
363 ok(U(iosb).Status == 0x55555555, "iosb.Status got changed to %x\n", U(iosb).Status);
364 todo_wine ok(WaitForSingleObjectEx(hEvent, 0, TRUE) == WAIT_TIMEOUT, "hEvent signaled\n");
365 ok(!ioapc_called, "IOAPC ran\n");
366
367 /* queue an user apc from a different thread */
368 hThread = CreateThread(NULL, 0, &thread, pOpenThread(MAXIMUM_ALLOWED, FALSE, GetCurrentThreadId()), 0, 0);
369 ok(hThread != INVALID_HANDLE_VALUE, "can't create thread, GetLastError: %x\n", GetLastError());
370
371 /* wine_todo: the earlier NtFsControlFile call gets cancelled after the pipe gets set into listen state
372 instead of before, so this NtFsControlFile will fail STATUS_INVALID_HANDLE */
373 res = listen_pipe(hPipe, hEvent, &iosb, TRUE);
374 todo_wine ok(res == STATUS_CANCELLED, "NtFsControlFile returned %x\n", res);
375
376 ok(userapc_called, "user apc didn't run\n");
377 todo_wine ok(U(iosb).Status == 0x55555555, "iosb.Status got changed to %x\n", U(iosb).Status);
378 ok(WaitForSingleObjectEx(hEvent, 0, TRUE) == WAIT_TIMEOUT, "hEvent signaled\n");
379 ok(!ioapc_called, "IOAPC ran\n");
380
381 WaitForSingleObject(hThread, INFINITE);
382
383 SleepEx(0, TRUE); /* get rid of the userapc, if NtFsControlFile failed */
384
385 ok(open_succeeded, "couldn't open client side pipe\n");
386
387 CloseHandle(hThread);
388 DisconnectNamedPipe(hPipe);
389
390 /* finally try without an apc */
391 hThread = CreateThread(NULL, 0, &thread, 0, 0, 0);
392 ok(hThread != INVALID_HANDLE_VALUE, "can't create thread, GetLastError: %x\n", GetLastError());
393
394 res = listen_pipe(hPipe, hEvent, &iosb, TRUE);
395 todo_wine ok(!res, "NtFsControlFile returned %x\n", res);
396
397 ok(open_succeeded, "couldn't open client side pipe\n");
398 ok(!U(iosb).Status, "Wrong iostatus %x\n", U(iosb).Status);
399 todo_wine ok(WaitForSingleObject(hEvent, 0) == 0, "hEvent not signaled\n");
400
401 WaitForSingleObject(hThread, INFINITE);
402 CloseHandle(hThread);
403 CloseHandle(hEvent);
404 CloseHandle(hPipe);
405 }
406
407 static void test_nonalertable(void)
408 {
409 IO_STATUS_BLOCK iosb;
410 HANDLE hEvent;
411 HANDLE hPipe;
412 NTSTATUS res;
413 HANDLE hThread;
414 DWORD ret;
415
416 memset(&iosb, 0x55, sizeof(iosb));
417
418 hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
419 ok(hEvent != INVALID_HANDLE_VALUE, "can't create event, GetLastError: %x\n", GetLastError());
420
421 res = create_pipe(&hPipe, FILE_SHARE_READ | FILE_SHARE_WRITE, FILE_SYNCHRONOUS_IO_NONALERT);
422 ok(!res, "NtCreateNamedPipeFile returned %x\n", res);
423
424 hThread = CreateThread(NULL, 0, &thread, 0, 0, 0);
425 ok(hThread != INVALID_HANDLE_VALUE, "can't create thread, GetLastError: %x\n", GetLastError());
426
427 userapc_called = FALSE;
428 ret = pQueueUserAPC(&userapc, GetCurrentThread(), 0);
429 ok(ret, "can't queue user apc, GetLastError: %x\n", GetLastError());
430
431 res = listen_pipe(hPipe, hEvent, &iosb, TRUE);
432 todo_wine ok(!res, "NtFsControlFile returned %x\n", res);
433
434 ok(open_succeeded, "couldn't open client side pipe\n");
435 todo_wine ok(!U(iosb).Status, "Wrong iostatus %x\n", U(iosb).Status);
436 todo_wine ok(WaitForSingleObject(hEvent, 0) == 0, "hEvent not signaled\n");
437
438 ok(!ioapc_called, "IOAPC ran too early\n");
439 ok(!userapc_called, "user apc ran too early\n");
440
441 SleepEx(0, TRUE); /* alertable wait state */
442
443 ok(ioapc_called, "IOAPC didn't run\n");
444 ok(userapc_called, "user apc didn't run\n");
445
446 WaitForSingleObject(hThread, INFINITE);
447 CloseHandle(hThread);
448 CloseHandle(hEvent);
449 CloseHandle(hPipe);
450 }
451
452 static void test_cancelio(void)
453 {
454 IO_STATUS_BLOCK iosb;
455 IO_STATUS_BLOCK cancel_sb;
456 HANDLE hEvent;
457 HANDLE hPipe;
458 NTSTATUS res;
459
460 hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
461 ok(hEvent != INVALID_HANDLE_VALUE, "can't create event, GetLastError: %x\n", GetLastError());
462
463 res = create_pipe(&hPipe, FILE_SHARE_READ | FILE_SHARE_WRITE, 0 /* OVERLAPPED */);
464 ok(!res, "NtCreateNamedPipeFile returned %x\n", res);
465
466 memset(&iosb, 0x55, sizeof(iosb));
467
468 res = listen_pipe(hPipe, hEvent, &iosb, TRUE);
469 ok(res == STATUS_PENDING, "NtFsControlFile returned %x\n", res);
470
471 res = pNtCancelIoFile(hPipe, &cancel_sb);
472 ok(!res, "NtCancelIoFile returned %x\n", res);
473
474 ok(U(iosb).Status == STATUS_CANCELLED, "Wrong iostatus %x\n", U(iosb).Status);
475 ok(WaitForSingleObject(hEvent, 0) == 0, "hEvent not signaled\n");
476
477 ok(!ioapc_called, "IOAPC ran too early\n");
478
479 SleepEx(0, TRUE); /* alertable wait state */
480
481 ok(ioapc_called, "IOAPC didn't run\n");
482
483 CloseHandle(hEvent);
484 CloseHandle(hPipe);
485 }
486
487 static void _check_pipe_handle_state(int line, HANDLE handle, ULONG read, ULONG completion)
488 {
489 IO_STATUS_BLOCK iosb;
490 FILE_PIPE_INFORMATION fpi;
491 NTSTATUS res;
492 if (handle != INVALID_HANDLE_VALUE)
493 {
494 memset(&fpi, 0x55, sizeof(fpi));
495 res = pNtQueryInformationFile(handle, &iosb, &fpi, sizeof(fpi), (FILE_INFORMATION_CLASS)23);
496 ok_(__FILE__, line)(!res, "NtQueryInformationFile returned %x\n", res);
497 ok_(__FILE__, line)(fpi.ReadMode == read, "Unexpected ReadMode, expected %x, got %x\n",
498 read, fpi.ReadMode);
499 ok_(__FILE__, line)(fpi.CompletionMode == completion, "Unexpected CompletionMode, expected %x, got %x\n",
500 completion, fpi.CompletionMode);
501 }
502 }
503 #define check_pipe_handle_state(handle, r, c) _check_pipe_handle_state(__LINE__, handle, r, c)
504
505 static void test_filepipeinfo(void)
506 {
507 IO_STATUS_BLOCK iosb;
508 OBJECT_ATTRIBUTES attr;
509 UNICODE_STRING name;
510 LARGE_INTEGER timeout;
511 HANDLE hServer, hClient;
512 FILE_PIPE_INFORMATION fpi;
513 NTSTATUS res;
514
515 pRtlInitUnicodeString(&name, testpipe_nt);
516
517 attr.Length = sizeof(attr);
518 attr.RootDirectory = 0;
519 attr.ObjectName = &name;
520 attr.Attributes = 0x40; /* case insensitive */
521 attr.SecurityDescriptor = NULL;
522 attr.SecurityQualityOfService = NULL;
523
524 timeout.QuadPart = -100000000000ll;
525
526 /* test with INVALID_HANDLE_VALUE */
527 res = pNtQueryInformationFile(INVALID_HANDLE_VALUE, &iosb, &fpi, sizeof(fpi), (FILE_INFORMATION_CLASS)23);
528 ok(res == STATUS_OBJECT_TYPE_MISMATCH, "NtQueryInformationFile returned %x\n", res);
529
530 fpi.ReadMode = 0;
531 fpi.CompletionMode = 0;
532 res = pNtSetInformationFile(INVALID_HANDLE_VALUE, &iosb, &fpi, sizeof(fpi), (FILE_INFORMATION_CLASS)23);
533 ok(res == STATUS_OBJECT_TYPE_MISMATCH, "NtSetInformationFile returned %x\n", res);
534
535 /* server end with read-only attributes */
536 res = pNtCreateNamedPipeFile(&hServer, FILE_READ_ATTRIBUTES | SYNCHRONIZE, &attr, &iosb,
537 FILE_SHARE_READ | FILE_SHARE_WRITE, 2 /* FILE_CREATE */,
538 0, 0, 0, 1, 0xFFFFFFFF, 500, 500, &timeout);
539 ok(!res, "NtCreateNamedPipeFile returned %x\n", res);
540
541 check_pipe_handle_state(hServer, 0, 1);
542
543 hClient = CreateFileW(testpipe, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
544 ok(hClient != INVALID_HANDLE_VALUE, "can't open pipe, GetLastError: %x\n", GetLastError());
545
546 check_pipe_handle_state(hServer, 0, 1);
547 check_pipe_handle_state(hClient, 0, 0);
548
549 fpi.ReadMode = 0;
550 fpi.CompletionMode = 0;
551 res = pNtSetInformationFile(hServer, &iosb, &fpi, sizeof(fpi), (FILE_INFORMATION_CLASS)23);
552 ok(res == STATUS_ACCESS_DENIED, "NtSetInformationFile returned %x\n", res);
553
554 check_pipe_handle_state(hServer, 0, 1);
555 check_pipe_handle_state(hClient, 0, 0);
556
557 fpi.ReadMode = 1; /* invalid on a byte stream pipe */
558 fpi.CompletionMode = 1;
559 res = pNtSetInformationFile(hServer, &iosb, &fpi, sizeof(fpi), (FILE_INFORMATION_CLASS)23);
560 ok(res == STATUS_ACCESS_DENIED, "NtSetInformationFile returned %x\n", res);
561
562 check_pipe_handle_state(hServer, 0, 1);
563 check_pipe_handle_state(hClient, 0, 0);
564
565 if (hClient != INVALID_HANDLE_VALUE)
566 {
567 fpi.ReadMode = 1; /* invalid on a byte stream pipe */
568 fpi.CompletionMode = 1;
569 res = pNtSetInformationFile(hClient, &iosb, &fpi, sizeof(fpi), (FILE_INFORMATION_CLASS)23);
570 ok(res == STATUS_INVALID_PARAMETER, "NtSetInformationFile returned %x\n", res);
571 }
572
573 check_pipe_handle_state(hServer, 0, 1);
574 check_pipe_handle_state(hClient, 0, 0);
575
576 if (hClient != INVALID_HANDLE_VALUE)
577 {
578 fpi.ReadMode = 0;
579 fpi.CompletionMode = 1;
580 res = pNtSetInformationFile(hClient, &iosb, &fpi, sizeof(fpi), (FILE_INFORMATION_CLASS)23);
581 ok(!res, "NtSetInformationFile returned %x\n", res);
582 }
583
584 check_pipe_handle_state(hServer, 0, 1);
585 check_pipe_handle_state(hClient, 0, 1);
586
587 if (hClient != INVALID_HANDLE_VALUE)
588 {
589 fpi.ReadMode = 0;
590 fpi.CompletionMode = 2; /* not in range 0-1 */
591 res = pNtSetInformationFile(hClient, &iosb, &fpi, sizeof(fpi), (FILE_INFORMATION_CLASS)23);
592 ok(res == STATUS_INVALID_PARAMETER || broken(!res) /* < Vista */, "NtSetInformationFile returned %x\n", res);
593
594 fpi.ReadMode = 2; /* not in range 0-1 */
595 fpi.CompletionMode = 0;
596 res = pNtSetInformationFile(hClient, &iosb, &fpi, sizeof(fpi), (FILE_INFORMATION_CLASS)23);
597 ok(res == STATUS_INVALID_PARAMETER || broken(!res) /* < Vista */, "NtSetInformationFile returned %x\n", res);
598 }
599
600 CloseHandle(hClient);
601
602 check_pipe_handle_state(hServer, 0, 1);
603
604 fpi.ReadMode = 0;
605 fpi.CompletionMode = 0;
606 res = pNtSetInformationFile(hServer, &iosb, &fpi, sizeof(fpi), (FILE_INFORMATION_CLASS)23);
607 ok(res == STATUS_ACCESS_DENIED, "NtSetInformationFile returned %x\n", res);
608
609 CloseHandle(hServer);
610
611 /* message mode server with read/write attributes */
612 res = pNtCreateNamedPipeFile(&hServer, FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES | SYNCHRONIZE, &attr, &iosb,
613 FILE_SHARE_READ | FILE_SHARE_WRITE, 2 /* FILE_CREATE */,
614 0, 1, 1, 0, 0xFFFFFFFF, 500, 500, &timeout);
615 ok(!res, "NtCreateNamedPipeFile returned %x\n", res);
616
617 check_pipe_handle_state(hServer, 1, 0);
618
619 hClient = CreateFileW(testpipe, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
620 ok(hClient != INVALID_HANDLE_VALUE, "can't open pipe, GetLastError: %x\n", GetLastError());
621
622 check_pipe_handle_state(hServer, 1, 0);
623 check_pipe_handle_state(hClient, 0, 0);
624
625 if (hClient != INVALID_HANDLE_VALUE)
626 {
627 fpi.ReadMode = 1;
628 fpi.CompletionMode = 1;
629 res = pNtSetInformationFile(hClient, &iosb, &fpi, sizeof(fpi), (FILE_INFORMATION_CLASS)23);
630 ok(!res, "NtSetInformationFile returned %x\n", res);
631 }
632
633 check_pipe_handle_state(hServer, 1, 0);
634 check_pipe_handle_state(hClient, 1, 1);
635
636 fpi.ReadMode = 0;
637 fpi.CompletionMode = 1;
638 res = pNtSetInformationFile(hServer, &iosb, &fpi, sizeof(fpi), (FILE_INFORMATION_CLASS)23);
639 ok(!res, "NtSetInformationFile returned %x\n", res);
640
641 check_pipe_handle_state(hServer, 0, 1);
642 check_pipe_handle_state(hClient, 1, 1);
643
644 if (hClient != INVALID_HANDLE_VALUE)
645 {
646 fpi.ReadMode = 0;
647 fpi.CompletionMode = 2; /* not in range 0-1 */
648 res = pNtSetInformationFile(hClient, &iosb, &fpi, sizeof(fpi), (FILE_INFORMATION_CLASS)23);
649 ok(res == STATUS_INVALID_PARAMETER || broken(!res) /* < Vista */, "NtSetInformationFile returned %x\n", res);
650
651 fpi.ReadMode = 2; /* not in range 0-1 */
652 fpi.CompletionMode = 0;
653 res = pNtSetInformationFile(hClient, &iosb, &fpi, sizeof(fpi), (FILE_INFORMATION_CLASS)23);
654 ok(res == STATUS_INVALID_PARAMETER || broken(!res) /* < Vista */, "NtSetInformationFile returned %x\n", res);
655 }
656
657 CloseHandle(hClient);
658
659 check_pipe_handle_state(hServer, 0, 1);
660
661 fpi.ReadMode = 1;
662 fpi.CompletionMode = 0;
663 res = pNtSetInformationFile(hServer, &iosb, &fpi, sizeof(fpi), (FILE_INFORMATION_CLASS)23);
664 ok(!res, "NtSetInformationFile returned %x\n", res);
665
666 check_pipe_handle_state(hServer, 1, 0);
667
668 CloseHandle(hServer);
669 }
670
671 START_TEST(pipe)
672 {
673 if (!init_func_ptrs())
674 return;
675
676 trace("starting invalid create tests\n");
677 test_create_invalid();
678
679 trace("starting create tests\n");
680 test_create();
681
682 trace("starting overlapped tests\n");
683 test_overlapped();
684
685 trace("starting FILE_PIPE_INFORMATION tests\n");
686 test_filepipeinfo();
687
688 if (!pOpenThread || !pQueueUserAPC)
689 return;
690
691 trace("starting alertable tests\n");
692 test_alertable();
693
694 trace("starting nonalertable tests\n");
695 test_nonalertable();
696
697 trace("starting cancelio tests\n");
698 test_cancelio();
699 }