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
25 #define WIN32_NO_STATUS
29 #include "wine/test.h"
31 #define PIPENAME "\\\\.\\PiPe\\tests_pipe.c"
33 #define NB_SERVER_LOOPS 8
35 static HANDLE alarm_event
;
36 static BOOL (WINAPI
*pDuplicateTokenEx
)(HANDLE
,DWORD
,LPSECURITY_ATTRIBUTES
,
37 SECURITY_IMPERSONATION_LEVEL
,TOKEN_TYPE
,PHANDLE
);
38 static DWORD (WINAPI
*pQueueUserAPC
)(PAPCFUNC pfnAPC
, HANDLE hThread
, ULONG_PTR dwData
);
40 static BOOL user_apc_ran
;
41 static void CALLBACK
user_apc(ULONG_PTR param
)
54 ULONG_PTR returnValue
;
60 static DWORD CALLBACK
rpcThreadMain(LPVOID arg
)
62 struct rpcThreadArgs
*rpcargs
= (struct rpcThreadArgs
*)arg
;
63 trace("rpcThreadMain starting\n");
64 SetLastError( rpcargs
->lastError
);
69 rpcargs
->returnValue
= (ULONG_PTR
)ReadFile( (HANDLE
)rpcargs
->args
[0], /* hFile */
70 (LPVOID
)rpcargs
->args
[1], /* buffer */
71 (DWORD
)rpcargs
->args
[2], /* bytesToRead */
72 (LPDWORD
)rpcargs
->args
[3], /* bytesRead */
73 (LPOVERLAPPED
)rpcargs
->args
[4] ); /* overlapped */
77 SetLastError( ERROR_CALL_NOT_IMPLEMENTED
);
78 rpcargs
->returnValue
= 0;
82 rpcargs
->lastError
= GetLastError();
83 trace("rpcThreadMain returning\n");
87 /* Runs ReadFile(...) from a different thread */
88 static BOOL
RpcReadFile(HANDLE hFile
, LPVOID buffer
, DWORD bytesToRead
, LPDWORD bytesRead
, LPOVERLAPPED overlapped
)
90 struct rpcThreadArgs rpcargs
;
94 rpcargs
.returnValue
= 0;
95 rpcargs
.lastError
= GetLastError();
96 rpcargs
.op
= RPC_READFILE
;
97 rpcargs
.args
[0] = (ULONG_PTR
)hFile
;
98 rpcargs
.args
[1] = (ULONG_PTR
)buffer
;
99 rpcargs
.args
[2] = (ULONG_PTR
)bytesToRead
;
100 rpcargs
.args
[3] = (ULONG_PTR
)bytesRead
;
101 rpcargs
.args
[4] = (ULONG_PTR
)overlapped
;
103 thread
= CreateThread(NULL
, 0, rpcThreadMain
, (void *)&rpcargs
, 0, &threadId
);
104 ok(thread
!= NULL
, "CreateThread failed. %d\n", GetLastError());
105 ret
= WaitForSingleObject(thread
, INFINITE
);
106 ok(ret
== WAIT_OBJECT_0
, "WaitForSingleObject failed with %d.\n", GetLastError());
109 SetLastError(rpcargs
.lastError
);
110 return (BOOL
)rpcargs
.returnValue
;
113 static void test_CreateNamedPipe(int pipemode
)
117 static const char obuf
[] = "Bit Bucket";
118 static const char obuf2
[] = "More bits";
119 char ibuf
[32], *pbuf
;
126 if (pipemode
== PIPE_TYPE_BYTE
)
127 trace("test_CreateNamedPipe starting in byte mode\n");
129 trace("test_CreateNamedPipe starting in message mode\n");
131 /* Wait for nonexistent pipe */
132 ret
= WaitNamedPipeA(PIPENAME
, 2000);
133 ok(ret
== 0, "WaitNamedPipe returned %d for nonexistent pipe\n", ret
);
134 ok(GetLastError() == ERROR_FILE_NOT_FOUND
, "wrong error %u\n", GetLastError());
136 /* Bad parameter checks */
137 hnp
= CreateNamedPipeA("not a named pipe", PIPE_ACCESS_DUPLEX
, pipemode
| PIPE_WAIT
,
138 /* nMaxInstances */ 1,
139 /* nOutBufSize */ 1024,
140 /* nInBufSize */ 1024,
141 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
142 /* lpSecurityAttrib */ NULL
);
143 ok(hnp
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_INVALID_NAME
,
144 "CreateNamedPipe should fail if name doesn't start with \\\\.\\pipe\n");
146 if (pipemode
== PIPE_TYPE_BYTE
)
148 /* Bad parameter checks */
149 hnp
= CreateNamedPipeA(PIPENAME
, PIPE_ACCESS_DUPLEX
, PIPE_TYPE_BYTE
| PIPE_READMODE_MESSAGE
,
150 /* nMaxInstances */ 1,
151 /* nOutBufSize */ 1024,
152 /* nInBufSize */ 1024,
153 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
154 /* lpSecurityAttrib */ NULL
);
155 ok(hnp
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_INVALID_PARAMETER
,
156 "CreateNamedPipe should fail with PIPE_TYPE_BYTE | PIPE_READMODE_MESSAGE\n");
159 hnp
= CreateNamedPipeA(NULL
,
160 PIPE_ACCESS_DUPLEX
, pipemode
| PIPE_WAIT
,
161 1, 1024, 1024, NMPWAIT_USE_DEFAULT_WAIT
, NULL
);
162 ok(hnp
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_PATH_NOT_FOUND
,
163 "CreateNamedPipe should fail if name is NULL\n");
165 hFile
= CreateFileA(PIPENAME
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0);
166 ok(hFile
== INVALID_HANDLE_VALUE
167 && GetLastError() == ERROR_FILE_NOT_FOUND
,
168 "connecting to nonexistent named pipe should fail with ERROR_FILE_NOT_FOUND\n");
170 /* Functional checks */
172 hnp
= CreateNamedPipeA(PIPENAME
, PIPE_ACCESS_DUPLEX
, pipemode
| PIPE_WAIT
,
173 /* nMaxInstances */ 1,
174 /* nOutBufSize */ 1024,
175 /* nInBufSize */ 1024,
176 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
177 /* lpSecurityAttrib */ NULL
);
178 ok(hnp
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
180 ret
= WaitNamedPipeA(PIPENAME
, 2000);
181 ok(ret
, "WaitNamedPipe failed (%d)\n", GetLastError());
183 hFile
= CreateFileA(PIPENAME
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0);
184 ok(hFile
!= INVALID_HANDLE_VALUE
, "CreateFile failed (%d)\n", GetLastError());
186 ok(!WaitNamedPipeA(PIPENAME
, 1000), "WaitNamedPipe succeeded\n");
188 ok(GetLastError() == ERROR_SEM_TIMEOUT
, "wrong error %u\n", GetLastError());
190 /* don't try to do i/o if one side couldn't be opened, as it hangs */
191 if (hFile
!= INVALID_HANDLE_VALUE
) {
194 /* Make sure we can read and write a few bytes in both directions */
195 memset(ibuf
, 0, sizeof(ibuf
));
196 ok(WriteFile(hnp
, obuf
, sizeof(obuf
), &written
, NULL
), "WriteFile\n");
197 ok(written
== sizeof(obuf
), "write file len\n");
198 ok(ReadFile(hFile
, ibuf
, sizeof(ibuf
), &readden
, NULL
), "ReadFile\n");
199 ok(readden
== sizeof(obuf
), "read got %d bytes\n", readden
);
200 ok(memcmp(obuf
, ibuf
, written
) == 0, "content check\n");
202 memset(ibuf
, 0, sizeof(ibuf
));
203 ok(WriteFile(hFile
, obuf2
, sizeof(obuf2
), &written
, NULL
), "WriteFile\n");
204 ok(written
== sizeof(obuf2
), "write file len\n");
205 ok(ReadFile(hnp
, ibuf
, sizeof(ibuf
), &readden
, NULL
), "ReadFile\n");
206 ok(readden
== sizeof(obuf2
), "read got %d bytes\n", readden
);
207 ok(memcmp(obuf2
, ibuf
, written
) == 0, "content check\n");
209 /* Now the same again, but with an additional call to PeekNamedPipe */
210 memset(ibuf
, 0, sizeof(ibuf
));
211 ok(WriteFile(hnp
, obuf
, sizeof(obuf
), &written
, NULL
), "WriteFile\n");
212 ok(written
== sizeof(obuf
), "write file len 1\n");
213 ok(PeekNamedPipe(hFile
, NULL
, 0, NULL
, &readden
, NULL
), "Peek\n");
214 ok(readden
== sizeof(obuf
), "peek 1 got %d bytes\n", readden
);
215 ok(ReadFile(hFile
, ibuf
, sizeof(ibuf
), &readden
, NULL
), "ReadFile\n");
216 ok(readden
== sizeof(obuf
), "read 1 got %d bytes\n", readden
);
217 ok(memcmp(obuf
, ibuf
, written
) == 0, "content 1 check\n");
219 memset(ibuf
, 0, sizeof(ibuf
));
220 ok(WriteFile(hFile
, obuf2
, sizeof(obuf2
), &written
, NULL
), "WriteFile\n");
221 ok(written
== sizeof(obuf2
), "write file len 2\n");
222 ok(PeekNamedPipe(hnp
, NULL
, 0, NULL
, &readden
, NULL
), "Peek\n");
223 ok(readden
== sizeof(obuf2
), "peek 2 got %d bytes\n", readden
);
224 ok(PeekNamedPipe(hnp
, (LPVOID
)1, 0, NULL
, &readden
, NULL
), "Peek\n");
225 ok(readden
== sizeof(obuf2
), "peek 2 got %d bytes\n", readden
);
226 ok(ReadFile(hnp
, ibuf
, sizeof(ibuf
), &readden
, NULL
), "ReadFile\n");
227 ok(readden
== sizeof(obuf2
), "read 2 got %d bytes\n", readden
);
228 ok(memcmp(obuf2
, ibuf
, written
) == 0, "content 2 check\n");
230 /* Test how ReadFile behaves when the buffer is not big enough for the whole message */
231 memset(ibuf
, 0, sizeof(ibuf
));
232 ok(WriteFile(hnp
, obuf2
, sizeof(obuf2
), &written
, NULL
), "WriteFile\n");
233 ok(written
== sizeof(obuf2
), "write file len\n");
234 ok(ReadFile(hFile
, ibuf
, 4, &readden
, NULL
), "ReadFile\n");
235 ok(readden
== 4, "read got %d bytes\n", readden
);
236 ok(ReadFile(hFile
, ibuf
+ 4, sizeof(ibuf
) - 4, &readden
, NULL
), "ReadFile\n");
237 ok(readden
== sizeof(obuf2
) - 4, "read got %d bytes\n", readden
);
238 ok(memcmp(obuf2
, ibuf
, written
) == 0, "content check\n");
240 memset(ibuf
, 0, sizeof(ibuf
));
241 ok(WriteFile(hFile
, obuf
, sizeof(obuf
), &written
, NULL
), "WriteFile\n");
242 ok(written
== sizeof(obuf
), "write file len\n");
243 if (pipemode
== PIPE_TYPE_BYTE
)
245 ok(ReadFile(hnp
, ibuf
, 4, &readden
, NULL
), "ReadFile\n");
249 SetLastError(0xdeadbeef);
251 ok(!ReadFile(hnp
, ibuf
, 4, &readden
, NULL
), "ReadFile\n");
253 ok(GetLastError() == ERROR_MORE_DATA
, "wrong error\n");
255 ok(readden
== 4, "read got %d bytes\n", readden
);
256 ok(ReadFile(hnp
, ibuf
+ 4, sizeof(ibuf
) - 4, &readden
, NULL
), "ReadFile\n");
257 ok(readden
== sizeof(obuf
) - 4, "read got %d bytes\n", readden
);
258 ok(memcmp(obuf
, ibuf
, written
) == 0, "content check\n");
260 /* Similar to above, but use a read buffer size small enough to read in three parts */
261 memset(ibuf
, 0, sizeof(ibuf
));
262 ok(WriteFile(hFile
, obuf2
, sizeof(obuf2
), &written
, NULL
), "WriteFile\n");
263 ok(written
== sizeof(obuf2
), "write file len\n");
264 if (pipemode
== PIPE_TYPE_BYTE
)
266 ok(ReadFile(hnp
, ibuf
, 4, &readden
, NULL
), "ReadFile\n");
267 ok(readden
== 4, "read got %d bytes\n", readden
);
268 ok(ReadFile(hnp
, ibuf
+ 4, 4, &readden
, NULL
), "ReadFile\n");
272 SetLastError(0xdeadbeef);
274 ok(!ReadFile(hnp
, ibuf
, 4, &readden
, NULL
), "ReadFile\n");
276 ok(GetLastError() == ERROR_MORE_DATA
, "wrong error\n");
277 ok(readden
== 4, "read got %d bytes\n", readden
);
278 SetLastError(0xdeadbeef);
280 ok(!ReadFile(hnp
, ibuf
+ 4, 4, &readden
, NULL
), "ReadFile\n");
282 ok(GetLastError() == ERROR_MORE_DATA
, "wrong error\n");
284 ok(readden
== 4, "read got %d bytes\n", readden
);
285 ok(ReadFile(hnp
, ibuf
+ 8, sizeof(ibuf
) - 8, &readden
, NULL
), "ReadFile\n");
286 ok(readden
== sizeof(obuf2
) - 8, "read got %d bytes\n", readden
);
287 ok(memcmp(obuf2
, ibuf
, written
) == 0, "content check\n");
289 /* Test reading of multiple writes */
290 memset(ibuf
, 0, sizeof(ibuf
));
291 ok(WriteFile(hnp
, obuf
, sizeof(obuf
), &written
, NULL
), "WriteFile3a\n");
292 ok(written
== sizeof(obuf
), "write file len 3a\n");
293 ok(WriteFile(hnp
, obuf2
, sizeof(obuf2
), &written
, NULL
), " WriteFile3b\n");
294 ok(written
== sizeof(obuf2
), "write file len 3b\n");
295 ok(PeekNamedPipe(hFile
, ibuf
, sizeof(ibuf
), &readden
, &avail
, NULL
), "Peek3\n");
296 if (pipemode
== PIPE_TYPE_BYTE
) {
297 todo_wine
ok(readden
== sizeof(obuf
) + sizeof(obuf2
), "peek3 got %d bytes\n", readden
);
301 ok(readden
== sizeof(obuf
), "peek3 got %d bytes\n", readden
);
303 ok(avail
== sizeof(obuf
) + sizeof(obuf2
), "peek3 got %d bytes available\n", avail
);
305 ok(memcmp(obuf
, pbuf
, sizeof(obuf
)) == 0, "pipe content 3a check\n");
306 if (pipemode
== PIPE_TYPE_BYTE
&& readden
>= sizeof(obuf
)+sizeof(obuf2
)) {
307 pbuf
+= sizeof(obuf
);
308 ok(memcmp(obuf2
, pbuf
, sizeof(obuf2
)) == 0, "pipe content 3b check\n");
310 ok(ReadFile(hFile
, ibuf
, sizeof(ibuf
), &readden
, NULL
), "ReadFile\n");
311 ok(readden
== sizeof(obuf
) + sizeof(obuf2
), "read 3 got %d bytes\n", readden
);
313 ok(memcmp(obuf
, pbuf
, sizeof(obuf
)) == 0, "content 3a check\n");
314 pbuf
+= sizeof(obuf
);
315 ok(memcmp(obuf2
, pbuf
, sizeof(obuf2
)) == 0, "content 3b check\n");
317 /* Multiple writes in the reverse direction */
318 memset(ibuf
, 0, sizeof(ibuf
));
319 ok(WriteFile(hFile
, obuf
, sizeof(obuf
), &written
, NULL
), "WriteFile4a\n");
320 ok(written
== sizeof(obuf
), "write file len 4a\n");
321 ok(WriteFile(hFile
, obuf2
, sizeof(obuf2
), &written
, NULL
), " WriteFile4b\n");
322 ok(written
== sizeof(obuf2
), "write file len 4b\n");
323 ok(PeekNamedPipe(hnp
, ibuf
, sizeof(ibuf
), &readden
, &avail
, NULL
), "Peek4\n");
324 if (pipemode
== PIPE_TYPE_BYTE
) {
325 todo_wine
ok(readden
== sizeof(obuf
) + sizeof(obuf2
), "peek4 got %d bytes\n", readden
);
329 ok(readden
== sizeof(obuf
), "peek4 got %d bytes\n", readden
);
331 ok(avail
== sizeof(obuf
) + sizeof(obuf2
), "peek4 got %d bytes available\n", avail
);
333 ok(memcmp(obuf
, pbuf
, sizeof(obuf
)) == 0, "pipe content 4a check\n");
334 if (pipemode
== PIPE_TYPE_BYTE
&& readden
>= sizeof(obuf
)+sizeof(obuf2
)) {
335 pbuf
+= sizeof(obuf
);
336 ok(memcmp(obuf2
, pbuf
, sizeof(obuf2
)) == 0, "pipe content 4b check\n");
338 ok(ReadFile(hnp
, ibuf
, sizeof(ibuf
), &readden
, NULL
), "ReadFile\n");
339 if (pipemode
== PIPE_TYPE_BYTE
) {
340 ok(readden
== sizeof(obuf
) + sizeof(obuf2
), "read 4 got %d bytes\n", readden
);
344 ok(readden
== sizeof(obuf
), "read 4 got %d bytes\n", readden
);
348 ok(memcmp(obuf
, pbuf
, sizeof(obuf
)) == 0, "content 4a check\n");
349 if (pipemode
== PIPE_TYPE_BYTE
) {
350 pbuf
+= sizeof(obuf
);
351 ok(memcmp(obuf2
, pbuf
, sizeof(obuf2
)) == 0, "content 4b check\n");
354 /* Test reading of multiple writes after a mode change
355 (CreateFile always creates a byte mode pipe) */
356 lpmode
= PIPE_READMODE_MESSAGE
;
357 if (pipemode
== PIPE_TYPE_BYTE
) {
358 /* trying to change the client end of a byte pipe to message mode should fail */
359 ok(!SetNamedPipeHandleState(hFile
, &lpmode
, NULL
, NULL
), "Change mode\n");
362 ok(SetNamedPipeHandleState(hFile
, &lpmode
, NULL
, NULL
), "Change mode\n");
364 memset(ibuf
, 0, sizeof(ibuf
));
365 ok(WriteFile(hnp
, obuf
, sizeof(obuf
), &written
, NULL
), "WriteFile5a\n");
366 ok(written
== sizeof(obuf
), "write file len 3a\n");
367 ok(WriteFile(hnp
, obuf2
, sizeof(obuf2
), &written
, NULL
), " WriteFile5b\n");
368 ok(written
== sizeof(obuf2
), "write file len 3b\n");
369 ok(PeekNamedPipe(hFile
, ibuf
, sizeof(ibuf
), &readden
, &avail
, NULL
), "Peek5\n");
370 ok(readden
== sizeof(obuf
), "peek5 got %d bytes\n", readden
);
371 ok(avail
== sizeof(obuf
) + sizeof(obuf2
), "peek5 got %d bytes available\n", avail
);
373 ok(memcmp(obuf
, pbuf
, sizeof(obuf
)) == 0, "content 5a check\n");
374 ok(ReadFile(hFile
, ibuf
, sizeof(ibuf
), &readden
, NULL
), "ReadFile\n");
376 ok(readden
== sizeof(obuf
), "read 5 got %d bytes\n", readden
);
379 ok(memcmp(obuf
, pbuf
, sizeof(obuf
)) == 0, "content 5a check\n");
380 if (readden
<= sizeof(obuf
))
381 ok(ReadFile(hFile
, ibuf
, sizeof(ibuf
), &readden
, NULL
), "ReadFile\n");
383 /* Multiple writes in the reverse direction */
384 /* the write of obuf2 from write4 should still be in the buffer */
385 ok(PeekNamedPipe(hnp
, ibuf
, sizeof(ibuf
), &readden
, &avail
, NULL
), "Peek6a\n");
387 ok(readden
== sizeof(obuf2
), "peek6a got %d bytes\n", readden
);
388 ok(avail
== sizeof(obuf2
), "peek6a got %d bytes available\n", avail
);
391 ok(ReadFile(hnp
, ibuf
, sizeof(ibuf
), &readden
, NULL
), "ReadFile\n");
392 ok(readden
== sizeof(obuf2
), "read 6a got %d bytes\n", readden
);
394 ok(memcmp(obuf2
, pbuf
, sizeof(obuf2
)) == 0, "content 6a check\n");
396 memset(ibuf
, 0, sizeof(ibuf
));
397 ok(WriteFile(hFile
, obuf
, sizeof(obuf
), &written
, NULL
), "WriteFile6a\n");
398 ok(written
== sizeof(obuf
), "write file len 6a\n");
399 ok(WriteFile(hFile
, obuf2
, sizeof(obuf2
), &written
, NULL
), " WriteFile6b\n");
400 ok(written
== sizeof(obuf2
), "write file len 6b\n");
401 ok(PeekNamedPipe(hnp
, ibuf
, sizeof(ibuf
), &readden
, &avail
, NULL
), "Peek6\n");
402 ok(readden
== sizeof(obuf
), "peek6 got %d bytes\n", readden
);
403 ok(avail
== sizeof(obuf
) + sizeof(obuf2
), "peek6b got %d bytes available\n", avail
);
405 ok(memcmp(obuf
, pbuf
, sizeof(obuf
)) == 0, "content 6a check\n");
406 ok(ReadFile(hnp
, ibuf
, sizeof(ibuf
), &readden
, NULL
), "ReadFile\n");
408 ok(readden
== sizeof(obuf
), "read 6b got %d bytes\n", readden
);
411 ok(memcmp(obuf
, pbuf
, sizeof(obuf
)) == 0, "content 6a check\n");
412 if (readden
<= sizeof(obuf
))
413 ok(ReadFile(hnp
, ibuf
, sizeof(ibuf
), &readden
, NULL
), "ReadFile\n");
415 /* Test how ReadFile behaves when the buffer is not big enough for the whole message */
416 memset(ibuf
, 0, sizeof(ibuf
));
417 ok(WriteFile(hnp
, obuf2
, sizeof(obuf2
), &written
, NULL
), "WriteFile 7\n");
418 ok(written
== sizeof(obuf2
), "write file len 7\n");
419 SetLastError(0xdeadbeef);
421 ok(!ReadFile(hFile
, ibuf
, 4, &readden
, NULL
), "ReadFile 7\n");
423 ok(GetLastError() == ERROR_MORE_DATA
, "wrong error 7\n");
424 ok(readden
== 4, "read got %d bytes 7\n", readden
);
425 ok(ReadFile(hFile
, ibuf
+ 4, sizeof(ibuf
) - 4, &readden
, NULL
), "ReadFile 7\n");
426 ok(readden
== sizeof(obuf2
) - 4, "read got %d bytes 7\n", readden
);
427 ok(memcmp(obuf2
, ibuf
, written
) == 0, "content check 7\n");
429 memset(ibuf
, 0, sizeof(ibuf
));
430 ok(WriteFile(hFile
, obuf
, sizeof(obuf
), &written
, NULL
), "WriteFile 8\n");
431 ok(written
== sizeof(obuf
), "write file len 8\n");
432 SetLastError(0xdeadbeef);
434 ok(!ReadFile(hnp
, ibuf
, 4, &readden
, NULL
), "ReadFile 8\n");
436 ok(GetLastError() == ERROR_MORE_DATA
, "wrong error 8\n");
437 ok(readden
== 4, "read got %d bytes 8\n", readden
);
438 ok(ReadFile(hnp
, ibuf
+ 4, sizeof(ibuf
) - 4, &readden
, NULL
), "ReadFile 8\n");
439 ok(readden
== sizeof(obuf
) - 4, "read got %d bytes 8\n", readden
);
440 ok(memcmp(obuf
, ibuf
, written
) == 0, "content check 8\n");
442 /* The following test shows that when doing a partial read of a message, the rest
443 * is still in the pipe, and can be received from a second thread. This shows
444 * especially that the content is _not_ stored in thread-local-storage until it is
445 * completely transmitted. The same method works even across multiple processes. */
446 memset(ibuf
, 0, sizeof(ibuf
));
447 ok(WriteFile(hnp
, obuf
, sizeof(obuf
), &written
, NULL
), "WriteFile 9\n");
448 ok(written
== sizeof(obuf
), "write file len 9\n");
449 ok(WriteFile(hnp
, obuf2
, sizeof(obuf2
), &written
, NULL
), "WriteFile 9\n");
450 ok(written
== sizeof(obuf2
), "write file len 9\n");
451 SetLastError(0xdeadbeef);
453 ok(!ReadFile(hFile
, ibuf
, 4, &readden
, NULL
), "ReadFile 9\n");
455 ok(GetLastError() == ERROR_MORE_DATA
, "wrong error 9\n");
456 ok(readden
== 4, "read got %d bytes 9\n", readden
);
457 SetLastError(0xdeadbeef);
458 ret
= RpcReadFile(hFile
, ibuf
+ 4, 4, &readden
, NULL
);
460 ok(!ret
, "RpcReadFile 9\n");
462 ok(GetLastError() == ERROR_MORE_DATA
, "wrong error 9\n");
463 ok(readden
== 4, "read got %d bytes 9\n", readden
);
464 ret
= RpcReadFile(hFile
, ibuf
+ 8, sizeof(ibuf
), &readden
, NULL
);
465 ok(ret
, "RpcReadFile 9\n");
467 ok(readden
== sizeof(obuf
) - 8, "read got %d bytes 9\n", readden
);
468 ok(memcmp(obuf
, ibuf
, sizeof(obuf
)) == 0, "content check 9\n");
469 if (readden
<= sizeof(obuf
) - 8) /* blocks forever if second part was already received */
471 memset(ibuf
, 0, sizeof(ibuf
));
472 SetLastError(0xdeadbeef);
473 ret
= RpcReadFile(hFile
, ibuf
, 4, &readden
, NULL
);
474 ok(!ret
, "RpcReadFile 9\n");
476 ok(GetLastError() == ERROR_MORE_DATA
, "wrong error 9\n");
477 ok(readden
== 4, "read got %d bytes 9\n", readden
);
478 SetLastError(0xdeadbeef);
480 ok(!ReadFile(hFile
, ibuf
+ 4, 4, &readden
, NULL
), "ReadFile 9\n");
482 ok(GetLastError() == ERROR_MORE_DATA
, "wrong error 9\n");
483 ok(readden
== 4, "read got %d bytes 9\n", readden
);
484 ret
= RpcReadFile(hFile
, ibuf
+ 8, sizeof(ibuf
), &readden
, NULL
);
485 ok(ret
, "RpcReadFile 9\n");
486 ok(readden
== sizeof(obuf2
) - 8, "read got %d bytes 9\n", readden
);
487 ok(memcmp(obuf2
, ibuf
, sizeof(obuf2
)) == 0, "content check 9\n");
490 /* Now the reverse direction */
491 memset(ibuf
, 0, sizeof(ibuf
));
492 ok(WriteFile(hFile
, obuf2
, sizeof(obuf2
), &written
, NULL
), "WriteFile 10\n");
493 ok(written
== sizeof(obuf2
), "write file len 10\n");
494 ok(WriteFile(hFile
, obuf
, sizeof(obuf
), &written
, NULL
), "WriteFile 10\n");
495 ok(written
== sizeof(obuf
), "write file len 10\n");
496 SetLastError(0xdeadbeef);
498 ok(!ReadFile(hnp
, ibuf
, 4, &readden
, NULL
), "ReadFile 10\n");
500 ok(GetLastError() == ERROR_MORE_DATA
, "wrong error 10\n");
501 ok(readden
== 4, "read got %d bytes 10\n", readden
);
502 SetLastError(0xdeadbeef);
503 ret
= RpcReadFile(hnp
, ibuf
+ 4, 4, &readden
, NULL
);
505 ok(!ret
, "RpcReadFile 10\n");
507 ok(GetLastError() == ERROR_MORE_DATA
, "wrong error 10\n");
508 ok(readden
== 4, "read got %d bytes 10\n", readden
);
509 ret
= RpcReadFile(hnp
, ibuf
+ 8, sizeof(ibuf
), &readden
, NULL
);
510 ok(ret
, "RpcReadFile 10\n");
512 ok(readden
== sizeof(obuf2
) - 8, "read got %d bytes 10\n", readden
);
513 ok(memcmp(obuf2
, ibuf
, sizeof(obuf2
)) == 0, "content check 10\n");
514 if (readden
<= sizeof(obuf2
) - 8) /* blocks forever if second part was already received */
516 memset(ibuf
, 0, sizeof(ibuf
));
517 SetLastError(0xdeadbeef);
518 ret
= RpcReadFile(hnp
, ibuf
, 4, &readden
, NULL
);
519 ok(!ret
, "RpcReadFile 10\n");
521 ok(GetLastError() == ERROR_MORE_DATA
, "wrong error 10\n");
522 ok(readden
== 4, "read got %d bytes 10\n", readden
);
523 SetLastError(0xdeadbeef);
525 ok(!ReadFile(hnp
, ibuf
+ 4, 4, &readden
, NULL
), "ReadFile 10\n");
527 ok(GetLastError() == ERROR_MORE_DATA
, "wrong error 10\n");
528 ok(readden
== 4, "read got %d bytes 10\n", readden
);
529 ret
= RpcReadFile(hnp
, ibuf
+ 8, sizeof(ibuf
), &readden
, NULL
);
530 ok(ret
, "RpcReadFile 10\n");
531 ok(readden
== sizeof(obuf
) - 8, "read got %d bytes 10\n", readden
);
532 ok(memcmp(obuf
, ibuf
, sizeof(obuf
)) == 0, "content check 10\n");
537 /* Picky conformance tests */
539 /* Verify that you can't connect to pipe again
540 * until server calls DisconnectNamedPipe+ConnectNamedPipe
541 * or creates a new pipe
542 * case 1: other client not yet closed
544 hFile2
= CreateFileA(PIPENAME
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0);
545 ok(hFile2
== INVALID_HANDLE_VALUE
,
546 "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail\n");
547 ok(GetLastError() == ERROR_PIPE_BUSY
,
548 "connecting to named pipe before other client closes should fail with ERROR_PIPE_BUSY\n");
550 ok(CloseHandle(hFile
), "CloseHandle\n");
552 /* case 2: other client already closed */
553 hFile
= CreateFileA(PIPENAME
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0);
554 ok(hFile
== INVALID_HANDLE_VALUE
,
555 "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail\n");
556 ok(GetLastError() == ERROR_PIPE_BUSY
,
557 "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail with ERROR_PIPE_BUSY\n");
559 ok(DisconnectNamedPipe(hnp
), "DisconnectNamedPipe\n");
561 /* case 3: server has called DisconnectNamedPipe but not ConnectNamed Pipe */
562 hFile
= CreateFileA(PIPENAME
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0);
563 ok(hFile
== INVALID_HANDLE_VALUE
,
564 "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail\n");
565 ok(GetLastError() == ERROR_PIPE_BUSY
,
566 "connecting to named pipe after other client closes but before ConnectNamedPipe should fail with ERROR_PIPE_BUSY\n");
568 /* to be complete, we'd call ConnectNamedPipe here and loop,
569 * but by default that's blocking, so we'd either have
570 * to turn on the uncommon nonblocking mode, or
571 * use another thread.
575 ok(CloseHandle(hnp
), "CloseHandle\n");
577 trace("test_CreateNamedPipe returning\n");
580 static void test_CreateNamedPipe_instances_must_match(void)
584 /* Check no mismatch */
585 hnp
= CreateNamedPipeA(PIPENAME
, PIPE_ACCESS_DUPLEX
, PIPE_TYPE_BYTE
| PIPE_WAIT
,
586 /* nMaxInstances */ 2,
587 /* nOutBufSize */ 1024,
588 /* nInBufSize */ 1024,
589 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
590 /* lpSecurityAttrib */ NULL
);
591 ok(hnp
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
593 hnp2
= CreateNamedPipeA(PIPENAME
, PIPE_ACCESS_DUPLEX
, PIPE_TYPE_BYTE
| PIPE_WAIT
,
594 /* nMaxInstances */ 2,
595 /* nOutBufSize */ 1024,
596 /* nInBufSize */ 1024,
597 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
598 /* lpSecurityAttrib */ NULL
);
599 ok(hnp2
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
601 ok(CloseHandle(hnp
), "CloseHandle\n");
602 ok(CloseHandle(hnp2
), "CloseHandle\n");
604 /* Check nMaxInstances */
605 hnp
= CreateNamedPipeA(PIPENAME
, PIPE_ACCESS_DUPLEX
, PIPE_TYPE_BYTE
| PIPE_WAIT
,
606 /* nMaxInstances */ 1,
607 /* nOutBufSize */ 1024,
608 /* nInBufSize */ 1024,
609 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
610 /* lpSecurityAttrib */ NULL
);
611 ok(hnp
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
613 hnp2
= CreateNamedPipeA(PIPENAME
, PIPE_ACCESS_DUPLEX
, PIPE_TYPE_BYTE
| PIPE_WAIT
,
614 /* nMaxInstances */ 1,
615 /* nOutBufSize */ 1024,
616 /* nInBufSize */ 1024,
617 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
618 /* lpSecurityAttrib */ NULL
);
619 ok(hnp2
== INVALID_HANDLE_VALUE
620 && GetLastError() == ERROR_PIPE_BUSY
, "nMaxInstances not obeyed\n");
622 ok(CloseHandle(hnp
), "CloseHandle\n");
624 /* Check PIPE_ACCESS_* */
625 hnp
= CreateNamedPipeA(PIPENAME
, PIPE_ACCESS_DUPLEX
, PIPE_TYPE_BYTE
| PIPE_WAIT
,
626 /* nMaxInstances */ 2,
627 /* nOutBufSize */ 1024,
628 /* nInBufSize */ 1024,
629 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
630 /* lpSecurityAttrib */ NULL
);
631 ok(hnp
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
633 hnp2
= CreateNamedPipeA(PIPENAME
, PIPE_ACCESS_INBOUND
, PIPE_TYPE_BYTE
| PIPE_WAIT
,
634 /* nMaxInstances */ 2,
635 /* nOutBufSize */ 1024,
636 /* nInBufSize */ 1024,
637 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
638 /* lpSecurityAttrib */ NULL
);
639 ok(hnp2
== INVALID_HANDLE_VALUE
640 && GetLastError() == ERROR_ACCESS_DENIED
, "PIPE_ACCESS_* mismatch allowed\n");
642 ok(CloseHandle(hnp
), "CloseHandle\n");
644 /* check everything else */
645 hnp
= CreateNamedPipeA(PIPENAME
, PIPE_ACCESS_DUPLEX
, PIPE_TYPE_BYTE
| PIPE_WAIT
,
646 /* nMaxInstances */ 4,
647 /* nOutBufSize */ 1024,
648 /* nInBufSize */ 1024,
649 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
650 /* lpSecurityAttrib */ NULL
);
651 ok(hnp
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
653 hnp2
= CreateNamedPipeA(PIPENAME
, PIPE_ACCESS_DUPLEX
, PIPE_TYPE_MESSAGE
,
654 /* nMaxInstances */ 3,
655 /* nOutBufSize */ 102,
657 /* nDefaultWait */ 1234,
658 /* lpSecurityAttrib */ NULL
);
659 ok(hnp2
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
661 ok(CloseHandle(hnp
), "CloseHandle\n");
662 ok(CloseHandle(hnp2
), "CloseHandle\n");
665 /** implementation of alarm() */
666 static DWORD CALLBACK
alarmThreadMain(LPVOID arg
)
668 DWORD_PTR timeout
= (DWORD_PTR
) arg
;
669 trace("alarmThreadMain\n");
670 if (WaitForSingleObject( alarm_event
, timeout
) == WAIT_TIMEOUT
)
672 ok(FALSE
, "alarm\n");
678 static HANDLE hnp
= INVALID_HANDLE_VALUE
;
680 /** Trivial byte echo server - disconnects after each session */
681 static DWORD CALLBACK
serverThreadMain1(LPVOID arg
)
685 trace("serverThreadMain1 start\n");
686 /* Set up a simple echo server */
687 hnp
= CreateNamedPipeA(PIPENAME
"serverThreadMain1", PIPE_ACCESS_DUPLEX
,
688 PIPE_TYPE_BYTE
| PIPE_WAIT
,
689 /* nMaxInstances */ 1,
690 /* nOutBufSize */ 1024,
691 /* nInBufSize */ 1024,
692 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
693 /* lpSecurityAttrib */ NULL
);
695 ok(hnp
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
696 for (i
= 0; i
< NB_SERVER_LOOPS
; i
++) {
702 /* Wait for client to connect */
703 trace("Server calling ConnectNamedPipe...\n");
704 ok(ConnectNamedPipe(hnp
, NULL
)
705 || GetLastError() == ERROR_PIPE_CONNECTED
, "ConnectNamedPipe\n");
706 trace("ConnectNamedPipe returned.\n");
708 /* Echo bytes once */
709 memset(buf
, 0, sizeof(buf
));
711 trace("Server reading...\n");
712 success
= ReadFile(hnp
, buf
, sizeof(buf
), &readden
, NULL
);
713 trace("Server done reading.\n");
714 ok(success
, "ReadFile\n");
715 ok(readden
, "short read\n");
717 trace("Server writing...\n");
718 ok(WriteFile(hnp
, buf
, readden
, &written
, NULL
), "WriteFile\n");
719 trace("Server done writing.\n");
720 ok(written
== readden
, "write file len\n");
722 /* finish this connection, wait for next one */
723 ok(FlushFileBuffers(hnp
), "FlushFileBuffers\n");
724 trace("Server done flushing.\n");
725 ok(DisconnectNamedPipe(hnp
), "DisconnectNamedPipe\n");
726 trace("Server done disconnecting.\n");
731 /** Trivial byte echo server - closes after each connection */
732 static DWORD CALLBACK
serverThreadMain2(LPVOID arg
)
737 trace("serverThreadMain2\n");
738 /* Set up a simple echo server */
739 hnp
= CreateNamedPipeA(PIPENAME
"serverThreadMain2", PIPE_ACCESS_DUPLEX
,
740 PIPE_TYPE_BYTE
| PIPE_WAIT
,
741 /* nMaxInstances */ 2,
742 /* nOutBufSize */ 1024,
743 /* nInBufSize */ 1024,
744 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
745 /* lpSecurityAttrib */ NULL
);
746 ok(hnp
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
748 for (i
= 0; i
< NB_SERVER_LOOPS
; i
++) {
756 user_apc_ran
= FALSE
;
757 if (i
== 0 && pQueueUserAPC
) {
758 trace("Queueing an user APC\n"); /* verify the pipe is non alerable */
759 ret
= pQueueUserAPC(&user_apc
, GetCurrentThread(), 0);
760 ok(ret
, "QueueUserAPC failed: %d\n", GetLastError());
763 /* Wait for client to connect */
764 trace("Server calling ConnectNamedPipe...\n");
765 ok(ConnectNamedPipe(hnp
, NULL
)
766 || GetLastError() == ERROR_PIPE_CONNECTED
, "ConnectNamedPipe\n");
767 trace("ConnectNamedPipe returned.\n");
769 /* Echo bytes once */
770 memset(buf
, 0, sizeof(buf
));
772 trace("Server reading...\n");
773 success
= ReadFile(hnp
, buf
, sizeof(buf
), &readden
, NULL
);
774 trace("Server done reading.\n");
775 ok(success
, "ReadFile\n");
777 trace("Server writing...\n");
778 ok(WriteFile(hnp
, buf
, readden
, &written
, NULL
), "WriteFile\n");
779 trace("Server done writing.\n");
780 ok(written
== readden
, "write file len\n");
782 /* finish this connection, wait for next one */
783 ok(FlushFileBuffers(hnp
), "FlushFileBuffers\n");
784 ok(DisconnectNamedPipe(hnp
), "DisconnectNamedPipe\n");
786 ok(user_apc_ran
== FALSE
, "UserAPC ran, pipe using alertable io mode\n");
788 if (i
== 0 && pQueueUserAPC
)
789 SleepEx(0, TRUE
); /* get rid of apc */
791 /* Set up next echo server */
793 CreateNamedPipeA(PIPENAME
"serverThreadMain2", PIPE_ACCESS_DUPLEX
,
794 PIPE_TYPE_BYTE
| PIPE_WAIT
,
795 /* nMaxInstances */ 2,
796 /* nOutBufSize */ 1024,
797 /* nInBufSize */ 1024,
798 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
799 /* lpSecurityAttrib */ NULL
);
801 ok(hnpNext
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
803 ok(CloseHandle(hnp
), "CloseHandle\n");
809 /** Trivial byte echo server - uses overlapped named pipe calls */
810 static DWORD CALLBACK
serverThreadMain3(LPVOID arg
)
815 trace("serverThreadMain3\n");
816 /* Set up a simple echo server */
817 hnp
= CreateNamedPipeA(PIPENAME
"serverThreadMain3", PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
818 PIPE_TYPE_BYTE
| PIPE_WAIT
,
819 /* nMaxInstances */ 1,
820 /* nOutBufSize */ 1024,
821 /* nInBufSize */ 1024,
822 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
823 /* lpSecurityAttrib */ NULL
);
824 ok(hnp
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
826 hEvent
= CreateEventW(NULL
, /* security attribute */
827 TRUE
, /* manual reset event */
828 FALSE
, /* initial state */
830 ok(hEvent
!= NULL
, "CreateEvent\n");
832 for (i
= 0; i
< NB_SERVER_LOOPS
; i
++) {
839 int letWFSOEwait
= (i
& 2);
840 int letGORwait
= (i
& 1);
843 memset(&oOverlap
, 0, sizeof(oOverlap
));
844 oOverlap
.hEvent
= hEvent
;
846 /* Wait for client to connect */
848 trace("Server calling non-overlapped ConnectNamedPipe on overlapped pipe...\n");
849 success
= ConnectNamedPipe(hnp
, NULL
);
850 err
= GetLastError();
851 ok(success
|| (err
== ERROR_PIPE_CONNECTED
), "ConnectNamedPipe failed: %d\n", err
);
852 trace("ConnectNamedPipe operation complete.\n");
854 trace("Server calling overlapped ConnectNamedPipe...\n");
855 success
= ConnectNamedPipe(hnp
, &oOverlap
);
856 err
= GetLastError();
857 ok(!success
&& (err
== ERROR_IO_PENDING
|| err
== ERROR_PIPE_CONNECTED
), "overlapped ConnectNamedPipe\n");
858 trace("overlapped ConnectNamedPipe returned.\n");
859 if (!success
&& (err
== ERROR_IO_PENDING
)) {
864 ret
= WaitForSingleObjectEx(hEvent
, INFINITE
, TRUE
);
865 } while (ret
== WAIT_IO_COMPLETION
);
866 ok(ret
== 0, "wait ConnectNamedPipe returned %x\n", ret
);
868 success
= GetOverlappedResult(hnp
, &oOverlap
, &dummy
, letGORwait
);
869 if (!letGORwait
&& !letWFSOEwait
&& !success
) {
870 ok(GetLastError() == ERROR_IO_INCOMPLETE
, "GetOverlappedResult\n");
871 success
= GetOverlappedResult(hnp
, &oOverlap
, &dummy
, TRUE
);
874 ok(success
|| (err
== ERROR_PIPE_CONNECTED
), "GetOverlappedResult ConnectNamedPipe\n");
875 trace("overlapped ConnectNamedPipe operation complete.\n");
878 /* Echo bytes once */
879 memset(buf
, 0, sizeof(buf
));
881 trace("Server reading...\n");
882 success
= ReadFile(hnp
, buf
, sizeof(buf
), &readden
, &oOverlap
);
883 trace("Server ReadFile returned...\n");
884 err
= GetLastError();
885 ok(success
|| err
== ERROR_IO_PENDING
, "overlapped ReadFile\n");
886 trace("overlapped ReadFile returned.\n");
887 if (!success
&& (err
== ERROR_IO_PENDING
)) {
892 ret
= WaitForSingleObjectEx(hEvent
, INFINITE
, TRUE
);
893 } while (ret
== WAIT_IO_COMPLETION
);
894 ok(ret
== 0, "wait ReadFile returned %x\n", ret
);
896 success
= GetOverlappedResult(hnp
, &oOverlap
, &readden
, letGORwait
);
897 if (!letGORwait
&& !letWFSOEwait
&& !success
) {
898 ok(GetLastError() == ERROR_IO_INCOMPLETE
, "GetOverlappedResult\n");
899 success
= GetOverlappedResult(hnp
, &oOverlap
, &readden
, TRUE
);
902 trace("Server done reading.\n");
903 ok(success
, "overlapped ReadFile\n");
905 trace("Server writing...\n");
906 success
= WriteFile(hnp
, buf
, readden
, &written
, &oOverlap
);
907 trace("Server WriteFile returned...\n");
908 err
= GetLastError();
909 ok(success
|| err
== ERROR_IO_PENDING
, "overlapped WriteFile\n");
910 trace("overlapped WriteFile returned.\n");
911 if (!success
&& (err
== ERROR_IO_PENDING
)) {
916 ret
= WaitForSingleObjectEx(hEvent
, INFINITE
, TRUE
);
917 } while (ret
== WAIT_IO_COMPLETION
);
918 ok(ret
== 0, "wait WriteFile returned %x\n", ret
);
920 success
= GetOverlappedResult(hnp
, &oOverlap
, &written
, letGORwait
);
921 if (!letGORwait
&& !letWFSOEwait
&& !success
) {
922 ok(GetLastError() == ERROR_IO_INCOMPLETE
, "GetOverlappedResult\n");
923 success
= GetOverlappedResult(hnp
, &oOverlap
, &written
, TRUE
);
926 trace("Server done writing.\n");
927 ok(success
, "overlapped WriteFile\n");
928 ok(written
== readden
, "write file len\n");
930 /* finish this connection, wait for next one */
931 ok(FlushFileBuffers(hnp
), "FlushFileBuffers\n");
932 ok(DisconnectNamedPipe(hnp
), "DisconnectNamedPipe\n");
937 /** Trivial byte echo server - uses i/o completion ports */
938 static DWORD CALLBACK
serverThreadMain4(LPVOID arg
)
944 trace("serverThreadMain4\n");
945 /* Set up a simple echo server */
946 hnp
= CreateNamedPipeA(PIPENAME
"serverThreadMain4", PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
947 PIPE_TYPE_BYTE
| PIPE_WAIT
,
948 /* nMaxInstances */ 1,
949 /* nOutBufSize */ 1024,
950 /* nInBufSize */ 1024,
951 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
952 /* lpSecurityAttrib */ NULL
);
953 ok(hnp
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
955 hcompletion
= CreateIoCompletionPort(hnp
, NULL
, 12345, 1);
956 ok(hcompletion
!= NULL
, "CreateIoCompletionPort failed, error=%i\n", GetLastError());
958 for (i
= 0; i
< NB_SERVER_LOOPS
; i
++) {
971 memset(&oConnect
, 0, sizeof(oConnect
));
972 memset(&oRead
, 0, sizeof(oRead
));
973 memset(&oWrite
, 0, sizeof(oWrite
));
975 /* Wait for client to connect */
976 trace("Server calling overlapped ConnectNamedPipe...\n");
977 success
= ConnectNamedPipe(hnp
, &oConnect
);
978 err
= GetLastError();
979 ok(!success
&& (err
== ERROR_IO_PENDING
|| err
== ERROR_PIPE_CONNECTED
),
980 "overlapped ConnectNamedPipe got %u err %u\n", success
, err
);
981 if (!success
&& err
== ERROR_IO_PENDING
) {
982 trace("ConnectNamedPipe GetQueuedCompletionStatus\n");
983 success
= GetQueuedCompletionStatus(hcompletion
, &dummy
, &compkey
, &oResult
, 0);
986 ok( GetLastError() == WAIT_TIMEOUT
,
987 "ConnectNamedPipe GetQueuedCompletionStatus wrong error %u\n", GetLastError());
988 success
= GetQueuedCompletionStatus(hcompletion
, &dummy
, &compkey
, &oResult
, 10000);
990 ok(success
, "ConnectNamedPipe GetQueuedCompletionStatus failed, errno=%i\n", GetLastError());
993 ok(compkey
== 12345, "got completion key %i instead of 12345\n", (int)compkey
);
994 ok(oResult
== &oConnect
, "got overlapped pointer %p instead of %p\n", oResult
, &oConnect
);
997 trace("overlapped ConnectNamedPipe operation complete.\n");
999 /* Echo bytes once */
1000 memset(buf
, 0, sizeof(buf
));
1002 trace("Server reading...\n");
1003 success
= ReadFile(hnp
, buf
, sizeof(buf
), &readden
, &oRead
);
1004 trace("Server ReadFile returned...\n");
1005 err
= GetLastError();
1006 ok(success
|| err
== ERROR_IO_PENDING
, "overlapped ReadFile, err=%i\n", err
);
1007 success
= GetQueuedCompletionStatus(hcompletion
, &readden
, &compkey
,
1009 ok(success
, "ReadFile GetQueuedCompletionStatus failed, errno=%i\n", GetLastError());
1012 ok(compkey
== 12345, "got completion key %i instead of 12345\n", (int)compkey
);
1013 ok(oResult
== &oRead
, "got overlapped pointer %p instead of %p\n", oResult
, &oRead
);
1015 trace("Server done reading.\n");
1017 trace("Server writing...\n");
1018 success
= WriteFile(hnp
, buf
, readden
, &written
, &oWrite
);
1019 trace("Server WriteFile returned...\n");
1020 err
= GetLastError();
1021 ok(success
|| err
== ERROR_IO_PENDING
, "overlapped WriteFile failed, err=%u\n", err
);
1022 success
= GetQueuedCompletionStatus(hcompletion
, &written
, &compkey
,
1024 ok(success
, "WriteFile GetQueuedCompletionStatus failed, errno=%i\n", GetLastError());
1027 ok(compkey
== 12345, "got completion key %i instead of 12345\n", (int)compkey
);
1028 ok(oResult
== &oWrite
, "got overlapped pointer %p instead of %p\n", oResult
, &oWrite
);
1029 ok(written
== readden
, "write file len\n");
1031 trace("Server done writing.\n");
1033 /* finish this connection, wait for next one */
1034 ok(FlushFileBuffers(hnp
), "FlushFileBuffers\n");
1035 success
= DisconnectNamedPipe(hnp
);
1036 ok(success
, "DisconnectNamedPipe failed, err %u\n", GetLastError());
1039 ret
= CloseHandle(hnp
);
1040 ok(ret
, "CloseHandle named pipe failed, err=%i\n", GetLastError());
1041 ret
= CloseHandle(hcompletion
);
1042 ok(ret
, "CloseHandle completion failed, err=%i\n", GetLastError());
1047 static int completion_called
;
1048 static DWORD completion_errorcode
;
1049 static DWORD completion_num_bytes
;
1050 static LPOVERLAPPED completion_lpoverlapped
;
1052 static VOID WINAPI
completion_routine(DWORD errorcode
, DWORD num_bytes
, LPOVERLAPPED lpoverlapped
)
1054 completion_called
++;
1055 completion_errorcode
= errorcode
;
1056 completion_num_bytes
= num_bytes
;
1057 completion_lpoverlapped
= lpoverlapped
;
1058 SetEvent(lpoverlapped
->hEvent
);
1061 /** Trivial byte echo server - uses ReadFileEx/WriteFileEx */
1062 static DWORD CALLBACK
serverThreadMain5(LPVOID arg
)
1067 trace("serverThreadMain5\n");
1068 /* Set up a simple echo server */
1069 hnp
= CreateNamedPipeA(PIPENAME
"serverThreadMain5", PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1070 PIPE_TYPE_BYTE
| PIPE_WAIT
,
1071 /* nMaxInstances */ 1,
1072 /* nOutBufSize */ 1024,
1073 /* nInBufSize */ 1024,
1074 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
1075 /* lpSecurityAttrib */ NULL
);
1076 ok(hnp
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
1078 hEvent
= CreateEventW(NULL
, /* security attribute */
1079 TRUE
, /* manual reset event */
1080 FALSE
, /* initial state */
1082 ok(hEvent
!= NULL
, "CreateEvent\n");
1084 for (i
= 0; i
< NB_SERVER_LOOPS
; i
++) {
1088 OVERLAPPED oOverlap
;
1091 memset(&oOverlap
, 0, sizeof(oOverlap
));
1092 oOverlap
.hEvent
= hEvent
;
1094 /* Wait for client to connect */
1095 trace("Server calling ConnectNamedPipe...\n");
1096 success
= ConnectNamedPipe(hnp
, NULL
);
1097 err
= GetLastError();
1098 ok(success
|| (err
== ERROR_PIPE_CONNECTED
), "ConnectNamedPipe failed: %d\n", err
);
1099 trace("ConnectNamedPipe operation complete.\n");
1101 /* Echo bytes once */
1102 memset(buf
, 0, sizeof(buf
));
1104 trace("Server reading...\n");
1105 completion_called
= 0;
1107 success
= ReadFileEx(hnp
, buf
, sizeof(buf
), &oOverlap
, completion_routine
);
1108 trace("Server ReadFileEx returned...\n");
1109 ok(success
, "ReadFileEx failed, err=%i\n", GetLastError());
1110 ok(completion_called
== 0, "completion routine called before ReadFileEx return\n");
1111 trace("ReadFileEx returned.\n");
1115 ret
= WaitForSingleObjectEx(hEvent
, INFINITE
, TRUE
);
1116 } while (ret
== WAIT_IO_COMPLETION
);
1117 ok(ret
== 0, "wait ReadFileEx returned %x\n", ret
);
1119 ok(completion_called
== 1, "completion routine called %i times\n", completion_called
);
1120 ok(completion_errorcode
== ERROR_SUCCESS
, "completion routine got error %d\n", completion_errorcode
);
1121 ok(completion_num_bytes
!= 0, "read 0 bytes\n");
1122 ok(completion_lpoverlapped
== &oOverlap
, "got wrong overlapped pointer %p\n", completion_lpoverlapped
);
1123 readden
= completion_num_bytes
;
1124 trace("Server done reading.\n");
1126 trace("Server writing...\n");
1127 completion_called
= 0;
1129 success
= WriteFileEx(hnp
, buf
, readden
, &oOverlap
, completion_routine
);
1130 trace("Server WriteFileEx returned...\n");
1131 ok(success
, "WriteFileEx failed, err=%i\n", GetLastError());
1132 ok(completion_called
== 0, "completion routine called before ReadFileEx return\n");
1133 trace("overlapped WriteFile returned.\n");
1137 ret
= WaitForSingleObjectEx(hEvent
, INFINITE
, TRUE
);
1138 } while (ret
== WAIT_IO_COMPLETION
);
1139 ok(ret
== 0, "wait WriteFileEx returned %x\n", ret
);
1141 trace("Server done writing.\n");
1142 ok(completion_called
== 1, "completion routine called %i times\n", completion_called
);
1143 ok(completion_errorcode
== ERROR_SUCCESS
, "completion routine got error %d\n", completion_errorcode
);
1144 ok(completion_num_bytes
== readden
, "read %i bytes wrote %i\n", readden
, completion_num_bytes
);
1145 ok(completion_lpoverlapped
== &oOverlap
, "got wrong overlapped pointer %p\n", completion_lpoverlapped
);
1147 /* finish this connection, wait for next one */
1148 ok(FlushFileBuffers(hnp
), "FlushFileBuffers\n");
1149 ok(DisconnectNamedPipe(hnp
), "DisconnectNamedPipe\n");
1154 static void exercizeServer(const char *pipename
, HANDLE serverThread
)
1158 trace("exercizeServer starting\n");
1159 for (i
= 0; i
< NB_SERVER_LOOPS
; i
++) {
1160 HANDLE hFile
=INVALID_HANDLE_VALUE
;
1161 static const char obuf
[] = "Bit Bucket";
1167 for (loop
= 0; loop
< 3; loop
++) {
1169 trace("Client connecting...\n");
1170 /* Connect to the server */
1171 hFile
= CreateFileA(pipename
, GENERIC_READ
| GENERIC_WRITE
, 0,
1172 NULL
, OPEN_EXISTING
, 0, 0);
1173 if (hFile
!= INVALID_HANDLE_VALUE
)
1175 err
= GetLastError();
1177 ok(err
== ERROR_PIPE_BUSY
|| err
== ERROR_FILE_NOT_FOUND
, "connecting to pipe\n");
1179 ok(err
== ERROR_PIPE_BUSY
, "connecting to pipe\n");
1180 trace("connect failed, retrying\n");
1183 ok(hFile
!= INVALID_HANDLE_VALUE
, "client opening named pipe\n");
1185 /* Make sure it can echo */
1186 memset(ibuf
, 0, sizeof(ibuf
));
1187 trace("Client writing...\n");
1188 ok(WriteFile(hFile
, obuf
, sizeof(obuf
), &written
, NULL
), "WriteFile to client end of pipe\n");
1189 ok(written
== sizeof(obuf
), "write file len\n");
1190 trace("Client reading...\n");
1191 ok(ReadFile(hFile
, ibuf
, sizeof(obuf
), &readden
, NULL
), "ReadFile from client end of pipe\n");
1192 ok(readden
== sizeof(obuf
), "read file len\n");
1193 ok(memcmp(obuf
, ibuf
, written
) == 0, "content check\n");
1195 trace("Client closing...\n");
1196 ok(CloseHandle(hFile
), "CloseHandle\n");
1199 ok(WaitForSingleObject(serverThread
,INFINITE
) == WAIT_OBJECT_0
, "WaitForSingleObject\n");
1201 trace("exercizeServer returning\n");
1204 static void test_NamedPipe_2(void)
1206 HANDLE serverThread
;
1207 DWORD serverThreadId
;
1209 DWORD alarmThreadId
;
1211 trace("test_NamedPipe_2 starting\n");
1212 /* Set up a twenty second timeout */
1213 alarm_event
= CreateEventW( NULL
, TRUE
, FALSE
, NULL
);
1214 SetLastError(0xdeadbeef);
1215 alarmThread
= CreateThread(NULL
, 0, alarmThreadMain
, (void *) 20000, 0, &alarmThreadId
);
1216 ok(alarmThread
!= NULL
, "CreateThread failed: %d\n", GetLastError());
1218 /* The servers we're about to exercise do try to clean up carefully,
1219 * but to reduce the chance of a test failure due to a pipe handle
1220 * leak in the test code, we'll use a different pipe name for each server.
1224 SetLastError(0xdeadbeef);
1225 serverThread
= CreateThread(NULL
, 0, serverThreadMain1
, (void *)8, 0, &serverThreadId
);
1226 ok(serverThread
!= NULL
, "CreateThread failed: %d\n", GetLastError());
1227 exercizeServer(PIPENAME
"serverThreadMain1", serverThread
);
1230 SetLastError(0xdeadbeef);
1231 serverThread
= CreateThread(NULL
, 0, serverThreadMain2
, 0, 0, &serverThreadId
);
1232 ok(serverThread
!= NULL
, "CreateThread failed: %d\n", GetLastError());
1233 exercizeServer(PIPENAME
"serverThreadMain2", serverThread
);
1236 SetLastError(0xdeadbeef);
1237 serverThread
= CreateThread(NULL
, 0, serverThreadMain3
, 0, 0, &serverThreadId
);
1238 ok(serverThread
!= NULL
, "CreateThread failed: %d\n", GetLastError());
1239 exercizeServer(PIPENAME
"serverThreadMain3", serverThread
);
1242 SetLastError(0xdeadbeef);
1243 serverThread
= CreateThread(NULL
, 0, serverThreadMain4
, 0, 0, &serverThreadId
);
1244 ok(serverThread
!= NULL
, "CreateThread failed: %d\n", GetLastError());
1245 exercizeServer(PIPENAME
"serverThreadMain4", serverThread
);
1248 SetLastError(0xdeadbeef);
1249 serverThread
= CreateThread(NULL
, 0, serverThreadMain5
, 0, 0, &serverThreadId
);
1250 ok(serverThread
!= NULL
, "CreateThread failed: %d\n", GetLastError());
1251 exercizeServer(PIPENAME
"serverThreadMain5", serverThread
);
1253 ok(SetEvent( alarm_event
), "SetEvent\n");
1254 CloseHandle( alarm_event
);
1255 trace("test_NamedPipe_2 returning\n");
1258 static int test_DisconnectNamedPipe(void)
1262 static const char obuf
[] = "Bit Bucket";
1268 SetLastError(0xdeadbeef);
1269 hnp
= CreateNamedPipeA(PIPENAME
, PIPE_ACCESS_DUPLEX
, PIPE_TYPE_BYTE
| PIPE_WAIT
,
1270 /* nMaxInstances */ 1,
1271 /* nOutBufSize */ 1024,
1272 /* nInBufSize */ 1024,
1273 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
1274 /* lpSecurityAttrib */ NULL
);
1275 if ((hnp
== INVALID_HANDLE_VALUE
/* Win98 */ || !hnp
/* Win95 */)
1276 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
) {
1278 win_skip("Named pipes are not implemented\n");
1282 ok(WriteFile(hnp
, obuf
, sizeof(obuf
), &written
, NULL
) == 0
1283 && GetLastError() == ERROR_PIPE_LISTENING
, "WriteFile to not-yet-connected pipe\n");
1284 ok(ReadFile(hnp
, ibuf
, sizeof(ibuf
), &readden
, NULL
) == 0
1285 && GetLastError() == ERROR_PIPE_LISTENING
, "ReadFile from not-yet-connected pipe\n");
1287 hFile
= CreateFileA(PIPENAME
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0);
1288 ok(hFile
!= INVALID_HANDLE_VALUE
, "CreateFile failed\n");
1290 /* don't try to do i/o if one side couldn't be opened, as it hangs */
1291 if (hFile
!= INVALID_HANDLE_VALUE
) {
1293 /* see what happens if server calls DisconnectNamedPipe
1294 * when there are bytes in the pipe
1297 ok(WriteFile(hFile
, obuf
, sizeof(obuf
), &written
, NULL
), "WriteFile\n");
1298 ok(written
== sizeof(obuf
), "write file len\n");
1299 ok(DisconnectNamedPipe(hnp
), "DisconnectNamedPipe while messages waiting\n");
1300 ok(WriteFile(hFile
, obuf
, sizeof(obuf
), &written
, NULL
) == 0
1301 && GetLastError() == ERROR_PIPE_NOT_CONNECTED
, "WriteFile to disconnected pipe\n");
1302 ok(ReadFile(hnp
, ibuf
, sizeof(ibuf
), &readden
, NULL
) == 0
1303 && GetLastError() == ERROR_PIPE_NOT_CONNECTED
,
1304 "ReadFile from disconnected pipe with bytes waiting\n");
1305 ok(!DisconnectNamedPipe(hnp
) && GetLastError() == ERROR_PIPE_NOT_CONNECTED
,
1306 "DisconnectNamedPipe worked twice\n");
1307 ret
= WaitForSingleObject(hFile
, 0);
1308 ok(ret
== WAIT_TIMEOUT
, "WaitForSingleObject returned %X\n", ret
);
1309 ok(CloseHandle(hFile
), "CloseHandle\n");
1312 ok(CloseHandle(hnp
), "CloseHandle\n");
1316 static void test_CreatePipe(void)
1318 SECURITY_ATTRIBUTES pipe_attr
;
1319 HANDLE piperead
, pipewrite
;
1326 user_apc_ran
= FALSE
;
1328 ok(pQueueUserAPC(user_apc
, GetCurrentThread(), 0), "couldn't create user apc\n");
1330 pipe_attr
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
1331 pipe_attr
.bInheritHandle
= TRUE
;
1332 pipe_attr
.lpSecurityDescriptor
= NULL
;
1333 ok(CreatePipe(&piperead
, &pipewrite
, &pipe_attr
, 0) != 0, "CreatePipe failed\n");
1334 ok(WriteFile(pipewrite
,PIPENAME
,sizeof(PIPENAME
), &written
, NULL
), "Write to anonymous pipe failed\n");
1335 ok(written
== sizeof(PIPENAME
), "Write to anonymous pipe wrote %d bytes\n", written
);
1336 ok(ReadFile(piperead
,readbuf
,sizeof(readbuf
),&read
, NULL
), "Read from non empty pipe failed\n");
1337 ok(read
== sizeof(PIPENAME
), "Read from anonymous pipe got %d bytes\n", read
);
1338 ok(CloseHandle(pipewrite
), "CloseHandle for the write pipe failed\n");
1339 ok(CloseHandle(piperead
), "CloseHandle for the read pipe failed\n");
1341 /* Now write another chunk*/
1342 ok(CreatePipe(&piperead
, &pipewrite
, &pipe_attr
, 0) != 0, "CreatePipe failed\n");
1343 ok(WriteFile(pipewrite
,PIPENAME
,sizeof(PIPENAME
), &written
, NULL
), "Write to anonymous pipe failed\n");
1344 ok(written
== sizeof(PIPENAME
), "Write to anonymous pipe wrote %d bytes\n", written
);
1345 /* and close the write end, read should still succeed*/
1346 ok(CloseHandle(pipewrite
), "CloseHandle for the Write Pipe failed\n");
1347 ok(ReadFile(piperead
,readbuf
,sizeof(readbuf
),&read
, NULL
), "Read from broken pipe withe with pending data failed\n");
1348 ok(read
== sizeof(PIPENAME
), "Read from anonymous pipe got %d bytes\n", read
);
1349 /* But now we need to get informed that the pipe is closed */
1350 ok(ReadFile(piperead
,readbuf
,sizeof(readbuf
),&read
, NULL
) == 0, "Broken pipe not detected\n");
1351 ok(CloseHandle(piperead
), "CloseHandle for the read pipe failed\n");
1353 /* Try bigger chunks */
1355 buffer
= HeapAlloc( GetProcessHeap(), 0, size
);
1356 for (i
= 0; i
< size
; i
++) buffer
[i
] = i
;
1357 ok(CreatePipe(&piperead
, &pipewrite
, &pipe_attr
, (size
+ 24)) != 0, "CreatePipe failed\n");
1358 ok(WriteFile(pipewrite
, buffer
, size
, &written
, NULL
), "Write to anonymous pipe failed\n");
1359 ok(written
== size
, "Write to anonymous pipe wrote %d bytes\n", written
);
1360 /* and close the write end, read should still succeed*/
1361 ok(CloseHandle(pipewrite
), "CloseHandle for the Write Pipe failed\n");
1362 memset( buffer
, 0, size
);
1363 ok(ReadFile(piperead
, buffer
, size
, &read
, NULL
), "Read from broken pipe withe with pending data failed\n");
1364 ok(read
== size
, "Read from anonymous pipe got %d bytes\n", read
);
1365 for (i
= 0; i
< size
; i
++) ok( buffer
[i
] == (BYTE
)i
, "invalid data %x at %x\n", buffer
[i
], i
);
1366 /* But now we need to get informed that the pipe is closed */
1367 ok(ReadFile(piperead
,readbuf
,sizeof(readbuf
),&read
, NULL
) == 0, "Broken pipe not detected\n");
1368 ok(CloseHandle(piperead
), "CloseHandle for the read pipe failed\n");
1369 HeapFree(GetProcessHeap(), 0, buffer
);
1371 ok(user_apc_ran
== FALSE
, "user apc ran, pipe using alertable io mode\n");
1372 SleepEx(0, TRUE
); /* get rid of apc */
1375 struct named_pipe_client_params
1377 DWORD security_flags
;
1382 #define PIPE_NAME "\\\\.\\pipe\\named_pipe_test"
1384 static DWORD CALLBACK
named_pipe_client_func(LPVOID p
)
1386 struct named_pipe_client_params
*params
= p
;
1389 const char message
[] = "Test";
1390 DWORD bytes_read
, bytes_written
;
1392 TOKEN_PRIVILEGES
*Privileges
= NULL
;
1398 /* modify the token so we can tell if the pipe impersonation
1399 * token reverts to the process token */
1400 ret
= AdjustTokenPrivileges(params
->token
, TRUE
, NULL
, 0, NULL
, NULL
);
1401 ok(ret
, "AdjustTokenPrivileges failed with error %d\n", GetLastError());
1403 ret
= SetThreadToken(NULL
, params
->token
);
1404 ok(ret
, "SetThreadToken failed with error %d\n", GetLastError());
1409 HANDLE process_token
;
1411 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY
|TOKEN_ADJUST_PRIVILEGES
, &process_token
);
1412 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
1414 ret
= GetTokenInformation(process_token
, TokenPrivileges
, NULL
, 0, &Size
);
1415 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "GetTokenInformation(TokenPrivileges) failed with %d\n", GetLastError());
1416 Privileges
= HeapAlloc(GetProcessHeap(), 0, Size
);
1417 ret
= GetTokenInformation(process_token
, TokenPrivileges
, Privileges
, Size
, &Size
);
1418 ok(ret
, "GetTokenInformation(TokenPrivileges) failed with %d\n", GetLastError());
1420 ret
= AdjustTokenPrivileges(process_token
, TRUE
, NULL
, 0, NULL
, NULL
);
1421 ok(ret
, "AdjustTokenPrivileges failed with error %d\n", GetLastError());
1423 CloseHandle(process_token
);
1426 pipe
= CreateFileA(PIPE_NAME
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, params
->security_flags
, NULL
);
1427 ok(pipe
!= INVALID_HANDLE_VALUE
, "CreateFile for pipe failed with error %d\n", GetLastError());
1429 ret
= WriteFile(pipe
, message
, sizeof(message
), &bytes_written
, NULL
);
1430 ok(ret
, "WriteFile failed with error %d\n", GetLastError());
1432 ret
= ReadFile(pipe
, &dummy
, sizeof(dummy
), &bytes_read
, NULL
);
1433 ok(ret
, "ReadFile failed with error %d\n", GetLastError());
1439 ret
= RevertToSelf();
1440 ok(ret
, "RevertToSelf failed with error %d\n", GetLastError());
1444 ret
= AdjustTokenPrivileges(params
->token
, TRUE
, NULL
, 0, NULL
, NULL
);
1445 ok(ret
, "AdjustTokenPrivileges failed with error %d\n", GetLastError());
1450 HANDLE process_token
;
1452 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES
, &process_token
);
1453 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
1455 ret
= AdjustTokenPrivileges(process_token
, FALSE
, Privileges
, 0, NULL
, NULL
);
1456 ok(ret
, "AdjustTokenPrivileges failed with error %d\n", GetLastError());
1458 HeapFree(GetProcessHeap(), 0, Privileges
);
1460 CloseHandle(process_token
);
1463 ret
= WriteFile(pipe
, message
, sizeof(message
), &bytes_written
, NULL
);
1464 ok(ret
, "WriteFile failed with error %d\n", GetLastError());
1466 ret
= ReadFile(pipe
, &dummy
, sizeof(dummy
), &bytes_read
, NULL
);
1467 ok(ret
, "ReadFile failed with error %d\n", GetLastError());
1474 static HANDLE
make_impersonation_token(DWORD Access
, SECURITY_IMPERSONATION_LEVEL ImpersonationLevel
)
1476 HANDLE ProcessToken
;
1477 HANDLE Token
= NULL
;
1480 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE
, &ProcessToken
);
1481 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
1483 ret
= pDuplicateTokenEx(ProcessToken
, Access
, NULL
, ImpersonationLevel
, TokenImpersonation
, &Token
);
1484 ok(ret
, "DuplicateToken failed with error %d\n", GetLastError());
1486 CloseHandle(ProcessToken
);
1491 static void test_ImpersonateNamedPipeClient(HANDLE hClientToken
, DWORD security_flags
, BOOL revert
, void (*test_func
)(int, HANDLE
))
1500 struct named_pipe_client_params params
;
1502 DWORD dwBytesWritten
;
1503 HANDLE hToken
= NULL
;
1504 SECURITY_IMPERSONATION_LEVEL ImpersonationLevel
;
1507 hPipeServer
= CreateNamedPipeA(PIPE_NAME
, PIPE_ACCESS_DUPLEX
, PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
| PIPE_WAIT
, 1, 100, 100, NMPWAIT_USE_DEFAULT_WAIT
, NULL
);
1508 ok(hPipeServer
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed with error %d\n", GetLastError());
1510 params
.security_flags
= security_flags
;
1511 params
.token
= hClientToken
;
1512 params
.revert
= revert
;
1513 hThread
= CreateThread(NULL
, 0, named_pipe_client_func
, ¶ms
, 0, &dwTid
);
1514 ok(hThread
!= NULL
, "CreateThread failed with error %d\n", GetLastError());
1516 SetLastError(0xdeadbeef);
1517 ret
= ImpersonateNamedPipeClient(hPipeServer
);
1518 error
= GetLastError();
1519 ok(ret
/* win2k3 */ || (error
== ERROR_CANNOT_IMPERSONATE
),
1520 "ImpersonateNamedPipeClient should have failed with ERROR_CANNOT_IMPERSONATE instead of %d\n", GetLastError());
1522 ret
= ConnectNamedPipe(hPipeServer
, NULL
);
1523 ok(ret
|| (GetLastError() == ERROR_PIPE_CONNECTED
), "ConnectNamedPipe failed with error %d\n", GetLastError());
1525 ret
= ReadFile(hPipeServer
, buffer
, sizeof(buffer
), &dwBytesRead
, NULL
);
1526 ok(ret
, "ReadFile failed with error %d\n", GetLastError());
1528 ret
= ImpersonateNamedPipeClient(hPipeServer
);
1529 ok(ret
, "ImpersonateNamedPipeClient failed with error %d\n", GetLastError());
1531 ret
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
, FALSE
, &hToken
);
1532 ok(ret
, "OpenThreadToken failed with error %d\n", GetLastError());
1534 (*test_func
)(0, hToken
);
1536 ImpersonationLevel
= 0xdeadbeef; /* to avoid false positives */
1537 ret
= GetTokenInformation(hToken
, TokenImpersonationLevel
, &ImpersonationLevel
, sizeof(ImpersonationLevel
), &size
);
1538 ok(ret
, "GetTokenInformation(TokenImpersonationLevel) failed with error %d\n", GetLastError());
1539 ok(ImpersonationLevel
== SecurityImpersonation
, "ImpersonationLevel should have been SecurityImpersonation(%d) instead of %d\n", SecurityImpersonation
, ImpersonationLevel
);
1541 CloseHandle(hToken
);
1545 ret
= WriteFile(hPipeServer
, &dummy
, sizeof(dummy
), &dwBytesWritten
, NULL
);
1546 ok(ret
, "WriteFile failed with error %d\n", GetLastError());
1548 ret
= ReadFile(hPipeServer
, buffer
, sizeof(buffer
), &dwBytesRead
, NULL
);
1549 ok(ret
, "ReadFile failed with error %d\n", GetLastError());
1551 ret
= ImpersonateNamedPipeClient(hPipeServer
);
1552 ok(ret
, "ImpersonateNamedPipeClient failed with error %d\n", GetLastError());
1554 ret
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
, FALSE
, &hToken
);
1555 ok(ret
, "OpenThreadToken failed with error %d\n", GetLastError());
1557 (*test_func
)(1, hToken
);
1559 CloseHandle(hToken
);
1563 ret
= WriteFile(hPipeServer
, &dummy
, sizeof(dummy
), &dwBytesWritten
, NULL
);
1564 ok(ret
, "WriteFile failed with error %d\n", GetLastError());
1566 WaitForSingleObject(hThread
, INFINITE
);
1568 ret
= ImpersonateNamedPipeClient(hPipeServer
);
1569 ok(ret
, "ImpersonateNamedPipeClient failed with error %d\n", GetLastError());
1573 CloseHandle(hThread
);
1574 CloseHandle(hPipeServer
);
1577 static BOOL
are_all_privileges_disabled(HANDLE hToken
)
1580 TOKEN_PRIVILEGES
*Privileges
= NULL
;
1582 BOOL all_privs_disabled
= TRUE
;
1585 ret
= GetTokenInformation(hToken
, TokenPrivileges
, NULL
, 0, &Size
);
1586 if (!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
1588 Privileges
= HeapAlloc(GetProcessHeap(), 0, Size
);
1589 ret
= GetTokenInformation(hToken
, TokenPrivileges
, Privileges
, Size
, &Size
);
1592 HeapFree(GetProcessHeap(), 0, Privileges
);
1599 for (i
= 0; i
< Privileges
->PrivilegeCount
; i
++)
1601 if (Privileges
->Privileges
[i
].Attributes
& SE_PRIVILEGE_ENABLED
)
1603 all_privs_disabled
= FALSE
;
1608 HeapFree(GetProcessHeap(), 0, Privileges
);
1610 return all_privs_disabled
;
1613 static DWORD
get_privilege_count(HANDLE hToken
)
1615 TOKEN_STATISTICS Statistics
;
1616 DWORD Size
= sizeof(Statistics
);
1619 ret
= GetTokenInformation(hToken
, TokenStatistics
, &Statistics
, Size
, &Size
);
1620 ok(ret
, "GetTokenInformation(TokenStatistics)\n");
1621 if (!ret
) return -1;
1623 return Statistics
.PrivilegeCount
;
1626 static void test_no_sqos_no_token(int call_index
, HANDLE hToken
)
1633 priv_count
= get_privilege_count(hToken
);
1635 ok(priv_count
== 0, "privilege count should have been 0 instead of %d\n", priv_count
);
1638 priv_count
= get_privilege_count(hToken
);
1639 ok(priv_count
> 0, "privilege count should now be > 0 instead of 0\n");
1640 ok(!are_all_privileges_disabled(hToken
), "impersonated token should not have been modified\n");
1643 ok(0, "shouldn't happen\n");
1647 static void test_no_sqos(int call_index
, HANDLE hToken
)
1652 ok(!are_all_privileges_disabled(hToken
), "token should be a copy of the process one\n");
1656 ok(are_all_privileges_disabled(hToken
), "impersonated token should have been modified\n");
1659 ok(0, "shouldn't happen\n");
1663 static void test_static_context(int call_index
, HANDLE hToken
)
1668 ok(!are_all_privileges_disabled(hToken
), "token should be a copy of the process one\n");
1671 ok(!are_all_privileges_disabled(hToken
), "impersonated token should not have been modified\n");
1674 ok(0, "shouldn't happen\n");
1678 static void test_dynamic_context(int call_index
, HANDLE hToken
)
1683 ok(!are_all_privileges_disabled(hToken
), "token should be a copy of the process one\n");
1687 ok(are_all_privileges_disabled(hToken
), "impersonated token should have been modified\n");
1690 ok(0, "shouldn't happen\n");
1694 static void test_dynamic_context_no_token(int call_index
, HANDLE hToken
)
1699 ok(are_all_privileges_disabled(hToken
), "token should be a copy of the process one\n");
1702 ok(!are_all_privileges_disabled(hToken
), "process token modification should have been detected and impersonation token updated\n");
1705 ok(0, "shouldn't happen\n");
1709 static void test_no_sqos_revert(int call_index
, HANDLE hToken
)
1715 priv_count
= get_privilege_count(hToken
);
1717 ok(priv_count
== 0, "privilege count should have been 0 instead of %d\n", priv_count
);
1720 priv_count
= get_privilege_count(hToken
);
1721 ok(priv_count
> 0, "privilege count should now be > 0 instead of 0\n");
1722 ok(!are_all_privileges_disabled(hToken
), "impersonated token should not have been modified\n");
1725 ok(0, "shouldn't happen\n");
1729 static void test_static_context_revert(int call_index
, HANDLE hToken
)
1735 ok(are_all_privileges_disabled(hToken
), "privileges should have been disabled\n");
1739 ok(are_all_privileges_disabled(hToken
), "impersonated token should not have been modified\n");
1742 ok(0, "shouldn't happen\n");
1746 static void test_dynamic_context_revert(int call_index
, HANDLE hToken
)
1752 ok(are_all_privileges_disabled(hToken
), "privileges should have been disabled\n");
1755 ok(!are_all_privileges_disabled(hToken
), "impersonated token should now be process token\n");
1758 ok(0, "shouldn't happen\n");
1762 static void test_impersonation(void)
1764 HANDLE hClientToken
;
1765 HANDLE hProcessToken
;
1768 if( !pDuplicateTokenEx
) {
1769 skip("DuplicateTokenEx not found\n");
1773 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY
, &hProcessToken
);
1776 skip("couldn't open process token, skipping impersonation tests\n");
1780 if (!get_privilege_count(hProcessToken
) || are_all_privileges_disabled(hProcessToken
))
1782 skip("token didn't have any privileges or they were all disabled. token not suitable for impersonation tests\n");
1783 CloseHandle(hProcessToken
);
1786 CloseHandle(hProcessToken
);
1788 test_ImpersonateNamedPipeClient(NULL
, 0, FALSE
, test_no_sqos_no_token
);
1789 hClientToken
= make_impersonation_token(TOKEN_IMPERSONATE
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY
, SecurityImpersonation
);
1790 test_ImpersonateNamedPipeClient(hClientToken
, 0, FALSE
, test_no_sqos
);
1791 CloseHandle(hClientToken
);
1792 hClientToken
= make_impersonation_token(TOKEN_IMPERSONATE
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY
, SecurityImpersonation
);
1793 test_ImpersonateNamedPipeClient(hClientToken
,
1794 SECURITY_SQOS_PRESENT
| SECURITY_IMPERSONATION
, FALSE
,
1795 test_static_context
);
1796 CloseHandle(hClientToken
);
1797 hClientToken
= make_impersonation_token(TOKEN_IMPERSONATE
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY
, SecurityImpersonation
);
1798 test_ImpersonateNamedPipeClient(hClientToken
,
1799 SECURITY_SQOS_PRESENT
| SECURITY_CONTEXT_TRACKING
| SECURITY_IMPERSONATION
,
1800 FALSE
, test_dynamic_context
);
1801 CloseHandle(hClientToken
);
1802 test_ImpersonateNamedPipeClient(NULL
,
1803 SECURITY_SQOS_PRESENT
| SECURITY_CONTEXT_TRACKING
| SECURITY_IMPERSONATION
,
1804 FALSE
, test_dynamic_context_no_token
);
1806 hClientToken
= make_impersonation_token(TOKEN_IMPERSONATE
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY
, SecurityImpersonation
);
1807 test_ImpersonateNamedPipeClient(hClientToken
, 0, TRUE
, test_no_sqos_revert
);
1808 CloseHandle(hClientToken
);
1809 hClientToken
= make_impersonation_token(TOKEN_IMPERSONATE
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY
, SecurityImpersonation
);
1810 test_ImpersonateNamedPipeClient(hClientToken
,
1811 SECURITY_SQOS_PRESENT
| SECURITY_IMPERSONATION
, TRUE
,
1812 test_static_context_revert
);
1813 CloseHandle(hClientToken
);
1814 hClientToken
= make_impersonation_token(TOKEN_IMPERSONATE
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY
, SecurityImpersonation
);
1815 test_ImpersonateNamedPipeClient(hClientToken
,
1816 SECURITY_SQOS_PRESENT
| SECURITY_CONTEXT_TRACKING
| SECURITY_IMPERSONATION
,
1817 TRUE
, test_dynamic_context_revert
);
1818 CloseHandle(hClientToken
);
1821 struct overlapped_server_args
1823 HANDLE pipe_created
;
1826 static DWORD CALLBACK
overlapped_server(LPVOID arg
)
1831 struct overlapped_server_args
*a
= arg
;
1835 pipe
= CreateNamedPipeA("\\\\.\\pipe\\my pipe", FILE_FLAG_OVERLAPPED
| PIPE_ACCESS_DUPLEX
, PIPE_TYPE_MESSAGE
| PIPE_READMODE_MESSAGE
, 1, 0, 0, 100000, NULL
);
1836 ok(pipe
!= NULL
, "pipe NULL\n");
1838 ol
.hEvent
= CreateEventA(0, 1, 0, 0);
1839 ok(ol
.hEvent
!= NULL
, "event NULL\n");
1840 ret
= ConnectNamedPipe(pipe
, &ol
);
1841 err
= GetLastError();
1842 ok(ret
== 0, "ret %d\n", ret
);
1843 ok(err
== ERROR_IO_PENDING
, "gle %d\n", err
);
1844 SetEvent(a
->pipe_created
);
1846 ret
= WaitForSingleObjectEx(ol
.hEvent
, INFINITE
, 1);
1847 ok(ret
== WAIT_OBJECT_0
, "ret %x\n", ret
);
1849 ret
= GetOverlappedResult(pipe
, &ol
, &num
, 1);
1850 ok(ret
== 1, "ret %d\n", ret
);
1852 /* This should block */
1853 ret
= ReadFile(pipe
, buf
, sizeof(buf
), &num
, NULL
);
1854 ok(ret
== 1, "ret %d\n", ret
);
1856 DisconnectNamedPipe(pipe
);
1857 CloseHandle(ol
.hEvent
);
1862 static void test_overlapped(void)
1865 HANDLE thread
, pipe
;
1867 struct overlapped_server_args args
;
1869 args
.pipe_created
= CreateEventA(0, 1, 0, 0);
1870 thread
= CreateThread(NULL
, 0, overlapped_server
, &args
, 0, &tid
);
1872 WaitForSingleObject(args
.pipe_created
, INFINITE
);
1873 pipe
= CreateFileA("\\\\.\\pipe\\my pipe", GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, NULL
);
1874 ok(pipe
!= INVALID_HANDLE_VALUE
, "cf failed\n");
1876 /* Sleep to try to get the ReadFile in the server to occur before the following WriteFile */
1879 ret
= WriteFile(pipe
, "x", 1, &num
, NULL
);
1880 ok(ret
, "WriteFile failed with error %d\n", GetLastError());
1882 WaitForSingleObject(thread
, INFINITE
);
1884 CloseHandle(args
.pipe_created
);
1885 CloseHandle(thread
);
1888 static void test_NamedPipeHandleState(void)
1890 HANDLE server
, client
;
1892 DWORD state
, instances
, maxCollectionCount
, collectDataTimeout
;
1893 char userName
[MAX_PATH
];
1895 server
= CreateNamedPipeA(PIPENAME
, PIPE_ACCESS_DUPLEX
,
1896 /* dwOpenMode */ PIPE_TYPE_BYTE
| PIPE_WAIT
,
1897 /* nMaxInstances */ 1,
1898 /* nOutBufSize */ 1024,
1899 /* nInBufSize */ 1024,
1900 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
1901 /* lpSecurityAttrib */ NULL
);
1902 ok(server
!= INVALID_HANDLE_VALUE
, "cf failed\n");
1903 ret
= GetNamedPipeHandleStateA(server
, NULL
, NULL
, NULL
, NULL
, NULL
, 0);
1904 ok(ret
, "GetNamedPipeHandleState failed: %d\n", GetLastError());
1905 ret
= GetNamedPipeHandleStateA(server
, &state
, &instances
, NULL
, NULL
, NULL
,
1907 ok(ret
, "GetNamedPipeHandleState failed: %d\n", GetLastError());
1910 ok(state
== 0, "unexpected state %08x\n", state
);
1911 ok(instances
== 1, "expected 1 instances, got %d\n", instances
);
1913 /* Some parameters have no meaning, and therefore can't be retrieved,
1916 SetLastError(0xdeadbeef);
1917 ret
= GetNamedPipeHandleStateA(server
, &state
, &instances
,
1918 &maxCollectionCount
, &collectDataTimeout
, userName
,
1919 sizeof(userName
) / sizeof(userName
[0]));
1921 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
1922 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1923 /* A byte-mode pipe server can't be changed to message mode. */
1924 state
= PIPE_READMODE_MESSAGE
;
1925 SetLastError(0xdeadbeef);
1926 ret
= SetNamedPipeHandleState(server
, &state
, NULL
, NULL
);
1927 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
1928 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1930 client
= CreateFileA(PIPENAME
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1931 OPEN_EXISTING
, 0, NULL
);
1932 ok(client
!= INVALID_HANDLE_VALUE
, "cf failed\n");
1934 state
= PIPE_READMODE_BYTE
;
1935 ret
= SetNamedPipeHandleState(client
, &state
, NULL
, NULL
);
1936 ok(ret
, "SetNamedPipeHandleState failed: %d\n", GetLastError());
1937 /* A byte-mode pipe client can't be changed to message mode, either. */
1938 state
= PIPE_READMODE_MESSAGE
;
1939 SetLastError(0xdeadbeef);
1940 ret
= SetNamedPipeHandleState(server
, &state
, NULL
, NULL
);
1941 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
1942 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1944 CloseHandle(client
);
1945 CloseHandle(server
);
1947 server
= CreateNamedPipeA(PIPENAME
, PIPE_ACCESS_DUPLEX
,
1948 /* dwOpenMode */ PIPE_TYPE_MESSAGE
| PIPE_WAIT
,
1949 /* nMaxInstances */ 1,
1950 /* nOutBufSize */ 1024,
1951 /* nInBufSize */ 1024,
1952 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
1953 /* lpSecurityAttrib */ NULL
);
1954 ok(server
!= INVALID_HANDLE_VALUE
, "cf failed\n");
1955 ret
= GetNamedPipeHandleStateA(server
, NULL
, NULL
, NULL
, NULL
, NULL
, 0);
1956 ok(ret
, "GetNamedPipeHandleState failed: %d\n", GetLastError());
1957 ret
= GetNamedPipeHandleStateA(server
, &state
, &instances
, NULL
, NULL
, NULL
,
1959 ok(ret
, "GetNamedPipeHandleState failed: %d\n", GetLastError());
1962 ok(state
== 0, "unexpected state %08x\n", state
);
1963 ok(instances
== 1, "expected 1 instances, got %d\n", instances
);
1965 /* In contrast to byte-mode pipes, a message-mode pipe server can be
1966 * changed to byte mode.
1968 state
= PIPE_READMODE_BYTE
;
1969 ret
= SetNamedPipeHandleState(server
, &state
, NULL
, NULL
);
1970 ok(ret
, "SetNamedPipeHandleState failed: %d\n", GetLastError());
1972 client
= CreateFileA(PIPENAME
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1973 OPEN_EXISTING
, 0, NULL
);
1974 ok(client
!= INVALID_HANDLE_VALUE
, "cf failed\n");
1976 state
= PIPE_READMODE_MESSAGE
;
1977 ret
= SetNamedPipeHandleState(client
, &state
, NULL
, NULL
);
1978 ok(ret
, "SetNamedPipeHandleState failed: %d\n", GetLastError());
1979 /* A message-mode pipe client can also be changed to byte mode.
1981 state
= PIPE_READMODE_BYTE
;
1982 ret
= SetNamedPipeHandleState(client
, &state
, NULL
, NULL
);
1983 ok(ret
, "SetNamedPipeHandleState failed: %d\n", GetLastError());
1985 CloseHandle(client
);
1986 CloseHandle(server
);
1989 static void test_readfileex_pending(void)
1991 HANDLE server
, client
, event
;
1993 DWORD err
, wait
, num_bytes
;
1994 OVERLAPPED overlapped
;
1995 char read_buf
[1024];
1996 char write_buf
[1024];
1997 const char test_string
[] = "test";
2000 server
= CreateNamedPipeA(PIPENAME
, FILE_FLAG_OVERLAPPED
| PIPE_ACCESS_DUPLEX
,
2001 /* dwOpenMode */ PIPE_TYPE_BYTE
| PIPE_WAIT
,
2002 /* nMaxInstances */ 1,
2003 /* nOutBufSize */ 1024,
2004 /* nInBufSize */ 1024,
2005 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
2006 /* lpSecurityAttrib */ NULL
);
2007 ok(server
!= INVALID_HANDLE_VALUE
, "cf failed\n");
2009 event
= CreateEventA(NULL
, TRUE
, FALSE
, NULL
);
2010 ok(event
!= NULL
, "CreateEventA failed\n");
2012 memset(&overlapped
, 0, sizeof(overlapped
));
2013 overlapped
.hEvent
= event
;
2015 ret
= ConnectNamedPipe(server
, &overlapped
);
2016 err
= GetLastError();
2017 ok(ret
== FALSE
, "ConnectNamedPipe succeeded\n");
2018 ok(err
== ERROR_IO_PENDING
, "ConnectNamedPipe set error %i\n", err
);
2020 wait
= WaitForSingleObject(event
, 0);
2021 ok(wait
== WAIT_TIMEOUT
, "WaitForSingleObject returned %x\n", wait
);
2023 client
= CreateFileA(PIPENAME
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
2024 OPEN_EXISTING
, 0, NULL
);
2025 ok(client
!= INVALID_HANDLE_VALUE
, "cf failed\n");
2027 wait
= WaitForSingleObject(event
, 0);
2028 ok(wait
== WAIT_OBJECT_0
, "WaitForSingleObject returned %x\n", wait
);
2030 /* Start a read that can't complete immediately. */
2031 completion_called
= 0;
2033 ret
= ReadFileEx(server
, read_buf
, sizeof(read_buf
), &overlapped
, completion_routine
);
2034 ok(ret
== TRUE
, "ReadFileEx failed, err=%i\n", GetLastError());
2035 ok(completion_called
== 0, "completion routine called before ReadFileEx returned\n");
2037 ret
= WriteFile(client
, test_string
, strlen(test_string
), &num_bytes
, NULL
);
2038 ok(ret
== TRUE
, "WriteFile failed\n");
2039 ok(num_bytes
== strlen(test_string
), "only %i bytes written\n", num_bytes
);
2041 ok(completion_called
== 0, "completion routine called during WriteFile\n");
2043 wait
= WaitForSingleObjectEx(event
, 0, TRUE
);
2044 ok(wait
== WAIT_IO_COMPLETION
|| wait
== WAIT_OBJECT_0
, "WaitForSingleObjectEx returned %x\n", wait
);
2046 ok(completion_called
== 1, "completion not called after writing pipe\n");
2047 ok(completion_errorcode
== 0, "completion called with error %x\n", completion_errorcode
);
2048 ok(completion_num_bytes
== strlen(test_string
), "ReadFileEx returned only %d bytes\n", completion_num_bytes
);
2049 ok(completion_lpoverlapped
== &overlapped
, "completion called with wrong overlapped pointer\n");
2050 ok(!memcmp(test_string
, read_buf
, strlen(test_string
)), "ReadFileEx read wrong bytes\n");
2052 /* Make writes until the pipe is full and the write fails */
2053 memset(write_buf
, 0xaa, sizeof(write_buf
));
2054 for (i
=0; i
<256; i
++)
2056 completion_called
= 0;
2058 ret
= WriteFileEx(server
, write_buf
, sizeof(write_buf
), &overlapped
, completion_routine
);
2059 err
= GetLastError();
2061 ok(completion_called
== 0, "completion routine called during WriteFileEx\n");
2063 wait
= WaitForSingleObjectEx(event
, 0, TRUE
);
2065 if (wait
== WAIT_TIMEOUT
)
2066 /* write couldn't complete immediately, presumably the pipe is full */
2069 ok(wait
== WAIT_IO_COMPLETION
|| wait
== WAIT_OBJECT_0
, "WaitForSingleObject returned %x\n", wait
);
2071 ok(ret
== TRUE
, "WriteFileEx failed, err=%i\n", err
);
2072 ok(completion_errorcode
== 0, "completion called with error %x\n", completion_errorcode
);
2073 ok(completion_lpoverlapped
== &overlapped
, "completion called with wrong overlapped pointer\n");
2076 ok(ret
== TRUE
, "WriteFileEx failed, err=%i\n", err
);
2077 ok(completion_called
== 0, "completion routine called but wait timed out\n");
2078 ok(completion_errorcode
== 0, "completion called with error %x\n", completion_errorcode
);
2079 ok(completion_lpoverlapped
== &overlapped
, "completion called with wrong overlapped pointer\n");
2081 /* free up some space in the pipe */
2082 ret
= ReadFile(client
, read_buf
, sizeof(read_buf
), &num_bytes
, NULL
);
2083 ok(ret
== TRUE
, "ReadFile failed\n");
2085 ok(completion_called
== 0, "completion routine called during ReadFile\n");
2087 wait
= WaitForSingleObjectEx(event
, 0, TRUE
);
2088 ok(wait
== WAIT_IO_COMPLETION
|| wait
== WAIT_OBJECT_0
, "WaitForSingleObject returned %x\n", wait
);
2089 if (wait
== WAIT_TIMEOUT
)
2091 ret
= ReadFile(client
, read_buf
, sizeof(read_buf
), &num_bytes
, NULL
);
2092 ok(ret
== TRUE
, "ReadFile failed\n");
2093 ok(completion_called
== 0, "completion routine called during ReadFile\n");
2094 wait
= WaitForSingleObjectEx(event
, 0, TRUE
);
2095 ok(wait
== WAIT_IO_COMPLETION
|| wait
== WAIT_OBJECT_0
, "WaitForSingleObject returned %x\n", wait
);
2098 ok(completion_called
== 1, "completion routine not called\n");
2099 ok(completion_errorcode
== 0, "completion called with error %x\n", completion_errorcode
);
2100 ok(completion_lpoverlapped
== &overlapped
, "completion called with wrong overlapped pointer\n");
2102 num_bytes
= 0xdeadbeef;
2103 SetLastError(0xdeadbeef);
2104 ret
= ReadFile(INVALID_HANDLE_VALUE
, read_buf
, 0, &num_bytes
, NULL
);
2105 ok(!ret
, "ReadFile should fail\n");
2106 ok(GetLastError() == ERROR_INVALID_HANDLE
, "wrong error %u\n", GetLastError());
2107 ok(num_bytes
== 0, "expected 0, got %u\n", num_bytes
);
2109 S(U(overlapped
)).Offset
= 0;
2110 S(U(overlapped
)).OffsetHigh
= 0;
2111 overlapped
.Internal
= -1;
2112 overlapped
.InternalHigh
= -1;
2113 overlapped
.hEvent
= event
;
2114 num_bytes
= 0xdeadbeef;
2115 SetLastError(0xdeadbeef);
2116 ret
= ReadFile(server
, read_buf
, 0, &num_bytes
, &overlapped
);
2118 ok(GetLastError() == ERROR_IO_PENDING
, "expected ERROR_IO_PENDING, got %d\n", GetLastError());
2119 ok(num_bytes
== 0, "bytes %u\n", num_bytes
);
2120 ok((NTSTATUS
)overlapped
.Internal
== STATUS_PENDING
, "expected STATUS_PENDING, got %#lx\n", overlapped
.Internal
);
2122 ok(overlapped
.InternalHigh
== -1, "expected -1, got %lu\n", overlapped
.InternalHigh
);
2124 wait
= WaitForSingleObject(event
, 100);
2125 ok(wait
== WAIT_TIMEOUT
, "WaitForSingleObject returned %x\n", wait
);
2127 num_bytes
= 0xdeadbeef;
2128 ret
= WriteFile(client
, test_string
, 1, &num_bytes
, NULL
);
2129 ok(ret
, "WriteFile failed\n");
2130 ok(num_bytes
== 1, "bytes %u\n", num_bytes
);
2132 wait
= WaitForSingleObject(event
, 100);
2134 ok(wait
== WAIT_OBJECT_0
, "WaitForSingleObject returned %x\n", wait
);
2136 ok(num_bytes
== 1, "bytes %u\n", num_bytes
);
2138 ok((NTSTATUS
)overlapped
.Internal
== STATUS_SUCCESS
, "expected STATUS_SUCCESS, got %#lx\n", overlapped
.Internal
);
2139 ok(overlapped
.InternalHigh
== 0, "expected 0, got %lu\n", overlapped
.InternalHigh
);
2141 /* read the pending byte and clear the pipe */
2142 num_bytes
= 0xdeadbeef;
2143 ret
= ReadFile(server
, read_buf
, 1, &num_bytes
, &overlapped
);
2144 ok(ret
, "ReadFile failed\n");
2145 ok(num_bytes
== 1, "bytes %u\n", num_bytes
);
2147 CloseHandle(client
);
2148 CloseHandle(server
);
2156 hmod
= GetModuleHandleA("advapi32.dll");
2157 pDuplicateTokenEx
= (void *) GetProcAddress(hmod
, "DuplicateTokenEx");
2158 hmod
= GetModuleHandleA("kernel32.dll");
2159 pQueueUserAPC
= (void *) GetProcAddress(hmod
, "QueueUserAPC");
2161 if (test_DisconnectNamedPipe())
2163 test_CreateNamedPipe_instances_must_match();
2165 test_CreateNamedPipe(PIPE_TYPE_BYTE
);
2166 test_CreateNamedPipe(PIPE_TYPE_MESSAGE
| PIPE_READMODE_MESSAGE
);
2168 test_impersonation();
2170 test_NamedPipeHandleState();
2171 test_readfileex_pending();