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"
34 #define PIPENAME "\\\\.\\PiPe\\tests_pipe.c"
36 #define NB_SERVER_LOOPS 8
38 static HANDLE alarm_event;
40 static void test_CreateNamedPipe(int pipemode)
44 static const char obuf[] = "Bit Bucket";
45 static const char obuf2[] = "More bits";
52 if (pipemode == PIPE_TYPE_BYTE)
53 trace("test_CreateNamedPipe starting in byte mode\n");
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);
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");
70 ok(hnp == INVALID_HANDLE_VALUE && GetLastError() == ERROR_INVALID_NAME,
71 "CreateNamedPipe should fail if name doesn't start with \\\\.\\pipe\n");
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");
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");
84 /* Functional checks */
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");
94 hFile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
95 ok(hFile != INVALID_HANDLE_VALUE, "CreateFile failed\n");
97 /* don't try to do i/o if one side couldn't be opened, as it hangs */
98 if (hFile != INVALID_HANDLE_VALUE) {
101 /* Make sure we can read and write a few bytes in both directions */
102 memset(ibuf, 0, sizeof(ibuf));
103 ok(WriteFile(hnp, obuf, sizeof(obuf), &written, NULL), "WriteFile\n");
104 ok(written == sizeof(obuf), "write file len 1\n");
105 ok(PeekNamedPipe(hFile, NULL, 0, NULL, &readden, NULL), "Peek\n");
106 ok(readden == sizeof(obuf), "peek 1 got %ld bytes\n", readden);
107 ok(ReadFile(hFile, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
108 ok(readden == sizeof(obuf), "read 1 got %ld bytes\n", readden);
109 ok(memcmp(obuf, ibuf, written) == 0, "content 1 check\n");
111 memset(ibuf, 0, sizeof(ibuf));
112 ok(WriteFile(hFile, obuf2, sizeof(obuf2), &written, NULL), "WriteFile\n");
113 ok(written == sizeof(obuf2), "write file len 2\n");
114 ok(PeekNamedPipe(hnp, NULL, 0, NULL, &readden, NULL), "Peek\n");
115 ok(readden == sizeof(obuf2), "peek 2 got %ld bytes\n", readden);
116 ok(PeekNamedPipe(hnp, (LPVOID)1, 0, NULL, &readden, NULL), "Peek\n");
117 ok(readden == sizeof(obuf2), "peek 2 got %ld bytes\n", readden);
118 ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
119 ok(readden == sizeof(obuf2), "read 2 got %ld bytes\n", readden);
120 ok(memcmp(obuf2, ibuf, written) == 0, "content 2 check\n");
122 /* Test reading of multiple writes */
123 memset(ibuf, 0, sizeof(ibuf));
124 ok(WriteFile(hnp, obuf, sizeof(obuf), &written, NULL), "WriteFile3a\n");
125 ok(written == sizeof(obuf), "write file len 3a\n");
126 ok(WriteFile(hnp, obuf2, sizeof(obuf2), &written, NULL), " WriteFile3b\n");
127 ok(written == sizeof(obuf2), "write file len 3b\n");
128 ok(PeekNamedPipe(hFile, ibuf, sizeof(ibuf), &readden, &avail, NULL), "Peek3\n");
129 if (pipemode == PIPE_TYPE_BYTE) {
131 /* should return all 23 bytes */
132 ok(readden == sizeof(obuf) + sizeof(obuf2), "peek3 got %ld bytes\n", readden);
136 ok(readden == sizeof(obuf), "peek3 got %ld bytes\n", readden);
137 if (avail != sizeof(obuf)) /* older Linux kernels only return the first write here */
138 ok(avail == sizeof(obuf) + sizeof(obuf2), "peek3 got %ld bytes available\n", avail);
140 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "pipe content 3a check\n");
141 if (pipemode == PIPE_TYPE_BYTE) {
143 pbuf += sizeof(obuf);
144 ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "pipe content 3b check\n");
147 ok(ReadFile(hFile, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
148 ok(readden == sizeof(obuf) + sizeof(obuf2), "read 3 got %ld bytes\n", readden);
150 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 3a check\n");
151 pbuf += sizeof(obuf);
152 ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "content 3b check\n");
154 /* Multiple writes in the reverse direction */
155 memset(ibuf, 0, sizeof(ibuf));
156 ok(WriteFile(hFile, obuf, sizeof(obuf), &written, NULL), "WriteFile4a\n");
157 ok(written == sizeof(obuf), "write file len 4a\n");
158 ok(WriteFile(hFile, obuf2, sizeof(obuf2), &written, NULL), " WriteFile4b\n");
159 ok(written == sizeof(obuf2), "write file len 4b\n");
160 ok(PeekNamedPipe(hnp, ibuf, sizeof(ibuf), &readden, &avail, NULL), "Peek4\n");
161 if (pipemode == PIPE_TYPE_BYTE) {
163 /* should return all 23 bytes */
164 ok(readden == sizeof(obuf) + sizeof(obuf2), "peek4 got %ld bytes\n", readden);
168 ok(readden == sizeof(obuf), "peek4 got %ld bytes\n", readden);
169 if (avail != sizeof(obuf)) /* older Linux kernels only return the first write here */
170 ok(avail == sizeof(obuf) + sizeof(obuf2), "peek4 got %ld bytes available\n", avail);
172 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "pipe content 4a check\n");
173 if (pipemode == PIPE_TYPE_BYTE) {
175 pbuf += sizeof(obuf);
176 ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "pipe content 4b check\n");
179 ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
180 if (pipemode == PIPE_TYPE_BYTE) {
181 ok(readden == sizeof(obuf) + sizeof(obuf2), "read 4 got %ld bytes\n", readden);
185 ok(readden == sizeof(obuf), "read 4 got %ld bytes\n", readden);
189 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 4a check\n");
190 if (pipemode == PIPE_TYPE_BYTE) {
191 pbuf += sizeof(obuf);
192 ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "content 4b check\n");
195 /* Test reading of multiple writes after a mode change
196 (CreateFile always creates a byte mode pipe) */
197 lpmode = PIPE_READMODE_MESSAGE;
198 if (pipemode == PIPE_TYPE_BYTE) {
199 /* trying to change the client end of a byte pipe to message mode should fail */
200 ok(!SetNamedPipeHandleState(hFile, &lpmode, NULL, NULL), "Change mode\n");
204 ok(SetNamedPipeHandleState(hFile, &lpmode, NULL, NULL), "Change mode\n");
207 memset(ibuf, 0, sizeof(ibuf));
208 ok(WriteFile(hnp, obuf, sizeof(obuf), &written, NULL), "WriteFile5a\n");
209 ok(written == sizeof(obuf), "write file len 3a\n");
210 ok(WriteFile(hnp, obuf2, sizeof(obuf2), &written, NULL), " WriteFile5b\n");
211 ok(written == sizeof(obuf2), "write file len 3b\n");
212 ok(PeekNamedPipe(hFile, ibuf, sizeof(ibuf), &readden, &avail, NULL), "Peek5\n");
213 ok(readden == sizeof(obuf), "peek5 got %ld bytes\n", readden);
214 if (avail != sizeof(obuf)) /* older Linux kernels only return the first write here */
215 ok(avail == sizeof(obuf) + sizeof(obuf2), "peek5 got %ld bytes available\n", avail);
217 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 5a check\n");
218 ok(ReadFile(hFile, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
220 ok(readden == sizeof(obuf), "read 5 got %ld bytes\n", readden);
223 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 5a check\n");
225 /* Multiple writes in the reverse direction */
226 /* the write of obuf2 from write4 should still be in the buffer */
227 ok(PeekNamedPipe(hnp, ibuf, sizeof(ibuf), &readden, &avail, NULL), "Peek6a\n");
229 ok(readden == sizeof(obuf2), "peek6a got %ld bytes\n", readden);
230 ok(avail == sizeof(obuf2), "peek6a got %ld bytes available\n", avail);
233 ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
234 ok(readden == sizeof(obuf2), "read 6a got %ld bytes\n", readden);
236 ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "content 6a check\n");
238 memset(ibuf, 0, sizeof(ibuf));
239 ok(WriteFile(hFile, obuf, sizeof(obuf), &written, NULL), "WriteFile6a\n");
240 ok(written == sizeof(obuf), "write file len 6a\n");
241 ok(WriteFile(hFile, obuf2, sizeof(obuf2), &written, NULL), " WriteFile6b\n");
242 ok(written == sizeof(obuf2), "write file len 6b\n");
243 ok(PeekNamedPipe(hnp, ibuf, sizeof(ibuf), &readden, &avail, NULL), "Peek6\n");
244 ok(readden == sizeof(obuf), "peek6 got %ld bytes\n", readden);
245 if (avail != sizeof(obuf)) /* older Linux kernels only return the first write here */
246 ok(avail == sizeof(obuf) + sizeof(obuf2), "peek6b got %ld bytes available\n", avail);
248 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 6a check\n");
249 ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
251 ok(readden == sizeof(obuf), "read 6b got %ld bytes\n", readden);
254 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 6a check\n");
257 /* Picky conformance tests */
259 /* Verify that you can't connect to pipe again
260 * until server calls DisconnectNamedPipe+ConnectNamedPipe
261 * or creates a new pipe
262 * case 1: other client not yet closed
264 hFile2 = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
265 ok(hFile2 == INVALID_HANDLE_VALUE,
266 "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail\n");
267 ok(GetLastError() == ERROR_PIPE_BUSY,
268 "connecting to named pipe before other client closes should fail with ERROR_PIPE_BUSY\n");
270 ok(CloseHandle(hFile), "CloseHandle\n");
272 /* case 2: other client already closed */
273 hFile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
274 ok(hFile == INVALID_HANDLE_VALUE,
275 "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail\n");
276 ok(GetLastError() == ERROR_PIPE_BUSY,
277 "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail with ERROR_PIPE_BUSY\n");
279 ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe\n");
281 /* case 3: server has called DisconnectNamedPipe but not ConnectNamed Pipe */
282 hFile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
283 ok(hFile == INVALID_HANDLE_VALUE,
284 "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail\n");
285 ok(GetLastError() == ERROR_PIPE_BUSY,
286 "connecting to named pipe after other client closes but before ConnectNamedPipe should fail with ERROR_PIPE_BUSY\n");
288 /* to be complete, we'd call ConnectNamedPipe here and loop,
289 * but by default that's blocking, so we'd either have
290 * to turn on the uncommon nonblocking mode, or
291 * use another thread.
295 ok(CloseHandle(hnp), "CloseHandle\n");
297 trace("test_CreateNamedPipe returning\n");
300 static void test_CreateNamedPipe_instances_must_match(void)
304 /* Check no mismatch */
305 hnp = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
306 /* nMaxInstances */ 2,
307 /* nOutBufSize */ 1024,
308 /* nInBufSize */ 1024,
309 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
310 /* lpSecurityAttrib */ NULL);
311 ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
313 hnp2 = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
314 /* nMaxInstances */ 2,
315 /* nOutBufSize */ 1024,
316 /* nInBufSize */ 1024,
317 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
318 /* lpSecurityAttrib */ NULL);
319 ok(hnp2 != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
321 ok(CloseHandle(hnp), "CloseHandle\n");
322 ok(CloseHandle(hnp2), "CloseHandle\n");
324 /* Check nMaxInstances */
325 hnp = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
326 /* nMaxInstances */ 1,
327 /* nOutBufSize */ 1024,
328 /* nInBufSize */ 1024,
329 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
330 /* lpSecurityAttrib */ NULL);
331 ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
333 hnp2 = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
334 /* nMaxInstances */ 1,
335 /* nOutBufSize */ 1024,
336 /* nInBufSize */ 1024,
337 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
338 /* lpSecurityAttrib */ NULL);
339 ok(hnp2 == INVALID_HANDLE_VALUE
340 && GetLastError() == ERROR_PIPE_BUSY, "nMaxInstances not obeyed\n");
342 ok(CloseHandle(hnp), "CloseHandle\n");
344 /* Check PIPE_ACCESS_* */
345 hnp = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
346 /* nMaxInstances */ 2,
347 /* nOutBufSize */ 1024,
348 /* nInBufSize */ 1024,
349 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
350 /* lpSecurityAttrib */ NULL);
351 ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
353 hnp2 = CreateNamedPipe(PIPENAME, PIPE_ACCESS_INBOUND, PIPE_TYPE_BYTE | PIPE_WAIT,
354 /* nMaxInstances */ 1,
355 /* nOutBufSize */ 1024,
356 /* nInBufSize */ 1024,
357 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
358 /* lpSecurityAttrib */ NULL);
359 ok(hnp2 == INVALID_HANDLE_VALUE
360 && GetLastError() == ERROR_ACCESS_DENIED, "PIPE_ACCESS_* mismatch allowed\n");
362 ok(CloseHandle(hnp), "CloseHandle\n");
367 /** implementation of alarm() */
368 static DWORD CALLBACK alarmThreadMain(LPVOID arg)
370 DWORD timeout = (DWORD) arg;
371 trace("alarmThreadMain\n");
372 if (WaitForSingleObject( alarm_event, timeout ) == WAIT_TIMEOUT)
374 ok(FALSE, "alarm\n");
380 HANDLE hnp = INVALID_HANDLE_VALUE;
382 /** Trivial byte echo server - disconnects after each session */
383 static DWORD CALLBACK serverThreadMain1(LPVOID arg)
387 trace("serverThreadMain1 start\n");
388 /* Set up a simple echo server */
389 hnp = CreateNamedPipe(PIPENAME "serverThreadMain1", PIPE_ACCESS_DUPLEX,
390 PIPE_TYPE_BYTE | PIPE_WAIT,
391 /* nMaxInstances */ 1,
392 /* nOutBufSize */ 1024,
393 /* nInBufSize */ 1024,
394 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
395 /* lpSecurityAttrib */ NULL);
397 ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
398 for (i = 0; i < NB_SERVER_LOOPS; i++) {
404 /* Wait for client to connect */
405 trace("Server calling ConnectNamedPipe...\n");
406 ok(ConnectNamedPipe(hnp, NULL)
407 || GetLastError() == ERROR_PIPE_CONNECTED, "ConnectNamedPipe\n");
408 trace("ConnectNamedPipe returned.\n");
410 /* Echo bytes once */
411 memset(buf, 0, sizeof(buf));
413 trace("Server reading...\n");
414 success = ReadFile(hnp, buf, sizeof(buf), &readden, NULL);
415 trace("Server done reading.\n");
416 ok(success, "ReadFile\n");
417 ok(readden, "short read\n");
419 trace("Server writing...\n");
420 ok(WriteFile(hnp, buf, readden, &written, NULL), "WriteFile\n");
421 trace("Server done writing.\n");
422 ok(written == readden, "write file len\n");
424 /* finish this connection, wait for next one */
425 ok(FlushFileBuffers(hnp), "FlushFileBuffers\n");
426 trace("Server done flushing.\n");
427 ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe\n");
428 trace("Server done disconnecting.\n");
433 /** Trivial byte echo server - closes after each connection */
434 static DWORD CALLBACK serverThreadMain2(LPVOID arg)
439 trace("serverThreadMain2\n");
440 /* Set up a simple echo server */
441 hnp = CreateNamedPipe(PIPENAME "serverThreadMain2", PIPE_ACCESS_DUPLEX,
442 PIPE_TYPE_BYTE | PIPE_WAIT,
443 /* nMaxInstances */ 2,
444 /* nOutBufSize */ 1024,
445 /* nInBufSize */ 1024,
446 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
447 /* lpSecurityAttrib */ NULL);
448 ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
450 for (i = 0; i < NB_SERVER_LOOPS; i++) {
456 /* Wait for client to connect */
457 trace("Server calling ConnectNamedPipe...\n");
458 ok(ConnectNamedPipe(hnp, NULL)
459 || GetLastError() == ERROR_PIPE_CONNECTED, "ConnectNamedPipe\n");
460 trace("ConnectNamedPipe returned.\n");
462 /* Echo bytes once */
463 memset(buf, 0, sizeof(buf));
465 trace("Server reading...\n");
466 success = ReadFile(hnp, buf, sizeof(buf), &readden, NULL);
467 trace("Server done reading.\n");
468 ok(success, "ReadFile\n");
470 trace("Server writing...\n");
471 ok(WriteFile(hnp, buf, readden, &written, NULL), "WriteFile\n");
472 trace("Server done writing.\n");
473 ok(written == readden, "write file len\n");
475 /* finish this connection, wait for next one */
476 ok(FlushFileBuffers(hnp), "FlushFileBuffers\n");
477 ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe\n");
479 /* Set up next echo server */
481 CreateNamedPipe(PIPENAME "serverThreadMain2", PIPE_ACCESS_DUPLEX,
482 PIPE_TYPE_BYTE | PIPE_WAIT,
483 /* nMaxInstances */ 2,
484 /* nOutBufSize */ 1024,
485 /* nInBufSize */ 1024,
486 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
487 /* lpSecurityAttrib */ NULL);
489 ok(hnpNext != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
491 ok(CloseHandle(hnp), "CloseHandle\n");
497 /** Trivial byte echo server - uses overlapped named pipe calls */
498 static DWORD CALLBACK serverThreadMain3(LPVOID arg)
503 trace("serverThreadMain3\n");
504 /* Set up a simple echo server */
505 hnp = CreateNamedPipe(PIPENAME "serverThreadMain3", PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
506 PIPE_TYPE_BYTE | PIPE_WAIT,
507 /* nMaxInstances */ 1,
508 /* nOutBufSize */ 1024,
509 /* nInBufSize */ 1024,
510 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
511 /* lpSecurityAttrib */ NULL);
512 ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
514 hEvent = CreateEvent(NULL, /* security attribute */
515 TRUE, /* manual reset event */
516 FALSE, /* initial state */
518 ok(hEvent != NULL, "CreateEvent\n");
520 for (i = 0; i < NB_SERVER_LOOPS; i++) {
527 int letWFSOEwait = (i & 2);
528 int letGORwait = (i & 1);
531 memset(&oOverlap, 0, sizeof(oOverlap));
532 oOverlap.hEvent = hEvent;
534 /* Wait for client to connect */
535 trace("Server calling overlapped ConnectNamedPipe...\n");
536 success = ConnectNamedPipe(hnp, &oOverlap);
537 err = GetLastError();
538 ok(success || err == ERROR_IO_PENDING
539 || err == ERROR_PIPE_CONNECTED, "overlapped ConnectNamedPipe\n");
540 trace("overlapped ConnectNamedPipe returned.\n");
541 if (!success && (err == ERROR_IO_PENDING) && letWFSOEwait)
542 ok(WaitForSingleObjectEx(hEvent, INFINITE, TRUE) == 0, "wait ConnectNamedPipe\n");
543 success = GetOverlappedResult(hnp, &oOverlap, &dummy, letGORwait);
544 if (!letGORwait && !letWFSOEwait && !success) {
545 ok(GetLastError() == ERROR_IO_INCOMPLETE, "GetOverlappedResult\n");
546 success = GetOverlappedResult(hnp, &oOverlap, &dummy, TRUE);
548 ok(success, "GetOverlappedResult ConnectNamedPipe\n");
549 trace("overlapped ConnectNamedPipe operation complete.\n");
551 /* Echo bytes once */
552 memset(buf, 0, sizeof(buf));
554 trace("Server reading...\n");
555 success = ReadFile(hnp, buf, sizeof(buf), NULL, &oOverlap);
556 trace("Server ReadFile returned...\n");
557 err = GetLastError();
558 ok(success || err == ERROR_IO_PENDING, "overlapped ReadFile\n");
559 trace("overlapped ReadFile returned.\n");
560 if (!success && (err == ERROR_IO_PENDING) && letWFSOEwait)
561 ok(WaitForSingleObjectEx(hEvent, INFINITE, TRUE) == 0, "wait ReadFile\n");
562 success = GetOverlappedResult(hnp, &oOverlap, &readden, letGORwait);
563 if (!letGORwait && !letWFSOEwait && !success) {
564 ok(GetLastError() == ERROR_IO_INCOMPLETE, "GetOverlappedResult\n");
565 success = GetOverlappedResult(hnp, &oOverlap, &readden, TRUE);
567 trace("Server done reading.\n");
568 ok(success, "overlapped ReadFile\n");
570 trace("Server writing...\n");
571 success = WriteFile(hnp, buf, readden, NULL, &oOverlap);
572 trace("Server WriteFile returned...\n");
573 err = GetLastError();
574 ok(success || err == ERROR_IO_PENDING, "overlapped WriteFile\n");
575 trace("overlapped WriteFile returned.\n");
576 if (!success && (err == ERROR_IO_PENDING) && letWFSOEwait)
577 ok(WaitForSingleObjectEx(hEvent, INFINITE, TRUE) == 0, "wait WriteFile\n");
578 success = GetOverlappedResult(hnp, &oOverlap, &written, letGORwait);
579 if (!letGORwait && !letWFSOEwait && !success) {
580 ok(GetLastError() == ERROR_IO_INCOMPLETE, "GetOverlappedResult\n");
581 success = GetOverlappedResult(hnp, &oOverlap, &written, TRUE);
583 trace("Server done writing.\n");
584 ok(success, "overlapped WriteFile\n");
585 ok(written == readden, "write file len\n");
587 /* finish this connection, wait for next one */
588 ok(FlushFileBuffers(hnp), "FlushFileBuffers\n");
589 ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe\n");
594 static void exercizeServer(const char *pipename, HANDLE serverThread)
598 trace("exercizeServer starting\n");
599 for (i = 0; i < NB_SERVER_LOOPS; i++) {
600 HANDLE hFile=INVALID_HANDLE_VALUE;
601 static const char obuf[] = "Bit Bucket";
607 for (loop = 0; loop < 3; loop++) {
609 trace("Client connecting...\n");
610 /* Connect to the server */
611 hFile = CreateFileA(pipename, GENERIC_READ | GENERIC_WRITE, 0,
612 NULL, OPEN_EXISTING, 0, 0);
613 if (hFile != INVALID_HANDLE_VALUE)
615 err = GetLastError();
617 ok(err == ERROR_PIPE_BUSY || err == ERROR_FILE_NOT_FOUND, "connecting to pipe\n");
619 ok(err == ERROR_PIPE_BUSY, "connecting to pipe\n");
620 trace("connect failed, retrying\n");
623 ok(hFile != INVALID_HANDLE_VALUE, "client opening named pipe\n");
625 /* Make sure it can echo */
626 memset(ibuf, 0, sizeof(ibuf));
627 trace("Client writing...\n");
628 ok(WriteFile(hFile, obuf, sizeof(obuf), &written, NULL), "WriteFile to client end of pipe\n");
629 ok(written == sizeof(obuf), "write file len\n");
630 trace("Client reading...\n");
631 ok(ReadFile(hFile, ibuf, sizeof(obuf), &readden, NULL), "ReadFile from client end of pipe\n");
632 ok(readden == sizeof(obuf), "read file len\n");
633 ok(memcmp(obuf, ibuf, written) == 0, "content check\n");
635 trace("Client closing...\n");
636 ok(CloseHandle(hFile), "CloseHandle\n");
639 ok(WaitForSingleObject(serverThread,INFINITE) == WAIT_OBJECT_0, "WaitForSingleObject\n");
641 trace("exercizeServer returning\n");
644 static void test_NamedPipe_2(void)
647 DWORD serverThreadId;
651 trace("test_NamedPipe_2 starting\n");
652 /* Set up a ten second timeout */
653 alarm_event = CreateEvent( NULL, TRUE, FALSE, NULL );
654 alarmThread = CreateThread(NULL, 0, alarmThreadMain, (void *) 10000, 0, &alarmThreadId);
656 /* The servers we're about to exercize do try to clean up carefully,
657 * but to reduce the change of a test failure due to a pipe handle
658 * leak in the test code, we'll use a different pipe name for each server.
662 serverThread = CreateThread(NULL, 0, serverThreadMain1, (void *)8, 0, &serverThreadId);
663 ok(serverThread != INVALID_HANDLE_VALUE, "CreateThread\n");
664 exercizeServer(PIPENAME "serverThreadMain1", serverThread);
667 serverThread = CreateThread(NULL, 0, serverThreadMain2, 0, 0, &serverThreadId);
668 ok(serverThread != INVALID_HANDLE_VALUE, "CreateThread\n");
669 exercizeServer(PIPENAME "serverThreadMain2", serverThread);
671 if( 0 ) /* overlapped pipe server doesn't work yet - it randomly fails */
674 serverThread = CreateThread(NULL, 0, serverThreadMain3, 0, 0, &serverThreadId);
675 ok(serverThread != INVALID_HANDLE_VALUE, "CreateThread\n");
676 exercizeServer(PIPENAME "serverThreadMain3", serverThread);
679 ok(SetEvent( alarm_event ), "SetEvent\n");
680 CloseHandle( alarm_event );
681 trace("test_NamedPipe_2 returning\n");
684 static int test_DisconnectNamedPipe(void)
688 static const char obuf[] = "Bit Bucket";
693 hnp = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
694 /* nMaxInstances */ 1,
695 /* nOutBufSize */ 1024,
696 /* nInBufSize */ 1024,
697 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
698 /* lpSecurityAttrib */ NULL);
699 if (INVALID_HANDLE_VALUE == hnp) {
700 trace ("Seems we have no named pipes.\n");
704 ok(WriteFile(hnp, obuf, sizeof(obuf), &written, NULL) == 0
705 && GetLastError() == ERROR_PIPE_LISTENING, "WriteFile to not-yet-connected pipe\n");
706 ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL) == 0
707 && GetLastError() == ERROR_PIPE_LISTENING, "ReadFile from not-yet-connected pipe\n");
709 hFile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
710 ok(hFile != INVALID_HANDLE_VALUE, "CreateFile failed\n");
712 /* don't try to do i/o if one side couldn't be opened, as it hangs */
713 if (hFile != INVALID_HANDLE_VALUE) {
715 /* see what happens if server calls DisconnectNamedPipe
716 * when there are bytes in the pipe
719 ok(WriteFile(hFile, obuf, sizeof(obuf), &written, NULL), "WriteFile\n");
720 ok(written == sizeof(obuf), "write file len\n");
721 ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe while messages waiting\n");
722 ok(WriteFile(hFile, obuf, sizeof(obuf), &written, NULL) == 0
723 && GetLastError() == ERROR_PIPE_NOT_CONNECTED, "WriteFile to disconnected pipe\n");
724 ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL) == 0
725 && GetLastError() == ERROR_PIPE_NOT_CONNECTED,
726 "ReadFile from disconnected pipe with bytes waiting\n");
727 ok(CloseHandle(hFile), "CloseHandle\n");
730 ok(CloseHandle(hnp), "CloseHandle\n");
734 static void test_CreatePipe(void)
736 SECURITY_ATTRIBUTES pipe_attr;
737 HANDLE piperead, pipewrite;
742 pipe_attr.nLength = sizeof(SECURITY_ATTRIBUTES);
743 pipe_attr.bInheritHandle = TRUE;
744 pipe_attr.lpSecurityDescriptor = NULL;
745 ok(CreatePipe(&piperead, &pipewrite, &pipe_attr, 0) != 0, "CreatePipe failed\n");
746 ok(WriteFile(pipewrite,PIPENAME,sizeof(PIPENAME), &written, NULL), "Write to anonymous pipe failed\n");
747 ok(written == sizeof(PIPENAME), "Write to anonymous pipe wrote %ld bytes instead of %d\n", written,sizeof(PIPENAME));
748 ok(ReadFile(piperead,readbuf,sizeof(readbuf),&read, NULL), "Read from non empty pipe failed\n");
749 ok(read == sizeof(PIPENAME), "Read from anonymous pipe got %ld bytes instead of %d\n", read, sizeof(PIPENAME));
751 /* Now write another chunk*/
752 ok(CreatePipe(&piperead, &pipewrite, &pipe_attr, 0) != 0, "CreatePipe failed\n");
753 ok(WriteFile(pipewrite,PIPENAME,sizeof(PIPENAME), &written, NULL), "Write to anonymous pipe failed\n");
754 ok(written == sizeof(PIPENAME), "Write to anonymous pipe wrote %ld bytes instead of %d\n", written,sizeof(PIPENAME));
755 /* and close the write end, read should still succeed*/
756 ok(CloseHandle(pipewrite), "CloseHandle for the Write Pipe failed\n");
757 ok(ReadFile(piperead,readbuf,sizeof(readbuf),&read, NULL), "Read from broken pipe withe with pending data failed\n");
758 ok(read == sizeof(PIPENAME), "Read from anonymous pipe got %ld bytes instead of %d\n", read, sizeof(PIPENAME));
759 /* But now we need to get informed that the pipe is closed */
760 todo_wine ok(ReadFile(piperead,readbuf,sizeof(readbuf),&read, NULL) == 0, "Broken pipe not detected\n");
765 trace("test 1 of 6:\n");
766 if (test_DisconnectNamedPipe())
768 trace("test 2 of 6:\n");
769 test_CreateNamedPipe_instances_must_match();
770 trace("test 3 of 6:\n");
772 trace("test 4 of 6:\n");
773 test_CreateNamedPipe(PIPE_TYPE_BYTE);
774 trace("test 5 of 6\n");
775 test_CreateNamedPipe(PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE);
776 trace("test 6 of 6\n");
778 trace("all tests done\n");