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 #define NB_SERVER_LOOPS 8
55 static HANDLE alarm_event;
57 static void test_CreateNamedPipe(pipemode)
61 static const char obuf[] = "Bit Bucket";
62 static const char obuf2[] = "More bits";
69 if (pipemode == PIPE_TYPE_BYTE)
70 trace("test_CreateNamedPipe starting in byte mode\n");
72 trace("test_CreateNamedPipe starting in message mode\n");
73 /* Bad parameter checks */
74 hnp = CreateNamedPipe("not a named pipe", PIPE_ACCESS_DUPLEX, pipemode | PIPE_WAIT,
75 /* nMaxInstances */ 1,
76 /* nOutBufSize */ 1024,
77 /* nInBufSize */ 1024,
78 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
79 /* lpSecurityAttrib */ NULL);
81 if (hnp == INVALID_HANDLE_VALUE && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) {
82 /* Is this the right way to notify user of skipped tests? */
83 ok(hnp == INVALID_HANDLE_VALUE && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED,
84 "CreateNamedPipe not supported on this platform, skipping tests.\n");
87 ok(hnp == INVALID_HANDLE_VALUE && GetLastError() == ERROR_INVALID_NAME,
88 "CreateNamedPipe should fail if name doesn't start with \\\\.\\pipe\n");
90 hnp = CreateNamedPipe(NULL,
91 PIPE_ACCESS_DUPLEX, pipemode | PIPE_WAIT,
92 1, 1024, 1024, NMPWAIT_USE_DEFAULT_WAIT, NULL);
93 ok(hnp == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
94 "CreateNamedPipe should fail if name is NULL\n");
96 hFile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
97 ok(hFile == INVALID_HANDLE_VALUE
98 && GetLastError() == ERROR_FILE_NOT_FOUND,
99 "connecting to nonexistent named pipe should fail with ERROR_FILE_NOT_FOUND\n");
101 /* Functional checks */
103 hnp = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, pipemode | PIPE_WAIT,
104 /* nMaxInstances */ 1,
105 /* nOutBufSize */ 1024,
106 /* nInBufSize */ 1024,
107 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
108 /* lpSecurityAttrib */ NULL);
109 ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
111 hFile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
112 ok(hFile != INVALID_HANDLE_VALUE, "CreateFile failed\n");
114 /* don't try to do i/o if one side couldn't be opened, as it hangs */
115 if (hFile != INVALID_HANDLE_VALUE) {
118 /* Make sure we can read and write a few bytes in both directions */
119 memset(ibuf, 0, sizeof(ibuf));
120 ok(WriteFile(hnp, obuf, sizeof(obuf), &written, NULL), "WriteFile\n");
121 ok(written == sizeof(obuf), "write file len 1\n");
122 ok(PeekNamedPipe(hFile, NULL, 0, NULL, &readden, NULL), "Peek\n");
123 ok(readden == sizeof(obuf), "peek 1 got %ld bytes\n", readden);
124 ok(ReadFile(hFile, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
125 ok(readden == sizeof(obuf), "read 1 got %ld bytes\n", readden);
126 ok(memcmp(obuf, ibuf, written) == 0, "content 1 check\n");
128 memset(ibuf, 0, sizeof(ibuf));
129 ok(WriteFile(hFile, obuf2, sizeof(obuf2), &written, NULL), "WriteFile\n");
130 ok(written == sizeof(obuf2), "write file len 2\n");
131 ok(PeekNamedPipe(hnp, NULL, 0, NULL, &readden, NULL), "Peek\n");
132 ok(readden == sizeof(obuf2), "peek 2 got %ld bytes\n", readden);
133 ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
134 ok(readden == sizeof(obuf2), "read 2 got %ld bytes\n", readden);
135 ok(memcmp(obuf2, ibuf, written) == 0, "content 2 check\n");
137 /* Test reading of multiple writes */
138 memset(ibuf, 0, sizeof(ibuf));
139 ok(WriteFile(hnp, obuf, sizeof(obuf), &written, NULL), "WriteFile3a\n");
140 ok(written == sizeof(obuf), "write file len 3a\n");
141 ok(WriteFile(hnp, obuf2, sizeof(obuf2), &written, NULL), " WriteFile3b\n");
142 ok(written == sizeof(obuf2), "write file len 3b\n");
143 ok(PeekNamedPipe(hFile, ibuf, sizeof(ibuf), &readden, &avail, NULL), "Peek3\n");
144 if (pipemode == PIPE_TYPE_BYTE) {
146 /* should return all 23 bytes */
147 ok(readden == sizeof(obuf) + sizeof(obuf2), "peek3 got %ld bytes\n", readden);
151 ok(readden == sizeof(obuf), "peek3 got %ld bytes\n", readden);
153 ok(avail == sizeof(obuf) + sizeof(obuf2), "peek3 got %ld bytes available\n", avail);
156 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "pipe content 3a check\n");
157 if (pipemode == PIPE_TYPE_BYTE) {
159 pbuf += sizeof(obuf);
160 ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "pipe content 3b check\n");
163 ok(ReadFile(hFile, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
164 ok(readden == sizeof(obuf) + sizeof(obuf2), "read 3 got %ld bytes\n", readden);
166 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 3a check\n");
167 pbuf += sizeof(obuf);
168 ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "content 3b check\n");
170 /* Multiple writes in the reverse direction */
171 memset(ibuf, 0, sizeof(ibuf));
172 ok(WriteFile(hFile, obuf, sizeof(obuf), &written, NULL), "WriteFile4a\n");
173 ok(written == sizeof(obuf), "write file len 4a\n");
174 ok(WriteFile(hFile, obuf2, sizeof(obuf2), &written, NULL), " WriteFile4b\n");
175 ok(written == sizeof(obuf2), "write file len 4b\n");
176 ok(PeekNamedPipe(hnp, ibuf, sizeof(ibuf), &readden, &avail, NULL), "Peek4\n");
177 if (pipemode == PIPE_TYPE_BYTE) {
179 /* should return all 23 bytes */
180 ok(readden == sizeof(obuf) + sizeof(obuf2), "peek4 got %ld bytes\n", readden);
184 ok(readden == sizeof(obuf), "peek4 got %ld bytes\n", readden);
186 ok(avail == sizeof(obuf) + sizeof(obuf2), "peek4 got %ld bytes available\n", avail);
189 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "pipe content 4a check\n");
190 if (pipemode == PIPE_TYPE_BYTE) {
192 pbuf += sizeof(obuf);
193 ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "pipe content 4b check\n");
196 ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
197 if (pipemode == PIPE_TYPE_BYTE) {
198 ok(readden == sizeof(obuf) + sizeof(obuf2), "read 4 got %ld bytes\n", readden);
202 ok(readden == sizeof(obuf), "read 4 got %ld bytes\n", readden);
206 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 4a check\n");
207 if (pipemode == PIPE_TYPE_BYTE) {
208 pbuf += sizeof(obuf);
209 ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "content 4b check\n");
212 /* Test reading of multiple writes after a mode change
213 (CreateFile always creates a byte mode pipe) */
214 lpmode = PIPE_READMODE_MESSAGE;
215 if (pipemode == PIPE_TYPE_BYTE) {
216 /* trying to change the client end of a byte pipe to message mode should fail */
217 ok(!SetNamedPipeHandleState(hFile, &lpmode, NULL, NULL), "Change mode\n");
221 ok(SetNamedPipeHandleState(hFile, &lpmode, NULL, NULL), "Change mode\n");
224 memset(ibuf, 0, sizeof(ibuf));
225 ok(WriteFile(hnp, obuf, sizeof(obuf), &written, NULL), "WriteFile5a\n");
226 ok(written == sizeof(obuf), "write file len 3a\n");
227 ok(WriteFile(hnp, obuf2, sizeof(obuf2), &written, NULL), " WriteFile5b\n");
228 ok(written == sizeof(obuf2), "write file len 3b\n");
229 ok(PeekNamedPipe(hFile, ibuf, sizeof(ibuf), &readden, &avail, NULL), "Peek5\n");
230 ok(readden == sizeof(obuf), "peek5 got %ld bytes\n", readden);
232 ok(avail == sizeof(obuf) + sizeof(obuf2), "peek5 got %ld bytes available\n", avail);
235 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 5a check\n");
236 ok(ReadFile(hFile, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
238 ok(readden == sizeof(obuf), "read 5 got %ld bytes\n", readden);
241 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 5a check\n");
243 /* Multiple writes in the reverse direction */
244 /* the write of obuf2 from write4 should still be in the buffer */
245 ok(PeekNamedPipe(hnp, ibuf, sizeof(ibuf), &readden, &avail, NULL), "Peek6a\n");
247 ok(readden == sizeof(obuf2), "peek6a got %ld bytes\n", readden);
248 ok(avail == sizeof(obuf2), "peek6a got %ld bytes available\n", avail);
251 ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
252 ok(readden == sizeof(obuf2), "read 6a got %ld bytes\n", readden);
254 ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "content 6a check\n");
256 memset(ibuf, 0, sizeof(ibuf));
257 ok(WriteFile(hFile, obuf, sizeof(obuf), &written, NULL), "WriteFile6a\n");
258 ok(written == sizeof(obuf), "write file len 6a\n");
259 ok(WriteFile(hFile, obuf2, sizeof(obuf2), &written, NULL), " WriteFile6b\n");
260 ok(written == sizeof(obuf2), "write file len 6b\n");
261 ok(PeekNamedPipe(hnp, ibuf, sizeof(ibuf), &readden, &avail, NULL), "Peek6\n");
262 ok(readden == sizeof(obuf), "peek6 got %ld bytes\n", readden);
264 ok(avail == sizeof(obuf) + sizeof(obuf2), "peek6b got %ld bytes available\n", avail);
267 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 6a check\n");
268 ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
270 ok(readden == sizeof(obuf), "read 6b got %ld bytes\n", readden);
273 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 6a check\n");
276 /* Picky conformance tests */
278 /* Verify that you can't connect to pipe again
279 * until server calls DisconnectNamedPipe+ConnectNamedPipe
280 * or creates a new pipe
281 * case 1: other client not yet closed
283 hFile2 = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
284 ok(hFile2 == INVALID_HANDLE_VALUE,
285 "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail\n");
286 ok(GetLastError() == ERROR_PIPE_BUSY,
287 "connecting to named pipe before other client closes should fail with ERROR_PIPE_BUSY\n");
289 ok(CloseHandle(hFile), "CloseHandle\n");
291 /* case 2: other client already closed */
292 hFile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
293 ok(hFile == INVALID_HANDLE_VALUE,
294 "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail\n");
295 ok(GetLastError() == ERROR_PIPE_BUSY,
296 "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail with ERROR_PIPE_BUSY\n");
298 ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe\n");
300 /* case 3: server has called DisconnectNamedPipe but not ConnectNamed Pipe */
301 hFile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
302 ok(hFile == INVALID_HANDLE_VALUE,
303 "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail\n");
304 ok(GetLastError() == ERROR_PIPE_BUSY,
305 "connecting to named pipe after other client closes but before ConnectNamedPipe should fail with ERROR_PIPE_BUSY\n");
307 /* to be complete, we'd call ConnectNamedPipe here and loop,
308 * but by default that's blocking, so we'd either have
309 * to turn on the uncommon nonblocking mode, or
310 * use another thread.
314 ok(CloseHandle(hnp), "CloseHandle\n");
316 trace("test_CreateNamedPipe returning\n");
319 void test_CreateNamedPipe_instances_must_match(void)
323 /* Check no mismatch */
324 hnp = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
325 /* nMaxInstances */ 2,
326 /* nOutBufSize */ 1024,
327 /* nInBufSize */ 1024,
328 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
329 /* lpSecurityAttrib */ NULL);
330 ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
332 hnp2 = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
333 /* nMaxInstances */ 2,
334 /* nOutBufSize */ 1024,
335 /* nInBufSize */ 1024,
336 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
337 /* lpSecurityAttrib */ NULL);
338 ok(hnp2 != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
340 ok(CloseHandle(hnp), "CloseHandle\n");
341 ok(CloseHandle(hnp2), "CloseHandle\n");
343 /* Check nMaxInstances */
344 hnp = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
345 /* nMaxInstances */ 1,
346 /* nOutBufSize */ 1024,
347 /* nInBufSize */ 1024,
348 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
349 /* lpSecurityAttrib */ NULL);
350 ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
352 hnp2 = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
353 /* nMaxInstances */ 1,
354 /* nOutBufSize */ 1024,
355 /* nInBufSize */ 1024,
356 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
357 /* lpSecurityAttrib */ NULL);
358 ok(hnp2 == INVALID_HANDLE_VALUE
359 && GetLastError() == ERROR_PIPE_BUSY, "nMaxInstances not obeyed\n");
361 ok(CloseHandle(hnp), "CloseHandle\n");
363 /* Check PIPE_ACCESS_* */
364 hnp = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
365 /* nMaxInstances */ 2,
366 /* nOutBufSize */ 1024,
367 /* nInBufSize */ 1024,
368 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
369 /* lpSecurityAttrib */ NULL);
370 ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
372 hnp2 = CreateNamedPipe(PIPENAME, PIPE_ACCESS_INBOUND, PIPE_TYPE_BYTE | PIPE_WAIT,
373 /* nMaxInstances */ 1,
374 /* nOutBufSize */ 1024,
375 /* nInBufSize */ 1024,
376 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
377 /* lpSecurityAttrib */ NULL);
378 ok(hnp2 == INVALID_HANDLE_VALUE
379 && GetLastError() == ERROR_ACCESS_DENIED, "PIPE_ACCESS_* mismatch allowed\n");
381 ok(CloseHandle(hnp), "CloseHandle\n");
386 /** implementation of alarm() */
387 static DWORD CALLBACK alarmThreadMain(LPVOID arg)
389 DWORD timeout = (DWORD) arg;
390 trace("alarmThreadMain\n");
391 if (WaitForSingleObject( alarm_event, timeout ) == WAIT_TIMEOUT)
393 ok(FALSE, "alarm\n");
399 HANDLE hnp = INVALID_HANDLE_VALUE;
401 /** Trivial byte echo server - disconnects after each session */
402 static DWORD CALLBACK serverThreadMain1(LPVOID arg)
406 trace("serverThreadMain1 start\n");
407 /* Set up a simple echo server */
408 hnp = CreateNamedPipe(PIPENAME "serverThreadMain1", PIPE_ACCESS_DUPLEX,
409 PIPE_TYPE_BYTE | PIPE_WAIT,
410 /* nMaxInstances */ 1,
411 /* nOutBufSize */ 1024,
412 /* nInBufSize */ 1024,
413 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
414 /* lpSecurityAttrib */ NULL);
416 ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
417 for (i = 0; i < NB_SERVER_LOOPS; i++) {
423 /* Wait for client to connect */
424 trace("Server calling ConnectNamedPipe...\n");
425 ok(ConnectNamedPipe(hnp, NULL)
426 || GetLastError() == ERROR_PIPE_CONNECTED, "ConnectNamedPipe\n");
427 trace("ConnectNamedPipe returned.\n");
429 /* Echo bytes once */
430 memset(buf, 0, sizeof(buf));
432 trace("Server reading...\n");
433 success = ReadFile(hnp, buf, sizeof(buf), &readden, NULL);
434 trace("Server done reading.\n");
435 ok(success, "ReadFile\n");
436 ok(readden, "short read\n");
438 trace("Server writing...\n");
439 ok(WriteFile(hnp, buf, readden, &written, NULL), "WriteFile\n");
440 trace("Server done writing.\n");
441 ok(written == readden, "write file len\n");
443 /* finish this connection, wait for next one */
444 ok(FlushFileBuffers(hnp), "FlushFileBuffers\n");
445 trace("Server done flushing.\n");
446 ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe\n");
447 trace("Server done disconnecting.\n");
452 /** Trivial byte echo server - closes after each connection */
453 static DWORD CALLBACK serverThreadMain2(LPVOID arg)
458 trace("serverThreadMain2\n");
459 /* Set up a simple echo server */
460 hnp = CreateNamedPipe(PIPENAME "serverThreadMain2", PIPE_ACCESS_DUPLEX,
461 PIPE_TYPE_BYTE | PIPE_WAIT,
462 /* nMaxInstances */ 2,
463 /* nOutBufSize */ 1024,
464 /* nInBufSize */ 1024,
465 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
466 /* lpSecurityAttrib */ NULL);
467 ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
469 for (i = 0; i < NB_SERVER_LOOPS; i++) {
475 /* Wait for client to connect */
476 trace("Server calling ConnectNamedPipe...\n");
477 ok(ConnectNamedPipe(hnp, NULL)
478 || GetLastError() == ERROR_PIPE_CONNECTED, "ConnectNamedPipe\n");
479 trace("ConnectNamedPipe returned.\n");
481 /* Echo bytes once */
482 memset(buf, 0, sizeof(buf));
484 trace("Server reading...\n");
485 success = ReadFile(hnp, buf, sizeof(buf), &readden, NULL);
486 trace("Server done reading.\n");
487 ok(success, "ReadFile\n");
489 trace("Server writing...\n");
490 ok(WriteFile(hnp, buf, readden, &written, NULL), "WriteFile\n");
491 trace("Server done writing.\n");
492 ok(written == readden, "write file len\n");
494 /* finish this connection, wait for next one */
495 ok(FlushFileBuffers(hnp), "FlushFileBuffers\n");
496 ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe\n");
498 /* Set up next echo server */
500 CreateNamedPipe(PIPENAME "serverThreadMain2", PIPE_ACCESS_DUPLEX,
501 PIPE_TYPE_BYTE | PIPE_WAIT,
502 /* nMaxInstances */ 2,
503 /* nOutBufSize */ 1024,
504 /* nInBufSize */ 1024,
505 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
506 /* lpSecurityAttrib */ NULL);
508 ok(hnpNext != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
510 ok(CloseHandle(hnp), "CloseHandle\n");
516 /** Trivial byte echo server - uses overlapped named pipe calls */
517 static DWORD CALLBACK serverThreadMain3(LPVOID arg)
522 trace("serverThreadMain3\n");
523 /* Set up a simple echo server */
524 hnp = CreateNamedPipe(PIPENAME "serverThreadMain3", PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
525 PIPE_TYPE_BYTE | PIPE_WAIT,
526 /* nMaxInstances */ 1,
527 /* nOutBufSize */ 1024,
528 /* nInBufSize */ 1024,
529 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
530 /* lpSecurityAttrib */ NULL);
531 ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
533 hEvent = CreateEvent(NULL, /* security attribute */
534 TRUE, /* manual reset event */
535 FALSE, /* initial state */
537 ok(hEvent != NULL, "CreateEvent\n");
539 for (i = 0; i < NB_SERVER_LOOPS; i++) {
546 int letWFSOEwait = (i & 2);
547 int letGORwait = (i & 1);
550 memset(&oOverlap, 0, sizeof(oOverlap));
551 oOverlap.hEvent = hEvent;
553 /* Wait for client to connect */
554 trace("Server calling overlapped ConnectNamedPipe...\n");
555 success = ConnectNamedPipe(hnp, &oOverlap);
556 err = GetLastError();
557 ok(success || err == ERROR_IO_PENDING
558 || err == ERROR_PIPE_CONNECTED, "overlapped ConnectNamedPipe\n");
559 trace("overlapped ConnectNamedPipe returned.\n");
560 if (!success && (err == ERROR_IO_PENDING) && letWFSOEwait)
561 ok(WaitForSingleObjectEx(hEvent, INFINITE, TRUE) == 0, "wait ConnectNamedPipe\n");
562 success = GetOverlappedResult(hnp, &oOverlap, &dummy, letGORwait);
563 if (!letGORwait && !letWFSOEwait && !success) {
564 ok(GetLastError() == ERROR_IO_INCOMPLETE, "GetOverlappedResult\n");
565 success = GetOverlappedResult(hnp, &oOverlap, &dummy, TRUE);
567 ok(success, "GetOverlappedResult ConnectNamedPipe\n");
568 trace("overlapped ConnectNamedPipe operation complete.\n");
570 /* Echo bytes once */
571 memset(buf, 0, sizeof(buf));
573 trace("Server reading...\n");
574 success = ReadFile(hnp, buf, sizeof(buf), NULL, &oOverlap);
575 trace("Server ReadFile returned...\n");
576 err = GetLastError();
577 ok(success || err == ERROR_IO_PENDING, "overlapped ReadFile\n");
578 trace("overlapped ReadFile returned.\n");
579 if (!success && (err == ERROR_IO_PENDING) && letWFSOEwait)
580 ok(WaitForSingleObjectEx(hEvent, INFINITE, TRUE) == 0, "wait ReadFile\n");
581 success = GetOverlappedResult(hnp, &oOverlap, &readden, letGORwait);
582 if (!letGORwait && !letWFSOEwait && !success) {
583 ok(GetLastError() == ERROR_IO_INCOMPLETE, "GetOverlappedResult\n");
584 success = GetOverlappedResult(hnp, &oOverlap, &readden, TRUE);
586 trace("Server done reading.\n");
587 ok(success, "overlapped ReadFile\n");
589 trace("Server writing...\n");
590 success = WriteFile(hnp, buf, readden, NULL, &oOverlap);
591 trace("Server WriteFile returned...\n");
592 err = GetLastError();
593 ok(success || err == ERROR_IO_PENDING, "overlapped WriteFile\n");
594 trace("overlapped WriteFile returned.\n");
595 if (!success && (err == ERROR_IO_PENDING) && letWFSOEwait)
596 ok(WaitForSingleObjectEx(hEvent, INFINITE, TRUE) == 0, "wait WriteFile\n");
597 success = GetOverlappedResult(hnp, &oOverlap, &written, letGORwait);
598 if (!letGORwait && !letWFSOEwait && !success) {
599 ok(GetLastError() == ERROR_IO_INCOMPLETE, "GetOverlappedResult\n");
600 success = GetOverlappedResult(hnp, &oOverlap, &written, TRUE);
602 trace("Server done writing.\n");
603 ok(success, "overlapped WriteFile\n");
604 ok(written == readden, "write file len\n");
606 /* finish this connection, wait for next one */
607 ok(FlushFileBuffers(hnp), "FlushFileBuffers\n");
608 ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe\n");
613 static void exercizeServer(const char *pipename, HANDLE serverThread)
617 trace("exercizeServer starting\n");
618 for (i = 0; i < NB_SERVER_LOOPS; i++) {
619 HANDLE hFile=INVALID_HANDLE_VALUE;
620 static const char obuf[] = "Bit Bucket";
626 for (loop = 0; loop < 3; loop++) {
628 trace("Client connecting...\n");
629 /* Connect to the server */
630 hFile = CreateFileA(pipename, GENERIC_READ | GENERIC_WRITE, 0,
631 NULL, OPEN_EXISTING, 0, 0);
632 if (hFile != INVALID_HANDLE_VALUE)
634 err = GetLastError();
636 ok(err == ERROR_PIPE_BUSY || err == ERROR_FILE_NOT_FOUND, "connecting to pipe\n");
638 ok(err == ERROR_PIPE_BUSY, "connecting to pipe\n");
639 trace("connect failed, retrying\n");
642 ok(hFile != INVALID_HANDLE_VALUE, "client opening named pipe\n");
644 /* Make sure it can echo */
645 memset(ibuf, 0, sizeof(ibuf));
646 trace("Client writing...\n");
647 ok(WriteFile(hFile, obuf, sizeof(obuf), &written, NULL), "WriteFile to client end of pipe\n");
648 ok(written == sizeof(obuf), "write file len\n");
649 trace("Client reading...\n");
650 ok(ReadFile(hFile, ibuf, sizeof(obuf), &readden, NULL), "ReadFile from client end of pipe\n");
651 ok(readden == sizeof(obuf), "read file len\n");
652 ok(memcmp(obuf, ibuf, written) == 0, "content check\n");
654 trace("Client closing...\n");
655 ok(CloseHandle(hFile), "CloseHandle\n");
658 ok(WaitForSingleObject(serverThread,INFINITE) == WAIT_OBJECT_0, "WaitForSingleObject\n");
660 trace("exercizeServer returning\n");
663 static void test_NamedPipe_2(void)
666 DWORD serverThreadId;
670 trace("test_NamedPipe_2 starting\n");
671 /* Set up a ten second timeout */
672 alarm_event = CreateEvent( NULL, TRUE, FALSE, NULL );
673 alarmThread = CreateThread(NULL, 0, alarmThreadMain, (void *) 10000, 0, &alarmThreadId);
675 /* The servers we're about to exercize do try to clean up carefully,
676 * but to reduce the change of a test failure due to a pipe handle
677 * leak in the test code, we'll use a different pipe name for each server.
681 serverThread = CreateThread(NULL, 0, serverThreadMain1, (void *)8, 0, &serverThreadId);
682 ok(serverThread != INVALID_HANDLE_VALUE, "CreateThread\n");
683 exercizeServer(PIPENAME "serverThreadMain1", serverThread);
686 serverThread = CreateThread(NULL, 0, serverThreadMain2, 0, 0, &serverThreadId);
687 ok(serverThread != INVALID_HANDLE_VALUE, "CreateThread\n");
688 exercizeServer(PIPENAME "serverThreadMain2", serverThread);
690 if( 0 ) /* overlapped pipe server doesn't work yet - it randomly fails */
693 serverThread = CreateThread(NULL, 0, serverThreadMain3, 0, 0, &serverThreadId);
694 ok(serverThread != INVALID_HANDLE_VALUE, "CreateThread\n");
695 exercizeServer(PIPENAME "serverThreadMain3", serverThread);
698 ok(SetEvent( alarm_event ), "SetEvent\n");
699 CloseHandle( alarm_event );
700 trace("test_NamedPipe_2 returning\n");
703 static void test_DisconnectNamedPipe(void)
707 static const char obuf[] = "Bit Bucket";
712 hnp = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
713 /* nMaxInstances */ 1,
714 /* nOutBufSize */ 1024,
715 /* nInBufSize */ 1024,
716 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
717 /* lpSecurityAttrib */ NULL);
718 ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
720 ok(WriteFile(hnp, obuf, sizeof(obuf), &written, NULL) == 0
721 && GetLastError() == ERROR_PIPE_LISTENING, "WriteFile to not-yet-connected pipe\n");
722 ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL) == 0
723 && GetLastError() == ERROR_PIPE_LISTENING, "ReadFile from not-yet-connected pipe\n");
725 hFile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
726 ok(hFile != INVALID_HANDLE_VALUE, "CreateFile failed\n");
728 /* don't try to do i/o if one side couldn't be opened, as it hangs */
729 if (hFile != INVALID_HANDLE_VALUE) {
731 /* see what happens if server calls DisconnectNamedPipe
732 * when there are bytes in the pipe
735 ok(WriteFile(hFile, obuf, sizeof(obuf), &written, NULL), "WriteFile\n");
736 ok(written == sizeof(obuf), "write file len\n");
737 ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe while messages waiting\n");
738 ok(WriteFile(hFile, obuf, sizeof(obuf), &written, NULL) == 0
739 && GetLastError() == ERROR_PIPE_NOT_CONNECTED, "WriteFile to disconnected pipe\n");
740 ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL) == 0
741 && GetLastError() == ERROR_PIPE_NOT_CONNECTED,
742 "ReadFile from disconnected pipe with bytes waiting\n");
743 ok(CloseHandle(hFile), "CloseHandle\n");
746 ok(CloseHandle(hnp), "CloseHandle\n");
752 trace("test 1 of 4:\n");
753 test_DisconnectNamedPipe();
754 trace("test 2 of 4:\n");
755 test_CreateNamedPipe_instances_must_match();
756 trace("test 3 of 4:\n");
758 trace("test 4 of 4:\n");
759 test_CreateNamedPipe(PIPE_TYPE_BYTE);
760 trace("all tests done\n");
761 test_CreateNamedPipe(PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE);
762 trace("all tests done\n");