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 ok(WaitForSingleObject(thread
, INFINITE
) == WAIT_OBJECT_0
, "WaitForSingleObject failed with %d.\n", GetLastError());
108 SetLastError(rpcargs
.lastError
);
109 return (BOOL
)rpcargs
.returnValue
;
112 static void test_CreateNamedPipe(int pipemode
)
116 static const char obuf
[] = "Bit Bucket";
117 static const char obuf2
[] = "More bits";
118 char ibuf
[32], *pbuf
;
125 if (pipemode
== PIPE_TYPE_BYTE
)
126 trace("test_CreateNamedPipe starting in byte mode\n");
128 trace("test_CreateNamedPipe starting in message mode\n");
130 /* Wait for non existing pipe */
131 ret
= WaitNamedPipeA(PIPENAME
, 2000);
132 ok(ret
== 0, "WaitNamedPipe returned %d for non existing pipe\n", ret
);
133 ok(GetLastError() == ERROR_FILE_NOT_FOUND
, "wrong error %u\n", GetLastError());
135 /* Bad parameter checks */
136 hnp
= CreateNamedPipeA("not a named pipe", PIPE_ACCESS_DUPLEX
, pipemode
| PIPE_WAIT
,
137 /* nMaxInstances */ 1,
138 /* nOutBufSize */ 1024,
139 /* nInBufSize */ 1024,
140 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
141 /* lpSecurityAttrib */ NULL
);
142 ok(hnp
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_INVALID_NAME
,
143 "CreateNamedPipe should fail if name doesn't start with \\\\.\\pipe\n");
145 if (pipemode
== PIPE_TYPE_BYTE
)
147 /* Bad parameter checks */
148 hnp
= CreateNamedPipeA(PIPENAME
, PIPE_ACCESS_DUPLEX
, PIPE_TYPE_BYTE
| PIPE_READMODE_MESSAGE
,
149 /* nMaxInstances */ 1,
150 /* nOutBufSize */ 1024,
151 /* nInBufSize */ 1024,
152 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
153 /* lpSecurityAttrib */ NULL
);
154 ok(hnp
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_INVALID_PARAMETER
,
155 "CreateNamedPipe should fail with PIPE_TYPE_BYTE | PIPE_READMODE_MESSAGE\n");
158 hnp
= CreateNamedPipeA(NULL
,
159 PIPE_ACCESS_DUPLEX
, pipemode
| PIPE_WAIT
,
160 1, 1024, 1024, NMPWAIT_USE_DEFAULT_WAIT
, NULL
);
161 ok(hnp
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_PATH_NOT_FOUND
,
162 "CreateNamedPipe should fail if name is NULL\n");
164 hFile
= CreateFileA(PIPENAME
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0);
165 ok(hFile
== INVALID_HANDLE_VALUE
166 && GetLastError() == ERROR_FILE_NOT_FOUND
,
167 "connecting to nonexistent named pipe should fail with ERROR_FILE_NOT_FOUND\n");
169 /* Functional checks */
171 hnp
= CreateNamedPipeA(PIPENAME
, PIPE_ACCESS_DUPLEX
, pipemode
| PIPE_WAIT
,
172 /* nMaxInstances */ 1,
173 /* nOutBufSize */ 1024,
174 /* nInBufSize */ 1024,
175 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
176 /* lpSecurityAttrib */ NULL
);
177 ok(hnp
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
179 ret
= WaitNamedPipeA(PIPENAME
, 2000);
180 ok(ret
, "WaitNamedPipe failed (%d)\n", GetLastError());
182 hFile
= CreateFileA(PIPENAME
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0);
183 ok(hFile
!= INVALID_HANDLE_VALUE
, "CreateFile failed (%d)\n", GetLastError());
185 ok(!WaitNamedPipeA(PIPENAME
, 1000), "WaitNamedPipe succeeded\n");
187 ok(GetLastError() == ERROR_SEM_TIMEOUT
, "wrong error %u\n", GetLastError());
189 /* don't try to do i/o if one side couldn't be opened, as it hangs */
190 if (hFile
!= INVALID_HANDLE_VALUE
) {
193 /* Make sure we can read and write a few bytes in both directions */
194 memset(ibuf
, 0, sizeof(ibuf
));
195 ok(WriteFile(hnp
, obuf
, sizeof(obuf
), &written
, NULL
), "WriteFile\n");
196 ok(written
== sizeof(obuf
), "write file len\n");
197 ok(ReadFile(hFile
, ibuf
, sizeof(ibuf
), &readden
, NULL
), "ReadFile\n");
198 ok(readden
== sizeof(obuf
), "read got %d bytes\n", readden
);
199 ok(memcmp(obuf
, ibuf
, written
) == 0, "content check\n");
201 memset(ibuf
, 0, sizeof(ibuf
));
202 ok(WriteFile(hFile
, obuf2
, sizeof(obuf2
), &written
, NULL
), "WriteFile\n");
203 ok(written
== sizeof(obuf2
), "write file len\n");
204 ok(ReadFile(hnp
, ibuf
, sizeof(ibuf
), &readden
, NULL
), "ReadFile\n");
205 ok(readden
== sizeof(obuf2
), "read got %d bytes\n", readden
);
206 ok(memcmp(obuf2
, ibuf
, written
) == 0, "content check\n");
208 /* Now the same again, but with an additional call to PeekNamedPipe */
209 memset(ibuf
, 0, sizeof(ibuf
));
210 ok(WriteFile(hnp
, obuf
, sizeof(obuf
), &written
, NULL
), "WriteFile\n");
211 ok(written
== sizeof(obuf
), "write file len 1\n");
212 ok(PeekNamedPipe(hFile
, NULL
, 0, NULL
, &readden
, NULL
), "Peek\n");
213 ok(readden
== sizeof(obuf
), "peek 1 got %d bytes\n", readden
);
214 ok(ReadFile(hFile
, ibuf
, sizeof(ibuf
), &readden
, NULL
), "ReadFile\n");
215 ok(readden
== sizeof(obuf
), "read 1 got %d bytes\n", readden
);
216 ok(memcmp(obuf
, ibuf
, written
) == 0, "content 1 check\n");
218 memset(ibuf
, 0, sizeof(ibuf
));
219 ok(WriteFile(hFile
, obuf2
, sizeof(obuf2
), &written
, NULL
), "WriteFile\n");
220 ok(written
== sizeof(obuf2
), "write file len 2\n");
221 ok(PeekNamedPipe(hnp
, NULL
, 0, NULL
, &readden
, NULL
), "Peek\n");
222 ok(readden
== sizeof(obuf2
), "peek 2 got %d bytes\n", readden
);
223 ok(PeekNamedPipe(hnp
, (LPVOID
)1, 0, NULL
, &readden
, NULL
), "Peek\n");
224 ok(readden
== sizeof(obuf2
), "peek 2 got %d bytes\n", readden
);
225 ok(ReadFile(hnp
, ibuf
, sizeof(ibuf
), &readden
, NULL
), "ReadFile\n");
226 ok(readden
== sizeof(obuf2
), "read 2 got %d bytes\n", readden
);
227 ok(memcmp(obuf2
, ibuf
, written
) == 0, "content 2 check\n");
229 /* Test how ReadFile behaves when the buffer is not big enough for the whole message */
230 memset(ibuf
, 0, sizeof(ibuf
));
231 ok(WriteFile(hnp
, obuf2
, sizeof(obuf2
), &written
, NULL
), "WriteFile\n");
232 ok(written
== sizeof(obuf2
), "write file len\n");
233 ok(ReadFile(hFile
, ibuf
, 4, &readden
, NULL
), "ReadFile\n");
234 ok(readden
== 4, "read got %d bytes\n", readden
);
235 ok(ReadFile(hFile
, ibuf
+ 4, sizeof(ibuf
) - 4, &readden
, NULL
), "ReadFile\n");
236 ok(readden
== sizeof(obuf2
) - 4, "read got %d bytes\n", readden
);
237 ok(memcmp(obuf2
, ibuf
, written
) == 0, "content check\n");
239 memset(ibuf
, 0, sizeof(ibuf
));
240 ok(WriteFile(hFile
, obuf
, sizeof(obuf
), &written
, NULL
), "WriteFile\n");
241 ok(written
== sizeof(obuf
), "write file len\n");
242 if (pipemode
== PIPE_TYPE_BYTE
)
244 ok(ReadFile(hnp
, ibuf
, 4, &readden
, NULL
), "ReadFile\n");
248 SetLastError(0xdeadbeef);
250 ok(!ReadFile(hnp
, ibuf
, 4, &readden
, NULL
), "ReadFile\n");
252 ok(GetLastError() == ERROR_MORE_DATA
, "wrong error\n");
254 ok(readden
== 4, "read got %d bytes\n", readden
);
255 ok(ReadFile(hnp
, ibuf
+ 4, sizeof(ibuf
) - 4, &readden
, NULL
), "ReadFile\n");
256 ok(readden
== sizeof(obuf
) - 4, "read got %d bytes\n", readden
);
257 ok(memcmp(obuf
, ibuf
, written
) == 0, "content check\n");
259 /* Similar to above, but use a read buffer size small enough to read in three parts */
260 memset(ibuf
, 0, sizeof(ibuf
));
261 ok(WriteFile(hFile
, obuf2
, sizeof(obuf2
), &written
, NULL
), "WriteFile\n");
262 ok(written
== sizeof(obuf2
), "write file len\n");
263 if (pipemode
== PIPE_TYPE_BYTE
)
265 ok(ReadFile(hnp
, ibuf
, 4, &readden
, NULL
), "ReadFile\n");
266 ok(readden
== 4, "read got %d bytes\n", readden
);
267 ok(ReadFile(hnp
, ibuf
+ 4, 4, &readden
, NULL
), "ReadFile\n");
271 SetLastError(0xdeadbeef);
273 ok(!ReadFile(hnp
, ibuf
, 4, &readden
, NULL
), "ReadFile\n");
275 ok(GetLastError() == ERROR_MORE_DATA
, "wrong error\n");
276 ok(readden
== 4, "read got %d bytes\n", readden
);
277 SetLastError(0xdeadbeef);
279 ok(!ReadFile(hnp
, ibuf
+ 4, 4, &readden
, NULL
), "ReadFile\n");
281 ok(GetLastError() == ERROR_MORE_DATA
, "wrong error\n");
283 ok(readden
== 4, "read got %d bytes\n", readden
);
284 ok(ReadFile(hnp
, ibuf
+ 8, sizeof(ibuf
) - 8, &readden
, NULL
), "ReadFile\n");
285 ok(readden
== sizeof(obuf2
) - 8, "read got %d bytes\n", readden
);
286 ok(memcmp(obuf2
, ibuf
, written
) == 0, "content check\n");
288 /* Test reading of multiple writes */
289 memset(ibuf
, 0, sizeof(ibuf
));
290 ok(WriteFile(hnp
, obuf
, sizeof(obuf
), &written
, NULL
), "WriteFile3a\n");
291 ok(written
== sizeof(obuf
), "write file len 3a\n");
292 ok(WriteFile(hnp
, obuf2
, sizeof(obuf2
), &written
, NULL
), " WriteFile3b\n");
293 ok(written
== sizeof(obuf2
), "write file len 3b\n");
294 ok(PeekNamedPipe(hFile
, ibuf
, sizeof(ibuf
), &readden
, &avail
, NULL
), "Peek3\n");
295 if (pipemode
== PIPE_TYPE_BYTE
) {
296 todo_wine
ok(readden
== sizeof(obuf
) + sizeof(obuf2
), "peek3 got %d bytes\n", readden
);
300 ok(readden
== sizeof(obuf
), "peek3 got %d bytes\n", readden
);
302 ok(avail
== sizeof(obuf
) + sizeof(obuf2
), "peek3 got %d bytes available\n", avail
);
304 ok(memcmp(obuf
, pbuf
, sizeof(obuf
)) == 0, "pipe content 3a check\n");
305 if (pipemode
== PIPE_TYPE_BYTE
&& readden
>= sizeof(obuf
)+sizeof(obuf2
)) {
306 pbuf
+= sizeof(obuf
);
307 ok(memcmp(obuf2
, pbuf
, sizeof(obuf2
)) == 0, "pipe content 3b check\n");
309 ok(ReadFile(hFile
, ibuf
, sizeof(ibuf
), &readden
, NULL
), "ReadFile\n");
310 ok(readden
== sizeof(obuf
) + sizeof(obuf2
), "read 3 got %d bytes\n", readden
);
312 ok(memcmp(obuf
, pbuf
, sizeof(obuf
)) == 0, "content 3a check\n");
313 pbuf
+= sizeof(obuf
);
314 ok(memcmp(obuf2
, pbuf
, sizeof(obuf2
)) == 0, "content 3b check\n");
316 /* Multiple writes in the reverse direction */
317 memset(ibuf
, 0, sizeof(ibuf
));
318 ok(WriteFile(hFile
, obuf
, sizeof(obuf
), &written
, NULL
), "WriteFile4a\n");
319 ok(written
== sizeof(obuf
), "write file len 4a\n");
320 ok(WriteFile(hFile
, obuf2
, sizeof(obuf2
), &written
, NULL
), " WriteFile4b\n");
321 ok(written
== sizeof(obuf2
), "write file len 4b\n");
322 ok(PeekNamedPipe(hnp
, ibuf
, sizeof(ibuf
), &readden
, &avail
, NULL
), "Peek4\n");
323 if (pipemode
== PIPE_TYPE_BYTE
) {
324 todo_wine
ok(readden
== sizeof(obuf
) + sizeof(obuf2
), "peek4 got %d bytes\n", readden
);
328 ok(readden
== sizeof(obuf
), "peek4 got %d bytes\n", readden
);
330 ok(avail
== sizeof(obuf
) + sizeof(obuf2
), "peek4 got %d bytes available\n", avail
);
332 ok(memcmp(obuf
, pbuf
, sizeof(obuf
)) == 0, "pipe content 4a check\n");
333 if (pipemode
== PIPE_TYPE_BYTE
&& readden
>= sizeof(obuf
)+sizeof(obuf2
)) {
334 pbuf
+= sizeof(obuf
);
335 ok(memcmp(obuf2
, pbuf
, sizeof(obuf2
)) == 0, "pipe content 4b check\n");
337 ok(ReadFile(hnp
, ibuf
, sizeof(ibuf
), &readden
, NULL
), "ReadFile\n");
338 if (pipemode
== PIPE_TYPE_BYTE
) {
339 ok(readden
== sizeof(obuf
) + sizeof(obuf2
), "read 4 got %d bytes\n", readden
);
343 ok(readden
== sizeof(obuf
), "read 4 got %d bytes\n", readden
);
347 ok(memcmp(obuf
, pbuf
, sizeof(obuf
)) == 0, "content 4a check\n");
348 if (pipemode
== PIPE_TYPE_BYTE
) {
349 pbuf
+= sizeof(obuf
);
350 ok(memcmp(obuf2
, pbuf
, sizeof(obuf2
)) == 0, "content 4b check\n");
353 /* Test reading of multiple writes after a mode change
354 (CreateFile always creates a byte mode pipe) */
355 lpmode
= PIPE_READMODE_MESSAGE
;
356 if (pipemode
== PIPE_TYPE_BYTE
) {
357 /* trying to change the client end of a byte pipe to message mode should fail */
358 ok(!SetNamedPipeHandleState(hFile
, &lpmode
, NULL
, NULL
), "Change mode\n");
361 ok(SetNamedPipeHandleState(hFile
, &lpmode
, NULL
, NULL
), "Change mode\n");
363 memset(ibuf
, 0, sizeof(ibuf
));
364 ok(WriteFile(hnp
, obuf
, sizeof(obuf
), &written
, NULL
), "WriteFile5a\n");
365 ok(written
== sizeof(obuf
), "write file len 3a\n");
366 ok(WriteFile(hnp
, obuf2
, sizeof(obuf2
), &written
, NULL
), " WriteFile5b\n");
367 ok(written
== sizeof(obuf2
), "write file len 3b\n");
368 ok(PeekNamedPipe(hFile
, ibuf
, sizeof(ibuf
), &readden
, &avail
, NULL
), "Peek5\n");
369 ok(readden
== sizeof(obuf
), "peek5 got %d bytes\n", readden
);
370 ok(avail
== sizeof(obuf
) + sizeof(obuf2
), "peek5 got %d bytes available\n", avail
);
372 ok(memcmp(obuf
, pbuf
, sizeof(obuf
)) == 0, "content 5a check\n");
373 ok(ReadFile(hFile
, ibuf
, sizeof(ibuf
), &readden
, NULL
), "ReadFile\n");
375 ok(readden
== sizeof(obuf
), "read 5 got %d bytes\n", readden
);
378 ok(memcmp(obuf
, pbuf
, sizeof(obuf
)) == 0, "content 5a check\n");
379 if (readden
<= sizeof(obuf
))
380 ok(ReadFile(hFile
, ibuf
, sizeof(ibuf
), &readden
, NULL
), "ReadFile\n");
382 /* Multiple writes in the reverse direction */
383 /* the write of obuf2 from write4 should still be in the buffer */
384 ok(PeekNamedPipe(hnp
, ibuf
, sizeof(ibuf
), &readden
, &avail
, NULL
), "Peek6a\n");
386 ok(readden
== sizeof(obuf2
), "peek6a got %d bytes\n", readden
);
387 ok(avail
== sizeof(obuf2
), "peek6a got %d bytes available\n", avail
);
390 ok(ReadFile(hnp
, ibuf
, sizeof(ibuf
), &readden
, NULL
), "ReadFile\n");
391 ok(readden
== sizeof(obuf2
), "read 6a got %d bytes\n", readden
);
393 ok(memcmp(obuf2
, pbuf
, sizeof(obuf2
)) == 0, "content 6a check\n");
395 memset(ibuf
, 0, sizeof(ibuf
));
396 ok(WriteFile(hFile
, obuf
, sizeof(obuf
), &written
, NULL
), "WriteFile6a\n");
397 ok(written
== sizeof(obuf
), "write file len 6a\n");
398 ok(WriteFile(hFile
, obuf2
, sizeof(obuf2
), &written
, NULL
), " WriteFile6b\n");
399 ok(written
== sizeof(obuf2
), "write file len 6b\n");
400 ok(PeekNamedPipe(hnp
, ibuf
, sizeof(ibuf
), &readden
, &avail
, NULL
), "Peek6\n");
401 ok(readden
== sizeof(obuf
), "peek6 got %d bytes\n", readden
);
402 ok(avail
== sizeof(obuf
) + sizeof(obuf2
), "peek6b got %d bytes available\n", avail
);
404 ok(memcmp(obuf
, pbuf
, sizeof(obuf
)) == 0, "content 6a check\n");
405 ok(ReadFile(hnp
, ibuf
, sizeof(ibuf
), &readden
, NULL
), "ReadFile\n");
407 ok(readden
== sizeof(obuf
), "read 6b got %d bytes\n", readden
);
410 ok(memcmp(obuf
, pbuf
, sizeof(obuf
)) == 0, "content 6a check\n");
411 if (readden
<= sizeof(obuf
))
412 ok(ReadFile(hnp
, ibuf
, sizeof(ibuf
), &readden
, NULL
), "ReadFile\n");
414 /* Test how ReadFile behaves when the buffer is not big enough for the whole message */
415 memset(ibuf
, 0, sizeof(ibuf
));
416 ok(WriteFile(hnp
, obuf2
, sizeof(obuf2
), &written
, NULL
), "WriteFile 7\n");
417 ok(written
== sizeof(obuf2
), "write file len 7\n");
418 SetLastError(0xdeadbeef);
420 ok(!ReadFile(hFile
, ibuf
, 4, &readden
, NULL
), "ReadFile 7\n");
422 ok(GetLastError() == ERROR_MORE_DATA
, "wrong error 7\n");
423 ok(readden
== 4, "read got %d bytes 7\n", readden
);
424 ok(ReadFile(hFile
, ibuf
+ 4, sizeof(ibuf
) - 4, &readden
, NULL
), "ReadFile 7\n");
425 ok(readden
== sizeof(obuf2
) - 4, "read got %d bytes 7\n", readden
);
426 ok(memcmp(obuf2
, ibuf
, written
) == 0, "content check 7\n");
428 memset(ibuf
, 0, sizeof(ibuf
));
429 ok(WriteFile(hFile
, obuf
, sizeof(obuf
), &written
, NULL
), "WriteFile 8\n");
430 ok(written
== sizeof(obuf
), "write file len 8\n");
431 SetLastError(0xdeadbeef);
433 ok(!ReadFile(hnp
, ibuf
, 4, &readden
, NULL
), "ReadFile 8\n");
435 ok(GetLastError() == ERROR_MORE_DATA
, "wrong error 8\n");
436 ok(readden
== 4, "read got %d bytes 8\n", readden
);
437 ok(ReadFile(hnp
, ibuf
+ 4, sizeof(ibuf
) - 4, &readden
, NULL
), "ReadFile 8\n");
438 ok(readden
== sizeof(obuf
) - 4, "read got %d bytes 8\n", readden
);
439 ok(memcmp(obuf
, ibuf
, written
) == 0, "content check 8\n");
441 /* The following test shows that when doing a partial read of a message, the rest
442 * is still in the pipe, and can be received from a second thread. This shows
443 * especially that the content is _not_ stored in thread-local-storage until it is
444 * completely transmitted. The same method works even across multiple processes. */
445 memset(ibuf
, 0, sizeof(ibuf
));
446 ok(WriteFile(hnp
, obuf
, sizeof(obuf
), &written
, NULL
), "WriteFile 9\n");
447 ok(written
== sizeof(obuf
), "write file len 9\n");
448 ok(WriteFile(hnp
, obuf2
, sizeof(obuf2
), &written
, NULL
), "WriteFile 9\n");
449 ok(written
== sizeof(obuf2
), "write file len 9\n");
450 SetLastError(0xdeadbeef);
452 ok(!ReadFile(hFile
, ibuf
, 4, &readden
, NULL
), "ReadFile 9\n");
454 ok(GetLastError() == ERROR_MORE_DATA
, "wrong error 9\n");
455 ok(readden
== 4, "read got %d bytes 9\n", readden
);
456 SetLastError(0xdeadbeef);
457 ret
= RpcReadFile(hFile
, ibuf
+ 4, 4, &readden
, NULL
);
459 ok(!ret
, "RpcReadFile 9\n");
461 ok(GetLastError() == ERROR_MORE_DATA
, "wrong error 9\n");
462 ok(readden
== 4, "read got %d bytes 9\n", readden
);
463 ret
= RpcReadFile(hFile
, ibuf
+ 8, sizeof(ibuf
), &readden
, NULL
);
464 ok(ret
, "RpcReadFile 9\n");
466 ok(readden
== sizeof(obuf
) - 8, "read got %d bytes 9\n", readden
);
467 ok(memcmp(obuf
, ibuf
, sizeof(obuf
)) == 0, "content check 9\n");
468 if (readden
<= sizeof(obuf
) - 8) /* blocks forever if second part was already received */
470 memset(ibuf
, 0, sizeof(ibuf
));
471 SetLastError(0xdeadbeef);
472 ret
= RpcReadFile(hFile
, ibuf
, 4, &readden
, NULL
);
473 ok(!ret
, "RpcReadFile 9\n");
475 ok(GetLastError() == ERROR_MORE_DATA
, "wrong error 9\n");
476 ok(readden
== 4, "read got %d bytes 9\n", readden
);
477 SetLastError(0xdeadbeef);
479 ok(!ReadFile(hFile
, ibuf
+ 4, 4, &readden
, NULL
), "ReadFile 9\n");
481 ok(GetLastError() == ERROR_MORE_DATA
, "wrong error 9\n");
482 ok(readden
== 4, "read got %d bytes 9\n", readden
);
483 ret
= RpcReadFile(hFile
, ibuf
+ 8, sizeof(ibuf
), &readden
, NULL
);
484 ok(ret
, "RpcReadFile 9\n");
485 ok(readden
== sizeof(obuf2
) - 8, "read got %d bytes 9\n", readden
);
486 ok(memcmp(obuf2
, ibuf
, sizeof(obuf2
)) == 0, "content check 9\n");
489 /* Now the reverse direction */
490 memset(ibuf
, 0, sizeof(ibuf
));
491 ok(WriteFile(hFile
, obuf2
, sizeof(obuf2
), &written
, NULL
), "WriteFile 10\n");
492 ok(written
== sizeof(obuf2
), "write file len 10\n");
493 ok(WriteFile(hFile
, obuf
, sizeof(obuf
), &written
, NULL
), "WriteFile 10\n");
494 ok(written
== sizeof(obuf
), "write file len 10\n");
495 SetLastError(0xdeadbeef);
497 ok(!ReadFile(hnp
, ibuf
, 4, &readden
, NULL
), "ReadFile 10\n");
499 ok(GetLastError() == ERROR_MORE_DATA
, "wrong error 10\n");
500 ok(readden
== 4, "read got %d bytes 10\n", readden
);
501 SetLastError(0xdeadbeef);
502 ret
= RpcReadFile(hnp
, ibuf
+ 4, 4, &readden
, NULL
);
504 ok(!ret
, "RpcReadFile 10\n");
506 ok(GetLastError() == ERROR_MORE_DATA
, "wrong error 10\n");
507 ok(readden
== 4, "read got %d bytes 10\n", readden
);
508 ret
= RpcReadFile(hnp
, ibuf
+ 8, sizeof(ibuf
), &readden
, NULL
);
509 ok(ret
, "RpcReadFile 10\n");
511 ok(readden
== sizeof(obuf2
) - 8, "read got %d bytes 10\n", readden
);
512 ok(memcmp(obuf2
, ibuf
, sizeof(obuf2
)) == 0, "content check 10\n");
513 if (readden
<= sizeof(obuf2
) - 8) /* blocks forever if second part was already received */
515 memset(ibuf
, 0, sizeof(ibuf
));
516 SetLastError(0xdeadbeef);
517 ret
= RpcReadFile(hnp
, ibuf
, 4, &readden
, NULL
);
518 ok(!ret
, "RpcReadFile 10\n");
520 ok(GetLastError() == ERROR_MORE_DATA
, "wrong error 10\n");
521 ok(readden
== 4, "read got %d bytes 10\n", readden
);
522 SetLastError(0xdeadbeef);
524 ok(!ReadFile(hnp
, ibuf
+ 4, 4, &readden
, NULL
), "ReadFile 10\n");
526 ok(GetLastError() == ERROR_MORE_DATA
, "wrong error 10\n");
527 ok(readden
== 4, "read got %d bytes 10\n", readden
);
528 ret
= RpcReadFile(hnp
, ibuf
+ 8, sizeof(ibuf
), &readden
, NULL
);
529 ok(ret
, "RpcReadFile 10\n");
530 ok(readden
== sizeof(obuf
) - 8, "read got %d bytes 10\n", readden
);
531 ok(memcmp(obuf
, ibuf
, sizeof(obuf
)) == 0, "content check 10\n");
536 /* Picky conformance tests */
538 /* Verify that you can't connect to pipe again
539 * until server calls DisconnectNamedPipe+ConnectNamedPipe
540 * or creates a new pipe
541 * case 1: other client not yet closed
543 hFile2
= CreateFileA(PIPENAME
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0);
544 ok(hFile2
== INVALID_HANDLE_VALUE
,
545 "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail\n");
546 ok(GetLastError() == ERROR_PIPE_BUSY
,
547 "connecting to named pipe before other client closes should fail with ERROR_PIPE_BUSY\n");
549 ok(CloseHandle(hFile
), "CloseHandle\n");
551 /* case 2: other client already closed */
552 hFile
= CreateFileA(PIPENAME
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0);
553 ok(hFile
== INVALID_HANDLE_VALUE
,
554 "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail\n");
555 ok(GetLastError() == ERROR_PIPE_BUSY
,
556 "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail with ERROR_PIPE_BUSY\n");
558 ok(DisconnectNamedPipe(hnp
), "DisconnectNamedPipe\n");
560 /* case 3: server has called DisconnectNamedPipe but not ConnectNamed Pipe */
561 hFile
= CreateFileA(PIPENAME
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0);
562 ok(hFile
== INVALID_HANDLE_VALUE
,
563 "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail\n");
564 ok(GetLastError() == ERROR_PIPE_BUSY
,
565 "connecting to named pipe after other client closes but before ConnectNamedPipe should fail with ERROR_PIPE_BUSY\n");
567 /* to be complete, we'd call ConnectNamedPipe here and loop,
568 * but by default that's blocking, so we'd either have
569 * to turn on the uncommon nonblocking mode, or
570 * use another thread.
574 ok(CloseHandle(hnp
), "CloseHandle\n");
576 trace("test_CreateNamedPipe returning\n");
579 static void test_CreateNamedPipe_instances_must_match(void)
583 /* Check no mismatch */
584 hnp
= CreateNamedPipeA(PIPENAME
, PIPE_ACCESS_DUPLEX
, PIPE_TYPE_BYTE
| PIPE_WAIT
,
585 /* nMaxInstances */ 2,
586 /* nOutBufSize */ 1024,
587 /* nInBufSize */ 1024,
588 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
589 /* lpSecurityAttrib */ NULL
);
590 ok(hnp
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
592 hnp2
= CreateNamedPipeA(PIPENAME
, PIPE_ACCESS_DUPLEX
, PIPE_TYPE_BYTE
| PIPE_WAIT
,
593 /* nMaxInstances */ 2,
594 /* nOutBufSize */ 1024,
595 /* nInBufSize */ 1024,
596 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
597 /* lpSecurityAttrib */ NULL
);
598 ok(hnp2
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
600 ok(CloseHandle(hnp
), "CloseHandle\n");
601 ok(CloseHandle(hnp2
), "CloseHandle\n");
603 /* Check nMaxInstances */
604 hnp
= CreateNamedPipeA(PIPENAME
, PIPE_ACCESS_DUPLEX
, PIPE_TYPE_BYTE
| PIPE_WAIT
,
605 /* nMaxInstances */ 1,
606 /* nOutBufSize */ 1024,
607 /* nInBufSize */ 1024,
608 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
609 /* lpSecurityAttrib */ NULL
);
610 ok(hnp
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
612 hnp2
= CreateNamedPipeA(PIPENAME
, PIPE_ACCESS_DUPLEX
, PIPE_TYPE_BYTE
| PIPE_WAIT
,
613 /* nMaxInstances */ 1,
614 /* nOutBufSize */ 1024,
615 /* nInBufSize */ 1024,
616 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
617 /* lpSecurityAttrib */ NULL
);
618 ok(hnp2
== INVALID_HANDLE_VALUE
619 && GetLastError() == ERROR_PIPE_BUSY
, "nMaxInstances not obeyed\n");
621 ok(CloseHandle(hnp
), "CloseHandle\n");
623 /* Check PIPE_ACCESS_* */
624 hnp
= CreateNamedPipeA(PIPENAME
, PIPE_ACCESS_DUPLEX
, PIPE_TYPE_BYTE
| PIPE_WAIT
,
625 /* nMaxInstances */ 2,
626 /* nOutBufSize */ 1024,
627 /* nInBufSize */ 1024,
628 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
629 /* lpSecurityAttrib */ NULL
);
630 ok(hnp
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
632 hnp2
= CreateNamedPipeA(PIPENAME
, PIPE_ACCESS_INBOUND
, PIPE_TYPE_BYTE
| PIPE_WAIT
,
633 /* nMaxInstances */ 2,
634 /* nOutBufSize */ 1024,
635 /* nInBufSize */ 1024,
636 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
637 /* lpSecurityAttrib */ NULL
);
638 ok(hnp2
== INVALID_HANDLE_VALUE
639 && GetLastError() == ERROR_ACCESS_DENIED
, "PIPE_ACCESS_* mismatch allowed\n");
641 ok(CloseHandle(hnp
), "CloseHandle\n");
643 /* check everything else */
644 hnp
= CreateNamedPipeA(PIPENAME
, PIPE_ACCESS_DUPLEX
, PIPE_TYPE_BYTE
| PIPE_WAIT
,
645 /* nMaxInstances */ 4,
646 /* nOutBufSize */ 1024,
647 /* nInBufSize */ 1024,
648 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
649 /* lpSecurityAttrib */ NULL
);
650 ok(hnp
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
652 hnp2
= CreateNamedPipeA(PIPENAME
, PIPE_ACCESS_DUPLEX
, PIPE_TYPE_MESSAGE
,
653 /* nMaxInstances */ 3,
654 /* nOutBufSize */ 102,
656 /* nDefaultWait */ 1234,
657 /* lpSecurityAttrib */ NULL
);
658 ok(hnp2
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
660 ok(CloseHandle(hnp
), "CloseHandle\n");
661 ok(CloseHandle(hnp2
), "CloseHandle\n");
664 /** implementation of alarm() */
665 static DWORD CALLBACK
alarmThreadMain(LPVOID arg
)
667 DWORD_PTR timeout
= (DWORD_PTR
) arg
;
668 trace("alarmThreadMain\n");
669 if (WaitForSingleObject( alarm_event
, timeout
) == WAIT_TIMEOUT
)
671 ok(FALSE
, "alarm\n");
677 static HANDLE hnp
= INVALID_HANDLE_VALUE
;
679 /** Trivial byte echo server - disconnects after each session */
680 static DWORD CALLBACK
serverThreadMain1(LPVOID arg
)
684 trace("serverThreadMain1 start\n");
685 /* Set up a simple echo server */
686 hnp
= CreateNamedPipeA(PIPENAME
"serverThreadMain1", PIPE_ACCESS_DUPLEX
,
687 PIPE_TYPE_BYTE
| PIPE_WAIT
,
688 /* nMaxInstances */ 1,
689 /* nOutBufSize */ 1024,
690 /* nInBufSize */ 1024,
691 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
692 /* lpSecurityAttrib */ NULL
);
694 ok(hnp
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
695 for (i
= 0; i
< NB_SERVER_LOOPS
; i
++) {
701 /* Wait for client to connect */
702 trace("Server calling ConnectNamedPipe...\n");
703 ok(ConnectNamedPipe(hnp
, NULL
)
704 || GetLastError() == ERROR_PIPE_CONNECTED
, "ConnectNamedPipe\n");
705 trace("ConnectNamedPipe returned.\n");
707 /* Echo bytes once */
708 memset(buf
, 0, sizeof(buf
));
710 trace("Server reading...\n");
711 success
= ReadFile(hnp
, buf
, sizeof(buf
), &readden
, NULL
);
712 trace("Server done reading.\n");
713 ok(success
, "ReadFile\n");
714 ok(readden
, "short read\n");
716 trace("Server writing...\n");
717 ok(WriteFile(hnp
, buf
, readden
, &written
, NULL
), "WriteFile\n");
718 trace("Server done writing.\n");
719 ok(written
== readden
, "write file len\n");
721 /* finish this connection, wait for next one */
722 ok(FlushFileBuffers(hnp
), "FlushFileBuffers\n");
723 trace("Server done flushing.\n");
724 ok(DisconnectNamedPipe(hnp
), "DisconnectNamedPipe\n");
725 trace("Server done disconnecting.\n");
730 /** Trivial byte echo server - closes after each connection */
731 static DWORD CALLBACK
serverThreadMain2(LPVOID arg
)
736 trace("serverThreadMain2\n");
737 /* Set up a simple echo server */
738 hnp
= CreateNamedPipeA(PIPENAME
"serverThreadMain2", PIPE_ACCESS_DUPLEX
,
739 PIPE_TYPE_BYTE
| PIPE_WAIT
,
740 /* nMaxInstances */ 2,
741 /* nOutBufSize */ 1024,
742 /* nInBufSize */ 1024,
743 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
744 /* lpSecurityAttrib */ NULL
);
745 ok(hnp
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
747 for (i
= 0; i
< NB_SERVER_LOOPS
; i
++) {
755 user_apc_ran
= FALSE
;
756 if (i
== 0 && pQueueUserAPC
) {
757 trace("Queueing an user APC\n"); /* verify the pipe is non alerable */
758 ret
= pQueueUserAPC(&user_apc
, GetCurrentThread(), 0);
759 ok(ret
, "QueueUserAPC failed: %d\n", GetLastError());
762 /* Wait for client to connect */
763 trace("Server calling ConnectNamedPipe...\n");
764 ok(ConnectNamedPipe(hnp
, NULL
)
765 || GetLastError() == ERROR_PIPE_CONNECTED
, "ConnectNamedPipe\n");
766 trace("ConnectNamedPipe returned.\n");
768 /* Echo bytes once */
769 memset(buf
, 0, sizeof(buf
));
771 trace("Server reading...\n");
772 success
= ReadFile(hnp
, buf
, sizeof(buf
), &readden
, NULL
);
773 trace("Server done reading.\n");
774 ok(success
, "ReadFile\n");
776 trace("Server writing...\n");
777 ok(WriteFile(hnp
, buf
, readden
, &written
, NULL
), "WriteFile\n");
778 trace("Server done writing.\n");
779 ok(written
== readden
, "write file len\n");
781 /* finish this connection, wait for next one */
782 ok(FlushFileBuffers(hnp
), "FlushFileBuffers\n");
783 ok(DisconnectNamedPipe(hnp
), "DisconnectNamedPipe\n");
785 ok(user_apc_ran
== FALSE
, "UserAPC ran, pipe using alertable io mode\n");
787 if (i
== 0 && pQueueUserAPC
)
788 SleepEx(0, TRUE
); /* get rid of apc */
790 /* Set up next echo server */
792 CreateNamedPipeA(PIPENAME
"serverThreadMain2", PIPE_ACCESS_DUPLEX
,
793 PIPE_TYPE_BYTE
| PIPE_WAIT
,
794 /* nMaxInstances */ 2,
795 /* nOutBufSize */ 1024,
796 /* nInBufSize */ 1024,
797 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
798 /* lpSecurityAttrib */ NULL
);
800 ok(hnpNext
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
802 ok(CloseHandle(hnp
), "CloseHandle\n");
808 /** Trivial byte echo server - uses overlapped named pipe calls */
809 static DWORD CALLBACK
serverThreadMain3(LPVOID arg
)
814 trace("serverThreadMain3\n");
815 /* Set up a simple echo server */
816 hnp
= CreateNamedPipeA(PIPENAME
"serverThreadMain3", PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
817 PIPE_TYPE_BYTE
| PIPE_WAIT
,
818 /* nMaxInstances */ 1,
819 /* nOutBufSize */ 1024,
820 /* nInBufSize */ 1024,
821 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
822 /* lpSecurityAttrib */ NULL
);
823 ok(hnp
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
825 hEvent
= CreateEventW(NULL
, /* security attribute */
826 TRUE
, /* manual reset event */
827 FALSE
, /* initial state */
829 ok(hEvent
!= NULL
, "CreateEvent\n");
831 for (i
= 0; i
< NB_SERVER_LOOPS
; i
++) {
838 int letWFSOEwait
= (i
& 2);
839 int letGORwait
= (i
& 1);
842 memset(&oOverlap
, 0, sizeof(oOverlap
));
843 oOverlap
.hEvent
= hEvent
;
845 /* Wait for client to connect */
847 trace("Server calling non-overlapped ConnectNamedPipe on overlapped pipe...\n");
848 success
= ConnectNamedPipe(hnp
, NULL
);
849 err
= GetLastError();
850 ok(success
|| (err
== ERROR_PIPE_CONNECTED
), "ConnectNamedPipe failed: %d\n", err
);
851 trace("ConnectNamedPipe operation complete.\n");
853 trace("Server calling overlapped ConnectNamedPipe...\n");
854 success
= ConnectNamedPipe(hnp
, &oOverlap
);
855 err
= GetLastError();
856 ok(!success
&& (err
== ERROR_IO_PENDING
|| err
== ERROR_PIPE_CONNECTED
), "overlapped ConnectNamedPipe\n");
857 trace("overlapped ConnectNamedPipe returned.\n");
858 if (!success
&& (err
== ERROR_IO_PENDING
)) {
863 ret
= WaitForSingleObjectEx(hEvent
, INFINITE
, TRUE
);
864 } while (ret
== WAIT_IO_COMPLETION
);
865 ok(ret
== 0, "wait ConnectNamedPipe returned %x\n", ret
);
867 success
= GetOverlappedResult(hnp
, &oOverlap
, &dummy
, letGORwait
);
868 if (!letGORwait
&& !letWFSOEwait
&& !success
) {
869 ok(GetLastError() == ERROR_IO_INCOMPLETE
, "GetOverlappedResult\n");
870 success
= GetOverlappedResult(hnp
, &oOverlap
, &dummy
, TRUE
);
873 ok(success
|| (err
== ERROR_PIPE_CONNECTED
), "GetOverlappedResult ConnectNamedPipe\n");
874 trace("overlapped ConnectNamedPipe operation complete.\n");
877 /* Echo bytes once */
878 memset(buf
, 0, sizeof(buf
));
880 trace("Server reading...\n");
881 success
= ReadFile(hnp
, buf
, sizeof(buf
), &readden
, &oOverlap
);
882 trace("Server ReadFile returned...\n");
883 err
= GetLastError();
884 ok(success
|| err
== ERROR_IO_PENDING
, "overlapped ReadFile\n");
885 trace("overlapped ReadFile returned.\n");
886 if (!success
&& (err
== ERROR_IO_PENDING
)) {
891 ret
= WaitForSingleObjectEx(hEvent
, INFINITE
, TRUE
);
892 } while (ret
== WAIT_IO_COMPLETION
);
893 ok(ret
== 0, "wait ReadFile returned %x\n", ret
);
895 success
= GetOverlappedResult(hnp
, &oOverlap
, &readden
, letGORwait
);
896 if (!letGORwait
&& !letWFSOEwait
&& !success
) {
897 ok(GetLastError() == ERROR_IO_INCOMPLETE
, "GetOverlappedResult\n");
898 success
= GetOverlappedResult(hnp
, &oOverlap
, &readden
, TRUE
);
901 trace("Server done reading.\n");
902 ok(success
, "overlapped ReadFile\n");
904 trace("Server writing...\n");
905 success
= WriteFile(hnp
, buf
, readden
, &written
, &oOverlap
);
906 trace("Server WriteFile returned...\n");
907 err
= GetLastError();
908 ok(success
|| err
== ERROR_IO_PENDING
, "overlapped WriteFile\n");
909 trace("overlapped WriteFile returned.\n");
910 if (!success
&& (err
== ERROR_IO_PENDING
)) {
915 ret
= WaitForSingleObjectEx(hEvent
, INFINITE
, TRUE
);
916 } while (ret
== WAIT_IO_COMPLETION
);
917 ok(ret
== 0, "wait WriteFile returned %x\n", ret
);
919 success
= GetOverlappedResult(hnp
, &oOverlap
, &written
, letGORwait
);
920 if (!letGORwait
&& !letWFSOEwait
&& !success
) {
921 ok(GetLastError() == ERROR_IO_INCOMPLETE
, "GetOverlappedResult\n");
922 success
= GetOverlappedResult(hnp
, &oOverlap
, &written
, TRUE
);
925 trace("Server done writing.\n");
926 ok(success
, "overlapped WriteFile\n");
927 ok(written
== readden
, "write file len\n");
929 /* finish this connection, wait for next one */
930 ok(FlushFileBuffers(hnp
), "FlushFileBuffers\n");
931 ok(DisconnectNamedPipe(hnp
), "DisconnectNamedPipe\n");
936 /** Trivial byte echo server - uses i/o completion ports */
937 static DWORD CALLBACK
serverThreadMain4(LPVOID arg
)
943 trace("serverThreadMain4\n");
944 /* Set up a simple echo server */
945 hnp
= CreateNamedPipeA(PIPENAME
"serverThreadMain4", PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
946 PIPE_TYPE_BYTE
| PIPE_WAIT
,
947 /* nMaxInstances */ 1,
948 /* nOutBufSize */ 1024,
949 /* nInBufSize */ 1024,
950 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
951 /* lpSecurityAttrib */ NULL
);
952 ok(hnp
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
954 hcompletion
= CreateIoCompletionPort(hnp
, NULL
, 12345, 1);
955 ok(hcompletion
!= NULL
, "CreateIoCompletionPort failed, error=%i\n", GetLastError());
957 for (i
= 0; i
< NB_SERVER_LOOPS
; i
++) {
970 memset(&oConnect
, 0, sizeof(oConnect
));
971 memset(&oRead
, 0, sizeof(oRead
));
972 memset(&oWrite
, 0, sizeof(oWrite
));
974 /* Wait for client to connect */
975 trace("Server calling overlapped ConnectNamedPipe...\n");
976 success
= ConnectNamedPipe(hnp
, &oConnect
);
977 err
= GetLastError();
978 ok(!success
&& (err
== ERROR_IO_PENDING
|| err
== ERROR_PIPE_CONNECTED
),
979 "overlapped ConnectNamedPipe got %u err %u\n", success
, err
);
980 if (!success
&& err
== ERROR_IO_PENDING
) {
981 trace("ConnectNamedPipe GetQueuedCompletionStatus\n");
982 success
= GetQueuedCompletionStatus(hcompletion
, &dummy
, &compkey
, &oResult
, 0);
985 ok( GetLastError() == WAIT_TIMEOUT
,
986 "ConnectNamedPipe GetQueuedCompletionStatus wrong error %u\n", GetLastError());
987 success
= GetQueuedCompletionStatus(hcompletion
, &dummy
, &compkey
, &oResult
, 10000);
989 ok(success
, "ConnectNamedPipe GetQueuedCompletionStatus failed, errno=%i\n", GetLastError());
992 ok(compkey
== 12345, "got completion key %i instead of 12345\n", (int)compkey
);
993 ok(oResult
== &oConnect
, "got overlapped pointer %p instead of %p\n", oResult
, &oConnect
);
996 trace("overlapped ConnectNamedPipe operation complete.\n");
998 /* Echo bytes once */
999 memset(buf
, 0, sizeof(buf
));
1001 trace("Server reading...\n");
1002 success
= ReadFile(hnp
, buf
, sizeof(buf
), &readden
, &oRead
);
1003 trace("Server ReadFile returned...\n");
1004 err
= GetLastError();
1005 ok(success
|| err
== ERROR_IO_PENDING
, "overlapped ReadFile, err=%i\n", err
);
1006 success
= GetQueuedCompletionStatus(hcompletion
, &readden
, &compkey
,
1008 ok(success
, "ReadFile GetQueuedCompletionStatus failed, errno=%i\n", GetLastError());
1011 ok(compkey
== 12345, "got completion key %i instead of 12345\n", (int)compkey
);
1012 ok(oResult
== &oRead
, "got overlapped pointer %p instead of %p\n", oResult
, &oRead
);
1014 trace("Server done reading.\n");
1016 trace("Server writing...\n");
1017 success
= WriteFile(hnp
, buf
, readden
, &written
, &oWrite
);
1018 trace("Server WriteFile returned...\n");
1019 err
= GetLastError();
1020 ok(success
|| err
== ERROR_IO_PENDING
, "overlapped WriteFile failed, err=%u\n", err
);
1021 success
= GetQueuedCompletionStatus(hcompletion
, &written
, &compkey
,
1023 ok(success
, "WriteFile GetQueuedCompletionStatus failed, errno=%i\n", GetLastError());
1026 ok(compkey
== 12345, "got completion key %i instead of 12345\n", (int)compkey
);
1027 ok(oResult
== &oWrite
, "got overlapped pointer %p instead of %p\n", oResult
, &oWrite
);
1028 ok(written
== readden
, "write file len\n");
1030 trace("Server done writing.\n");
1032 /* finish this connection, wait for next one */
1033 ok(FlushFileBuffers(hnp
), "FlushFileBuffers\n");
1034 success
= DisconnectNamedPipe(hnp
);
1035 ok(success
, "DisconnectNamedPipe failed, err %u\n", GetLastError());
1038 ret
= CloseHandle(hnp
);
1039 ok(ret
, "CloseHandle named pipe failed, err=%i\n", GetLastError());
1040 ret
= CloseHandle(hcompletion
);
1041 ok(ret
, "CloseHandle completion failed, err=%i\n", GetLastError());
1046 static int completion_called
;
1047 static DWORD completion_errorcode
;
1048 static DWORD completion_num_bytes
;
1049 static LPOVERLAPPED completion_lpoverlapped
;
1051 static VOID WINAPI
completion_routine(DWORD errorcode
, DWORD num_bytes
, LPOVERLAPPED lpoverlapped
)
1053 completion_called
++;
1054 completion_errorcode
= errorcode
;
1055 completion_num_bytes
= num_bytes
;
1056 completion_lpoverlapped
= lpoverlapped
;
1057 SetEvent(lpoverlapped
->hEvent
);
1060 /** Trivial byte echo server - uses ReadFileEx/WriteFileEx */
1061 static DWORD CALLBACK
serverThreadMain5(LPVOID arg
)
1066 trace("serverThreadMain5\n");
1067 /* Set up a simple echo server */
1068 hnp
= CreateNamedPipeA(PIPENAME
"serverThreadMain5", PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1069 PIPE_TYPE_BYTE
| PIPE_WAIT
,
1070 /* nMaxInstances */ 1,
1071 /* nOutBufSize */ 1024,
1072 /* nInBufSize */ 1024,
1073 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
1074 /* lpSecurityAttrib */ NULL
);
1075 ok(hnp
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
1077 hEvent
= CreateEventW(NULL
, /* security attribute */
1078 TRUE
, /* manual reset event */
1079 FALSE
, /* initial state */
1081 ok(hEvent
!= NULL
, "CreateEvent\n");
1083 for (i
= 0; i
< NB_SERVER_LOOPS
; i
++) {
1087 OVERLAPPED oOverlap
;
1090 memset(&oOverlap
, 0, sizeof(oOverlap
));
1091 oOverlap
.hEvent
= hEvent
;
1093 /* Wait for client to connect */
1094 trace("Server calling ConnectNamedPipe...\n");
1095 success
= ConnectNamedPipe(hnp
, NULL
);
1096 err
= GetLastError();
1097 ok(success
|| (err
== ERROR_PIPE_CONNECTED
), "ConnectNamedPipe failed: %d\n", err
);
1098 trace("ConnectNamedPipe operation complete.\n");
1100 /* Echo bytes once */
1101 memset(buf
, 0, sizeof(buf
));
1103 trace("Server reading...\n");
1104 completion_called
= 0;
1106 success
= ReadFileEx(hnp
, buf
, sizeof(buf
), &oOverlap
, completion_routine
);
1107 trace("Server ReadFileEx returned...\n");
1108 ok(success
, "ReadFileEx failed, err=%i\n", GetLastError());
1109 ok(completion_called
== 0, "completion routine called before ReadFileEx return\n");
1110 trace("ReadFileEx returned.\n");
1114 ret
= WaitForSingleObjectEx(hEvent
, INFINITE
, TRUE
);
1115 } while (ret
== WAIT_IO_COMPLETION
);
1116 ok(ret
== 0, "wait ReadFileEx returned %x\n", ret
);
1118 ok(completion_called
== 1, "completion routine called %i times\n", completion_called
);
1119 ok(completion_errorcode
== ERROR_SUCCESS
, "completion routine got error %d\n", completion_errorcode
);
1120 ok(completion_num_bytes
!= 0, "read 0 bytes\n");
1121 ok(completion_lpoverlapped
== &oOverlap
, "got wrong overlapped pointer %p\n", completion_lpoverlapped
);
1122 readden
= completion_num_bytes
;
1123 trace("Server done reading.\n");
1125 trace("Server writing...\n");
1126 completion_called
= 0;
1128 success
= WriteFileEx(hnp
, buf
, readden
, &oOverlap
, completion_routine
);
1129 trace("Server WriteFileEx returned...\n");
1130 ok(success
, "WriteFileEx failed, err=%i\n", GetLastError());
1131 ok(completion_called
== 0, "completion routine called before ReadFileEx return\n");
1132 trace("overlapped WriteFile returned.\n");
1136 ret
= WaitForSingleObjectEx(hEvent
, INFINITE
, TRUE
);
1137 } while (ret
== WAIT_IO_COMPLETION
);
1138 ok(ret
== 0, "wait WriteFileEx returned %x\n", ret
);
1140 trace("Server done writing.\n");
1141 ok(completion_called
== 1, "completion routine called %i times\n", completion_called
);
1142 ok(completion_errorcode
== ERROR_SUCCESS
, "completion routine got error %d\n", completion_errorcode
);
1143 ok(completion_num_bytes
== readden
, "read %i bytes wrote %i\n", readden
, completion_num_bytes
);
1144 ok(completion_lpoverlapped
== &oOverlap
, "got wrong overlapped pointer %p\n", completion_lpoverlapped
);
1146 /* finish this connection, wait for next one */
1147 ok(FlushFileBuffers(hnp
), "FlushFileBuffers\n");
1148 ok(DisconnectNamedPipe(hnp
), "DisconnectNamedPipe\n");
1153 static void exercizeServer(const char *pipename
, HANDLE serverThread
)
1157 trace("exercizeServer starting\n");
1158 for (i
= 0; i
< NB_SERVER_LOOPS
; i
++) {
1159 HANDLE hFile
=INVALID_HANDLE_VALUE
;
1160 static const char obuf
[] = "Bit Bucket";
1166 for (loop
= 0; loop
< 3; loop
++) {
1168 trace("Client connecting...\n");
1169 /* Connect to the server */
1170 hFile
= CreateFileA(pipename
, GENERIC_READ
| GENERIC_WRITE
, 0,
1171 NULL
, OPEN_EXISTING
, 0, 0);
1172 if (hFile
!= INVALID_HANDLE_VALUE
)
1174 err
= GetLastError();
1176 ok(err
== ERROR_PIPE_BUSY
|| err
== ERROR_FILE_NOT_FOUND
, "connecting to pipe\n");
1178 ok(err
== ERROR_PIPE_BUSY
, "connecting to pipe\n");
1179 trace("connect failed, retrying\n");
1182 ok(hFile
!= INVALID_HANDLE_VALUE
, "client opening named pipe\n");
1184 /* Make sure it can echo */
1185 memset(ibuf
, 0, sizeof(ibuf
));
1186 trace("Client writing...\n");
1187 ok(WriteFile(hFile
, obuf
, sizeof(obuf
), &written
, NULL
), "WriteFile to client end of pipe\n");
1188 ok(written
== sizeof(obuf
), "write file len\n");
1189 trace("Client reading...\n");
1190 ok(ReadFile(hFile
, ibuf
, sizeof(obuf
), &readden
, NULL
), "ReadFile from client end of pipe\n");
1191 ok(readden
== sizeof(obuf
), "read file len\n");
1192 ok(memcmp(obuf
, ibuf
, written
) == 0, "content check\n");
1194 trace("Client closing...\n");
1195 ok(CloseHandle(hFile
), "CloseHandle\n");
1198 ok(WaitForSingleObject(serverThread
,INFINITE
) == WAIT_OBJECT_0
, "WaitForSingleObject\n");
1200 trace("exercizeServer returning\n");
1203 static void test_NamedPipe_2(void)
1205 HANDLE serverThread
;
1206 DWORD serverThreadId
;
1208 DWORD alarmThreadId
;
1210 trace("test_NamedPipe_2 starting\n");
1211 /* Set up a twenty second timeout */
1212 alarm_event
= CreateEventW( NULL
, TRUE
, FALSE
, NULL
);
1213 SetLastError(0xdeadbeef);
1214 alarmThread
= CreateThread(NULL
, 0, alarmThreadMain
, (void *) 20000, 0, &alarmThreadId
);
1215 ok(alarmThread
!= NULL
, "CreateThread failed: %d\n", GetLastError());
1217 /* The servers we're about to exercise do try to clean up carefully,
1218 * but to reduce the chance of a test failure due to a pipe handle
1219 * leak in the test code, we'll use a different pipe name for each server.
1223 SetLastError(0xdeadbeef);
1224 serverThread
= CreateThread(NULL
, 0, serverThreadMain1
, (void *)8, 0, &serverThreadId
);
1225 ok(serverThread
!= NULL
, "CreateThread failed: %d\n", GetLastError());
1226 exercizeServer(PIPENAME
"serverThreadMain1", serverThread
);
1229 SetLastError(0xdeadbeef);
1230 serverThread
= CreateThread(NULL
, 0, serverThreadMain2
, 0, 0, &serverThreadId
);
1231 ok(serverThread
!= NULL
, "CreateThread failed: %d\n", GetLastError());
1232 exercizeServer(PIPENAME
"serverThreadMain2", serverThread
);
1235 SetLastError(0xdeadbeef);
1236 serverThread
= CreateThread(NULL
, 0, serverThreadMain3
, 0, 0, &serverThreadId
);
1237 ok(serverThread
!= NULL
, "CreateThread failed: %d\n", GetLastError());
1238 exercizeServer(PIPENAME
"serverThreadMain3", serverThread
);
1241 SetLastError(0xdeadbeef);
1242 serverThread
= CreateThread(NULL
, 0, serverThreadMain4
, 0, 0, &serverThreadId
);
1243 ok(serverThread
!= NULL
, "CreateThread failed: %d\n", GetLastError());
1244 exercizeServer(PIPENAME
"serverThreadMain4", serverThread
);
1247 SetLastError(0xdeadbeef);
1248 serverThread
= CreateThread(NULL
, 0, serverThreadMain5
, 0, 0, &serverThreadId
);
1249 ok(serverThread
!= NULL
, "CreateThread failed: %d\n", GetLastError());
1250 exercizeServer(PIPENAME
"serverThreadMain5", serverThread
);
1252 ok(SetEvent( alarm_event
), "SetEvent\n");
1253 CloseHandle( alarm_event
);
1254 trace("test_NamedPipe_2 returning\n");
1257 static int test_DisconnectNamedPipe(void)
1261 static const char obuf
[] = "Bit Bucket";
1267 SetLastError(0xdeadbeef);
1268 hnp
= CreateNamedPipeA(PIPENAME
, PIPE_ACCESS_DUPLEX
, PIPE_TYPE_BYTE
| PIPE_WAIT
,
1269 /* nMaxInstances */ 1,
1270 /* nOutBufSize */ 1024,
1271 /* nInBufSize */ 1024,
1272 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
1273 /* lpSecurityAttrib */ NULL
);
1274 if ((hnp
== INVALID_HANDLE_VALUE
/* Win98 */ || !hnp
/* Win95 */)
1275 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
) {
1277 win_skip("Named pipes are not implemented\n");
1281 ok(WriteFile(hnp
, obuf
, sizeof(obuf
), &written
, NULL
) == 0
1282 && GetLastError() == ERROR_PIPE_LISTENING
, "WriteFile to not-yet-connected pipe\n");
1283 ok(ReadFile(hnp
, ibuf
, sizeof(ibuf
), &readden
, NULL
) == 0
1284 && GetLastError() == ERROR_PIPE_LISTENING
, "ReadFile from not-yet-connected pipe\n");
1286 hFile
= CreateFileA(PIPENAME
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0);
1287 ok(hFile
!= INVALID_HANDLE_VALUE
, "CreateFile failed\n");
1289 /* don't try to do i/o if one side couldn't be opened, as it hangs */
1290 if (hFile
!= INVALID_HANDLE_VALUE
) {
1292 /* see what happens if server calls DisconnectNamedPipe
1293 * when there are bytes in the pipe
1296 ok(WriteFile(hFile
, obuf
, sizeof(obuf
), &written
, NULL
), "WriteFile\n");
1297 ok(written
== sizeof(obuf
), "write file len\n");
1298 ok(DisconnectNamedPipe(hnp
), "DisconnectNamedPipe while messages waiting\n");
1299 ok(WriteFile(hFile
, obuf
, sizeof(obuf
), &written
, NULL
) == 0
1300 && GetLastError() == ERROR_PIPE_NOT_CONNECTED
, "WriteFile to disconnected pipe\n");
1301 ok(ReadFile(hnp
, ibuf
, sizeof(ibuf
), &readden
, NULL
) == 0
1302 && GetLastError() == ERROR_PIPE_NOT_CONNECTED
,
1303 "ReadFile from disconnected pipe with bytes waiting\n");
1304 ok(!DisconnectNamedPipe(hnp
) && GetLastError() == ERROR_PIPE_NOT_CONNECTED
,
1305 "DisconnectNamedPipe worked twice\n");
1306 ret
= WaitForSingleObject(hFile
, 0);
1307 ok(ret
== WAIT_TIMEOUT
, "WaitForSingleObject returned %X\n", ret
);
1308 ok(CloseHandle(hFile
), "CloseHandle\n");
1311 ok(CloseHandle(hnp
), "CloseHandle\n");
1315 static void test_CreatePipe(void)
1317 SECURITY_ATTRIBUTES pipe_attr
;
1318 HANDLE piperead
, pipewrite
;
1325 user_apc_ran
= FALSE
;
1327 ok(pQueueUserAPC(user_apc
, GetCurrentThread(), 0), "couldn't create user apc\n");
1329 pipe_attr
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
1330 pipe_attr
.bInheritHandle
= TRUE
;
1331 pipe_attr
.lpSecurityDescriptor
= NULL
;
1332 ok(CreatePipe(&piperead
, &pipewrite
, &pipe_attr
, 0) != 0, "CreatePipe failed\n");
1333 ok(WriteFile(pipewrite
,PIPENAME
,sizeof(PIPENAME
), &written
, NULL
), "Write to anonymous pipe failed\n");
1334 ok(written
== sizeof(PIPENAME
), "Write to anonymous pipe wrote %d bytes\n", written
);
1335 ok(ReadFile(piperead
,readbuf
,sizeof(readbuf
),&read
, NULL
), "Read from non empty pipe failed\n");
1336 ok(read
== sizeof(PIPENAME
), "Read from anonymous pipe got %d bytes\n", read
);
1337 ok(CloseHandle(pipewrite
), "CloseHandle for the write pipe failed\n");
1338 ok(CloseHandle(piperead
), "CloseHandle for the read pipe failed\n");
1340 /* Now write another chunk*/
1341 ok(CreatePipe(&piperead
, &pipewrite
, &pipe_attr
, 0) != 0, "CreatePipe failed\n");
1342 ok(WriteFile(pipewrite
,PIPENAME
,sizeof(PIPENAME
), &written
, NULL
), "Write to anonymous pipe failed\n");
1343 ok(written
== sizeof(PIPENAME
), "Write to anonymous pipe wrote %d bytes\n", written
);
1344 /* and close the write end, read should still succeed*/
1345 ok(CloseHandle(pipewrite
), "CloseHandle for the Write Pipe failed\n");
1346 ok(ReadFile(piperead
,readbuf
,sizeof(readbuf
),&read
, NULL
), "Read from broken pipe withe with pending data failed\n");
1347 ok(read
== sizeof(PIPENAME
), "Read from anonymous pipe got %d bytes\n", read
);
1348 /* But now we need to get informed that the pipe is closed */
1349 ok(ReadFile(piperead
,readbuf
,sizeof(readbuf
),&read
, NULL
) == 0, "Broken pipe not detected\n");
1350 ok(CloseHandle(piperead
), "CloseHandle for the read pipe failed\n");
1352 /* Try bigger chunks */
1354 buffer
= HeapAlloc( GetProcessHeap(), 0, size
);
1355 for (i
= 0; i
< size
; i
++) buffer
[i
] = i
;
1356 ok(CreatePipe(&piperead
, &pipewrite
, &pipe_attr
, (size
+ 24)) != 0, "CreatePipe failed\n");
1357 ok(WriteFile(pipewrite
, buffer
, size
, &written
, NULL
), "Write to anonymous pipe failed\n");
1358 ok(written
== size
, "Write to anonymous pipe wrote %d bytes\n", written
);
1359 /* and close the write end, read should still succeed*/
1360 ok(CloseHandle(pipewrite
), "CloseHandle for the Write Pipe failed\n");
1361 memset( buffer
, 0, size
);
1362 ok(ReadFile(piperead
, buffer
, size
, &read
, NULL
), "Read from broken pipe withe with pending data failed\n");
1363 ok(read
== size
, "Read from anonymous pipe got %d bytes\n", read
);
1364 for (i
= 0; i
< size
; i
++) ok( buffer
[i
] == (BYTE
)i
, "invalid data %x at %x\n", buffer
[i
], i
);
1365 /* But now we need to get informed that the pipe is closed */
1366 ok(ReadFile(piperead
,readbuf
,sizeof(readbuf
),&read
, NULL
) == 0, "Broken pipe not detected\n");
1367 ok(CloseHandle(piperead
), "CloseHandle for the read pipe failed\n");
1368 HeapFree(GetProcessHeap(), 0, buffer
);
1370 ok(user_apc_ran
== FALSE
, "user apc ran, pipe using alertable io mode\n");
1371 SleepEx(0, TRUE
); /* get rid of apc */
1374 struct named_pipe_client_params
1376 DWORD security_flags
;
1381 #define PIPE_NAME "\\\\.\\pipe\\named_pipe_test"
1383 static DWORD CALLBACK
named_pipe_client_func(LPVOID p
)
1385 struct named_pipe_client_params
*params
= p
;
1388 const char message
[] = "Test";
1389 DWORD bytes_read
, bytes_written
;
1391 TOKEN_PRIVILEGES
*Privileges
= NULL
;
1397 /* modify the token so we can tell if the pipe impersonation
1398 * token reverts to the process token */
1399 ret
= AdjustTokenPrivileges(params
->token
, TRUE
, NULL
, 0, NULL
, NULL
);
1400 ok(ret
, "AdjustTokenPrivileges failed with error %d\n", GetLastError());
1402 ret
= SetThreadToken(NULL
, params
->token
);
1403 ok(ret
, "SetThreadToken failed with error %d\n", GetLastError());
1408 HANDLE process_token
;
1410 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY
|TOKEN_ADJUST_PRIVILEGES
, &process_token
);
1411 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
1413 ret
= GetTokenInformation(process_token
, TokenPrivileges
, NULL
, 0, &Size
);
1414 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "GetTokenInformation(TokenPrivileges) failed with %d\n", GetLastError());
1415 Privileges
= HeapAlloc(GetProcessHeap(), 0, Size
);
1416 ret
= GetTokenInformation(process_token
, TokenPrivileges
, Privileges
, Size
, &Size
);
1417 ok(ret
, "GetTokenInformation(TokenPrivileges) failed with %d\n", GetLastError());
1419 ret
= AdjustTokenPrivileges(process_token
, TRUE
, NULL
, 0, NULL
, NULL
);
1420 ok(ret
, "AdjustTokenPrivileges failed with error %d\n", GetLastError());
1422 CloseHandle(process_token
);
1425 pipe
= CreateFileA(PIPE_NAME
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, params
->security_flags
, NULL
);
1426 ok(pipe
!= INVALID_HANDLE_VALUE
, "CreateFile for pipe failed with error %d\n", GetLastError());
1428 ret
= WriteFile(pipe
, message
, sizeof(message
), &bytes_written
, NULL
);
1429 ok(ret
, "WriteFile failed with error %d\n", GetLastError());
1431 ret
= ReadFile(pipe
, &dummy
, sizeof(dummy
), &bytes_read
, NULL
);
1432 ok(ret
, "ReadFile failed with error %d\n", GetLastError());
1438 ret
= RevertToSelf();
1439 ok(ret
, "RevertToSelf failed with error %d\n", GetLastError());
1443 ret
= AdjustTokenPrivileges(params
->token
, TRUE
, NULL
, 0, NULL
, NULL
);
1444 ok(ret
, "AdjustTokenPrivileges failed with error %d\n", GetLastError());
1449 HANDLE process_token
;
1451 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES
, &process_token
);
1452 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
1454 ret
= AdjustTokenPrivileges(process_token
, FALSE
, Privileges
, 0, NULL
, NULL
);
1455 ok(ret
, "AdjustTokenPrivileges failed with error %d\n", GetLastError());
1457 HeapFree(GetProcessHeap(), 0, Privileges
);
1459 CloseHandle(process_token
);
1462 ret
= WriteFile(pipe
, message
, sizeof(message
), &bytes_written
, NULL
);
1463 ok(ret
, "WriteFile failed with error %d\n", GetLastError());
1465 ret
= ReadFile(pipe
, &dummy
, sizeof(dummy
), &bytes_read
, NULL
);
1466 ok(ret
, "ReadFile failed with error %d\n", GetLastError());
1473 static HANDLE
make_impersonation_token(DWORD Access
, SECURITY_IMPERSONATION_LEVEL ImpersonationLevel
)
1475 HANDLE ProcessToken
;
1476 HANDLE Token
= NULL
;
1479 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE
, &ProcessToken
);
1480 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
1482 ret
= pDuplicateTokenEx(ProcessToken
, Access
, NULL
, ImpersonationLevel
, TokenImpersonation
, &Token
);
1483 ok(ret
, "DuplicateToken failed with error %d\n", GetLastError());
1485 CloseHandle(ProcessToken
);
1490 static void test_ImpersonateNamedPipeClient(HANDLE hClientToken
, DWORD security_flags
, BOOL revert
, void (*test_func
)(int, HANDLE
))
1499 struct named_pipe_client_params params
;
1501 DWORD dwBytesWritten
;
1502 HANDLE hToken
= NULL
;
1503 SECURITY_IMPERSONATION_LEVEL ImpersonationLevel
;
1506 hPipeServer
= CreateNamedPipeA(PIPE_NAME
, PIPE_ACCESS_DUPLEX
, PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
| PIPE_WAIT
, 1, 100, 100, NMPWAIT_USE_DEFAULT_WAIT
, NULL
);
1507 ok(hPipeServer
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed with error %d\n", GetLastError());
1509 params
.security_flags
= security_flags
;
1510 params
.token
= hClientToken
;
1511 params
.revert
= revert
;
1512 hThread
= CreateThread(NULL
, 0, named_pipe_client_func
, ¶ms
, 0, &dwTid
);
1513 ok(hThread
!= NULL
, "CreateThread failed with error %d\n", GetLastError());
1515 SetLastError(0xdeadbeef);
1516 ret
= ImpersonateNamedPipeClient(hPipeServer
);
1517 error
= GetLastError();
1518 ok(ret
/* win2k3 */ || (error
== ERROR_CANNOT_IMPERSONATE
),
1519 "ImpersonateNamedPipeClient should have failed with ERROR_CANNOT_IMPERSONATE instead of %d\n", GetLastError());
1521 ret
= ConnectNamedPipe(hPipeServer
, NULL
);
1522 ok(ret
|| (GetLastError() == ERROR_PIPE_CONNECTED
), "ConnectNamedPipe failed with error %d\n", GetLastError());
1524 ret
= ReadFile(hPipeServer
, buffer
, sizeof(buffer
), &dwBytesRead
, NULL
);
1525 ok(ret
, "ReadFile failed with error %d\n", GetLastError());
1527 ret
= ImpersonateNamedPipeClient(hPipeServer
);
1528 ok(ret
, "ImpersonateNamedPipeClient failed with error %d\n", GetLastError());
1530 ret
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
, FALSE
, &hToken
);
1531 ok(ret
, "OpenThreadToken failed with error %d\n", GetLastError());
1533 (*test_func
)(0, hToken
);
1535 ImpersonationLevel
= 0xdeadbeef; /* to avoid false positives */
1536 ret
= GetTokenInformation(hToken
, TokenImpersonationLevel
, &ImpersonationLevel
, sizeof(ImpersonationLevel
), &size
);
1537 ok(ret
, "GetTokenInformation(TokenImpersonationLevel) failed with error %d\n", GetLastError());
1538 ok(ImpersonationLevel
== SecurityImpersonation
, "ImpersonationLevel should have been SecurityImpersonation(%d) instead of %d\n", SecurityImpersonation
, ImpersonationLevel
);
1540 CloseHandle(hToken
);
1544 ret
= WriteFile(hPipeServer
, &dummy
, sizeof(dummy
), &dwBytesWritten
, NULL
);
1545 ok(ret
, "WriteFile failed with error %d\n", GetLastError());
1547 ret
= ReadFile(hPipeServer
, buffer
, sizeof(buffer
), &dwBytesRead
, NULL
);
1548 ok(ret
, "ReadFile failed with error %d\n", GetLastError());
1550 ret
= ImpersonateNamedPipeClient(hPipeServer
);
1551 ok(ret
, "ImpersonateNamedPipeClient failed with error %d\n", GetLastError());
1553 ret
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
, FALSE
, &hToken
);
1554 ok(ret
, "OpenThreadToken failed with error %d\n", GetLastError());
1556 (*test_func
)(1, hToken
);
1558 CloseHandle(hToken
);
1562 ret
= WriteFile(hPipeServer
, &dummy
, sizeof(dummy
), &dwBytesWritten
, NULL
);
1563 ok(ret
, "WriteFile failed with error %d\n", GetLastError());
1565 WaitForSingleObject(hThread
, INFINITE
);
1567 ret
= ImpersonateNamedPipeClient(hPipeServer
);
1568 ok(ret
, "ImpersonateNamedPipeClient failed with error %d\n", GetLastError());
1572 CloseHandle(hThread
);
1573 CloseHandle(hPipeServer
);
1576 static BOOL
are_all_privileges_disabled(HANDLE hToken
)
1579 TOKEN_PRIVILEGES
*Privileges
= NULL
;
1581 BOOL all_privs_disabled
= TRUE
;
1584 ret
= GetTokenInformation(hToken
, TokenPrivileges
, NULL
, 0, &Size
);
1585 if (!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
1587 Privileges
= HeapAlloc(GetProcessHeap(), 0, Size
);
1588 ret
= GetTokenInformation(hToken
, TokenPrivileges
, Privileges
, Size
, &Size
);
1591 HeapFree(GetProcessHeap(), 0, Privileges
);
1598 for (i
= 0; i
< Privileges
->PrivilegeCount
; i
++)
1600 if (Privileges
->Privileges
[i
].Attributes
& SE_PRIVILEGE_ENABLED
)
1602 all_privs_disabled
= FALSE
;
1607 HeapFree(GetProcessHeap(), 0, Privileges
);
1609 return all_privs_disabled
;
1612 static DWORD
get_privilege_count(HANDLE hToken
)
1614 TOKEN_STATISTICS Statistics
;
1615 DWORD Size
= sizeof(Statistics
);
1618 ret
= GetTokenInformation(hToken
, TokenStatistics
, &Statistics
, Size
, &Size
);
1619 ok(ret
, "GetTokenInformation(TokenStatistics)\n");
1620 if (!ret
) return -1;
1622 return Statistics
.PrivilegeCount
;
1625 static void test_no_sqos_no_token(int call_index
, HANDLE hToken
)
1632 priv_count
= get_privilege_count(hToken
);
1634 ok(priv_count
== 0, "privilege count should have been 0 instead of %d\n", priv_count
);
1637 priv_count
= get_privilege_count(hToken
);
1638 ok(priv_count
> 0, "privilege count should now be > 0 instead of 0\n");
1639 ok(!are_all_privileges_disabled(hToken
), "impersonated token should not have been modified\n");
1642 ok(0, "shouldn't happen\n");
1646 static void test_no_sqos(int call_index
, HANDLE hToken
)
1651 ok(!are_all_privileges_disabled(hToken
), "token should be a copy of the process one\n");
1655 ok(are_all_privileges_disabled(hToken
), "impersonated token should have been modified\n");
1658 ok(0, "shouldn't happen\n");
1662 static void test_static_context(int call_index
, HANDLE hToken
)
1667 ok(!are_all_privileges_disabled(hToken
), "token should be a copy of the process one\n");
1670 ok(!are_all_privileges_disabled(hToken
), "impersonated token should not have been modified\n");
1673 ok(0, "shouldn't happen\n");
1677 static void test_dynamic_context(int call_index
, HANDLE hToken
)
1682 ok(!are_all_privileges_disabled(hToken
), "token should be a copy of the process one\n");
1686 ok(are_all_privileges_disabled(hToken
), "impersonated token should have been modified\n");
1689 ok(0, "shouldn't happen\n");
1693 static void test_dynamic_context_no_token(int call_index
, HANDLE hToken
)
1698 ok(are_all_privileges_disabled(hToken
), "token should be a copy of the process one\n");
1701 ok(!are_all_privileges_disabled(hToken
), "process token modification should have been detected and impersonation token updated\n");
1704 ok(0, "shouldn't happen\n");
1708 static void test_no_sqos_revert(int call_index
, HANDLE hToken
)
1714 priv_count
= get_privilege_count(hToken
);
1716 ok(priv_count
== 0, "privilege count should have been 0 instead of %d\n", priv_count
);
1719 priv_count
= get_privilege_count(hToken
);
1720 ok(priv_count
> 0, "privilege count should now be > 0 instead of 0\n");
1721 ok(!are_all_privileges_disabled(hToken
), "impersonated token should not have been modified\n");
1724 ok(0, "shouldn't happen\n");
1728 static void test_static_context_revert(int call_index
, HANDLE hToken
)
1734 ok(are_all_privileges_disabled(hToken
), "privileges should have been disabled\n");
1738 ok(are_all_privileges_disabled(hToken
), "impersonated token should not have been modified\n");
1741 ok(0, "shouldn't happen\n");
1745 static void test_dynamic_context_revert(int call_index
, HANDLE hToken
)
1751 ok(are_all_privileges_disabled(hToken
), "privileges should have been disabled\n");
1754 ok(!are_all_privileges_disabled(hToken
), "impersonated token should now be process token\n");
1757 ok(0, "shouldn't happen\n");
1761 static void test_impersonation(void)
1763 HANDLE hClientToken
;
1764 HANDLE hProcessToken
;
1767 if( !pDuplicateTokenEx
) {
1768 skip("DuplicateTokenEx not found\n");
1772 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY
, &hProcessToken
);
1775 skip("couldn't open process token, skipping impersonation tests\n");
1779 if (!get_privilege_count(hProcessToken
) || are_all_privileges_disabled(hProcessToken
))
1781 skip("token didn't have any privileges or they were all disabled. token not suitable for impersonation tests\n");
1782 CloseHandle(hProcessToken
);
1785 CloseHandle(hProcessToken
);
1787 test_ImpersonateNamedPipeClient(NULL
, 0, FALSE
, test_no_sqos_no_token
);
1788 hClientToken
= make_impersonation_token(TOKEN_IMPERSONATE
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY
, SecurityImpersonation
);
1789 test_ImpersonateNamedPipeClient(hClientToken
, 0, FALSE
, test_no_sqos
);
1790 CloseHandle(hClientToken
);
1791 hClientToken
= make_impersonation_token(TOKEN_IMPERSONATE
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY
, SecurityImpersonation
);
1792 test_ImpersonateNamedPipeClient(hClientToken
,
1793 SECURITY_SQOS_PRESENT
| SECURITY_IMPERSONATION
, FALSE
,
1794 test_static_context
);
1795 CloseHandle(hClientToken
);
1796 hClientToken
= make_impersonation_token(TOKEN_IMPERSONATE
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY
, SecurityImpersonation
);
1797 test_ImpersonateNamedPipeClient(hClientToken
,
1798 SECURITY_SQOS_PRESENT
| SECURITY_CONTEXT_TRACKING
| SECURITY_IMPERSONATION
,
1799 FALSE
, test_dynamic_context
);
1800 CloseHandle(hClientToken
);
1801 test_ImpersonateNamedPipeClient(NULL
,
1802 SECURITY_SQOS_PRESENT
| SECURITY_CONTEXT_TRACKING
| SECURITY_IMPERSONATION
,
1803 FALSE
, test_dynamic_context_no_token
);
1805 hClientToken
= make_impersonation_token(TOKEN_IMPERSONATE
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY
, SecurityImpersonation
);
1806 test_ImpersonateNamedPipeClient(hClientToken
, 0, TRUE
, test_no_sqos_revert
);
1807 CloseHandle(hClientToken
);
1808 hClientToken
= make_impersonation_token(TOKEN_IMPERSONATE
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY
, SecurityImpersonation
);
1809 test_ImpersonateNamedPipeClient(hClientToken
,
1810 SECURITY_SQOS_PRESENT
| SECURITY_IMPERSONATION
, TRUE
,
1811 test_static_context_revert
);
1812 CloseHandle(hClientToken
);
1813 hClientToken
= make_impersonation_token(TOKEN_IMPERSONATE
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY
, SecurityImpersonation
);
1814 test_ImpersonateNamedPipeClient(hClientToken
,
1815 SECURITY_SQOS_PRESENT
| SECURITY_CONTEXT_TRACKING
| SECURITY_IMPERSONATION
,
1816 TRUE
, test_dynamic_context_revert
);
1817 CloseHandle(hClientToken
);
1820 struct overlapped_server_args
1822 HANDLE pipe_created
;
1825 static DWORD CALLBACK
overlapped_server(LPVOID arg
)
1830 struct overlapped_server_args
*a
= arg
;
1834 pipe
= CreateNamedPipeA("\\\\.\\pipe\\my pipe", FILE_FLAG_OVERLAPPED
| PIPE_ACCESS_DUPLEX
, PIPE_TYPE_MESSAGE
| PIPE_READMODE_MESSAGE
, 1, 0, 0, 100000, NULL
);
1835 ok(pipe
!= NULL
, "pipe NULL\n");
1837 ol
.hEvent
= CreateEventA(0, 1, 0, 0);
1838 ok(ol
.hEvent
!= NULL
, "event NULL\n");
1839 ret
= ConnectNamedPipe(pipe
, &ol
);
1840 err
= GetLastError();
1841 ok(ret
== 0, "ret %d\n", ret
);
1842 ok(err
== ERROR_IO_PENDING
, "gle %d\n", err
);
1843 SetEvent(a
->pipe_created
);
1845 ret
= WaitForSingleObjectEx(ol
.hEvent
, INFINITE
, 1);
1846 ok(ret
== WAIT_OBJECT_0
, "ret %x\n", ret
);
1848 ret
= GetOverlappedResult(pipe
, &ol
, &num
, 1);
1849 ok(ret
== 1, "ret %d\n", ret
);
1851 /* This should block */
1852 ret
= ReadFile(pipe
, buf
, sizeof(buf
), &num
, NULL
);
1853 ok(ret
== 1, "ret %d\n", ret
);
1855 DisconnectNamedPipe(pipe
);
1856 CloseHandle(ol
.hEvent
);
1861 static void test_overlapped(void)
1864 HANDLE thread
, pipe
;
1866 struct overlapped_server_args args
;
1868 args
.pipe_created
= CreateEventA(0, 1, 0, 0);
1869 thread
= CreateThread(NULL
, 0, overlapped_server
, &args
, 0, &tid
);
1871 WaitForSingleObject(args
.pipe_created
, INFINITE
);
1872 pipe
= CreateFileA("\\\\.\\pipe\\my pipe", GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, NULL
);
1873 ok(pipe
!= INVALID_HANDLE_VALUE
, "cf failed\n");
1875 /* Sleep to try to get the ReadFile in the server to occur before the following WriteFile */
1878 ret
= WriteFile(pipe
, "x", 1, &num
, NULL
);
1879 ok(ret
, "WriteFile failed with error %d\n", GetLastError());
1881 WaitForSingleObject(thread
, INFINITE
);
1883 CloseHandle(args
.pipe_created
);
1884 CloseHandle(thread
);
1887 static void test_NamedPipeHandleState(void)
1889 HANDLE server
, client
;
1891 DWORD state
, instances
, maxCollectionCount
, collectDataTimeout
;
1892 char userName
[MAX_PATH
];
1894 server
= CreateNamedPipeA(PIPENAME
, PIPE_ACCESS_DUPLEX
,
1895 /* dwOpenMode */ PIPE_TYPE_BYTE
| PIPE_WAIT
,
1896 /* nMaxInstances */ 1,
1897 /* nOutBufSize */ 1024,
1898 /* nInBufSize */ 1024,
1899 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
1900 /* lpSecurityAttrib */ NULL
);
1901 ok(server
!= INVALID_HANDLE_VALUE
, "cf failed\n");
1902 ret
= GetNamedPipeHandleStateA(server
, NULL
, NULL
, NULL
, NULL
, NULL
, 0);
1903 ok(ret
, "GetNamedPipeHandleState failed: %d\n", GetLastError());
1904 ret
= GetNamedPipeHandleStateA(server
, &state
, &instances
, NULL
, NULL
, NULL
,
1906 ok(ret
, "GetNamedPipeHandleState failed: %d\n", GetLastError());
1909 ok(state
== 0, "unexpected state %08x\n", state
);
1910 ok(instances
== 1, "expected 1 instances, got %d\n", instances
);
1912 /* Some parameters have no meaning, and therefore can't be retrieved,
1915 SetLastError(0xdeadbeef);
1916 ret
= GetNamedPipeHandleStateA(server
, &state
, &instances
,
1917 &maxCollectionCount
, &collectDataTimeout
, userName
,
1918 sizeof(userName
) / sizeof(userName
[0]));
1920 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
1921 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1922 /* A byte-mode pipe server can't be changed to message mode. */
1923 state
= PIPE_READMODE_MESSAGE
;
1924 SetLastError(0xdeadbeef);
1925 ret
= SetNamedPipeHandleState(server
, &state
, NULL
, NULL
);
1926 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
1927 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1929 client
= CreateFileA(PIPENAME
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1930 OPEN_EXISTING
, 0, NULL
);
1931 ok(client
!= INVALID_HANDLE_VALUE
, "cf failed\n");
1933 state
= PIPE_READMODE_BYTE
;
1934 ret
= SetNamedPipeHandleState(client
, &state
, NULL
, NULL
);
1935 ok(ret
, "SetNamedPipeHandleState failed: %d\n", GetLastError());
1936 /* A byte-mode pipe client can't be changed to message mode, either. */
1937 state
= PIPE_READMODE_MESSAGE
;
1938 SetLastError(0xdeadbeef);
1939 ret
= SetNamedPipeHandleState(server
, &state
, NULL
, NULL
);
1940 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
1941 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1943 CloseHandle(client
);
1944 CloseHandle(server
);
1946 server
= CreateNamedPipeA(PIPENAME
, PIPE_ACCESS_DUPLEX
,
1947 /* dwOpenMode */ PIPE_TYPE_MESSAGE
| PIPE_WAIT
,
1948 /* nMaxInstances */ 1,
1949 /* nOutBufSize */ 1024,
1950 /* nInBufSize */ 1024,
1951 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
1952 /* lpSecurityAttrib */ NULL
);
1953 ok(server
!= INVALID_HANDLE_VALUE
, "cf failed\n");
1954 ret
= GetNamedPipeHandleStateA(server
, NULL
, NULL
, NULL
, NULL
, NULL
, 0);
1955 ok(ret
, "GetNamedPipeHandleState failed: %d\n", GetLastError());
1956 ret
= GetNamedPipeHandleStateA(server
, &state
, &instances
, NULL
, NULL
, NULL
,
1958 ok(ret
, "GetNamedPipeHandleState failed: %d\n", GetLastError());
1961 ok(state
== 0, "unexpected state %08x\n", state
);
1962 ok(instances
== 1, "expected 1 instances, got %d\n", instances
);
1964 /* In contrast to byte-mode pipes, a message-mode pipe server can be
1965 * changed to byte mode.
1967 state
= PIPE_READMODE_BYTE
;
1968 ret
= SetNamedPipeHandleState(server
, &state
, NULL
, NULL
);
1969 ok(ret
, "SetNamedPipeHandleState failed: %d\n", GetLastError());
1971 client
= CreateFileA(PIPENAME
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1972 OPEN_EXISTING
, 0, NULL
);
1973 ok(client
!= INVALID_HANDLE_VALUE
, "cf failed\n");
1975 state
= PIPE_READMODE_MESSAGE
;
1976 ret
= SetNamedPipeHandleState(client
, &state
, NULL
, NULL
);
1977 ok(ret
, "SetNamedPipeHandleState failed: %d\n", GetLastError());
1978 /* A message-mode pipe client can also be changed to byte mode.
1980 state
= PIPE_READMODE_BYTE
;
1981 ret
= SetNamedPipeHandleState(client
, &state
, NULL
, NULL
);
1982 ok(ret
, "SetNamedPipeHandleState failed: %d\n", GetLastError());
1984 CloseHandle(client
);
1985 CloseHandle(server
);
1988 static void test_readfileex_pending(void)
1990 HANDLE server
, client
, event
;
1992 DWORD err
, wait
, num_bytes
;
1993 OVERLAPPED overlapped
;
1994 char read_buf
[1024];
1995 char write_buf
[1024];
1996 const char test_string
[] = "test";
1999 server
= CreateNamedPipeA(PIPENAME
, FILE_FLAG_OVERLAPPED
| PIPE_ACCESS_DUPLEX
,
2000 /* dwOpenMode */ PIPE_TYPE_BYTE
| PIPE_WAIT
,
2001 /* nMaxInstances */ 1,
2002 /* nOutBufSize */ 1024,
2003 /* nInBufSize */ 1024,
2004 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
2005 /* lpSecurityAttrib */ NULL
);
2006 ok(server
!= INVALID_HANDLE_VALUE
, "cf failed\n");
2008 event
= CreateEventA(NULL
, TRUE
, FALSE
, NULL
);
2009 ok(event
!= NULL
, "CreateEventA failed\n");
2011 memset(&overlapped
, 0, sizeof(overlapped
));
2012 overlapped
.hEvent
= event
;
2014 ret
= ConnectNamedPipe(server
, &overlapped
);
2015 err
= GetLastError();
2016 ok(ret
== FALSE
, "ConnectNamedPipe succeeded\n");
2017 ok(err
== ERROR_IO_PENDING
, "ConnectNamedPipe set error %i\n", err
);
2019 wait
= WaitForSingleObject(event
, 0);
2020 ok(wait
== WAIT_TIMEOUT
, "WaitForSingleObject returned %x\n", wait
);
2022 client
= CreateFileA(PIPENAME
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
2023 OPEN_EXISTING
, 0, NULL
);
2024 ok(client
!= INVALID_HANDLE_VALUE
, "cf failed\n");
2026 wait
= WaitForSingleObject(event
, 0);
2027 ok(wait
== WAIT_OBJECT_0
, "WaitForSingleObject returned %x\n", wait
);
2029 /* Start a read that can't complete immediately. */
2030 completion_called
= 0;
2032 ret
= ReadFileEx(server
, read_buf
, sizeof(read_buf
), &overlapped
, completion_routine
);
2033 ok(ret
== TRUE
, "ReadFileEx failed, err=%i\n", GetLastError());
2034 ok(completion_called
== 0, "completion routine called before ReadFileEx returned\n");
2036 ret
= WriteFile(client
, test_string
, strlen(test_string
), &num_bytes
, NULL
);
2037 ok(ret
== TRUE
, "WriteFile failed\n");
2038 ok(num_bytes
== strlen(test_string
), "only %i bytes written\n", num_bytes
);
2040 ok(completion_called
== 0, "completion routine called during WriteFile\n");
2042 wait
= WaitForSingleObjectEx(event
, 0, TRUE
);
2043 ok(wait
== WAIT_IO_COMPLETION
|| wait
== WAIT_OBJECT_0
, "WaitForSingleObjectEx returned %x\n", wait
);
2045 ok(completion_called
== 1, "completion not called after writing pipe\n");
2046 ok(completion_errorcode
== 0, "completion called with error %x\n", completion_errorcode
);
2047 ok(completion_num_bytes
== strlen(test_string
), "ReadFileEx returned only %d bytes\n", completion_num_bytes
);
2048 ok(completion_lpoverlapped
== &overlapped
, "completion called with wrong overlapped pointer\n");
2049 ok(!memcmp(test_string
, read_buf
, strlen(test_string
)), "ReadFileEx read wrong bytes\n");
2051 /* Make writes until the pipe is full and the write fails */
2052 memset(write_buf
, 0xaa, sizeof(write_buf
));
2053 for (i
=0; i
<256; i
++)
2055 completion_called
= 0;
2057 ret
= WriteFileEx(server
, write_buf
, sizeof(write_buf
), &overlapped
, completion_routine
);
2058 err
= GetLastError();
2060 ok(completion_called
== 0, "completion routine called during WriteFileEx\n");
2062 wait
= WaitForSingleObjectEx(event
, 0, TRUE
);
2064 if (wait
== WAIT_TIMEOUT
)
2065 /* write couldn't complete immediately, presumably the pipe is full */
2068 ok(wait
== WAIT_IO_COMPLETION
|| wait
== WAIT_OBJECT_0
, "WaitForSingleObject returned %x\n", wait
);
2070 ok(ret
== TRUE
, "WriteFileEx failed, err=%i\n", err
);
2071 ok(completion_errorcode
== 0, "completion called with error %x\n", completion_errorcode
);
2072 ok(completion_lpoverlapped
== &overlapped
, "completion called with wrong overlapped pointer\n");
2075 ok(ret
== TRUE
, "WriteFileEx failed, err=%i\n", err
);
2076 ok(completion_called
== 0, "completion routine called but wait timed out\n");
2077 ok(completion_errorcode
== 0, "completion called with error %x\n", completion_errorcode
);
2078 ok(completion_lpoverlapped
== &overlapped
, "completion called with wrong overlapped pointer\n");
2080 /* free up some space in the pipe */
2081 ret
= ReadFile(client
, read_buf
, sizeof(read_buf
), &num_bytes
, NULL
);
2082 ok(ret
== TRUE
, "ReadFile failed\n");
2084 ok(completion_called
== 0, "completion routine called during ReadFile\n");
2086 wait
= WaitForSingleObjectEx(event
, 0, TRUE
);
2087 ok(wait
== WAIT_IO_COMPLETION
|| wait
== WAIT_OBJECT_0
, "WaitForSingleObject returned %x\n", wait
);
2088 if (wait
== WAIT_TIMEOUT
)
2090 ret
= ReadFile(client
, read_buf
, sizeof(read_buf
), &num_bytes
, NULL
);
2091 ok(ret
== TRUE
, "ReadFile failed\n");
2092 ok(completion_called
== 0, "completion routine called during ReadFile\n");
2093 wait
= WaitForSingleObjectEx(event
, 0, TRUE
);
2094 ok(wait
== WAIT_IO_COMPLETION
|| wait
== WAIT_OBJECT_0
, "WaitForSingleObject returned %x\n", wait
);
2097 ok(completion_called
== 1, "completion routine not called\n");
2098 ok(completion_errorcode
== 0, "completion called with error %x\n", completion_errorcode
);
2099 ok(completion_lpoverlapped
== &overlapped
, "completion called with wrong overlapped pointer\n");
2101 num_bytes
= 0xdeadbeef;
2102 SetLastError(0xdeadbeef);
2103 ret
= ReadFile(INVALID_HANDLE_VALUE
, read_buf
, 0, &num_bytes
, NULL
);
2104 ok(!ret
, "ReadFile should fail\n");
2105 ok(GetLastError() == ERROR_INVALID_HANDLE
, "wrong error %u\n", GetLastError());
2106 ok(num_bytes
== 0, "expected 0, got %u\n", num_bytes
);
2108 S(U(overlapped
)).Offset
= 0;
2109 S(U(overlapped
)).OffsetHigh
= 0;
2110 overlapped
.Internal
= -1;
2111 overlapped
.InternalHigh
= -1;
2112 overlapped
.hEvent
= event
;
2113 num_bytes
= 0xdeadbeef;
2114 SetLastError(0xdeadbeef);
2115 ret
= ReadFile(server
, read_buf
, 0, &num_bytes
, &overlapped
);
2117 ok(GetLastError() == ERROR_IO_PENDING
, "expected ERROR_IO_PENDING, got %d\n", GetLastError());
2118 ok(num_bytes
== 0, "bytes %u\n", num_bytes
);
2119 ok((NTSTATUS
)overlapped
.Internal
== STATUS_PENDING
, "expected STATUS_PENDING, got %#lx\n", overlapped
.Internal
);
2121 ok(overlapped
.InternalHigh
== -1, "expected -1, got %lu\n", overlapped
.InternalHigh
);
2123 wait
= WaitForSingleObject(event
, 100);
2124 ok(wait
== WAIT_TIMEOUT
, "WaitForSingleObject returned %x\n", wait
);
2126 num_bytes
= 0xdeadbeef;
2127 ret
= WriteFile(client
, test_string
, 1, &num_bytes
, NULL
);
2128 ok(ret
, "WriteFile failed\n");
2129 ok(num_bytes
== 1, "bytes %u\n", num_bytes
);
2131 wait
= WaitForSingleObject(event
, 100);
2133 ok(wait
== WAIT_OBJECT_0
, "WaitForSingleObject returned %x\n", wait
);
2135 ok(num_bytes
== 1, "bytes %u\n", num_bytes
);
2137 ok((NTSTATUS
)overlapped
.Internal
== STATUS_SUCCESS
, "expected STATUS_SUCCESS, got %#lx\n", overlapped
.Internal
);
2138 ok(overlapped
.InternalHigh
== 0, "expected 0, got %lu\n", overlapped
.InternalHigh
);
2140 /* read the pending byte and clear the pipe */
2141 num_bytes
= 0xdeadbeef;
2142 ret
= ReadFile(server
, read_buf
, 1, &num_bytes
, &overlapped
);
2143 ok(ret
, "ReadFile failed\n");
2144 ok(num_bytes
== 1, "bytes %u\n", num_bytes
);
2146 CloseHandle(client
);
2147 CloseHandle(server
);
2155 hmod
= GetModuleHandleA("advapi32.dll");
2156 pDuplicateTokenEx
= (void *) GetProcAddress(hmod
, "DuplicateTokenEx");
2157 hmod
= GetModuleHandleA("kernel32.dll");
2158 pQueueUserAPC
= (void *) GetProcAddress(hmod
, "QueueUserAPC");
2160 if (test_DisconnectNamedPipe())
2162 test_CreateNamedPipe_instances_must_match();
2164 test_CreateNamedPipe(PIPE_TYPE_BYTE
);
2165 test_CreateNamedPipe(PIPE_TYPE_MESSAGE
| PIPE_READMODE_MESSAGE
);
2167 test_impersonation();
2169 test_NamedPipeHandleState();
2170 test_readfileex_pending();