1 /* Unit test suite for Ntdll NamedPipe API functions
3 * Copyright 2011 Bernhard Loos
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.
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.
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
24 #define WIN32_NO_STATUS
30 #include "wine/test.h"
34 #ifndef __WINE_WINTERNL_H
38 ULONG NamedPipeConfiguration
;
39 ULONG MaximumInstances
;
40 ULONG CurrentInstances
;
42 ULONG ReadDataAvailable
;
44 ULONG WriteQuotaAvailable
;
47 } FILE_PIPE_LOCAL_INFORMATION
;
49 #ifndef FILE_SYNCHRONOUS_IO_ALERT
50 #define FILE_SYNCHRONOUS_IO_ALERT 0x10
53 #ifndef FILE_SYNCHRONOUS_IO_NONALERT
54 #define FILE_SYNCHRONOUS_IO_NONALERT 0x20
57 #ifndef FSCTL_PIPE_LISTEN
58 #define FSCTL_PIPE_LISTEN CTL_CODE(FILE_DEVICE_NAMED_PIPE, 2, METHOD_BUFFERED, FILE_ANY_ACCESS)
62 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
);
63 static NTSTATUS (WINAPI
*pNtCreateNamedPipeFile
) (PHANDLE handle
, ULONG access
,
64 POBJECT_ATTRIBUTES attr
, PIO_STATUS_BLOCK iosb
,
65 ULONG sharing
, ULONG dispo
, ULONG options
,
66 ULONG pipe_type
, ULONG read_mode
,
67 ULONG completion_mode
, ULONG max_inst
,
68 ULONG inbound_quota
, ULONG outbound_quota
,
69 PLARGE_INTEGER timeout
);
70 static NTSTATUS (WINAPI
*pNtQueryInformationFile
) (IN HANDLE FileHandle
, OUT PIO_STATUS_BLOCK IoStatusBlock
, OUT PVOID FileInformation
, IN ULONG Length
, IN FILE_INFORMATION_CLASS FileInformationClass
);
71 static NTSTATUS (WINAPI
*pNtCancelIoFile
) (HANDLE hFile
, PIO_STATUS_BLOCK io_status
);
72 static void (WINAPI
*pRtlInitUnicodeString
) (PUNICODE_STRING target
, PCWSTR source
);
74 static HANDLE (WINAPI
*pOpenThread
)(DWORD dwDesiredAccess
, BOOL bInheritHandle
, DWORD dwThreadId
);
75 static DWORD (WINAPI
*pQueueUserAPC
)(PAPCFUNC pfnAPC
, HANDLE hThread
, ULONG_PTR dwData
);
78 static BOOL
init_func_ptrs(void)
80 HMODULE module
= GetModuleHandle("ntdll.dll");
82 #define loadfunc(name) if (!(p##name = (void *)GetProcAddress(module, #name))) { \
83 trace("GetProcAddress(%s) failed\n", #name); \
87 loadfunc(NtFsControlFile
)
88 loadfunc(NtCreateNamedPipeFile
)
89 loadfunc(NtQueryInformationFile
)
90 loadfunc(NtCancelIoFile
)
91 loadfunc(RtlInitUnicodeString
)
94 module
= GetModuleHandle("kernel32.dll");
95 pOpenThread
= (void *)GetProcAddress(module
, "OpenThread");
96 pQueueUserAPC
= (void *)GetProcAddress(module
, "QueueUserAPC");
100 static const WCHAR testpipe
[] = { '\\', '\\', '.', '\\', 'p', 'i', 'p', 'e', '\\',
101 't', 'e', 's', 't', 'p', 'i', 'p', 'e', 0 };
102 static const WCHAR testpipe_nt
[] = { '\\', '?', '?', '\\', 'p', 'i', 'p', 'e', '\\',
103 't', 'e', 's', 't', 'p', 'i', 'p', 'e', 0 };
105 static NTSTATUS
create_pipe(PHANDLE handle
, ULONG sharing
, ULONG options
)
107 IO_STATUS_BLOCK iosb
;
108 OBJECT_ATTRIBUTES attr
;
110 LARGE_INTEGER timeout
;
113 pRtlInitUnicodeString(&name
, testpipe_nt
);
115 attr
.Length
= sizeof(attr
);
116 attr
.RootDirectory
= 0;
117 attr
.ObjectName
= &name
;
118 attr
.Attributes
= 0x40; /*case insensitive */
119 attr
.SecurityDescriptor
= NULL
;
120 attr
.SecurityQualityOfService
= NULL
;
122 timeout
.QuadPart
= -100000000000ll;
124 res
= pNtCreateNamedPipeFile(handle
, FILE_READ_ATTRIBUTES
| SYNCHRONIZE
, &attr
, &iosb
, sharing
, 2 /*FILE_CREATE*/,
125 options
, 1, 0, 0, 0xFFFFFFFF, 500, 500, &timeout
);
129 static void test_create_invalid(void)
131 IO_STATUS_BLOCK iosb
;
132 OBJECT_ATTRIBUTES attr
;
134 LARGE_INTEGER timeout
;
136 HANDLE handle
, handle2
;
137 FILE_PIPE_LOCAL_INFORMATION info
;
139 pRtlInitUnicodeString(&name
, testpipe_nt
);
141 attr
.Length
= sizeof(attr
);
142 attr
.RootDirectory
= 0;
143 attr
.ObjectName
= &name
;
144 attr
.Attributes
= 0x40; /*case insensitive */
145 attr
.SecurityDescriptor
= NULL
;
146 attr
.SecurityQualityOfService
= NULL
;
148 timeout
.QuadPart
= -100000000000ll;
150 /* create a pipe with sharing = 0 */
151 res
= pNtCreateNamedPipeFile(&handle
, FILE_READ_ATTRIBUTES
| SYNCHRONIZE
, &attr
, &iosb
, 0, 2 /*FILE_CREATE*/,
152 0, 1, 0, 0, 0xFFFFFFFF, 500, 500, &timeout
);
153 ok(res
== STATUS_INVALID_PARAMETER
, "NtCreateNamedPipeFile returned %x\n", res
);
157 /* create a pipe without r/w access */
158 res
= pNtCreateNamedPipeFile(&handle
, SYNCHRONIZE
, &attr
, &iosb
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, 2 /*FILE_CREATE*/,
159 0, 1, 0, 0, 0xFFFFFFFF, 500, 500, &timeout
);
160 ok(!res
, "NtCreateNamedPipeFile returned %x\n", res
);
162 res
= pNtQueryInformationFile(handle
, &iosb
, &info
, sizeof(info
), (FILE_INFORMATION_CLASS
)24);
163 todo_wine
ok(res
== STATUS_ACCESS_DENIED
, "NtQueryInformationFile returned %x\n", res
);
165 /* test FILE_CREATE creation disposition */
166 res
= pNtCreateNamedPipeFile(&handle2
, SYNCHRONIZE
, &attr
, &iosb
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, 2 /*FILE_CREATE*/,
167 0, 1, 0, 0, 0xFFFFFFFF, 500, 500, &timeout
);
168 todo_wine
ok(res
== STATUS_ACCESS_DENIED
, "NtCreateNamedPipeFile returned %x\n", res
);
170 CloseHandle(handle2
);
175 static BOOL ioapc_called
;
176 static void CALLBACK
ioapc(void *arg
, PIO_STATUS_BLOCK io
, ULONG reserved
)
181 static NTSTATUS
listen_pipe(HANDLE hPipe
, HANDLE hEvent
, PIO_STATUS_BLOCK iosb
, BOOL use_apc
)
185 ioapc_called
= FALSE
;
187 return pNtFsControlFile(hPipe
, hEvent
, use_apc
? &ioapc
: NULL
, use_apc
? &dummy
: NULL
, iosb
, FSCTL_PIPE_LISTEN
, 0, 0, 0, 0);
190 static void test_overlapped(void)
192 IO_STATUS_BLOCK iosb
;
198 hEvent
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
199 ok(hEvent
!= INVALID_HANDLE_VALUE
, "can't create event, GetLastError: %x\n", GetLastError());
201 res
= create_pipe(&hPipe
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0 /* OVERLAPPED */);
202 ok(!res
, "NtCreateNamedPipeFile returned %x\n", res
);
204 memset(&iosb
, 0x55, sizeof(iosb
));
206 /* try with event and apc */
207 res
= listen_pipe(hPipe
, hEvent
, &iosb
, TRUE
);
208 ok(res
== STATUS_PENDING
, "NtFsControlFile returned %x\n", res
);
210 hClient
= CreateFileW(testpipe
, GENERIC_READ
| GENERIC_WRITE
, 0, 0, OPEN_EXISTING
, 0, 0);
211 ok(hClient
!= INVALID_HANDLE_VALUE
, "can't open pipe, GetLastError: %x\n", GetLastError());
213 ok(U(iosb
).Status
== 0, "Wrong iostatus %x\n", U(iosb
).Status
);
214 ok(WaitForSingleObject(hEvent
, 0) == 0, "hEvent not signaled\n");
216 ok(!ioapc_called
, "IOAPC ran too early\n");
218 SleepEx(0, TRUE
); /* alertable wait state */
220 ok(ioapc_called
, "IOAPC didn't run\n");
224 CloseHandle(hClient
);
227 static BOOL userapc_called
;
228 static void CALLBACK
userapc(ULONG_PTR dwParam
)
230 userapc_called
= TRUE
;
233 static BOOL open_succeeded
;
234 static DWORD WINAPI
thread(PVOID main_thread
)
242 userapc_called
= FALSE
;
243 ret
= pQueueUserAPC(&userapc
, main_thread
, 0);
244 ok(ret
, "can't queue user apc, GetLastError: %x\n", GetLastError());
245 CloseHandle(main_thread
);
250 h
= CreateFileW(testpipe
, GENERIC_WRITE
| GENERIC_READ
, 0, 0, OPEN_EXISTING
, 0, 0);
252 if (h
!= INVALID_HANDLE_VALUE
) {
253 open_succeeded
= TRUE
;
257 open_succeeded
= FALSE
;
262 static void test_alertable(void)
264 IO_STATUS_BLOCK iosb
;
271 memset(&iosb
, 0x55, sizeof(iosb
));
273 hEvent
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
274 ok(hEvent
!= INVALID_HANDLE_VALUE
, "can't create event, GetLastError: %x\n", GetLastError());
276 res
= create_pipe(&hPipe
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, FILE_SYNCHRONOUS_IO_ALERT
);
277 ok(!res
, "NtCreateNamedPipeFile returned %x\n", res
);
279 /* queue an user apc before calling listen */
280 userapc_called
= FALSE
;
281 ret
= pQueueUserAPC(&userapc
, GetCurrentThread(), 0);
282 ok(ret
, "can't queue user apc, GetLastError: %x\n", GetLastError());
284 res
= listen_pipe(hPipe
, hEvent
, &iosb
, TRUE
);
285 todo_wine
ok(res
== STATUS_CANCELLED
, "NtFsControlFile returned %x\n", res
);
287 todo_wine
ok(userapc_called
, "user apc didn't run\n");
288 ok(U(iosb
).Status
== 0x55555555, "iosb.Status got changed to %x\n", U(iosb
).Status
);
289 todo_wine
ok(WaitForSingleObjectEx(hEvent
, 0, TRUE
) == WAIT_TIMEOUT
, "hEvent signaled\n");
290 ok(!ioapc_called
, "IOAPC ran\n");
292 /* queue an user apc from a different thread */
293 hThread
= CreateThread(NULL
, 0, &thread
, pOpenThread(MAXIMUM_ALLOWED
, FALSE
, GetCurrentThreadId()), 0, 0);
294 ok(hThread
!= INVALID_HANDLE_VALUE
, "can't create thread, GetLastError: %x\n", GetLastError());
296 /* wine_todo: the earlier NtFsControlFile call gets cancelled after the pipe gets set into listen state
297 instead of before, so this NtFsControlFile will fail STATUS_INVALID_HANDLE */
298 res
= listen_pipe(hPipe
, hEvent
, &iosb
, TRUE
);
299 todo_wine
ok(res
== STATUS_CANCELLED
, "NtFsControlFile returned %x\n", res
);
301 ok(userapc_called
, "user apc didn't run\n");
302 todo_wine
ok(U(iosb
).Status
== 0x55555555, "iosb.Status got changed to %x\n", U(iosb
).Status
);
303 ok(WaitForSingleObjectEx(hEvent
, 0, TRUE
) == WAIT_TIMEOUT
, "hEvent signaled\n");
304 ok(!ioapc_called
, "IOAPC ran\n");
306 WaitForSingleObject(hThread
, INFINITE
);
308 SleepEx(0, TRUE
); /* get rid of the userapc, if NtFsControlFile failed */
310 ok(open_succeeded
, "couldn't open client side pipe\n");
312 CloseHandle(hThread
);
313 DisconnectNamedPipe(hPipe
);
315 /* finally try without an apc */
316 hThread
= CreateThread(NULL
, 0, &thread
, 0, 0, 0);
317 ok(hThread
!= INVALID_HANDLE_VALUE
, "can't create thread, GetLastError: %x\n", GetLastError());
319 res
= listen_pipe(hPipe
, hEvent
, &iosb
, TRUE
);
320 todo_wine
ok(!res
, "NtFsControlFile returned %x\n", res
);
322 ok(open_succeeded
, "couldn't open client side pipe\n");
323 ok(!U(iosb
).Status
, "Wrong iostatus %x\n", U(iosb
).Status
);
324 todo_wine
ok(WaitForSingleObject(hEvent
, 0) == 0, "hEvent not signaled\n");
326 WaitForSingleObject(hThread
, INFINITE
);
327 CloseHandle(hThread
);
332 static void test_nonalertable(void)
334 IO_STATUS_BLOCK iosb
;
341 memset(&iosb
, 0x55, sizeof(iosb
));
343 hEvent
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
344 ok(hEvent
!= INVALID_HANDLE_VALUE
, "can't create event, GetLastError: %x\n", GetLastError());
346 res
= create_pipe(&hPipe
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, FILE_SYNCHRONOUS_IO_NONALERT
);
347 ok(!res
, "NtCreateNamedPipeFile returned %x\n", res
);
349 hThread
= CreateThread(NULL
, 0, &thread
, 0, 0, 0);
350 ok(hThread
!= INVALID_HANDLE_VALUE
, "can't create thread, GetLastError: %x\n", GetLastError());
352 userapc_called
= FALSE
;
353 ret
= pQueueUserAPC(&userapc
, GetCurrentThread(), 0);
354 ok(ret
, "can't queue user apc, GetLastError: %x\n", GetLastError());
356 res
= listen_pipe(hPipe
, hEvent
, &iosb
, TRUE
);
357 todo_wine
ok(!res
, "NtFsControlFile returned %x\n", res
);
359 ok(open_succeeded
, "couldn't open client side pipe\n");
360 todo_wine
ok(!U(iosb
).Status
, "Wrong iostatus %x\n", U(iosb
).Status
);
361 todo_wine
ok(WaitForSingleObject(hEvent
, 0) == 0, "hEvent not signaled\n");
363 ok(!ioapc_called
, "IOAPC ran too early\n");
364 ok(!userapc_called
, "user apc ran too early\n");
366 SleepEx(0, TRUE
); /* alertable wait state */
368 ok(ioapc_called
, "IOAPC didn't run\n");
369 ok(userapc_called
, "user apc didn't run\n");
371 WaitForSingleObject(hThread
, INFINITE
);
372 CloseHandle(hThread
);
377 static void test_cancelio(void)
379 IO_STATUS_BLOCK iosb
;
380 IO_STATUS_BLOCK cancel_sb
;
385 hEvent
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
386 ok(hEvent
!= INVALID_HANDLE_VALUE
, "can't create event, GetLastError: %x\n", GetLastError());
388 res
= create_pipe(&hPipe
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0 /* OVERLAPPED */);
389 ok(!res
, "NtCreateNamedPipeFile returned %x\n", res
);
391 memset(&iosb
, 0x55, sizeof(iosb
));
393 res
= listen_pipe(hPipe
, hEvent
, &iosb
, TRUE
);
394 ok(res
== STATUS_PENDING
, "NtFsControlFile returned %x\n", res
);
396 res
= pNtCancelIoFile(hPipe
, &cancel_sb
);
397 todo_wine
ok(!res
, "NtCancelIoFile returned %x\n", res
);
400 ok(U(iosb
).Status
== STATUS_CANCELLED
, "Wrong iostatus %x\n", U(iosb
).Status
);
401 ok(WaitForSingleObject(hEvent
, 0) == 0, "hEvent not signaled\n");
404 ok(!ioapc_called
, "IOAPC ran too early\n");
406 SleepEx(0, TRUE
); /* alertable wait state */
408 ok(ioapc_called
, "IOAPC didn't run\n");
416 if (!init_func_ptrs())
419 trace("starting invalid create tests\n");
420 test_create_invalid();
422 trace("starting overlapped tests\n");
425 if (!pOpenThread
|| !pQueueUserAPC
)
428 trace("starting alertable tests\n");
431 trace("starting nonalertable tests\n");
434 trace("starting cancelio tests\n");