kernel32/tests: Add tests for sending empty message in combination with CloseHandle.
[wine.git] / dlls / kernel32 / tests / pipe.c
blob4f1b2385ce0901927b148167d839c54b24f05c81
1 /*
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
21 #include <stdarg.h>
22 #include <stdio.h>
24 #include "ntstatus.h"
25 #define WIN32_NO_STATUS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winternl.h"
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)
43 user_apc_ran = TRUE;
47 enum rpcThreadOp
49 RPC_READFILE
52 struct rpcThreadArgs
54 ULONG_PTR returnValue;
55 DWORD lastError;
56 enum rpcThreadOp op;
57 ULONG_PTR args[5];
60 static DWORD CALLBACK rpcThreadMain(LPVOID arg)
62 struct rpcThreadArgs *rpcargs = (struct rpcThreadArgs *)arg;
63 trace("rpcThreadMain starting\n");
64 SetLastError( rpcargs->lastError );
66 switch (rpcargs->op)
68 case RPC_READFILE:
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 */
74 break;
76 default:
77 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
78 rpcargs->returnValue = 0;
79 break;
82 rpcargs->lastError = GetLastError();
83 trace("rpcThreadMain returning\n");
84 return 0;
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;
91 HANDLE thread;
92 DWORD threadId, ret;
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());
107 CloseHandle(thread);
109 SetLastError(rpcargs.lastError);
110 return (BOOL)rpcargs.returnValue;
113 static void test_CreateNamedPipe(int pipemode)
115 HANDLE hnp;
116 HANDLE hFile;
117 static const char obuf[] = "Bit Bucket";
118 static const char obuf2[] = "More bits";
119 char ibuf[32], *pbuf;
120 DWORD written;
121 DWORD readden;
122 DWORD avail;
123 DWORD lpmode;
124 BOOL ret;
126 if (pipemode == PIPE_TYPE_BYTE)
127 trace("test_CreateNamedPipe starting in byte mode\n");
128 else
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) {
192 HANDLE hFile2;
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");
247 else
249 SetLastError(0xdeadbeef);
250 todo_wine
251 ok(!ReadFile(hnp, ibuf, 4, &readden, NULL), "ReadFile\n");
252 todo_wine
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");
270 else
272 SetLastError(0xdeadbeef);
273 todo_wine
274 ok(!ReadFile(hnp, ibuf, 4, &readden, NULL), "ReadFile\n");
275 todo_wine
276 ok(GetLastError() == ERROR_MORE_DATA, "wrong error\n");
277 ok(readden == 4, "read got %d bytes\n", readden);
278 SetLastError(0xdeadbeef);
279 todo_wine
280 ok(!ReadFile(hnp, ibuf + 4, 4, &readden, NULL), "ReadFile\n");
281 todo_wine
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);
301 else
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);
307 pbuf = ibuf;
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);
315 pbuf = ibuf;
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);
332 else
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);
338 pbuf = ibuf;
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);
348 else {
349 todo_wine {
350 ok(readden == sizeof(obuf), "read 4 got %d bytes\n", readden);
353 pbuf = ibuf;
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");
367 else {
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);
381 pbuf = ibuf;
382 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 5a check\n");
383 ok(ReadFile(hFile, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
384 todo_wine {
385 ok(readden == sizeof(obuf), "read 5 got %d bytes\n", readden);
387 pbuf = ibuf;
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");
395 todo_wine {
396 ok(readden == sizeof(obuf2), "peek6a got %d bytes\n", readden);
397 ok(avail == sizeof(obuf2), "peek6a got %d bytes available\n", avail);
399 if (avail > 0) {
400 ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
401 ok(readden == sizeof(obuf2), "read 6a got %d bytes\n", readden);
402 pbuf = ibuf;
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);
416 pbuf = ibuf;
417 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 6a check\n");
418 ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
419 todo_wine {
420 ok(readden == sizeof(obuf), "read 6b got %d bytes\n", readden);
422 pbuf = ibuf;
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);
432 todo_wine
433 ok(!ReadFile(hFile, ibuf, 4, &readden, NULL), "ReadFile 7\n");
434 todo_wine
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);
445 todo_wine
446 ok(!ReadFile(hnp, ibuf, 4, &readden, NULL), "ReadFile 8\n");
447 todo_wine
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);
464 todo_wine
465 ok(!ReadFile(hFile, ibuf, 4, &readden, NULL), "ReadFile 9\n");
466 todo_wine
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);
471 todo_wine
472 ok(!ret, "RpcReadFile 9\n");
473 todo_wine
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");
478 todo_wine
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");
487 todo_wine
488 ok(GetLastError() == ERROR_MORE_DATA, "wrong error 9\n");
489 ok(readden == 4, "read got %d bytes 9\n", readden);
490 SetLastError(0xdeadbeef);
491 todo_wine
492 ok(!ReadFile(hFile, ibuf + 4, 4, &readden, NULL), "ReadFile 9\n");
493 todo_wine
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);
509 todo_wine
510 ok(!ReadFile(hnp, ibuf, 4, &readden, NULL), "ReadFile 10\n");
511 todo_wine
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);
516 todo_wine
517 ok(!ret, "RpcReadFile 10\n");
518 todo_wine
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");
523 todo_wine
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");
532 todo_wine
533 ok(GetLastError() == ERROR_MORE_DATA, "wrong error 10\n");
534 ok(readden == 4, "read got %d bytes 10\n", readden);
535 SetLastError(0xdeadbeef);
536 todo_wine
537 ok(!ReadFile(hnp, ibuf + 4, 4, &readden, NULL), "ReadFile 10\n");
538 todo_wine
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)
594 HANDLE hnp, hnp2;
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,
668 /* nInBufSize */ 24,
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");
685 ExitProcess(1);
687 return 1;
690 static HANDLE hnp = INVALID_HANDLE_VALUE;
692 /** Trivial byte echo server - disconnects after each session */
693 static DWORD CALLBACK serverThreadMain1(LPVOID arg)
695 int i;
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++) {
709 char buf[512];
710 DWORD written;
711 DWORD readden;
712 BOOL success;
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");
740 return 0;
743 /** Trivial byte echo server - closes after each connection */
744 static DWORD CALLBACK serverThreadMain2(LPVOID arg)
746 int i;
747 HANDLE hnpNext = 0;
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++) {
761 char buf[512];
762 DWORD written;
763 DWORD readden;
764 DWORD ret;
765 BOOL success;
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 */
804 hnpNext =
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");
816 hnp = hnpNext;
818 return 0;
821 /** Trivial byte echo server - uses overlapped named pipe calls */
822 static DWORD CALLBACK serverThreadMain3(LPVOID arg)
824 int i;
825 HANDLE hEvent;
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 */
841 NULL); /* name */
842 ok(hEvent != NULL, "CreateEvent\n");
844 for (i = 0; i < NB_SERVER_LOOPS; i++) {
845 char buf[512];
846 DWORD written;
847 DWORD readden;
848 DWORD dummy;
849 BOOL success;
850 OVERLAPPED oOverlap;
851 int letWFSOEwait = (i & 2);
852 int letGORwait = (i & 1);
853 DWORD err;
855 memset(&oOverlap, 0, sizeof(oOverlap));
856 oOverlap.hEvent = hEvent;
858 /* Wait for client to connect */
859 if (i == 0) {
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");
865 } else {
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)) {
872 if (letWFSOEwait)
874 DWORD ret;
875 do {
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)) {
900 if (letWFSOEwait)
902 DWORD ret;
903 do {
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)) {
924 if (letWFSOEwait)
926 DWORD ret;
927 do {
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");
946 return 0;
949 /** Trivial byte echo server - uses i/o completion ports */
950 static DWORD CALLBACK serverThreadMain4(LPVOID arg)
952 int i;
953 HANDLE hcompletion;
954 BOOL ret;
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++) {
971 char buf[512];
972 DWORD written;
973 DWORD readden;
974 DWORD dummy;
975 BOOL success;
976 OVERLAPPED oConnect;
977 OVERLAPPED oRead;
978 OVERLAPPED oWrite;
979 OVERLAPPED *oResult;
980 DWORD err;
981 ULONG_PTR compkey;
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);
996 if (!success)
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());
1003 if (success)
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,
1020 &oResult, 10000);
1021 ok(success, "ReadFile GetQueuedCompletionStatus failed, errno=%i\n", GetLastError());
1022 if (success)
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,
1035 &oResult, 10000);
1036 ok(success, "WriteFile GetQueuedCompletionStatus failed, errno=%i\n", GetLastError());
1037 if (success)
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());
1056 return 0;
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)
1076 int i;
1077 HANDLE hEvent;
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 */
1093 NULL); /* name */
1094 ok(hEvent != NULL, "CreateEvent\n");
1096 for (i = 0; i < NB_SERVER_LOOPS; i++) {
1097 char buf[512];
1098 DWORD readden;
1099 BOOL success;
1100 OVERLAPPED oOverlap;
1101 DWORD err;
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;
1118 ResetEvent(hEvent);
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");
1124 if (success) {
1125 DWORD ret;
1126 do {
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;
1140 ResetEvent(hEvent);
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");
1146 if (success) {
1147 DWORD ret;
1148 do {
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");
1163 return 0;
1166 static void exercizeServer(const char *pipename, HANDLE serverThread)
1168 int i;
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";
1174 char ibuf[32];
1175 DWORD written;
1176 DWORD readden;
1177 int loop;
1179 for (loop = 0; loop < 3; loop++) {
1180 DWORD err;
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)
1186 break;
1187 err = GetLastError();
1188 if (loop == 0)
1189 ok(err == ERROR_PIPE_BUSY || err == ERROR_FILE_NOT_FOUND, "connecting to pipe\n");
1190 else
1191 ok(err == ERROR_PIPE_BUSY, "connecting to pipe\n");
1192 trace("connect failed, retrying\n");
1193 Sleep(200);
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");
1212 CloseHandle(hnp);
1213 trace("exercizeServer returning\n");
1216 static void test_NamedPipe_2(void)
1218 HANDLE serverThread;
1219 DWORD serverThreadId;
1220 HANDLE alarmThread;
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.
1235 /* Try server #1 */
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);
1241 /* Try server #2 */
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);
1247 /* Try server #3 */
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);
1253 /* Try server #4 */
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);
1259 /* Try server #5 */
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)
1272 HANDLE hnp;
1273 HANDLE hFile;
1274 static const char obuf[] = "Bit Bucket";
1275 char ibuf[32];
1276 DWORD written;
1277 DWORD readden;
1278 DWORD ret;
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");
1291 return 1;
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");
1326 return 0;
1328 static void test_CreatePipe(void)
1330 SECURITY_ATTRIBUTES pipe_attr;
1331 HANDLE piperead, pipewrite;
1332 DWORD written;
1333 DWORD read;
1334 DWORD i, size;
1335 BYTE *buffer;
1336 char readbuf[32];
1338 user_apc_ran = FALSE;
1339 if (pQueueUserAPC)
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 */
1366 size = 32768;
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 HANDLE hpipe, hfile;
1391 char buffer[32];
1392 DWORD numbytes;
1393 BOOL ret;
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 SetLastError(0xdeadbeef);
1429 ret = ReadFile(hfile, buffer, 0, &numbytes, NULL);
1430 ok(!ret, "ReadFile unexpectedly succeeded\n");
1431 ok(GetLastError() == ERROR_BROKEN_PIPE, "expected ERROR_BROKEN_PIPE, got %u\n", GetLastError());
1433 CloseHandle(hfile);
1435 hpipe = CreateNamedPipeA(PIPENAME, PIPE_ACCESS_DUPLEX,
1436 PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
1437 1, 1024, 1024, NMPWAIT_USE_DEFAULT_WAIT, NULL);
1438 ok(hpipe != INVALID_HANDLE_VALUE, "CreateNamedPipe failed with %u\n", GetLastError());
1440 hfile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
1441 ok(hfile != INVALID_HANDLE_VALUE, "CreateFile failed with %u\n", GetLastError());
1443 numbytes = 0xdeadbeef;
1444 ret = WriteFile(hpipe, testdata, 0, &numbytes, NULL);
1445 ok(ret, "WriteFile failed with %u\n", GetLastError());
1446 ok(numbytes == 0, "expected 0, got %u\n", numbytes);
1448 ret = CloseHandle(hpipe);
1449 ok(ret, "CloseHandle failed with %u\n", GetLastError());
1451 numbytes = 0xdeadbeef;
1452 memset(buffer, 0, sizeof(buffer));
1453 ret = ReadFile(hfile, buffer, sizeof(buffer), &numbytes, NULL);
1454 todo_wine ok(ret, "ReadFile failed with %u\n", GetLastError());
1455 ok(numbytes == 0, "expected 0, got %u\n", numbytes);
1457 SetLastError(0xdeadbeef);
1458 ret = ReadFile(hfile, buffer, 0, &numbytes, NULL);
1459 ok(!ret, "ReadFile unexpectedly succeeded\n");
1460 todo_wine ok(GetLastError() == ERROR_BROKEN_PIPE, "expected ERROR_BROKEN_PIPE, got %u\n", GetLastError());
1462 CloseHandle(hfile);
1464 /* repeat test with hpipe <-> hfile swapped */
1466 hpipe = CreateNamedPipeA(PIPENAME, PIPE_ACCESS_DUPLEX,
1467 PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
1468 1, 1024, 1024, NMPWAIT_USE_DEFAULT_WAIT, NULL);
1469 ok(hpipe != INVALID_HANDLE_VALUE, "CreateNamedPipe failed with %u\n", GetLastError());
1471 hfile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
1472 ok(hfile != INVALID_HANDLE_VALUE, "CreateFile failed with %u\n", GetLastError());
1474 numbytes = 0xdeadbeef;
1475 ret = WriteFile(hfile, testdata, sizeof(testdata), &numbytes, NULL);
1476 ok(ret, "WriteFile failed with %u\n", GetLastError());
1477 ok(numbytes == sizeof(testdata), "expected sizeof(testdata), got %u\n", numbytes);
1479 numbytes = 0xdeadbeef;
1480 ret = PeekNamedPipe(hpipe, NULL, 0, NULL, &numbytes, NULL);
1481 ok(ret, "PeekNamedPipe failed with %u\n", GetLastError());
1482 ok(numbytes == sizeof(testdata), "expected sizeof(testdata), got %u\n", numbytes);
1484 ret = CloseHandle(hfile);
1485 ok(ret, "CloseHandle failed with %u\n", GetLastError());
1487 numbytes = 0xdeadbeef;
1488 memset(buffer, 0, sizeof(buffer));
1489 ret = ReadFile(hpipe, buffer, 0, &numbytes, NULL);
1490 todo_wine ok(ret || broken(GetLastError() == ERROR_MORE_DATA) /* >= Win 8 */,
1491 "ReadFile failed with %u\n", GetLastError());
1492 ok(numbytes == 0, "expected 0, got %u\n", numbytes);
1494 numbytes = 0xdeadbeef;
1495 memset(buffer, 0, sizeof(buffer));
1496 ret = ReadFile(hpipe, buffer, sizeof(buffer), &numbytes, NULL);
1497 ok(ret, "ReadFile failed with %u\n", GetLastError());
1498 ok(numbytes == sizeof(testdata), "expected sizeof(testdata), got %u\n", numbytes);
1500 SetLastError(0xdeadbeef);
1501 ret = ReadFile(hpipe, buffer, 0, &numbytes, NULL);
1502 ok(!ret, "ReadFile unexpectedly succeeded\n");
1503 ok(GetLastError() == ERROR_BROKEN_PIPE, "expected ERROR_BROKEN_PIPE, got %u\n", GetLastError());
1505 CloseHandle(hpipe);
1507 hpipe = CreateNamedPipeA(PIPENAME, PIPE_ACCESS_DUPLEX,
1508 PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
1509 1, 1024, 1024, NMPWAIT_USE_DEFAULT_WAIT, NULL);
1510 ok(hpipe != INVALID_HANDLE_VALUE, "CreateNamedPipe failed with %u\n", GetLastError());
1512 hfile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
1513 ok(hfile != INVALID_HANDLE_VALUE, "CreateFile failed with %u\n", GetLastError());
1515 numbytes = 0xdeadbeef;
1516 ret = WriteFile(hfile, testdata, 0, &numbytes, NULL);
1517 ok(ret, "WriteFile failed with %u\n", GetLastError());
1518 ok(numbytes == 0, "expected 0, got %u\n", numbytes);
1520 ret = CloseHandle(hfile);
1521 ok(ret, "CloseHandle failed with %u\n", GetLastError());
1523 numbytes = 0xdeadbeef;
1524 memset(buffer, 0, sizeof(buffer));
1525 ret = ReadFile(hpipe, buffer, sizeof(buffer), &numbytes, NULL);
1526 todo_wine ok(ret, "ReadFile failed with %u\n", GetLastError());
1527 ok(numbytes == 0, "expected 0, got %u\n", numbytes);
1529 SetLastError(0xdeadbeef);
1530 ret = ReadFile(hpipe, buffer, 0, &numbytes, NULL);
1531 ok(!ret, "ReadFile unexpectedly succeeded\n");
1532 ok(GetLastError() == ERROR_BROKEN_PIPE, "expected ERROR_BROKEN_PIPE, got %u\n", GetLastError());
1534 CloseHandle(hpipe);
1537 struct named_pipe_client_params
1539 DWORD security_flags;
1540 HANDLE token;
1541 BOOL revert;
1544 #define PIPE_NAME "\\\\.\\pipe\\named_pipe_test"
1546 static DWORD CALLBACK named_pipe_client_func(LPVOID p)
1548 struct named_pipe_client_params *params = p;
1549 HANDLE pipe;
1550 BOOL ret;
1551 const char message[] = "Test";
1552 DWORD bytes_read, bytes_written;
1553 char dummy;
1554 TOKEN_PRIVILEGES *Privileges = NULL;
1556 if (params->token)
1558 if (params->revert)
1560 /* modify the token so we can tell if the pipe impersonation
1561 * token reverts to the process token */
1562 ret = AdjustTokenPrivileges(params->token, TRUE, NULL, 0, NULL, NULL);
1563 ok(ret, "AdjustTokenPrivileges failed with error %d\n", GetLastError());
1565 ret = SetThreadToken(NULL, params->token);
1566 ok(ret, "SetThreadToken failed with error %d\n", GetLastError());
1568 else
1570 DWORD Size = 0;
1571 HANDLE process_token;
1573 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY|TOKEN_ADJUST_PRIVILEGES, &process_token);
1574 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
1576 ret = GetTokenInformation(process_token, TokenPrivileges, NULL, 0, &Size);
1577 ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "GetTokenInformation(TokenPrivileges) failed with %d\n", GetLastError());
1578 Privileges = HeapAlloc(GetProcessHeap(), 0, Size);
1579 ret = GetTokenInformation(process_token, TokenPrivileges, Privileges, Size, &Size);
1580 ok(ret, "GetTokenInformation(TokenPrivileges) failed with %d\n", GetLastError());
1582 ret = AdjustTokenPrivileges(process_token, TRUE, NULL, 0, NULL, NULL);
1583 ok(ret, "AdjustTokenPrivileges failed with error %d\n", GetLastError());
1585 CloseHandle(process_token);
1588 pipe = CreateFileA(PIPE_NAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, params->security_flags, NULL);
1589 ok(pipe != INVALID_HANDLE_VALUE, "CreateFile for pipe failed with error %d\n", GetLastError());
1591 ret = WriteFile(pipe, message, sizeof(message), &bytes_written, NULL);
1592 ok(ret, "WriteFile failed with error %d\n", GetLastError());
1594 ret = ReadFile(pipe, &dummy, sizeof(dummy), &bytes_read, NULL);
1595 ok(ret, "ReadFile failed with error %d\n", GetLastError());
1597 if (params->token)
1599 if (params->revert)
1601 ret = RevertToSelf();
1602 ok(ret, "RevertToSelf failed with error %d\n", GetLastError());
1604 else
1606 ret = AdjustTokenPrivileges(params->token, TRUE, NULL, 0, NULL, NULL);
1607 ok(ret, "AdjustTokenPrivileges failed with error %d\n", GetLastError());
1610 else
1612 HANDLE process_token;
1614 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &process_token);
1615 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
1617 ret = AdjustTokenPrivileges(process_token, FALSE, Privileges, 0, NULL, NULL);
1618 ok(ret, "AdjustTokenPrivileges failed with error %d\n", GetLastError());
1620 HeapFree(GetProcessHeap(), 0, Privileges);
1622 CloseHandle(process_token);
1625 ret = WriteFile(pipe, message, sizeof(message), &bytes_written, NULL);
1626 ok(ret, "WriteFile failed with error %d\n", GetLastError());
1628 ret = ReadFile(pipe, &dummy, sizeof(dummy), &bytes_read, NULL);
1629 ok(ret, "ReadFile failed with error %d\n", GetLastError());
1631 CloseHandle(pipe);
1633 return 0;
1636 static HANDLE make_impersonation_token(DWORD Access, SECURITY_IMPERSONATION_LEVEL ImpersonationLevel)
1638 HANDLE ProcessToken;
1639 HANDLE Token = NULL;
1640 BOOL ret;
1642 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE, &ProcessToken);
1643 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
1645 ret = pDuplicateTokenEx(ProcessToken, Access, NULL, ImpersonationLevel, TokenImpersonation, &Token);
1646 ok(ret, "DuplicateToken failed with error %d\n", GetLastError());
1648 CloseHandle(ProcessToken);
1650 return Token;
1653 static void test_ImpersonateNamedPipeClient(HANDLE hClientToken, DWORD security_flags, BOOL revert, void (*test_func)(int, HANDLE))
1655 HANDLE hPipeServer;
1656 BOOL ret;
1657 DWORD dwTid;
1658 HANDLE hThread;
1659 char buffer[256];
1660 DWORD dwBytesRead;
1661 DWORD error;
1662 struct named_pipe_client_params params;
1663 char dummy = 0;
1664 DWORD dwBytesWritten;
1665 HANDLE hToken = NULL;
1666 SECURITY_IMPERSONATION_LEVEL ImpersonationLevel;
1667 DWORD size;
1669 hPipeServer = CreateNamedPipeA(PIPE_NAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT, 1, 100, 100, NMPWAIT_USE_DEFAULT_WAIT, NULL);
1670 ok(hPipeServer != INVALID_HANDLE_VALUE, "CreateNamedPipe failed with error %d\n", GetLastError());
1672 params.security_flags = security_flags;
1673 params.token = hClientToken;
1674 params.revert = revert;
1675 hThread = CreateThread(NULL, 0, named_pipe_client_func, &params, 0, &dwTid);
1676 ok(hThread != NULL, "CreateThread failed with error %d\n", GetLastError());
1678 SetLastError(0xdeadbeef);
1679 ret = ImpersonateNamedPipeClient(hPipeServer);
1680 error = GetLastError();
1681 ok(ret /* win2k3 */ || (error == ERROR_CANNOT_IMPERSONATE),
1682 "ImpersonateNamedPipeClient should have failed with ERROR_CANNOT_IMPERSONATE instead of %d\n", GetLastError());
1684 ret = ConnectNamedPipe(hPipeServer, NULL);
1685 ok(ret || (GetLastError() == ERROR_PIPE_CONNECTED), "ConnectNamedPipe failed with error %d\n", GetLastError());
1687 ret = ReadFile(hPipeServer, buffer, sizeof(buffer), &dwBytesRead, NULL);
1688 ok(ret, "ReadFile failed with error %d\n", GetLastError());
1690 ret = ImpersonateNamedPipeClient(hPipeServer);
1691 ok(ret, "ImpersonateNamedPipeClient failed with error %d\n", GetLastError());
1693 ret = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, FALSE, &hToken);
1694 ok(ret, "OpenThreadToken failed with error %d\n", GetLastError());
1696 (*test_func)(0, hToken);
1698 ImpersonationLevel = 0xdeadbeef; /* to avoid false positives */
1699 ret = GetTokenInformation(hToken, TokenImpersonationLevel, &ImpersonationLevel, sizeof(ImpersonationLevel), &size);
1700 ok(ret, "GetTokenInformation(TokenImpersonationLevel) failed with error %d\n", GetLastError());
1701 ok(ImpersonationLevel == SecurityImpersonation, "ImpersonationLevel should have been SecurityImpersonation(%d) instead of %d\n", SecurityImpersonation, ImpersonationLevel);
1703 CloseHandle(hToken);
1705 RevertToSelf();
1707 ret = WriteFile(hPipeServer, &dummy, sizeof(dummy), &dwBytesWritten, NULL);
1708 ok(ret, "WriteFile failed with error %d\n", GetLastError());
1710 ret = ReadFile(hPipeServer, buffer, sizeof(buffer), &dwBytesRead, NULL);
1711 ok(ret, "ReadFile failed with error %d\n", GetLastError());
1713 ret = ImpersonateNamedPipeClient(hPipeServer);
1714 ok(ret, "ImpersonateNamedPipeClient failed with error %d\n", GetLastError());
1716 ret = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, FALSE, &hToken);
1717 ok(ret, "OpenThreadToken failed with error %d\n", GetLastError());
1719 (*test_func)(1, hToken);
1721 CloseHandle(hToken);
1723 RevertToSelf();
1725 ret = WriteFile(hPipeServer, &dummy, sizeof(dummy), &dwBytesWritten, NULL);
1726 ok(ret, "WriteFile failed with error %d\n", GetLastError());
1728 WaitForSingleObject(hThread, INFINITE);
1730 ret = ImpersonateNamedPipeClient(hPipeServer);
1731 ok(ret, "ImpersonateNamedPipeClient failed with error %d\n", GetLastError());
1733 RevertToSelf();
1735 CloseHandle(hThread);
1736 CloseHandle(hPipeServer);
1739 static BOOL are_all_privileges_disabled(HANDLE hToken)
1741 BOOL ret;
1742 TOKEN_PRIVILEGES *Privileges = NULL;
1743 DWORD Size = 0;
1744 BOOL all_privs_disabled = TRUE;
1745 DWORD i;
1747 ret = GetTokenInformation(hToken, TokenPrivileges, NULL, 0, &Size);
1748 if (!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1750 Privileges = HeapAlloc(GetProcessHeap(), 0, Size);
1751 ret = GetTokenInformation(hToken, TokenPrivileges, Privileges, Size, &Size);
1752 if (!ret)
1754 HeapFree(GetProcessHeap(), 0, Privileges);
1755 return FALSE;
1758 else
1759 return FALSE;
1761 for (i = 0; i < Privileges->PrivilegeCount; i++)
1763 if (Privileges->Privileges[i].Attributes & SE_PRIVILEGE_ENABLED)
1765 all_privs_disabled = FALSE;
1766 break;
1770 HeapFree(GetProcessHeap(), 0, Privileges);
1772 return all_privs_disabled;
1775 static DWORD get_privilege_count(HANDLE hToken)
1777 TOKEN_STATISTICS Statistics;
1778 DWORD Size = sizeof(Statistics);
1779 BOOL ret;
1781 ret = GetTokenInformation(hToken, TokenStatistics, &Statistics, Size, &Size);
1782 ok(ret, "GetTokenInformation(TokenStatistics)\n");
1783 if (!ret) return -1;
1785 return Statistics.PrivilegeCount;
1788 static void test_no_sqos_no_token(int call_index, HANDLE hToken)
1790 DWORD priv_count;
1792 switch (call_index)
1794 case 0:
1795 priv_count = get_privilege_count(hToken);
1796 todo_wine
1797 ok(priv_count == 0, "privilege count should have been 0 instead of %d\n", priv_count);
1798 break;
1799 case 1:
1800 priv_count = get_privilege_count(hToken);
1801 ok(priv_count > 0, "privilege count should now be > 0 instead of 0\n");
1802 ok(!are_all_privileges_disabled(hToken), "impersonated token should not have been modified\n");
1803 break;
1804 default:
1805 ok(0, "shouldn't happen\n");
1809 static void test_no_sqos(int call_index, HANDLE hToken)
1811 switch (call_index)
1813 case 0:
1814 ok(!are_all_privileges_disabled(hToken), "token should be a copy of the process one\n");
1815 break;
1816 case 1:
1817 todo_wine
1818 ok(are_all_privileges_disabled(hToken), "impersonated token should have been modified\n");
1819 break;
1820 default:
1821 ok(0, "shouldn't happen\n");
1825 static void test_static_context(int call_index, HANDLE hToken)
1827 switch (call_index)
1829 case 0:
1830 ok(!are_all_privileges_disabled(hToken), "token should be a copy of the process one\n");
1831 break;
1832 case 1:
1833 ok(!are_all_privileges_disabled(hToken), "impersonated token should not have been modified\n");
1834 break;
1835 default:
1836 ok(0, "shouldn't happen\n");
1840 static void test_dynamic_context(int call_index, HANDLE hToken)
1842 switch (call_index)
1844 case 0:
1845 ok(!are_all_privileges_disabled(hToken), "token should be a copy of the process one\n");
1846 break;
1847 case 1:
1848 todo_wine
1849 ok(are_all_privileges_disabled(hToken), "impersonated token should have been modified\n");
1850 break;
1851 default:
1852 ok(0, "shouldn't happen\n");
1856 static void test_dynamic_context_no_token(int call_index, HANDLE hToken)
1858 switch (call_index)
1860 case 0:
1861 ok(are_all_privileges_disabled(hToken), "token should be a copy of the process one\n");
1862 break;
1863 case 1:
1864 ok(!are_all_privileges_disabled(hToken), "process token modification should have been detected and impersonation token updated\n");
1865 break;
1866 default:
1867 ok(0, "shouldn't happen\n");
1871 static void test_no_sqos_revert(int call_index, HANDLE hToken)
1873 DWORD priv_count;
1874 switch (call_index)
1876 case 0:
1877 priv_count = get_privilege_count(hToken);
1878 todo_wine
1879 ok(priv_count == 0, "privilege count should have been 0 instead of %d\n", priv_count);
1880 break;
1881 case 1:
1882 priv_count = get_privilege_count(hToken);
1883 ok(priv_count > 0, "privilege count should now be > 0 instead of 0\n");
1884 ok(!are_all_privileges_disabled(hToken), "impersonated token should not have been modified\n");
1885 break;
1886 default:
1887 ok(0, "shouldn't happen\n");
1891 static void test_static_context_revert(int call_index, HANDLE hToken)
1893 switch (call_index)
1895 case 0:
1896 todo_wine
1897 ok(are_all_privileges_disabled(hToken), "privileges should have been disabled\n");
1898 break;
1899 case 1:
1900 todo_wine
1901 ok(are_all_privileges_disabled(hToken), "impersonated token should not have been modified\n");
1902 break;
1903 default:
1904 ok(0, "shouldn't happen\n");
1908 static void test_dynamic_context_revert(int call_index, HANDLE hToken)
1910 switch (call_index)
1912 case 0:
1913 todo_wine
1914 ok(are_all_privileges_disabled(hToken), "privileges should have been disabled\n");
1915 break;
1916 case 1:
1917 ok(!are_all_privileges_disabled(hToken), "impersonated token should now be process token\n");
1918 break;
1919 default:
1920 ok(0, "shouldn't happen\n");
1924 static void test_impersonation(void)
1926 HANDLE hClientToken;
1927 HANDLE hProcessToken;
1928 BOOL ret;
1930 if( !pDuplicateTokenEx ) {
1931 skip("DuplicateTokenEx not found\n");
1932 return;
1935 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hProcessToken);
1936 if (!ret)
1938 skip("couldn't open process token, skipping impersonation tests\n");
1939 return;
1942 if (!get_privilege_count(hProcessToken) || are_all_privileges_disabled(hProcessToken))
1944 skip("token didn't have any privileges or they were all disabled. token not suitable for impersonation tests\n");
1945 CloseHandle(hProcessToken);
1946 return;
1948 CloseHandle(hProcessToken);
1950 test_ImpersonateNamedPipeClient(NULL, 0, FALSE, test_no_sqos_no_token);
1951 hClientToken = make_impersonation_token(TOKEN_IMPERSONATE | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, SecurityImpersonation);
1952 test_ImpersonateNamedPipeClient(hClientToken, 0, FALSE, test_no_sqos);
1953 CloseHandle(hClientToken);
1954 hClientToken = make_impersonation_token(TOKEN_IMPERSONATE | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, SecurityImpersonation);
1955 test_ImpersonateNamedPipeClient(hClientToken,
1956 SECURITY_SQOS_PRESENT | SECURITY_IMPERSONATION, FALSE,
1957 test_static_context);
1958 CloseHandle(hClientToken);
1959 hClientToken = make_impersonation_token(TOKEN_IMPERSONATE | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, SecurityImpersonation);
1960 test_ImpersonateNamedPipeClient(hClientToken,
1961 SECURITY_SQOS_PRESENT | SECURITY_CONTEXT_TRACKING | SECURITY_IMPERSONATION,
1962 FALSE, test_dynamic_context);
1963 CloseHandle(hClientToken);
1964 test_ImpersonateNamedPipeClient(NULL,
1965 SECURITY_SQOS_PRESENT | SECURITY_CONTEXT_TRACKING | SECURITY_IMPERSONATION,
1966 FALSE, test_dynamic_context_no_token);
1968 hClientToken = make_impersonation_token(TOKEN_IMPERSONATE | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, SecurityImpersonation);
1969 test_ImpersonateNamedPipeClient(hClientToken, 0, TRUE, test_no_sqos_revert);
1970 CloseHandle(hClientToken);
1971 hClientToken = make_impersonation_token(TOKEN_IMPERSONATE | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, SecurityImpersonation);
1972 test_ImpersonateNamedPipeClient(hClientToken,
1973 SECURITY_SQOS_PRESENT | SECURITY_IMPERSONATION, TRUE,
1974 test_static_context_revert);
1975 CloseHandle(hClientToken);
1976 hClientToken = make_impersonation_token(TOKEN_IMPERSONATE | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, SecurityImpersonation);
1977 test_ImpersonateNamedPipeClient(hClientToken,
1978 SECURITY_SQOS_PRESENT | SECURITY_CONTEXT_TRACKING | SECURITY_IMPERSONATION,
1979 TRUE, test_dynamic_context_revert);
1980 CloseHandle(hClientToken);
1983 struct overlapped_server_args
1985 HANDLE pipe_created;
1988 static DWORD CALLBACK overlapped_server(LPVOID arg)
1990 OVERLAPPED ol;
1991 HANDLE pipe;
1992 int ret, err;
1993 struct overlapped_server_args *a = arg;
1994 DWORD num;
1995 char buf[100];
1997 pipe = CreateNamedPipeA("\\\\.\\pipe\\my pipe", FILE_FLAG_OVERLAPPED | PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE, 1, 0, 0, 100000, NULL);
1998 ok(pipe != NULL, "pipe NULL\n");
2000 ol.hEvent = CreateEventA(0, 1, 0, 0);
2001 ok(ol.hEvent != NULL, "event NULL\n");
2002 ret = ConnectNamedPipe(pipe, &ol);
2003 err = GetLastError();
2004 ok(ret == 0, "ret %d\n", ret);
2005 ok(err == ERROR_IO_PENDING, "gle %d\n", err);
2006 SetEvent(a->pipe_created);
2008 ret = WaitForSingleObjectEx(ol.hEvent, INFINITE, 1);
2009 ok(ret == WAIT_OBJECT_0, "ret %x\n", ret);
2011 ret = GetOverlappedResult(pipe, &ol, &num, 1);
2012 ok(ret == 1, "ret %d\n", ret);
2014 /* This should block */
2015 ret = ReadFile(pipe, buf, sizeof(buf), &num, NULL);
2016 ok(ret == 1, "ret %d\n", ret);
2018 DisconnectNamedPipe(pipe);
2019 CloseHandle(ol.hEvent);
2020 CloseHandle(pipe);
2021 return 1;
2024 static void test_overlapped(void)
2026 DWORD tid, num;
2027 HANDLE thread, pipe;
2028 BOOL ret;
2029 struct overlapped_server_args args;
2031 args.pipe_created = CreateEventA(0, 1, 0, 0);
2032 thread = CreateThread(NULL, 0, overlapped_server, &args, 0, &tid);
2034 WaitForSingleObject(args.pipe_created, INFINITE);
2035 pipe = CreateFileA("\\\\.\\pipe\\my pipe", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
2036 ok(pipe != INVALID_HANDLE_VALUE, "cf failed\n");
2038 /* Sleep to try to get the ReadFile in the server to occur before the following WriteFile */
2039 Sleep(1);
2041 ret = WriteFile(pipe, "x", 1, &num, NULL);
2042 ok(ret, "WriteFile failed with error %d\n", GetLastError());
2044 WaitForSingleObject(thread, INFINITE);
2045 CloseHandle(pipe);
2046 CloseHandle(args.pipe_created);
2047 CloseHandle(thread);
2050 static void test_NamedPipeHandleState(void)
2052 HANDLE server, client;
2053 BOOL ret;
2054 DWORD state, instances, maxCollectionCount, collectDataTimeout;
2055 char userName[MAX_PATH];
2057 server = CreateNamedPipeA(PIPENAME, PIPE_ACCESS_DUPLEX,
2058 /* dwOpenMode */ PIPE_TYPE_BYTE | PIPE_WAIT,
2059 /* nMaxInstances */ 1,
2060 /* nOutBufSize */ 1024,
2061 /* nInBufSize */ 1024,
2062 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
2063 /* lpSecurityAttrib */ NULL);
2064 ok(server != INVALID_HANDLE_VALUE, "cf failed\n");
2065 ret = GetNamedPipeHandleStateA(server, NULL, NULL, NULL, NULL, NULL, 0);
2066 ok(ret, "GetNamedPipeHandleState failed: %d\n", GetLastError());
2067 ret = GetNamedPipeHandleStateA(server, &state, &instances, NULL, NULL, NULL,
2069 ok(ret, "GetNamedPipeHandleState failed: %d\n", GetLastError());
2070 if (ret)
2072 ok(state == 0, "unexpected state %08x\n", state);
2073 ok(instances == 1, "expected 1 instances, got %d\n", instances);
2075 /* Some parameters have no meaning, and therefore can't be retrieved,
2076 * on a local pipe.
2078 SetLastError(0xdeadbeef);
2079 ret = GetNamedPipeHandleStateA(server, &state, &instances,
2080 &maxCollectionCount, &collectDataTimeout, userName,
2081 sizeof(userName) / sizeof(userName[0]));
2082 todo_wine
2083 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
2084 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
2085 /* A byte-mode pipe server can't be changed to message mode. */
2086 state = PIPE_READMODE_MESSAGE;
2087 SetLastError(0xdeadbeef);
2088 ret = SetNamedPipeHandleState(server, &state, NULL, NULL);
2089 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
2090 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
2092 client = CreateFileA(PIPENAME, GENERIC_READ|GENERIC_WRITE, 0, NULL,
2093 OPEN_EXISTING, 0, NULL);
2094 ok(client != INVALID_HANDLE_VALUE, "cf failed\n");
2096 state = PIPE_READMODE_BYTE;
2097 ret = SetNamedPipeHandleState(client, &state, NULL, NULL);
2098 ok(ret, "SetNamedPipeHandleState failed: %d\n", GetLastError());
2099 /* A byte-mode pipe client can't be changed to message mode, either. */
2100 state = PIPE_READMODE_MESSAGE;
2101 SetLastError(0xdeadbeef);
2102 ret = SetNamedPipeHandleState(server, &state, NULL, NULL);
2103 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
2104 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
2106 CloseHandle(client);
2107 CloseHandle(server);
2109 server = CreateNamedPipeA(PIPENAME, PIPE_ACCESS_DUPLEX,
2110 /* dwOpenMode */ PIPE_TYPE_MESSAGE | PIPE_WAIT,
2111 /* nMaxInstances */ 1,
2112 /* nOutBufSize */ 1024,
2113 /* nInBufSize */ 1024,
2114 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
2115 /* lpSecurityAttrib */ NULL);
2116 ok(server != INVALID_HANDLE_VALUE, "cf failed\n");
2117 ret = GetNamedPipeHandleStateA(server, NULL, NULL, NULL, NULL, NULL, 0);
2118 ok(ret, "GetNamedPipeHandleState failed: %d\n", GetLastError());
2119 ret = GetNamedPipeHandleStateA(server, &state, &instances, NULL, NULL, NULL,
2121 ok(ret, "GetNamedPipeHandleState failed: %d\n", GetLastError());
2122 if (ret)
2124 ok(state == 0, "unexpected state %08x\n", state);
2125 ok(instances == 1, "expected 1 instances, got %d\n", instances);
2127 /* In contrast to byte-mode pipes, a message-mode pipe server can be
2128 * changed to byte mode.
2130 state = PIPE_READMODE_BYTE;
2131 ret = SetNamedPipeHandleState(server, &state, NULL, NULL);
2132 ok(ret, "SetNamedPipeHandleState failed: %d\n", GetLastError());
2134 client = CreateFileA(PIPENAME, GENERIC_READ|GENERIC_WRITE, 0, NULL,
2135 OPEN_EXISTING, 0, NULL);
2136 ok(client != INVALID_HANDLE_VALUE, "cf failed\n");
2138 state = PIPE_READMODE_MESSAGE;
2139 ret = SetNamedPipeHandleState(client, &state, NULL, NULL);
2140 ok(ret, "SetNamedPipeHandleState failed: %d\n", GetLastError());
2141 /* A message-mode pipe client can also be changed to byte mode.
2143 state = PIPE_READMODE_BYTE;
2144 ret = SetNamedPipeHandleState(client, &state, NULL, NULL);
2145 ok(ret, "SetNamedPipeHandleState failed: %d\n", GetLastError());
2147 CloseHandle(client);
2148 CloseHandle(server);
2151 static void test_readfileex_pending(void)
2153 HANDLE server, client, event;
2154 BOOL ret;
2155 DWORD err, wait, num_bytes;
2156 OVERLAPPED overlapped;
2157 char read_buf[1024];
2158 char write_buf[1024];
2159 const char test_string[] = "test";
2160 int i;
2162 server = CreateNamedPipeA(PIPENAME, FILE_FLAG_OVERLAPPED | PIPE_ACCESS_DUPLEX,
2163 /* dwOpenMode */ PIPE_TYPE_BYTE | PIPE_WAIT,
2164 /* nMaxInstances */ 1,
2165 /* nOutBufSize */ 1024,
2166 /* nInBufSize */ 1024,
2167 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
2168 /* lpSecurityAttrib */ NULL);
2169 ok(server != INVALID_HANDLE_VALUE, "cf failed\n");
2171 event = CreateEventA(NULL, TRUE, FALSE, NULL);
2172 ok(event != NULL, "CreateEventA failed\n");
2174 memset(&overlapped, 0, sizeof(overlapped));
2175 overlapped.hEvent = event;
2177 ret = ConnectNamedPipe(server, &overlapped);
2178 err = GetLastError();
2179 ok(ret == FALSE, "ConnectNamedPipe succeeded\n");
2180 ok(err == ERROR_IO_PENDING, "ConnectNamedPipe set error %i\n", err);
2182 wait = WaitForSingleObject(event, 0);
2183 ok(wait == WAIT_TIMEOUT, "WaitForSingleObject returned %x\n", wait);
2185 client = CreateFileA(PIPENAME, GENERIC_READ|GENERIC_WRITE, 0, NULL,
2186 OPEN_EXISTING, 0, NULL);
2187 ok(client != INVALID_HANDLE_VALUE, "cf failed\n");
2189 wait = WaitForSingleObject(event, 0);
2190 ok(wait == WAIT_OBJECT_0, "WaitForSingleObject returned %x\n", wait);
2192 /* Start a read that can't complete immediately. */
2193 completion_called = 0;
2194 ResetEvent(event);
2195 ret = ReadFileEx(server, read_buf, sizeof(read_buf), &overlapped, completion_routine);
2196 ok(ret == TRUE, "ReadFileEx failed, err=%i\n", GetLastError());
2197 ok(completion_called == 0, "completion routine called before ReadFileEx returned\n");
2199 ret = WriteFile(client, test_string, strlen(test_string), &num_bytes, NULL);
2200 ok(ret == TRUE, "WriteFile failed\n");
2201 ok(num_bytes == strlen(test_string), "only %i bytes written\n", num_bytes);
2203 ok(completion_called == 0, "completion routine called during WriteFile\n");
2205 wait = WaitForSingleObjectEx(event, 0, TRUE);
2206 ok(wait == WAIT_IO_COMPLETION || wait == WAIT_OBJECT_0, "WaitForSingleObjectEx returned %x\n", wait);
2208 ok(completion_called == 1, "completion not called after writing pipe\n");
2209 ok(completion_errorcode == 0, "completion called with error %x\n", completion_errorcode);
2210 ok(completion_num_bytes == strlen(test_string), "ReadFileEx returned only %d bytes\n", completion_num_bytes);
2211 ok(completion_lpoverlapped == &overlapped, "completion called with wrong overlapped pointer\n");
2212 ok(!memcmp(test_string, read_buf, strlen(test_string)), "ReadFileEx read wrong bytes\n");
2214 /* Make writes until the pipe is full and the write fails */
2215 memset(write_buf, 0xaa, sizeof(write_buf));
2216 for (i=0; i<256; i++)
2218 completion_called = 0;
2219 ResetEvent(event);
2220 ret = WriteFileEx(server, write_buf, sizeof(write_buf), &overlapped, completion_routine);
2221 err = GetLastError();
2223 ok(completion_called == 0, "completion routine called during WriteFileEx\n");
2225 wait = WaitForSingleObjectEx(event, 0, TRUE);
2227 if (wait == WAIT_TIMEOUT)
2228 /* write couldn't complete immediately, presumably the pipe is full */
2229 break;
2231 ok(wait == WAIT_IO_COMPLETION || wait == WAIT_OBJECT_0, "WaitForSingleObject returned %x\n", wait);
2233 ok(ret == TRUE, "WriteFileEx failed, err=%i\n", err);
2234 ok(completion_errorcode == 0, "completion called with error %x\n", completion_errorcode);
2235 ok(completion_lpoverlapped == &overlapped, "completion called with wrong overlapped pointer\n");
2238 ok(ret == TRUE, "WriteFileEx failed, err=%i\n", err);
2239 ok(completion_called == 0, "completion routine called but wait timed out\n");
2240 ok(completion_errorcode == 0, "completion called with error %x\n", completion_errorcode);
2241 ok(completion_lpoverlapped == &overlapped, "completion called with wrong overlapped pointer\n");
2243 /* free up some space in the pipe */
2244 for (i=0; i<256; i++)
2246 ret = ReadFile(client, read_buf, sizeof(read_buf), &num_bytes, NULL);
2247 ok(ret == TRUE, "ReadFile failed\n");
2249 ok(completion_called == 0, "completion routine called during ReadFile\n");
2251 wait = WaitForSingleObjectEx(event, 0, TRUE);
2252 ok(wait == WAIT_IO_COMPLETION || wait == WAIT_OBJECT_0 || wait == WAIT_TIMEOUT,
2253 "WaitForSingleObject returned %x\n", wait);
2254 if (wait != WAIT_TIMEOUT) break;
2257 ok(completion_called == 1, "completion routine not called\n");
2258 ok(completion_errorcode == 0, "completion called with error %x\n", completion_errorcode);
2259 ok(completion_lpoverlapped == &overlapped, "completion called with wrong overlapped pointer\n");
2261 num_bytes = 0xdeadbeef;
2262 SetLastError(0xdeadbeef);
2263 ret = ReadFile(INVALID_HANDLE_VALUE, read_buf, 0, &num_bytes, NULL);
2264 ok(!ret, "ReadFile should fail\n");
2265 ok(GetLastError() == ERROR_INVALID_HANDLE, "wrong error %u\n", GetLastError());
2266 ok(num_bytes == 0, "expected 0, got %u\n", num_bytes);
2268 S(U(overlapped)).Offset = 0;
2269 S(U(overlapped)).OffsetHigh = 0;
2270 overlapped.Internal = -1;
2271 overlapped.InternalHigh = -1;
2272 overlapped.hEvent = event;
2273 num_bytes = 0xdeadbeef;
2274 SetLastError(0xdeadbeef);
2275 ret = ReadFile(server, read_buf, 0, &num_bytes, &overlapped);
2276 ok(!ret, "ReadFile should fail\n");
2277 todo_wine
2278 ok(GetLastError() == ERROR_IO_PENDING, "expected ERROR_IO_PENDING, got %d\n", GetLastError());
2279 ok(num_bytes == 0, "bytes %u\n", num_bytes);
2280 ok((NTSTATUS)overlapped.Internal == STATUS_PENDING, "expected STATUS_PENDING, got %#lx\n", overlapped.Internal);
2281 todo_wine
2282 ok(overlapped.InternalHigh == -1, "expected -1, got %lu\n", overlapped.InternalHigh);
2284 wait = WaitForSingleObject(event, 100);
2285 ok(wait == WAIT_TIMEOUT, "WaitForSingleObject returned %x\n", wait);
2287 num_bytes = 0xdeadbeef;
2288 ret = WriteFile(client, test_string, 1, &num_bytes, NULL);
2289 ok(ret, "WriteFile failed\n");
2290 ok(num_bytes == 1, "bytes %u\n", num_bytes);
2292 wait = WaitForSingleObject(event, 100);
2293 todo_wine
2294 ok(wait == WAIT_OBJECT_0, "WaitForSingleObject returned %x\n", wait);
2296 ok(num_bytes == 1, "bytes %u\n", num_bytes);
2297 todo_wine
2298 ok((NTSTATUS)overlapped.Internal == STATUS_SUCCESS, "expected STATUS_SUCCESS, got %#lx\n", overlapped.Internal);
2299 ok(overlapped.InternalHigh == 0, "expected 0, got %lu\n", overlapped.InternalHigh);
2301 /* read the pending byte and clear the pipe */
2302 num_bytes = 0xdeadbeef;
2303 ret = ReadFile(server, read_buf, 1, &num_bytes, &overlapped);
2304 ok(ret, "ReadFile failed\n");
2305 ok(num_bytes == 1, "bytes %u\n", num_bytes);
2307 CloseHandle(client);
2308 CloseHandle(server);
2309 CloseHandle(event);
2312 START_TEST(pipe)
2314 HMODULE hmod;
2316 hmod = GetModuleHandleA("advapi32.dll");
2317 pDuplicateTokenEx = (void *) GetProcAddress(hmod, "DuplicateTokenEx");
2318 hmod = GetModuleHandleA("kernel32.dll");
2319 pQueueUserAPC = (void *) GetProcAddress(hmod, "QueueUserAPC");
2321 if (test_DisconnectNamedPipe())
2322 return;
2323 test_CreateNamedPipe_instances_must_match();
2324 test_NamedPipe_2();
2325 test_CreateNamedPipe(PIPE_TYPE_BYTE);
2326 test_CreateNamedPipe(PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE);
2327 test_CreatePipe();
2328 test_CloseHandle();
2329 test_impersonation();
2330 test_overlapped();
2331 test_NamedPipeHandleState();
2332 test_readfileex_pending();