Add trailing '\n's to trace() call.
[wine] / dlls / kernel / 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <assert.h>
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <time.h>
25
26 #include <windef.h>
27 #include <winbase.h>
28 #include <winsock.h>
29 #include <wtypes.h>
30 #include <winerror.h>
31
32 #include "wine/test.h"
33
34 #define PIPENAME "\\\\.\\PiPe\\tests_pipe.c"
35
36 #define NB_SERVER_LOOPS 8
37
38 static HANDLE alarm_event;
39
40 static void test_CreateNamedPipe(int pipemode)
41 {
42     HANDLE hnp;
43     HANDLE hFile;
44     static const char obuf[] = "Bit Bucket";
45     static const char obuf2[] = "More bits";
46     char ibuf[32], *pbuf;
47     DWORD written;
48     DWORD readden;
49     DWORD avail;
50     DWORD lpmode;
51
52     if (pipemode == PIPE_TYPE_BYTE)
53         trace("test_CreateNamedPipe starting in byte mode\n");
54     else
55         trace("test_CreateNamedPipe starting in message mode\n");
56     /* Bad parameter checks */
57     hnp = CreateNamedPipe("not a named pipe", PIPE_ACCESS_DUPLEX, pipemode | PIPE_WAIT,
58         /* nMaxInstances */ 1,
59         /* nOutBufSize */ 1024,
60         /* nInBufSize */ 1024,
61         /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
62         /* lpSecurityAttrib */ NULL);
63
64     if (hnp == INVALID_HANDLE_VALUE && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) {
65         /* Is this the right way to notify user of skipped tests? */
66         ok(hnp == INVALID_HANDLE_VALUE && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED,
67             "CreateNamedPipe not supported on this platform, skipping tests.\n");
68         return;
69     }
70     ok(hnp == INVALID_HANDLE_VALUE && GetLastError() == ERROR_INVALID_NAME,
71         "CreateNamedPipe should fail if name doesn't start with \\\\.\\pipe\n");
72
73     hnp = CreateNamedPipe(NULL,
74         PIPE_ACCESS_DUPLEX, pipemode | PIPE_WAIT,
75         1, 1024, 1024, NMPWAIT_USE_DEFAULT_WAIT, NULL);
76     ok(hnp == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
77         "CreateNamedPipe should fail if name is NULL\n");
78
79     hFile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
80     ok(hFile == INVALID_HANDLE_VALUE
81         && GetLastError() == ERROR_FILE_NOT_FOUND,
82         "connecting to nonexistent named pipe should fail with ERROR_FILE_NOT_FOUND\n");
83
84     /* Functional checks */
85
86     hnp = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, pipemode | PIPE_WAIT,
87         /* nMaxInstances */ 1,
88         /* nOutBufSize */ 1024,
89         /* nInBufSize */ 1024,
90         /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
91         /* lpSecurityAttrib */ NULL);
92     ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
93
94     hFile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
95     ok(hFile != INVALID_HANDLE_VALUE, "CreateFile failed\n");
96
97     /* don't try to do i/o if one side couldn't be opened, as it hangs */
98     if (hFile != INVALID_HANDLE_VALUE) {
99         HANDLE hFile2;
100
101         /* Make sure we can read and write a few bytes in both directions */
102         memset(ibuf, 0, sizeof(ibuf));
103         ok(WriteFile(hnp, obuf, sizeof(obuf), &written, NULL), "WriteFile\n");
104         ok(written == sizeof(obuf), "write file len 1\n");
105         ok(PeekNamedPipe(hFile, NULL, 0, NULL, &readden, NULL), "Peek\n");
106         ok(readden == sizeof(obuf), "peek 1 got %ld bytes\n", readden);
107         ok(ReadFile(hFile, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
108         ok(readden == sizeof(obuf), "read 1 got %ld bytes\n", readden);
109         ok(memcmp(obuf, ibuf, written) == 0, "content 1 check\n");
110
111         memset(ibuf, 0, sizeof(ibuf));
112         ok(WriteFile(hFile, obuf2, sizeof(obuf2), &written, NULL), "WriteFile\n");
113         ok(written == sizeof(obuf2), "write file len 2\n");
114         ok(PeekNamedPipe(hnp, NULL, 0, NULL, &readden, NULL), "Peek\n");
115         ok(readden == sizeof(obuf2), "peek 2 got %ld bytes\n", readden);
116         ok(PeekNamedPipe(hnp, (LPVOID)1, 0, NULL, &readden, NULL), "Peek\n");
117         ok(readden == sizeof(obuf2), "peek 2 got %ld bytes\n", readden);
118         ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
119         ok(readden == sizeof(obuf2), "read 2 got %ld bytes\n", readden);
120         ok(memcmp(obuf2, ibuf, written) == 0, "content 2 check\n");
121
122         /* Test reading of multiple writes */
123         memset(ibuf, 0, sizeof(ibuf));
124         ok(WriteFile(hnp, obuf, sizeof(obuf), &written, NULL), "WriteFile3a\n");
125         ok(written == sizeof(obuf), "write file len 3a\n");
126         ok(WriteFile(hnp, obuf2, sizeof(obuf2), &written, NULL), " WriteFile3b\n");
127         ok(written == sizeof(obuf2), "write file len 3b\n");
128         ok(PeekNamedPipe(hFile, ibuf, sizeof(ibuf), &readden, &avail, NULL), "Peek3\n");
129         if (pipemode == PIPE_TYPE_BYTE) {
130             todo_wine {
131                 /* should return all 23 bytes */
132                 ok(readden == sizeof(obuf) + sizeof(obuf2), "peek3 got %ld bytes\n", readden);
133             }
134         }
135         else
136             ok(readden == sizeof(obuf), "peek3 got %ld bytes\n", readden);
137         if (avail != sizeof(obuf)) /* older Linux kernels only return the first write here */
138             ok(avail == sizeof(obuf) + sizeof(obuf2), "peek3 got %ld bytes available\n", avail);
139         pbuf = ibuf;
140         ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "pipe content 3a check\n");
141         if (pipemode == PIPE_TYPE_BYTE) {
142             todo_wine {
143                 pbuf += sizeof(obuf);
144                 ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "pipe content 3b check\n");
145             }
146         }
147         ok(ReadFile(hFile, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
148         ok(readden == sizeof(obuf) + sizeof(obuf2), "read 3 got %ld bytes\n", readden);
149         pbuf = ibuf;
150         ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 3a check\n");
151         pbuf += sizeof(obuf);
152         ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "content 3b check\n");
153
154         /* Multiple writes in the reverse direction */
155         memset(ibuf, 0, sizeof(ibuf));
156         ok(WriteFile(hFile, obuf, sizeof(obuf), &written, NULL), "WriteFile4a\n");
157         ok(written == sizeof(obuf), "write file len 4a\n");
158         ok(WriteFile(hFile, obuf2, sizeof(obuf2), &written, NULL), " WriteFile4b\n");
159         ok(written == sizeof(obuf2), "write file len 4b\n");
160         ok(PeekNamedPipe(hnp, ibuf, sizeof(ibuf), &readden, &avail, NULL), "Peek4\n");
161         if (pipemode == PIPE_TYPE_BYTE) {
162             todo_wine {
163                 /* should return all 23 bytes */
164                 ok(readden == sizeof(obuf) + sizeof(obuf2), "peek4 got %ld bytes\n", readden);
165             }
166         }
167         else
168             ok(readden == sizeof(obuf), "peek4 got %ld bytes\n", readden);
169         if (avail != sizeof(obuf)) /* older Linux kernels only return the first write here */
170             ok(avail == sizeof(obuf) + sizeof(obuf2), "peek4 got %ld bytes available\n", avail);
171         pbuf = ibuf;
172         ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "pipe content 4a check\n");
173         if (pipemode == PIPE_TYPE_BYTE) {
174             todo_wine {
175                 pbuf += sizeof(obuf);
176                 ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "pipe content 4b check\n");
177             }
178         }
179         ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
180         if (pipemode == PIPE_TYPE_BYTE) {
181             ok(readden == sizeof(obuf) + sizeof(obuf2), "read 4 got %ld bytes\n", readden);
182         }
183         else {
184             todo_wine {
185                 ok(readden == sizeof(obuf), "read 4 got %ld bytes\n", readden);
186             }
187         }
188         pbuf = ibuf;
189         ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 4a check\n");
190         if (pipemode == PIPE_TYPE_BYTE) {
191             pbuf += sizeof(obuf);
192             ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "content 4b check\n");
193         }
194
195         /* Test reading of multiple writes after a mode change
196           (CreateFile always creates a byte mode pipe) */
197         lpmode = PIPE_READMODE_MESSAGE;
198         if (pipemode == PIPE_TYPE_BYTE) {
199             /* trying to change the client end of a byte pipe to message mode should fail */
200             ok(!SetNamedPipeHandleState(hFile, &lpmode, NULL, NULL), "Change mode\n");
201         }
202         else {
203             todo_wine {
204                 ok(SetNamedPipeHandleState(hFile, &lpmode, NULL, NULL), "Change mode\n");
205             }
206         
207             memset(ibuf, 0, sizeof(ibuf));
208             ok(WriteFile(hnp, obuf, sizeof(obuf), &written, NULL), "WriteFile5a\n");
209             ok(written == sizeof(obuf), "write file len 3a\n");
210             ok(WriteFile(hnp, obuf2, sizeof(obuf2), &written, NULL), " WriteFile5b\n");
211             ok(written == sizeof(obuf2), "write file len 3b\n");
212             ok(PeekNamedPipe(hFile, ibuf, sizeof(ibuf), &readden, &avail, NULL), "Peek5\n");
213             ok(readden == sizeof(obuf), "peek5 got %ld bytes\n", readden);
214             if (avail != sizeof(obuf)) /* older Linux kernels only return the first write here */
215                 ok(avail == sizeof(obuf) + sizeof(obuf2), "peek5 got %ld bytes available\n", avail);
216             pbuf = ibuf;
217             ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 5a check\n");
218             ok(ReadFile(hFile, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
219             todo_wine {
220                 ok(readden == sizeof(obuf), "read 5 got %ld bytes\n", readden);
221             }
222             pbuf = ibuf;
223             ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 5a check\n");
224     
225             /* Multiple writes in the reverse direction */
226             /* the write of obuf2 from write4 should still be in the buffer */
227             ok(PeekNamedPipe(hnp, ibuf, sizeof(ibuf), &readden, &avail, NULL), "Peek6a\n");
228             todo_wine {
229                 ok(readden == sizeof(obuf2), "peek6a got %ld bytes\n", readden);
230                 ok(avail == sizeof(obuf2), "peek6a got %ld bytes available\n", avail);
231             }
232             if (avail > 0) {
233                 ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
234                 ok(readden == sizeof(obuf2), "read 6a got %ld bytes\n", readden);
235                 pbuf = ibuf;
236                 ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "content 6a check\n");
237             }
238             memset(ibuf, 0, sizeof(ibuf));
239             ok(WriteFile(hFile, obuf, sizeof(obuf), &written, NULL), "WriteFile6a\n");
240             ok(written == sizeof(obuf), "write file len 6a\n");
241             ok(WriteFile(hFile, obuf2, sizeof(obuf2), &written, NULL), " WriteFile6b\n");
242             ok(written == sizeof(obuf2), "write file len 6b\n");
243             ok(PeekNamedPipe(hnp, ibuf, sizeof(ibuf), &readden, &avail, NULL), "Peek6\n");
244             ok(readden == sizeof(obuf), "peek6 got %ld bytes\n", readden);
245             if (avail != sizeof(obuf)) /* older Linux kernels only return the first write here */
246                 ok(avail == sizeof(obuf) + sizeof(obuf2), "peek6b got %ld bytes available\n", avail);
247             pbuf = ibuf;
248             ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 6a check\n");
249             ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
250             todo_wine {
251                 ok(readden == sizeof(obuf), "read 6b got %ld bytes\n", readden);
252             }
253             pbuf = ibuf;
254             ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 6a check\n");
255         }
256
257         /* Picky conformance tests */
258
259         /* Verify that you can't connect to pipe again
260          * until server calls DisconnectNamedPipe+ConnectNamedPipe
261          * or creates a new pipe
262          * case 1: other client not yet closed
263          */
264         hFile2 = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
265         ok(hFile2 == INVALID_HANDLE_VALUE,
266             "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail\n");
267         ok(GetLastError() == ERROR_PIPE_BUSY,
268             "connecting to named pipe before other client closes should fail with ERROR_PIPE_BUSY\n");
269
270         ok(CloseHandle(hFile), "CloseHandle\n");
271
272         /* case 2: other client already closed */
273         hFile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
274         ok(hFile == INVALID_HANDLE_VALUE,
275             "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail\n");
276         ok(GetLastError() == ERROR_PIPE_BUSY,
277             "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail with ERROR_PIPE_BUSY\n");
278
279         ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe\n");
280
281         /* case 3: server has called DisconnectNamedPipe but not ConnectNamed Pipe */
282         hFile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
283         ok(hFile == 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 after other client closes but before ConnectNamedPipe should fail with ERROR_PIPE_BUSY\n");
287
288         /* to be complete, we'd call ConnectNamedPipe here and loop,
289          * but by default that's blocking, so we'd either have
290          * to turn on the uncommon nonblocking mode, or
291          * use another thread.
292          */
293     }
294
295     ok(CloseHandle(hnp), "CloseHandle\n");
296
297     trace("test_CreateNamedPipe returning\n");
298 }
299
300 static void test_CreateNamedPipe_instances_must_match(void)
301 {
302     HANDLE hnp, hnp2;
303
304     /* Check no mismatch */
305     hnp = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
306         /* nMaxInstances */ 2,
307         /* nOutBufSize */ 1024,
308         /* nInBufSize */ 1024,
309         /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
310         /* lpSecurityAttrib */ NULL);
311     ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
312
313     hnp2 = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
314         /* nMaxInstances */ 2,
315         /* nOutBufSize */ 1024,
316         /* nInBufSize */ 1024,
317         /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
318         /* lpSecurityAttrib */ NULL);
319     ok(hnp2 != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
320
321     ok(CloseHandle(hnp), "CloseHandle\n");
322     ok(CloseHandle(hnp2), "CloseHandle\n");
323
324     /* Check nMaxInstances */
325     hnp = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
326         /* nMaxInstances */ 1,
327         /* nOutBufSize */ 1024,
328         /* nInBufSize */ 1024,
329         /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
330         /* lpSecurityAttrib */ NULL);
331     ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
332
333     hnp2 = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
334         /* nMaxInstances */ 1,
335         /* nOutBufSize */ 1024,
336         /* nInBufSize */ 1024,
337         /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
338         /* lpSecurityAttrib */ NULL);
339     ok(hnp2 == INVALID_HANDLE_VALUE
340         && GetLastError() == ERROR_PIPE_BUSY, "nMaxInstances not obeyed\n");
341
342     ok(CloseHandle(hnp), "CloseHandle\n");
343
344     /* Check PIPE_ACCESS_* */
345     hnp = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
346         /* nMaxInstances */ 2,
347         /* nOutBufSize */ 1024,
348         /* nInBufSize */ 1024,
349         /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
350         /* lpSecurityAttrib */ NULL);
351     ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
352
353     hnp2 = CreateNamedPipe(PIPENAME, PIPE_ACCESS_INBOUND, PIPE_TYPE_BYTE | PIPE_WAIT,
354         /* nMaxInstances */ 1,
355         /* nOutBufSize */ 1024,
356         /* nInBufSize */ 1024,
357         /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
358         /* lpSecurityAttrib */ NULL);
359     ok(hnp2 == INVALID_HANDLE_VALUE
360         && GetLastError() == ERROR_ACCESS_DENIED, "PIPE_ACCESS_* mismatch allowed\n");
361
362     ok(CloseHandle(hnp), "CloseHandle\n");
363
364     /* etc, etc */
365 }
366
367 /** implementation of alarm() */
368 static DWORD CALLBACK alarmThreadMain(LPVOID arg)
369 {
370     DWORD timeout = (DWORD) arg;
371     trace("alarmThreadMain\n");
372     if (WaitForSingleObject( alarm_event, timeout ) == WAIT_TIMEOUT)
373     {
374         ok(FALSE, "alarm\n");
375         ExitProcess(1);
376     }
377     return 1;
378 }
379
380 HANDLE hnp = INVALID_HANDLE_VALUE;
381
382 /** Trivial byte echo server - disconnects after each session */
383 static DWORD CALLBACK serverThreadMain1(LPVOID arg)
384 {
385     int i;
386
387     trace("serverThreadMain1 start\n");
388     /* Set up a simple echo server */
389     hnp = CreateNamedPipe(PIPENAME "serverThreadMain1", PIPE_ACCESS_DUPLEX,
390         PIPE_TYPE_BYTE | PIPE_WAIT,
391         /* nMaxInstances */ 1,
392         /* nOutBufSize */ 1024,
393         /* nInBufSize */ 1024,
394         /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
395         /* lpSecurityAttrib */ NULL);
396
397     ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
398     for (i = 0; i < NB_SERVER_LOOPS; i++) {
399         char buf[512];
400         DWORD written;
401         DWORD readden;
402         DWORD success;
403
404         /* Wait for client to connect */
405         trace("Server calling ConnectNamedPipe...\n");
406         ok(ConnectNamedPipe(hnp, NULL)
407             || GetLastError() == ERROR_PIPE_CONNECTED, "ConnectNamedPipe\n");
408         trace("ConnectNamedPipe returned.\n");
409
410         /* Echo bytes once */
411         memset(buf, 0, sizeof(buf));
412
413         trace("Server reading...\n");
414         success = ReadFile(hnp, buf, sizeof(buf), &readden, NULL);
415         trace("Server done reading.\n");
416         ok(success, "ReadFile\n");
417         ok(readden, "short read\n");
418
419         trace("Server writing...\n");
420         ok(WriteFile(hnp, buf, readden, &written, NULL), "WriteFile\n");
421         trace("Server done writing.\n");
422         ok(written == readden, "write file len\n");
423
424         /* finish this connection, wait for next one */
425         ok(FlushFileBuffers(hnp), "FlushFileBuffers\n");
426         trace("Server done flushing.\n");
427         ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe\n");
428         trace("Server done disconnecting.\n");
429     }
430     return 0;
431 }
432
433 /** Trivial byte echo server - closes after each connection */
434 static DWORD CALLBACK serverThreadMain2(LPVOID arg)
435 {
436     int i;
437     HANDLE hnpNext = 0;
438
439     trace("serverThreadMain2\n");
440     /* Set up a simple echo server */
441     hnp = CreateNamedPipe(PIPENAME "serverThreadMain2", PIPE_ACCESS_DUPLEX,
442         PIPE_TYPE_BYTE | PIPE_WAIT,
443         /* nMaxInstances */ 2,
444         /* nOutBufSize */ 1024,
445         /* nInBufSize */ 1024,
446         /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
447         /* lpSecurityAttrib */ NULL);
448     ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
449
450     for (i = 0; i < NB_SERVER_LOOPS; i++) {
451         char buf[512];
452         DWORD written;
453         DWORD readden;
454         DWORD success;
455
456         /* Wait for client to connect */
457         trace("Server calling ConnectNamedPipe...\n");
458         ok(ConnectNamedPipe(hnp, NULL)
459             || GetLastError() == ERROR_PIPE_CONNECTED, "ConnectNamedPipe\n");
460         trace("ConnectNamedPipe returned.\n");
461
462         /* Echo bytes once */
463         memset(buf, 0, sizeof(buf));
464
465         trace("Server reading...\n");
466         success = ReadFile(hnp, buf, sizeof(buf), &readden, NULL);
467         trace("Server done reading.\n");
468         ok(success, "ReadFile\n");
469
470         trace("Server writing...\n");
471         ok(WriteFile(hnp, buf, readden, &written, NULL), "WriteFile\n");
472         trace("Server done writing.\n");
473         ok(written == readden, "write file len\n");
474
475         /* finish this connection, wait for next one */
476         ok(FlushFileBuffers(hnp), "FlushFileBuffers\n");
477         ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe\n");
478
479         /* Set up next echo server */
480         hnpNext =
481             CreateNamedPipe(PIPENAME "serverThreadMain2", PIPE_ACCESS_DUPLEX,
482             PIPE_TYPE_BYTE | PIPE_WAIT,
483             /* nMaxInstances */ 2,
484             /* nOutBufSize */ 1024,
485             /* nInBufSize */ 1024,
486             /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
487             /* lpSecurityAttrib */ NULL);
488
489         ok(hnpNext != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
490
491         ok(CloseHandle(hnp), "CloseHandle\n");
492         hnp = hnpNext;
493     }
494     return 0;
495 }
496
497 /** Trivial byte echo server - uses overlapped named pipe calls */
498 static DWORD CALLBACK serverThreadMain3(LPVOID arg)
499 {
500     int i;
501     HANDLE hEvent;
502
503     trace("serverThreadMain3\n");
504     /* Set up a simple echo server */
505     hnp = CreateNamedPipe(PIPENAME "serverThreadMain3", PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
506         PIPE_TYPE_BYTE | PIPE_WAIT,
507         /* nMaxInstances */ 1,
508         /* nOutBufSize */ 1024,
509         /* nInBufSize */ 1024,
510         /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
511         /* lpSecurityAttrib */ NULL);
512     ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
513
514     hEvent = CreateEvent(NULL,  /* security attribute */
515         TRUE,                   /* manual reset event */
516         FALSE,                  /* initial state */
517         NULL);                  /* name */
518     ok(hEvent != NULL, "CreateEvent\n");
519
520     for (i = 0; i < NB_SERVER_LOOPS; i++) {
521         char buf[512];
522         DWORD written;
523         DWORD readden;
524         DWORD dummy;
525         DWORD success;
526         OVERLAPPED oOverlap;
527         int letWFSOEwait = (i & 2);
528         int letGORwait = (i & 1);
529         DWORD err;
530
531         memset(&oOverlap, 0, sizeof(oOverlap));
532         oOverlap.hEvent = hEvent;
533
534         /* Wait for client to connect */
535         trace("Server calling overlapped ConnectNamedPipe...\n");
536         success = ConnectNamedPipe(hnp, &oOverlap);
537         err = GetLastError();
538         ok(success || err == ERROR_IO_PENDING
539             || err == ERROR_PIPE_CONNECTED, "overlapped ConnectNamedPipe\n");
540         trace("overlapped ConnectNamedPipe returned.\n");
541         if (!success && (err == ERROR_IO_PENDING) && letWFSOEwait)
542             ok(WaitForSingleObjectEx(hEvent, INFINITE, TRUE) == 0, "wait ConnectNamedPipe\n");
543         success = GetOverlappedResult(hnp, &oOverlap, &dummy, letGORwait);
544         if (!letGORwait && !letWFSOEwait && !success) {
545             ok(GetLastError() == ERROR_IO_INCOMPLETE, "GetOverlappedResult\n");
546             success = GetOverlappedResult(hnp, &oOverlap, &dummy, TRUE);
547         }
548         ok(success, "GetOverlappedResult ConnectNamedPipe\n");
549         trace("overlapped ConnectNamedPipe operation complete.\n");
550
551         /* Echo bytes once */
552         memset(buf, 0, sizeof(buf));
553
554         trace("Server reading...\n");
555         success = ReadFile(hnp, buf, sizeof(buf), NULL, &oOverlap);
556         trace("Server ReadFile returned...\n");
557         err = GetLastError();
558         ok(success || err == ERROR_IO_PENDING, "overlapped ReadFile\n");
559         trace("overlapped ReadFile returned.\n");
560         if (!success && (err == ERROR_IO_PENDING) && letWFSOEwait)
561             ok(WaitForSingleObjectEx(hEvent, INFINITE, TRUE) == 0, "wait ReadFile\n");
562         success = GetOverlappedResult(hnp, &oOverlap, &readden, letGORwait);
563         if (!letGORwait && !letWFSOEwait && !success) {
564             ok(GetLastError() == ERROR_IO_INCOMPLETE, "GetOverlappedResult\n");
565             success = GetOverlappedResult(hnp, &oOverlap, &readden, TRUE);
566         }
567         trace("Server done reading.\n");
568         ok(success, "overlapped ReadFile\n");
569
570         trace("Server writing...\n");
571         success = WriteFile(hnp, buf, readden, NULL, &oOverlap);
572         trace("Server WriteFile returned...\n");
573         err = GetLastError();
574         ok(success || err == ERROR_IO_PENDING, "overlapped WriteFile\n");
575         trace("overlapped WriteFile returned.\n");
576         if (!success && (err == ERROR_IO_PENDING) && letWFSOEwait)
577             ok(WaitForSingleObjectEx(hEvent, INFINITE, TRUE) == 0, "wait WriteFile\n");
578         success = GetOverlappedResult(hnp, &oOverlap, &written, letGORwait);
579         if (!letGORwait && !letWFSOEwait && !success) {
580             ok(GetLastError() == ERROR_IO_INCOMPLETE, "GetOverlappedResult\n");
581             success = GetOverlappedResult(hnp, &oOverlap, &written, TRUE);
582         }
583         trace("Server done writing.\n");
584         ok(success, "overlapped WriteFile\n");
585         ok(written == readden, "write file len\n");
586
587         /* finish this connection, wait for next one */
588         ok(FlushFileBuffers(hnp), "FlushFileBuffers\n");
589         ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe\n");
590     }
591     return 0;
592 }
593
594 static void exercizeServer(const char *pipename, HANDLE serverThread)
595 {
596     int i;
597
598     trace("exercizeServer starting\n");
599     for (i = 0; i < NB_SERVER_LOOPS; i++) {
600         HANDLE hFile=INVALID_HANDLE_VALUE;
601         static const char obuf[] = "Bit Bucket";
602         char ibuf[32];
603         DWORD written;
604         DWORD readden;
605         int loop;
606
607         for (loop = 0; loop < 3; loop++) {
608             DWORD err;
609             trace("Client connecting...\n");
610             /* Connect to the server */
611             hFile = CreateFileA(pipename, GENERIC_READ | GENERIC_WRITE, 0,
612                 NULL, OPEN_EXISTING, 0, 0);
613             if (hFile != INVALID_HANDLE_VALUE)
614                 break;
615             err = GetLastError();
616             if (loop == 0)
617                 ok(err == ERROR_PIPE_BUSY || err == ERROR_FILE_NOT_FOUND, "connecting to pipe\n");
618             else
619                 ok(err == ERROR_PIPE_BUSY, "connecting to pipe\n");
620             trace("connect failed, retrying\n");
621             Sleep(200);
622         }
623         ok(hFile != INVALID_HANDLE_VALUE, "client opening named pipe\n");
624
625         /* Make sure it can echo */
626         memset(ibuf, 0, sizeof(ibuf));
627         trace("Client writing...\n");
628         ok(WriteFile(hFile, obuf, sizeof(obuf), &written, NULL), "WriteFile to client end of pipe\n");
629         ok(written == sizeof(obuf), "write file len\n");
630         trace("Client reading...\n");
631         ok(ReadFile(hFile, ibuf, sizeof(obuf), &readden, NULL), "ReadFile from client end of pipe\n");
632         ok(readden == sizeof(obuf), "read file len\n");
633         ok(memcmp(obuf, ibuf, written) == 0, "content check\n");
634
635         trace("Client closing...\n");
636         ok(CloseHandle(hFile), "CloseHandle\n");
637     }
638
639     ok(WaitForSingleObject(serverThread,INFINITE) == WAIT_OBJECT_0, "WaitForSingleObject\n");
640     CloseHandle(hnp);
641     trace("exercizeServer returning\n");
642 }
643
644 static void test_NamedPipe_2(void)
645 {
646     HANDLE serverThread;
647     DWORD serverThreadId;
648     HANDLE alarmThread;
649     DWORD alarmThreadId;
650
651     trace("test_NamedPipe_2 starting\n");
652     /* Set up a ten second timeout */
653     alarm_event = CreateEvent( NULL, TRUE, FALSE, NULL );
654     alarmThread = CreateThread(NULL, 0, alarmThreadMain, (void *) 10000, 0, &alarmThreadId);
655
656     /* The servers we're about to exercize do try to clean up carefully,
657      * but to reduce the change of a test failure due to a pipe handle
658      * leak in the test code, we'll use a different pipe name for each server.
659      */
660
661     /* Try server #1 */
662     serverThread = CreateThread(NULL, 0, serverThreadMain1, (void *)8, 0, &serverThreadId);
663     ok(serverThread != INVALID_HANDLE_VALUE, "CreateThread\n");
664     exercizeServer(PIPENAME "serverThreadMain1", serverThread);
665
666     /* Try server #2 */
667     serverThread = CreateThread(NULL, 0, serverThreadMain2, 0, 0, &serverThreadId);
668     ok(serverThread != INVALID_HANDLE_VALUE, "CreateThread\n");
669     exercizeServer(PIPENAME "serverThreadMain2", serverThread);
670
671     if( 0 ) /* overlapped pipe server doesn't work yet - it randomly fails */
672     {
673     /* Try server #3 */
674     serverThread = CreateThread(NULL, 0, serverThreadMain3, 0, 0, &serverThreadId);
675     ok(serverThread != INVALID_HANDLE_VALUE, "CreateThread\n");
676     exercizeServer(PIPENAME "serverThreadMain3", serverThread);
677     }
678
679     ok(SetEvent( alarm_event ), "SetEvent\n");
680     CloseHandle( alarm_event );
681     trace("test_NamedPipe_2 returning\n");
682 }
683
684 static int test_DisconnectNamedPipe(void)
685 {
686     HANDLE hnp;
687     HANDLE hFile;
688     static const char obuf[] = "Bit Bucket";
689     char ibuf[32];
690     DWORD written;
691     DWORD readden;
692
693     hnp = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
694         /* nMaxInstances */ 1,
695         /* nOutBufSize */ 1024,
696         /* nInBufSize */ 1024,
697         /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
698         /* lpSecurityAttrib */ NULL);
699     if (INVALID_HANDLE_VALUE == hnp) {
700         trace ("Seems we have no named pipes.\n");
701         return 1;
702     }
703
704     ok(WriteFile(hnp, obuf, sizeof(obuf), &written, NULL) == 0
705         && GetLastError() == ERROR_PIPE_LISTENING, "WriteFile to not-yet-connected pipe\n");
706     ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL) == 0
707         && GetLastError() == ERROR_PIPE_LISTENING, "ReadFile from not-yet-connected pipe\n");
708
709     hFile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
710     ok(hFile != INVALID_HANDLE_VALUE, "CreateFile failed\n");
711
712     /* don't try to do i/o if one side couldn't be opened, as it hangs */
713     if (hFile != INVALID_HANDLE_VALUE) {
714
715         /* see what happens if server calls DisconnectNamedPipe
716          * when there are bytes in the pipe
717          */
718
719         ok(WriteFile(hFile, obuf, sizeof(obuf), &written, NULL), "WriteFile\n");
720         ok(written == sizeof(obuf), "write file len\n");
721         ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe while messages waiting\n");
722         ok(WriteFile(hFile, obuf, sizeof(obuf), &written, NULL) == 0
723             && GetLastError() == ERROR_PIPE_NOT_CONNECTED, "WriteFile to disconnected pipe\n");
724         ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL) == 0
725             && GetLastError() == ERROR_PIPE_NOT_CONNECTED,
726             "ReadFile from disconnected pipe with bytes waiting\n");
727         ok(CloseHandle(hFile), "CloseHandle\n");
728     }
729
730     ok(CloseHandle(hnp), "CloseHandle\n");
731
732     return 0;
733 }
734 static void test_CreatePipe(void)
735 {
736     SECURITY_ATTRIBUTES pipe_attr;
737     HANDLE piperead, pipewrite;
738     DWORD written;
739     DWORD read;
740     char readbuf[32];
741
742     pipe_attr.nLength = sizeof(SECURITY_ATTRIBUTES); 
743     pipe_attr.bInheritHandle = TRUE; 
744     pipe_attr.lpSecurityDescriptor = NULL; 
745     ok(CreatePipe(&piperead, &pipewrite, &pipe_attr, 0) != 0, "CreatePipe failed\n");
746     ok(WriteFile(pipewrite,PIPENAME,sizeof(PIPENAME), &written, NULL), "Write to anonymous pipe failed\n");
747     ok(written == sizeof(PIPENAME), "Write to anonymous pipe wrote %ld bytes instead of %d\n", written,sizeof(PIPENAME));
748     ok(ReadFile(piperead,readbuf,sizeof(readbuf),&read, NULL), "Read from non empty pipe failed\n");
749     ok(read == sizeof(PIPENAME), "Read from  anonymous pipe got %ld bytes instead of %d\n", read, sizeof(PIPENAME));
750
751     /* Now write another chunk*/
752     ok(CreatePipe(&piperead, &pipewrite, &pipe_attr, 0) != 0, "CreatePipe failed\n");
753     ok(WriteFile(pipewrite,PIPENAME,sizeof(PIPENAME), &written, NULL), "Write to anonymous pipe failed\n");
754     ok(written == sizeof(PIPENAME), "Write to anonymous pipe wrote %ld bytes instead of %d\n", written,sizeof(PIPENAME));
755     /* and close the write end, read should still succeed*/
756     ok(CloseHandle(pipewrite), "CloseHandle for the Write Pipe failed\n");
757     ok(ReadFile(piperead,readbuf,sizeof(readbuf),&read, NULL), "Read from broken pipe withe with pending data failed\n");
758     ok(read == sizeof(PIPENAME), "Read from  anonymous pipe got %ld bytes instead of %d\n", read, sizeof(PIPENAME));
759     /* But now we need to get informed that the pipe is closed */
760     todo_wine ok(ReadFile(piperead,readbuf,sizeof(readbuf),&read, NULL) == 0, "Broken pipe not detected\n");
761 }
762
763 START_TEST(pipe)
764 {
765     trace("test 1 of 6:\n");
766     if (test_DisconnectNamedPipe())
767         return;
768     trace("test 2 of 6:\n");
769     test_CreateNamedPipe_instances_must_match();
770     trace("test 3 of 6:\n");
771     test_NamedPipe_2();
772     trace("test 4 of 6:\n");
773     test_CreateNamedPipe(PIPE_TYPE_BYTE);
774     trace("test 5 of 6\n");
775     test_CreateNamedPipe(PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE);
776     trace("test 6 of 6\n");
777     test_CreatePipe();
778     trace("all tests done\n");
779 }