2 * Unit tests for named pipe functions in Wine
4 * Copyright (c) 2002 Dan Kegel
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #include "wine/test.h"
32 #define PIPENAME "\\\\.\\PiPe\\tests_pipe.c"
34 #define NB_SERVER_LOOPS 8
36 static HANDLE alarm_event
;
37 static BOOL (WINAPI
*pDuplicateTokenEx
)(HANDLE
,DWORD
,LPSECURITY_ATTRIBUTES
,
38 SECURITY_IMPERSONATION_LEVEL
,TOKEN_TYPE
,PHANDLE
);
39 static DWORD (WINAPI
*pQueueUserAPC
)(PAPCFUNC pfnAPC
, HANDLE hThread
, ULONG_PTR dwData
);
41 static BOOL user_apc_ran
;
42 static void CALLBACK
user_apc(ULONG_PTR param
)
47 static void test_CreateNamedPipe(int pipemode
)
51 static const char obuf
[] = "Bit Bucket";
52 static const char obuf2
[] = "More bits";
60 if (pipemode
== PIPE_TYPE_BYTE
)
61 trace("test_CreateNamedPipe starting in byte mode\n");
63 trace("test_CreateNamedPipe starting in message mode\n");
64 /* Bad parameter checks */
65 hnp
= CreateNamedPipe("not a named pipe", PIPE_ACCESS_DUPLEX
, pipemode
| PIPE_WAIT
,
66 /* nMaxInstances */ 1,
67 /* nOutBufSize */ 1024,
68 /* nInBufSize */ 1024,
69 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
70 /* lpSecurityAttrib */ NULL
);
71 ok(hnp
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_INVALID_NAME
,
72 "CreateNamedPipe should fail if name doesn't start with \\\\.\\pipe\n");
74 if (pipemode
== PIPE_TYPE_BYTE
)
76 /* Bad parameter checks */
77 hnp
= CreateNamedPipe(PIPENAME
, PIPE_ACCESS_DUPLEX
, PIPE_TYPE_BYTE
| PIPE_READMODE_MESSAGE
,
78 /* nMaxInstances */ 1,
79 /* nOutBufSize */ 1024,
80 /* nInBufSize */ 1024,
81 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
82 /* lpSecurityAttrib */ NULL
);
83 ok(hnp
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_INVALID_PARAMETER
,
84 "CreateNamedPipe should fail with PIPE_TYPE_BYTE | PIPE_READMODE_MESSAGE\n");
87 hnp
= CreateNamedPipe(NULL
,
88 PIPE_ACCESS_DUPLEX
, pipemode
| PIPE_WAIT
,
89 1, 1024, 1024, NMPWAIT_USE_DEFAULT_WAIT
, NULL
);
90 ok(hnp
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_PATH_NOT_FOUND
,
91 "CreateNamedPipe should fail if name is NULL\n");
93 hFile
= CreateFileA(PIPENAME
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0);
94 ok(hFile
== INVALID_HANDLE_VALUE
95 && GetLastError() == ERROR_FILE_NOT_FOUND
,
96 "connecting to nonexistent named pipe should fail with ERROR_FILE_NOT_FOUND\n");
98 /* Functional checks */
100 hnp
= CreateNamedPipe(PIPENAME
, PIPE_ACCESS_DUPLEX
, pipemode
| PIPE_WAIT
,
101 /* nMaxInstances */ 1,
102 /* nOutBufSize */ 1024,
103 /* nInBufSize */ 1024,
104 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
105 /* lpSecurityAttrib */ NULL
);
106 ok(hnp
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
108 ret
= WaitNamedPipeA(PIPENAME
, 2000);
109 ok(ret
, "WaitNamedPipe failed (%d)\n", GetLastError());
111 hFile
= CreateFileA(PIPENAME
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0);
112 ok(hFile
!= INVALID_HANDLE_VALUE
, "CreateFile failed (%d)\n", GetLastError());
114 ok(!WaitNamedPipeA(PIPENAME
, 1000), "WaitNamedPipe succeeded\n");
116 ok(GetLastError() == ERROR_SEM_TIMEOUT
, "wrong error %u\n", GetLastError());
118 /* don't try to do i/o if one side couldn't be opened, as it hangs */
119 if (hFile
!= INVALID_HANDLE_VALUE
) {
122 /* Make sure we can read and write a few bytes in both directions */
123 memset(ibuf
, 0, sizeof(ibuf
));
124 ok(WriteFile(hnp
, obuf
, sizeof(obuf
), &written
, NULL
), "WriteFile\n");
125 ok(written
== sizeof(obuf
), "write file len 1\n");
126 ok(PeekNamedPipe(hFile
, NULL
, 0, NULL
, &readden
, NULL
), "Peek\n");
127 ok(readden
== sizeof(obuf
), "peek 1 got %d bytes\n", readden
);
128 ok(ReadFile(hFile
, ibuf
, sizeof(ibuf
), &readden
, NULL
), "ReadFile\n");
129 ok(readden
== sizeof(obuf
), "read 1 got %d bytes\n", readden
);
130 ok(memcmp(obuf
, ibuf
, written
) == 0, "content 1 check\n");
132 memset(ibuf
, 0, sizeof(ibuf
));
133 ok(WriteFile(hFile
, obuf2
, sizeof(obuf2
), &written
, NULL
), "WriteFile\n");
134 ok(written
== sizeof(obuf2
), "write file len 2\n");
135 ok(PeekNamedPipe(hnp
, NULL
, 0, NULL
, &readden
, NULL
), "Peek\n");
136 ok(readden
== sizeof(obuf2
), "peek 2 got %d bytes\n", readden
);
137 ok(PeekNamedPipe(hnp
, (LPVOID
)1, 0, NULL
, &readden
, NULL
), "Peek\n");
138 ok(readden
== sizeof(obuf2
), "peek 2 got %d bytes\n", readden
);
139 ok(ReadFile(hnp
, ibuf
, sizeof(ibuf
), &readden
, NULL
), "ReadFile\n");
140 ok(readden
== sizeof(obuf2
), "read 2 got %d bytes\n", readden
);
141 ok(memcmp(obuf2
, ibuf
, written
) == 0, "content 2 check\n");
143 /* Test reading of multiple writes */
144 memset(ibuf
, 0, sizeof(ibuf
));
145 ok(WriteFile(hnp
, obuf
, sizeof(obuf
), &written
, NULL
), "WriteFile3a\n");
146 ok(written
== sizeof(obuf
), "write file len 3a\n");
147 ok(WriteFile(hnp
, obuf2
, sizeof(obuf2
), &written
, NULL
), " WriteFile3b\n");
148 ok(written
== sizeof(obuf2
), "write file len 3b\n");
149 ok(PeekNamedPipe(hFile
, ibuf
, sizeof(ibuf
), &readden
, &avail
, NULL
), "Peek3\n");
150 if (pipemode
== PIPE_TYPE_BYTE
) {
151 if (readden
!= sizeof(obuf
)) /* Linux only returns the first message */
152 ok(readden
== sizeof(obuf
) + sizeof(obuf2
), "peek3 got %d bytes\n", readden
);
154 todo_wine
ok(readden
== sizeof(obuf
) + sizeof(obuf2
), "peek3 got %d bytes\n", readden
);
158 if (readden
!= sizeof(obuf
) + sizeof(obuf2
)) /* MacOS returns both messages */
159 ok(readden
== sizeof(obuf
), "peek3 got %d bytes\n", readden
);
161 todo_wine
ok(readden
== sizeof(obuf
), "peek3 got %d bytes\n", readden
);
163 if (avail
!= sizeof(obuf
)) /* older Linux kernels only return the first write here */
164 ok(avail
== sizeof(obuf
) + sizeof(obuf2
), "peek3 got %d bytes available\n", avail
);
166 ok(memcmp(obuf
, pbuf
, sizeof(obuf
)) == 0, "pipe content 3a check\n");
167 if (pipemode
== PIPE_TYPE_BYTE
&& readden
>= sizeof(obuf
)+sizeof(obuf2
)) {
168 pbuf
+= sizeof(obuf
);
169 ok(memcmp(obuf2
, pbuf
, sizeof(obuf2
)) == 0, "pipe content 3b check\n");
171 ok(ReadFile(hFile
, ibuf
, sizeof(ibuf
), &readden
, NULL
), "ReadFile\n");
172 ok(readden
== sizeof(obuf
) + sizeof(obuf2
), "read 3 got %d bytes\n", readden
);
174 ok(memcmp(obuf
, pbuf
, sizeof(obuf
)) == 0, "content 3a check\n");
175 pbuf
+= sizeof(obuf
);
176 ok(memcmp(obuf2
, pbuf
, sizeof(obuf2
)) == 0, "content 3b check\n");
178 /* Multiple writes in the reverse direction */
179 memset(ibuf
, 0, sizeof(ibuf
));
180 ok(WriteFile(hFile
, obuf
, sizeof(obuf
), &written
, NULL
), "WriteFile4a\n");
181 ok(written
== sizeof(obuf
), "write file len 4a\n");
182 ok(WriteFile(hFile
, obuf2
, sizeof(obuf2
), &written
, NULL
), " WriteFile4b\n");
183 ok(written
== sizeof(obuf2
), "write file len 4b\n");
184 ok(PeekNamedPipe(hnp
, ibuf
, sizeof(ibuf
), &readden
, &avail
, NULL
), "Peek4\n");
185 if (pipemode
== PIPE_TYPE_BYTE
) {
186 if (readden
!= sizeof(obuf
)) /* Linux only returns the first message */
187 /* should return all 23 bytes */
188 ok(readden
== sizeof(obuf
) + sizeof(obuf2
), "peek4 got %d bytes\n", readden
);
190 todo_wine
ok(readden
== sizeof(obuf
) + sizeof(obuf2
), "peek4 got %d bytes\n", readden
);
194 if (readden
!= sizeof(obuf
) + sizeof(obuf2
)) /* MacOS returns both messages */
195 ok(readden
== sizeof(obuf
), "peek4 got %d bytes\n", readden
);
197 todo_wine
ok(readden
== sizeof(obuf
), "peek4 got %d bytes\n", readden
);
199 if (avail
!= sizeof(obuf
)) /* older Linux kernels only return the first write here */
200 ok(avail
== sizeof(obuf
) + sizeof(obuf2
), "peek4 got %d bytes available\n", avail
);
202 ok(memcmp(obuf
, pbuf
, sizeof(obuf
)) == 0, "pipe content 4a check\n");
203 if (pipemode
== PIPE_TYPE_BYTE
&& readden
>= sizeof(obuf
)+sizeof(obuf2
)) {
204 pbuf
+= sizeof(obuf
);
205 ok(memcmp(obuf2
, pbuf
, sizeof(obuf2
)) == 0, "pipe content 4b check\n");
207 ok(ReadFile(hnp
, ibuf
, sizeof(ibuf
), &readden
, NULL
), "ReadFile\n");
208 if (pipemode
== PIPE_TYPE_BYTE
) {
209 ok(readden
== sizeof(obuf
) + sizeof(obuf2
), "read 4 got %d bytes\n", readden
);
213 ok(readden
== sizeof(obuf
), "read 4 got %d bytes\n", readden
);
217 ok(memcmp(obuf
, pbuf
, sizeof(obuf
)) == 0, "content 4a check\n");
218 if (pipemode
== PIPE_TYPE_BYTE
) {
219 pbuf
+= sizeof(obuf
);
220 ok(memcmp(obuf2
, pbuf
, sizeof(obuf2
)) == 0, "content 4b check\n");
223 /* Test reading of multiple writes after a mode change
224 (CreateFile always creates a byte mode pipe) */
225 lpmode
= PIPE_READMODE_MESSAGE
;
226 if (pipemode
== PIPE_TYPE_BYTE
) {
227 /* trying to change the client end of a byte pipe to message mode should fail */
228 ok(!SetNamedPipeHandleState(hFile
, &lpmode
, NULL
, NULL
), "Change mode\n");
232 ok(SetNamedPipeHandleState(hFile
, &lpmode
, NULL
, NULL
), "Change mode\n");
235 memset(ibuf
, 0, sizeof(ibuf
));
236 ok(WriteFile(hnp
, obuf
, sizeof(obuf
), &written
, NULL
), "WriteFile5a\n");
237 ok(written
== sizeof(obuf
), "write file len 3a\n");
238 ok(WriteFile(hnp
, obuf2
, sizeof(obuf2
), &written
, NULL
), " WriteFile5b\n");
239 ok(written
== sizeof(obuf2
), "write file len 3b\n");
240 ok(PeekNamedPipe(hFile
, ibuf
, sizeof(ibuf
), &readden
, &avail
, NULL
), "Peek5\n");
241 if (readden
!= sizeof(obuf
) + sizeof(obuf2
)) /* MacOS returns both writes */
242 ok(readden
== sizeof(obuf
), "peek5 got %d bytes\n", readden
);
244 todo_wine
ok(readden
== sizeof(obuf
), "peek5 got %d bytes\n", readden
);
245 if (avail
!= sizeof(obuf
)) /* older Linux kernels only return the first write here */
246 ok(avail
== sizeof(obuf
) + sizeof(obuf2
), "peek5 got %d bytes available\n", avail
);
248 todo_wine
ok(avail
== sizeof(obuf
) + sizeof(obuf2
), "peek5 got %d bytes available\n", avail
);
250 ok(memcmp(obuf
, pbuf
, sizeof(obuf
)) == 0, "content 5a check\n");
251 ok(ReadFile(hFile
, ibuf
, sizeof(ibuf
), &readden
, NULL
), "ReadFile\n");
253 ok(readden
== sizeof(obuf
), "read 5 got %d bytes\n", readden
);
256 ok(memcmp(obuf
, pbuf
, sizeof(obuf
)) == 0, "content 5a check\n");
258 /* Multiple writes in the reverse direction */
259 /* the write of obuf2 from write4 should still be in the buffer */
260 ok(PeekNamedPipe(hnp
, ibuf
, sizeof(ibuf
), &readden
, &avail
, NULL
), "Peek6a\n");
262 ok(readden
== sizeof(obuf2
), "peek6a got %d bytes\n", readden
);
263 ok(avail
== sizeof(obuf2
), "peek6a got %d bytes available\n", avail
);
266 ok(ReadFile(hnp
, ibuf
, sizeof(ibuf
), &readden
, NULL
), "ReadFile\n");
267 ok(readden
== sizeof(obuf2
), "read 6a got %d bytes\n", readden
);
269 ok(memcmp(obuf2
, pbuf
, sizeof(obuf2
)) == 0, "content 6a check\n");
271 memset(ibuf
, 0, sizeof(ibuf
));
272 ok(WriteFile(hFile
, obuf
, sizeof(obuf
), &written
, NULL
), "WriteFile6a\n");
273 ok(written
== sizeof(obuf
), "write file len 6a\n");
274 ok(WriteFile(hFile
, obuf2
, sizeof(obuf2
), &written
, NULL
), " WriteFile6b\n");
275 ok(written
== sizeof(obuf2
), "write file len 6b\n");
276 ok(PeekNamedPipe(hnp
, ibuf
, sizeof(ibuf
), &readden
, &avail
, NULL
), "Peek6\n");
277 if (readden
!= sizeof(obuf
) + sizeof(obuf2
)) /* MacOS returns both writes */
278 ok(readden
== sizeof(obuf
), "peek6 got %d bytes\n", readden
);
280 todo_wine
ok(readden
== sizeof(obuf
), "peek6 got %d bytes\n", readden
);
281 if (avail
!= sizeof(obuf
)) /* older Linux kernels only return the first write here */
282 ok(avail
== sizeof(obuf
) + sizeof(obuf2
), "peek6b got %d bytes available\n", avail
);
284 ok(memcmp(obuf
, pbuf
, sizeof(obuf
)) == 0, "content 6a check\n");
285 ok(ReadFile(hnp
, ibuf
, sizeof(ibuf
), &readden
, NULL
), "ReadFile\n");
287 ok(readden
== sizeof(obuf
), "read 6b got %d bytes\n", readden
);
290 ok(memcmp(obuf
, pbuf
, sizeof(obuf
)) == 0, "content 6a check\n");
293 /* Picky conformance tests */
295 /* Verify that you can't connect to pipe again
296 * until server calls DisconnectNamedPipe+ConnectNamedPipe
297 * or creates a new pipe
298 * case 1: other client not yet closed
300 hFile2
= CreateFileA(PIPENAME
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0);
301 ok(hFile2
== INVALID_HANDLE_VALUE
,
302 "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail\n");
303 ok(GetLastError() == ERROR_PIPE_BUSY
,
304 "connecting to named pipe before other client closes should fail with ERROR_PIPE_BUSY\n");
306 ok(CloseHandle(hFile
), "CloseHandle\n");
308 /* case 2: other client already closed */
309 hFile
= CreateFileA(PIPENAME
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0);
310 ok(hFile
== INVALID_HANDLE_VALUE
,
311 "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail\n");
312 ok(GetLastError() == ERROR_PIPE_BUSY
,
313 "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail with ERROR_PIPE_BUSY\n");
315 ok(DisconnectNamedPipe(hnp
), "DisconnectNamedPipe\n");
317 /* case 3: server has called DisconnectNamedPipe but not ConnectNamed Pipe */
318 hFile
= CreateFileA(PIPENAME
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0);
319 ok(hFile
== INVALID_HANDLE_VALUE
,
320 "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail\n");
321 ok(GetLastError() == ERROR_PIPE_BUSY
,
322 "connecting to named pipe after other client closes but before ConnectNamedPipe should fail with ERROR_PIPE_BUSY\n");
324 /* to be complete, we'd call ConnectNamedPipe here and loop,
325 * but by default that's blocking, so we'd either have
326 * to turn on the uncommon nonblocking mode, or
327 * use another thread.
331 ok(CloseHandle(hnp
), "CloseHandle\n");
333 trace("test_CreateNamedPipe returning\n");
336 static void test_CreateNamedPipe_instances_must_match(void)
340 /* Check no mismatch */
341 hnp
= CreateNamedPipe(PIPENAME
, PIPE_ACCESS_DUPLEX
, PIPE_TYPE_BYTE
| PIPE_WAIT
,
342 /* nMaxInstances */ 2,
343 /* nOutBufSize */ 1024,
344 /* nInBufSize */ 1024,
345 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
346 /* lpSecurityAttrib */ NULL
);
347 ok(hnp
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
349 hnp2
= CreateNamedPipe(PIPENAME
, PIPE_ACCESS_DUPLEX
, PIPE_TYPE_BYTE
| PIPE_WAIT
,
350 /* nMaxInstances */ 2,
351 /* nOutBufSize */ 1024,
352 /* nInBufSize */ 1024,
353 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
354 /* lpSecurityAttrib */ NULL
);
355 ok(hnp2
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
357 ok(CloseHandle(hnp
), "CloseHandle\n");
358 ok(CloseHandle(hnp2
), "CloseHandle\n");
360 /* Check nMaxInstances */
361 hnp
= CreateNamedPipe(PIPENAME
, PIPE_ACCESS_DUPLEX
, PIPE_TYPE_BYTE
| PIPE_WAIT
,
362 /* nMaxInstances */ 1,
363 /* nOutBufSize */ 1024,
364 /* nInBufSize */ 1024,
365 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
366 /* lpSecurityAttrib */ NULL
);
367 ok(hnp
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
369 hnp2
= CreateNamedPipe(PIPENAME
, PIPE_ACCESS_DUPLEX
, PIPE_TYPE_BYTE
| PIPE_WAIT
,
370 /* nMaxInstances */ 1,
371 /* nOutBufSize */ 1024,
372 /* nInBufSize */ 1024,
373 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
374 /* lpSecurityAttrib */ NULL
);
375 ok(hnp2
== INVALID_HANDLE_VALUE
376 && GetLastError() == ERROR_PIPE_BUSY
, "nMaxInstances not obeyed\n");
378 ok(CloseHandle(hnp
), "CloseHandle\n");
380 /* Check PIPE_ACCESS_* */
381 hnp
= CreateNamedPipe(PIPENAME
, PIPE_ACCESS_DUPLEX
, PIPE_TYPE_BYTE
| PIPE_WAIT
,
382 /* nMaxInstances */ 2,
383 /* nOutBufSize */ 1024,
384 /* nInBufSize */ 1024,
385 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
386 /* lpSecurityAttrib */ NULL
);
387 ok(hnp
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
389 hnp2
= CreateNamedPipe(PIPENAME
, PIPE_ACCESS_INBOUND
, PIPE_TYPE_BYTE
| PIPE_WAIT
,
390 /* nMaxInstances */ 2,
391 /* nOutBufSize */ 1024,
392 /* nInBufSize */ 1024,
393 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
394 /* lpSecurityAttrib */ NULL
);
395 ok(hnp2
== INVALID_HANDLE_VALUE
396 && GetLastError() == ERROR_ACCESS_DENIED
, "PIPE_ACCESS_* mismatch allowed\n");
398 ok(CloseHandle(hnp
), "CloseHandle\n");
400 /* check everything else */
401 hnp
= CreateNamedPipe(PIPENAME
, PIPE_ACCESS_DUPLEX
, PIPE_TYPE_BYTE
| PIPE_WAIT
,
402 /* nMaxInstances */ 4,
403 /* nOutBufSize */ 1024,
404 /* nInBufSize */ 1024,
405 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
406 /* lpSecurityAttrib */ NULL
);
407 ok(hnp
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
409 hnp2
= CreateNamedPipe(PIPENAME
, PIPE_ACCESS_DUPLEX
, PIPE_TYPE_MESSAGE
,
410 /* nMaxInstances */ 3,
411 /* nOutBufSize */ 102,
413 /* nDefaultWait */ 1234,
414 /* lpSecurityAttrib */ NULL
);
415 ok(hnp2
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
417 ok(CloseHandle(hnp
), "CloseHandle\n");
418 ok(CloseHandle(hnp2
), "CloseHandle\n");
421 /** implementation of alarm() */
422 static DWORD CALLBACK
alarmThreadMain(LPVOID arg
)
424 DWORD_PTR timeout
= (DWORD_PTR
) arg
;
425 trace("alarmThreadMain\n");
426 if (WaitForSingleObject( alarm_event
, timeout
) == WAIT_TIMEOUT
)
428 ok(FALSE
, "alarm\n");
434 static HANDLE hnp
= INVALID_HANDLE_VALUE
;
436 /** Trivial byte echo server - disconnects after each session */
437 static DWORD CALLBACK
serverThreadMain1(LPVOID arg
)
441 trace("serverThreadMain1 start\n");
442 /* Set up a simple echo server */
443 hnp
= CreateNamedPipe(PIPENAME
"serverThreadMain1", PIPE_ACCESS_DUPLEX
,
444 PIPE_TYPE_BYTE
| PIPE_WAIT
,
445 /* nMaxInstances */ 1,
446 /* nOutBufSize */ 1024,
447 /* nInBufSize */ 1024,
448 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
449 /* lpSecurityAttrib */ NULL
);
451 ok(hnp
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
452 for (i
= 0; i
< NB_SERVER_LOOPS
; i
++) {
458 /* Wait for client to connect */
459 trace("Server calling ConnectNamedPipe...\n");
460 ok(ConnectNamedPipe(hnp
, NULL
)
461 || GetLastError() == ERROR_PIPE_CONNECTED
, "ConnectNamedPipe\n");
462 trace("ConnectNamedPipe returned.\n");
464 /* Echo bytes once */
465 memset(buf
, 0, sizeof(buf
));
467 trace("Server reading...\n");
468 success
= ReadFile(hnp
, buf
, sizeof(buf
), &readden
, NULL
);
469 trace("Server done reading.\n");
470 ok(success
, "ReadFile\n");
471 ok(readden
, "short read\n");
473 trace("Server writing...\n");
474 ok(WriteFile(hnp
, buf
, readden
, &written
, NULL
), "WriteFile\n");
475 trace("Server done writing.\n");
476 ok(written
== readden
, "write file len\n");
478 /* finish this connection, wait for next one */
479 ok(FlushFileBuffers(hnp
), "FlushFileBuffers\n");
480 trace("Server done flushing.\n");
481 ok(DisconnectNamedPipe(hnp
), "DisconnectNamedPipe\n");
482 trace("Server done disconnecting.\n");
487 /** Trivial byte echo server - closes after each connection */
488 static DWORD CALLBACK
serverThreadMain2(LPVOID arg
)
493 trace("serverThreadMain2\n");
494 /* Set up a simple echo server */
495 hnp
= CreateNamedPipe(PIPENAME
"serverThreadMain2", PIPE_ACCESS_DUPLEX
,
496 PIPE_TYPE_BYTE
| PIPE_WAIT
,
497 /* nMaxInstances */ 2,
498 /* nOutBufSize */ 1024,
499 /* nInBufSize */ 1024,
500 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
501 /* lpSecurityAttrib */ NULL
);
502 ok(hnp
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
504 for (i
= 0; i
< NB_SERVER_LOOPS
; i
++) {
510 user_apc_ran
= FALSE
;
511 if (i
== 0 && pQueueUserAPC
) {
512 trace("Queueing an user APC\n"); /* verify the pipe is non alerable */
513 success
= pQueueUserAPC(&user_apc
, GetCurrentThread(), 0);
514 ok(success
, "QueueUserAPC failed: %d\n", GetLastError());
517 /* Wait for client to connect */
518 trace("Server calling ConnectNamedPipe...\n");
519 ok(ConnectNamedPipe(hnp
, NULL
)
520 || GetLastError() == ERROR_PIPE_CONNECTED
, "ConnectNamedPipe\n");
521 trace("ConnectNamedPipe returned.\n");
523 /* Echo bytes once */
524 memset(buf
, 0, sizeof(buf
));
526 trace("Server reading...\n");
527 success
= ReadFile(hnp
, buf
, sizeof(buf
), &readden
, NULL
);
528 trace("Server done reading.\n");
529 ok(success
, "ReadFile\n");
531 trace("Server writing...\n");
532 ok(WriteFile(hnp
, buf
, readden
, &written
, NULL
), "WriteFile\n");
533 trace("Server done writing.\n");
534 ok(written
== readden
, "write file len\n");
536 /* finish this connection, wait for next one */
537 ok(FlushFileBuffers(hnp
), "FlushFileBuffers\n");
538 ok(DisconnectNamedPipe(hnp
), "DisconnectNamedPipe\n");
540 ok(user_apc_ran
== FALSE
, "UserAPC ran, pipe using alertable io mode\n");
542 if (i
== 0 && pQueueUserAPC
)
543 SleepEx(0, TRUE
); /* get rid of apc */
545 /* Set up next echo server */
547 CreateNamedPipe(PIPENAME
"serverThreadMain2", PIPE_ACCESS_DUPLEX
,
548 PIPE_TYPE_BYTE
| PIPE_WAIT
,
549 /* nMaxInstances */ 2,
550 /* nOutBufSize */ 1024,
551 /* nInBufSize */ 1024,
552 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
553 /* lpSecurityAttrib */ NULL
);
555 ok(hnpNext
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
557 ok(CloseHandle(hnp
), "CloseHandle\n");
563 /** Trivial byte echo server - uses overlapped named pipe calls */
564 static DWORD CALLBACK
serverThreadMain3(LPVOID arg
)
569 trace("serverThreadMain3\n");
570 /* Set up a simple echo server */
571 hnp
= CreateNamedPipe(PIPENAME
"serverThreadMain3", PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
572 PIPE_TYPE_BYTE
| PIPE_WAIT
,
573 /* nMaxInstances */ 1,
574 /* nOutBufSize */ 1024,
575 /* nInBufSize */ 1024,
576 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
577 /* lpSecurityAttrib */ NULL
);
578 ok(hnp
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
580 hEvent
= CreateEvent(NULL
, /* security attribute */
581 TRUE
, /* manual reset event */
582 FALSE
, /* initial state */
584 ok(hEvent
!= NULL
, "CreateEvent\n");
586 for (i
= 0; i
< NB_SERVER_LOOPS
; i
++) {
593 int letWFSOEwait
= (i
& 2);
594 int letGORwait
= (i
& 1);
597 memset(&oOverlap
, 0, sizeof(oOverlap
));
598 oOverlap
.hEvent
= hEvent
;
600 /* Wait for client to connect */
602 trace("Server calling non-overlapped ConnectNamedPipe on overlapped pipe...\n");
603 success
= ConnectNamedPipe(hnp
, NULL
);
604 err
= GetLastError();
605 ok(success
|| (err
== ERROR_PIPE_CONNECTED
), "ConnectNamedPipe failed: %d\n", err
);
606 trace("ConnectNamedPipe operation complete.\n");
608 trace("Server calling overlapped ConnectNamedPipe...\n");
609 success
= ConnectNamedPipe(hnp
, &oOverlap
);
610 err
= GetLastError();
611 ok(!success
&& (err
== ERROR_IO_PENDING
|| err
== ERROR_PIPE_CONNECTED
), "overlapped ConnectNamedPipe\n");
612 trace("overlapped ConnectNamedPipe returned.\n");
613 if (!success
&& (err
== ERROR_IO_PENDING
)) {
618 ret
= WaitForSingleObjectEx(hEvent
, INFINITE
, TRUE
);
619 } while (ret
== WAIT_IO_COMPLETION
);
620 ok(ret
== 0, "wait ConnectNamedPipe returned %x\n", ret
);
622 success
= GetOverlappedResult(hnp
, &oOverlap
, &dummy
, letGORwait
);
623 if (!letGORwait
&& !letWFSOEwait
&& !success
) {
624 ok(GetLastError() == ERROR_IO_INCOMPLETE
, "GetOverlappedResult\n");
625 success
= GetOverlappedResult(hnp
, &oOverlap
, &dummy
, TRUE
);
628 ok(success
|| (err
== ERROR_PIPE_CONNECTED
), "GetOverlappedResult ConnectNamedPipe\n");
629 trace("overlapped ConnectNamedPipe operation complete.\n");
632 /* Echo bytes once */
633 memset(buf
, 0, sizeof(buf
));
635 trace("Server reading...\n");
636 success
= ReadFile(hnp
, buf
, sizeof(buf
), &readden
, &oOverlap
);
637 trace("Server ReadFile returned...\n");
638 err
= GetLastError();
639 ok(success
|| err
== ERROR_IO_PENDING
, "overlapped ReadFile\n");
640 trace("overlapped ReadFile returned.\n");
641 if (!success
&& (err
== ERROR_IO_PENDING
)) {
646 ret
= WaitForSingleObjectEx(hEvent
, INFINITE
, TRUE
);
647 } while (ret
== WAIT_IO_COMPLETION
);
648 ok(ret
== 0, "wait ReadFile returned %x\n", ret
);
650 success
= GetOverlappedResult(hnp
, &oOverlap
, &readden
, letGORwait
);
651 if (!letGORwait
&& !letWFSOEwait
&& !success
) {
652 ok(GetLastError() == ERROR_IO_INCOMPLETE
, "GetOverlappedResult\n");
653 success
= GetOverlappedResult(hnp
, &oOverlap
, &readden
, TRUE
);
656 trace("Server done reading.\n");
657 ok(success
, "overlapped ReadFile\n");
659 trace("Server writing...\n");
660 success
= WriteFile(hnp
, buf
, readden
, &written
, &oOverlap
);
661 trace("Server WriteFile returned...\n");
662 err
= GetLastError();
663 ok(success
|| err
== ERROR_IO_PENDING
, "overlapped WriteFile\n");
664 trace("overlapped WriteFile returned.\n");
665 if (!success
&& (err
== ERROR_IO_PENDING
)) {
670 ret
= WaitForSingleObjectEx(hEvent
, INFINITE
, TRUE
);
671 } while (ret
== WAIT_IO_COMPLETION
);
672 ok(ret
== 0, "wait WriteFile returned %x\n", ret
);
674 success
= GetOverlappedResult(hnp
, &oOverlap
, &written
, letGORwait
);
675 if (!letGORwait
&& !letWFSOEwait
&& !success
) {
676 ok(GetLastError() == ERROR_IO_INCOMPLETE
, "GetOverlappedResult\n");
677 success
= GetOverlappedResult(hnp
, &oOverlap
, &written
, TRUE
);
680 trace("Server done writing.\n");
681 ok(success
, "overlapped WriteFile\n");
682 ok(written
== readden
, "write file len\n");
684 /* finish this connection, wait for next one */
685 ok(FlushFileBuffers(hnp
), "FlushFileBuffers\n");
686 ok(DisconnectNamedPipe(hnp
), "DisconnectNamedPipe\n");
691 /** Trivial byte echo server - uses i/o completion ports */
692 static DWORD CALLBACK
serverThreadMain4(LPVOID arg
)
698 trace("serverThreadMain4\n");
699 /* Set up a simple echo server */
700 hnp
= CreateNamedPipe(PIPENAME
"serverThreadMain4", PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
701 PIPE_TYPE_BYTE
| PIPE_WAIT
,
702 /* nMaxInstances */ 1,
703 /* nOutBufSize */ 1024,
704 /* nInBufSize */ 1024,
705 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
706 /* lpSecurityAttrib */ NULL
);
707 ok(hnp
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
709 hcompletion
= CreateIoCompletionPort(hnp
, NULL
, 12345, 1);
710 ok(hcompletion
!= NULL
, "CreateIoCompletionPort failed, error=%i\n", GetLastError());
712 for (i
= 0; i
< NB_SERVER_LOOPS
; i
++) {
725 memset(&oConnect
, 0, sizeof(oConnect
));
726 memset(&oRead
, 0, sizeof(oRead
));
727 memset(&oWrite
, 0, sizeof(oWrite
));
729 /* Wait for client to connect */
730 trace("Server calling overlapped ConnectNamedPipe...\n");
731 success
= ConnectNamedPipe(hnp
, &oConnect
);
732 err
= GetLastError();
733 ok(!success
&& (err
== ERROR_IO_PENDING
|| err
== ERROR_PIPE_CONNECTED
),
734 "overlapped ConnectNamedPipe got %u err %u\n", success
, err
);
735 if (!success
&& err
== ERROR_IO_PENDING
) {
736 trace("ConnectNamedPipe GetQueuedCompletionStatus\n");
737 success
= GetQueuedCompletionStatus(hcompletion
, &dummy
, &compkey
, &oResult
, 0);
740 ok( GetLastError() == WAIT_TIMEOUT
,
741 "ConnectNamedPipe GetQueuedCompletionStatus wrong error %u\n", GetLastError());
742 success
= GetQueuedCompletionStatus(hcompletion
, &dummy
, &compkey
, &oResult
, 10000);
744 ok(success
, "ConnectNamedPipe GetQueuedCompletionStatus failed, errno=%i\n", GetLastError());
747 ok(compkey
== 12345, "got completion key %i instead of 12345\n", (int)compkey
);
748 ok(oResult
== &oConnect
, "got overlapped pointer %p instead of %p\n", oResult
, &oConnect
);
751 trace("overlapped ConnectNamedPipe operation complete.\n");
753 /* Echo bytes once */
754 memset(buf
, 0, sizeof(buf
));
756 trace("Server reading...\n");
757 success
= ReadFile(hnp
, buf
, sizeof(buf
), &readden
, &oRead
);
758 trace("Server ReadFile returned...\n");
759 err
= GetLastError();
760 ok(success
|| err
== ERROR_IO_PENDING
, "overlapped ReadFile, err=%i\n", err
);
761 success
= GetQueuedCompletionStatus(hcompletion
, &readden
, &compkey
,
763 ok(success
, "ReadFile GetQueuedCompletionStatus failed, errno=%i\n", GetLastError());
766 ok(compkey
== 12345, "got completion key %i instead of 12345\n", (int)compkey
);
767 ok(oResult
== &oRead
, "got overlapped pointer %p instead of %p\n", oResult
, &oRead
);
769 trace("Server done reading.\n");
771 trace("Server writing...\n");
772 success
= WriteFile(hnp
, buf
, readden
, &written
, &oWrite
);
773 trace("Server WriteFile returned...\n");
774 err
= GetLastError();
775 ok(success
|| err
== ERROR_IO_PENDING
, "overlapped WriteFile failed, err=%u\n", err
);
776 success
= GetQueuedCompletionStatus(hcompletion
, &written
, &compkey
,
778 ok(success
, "WriteFile GetQueuedCompletionStatus failed, errno=%i\n", GetLastError());
781 ok(compkey
== 12345, "got completion key %i instead of 12345\n", (int)compkey
);
782 ok(oResult
== &oWrite
, "got overlapped pointer %p instead of %p\n", oResult
, &oWrite
);
783 ok(written
== readden
, "write file len\n");
785 trace("Server done writing.\n");
787 /* finish this connection, wait for next one */
788 ok(FlushFileBuffers(hnp
), "FlushFileBuffers\n");
789 success
= DisconnectNamedPipe(hnp
);
790 ok(success
, "DisconnectNamedPipe failed, err %u\n", GetLastError());
793 ret
= CloseHandle(hnp
);
794 ok(ret
, "CloseHandle named pipe failed, err=%i\n", GetLastError());
795 ret
= CloseHandle(hcompletion
);
796 ok(ret
, "CloseHandle completion failed, err=%i\n", GetLastError());
801 static void exercizeServer(const char *pipename
, HANDLE serverThread
)
805 trace("exercizeServer starting\n");
806 for (i
= 0; i
< NB_SERVER_LOOPS
; i
++) {
807 HANDLE hFile
=INVALID_HANDLE_VALUE
;
808 static const char obuf
[] = "Bit Bucket";
814 for (loop
= 0; loop
< 3; loop
++) {
816 trace("Client connecting...\n");
817 /* Connect to the server */
818 hFile
= CreateFileA(pipename
, GENERIC_READ
| GENERIC_WRITE
, 0,
819 NULL
, OPEN_EXISTING
, 0, 0);
820 if (hFile
!= INVALID_HANDLE_VALUE
)
822 err
= GetLastError();
824 ok(err
== ERROR_PIPE_BUSY
|| err
== ERROR_FILE_NOT_FOUND
, "connecting to pipe\n");
826 ok(err
== ERROR_PIPE_BUSY
, "connecting to pipe\n");
827 trace("connect failed, retrying\n");
830 ok(hFile
!= INVALID_HANDLE_VALUE
, "client opening named pipe\n");
832 /* Make sure it can echo */
833 memset(ibuf
, 0, sizeof(ibuf
));
834 trace("Client writing...\n");
835 ok(WriteFile(hFile
, obuf
, sizeof(obuf
), &written
, NULL
), "WriteFile to client end of pipe\n");
836 ok(written
== sizeof(obuf
), "write file len\n");
837 trace("Client reading...\n");
838 ok(ReadFile(hFile
, ibuf
, sizeof(obuf
), &readden
, NULL
), "ReadFile from client end of pipe\n");
839 ok(readden
== sizeof(obuf
), "read file len\n");
840 ok(memcmp(obuf
, ibuf
, written
) == 0, "content check\n");
842 trace("Client closing...\n");
843 ok(CloseHandle(hFile
), "CloseHandle\n");
846 ok(WaitForSingleObject(serverThread
,INFINITE
) == WAIT_OBJECT_0
, "WaitForSingleObject\n");
848 trace("exercizeServer returning\n");
851 static void test_NamedPipe_2(void)
854 DWORD serverThreadId
;
858 trace("test_NamedPipe_2 starting\n");
859 /* Set up a twenty second timeout */
860 alarm_event
= CreateEvent( NULL
, TRUE
, FALSE
, NULL
);
861 SetLastError(0xdeadbeef);
862 alarmThread
= CreateThread(NULL
, 0, alarmThreadMain
, (void *) 20000, 0, &alarmThreadId
);
863 ok(alarmThread
!= NULL
, "CreateThread failed: %d\n", GetLastError());
865 /* The servers we're about to exercise do try to clean up carefully,
866 * but to reduce the chance of a test failure due to a pipe handle
867 * leak in the test code, we'll use a different pipe name for each server.
871 SetLastError(0xdeadbeef);
872 serverThread
= CreateThread(NULL
, 0, serverThreadMain1
, (void *)8, 0, &serverThreadId
);
873 ok(serverThread
!= NULL
, "CreateThread failed: %d\n", GetLastError());
874 exercizeServer(PIPENAME
"serverThreadMain1", serverThread
);
877 SetLastError(0xdeadbeef);
878 serverThread
= CreateThread(NULL
, 0, serverThreadMain2
, 0, 0, &serverThreadId
);
879 ok(serverThread
!= NULL
, "CreateThread failed: %d\n", GetLastError());
880 exercizeServer(PIPENAME
"serverThreadMain2", serverThread
);
883 SetLastError(0xdeadbeef);
884 serverThread
= CreateThread(NULL
, 0, serverThreadMain3
, 0, 0, &serverThreadId
);
885 ok(serverThread
!= NULL
, "CreateThread failed: %d\n", GetLastError());
886 exercizeServer(PIPENAME
"serverThreadMain3", serverThread
);
889 SetLastError(0xdeadbeef);
890 serverThread
= CreateThread(NULL
, 0, serverThreadMain4
, 0, 0, &serverThreadId
);
891 ok(serverThread
!= NULL
, "CreateThread failed: %d\n", GetLastError());
892 exercizeServer(PIPENAME
"serverThreadMain4", serverThread
);
894 ok(SetEvent( alarm_event
), "SetEvent\n");
895 CloseHandle( alarm_event
);
896 trace("test_NamedPipe_2 returning\n");
899 static int test_DisconnectNamedPipe(void)
903 static const char obuf
[] = "Bit Bucket";
909 SetLastError(0xdeadbeef);
910 hnp
= CreateNamedPipe(PIPENAME
, PIPE_ACCESS_DUPLEX
, PIPE_TYPE_BYTE
| PIPE_WAIT
,
911 /* nMaxInstances */ 1,
912 /* nOutBufSize */ 1024,
913 /* nInBufSize */ 1024,
914 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
915 /* lpSecurityAttrib */ NULL
);
916 if ((hnp
== INVALID_HANDLE_VALUE
/* Win98 */ || !hnp
/* Win95 */)
917 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
) {
919 win_skip("Named pipes are not implemented\n");
923 ok(WriteFile(hnp
, obuf
, sizeof(obuf
), &written
, NULL
) == 0
924 && GetLastError() == ERROR_PIPE_LISTENING
, "WriteFile to not-yet-connected pipe\n");
925 ok(ReadFile(hnp
, ibuf
, sizeof(ibuf
), &readden
, NULL
) == 0
926 && GetLastError() == ERROR_PIPE_LISTENING
, "ReadFile from not-yet-connected pipe\n");
928 hFile
= CreateFileA(PIPENAME
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0);
929 ok(hFile
!= INVALID_HANDLE_VALUE
, "CreateFile failed\n");
931 /* don't try to do i/o if one side couldn't be opened, as it hangs */
932 if (hFile
!= INVALID_HANDLE_VALUE
) {
934 /* see what happens if server calls DisconnectNamedPipe
935 * when there are bytes in the pipe
938 ok(WriteFile(hFile
, obuf
, sizeof(obuf
), &written
, NULL
), "WriteFile\n");
939 ok(written
== sizeof(obuf
), "write file len\n");
940 ok(DisconnectNamedPipe(hnp
), "DisconnectNamedPipe while messages waiting\n");
941 ok(WriteFile(hFile
, obuf
, sizeof(obuf
), &written
, NULL
) == 0
942 && GetLastError() == ERROR_PIPE_NOT_CONNECTED
, "WriteFile to disconnected pipe\n");
943 ok(ReadFile(hnp
, ibuf
, sizeof(ibuf
), &readden
, NULL
) == 0
944 && GetLastError() == ERROR_PIPE_NOT_CONNECTED
,
945 "ReadFile from disconnected pipe with bytes waiting\n");
946 ok(!DisconnectNamedPipe(hnp
) && GetLastError() == ERROR_PIPE_NOT_CONNECTED
,
947 "DisconnectNamedPipe worked twice\n");
948 ret
= WaitForSingleObject(hFile
, 0);
949 ok(ret
== WAIT_TIMEOUT
, "WaitForSingleObject returned %X\n", ret
);
950 ok(CloseHandle(hFile
), "CloseHandle\n");
953 ok(CloseHandle(hnp
), "CloseHandle\n");
957 static void test_CreatePipe(void)
959 SECURITY_ATTRIBUTES pipe_attr
;
960 HANDLE piperead
, pipewrite
;
967 user_apc_ran
= FALSE
;
969 ok(pQueueUserAPC(user_apc
, GetCurrentThread(), 0), "couldn't create user apc\n");
971 pipe_attr
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
972 pipe_attr
.bInheritHandle
= TRUE
;
973 pipe_attr
.lpSecurityDescriptor
= NULL
;
974 ok(CreatePipe(&piperead
, &pipewrite
, &pipe_attr
, 0) != 0, "CreatePipe failed\n");
975 ok(WriteFile(pipewrite
,PIPENAME
,sizeof(PIPENAME
), &written
, NULL
), "Write to anonymous pipe failed\n");
976 ok(written
== sizeof(PIPENAME
), "Write to anonymous pipe wrote %d bytes\n", written
);
977 ok(ReadFile(piperead
,readbuf
,sizeof(readbuf
),&read
, NULL
), "Read from non empty pipe failed\n");
978 ok(read
== sizeof(PIPENAME
), "Read from anonymous pipe got %d bytes\n", read
);
979 ok(CloseHandle(pipewrite
), "CloseHandle for the write pipe failed\n");
980 ok(CloseHandle(piperead
), "CloseHandle for the read pipe failed\n");
982 /* Now write another chunk*/
983 ok(CreatePipe(&piperead
, &pipewrite
, &pipe_attr
, 0) != 0, "CreatePipe failed\n");
984 ok(WriteFile(pipewrite
,PIPENAME
,sizeof(PIPENAME
), &written
, NULL
), "Write to anonymous pipe failed\n");
985 ok(written
== sizeof(PIPENAME
), "Write to anonymous pipe wrote %d bytes\n", written
);
986 /* and close the write end, read should still succeed*/
987 ok(CloseHandle(pipewrite
), "CloseHandle for the Write Pipe failed\n");
988 ok(ReadFile(piperead
,readbuf
,sizeof(readbuf
),&read
, NULL
), "Read from broken pipe withe with pending data failed\n");
989 ok(read
== sizeof(PIPENAME
), "Read from anonymous pipe got %d bytes\n", read
);
990 /* But now we need to get informed that the pipe is closed */
991 ok(ReadFile(piperead
,readbuf
,sizeof(readbuf
),&read
, NULL
) == 0, "Broken pipe not detected\n");
992 ok(CloseHandle(piperead
), "CloseHandle for the read pipe failed\n");
994 /* Try bigger chunks */
996 buffer
= HeapAlloc( GetProcessHeap(), 0, size
);
997 for (i
= 0; i
< size
; i
++) buffer
[i
] = i
;
998 ok(CreatePipe(&piperead
, &pipewrite
, &pipe_attr
, (size
+ 24)) != 0, "CreatePipe failed\n");
999 ok(WriteFile(pipewrite
, buffer
, size
, &written
, NULL
), "Write to anonymous pipe failed\n");
1000 ok(written
== size
, "Write to anonymous pipe wrote %d bytes\n", written
);
1001 /* and close the write end, read should still succeed*/
1002 ok(CloseHandle(pipewrite
), "CloseHandle for the Write Pipe failed\n");
1003 memset( buffer
, 0, size
);
1004 ok(ReadFile(piperead
, buffer
, size
, &read
, NULL
), "Read from broken pipe withe with pending data failed\n");
1005 ok(read
== size
, "Read from anonymous pipe got %d bytes\n", read
);
1006 for (i
= 0; i
< size
; i
++) ok( buffer
[i
] == (BYTE
)i
, "invalid data %x at %x\n", buffer
[i
], i
);
1007 /* But now we need to get informed that the pipe is closed */
1008 ok(ReadFile(piperead
,readbuf
,sizeof(readbuf
),&read
, NULL
) == 0, "Broken pipe not detected\n");
1009 ok(CloseHandle(piperead
), "CloseHandle for the read pipe failed\n");
1010 HeapFree(GetProcessHeap(), 0, buffer
);
1012 ok(user_apc_ran
== FALSE
, "user apc ran, pipe using alertable io mode\n");
1013 SleepEx(0, TRUE
); /* get rid of apc */
1016 struct named_pipe_client_params
1018 DWORD security_flags
;
1023 #define PIPE_NAME "\\\\.\\pipe\\named_pipe_test"
1025 static DWORD CALLBACK
named_pipe_client_func(LPVOID p
)
1027 struct named_pipe_client_params
*params
= p
;
1030 const char message
[] = "Test";
1031 DWORD bytes_read
, bytes_written
;
1033 TOKEN_PRIVILEGES
*Privileges
= NULL
;
1039 /* modify the token so we can tell if the pipe impersonation
1040 * token reverts to the process token */
1041 ret
= AdjustTokenPrivileges(params
->token
, TRUE
, NULL
, 0, NULL
, NULL
);
1042 ok(ret
, "AdjustTokenPrivileges failed with error %d\n", GetLastError());
1044 ret
= SetThreadToken(NULL
, params
->token
);
1045 ok(ret
, "SetThreadToken failed with error %d\n", GetLastError());
1050 HANDLE process_token
;
1052 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY
|TOKEN_ADJUST_PRIVILEGES
, &process_token
);
1053 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
1055 ret
= GetTokenInformation(process_token
, TokenPrivileges
, NULL
, 0, &Size
);
1056 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "GetTokenInformation(TokenPrivileges) failed with %d\n", GetLastError());
1057 Privileges
= HeapAlloc(GetProcessHeap(), 0, Size
);
1058 ret
= GetTokenInformation(process_token
, TokenPrivileges
, Privileges
, Size
, &Size
);
1059 ok(ret
, "GetTokenInformation(TokenPrivileges) failed with %d\n", GetLastError());
1061 ret
= AdjustTokenPrivileges(process_token
, TRUE
, NULL
, 0, NULL
, NULL
);
1062 ok(ret
, "AdjustTokenPrivileges failed with error %d\n", GetLastError());
1064 CloseHandle(process_token
);
1067 pipe
= CreateFile(PIPE_NAME
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, params
->security_flags
, NULL
);
1068 ok(pipe
!= INVALID_HANDLE_VALUE
, "CreateFile for pipe failed with error %d\n", GetLastError());
1070 ret
= WriteFile(pipe
, message
, sizeof(message
), &bytes_written
, NULL
);
1071 ok(ret
, "WriteFile failed with error %d\n", GetLastError());
1073 ret
= ReadFile(pipe
, &dummy
, sizeof(dummy
), &bytes_read
, NULL
);
1074 ok(ret
, "ReadFile failed with error %d\n", GetLastError());
1080 ret
= RevertToSelf();
1081 ok(ret
, "RevertToSelf failed with error %d\n", GetLastError());
1085 ret
= AdjustTokenPrivileges(params
->token
, TRUE
, NULL
, 0, NULL
, NULL
);
1086 ok(ret
, "AdjustTokenPrivileges failed with error %d\n", GetLastError());
1091 HANDLE process_token
;
1093 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES
, &process_token
);
1094 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
1096 ret
= AdjustTokenPrivileges(process_token
, FALSE
, Privileges
, 0, NULL
, NULL
);
1097 ok(ret
, "AdjustTokenPrivileges failed with error %d\n", GetLastError());
1099 HeapFree(GetProcessHeap(), 0, Privileges
);
1101 CloseHandle(process_token
);
1104 ret
= WriteFile(pipe
, message
, sizeof(message
), &bytes_written
, NULL
);
1105 ok(ret
, "WriteFile failed with error %d\n", GetLastError());
1107 ret
= ReadFile(pipe
, &dummy
, sizeof(dummy
), &bytes_read
, NULL
);
1108 ok(ret
, "ReadFile failed with error %d\n", GetLastError());
1115 static HANDLE
make_impersonation_token(DWORD Access
, SECURITY_IMPERSONATION_LEVEL ImpersonationLevel
)
1117 HANDLE ProcessToken
;
1118 HANDLE Token
= NULL
;
1121 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE
, &ProcessToken
);
1122 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
1124 ret
= pDuplicateTokenEx(ProcessToken
, Access
, NULL
, ImpersonationLevel
, TokenImpersonation
, &Token
);
1125 ok(ret
, "DuplicateToken failed with error %d\n", GetLastError());
1127 CloseHandle(ProcessToken
);
1132 static void test_ImpersonateNamedPipeClient(HANDLE hClientToken
, DWORD security_flags
, BOOL revert
, void (*test_func
)(int, HANDLE
))
1141 struct named_pipe_client_params params
;
1143 DWORD dwBytesWritten
;
1144 HANDLE hToken
= NULL
;
1145 SECURITY_IMPERSONATION_LEVEL ImpersonationLevel
;
1148 hPipeServer
= CreateNamedPipe(PIPE_NAME
, PIPE_ACCESS_DUPLEX
, PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
| PIPE_WAIT
, 1, 100, 100, NMPWAIT_USE_DEFAULT_WAIT
, NULL
);
1149 ok(hPipeServer
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed with error %d\n", GetLastError());
1151 params
.security_flags
= security_flags
;
1152 params
.token
= hClientToken
;
1153 params
.revert
= revert
;
1154 hThread
= CreateThread(NULL
, 0, named_pipe_client_func
, ¶ms
, 0, &dwTid
);
1155 ok(hThread
!= NULL
, "CreateThread failed with error %d\n", GetLastError());
1157 SetLastError(0xdeadbeef);
1158 ret
= ImpersonateNamedPipeClient(hPipeServer
);
1159 error
= GetLastError();
1160 ok(ret
/* win2k3 */ || (error
== ERROR_CANNOT_IMPERSONATE
),
1161 "ImpersonateNamedPipeClient should have failed with ERROR_CANNOT_IMPERSONATE instead of %d\n", GetLastError());
1163 ret
= ConnectNamedPipe(hPipeServer
, NULL
);
1164 ok(ret
|| (GetLastError() == ERROR_PIPE_CONNECTED
), "ConnectNamedPipe failed with error %d\n", GetLastError());
1166 ret
= ReadFile(hPipeServer
, buffer
, sizeof(buffer
), &dwBytesRead
, NULL
);
1167 ok(ret
, "ReadFile failed with error %d\n", GetLastError());
1169 ret
= ImpersonateNamedPipeClient(hPipeServer
);
1170 ok(ret
, "ImpersonateNamedPipeClient failed with error %d\n", GetLastError());
1172 ret
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
, FALSE
, &hToken
);
1173 ok(ret
, "OpenThreadToken failed with error %d\n", GetLastError());
1175 (*test_func
)(0, hToken
);
1177 ImpersonationLevel
= 0xdeadbeef; /* to avoid false positives */
1178 ret
= GetTokenInformation(hToken
, TokenImpersonationLevel
, &ImpersonationLevel
, sizeof(ImpersonationLevel
), &size
);
1179 ok(ret
, "GetTokenInformation(TokenImpersonationLevel) failed with error %d\n", GetLastError());
1180 ok(ImpersonationLevel
== SecurityImpersonation
, "ImpersonationLevel should have been SecurityImpersonation(%d) instead of %d\n", SecurityImpersonation
, ImpersonationLevel
);
1182 CloseHandle(hToken
);
1186 ret
= WriteFile(hPipeServer
, &dummy
, sizeof(dummy
), &dwBytesWritten
, NULL
);
1187 ok(ret
, "WriteFile failed with error %d\n", GetLastError());
1189 ret
= ReadFile(hPipeServer
, buffer
, sizeof(buffer
), &dwBytesRead
, NULL
);
1190 ok(ret
, "ReadFile failed with error %d\n", GetLastError());
1192 ret
= ImpersonateNamedPipeClient(hPipeServer
);
1193 ok(ret
, "ImpersonateNamedPipeClient failed with error %d\n", GetLastError());
1195 ret
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
, FALSE
, &hToken
);
1196 ok(ret
, "OpenThreadToken failed with error %d\n", GetLastError());
1198 (*test_func
)(1, hToken
);
1200 CloseHandle(hToken
);
1204 ret
= WriteFile(hPipeServer
, &dummy
, sizeof(dummy
), &dwBytesWritten
, NULL
);
1205 ok(ret
, "WriteFile failed with error %d\n", GetLastError());
1207 WaitForSingleObject(hThread
, INFINITE
);
1209 ret
= ImpersonateNamedPipeClient(hPipeServer
);
1210 ok(ret
, "ImpersonateNamedPipeClient failed with error %d\n", GetLastError());
1214 CloseHandle(hThread
);
1215 CloseHandle(hPipeServer
);
1218 static BOOL
are_all_privileges_disabled(HANDLE hToken
)
1221 TOKEN_PRIVILEGES
*Privileges
= NULL
;
1223 BOOL all_privs_disabled
= TRUE
;
1226 ret
= GetTokenInformation(hToken
, TokenPrivileges
, NULL
, 0, &Size
);
1227 if (!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
1229 Privileges
= HeapAlloc(GetProcessHeap(), 0, Size
);
1230 ret
= GetTokenInformation(hToken
, TokenPrivileges
, Privileges
, Size
, &Size
);
1233 HeapFree(GetProcessHeap(), 0, Privileges
);
1240 for (i
= 0; i
< Privileges
->PrivilegeCount
; i
++)
1242 if (Privileges
->Privileges
[i
].Attributes
& SE_PRIVILEGE_ENABLED
)
1244 all_privs_disabled
= FALSE
;
1249 HeapFree(GetProcessHeap(), 0, Privileges
);
1251 return all_privs_disabled
;
1254 static DWORD
get_privilege_count(HANDLE hToken
)
1256 TOKEN_STATISTICS Statistics
;
1257 DWORD Size
= sizeof(Statistics
);
1260 ret
= GetTokenInformation(hToken
, TokenStatistics
, &Statistics
, Size
, &Size
);
1261 ok(ret
, "GetTokenInformation(TokenStatistics)\n");
1262 if (!ret
) return -1;
1264 return Statistics
.PrivilegeCount
;
1267 static void test_no_sqos_no_token(int call_index
, HANDLE hToken
)
1274 priv_count
= get_privilege_count(hToken
);
1276 ok(priv_count
== 0, "privilege count should have been 0 instead of %d\n", priv_count
);
1279 priv_count
= get_privilege_count(hToken
);
1280 ok(priv_count
> 0, "privilege count should now be > 0 instead of 0\n");
1281 ok(!are_all_privileges_disabled(hToken
), "impersonated token should not have been modified\n");
1284 ok(0, "shouldn't happen\n");
1288 static void test_no_sqos(int call_index
, HANDLE hToken
)
1293 ok(!are_all_privileges_disabled(hToken
), "token should be a copy of the process one\n");
1297 ok(are_all_privileges_disabled(hToken
), "impersonated token should have been modified\n");
1300 ok(0, "shouldn't happen\n");
1304 static void test_static_context(int call_index
, HANDLE hToken
)
1309 ok(!are_all_privileges_disabled(hToken
), "token should be a copy of the process one\n");
1312 ok(!are_all_privileges_disabled(hToken
), "impersonated token should not have been modified\n");
1315 ok(0, "shouldn't happen\n");
1319 static void test_dynamic_context(int call_index
, HANDLE hToken
)
1324 ok(!are_all_privileges_disabled(hToken
), "token should be a copy of the process one\n");
1328 ok(are_all_privileges_disabled(hToken
), "impersonated token should have been modified\n");
1331 ok(0, "shouldn't happen\n");
1335 static void test_dynamic_context_no_token(int call_index
, HANDLE hToken
)
1340 ok(are_all_privileges_disabled(hToken
), "token should be a copy of the process one\n");
1343 ok(!are_all_privileges_disabled(hToken
), "process token modification should have been detected and impersonation token updated\n");
1346 ok(0, "shouldn't happen\n");
1350 static void test_no_sqos_revert(int call_index
, HANDLE hToken
)
1356 priv_count
= get_privilege_count(hToken
);
1358 ok(priv_count
== 0, "privilege count should have been 0 instead of %d\n", priv_count
);
1361 priv_count
= get_privilege_count(hToken
);
1362 ok(priv_count
> 0, "privilege count should now be > 0 instead of 0\n");
1363 ok(!are_all_privileges_disabled(hToken
), "impersonated token should not have been modified\n");
1366 ok(0, "shouldn't happen\n");
1370 static void test_static_context_revert(int call_index
, HANDLE hToken
)
1376 ok(are_all_privileges_disabled(hToken
), "privileges should have been disabled\n");
1380 ok(are_all_privileges_disabled(hToken
), "impersonated token should not have been modified\n");
1383 ok(0, "shouldn't happen\n");
1387 static void test_dynamic_context_revert(int call_index
, HANDLE hToken
)
1393 ok(are_all_privileges_disabled(hToken
), "privileges should have been disabled\n");
1396 ok(!are_all_privileges_disabled(hToken
), "impersonated token should now be process token\n");
1399 ok(0, "shouldn't happen\n");
1403 static void test_impersonation(void)
1405 HANDLE hClientToken
;
1406 HANDLE hProcessToken
;
1409 if( !pDuplicateTokenEx
) {
1410 skip("DuplicateTokenEx not found\n");
1414 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY
, &hProcessToken
);
1417 skip("couldn't open process token, skipping impersonation tests\n");
1421 if (!get_privilege_count(hProcessToken
) || are_all_privileges_disabled(hProcessToken
))
1423 skip("token didn't have any privileges or they were all disabled. token not suitable for impersonation tests\n");
1424 CloseHandle(hProcessToken
);
1427 CloseHandle(hProcessToken
);
1429 test_ImpersonateNamedPipeClient(NULL
, 0, FALSE
, test_no_sqos_no_token
);
1430 hClientToken
= make_impersonation_token(TOKEN_IMPERSONATE
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY
, SecurityImpersonation
);
1431 test_ImpersonateNamedPipeClient(hClientToken
, 0, FALSE
, test_no_sqos
);
1432 CloseHandle(hClientToken
);
1433 hClientToken
= make_impersonation_token(TOKEN_IMPERSONATE
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY
, SecurityImpersonation
);
1434 test_ImpersonateNamedPipeClient(hClientToken
,
1435 SECURITY_SQOS_PRESENT
| SECURITY_IMPERSONATION
, FALSE
,
1436 test_static_context
);
1437 CloseHandle(hClientToken
);
1438 hClientToken
= make_impersonation_token(TOKEN_IMPERSONATE
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY
, SecurityImpersonation
);
1439 test_ImpersonateNamedPipeClient(hClientToken
,
1440 SECURITY_SQOS_PRESENT
| SECURITY_CONTEXT_TRACKING
| SECURITY_IMPERSONATION
,
1441 FALSE
, test_dynamic_context
);
1442 CloseHandle(hClientToken
);
1443 test_ImpersonateNamedPipeClient(NULL
,
1444 SECURITY_SQOS_PRESENT
| SECURITY_CONTEXT_TRACKING
| SECURITY_IMPERSONATION
,
1445 FALSE
, test_dynamic_context_no_token
);
1447 hClientToken
= make_impersonation_token(TOKEN_IMPERSONATE
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY
, SecurityImpersonation
);
1448 test_ImpersonateNamedPipeClient(hClientToken
, 0, TRUE
, test_no_sqos_revert
);
1449 CloseHandle(hClientToken
);
1450 hClientToken
= make_impersonation_token(TOKEN_IMPERSONATE
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY
, SecurityImpersonation
);
1451 test_ImpersonateNamedPipeClient(hClientToken
,
1452 SECURITY_SQOS_PRESENT
| SECURITY_IMPERSONATION
, TRUE
,
1453 test_static_context_revert
);
1454 CloseHandle(hClientToken
);
1455 hClientToken
= make_impersonation_token(TOKEN_IMPERSONATE
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY
, SecurityImpersonation
);
1456 test_ImpersonateNamedPipeClient(hClientToken
,
1457 SECURITY_SQOS_PRESENT
| SECURITY_CONTEXT_TRACKING
| SECURITY_IMPERSONATION
,
1458 TRUE
, test_dynamic_context_revert
);
1459 CloseHandle(hClientToken
);
1462 struct overlapped_server_args
1464 HANDLE pipe_created
;
1467 static DWORD CALLBACK
overlapped_server(LPVOID arg
)
1472 struct overlapped_server_args
*a
= arg
;
1476 pipe
= CreateNamedPipeA("\\\\.\\pipe\\my pipe", FILE_FLAG_OVERLAPPED
| PIPE_ACCESS_DUPLEX
, PIPE_TYPE_MESSAGE
| PIPE_READMODE_MESSAGE
, 1, 0, 0, 100000, NULL
);
1477 ok(pipe
!= NULL
, "pipe NULL\n");
1479 ol
.hEvent
= CreateEventA(0, 1, 0, 0);
1480 ok(ol
.hEvent
!= NULL
, "event NULL\n");
1481 ret
= ConnectNamedPipe(pipe
, &ol
);
1482 err
= GetLastError();
1483 ok(ret
== 0, "ret %d\n", ret
);
1484 ok(err
== ERROR_IO_PENDING
, "gle %d\n", err
);
1485 SetEvent(a
->pipe_created
);
1487 ret
= WaitForSingleObjectEx(ol
.hEvent
, INFINITE
, 1);
1488 ok(ret
== WAIT_OBJECT_0
, "ret %x\n", ret
);
1490 ret
= GetOverlappedResult(pipe
, &ol
, &num
, 1);
1491 ok(ret
== 1, "ret %d\n", ret
);
1493 /* This should block */
1494 ret
= ReadFile(pipe
, buf
, sizeof(buf
), &num
, NULL
);
1495 ok(ret
== 1, "ret %d\n", ret
);
1497 DisconnectNamedPipe(pipe
);
1498 CloseHandle(ol
.hEvent
);
1503 static void test_overlapped(void)
1506 HANDLE thread
, pipe
;
1508 struct overlapped_server_args args
;
1510 args
.pipe_created
= CreateEventA(0, 1, 0, 0);
1511 thread
= CreateThread(NULL
, 0, overlapped_server
, &args
, 0, &tid
);
1513 WaitForSingleObject(args
.pipe_created
, INFINITE
);
1514 pipe
= CreateFileA("\\\\.\\pipe\\my pipe", GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, NULL
);
1515 ok(pipe
!= INVALID_HANDLE_VALUE
, "cf failed\n");
1517 /* Sleep to try to get the ReadFile in the server to occur before the following WriteFile */
1520 ret
= WriteFile(pipe
, "x", 1, &num
, NULL
);
1521 ok(ret
, "WriteFile failed with error %d\n", GetLastError());
1523 WaitForSingleObject(thread
, INFINITE
);
1525 CloseHandle(args
.pipe_created
);
1526 CloseHandle(thread
);
1529 static void test_NamedPipeHandleState(void)
1531 HANDLE server
, client
;
1533 DWORD state
, instances
, maxCollectionCount
, collectDataTimeout
;
1534 char userName
[MAX_PATH
];
1536 server
= CreateNamedPipe(PIPENAME
, PIPE_ACCESS_DUPLEX
,
1537 /* dwOpenMode */ PIPE_TYPE_BYTE
| PIPE_WAIT
,
1538 /* nMaxInstances */ 1,
1539 /* nOutBufSize */ 1024,
1540 /* nInBufSize */ 1024,
1541 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
1542 /* lpSecurityAttrib */ NULL
);
1543 ok(server
!= INVALID_HANDLE_VALUE
, "cf failed\n");
1544 ret
= GetNamedPipeHandleState(server
, NULL
, NULL
, NULL
, NULL
, NULL
, 0);
1546 ok(ret
, "GetNamedPipeHandleState failed: %d\n", GetLastError());
1547 ret
= GetNamedPipeHandleState(server
, &state
, &instances
, NULL
, NULL
, NULL
,
1550 ok(ret
, "GetNamedPipeHandleState failed: %d\n", GetLastError());
1553 ok(state
== 0, "unexpected state %08x\n", state
);
1554 ok(instances
== 1, "expected 1 instances, got %d\n", instances
);
1556 /* Some parameters have no meaning, and therefore can't be retrieved,
1559 SetLastError(0xdeadbeef);
1560 ret
= GetNamedPipeHandleState(server
, &state
, &instances
,
1561 &maxCollectionCount
, &collectDataTimeout
, userName
,
1562 sizeof(userName
) / sizeof(userName
[0]));
1564 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
1565 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1566 /* A byte-mode pipe server can't be changed to message mode. */
1567 state
= PIPE_READMODE_MESSAGE
;
1568 SetLastError(0xdeadbeef);
1569 ret
= SetNamedPipeHandleState(server
, &state
, NULL
, NULL
);
1571 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
1572 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1574 client
= CreateFileA(PIPENAME
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1575 OPEN_EXISTING
, 0, NULL
);
1576 ok(client
!= INVALID_HANDLE_VALUE
, "cf failed\n");
1578 state
= PIPE_READMODE_BYTE
;
1579 ret
= SetNamedPipeHandleState(client
, &state
, NULL
, NULL
);
1581 ok(ret
, "SetNamedPipeHandleState failed: %d\n", GetLastError());
1582 /* A byte-mode pipe client can't be changed to message mode, either. */
1583 state
= PIPE_READMODE_MESSAGE
;
1584 SetLastError(0xdeadbeef);
1585 ret
= SetNamedPipeHandleState(server
, &state
, NULL
, NULL
);
1587 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
1588 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1590 CloseHandle(client
);
1591 CloseHandle(server
);
1593 server
= CreateNamedPipe(PIPENAME
, PIPE_ACCESS_DUPLEX
,
1594 /* dwOpenMode */ PIPE_TYPE_MESSAGE
| PIPE_WAIT
,
1595 /* nMaxInstances */ 1,
1596 /* nOutBufSize */ 1024,
1597 /* nInBufSize */ 1024,
1598 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
1599 /* lpSecurityAttrib */ NULL
);
1600 ok(server
!= INVALID_HANDLE_VALUE
, "cf failed\n");
1601 ret
= GetNamedPipeHandleState(server
, NULL
, NULL
, NULL
, NULL
, NULL
, 0);
1603 ok(ret
, "GetNamedPipeHandleState failed: %d\n", GetLastError());
1604 ret
= GetNamedPipeHandleState(server
, &state
, &instances
, NULL
, NULL
, NULL
,
1607 ok(ret
, "GetNamedPipeHandleState failed: %d\n", GetLastError());
1610 ok(state
== 0, "unexpected state %08x\n", state
);
1611 ok(instances
== 1, "expected 1 instances, got %d\n", instances
);
1613 /* In contrast to byte-mode pipes, a message-mode pipe server can be
1614 * changed to byte mode.
1616 state
= PIPE_READMODE_BYTE
;
1617 ret
= SetNamedPipeHandleState(server
, &state
, NULL
, NULL
);
1619 ok(ret
, "SetNamedPipeHandleState failed: %d\n", GetLastError());
1621 client
= CreateFileA(PIPENAME
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1622 OPEN_EXISTING
, 0, NULL
);
1623 ok(client
!= INVALID_HANDLE_VALUE
, "cf failed\n");
1625 state
= PIPE_READMODE_MESSAGE
;
1626 ret
= SetNamedPipeHandleState(client
, &state
, NULL
, NULL
);
1628 ok(ret
, "SetNamedPipeHandleState failed: %d\n", GetLastError());
1629 /* A message-mode pipe client can also be changed to byte mode.
1631 state
= PIPE_READMODE_BYTE
;
1632 ret
= SetNamedPipeHandleState(client
, &state
, NULL
, NULL
);
1634 ok(ret
, "SetNamedPipeHandleState failed: %d\n", GetLastError());
1636 CloseHandle(client
);
1637 CloseHandle(server
);
1644 hmod
= GetModuleHandle("advapi32.dll");
1645 pDuplicateTokenEx
= (void *) GetProcAddress(hmod
, "DuplicateTokenEx");
1646 hmod
= GetModuleHandle("kernel32.dll");
1647 pQueueUserAPC
= (void *) GetProcAddress(hmod
, "QueueUserAPC");
1649 if (test_DisconnectNamedPipe())
1651 test_CreateNamedPipe_instances_must_match();
1653 test_CreateNamedPipe(PIPE_TYPE_BYTE
);
1654 test_CreateNamedPipe(PIPE_TYPE_MESSAGE
| PIPE_READMODE_MESSAGE
);
1656 test_impersonation();
1658 test_NamedPipeHandleState();