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
31 #include "wine/test.h"
34 #define START_TEST(name) main(int argc, char **argv)
35 #define ok(condition, msg) \
39 fprintf(stderr,"failed at %d\n",__LINE__); \
50 #define PIPENAME "\\\\.\\PiPe\\tests_pipe.c"
52 #define NB_SERVER_LOOPS 8
54 static HANDLE alarm_event
;
56 static void test_CreateNamedPipe(int pipemode
)
60 static const char obuf
[] = "Bit Bucket";
61 static const char obuf2
[] = "More bits";
68 if (pipemode
== PIPE_TYPE_BYTE
)
69 trace("test_CreateNamedPipe starting in byte mode\n");
71 trace("test_CreateNamedPipe starting in message mode\n");
72 /* Bad parameter checks */
73 hnp
= CreateNamedPipe("not a named pipe", PIPE_ACCESS_DUPLEX
, pipemode
| PIPE_WAIT
,
74 /* nMaxInstances */ 1,
75 /* nOutBufSize */ 1024,
76 /* nInBufSize */ 1024,
77 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
78 /* lpSecurityAttrib */ NULL
);
80 if (hnp
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
) {
81 /* Is this the right way to notify user of skipped tests? */
82 ok(hnp
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
,
83 "CreateNamedPipe not supported on this platform, skipping tests.\n");
86 ok(hnp
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_INVALID_NAME
,
87 "CreateNamedPipe should fail if name doesn't start with \\\\.\\pipe\n");
89 hnp
= CreateNamedPipe(NULL
,
90 PIPE_ACCESS_DUPLEX
, pipemode
| PIPE_WAIT
,
91 1, 1024, 1024, NMPWAIT_USE_DEFAULT_WAIT
, NULL
);
92 ok(hnp
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_PATH_NOT_FOUND
,
93 "CreateNamedPipe should fail if name is NULL\n");
95 hFile
= CreateFileA(PIPENAME
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0);
96 ok(hFile
== INVALID_HANDLE_VALUE
97 && GetLastError() == ERROR_FILE_NOT_FOUND
,
98 "connecting to nonexistent named pipe should fail with ERROR_FILE_NOT_FOUND\n");
100 /* Functional checks */
102 hnp
= CreateNamedPipe(PIPENAME
, PIPE_ACCESS_DUPLEX
, pipemode
| PIPE_WAIT
,
103 /* nMaxInstances */ 1,
104 /* nOutBufSize */ 1024,
105 /* nInBufSize */ 1024,
106 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
107 /* lpSecurityAttrib */ NULL
);
108 ok(hnp
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
110 hFile
= CreateFileA(PIPENAME
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0);
111 ok(hFile
!= INVALID_HANDLE_VALUE
, "CreateFile failed\n");
113 /* don't try to do i/o if one side couldn't be opened, as it hangs */
114 if (hFile
!= INVALID_HANDLE_VALUE
) {
117 /* Make sure we can read and write a few bytes in both directions */
118 memset(ibuf
, 0, sizeof(ibuf
));
119 ok(WriteFile(hnp
, obuf
, sizeof(obuf
), &written
, NULL
), "WriteFile\n");
120 ok(written
== sizeof(obuf
), "write file len 1\n");
121 ok(PeekNamedPipe(hFile
, NULL
, 0, NULL
, &readden
, NULL
), "Peek\n");
122 ok(readden
== sizeof(obuf
), "peek 1 got %ld bytes\n", readden
);
123 ok(ReadFile(hFile
, ibuf
, sizeof(ibuf
), &readden
, NULL
), "ReadFile\n");
124 ok(readden
== sizeof(obuf
), "read 1 got %ld bytes\n", readden
);
125 ok(memcmp(obuf
, ibuf
, written
) == 0, "content 1 check\n");
127 memset(ibuf
, 0, sizeof(ibuf
));
128 ok(WriteFile(hFile
, obuf2
, sizeof(obuf2
), &written
, NULL
), "WriteFile\n");
129 ok(written
== sizeof(obuf2
), "write file len 2\n");
130 ok(PeekNamedPipe(hnp
, NULL
, 0, NULL
, &readden
, NULL
), "Peek\n");
131 ok(readden
== sizeof(obuf2
), "peek 2 got %ld bytes\n", readden
);
132 ok(PeekNamedPipe(hnp
, (LPVOID
)1, 0, NULL
, &readden
, NULL
), "Peek\n");
133 ok(readden
== sizeof(obuf2
), "peek 2 got %ld bytes\n", readden
);
134 ok(ReadFile(hnp
, ibuf
, sizeof(ibuf
), &readden
, NULL
), "ReadFile\n");
135 ok(readden
== sizeof(obuf2
), "read 2 got %ld bytes\n", readden
);
136 ok(memcmp(obuf2
, ibuf
, written
) == 0, "content 2 check\n");
138 /* Test reading of multiple writes */
139 memset(ibuf
, 0, sizeof(ibuf
));
140 ok(WriteFile(hnp
, obuf
, sizeof(obuf
), &written
, NULL
), "WriteFile3a\n");
141 ok(written
== sizeof(obuf
), "write file len 3a\n");
142 ok(WriteFile(hnp
, obuf2
, sizeof(obuf2
), &written
, NULL
), " WriteFile3b\n");
143 ok(written
== sizeof(obuf2
), "write file len 3b\n");
144 ok(PeekNamedPipe(hFile
, ibuf
, sizeof(ibuf
), &readden
, &avail
, NULL
), "Peek3\n");
145 if (pipemode
== PIPE_TYPE_BYTE
) {
147 /* should return all 23 bytes */
148 ok(readden
== sizeof(obuf
) + sizeof(obuf2
), "peek3 got %ld bytes\n", readden
);
152 ok(readden
== sizeof(obuf
), "peek3 got %ld bytes\n", readden
);
153 if (avail
!= sizeof(obuf
)) /* older Linux kernels only return the first write here */
154 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
);
185 if (avail
!= sizeof(obuf
)) /* older Linux kernels only return the first write here */
186 ok(avail
== sizeof(obuf
) + sizeof(obuf2
), "peek4 got %ld bytes available\n", avail
);
188 ok(memcmp(obuf
, pbuf
, sizeof(obuf
)) == 0, "pipe content 4a check\n");
189 if (pipemode
== PIPE_TYPE_BYTE
) {
191 pbuf
+= sizeof(obuf
);
192 ok(memcmp(obuf2
, pbuf
, sizeof(obuf2
)) == 0, "pipe content 4b check\n");
195 ok(ReadFile(hnp
, ibuf
, sizeof(ibuf
), &readden
, NULL
), "ReadFile\n");
196 if (pipemode
== PIPE_TYPE_BYTE
) {
197 ok(readden
== sizeof(obuf
) + sizeof(obuf2
), "read 4 got %ld bytes\n", readden
);
201 ok(readden
== sizeof(obuf
), "read 4 got %ld bytes\n", readden
);
205 ok(memcmp(obuf
, pbuf
, sizeof(obuf
)) == 0, "content 4a check\n");
206 if (pipemode
== PIPE_TYPE_BYTE
) {
207 pbuf
+= sizeof(obuf
);
208 ok(memcmp(obuf2
, pbuf
, sizeof(obuf2
)) == 0, "content 4b check\n");
211 /* Test reading of multiple writes after a mode change
212 (CreateFile always creates a byte mode pipe) */
213 lpmode
= PIPE_READMODE_MESSAGE
;
214 if (pipemode
== PIPE_TYPE_BYTE
) {
215 /* trying to change the client end of a byte pipe to message mode should fail */
216 ok(!SetNamedPipeHandleState(hFile
, &lpmode
, NULL
, NULL
), "Change mode\n");
220 ok(SetNamedPipeHandleState(hFile
, &lpmode
, NULL
, NULL
), "Change mode\n");
223 memset(ibuf
, 0, sizeof(ibuf
));
224 ok(WriteFile(hnp
, obuf
, sizeof(obuf
), &written
, NULL
), "WriteFile5a\n");
225 ok(written
== sizeof(obuf
), "write file len 3a\n");
226 ok(WriteFile(hnp
, obuf2
, sizeof(obuf2
), &written
, NULL
), " WriteFile5b\n");
227 ok(written
== sizeof(obuf2
), "write file len 3b\n");
228 ok(PeekNamedPipe(hFile
, ibuf
, sizeof(ibuf
), &readden
, &avail
, NULL
), "Peek5\n");
229 ok(readden
== sizeof(obuf
), "peek5 got %ld bytes\n", readden
);
230 if (avail
!= sizeof(obuf
)) /* older Linux kernels only return the first write here */
231 ok(avail
== sizeof(obuf
) + sizeof(obuf2
), "peek5 got %ld bytes available\n", avail
);
233 ok(memcmp(obuf
, pbuf
, sizeof(obuf
)) == 0, "content 5a check\n");
234 ok(ReadFile(hFile
, ibuf
, sizeof(ibuf
), &readden
, NULL
), "ReadFile\n");
236 ok(readden
== sizeof(obuf
), "read 5 got %ld bytes\n", readden
);
239 ok(memcmp(obuf
, pbuf
, sizeof(obuf
)) == 0, "content 5a check\n");
241 /* Multiple writes in the reverse direction */
242 /* the write of obuf2 from write4 should still be in the buffer */
243 ok(PeekNamedPipe(hnp
, ibuf
, sizeof(ibuf
), &readden
, &avail
, NULL
), "Peek6a\n");
245 ok(readden
== sizeof(obuf2
), "peek6a got %ld bytes\n", readden
);
246 ok(avail
== sizeof(obuf2
), "peek6a got %ld bytes available\n", avail
);
249 ok(ReadFile(hnp
, ibuf
, sizeof(ibuf
), &readden
, NULL
), "ReadFile\n");
250 ok(readden
== sizeof(obuf2
), "read 6a got %ld bytes\n", readden
);
252 ok(memcmp(obuf2
, pbuf
, sizeof(obuf2
)) == 0, "content 6a check\n");
254 memset(ibuf
, 0, sizeof(ibuf
));
255 ok(WriteFile(hFile
, obuf
, sizeof(obuf
), &written
, NULL
), "WriteFile6a\n");
256 ok(written
== sizeof(obuf
), "write file len 6a\n");
257 ok(WriteFile(hFile
, obuf2
, sizeof(obuf2
), &written
, NULL
), " WriteFile6b\n");
258 ok(written
== sizeof(obuf2
), "write file len 6b\n");
259 ok(PeekNamedPipe(hnp
, ibuf
, sizeof(ibuf
), &readden
, &avail
, NULL
), "Peek6\n");
260 ok(readden
== sizeof(obuf
), "peek6 got %ld bytes\n", readden
);
261 if (avail
!= sizeof(obuf
)) /* older Linux kernels only return the first write here */
262 ok(avail
== sizeof(obuf
) + sizeof(obuf2
), "peek6b got %ld bytes available\n", avail
);
264 ok(memcmp(obuf
, pbuf
, sizeof(obuf
)) == 0, "content 6a check\n");
265 ok(ReadFile(hnp
, ibuf
, sizeof(ibuf
), &readden
, NULL
), "ReadFile\n");
267 ok(readden
== sizeof(obuf
), "read 6b got %ld bytes\n", readden
);
270 ok(memcmp(obuf
, pbuf
, sizeof(obuf
)) == 0, "content 6a check\n");
273 /* Picky conformance tests */
275 /* Verify that you can't connect to pipe again
276 * until server calls DisconnectNamedPipe+ConnectNamedPipe
277 * or creates a new pipe
278 * case 1: other client not yet closed
280 hFile2
= CreateFileA(PIPENAME
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0);
281 ok(hFile2
== INVALID_HANDLE_VALUE
,
282 "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail\n");
283 ok(GetLastError() == ERROR_PIPE_BUSY
,
284 "connecting to named pipe before other client closes should fail with ERROR_PIPE_BUSY\n");
286 ok(CloseHandle(hFile
), "CloseHandle\n");
288 /* case 2: other client already closed */
289 hFile
= CreateFileA(PIPENAME
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0);
290 ok(hFile
== INVALID_HANDLE_VALUE
,
291 "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail\n");
292 ok(GetLastError() == ERROR_PIPE_BUSY
,
293 "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail with ERROR_PIPE_BUSY\n");
295 ok(DisconnectNamedPipe(hnp
), "DisconnectNamedPipe\n");
297 /* case 3: server has called DisconnectNamedPipe but not ConnectNamed Pipe */
298 hFile
= CreateFileA(PIPENAME
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0);
299 ok(hFile
== INVALID_HANDLE_VALUE
,
300 "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail\n");
301 ok(GetLastError() == ERROR_PIPE_BUSY
,
302 "connecting to named pipe after other client closes but before ConnectNamedPipe should fail with ERROR_PIPE_BUSY\n");
304 /* to be complete, we'd call ConnectNamedPipe here and loop,
305 * but by default that's blocking, so we'd either have
306 * to turn on the uncommon nonblocking mode, or
307 * use another thread.
311 ok(CloseHandle(hnp
), "CloseHandle\n");
313 trace("test_CreateNamedPipe returning\n");
316 void test_CreateNamedPipe_instances_must_match(void)
320 /* Check no mismatch */
321 hnp
= CreateNamedPipe(PIPENAME
, PIPE_ACCESS_DUPLEX
, PIPE_TYPE_BYTE
| PIPE_WAIT
,
322 /* nMaxInstances */ 2,
323 /* nOutBufSize */ 1024,
324 /* nInBufSize */ 1024,
325 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
326 /* lpSecurityAttrib */ NULL
);
327 ok(hnp
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
329 hnp2
= CreateNamedPipe(PIPENAME
, PIPE_ACCESS_DUPLEX
, PIPE_TYPE_BYTE
| PIPE_WAIT
,
330 /* nMaxInstances */ 2,
331 /* nOutBufSize */ 1024,
332 /* nInBufSize */ 1024,
333 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
334 /* lpSecurityAttrib */ NULL
);
335 ok(hnp2
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
337 ok(CloseHandle(hnp
), "CloseHandle\n");
338 ok(CloseHandle(hnp2
), "CloseHandle\n");
340 /* Check nMaxInstances */
341 hnp
= CreateNamedPipe(PIPENAME
, PIPE_ACCESS_DUPLEX
, PIPE_TYPE_BYTE
| PIPE_WAIT
,
342 /* nMaxInstances */ 1,
343 /* nOutBufSize */ 1024,
344 /* nInBufSize */ 1024,
345 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
346 /* lpSecurityAttrib */ NULL
);
347 ok(hnp
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
349 hnp2
= CreateNamedPipe(PIPENAME
, PIPE_ACCESS_DUPLEX
, PIPE_TYPE_BYTE
| PIPE_WAIT
,
350 /* nMaxInstances */ 1,
351 /* nOutBufSize */ 1024,
352 /* nInBufSize */ 1024,
353 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
354 /* lpSecurityAttrib */ NULL
);
355 ok(hnp2
== INVALID_HANDLE_VALUE
356 && GetLastError() == ERROR_PIPE_BUSY
, "nMaxInstances not obeyed\n");
358 ok(CloseHandle(hnp
), "CloseHandle\n");
360 /* Check PIPE_ACCESS_* */
361 hnp
= CreateNamedPipe(PIPENAME
, PIPE_ACCESS_DUPLEX
, PIPE_TYPE_BYTE
| PIPE_WAIT
,
362 /* nMaxInstances */ 2,
363 /* nOutBufSize */ 1024,
364 /* nInBufSize */ 1024,
365 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
366 /* lpSecurityAttrib */ NULL
);
367 ok(hnp
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
369 hnp2
= CreateNamedPipe(PIPENAME
, PIPE_ACCESS_INBOUND
, PIPE_TYPE_BYTE
| PIPE_WAIT
,
370 /* nMaxInstances */ 1,
371 /* nOutBufSize */ 1024,
372 /* nInBufSize */ 1024,
373 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
374 /* lpSecurityAttrib */ NULL
);
375 ok(hnp2
== INVALID_HANDLE_VALUE
376 && GetLastError() == ERROR_ACCESS_DENIED
, "PIPE_ACCESS_* mismatch allowed\n");
378 ok(CloseHandle(hnp
), "CloseHandle\n");
383 /** implementation of alarm() */
384 static DWORD CALLBACK
alarmThreadMain(LPVOID arg
)
386 DWORD timeout
= (DWORD
) arg
;
387 trace("alarmThreadMain\n");
388 if (WaitForSingleObject( alarm_event
, timeout
) == WAIT_TIMEOUT
)
390 ok(FALSE
, "alarm\n");
396 HANDLE hnp
= INVALID_HANDLE_VALUE
;
398 /** Trivial byte echo server - disconnects after each session */
399 static DWORD CALLBACK
serverThreadMain1(LPVOID arg
)
403 trace("serverThreadMain1 start\n");
404 /* Set up a simple echo server */
405 hnp
= CreateNamedPipe(PIPENAME
"serverThreadMain1", PIPE_ACCESS_DUPLEX
,
406 PIPE_TYPE_BYTE
| PIPE_WAIT
,
407 /* nMaxInstances */ 1,
408 /* nOutBufSize */ 1024,
409 /* nInBufSize */ 1024,
410 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
411 /* lpSecurityAttrib */ NULL
);
413 ok(hnp
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
414 for (i
= 0; i
< NB_SERVER_LOOPS
; i
++) {
420 /* Wait for client to connect */
421 trace("Server calling ConnectNamedPipe...\n");
422 ok(ConnectNamedPipe(hnp
, NULL
)
423 || GetLastError() == ERROR_PIPE_CONNECTED
, "ConnectNamedPipe\n");
424 trace("ConnectNamedPipe returned.\n");
426 /* Echo bytes once */
427 memset(buf
, 0, sizeof(buf
));
429 trace("Server reading...\n");
430 success
= ReadFile(hnp
, buf
, sizeof(buf
), &readden
, NULL
);
431 trace("Server done reading.\n");
432 ok(success
, "ReadFile\n");
433 ok(readden
, "short read\n");
435 trace("Server writing...\n");
436 ok(WriteFile(hnp
, buf
, readden
, &written
, NULL
), "WriteFile\n");
437 trace("Server done writing.\n");
438 ok(written
== readden
, "write file len\n");
440 /* finish this connection, wait for next one */
441 ok(FlushFileBuffers(hnp
), "FlushFileBuffers\n");
442 trace("Server done flushing.\n");
443 ok(DisconnectNamedPipe(hnp
), "DisconnectNamedPipe\n");
444 trace("Server done disconnecting.\n");
449 /** Trivial byte echo server - closes after each connection */
450 static DWORD CALLBACK
serverThreadMain2(LPVOID arg
)
455 trace("serverThreadMain2\n");
456 /* Set up a simple echo server */
457 hnp
= CreateNamedPipe(PIPENAME
"serverThreadMain2", PIPE_ACCESS_DUPLEX
,
458 PIPE_TYPE_BYTE
| PIPE_WAIT
,
459 /* nMaxInstances */ 2,
460 /* nOutBufSize */ 1024,
461 /* nInBufSize */ 1024,
462 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
463 /* lpSecurityAttrib */ NULL
);
464 ok(hnp
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
466 for (i
= 0; i
< NB_SERVER_LOOPS
; i
++) {
472 /* Wait for client to connect */
473 trace("Server calling ConnectNamedPipe...\n");
474 ok(ConnectNamedPipe(hnp
, NULL
)
475 || GetLastError() == ERROR_PIPE_CONNECTED
, "ConnectNamedPipe\n");
476 trace("ConnectNamedPipe returned.\n");
478 /* Echo bytes once */
479 memset(buf
, 0, sizeof(buf
));
481 trace("Server reading...\n");
482 success
= ReadFile(hnp
, buf
, sizeof(buf
), &readden
, NULL
);
483 trace("Server done reading.\n");
484 ok(success
, "ReadFile\n");
486 trace("Server writing...\n");
487 ok(WriteFile(hnp
, buf
, readden
, &written
, NULL
), "WriteFile\n");
488 trace("Server done writing.\n");
489 ok(written
== readden
, "write file len\n");
491 /* finish this connection, wait for next one */
492 ok(FlushFileBuffers(hnp
), "FlushFileBuffers\n");
493 ok(DisconnectNamedPipe(hnp
), "DisconnectNamedPipe\n");
495 /* Set up next echo server */
497 CreateNamedPipe(PIPENAME
"serverThreadMain2", PIPE_ACCESS_DUPLEX
,
498 PIPE_TYPE_BYTE
| PIPE_WAIT
,
499 /* nMaxInstances */ 2,
500 /* nOutBufSize */ 1024,
501 /* nInBufSize */ 1024,
502 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
503 /* lpSecurityAttrib */ NULL
);
505 ok(hnpNext
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
507 ok(CloseHandle(hnp
), "CloseHandle\n");
513 /** Trivial byte echo server - uses overlapped named pipe calls */
514 static DWORD CALLBACK
serverThreadMain3(LPVOID arg
)
519 trace("serverThreadMain3\n");
520 /* Set up a simple echo server */
521 hnp
= CreateNamedPipe(PIPENAME
"serverThreadMain3", PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
522 PIPE_TYPE_BYTE
| PIPE_WAIT
,
523 /* nMaxInstances */ 1,
524 /* nOutBufSize */ 1024,
525 /* nInBufSize */ 1024,
526 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
527 /* lpSecurityAttrib */ NULL
);
528 ok(hnp
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
530 hEvent
= CreateEvent(NULL
, /* security attribute */
531 TRUE
, /* manual reset event */
532 FALSE
, /* initial state */
534 ok(hEvent
!= NULL
, "CreateEvent\n");
536 for (i
= 0; i
< NB_SERVER_LOOPS
; i
++) {
543 int letWFSOEwait
= (i
& 2);
544 int letGORwait
= (i
& 1);
547 memset(&oOverlap
, 0, sizeof(oOverlap
));
548 oOverlap
.hEvent
= hEvent
;
550 /* Wait for client to connect */
551 trace("Server calling overlapped ConnectNamedPipe...\n");
552 success
= ConnectNamedPipe(hnp
, &oOverlap
);
553 err
= GetLastError();
554 ok(success
|| err
== ERROR_IO_PENDING
555 || err
== ERROR_PIPE_CONNECTED
, "overlapped ConnectNamedPipe\n");
556 trace("overlapped ConnectNamedPipe returned.\n");
557 if (!success
&& (err
== ERROR_IO_PENDING
) && letWFSOEwait
)
558 ok(WaitForSingleObjectEx(hEvent
, INFINITE
, TRUE
) == 0, "wait ConnectNamedPipe\n");
559 success
= GetOverlappedResult(hnp
, &oOverlap
, &dummy
, letGORwait
);
560 if (!letGORwait
&& !letWFSOEwait
&& !success
) {
561 ok(GetLastError() == ERROR_IO_INCOMPLETE
, "GetOverlappedResult\n");
562 success
= GetOverlappedResult(hnp
, &oOverlap
, &dummy
, TRUE
);
564 ok(success
, "GetOverlappedResult ConnectNamedPipe\n");
565 trace("overlapped ConnectNamedPipe operation complete.\n");
567 /* Echo bytes once */
568 memset(buf
, 0, sizeof(buf
));
570 trace("Server reading...\n");
571 success
= ReadFile(hnp
, buf
, sizeof(buf
), NULL
, &oOverlap
);
572 trace("Server ReadFile returned...\n");
573 err
= GetLastError();
574 ok(success
|| err
== ERROR_IO_PENDING
, "overlapped ReadFile\n");
575 trace("overlapped ReadFile returned.\n");
576 if (!success
&& (err
== ERROR_IO_PENDING
) && letWFSOEwait
)
577 ok(WaitForSingleObjectEx(hEvent
, INFINITE
, TRUE
) == 0, "wait ReadFile\n");
578 success
= GetOverlappedResult(hnp
, &oOverlap
, &readden
, letGORwait
);
579 if (!letGORwait
&& !letWFSOEwait
&& !success
) {
580 ok(GetLastError() == ERROR_IO_INCOMPLETE
, "GetOverlappedResult\n");
581 success
= GetOverlappedResult(hnp
, &oOverlap
, &readden
, TRUE
);
583 trace("Server done reading.\n");
584 ok(success
, "overlapped ReadFile\n");
586 trace("Server writing...\n");
587 success
= WriteFile(hnp
, buf
, readden
, NULL
, &oOverlap
);
588 trace("Server WriteFile returned...\n");
589 err
= GetLastError();
590 ok(success
|| err
== ERROR_IO_PENDING
, "overlapped WriteFile\n");
591 trace("overlapped WriteFile returned.\n");
592 if (!success
&& (err
== ERROR_IO_PENDING
) && letWFSOEwait
)
593 ok(WaitForSingleObjectEx(hEvent
, INFINITE
, TRUE
) == 0, "wait WriteFile\n");
594 success
= GetOverlappedResult(hnp
, &oOverlap
, &written
, letGORwait
);
595 if (!letGORwait
&& !letWFSOEwait
&& !success
) {
596 ok(GetLastError() == ERROR_IO_INCOMPLETE
, "GetOverlappedResult\n");
597 success
= GetOverlappedResult(hnp
, &oOverlap
, &written
, TRUE
);
599 trace("Server done writing.\n");
600 ok(success
, "overlapped WriteFile\n");
601 ok(written
== readden
, "write file len\n");
603 /* finish this connection, wait for next one */
604 ok(FlushFileBuffers(hnp
), "FlushFileBuffers\n");
605 ok(DisconnectNamedPipe(hnp
), "DisconnectNamedPipe\n");
610 static void exercizeServer(const char *pipename
, HANDLE serverThread
)
614 trace("exercizeServer starting\n");
615 for (i
= 0; i
< NB_SERVER_LOOPS
; i
++) {
616 HANDLE hFile
=INVALID_HANDLE_VALUE
;
617 static const char obuf
[] = "Bit Bucket";
623 for (loop
= 0; loop
< 3; loop
++) {
625 trace("Client connecting...\n");
626 /* Connect to the server */
627 hFile
= CreateFileA(pipename
, GENERIC_READ
| GENERIC_WRITE
, 0,
628 NULL
, OPEN_EXISTING
, 0, 0);
629 if (hFile
!= INVALID_HANDLE_VALUE
)
631 err
= GetLastError();
633 ok(err
== ERROR_PIPE_BUSY
|| err
== ERROR_FILE_NOT_FOUND
, "connecting to pipe\n");
635 ok(err
== ERROR_PIPE_BUSY
, "connecting to pipe\n");
636 trace("connect failed, retrying\n");
639 ok(hFile
!= INVALID_HANDLE_VALUE
, "client opening named pipe\n");
641 /* Make sure it can echo */
642 memset(ibuf
, 0, sizeof(ibuf
));
643 trace("Client writing...\n");
644 ok(WriteFile(hFile
, obuf
, sizeof(obuf
), &written
, NULL
), "WriteFile to client end of pipe\n");
645 ok(written
== sizeof(obuf
), "write file len\n");
646 trace("Client reading...\n");
647 ok(ReadFile(hFile
, ibuf
, sizeof(obuf
), &readden
, NULL
), "ReadFile from client end of pipe\n");
648 ok(readden
== sizeof(obuf
), "read file len\n");
649 ok(memcmp(obuf
, ibuf
, written
) == 0, "content check\n");
651 trace("Client closing...\n");
652 ok(CloseHandle(hFile
), "CloseHandle\n");
655 ok(WaitForSingleObject(serverThread
,INFINITE
) == WAIT_OBJECT_0
, "WaitForSingleObject\n");
657 trace("exercizeServer returning\n");
660 static void test_NamedPipe_2(void)
663 DWORD serverThreadId
;
667 trace("test_NamedPipe_2 starting\n");
668 /* Set up a ten second timeout */
669 alarm_event
= CreateEvent( NULL
, TRUE
, FALSE
, NULL
);
670 alarmThread
= CreateThread(NULL
, 0, alarmThreadMain
, (void *) 10000, 0, &alarmThreadId
);
672 /* The servers we're about to exercize do try to clean up carefully,
673 * but to reduce the change of a test failure due to a pipe handle
674 * leak in the test code, we'll use a different pipe name for each server.
678 serverThread
= CreateThread(NULL
, 0, serverThreadMain1
, (void *)8, 0, &serverThreadId
);
679 ok(serverThread
!= INVALID_HANDLE_VALUE
, "CreateThread\n");
680 exercizeServer(PIPENAME
"serverThreadMain1", serverThread
);
683 serverThread
= CreateThread(NULL
, 0, serverThreadMain2
, 0, 0, &serverThreadId
);
684 ok(serverThread
!= INVALID_HANDLE_VALUE
, "CreateThread\n");
685 exercizeServer(PIPENAME
"serverThreadMain2", serverThread
);
687 if( 0 ) /* overlapped pipe server doesn't work yet - it randomly fails */
690 serverThread
= CreateThread(NULL
, 0, serverThreadMain3
, 0, 0, &serverThreadId
);
691 ok(serverThread
!= INVALID_HANDLE_VALUE
, "CreateThread\n");
692 exercizeServer(PIPENAME
"serverThreadMain3", serverThread
);
695 ok(SetEvent( alarm_event
), "SetEvent\n");
696 CloseHandle( alarm_event
);
697 trace("test_NamedPipe_2 returning\n");
700 static int test_DisconnectNamedPipe(void)
704 static const char obuf
[] = "Bit Bucket";
709 hnp
= CreateNamedPipe(PIPENAME
, PIPE_ACCESS_DUPLEX
, PIPE_TYPE_BYTE
| PIPE_WAIT
,
710 /* nMaxInstances */ 1,
711 /* nOutBufSize */ 1024,
712 /* nInBufSize */ 1024,
713 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
714 /* lpSecurityAttrib */ NULL
);
715 if (INVALID_HANDLE_VALUE
== hnp
) {
716 trace ("Seems we have no named pipes.\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");
753 trace("test 1 of 4:\n");
754 if (test_DisconnectNamedPipe())
756 trace("test 2 of 4:\n");
757 test_CreateNamedPipe_instances_must_match();
758 trace("test 3 of 4:\n");
760 trace("test 4 of 4:\n");
761 test_CreateNamedPipe(PIPE_TYPE_BYTE
);
762 trace("all tests done\n");
763 test_CreateNamedPipe(PIPE_TYPE_MESSAGE
| PIPE_READMODE_MESSAGE
);
764 trace("all tests done\n");