kernel32: Implement SetNamedPipeHandleState.
[wine/wine-gecko.git] / dlls / kernel32 / tests / pipe.c
blob457e62bd0036b51721fe192fa194196129ea671c
1 /*
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
21 #include <stdarg.h>
22 #include <stdio.h>
24 #include "ntstatus.h"
25 #define WIN32_NO_STATUS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winternl.h"
29 #include "wine/test.h"
31 #define PIPENAME "\\\\.\\PiPe\\tests_pipe.c"
33 #define NB_SERVER_LOOPS 8
35 static HANDLE alarm_event;
36 static BOOL (WINAPI *pDuplicateTokenEx)(HANDLE,DWORD,LPSECURITY_ATTRIBUTES,
37 SECURITY_IMPERSONATION_LEVEL,TOKEN_TYPE,PHANDLE);
38 static DWORD (WINAPI *pQueueUserAPC)(PAPCFUNC pfnAPC, HANDLE hThread, ULONG_PTR dwData);
40 static BOOL user_apc_ran;
41 static void CALLBACK user_apc(ULONG_PTR param)
43 user_apc_ran = TRUE;
46 static void test_CreateNamedPipe(int pipemode)
48 HANDLE hnp;
49 HANDLE hFile;
50 static const char obuf[] = "Bit Bucket";
51 static const char obuf2[] = "More bits";
52 char ibuf[32], *pbuf;
53 DWORD written;
54 DWORD readden;
55 DWORD avail;
56 DWORD lpmode;
57 BOOL ret;
59 if (pipemode == PIPE_TYPE_BYTE)
60 trace("test_CreateNamedPipe starting in byte mode\n");
61 else
62 trace("test_CreateNamedPipe starting in message mode\n");
64 /* Wait for non existing pipe */
65 ret = WaitNamedPipeA(PIPENAME, 2000);
66 ok(ret == 0, "WaitNamedPipe returned %d for non existing pipe\n", ret);
67 ok(GetLastError() == ERROR_FILE_NOT_FOUND, "wrong error %u\n", GetLastError());
69 /* Bad parameter checks */
70 hnp = CreateNamedPipeA("not a named pipe", PIPE_ACCESS_DUPLEX, pipemode | PIPE_WAIT,
71 /* nMaxInstances */ 1,
72 /* nOutBufSize */ 1024,
73 /* nInBufSize */ 1024,
74 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
75 /* lpSecurityAttrib */ NULL);
76 ok(hnp == INVALID_HANDLE_VALUE && GetLastError() == ERROR_INVALID_NAME,
77 "CreateNamedPipe should fail if name doesn't start with \\\\.\\pipe\n");
79 if (pipemode == PIPE_TYPE_BYTE)
81 /* Bad parameter checks */
82 hnp = CreateNamedPipeA(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_READMODE_MESSAGE,
83 /* nMaxInstances */ 1,
84 /* nOutBufSize */ 1024,
85 /* nInBufSize */ 1024,
86 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
87 /* lpSecurityAttrib */ NULL);
88 ok(hnp == INVALID_HANDLE_VALUE && GetLastError() == ERROR_INVALID_PARAMETER,
89 "CreateNamedPipe should fail with PIPE_TYPE_BYTE | PIPE_READMODE_MESSAGE\n");
92 hnp = CreateNamedPipeA(NULL,
93 PIPE_ACCESS_DUPLEX, pipemode | PIPE_WAIT,
94 1, 1024, 1024, NMPWAIT_USE_DEFAULT_WAIT, NULL);
95 ok(hnp == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
96 "CreateNamedPipe should fail if name is NULL\n");
98 hFile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
99 ok(hFile == INVALID_HANDLE_VALUE
100 && GetLastError() == ERROR_FILE_NOT_FOUND,
101 "connecting to nonexistent named pipe should fail with ERROR_FILE_NOT_FOUND\n");
103 /* Functional checks */
105 hnp = CreateNamedPipeA(PIPENAME, PIPE_ACCESS_DUPLEX, pipemode | PIPE_WAIT,
106 /* nMaxInstances */ 1,
107 /* nOutBufSize */ 1024,
108 /* nInBufSize */ 1024,
109 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
110 /* lpSecurityAttrib */ NULL);
111 ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
113 ret = WaitNamedPipeA(PIPENAME, 2000);
114 ok(ret, "WaitNamedPipe failed (%d)\n", GetLastError());
116 hFile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
117 ok(hFile != INVALID_HANDLE_VALUE, "CreateFile failed (%d)\n", GetLastError());
119 ok(!WaitNamedPipeA(PIPENAME, 1000), "WaitNamedPipe succeeded\n");
121 ok(GetLastError() == ERROR_SEM_TIMEOUT, "wrong error %u\n", GetLastError());
123 /* don't try to do i/o if one side couldn't be opened, as it hangs */
124 if (hFile != INVALID_HANDLE_VALUE) {
125 HANDLE hFile2;
127 /* Make sure we can read and write a few bytes in both directions */
128 memset(ibuf, 0, sizeof(ibuf));
129 ok(WriteFile(hnp, obuf, sizeof(obuf), &written, NULL), "WriteFile\n");
130 ok(written == sizeof(obuf), "write file len 1\n");
131 ok(PeekNamedPipe(hFile, NULL, 0, NULL, &readden, NULL), "Peek\n");
132 ok(readden == sizeof(obuf), "peek 1 got %d bytes\n", readden);
133 ok(ReadFile(hFile, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
134 ok(readden == sizeof(obuf), "read 1 got %d bytes\n", readden);
135 ok(memcmp(obuf, ibuf, written) == 0, "content 1 check\n");
137 memset(ibuf, 0, sizeof(ibuf));
138 ok(WriteFile(hFile, obuf2, sizeof(obuf2), &written, NULL), "WriteFile\n");
139 ok(written == sizeof(obuf2), "write file len 2\n");
140 ok(PeekNamedPipe(hnp, NULL, 0, NULL, &readden, NULL), "Peek\n");
141 ok(readden == sizeof(obuf2), "peek 2 got %d bytes\n", readden);
142 ok(PeekNamedPipe(hnp, (LPVOID)1, 0, NULL, &readden, NULL), "Peek\n");
143 ok(readden == sizeof(obuf2), "peek 2 got %d bytes\n", readden);
144 ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
145 ok(readden == sizeof(obuf2), "read 2 got %d bytes\n", readden);
146 ok(memcmp(obuf2, ibuf, written) == 0, "content 2 check\n");
148 /* Test reading of multiple writes */
149 memset(ibuf, 0, sizeof(ibuf));
150 ok(WriteFile(hnp, obuf, sizeof(obuf), &written, NULL), "WriteFile3a\n");
151 ok(written == sizeof(obuf), "write file len 3a\n");
152 ok(WriteFile(hnp, obuf2, sizeof(obuf2), &written, NULL), " WriteFile3b\n");
153 ok(written == sizeof(obuf2), "write file len 3b\n");
154 ok(PeekNamedPipe(hFile, ibuf, sizeof(ibuf), &readden, &avail, NULL), "Peek3\n");
155 if (pipemode == PIPE_TYPE_BYTE) {
156 if (readden != sizeof(obuf)) /* Linux only returns the first message */
157 ok(readden == sizeof(obuf) + sizeof(obuf2), "peek3 got %d bytes\n", readden);
158 else
159 todo_wine ok(readden == sizeof(obuf) + sizeof(obuf2), "peek3 got %d bytes\n", readden);
161 else
163 if (readden != sizeof(obuf) + sizeof(obuf2)) /* MacOS returns both messages */
164 ok(readden == sizeof(obuf), "peek3 got %d bytes\n", readden);
165 else
166 todo_wine ok(readden == sizeof(obuf), "peek3 got %d bytes\n", readden);
168 if (avail != sizeof(obuf)) /* older Linux kernels only return the first write here */
169 ok(avail == sizeof(obuf) + sizeof(obuf2), "peek3 got %d bytes available\n", avail);
170 pbuf = ibuf;
171 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "pipe content 3a check\n");
172 if (pipemode == PIPE_TYPE_BYTE && readden >= sizeof(obuf)+sizeof(obuf2)) {
173 pbuf += sizeof(obuf);
174 ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "pipe content 3b check\n");
176 ok(ReadFile(hFile, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
177 ok(readden == sizeof(obuf) + sizeof(obuf2), "read 3 got %d bytes\n", readden);
178 pbuf = ibuf;
179 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 3a check\n");
180 pbuf += sizeof(obuf);
181 ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "content 3b check\n");
183 /* Multiple writes in the reverse direction */
184 memset(ibuf, 0, sizeof(ibuf));
185 ok(WriteFile(hFile, obuf, sizeof(obuf), &written, NULL), "WriteFile4a\n");
186 ok(written == sizeof(obuf), "write file len 4a\n");
187 ok(WriteFile(hFile, obuf2, sizeof(obuf2), &written, NULL), " WriteFile4b\n");
188 ok(written == sizeof(obuf2), "write file len 4b\n");
189 ok(PeekNamedPipe(hnp, ibuf, sizeof(ibuf), &readden, &avail, NULL), "Peek4\n");
190 if (pipemode == PIPE_TYPE_BYTE) {
191 if (readden != sizeof(obuf)) /* Linux only returns the first message */
192 /* should return all 23 bytes */
193 ok(readden == sizeof(obuf) + sizeof(obuf2), "peek4 got %d bytes\n", readden);
194 else
195 todo_wine ok(readden == sizeof(obuf) + sizeof(obuf2), "peek4 got %d bytes\n", readden);
197 else
199 if (readden != sizeof(obuf) + sizeof(obuf2)) /* MacOS returns both messages */
200 ok(readden == sizeof(obuf), "peek4 got %d bytes\n", readden);
201 else
202 todo_wine ok(readden == sizeof(obuf), "peek4 got %d bytes\n", readden);
204 if (avail != sizeof(obuf)) /* older Linux kernels only return the first write here */
205 ok(avail == sizeof(obuf) + sizeof(obuf2), "peek4 got %d bytes available\n", avail);
206 pbuf = ibuf;
207 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "pipe content 4a check\n");
208 if (pipemode == PIPE_TYPE_BYTE && readden >= sizeof(obuf)+sizeof(obuf2)) {
209 pbuf += sizeof(obuf);
210 ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "pipe content 4b check\n");
212 ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
213 if (pipemode == PIPE_TYPE_BYTE) {
214 ok(readden == sizeof(obuf) + sizeof(obuf2), "read 4 got %d bytes\n", readden);
216 else {
217 todo_wine {
218 ok(readden == sizeof(obuf), "read 4 got %d bytes\n", readden);
221 pbuf = ibuf;
222 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 4a check\n");
223 if (pipemode == PIPE_TYPE_BYTE) {
224 pbuf += sizeof(obuf);
225 ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "content 4b check\n");
228 /* Test reading of multiple writes after a mode change
229 (CreateFile always creates a byte mode pipe) */
230 lpmode = PIPE_READMODE_MESSAGE;
231 if (pipemode == PIPE_TYPE_BYTE) {
232 /* trying to change the client end of a byte pipe to message mode should fail */
233 ok(!SetNamedPipeHandleState(hFile, &lpmode, NULL, NULL), "Change mode\n");
235 else {
236 ok(SetNamedPipeHandleState(hFile, &lpmode, NULL, NULL), "Change mode\n");
238 memset(ibuf, 0, sizeof(ibuf));
239 ok(WriteFile(hnp, obuf, sizeof(obuf), &written, NULL), "WriteFile5a\n");
240 ok(written == sizeof(obuf), "write file len 3a\n");
241 ok(WriteFile(hnp, obuf2, sizeof(obuf2), &written, NULL), " WriteFile5b\n");
242 ok(written == sizeof(obuf2), "write file len 3b\n");
243 ok(PeekNamedPipe(hFile, ibuf, sizeof(ibuf), &readden, &avail, NULL), "Peek5\n");
244 if (readden != sizeof(obuf) + sizeof(obuf2)) /* MacOS returns both writes */
245 ok(readden == sizeof(obuf), "peek5 got %d bytes\n", readden);
246 else
247 todo_wine ok(readden == sizeof(obuf), "peek5 got %d bytes\n", readden);
248 if (avail != sizeof(obuf)) /* older Linux kernels only return the first write here */
249 ok(avail == sizeof(obuf) + sizeof(obuf2), "peek5 got %d bytes available\n", avail);
250 else
251 todo_wine ok(avail == sizeof(obuf) + sizeof(obuf2), "peek5 got %d bytes available\n", avail);
252 pbuf = ibuf;
253 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 5a check\n");
254 ok(ReadFile(hFile, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
255 todo_wine {
256 ok(readden == sizeof(obuf), "read 5 got %d bytes\n", readden);
258 pbuf = ibuf;
259 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 5a check\n");
261 /* Multiple writes in the reverse direction */
262 /* the write of obuf2 from write4 should still be in the buffer */
263 ok(PeekNamedPipe(hnp, ibuf, sizeof(ibuf), &readden, &avail, NULL), "Peek6a\n");
264 todo_wine {
265 ok(readden == sizeof(obuf2), "peek6a got %d bytes\n", readden);
266 ok(avail == sizeof(obuf2), "peek6a got %d bytes available\n", avail);
268 if (avail > 0) {
269 ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
270 ok(readden == sizeof(obuf2), "read 6a got %d bytes\n", readden);
271 pbuf = ibuf;
272 ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "content 6a check\n");
274 memset(ibuf, 0, sizeof(ibuf));
275 ok(WriteFile(hFile, obuf, sizeof(obuf), &written, NULL), "WriteFile6a\n");
276 ok(written == sizeof(obuf), "write file len 6a\n");
277 ok(WriteFile(hFile, obuf2, sizeof(obuf2), &written, NULL), " WriteFile6b\n");
278 ok(written == sizeof(obuf2), "write file len 6b\n");
279 ok(PeekNamedPipe(hnp, ibuf, sizeof(ibuf), &readden, &avail, NULL), "Peek6\n");
280 if (readden != sizeof(obuf) + sizeof(obuf2)) /* MacOS returns both writes */
281 ok(readden == sizeof(obuf), "peek6 got %d bytes\n", readden);
282 else
283 todo_wine ok(readden == sizeof(obuf), "peek6 got %d bytes\n", readden);
284 if (avail != sizeof(obuf)) /* older Linux kernels only return the first write here */
285 ok(avail == sizeof(obuf) + sizeof(obuf2), "peek6b got %d bytes available\n", avail);
286 pbuf = ibuf;
287 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 6a check\n");
288 ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
289 todo_wine {
290 ok(readden == sizeof(obuf), "read 6b got %d bytes\n", readden);
292 pbuf = ibuf;
293 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 6a check\n");
296 /* Picky conformance tests */
298 /* Verify that you can't connect to pipe again
299 * until server calls DisconnectNamedPipe+ConnectNamedPipe
300 * or creates a new pipe
301 * case 1: other client not yet closed
303 hFile2 = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
304 ok(hFile2 == INVALID_HANDLE_VALUE,
305 "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail\n");
306 ok(GetLastError() == ERROR_PIPE_BUSY,
307 "connecting to named pipe before other client closes should fail with ERROR_PIPE_BUSY\n");
309 ok(CloseHandle(hFile), "CloseHandle\n");
311 /* case 2: other client already closed */
312 hFile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
313 ok(hFile == INVALID_HANDLE_VALUE,
314 "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail\n");
315 ok(GetLastError() == ERROR_PIPE_BUSY,
316 "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail with ERROR_PIPE_BUSY\n");
318 ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe\n");
320 /* case 3: server has called DisconnectNamedPipe but not ConnectNamed Pipe */
321 hFile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
322 ok(hFile == INVALID_HANDLE_VALUE,
323 "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail\n");
324 ok(GetLastError() == ERROR_PIPE_BUSY,
325 "connecting to named pipe after other client closes but before ConnectNamedPipe should fail with ERROR_PIPE_BUSY\n");
327 /* to be complete, we'd call ConnectNamedPipe here and loop,
328 * but by default that's blocking, so we'd either have
329 * to turn on the uncommon nonblocking mode, or
330 * use another thread.
334 ok(CloseHandle(hnp), "CloseHandle\n");
336 trace("test_CreateNamedPipe returning\n");
339 static void test_CreateNamedPipe_instances_must_match(void)
341 HANDLE hnp, hnp2;
343 /* Check no mismatch */
344 hnp = CreateNamedPipeA(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
345 /* nMaxInstances */ 2,
346 /* nOutBufSize */ 1024,
347 /* nInBufSize */ 1024,
348 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
349 /* lpSecurityAttrib */ NULL);
350 ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
352 hnp2 = CreateNamedPipeA(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
353 /* nMaxInstances */ 2,
354 /* nOutBufSize */ 1024,
355 /* nInBufSize */ 1024,
356 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
357 /* lpSecurityAttrib */ NULL);
358 ok(hnp2 != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
360 ok(CloseHandle(hnp), "CloseHandle\n");
361 ok(CloseHandle(hnp2), "CloseHandle\n");
363 /* Check nMaxInstances */
364 hnp = CreateNamedPipeA(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
365 /* nMaxInstances */ 1,
366 /* nOutBufSize */ 1024,
367 /* nInBufSize */ 1024,
368 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
369 /* lpSecurityAttrib */ NULL);
370 ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
372 hnp2 = CreateNamedPipeA(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
373 /* nMaxInstances */ 1,
374 /* nOutBufSize */ 1024,
375 /* nInBufSize */ 1024,
376 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
377 /* lpSecurityAttrib */ NULL);
378 ok(hnp2 == INVALID_HANDLE_VALUE
379 && GetLastError() == ERROR_PIPE_BUSY, "nMaxInstances not obeyed\n");
381 ok(CloseHandle(hnp), "CloseHandle\n");
383 /* Check PIPE_ACCESS_* */
384 hnp = CreateNamedPipeA(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
385 /* nMaxInstances */ 2,
386 /* nOutBufSize */ 1024,
387 /* nInBufSize */ 1024,
388 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
389 /* lpSecurityAttrib */ NULL);
390 ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
392 hnp2 = CreateNamedPipeA(PIPENAME, PIPE_ACCESS_INBOUND, PIPE_TYPE_BYTE | PIPE_WAIT,
393 /* nMaxInstances */ 2,
394 /* nOutBufSize */ 1024,
395 /* nInBufSize */ 1024,
396 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
397 /* lpSecurityAttrib */ NULL);
398 ok(hnp2 == INVALID_HANDLE_VALUE
399 && GetLastError() == ERROR_ACCESS_DENIED, "PIPE_ACCESS_* mismatch allowed\n");
401 ok(CloseHandle(hnp), "CloseHandle\n");
403 /* check everything else */
404 hnp = CreateNamedPipeA(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
405 /* nMaxInstances */ 4,
406 /* nOutBufSize */ 1024,
407 /* nInBufSize */ 1024,
408 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
409 /* lpSecurityAttrib */ NULL);
410 ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
412 hnp2 = CreateNamedPipeA(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE,
413 /* nMaxInstances */ 3,
414 /* nOutBufSize */ 102,
415 /* nInBufSize */ 24,
416 /* nDefaultWait */ 1234,
417 /* lpSecurityAttrib */ NULL);
418 ok(hnp2 != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
420 ok(CloseHandle(hnp), "CloseHandle\n");
421 ok(CloseHandle(hnp2), "CloseHandle\n");
424 /** implementation of alarm() */
425 static DWORD CALLBACK alarmThreadMain(LPVOID arg)
427 DWORD_PTR timeout = (DWORD_PTR) arg;
428 trace("alarmThreadMain\n");
429 if (WaitForSingleObject( alarm_event, timeout ) == WAIT_TIMEOUT)
431 ok(FALSE, "alarm\n");
432 ExitProcess(1);
434 return 1;
437 static HANDLE hnp = INVALID_HANDLE_VALUE;
439 /** Trivial byte echo server - disconnects after each session */
440 static DWORD CALLBACK serverThreadMain1(LPVOID arg)
442 int i;
444 trace("serverThreadMain1 start\n");
445 /* Set up a simple echo server */
446 hnp = CreateNamedPipeA(PIPENAME "serverThreadMain1", PIPE_ACCESS_DUPLEX,
447 PIPE_TYPE_BYTE | PIPE_WAIT,
448 /* nMaxInstances */ 1,
449 /* nOutBufSize */ 1024,
450 /* nInBufSize */ 1024,
451 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
452 /* lpSecurityAttrib */ NULL);
454 ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
455 for (i = 0; i < NB_SERVER_LOOPS; i++) {
456 char buf[512];
457 DWORD written;
458 DWORD readden;
459 BOOL success;
461 /* Wait for client to connect */
462 trace("Server calling ConnectNamedPipe...\n");
463 ok(ConnectNamedPipe(hnp, NULL)
464 || GetLastError() == ERROR_PIPE_CONNECTED, "ConnectNamedPipe\n");
465 trace("ConnectNamedPipe returned.\n");
467 /* Echo bytes once */
468 memset(buf, 0, sizeof(buf));
470 trace("Server reading...\n");
471 success = ReadFile(hnp, buf, sizeof(buf), &readden, NULL);
472 trace("Server done reading.\n");
473 ok(success, "ReadFile\n");
474 ok(readden, "short read\n");
476 trace("Server writing...\n");
477 ok(WriteFile(hnp, buf, readden, &written, NULL), "WriteFile\n");
478 trace("Server done writing.\n");
479 ok(written == readden, "write file len\n");
481 /* finish this connection, wait for next one */
482 ok(FlushFileBuffers(hnp), "FlushFileBuffers\n");
483 trace("Server done flushing.\n");
484 ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe\n");
485 trace("Server done disconnecting.\n");
487 return 0;
490 /** Trivial byte echo server - closes after each connection */
491 static DWORD CALLBACK serverThreadMain2(LPVOID arg)
493 int i;
494 HANDLE hnpNext = 0;
496 trace("serverThreadMain2\n");
497 /* Set up a simple echo server */
498 hnp = CreateNamedPipeA(PIPENAME "serverThreadMain2", PIPE_ACCESS_DUPLEX,
499 PIPE_TYPE_BYTE | PIPE_WAIT,
500 /* nMaxInstances */ 2,
501 /* nOutBufSize */ 1024,
502 /* nInBufSize */ 1024,
503 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
504 /* lpSecurityAttrib */ NULL);
505 ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
507 for (i = 0; i < NB_SERVER_LOOPS; i++) {
508 char buf[512];
509 DWORD written;
510 DWORD readden;
511 DWORD ret;
512 BOOL success;
515 user_apc_ran = FALSE;
516 if (i == 0 && pQueueUserAPC) {
517 trace("Queueing an user APC\n"); /* verify the pipe is non alerable */
518 ret = pQueueUserAPC(&user_apc, GetCurrentThread(), 0);
519 ok(ret, "QueueUserAPC failed: %d\n", GetLastError());
522 /* Wait for client to connect */
523 trace("Server calling ConnectNamedPipe...\n");
524 ok(ConnectNamedPipe(hnp, NULL)
525 || GetLastError() == ERROR_PIPE_CONNECTED, "ConnectNamedPipe\n");
526 trace("ConnectNamedPipe returned.\n");
528 /* Echo bytes once */
529 memset(buf, 0, sizeof(buf));
531 trace("Server reading...\n");
532 success = ReadFile(hnp, buf, sizeof(buf), &readden, NULL);
533 trace("Server done reading.\n");
534 ok(success, "ReadFile\n");
536 trace("Server writing...\n");
537 ok(WriteFile(hnp, buf, readden, &written, NULL), "WriteFile\n");
538 trace("Server done writing.\n");
539 ok(written == readden, "write file len\n");
541 /* finish this connection, wait for next one */
542 ok(FlushFileBuffers(hnp), "FlushFileBuffers\n");
543 ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe\n");
545 ok(user_apc_ran == FALSE, "UserAPC ran, pipe using alertable io mode\n");
547 if (i == 0 && pQueueUserAPC)
548 SleepEx(0, TRUE); /* get rid of apc */
550 /* Set up next echo server */
551 hnpNext =
552 CreateNamedPipeA(PIPENAME "serverThreadMain2", PIPE_ACCESS_DUPLEX,
553 PIPE_TYPE_BYTE | PIPE_WAIT,
554 /* nMaxInstances */ 2,
555 /* nOutBufSize */ 1024,
556 /* nInBufSize */ 1024,
557 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
558 /* lpSecurityAttrib */ NULL);
560 ok(hnpNext != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
562 ok(CloseHandle(hnp), "CloseHandle\n");
563 hnp = hnpNext;
565 return 0;
568 /** Trivial byte echo server - uses overlapped named pipe calls */
569 static DWORD CALLBACK serverThreadMain3(LPVOID arg)
571 int i;
572 HANDLE hEvent;
574 trace("serverThreadMain3\n");
575 /* Set up a simple echo server */
576 hnp = CreateNamedPipeA(PIPENAME "serverThreadMain3", PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
577 PIPE_TYPE_BYTE | PIPE_WAIT,
578 /* nMaxInstances */ 1,
579 /* nOutBufSize */ 1024,
580 /* nInBufSize */ 1024,
581 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
582 /* lpSecurityAttrib */ NULL);
583 ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
585 hEvent = CreateEventW(NULL, /* security attribute */
586 TRUE, /* manual reset event */
587 FALSE, /* initial state */
588 NULL); /* name */
589 ok(hEvent != NULL, "CreateEvent\n");
591 for (i = 0; i < NB_SERVER_LOOPS; i++) {
592 char buf[512];
593 DWORD written;
594 DWORD readden;
595 DWORD dummy;
596 BOOL success;
597 OVERLAPPED oOverlap;
598 int letWFSOEwait = (i & 2);
599 int letGORwait = (i & 1);
600 DWORD err;
602 memset(&oOverlap, 0, sizeof(oOverlap));
603 oOverlap.hEvent = hEvent;
605 /* Wait for client to connect */
606 if (i == 0) {
607 trace("Server calling non-overlapped ConnectNamedPipe on overlapped pipe...\n");
608 success = ConnectNamedPipe(hnp, NULL);
609 err = GetLastError();
610 ok(success || (err == ERROR_PIPE_CONNECTED), "ConnectNamedPipe failed: %d\n", err);
611 trace("ConnectNamedPipe operation complete.\n");
612 } else {
613 trace("Server calling overlapped ConnectNamedPipe...\n");
614 success = ConnectNamedPipe(hnp, &oOverlap);
615 err = GetLastError();
616 ok(!success && (err == ERROR_IO_PENDING || err == ERROR_PIPE_CONNECTED), "overlapped ConnectNamedPipe\n");
617 trace("overlapped ConnectNamedPipe returned.\n");
618 if (!success && (err == ERROR_IO_PENDING)) {
619 if (letWFSOEwait)
621 DWORD ret;
622 do {
623 ret = WaitForSingleObjectEx(hEvent, INFINITE, TRUE);
624 } while (ret == WAIT_IO_COMPLETION);
625 ok(ret == 0, "wait ConnectNamedPipe returned %x\n", ret);
627 success = GetOverlappedResult(hnp, &oOverlap, &dummy, letGORwait);
628 if (!letGORwait && !letWFSOEwait && !success) {
629 ok(GetLastError() == ERROR_IO_INCOMPLETE, "GetOverlappedResult\n");
630 success = GetOverlappedResult(hnp, &oOverlap, &dummy, TRUE);
633 ok(success || (err == ERROR_PIPE_CONNECTED), "GetOverlappedResult ConnectNamedPipe\n");
634 trace("overlapped ConnectNamedPipe operation complete.\n");
637 /* Echo bytes once */
638 memset(buf, 0, sizeof(buf));
640 trace("Server reading...\n");
641 success = ReadFile(hnp, buf, sizeof(buf), &readden, &oOverlap);
642 trace("Server ReadFile returned...\n");
643 err = GetLastError();
644 ok(success || err == ERROR_IO_PENDING, "overlapped ReadFile\n");
645 trace("overlapped ReadFile returned.\n");
646 if (!success && (err == ERROR_IO_PENDING)) {
647 if (letWFSOEwait)
649 DWORD ret;
650 do {
651 ret = WaitForSingleObjectEx(hEvent, INFINITE, TRUE);
652 } while (ret == WAIT_IO_COMPLETION);
653 ok(ret == 0, "wait ReadFile returned %x\n", ret);
655 success = GetOverlappedResult(hnp, &oOverlap, &readden, letGORwait);
656 if (!letGORwait && !letWFSOEwait && !success) {
657 ok(GetLastError() == ERROR_IO_INCOMPLETE, "GetOverlappedResult\n");
658 success = GetOverlappedResult(hnp, &oOverlap, &readden, TRUE);
661 trace("Server done reading.\n");
662 ok(success, "overlapped ReadFile\n");
664 trace("Server writing...\n");
665 success = WriteFile(hnp, buf, readden, &written, &oOverlap);
666 trace("Server WriteFile returned...\n");
667 err = GetLastError();
668 ok(success || err == ERROR_IO_PENDING, "overlapped WriteFile\n");
669 trace("overlapped WriteFile returned.\n");
670 if (!success && (err == ERROR_IO_PENDING)) {
671 if (letWFSOEwait)
673 DWORD ret;
674 do {
675 ret = WaitForSingleObjectEx(hEvent, INFINITE, TRUE);
676 } while (ret == WAIT_IO_COMPLETION);
677 ok(ret == 0, "wait WriteFile returned %x\n", ret);
679 success = GetOverlappedResult(hnp, &oOverlap, &written, letGORwait);
680 if (!letGORwait && !letWFSOEwait && !success) {
681 ok(GetLastError() == ERROR_IO_INCOMPLETE, "GetOverlappedResult\n");
682 success = GetOverlappedResult(hnp, &oOverlap, &written, TRUE);
685 trace("Server done writing.\n");
686 ok(success, "overlapped WriteFile\n");
687 ok(written == readden, "write file len\n");
689 /* finish this connection, wait for next one */
690 ok(FlushFileBuffers(hnp), "FlushFileBuffers\n");
691 ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe\n");
693 return 0;
696 /** Trivial byte echo server - uses i/o completion ports */
697 static DWORD CALLBACK serverThreadMain4(LPVOID arg)
699 int i;
700 HANDLE hcompletion;
701 BOOL ret;
703 trace("serverThreadMain4\n");
704 /* Set up a simple echo server */
705 hnp = CreateNamedPipeA(PIPENAME "serverThreadMain4", PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
706 PIPE_TYPE_BYTE | PIPE_WAIT,
707 /* nMaxInstances */ 1,
708 /* nOutBufSize */ 1024,
709 /* nInBufSize */ 1024,
710 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
711 /* lpSecurityAttrib */ NULL);
712 ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
714 hcompletion = CreateIoCompletionPort(hnp, NULL, 12345, 1);
715 ok(hcompletion != NULL, "CreateIoCompletionPort failed, error=%i\n", GetLastError());
717 for (i = 0; i < NB_SERVER_LOOPS; i++) {
718 char buf[512];
719 DWORD written;
720 DWORD readden;
721 DWORD dummy;
722 BOOL success;
723 OVERLAPPED oConnect;
724 OVERLAPPED oRead;
725 OVERLAPPED oWrite;
726 OVERLAPPED *oResult;
727 DWORD err;
728 ULONG_PTR compkey;
730 memset(&oConnect, 0, sizeof(oConnect));
731 memset(&oRead, 0, sizeof(oRead));
732 memset(&oWrite, 0, sizeof(oWrite));
734 /* Wait for client to connect */
735 trace("Server calling overlapped ConnectNamedPipe...\n");
736 success = ConnectNamedPipe(hnp, &oConnect);
737 err = GetLastError();
738 ok(!success && (err == ERROR_IO_PENDING || err == ERROR_PIPE_CONNECTED),
739 "overlapped ConnectNamedPipe got %u err %u\n", success, err );
740 if (!success && err == ERROR_IO_PENDING) {
741 trace("ConnectNamedPipe GetQueuedCompletionStatus\n");
742 success = GetQueuedCompletionStatus(hcompletion, &dummy, &compkey, &oResult, 0);
743 if (!success)
745 ok( GetLastError() == WAIT_TIMEOUT,
746 "ConnectNamedPipe GetQueuedCompletionStatus wrong error %u\n", GetLastError());
747 success = GetQueuedCompletionStatus(hcompletion, &dummy, &compkey, &oResult, 10000);
749 ok(success, "ConnectNamedPipe GetQueuedCompletionStatus failed, errno=%i\n", GetLastError());
750 if (success)
752 ok(compkey == 12345, "got completion key %i instead of 12345\n", (int)compkey);
753 ok(oResult == &oConnect, "got overlapped pointer %p instead of %p\n", oResult, &oConnect);
756 trace("overlapped ConnectNamedPipe operation complete.\n");
758 /* Echo bytes once */
759 memset(buf, 0, sizeof(buf));
761 trace("Server reading...\n");
762 success = ReadFile(hnp, buf, sizeof(buf), &readden, &oRead);
763 trace("Server ReadFile returned...\n");
764 err = GetLastError();
765 ok(success || err == ERROR_IO_PENDING, "overlapped ReadFile, err=%i\n", err);
766 success = GetQueuedCompletionStatus(hcompletion, &readden, &compkey,
767 &oResult, 10000);
768 ok(success, "ReadFile GetQueuedCompletionStatus failed, errno=%i\n", GetLastError());
769 if (success)
771 ok(compkey == 12345, "got completion key %i instead of 12345\n", (int)compkey);
772 ok(oResult == &oRead, "got overlapped pointer %p instead of %p\n", oResult, &oRead);
774 trace("Server done reading.\n");
776 trace("Server writing...\n");
777 success = WriteFile(hnp, buf, readden, &written, &oWrite);
778 trace("Server WriteFile returned...\n");
779 err = GetLastError();
780 ok(success || err == ERROR_IO_PENDING, "overlapped WriteFile failed, err=%u\n", err);
781 success = GetQueuedCompletionStatus(hcompletion, &written, &compkey,
782 &oResult, 10000);
783 ok(success, "WriteFile GetQueuedCompletionStatus failed, errno=%i\n", GetLastError());
784 if (success)
786 ok(compkey == 12345, "got completion key %i instead of 12345\n", (int)compkey);
787 ok(oResult == &oWrite, "got overlapped pointer %p instead of %p\n", oResult, &oWrite);
788 ok(written == readden, "write file len\n");
790 trace("Server done writing.\n");
792 /* finish this connection, wait for next one */
793 ok(FlushFileBuffers(hnp), "FlushFileBuffers\n");
794 success = DisconnectNamedPipe(hnp);
795 ok(success, "DisconnectNamedPipe failed, err %u\n", GetLastError());
798 ret = CloseHandle(hnp);
799 ok(ret, "CloseHandle named pipe failed, err=%i\n", GetLastError());
800 ret = CloseHandle(hcompletion);
801 ok(ret, "CloseHandle completion failed, err=%i\n", GetLastError());
803 return 0;
806 static int completion_called;
807 static DWORD completion_errorcode;
808 static DWORD completion_num_bytes;
809 static LPOVERLAPPED completion_lpoverlapped;
811 static VOID WINAPI completion_routine(DWORD errorcode, DWORD num_bytes, LPOVERLAPPED lpoverlapped)
813 completion_called++;
814 completion_errorcode = errorcode;
815 completion_num_bytes = num_bytes;
816 completion_lpoverlapped = lpoverlapped;
817 SetEvent(lpoverlapped->hEvent);
820 /** Trivial byte echo server - uses ReadFileEx/WriteFileEx */
821 static DWORD CALLBACK serverThreadMain5(LPVOID arg)
823 int i;
824 HANDLE hEvent;
826 trace("serverThreadMain5\n");
827 /* Set up a simple echo server */
828 hnp = CreateNamedPipeA(PIPENAME "serverThreadMain5", PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
829 PIPE_TYPE_BYTE | PIPE_WAIT,
830 /* nMaxInstances */ 1,
831 /* nOutBufSize */ 1024,
832 /* nInBufSize */ 1024,
833 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
834 /* lpSecurityAttrib */ NULL);
835 ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
837 hEvent = CreateEventW(NULL, /* security attribute */
838 TRUE, /* manual reset event */
839 FALSE, /* initial state */
840 NULL); /* name */
841 ok(hEvent != NULL, "CreateEvent\n");
843 for (i = 0; i < NB_SERVER_LOOPS; i++) {
844 char buf[512];
845 DWORD readden;
846 BOOL success;
847 OVERLAPPED oOverlap;
848 DWORD err;
850 memset(&oOverlap, 0, sizeof(oOverlap));
851 oOverlap.hEvent = hEvent;
853 /* Wait for client to connect */
854 trace("Server calling ConnectNamedPipe...\n");
855 success = ConnectNamedPipe(hnp, NULL);
856 err = GetLastError();
857 ok(success || (err == ERROR_PIPE_CONNECTED), "ConnectNamedPipe failed: %d\n", err);
858 trace("ConnectNamedPipe operation complete.\n");
860 /* Echo bytes once */
861 memset(buf, 0, sizeof(buf));
863 trace("Server reading...\n");
864 completion_called = 0;
865 ResetEvent(hEvent);
866 success = ReadFileEx(hnp, buf, sizeof(buf), &oOverlap, completion_routine);
867 trace("Server ReadFileEx returned...\n");
868 ok(success, "ReadFileEx failed, err=%i\n", GetLastError());
869 ok(completion_called == 0, "completion routine called before ReadFileEx return\n");
870 trace("ReadFileEx returned.\n");
871 if (success) {
872 DWORD ret;
873 do {
874 ret = WaitForSingleObjectEx(hEvent, INFINITE, TRUE);
875 } while (ret == WAIT_IO_COMPLETION);
876 ok(ret == 0, "wait ReadFileEx returned %x\n", ret);
878 ok(completion_called == 1, "completion routine called %i times\n", completion_called);
879 ok(completion_errorcode == ERROR_SUCCESS, "completion routine got error %d\n", completion_errorcode);
880 ok(completion_num_bytes != 0, "read 0 bytes\n");
881 ok(completion_lpoverlapped == &oOverlap, "got wrong overlapped pointer %p\n", completion_lpoverlapped);
882 readden = completion_num_bytes;
883 trace("Server done reading.\n");
885 trace("Server writing...\n");
886 completion_called = 0;
887 ResetEvent(hEvent);
888 success = WriteFileEx(hnp, buf, readden, &oOverlap, completion_routine);
889 trace("Server WriteFileEx returned...\n");
890 ok(success, "WriteFileEx failed, err=%i\n", GetLastError());
891 ok(completion_called == 0, "completion routine called before ReadFileEx return\n");
892 trace("overlapped WriteFile returned.\n");
893 if (success) {
894 DWORD ret;
895 do {
896 ret = WaitForSingleObjectEx(hEvent, INFINITE, TRUE);
897 } while (ret == WAIT_IO_COMPLETION);
898 ok(ret == 0, "wait WriteFileEx returned %x\n", ret);
900 trace("Server done writing.\n");
901 ok(completion_called == 1, "completion routine called %i times\n", completion_called);
902 ok(completion_errorcode == ERROR_SUCCESS, "completion routine got error %d\n", completion_errorcode);
903 ok(completion_num_bytes == readden, "read %i bytes wrote %i\n", readden, completion_num_bytes);
904 ok(completion_lpoverlapped == &oOverlap, "got wrong overlapped pointer %p\n", completion_lpoverlapped);
906 /* finish this connection, wait for next one */
907 ok(FlushFileBuffers(hnp), "FlushFileBuffers\n");
908 ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe\n");
910 return 0;
913 static void exercizeServer(const char *pipename, HANDLE serverThread)
915 int i;
917 trace("exercizeServer starting\n");
918 for (i = 0; i < NB_SERVER_LOOPS; i++) {
919 HANDLE hFile=INVALID_HANDLE_VALUE;
920 static const char obuf[] = "Bit Bucket";
921 char ibuf[32];
922 DWORD written;
923 DWORD readden;
924 int loop;
926 for (loop = 0; loop < 3; loop++) {
927 DWORD err;
928 trace("Client connecting...\n");
929 /* Connect to the server */
930 hFile = CreateFileA(pipename, GENERIC_READ | GENERIC_WRITE, 0,
931 NULL, OPEN_EXISTING, 0, 0);
932 if (hFile != INVALID_HANDLE_VALUE)
933 break;
934 err = GetLastError();
935 if (loop == 0)
936 ok(err == ERROR_PIPE_BUSY || err == ERROR_FILE_NOT_FOUND, "connecting to pipe\n");
937 else
938 ok(err == ERROR_PIPE_BUSY, "connecting to pipe\n");
939 trace("connect failed, retrying\n");
940 Sleep(200);
942 ok(hFile != INVALID_HANDLE_VALUE, "client opening named pipe\n");
944 /* Make sure it can echo */
945 memset(ibuf, 0, sizeof(ibuf));
946 trace("Client writing...\n");
947 ok(WriteFile(hFile, obuf, sizeof(obuf), &written, NULL), "WriteFile to client end of pipe\n");
948 ok(written == sizeof(obuf), "write file len\n");
949 trace("Client reading...\n");
950 ok(ReadFile(hFile, ibuf, sizeof(obuf), &readden, NULL), "ReadFile from client end of pipe\n");
951 ok(readden == sizeof(obuf), "read file len\n");
952 ok(memcmp(obuf, ibuf, written) == 0, "content check\n");
954 trace("Client closing...\n");
955 ok(CloseHandle(hFile), "CloseHandle\n");
958 ok(WaitForSingleObject(serverThread,INFINITE) == WAIT_OBJECT_0, "WaitForSingleObject\n");
959 CloseHandle(hnp);
960 trace("exercizeServer returning\n");
963 static void test_NamedPipe_2(void)
965 HANDLE serverThread;
966 DWORD serverThreadId;
967 HANDLE alarmThread;
968 DWORD alarmThreadId;
970 trace("test_NamedPipe_2 starting\n");
971 /* Set up a twenty second timeout */
972 alarm_event = CreateEventW( NULL, TRUE, FALSE, NULL );
973 SetLastError(0xdeadbeef);
974 alarmThread = CreateThread(NULL, 0, alarmThreadMain, (void *) 20000, 0, &alarmThreadId);
975 ok(alarmThread != NULL, "CreateThread failed: %d\n", GetLastError());
977 /* The servers we're about to exercise do try to clean up carefully,
978 * but to reduce the chance of a test failure due to a pipe handle
979 * leak in the test code, we'll use a different pipe name for each server.
982 /* Try server #1 */
983 SetLastError(0xdeadbeef);
984 serverThread = CreateThread(NULL, 0, serverThreadMain1, (void *)8, 0, &serverThreadId);
985 ok(serverThread != NULL, "CreateThread failed: %d\n", GetLastError());
986 exercizeServer(PIPENAME "serverThreadMain1", serverThread);
988 /* Try server #2 */
989 SetLastError(0xdeadbeef);
990 serverThread = CreateThread(NULL, 0, serverThreadMain2, 0, 0, &serverThreadId);
991 ok(serverThread != NULL, "CreateThread failed: %d\n", GetLastError());
992 exercizeServer(PIPENAME "serverThreadMain2", serverThread);
994 /* Try server #3 */
995 SetLastError(0xdeadbeef);
996 serverThread = CreateThread(NULL, 0, serverThreadMain3, 0, 0, &serverThreadId);
997 ok(serverThread != NULL, "CreateThread failed: %d\n", GetLastError());
998 exercizeServer(PIPENAME "serverThreadMain3", serverThread);
1000 /* Try server #4 */
1001 SetLastError(0xdeadbeef);
1002 serverThread = CreateThread(NULL, 0, serverThreadMain4, 0, 0, &serverThreadId);
1003 ok(serverThread != NULL, "CreateThread failed: %d\n", GetLastError());
1004 exercizeServer(PIPENAME "serverThreadMain4", serverThread);
1006 /* Try server #5 */
1007 SetLastError(0xdeadbeef);
1008 serverThread = CreateThread(NULL, 0, serverThreadMain5, 0, 0, &serverThreadId);
1009 ok(serverThread != NULL, "CreateThread failed: %d\n", GetLastError());
1010 exercizeServer(PIPENAME "serverThreadMain5", serverThread);
1012 ok(SetEvent( alarm_event ), "SetEvent\n");
1013 CloseHandle( alarm_event );
1014 trace("test_NamedPipe_2 returning\n");
1017 static int test_DisconnectNamedPipe(void)
1019 HANDLE hnp;
1020 HANDLE hFile;
1021 static const char obuf[] = "Bit Bucket";
1022 char ibuf[32];
1023 DWORD written;
1024 DWORD readden;
1025 DWORD ret;
1027 SetLastError(0xdeadbeef);
1028 hnp = CreateNamedPipeA(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
1029 /* nMaxInstances */ 1,
1030 /* nOutBufSize */ 1024,
1031 /* nInBufSize */ 1024,
1032 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
1033 /* lpSecurityAttrib */ NULL);
1034 if ((hnp == INVALID_HANDLE_VALUE /* Win98 */ || !hnp /* Win95 */)
1035 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) {
1037 win_skip("Named pipes are not implemented\n");
1038 return 1;
1041 ok(WriteFile(hnp, obuf, sizeof(obuf), &written, NULL) == 0
1042 && GetLastError() == ERROR_PIPE_LISTENING, "WriteFile to not-yet-connected pipe\n");
1043 ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL) == 0
1044 && GetLastError() == ERROR_PIPE_LISTENING, "ReadFile from not-yet-connected pipe\n");
1046 hFile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
1047 ok(hFile != INVALID_HANDLE_VALUE, "CreateFile failed\n");
1049 /* don't try to do i/o if one side couldn't be opened, as it hangs */
1050 if (hFile != INVALID_HANDLE_VALUE) {
1052 /* see what happens if server calls DisconnectNamedPipe
1053 * when there are bytes in the pipe
1056 ok(WriteFile(hFile, obuf, sizeof(obuf), &written, NULL), "WriteFile\n");
1057 ok(written == sizeof(obuf), "write file len\n");
1058 ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe while messages waiting\n");
1059 ok(WriteFile(hFile, obuf, sizeof(obuf), &written, NULL) == 0
1060 && GetLastError() == ERROR_PIPE_NOT_CONNECTED, "WriteFile to disconnected pipe\n");
1061 ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL) == 0
1062 && GetLastError() == ERROR_PIPE_NOT_CONNECTED,
1063 "ReadFile from disconnected pipe with bytes waiting\n");
1064 ok(!DisconnectNamedPipe(hnp) && GetLastError() == ERROR_PIPE_NOT_CONNECTED,
1065 "DisconnectNamedPipe worked twice\n");
1066 ret = WaitForSingleObject(hFile, 0);
1067 ok(ret == WAIT_TIMEOUT, "WaitForSingleObject returned %X\n", ret);
1068 ok(CloseHandle(hFile), "CloseHandle\n");
1071 ok(CloseHandle(hnp), "CloseHandle\n");
1073 return 0;
1075 static void test_CreatePipe(void)
1077 SECURITY_ATTRIBUTES pipe_attr;
1078 HANDLE piperead, pipewrite;
1079 DWORD written;
1080 DWORD read;
1081 DWORD i, size;
1082 BYTE *buffer;
1083 char readbuf[32];
1085 user_apc_ran = FALSE;
1086 if (pQueueUserAPC)
1087 ok(pQueueUserAPC(user_apc, GetCurrentThread(), 0), "couldn't create user apc\n");
1089 pipe_attr.nLength = sizeof(SECURITY_ATTRIBUTES);
1090 pipe_attr.bInheritHandle = TRUE;
1091 pipe_attr.lpSecurityDescriptor = NULL;
1092 ok(CreatePipe(&piperead, &pipewrite, &pipe_attr, 0) != 0, "CreatePipe failed\n");
1093 ok(WriteFile(pipewrite,PIPENAME,sizeof(PIPENAME), &written, NULL), "Write to anonymous pipe failed\n");
1094 ok(written == sizeof(PIPENAME), "Write to anonymous pipe wrote %d bytes\n", written);
1095 ok(ReadFile(piperead,readbuf,sizeof(readbuf),&read, NULL), "Read from non empty pipe failed\n");
1096 ok(read == sizeof(PIPENAME), "Read from anonymous pipe got %d bytes\n", read);
1097 ok(CloseHandle(pipewrite), "CloseHandle for the write pipe failed\n");
1098 ok(CloseHandle(piperead), "CloseHandle for the read pipe failed\n");
1100 /* Now write another chunk*/
1101 ok(CreatePipe(&piperead, &pipewrite, &pipe_attr, 0) != 0, "CreatePipe failed\n");
1102 ok(WriteFile(pipewrite,PIPENAME,sizeof(PIPENAME), &written, NULL), "Write to anonymous pipe failed\n");
1103 ok(written == sizeof(PIPENAME), "Write to anonymous pipe wrote %d bytes\n", written);
1104 /* and close the write end, read should still succeed*/
1105 ok(CloseHandle(pipewrite), "CloseHandle for the Write Pipe failed\n");
1106 ok(ReadFile(piperead,readbuf,sizeof(readbuf),&read, NULL), "Read from broken pipe withe with pending data failed\n");
1107 ok(read == sizeof(PIPENAME), "Read from anonymous pipe got %d bytes\n", read);
1108 /* But now we need to get informed that the pipe is closed */
1109 ok(ReadFile(piperead,readbuf,sizeof(readbuf),&read, NULL) == 0, "Broken pipe not detected\n");
1110 ok(CloseHandle(piperead), "CloseHandle for the read pipe failed\n");
1112 /* Try bigger chunks */
1113 size = 32768;
1114 buffer = HeapAlloc( GetProcessHeap(), 0, size );
1115 for (i = 0; i < size; i++) buffer[i] = i;
1116 ok(CreatePipe(&piperead, &pipewrite, &pipe_attr, (size + 24)) != 0, "CreatePipe failed\n");
1117 ok(WriteFile(pipewrite, buffer, size, &written, NULL), "Write to anonymous pipe failed\n");
1118 ok(written == size, "Write to anonymous pipe wrote %d bytes\n", written);
1119 /* and close the write end, read should still succeed*/
1120 ok(CloseHandle(pipewrite), "CloseHandle for the Write Pipe failed\n");
1121 memset( buffer, 0, size );
1122 ok(ReadFile(piperead, buffer, size, &read, NULL), "Read from broken pipe withe with pending data failed\n");
1123 ok(read == size, "Read from anonymous pipe got %d bytes\n", read);
1124 for (i = 0; i < size; i++) ok( buffer[i] == (BYTE)i, "invalid data %x at %x\n", buffer[i], i );
1125 /* But now we need to get informed that the pipe is closed */
1126 ok(ReadFile(piperead,readbuf,sizeof(readbuf),&read, NULL) == 0, "Broken pipe not detected\n");
1127 ok(CloseHandle(piperead), "CloseHandle for the read pipe failed\n");
1128 HeapFree(GetProcessHeap(), 0, buffer);
1130 ok(user_apc_ran == FALSE, "user apc ran, pipe using alertable io mode\n");
1131 SleepEx(0, TRUE); /* get rid of apc */
1134 struct named_pipe_client_params
1136 DWORD security_flags;
1137 HANDLE token;
1138 BOOL revert;
1141 #define PIPE_NAME "\\\\.\\pipe\\named_pipe_test"
1143 static DWORD CALLBACK named_pipe_client_func(LPVOID p)
1145 struct named_pipe_client_params *params = p;
1146 HANDLE pipe;
1147 BOOL ret;
1148 const char message[] = "Test";
1149 DWORD bytes_read, bytes_written;
1150 char dummy;
1151 TOKEN_PRIVILEGES *Privileges = NULL;
1153 if (params->token)
1155 if (params->revert)
1157 /* modify the token so we can tell if the pipe impersonation
1158 * token reverts to the process token */
1159 ret = AdjustTokenPrivileges(params->token, TRUE, NULL, 0, NULL, NULL);
1160 ok(ret, "AdjustTokenPrivileges failed with error %d\n", GetLastError());
1162 ret = SetThreadToken(NULL, params->token);
1163 ok(ret, "SetThreadToken failed with error %d\n", GetLastError());
1165 else
1167 DWORD Size = 0;
1168 HANDLE process_token;
1170 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY|TOKEN_ADJUST_PRIVILEGES, &process_token);
1171 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
1173 ret = GetTokenInformation(process_token, TokenPrivileges, NULL, 0, &Size);
1174 ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "GetTokenInformation(TokenPrivileges) failed with %d\n", GetLastError());
1175 Privileges = HeapAlloc(GetProcessHeap(), 0, Size);
1176 ret = GetTokenInformation(process_token, TokenPrivileges, Privileges, Size, &Size);
1177 ok(ret, "GetTokenInformation(TokenPrivileges) failed with %d\n", GetLastError());
1179 ret = AdjustTokenPrivileges(process_token, TRUE, NULL, 0, NULL, NULL);
1180 ok(ret, "AdjustTokenPrivileges failed with error %d\n", GetLastError());
1182 CloseHandle(process_token);
1185 pipe = CreateFileA(PIPE_NAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, params->security_flags, NULL);
1186 ok(pipe != INVALID_HANDLE_VALUE, "CreateFile for pipe failed with error %d\n", GetLastError());
1188 ret = WriteFile(pipe, message, sizeof(message), &bytes_written, NULL);
1189 ok(ret, "WriteFile failed with error %d\n", GetLastError());
1191 ret = ReadFile(pipe, &dummy, sizeof(dummy), &bytes_read, NULL);
1192 ok(ret, "ReadFile failed with error %d\n", GetLastError());
1194 if (params->token)
1196 if (params->revert)
1198 ret = RevertToSelf();
1199 ok(ret, "RevertToSelf failed with error %d\n", GetLastError());
1201 else
1203 ret = AdjustTokenPrivileges(params->token, TRUE, NULL, 0, NULL, NULL);
1204 ok(ret, "AdjustTokenPrivileges failed with error %d\n", GetLastError());
1207 else
1209 HANDLE process_token;
1211 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &process_token);
1212 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
1214 ret = AdjustTokenPrivileges(process_token, FALSE, Privileges, 0, NULL, NULL);
1215 ok(ret, "AdjustTokenPrivileges failed with error %d\n", GetLastError());
1217 HeapFree(GetProcessHeap(), 0, Privileges);
1219 CloseHandle(process_token);
1222 ret = WriteFile(pipe, message, sizeof(message), &bytes_written, NULL);
1223 ok(ret, "WriteFile failed with error %d\n", GetLastError());
1225 ret = ReadFile(pipe, &dummy, sizeof(dummy), &bytes_read, NULL);
1226 ok(ret, "ReadFile failed with error %d\n", GetLastError());
1228 CloseHandle(pipe);
1230 return 0;
1233 static HANDLE make_impersonation_token(DWORD Access, SECURITY_IMPERSONATION_LEVEL ImpersonationLevel)
1235 HANDLE ProcessToken;
1236 HANDLE Token = NULL;
1237 BOOL ret;
1239 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE, &ProcessToken);
1240 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
1242 ret = pDuplicateTokenEx(ProcessToken, Access, NULL, ImpersonationLevel, TokenImpersonation, &Token);
1243 ok(ret, "DuplicateToken failed with error %d\n", GetLastError());
1245 CloseHandle(ProcessToken);
1247 return Token;
1250 static void test_ImpersonateNamedPipeClient(HANDLE hClientToken, DWORD security_flags, BOOL revert, void (*test_func)(int, HANDLE))
1252 HANDLE hPipeServer;
1253 BOOL ret;
1254 DWORD dwTid;
1255 HANDLE hThread;
1256 char buffer[256];
1257 DWORD dwBytesRead;
1258 DWORD error;
1259 struct named_pipe_client_params params;
1260 char dummy = 0;
1261 DWORD dwBytesWritten;
1262 HANDLE hToken = NULL;
1263 SECURITY_IMPERSONATION_LEVEL ImpersonationLevel;
1264 DWORD size;
1266 hPipeServer = CreateNamedPipeA(PIPE_NAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT, 1, 100, 100, NMPWAIT_USE_DEFAULT_WAIT, NULL);
1267 ok(hPipeServer != INVALID_HANDLE_VALUE, "CreateNamedPipe failed with error %d\n", GetLastError());
1269 params.security_flags = security_flags;
1270 params.token = hClientToken;
1271 params.revert = revert;
1272 hThread = CreateThread(NULL, 0, named_pipe_client_func, &params, 0, &dwTid);
1273 ok(hThread != NULL, "CreateThread failed with error %d\n", GetLastError());
1275 SetLastError(0xdeadbeef);
1276 ret = ImpersonateNamedPipeClient(hPipeServer);
1277 error = GetLastError();
1278 ok(ret /* win2k3 */ || (error == ERROR_CANNOT_IMPERSONATE),
1279 "ImpersonateNamedPipeClient should have failed with ERROR_CANNOT_IMPERSONATE instead of %d\n", GetLastError());
1281 ret = ConnectNamedPipe(hPipeServer, NULL);
1282 ok(ret || (GetLastError() == ERROR_PIPE_CONNECTED), "ConnectNamedPipe failed with error %d\n", GetLastError());
1284 ret = ReadFile(hPipeServer, buffer, sizeof(buffer), &dwBytesRead, NULL);
1285 ok(ret, "ReadFile failed with error %d\n", GetLastError());
1287 ret = ImpersonateNamedPipeClient(hPipeServer);
1288 ok(ret, "ImpersonateNamedPipeClient failed with error %d\n", GetLastError());
1290 ret = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, FALSE, &hToken);
1291 ok(ret, "OpenThreadToken failed with error %d\n", GetLastError());
1293 (*test_func)(0, hToken);
1295 ImpersonationLevel = 0xdeadbeef; /* to avoid false positives */
1296 ret = GetTokenInformation(hToken, TokenImpersonationLevel, &ImpersonationLevel, sizeof(ImpersonationLevel), &size);
1297 ok(ret, "GetTokenInformation(TokenImpersonationLevel) failed with error %d\n", GetLastError());
1298 ok(ImpersonationLevel == SecurityImpersonation, "ImpersonationLevel should have been SecurityImpersonation(%d) instead of %d\n", SecurityImpersonation, ImpersonationLevel);
1300 CloseHandle(hToken);
1302 RevertToSelf();
1304 ret = WriteFile(hPipeServer, &dummy, sizeof(dummy), &dwBytesWritten, NULL);
1305 ok(ret, "WriteFile failed with error %d\n", GetLastError());
1307 ret = ReadFile(hPipeServer, buffer, sizeof(buffer), &dwBytesRead, NULL);
1308 ok(ret, "ReadFile failed with error %d\n", GetLastError());
1310 ret = ImpersonateNamedPipeClient(hPipeServer);
1311 ok(ret, "ImpersonateNamedPipeClient failed with error %d\n", GetLastError());
1313 ret = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, FALSE, &hToken);
1314 ok(ret, "OpenThreadToken failed with error %d\n", GetLastError());
1316 (*test_func)(1, hToken);
1318 CloseHandle(hToken);
1320 RevertToSelf();
1322 ret = WriteFile(hPipeServer, &dummy, sizeof(dummy), &dwBytesWritten, NULL);
1323 ok(ret, "WriteFile failed with error %d\n", GetLastError());
1325 WaitForSingleObject(hThread, INFINITE);
1327 ret = ImpersonateNamedPipeClient(hPipeServer);
1328 ok(ret, "ImpersonateNamedPipeClient failed with error %d\n", GetLastError());
1330 RevertToSelf();
1332 CloseHandle(hThread);
1333 CloseHandle(hPipeServer);
1336 static BOOL are_all_privileges_disabled(HANDLE hToken)
1338 BOOL ret;
1339 TOKEN_PRIVILEGES *Privileges = NULL;
1340 DWORD Size = 0;
1341 BOOL all_privs_disabled = TRUE;
1342 DWORD i;
1344 ret = GetTokenInformation(hToken, TokenPrivileges, NULL, 0, &Size);
1345 if (!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1347 Privileges = HeapAlloc(GetProcessHeap(), 0, Size);
1348 ret = GetTokenInformation(hToken, TokenPrivileges, Privileges, Size, &Size);
1349 if (!ret)
1351 HeapFree(GetProcessHeap(), 0, Privileges);
1352 return FALSE;
1355 else
1356 return FALSE;
1358 for (i = 0; i < Privileges->PrivilegeCount; i++)
1360 if (Privileges->Privileges[i].Attributes & SE_PRIVILEGE_ENABLED)
1362 all_privs_disabled = FALSE;
1363 break;
1367 HeapFree(GetProcessHeap(), 0, Privileges);
1369 return all_privs_disabled;
1372 static DWORD get_privilege_count(HANDLE hToken)
1374 TOKEN_STATISTICS Statistics;
1375 DWORD Size = sizeof(Statistics);
1376 BOOL ret;
1378 ret = GetTokenInformation(hToken, TokenStatistics, &Statistics, Size, &Size);
1379 ok(ret, "GetTokenInformation(TokenStatistics)\n");
1380 if (!ret) return -1;
1382 return Statistics.PrivilegeCount;
1385 static void test_no_sqos_no_token(int call_index, HANDLE hToken)
1387 DWORD priv_count;
1389 switch (call_index)
1391 case 0:
1392 priv_count = get_privilege_count(hToken);
1393 todo_wine
1394 ok(priv_count == 0, "privilege count should have been 0 instead of %d\n", priv_count);
1395 break;
1396 case 1:
1397 priv_count = get_privilege_count(hToken);
1398 ok(priv_count > 0, "privilege count should now be > 0 instead of 0\n");
1399 ok(!are_all_privileges_disabled(hToken), "impersonated token should not have been modified\n");
1400 break;
1401 default:
1402 ok(0, "shouldn't happen\n");
1406 static void test_no_sqos(int call_index, HANDLE hToken)
1408 switch (call_index)
1410 case 0:
1411 ok(!are_all_privileges_disabled(hToken), "token should be a copy of the process one\n");
1412 break;
1413 case 1:
1414 todo_wine
1415 ok(are_all_privileges_disabled(hToken), "impersonated token should have been modified\n");
1416 break;
1417 default:
1418 ok(0, "shouldn't happen\n");
1422 static void test_static_context(int call_index, HANDLE hToken)
1424 switch (call_index)
1426 case 0:
1427 ok(!are_all_privileges_disabled(hToken), "token should be a copy of the process one\n");
1428 break;
1429 case 1:
1430 ok(!are_all_privileges_disabled(hToken), "impersonated token should not have been modified\n");
1431 break;
1432 default:
1433 ok(0, "shouldn't happen\n");
1437 static void test_dynamic_context(int call_index, HANDLE hToken)
1439 switch (call_index)
1441 case 0:
1442 ok(!are_all_privileges_disabled(hToken), "token should be a copy of the process one\n");
1443 break;
1444 case 1:
1445 todo_wine
1446 ok(are_all_privileges_disabled(hToken), "impersonated token should have been modified\n");
1447 break;
1448 default:
1449 ok(0, "shouldn't happen\n");
1453 static void test_dynamic_context_no_token(int call_index, HANDLE hToken)
1455 switch (call_index)
1457 case 0:
1458 ok(are_all_privileges_disabled(hToken), "token should be a copy of the process one\n");
1459 break;
1460 case 1:
1461 ok(!are_all_privileges_disabled(hToken), "process token modification should have been detected and impersonation token updated\n");
1462 break;
1463 default:
1464 ok(0, "shouldn't happen\n");
1468 static void test_no_sqos_revert(int call_index, HANDLE hToken)
1470 DWORD priv_count;
1471 switch (call_index)
1473 case 0:
1474 priv_count = get_privilege_count(hToken);
1475 todo_wine
1476 ok(priv_count == 0, "privilege count should have been 0 instead of %d\n", priv_count);
1477 break;
1478 case 1:
1479 priv_count = get_privilege_count(hToken);
1480 ok(priv_count > 0, "privilege count should now be > 0 instead of 0\n");
1481 ok(!are_all_privileges_disabled(hToken), "impersonated token should not have been modified\n");
1482 break;
1483 default:
1484 ok(0, "shouldn't happen\n");
1488 static void test_static_context_revert(int call_index, HANDLE hToken)
1490 switch (call_index)
1492 case 0:
1493 todo_wine
1494 ok(are_all_privileges_disabled(hToken), "privileges should have been disabled\n");
1495 break;
1496 case 1:
1497 todo_wine
1498 ok(are_all_privileges_disabled(hToken), "impersonated token should not have been modified\n");
1499 break;
1500 default:
1501 ok(0, "shouldn't happen\n");
1505 static void test_dynamic_context_revert(int call_index, HANDLE hToken)
1507 switch (call_index)
1509 case 0:
1510 todo_wine
1511 ok(are_all_privileges_disabled(hToken), "privileges should have been disabled\n");
1512 break;
1513 case 1:
1514 ok(!are_all_privileges_disabled(hToken), "impersonated token should now be process token\n");
1515 break;
1516 default:
1517 ok(0, "shouldn't happen\n");
1521 static void test_impersonation(void)
1523 HANDLE hClientToken;
1524 HANDLE hProcessToken;
1525 BOOL ret;
1527 if( !pDuplicateTokenEx ) {
1528 skip("DuplicateTokenEx not found\n");
1529 return;
1532 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hProcessToken);
1533 if (!ret)
1535 skip("couldn't open process token, skipping impersonation tests\n");
1536 return;
1539 if (!get_privilege_count(hProcessToken) || are_all_privileges_disabled(hProcessToken))
1541 skip("token didn't have any privileges or they were all disabled. token not suitable for impersonation tests\n");
1542 CloseHandle(hProcessToken);
1543 return;
1545 CloseHandle(hProcessToken);
1547 test_ImpersonateNamedPipeClient(NULL, 0, FALSE, test_no_sqos_no_token);
1548 hClientToken = make_impersonation_token(TOKEN_IMPERSONATE | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, SecurityImpersonation);
1549 test_ImpersonateNamedPipeClient(hClientToken, 0, FALSE, test_no_sqos);
1550 CloseHandle(hClientToken);
1551 hClientToken = make_impersonation_token(TOKEN_IMPERSONATE | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, SecurityImpersonation);
1552 test_ImpersonateNamedPipeClient(hClientToken,
1553 SECURITY_SQOS_PRESENT | SECURITY_IMPERSONATION, FALSE,
1554 test_static_context);
1555 CloseHandle(hClientToken);
1556 hClientToken = make_impersonation_token(TOKEN_IMPERSONATE | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, SecurityImpersonation);
1557 test_ImpersonateNamedPipeClient(hClientToken,
1558 SECURITY_SQOS_PRESENT | SECURITY_CONTEXT_TRACKING | SECURITY_IMPERSONATION,
1559 FALSE, test_dynamic_context);
1560 CloseHandle(hClientToken);
1561 test_ImpersonateNamedPipeClient(NULL,
1562 SECURITY_SQOS_PRESENT | SECURITY_CONTEXT_TRACKING | SECURITY_IMPERSONATION,
1563 FALSE, test_dynamic_context_no_token);
1565 hClientToken = make_impersonation_token(TOKEN_IMPERSONATE | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, SecurityImpersonation);
1566 test_ImpersonateNamedPipeClient(hClientToken, 0, TRUE, test_no_sqos_revert);
1567 CloseHandle(hClientToken);
1568 hClientToken = make_impersonation_token(TOKEN_IMPERSONATE | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, SecurityImpersonation);
1569 test_ImpersonateNamedPipeClient(hClientToken,
1570 SECURITY_SQOS_PRESENT | SECURITY_IMPERSONATION, TRUE,
1571 test_static_context_revert);
1572 CloseHandle(hClientToken);
1573 hClientToken = make_impersonation_token(TOKEN_IMPERSONATE | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, SecurityImpersonation);
1574 test_ImpersonateNamedPipeClient(hClientToken,
1575 SECURITY_SQOS_PRESENT | SECURITY_CONTEXT_TRACKING | SECURITY_IMPERSONATION,
1576 TRUE, test_dynamic_context_revert);
1577 CloseHandle(hClientToken);
1580 struct overlapped_server_args
1582 HANDLE pipe_created;
1585 static DWORD CALLBACK overlapped_server(LPVOID arg)
1587 OVERLAPPED ol;
1588 HANDLE pipe;
1589 int ret, err;
1590 struct overlapped_server_args *a = arg;
1591 DWORD num;
1592 char buf[100];
1594 pipe = CreateNamedPipeA("\\\\.\\pipe\\my pipe", FILE_FLAG_OVERLAPPED | PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE, 1, 0, 0, 100000, NULL);
1595 ok(pipe != NULL, "pipe NULL\n");
1597 ol.hEvent = CreateEventA(0, 1, 0, 0);
1598 ok(ol.hEvent != NULL, "event NULL\n");
1599 ret = ConnectNamedPipe(pipe, &ol);
1600 err = GetLastError();
1601 ok(ret == 0, "ret %d\n", ret);
1602 ok(err == ERROR_IO_PENDING, "gle %d\n", err);
1603 SetEvent(a->pipe_created);
1605 ret = WaitForSingleObjectEx(ol.hEvent, INFINITE, 1);
1606 ok(ret == WAIT_OBJECT_0, "ret %x\n", ret);
1608 ret = GetOverlappedResult(pipe, &ol, &num, 1);
1609 ok(ret == 1, "ret %d\n", ret);
1611 /* This should block */
1612 ret = ReadFile(pipe, buf, sizeof(buf), &num, NULL);
1613 ok(ret == 1, "ret %d\n", ret);
1615 DisconnectNamedPipe(pipe);
1616 CloseHandle(ol.hEvent);
1617 CloseHandle(pipe);
1618 return 1;
1621 static void test_overlapped(void)
1623 DWORD tid, num;
1624 HANDLE thread, pipe;
1625 BOOL ret;
1626 struct overlapped_server_args args;
1628 args.pipe_created = CreateEventA(0, 1, 0, 0);
1629 thread = CreateThread(NULL, 0, overlapped_server, &args, 0, &tid);
1631 WaitForSingleObject(args.pipe_created, INFINITE);
1632 pipe = CreateFileA("\\\\.\\pipe\\my pipe", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
1633 ok(pipe != INVALID_HANDLE_VALUE, "cf failed\n");
1635 /* Sleep to try to get the ReadFile in the server to occur before the following WriteFile */
1636 Sleep(1);
1638 ret = WriteFile(pipe, "x", 1, &num, NULL);
1639 ok(ret, "WriteFile failed with error %d\n", GetLastError());
1641 WaitForSingleObject(thread, INFINITE);
1642 CloseHandle(pipe);
1643 CloseHandle(args.pipe_created);
1644 CloseHandle(thread);
1647 static void test_NamedPipeHandleState(void)
1649 HANDLE server, client;
1650 BOOL ret;
1651 DWORD state, instances, maxCollectionCount, collectDataTimeout;
1652 char userName[MAX_PATH];
1654 server = CreateNamedPipeA(PIPENAME, PIPE_ACCESS_DUPLEX,
1655 /* dwOpenMode */ PIPE_TYPE_BYTE | PIPE_WAIT,
1656 /* nMaxInstances */ 1,
1657 /* nOutBufSize */ 1024,
1658 /* nInBufSize */ 1024,
1659 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
1660 /* lpSecurityAttrib */ NULL);
1661 ok(server != INVALID_HANDLE_VALUE, "cf failed\n");
1662 ret = GetNamedPipeHandleStateA(server, NULL, NULL, NULL, NULL, NULL, 0);
1663 ok(ret, "GetNamedPipeHandleState failed: %d\n", GetLastError());
1664 ret = GetNamedPipeHandleStateA(server, &state, &instances, NULL, NULL, NULL,
1666 ok(ret, "GetNamedPipeHandleState failed: %d\n", GetLastError());
1667 if (ret)
1669 ok(state == 0, "unexpected state %08x\n", state);
1670 ok(instances == 1, "expected 1 instances, got %d\n", instances);
1672 /* Some parameters have no meaning, and therefore can't be retrieved,
1673 * on a local pipe.
1675 SetLastError(0xdeadbeef);
1676 ret = GetNamedPipeHandleStateA(server, &state, &instances,
1677 &maxCollectionCount, &collectDataTimeout, userName,
1678 sizeof(userName) / sizeof(userName[0]));
1679 todo_wine
1680 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
1681 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1682 /* A byte-mode pipe server can't be changed to message mode. */
1683 state = PIPE_READMODE_MESSAGE;
1684 SetLastError(0xdeadbeef);
1685 ret = SetNamedPipeHandleState(server, &state, NULL, NULL);
1686 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
1687 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1689 client = CreateFileA(PIPENAME, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1690 OPEN_EXISTING, 0, NULL);
1691 ok(client != INVALID_HANDLE_VALUE, "cf failed\n");
1693 state = PIPE_READMODE_BYTE;
1694 ret = SetNamedPipeHandleState(client, &state, NULL, NULL);
1695 ok(ret, "SetNamedPipeHandleState failed: %d\n", GetLastError());
1696 /* A byte-mode pipe client can't be changed to message mode, either. */
1697 state = PIPE_READMODE_MESSAGE;
1698 SetLastError(0xdeadbeef);
1699 ret = SetNamedPipeHandleState(server, &state, NULL, NULL);
1700 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
1701 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1703 CloseHandle(client);
1704 CloseHandle(server);
1706 server = CreateNamedPipeA(PIPENAME, PIPE_ACCESS_DUPLEX,
1707 /* dwOpenMode */ PIPE_TYPE_MESSAGE | PIPE_WAIT,
1708 /* nMaxInstances */ 1,
1709 /* nOutBufSize */ 1024,
1710 /* nInBufSize */ 1024,
1711 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
1712 /* lpSecurityAttrib */ NULL);
1713 ok(server != INVALID_HANDLE_VALUE, "cf failed\n");
1714 ret = GetNamedPipeHandleStateA(server, NULL, NULL, NULL, NULL, NULL, 0);
1715 ok(ret, "GetNamedPipeHandleState failed: %d\n", GetLastError());
1716 ret = GetNamedPipeHandleStateA(server, &state, &instances, NULL, NULL, NULL,
1718 ok(ret, "GetNamedPipeHandleState failed: %d\n", GetLastError());
1719 if (ret)
1721 ok(state == 0, "unexpected state %08x\n", state);
1722 ok(instances == 1, "expected 1 instances, got %d\n", instances);
1724 /* In contrast to byte-mode pipes, a message-mode pipe server can be
1725 * changed to byte mode.
1727 state = PIPE_READMODE_BYTE;
1728 ret = SetNamedPipeHandleState(server, &state, NULL, NULL);
1729 ok(ret, "SetNamedPipeHandleState failed: %d\n", GetLastError());
1731 client = CreateFileA(PIPENAME, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1732 OPEN_EXISTING, 0, NULL);
1733 ok(client != INVALID_HANDLE_VALUE, "cf failed\n");
1735 state = PIPE_READMODE_MESSAGE;
1736 ret = SetNamedPipeHandleState(client, &state, NULL, NULL);
1737 ok(ret, "SetNamedPipeHandleState failed: %d\n", GetLastError());
1738 /* A message-mode pipe client can also be changed to byte mode.
1740 state = PIPE_READMODE_BYTE;
1741 ret = SetNamedPipeHandleState(client, &state, NULL, NULL);
1742 ok(ret, "SetNamedPipeHandleState failed: %d\n", GetLastError());
1744 CloseHandle(client);
1745 CloseHandle(server);
1748 static void test_readfileex_pending(void)
1750 HANDLE server, client, event;
1751 BOOL ret;
1752 DWORD err, wait, num_bytes;
1753 OVERLAPPED overlapped;
1754 char read_buf[1024];
1755 char write_buf[1024];
1756 const char test_string[] = "test";
1757 int i;
1759 server = CreateNamedPipeA(PIPENAME, FILE_FLAG_OVERLAPPED | PIPE_ACCESS_DUPLEX,
1760 /* dwOpenMode */ PIPE_TYPE_BYTE | PIPE_WAIT,
1761 /* nMaxInstances */ 1,
1762 /* nOutBufSize */ 1024,
1763 /* nInBufSize */ 1024,
1764 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
1765 /* lpSecurityAttrib */ NULL);
1766 ok(server != INVALID_HANDLE_VALUE, "cf failed\n");
1768 event = CreateEventA(NULL, TRUE, FALSE, NULL);
1769 ok(event != NULL, "CreateEventA failed\n");
1771 memset(&overlapped, 0, sizeof(overlapped));
1772 overlapped.hEvent = event;
1774 ret = ConnectNamedPipe(server, &overlapped);
1775 err = GetLastError();
1776 ok(ret == FALSE, "ConnectNamedPipe succeeded\n");
1777 ok(err == ERROR_IO_PENDING, "ConnectNamedPipe set error %i\n", err);
1779 wait = WaitForSingleObject(event, 0);
1780 ok(wait == WAIT_TIMEOUT, "WaitForSingleObject returned %x\n", wait);
1782 client = CreateFileA(PIPENAME, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1783 OPEN_EXISTING, 0, NULL);
1784 ok(client != INVALID_HANDLE_VALUE, "cf failed\n");
1786 wait = WaitForSingleObject(event, 0);
1787 ok(wait == WAIT_OBJECT_0, "WaitForSingleObject returned %x\n", wait);
1789 /* Start a read that can't complete immediately. */
1790 completion_called = 0;
1791 ResetEvent(event);
1792 ret = ReadFileEx(server, read_buf, sizeof(read_buf), &overlapped, completion_routine);
1793 ok(ret == TRUE, "ReadFileEx failed, err=%i\n", GetLastError());
1794 ok(completion_called == 0, "completion routine called before ReadFileEx returned\n");
1796 ret = WriteFile(client, test_string, strlen(test_string), &num_bytes, NULL);
1797 ok(ret == TRUE, "WriteFile failed\n");
1798 ok(num_bytes == strlen(test_string), "only %i bytes written\n", num_bytes);
1800 ok(completion_called == 0, "completion routine called during WriteFile\n");
1802 wait = WaitForSingleObjectEx(event, 0, TRUE);
1803 ok(wait == WAIT_IO_COMPLETION || wait == WAIT_OBJECT_0, "WaitForSingleObjectEx returned %x\n", wait);
1805 ok(completion_called == 1, "completion not called after writing pipe\n");
1806 ok(completion_errorcode == 0, "completion called with error %x\n", completion_errorcode);
1807 ok(completion_num_bytes == strlen(test_string), "ReadFileEx returned only %d bytes\n", completion_num_bytes);
1808 ok(completion_lpoverlapped == &overlapped, "completion called with wrong overlapped pointer\n");
1809 ok(!memcmp(test_string, read_buf, strlen(test_string)), "ReadFileEx read wrong bytes\n");
1811 /* Make writes until the pipe is full and the write fails */
1812 memset(write_buf, 0xaa, sizeof(write_buf));
1813 for (i=0; i<256; i++)
1815 completion_called = 0;
1816 ResetEvent(event);
1817 ret = WriteFileEx(server, write_buf, sizeof(write_buf), &overlapped, completion_routine);
1818 err = GetLastError();
1820 ok(completion_called == 0, "completion routine called during WriteFileEx\n");
1822 wait = WaitForSingleObjectEx(event, 0, TRUE);
1824 if (wait == WAIT_TIMEOUT)
1825 /* write couldn't complete immediately, presumably the pipe is full */
1826 break;
1828 ok(wait == WAIT_IO_COMPLETION || wait == WAIT_OBJECT_0, "WaitForSingleObject returned %x\n", wait);
1830 ok(ret == TRUE, "WriteFileEx failed, err=%i\n", err);
1831 ok(completion_errorcode == 0, "completion called with error %x\n", completion_errorcode);
1832 ok(completion_lpoverlapped == &overlapped, "completion called with wrong overlapped pointer\n");
1835 ok(ret == TRUE, "WriteFileEx failed, err=%i\n", err);
1836 ok(completion_called == 0, "completion routine called but wait timed out\n");
1837 ok(completion_errorcode == 0, "completion called with error %x\n", completion_errorcode);
1838 ok(completion_lpoverlapped == &overlapped, "completion called with wrong overlapped pointer\n");
1840 /* free up some space in the pipe */
1841 ret = ReadFile(client, read_buf, sizeof(read_buf), &num_bytes, NULL);
1842 ok(ret == TRUE, "ReadFile failed\n");
1844 ok(completion_called == 0, "completion routine called during ReadFile\n");
1846 wait = WaitForSingleObjectEx(event, 0, TRUE);
1847 ok(wait == WAIT_IO_COMPLETION || wait == WAIT_OBJECT_0, "WaitForSingleObject returned %x\n", wait);
1849 ok(completion_called == 1, "completion routine not called\n");
1850 ok(completion_errorcode == 0, "completion called with error %x\n", completion_errorcode);
1851 ok(completion_lpoverlapped == &overlapped, "completion called with wrong overlapped pointer\n");
1853 num_bytes = 0xdeadbeef;
1854 SetLastError(0xdeadbeef);
1855 ret = ReadFile(INVALID_HANDLE_VALUE, read_buf, 0, &num_bytes, NULL);
1856 ok(!ret, "ReadFile should fail\n");
1857 ok(GetLastError() == ERROR_INVALID_HANDLE, "wrong error %u\n", GetLastError());
1858 ok(num_bytes == 0, "expected 0, got %u\n", num_bytes);
1860 S(U(overlapped)).Offset = 0;
1861 S(U(overlapped)).OffsetHigh = 0;
1862 overlapped.Internal = -1;
1863 overlapped.InternalHigh = -1;
1864 overlapped.hEvent = event;
1865 num_bytes = 0xdeadbeef;
1866 SetLastError(0xdeadbeef);
1867 ret = ReadFile(server, read_buf, 0, &num_bytes, &overlapped);
1868 todo_wine
1869 ok(GetLastError() == ERROR_IO_PENDING, "expected ERROR_IO_PENDING, got %d\n", GetLastError());
1870 ok(num_bytes == 0, "bytes %u\n", num_bytes);
1871 ok((NTSTATUS)overlapped.Internal == STATUS_PENDING, "expected STATUS_PENDING, got %#lx\n", overlapped.Internal);
1872 todo_wine
1873 ok(overlapped.InternalHigh == -1, "expected -1, got %lu\n", overlapped.InternalHigh);
1875 wait = WaitForSingleObject(event, 100);
1876 ok(wait == WAIT_TIMEOUT, "WaitForSingleObject returned %x\n", wait);
1878 num_bytes = 0xdeadbeef;
1879 ret = WriteFile(client, test_string, 1, &num_bytes, NULL);
1880 ok(ret, "WriteFile failed\n");
1881 ok(num_bytes == 1, "bytes %u\n", num_bytes);
1883 wait = WaitForSingleObject(event, 100);
1884 todo_wine
1885 ok(wait == WAIT_OBJECT_0, "WaitForSingleObject returned %x\n", wait);
1887 ok(num_bytes == 1, "bytes %u\n", num_bytes);
1888 todo_wine
1889 ok((NTSTATUS)overlapped.Internal == STATUS_SUCCESS, "expected STATUS_SUCCESS, got %#lx\n", overlapped.Internal);
1890 ok(overlapped.InternalHigh == 0, "expected 0, got %lu\n", overlapped.InternalHigh);
1892 /* read the pending byte and clear the pipe */
1893 num_bytes = 0xdeadbeef;
1894 ret = ReadFile(server, read_buf, 1, &num_bytes, &overlapped);
1895 ok(ret, "ReadFile failed\n");
1896 ok(num_bytes == 1, "bytes %u\n", num_bytes);
1898 CloseHandle(client);
1899 CloseHandle(server);
1900 CloseHandle(event);
1903 START_TEST(pipe)
1905 HMODULE hmod;
1907 hmod = GetModuleHandleA("advapi32.dll");
1908 pDuplicateTokenEx = (void *) GetProcAddress(hmod, "DuplicateTokenEx");
1909 hmod = GetModuleHandleA("kernel32.dll");
1910 pQueueUserAPC = (void *) GetProcAddress(hmod, "QueueUserAPC");
1912 if (test_DisconnectNamedPipe())
1913 return;
1914 test_CreateNamedPipe_instances_must_match();
1915 test_NamedPipe_2();
1916 test_CreateNamedPipe(PIPE_TYPE_BYTE);
1917 test_CreateNamedPipe(PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE);
1918 test_CreatePipe();
1919 test_impersonation();
1920 test_overlapped();
1921 test_NamedPipeHandleState();
1922 test_readfileex_pending();