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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 ok(WaitNamedPipeA(PIPENAME, 2000), "WaitNamedPipe failed (%08x)\n", GetLastError());
96 hFile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
97 ok(hFile != INVALID_HANDLE_VALUE, "CreateFile failed (%08x)\n", GetLastError());
99 /* don't try to do i/o if one side couldn't be opened, as it hangs */
100 if (hFile != INVALID_HANDLE_VALUE) {
103 /* Make sure we can read and write a few bytes in both directions */
104 memset(ibuf, 0, sizeof(ibuf));
105 ok(WriteFile(hnp, obuf, sizeof(obuf), &written, NULL), "WriteFile\n");
106 ok(written == sizeof(obuf), "write file len 1\n");
107 ok(PeekNamedPipe(hFile, NULL, 0, NULL, &readden, NULL), "Peek\n");
108 ok(readden == sizeof(obuf), "peek 1 got %d bytes\n", readden);
109 ok(ReadFile(hFile, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
110 ok(readden == sizeof(obuf), "read 1 got %d bytes\n", readden);
111 ok(memcmp(obuf, ibuf, written) == 0, "content 1 check\n");
113 memset(ibuf, 0, sizeof(ibuf));
114 ok(WriteFile(hFile, obuf2, sizeof(obuf2), &written, NULL), "WriteFile\n");
115 ok(written == sizeof(obuf2), "write file len 2\n");
116 ok(PeekNamedPipe(hnp, NULL, 0, NULL, &readden, NULL), "Peek\n");
117 ok(readden == sizeof(obuf2), "peek 2 got %d bytes\n", readden);
118 ok(PeekNamedPipe(hnp, (LPVOID)1, 0, NULL, &readden, NULL), "Peek\n");
119 ok(readden == sizeof(obuf2), "peek 2 got %d bytes\n", readden);
120 ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
121 ok(readden == sizeof(obuf2), "read 2 got %d bytes\n", readden);
122 ok(memcmp(obuf2, ibuf, written) == 0, "content 2 check\n");
124 /* Test reading of multiple writes */
125 memset(ibuf, 0, sizeof(ibuf));
126 ok(WriteFile(hnp, obuf, sizeof(obuf), &written, NULL), "WriteFile3a\n");
127 ok(written == sizeof(obuf), "write file len 3a\n");
128 ok(WriteFile(hnp, obuf2, sizeof(obuf2), &written, NULL), " WriteFile3b\n");
129 ok(written == sizeof(obuf2), "write file len 3b\n");
130 ok(PeekNamedPipe(hFile, ibuf, sizeof(ibuf), &readden, &avail, NULL), "Peek3\n");
131 if (pipemode == PIPE_TYPE_BYTE) {
132 if (readden != sizeof(obuf)) /* Linux only returns the first message */
133 ok(readden == sizeof(obuf) + sizeof(obuf2), "peek3 got %d bytes\n", readden);
135 todo_wine ok(readden == sizeof(obuf) + sizeof(obuf2), "peek3 got %d bytes\n", readden);
139 if (readden != sizeof(obuf) + sizeof(obuf2)) /* MacOS returns both messages */
140 ok(readden == sizeof(obuf), "peek3 got %d bytes\n", readden);
142 todo_wine ok(readden == sizeof(obuf), "peek3 got %d bytes\n", readden);
144 if (avail != sizeof(obuf)) /* older Linux kernels only return the first write here */
145 ok(avail == sizeof(obuf) + sizeof(obuf2), "peek3 got %d bytes available\n", avail);
147 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "pipe content 3a check\n");
148 if (pipemode == PIPE_TYPE_BYTE && readden >= sizeof(obuf)+sizeof(obuf2)) {
149 pbuf += sizeof(obuf);
150 ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "pipe content 3b check\n");
152 ok(ReadFile(hFile, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
153 ok(readden == sizeof(obuf) + sizeof(obuf2), "read 3 got %d bytes\n", readden);
155 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 3a check\n");
156 pbuf += sizeof(obuf);
157 ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "content 3b check\n");
159 /* Multiple writes in the reverse direction */
160 memset(ibuf, 0, sizeof(ibuf));
161 ok(WriteFile(hFile, obuf, sizeof(obuf), &written, NULL), "WriteFile4a\n");
162 ok(written == sizeof(obuf), "write file len 4a\n");
163 ok(WriteFile(hFile, obuf2, sizeof(obuf2), &written, NULL), " WriteFile4b\n");
164 ok(written == sizeof(obuf2), "write file len 4b\n");
165 ok(PeekNamedPipe(hnp, ibuf, sizeof(ibuf), &readden, &avail, NULL), "Peek4\n");
166 if (pipemode == PIPE_TYPE_BYTE) {
167 if (readden != sizeof(obuf)) /* Linux only returns the first message */
168 /* should return all 23 bytes */
169 ok(readden == sizeof(obuf) + sizeof(obuf2), "peek4 got %d bytes\n", readden);
171 todo_wine ok(readden == sizeof(obuf) + sizeof(obuf2), "peek4 got %d bytes\n", readden);
175 if (readden != sizeof(obuf) + sizeof(obuf2)) /* MacOS returns both messages */
176 ok(readden == sizeof(obuf), "peek4 got %d bytes\n", readden);
178 todo_wine ok(readden == sizeof(obuf), "peek4 got %d bytes\n", readden);
180 if (avail != sizeof(obuf)) /* older Linux kernels only return the first write here */
181 ok(avail == sizeof(obuf) + sizeof(obuf2), "peek4 got %d bytes available\n", avail);
183 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "pipe content 4a check\n");
184 if (pipemode == PIPE_TYPE_BYTE && readden >= sizeof(obuf)+sizeof(obuf2)) {
185 pbuf += sizeof(obuf);
186 ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "pipe content 4b check\n");
188 ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
189 if (pipemode == PIPE_TYPE_BYTE) {
190 ok(readden == sizeof(obuf) + sizeof(obuf2), "read 4 got %d bytes\n", readden);
194 ok(readden == sizeof(obuf), "read 4 got %d bytes\n", readden);
198 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 4a check\n");
199 if (pipemode == PIPE_TYPE_BYTE) {
200 pbuf += sizeof(obuf);
201 ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "content 4b check\n");
204 /* Test reading of multiple writes after a mode change
205 (CreateFile always creates a byte mode pipe) */
206 lpmode = PIPE_READMODE_MESSAGE;
207 if (pipemode == PIPE_TYPE_BYTE) {
208 /* trying to change the client end of a byte pipe to message mode should fail */
209 ok(!SetNamedPipeHandleState(hFile, &lpmode, NULL, NULL), "Change mode\n");
213 ok(SetNamedPipeHandleState(hFile, &lpmode, NULL, NULL), "Change mode\n");
216 memset(ibuf, 0, sizeof(ibuf));
217 ok(WriteFile(hnp, obuf, sizeof(obuf), &written, NULL), "WriteFile5a\n");
218 ok(written == sizeof(obuf), "write file len 3a\n");
219 ok(WriteFile(hnp, obuf2, sizeof(obuf2), &written, NULL), " WriteFile5b\n");
220 ok(written == sizeof(obuf2), "write file len 3b\n");
221 ok(PeekNamedPipe(hFile, ibuf, sizeof(ibuf), &readden, &avail, NULL), "Peek5\n");
222 if (readden != sizeof(obuf) + sizeof(obuf2)) /* MacOS returns both writes */
223 ok(readden == sizeof(obuf), "peek5 got %d bytes\n", readden);
225 todo_wine ok(readden == sizeof(obuf), "peek5 got %d bytes\n", readden);
226 if (avail != sizeof(obuf)) /* older Linux kernels only return the first write here */
227 ok(avail == sizeof(obuf) + sizeof(obuf2), "peek5 got %d bytes available\n", avail);
229 todo_wine ok(avail == sizeof(obuf) + sizeof(obuf2), "peek5 got %d 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 %d 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 %d bytes\n", readden);
244 ok(avail == sizeof(obuf2), "peek6a got %d bytes available\n", avail);
247 ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
248 ok(readden == sizeof(obuf2), "read 6a got %d 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 if (readden != sizeof(obuf) + sizeof(obuf2)) /* MacOS returns both writes */
259 ok(readden == sizeof(obuf), "peek6 got %d bytes\n", readden);
261 todo_wine ok(readden == sizeof(obuf), "peek6 got %d bytes\n", readden);
262 if (avail != sizeof(obuf)) /* older Linux kernels only return the first write here */
263 ok(avail == sizeof(obuf) + sizeof(obuf2), "peek6b got %d bytes available\n", avail);
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 %d 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 static 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");
751 static void test_CreatePipe(void)
753 SECURITY_ATTRIBUTES pipe_attr;
754 HANDLE piperead, pipewrite;
759 pipe_attr.nLength = sizeof(SECURITY_ATTRIBUTES);
760 pipe_attr.bInheritHandle = TRUE;
761 pipe_attr.lpSecurityDescriptor = NULL;
762 ok(CreatePipe(&piperead, &pipewrite, &pipe_attr, 0) != 0, "CreatePipe failed\n");
763 ok(WriteFile(pipewrite,PIPENAME,sizeof(PIPENAME), &written, NULL), "Write to anonymous pipe failed\n");
764 ok(written == sizeof(PIPENAME), "Write to anonymous pipe wrote %d bytes\n", written);
765 ok(ReadFile(piperead,readbuf,sizeof(readbuf),&read, NULL), "Read from non empty pipe failed\n");
766 ok(read == sizeof(PIPENAME), "Read from anonymous pipe got %d bytes\n", read);
768 /* Now write another chunk*/
769 ok(CreatePipe(&piperead, &pipewrite, &pipe_attr, 0) != 0, "CreatePipe failed\n");
770 ok(WriteFile(pipewrite,PIPENAME,sizeof(PIPENAME), &written, NULL), "Write to anonymous pipe failed\n");
771 ok(written == sizeof(PIPENAME), "Write to anonymous pipe wrote %d bytes\n", written);
772 /* and close the write end, read should still succeed*/
773 ok(CloseHandle(pipewrite), "CloseHandle for the Write Pipe failed\n");
774 ok(ReadFile(piperead,readbuf,sizeof(readbuf),&read, NULL), "Read from broken pipe withe with pending data failed\n");
775 ok(read == sizeof(PIPENAME), "Read from anonymous pipe got %d bytes\n", read);
776 /* But now we need to get informed that the pipe is closed */
777 ok(ReadFile(piperead,readbuf,sizeof(readbuf),&read, NULL) == 0, "Broken pipe not detected\n");
782 trace("test 1 of 6:\n");
783 if (test_DisconnectNamedPipe())
785 trace("test 2 of 6:\n");
786 test_CreateNamedPipe_instances_must_match();
787 trace("test 3 of 6:\n");
789 trace("test 4 of 6:\n");
790 test_CreateNamedPipe(PIPE_TYPE_BYTE);
791 trace("test 5 of 6\n");
792 test_CreateNamedPipe(PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE);
793 trace("test 6 of 6\n");
795 trace("all tests done\n");