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_pipe.c"
53 #define NB_SERVER_LOOPS 8
55 static HANDLE alarm_event;
57 static void test_CreateNamedPipe(int 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(PeekNamedPipe(hnp, (LPVOID)1, 0, NULL, &readden, NULL), "Peek\n");
134 ok(readden == sizeof(obuf2), "peek 2 got %ld bytes\n", readden);
135 ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
136 ok(readden == sizeof(obuf2), "read 2 got %ld bytes\n", readden);
137 ok(memcmp(obuf2, ibuf, written) == 0, "content 2 check\n");
139 /* Test reading of multiple writes */
140 memset(ibuf, 0, sizeof(ibuf));
141 ok(WriteFile(hnp, obuf, sizeof(obuf), &written, NULL), "WriteFile3a\n");
142 ok(written == sizeof(obuf), "write file len 3a\n");
143 ok(WriteFile(hnp, obuf2, sizeof(obuf2), &written, NULL), " WriteFile3b\n");
144 ok(written == sizeof(obuf2), "write file len 3b\n");
145 ok(PeekNamedPipe(hFile, ibuf, sizeof(ibuf), &readden, &avail, NULL), "Peek3\n");
146 if (pipemode == PIPE_TYPE_BYTE) {
148 /* should return all 23 bytes */
149 ok(readden == sizeof(obuf) + sizeof(obuf2), "peek3 got %ld bytes\n", readden);
153 ok(readden == sizeof(obuf), "peek3 got %ld bytes\n", readden);
154 if (avail != sizeof(obuf)) /* older Linux kernels only return the first write here */
155 ok(avail == sizeof(obuf) + sizeof(obuf2), "peek3 got %ld bytes available\n", avail);
157 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "pipe content 3a check\n");
158 if (pipemode == PIPE_TYPE_BYTE) {
160 pbuf += sizeof(obuf);
161 ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "pipe content 3b check\n");
164 ok(ReadFile(hFile, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
165 ok(readden == sizeof(obuf) + sizeof(obuf2), "read 3 got %ld bytes\n", readden);
167 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 3a check\n");
168 pbuf += sizeof(obuf);
169 ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "content 3b check\n");
171 /* Multiple writes in the reverse direction */
172 memset(ibuf, 0, sizeof(ibuf));
173 ok(WriteFile(hFile, obuf, sizeof(obuf), &written, NULL), "WriteFile4a\n");
174 ok(written == sizeof(obuf), "write file len 4a\n");
175 ok(WriteFile(hFile, obuf2, sizeof(obuf2), &written, NULL), " WriteFile4b\n");
176 ok(written == sizeof(obuf2), "write file len 4b\n");
177 ok(PeekNamedPipe(hnp, ibuf, sizeof(ibuf), &readden, &avail, NULL), "Peek4\n");
178 if (pipemode == PIPE_TYPE_BYTE) {
180 /* should return all 23 bytes */
181 ok(readden == sizeof(obuf) + sizeof(obuf2), "peek4 got %ld bytes\n", readden);
185 ok(readden == sizeof(obuf), "peek4 got %ld bytes\n", readden);
186 if (avail != sizeof(obuf)) /* older Linux kernels only return the first write here */
187 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);
231 if (avail != sizeof(obuf)) /* older Linux kernels only return the first write here */
232 ok(avail == sizeof(obuf) + sizeof(obuf2), "peek5 got %ld bytes available\n", avail);
234 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 5a check\n");
235 ok(ReadFile(hFile, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
237 ok(readden == sizeof(obuf), "read 5 got %ld bytes\n", readden);
240 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 5a check\n");
242 /* Multiple writes in the reverse direction */
243 /* the write of obuf2 from write4 should still be in the buffer */
244 ok(PeekNamedPipe(hnp, ibuf, sizeof(ibuf), &readden, &avail, NULL), "Peek6a\n");
246 ok(readden == sizeof(obuf2), "peek6a got %ld bytes\n", readden);
247 ok(avail == sizeof(obuf2), "peek6a got %ld bytes available\n", avail);
250 ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
251 ok(readden == sizeof(obuf2), "read 6a got %ld bytes\n", readden);
253 ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "content 6a check\n");
255 memset(ibuf, 0, sizeof(ibuf));
256 ok(WriteFile(hFile, obuf, sizeof(obuf), &written, NULL), "WriteFile6a\n");
257 ok(written == sizeof(obuf), "write file len 6a\n");
258 ok(WriteFile(hFile, obuf2, sizeof(obuf2), &written, NULL), " WriteFile6b\n");
259 ok(written == sizeof(obuf2), "write file len 6b\n");
260 ok(PeekNamedPipe(hnp, ibuf, sizeof(ibuf), &readden, &avail, NULL), "Peek6\n");
261 ok(readden == sizeof(obuf), "peek6 got %ld 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 %ld bytes available\n", avail);
265 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 6a check\n");
266 ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
268 ok(readden == sizeof(obuf), "read 6b got %ld bytes\n", readden);
271 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 6a check\n");
274 /* Picky conformance tests */
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
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");
287 ok(CloseHandle(hFile), "CloseHandle\n");
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");
296 ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe\n");
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");
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.
312 ok(CloseHandle(hnp), "CloseHandle\n");
314 trace("test_CreateNamedPipe returning\n");
317 void test_CreateNamedPipe_instances_must_match(void)
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");
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");
338 ok(CloseHandle(hnp), "CloseHandle\n");
339 ok(CloseHandle(hnp2), "CloseHandle\n");
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");
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");
359 ok(CloseHandle(hnp), "CloseHandle\n");
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");
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");
379 ok(CloseHandle(hnp), "CloseHandle\n");
384 /** implementation of alarm() */
385 static DWORD CALLBACK alarmThreadMain(LPVOID arg)
387 DWORD timeout = (DWORD) arg;
388 trace("alarmThreadMain\n");
389 if (WaitForSingleObject( alarm_event, timeout ) == WAIT_TIMEOUT)
391 ok(FALSE, "alarm\n");
397 HANDLE hnp = INVALID_HANDLE_VALUE;
399 /** Trivial byte echo server - disconnects after each session */
400 static DWORD CALLBACK serverThreadMain1(LPVOID arg)
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);
414 ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
415 for (i = 0; i < NB_SERVER_LOOPS; i++) {
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");
427 /* Echo bytes once */
428 memset(buf, 0, sizeof(buf));
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");
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");
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");
450 /** Trivial byte echo server - closes after each connection */
451 static DWORD CALLBACK serverThreadMain2(LPVOID arg)
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");
467 for (i = 0; i < NB_SERVER_LOOPS; i++) {
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");
479 /* Echo bytes once */
480 memset(buf, 0, sizeof(buf));
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");
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");
492 /* finish this connection, wait for next one */
493 ok(FlushFileBuffers(hnp), "FlushFileBuffers\n");
494 ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe\n");
496 /* Set up next echo server */
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);
506 ok(hnpNext != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
508 ok(CloseHandle(hnp), "CloseHandle\n");
514 /** Trivial byte echo server - uses overlapped named pipe calls */
515 static DWORD CALLBACK serverThreadMain3(LPVOID arg)
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");
531 hEvent = CreateEvent(NULL, /* security attribute */
532 TRUE, /* manual reset event */
533 FALSE, /* initial state */
535 ok(hEvent != NULL, "CreateEvent\n");
537 for (i = 0; i < NB_SERVER_LOOPS; i++) {
544 int letWFSOEwait = (i & 2);
545 int letGORwait = (i & 1);
548 memset(&oOverlap, 0, sizeof(oOverlap));
549 oOverlap.hEvent = hEvent;
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);
565 ok(success, "GetOverlappedResult ConnectNamedPipe\n");
566 trace("overlapped ConnectNamedPipe operation complete.\n");
568 /* Echo bytes once */
569 memset(buf, 0, sizeof(buf));
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);
584 trace("Server done reading.\n");
585 ok(success, "overlapped ReadFile\n");
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);
600 trace("Server done writing.\n");
601 ok(success, "overlapped WriteFile\n");
602 ok(written == readden, "write file len\n");
604 /* finish this connection, wait for next one */
605 ok(FlushFileBuffers(hnp), "FlushFileBuffers\n");
606 ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe\n");
611 static void exercizeServer(const char *pipename, HANDLE serverThread)
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";
624 for (loop = 0; loop < 3; loop++) {
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)
632 err = GetLastError();
634 ok(err == ERROR_PIPE_BUSY || err == ERROR_FILE_NOT_FOUND, "connecting to pipe\n");
636 ok(err == ERROR_PIPE_BUSY, "connecting to pipe\n");
637 trace("connect failed, retrying\n");
640 ok(hFile != INVALID_HANDLE_VALUE, "client opening named pipe\n");
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");
652 trace("Client closing...\n");
653 ok(CloseHandle(hFile), "CloseHandle\n");
656 ok(WaitForSingleObject(serverThread,INFINITE) == WAIT_OBJECT_0, "WaitForSingleObject\n");
658 trace("exercizeServer returning\n");
661 static void test_NamedPipe_2(void)
664 DWORD serverThreadId;
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);
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.
679 serverThread = CreateThread(NULL, 0, serverThreadMain1, (void *)8, 0, &serverThreadId);
680 ok(serverThread != INVALID_HANDLE_VALUE, "CreateThread\n");
681 exercizeServer(PIPENAME "serverThreadMain1", serverThread);
684 serverThread = CreateThread(NULL, 0, serverThreadMain2, 0, 0, &serverThreadId);
685 ok(serverThread != INVALID_HANDLE_VALUE, "CreateThread\n");
686 exercizeServer(PIPENAME "serverThreadMain2", serverThread);
688 if( 0 ) /* overlapped pipe server doesn't work yet - it randomly fails */
691 serverThread = CreateThread(NULL, 0, serverThreadMain3, 0, 0, &serverThreadId);
692 ok(serverThread != INVALID_HANDLE_VALUE, "CreateThread\n");
693 exercizeServer(PIPENAME "serverThreadMain3", serverThread);
696 ok(SetEvent( alarm_event ), "SetEvent\n");
697 CloseHandle( alarm_event );
698 trace("test_NamedPipe_2 returning\n");
701 static int test_DisconnectNamedPipe(void)
705 static const char obuf[] = "Bit Bucket";
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");
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");
726 hFile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
727 ok(hFile != INVALID_HANDLE_VALUE, "CreateFile failed\n");
729 /* don't try to do i/o if one side couldn't be opened, as it hangs */
730 if (hFile != INVALID_HANDLE_VALUE) {
732 /* see what happens if server calls DisconnectNamedPipe
733 * when there are bytes in the pipe
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");
747 ok(CloseHandle(hnp), "CloseHandle\n");
754 trace("test 1 of 4:\n");
755 if (test_DisconnectNamedPipe())
757 trace("test 2 of 4:\n");
758 test_CreateNamedPipe_instances_must_match();
759 trace("test 3 of 4:\n");
761 trace("test 4 of 4:\n");
762 test_CreateNamedPipe(PIPE_TYPE_BYTE);
763 trace("all tests done\n");
764 test_CreateNamedPipe(PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE);
765 trace("all tests done\n");