2 * Unit tests for named pipe functions in Wine
4 * Copyright (c) 2002 Dan Kegel
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include "wine/test.h"
35 #define START_TEST(name) main(int argc, char **argv)
36 #define ok(condition, msg) \
40 fprintf(stderr,"failed at %d\n",__LINE__); \
51 #define PIPENAME "\\\\.\\PiPe\\tests_" __FILE__
53 void test_CreateNamedPipe(pipemode)
57 const char obuf[] = "Bit Bucket";
58 const char obuf2[] = "More bits";
65 if (pipemode == PIPE_TYPE_BYTE)
66 trace("test_CreateNamedPipe starting in byte mode\n");
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);
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");
83 ok(hnp == INVALID_HANDLE_VALUE && GetLastError() == ERROR_INVALID_NAME,
84 "CreateNamedPipe should fail if name doesn't start with \\\\.\\pipe\n");
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");
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");
97 /* Functional checks */
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");
107 hFile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
108 ok(hFile != INVALID_HANDLE_VALUE, "CreateFile failed\n");
110 /* don't try to do i/o if one side couldn't be opened, as it hangs */
111 if (hFile != INVALID_HANDLE_VALUE) {
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");
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");
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) {
142 /* should return all 23 bytes */
143 ok(readden == sizeof(obuf) + sizeof(obuf2), "peek3 got %ld bytes\n", readden);
147 ok(readden == sizeof(obuf), "peek3 got %ld bytes\n", readden);
149 ok(avail == sizeof(obuf) + sizeof(obuf2), "peek3 got %ld bytes available\n", avail);
152 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "pipe content 3a check\n");
153 if (pipemode == PIPE_TYPE_BYTE) {
155 pbuf += sizeof(obuf);
156 ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "pipe content 3b check\n");
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);
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");
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) {
175 /* should return all 23 bytes */
176 ok(readden == sizeof(obuf) + sizeof(obuf2), "peek4 got %ld bytes\n", readden);
180 ok(readden == sizeof(obuf), "peek4 got %ld bytes\n", readden);
182 ok(avail == sizeof(obuf) + sizeof(obuf2), "peek4 got %ld bytes available\n", avail);
185 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "pipe content 4a check\n");
186 if (pipemode == PIPE_TYPE_BYTE) {
188 pbuf += sizeof(obuf);
189 ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "pipe content 4b check\n");
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);
198 ok(readden == sizeof(obuf), "read 4 got %ld bytes\n", readden);
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");
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");
217 ok(SetNamedPipeHandleState(hFile, &lpmode, NULL, NULL), "Change mode\n");
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);
228 ok(avail == sizeof(obuf) + sizeof(obuf2), "peek5 got %ld bytes available\n", avail);
231 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 5a check\n");
232 ok(ReadFile(hFile, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
234 ok(readden == sizeof(obuf), "read 5 got %ld bytes\n", readden);
237 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 5a check\n");
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");
243 ok(readden == sizeof(obuf2), "peek6a got %ld bytes\n", readden);
244 ok(avail == sizeof(obuf2), "peek6a got %ld bytes available\n", avail);
247 ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
248 ok(readden == sizeof(obuf2), "read 6a got %ld bytes\n", readden);
250 ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "content 6a check\n");
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);
260 ok(avail == sizeof(obuf) + sizeof(obuf2), "peek6b got %ld bytes available\n", avail);
263 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 6a check\n");
264 ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
266 ok(readden == sizeof(obuf), "read 6b got %ld bytes\n", readden);
269 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 6a check\n");
272 /* Picky conformance tests */
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
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");
285 ok(CloseHandle(hFile), "CloseHandle\n");
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");
294 ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe\n");
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");
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.
310 ok(CloseHandle(hnp), "CloseHandle\n");
312 trace("test_CreateNamedPipe returning\n");
315 void test_CreateNamedPipe_instances_must_match(void)
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");
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");
336 ok(CloseHandle(hnp), "CloseHandle\n");
337 ok(CloseHandle(hnp2), "CloseHandle\n");
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");
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");
357 ok(CloseHandle(hnp), "CloseHandle\n");
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");
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");
377 ok(CloseHandle(hnp), "CloseHandle\n");
382 /** implementation of alarm() */
383 static DWORD CALLBACK alarmThreadMain(LPVOID arg)
385 DWORD timeout = (DWORD) arg;
386 trace("alarmThreadMain\n");
388 ok(FALSE, "alarm\n");
393 HANDLE hnp = INVALID_HANDLE_VALUE;
395 /** Trivial byte echo server - disconnects after each session */
396 static DWORD CALLBACK serverThreadMain1(LPVOID arg)
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);
410 ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
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");
423 /* Echo bytes once */
424 memset(buf, 0, sizeof(buf));
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");
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");
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");
445 /** Trivial byte echo server - closes after each connection */
446 static DWORD CALLBACK serverThreadMain2(LPVOID arg)
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");
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");
474 /* Echo bytes once */
475 memset(buf, 0, sizeof(buf));
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");
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");
487 /* finish this connection, wait for next one */
488 ok(FlushFileBuffers(hnp), "FlushFileBuffers\n");
489 ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe\n");
491 /* Set up next echo server */
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);
501 ok(hnpNext != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
503 ok(CloseHandle(hnp), "CloseHandle\n");
508 /** Trivial byte echo server - uses overlapped named pipe calls */
509 static DWORD CALLBACK serverThreadMain3(LPVOID arg)
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");
525 hEvent = CreateEvent(NULL, /* security attribute */
526 TRUE, /* manual reset event */
527 FALSE, /* initial state */
529 ok(hEvent != NULL, "CreateEvent\n");
538 int letWFSOEwait = (i & 2);
539 int letGORwait = (i & 1);
542 memset(&oOverlap, 0, sizeof(oOverlap));
543 oOverlap.hEvent = hEvent;
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);
559 ok(success, "GetOverlappedResult ConnectNamedPipe\n");
560 trace("overlapped ConnectNamedPipe operation complete.\n");
562 /* Echo bytes once */
563 memset(buf, 0, sizeof(buf));
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);
578 trace("Server done reading.\n");
579 ok(success, "overlapped ReadFile\n");
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);
594 trace("Server done writing.\n");
595 ok(success, "overlapped WriteFile\n");
596 ok(written == readden, "write file len\n");
598 /* finish this connection, wait for next one */
599 ok(FlushFileBuffers(hnp), "FlushFileBuffers\n");
600 ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe\n");
604 static void exercizeServer(const char *pipename, HANDLE serverThread)
608 trace("exercizeServer starting\n");
609 for (i = 0; i < 8; i++) {
610 HANDLE hFile=INVALID_HANDLE_VALUE;
611 const char obuf[] = "Bit Bucket";
617 for (loop = 0; loop < 3; loop++) {
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)
625 err = GetLastError();
627 ok(err == ERROR_PIPE_BUSY || err == ERROR_FILE_NOT_FOUND, "connecting to pipe\n");
629 ok(err == ERROR_PIPE_BUSY, "connecting to pipe\n");
630 trace("connect failed, retrying\n");
633 ok(hFile != INVALID_HANDLE_VALUE, "client opening named pipe\n");
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");
645 trace("Client closing...\n");
646 ok(CloseHandle(hFile), "CloseHandle\n");
649 ok(TerminateThread(serverThread, 0), "TerminateThread\n");
651 trace("exercizeServer returning\n");
654 void test_NamedPipe_2(void)
657 DWORD serverThreadId;
661 trace("test_NamedPipe_2 starting\n");
662 /* Set up a ten second timeout */
663 alarmThread = CreateThread(NULL, 0, alarmThreadMain, (void *) 10000, 0, &alarmThreadId);
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.
671 serverThread = CreateThread(NULL, 0, serverThreadMain1, 0, 0, &serverThreadId);
672 ok(serverThread != INVALID_HANDLE_VALUE, "CreateThread\n");
673 exercizeServer(PIPENAME "serverThreadMain1", serverThread);
676 serverThread = CreateThread(NULL, 0, serverThreadMain2, 0, 0, &serverThreadId);
677 ok(serverThread != INVALID_HANDLE_VALUE, "CreateThread\n");
678 exercizeServer(PIPENAME "serverThreadMain2", serverThread);
680 if( 0 ) /* overlapped pipe server doesn't work yet - it randomly fails */
683 serverThread = CreateThread(NULL, 0, serverThreadMain3, 0, 0, &serverThreadId);
684 ok(serverThread != INVALID_HANDLE_VALUE, "CreateThread\n");
685 exercizeServer(PIPENAME "serverThreadMain3", serverThread);
688 ok(TerminateThread(alarmThread, 0), "TerminateThread\n");
689 trace("test_NamedPipe_2 returning\n");
692 void test_DisconnectNamedPipe(void)
696 const char obuf[] = "Bit Bucket";
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");
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");
714 hFile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
715 ok(hFile != INVALID_HANDLE_VALUE, "CreateFile failed\n");
717 /* don't try to do i/o if one side couldn't be opened, as it hangs */
718 if (hFile != INVALID_HANDLE_VALUE) {
720 /* see what happens if server calls DisconnectNamedPipe
721 * when there are bytes in the pipe
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");
735 ok(CloseHandle(hnp), "CloseHandle\n");
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");
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");