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 /* currently the Wine behavior depends on the kernel version */
298 /* ok(readden == sizeof(obuf) + sizeof(obuf2), "peek3 got %d bytes\n", readden); */
299 if (readden
!= sizeof(obuf
) + sizeof(obuf2
)) todo_wine
ok(0, "peek3 got %d bytes\n", readden
);
303 /* ok(readden == sizeof(obuf), "peek3 got %d bytes\n", readden); */
304 if (readden
!= sizeof(obuf
)) todo_wine
ok(0, "peek3 got %d bytes\n", readden
);
306 ok(avail
== sizeof(obuf
) + sizeof(obuf2
), "peek3 got %d bytes available\n", avail
);
308 ok(memcmp(obuf
, pbuf
, sizeof(obuf
)) == 0, "pipe content 3a check\n");
309 if (pipemode
== PIPE_TYPE_BYTE
&& readden
>= sizeof(obuf
)+sizeof(obuf2
)) {
310 pbuf
+= sizeof(obuf
);
311 ok(memcmp(obuf2
, pbuf
, sizeof(obuf2
)) == 0, "pipe content 3b check\n");
313 ok(ReadFile(hFile
, ibuf
, sizeof(ibuf
), &readden
, NULL
), "ReadFile\n");
314 ok(readden
== sizeof(obuf
) + sizeof(obuf2
), "read 3 got %d bytes\n", readden
);
316 ok(memcmp(obuf
, pbuf
, sizeof(obuf
)) == 0, "content 3a check\n");
317 pbuf
+= sizeof(obuf
);
318 ok(memcmp(obuf2
, pbuf
, sizeof(obuf2
)) == 0, "content 3b check\n");
320 /* Multiple writes in the reverse direction */
321 memset(ibuf
, 0, sizeof(ibuf
));
322 ok(WriteFile(hFile
, obuf
, sizeof(obuf
), &written
, NULL
), "WriteFile4a\n");
323 ok(written
== sizeof(obuf
), "write file len 4a\n");
324 ok(WriteFile(hFile
, obuf2
, sizeof(obuf2
), &written
, NULL
), " WriteFile4b\n");
325 ok(written
== sizeof(obuf2
), "write file len 4b\n");
326 ok(PeekNamedPipe(hnp
, ibuf
, sizeof(ibuf
), &readden
, &avail
, NULL
), "Peek4\n");
327 if (pipemode
== PIPE_TYPE_BYTE
) {
328 /* currently the Wine behavior depends on the kernel version */
329 /* ok(readden == sizeof(obuf) + sizeof(obuf2), "peek4 got %d bytes\n", readden); */
330 if (readden
!= sizeof(obuf
) + sizeof(obuf2
)) todo_wine
ok(0, "peek4 got %d bytes\n", readden
);
334 /* ok(readden == sizeof(obuf), "peek4 got %d bytes\n", readden); */
335 if (readden
!= sizeof(obuf
)) todo_wine
ok(0, "peek4 got %d bytes\n", readden
);
337 ok(avail
== sizeof(obuf
) + sizeof(obuf2
), "peek4 got %d bytes available\n", avail
);
339 ok(memcmp(obuf
, pbuf
, sizeof(obuf
)) == 0, "pipe content 4a check\n");
340 if (pipemode
== PIPE_TYPE_BYTE
&& readden
>= sizeof(obuf
)+sizeof(obuf2
)) {
341 pbuf
+= sizeof(obuf
);
342 ok(memcmp(obuf2
, pbuf
, sizeof(obuf2
)) == 0, "pipe content 4b check\n");
344 ok(ReadFile(hnp
, ibuf
, sizeof(ibuf
), &readden
, NULL
), "ReadFile\n");
345 if (pipemode
== PIPE_TYPE_BYTE
) {
346 ok(readden
== sizeof(obuf
) + sizeof(obuf2
), "read 4 got %d bytes\n", readden
);
350 ok(readden
== sizeof(obuf
), "read 4 got %d bytes\n", readden
);
354 ok(memcmp(obuf
, pbuf
, sizeof(obuf
)) == 0, "content 4a check\n");
355 if (pipemode
== PIPE_TYPE_BYTE
) {
356 pbuf
+= sizeof(obuf
);
357 ok(memcmp(obuf2
, pbuf
, sizeof(obuf2
)) == 0, "content 4b check\n");
360 /* Test reading of multiple writes after a mode change
361 (CreateFile always creates a byte mode pipe) */
362 lpmode
= PIPE_READMODE_MESSAGE
;
363 if (pipemode
== PIPE_TYPE_BYTE
) {
364 /* trying to change the client end of a byte pipe to message mode should fail */
365 ok(!SetNamedPipeHandleState(hFile
, &lpmode
, NULL
, NULL
), "Change mode\n");
368 ok(SetNamedPipeHandleState(hFile
, &lpmode
, NULL
, NULL
), "Change mode\n");
370 memset(ibuf
, 0, sizeof(ibuf
));
371 ok(WriteFile(hnp
, obuf
, sizeof(obuf
), &written
, NULL
), "WriteFile5a\n");
372 ok(written
== sizeof(obuf
), "write file len 3a\n");
373 ok(WriteFile(hnp
, obuf2
, sizeof(obuf2
), &written
, NULL
), " WriteFile5b\n");
374 ok(written
== sizeof(obuf2
), "write file len 3b\n");
375 ok(PeekNamedPipe(hFile
, ibuf
, sizeof(ibuf
), &readden
, &avail
, NULL
), "Peek5\n");
376 /* currently the Wine behavior depends on the kernel version */
377 /* ok(readden == sizeof(obuf), "peek5 got %d bytes\n", readden); */
378 if (readden
!= sizeof(obuf
)) todo_wine
ok(0, "peek5 got %d bytes\n", readden
);
380 ok(avail
== sizeof(obuf
) + sizeof(obuf2
), "peek5 got %d bytes available\n", avail
);
382 ok(memcmp(obuf
, pbuf
, sizeof(obuf
)) == 0, "content 5a check\n");
383 ok(ReadFile(hFile
, ibuf
, sizeof(ibuf
), &readden
, NULL
), "ReadFile\n");
385 ok(readden
== sizeof(obuf
), "read 5 got %d bytes\n", readden
);
388 ok(memcmp(obuf
, pbuf
, sizeof(obuf
)) == 0, "content 5a check\n");
389 if (readden
<= sizeof(obuf
))
390 ok(ReadFile(hFile
, ibuf
, sizeof(ibuf
), &readden
, NULL
), "ReadFile\n");
392 /* Multiple writes in the reverse direction */
393 /* the write of obuf2 from write4 should still be in the buffer */
394 ok(PeekNamedPipe(hnp
, ibuf
, sizeof(ibuf
), &readden
, &avail
, NULL
), "Peek6a\n");
396 ok(readden
== sizeof(obuf2
), "peek6a got %d bytes\n", readden
);
397 ok(avail
== sizeof(obuf2
), "peek6a got %d bytes available\n", avail
);
400 ok(ReadFile(hnp
, ibuf
, sizeof(ibuf
), &readden
, NULL
), "ReadFile\n");
401 ok(readden
== sizeof(obuf2
), "read 6a got %d bytes\n", readden
);
403 ok(memcmp(obuf2
, pbuf
, sizeof(obuf2
)) == 0, "content 6a check\n");
405 memset(ibuf
, 0, sizeof(ibuf
));
406 ok(WriteFile(hFile
, obuf
, sizeof(obuf
), &written
, NULL
), "WriteFile6a\n");
407 ok(written
== sizeof(obuf
), "write file len 6a\n");
408 ok(WriteFile(hFile
, obuf2
, sizeof(obuf2
), &written
, NULL
), " WriteFile6b\n");
409 ok(written
== sizeof(obuf2
), "write file len 6b\n");
410 ok(PeekNamedPipe(hnp
, ibuf
, sizeof(ibuf
), &readden
, &avail
, NULL
), "Peek6\n");
411 /* currently the Wine behavior depends on the kernel version */
412 /* ok(readden == sizeof(obuf), "peek6 got %d bytes\n", readden); */
413 if (readden
!= sizeof(obuf
)) todo_wine
ok(0, "peek6 got %d bytes\n", readden
);
415 ok(avail
== sizeof(obuf
) + sizeof(obuf2
), "peek6b got %d bytes available\n", avail
);
417 ok(memcmp(obuf
, pbuf
, sizeof(obuf
)) == 0, "content 6a check\n");
418 ok(ReadFile(hnp
, ibuf
, sizeof(ibuf
), &readden
, NULL
), "ReadFile\n");
420 ok(readden
== sizeof(obuf
), "read 6b got %d bytes\n", readden
);
423 ok(memcmp(obuf
, pbuf
, sizeof(obuf
)) == 0, "content 6a check\n");
424 if (readden
<= sizeof(obuf
))
425 ok(ReadFile(hnp
, ibuf
, sizeof(ibuf
), &readden
, NULL
), "ReadFile\n");
427 /* Test how ReadFile behaves when the buffer is not big enough for the whole message */
428 memset(ibuf
, 0, sizeof(ibuf
));
429 ok(WriteFile(hnp
, obuf2
, sizeof(obuf2
), &written
, NULL
), "WriteFile 7\n");
430 ok(written
== sizeof(obuf2
), "write file len 7\n");
431 SetLastError(0xdeadbeef);
433 ok(!ReadFile(hFile
, ibuf
, 4, &readden
, NULL
), "ReadFile 7\n");
435 ok(GetLastError() == ERROR_MORE_DATA
, "wrong error 7\n");
436 ok(readden
== 4, "read got %d bytes 7\n", readden
);
437 ok(ReadFile(hFile
, ibuf
+ 4, sizeof(ibuf
) - 4, &readden
, NULL
), "ReadFile 7\n");
438 ok(readden
== sizeof(obuf2
) - 4, "read got %d bytes 7\n", readden
);
439 ok(memcmp(obuf2
, ibuf
, written
) == 0, "content check 7\n");
441 memset(ibuf
, 0, sizeof(ibuf
));
442 ok(WriteFile(hFile
, obuf
, sizeof(obuf
), &written
, NULL
), "WriteFile 8\n");
443 ok(written
== sizeof(obuf
), "write file len 8\n");
444 SetLastError(0xdeadbeef);
446 ok(!ReadFile(hnp
, ibuf
, 4, &readden
, NULL
), "ReadFile 8\n");
448 ok(GetLastError() == ERROR_MORE_DATA
, "wrong error 8\n");
449 ok(readden
== 4, "read got %d bytes 8\n", readden
);
450 ok(ReadFile(hnp
, ibuf
+ 4, sizeof(ibuf
) - 4, &readden
, NULL
), "ReadFile 8\n");
451 ok(readden
== sizeof(obuf
) - 4, "read got %d bytes 8\n", readden
);
452 ok(memcmp(obuf
, ibuf
, written
) == 0, "content check 8\n");
454 /* The following test shows that when doing a partial read of a message, the rest
455 * is still in the pipe, and can be received from a second thread. This shows
456 * especially that the content is _not_ stored in thread-local-storage until it is
457 * completely transmitted. The same method works even across multiple processes. */
458 memset(ibuf
, 0, sizeof(ibuf
));
459 ok(WriteFile(hnp
, obuf
, sizeof(obuf
), &written
, NULL
), "WriteFile 9\n");
460 ok(written
== sizeof(obuf
), "write file len 9\n");
461 ok(WriteFile(hnp
, obuf2
, sizeof(obuf2
), &written
, NULL
), "WriteFile 9\n");
462 ok(written
== sizeof(obuf2
), "write file len 9\n");
463 SetLastError(0xdeadbeef);
465 ok(!ReadFile(hFile
, ibuf
, 4, &readden
, NULL
), "ReadFile 9\n");
467 ok(GetLastError() == ERROR_MORE_DATA
, "wrong error 9\n");
468 ok(readden
== 4, "read got %d bytes 9\n", readden
);
469 SetLastError(0xdeadbeef);
470 ret
= RpcReadFile(hFile
, ibuf
+ 4, 4, &readden
, NULL
);
472 ok(!ret
, "RpcReadFile 9\n");
474 ok(GetLastError() == ERROR_MORE_DATA
, "wrong error 9\n");
475 ok(readden
== 4, "read got %d bytes 9\n", readden
);
476 ret
= RpcReadFile(hFile
, ibuf
+ 8, sizeof(ibuf
), &readden
, NULL
);
477 ok(ret
, "RpcReadFile 9\n");
479 ok(readden
== sizeof(obuf
) - 8, "read got %d bytes 9\n", readden
);
480 ok(memcmp(obuf
, ibuf
, sizeof(obuf
)) == 0, "content check 9\n");
481 if (readden
<= sizeof(obuf
) - 8) /* blocks forever if second part was already received */
483 memset(ibuf
, 0, sizeof(ibuf
));
484 SetLastError(0xdeadbeef);
485 ret
= RpcReadFile(hFile
, ibuf
, 4, &readden
, NULL
);
486 ok(!ret
, "RpcReadFile 9\n");
488 ok(GetLastError() == ERROR_MORE_DATA
, "wrong error 9\n");
489 ok(readden
== 4, "read got %d bytes 9\n", readden
);
490 SetLastError(0xdeadbeef);
492 ok(!ReadFile(hFile
, ibuf
+ 4, 4, &readden
, NULL
), "ReadFile 9\n");
494 ok(GetLastError() == ERROR_MORE_DATA
, "wrong error 9\n");
495 ok(readden
== 4, "read got %d bytes 9\n", readden
);
496 ret
= RpcReadFile(hFile
, ibuf
+ 8, sizeof(ibuf
), &readden
, NULL
);
497 ok(ret
, "RpcReadFile 9\n");
498 ok(readden
== sizeof(obuf2
) - 8, "read got %d bytes 9\n", readden
);
499 ok(memcmp(obuf2
, ibuf
, sizeof(obuf2
)) == 0, "content check 9\n");
502 /* Now the reverse direction */
503 memset(ibuf
, 0, sizeof(ibuf
));
504 ok(WriteFile(hFile
, obuf2
, sizeof(obuf2
), &written
, NULL
), "WriteFile 10\n");
505 ok(written
== sizeof(obuf2
), "write file len 10\n");
506 ok(WriteFile(hFile
, obuf
, sizeof(obuf
), &written
, NULL
), "WriteFile 10\n");
507 ok(written
== sizeof(obuf
), "write file len 10\n");
508 SetLastError(0xdeadbeef);
510 ok(!ReadFile(hnp
, ibuf
, 4, &readden
, NULL
), "ReadFile 10\n");
512 ok(GetLastError() == ERROR_MORE_DATA
, "wrong error 10\n");
513 ok(readden
== 4, "read got %d bytes 10\n", readden
);
514 SetLastError(0xdeadbeef);
515 ret
= RpcReadFile(hnp
, ibuf
+ 4, 4, &readden
, NULL
);
517 ok(!ret
, "RpcReadFile 10\n");
519 ok(GetLastError() == ERROR_MORE_DATA
, "wrong error 10\n");
520 ok(readden
== 4, "read got %d bytes 10\n", readden
);
521 ret
= RpcReadFile(hnp
, ibuf
+ 8, sizeof(ibuf
), &readden
, NULL
);
522 ok(ret
, "RpcReadFile 10\n");
524 ok(readden
== sizeof(obuf2
) - 8, "read got %d bytes 10\n", readden
);
525 ok(memcmp(obuf2
, ibuf
, sizeof(obuf2
)) == 0, "content check 10\n");
526 if (readden
<= sizeof(obuf2
) - 8) /* blocks forever if second part was already received */
528 memset(ibuf
, 0, sizeof(ibuf
));
529 SetLastError(0xdeadbeef);
530 ret
= RpcReadFile(hnp
, ibuf
, 4, &readden
, NULL
);
531 ok(!ret
, "RpcReadFile 10\n");
533 ok(GetLastError() == ERROR_MORE_DATA
, "wrong error 10\n");
534 ok(readden
== 4, "read got %d bytes 10\n", readden
);
535 SetLastError(0xdeadbeef);
537 ok(!ReadFile(hnp
, ibuf
+ 4, 4, &readden
, NULL
), "ReadFile 10\n");
539 ok(GetLastError() == ERROR_MORE_DATA
, "wrong error 10\n");
540 ok(readden
== 4, "read got %d bytes 10\n", readden
);
541 ret
= RpcReadFile(hnp
, ibuf
+ 8, sizeof(ibuf
), &readden
, NULL
);
542 ok(ret
, "RpcReadFile 10\n");
543 ok(readden
== sizeof(obuf
) - 8, "read got %d bytes 10\n", readden
);
544 ok(memcmp(obuf
, ibuf
, sizeof(obuf
)) == 0, "content check 10\n");
549 /* Picky conformance tests */
551 /* Verify that you can't connect to pipe again
552 * until server calls DisconnectNamedPipe+ConnectNamedPipe
553 * or creates a new pipe
554 * case 1: other client not yet closed
556 hFile2
= CreateFileA(PIPENAME
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0);
557 ok(hFile2
== INVALID_HANDLE_VALUE
,
558 "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail\n");
559 ok(GetLastError() == ERROR_PIPE_BUSY
,
560 "connecting to named pipe before other client closes should fail with ERROR_PIPE_BUSY\n");
562 ok(CloseHandle(hFile
), "CloseHandle\n");
564 /* case 2: other client already closed */
565 hFile
= CreateFileA(PIPENAME
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0);
566 ok(hFile
== INVALID_HANDLE_VALUE
,
567 "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail\n");
568 ok(GetLastError() == ERROR_PIPE_BUSY
,
569 "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail with ERROR_PIPE_BUSY\n");
571 ok(DisconnectNamedPipe(hnp
), "DisconnectNamedPipe\n");
573 /* case 3: server has called DisconnectNamedPipe but not ConnectNamed Pipe */
574 hFile
= CreateFileA(PIPENAME
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0);
575 ok(hFile
== INVALID_HANDLE_VALUE
,
576 "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail\n");
577 ok(GetLastError() == ERROR_PIPE_BUSY
,
578 "connecting to named pipe after other client closes but before ConnectNamedPipe should fail with ERROR_PIPE_BUSY\n");
580 /* to be complete, we'd call ConnectNamedPipe here and loop,
581 * but by default that's blocking, so we'd either have
582 * to turn on the uncommon nonblocking mode, or
583 * use another thread.
587 ok(CloseHandle(hnp
), "CloseHandle\n");
589 trace("test_CreateNamedPipe returning\n");
592 static void test_CreateNamedPipe_instances_must_match(void)
596 /* Check no mismatch */
597 hnp
= CreateNamedPipeA(PIPENAME
, PIPE_ACCESS_DUPLEX
, PIPE_TYPE_BYTE
| PIPE_WAIT
,
598 /* nMaxInstances */ 2,
599 /* nOutBufSize */ 1024,
600 /* nInBufSize */ 1024,
601 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
602 /* lpSecurityAttrib */ NULL
);
603 ok(hnp
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
605 hnp2
= CreateNamedPipeA(PIPENAME
, PIPE_ACCESS_DUPLEX
, PIPE_TYPE_BYTE
| PIPE_WAIT
,
606 /* nMaxInstances */ 2,
607 /* nOutBufSize */ 1024,
608 /* nInBufSize */ 1024,
609 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
610 /* lpSecurityAttrib */ NULL
);
611 ok(hnp2
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
613 ok(CloseHandle(hnp
), "CloseHandle\n");
614 ok(CloseHandle(hnp2
), "CloseHandle\n");
616 /* Check nMaxInstances */
617 hnp
= CreateNamedPipeA(PIPENAME
, PIPE_ACCESS_DUPLEX
, PIPE_TYPE_BYTE
| PIPE_WAIT
,
618 /* nMaxInstances */ 1,
619 /* nOutBufSize */ 1024,
620 /* nInBufSize */ 1024,
621 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
622 /* lpSecurityAttrib */ NULL
);
623 ok(hnp
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
625 hnp2
= CreateNamedPipeA(PIPENAME
, PIPE_ACCESS_DUPLEX
, PIPE_TYPE_BYTE
| PIPE_WAIT
,
626 /* nMaxInstances */ 1,
627 /* nOutBufSize */ 1024,
628 /* nInBufSize */ 1024,
629 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
630 /* lpSecurityAttrib */ NULL
);
631 ok(hnp2
== INVALID_HANDLE_VALUE
632 && GetLastError() == ERROR_PIPE_BUSY
, "nMaxInstances not obeyed\n");
634 ok(CloseHandle(hnp
), "CloseHandle\n");
636 /* Check PIPE_ACCESS_* */
637 hnp
= CreateNamedPipeA(PIPENAME
, PIPE_ACCESS_DUPLEX
, PIPE_TYPE_BYTE
| PIPE_WAIT
,
638 /* nMaxInstances */ 2,
639 /* nOutBufSize */ 1024,
640 /* nInBufSize */ 1024,
641 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
642 /* lpSecurityAttrib */ NULL
);
643 ok(hnp
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
645 hnp2
= CreateNamedPipeA(PIPENAME
, PIPE_ACCESS_INBOUND
, PIPE_TYPE_BYTE
| PIPE_WAIT
,
646 /* nMaxInstances */ 2,
647 /* nOutBufSize */ 1024,
648 /* nInBufSize */ 1024,
649 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
650 /* lpSecurityAttrib */ NULL
);
651 ok(hnp2
== INVALID_HANDLE_VALUE
652 && GetLastError() == ERROR_ACCESS_DENIED
, "PIPE_ACCESS_* mismatch allowed\n");
654 ok(CloseHandle(hnp
), "CloseHandle\n");
656 /* check everything else */
657 hnp
= CreateNamedPipeA(PIPENAME
, PIPE_ACCESS_DUPLEX
, PIPE_TYPE_BYTE
| PIPE_WAIT
,
658 /* nMaxInstances */ 4,
659 /* nOutBufSize */ 1024,
660 /* nInBufSize */ 1024,
661 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
662 /* lpSecurityAttrib */ NULL
);
663 ok(hnp
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
665 hnp2
= CreateNamedPipeA(PIPENAME
, PIPE_ACCESS_DUPLEX
, PIPE_TYPE_MESSAGE
,
666 /* nMaxInstances */ 3,
667 /* nOutBufSize */ 102,
669 /* nDefaultWait */ 1234,
670 /* lpSecurityAttrib */ NULL
);
671 ok(hnp2
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
673 ok(CloseHandle(hnp
), "CloseHandle\n");
674 ok(CloseHandle(hnp2
), "CloseHandle\n");
677 /** implementation of alarm() */
678 static DWORD CALLBACK
alarmThreadMain(LPVOID arg
)
680 DWORD_PTR timeout
= (DWORD_PTR
) arg
;
681 trace("alarmThreadMain\n");
682 if (WaitForSingleObject( alarm_event
, timeout
) == WAIT_TIMEOUT
)
684 ok(FALSE
, "alarm\n");
690 static HANDLE hnp
= INVALID_HANDLE_VALUE
;
692 /** Trivial byte echo server - disconnects after each session */
693 static DWORD CALLBACK
serverThreadMain1(LPVOID arg
)
697 trace("serverThreadMain1 start\n");
698 /* Set up a simple echo server */
699 hnp
= CreateNamedPipeA(PIPENAME
"serverThreadMain1", PIPE_ACCESS_DUPLEX
,
700 PIPE_TYPE_BYTE
| PIPE_WAIT
,
701 /* nMaxInstances */ 1,
702 /* nOutBufSize */ 1024,
703 /* nInBufSize */ 1024,
704 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
705 /* lpSecurityAttrib */ NULL
);
707 ok(hnp
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
708 for (i
= 0; i
< NB_SERVER_LOOPS
; i
++) {
714 /* Wait for client to connect */
715 trace("Server calling ConnectNamedPipe...\n");
716 ok(ConnectNamedPipe(hnp
, NULL
)
717 || GetLastError() == ERROR_PIPE_CONNECTED
, "ConnectNamedPipe\n");
718 trace("ConnectNamedPipe returned.\n");
720 /* Echo bytes once */
721 memset(buf
, 0, sizeof(buf
));
723 trace("Server reading...\n");
724 success
= ReadFile(hnp
, buf
, sizeof(buf
), &readden
, NULL
);
725 trace("Server done reading.\n");
726 ok(success
, "ReadFile\n");
727 ok(readden
, "short read\n");
729 trace("Server writing...\n");
730 ok(WriteFile(hnp
, buf
, readden
, &written
, NULL
), "WriteFile\n");
731 trace("Server done writing.\n");
732 ok(written
== readden
, "write file len\n");
734 /* finish this connection, wait for next one */
735 ok(FlushFileBuffers(hnp
), "FlushFileBuffers\n");
736 trace("Server done flushing.\n");
737 ok(DisconnectNamedPipe(hnp
), "DisconnectNamedPipe\n");
738 trace("Server done disconnecting.\n");
743 /** Trivial byte echo server - closes after each connection */
744 static DWORD CALLBACK
serverThreadMain2(LPVOID arg
)
749 trace("serverThreadMain2\n");
750 /* Set up a simple echo server */
751 hnp
= CreateNamedPipeA(PIPENAME
"serverThreadMain2", PIPE_ACCESS_DUPLEX
,
752 PIPE_TYPE_BYTE
| PIPE_WAIT
,
753 /* nMaxInstances */ 2,
754 /* nOutBufSize */ 1024,
755 /* nInBufSize */ 1024,
756 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
757 /* lpSecurityAttrib */ NULL
);
758 ok(hnp
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
760 for (i
= 0; i
< NB_SERVER_LOOPS
; i
++) {
768 user_apc_ran
= FALSE
;
769 if (i
== 0 && pQueueUserAPC
) {
770 trace("Queueing an user APC\n"); /* verify the pipe is non alerable */
771 ret
= pQueueUserAPC(&user_apc
, GetCurrentThread(), 0);
772 ok(ret
, "QueueUserAPC failed: %d\n", GetLastError());
775 /* Wait for client to connect */
776 trace("Server calling ConnectNamedPipe...\n");
777 ok(ConnectNamedPipe(hnp
, NULL
)
778 || GetLastError() == ERROR_PIPE_CONNECTED
, "ConnectNamedPipe\n");
779 trace("ConnectNamedPipe returned.\n");
781 /* Echo bytes once */
782 memset(buf
, 0, sizeof(buf
));
784 trace("Server reading...\n");
785 success
= ReadFile(hnp
, buf
, sizeof(buf
), &readden
, NULL
);
786 trace("Server done reading.\n");
787 ok(success
, "ReadFile\n");
789 trace("Server writing...\n");
790 ok(WriteFile(hnp
, buf
, readden
, &written
, NULL
), "WriteFile\n");
791 trace("Server done writing.\n");
792 ok(written
== readden
, "write file len\n");
794 /* finish this connection, wait for next one */
795 ok(FlushFileBuffers(hnp
), "FlushFileBuffers\n");
796 ok(DisconnectNamedPipe(hnp
), "DisconnectNamedPipe\n");
798 ok(user_apc_ran
== FALSE
, "UserAPC ran, pipe using alertable io mode\n");
800 if (i
== 0 && pQueueUserAPC
)
801 SleepEx(0, TRUE
); /* get rid of apc */
803 /* Set up next echo server */
805 CreateNamedPipeA(PIPENAME
"serverThreadMain2", PIPE_ACCESS_DUPLEX
,
806 PIPE_TYPE_BYTE
| PIPE_WAIT
,
807 /* nMaxInstances */ 2,
808 /* nOutBufSize */ 1024,
809 /* nInBufSize */ 1024,
810 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
811 /* lpSecurityAttrib */ NULL
);
813 ok(hnpNext
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
815 ok(CloseHandle(hnp
), "CloseHandle\n");
821 /** Trivial byte echo server - uses overlapped named pipe calls */
822 static DWORD CALLBACK
serverThreadMain3(LPVOID arg
)
827 trace("serverThreadMain3\n");
828 /* Set up a simple echo server */
829 hnp
= CreateNamedPipeA(PIPENAME
"serverThreadMain3", PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
830 PIPE_TYPE_BYTE
| PIPE_WAIT
,
831 /* nMaxInstances */ 1,
832 /* nOutBufSize */ 1024,
833 /* nInBufSize */ 1024,
834 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
835 /* lpSecurityAttrib */ NULL
);
836 ok(hnp
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
838 hEvent
= CreateEventW(NULL
, /* security attribute */
839 TRUE
, /* manual reset event */
840 FALSE
, /* initial state */
842 ok(hEvent
!= NULL
, "CreateEvent\n");
844 for (i
= 0; i
< NB_SERVER_LOOPS
; i
++) {
851 int letWFSOEwait
= (i
& 2);
852 int letGORwait
= (i
& 1);
855 memset(&oOverlap
, 0, sizeof(oOverlap
));
856 oOverlap
.hEvent
= hEvent
;
858 /* Wait for client to connect */
860 trace("Server calling non-overlapped ConnectNamedPipe on overlapped pipe...\n");
861 success
= ConnectNamedPipe(hnp
, NULL
);
862 err
= GetLastError();
863 ok(success
|| (err
== ERROR_PIPE_CONNECTED
), "ConnectNamedPipe failed: %d\n", err
);
864 trace("ConnectNamedPipe operation complete.\n");
866 trace("Server calling overlapped ConnectNamedPipe...\n");
867 success
= ConnectNamedPipe(hnp
, &oOverlap
);
868 err
= GetLastError();
869 ok(!success
&& (err
== ERROR_IO_PENDING
|| err
== ERROR_PIPE_CONNECTED
), "overlapped ConnectNamedPipe\n");
870 trace("overlapped ConnectNamedPipe returned.\n");
871 if (!success
&& (err
== ERROR_IO_PENDING
)) {
876 ret
= WaitForSingleObjectEx(hEvent
, INFINITE
, TRUE
);
877 } while (ret
== WAIT_IO_COMPLETION
);
878 ok(ret
== 0, "wait ConnectNamedPipe returned %x\n", ret
);
880 success
= GetOverlappedResult(hnp
, &oOverlap
, &dummy
, letGORwait
);
881 if (!letGORwait
&& !letWFSOEwait
&& !success
) {
882 ok(GetLastError() == ERROR_IO_INCOMPLETE
, "GetOverlappedResult\n");
883 success
= GetOverlappedResult(hnp
, &oOverlap
, &dummy
, TRUE
);
886 ok(success
|| (err
== ERROR_PIPE_CONNECTED
), "GetOverlappedResult ConnectNamedPipe\n");
887 trace("overlapped ConnectNamedPipe operation complete.\n");
890 /* Echo bytes once */
891 memset(buf
, 0, sizeof(buf
));
893 trace("Server reading...\n");
894 success
= ReadFile(hnp
, buf
, sizeof(buf
), &readden
, &oOverlap
);
895 trace("Server ReadFile returned...\n");
896 err
= GetLastError();
897 ok(success
|| err
== ERROR_IO_PENDING
, "overlapped ReadFile\n");
898 trace("overlapped ReadFile returned.\n");
899 if (!success
&& (err
== ERROR_IO_PENDING
)) {
904 ret
= WaitForSingleObjectEx(hEvent
, INFINITE
, TRUE
);
905 } while (ret
== WAIT_IO_COMPLETION
);
906 ok(ret
== 0, "wait ReadFile returned %x\n", ret
);
908 success
= GetOverlappedResult(hnp
, &oOverlap
, &readden
, letGORwait
);
909 if (!letGORwait
&& !letWFSOEwait
&& !success
) {
910 ok(GetLastError() == ERROR_IO_INCOMPLETE
, "GetOverlappedResult\n");
911 success
= GetOverlappedResult(hnp
, &oOverlap
, &readden
, TRUE
);
914 trace("Server done reading.\n");
915 ok(success
, "overlapped ReadFile\n");
917 trace("Server writing...\n");
918 success
= WriteFile(hnp
, buf
, readden
, &written
, &oOverlap
);
919 trace("Server WriteFile returned...\n");
920 err
= GetLastError();
921 ok(success
|| err
== ERROR_IO_PENDING
, "overlapped WriteFile\n");
922 trace("overlapped WriteFile returned.\n");
923 if (!success
&& (err
== ERROR_IO_PENDING
)) {
928 ret
= WaitForSingleObjectEx(hEvent
, INFINITE
, TRUE
);
929 } while (ret
== WAIT_IO_COMPLETION
);
930 ok(ret
== 0, "wait WriteFile returned %x\n", ret
);
932 success
= GetOverlappedResult(hnp
, &oOverlap
, &written
, letGORwait
);
933 if (!letGORwait
&& !letWFSOEwait
&& !success
) {
934 ok(GetLastError() == ERROR_IO_INCOMPLETE
, "GetOverlappedResult\n");
935 success
= GetOverlappedResult(hnp
, &oOverlap
, &written
, TRUE
);
938 trace("Server done writing.\n");
939 ok(success
, "overlapped WriteFile\n");
940 ok(written
== readden
, "write file len\n");
942 /* finish this connection, wait for next one */
943 ok(FlushFileBuffers(hnp
), "FlushFileBuffers\n");
944 ok(DisconnectNamedPipe(hnp
), "DisconnectNamedPipe\n");
949 /** Trivial byte echo server - uses i/o completion ports */
950 static DWORD CALLBACK
serverThreadMain4(LPVOID arg
)
956 trace("serverThreadMain4\n");
957 /* Set up a simple echo server */
958 hnp
= CreateNamedPipeA(PIPENAME
"serverThreadMain4", PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
959 PIPE_TYPE_BYTE
| PIPE_WAIT
,
960 /* nMaxInstances */ 1,
961 /* nOutBufSize */ 1024,
962 /* nInBufSize */ 1024,
963 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
964 /* lpSecurityAttrib */ NULL
);
965 ok(hnp
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
967 hcompletion
= CreateIoCompletionPort(hnp
, NULL
, 12345, 1);
968 ok(hcompletion
!= NULL
, "CreateIoCompletionPort failed, error=%i\n", GetLastError());
970 for (i
= 0; i
< NB_SERVER_LOOPS
; i
++) {
983 memset(&oConnect
, 0, sizeof(oConnect
));
984 memset(&oRead
, 0, sizeof(oRead
));
985 memset(&oWrite
, 0, sizeof(oWrite
));
987 /* Wait for client to connect */
988 trace("Server calling overlapped ConnectNamedPipe...\n");
989 success
= ConnectNamedPipe(hnp
, &oConnect
);
990 err
= GetLastError();
991 ok(!success
&& (err
== ERROR_IO_PENDING
|| err
== ERROR_PIPE_CONNECTED
),
992 "overlapped ConnectNamedPipe got %u err %u\n", success
, err
);
993 if (!success
&& err
== ERROR_IO_PENDING
) {
994 trace("ConnectNamedPipe GetQueuedCompletionStatus\n");
995 success
= GetQueuedCompletionStatus(hcompletion
, &dummy
, &compkey
, &oResult
, 0);
998 ok( GetLastError() == WAIT_TIMEOUT
,
999 "ConnectNamedPipe GetQueuedCompletionStatus wrong error %u\n", GetLastError());
1000 success
= GetQueuedCompletionStatus(hcompletion
, &dummy
, &compkey
, &oResult
, 10000);
1002 ok(success
, "ConnectNamedPipe GetQueuedCompletionStatus failed, errno=%i\n", GetLastError());
1005 ok(compkey
== 12345, "got completion key %i instead of 12345\n", (int)compkey
);
1006 ok(oResult
== &oConnect
, "got overlapped pointer %p instead of %p\n", oResult
, &oConnect
);
1009 trace("overlapped ConnectNamedPipe operation complete.\n");
1011 /* Echo bytes once */
1012 memset(buf
, 0, sizeof(buf
));
1014 trace("Server reading...\n");
1015 success
= ReadFile(hnp
, buf
, sizeof(buf
), &readden
, &oRead
);
1016 trace("Server ReadFile returned...\n");
1017 err
= GetLastError();
1018 ok(success
|| err
== ERROR_IO_PENDING
, "overlapped ReadFile, err=%i\n", err
);
1019 success
= GetQueuedCompletionStatus(hcompletion
, &readden
, &compkey
,
1021 ok(success
, "ReadFile GetQueuedCompletionStatus failed, errno=%i\n", GetLastError());
1024 ok(compkey
== 12345, "got completion key %i instead of 12345\n", (int)compkey
);
1025 ok(oResult
== &oRead
, "got overlapped pointer %p instead of %p\n", oResult
, &oRead
);
1027 trace("Server done reading.\n");
1029 trace("Server writing...\n");
1030 success
= WriteFile(hnp
, buf
, readden
, &written
, &oWrite
);
1031 trace("Server WriteFile returned...\n");
1032 err
= GetLastError();
1033 ok(success
|| err
== ERROR_IO_PENDING
, "overlapped WriteFile failed, err=%u\n", err
);
1034 success
= GetQueuedCompletionStatus(hcompletion
, &written
, &compkey
,
1036 ok(success
, "WriteFile GetQueuedCompletionStatus failed, errno=%i\n", GetLastError());
1039 ok(compkey
== 12345, "got completion key %i instead of 12345\n", (int)compkey
);
1040 ok(oResult
== &oWrite
, "got overlapped pointer %p instead of %p\n", oResult
, &oWrite
);
1041 ok(written
== readden
, "write file len\n");
1043 trace("Server done writing.\n");
1045 /* finish this connection, wait for next one */
1046 ok(FlushFileBuffers(hnp
), "FlushFileBuffers\n");
1047 success
= DisconnectNamedPipe(hnp
);
1048 ok(success
, "DisconnectNamedPipe failed, err %u\n", GetLastError());
1051 ret
= CloseHandle(hnp
);
1052 ok(ret
, "CloseHandle named pipe failed, err=%i\n", GetLastError());
1053 ret
= CloseHandle(hcompletion
);
1054 ok(ret
, "CloseHandle completion failed, err=%i\n", GetLastError());
1059 static int completion_called
;
1060 static DWORD completion_errorcode
;
1061 static DWORD completion_num_bytes
;
1062 static LPOVERLAPPED completion_lpoverlapped
;
1064 static VOID WINAPI
completion_routine(DWORD errorcode
, DWORD num_bytes
, LPOVERLAPPED lpoverlapped
)
1066 completion_called
++;
1067 completion_errorcode
= errorcode
;
1068 completion_num_bytes
= num_bytes
;
1069 completion_lpoverlapped
= lpoverlapped
;
1070 SetEvent(lpoverlapped
->hEvent
);
1073 /** Trivial byte echo server - uses ReadFileEx/WriteFileEx */
1074 static DWORD CALLBACK
serverThreadMain5(LPVOID arg
)
1079 trace("serverThreadMain5\n");
1080 /* Set up a simple echo server */
1081 hnp
= CreateNamedPipeA(PIPENAME
"serverThreadMain5", PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1082 PIPE_TYPE_BYTE
| PIPE_WAIT
,
1083 /* nMaxInstances */ 1,
1084 /* nOutBufSize */ 1024,
1085 /* nInBufSize */ 1024,
1086 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
1087 /* lpSecurityAttrib */ NULL
);
1088 ok(hnp
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
1090 hEvent
= CreateEventW(NULL
, /* security attribute */
1091 TRUE
, /* manual reset event */
1092 FALSE
, /* initial state */
1094 ok(hEvent
!= NULL
, "CreateEvent\n");
1096 for (i
= 0; i
< NB_SERVER_LOOPS
; i
++) {
1100 OVERLAPPED oOverlap
;
1103 memset(&oOverlap
, 0, sizeof(oOverlap
));
1104 oOverlap
.hEvent
= hEvent
;
1106 /* Wait for client to connect */
1107 trace("Server calling ConnectNamedPipe...\n");
1108 success
= ConnectNamedPipe(hnp
, NULL
);
1109 err
= GetLastError();
1110 ok(success
|| (err
== ERROR_PIPE_CONNECTED
), "ConnectNamedPipe failed: %d\n", err
);
1111 trace("ConnectNamedPipe operation complete.\n");
1113 /* Echo bytes once */
1114 memset(buf
, 0, sizeof(buf
));
1116 trace("Server reading...\n");
1117 completion_called
= 0;
1119 success
= ReadFileEx(hnp
, buf
, sizeof(buf
), &oOverlap
, completion_routine
);
1120 trace("Server ReadFileEx returned...\n");
1121 ok(success
, "ReadFileEx failed, err=%i\n", GetLastError());
1122 ok(completion_called
== 0, "completion routine called before ReadFileEx return\n");
1123 trace("ReadFileEx returned.\n");
1127 ret
= WaitForSingleObjectEx(hEvent
, INFINITE
, TRUE
);
1128 } while (ret
== WAIT_IO_COMPLETION
);
1129 ok(ret
== 0, "wait ReadFileEx returned %x\n", ret
);
1131 ok(completion_called
== 1, "completion routine called %i times\n", completion_called
);
1132 ok(completion_errorcode
== ERROR_SUCCESS
, "completion routine got error %d\n", completion_errorcode
);
1133 ok(completion_num_bytes
!= 0, "read 0 bytes\n");
1134 ok(completion_lpoverlapped
== &oOverlap
, "got wrong overlapped pointer %p\n", completion_lpoverlapped
);
1135 readden
= completion_num_bytes
;
1136 trace("Server done reading.\n");
1138 trace("Server writing...\n");
1139 completion_called
= 0;
1141 success
= WriteFileEx(hnp
, buf
, readden
, &oOverlap
, completion_routine
);
1142 trace("Server WriteFileEx returned...\n");
1143 ok(success
, "WriteFileEx failed, err=%i\n", GetLastError());
1144 ok(completion_called
== 0, "completion routine called before ReadFileEx return\n");
1145 trace("overlapped WriteFile returned.\n");
1149 ret
= WaitForSingleObjectEx(hEvent
, INFINITE
, TRUE
);
1150 } while (ret
== WAIT_IO_COMPLETION
);
1151 ok(ret
== 0, "wait WriteFileEx returned %x\n", ret
);
1153 trace("Server done writing.\n");
1154 ok(completion_called
== 1, "completion routine called %i times\n", completion_called
);
1155 ok(completion_errorcode
== ERROR_SUCCESS
, "completion routine got error %d\n", completion_errorcode
);
1156 ok(completion_num_bytes
== readden
, "read %i bytes wrote %i\n", readden
, completion_num_bytes
);
1157 ok(completion_lpoverlapped
== &oOverlap
, "got wrong overlapped pointer %p\n", completion_lpoverlapped
);
1159 /* finish this connection, wait for next one */
1160 ok(FlushFileBuffers(hnp
), "FlushFileBuffers\n");
1161 ok(DisconnectNamedPipe(hnp
), "DisconnectNamedPipe\n");
1166 static void exercizeServer(const char *pipename
, HANDLE serverThread
)
1170 trace("exercizeServer starting\n");
1171 for (i
= 0; i
< NB_SERVER_LOOPS
; i
++) {
1172 HANDLE hFile
=INVALID_HANDLE_VALUE
;
1173 static const char obuf
[] = "Bit Bucket";
1179 for (loop
= 0; loop
< 3; loop
++) {
1181 trace("Client connecting...\n");
1182 /* Connect to the server */
1183 hFile
= CreateFileA(pipename
, GENERIC_READ
| GENERIC_WRITE
, 0,
1184 NULL
, OPEN_EXISTING
, 0, 0);
1185 if (hFile
!= INVALID_HANDLE_VALUE
)
1187 err
= GetLastError();
1189 ok(err
== ERROR_PIPE_BUSY
|| err
== ERROR_FILE_NOT_FOUND
, "connecting to pipe\n");
1191 ok(err
== ERROR_PIPE_BUSY
, "connecting to pipe\n");
1192 trace("connect failed, retrying\n");
1195 ok(hFile
!= INVALID_HANDLE_VALUE
, "client opening named pipe\n");
1197 /* Make sure it can echo */
1198 memset(ibuf
, 0, sizeof(ibuf
));
1199 trace("Client writing...\n");
1200 ok(WriteFile(hFile
, obuf
, sizeof(obuf
), &written
, NULL
), "WriteFile to client end of pipe\n");
1201 ok(written
== sizeof(obuf
), "write file len\n");
1202 trace("Client reading...\n");
1203 ok(ReadFile(hFile
, ibuf
, sizeof(obuf
), &readden
, NULL
), "ReadFile from client end of pipe\n");
1204 ok(readden
== sizeof(obuf
), "read file len\n");
1205 ok(memcmp(obuf
, ibuf
, written
) == 0, "content check\n");
1207 trace("Client closing...\n");
1208 ok(CloseHandle(hFile
), "CloseHandle\n");
1211 ok(WaitForSingleObject(serverThread
,INFINITE
) == WAIT_OBJECT_0
, "WaitForSingleObject\n");
1213 trace("exercizeServer returning\n");
1216 static void test_NamedPipe_2(void)
1218 HANDLE serverThread
;
1219 DWORD serverThreadId
;
1221 DWORD alarmThreadId
;
1223 trace("test_NamedPipe_2 starting\n");
1224 /* Set up a twenty second timeout */
1225 alarm_event
= CreateEventW( NULL
, TRUE
, FALSE
, NULL
);
1226 SetLastError(0xdeadbeef);
1227 alarmThread
= CreateThread(NULL
, 0, alarmThreadMain
, (void *) 20000, 0, &alarmThreadId
);
1228 ok(alarmThread
!= NULL
, "CreateThread failed: %d\n", GetLastError());
1230 /* The servers we're about to exercise do try to clean up carefully,
1231 * but to reduce the chance of a test failure due to a pipe handle
1232 * leak in the test code, we'll use a different pipe name for each server.
1236 SetLastError(0xdeadbeef);
1237 serverThread
= CreateThread(NULL
, 0, serverThreadMain1
, (void *)8, 0, &serverThreadId
);
1238 ok(serverThread
!= NULL
, "CreateThread failed: %d\n", GetLastError());
1239 exercizeServer(PIPENAME
"serverThreadMain1", serverThread
);
1242 SetLastError(0xdeadbeef);
1243 serverThread
= CreateThread(NULL
, 0, serverThreadMain2
, 0, 0, &serverThreadId
);
1244 ok(serverThread
!= NULL
, "CreateThread failed: %d\n", GetLastError());
1245 exercizeServer(PIPENAME
"serverThreadMain2", serverThread
);
1248 SetLastError(0xdeadbeef);
1249 serverThread
= CreateThread(NULL
, 0, serverThreadMain3
, 0, 0, &serverThreadId
);
1250 ok(serverThread
!= NULL
, "CreateThread failed: %d\n", GetLastError());
1251 exercizeServer(PIPENAME
"serverThreadMain3", serverThread
);
1254 SetLastError(0xdeadbeef);
1255 serverThread
= CreateThread(NULL
, 0, serverThreadMain4
, 0, 0, &serverThreadId
);
1256 ok(serverThread
!= NULL
, "CreateThread failed: %d\n", GetLastError());
1257 exercizeServer(PIPENAME
"serverThreadMain4", serverThread
);
1260 SetLastError(0xdeadbeef);
1261 serverThread
= CreateThread(NULL
, 0, serverThreadMain5
, 0, 0, &serverThreadId
);
1262 ok(serverThread
!= NULL
, "CreateThread failed: %d\n", GetLastError());
1263 exercizeServer(PIPENAME
"serverThreadMain5", serverThread
);
1265 ok(SetEvent( alarm_event
), "SetEvent\n");
1266 CloseHandle( alarm_event
);
1267 trace("test_NamedPipe_2 returning\n");
1270 static int test_DisconnectNamedPipe(void)
1274 static const char obuf
[] = "Bit Bucket";
1280 SetLastError(0xdeadbeef);
1281 hnp
= CreateNamedPipeA(PIPENAME
, PIPE_ACCESS_DUPLEX
, PIPE_TYPE_BYTE
| PIPE_WAIT
,
1282 /* nMaxInstances */ 1,
1283 /* nOutBufSize */ 1024,
1284 /* nInBufSize */ 1024,
1285 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
1286 /* lpSecurityAttrib */ NULL
);
1287 if ((hnp
== INVALID_HANDLE_VALUE
/* Win98 */ || !hnp
/* Win95 */)
1288 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
) {
1290 win_skip("Named pipes are not implemented\n");
1294 ok(WriteFile(hnp
, obuf
, sizeof(obuf
), &written
, NULL
) == 0
1295 && GetLastError() == ERROR_PIPE_LISTENING
, "WriteFile to not-yet-connected pipe\n");
1296 ok(ReadFile(hnp
, ibuf
, sizeof(ibuf
), &readden
, NULL
) == 0
1297 && GetLastError() == ERROR_PIPE_LISTENING
, "ReadFile from not-yet-connected pipe\n");
1299 hFile
= CreateFileA(PIPENAME
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0);
1300 ok(hFile
!= INVALID_HANDLE_VALUE
, "CreateFile failed\n");
1302 /* don't try to do i/o if one side couldn't be opened, as it hangs */
1303 if (hFile
!= INVALID_HANDLE_VALUE
) {
1305 /* see what happens if server calls DisconnectNamedPipe
1306 * when there are bytes in the pipe
1309 ok(WriteFile(hFile
, obuf
, sizeof(obuf
), &written
, NULL
), "WriteFile\n");
1310 ok(written
== sizeof(obuf
), "write file len\n");
1311 ok(DisconnectNamedPipe(hnp
), "DisconnectNamedPipe while messages waiting\n");
1312 ok(WriteFile(hFile
, obuf
, sizeof(obuf
), &written
, NULL
) == 0
1313 && GetLastError() == ERROR_PIPE_NOT_CONNECTED
, "WriteFile to disconnected pipe\n");
1314 ok(ReadFile(hnp
, ibuf
, sizeof(ibuf
), &readden
, NULL
) == 0
1315 && GetLastError() == ERROR_PIPE_NOT_CONNECTED
,
1316 "ReadFile from disconnected pipe with bytes waiting\n");
1317 ok(!DisconnectNamedPipe(hnp
) && GetLastError() == ERROR_PIPE_NOT_CONNECTED
,
1318 "DisconnectNamedPipe worked twice\n");
1319 ret
= WaitForSingleObject(hFile
, 0);
1320 ok(ret
== WAIT_TIMEOUT
, "WaitForSingleObject returned %X\n", ret
);
1321 ok(CloseHandle(hFile
), "CloseHandle\n");
1324 ok(CloseHandle(hnp
), "CloseHandle\n");
1328 static void test_CreatePipe(void)
1330 SECURITY_ATTRIBUTES pipe_attr
;
1331 HANDLE piperead
, pipewrite
;
1338 user_apc_ran
= FALSE
;
1340 ok(pQueueUserAPC(user_apc
, GetCurrentThread(), 0), "couldn't create user apc\n");
1342 pipe_attr
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
1343 pipe_attr
.bInheritHandle
= TRUE
;
1344 pipe_attr
.lpSecurityDescriptor
= NULL
;
1345 ok(CreatePipe(&piperead
, &pipewrite
, &pipe_attr
, 0) != 0, "CreatePipe failed\n");
1346 ok(WriteFile(pipewrite
,PIPENAME
,sizeof(PIPENAME
), &written
, NULL
), "Write to anonymous pipe failed\n");
1347 ok(written
== sizeof(PIPENAME
), "Write to anonymous pipe wrote %d bytes\n", written
);
1348 ok(ReadFile(piperead
,readbuf
,sizeof(readbuf
),&read
, NULL
), "Read from non empty pipe failed\n");
1349 ok(read
== sizeof(PIPENAME
), "Read from anonymous pipe got %d bytes\n", read
);
1350 ok(CloseHandle(pipewrite
), "CloseHandle for the write pipe failed\n");
1351 ok(CloseHandle(piperead
), "CloseHandle for the read pipe failed\n");
1353 /* Now write another chunk*/
1354 ok(CreatePipe(&piperead
, &pipewrite
, &pipe_attr
, 0) != 0, "CreatePipe failed\n");
1355 ok(WriteFile(pipewrite
,PIPENAME
,sizeof(PIPENAME
), &written
, NULL
), "Write to anonymous pipe failed\n");
1356 ok(written
== sizeof(PIPENAME
), "Write to anonymous pipe wrote %d bytes\n", written
);
1357 /* and close the write end, read should still succeed*/
1358 ok(CloseHandle(pipewrite
), "CloseHandle for the Write Pipe failed\n");
1359 ok(ReadFile(piperead
,readbuf
,sizeof(readbuf
),&read
, NULL
), "Read from broken pipe withe with pending data failed\n");
1360 ok(read
== sizeof(PIPENAME
), "Read from anonymous pipe got %d bytes\n", read
);
1361 /* But now we need to get informed that the pipe is closed */
1362 ok(ReadFile(piperead
,readbuf
,sizeof(readbuf
),&read
, NULL
) == 0, "Broken pipe not detected\n");
1363 ok(CloseHandle(piperead
), "CloseHandle for the read pipe failed\n");
1365 /* Try bigger chunks */
1367 buffer
= HeapAlloc( GetProcessHeap(), 0, size
);
1368 for (i
= 0; i
< size
; i
++) buffer
[i
] = i
;
1369 ok(CreatePipe(&piperead
, &pipewrite
, &pipe_attr
, (size
+ 24)) != 0, "CreatePipe failed\n");
1370 ok(WriteFile(pipewrite
, buffer
, size
, &written
, NULL
), "Write to anonymous pipe failed\n");
1371 ok(written
== size
, "Write to anonymous pipe wrote %d bytes\n", written
);
1372 /* and close the write end, read should still succeed*/
1373 ok(CloseHandle(pipewrite
), "CloseHandle for the Write Pipe failed\n");
1374 memset( buffer
, 0, size
);
1375 ok(ReadFile(piperead
, buffer
, size
, &read
, NULL
), "Read from broken pipe withe with pending data failed\n");
1376 ok(read
== size
, "Read from anonymous pipe got %d bytes\n", read
);
1377 for (i
= 0; i
< size
; i
++) ok( buffer
[i
] == (BYTE
)i
, "invalid data %x at %x\n", buffer
[i
], i
);
1378 /* But now we need to get informed that the pipe is closed */
1379 ok(ReadFile(piperead
,readbuf
,sizeof(readbuf
),&read
, NULL
) == 0, "Broken pipe not detected\n");
1380 ok(CloseHandle(piperead
), "CloseHandle for the read pipe failed\n");
1381 HeapFree(GetProcessHeap(), 0, buffer
);
1383 ok(user_apc_ran
== FALSE
, "user apc ran, pipe using alertable io mode\n");
1384 SleepEx(0, TRUE
); /* get rid of apc */
1387 static void test_CloseHandle(void)
1389 static const char testdata
[] = "Hello World";
1390 DWORD state
, numbytes
;
1391 HANDLE hpipe
, hfile
;
1395 hpipe
= CreateNamedPipeA(PIPENAME
, PIPE_ACCESS_DUPLEX
,
1396 PIPE_TYPE_MESSAGE
| PIPE_READMODE_MESSAGE
| PIPE_WAIT
,
1397 1, 1024, 1024, NMPWAIT_USE_DEFAULT_WAIT
, NULL
);
1398 ok(hpipe
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed with %u\n", GetLastError());
1400 hfile
= CreateFileA(PIPENAME
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0);
1401 ok(hfile
!= INVALID_HANDLE_VALUE
, "CreateFile failed with %u\n", GetLastError());
1403 numbytes
= 0xdeadbeef;
1404 ret
= WriteFile(hpipe
, testdata
, sizeof(testdata
), &numbytes
, NULL
);
1405 ok(ret
, "WriteFile failed with %u\n", GetLastError());
1406 ok(numbytes
== sizeof(testdata
), "expected sizeof(testdata), got %u\n", numbytes
);
1408 numbytes
= 0xdeadbeef;
1409 ret
= PeekNamedPipe(hfile
, NULL
, 0, NULL
, &numbytes
, NULL
);
1410 ok(ret
, "PeekNamedPipe failed with %u\n", GetLastError());
1411 ok(numbytes
== sizeof(testdata
), "expected sizeof(testdata), got %u\n", numbytes
);
1413 ret
= CloseHandle(hpipe
);
1414 ok(ret
, "CloseHandle failed with %u\n", GetLastError());
1416 numbytes
= 0xdeadbeef;
1417 memset(buffer
, 0, sizeof(buffer
));
1418 ret
= ReadFile(hfile
, buffer
, 0, &numbytes
, NULL
);
1419 todo_wine
ok(ret
, "ReadFile failed with %u\n", GetLastError());
1420 ok(numbytes
== 0, "expected 0, got %u\n", numbytes
);
1422 numbytes
= 0xdeadbeef;
1423 memset(buffer
, 0, sizeof(buffer
));
1424 ret
= ReadFile(hfile
, buffer
, sizeof(buffer
), &numbytes
, NULL
);
1425 ok(ret
, "ReadFile failed with %u\n", GetLastError());
1426 ok(numbytes
== sizeof(testdata
), "expected sizeof(testdata), got %u\n", numbytes
);
1428 ret
= GetNamedPipeHandleStateA(hfile
, &state
, NULL
, NULL
, NULL
, NULL
, 0);
1429 ok(ret
, "GetNamedPipeHandleState failed with %u\n", GetLastError());
1430 state
= PIPE_READMODE_MESSAGE
| PIPE_WAIT
;
1431 ret
= SetNamedPipeHandleState(hfile
, &state
, NULL
, NULL
);
1432 ok(ret
, "SetNamedPipeHandleState failed with %u\n", GetLastError());
1434 SetLastError(0xdeadbeef);
1435 ret
= ReadFile(hfile
, buffer
, 0, &numbytes
, NULL
);
1436 ok(!ret
, "ReadFile unexpectedly succeeded\n");
1437 ok(GetLastError() == ERROR_BROKEN_PIPE
, "expected ERROR_BROKEN_PIPE, got %u\n", GetLastError());
1439 SetLastError(0xdeadbeef);
1440 ret
= WriteFile(hfile
, testdata
, sizeof(testdata
), &numbytes
, NULL
);
1441 ok(!ret
, "WriteFile unexpectedly succeeded\n");
1442 todo_wine
ok(GetLastError() == ERROR_NO_DATA
, "expected ERROR_NO_DATA, got %u\n", GetLastError());
1446 hpipe
= CreateNamedPipeA(PIPENAME
, PIPE_ACCESS_DUPLEX
,
1447 PIPE_TYPE_MESSAGE
| PIPE_READMODE_MESSAGE
| PIPE_WAIT
,
1448 1, 1024, 1024, NMPWAIT_USE_DEFAULT_WAIT
, NULL
);
1449 ok(hpipe
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed with %u\n", GetLastError());
1451 hfile
= CreateFileA(PIPENAME
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0);
1452 ok(hfile
!= INVALID_HANDLE_VALUE
, "CreateFile failed with %u\n", GetLastError());
1454 numbytes
= 0xdeadbeef;
1455 ret
= WriteFile(hpipe
, testdata
, 0, &numbytes
, NULL
);
1456 ok(ret
, "WriteFile failed with %u\n", GetLastError());
1457 ok(numbytes
== 0, "expected 0, got %u\n", numbytes
);
1459 ret
= CloseHandle(hpipe
);
1460 ok(ret
, "CloseHandle failed with %u\n", GetLastError());
1462 numbytes
= 0xdeadbeef;
1463 memset(buffer
, 0, sizeof(buffer
));
1464 ret
= ReadFile(hfile
, buffer
, sizeof(buffer
), &numbytes
, NULL
);
1465 todo_wine
ok(ret
, "ReadFile failed with %u\n", GetLastError());
1466 ok(numbytes
== 0, "expected 0, got %u\n", numbytes
);
1468 ret
= GetNamedPipeHandleStateA(hfile
, &state
, NULL
, NULL
, NULL
, NULL
, 0);
1469 ok(ret
, "GetNamedPipeHandleState failed with %u\n", GetLastError());
1470 state
= PIPE_READMODE_MESSAGE
| PIPE_WAIT
;
1471 ret
= SetNamedPipeHandleState(hfile
, &state
, NULL
, NULL
);
1472 ok(ret
, "SetNamedPipeHandleState failed with %u\n", GetLastError());
1474 SetLastError(0xdeadbeef);
1475 ret
= ReadFile(hfile
, buffer
, 0, &numbytes
, NULL
);
1476 ok(!ret
, "ReadFile unexpectedly succeeded\n");
1477 todo_wine
ok(GetLastError() == ERROR_BROKEN_PIPE
, "expected ERROR_BROKEN_PIPE, got %u\n", GetLastError());
1479 SetLastError(0xdeadbeef);
1480 ret
= WriteFile(hfile
, testdata
, sizeof(testdata
), &numbytes
, NULL
);
1481 ok(!ret
, "WriteFile unexpectedly succeeded\n");
1482 todo_wine
ok(GetLastError() == ERROR_NO_DATA
, "expected ERROR_NO_DATA, got %u\n", GetLastError());
1486 /* repeat test with hpipe <-> hfile swapped */
1488 hpipe
= CreateNamedPipeA(PIPENAME
, PIPE_ACCESS_DUPLEX
,
1489 PIPE_TYPE_MESSAGE
| PIPE_READMODE_MESSAGE
| PIPE_WAIT
,
1490 1, 1024, 1024, NMPWAIT_USE_DEFAULT_WAIT
, NULL
);
1491 ok(hpipe
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed with %u\n", GetLastError());
1493 hfile
= CreateFileA(PIPENAME
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0);
1494 ok(hfile
!= INVALID_HANDLE_VALUE
, "CreateFile failed with %u\n", GetLastError());
1496 numbytes
= 0xdeadbeef;
1497 ret
= WriteFile(hfile
, testdata
, sizeof(testdata
), &numbytes
, NULL
);
1498 ok(ret
, "WriteFile failed with %u\n", GetLastError());
1499 ok(numbytes
== sizeof(testdata
), "expected sizeof(testdata), got %u\n", numbytes
);
1501 numbytes
= 0xdeadbeef;
1502 ret
= PeekNamedPipe(hpipe
, NULL
, 0, NULL
, &numbytes
, NULL
);
1503 ok(ret
, "PeekNamedPipe failed with %u\n", GetLastError());
1504 ok(numbytes
== sizeof(testdata
), "expected sizeof(testdata), got %u\n", numbytes
);
1506 ret
= CloseHandle(hfile
);
1507 ok(ret
, "CloseHandle failed with %u\n", GetLastError());
1509 numbytes
= 0xdeadbeef;
1510 memset(buffer
, 0, sizeof(buffer
));
1511 ret
= ReadFile(hpipe
, buffer
, 0, &numbytes
, NULL
);
1512 todo_wine
ok(ret
|| broken(GetLastError() == ERROR_MORE_DATA
) /* >= Win 8 */,
1513 "ReadFile failed with %u\n", GetLastError());
1514 ok(numbytes
== 0, "expected 0, got %u\n", numbytes
);
1516 numbytes
= 0xdeadbeef;
1517 memset(buffer
, 0, sizeof(buffer
));
1518 ret
= ReadFile(hpipe
, buffer
, sizeof(buffer
), &numbytes
, NULL
);
1519 ok(ret
, "ReadFile failed with %u\n", GetLastError());
1520 ok(numbytes
== sizeof(testdata
), "expected sizeof(testdata), got %u\n", numbytes
);
1522 ret
= GetNamedPipeHandleStateA(hpipe
, &state
, NULL
, NULL
, NULL
, NULL
, 0);
1523 ok(ret
, "GetNamedPipeHandleState failed with %u\n", GetLastError());
1524 state
= PIPE_READMODE_MESSAGE
| PIPE_WAIT
;
1525 ret
= SetNamedPipeHandleState(hpipe
, &state
, NULL
, NULL
);
1526 ok(ret
, "SetNamedPipeHandleState failed with %u\n", GetLastError());
1528 SetLastError(0xdeadbeef);
1529 ret
= ReadFile(hpipe
, buffer
, 0, &numbytes
, NULL
);
1530 ok(!ret
, "ReadFile unexpectedly succeeded\n");
1531 ok(GetLastError() == ERROR_BROKEN_PIPE
, "expected ERROR_BROKEN_PIPE, got %u\n", GetLastError());
1533 SetLastError(0xdeadbeef);
1534 ret
= WriteFile(hpipe
, testdata
, sizeof(testdata
), &numbytes
, NULL
);
1535 ok(!ret
, "WriteFile unexpectedly succeeded\n");
1536 todo_wine
ok(GetLastError() == ERROR_NO_DATA
, "expected ERROR_NO_DATA, got %u\n", GetLastError());
1540 hpipe
= CreateNamedPipeA(PIPENAME
, PIPE_ACCESS_DUPLEX
,
1541 PIPE_TYPE_MESSAGE
| PIPE_READMODE_MESSAGE
| PIPE_WAIT
,
1542 1, 1024, 1024, NMPWAIT_USE_DEFAULT_WAIT
, NULL
);
1543 ok(hpipe
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed with %u\n", GetLastError());
1545 hfile
= CreateFileA(PIPENAME
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0);
1546 ok(hfile
!= INVALID_HANDLE_VALUE
, "CreateFile failed with %u\n", GetLastError());
1548 numbytes
= 0xdeadbeef;
1549 ret
= WriteFile(hfile
, testdata
, 0, &numbytes
, NULL
);
1550 ok(ret
, "WriteFile failed with %u\n", GetLastError());
1551 ok(numbytes
== 0, "expected 0, got %u\n", numbytes
);
1553 ret
= CloseHandle(hfile
);
1554 ok(ret
, "CloseHandle failed with %u\n", GetLastError());
1556 numbytes
= 0xdeadbeef;
1557 memset(buffer
, 0, sizeof(buffer
));
1558 ret
= ReadFile(hpipe
, buffer
, sizeof(buffer
), &numbytes
, NULL
);
1559 todo_wine
ok(ret
, "ReadFile failed with %u\n", GetLastError());
1560 ok(numbytes
== 0, "expected 0, got %u\n", numbytes
);
1562 ret
= GetNamedPipeHandleStateA(hpipe
, &state
, NULL
, NULL
, NULL
, NULL
, 0);
1563 ok(ret
, "GetNamedPipeHandleState failed with %u\n", GetLastError());
1564 state
= PIPE_READMODE_MESSAGE
| PIPE_WAIT
;
1565 ret
= SetNamedPipeHandleState(hpipe
, &state
, NULL
, NULL
);
1566 ok(ret
, "SetNamedPipeHandleState failed with %u\n", GetLastError());
1568 SetLastError(0xdeadbeef);
1569 ret
= ReadFile(hpipe
, buffer
, 0, &numbytes
, NULL
);
1570 ok(!ret
, "ReadFile unexpectedly succeeded\n");
1571 ok(GetLastError() == ERROR_BROKEN_PIPE
, "expected ERROR_BROKEN_PIPE, got %u\n", GetLastError());
1573 SetLastError(0xdeadbeef);
1574 ret
= WriteFile(hpipe
, testdata
, sizeof(testdata
), &numbytes
, NULL
);
1575 ok(!ret
, "WriteFile unexpectedly succeeded\n");
1576 todo_wine
ok(GetLastError() == ERROR_NO_DATA
, "expected ERROR_NO_DATA, got %u\n", GetLastError());
1581 struct named_pipe_client_params
1583 DWORD security_flags
;
1588 #define PIPE_NAME "\\\\.\\pipe\\named_pipe_test"
1590 static DWORD CALLBACK
named_pipe_client_func(LPVOID p
)
1592 struct named_pipe_client_params
*params
= p
;
1595 const char message
[] = "Test";
1596 DWORD bytes_read
, bytes_written
;
1598 TOKEN_PRIVILEGES
*Privileges
= NULL
;
1604 /* modify the token so we can tell if the pipe impersonation
1605 * token reverts to the process token */
1606 ret
= AdjustTokenPrivileges(params
->token
, TRUE
, NULL
, 0, NULL
, NULL
);
1607 ok(ret
, "AdjustTokenPrivileges failed with error %d\n", GetLastError());
1609 ret
= SetThreadToken(NULL
, params
->token
);
1610 ok(ret
, "SetThreadToken failed with error %d\n", GetLastError());
1615 HANDLE process_token
;
1617 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY
|TOKEN_ADJUST_PRIVILEGES
, &process_token
);
1618 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
1620 ret
= GetTokenInformation(process_token
, TokenPrivileges
, NULL
, 0, &Size
);
1621 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "GetTokenInformation(TokenPrivileges) failed with %d\n", GetLastError());
1622 Privileges
= HeapAlloc(GetProcessHeap(), 0, Size
);
1623 ret
= GetTokenInformation(process_token
, TokenPrivileges
, Privileges
, Size
, &Size
);
1624 ok(ret
, "GetTokenInformation(TokenPrivileges) failed with %d\n", GetLastError());
1626 ret
= AdjustTokenPrivileges(process_token
, TRUE
, NULL
, 0, NULL
, NULL
);
1627 ok(ret
, "AdjustTokenPrivileges failed with error %d\n", GetLastError());
1629 CloseHandle(process_token
);
1632 pipe
= CreateFileA(PIPE_NAME
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, params
->security_flags
, NULL
);
1633 ok(pipe
!= INVALID_HANDLE_VALUE
, "CreateFile for pipe failed with error %d\n", GetLastError());
1635 ret
= WriteFile(pipe
, message
, sizeof(message
), &bytes_written
, NULL
);
1636 ok(ret
, "WriteFile failed with error %d\n", GetLastError());
1638 ret
= ReadFile(pipe
, &dummy
, sizeof(dummy
), &bytes_read
, NULL
);
1639 ok(ret
, "ReadFile failed with error %d\n", GetLastError());
1645 ret
= RevertToSelf();
1646 ok(ret
, "RevertToSelf failed with error %d\n", GetLastError());
1650 ret
= AdjustTokenPrivileges(params
->token
, TRUE
, NULL
, 0, NULL
, NULL
);
1651 ok(ret
, "AdjustTokenPrivileges failed with error %d\n", GetLastError());
1656 HANDLE process_token
;
1658 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES
, &process_token
);
1659 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
1661 ret
= AdjustTokenPrivileges(process_token
, FALSE
, Privileges
, 0, NULL
, NULL
);
1662 ok(ret
, "AdjustTokenPrivileges failed with error %d\n", GetLastError());
1664 HeapFree(GetProcessHeap(), 0, Privileges
);
1666 CloseHandle(process_token
);
1669 ret
= WriteFile(pipe
, message
, sizeof(message
), &bytes_written
, NULL
);
1670 ok(ret
, "WriteFile failed with error %d\n", GetLastError());
1672 ret
= ReadFile(pipe
, &dummy
, sizeof(dummy
), &bytes_read
, NULL
);
1673 ok(ret
, "ReadFile failed with error %d\n", GetLastError());
1680 static HANDLE
make_impersonation_token(DWORD Access
, SECURITY_IMPERSONATION_LEVEL ImpersonationLevel
)
1682 HANDLE ProcessToken
;
1683 HANDLE Token
= NULL
;
1686 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE
, &ProcessToken
);
1687 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
1689 ret
= pDuplicateTokenEx(ProcessToken
, Access
, NULL
, ImpersonationLevel
, TokenImpersonation
, &Token
);
1690 ok(ret
, "DuplicateToken failed with error %d\n", GetLastError());
1692 CloseHandle(ProcessToken
);
1697 static void test_ImpersonateNamedPipeClient(HANDLE hClientToken
, DWORD security_flags
, BOOL revert
, void (*test_func
)(int, HANDLE
))
1706 struct named_pipe_client_params params
;
1708 DWORD dwBytesWritten
;
1709 HANDLE hToken
= NULL
;
1710 SECURITY_IMPERSONATION_LEVEL ImpersonationLevel
;
1713 hPipeServer
= CreateNamedPipeA(PIPE_NAME
, PIPE_ACCESS_DUPLEX
, PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
| PIPE_WAIT
, 1, 100, 100, NMPWAIT_USE_DEFAULT_WAIT
, NULL
);
1714 ok(hPipeServer
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed with error %d\n", GetLastError());
1716 params
.security_flags
= security_flags
;
1717 params
.token
= hClientToken
;
1718 params
.revert
= revert
;
1719 hThread
= CreateThread(NULL
, 0, named_pipe_client_func
, ¶ms
, 0, &dwTid
);
1720 ok(hThread
!= NULL
, "CreateThread failed with error %d\n", GetLastError());
1722 SetLastError(0xdeadbeef);
1723 ret
= ImpersonateNamedPipeClient(hPipeServer
);
1724 error
= GetLastError();
1725 ok(ret
/* win2k3 */ || (error
== ERROR_CANNOT_IMPERSONATE
),
1726 "ImpersonateNamedPipeClient should have failed with ERROR_CANNOT_IMPERSONATE instead of %d\n", GetLastError());
1728 ret
= ConnectNamedPipe(hPipeServer
, NULL
);
1729 ok(ret
|| (GetLastError() == ERROR_PIPE_CONNECTED
), "ConnectNamedPipe failed with error %d\n", GetLastError());
1731 ret
= ReadFile(hPipeServer
, buffer
, sizeof(buffer
), &dwBytesRead
, NULL
);
1732 ok(ret
, "ReadFile failed with error %d\n", GetLastError());
1734 ret
= ImpersonateNamedPipeClient(hPipeServer
);
1735 ok(ret
, "ImpersonateNamedPipeClient failed with error %d\n", GetLastError());
1737 ret
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
, FALSE
, &hToken
);
1738 ok(ret
, "OpenThreadToken failed with error %d\n", GetLastError());
1740 (*test_func
)(0, hToken
);
1742 ImpersonationLevel
= 0xdeadbeef; /* to avoid false positives */
1743 ret
= GetTokenInformation(hToken
, TokenImpersonationLevel
, &ImpersonationLevel
, sizeof(ImpersonationLevel
), &size
);
1744 ok(ret
, "GetTokenInformation(TokenImpersonationLevel) failed with error %d\n", GetLastError());
1745 ok(ImpersonationLevel
== SecurityImpersonation
, "ImpersonationLevel should have been SecurityImpersonation(%d) instead of %d\n", SecurityImpersonation
, ImpersonationLevel
);
1747 CloseHandle(hToken
);
1751 ret
= WriteFile(hPipeServer
, &dummy
, sizeof(dummy
), &dwBytesWritten
, NULL
);
1752 ok(ret
, "WriteFile failed with error %d\n", GetLastError());
1754 ret
= ReadFile(hPipeServer
, buffer
, sizeof(buffer
), &dwBytesRead
, NULL
);
1755 ok(ret
, "ReadFile failed with error %d\n", GetLastError());
1757 ret
= ImpersonateNamedPipeClient(hPipeServer
);
1758 ok(ret
, "ImpersonateNamedPipeClient failed with error %d\n", GetLastError());
1760 ret
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
, FALSE
, &hToken
);
1761 ok(ret
, "OpenThreadToken failed with error %d\n", GetLastError());
1763 (*test_func
)(1, hToken
);
1765 CloseHandle(hToken
);
1769 ret
= WriteFile(hPipeServer
, &dummy
, sizeof(dummy
), &dwBytesWritten
, NULL
);
1770 ok(ret
, "WriteFile failed with error %d\n", GetLastError());
1772 WaitForSingleObject(hThread
, INFINITE
);
1774 ret
= ImpersonateNamedPipeClient(hPipeServer
);
1775 ok(ret
, "ImpersonateNamedPipeClient failed with error %d\n", GetLastError());
1779 CloseHandle(hThread
);
1780 CloseHandle(hPipeServer
);
1783 static BOOL
are_all_privileges_disabled(HANDLE hToken
)
1786 TOKEN_PRIVILEGES
*Privileges
= NULL
;
1788 BOOL all_privs_disabled
= TRUE
;
1791 ret
= GetTokenInformation(hToken
, TokenPrivileges
, NULL
, 0, &Size
);
1792 if (!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
1794 Privileges
= HeapAlloc(GetProcessHeap(), 0, Size
);
1795 ret
= GetTokenInformation(hToken
, TokenPrivileges
, Privileges
, Size
, &Size
);
1798 HeapFree(GetProcessHeap(), 0, Privileges
);
1805 for (i
= 0; i
< Privileges
->PrivilegeCount
; i
++)
1807 if (Privileges
->Privileges
[i
].Attributes
& SE_PRIVILEGE_ENABLED
)
1809 all_privs_disabled
= FALSE
;
1814 HeapFree(GetProcessHeap(), 0, Privileges
);
1816 return all_privs_disabled
;
1819 static DWORD
get_privilege_count(HANDLE hToken
)
1821 TOKEN_STATISTICS Statistics
;
1822 DWORD Size
= sizeof(Statistics
);
1825 ret
= GetTokenInformation(hToken
, TokenStatistics
, &Statistics
, Size
, &Size
);
1826 ok(ret
, "GetTokenInformation(TokenStatistics)\n");
1827 if (!ret
) return -1;
1829 return Statistics
.PrivilegeCount
;
1832 static void test_no_sqos_no_token(int call_index
, HANDLE hToken
)
1839 priv_count
= get_privilege_count(hToken
);
1841 ok(priv_count
== 0, "privilege count should have been 0 instead of %d\n", priv_count
);
1844 priv_count
= get_privilege_count(hToken
);
1845 ok(priv_count
> 0, "privilege count should now be > 0 instead of 0\n");
1846 ok(!are_all_privileges_disabled(hToken
), "impersonated token should not have been modified\n");
1849 ok(0, "shouldn't happen\n");
1853 static void test_no_sqos(int call_index
, HANDLE hToken
)
1858 ok(!are_all_privileges_disabled(hToken
), "token should be a copy of the process one\n");
1862 ok(are_all_privileges_disabled(hToken
), "impersonated token should have been modified\n");
1865 ok(0, "shouldn't happen\n");
1869 static void test_static_context(int call_index
, HANDLE hToken
)
1874 ok(!are_all_privileges_disabled(hToken
), "token should be a copy of the process one\n");
1877 ok(!are_all_privileges_disabled(hToken
), "impersonated token should not have been modified\n");
1880 ok(0, "shouldn't happen\n");
1884 static void test_dynamic_context(int call_index
, HANDLE hToken
)
1889 ok(!are_all_privileges_disabled(hToken
), "token should be a copy of the process one\n");
1893 ok(are_all_privileges_disabled(hToken
), "impersonated token should have been modified\n");
1896 ok(0, "shouldn't happen\n");
1900 static void test_dynamic_context_no_token(int call_index
, HANDLE hToken
)
1905 ok(are_all_privileges_disabled(hToken
), "token should be a copy of the process one\n");
1908 ok(!are_all_privileges_disabled(hToken
), "process token modification should have been detected and impersonation token updated\n");
1911 ok(0, "shouldn't happen\n");
1915 static void test_no_sqos_revert(int call_index
, HANDLE hToken
)
1921 priv_count
= get_privilege_count(hToken
);
1923 ok(priv_count
== 0, "privilege count should have been 0 instead of %d\n", priv_count
);
1926 priv_count
= get_privilege_count(hToken
);
1927 ok(priv_count
> 0, "privilege count should now be > 0 instead of 0\n");
1928 ok(!are_all_privileges_disabled(hToken
), "impersonated token should not have been modified\n");
1931 ok(0, "shouldn't happen\n");
1935 static void test_static_context_revert(int call_index
, HANDLE hToken
)
1941 ok(are_all_privileges_disabled(hToken
), "privileges should have been disabled\n");
1945 ok(are_all_privileges_disabled(hToken
), "impersonated token should not have been modified\n");
1948 ok(0, "shouldn't happen\n");
1952 static void test_dynamic_context_revert(int call_index
, HANDLE hToken
)
1958 ok(are_all_privileges_disabled(hToken
), "privileges should have been disabled\n");
1961 ok(!are_all_privileges_disabled(hToken
), "impersonated token should now be process token\n");
1964 ok(0, "shouldn't happen\n");
1968 static void test_impersonation(void)
1970 HANDLE hClientToken
;
1971 HANDLE hProcessToken
;
1974 if( !pDuplicateTokenEx
) {
1975 skip("DuplicateTokenEx not found\n");
1979 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY
, &hProcessToken
);
1982 skip("couldn't open process token, skipping impersonation tests\n");
1986 if (!get_privilege_count(hProcessToken
) || are_all_privileges_disabled(hProcessToken
))
1988 skip("token didn't have any privileges or they were all disabled. token not suitable for impersonation tests\n");
1989 CloseHandle(hProcessToken
);
1992 CloseHandle(hProcessToken
);
1994 test_ImpersonateNamedPipeClient(NULL
, 0, FALSE
, test_no_sqos_no_token
);
1995 hClientToken
= make_impersonation_token(TOKEN_IMPERSONATE
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY
, SecurityImpersonation
);
1996 test_ImpersonateNamedPipeClient(hClientToken
, 0, FALSE
, test_no_sqos
);
1997 CloseHandle(hClientToken
);
1998 hClientToken
= make_impersonation_token(TOKEN_IMPERSONATE
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY
, SecurityImpersonation
);
1999 test_ImpersonateNamedPipeClient(hClientToken
,
2000 SECURITY_SQOS_PRESENT
| SECURITY_IMPERSONATION
, FALSE
,
2001 test_static_context
);
2002 CloseHandle(hClientToken
);
2003 hClientToken
= make_impersonation_token(TOKEN_IMPERSONATE
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY
, SecurityImpersonation
);
2004 test_ImpersonateNamedPipeClient(hClientToken
,
2005 SECURITY_SQOS_PRESENT
| SECURITY_CONTEXT_TRACKING
| SECURITY_IMPERSONATION
,
2006 FALSE
, test_dynamic_context
);
2007 CloseHandle(hClientToken
);
2008 test_ImpersonateNamedPipeClient(NULL
,
2009 SECURITY_SQOS_PRESENT
| SECURITY_CONTEXT_TRACKING
| SECURITY_IMPERSONATION
,
2010 FALSE
, test_dynamic_context_no_token
);
2012 hClientToken
= make_impersonation_token(TOKEN_IMPERSONATE
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY
, SecurityImpersonation
);
2013 test_ImpersonateNamedPipeClient(hClientToken
, 0, TRUE
, test_no_sqos_revert
);
2014 CloseHandle(hClientToken
);
2015 hClientToken
= make_impersonation_token(TOKEN_IMPERSONATE
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY
, SecurityImpersonation
);
2016 test_ImpersonateNamedPipeClient(hClientToken
,
2017 SECURITY_SQOS_PRESENT
| SECURITY_IMPERSONATION
, TRUE
,
2018 test_static_context_revert
);
2019 CloseHandle(hClientToken
);
2020 hClientToken
= make_impersonation_token(TOKEN_IMPERSONATE
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY
, SecurityImpersonation
);
2021 test_ImpersonateNamedPipeClient(hClientToken
,
2022 SECURITY_SQOS_PRESENT
| SECURITY_CONTEXT_TRACKING
| SECURITY_IMPERSONATION
,
2023 TRUE
, test_dynamic_context_revert
);
2024 CloseHandle(hClientToken
);
2027 struct overlapped_server_args
2029 HANDLE pipe_created
;
2032 static DWORD CALLBACK
overlapped_server(LPVOID arg
)
2037 struct overlapped_server_args
*a
= arg
;
2041 pipe
= CreateNamedPipeA("\\\\.\\pipe\\my pipe", FILE_FLAG_OVERLAPPED
| PIPE_ACCESS_DUPLEX
, PIPE_TYPE_MESSAGE
| PIPE_READMODE_MESSAGE
, 1, 0, 0, 100000, NULL
);
2042 ok(pipe
!= NULL
, "pipe NULL\n");
2044 ol
.hEvent
= CreateEventA(0, 1, 0, 0);
2045 ok(ol
.hEvent
!= NULL
, "event NULL\n");
2046 ret
= ConnectNamedPipe(pipe
, &ol
);
2047 err
= GetLastError();
2048 ok(ret
== 0, "ret %d\n", ret
);
2049 ok(err
== ERROR_IO_PENDING
, "gle %d\n", err
);
2050 SetEvent(a
->pipe_created
);
2052 ret
= WaitForSingleObjectEx(ol
.hEvent
, INFINITE
, 1);
2053 ok(ret
== WAIT_OBJECT_0
, "ret %x\n", ret
);
2055 ret
= GetOverlappedResult(pipe
, &ol
, &num
, 1);
2056 ok(ret
== 1, "ret %d\n", ret
);
2058 /* This should block */
2059 ret
= ReadFile(pipe
, buf
, sizeof(buf
), &num
, NULL
);
2060 ok(ret
== 1, "ret %d\n", ret
);
2062 DisconnectNamedPipe(pipe
);
2063 CloseHandle(ol
.hEvent
);
2068 static void test_overlapped(void)
2071 HANDLE thread
, pipe
;
2073 struct overlapped_server_args args
;
2075 args
.pipe_created
= CreateEventA(0, 1, 0, 0);
2076 thread
= CreateThread(NULL
, 0, overlapped_server
, &args
, 0, &tid
);
2078 WaitForSingleObject(args
.pipe_created
, INFINITE
);
2079 pipe
= CreateFileA("\\\\.\\pipe\\my pipe", GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, NULL
);
2080 ok(pipe
!= INVALID_HANDLE_VALUE
, "cf failed\n");
2082 /* Sleep to try to get the ReadFile in the server to occur before the following WriteFile */
2085 ret
= WriteFile(pipe
, "x", 1, &num
, NULL
);
2086 ok(ret
, "WriteFile failed with error %d\n", GetLastError());
2088 WaitForSingleObject(thread
, INFINITE
);
2090 CloseHandle(args
.pipe_created
);
2091 CloseHandle(thread
);
2094 static void test_overlapped_error(void)
2096 HANDLE pipe
, file
, event
;
2097 DWORD err
, numbytes
;
2098 OVERLAPPED overlapped
;
2101 event
= CreateEventA(NULL
, TRUE
, FALSE
, NULL
);
2102 ok(event
!= NULL
, "CreateEventA failed with %u\n", GetLastError());
2104 pipe
= CreateNamedPipeA(PIPENAME
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
2105 PIPE_TYPE_MESSAGE
| PIPE_READMODE_MESSAGE
| PIPE_WAIT
,
2106 1, 1024, 1024, NMPWAIT_WAIT_FOREVER
, NULL
);
2107 ok(pipe
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed with %u\n", GetLastError());
2109 memset(&overlapped
, 0, sizeof(overlapped
));
2110 overlapped
.hEvent
= event
;
2111 ret
= ConnectNamedPipe(pipe
, &overlapped
);
2112 err
= GetLastError();
2113 ok(ret
== FALSE
, "ConnectNamedPipe succeeded\n");
2114 ok(err
== ERROR_IO_PENDING
, "expected ERROR_IO_PENDING, got %u\n", err
);
2116 file
= CreateFileA(PIPENAME
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
,
2117 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
2118 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile failed with %u\n", GetLastError());
2120 numbytes
= 0xdeadbeef;
2121 ret
= GetOverlappedResult(pipe
, &overlapped
, &numbytes
, TRUE
);
2122 ok(ret
== TRUE
, "GetOverlappedResult failed\n");
2123 ok(numbytes
== 0, "expected 0, got %u\n", numbytes
);
2124 ok(overlapped
.Internal
== STATUS_SUCCESS
, "expected STATUS_SUCCESS, got %08lx\n", overlapped
.Internal
);
2129 pipe
= CreateNamedPipeA(PIPENAME
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
2130 PIPE_TYPE_MESSAGE
| PIPE_READMODE_MESSAGE
| PIPE_WAIT
,
2131 1, 1024, 1024, NMPWAIT_WAIT_FOREVER
, NULL
);
2132 ok(pipe
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed with %u\n", GetLastError());
2134 file
= CreateFileA(PIPENAME
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
,
2135 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
2136 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile failed with %u\n", GetLastError());
2138 memset(&overlapped
, 0, sizeof(overlapped
));
2139 overlapped
.hEvent
= event
;
2140 ret
= ConnectNamedPipe(pipe
, &overlapped
);
2141 err
= GetLastError();
2142 ok(ret
== FALSE
, "ConnectNamedPipe succeeded\n");
2143 ok(err
== ERROR_PIPE_CONNECTED
, "expected ERROR_PIPE_CONNECTED, got %u\n", err
);
2144 ok(overlapped
.Internal
== STATUS_PENDING
, "expected STATUS_PENDING, got %08lx\n", overlapped
.Internal
);
2152 static void test_NamedPipeHandleState(void)
2154 HANDLE server
, client
;
2156 DWORD state
, instances
, maxCollectionCount
, collectDataTimeout
;
2157 char userName
[MAX_PATH
];
2159 server
= CreateNamedPipeA(PIPENAME
, PIPE_ACCESS_DUPLEX
,
2160 /* dwOpenMode */ PIPE_TYPE_BYTE
| PIPE_WAIT
,
2161 /* nMaxInstances */ 1,
2162 /* nOutBufSize */ 1024,
2163 /* nInBufSize */ 1024,
2164 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
2165 /* lpSecurityAttrib */ NULL
);
2166 ok(server
!= INVALID_HANDLE_VALUE
, "cf failed\n");
2167 ret
= GetNamedPipeHandleStateA(server
, NULL
, NULL
, NULL
, NULL
, NULL
, 0);
2168 ok(ret
, "GetNamedPipeHandleState failed: %d\n", GetLastError());
2169 ret
= GetNamedPipeHandleStateA(server
, &state
, &instances
, NULL
, NULL
, NULL
,
2171 ok(ret
, "GetNamedPipeHandleState failed: %d\n", GetLastError());
2174 ok(state
== 0, "unexpected state %08x\n", state
);
2175 ok(instances
== 1, "expected 1 instances, got %d\n", instances
);
2177 /* Some parameters have no meaning, and therefore can't be retrieved,
2180 SetLastError(0xdeadbeef);
2181 ret
= GetNamedPipeHandleStateA(server
, &state
, &instances
,
2182 &maxCollectionCount
, &collectDataTimeout
, userName
,
2183 sizeof(userName
) / sizeof(userName
[0]));
2185 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
2186 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
2187 /* A byte-mode pipe server can't be changed to message mode. */
2188 state
= PIPE_READMODE_MESSAGE
;
2189 SetLastError(0xdeadbeef);
2190 ret
= SetNamedPipeHandleState(server
, &state
, NULL
, NULL
);
2191 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
2192 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
2194 client
= CreateFileA(PIPENAME
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
2195 OPEN_EXISTING
, 0, NULL
);
2196 ok(client
!= INVALID_HANDLE_VALUE
, "cf failed\n");
2198 state
= PIPE_READMODE_BYTE
;
2199 ret
= SetNamedPipeHandleState(client
, &state
, NULL
, NULL
);
2200 ok(ret
, "SetNamedPipeHandleState failed: %d\n", GetLastError());
2201 /* A byte-mode pipe client can't be changed to message mode, either. */
2202 state
= PIPE_READMODE_MESSAGE
;
2203 SetLastError(0xdeadbeef);
2204 ret
= SetNamedPipeHandleState(server
, &state
, NULL
, NULL
);
2205 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
2206 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
2208 CloseHandle(client
);
2209 CloseHandle(server
);
2211 server
= CreateNamedPipeA(PIPENAME
, PIPE_ACCESS_DUPLEX
,
2212 /* dwOpenMode */ PIPE_TYPE_MESSAGE
| PIPE_WAIT
,
2213 /* nMaxInstances */ 1,
2214 /* nOutBufSize */ 1024,
2215 /* nInBufSize */ 1024,
2216 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
2217 /* lpSecurityAttrib */ NULL
);
2218 ok(server
!= INVALID_HANDLE_VALUE
, "cf failed\n");
2219 ret
= GetNamedPipeHandleStateA(server
, NULL
, NULL
, NULL
, NULL
, NULL
, 0);
2220 ok(ret
, "GetNamedPipeHandleState failed: %d\n", GetLastError());
2221 ret
= GetNamedPipeHandleStateA(server
, &state
, &instances
, NULL
, NULL
, NULL
,
2223 ok(ret
, "GetNamedPipeHandleState failed: %d\n", GetLastError());
2226 ok(state
== 0, "unexpected state %08x\n", state
);
2227 ok(instances
== 1, "expected 1 instances, got %d\n", instances
);
2229 /* In contrast to byte-mode pipes, a message-mode pipe server can be
2230 * changed to byte mode.
2232 state
= PIPE_READMODE_BYTE
;
2233 ret
= SetNamedPipeHandleState(server
, &state
, NULL
, NULL
);
2234 ok(ret
, "SetNamedPipeHandleState failed: %d\n", GetLastError());
2236 client
= CreateFileA(PIPENAME
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
2237 OPEN_EXISTING
, 0, NULL
);
2238 ok(client
!= INVALID_HANDLE_VALUE
, "cf failed\n");
2240 state
= PIPE_READMODE_MESSAGE
;
2241 ret
= SetNamedPipeHandleState(client
, &state
, NULL
, NULL
);
2242 ok(ret
, "SetNamedPipeHandleState failed: %d\n", GetLastError());
2243 /* A message-mode pipe client can also be changed to byte mode.
2245 state
= PIPE_READMODE_BYTE
;
2246 ret
= SetNamedPipeHandleState(client
, &state
, NULL
, NULL
);
2247 ok(ret
, "SetNamedPipeHandleState failed: %d\n", GetLastError());
2249 CloseHandle(client
);
2250 CloseHandle(server
);
2253 #define test_pipe_info(a,b,c,d,e) _test_pipe_info(__LINE__,a,b,c,d,e)
2254 static void _test_pipe_info(unsigned line
, HANDLE pipe
, DWORD ex_flags
, DWORD ex_out_buf_size
, DWORD ex_in_buf_size
, DWORD ex_max_instances
)
2256 DWORD flags
= 0xdeadbeef, out_buf_size
= 0xdeadbeef, in_buf_size
= 0xdeadbeef, max_instances
= 0xdeadbeef;
2259 res
= GetNamedPipeInfo(pipe
, &flags
, &out_buf_size
, &in_buf_size
, &max_instances
);
2260 ok_(__FILE__
,line
)(res
, "GetNamedPipeInfo failed: %x\n", res
);
2261 ok_(__FILE__
,line
)(flags
== ex_flags
, "flags = %x, expected %x\n", flags
, ex_flags
);
2262 ok_(__FILE__
,line
)(out_buf_size
== ex_out_buf_size
, "out_buf_size = %x, expected %u\n", out_buf_size
, ex_out_buf_size
);
2263 ok_(__FILE__
,line
)(in_buf_size
== ex_in_buf_size
, "in_buf_size = %x, expected %u\n", in_buf_size
, ex_in_buf_size
);
2264 ok_(__FILE__
,line
)(max_instances
== ex_max_instances
, "max_instances = %x, expected %u\n", max_instances
, ex_max_instances
);
2267 static void test_GetNamedPipeInfo(void)
2271 server
= CreateNamedPipeA(PIPENAME
, PIPE_ACCESS_DUPLEX
,
2272 /* dwOpenMode */ PIPE_TYPE_BYTE
| PIPE_WAIT
,
2273 /* nMaxInstances */ 1,
2274 /* nOutBufSize */ 1024,
2275 /* nInBufSize */ 1024,
2276 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
2277 /* lpSecurityAttrib */ NULL
);
2278 ok(server
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
2280 test_pipe_info(server
, PIPE_SERVER_END
| PIPE_TYPE_BYTE
, 1024, 1024, 1);
2282 CloseHandle(server
);
2284 server
= CreateNamedPipeA(PIPENAME
, PIPE_ACCESS_DUPLEX
,
2285 /* dwOpenMode */ PIPE_TYPE_MESSAGE
| PIPE_NOWAIT
,
2286 /* nMaxInstances */ 3,
2287 /* nOutBufSize */ 1024,
2288 /* nInBufSize */ 1024,
2289 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
2290 /* lpSecurityAttrib */ NULL
);
2291 ok(server
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
2293 test_pipe_info(server
, PIPE_SERVER_END
| PIPE_TYPE_MESSAGE
, 1024, 1024, 3);
2295 CloseHandle(server
);
2297 server
= CreateNamedPipeA(PIPENAME
, FILE_FLAG_OVERLAPPED
| PIPE_ACCESS_DUPLEX
,
2298 /* dwOpenMode */ PIPE_TYPE_MESSAGE
| PIPE_WAIT
,
2299 /* nMaxInstances */ 1,
2300 /* nOutBufSize */ 0,
2302 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
2303 /* lpSecurityAttrib */ NULL
);
2304 ok(server
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
2306 test_pipe_info(server
, PIPE_SERVER_END
| PIPE_TYPE_MESSAGE
, 0, 0, 1);
2308 CloseHandle(server
);
2310 server
= CreateNamedPipeA(PIPENAME
, FILE_FLAG_OVERLAPPED
| PIPE_ACCESS_DUPLEX
,
2311 /* dwOpenMode */ PIPE_TYPE_MESSAGE
| PIPE_WAIT
,
2312 /* nMaxInstances */ 1,
2313 /* nOutBufSize */ 0xf000,
2314 /* nInBufSize */ 0xf000,
2315 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
2316 /* lpSecurityAttrib */ NULL
);
2317 ok(server
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe failed\n");
2319 test_pipe_info(server
, PIPE_SERVER_END
| PIPE_TYPE_MESSAGE
, 0xf000, 0xf000, 1);
2321 CloseHandle(server
);
2324 static void test_readfileex_pending(void)
2326 HANDLE server
, client
, event
;
2328 DWORD err
, wait
, num_bytes
;
2329 OVERLAPPED overlapped
;
2330 char read_buf
[1024];
2331 char write_buf
[1024];
2332 const char test_string
[] = "test";
2335 server
= CreateNamedPipeA(PIPENAME
, FILE_FLAG_OVERLAPPED
| PIPE_ACCESS_DUPLEX
,
2336 /* dwOpenMode */ PIPE_TYPE_BYTE
| PIPE_WAIT
,
2337 /* nMaxInstances */ 1,
2338 /* nOutBufSize */ 1024,
2339 /* nInBufSize */ 1024,
2340 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT
,
2341 /* lpSecurityAttrib */ NULL
);
2342 ok(server
!= INVALID_HANDLE_VALUE
, "cf failed\n");
2344 event
= CreateEventA(NULL
, TRUE
, FALSE
, NULL
);
2345 ok(event
!= NULL
, "CreateEventA failed\n");
2347 memset(&overlapped
, 0, sizeof(overlapped
));
2348 overlapped
.hEvent
= event
;
2350 ret
= ConnectNamedPipe(server
, &overlapped
);
2351 err
= GetLastError();
2352 ok(ret
== FALSE
, "ConnectNamedPipe succeeded\n");
2353 ok(err
== ERROR_IO_PENDING
, "ConnectNamedPipe set error %i\n", err
);
2355 wait
= WaitForSingleObject(event
, 0);
2356 ok(wait
== WAIT_TIMEOUT
, "WaitForSingleObject returned %x\n", wait
);
2358 client
= CreateFileA(PIPENAME
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
2359 OPEN_EXISTING
, 0, NULL
);
2360 ok(client
!= INVALID_HANDLE_VALUE
, "cf failed\n");
2362 wait
= WaitForSingleObject(event
, 0);
2363 ok(wait
== WAIT_OBJECT_0
, "WaitForSingleObject returned %x\n", wait
);
2365 /* Start a read that can't complete immediately. */
2366 completion_called
= 0;
2368 ret
= ReadFileEx(server
, read_buf
, sizeof(read_buf
), &overlapped
, completion_routine
);
2369 ok(ret
== TRUE
, "ReadFileEx failed, err=%i\n", GetLastError());
2370 ok(completion_called
== 0, "completion routine called before ReadFileEx returned\n");
2372 ret
= WriteFile(client
, test_string
, strlen(test_string
), &num_bytes
, NULL
);
2373 ok(ret
== TRUE
, "WriteFile failed\n");
2374 ok(num_bytes
== strlen(test_string
), "only %i bytes written\n", num_bytes
);
2376 ok(completion_called
== 0, "completion routine called during WriteFile\n");
2378 wait
= WaitForSingleObjectEx(event
, 0, TRUE
);
2379 ok(wait
== WAIT_IO_COMPLETION
|| wait
== WAIT_OBJECT_0
, "WaitForSingleObjectEx returned %x\n", wait
);
2381 ok(completion_called
== 1, "completion not called after writing pipe\n");
2382 ok(completion_errorcode
== 0, "completion called with error %x\n", completion_errorcode
);
2383 ok(completion_num_bytes
== strlen(test_string
), "ReadFileEx returned only %d bytes\n", completion_num_bytes
);
2384 ok(completion_lpoverlapped
== &overlapped
, "completion called with wrong overlapped pointer\n");
2385 ok(!memcmp(test_string
, read_buf
, strlen(test_string
)), "ReadFileEx read wrong bytes\n");
2387 /* Make writes until the pipe is full and the write fails */
2388 memset(write_buf
, 0xaa, sizeof(write_buf
));
2389 for (i
=0; i
<256; i
++)
2391 completion_called
= 0;
2393 ret
= WriteFileEx(server
, write_buf
, sizeof(write_buf
), &overlapped
, completion_routine
);
2394 err
= GetLastError();
2396 ok(completion_called
== 0, "completion routine called during WriteFileEx\n");
2398 wait
= WaitForSingleObjectEx(event
, 0, TRUE
);
2400 if (wait
== WAIT_TIMEOUT
)
2401 /* write couldn't complete immediately, presumably the pipe is full */
2404 ok(wait
== WAIT_IO_COMPLETION
|| wait
== WAIT_OBJECT_0
, "WaitForSingleObject returned %x\n", wait
);
2406 ok(ret
== TRUE
, "WriteFileEx failed, err=%i\n", err
);
2407 ok(completion_errorcode
== 0, "completion called with error %x\n", completion_errorcode
);
2408 ok(completion_lpoverlapped
== &overlapped
, "completion called with wrong overlapped pointer\n");
2411 ok(ret
== TRUE
, "WriteFileEx failed, err=%i\n", err
);
2412 ok(completion_called
== 0, "completion routine called but wait timed out\n");
2413 ok(completion_errorcode
== 0, "completion called with error %x\n", completion_errorcode
);
2414 ok(completion_lpoverlapped
== &overlapped
, "completion called with wrong overlapped pointer\n");
2416 /* free up some space in the pipe */
2417 for (i
=0; i
<256; i
++)
2419 ret
= ReadFile(client
, read_buf
, sizeof(read_buf
), &num_bytes
, NULL
);
2420 ok(ret
== TRUE
, "ReadFile failed\n");
2422 ok(completion_called
== 0, "completion routine called during ReadFile\n");
2424 wait
= WaitForSingleObjectEx(event
, 0, TRUE
);
2425 ok(wait
== WAIT_IO_COMPLETION
|| wait
== WAIT_OBJECT_0
|| wait
== WAIT_TIMEOUT
,
2426 "WaitForSingleObject returned %x\n", wait
);
2427 if (wait
!= WAIT_TIMEOUT
) break;
2430 ok(completion_called
== 1, "completion routine not called\n");
2431 ok(completion_errorcode
== 0, "completion called with error %x\n", completion_errorcode
);
2432 ok(completion_lpoverlapped
== &overlapped
, "completion called with wrong overlapped pointer\n");
2434 num_bytes
= 0xdeadbeef;
2435 SetLastError(0xdeadbeef);
2436 ret
= ReadFile(INVALID_HANDLE_VALUE
, read_buf
, 0, &num_bytes
, NULL
);
2437 ok(!ret
, "ReadFile should fail\n");
2438 ok(GetLastError() == ERROR_INVALID_HANDLE
, "wrong error %u\n", GetLastError());
2439 ok(num_bytes
== 0, "expected 0, got %u\n", num_bytes
);
2441 S(U(overlapped
)).Offset
= 0;
2442 S(U(overlapped
)).OffsetHigh
= 0;
2443 overlapped
.Internal
= -1;
2444 overlapped
.InternalHigh
= -1;
2445 overlapped
.hEvent
= event
;
2446 num_bytes
= 0xdeadbeef;
2447 SetLastError(0xdeadbeef);
2448 ret
= ReadFile(server
, read_buf
, 0, &num_bytes
, &overlapped
);
2449 ok(!ret
, "ReadFile should fail\n");
2451 ok(GetLastError() == ERROR_IO_PENDING
, "expected ERROR_IO_PENDING, got %d\n", GetLastError());
2452 ok(num_bytes
== 0, "bytes %u\n", num_bytes
);
2453 ok((NTSTATUS
)overlapped
.Internal
== STATUS_PENDING
, "expected STATUS_PENDING, got %#lx\n", overlapped
.Internal
);
2455 ok(overlapped
.InternalHigh
== -1, "expected -1, got %lu\n", overlapped
.InternalHigh
);
2457 wait
= WaitForSingleObject(event
, 100);
2458 ok(wait
== WAIT_TIMEOUT
, "WaitForSingleObject returned %x\n", wait
);
2460 num_bytes
= 0xdeadbeef;
2461 ret
= WriteFile(client
, test_string
, 1, &num_bytes
, NULL
);
2462 ok(ret
, "WriteFile failed\n");
2463 ok(num_bytes
== 1, "bytes %u\n", num_bytes
);
2465 wait
= WaitForSingleObject(event
, 100);
2467 ok(wait
== WAIT_OBJECT_0
, "WaitForSingleObject returned %x\n", wait
);
2469 ok(num_bytes
== 1, "bytes %u\n", num_bytes
);
2471 ok((NTSTATUS
)overlapped
.Internal
== STATUS_SUCCESS
, "expected STATUS_SUCCESS, got %#lx\n", overlapped
.Internal
);
2472 ok(overlapped
.InternalHigh
== 0, "expected 0, got %lu\n", overlapped
.InternalHigh
);
2474 /* read the pending byte and clear the pipe */
2475 num_bytes
= 0xdeadbeef;
2476 ret
= ReadFile(server
, read_buf
, 1, &num_bytes
, &overlapped
);
2477 ok(ret
, "ReadFile failed\n");
2478 ok(num_bytes
== 1, "bytes %u\n", num_bytes
);
2480 CloseHandle(client
);
2481 CloseHandle(server
);
2489 hmod
= GetModuleHandleA("advapi32.dll");
2490 pDuplicateTokenEx
= (void *) GetProcAddress(hmod
, "DuplicateTokenEx");
2491 hmod
= GetModuleHandleA("kernel32.dll");
2492 pQueueUserAPC
= (void *) GetProcAddress(hmod
, "QueueUserAPC");
2494 if (test_DisconnectNamedPipe())
2496 test_CreateNamedPipe_instances_must_match();
2498 test_CreateNamedPipe(PIPE_TYPE_BYTE
);
2499 test_CreateNamedPipe(PIPE_TYPE_MESSAGE
| PIPE_READMODE_MESSAGE
);
2502 test_impersonation();
2504 test_overlapped_error();
2505 test_NamedPipeHandleState();
2506 test_GetNamedPipeInfo();
2507 test_readfileex_pending();