crypt32: Add additional path for Solaris 11 Express.
[wine] / dlls / kernel32 / tests / pipe.c
1 /*
2  * Unit tests for named pipe functions in Wine
3  *
4  * Copyright (c) 2002 Dan Kegel
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <assert.h>
22 #include <stdarg.h>
23 #include <stdio.h>
24
25 #include <windef.h>
26 #include <winbase.h>
27 #include <winsock.h>
28 #include <wtypes.h>
29 #include <winerror.h>
30
31 #include "wine/test.h"
32
33 #define PIPENAME "\\\\.\\PiPe\\tests_pipe.c"
34
35 #define NB_SERVER_LOOPS 8
36
37 static HANDLE alarm_event;
38 static BOOL (WINAPI *pDuplicateTokenEx)(HANDLE,DWORD,LPSECURITY_ATTRIBUTES,
39                                         SECURITY_IMPERSONATION_LEVEL,TOKEN_TYPE,PHANDLE);
40
41
42 static void test_CreateNamedPipe(int pipemode)
43 {
44     HANDLE hnp;
45     HANDLE hFile;
46     static const char obuf[] = "Bit Bucket";
47     static const char obuf2[] = "More bits";
48     char ibuf[32], *pbuf;
49     DWORD written;
50     DWORD readden;
51     DWORD avail;
52     DWORD lpmode;
53     BOOL ret;
54
55     if (pipemode == PIPE_TYPE_BYTE)
56         trace("test_CreateNamedPipe starting in byte mode\n");
57     else
58         trace("test_CreateNamedPipe starting in message mode\n");
59     /* Bad parameter checks */
60     hnp = CreateNamedPipe("not a named pipe", PIPE_ACCESS_DUPLEX, pipemode | PIPE_WAIT,
61         /* nMaxInstances */ 1,
62         /* nOutBufSize */ 1024,
63         /* nInBufSize */ 1024,
64         /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
65         /* lpSecurityAttrib */ NULL);
66     ok(hnp == INVALID_HANDLE_VALUE && GetLastError() == ERROR_INVALID_NAME,
67         "CreateNamedPipe should fail if name doesn't start with \\\\.\\pipe\n");
68
69     hnp = CreateNamedPipe(NULL,
70         PIPE_ACCESS_DUPLEX, pipemode | PIPE_WAIT,
71         1, 1024, 1024, NMPWAIT_USE_DEFAULT_WAIT, NULL);
72     ok(hnp == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
73         "CreateNamedPipe should fail if name is NULL\n");
74
75     hFile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
76     ok(hFile == INVALID_HANDLE_VALUE
77         && GetLastError() == ERROR_FILE_NOT_FOUND,
78         "connecting to nonexistent named pipe should fail with ERROR_FILE_NOT_FOUND\n");
79
80     /* Functional checks */
81
82     hnp = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, pipemode | PIPE_WAIT,
83         /* nMaxInstances */ 1,
84         /* nOutBufSize */ 1024,
85         /* nInBufSize */ 1024,
86         /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
87         /* lpSecurityAttrib */ NULL);
88     ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
89
90     ret = WaitNamedPipeA(PIPENAME, 2000);
91     ok(ret, "WaitNamedPipe failed (%d)\n", GetLastError());
92
93     hFile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
94     ok(hFile != INVALID_HANDLE_VALUE, "CreateFile failed (%d)\n", GetLastError());
95
96     ok(!WaitNamedPipeA(PIPENAME, 1000), "WaitNamedPipe succeeded\n");
97
98     ok(GetLastError() == ERROR_SEM_TIMEOUT, "wrong error %u\n", GetLastError());
99
100     /* don't try to do i/o if one side couldn't be opened, as it hangs */
101     if (hFile != INVALID_HANDLE_VALUE) {
102         HANDLE hFile2;
103
104         /* Make sure we can read and write a few bytes in both directions */
105         memset(ibuf, 0, sizeof(ibuf));
106         ok(WriteFile(hnp, obuf, sizeof(obuf), &written, NULL), "WriteFile\n");
107         ok(written == sizeof(obuf), "write file len 1\n");
108         ok(PeekNamedPipe(hFile, NULL, 0, NULL, &readden, NULL), "Peek\n");
109         ok(readden == sizeof(obuf), "peek 1 got %d bytes\n", readden);
110         ok(ReadFile(hFile, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
111         ok(readden == sizeof(obuf), "read 1 got %d bytes\n", readden);
112         ok(memcmp(obuf, ibuf, written) == 0, "content 1 check\n");
113
114         memset(ibuf, 0, sizeof(ibuf));
115         ok(WriteFile(hFile, obuf2, sizeof(obuf2), &written, NULL), "WriteFile\n");
116         ok(written == sizeof(obuf2), "write file len 2\n");
117         ok(PeekNamedPipe(hnp, NULL, 0, NULL, &readden, NULL), "Peek\n");
118         ok(readden == sizeof(obuf2), "peek 2 got %d bytes\n", readden);
119         ok(PeekNamedPipe(hnp, (LPVOID)1, 0, NULL, &readden, NULL), "Peek\n");
120         ok(readden == sizeof(obuf2), "peek 2 got %d bytes\n", readden);
121         ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
122         ok(readden == sizeof(obuf2), "read 2 got %d bytes\n", readden);
123         ok(memcmp(obuf2, ibuf, written) == 0, "content 2 check\n");
124
125         /* Test reading of multiple writes */
126         memset(ibuf, 0, sizeof(ibuf));
127         ok(WriteFile(hnp, obuf, sizeof(obuf), &written, NULL), "WriteFile3a\n");
128         ok(written == sizeof(obuf), "write file len 3a\n");
129         ok(WriteFile(hnp, obuf2, sizeof(obuf2), &written, NULL), " WriteFile3b\n");
130         ok(written == sizeof(obuf2), "write file len 3b\n");
131         ok(PeekNamedPipe(hFile, ibuf, sizeof(ibuf), &readden, &avail, NULL), "Peek3\n");
132         if (pipemode == PIPE_TYPE_BYTE) {
133             if (readden != sizeof(obuf))  /* Linux only returns the first message */
134                 ok(readden == sizeof(obuf) + sizeof(obuf2), "peek3 got %d bytes\n", readden);
135             else
136                 todo_wine ok(readden == sizeof(obuf) + sizeof(obuf2), "peek3 got %d bytes\n", readden);
137         }
138         else
139         {
140             if (readden != sizeof(obuf) + sizeof(obuf2))  /* MacOS returns both messages */
141                 ok(readden == sizeof(obuf), "peek3 got %d bytes\n", readden);
142             else
143                 todo_wine ok(readden == sizeof(obuf), "peek3 got %d bytes\n", readden);
144         }
145         if (avail != sizeof(obuf)) /* older Linux kernels only return the first write here */
146             ok(avail == sizeof(obuf) + sizeof(obuf2), "peek3 got %d bytes available\n", avail);
147         pbuf = ibuf;
148         ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "pipe content 3a check\n");
149         if (pipemode == PIPE_TYPE_BYTE && readden >= sizeof(obuf)+sizeof(obuf2)) {
150             pbuf += sizeof(obuf);
151             ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "pipe content 3b check\n");
152         }
153         ok(ReadFile(hFile, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
154         ok(readden == sizeof(obuf) + sizeof(obuf2), "read 3 got %d bytes\n", readden);
155         pbuf = ibuf;
156         ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 3a check\n");
157         pbuf += sizeof(obuf);
158         ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "content 3b check\n");
159
160         /* Multiple writes in the reverse direction */
161         memset(ibuf, 0, sizeof(ibuf));
162         ok(WriteFile(hFile, obuf, sizeof(obuf), &written, NULL), "WriteFile4a\n");
163         ok(written == sizeof(obuf), "write file len 4a\n");
164         ok(WriteFile(hFile, obuf2, sizeof(obuf2), &written, NULL), " WriteFile4b\n");
165         ok(written == sizeof(obuf2), "write file len 4b\n");
166         ok(PeekNamedPipe(hnp, ibuf, sizeof(ibuf), &readden, &avail, NULL), "Peek4\n");
167         if (pipemode == PIPE_TYPE_BYTE) {
168             if (readden != sizeof(obuf))  /* Linux only returns the first message */
169                 /* should return all 23 bytes */
170                 ok(readden == sizeof(obuf) + sizeof(obuf2), "peek4 got %d bytes\n", readden);
171             else
172                 todo_wine ok(readden == sizeof(obuf) + sizeof(obuf2), "peek4 got %d bytes\n", readden);
173         }
174         else
175         {
176             if (readden != sizeof(obuf) + sizeof(obuf2))  /* MacOS returns both messages */
177                 ok(readden == sizeof(obuf), "peek4 got %d bytes\n", readden);
178             else
179                 todo_wine ok(readden == sizeof(obuf), "peek4 got %d bytes\n", readden);
180         }
181         if (avail != sizeof(obuf)) /* older Linux kernels only return the first write here */
182             ok(avail == sizeof(obuf) + sizeof(obuf2), "peek4 got %d bytes available\n", avail);
183         pbuf = ibuf;
184         ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "pipe content 4a check\n");
185         if (pipemode == PIPE_TYPE_BYTE && readden >= sizeof(obuf)+sizeof(obuf2)) {
186             pbuf += sizeof(obuf);
187             ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "pipe content 4b check\n");
188         }
189         ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
190         if (pipemode == PIPE_TYPE_BYTE) {
191             ok(readden == sizeof(obuf) + sizeof(obuf2), "read 4 got %d bytes\n", readden);
192         }
193         else {
194             todo_wine {
195                 ok(readden == sizeof(obuf), "read 4 got %d bytes\n", readden);
196             }
197         }
198         pbuf = ibuf;
199         ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 4a check\n");
200         if (pipemode == PIPE_TYPE_BYTE) {
201             pbuf += sizeof(obuf);
202             ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "content 4b check\n");
203         }
204
205         /* Test reading of multiple writes after a mode change
206           (CreateFile always creates a byte mode pipe) */
207         lpmode = PIPE_READMODE_MESSAGE;
208         if (pipemode == PIPE_TYPE_BYTE) {
209             /* trying to change the client end of a byte pipe to message mode should fail */
210             ok(!SetNamedPipeHandleState(hFile, &lpmode, NULL, NULL), "Change mode\n");
211         }
212         else {
213             todo_wine {
214                 ok(SetNamedPipeHandleState(hFile, &lpmode, NULL, NULL), "Change mode\n");
215             }
216         
217             memset(ibuf, 0, sizeof(ibuf));
218             ok(WriteFile(hnp, obuf, sizeof(obuf), &written, NULL), "WriteFile5a\n");
219             ok(written == sizeof(obuf), "write file len 3a\n");
220             ok(WriteFile(hnp, obuf2, sizeof(obuf2), &written, NULL), " WriteFile5b\n");
221             ok(written == sizeof(obuf2), "write file len 3b\n");
222             ok(PeekNamedPipe(hFile, ibuf, sizeof(ibuf), &readden, &avail, NULL), "Peek5\n");
223             if (readden != sizeof(obuf) + sizeof(obuf2))  /* MacOS returns both writes */
224                 ok(readden == sizeof(obuf), "peek5 got %d bytes\n", readden);
225             else
226                 todo_wine ok(readden == sizeof(obuf), "peek5 got %d bytes\n", readden);
227             if (avail != sizeof(obuf)) /* older Linux kernels only return the first write here */
228                 ok(avail == sizeof(obuf) + sizeof(obuf2), "peek5 got %d bytes available\n", avail);
229             else
230                 todo_wine ok(avail == sizeof(obuf) + sizeof(obuf2), "peek5 got %d bytes available\n", avail);
231             pbuf = ibuf;
232             ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 5a check\n");
233             ok(ReadFile(hFile, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
234             todo_wine {
235                 ok(readden == sizeof(obuf), "read 5 got %d bytes\n", readden);
236             }
237             pbuf = ibuf;
238             ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 5a check\n");
239     
240             /* Multiple writes in the reverse direction */
241             /* the write of obuf2 from write4 should still be in the buffer */
242             ok(PeekNamedPipe(hnp, ibuf, sizeof(ibuf), &readden, &avail, NULL), "Peek6a\n");
243             todo_wine {
244                 ok(readden == sizeof(obuf2), "peek6a got %d bytes\n", readden);
245                 ok(avail == sizeof(obuf2), "peek6a got %d bytes available\n", avail);
246             }
247             if (avail > 0) {
248                 ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
249                 ok(readden == sizeof(obuf2), "read 6a got %d bytes\n", readden);
250                 pbuf = ibuf;
251                 ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "content 6a check\n");
252             }
253             memset(ibuf, 0, sizeof(ibuf));
254             ok(WriteFile(hFile, obuf, sizeof(obuf), &written, NULL), "WriteFile6a\n");
255             ok(written == sizeof(obuf), "write file len 6a\n");
256             ok(WriteFile(hFile, obuf2, sizeof(obuf2), &written, NULL), " WriteFile6b\n");
257             ok(written == sizeof(obuf2), "write file len 6b\n");
258             ok(PeekNamedPipe(hnp, ibuf, sizeof(ibuf), &readden, &avail, NULL), "Peek6\n");
259             if (readden != sizeof(obuf) + sizeof(obuf2))  /* MacOS returns both writes */
260                 ok(readden == sizeof(obuf), "peek6 got %d bytes\n", readden);
261             else
262                 todo_wine ok(readden == sizeof(obuf), "peek6 got %d bytes\n", readden);
263             if (avail != sizeof(obuf)) /* older Linux kernels only return the first write here */
264                 ok(avail == sizeof(obuf) + sizeof(obuf2), "peek6b got %d bytes available\n", avail);
265             pbuf = ibuf;
266             ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 6a check\n");
267             ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
268             todo_wine {
269                 ok(readden == sizeof(obuf), "read 6b got %d bytes\n", readden);
270             }
271             pbuf = ibuf;
272             ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 6a check\n");
273         }
274
275         /* Picky conformance tests */
276
277         /* Verify that you can't connect to pipe again
278          * until server calls DisconnectNamedPipe+ConnectNamedPipe
279          * or creates a new pipe
280          * case 1: other client not yet closed
281          */
282         hFile2 = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
283         ok(hFile2 == INVALID_HANDLE_VALUE,
284             "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail\n");
285         ok(GetLastError() == ERROR_PIPE_BUSY,
286             "connecting to named pipe before other client closes should fail with ERROR_PIPE_BUSY\n");
287
288         ok(CloseHandle(hFile), "CloseHandle\n");
289
290         /* case 2: other client already closed */
291         hFile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
292         ok(hFile == INVALID_HANDLE_VALUE,
293             "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail\n");
294         ok(GetLastError() == ERROR_PIPE_BUSY,
295             "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail with ERROR_PIPE_BUSY\n");
296
297         ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe\n");
298
299         /* case 3: server has called DisconnectNamedPipe but not ConnectNamed Pipe */
300         hFile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
301         ok(hFile == 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 after other client closes but before ConnectNamedPipe should fail with ERROR_PIPE_BUSY\n");
305
306         /* to be complete, we'd call ConnectNamedPipe here and loop,
307          * but by default that's blocking, so we'd either have
308          * to turn on the uncommon nonblocking mode, or
309          * use another thread.
310          */
311     }
312
313     ok(CloseHandle(hnp), "CloseHandle\n");
314
315     trace("test_CreateNamedPipe returning\n");
316 }
317
318 static void test_CreateNamedPipe_instances_must_match(void)
319 {
320     HANDLE hnp, hnp2;
321
322     /* Check no mismatch */
323     hnp = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
324         /* nMaxInstances */ 2,
325         /* nOutBufSize */ 1024,
326         /* nInBufSize */ 1024,
327         /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
328         /* lpSecurityAttrib */ NULL);
329     ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
330
331     hnp2 = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
332         /* nMaxInstances */ 2,
333         /* nOutBufSize */ 1024,
334         /* nInBufSize */ 1024,
335         /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
336         /* lpSecurityAttrib */ NULL);
337     ok(hnp2 != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
338
339     ok(CloseHandle(hnp), "CloseHandle\n");
340     ok(CloseHandle(hnp2), "CloseHandle\n");
341
342     /* Check nMaxInstances */
343     hnp = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
344         /* nMaxInstances */ 1,
345         /* nOutBufSize */ 1024,
346         /* nInBufSize */ 1024,
347         /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
348         /* lpSecurityAttrib */ NULL);
349     ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
350
351     hnp2 = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
352         /* nMaxInstances */ 1,
353         /* nOutBufSize */ 1024,
354         /* nInBufSize */ 1024,
355         /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
356         /* lpSecurityAttrib */ NULL);
357     ok(hnp2 == INVALID_HANDLE_VALUE
358         && GetLastError() == ERROR_PIPE_BUSY, "nMaxInstances not obeyed\n");
359
360     ok(CloseHandle(hnp), "CloseHandle\n");
361
362     /* Check PIPE_ACCESS_* */
363     hnp = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
364         /* nMaxInstances */ 2,
365         /* nOutBufSize */ 1024,
366         /* nInBufSize */ 1024,
367         /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
368         /* lpSecurityAttrib */ NULL);
369     ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
370
371     hnp2 = CreateNamedPipe(PIPENAME, PIPE_ACCESS_INBOUND, PIPE_TYPE_BYTE | PIPE_WAIT,
372         /* nMaxInstances */ 1,
373         /* nOutBufSize */ 1024,
374         /* nInBufSize */ 1024,
375         /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
376         /* lpSecurityAttrib */ NULL);
377     ok(hnp2 == INVALID_HANDLE_VALUE
378         && GetLastError() == ERROR_ACCESS_DENIED, "PIPE_ACCESS_* mismatch allowed\n");
379
380     ok(CloseHandle(hnp), "CloseHandle\n");
381
382     /* etc, etc */
383 }
384
385 /** implementation of alarm() */
386 static DWORD CALLBACK alarmThreadMain(LPVOID arg)
387 {
388     DWORD_PTR timeout = (DWORD_PTR) arg;
389     trace("alarmThreadMain\n");
390     if (WaitForSingleObject( alarm_event, timeout ) == WAIT_TIMEOUT)
391     {
392         ok(FALSE, "alarm\n");
393         ExitProcess(1);
394     }
395     return 1;
396 }
397
398 static HANDLE hnp = INVALID_HANDLE_VALUE;
399
400 /** Trivial byte echo server - disconnects after each session */
401 static DWORD CALLBACK serverThreadMain1(LPVOID arg)
402 {
403     int i;
404
405     trace("serverThreadMain1 start\n");
406     /* Set up a simple echo server */
407     hnp = CreateNamedPipe(PIPENAME "serverThreadMain1", PIPE_ACCESS_DUPLEX,
408         PIPE_TYPE_BYTE | PIPE_WAIT,
409         /* nMaxInstances */ 1,
410         /* nOutBufSize */ 1024,
411         /* nInBufSize */ 1024,
412         /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
413         /* lpSecurityAttrib */ NULL);
414
415     ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
416     for (i = 0; i < NB_SERVER_LOOPS; i++) {
417         char buf[512];
418         DWORD written;
419         DWORD readden;
420         DWORD success;
421
422         /* Wait for client to connect */
423         trace("Server calling ConnectNamedPipe...\n");
424         ok(ConnectNamedPipe(hnp, NULL)
425             || GetLastError() == ERROR_PIPE_CONNECTED, "ConnectNamedPipe\n");
426         trace("ConnectNamedPipe returned.\n");
427
428         /* Echo bytes once */
429         memset(buf, 0, sizeof(buf));
430
431         trace("Server reading...\n");
432         success = ReadFile(hnp, buf, sizeof(buf), &readden, NULL);
433         trace("Server done reading.\n");
434         ok(success, "ReadFile\n");
435         ok(readden, "short read\n");
436
437         trace("Server writing...\n");
438         ok(WriteFile(hnp, buf, readden, &written, NULL), "WriteFile\n");
439         trace("Server done writing.\n");
440         ok(written == readden, "write file len\n");
441
442         /* finish this connection, wait for next one */
443         ok(FlushFileBuffers(hnp), "FlushFileBuffers\n");
444         trace("Server done flushing.\n");
445         ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe\n");
446         trace("Server done disconnecting.\n");
447     }
448     return 0;
449 }
450
451 /** Trivial byte echo server - closes after each connection */
452 static DWORD CALLBACK serverThreadMain2(LPVOID arg)
453 {
454     int i;
455     HANDLE hnpNext = 0;
456
457     trace("serverThreadMain2\n");
458     /* Set up a simple echo server */
459     hnp = CreateNamedPipe(PIPENAME "serverThreadMain2", PIPE_ACCESS_DUPLEX,
460         PIPE_TYPE_BYTE | PIPE_WAIT,
461         /* nMaxInstances */ 2,
462         /* nOutBufSize */ 1024,
463         /* nInBufSize */ 1024,
464         /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
465         /* lpSecurityAttrib */ NULL);
466     ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
467
468     for (i = 0; i < NB_SERVER_LOOPS; i++) {
469         char buf[512];
470         DWORD written;
471         DWORD readden;
472         DWORD success;
473
474         /* Wait for client to connect */
475         trace("Server calling ConnectNamedPipe...\n");
476         ok(ConnectNamedPipe(hnp, NULL)
477             || GetLastError() == ERROR_PIPE_CONNECTED, "ConnectNamedPipe\n");
478         trace("ConnectNamedPipe returned.\n");
479
480         /* Echo bytes once */
481         memset(buf, 0, sizeof(buf));
482
483         trace("Server reading...\n");
484         success = ReadFile(hnp, buf, sizeof(buf), &readden, NULL);
485         trace("Server done reading.\n");
486         ok(success, "ReadFile\n");
487
488         trace("Server writing...\n");
489         ok(WriteFile(hnp, buf, readden, &written, NULL), "WriteFile\n");
490         trace("Server done writing.\n");
491         ok(written == readden, "write file len\n");
492
493         /* finish this connection, wait for next one */
494         ok(FlushFileBuffers(hnp), "FlushFileBuffers\n");
495         ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe\n");
496
497         /* Set up next echo server */
498         hnpNext =
499             CreateNamedPipe(PIPENAME "serverThreadMain2", PIPE_ACCESS_DUPLEX,
500             PIPE_TYPE_BYTE | PIPE_WAIT,
501             /* nMaxInstances */ 2,
502             /* nOutBufSize */ 1024,
503             /* nInBufSize */ 1024,
504             /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
505             /* lpSecurityAttrib */ NULL);
506
507         ok(hnpNext != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
508
509         ok(CloseHandle(hnp), "CloseHandle\n");
510         hnp = hnpNext;
511     }
512     return 0;
513 }
514
515 /** Trivial byte echo server - uses overlapped named pipe calls */
516 static DWORD CALLBACK serverThreadMain3(LPVOID arg)
517 {
518     int i;
519     HANDLE hEvent;
520
521     trace("serverThreadMain3\n");
522     /* Set up a simple echo server */
523     hnp = CreateNamedPipe(PIPENAME "serverThreadMain3", PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
524         PIPE_TYPE_BYTE | PIPE_WAIT,
525         /* nMaxInstances */ 1,
526         /* nOutBufSize */ 1024,
527         /* nInBufSize */ 1024,
528         /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
529         /* lpSecurityAttrib */ NULL);
530     ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
531
532     hEvent = CreateEvent(NULL,  /* security attribute */
533         TRUE,                   /* manual reset event */
534         FALSE,                  /* initial state */
535         NULL);                  /* name */
536     ok(hEvent != NULL, "CreateEvent\n");
537
538     for (i = 0; i < NB_SERVER_LOOPS; i++) {
539         char buf[512];
540         DWORD written;
541         DWORD readden;
542         DWORD dummy;
543         DWORD success;
544         OVERLAPPED oOverlap;
545         int letWFSOEwait = (i & 2);
546         int letGORwait = (i & 1);
547         DWORD err;
548
549         memset(&oOverlap, 0, sizeof(oOverlap));
550         oOverlap.hEvent = hEvent;
551
552         /* Wait for client to connect */
553         if (i == 0) {
554             trace("Server calling non-overlapped ConnectNamedPipe on overlapped pipe...\n");
555             success = ConnectNamedPipe(hnp, NULL);
556             err = GetLastError();
557             ok(success || (err == ERROR_PIPE_CONNECTED), "ConnectNamedPipe failed: %d\n", err);
558             trace("ConnectNamedPipe operation complete.\n");
559         } else {
560             trace("Server calling overlapped ConnectNamedPipe...\n");
561             success = ConnectNamedPipe(hnp, &oOverlap);
562             err = GetLastError();
563             ok(!success && (err == ERROR_IO_PENDING || err == ERROR_PIPE_CONNECTED), "overlapped ConnectNamedPipe\n");
564             trace("overlapped ConnectNamedPipe returned.\n");
565             if (!success && (err == ERROR_IO_PENDING)) {
566                 if (letWFSOEwait)
567                 {
568                     DWORD ret;
569                     do {
570                         ret = WaitForSingleObjectEx(hEvent, INFINITE, TRUE);
571                     } while (ret == WAIT_IO_COMPLETION);
572                     ok(ret == 0, "wait ConnectNamedPipe returned %x\n", ret);
573                 }
574                 success = GetOverlappedResult(hnp, &oOverlap, &dummy, letGORwait);
575                 if (!letGORwait && !letWFSOEwait && !success) {
576                     ok(GetLastError() == ERROR_IO_INCOMPLETE, "GetOverlappedResult\n");
577                     success = GetOverlappedResult(hnp, &oOverlap, &dummy, TRUE);
578                 }
579             }
580             ok(success || (err == ERROR_PIPE_CONNECTED), "GetOverlappedResult ConnectNamedPipe\n");
581             trace("overlapped ConnectNamedPipe operation complete.\n");
582         }
583
584         /* Echo bytes once */
585         memset(buf, 0, sizeof(buf));
586
587         trace("Server reading...\n");
588         success = ReadFile(hnp, buf, sizeof(buf), &readden, &oOverlap);
589         trace("Server ReadFile returned...\n");
590         err = GetLastError();
591         ok(success || err == ERROR_IO_PENDING, "overlapped ReadFile\n");
592         trace("overlapped ReadFile returned.\n");
593         if (!success && (err == ERROR_IO_PENDING)) {
594             if (letWFSOEwait)
595             {
596                 DWORD ret;
597                 do {
598                     ret = WaitForSingleObjectEx(hEvent, INFINITE, TRUE);
599                 } while (ret == WAIT_IO_COMPLETION);
600                 ok(ret == 0, "wait ReadFile returned %x\n", ret);
601             }
602             success = GetOverlappedResult(hnp, &oOverlap, &readden, letGORwait);
603             if (!letGORwait && !letWFSOEwait && !success) {
604                 ok(GetLastError() == ERROR_IO_INCOMPLETE, "GetOverlappedResult\n");
605                 success = GetOverlappedResult(hnp, &oOverlap, &readden, TRUE);
606             }
607         }
608         trace("Server done reading.\n");
609         ok(success, "overlapped ReadFile\n");
610
611         trace("Server writing...\n");
612         success = WriteFile(hnp, buf, readden, &written, &oOverlap);
613         trace("Server WriteFile returned...\n");
614         err = GetLastError();
615         ok(success || err == ERROR_IO_PENDING, "overlapped WriteFile\n");
616         trace("overlapped WriteFile returned.\n");
617         if (!success && (err == ERROR_IO_PENDING)) {
618             if (letWFSOEwait)
619             {
620                 DWORD ret;
621                 do {
622                     ret = WaitForSingleObjectEx(hEvent, INFINITE, TRUE);
623                 } while (ret == WAIT_IO_COMPLETION);
624                 ok(ret == 0, "wait WriteFile returned %x\n", ret);
625             }
626             success = GetOverlappedResult(hnp, &oOverlap, &written, letGORwait);
627             if (!letGORwait && !letWFSOEwait && !success) {
628                 ok(GetLastError() == ERROR_IO_INCOMPLETE, "GetOverlappedResult\n");
629                 success = GetOverlappedResult(hnp, &oOverlap, &written, TRUE);
630             }
631         }
632         trace("Server done writing.\n");
633         ok(success, "overlapped WriteFile\n");
634         ok(written == readden, "write file len\n");
635
636         /* finish this connection, wait for next one */
637         ok(FlushFileBuffers(hnp), "FlushFileBuffers\n");
638         ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe\n");
639     }
640     return 0;
641 }
642
643 /** Trivial byte echo server - uses i/o completion ports */
644 static DWORD CALLBACK serverThreadMain4(LPVOID arg)
645 {
646     int i;
647     HANDLE hcompletion;
648     BOOL ret;
649
650     trace("serverThreadMain4\n");
651     /* Set up a simple echo server */
652     hnp = CreateNamedPipe(PIPENAME "serverThreadMain4", PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
653         PIPE_TYPE_BYTE | PIPE_WAIT,
654         /* nMaxInstances */ 1,
655         /* nOutBufSize */ 1024,
656         /* nInBufSize */ 1024,
657         /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
658         /* lpSecurityAttrib */ NULL);
659     ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
660
661     hcompletion = CreateIoCompletionPort(hnp, NULL, 12345, 1);
662     ok(hcompletion != NULL, "CreateIoCompletionPort failed, error=%i\n", GetLastError());
663
664     for (i = 0; i < NB_SERVER_LOOPS; i++) {
665         char buf[512];
666         DWORD written;
667         DWORD readden;
668         DWORD dummy;
669         DWORD success;
670         OVERLAPPED oConnect;
671         OVERLAPPED oRead;
672         OVERLAPPED oWrite;
673         OVERLAPPED *oResult;
674         DWORD err;
675         ULONG_PTR compkey;
676
677         memset(&oConnect, 0, sizeof(oConnect));
678         memset(&oRead, 0, sizeof(oRead));
679         memset(&oWrite, 0, sizeof(oWrite));
680
681         /* Wait for client to connect */
682         trace("Server calling overlapped ConnectNamedPipe...\n");
683         success = ConnectNamedPipe(hnp, &oConnect);
684         err = GetLastError();
685         ok(!success && (err == ERROR_IO_PENDING || err == ERROR_PIPE_CONNECTED),
686            "overlapped ConnectNamedPipe got %u err %u\n", success, err );
687         if (!success && err == ERROR_IO_PENDING) {
688             trace("ConnectNamedPipe GetQueuedCompletionStatus\n");
689             success = GetQueuedCompletionStatus(hcompletion, &dummy, &compkey, &oResult, 0);
690             if (!success)
691             {
692                 ok( GetLastError() == WAIT_TIMEOUT,
693                     "ConnectNamedPipe GetQueuedCompletionStatus wrong error %u\n", GetLastError());
694                 success = GetQueuedCompletionStatus(hcompletion, &dummy, &compkey, &oResult, 10000);
695             }
696             ok(success, "ConnectNamedPipe GetQueuedCompletionStatus failed, errno=%i\n", GetLastError());
697             if (success)
698             {
699                 ok(compkey == 12345, "got completion key %i instead of 12345\n", (int)compkey);
700                 ok(oResult == &oConnect, "got overlapped pointer %p instead of %p\n", oResult, &oConnect);
701             }
702         }
703         trace("overlapped ConnectNamedPipe operation complete.\n");
704
705         /* Echo bytes once */
706         memset(buf, 0, sizeof(buf));
707
708         trace("Server reading...\n");
709         success = ReadFile(hnp, buf, sizeof(buf), &readden, &oRead);
710         trace("Server ReadFile returned...\n");
711         err = GetLastError();
712         ok(success || err == ERROR_IO_PENDING, "overlapped ReadFile, err=%i\n", err);
713         success = GetQueuedCompletionStatus(hcompletion, &readden, &compkey,
714             &oResult, 10000);
715         ok(success, "ReadFile GetQueuedCompletionStatus failed, errno=%i\n", GetLastError());
716         if (success)
717         {
718             ok(compkey == 12345, "got completion key %i instead of 12345\n", (int)compkey);
719             ok(oResult == &oRead, "got overlapped pointer %p instead of %p\n", oResult, &oRead);
720         }
721         trace("Server done reading.\n");
722
723         trace("Server writing...\n");
724         success = WriteFile(hnp, buf, readden, &written, &oWrite);
725         trace("Server WriteFile returned...\n");
726         err = GetLastError();
727         ok(success || err == ERROR_IO_PENDING, "overlapped WriteFile failed, err=%u\n", err);
728         success = GetQueuedCompletionStatus(hcompletion, &written, &compkey,
729             &oResult, 10000);
730         ok(success, "WriteFile GetQueuedCompletionStatus failed, errno=%i\n", GetLastError());
731         if (success)
732         {
733             ok(compkey == 12345, "got completion key %i instead of 12345\n", (int)compkey);
734             ok(oResult == &oWrite, "got overlapped pointer %p instead of %p\n", oResult, &oWrite);
735             ok(written == readden, "write file len\n");
736         }
737         trace("Server done writing.\n");
738
739         /* finish this connection, wait for next one */
740         ok(FlushFileBuffers(hnp), "FlushFileBuffers\n");
741         success = DisconnectNamedPipe(hnp);
742         ok(success, "DisconnectNamedPipe failed, err %u\n", GetLastError());
743     }
744
745     ret = CloseHandle(hnp);
746     ok(ret, "CloseHandle named pipe failed, err=%i\n", GetLastError());
747     ret = CloseHandle(hcompletion);
748     ok(ret, "CloseHandle completion failed, err=%i\n", GetLastError());
749
750     return 0;
751 }
752
753 static void exercizeServer(const char *pipename, HANDLE serverThread)
754 {
755     int i;
756
757     trace("exercizeServer starting\n");
758     for (i = 0; i < NB_SERVER_LOOPS; i++) {
759         HANDLE hFile=INVALID_HANDLE_VALUE;
760         static const char obuf[] = "Bit Bucket";
761         char ibuf[32];
762         DWORD written;
763         DWORD readden;
764         int loop;
765
766         for (loop = 0; loop < 3; loop++) {
767             DWORD err;
768             trace("Client connecting...\n");
769             /* Connect to the server */
770             hFile = CreateFileA(pipename, GENERIC_READ | GENERIC_WRITE, 0,
771                 NULL, OPEN_EXISTING, 0, 0);
772             if (hFile != INVALID_HANDLE_VALUE)
773                 break;
774             err = GetLastError();
775             if (loop == 0)
776                 ok(err == ERROR_PIPE_BUSY || err == ERROR_FILE_NOT_FOUND, "connecting to pipe\n");
777             else
778                 ok(err == ERROR_PIPE_BUSY, "connecting to pipe\n");
779             trace("connect failed, retrying\n");
780             Sleep(200);
781         }
782         ok(hFile != INVALID_HANDLE_VALUE, "client opening named pipe\n");
783
784         /* Make sure it can echo */
785         memset(ibuf, 0, sizeof(ibuf));
786         trace("Client writing...\n");
787         ok(WriteFile(hFile, obuf, sizeof(obuf), &written, NULL), "WriteFile to client end of pipe\n");
788         ok(written == sizeof(obuf), "write file len\n");
789         trace("Client reading...\n");
790         ok(ReadFile(hFile, ibuf, sizeof(obuf), &readden, NULL), "ReadFile from client end of pipe\n");
791         ok(readden == sizeof(obuf), "read file len\n");
792         ok(memcmp(obuf, ibuf, written) == 0, "content check\n");
793
794         trace("Client closing...\n");
795         ok(CloseHandle(hFile), "CloseHandle\n");
796     }
797
798     ok(WaitForSingleObject(serverThread,INFINITE) == WAIT_OBJECT_0, "WaitForSingleObject\n");
799     CloseHandle(hnp);
800     trace("exercizeServer returning\n");
801 }
802
803 static void test_NamedPipe_2(void)
804 {
805     HANDLE serverThread;
806     DWORD serverThreadId;
807     HANDLE alarmThread;
808     DWORD alarmThreadId;
809
810     trace("test_NamedPipe_2 starting\n");
811     /* Set up a twenty second timeout */
812     alarm_event = CreateEvent( NULL, TRUE, FALSE, NULL );
813     SetLastError(0xdeadbeef);
814     alarmThread = CreateThread(NULL, 0, alarmThreadMain, (void *) 20000, 0, &alarmThreadId);
815     ok(alarmThread != NULL, "CreateThread failed: %d\n", GetLastError());
816
817     /* The servers we're about to exercise do try to clean up carefully,
818      * but to reduce the chance of a test failure due to a pipe handle
819      * leak in the test code, we'll use a different pipe name for each server.
820      */
821
822     /* Try server #1 */
823     SetLastError(0xdeadbeef);
824     serverThread = CreateThread(NULL, 0, serverThreadMain1, (void *)8, 0, &serverThreadId);
825     ok(serverThread != NULL, "CreateThread failed: %d\n", GetLastError());
826     exercizeServer(PIPENAME "serverThreadMain1", serverThread);
827
828     /* Try server #2 */
829     SetLastError(0xdeadbeef);
830     serverThread = CreateThread(NULL, 0, serverThreadMain2, 0, 0, &serverThreadId);
831     ok(serverThread != NULL, "CreateThread failed: %d\n", GetLastError());
832     exercizeServer(PIPENAME "serverThreadMain2", serverThread);
833
834     /* Try server #3 */
835     SetLastError(0xdeadbeef);
836     serverThread = CreateThread(NULL, 0, serverThreadMain3, 0, 0, &serverThreadId);
837     ok(serverThread != NULL, "CreateThread failed: %d\n", GetLastError());
838     exercizeServer(PIPENAME "serverThreadMain3", serverThread);
839
840     /* Try server #4 */
841     SetLastError(0xdeadbeef);
842     serverThread = CreateThread(NULL, 0, serverThreadMain4, 0, 0, &serverThreadId);
843     ok(serverThread != NULL, "CreateThread failed: %d\n", GetLastError());
844     exercizeServer(PIPENAME "serverThreadMain4", serverThread);
845
846     ok(SetEvent( alarm_event ), "SetEvent\n");
847     CloseHandle( alarm_event );
848     trace("test_NamedPipe_2 returning\n");
849 }
850
851 static int test_DisconnectNamedPipe(void)
852 {
853     HANDLE hnp;
854     HANDLE hFile;
855     static const char obuf[] = "Bit Bucket";
856     char ibuf[32];
857     DWORD written;
858     DWORD readden;
859     DWORD ret;
860
861     SetLastError(0xdeadbeef);
862     hnp = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
863         /* nMaxInstances */ 1,
864         /* nOutBufSize */ 1024,
865         /* nInBufSize */ 1024,
866         /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
867         /* lpSecurityAttrib */ NULL);
868     if ((hnp == INVALID_HANDLE_VALUE /* Win98 */ || !hnp /* Win95 */)
869         && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) {
870
871         win_skip("Named pipes are not implemented\n");
872         return 1;
873     }
874
875     ok(WriteFile(hnp, obuf, sizeof(obuf), &written, NULL) == 0
876         && GetLastError() == ERROR_PIPE_LISTENING, "WriteFile to not-yet-connected pipe\n");
877     ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL) == 0
878         && GetLastError() == ERROR_PIPE_LISTENING, "ReadFile from not-yet-connected pipe\n");
879
880     hFile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
881     ok(hFile != INVALID_HANDLE_VALUE, "CreateFile failed\n");
882
883     /* don't try to do i/o if one side couldn't be opened, as it hangs */
884     if (hFile != INVALID_HANDLE_VALUE) {
885
886         /* see what happens if server calls DisconnectNamedPipe
887          * when there are bytes in the pipe
888          */
889
890         ok(WriteFile(hFile, obuf, sizeof(obuf), &written, NULL), "WriteFile\n");
891         ok(written == sizeof(obuf), "write file len\n");
892         ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe while messages waiting\n");
893         ok(WriteFile(hFile, obuf, sizeof(obuf), &written, NULL) == 0
894             && GetLastError() == ERROR_PIPE_NOT_CONNECTED, "WriteFile to disconnected pipe\n");
895         ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL) == 0
896             && GetLastError() == ERROR_PIPE_NOT_CONNECTED,
897             "ReadFile from disconnected pipe with bytes waiting\n");
898         ok(!DisconnectNamedPipe(hnp) && GetLastError() == ERROR_PIPE_NOT_CONNECTED,
899            "DisconnectNamedPipe worked twice\n");
900         ret = WaitForSingleObject(hFile, 0);
901         ok(ret == WAIT_TIMEOUT, "WaitForSingleObject returned %X\n", ret);
902         ok(CloseHandle(hFile), "CloseHandle\n");
903     }
904
905     ok(CloseHandle(hnp), "CloseHandle\n");
906
907     return 0;
908 }
909 static void test_CreatePipe(void)
910 {
911     SECURITY_ATTRIBUTES pipe_attr;
912     HANDLE piperead, pipewrite;
913     DWORD written;
914     DWORD read;
915     DWORD i, size;
916     BYTE *buffer;
917     char readbuf[32];
918
919     pipe_attr.nLength = sizeof(SECURITY_ATTRIBUTES); 
920     pipe_attr.bInheritHandle = TRUE; 
921     pipe_attr.lpSecurityDescriptor = NULL; 
922     ok(CreatePipe(&piperead, &pipewrite, &pipe_attr, 0) != 0, "CreatePipe failed\n");
923     ok(WriteFile(pipewrite,PIPENAME,sizeof(PIPENAME), &written, NULL), "Write to anonymous pipe failed\n");
924     ok(written == sizeof(PIPENAME), "Write to anonymous pipe wrote %d bytes\n", written);
925     ok(ReadFile(piperead,readbuf,sizeof(readbuf),&read, NULL), "Read from non empty pipe failed\n");
926     ok(read == sizeof(PIPENAME), "Read from  anonymous pipe got %d bytes\n", read);
927     ok(CloseHandle(pipewrite), "CloseHandle for the write pipe failed\n");
928     ok(CloseHandle(piperead), "CloseHandle for the read pipe failed\n");
929
930     /* Now write another chunk*/
931     ok(CreatePipe(&piperead, &pipewrite, &pipe_attr, 0) != 0, "CreatePipe failed\n");
932     ok(WriteFile(pipewrite,PIPENAME,sizeof(PIPENAME), &written, NULL), "Write to anonymous pipe failed\n");
933     ok(written == sizeof(PIPENAME), "Write to anonymous pipe wrote %d bytes\n", written);
934     /* and close the write end, read should still succeed*/
935     ok(CloseHandle(pipewrite), "CloseHandle for the Write Pipe failed\n");
936     ok(ReadFile(piperead,readbuf,sizeof(readbuf),&read, NULL), "Read from broken pipe withe with pending data failed\n");
937     ok(read == sizeof(PIPENAME), "Read from  anonymous pipe got %d bytes\n", read);
938     /* But now we need to get informed that the pipe is closed */
939     ok(ReadFile(piperead,readbuf,sizeof(readbuf),&read, NULL) == 0, "Broken pipe not detected\n");
940     ok(CloseHandle(piperead), "CloseHandle for the read pipe failed\n");
941
942     /* Try bigger chunks */
943     size = 32768;
944     buffer = HeapAlloc( GetProcessHeap(), 0, size );
945     for (i = 0; i < size; i++) buffer[i] = i;
946     ok(CreatePipe(&piperead, &pipewrite, &pipe_attr, (size + 24)) != 0, "CreatePipe failed\n");
947     ok(WriteFile(pipewrite, buffer, size, &written, NULL), "Write to anonymous pipe failed\n");
948     ok(written == size, "Write to anonymous pipe wrote %d bytes\n", written);
949     /* and close the write end, read should still succeed*/
950     ok(CloseHandle(pipewrite), "CloseHandle for the Write Pipe failed\n");
951     memset( buffer, 0, size );
952     ok(ReadFile(piperead, buffer, size, &read, NULL), "Read from broken pipe withe with pending data failed\n");
953     ok(read == size, "Read from  anonymous pipe got %d bytes\n", read);
954     for (i = 0; i < size; i++) ok( buffer[i] == (BYTE)i, "invalid data %x at %x\n", buffer[i], i );
955     /* But now we need to get informed that the pipe is closed */
956     ok(ReadFile(piperead,readbuf,sizeof(readbuf),&read, NULL) == 0, "Broken pipe not detected\n");
957     ok(CloseHandle(piperead), "CloseHandle for the read pipe failed\n");
958     HeapFree(GetProcessHeap(), 0, buffer);
959 }
960
961 struct named_pipe_client_params
962 {
963     DWORD security_flags;
964     HANDLE token;
965     BOOL revert;
966 };
967
968 #define PIPE_NAME "\\\\.\\pipe\\named_pipe_test"
969
970 static DWORD CALLBACK named_pipe_client_func(LPVOID p)
971 {
972     struct named_pipe_client_params *params = p;
973     HANDLE pipe;
974     BOOL ret;
975     const char message[] = "Test";
976     DWORD bytes_read, bytes_written;
977     char dummy;
978     TOKEN_PRIVILEGES *Privileges = NULL;
979
980     if (params->token)
981     {
982         if (params->revert)
983         {
984             /* modify the token so we can tell if the pipe impersonation
985              * token reverts to the process token */
986             ret = AdjustTokenPrivileges(params->token, TRUE, NULL, 0, NULL, NULL);
987             ok(ret, "AdjustTokenPrivileges failed with error %d\n", GetLastError());
988         }
989         ret = SetThreadToken(NULL, params->token);
990         ok(ret, "SetThreadToken failed with error %d\n", GetLastError());
991     }
992     else
993     {
994         DWORD Size = 0;
995         HANDLE process_token;
996
997         ret = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY|TOKEN_ADJUST_PRIVILEGES, &process_token);
998         ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
999
1000         ret = GetTokenInformation(process_token, TokenPrivileges, NULL, 0, &Size);
1001         ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "GetTokenInformation(TokenPrivileges) failed with %d\n", GetLastError());
1002         Privileges = HeapAlloc(GetProcessHeap(), 0, Size);
1003         ret = GetTokenInformation(process_token, TokenPrivileges, Privileges, Size, &Size);
1004         ok(ret, "GetTokenInformation(TokenPrivileges) failed with %d\n", GetLastError());
1005
1006         ret = AdjustTokenPrivileges(process_token, TRUE, NULL, 0, NULL, NULL);
1007         ok(ret, "AdjustTokenPrivileges failed with error %d\n", GetLastError());
1008
1009         CloseHandle(process_token);
1010     }
1011
1012     pipe = CreateFile(PIPE_NAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, params->security_flags, NULL);
1013     ok(pipe != INVALID_HANDLE_VALUE, "CreateFile for pipe failed with error %d\n", GetLastError());
1014
1015     ret = WriteFile(pipe, message, sizeof(message), &bytes_written, NULL);
1016     ok(ret, "WriteFile failed with error %d\n", GetLastError());
1017
1018     ret = ReadFile(pipe, &dummy, sizeof(dummy), &bytes_read, NULL);
1019     ok(ret, "ReadFile failed with error %d\n", GetLastError());
1020
1021     if (params->token)
1022     {
1023         if (params->revert)
1024         {
1025             ret = RevertToSelf();
1026             ok(ret, "RevertToSelf failed with error %d\n", GetLastError());
1027         }
1028         else
1029         {
1030             ret = AdjustTokenPrivileges(params->token, TRUE, NULL, 0, NULL, NULL);
1031             ok(ret, "AdjustTokenPrivileges failed with error %d\n", GetLastError());
1032         }
1033     }
1034     else
1035     {
1036         HANDLE process_token;
1037
1038         ret = OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &process_token);
1039         ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
1040
1041         ret = AdjustTokenPrivileges(process_token, FALSE, Privileges, 0, NULL, NULL);
1042         ok(ret, "AdjustTokenPrivileges failed with error %d\n", GetLastError());
1043
1044         HeapFree(GetProcessHeap(), 0, Privileges);
1045
1046         CloseHandle(process_token);
1047     }
1048
1049     ret = WriteFile(pipe, message, sizeof(message), &bytes_written, NULL);
1050     ok(ret, "WriteFile failed with error %d\n", GetLastError());
1051
1052     ret = ReadFile(pipe, &dummy, sizeof(dummy), &bytes_read, NULL);
1053     ok(ret, "ReadFile failed with error %d\n", GetLastError());
1054
1055     CloseHandle(pipe);
1056
1057     return 0;
1058 }
1059
1060 static HANDLE make_impersonation_token(DWORD Access, SECURITY_IMPERSONATION_LEVEL ImpersonationLevel)
1061 {
1062     HANDLE ProcessToken;
1063     HANDLE Token = NULL;
1064     BOOL ret;
1065
1066     ret = OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE, &ProcessToken);
1067     ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
1068
1069     ret = pDuplicateTokenEx(ProcessToken, Access, NULL, ImpersonationLevel, TokenImpersonation, &Token);
1070     ok(ret, "DuplicateToken failed with error %d\n", GetLastError());
1071
1072     CloseHandle(ProcessToken);
1073
1074     return Token;
1075 }
1076
1077 static void test_ImpersonateNamedPipeClient(HANDLE hClientToken, DWORD security_flags, BOOL revert, void (*test_func)(int, HANDLE))
1078 {
1079     HANDLE hPipeServer;
1080     BOOL ret;
1081     DWORD dwTid;
1082     HANDLE hThread;
1083     char buffer[256];
1084     DWORD dwBytesRead;
1085     DWORD error;
1086     struct named_pipe_client_params params;
1087     char dummy = 0;
1088     DWORD dwBytesWritten;
1089     HANDLE hToken = NULL;
1090     SECURITY_IMPERSONATION_LEVEL ImpersonationLevel;
1091     DWORD size;
1092
1093     hPipeServer = CreateNamedPipe(PIPE_NAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT, 1, 100, 100, NMPWAIT_USE_DEFAULT_WAIT, NULL);
1094     ok(hPipeServer != INVALID_HANDLE_VALUE, "CreateNamedPipe failed with error %d\n", GetLastError());
1095
1096     params.security_flags = security_flags;
1097     params.token = hClientToken;
1098     params.revert = revert;
1099     hThread = CreateThread(NULL, 0, named_pipe_client_func, &params, 0, &dwTid);
1100     ok(hThread != NULL, "CreateThread failed with error %d\n", GetLastError());
1101
1102     SetLastError(0xdeadbeef);
1103     ret = ImpersonateNamedPipeClient(hPipeServer);
1104     error = GetLastError();
1105     ok(ret /* win2k3 */ || (error == ERROR_CANNOT_IMPERSONATE),
1106        "ImpersonateNamedPipeClient should have failed with ERROR_CANNOT_IMPERSONATE instead of %d\n", GetLastError());
1107
1108     ret = ConnectNamedPipe(hPipeServer, NULL);
1109     ok(ret || (GetLastError() == ERROR_PIPE_CONNECTED), "ConnectNamedPipe failed with error %d\n", GetLastError());
1110
1111     ret = ReadFile(hPipeServer, buffer, sizeof(buffer), &dwBytesRead, NULL);
1112     ok(ret, "ReadFile failed with error %d\n", GetLastError());
1113
1114     ret = ImpersonateNamedPipeClient(hPipeServer);
1115     ok(ret, "ImpersonateNamedPipeClient failed with error %d\n", GetLastError());
1116
1117     ret = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, FALSE, &hToken);
1118     ok(ret, "OpenThreadToken failed with error %d\n", GetLastError());
1119
1120     (*test_func)(0, hToken);
1121
1122     ImpersonationLevel = 0xdeadbeef; /* to avoid false positives */
1123     ret = GetTokenInformation(hToken, TokenImpersonationLevel, &ImpersonationLevel, sizeof(ImpersonationLevel), &size);
1124     ok(ret, "GetTokenInformation(TokenImpersonationLevel) failed with error %d\n", GetLastError());
1125     ok(ImpersonationLevel == SecurityImpersonation, "ImpersonationLevel should have been SecurityImpersonation(%d) instead of %d\n", SecurityImpersonation, ImpersonationLevel);
1126
1127     CloseHandle(hToken);
1128
1129     RevertToSelf();
1130
1131     ret = WriteFile(hPipeServer, &dummy, sizeof(dummy), &dwBytesWritten, NULL);
1132     ok(ret, "WriteFile failed with error %d\n", GetLastError());
1133
1134     ret = ReadFile(hPipeServer, buffer, sizeof(buffer), &dwBytesRead, NULL);
1135     ok(ret, "ReadFile failed with error %d\n", GetLastError());
1136
1137     ret = ImpersonateNamedPipeClient(hPipeServer);
1138     ok(ret, "ImpersonateNamedPipeClient failed with error %d\n", GetLastError());
1139
1140     ret = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, FALSE, &hToken);
1141     ok(ret, "OpenThreadToken failed with error %d\n", GetLastError());
1142
1143     (*test_func)(1, hToken);
1144
1145     CloseHandle(hToken);
1146
1147     RevertToSelf();
1148
1149     ret = WriteFile(hPipeServer, &dummy, sizeof(dummy), &dwBytesWritten, NULL);
1150     ok(ret, "WriteFile failed with error %d\n", GetLastError());
1151
1152     WaitForSingleObject(hThread, INFINITE);
1153
1154     ret = ImpersonateNamedPipeClient(hPipeServer);
1155     ok(ret, "ImpersonateNamedPipeClient failed with error %d\n", GetLastError());
1156
1157     RevertToSelf();
1158
1159     CloseHandle(hThread);
1160     CloseHandle(hPipeServer);
1161 }
1162
1163 static BOOL are_all_privileges_disabled(HANDLE hToken)
1164 {
1165     BOOL ret;
1166     TOKEN_PRIVILEGES *Privileges = NULL;
1167     DWORD Size = 0;
1168     BOOL all_privs_disabled = TRUE;
1169     DWORD i;
1170
1171     ret = GetTokenInformation(hToken, TokenPrivileges, NULL, 0, &Size);
1172     if (!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1173     {
1174         Privileges = HeapAlloc(GetProcessHeap(), 0, Size);
1175         ret = GetTokenInformation(hToken, TokenPrivileges, Privileges, Size, &Size);
1176         if (!ret)
1177         {
1178             HeapFree(GetProcessHeap(), 0, Privileges);
1179             return FALSE;
1180         }
1181     }
1182     else
1183         return FALSE;
1184
1185     for (i = 0; i < Privileges->PrivilegeCount; i++)
1186     {
1187         if (Privileges->Privileges[i].Attributes & SE_PRIVILEGE_ENABLED)
1188         {
1189             all_privs_disabled = FALSE;
1190             break;
1191         }
1192     }
1193
1194     HeapFree(GetProcessHeap(), 0, Privileges);
1195
1196     return all_privs_disabled;
1197 }
1198
1199 static DWORD get_privilege_count(HANDLE hToken)
1200 {
1201     TOKEN_STATISTICS Statistics;
1202     DWORD Size = sizeof(Statistics);
1203     BOOL ret;
1204
1205     ret = GetTokenInformation(hToken, TokenStatistics, &Statistics, Size, &Size);
1206     ok(ret, "GetTokenInformation(TokenStatistics)\n");
1207     if (!ret) return -1;
1208
1209     return Statistics.PrivilegeCount;
1210 }
1211
1212 static void test_no_sqos_no_token(int call_index, HANDLE hToken)
1213 {
1214     DWORD priv_count;
1215
1216     switch (call_index)
1217     {
1218     case 0:
1219         priv_count = get_privilege_count(hToken);
1220         todo_wine
1221         ok(priv_count == 0, "privilege count should have been 0 instead of %d\n", priv_count);
1222         break;
1223     case 1:
1224         priv_count = get_privilege_count(hToken);
1225         ok(priv_count > 0, "privilege count should now be > 0 instead of 0\n");
1226         ok(!are_all_privileges_disabled(hToken), "impersonated token should not have been modified\n");
1227         break;
1228     default:
1229         ok(0, "shouldn't happen\n");
1230     }
1231 }
1232
1233 static void test_no_sqos(int call_index, HANDLE hToken)
1234 {
1235     switch (call_index)
1236     {
1237     case 0:
1238         ok(!are_all_privileges_disabled(hToken), "token should be a copy of the process one\n");
1239         break;
1240     case 1:
1241         todo_wine
1242         ok(are_all_privileges_disabled(hToken), "impersonated token should have been modified\n");
1243         break;
1244     default:
1245         ok(0, "shouldn't happen\n");
1246     }
1247 }
1248
1249 static void test_static_context(int call_index, HANDLE hToken)
1250 {
1251     switch (call_index)
1252     {
1253     case 0:
1254         ok(!are_all_privileges_disabled(hToken), "token should be a copy of the process one\n");
1255         break;
1256     case 1:
1257         ok(!are_all_privileges_disabled(hToken), "impersonated token should not have been modified\n");
1258         break;
1259     default:
1260         ok(0, "shouldn't happen\n");
1261     }
1262 }
1263
1264 static void test_dynamic_context(int call_index, HANDLE hToken)
1265 {
1266     switch (call_index)
1267     {
1268     case 0:
1269         ok(!are_all_privileges_disabled(hToken), "token should be a copy of the process one\n");
1270         break;
1271     case 1:
1272         todo_wine
1273         ok(are_all_privileges_disabled(hToken), "impersonated token should have been modified\n");
1274         break;
1275     default:
1276         ok(0, "shouldn't happen\n");
1277     }
1278 }
1279
1280 static void test_dynamic_context_no_token(int call_index, HANDLE hToken)
1281 {
1282     switch (call_index)
1283     {
1284     case 0:
1285         ok(are_all_privileges_disabled(hToken), "token should be a copy of the process one\n");
1286         break;
1287     case 1:
1288         ok(!are_all_privileges_disabled(hToken), "process token modification should have been detected and impersonation token updated\n");
1289         break;
1290     default:
1291         ok(0, "shouldn't happen\n");
1292     }
1293 }
1294
1295 static void test_no_sqos_revert(int call_index, HANDLE hToken)
1296 {
1297     DWORD priv_count;
1298     switch (call_index)
1299     {
1300     case 0:
1301         priv_count = get_privilege_count(hToken);
1302         todo_wine
1303         ok(priv_count == 0, "privilege count should have been 0 instead of %d\n", priv_count);
1304         break;
1305     case 1:
1306         priv_count = get_privilege_count(hToken);
1307         ok(priv_count > 0, "privilege count should now be > 0 instead of 0\n");
1308         ok(!are_all_privileges_disabled(hToken), "impersonated token should not have been modified\n");
1309         break;
1310     default:
1311         ok(0, "shouldn't happen\n");
1312     }
1313 }
1314
1315 static void test_static_context_revert(int call_index, HANDLE hToken)
1316 {
1317     switch (call_index)
1318     {
1319     case 0:
1320         todo_wine
1321         ok(are_all_privileges_disabled(hToken), "privileges should have been disabled\n");
1322         break;
1323     case 1:
1324         todo_wine
1325         ok(are_all_privileges_disabled(hToken), "impersonated token should not have been modified\n");
1326         break;
1327     default:
1328         ok(0, "shouldn't happen\n");
1329     }
1330 }
1331
1332 static void test_dynamic_context_revert(int call_index, HANDLE hToken)
1333 {
1334     switch (call_index)
1335     {
1336     case 0:
1337         todo_wine
1338         ok(are_all_privileges_disabled(hToken), "privileges should have been disabled\n");
1339         break;
1340     case 1:
1341         ok(!are_all_privileges_disabled(hToken), "impersonated token should now be process token\n");
1342         break;
1343     default:
1344         ok(0, "shouldn't happen\n");
1345     }
1346 }
1347
1348 static void test_impersonation(void)
1349 {
1350     HANDLE hClientToken;
1351     HANDLE hProcessToken;
1352     BOOL ret;
1353
1354     if( !pDuplicateTokenEx ) {
1355         skip("DuplicateTokenEx not found\n");
1356         return;
1357     }
1358
1359     ret = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hProcessToken);
1360     if (!ret)
1361     {
1362         skip("couldn't open process token, skipping impersonation tests\n");
1363         return;
1364     }
1365
1366     if (!get_privilege_count(hProcessToken) || are_all_privileges_disabled(hProcessToken))
1367     {
1368         skip("token didn't have any privileges or they were all disabled. token not suitable for impersonation tests\n");
1369         CloseHandle(hProcessToken);
1370         return;
1371     }
1372     CloseHandle(hProcessToken);
1373
1374     test_ImpersonateNamedPipeClient(NULL, 0, FALSE, test_no_sqos_no_token);
1375     hClientToken = make_impersonation_token(TOKEN_IMPERSONATE | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, SecurityImpersonation);
1376     test_ImpersonateNamedPipeClient(hClientToken, 0, FALSE, test_no_sqos);
1377     CloseHandle(hClientToken);
1378     hClientToken = make_impersonation_token(TOKEN_IMPERSONATE | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, SecurityImpersonation);
1379     test_ImpersonateNamedPipeClient(hClientToken,
1380         SECURITY_SQOS_PRESENT | SECURITY_IMPERSONATION, FALSE,
1381         test_static_context);
1382     CloseHandle(hClientToken);
1383     hClientToken = make_impersonation_token(TOKEN_IMPERSONATE | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, SecurityImpersonation);
1384     test_ImpersonateNamedPipeClient(hClientToken,
1385         SECURITY_SQOS_PRESENT | SECURITY_CONTEXT_TRACKING | SECURITY_IMPERSONATION,
1386         FALSE, test_dynamic_context);
1387     CloseHandle(hClientToken);
1388     test_ImpersonateNamedPipeClient(NULL,
1389         SECURITY_SQOS_PRESENT | SECURITY_CONTEXT_TRACKING | SECURITY_IMPERSONATION,
1390         FALSE, test_dynamic_context_no_token);
1391
1392     hClientToken = make_impersonation_token(TOKEN_IMPERSONATE | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, SecurityImpersonation);
1393     test_ImpersonateNamedPipeClient(hClientToken, 0, TRUE, test_no_sqos_revert);
1394     CloseHandle(hClientToken);
1395     hClientToken = make_impersonation_token(TOKEN_IMPERSONATE | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, SecurityImpersonation);
1396     test_ImpersonateNamedPipeClient(hClientToken,
1397         SECURITY_SQOS_PRESENT | SECURITY_IMPERSONATION, TRUE,
1398         test_static_context_revert);
1399     CloseHandle(hClientToken);
1400     hClientToken = make_impersonation_token(TOKEN_IMPERSONATE | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, SecurityImpersonation);
1401     test_ImpersonateNamedPipeClient(hClientToken,
1402         SECURITY_SQOS_PRESENT | SECURITY_CONTEXT_TRACKING | SECURITY_IMPERSONATION,
1403         TRUE, test_dynamic_context_revert);
1404     CloseHandle(hClientToken);
1405 }
1406
1407 struct overlapped_server_args
1408 {
1409     HANDLE pipe_created;
1410 };
1411
1412 static DWORD CALLBACK overlapped_server(LPVOID arg)
1413 {
1414     OVERLAPPED ol;
1415     HANDLE pipe;
1416     int ret, err;
1417     struct overlapped_server_args *a = arg;
1418     DWORD num;
1419     char buf[100];
1420
1421     pipe = CreateNamedPipeA("\\\\.\\pipe\\my pipe", FILE_FLAG_OVERLAPPED | PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE, 1, 0, 0, 100000, NULL);
1422     ok(pipe != NULL, "pipe NULL\n");
1423
1424     ol.hEvent = CreateEventA(0, 1, 0, 0);
1425     ok(ol.hEvent != NULL, "event NULL\n");
1426     ret = ConnectNamedPipe(pipe, &ol);
1427     err = GetLastError();
1428     ok(ret == 0, "ret %d\n", ret);
1429     ok(err == ERROR_IO_PENDING, "gle %d\n", err);
1430     SetEvent(a->pipe_created);
1431
1432     ret = WaitForSingleObjectEx(ol.hEvent, INFINITE, 1);
1433     ok(ret == WAIT_OBJECT_0, "ret %x\n", ret);
1434
1435     ret = GetOverlappedResult(pipe, &ol, &num, 1);
1436     ok(ret == 1, "ret %d\n", ret);
1437
1438     /* This should block */
1439     ret = ReadFile(pipe, buf, sizeof(buf), &num, NULL);
1440     ok(ret == 1, "ret %d\n", ret);
1441
1442     DisconnectNamedPipe(pipe);
1443     CloseHandle(ol.hEvent);
1444     CloseHandle(pipe);
1445     return 1;
1446 }
1447
1448 static void test_overlapped(void)
1449 {
1450     DWORD tid, num;
1451     HANDLE thread, pipe;
1452     BOOL ret;
1453     struct overlapped_server_args args;
1454
1455     args.pipe_created = CreateEventA(0, 1, 0, 0);
1456     thread = CreateThread(NULL, 0, overlapped_server, &args, 0, &tid);
1457
1458     WaitForSingleObject(args.pipe_created, INFINITE);
1459     pipe = CreateFileA("\\\\.\\pipe\\my pipe", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
1460     ok(pipe != INVALID_HANDLE_VALUE, "cf failed\n");
1461
1462     /* Sleep to try to get the ReadFile in the server to occur before the following WriteFile */
1463     Sleep(1);
1464
1465     ret = WriteFile(pipe, "x", 1, &num, NULL);
1466     ok(ret, "WriteFile failed with error %d\n", GetLastError());
1467
1468     WaitForSingleObject(thread, INFINITE);
1469     CloseHandle(pipe);
1470     CloseHandle(args.pipe_created);
1471     CloseHandle(thread);
1472 }
1473
1474 static void test_NamedPipeHandleState(void)
1475 {
1476     HANDLE server, client;
1477     BOOL ret;
1478     DWORD state, instances, maxCollectionCount, collectDataTimeout;
1479     char userName[MAX_PATH];
1480
1481     server = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX,
1482         /* dwOpenMode */ PIPE_TYPE_BYTE | PIPE_WAIT,
1483         /* nMaxInstances */ 1,
1484         /* nOutBufSize */ 1024,
1485         /* nInBufSize */ 1024,
1486         /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
1487         /* lpSecurityAttrib */ NULL);
1488     ok(server != INVALID_HANDLE_VALUE, "cf failed\n");
1489     ret = GetNamedPipeHandleState(server, NULL, NULL, NULL, NULL, NULL, 0);
1490     todo_wine
1491     ok(ret, "GetNamedPipeHandleState failed: %d\n", GetLastError());
1492     ret = GetNamedPipeHandleState(server, &state, &instances, NULL, NULL, NULL,
1493         0);
1494     todo_wine
1495     ok(ret, "GetNamedPipeHandleState failed: %d\n", GetLastError());
1496     if (ret)
1497     {
1498         ok(state == 0, "unexpected state %08x\n", state);
1499         ok(instances == 1, "expected 1 instances, got %d\n", instances);
1500     }
1501     /* Some parameters have no meaning, and therefore can't be retrieved,
1502      * on a local pipe.
1503      */
1504     SetLastError(0xdeadbeef);
1505     ret = GetNamedPipeHandleState(server, &state, &instances,
1506         &maxCollectionCount, &collectDataTimeout, userName,
1507         sizeof(userName) / sizeof(userName[0]));
1508     todo_wine
1509     ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
1510        "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1511     /* A byte-mode pipe server can't be changed to message mode. */
1512     state = PIPE_READMODE_MESSAGE;
1513     SetLastError(0xdeadbeef);
1514     ret = SetNamedPipeHandleState(server, &state, NULL, NULL);
1515     todo_wine
1516     ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
1517        "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1518
1519     client = CreateFileA(PIPENAME, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1520         OPEN_EXISTING, 0, NULL);
1521     ok(client != INVALID_HANDLE_VALUE, "cf failed\n");
1522
1523     state = PIPE_READMODE_BYTE;
1524     ret = SetNamedPipeHandleState(client, &state, NULL, NULL);
1525     todo_wine
1526     ok(ret, "SetNamedPipeHandleState failed: %d\n", GetLastError());
1527     /* A byte-mode pipe client can't be changed to message mode, either. */
1528     state = PIPE_READMODE_MESSAGE;
1529     SetLastError(0xdeadbeef);
1530     ret = SetNamedPipeHandleState(server, &state, NULL, NULL);
1531     todo_wine
1532     ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
1533        "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1534
1535     CloseHandle(client);
1536     CloseHandle(server);
1537
1538     server = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX,
1539         /* dwOpenMode */ PIPE_TYPE_MESSAGE | PIPE_WAIT,
1540         /* nMaxInstances */ 1,
1541         /* nOutBufSize */ 1024,
1542         /* nInBufSize */ 1024,
1543         /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
1544         /* lpSecurityAttrib */ NULL);
1545     ok(server != INVALID_HANDLE_VALUE, "cf failed\n");
1546     ret = GetNamedPipeHandleState(server, NULL, NULL, NULL, NULL, NULL, 0);
1547     todo_wine
1548     ok(ret, "GetNamedPipeHandleState failed: %d\n", GetLastError());
1549     ret = GetNamedPipeHandleState(server, &state, &instances, NULL, NULL, NULL,
1550         0);
1551     todo_wine
1552     ok(ret, "GetNamedPipeHandleState failed: %d\n", GetLastError());
1553     if (ret)
1554     {
1555         ok(state == 0, "unexpected state %08x\n", state);
1556         ok(instances == 1, "expected 1 instances, got %d\n", instances);
1557     }
1558     /* In contrast to byte-mode pipes, a message-mode pipe server can be
1559      * changed to byte mode.
1560      */
1561     state = PIPE_READMODE_BYTE;
1562     ret = SetNamedPipeHandleState(server, &state, NULL, NULL);
1563     todo_wine
1564     ok(ret, "SetNamedPipeHandleState failed: %d\n", GetLastError());
1565
1566     client = CreateFileA(PIPENAME, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1567         OPEN_EXISTING, 0, NULL);
1568     ok(client != INVALID_HANDLE_VALUE, "cf failed\n");
1569
1570     state = PIPE_READMODE_MESSAGE;
1571     ret = SetNamedPipeHandleState(client, &state, NULL, NULL);
1572     todo_wine
1573     ok(ret, "SetNamedPipeHandleState failed: %d\n", GetLastError());
1574     /* A message-mode pipe client can also be changed to byte mode.
1575      */
1576     state = PIPE_READMODE_BYTE;
1577     ret = SetNamedPipeHandleState(client, &state, NULL, NULL);
1578     todo_wine
1579     ok(ret, "SetNamedPipeHandleState failed: %d\n", GetLastError());
1580
1581     CloseHandle(client);
1582     CloseHandle(server);
1583 }
1584
1585 START_TEST(pipe)
1586 {
1587     HMODULE hmod;
1588
1589     hmod = GetModuleHandle("advapi32.dll");
1590     pDuplicateTokenEx = (void *) GetProcAddress(hmod, "DuplicateTokenEx");
1591
1592     if (test_DisconnectNamedPipe())
1593         return;
1594     test_CreateNamedPipe_instances_must_match();
1595     test_NamedPipe_2();
1596     test_CreateNamedPipe(PIPE_TYPE_BYTE);
1597     test_CreateNamedPipe(PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE);
1598     test_CreatePipe();
1599     test_impersonation();
1600     test_overlapped();
1601     test_NamedPipeHandleState();
1602 }