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