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