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
31 #include "wine/test.h"
33 #define PIPENAME "\\\\.\\PiPe\\tests_pipe.c"
35 #define NB_SERVER_LOOPS 8
37 static HANDLE alarm_event;
38 static BOOL (WINAPI *pDuplicateTokenEx)(HANDLE,DWORD,LPSECURITY_ATTRIBUTES,
39 SECURITY_IMPERSONATION_LEVEL,TOKEN_TYPE,PHANDLE);
42 static void test_CreateNamedPipe(int pipemode)
46 static const char obuf[] = "Bit Bucket";
47 static const char obuf2[] = "More bits";
55 if (pipemode == PIPE_TYPE_BYTE)
56 trace("test_CreateNamedPipe starting in byte mode\n");
58 trace("test_CreateNamedPipe starting in message mode\n");
59 /* Bad parameter checks */
60 hnp = CreateNamedPipe("not a named pipe", PIPE_ACCESS_DUPLEX, pipemode | PIPE_WAIT,
61 /* nMaxInstances */ 1,
62 /* nOutBufSize */ 1024,
63 /* nInBufSize */ 1024,
64 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
65 /* lpSecurityAttrib */ NULL);
66 ok(hnp == INVALID_HANDLE_VALUE && GetLastError() == ERROR_INVALID_NAME,
67 "CreateNamedPipe should fail if name doesn't start with \\\\.\\pipe\n");
69 hnp = CreateNamedPipe(NULL,
70 PIPE_ACCESS_DUPLEX, pipemode | PIPE_WAIT,
71 1, 1024, 1024, NMPWAIT_USE_DEFAULT_WAIT, NULL);
72 ok(hnp == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
73 "CreateNamedPipe should fail if name is NULL\n");
75 hFile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
76 ok(hFile == INVALID_HANDLE_VALUE
77 && GetLastError() == ERROR_FILE_NOT_FOUND,
78 "connecting to nonexistent named pipe should fail with ERROR_FILE_NOT_FOUND\n");
80 /* Functional checks */
82 hnp = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, pipemode | PIPE_WAIT,
83 /* nMaxInstances */ 1,
84 /* nOutBufSize */ 1024,
85 /* nInBufSize */ 1024,
86 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
87 /* lpSecurityAttrib */ NULL);
88 ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
90 ret = WaitNamedPipeA(PIPENAME, 2000);
91 ok(ret, "WaitNamedPipe failed (%d)\n", GetLastError());
93 hFile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
94 ok(hFile != INVALID_HANDLE_VALUE, "CreateFile failed (%d)\n", GetLastError());
96 ok(!WaitNamedPipeA(PIPENAME, 1000), "WaitNamedPipe succeeded\n");
98 ok(GetLastError() == ERROR_SEM_TIMEOUT, "wrong error %u\n", GetLastError());
100 /* don't try to do i/o if one side couldn't be opened, as it hangs */
101 if (hFile != INVALID_HANDLE_VALUE) {
104 /* Make sure we can read and write a few bytes in both directions */
105 memset(ibuf, 0, sizeof(ibuf));
106 ok(WriteFile(hnp, obuf, sizeof(obuf), &written, NULL), "WriteFile\n");
107 ok(written == sizeof(obuf), "write file len 1\n");
108 ok(PeekNamedPipe(hFile, NULL, 0, NULL, &readden, NULL), "Peek\n");
109 ok(readden == sizeof(obuf), "peek 1 got %d bytes\n", readden);
110 ok(ReadFile(hFile, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
111 ok(readden == sizeof(obuf), "read 1 got %d bytes\n", readden);
112 ok(memcmp(obuf, ibuf, written) == 0, "content 1 check\n");
114 memset(ibuf, 0, sizeof(ibuf));
115 ok(WriteFile(hFile, obuf2, sizeof(obuf2), &written, NULL), "WriteFile\n");
116 ok(written == sizeof(obuf2), "write file len 2\n");
117 ok(PeekNamedPipe(hnp, NULL, 0, NULL, &readden, NULL), "Peek\n");
118 ok(readden == sizeof(obuf2), "peek 2 got %d bytes\n", readden);
119 ok(PeekNamedPipe(hnp, (LPVOID)1, 0, NULL, &readden, NULL), "Peek\n");
120 ok(readden == sizeof(obuf2), "peek 2 got %d bytes\n", readden);
121 ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
122 ok(readden == sizeof(obuf2), "read 2 got %d bytes\n", readden);
123 ok(memcmp(obuf2, ibuf, written) == 0, "content 2 check\n");
125 /* Test reading of multiple writes */
126 memset(ibuf, 0, sizeof(ibuf));
127 ok(WriteFile(hnp, obuf, sizeof(obuf), &written, NULL), "WriteFile3a\n");
128 ok(written == sizeof(obuf), "write file len 3a\n");
129 ok(WriteFile(hnp, obuf2, sizeof(obuf2), &written, NULL), " WriteFile3b\n");
130 ok(written == sizeof(obuf2), "write file len 3b\n");
131 ok(PeekNamedPipe(hFile, ibuf, sizeof(ibuf), &readden, &avail, NULL), "Peek3\n");
132 if (pipemode == PIPE_TYPE_BYTE) {
133 if (readden != sizeof(obuf)) /* Linux only returns the first message */
134 ok(readden == sizeof(obuf) + sizeof(obuf2), "peek3 got %d bytes\n", readden);
136 todo_wine ok(readden == sizeof(obuf) + sizeof(obuf2), "peek3 got %d bytes\n", readden);
140 if (readden != sizeof(obuf) + sizeof(obuf2)) /* MacOS returns both messages */
141 ok(readden == sizeof(obuf), "peek3 got %d bytes\n", readden);
143 todo_wine ok(readden == sizeof(obuf), "peek3 got %d bytes\n", readden);
145 if (avail != sizeof(obuf)) /* older Linux kernels only return the first write here */
146 ok(avail == sizeof(obuf) + sizeof(obuf2), "peek3 got %d bytes available\n", avail);
148 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "pipe content 3a check\n");
149 if (pipemode == PIPE_TYPE_BYTE && readden >= sizeof(obuf)+sizeof(obuf2)) {
150 pbuf += sizeof(obuf);
151 ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "pipe content 3b check\n");
153 ok(ReadFile(hFile, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
154 ok(readden == sizeof(obuf) + sizeof(obuf2), "read 3 got %d bytes\n", readden);
156 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 3a check\n");
157 pbuf += sizeof(obuf);
158 ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "content 3b check\n");
160 /* Multiple writes in the reverse direction */
161 memset(ibuf, 0, sizeof(ibuf));
162 ok(WriteFile(hFile, obuf, sizeof(obuf), &written, NULL), "WriteFile4a\n");
163 ok(written == sizeof(obuf), "write file len 4a\n");
164 ok(WriteFile(hFile, obuf2, sizeof(obuf2), &written, NULL), " WriteFile4b\n");
165 ok(written == sizeof(obuf2), "write file len 4b\n");
166 ok(PeekNamedPipe(hnp, ibuf, sizeof(ibuf), &readden, &avail, NULL), "Peek4\n");
167 if (pipemode == PIPE_TYPE_BYTE) {
168 if (readden != sizeof(obuf)) /* Linux only returns the first message */
169 /* should return all 23 bytes */
170 ok(readden == sizeof(obuf) + sizeof(obuf2), "peek4 got %d bytes\n", readden);
172 todo_wine ok(readden == sizeof(obuf) + sizeof(obuf2), "peek4 got %d bytes\n", readden);
176 if (readden != sizeof(obuf) + sizeof(obuf2)) /* MacOS returns both messages */
177 ok(readden == sizeof(obuf), "peek4 got %d bytes\n", readden);
179 todo_wine ok(readden == sizeof(obuf), "peek4 got %d bytes\n", readden);
181 if (avail != sizeof(obuf)) /* older Linux kernels only return the first write here */
182 ok(avail == sizeof(obuf) + sizeof(obuf2), "peek4 got %d bytes available\n", avail);
184 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "pipe content 4a check\n");
185 if (pipemode == PIPE_TYPE_BYTE && readden >= sizeof(obuf)+sizeof(obuf2)) {
186 pbuf += sizeof(obuf);
187 ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "pipe content 4b check\n");
189 ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
190 if (pipemode == PIPE_TYPE_BYTE) {
191 ok(readden == sizeof(obuf) + sizeof(obuf2), "read 4 got %d bytes\n", readden);
195 ok(readden == sizeof(obuf), "read 4 got %d bytes\n", readden);
199 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 4a check\n");
200 if (pipemode == PIPE_TYPE_BYTE) {
201 pbuf += sizeof(obuf);
202 ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "content 4b check\n");
205 /* Test reading of multiple writes after a mode change
206 (CreateFile always creates a byte mode pipe) */
207 lpmode = PIPE_READMODE_MESSAGE;
208 if (pipemode == PIPE_TYPE_BYTE) {
209 /* trying to change the client end of a byte pipe to message mode should fail */
210 ok(!SetNamedPipeHandleState(hFile, &lpmode, NULL, NULL), "Change mode\n");
214 ok(SetNamedPipeHandleState(hFile, &lpmode, NULL, NULL), "Change mode\n");
217 memset(ibuf, 0, sizeof(ibuf));
218 ok(WriteFile(hnp, obuf, sizeof(obuf), &written, NULL), "WriteFile5a\n");
219 ok(written == sizeof(obuf), "write file len 3a\n");
220 ok(WriteFile(hnp, obuf2, sizeof(obuf2), &written, NULL), " WriteFile5b\n");
221 ok(written == sizeof(obuf2), "write file len 3b\n");
222 ok(PeekNamedPipe(hFile, ibuf, sizeof(ibuf), &readden, &avail, NULL), "Peek5\n");
223 if (readden != sizeof(obuf) + sizeof(obuf2)) /* MacOS returns both writes */
224 ok(readden == sizeof(obuf), "peek5 got %d bytes\n", readden);
226 todo_wine ok(readden == sizeof(obuf), "peek5 got %d bytes\n", readden);
227 if (avail != sizeof(obuf)) /* older Linux kernels only return the first write here */
228 ok(avail == sizeof(obuf) + sizeof(obuf2), "peek5 got %d bytes available\n", avail);
230 todo_wine ok(avail == sizeof(obuf) + sizeof(obuf2), "peek5 got %d bytes available\n", avail);
232 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 5a check\n");
233 ok(ReadFile(hFile, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
235 ok(readden == sizeof(obuf), "read 5 got %d bytes\n", readden);
238 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 5a check\n");
240 /* Multiple writes in the reverse direction */
241 /* the write of obuf2 from write4 should still be in the buffer */
242 ok(PeekNamedPipe(hnp, ibuf, sizeof(ibuf), &readden, &avail, NULL), "Peek6a\n");
244 ok(readden == sizeof(obuf2), "peek6a got %d bytes\n", readden);
245 ok(avail == sizeof(obuf2), "peek6a got %d bytes available\n", avail);
248 ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
249 ok(readden == sizeof(obuf2), "read 6a got %d bytes\n", readden);
251 ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "content 6a check\n");
253 memset(ibuf, 0, sizeof(ibuf));
254 ok(WriteFile(hFile, obuf, sizeof(obuf), &written, NULL), "WriteFile6a\n");
255 ok(written == sizeof(obuf), "write file len 6a\n");
256 ok(WriteFile(hFile, obuf2, sizeof(obuf2), &written, NULL), " WriteFile6b\n");
257 ok(written == sizeof(obuf2), "write file len 6b\n");
258 ok(PeekNamedPipe(hnp, ibuf, sizeof(ibuf), &readden, &avail, NULL), "Peek6\n");
259 if (readden != sizeof(obuf) + sizeof(obuf2)) /* MacOS returns both writes */
260 ok(readden == sizeof(obuf), "peek6 got %d bytes\n", readden);
262 todo_wine ok(readden == sizeof(obuf), "peek6 got %d bytes\n", readden);
263 if (avail != sizeof(obuf)) /* older Linux kernels only return the first write here */
264 ok(avail == sizeof(obuf) + sizeof(obuf2), "peek6b got %d bytes available\n", avail);
266 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 6a check\n");
267 ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
269 ok(readden == sizeof(obuf), "read 6b got %d bytes\n", readden);
272 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 6a check\n");
275 /* Picky conformance tests */
277 /* Verify that you can't connect to pipe again
278 * until server calls DisconnectNamedPipe+ConnectNamedPipe
279 * or creates a new pipe
280 * case 1: other client not yet closed
282 hFile2 = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
283 ok(hFile2 == INVALID_HANDLE_VALUE,
284 "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail\n");
285 ok(GetLastError() == ERROR_PIPE_BUSY,
286 "connecting to named pipe before other client closes should fail with ERROR_PIPE_BUSY\n");
288 ok(CloseHandle(hFile), "CloseHandle\n");
290 /* case 2: other client already closed */
291 hFile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
292 ok(hFile == INVALID_HANDLE_VALUE,
293 "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail\n");
294 ok(GetLastError() == ERROR_PIPE_BUSY,
295 "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail with ERROR_PIPE_BUSY\n");
297 ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe\n");
299 /* case 3: server has called DisconnectNamedPipe but not ConnectNamed Pipe */
300 hFile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
301 ok(hFile == INVALID_HANDLE_VALUE,
302 "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail\n");
303 ok(GetLastError() == ERROR_PIPE_BUSY,
304 "connecting to named pipe after other client closes but before ConnectNamedPipe should fail with ERROR_PIPE_BUSY\n");
306 /* to be complete, we'd call ConnectNamedPipe here and loop,
307 * but by default that's blocking, so we'd either have
308 * to turn on the uncommon nonblocking mode, or
309 * use another thread.
313 ok(CloseHandle(hnp), "CloseHandle\n");
315 trace("test_CreateNamedPipe returning\n");
318 static void test_CreateNamedPipe_instances_must_match(void)
322 /* Check no mismatch */
323 hnp = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
324 /* nMaxInstances */ 2,
325 /* nOutBufSize */ 1024,
326 /* nInBufSize */ 1024,
327 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
328 /* lpSecurityAttrib */ NULL);
329 ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
331 hnp2 = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
332 /* nMaxInstances */ 2,
333 /* nOutBufSize */ 1024,
334 /* nInBufSize */ 1024,
335 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
336 /* lpSecurityAttrib */ NULL);
337 ok(hnp2 != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
339 ok(CloseHandle(hnp), "CloseHandle\n");
340 ok(CloseHandle(hnp2), "CloseHandle\n");
342 /* Check nMaxInstances */
343 hnp = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
344 /* nMaxInstances */ 1,
345 /* nOutBufSize */ 1024,
346 /* nInBufSize */ 1024,
347 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
348 /* lpSecurityAttrib */ NULL);
349 ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
351 hnp2 = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
352 /* nMaxInstances */ 1,
353 /* nOutBufSize */ 1024,
354 /* nInBufSize */ 1024,
355 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
356 /* lpSecurityAttrib */ NULL);
357 ok(hnp2 == INVALID_HANDLE_VALUE
358 && GetLastError() == ERROR_PIPE_BUSY, "nMaxInstances not obeyed\n");
360 ok(CloseHandle(hnp), "CloseHandle\n");
362 /* Check PIPE_ACCESS_* */
363 hnp = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
364 /* nMaxInstances */ 2,
365 /* nOutBufSize */ 1024,
366 /* nInBufSize */ 1024,
367 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
368 /* lpSecurityAttrib */ NULL);
369 ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
371 hnp2 = CreateNamedPipe(PIPENAME, PIPE_ACCESS_INBOUND, PIPE_TYPE_BYTE | PIPE_WAIT,
372 /* nMaxInstances */ 1,
373 /* nOutBufSize */ 1024,
374 /* nInBufSize */ 1024,
375 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
376 /* lpSecurityAttrib */ NULL);
377 ok(hnp2 == INVALID_HANDLE_VALUE
378 && GetLastError() == ERROR_ACCESS_DENIED, "PIPE_ACCESS_* mismatch allowed\n");
380 ok(CloseHandle(hnp), "CloseHandle\n");
385 /** implementation of alarm() */
386 static DWORD CALLBACK alarmThreadMain(LPVOID arg)
388 DWORD_PTR timeout = (DWORD_PTR) arg;
389 trace("alarmThreadMain\n");
390 if (WaitForSingleObject( alarm_event, timeout ) == WAIT_TIMEOUT)
392 ok(FALSE, "alarm\n");
398 static HANDLE hnp = INVALID_HANDLE_VALUE;
400 /** Trivial byte echo server - disconnects after each session */
401 static DWORD CALLBACK serverThreadMain1(LPVOID arg)
405 trace("serverThreadMain1 start\n");
406 /* Set up a simple echo server */
407 hnp = CreateNamedPipe(PIPENAME "serverThreadMain1", PIPE_ACCESS_DUPLEX,
408 PIPE_TYPE_BYTE | PIPE_WAIT,
409 /* nMaxInstances */ 1,
410 /* nOutBufSize */ 1024,
411 /* nInBufSize */ 1024,
412 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
413 /* lpSecurityAttrib */ NULL);
415 ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
416 for (i = 0; i < NB_SERVER_LOOPS; i++) {
422 /* Wait for client to connect */
423 trace("Server calling ConnectNamedPipe...\n");
424 ok(ConnectNamedPipe(hnp, NULL)
425 || GetLastError() == ERROR_PIPE_CONNECTED, "ConnectNamedPipe\n");
426 trace("ConnectNamedPipe returned.\n");
428 /* Echo bytes once */
429 memset(buf, 0, sizeof(buf));
431 trace("Server reading...\n");
432 success = ReadFile(hnp, buf, sizeof(buf), &readden, NULL);
433 trace("Server done reading.\n");
434 ok(success, "ReadFile\n");
435 ok(readden, "short read\n");
437 trace("Server writing...\n");
438 ok(WriteFile(hnp, buf, readden, &written, NULL), "WriteFile\n");
439 trace("Server done writing.\n");
440 ok(written == readden, "write file len\n");
442 /* finish this connection, wait for next one */
443 ok(FlushFileBuffers(hnp), "FlushFileBuffers\n");
444 trace("Server done flushing.\n");
445 ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe\n");
446 trace("Server done disconnecting.\n");
451 /** Trivial byte echo server - closes after each connection */
452 static DWORD CALLBACK serverThreadMain2(LPVOID arg)
457 trace("serverThreadMain2\n");
458 /* Set up a simple echo server */
459 hnp = CreateNamedPipe(PIPENAME "serverThreadMain2", PIPE_ACCESS_DUPLEX,
460 PIPE_TYPE_BYTE | PIPE_WAIT,
461 /* nMaxInstances */ 2,
462 /* nOutBufSize */ 1024,
463 /* nInBufSize */ 1024,
464 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
465 /* lpSecurityAttrib */ NULL);
466 ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
468 for (i = 0; i < NB_SERVER_LOOPS; i++) {
474 /* Wait for client to connect */
475 trace("Server calling ConnectNamedPipe...\n");
476 ok(ConnectNamedPipe(hnp, NULL)
477 || GetLastError() == ERROR_PIPE_CONNECTED, "ConnectNamedPipe\n");
478 trace("ConnectNamedPipe returned.\n");
480 /* Echo bytes once */
481 memset(buf, 0, sizeof(buf));
483 trace("Server reading...\n");
484 success = ReadFile(hnp, buf, sizeof(buf), &readden, NULL);
485 trace("Server done reading.\n");
486 ok(success, "ReadFile\n");
488 trace("Server writing...\n");
489 ok(WriteFile(hnp, buf, readden, &written, NULL), "WriteFile\n");
490 trace("Server done writing.\n");
491 ok(written == readden, "write file len\n");
493 /* finish this connection, wait for next one */
494 ok(FlushFileBuffers(hnp), "FlushFileBuffers\n");
495 ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe\n");
497 /* Set up next echo server */
499 CreateNamedPipe(PIPENAME "serverThreadMain2", PIPE_ACCESS_DUPLEX,
500 PIPE_TYPE_BYTE | PIPE_WAIT,
501 /* nMaxInstances */ 2,
502 /* nOutBufSize */ 1024,
503 /* nInBufSize */ 1024,
504 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
505 /* lpSecurityAttrib */ NULL);
507 ok(hnpNext != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
509 ok(CloseHandle(hnp), "CloseHandle\n");
515 /** Trivial byte echo server - uses overlapped named pipe calls */
516 static DWORD CALLBACK serverThreadMain3(LPVOID arg)
521 trace("serverThreadMain3\n");
522 /* Set up a simple echo server */
523 hnp = CreateNamedPipe(PIPENAME "serverThreadMain3", PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
524 PIPE_TYPE_BYTE | PIPE_WAIT,
525 /* nMaxInstances */ 1,
526 /* nOutBufSize */ 1024,
527 /* nInBufSize */ 1024,
528 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
529 /* lpSecurityAttrib */ NULL);
530 ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
532 hEvent = CreateEvent(NULL, /* security attribute */
533 TRUE, /* manual reset event */
534 FALSE, /* initial state */
536 ok(hEvent != NULL, "CreateEvent\n");
538 for (i = 0; i < NB_SERVER_LOOPS; i++) {
545 int letWFSOEwait = (i & 2);
546 int letGORwait = (i & 1);
549 memset(&oOverlap, 0, sizeof(oOverlap));
550 oOverlap.hEvent = hEvent;
552 /* Wait for client to connect */
554 trace("Server calling non-overlapped ConnectNamedPipe on overlapped pipe...\n");
555 success = ConnectNamedPipe(hnp, NULL);
556 err = GetLastError();
557 ok(success || (err == ERROR_PIPE_CONNECTED), "ConnectNamedPipe failed: %d\n", err);
558 trace("ConnectNamedPipe operation complete.\n");
560 trace("Server calling overlapped ConnectNamedPipe...\n");
561 success = ConnectNamedPipe(hnp, &oOverlap);
562 err = GetLastError();
563 ok(!success && (err == ERROR_IO_PENDING || err == ERROR_PIPE_CONNECTED), "overlapped ConnectNamedPipe\n");
564 trace("overlapped ConnectNamedPipe returned.\n");
565 if (!success && (err == ERROR_IO_PENDING)) {
570 ret = WaitForSingleObjectEx(hEvent, INFINITE, TRUE);
571 } while (ret == WAIT_IO_COMPLETION);
572 ok(ret == 0, "wait ConnectNamedPipe returned %x\n", ret);
574 success = GetOverlappedResult(hnp, &oOverlap, &dummy, letGORwait);
575 if (!letGORwait && !letWFSOEwait && !success) {
576 ok(GetLastError() == ERROR_IO_INCOMPLETE, "GetOverlappedResult\n");
577 success = GetOverlappedResult(hnp, &oOverlap, &dummy, TRUE);
580 ok(success || (err == ERROR_PIPE_CONNECTED), "GetOverlappedResult ConnectNamedPipe\n");
581 trace("overlapped ConnectNamedPipe operation complete.\n");
584 /* Echo bytes once */
585 memset(buf, 0, sizeof(buf));
587 trace("Server reading...\n");
588 success = ReadFile(hnp, buf, sizeof(buf), &readden, &oOverlap);
589 trace("Server ReadFile returned...\n");
590 err = GetLastError();
591 ok(success || err == ERROR_IO_PENDING, "overlapped ReadFile\n");
592 trace("overlapped ReadFile returned.\n");
593 if (!success && (err == ERROR_IO_PENDING)) {
598 ret = WaitForSingleObjectEx(hEvent, INFINITE, TRUE);
599 } while (ret == WAIT_IO_COMPLETION);
600 ok(ret == 0, "wait ReadFile returned %x\n", ret);
602 success = GetOverlappedResult(hnp, &oOverlap, &readden, letGORwait);
603 if (!letGORwait && !letWFSOEwait && !success) {
604 ok(GetLastError() == ERROR_IO_INCOMPLETE, "GetOverlappedResult\n");
605 success = GetOverlappedResult(hnp, &oOverlap, &readden, TRUE);
608 trace("Server done reading.\n");
609 ok(success, "overlapped ReadFile\n");
611 trace("Server writing...\n");
612 success = WriteFile(hnp, buf, readden, &written, &oOverlap);
613 trace("Server WriteFile returned...\n");
614 err = GetLastError();
615 ok(success || err == ERROR_IO_PENDING, "overlapped WriteFile\n");
616 trace("overlapped WriteFile returned.\n");
617 if (!success && (err == ERROR_IO_PENDING)) {
622 ret = WaitForSingleObjectEx(hEvent, INFINITE, TRUE);
623 } while (ret == WAIT_IO_COMPLETION);
624 ok(ret == 0, "wait WriteFile returned %x\n", ret);
626 success = GetOverlappedResult(hnp, &oOverlap, &written, letGORwait);
627 if (!letGORwait && !letWFSOEwait && !success) {
628 ok(GetLastError() == ERROR_IO_INCOMPLETE, "GetOverlappedResult\n");
629 success = GetOverlappedResult(hnp, &oOverlap, &written, TRUE);
632 trace("Server done writing.\n");
633 ok(success, "overlapped WriteFile\n");
634 ok(written == readden, "write file len\n");
636 /* finish this connection, wait for next one */
637 ok(FlushFileBuffers(hnp), "FlushFileBuffers\n");
638 ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe\n");
643 /** Trivial byte echo server - uses i/o completion ports */
644 static DWORD CALLBACK serverThreadMain4(LPVOID arg)
650 trace("serverThreadMain4\n");
651 /* Set up a simple echo server */
652 hnp = CreateNamedPipe(PIPENAME "serverThreadMain4", PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
653 PIPE_TYPE_BYTE | PIPE_WAIT,
654 /* nMaxInstances */ 1,
655 /* nOutBufSize */ 1024,
656 /* nInBufSize */ 1024,
657 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
658 /* lpSecurityAttrib */ NULL);
659 ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
661 hcompletion = CreateIoCompletionPort(hnp, NULL, 12345, 1);
662 ok(hcompletion != NULL, "CreateIoCompletionPort failed, error=%i\n", GetLastError());
664 for (i = 0; i < NB_SERVER_LOOPS; i++) {
677 memset(&oConnect, 0, sizeof(oConnect));
678 memset(&oRead, 0, sizeof(oRead));
679 memset(&oWrite, 0, sizeof(oWrite));
681 /* Wait for client to connect */
682 trace("Server calling overlapped ConnectNamedPipe...\n");
683 success = ConnectNamedPipe(hnp, &oConnect);
684 err = GetLastError();
685 ok(!success && (err == ERROR_IO_PENDING || err == ERROR_PIPE_CONNECTED),
686 "overlapped ConnectNamedPipe got %u err %u\n", success, err );
687 if (!success && err == ERROR_IO_PENDING) {
688 trace("ConnectNamedPipe GetQueuedCompletionStatus\n");
689 success = GetQueuedCompletionStatus(hcompletion, &dummy, &compkey, &oResult, 0);
692 ok( GetLastError() == WAIT_TIMEOUT,
693 "ConnectNamedPipe GetQueuedCompletionStatus wrong error %u\n", GetLastError());
694 success = GetQueuedCompletionStatus(hcompletion, &dummy, &compkey, &oResult, 10000);
696 ok(success, "ConnectNamedPipe GetQueuedCompletionStatus failed, errno=%i\n", GetLastError());
699 ok(compkey == 12345, "got completion key %i instead of 12345\n", (int)compkey);
700 ok(oResult == &oConnect, "got overlapped pointer %p instead of %p\n", oResult, &oConnect);
703 trace("overlapped ConnectNamedPipe operation complete.\n");
705 /* Echo bytes once */
706 memset(buf, 0, sizeof(buf));
708 trace("Server reading...\n");
709 success = ReadFile(hnp, buf, sizeof(buf), &readden, &oRead);
710 trace("Server ReadFile returned...\n");
711 err = GetLastError();
712 ok(success || err == ERROR_IO_PENDING, "overlapped ReadFile, err=%i\n", err);
713 success = GetQueuedCompletionStatus(hcompletion, &readden, &compkey,
715 ok(success, "ReadFile GetQueuedCompletionStatus failed, errno=%i\n", GetLastError());
718 ok(compkey == 12345, "got completion key %i instead of 12345\n", (int)compkey);
719 ok(oResult == &oRead, "got overlapped pointer %p instead of %p\n", oResult, &oRead);
721 trace("Server done reading.\n");
723 trace("Server writing...\n");
724 success = WriteFile(hnp, buf, readden, &written, &oWrite);
725 trace("Server WriteFile returned...\n");
726 err = GetLastError();
727 ok(success || err == ERROR_IO_PENDING, "overlapped WriteFile failed, err=%u\n", err);
728 success = GetQueuedCompletionStatus(hcompletion, &written, &compkey,
730 ok(success, "WriteFile GetQueuedCompletionStatus failed, errno=%i\n", GetLastError());
733 ok(compkey == 12345, "got completion key %i instead of 12345\n", (int)compkey);
734 ok(oResult == &oWrite, "got overlapped pointer %p instead of %p\n", oResult, &oWrite);
735 ok(written == readden, "write file len\n");
737 trace("Server done writing.\n");
739 /* finish this connection, wait for next one */
740 ok(FlushFileBuffers(hnp), "FlushFileBuffers\n");
741 success = DisconnectNamedPipe(hnp);
742 ok(success, "DisconnectNamedPipe failed, err %u\n", GetLastError());
745 ret = CloseHandle(hnp);
746 ok(ret, "CloseHandle named pipe failed, err=%i\n", GetLastError());
747 ret = CloseHandle(hcompletion);
748 ok(ret, "CloseHandle completion failed, err=%i\n", GetLastError());
753 static void exercizeServer(const char *pipename, HANDLE serverThread)
757 trace("exercizeServer starting\n");
758 for (i = 0; i < NB_SERVER_LOOPS; i++) {
759 HANDLE hFile=INVALID_HANDLE_VALUE;
760 static const char obuf[] = "Bit Bucket";
766 for (loop = 0; loop < 3; loop++) {
768 trace("Client connecting...\n");
769 /* Connect to the server */
770 hFile = CreateFileA(pipename, GENERIC_READ | GENERIC_WRITE, 0,
771 NULL, OPEN_EXISTING, 0, 0);
772 if (hFile != INVALID_HANDLE_VALUE)
774 err = GetLastError();
776 ok(err == ERROR_PIPE_BUSY || err == ERROR_FILE_NOT_FOUND, "connecting to pipe\n");
778 ok(err == ERROR_PIPE_BUSY, "connecting to pipe\n");
779 trace("connect failed, retrying\n");
782 ok(hFile != INVALID_HANDLE_VALUE, "client opening named pipe\n");
784 /* Make sure it can echo */
785 memset(ibuf, 0, sizeof(ibuf));
786 trace("Client writing...\n");
787 ok(WriteFile(hFile, obuf, sizeof(obuf), &written, NULL), "WriteFile to client end of pipe\n");
788 ok(written == sizeof(obuf), "write file len\n");
789 trace("Client reading...\n");
790 ok(ReadFile(hFile, ibuf, sizeof(obuf), &readden, NULL), "ReadFile from client end of pipe\n");
791 ok(readden == sizeof(obuf), "read file len\n");
792 ok(memcmp(obuf, ibuf, written) == 0, "content check\n");
794 trace("Client closing...\n");
795 ok(CloseHandle(hFile), "CloseHandle\n");
798 ok(WaitForSingleObject(serverThread,INFINITE) == WAIT_OBJECT_0, "WaitForSingleObject\n");
800 trace("exercizeServer returning\n");
803 static void test_NamedPipe_2(void)
806 DWORD serverThreadId;
810 trace("test_NamedPipe_2 starting\n");
811 /* Set up a twenty second timeout */
812 alarm_event = CreateEvent( NULL, TRUE, FALSE, NULL );
813 SetLastError(0xdeadbeef);
814 alarmThread = CreateThread(NULL, 0, alarmThreadMain, (void *) 20000, 0, &alarmThreadId);
815 ok(alarmThread != NULL, "CreateThread failed: %d\n", GetLastError());
817 /* The servers we're about to exercise do try to clean up carefully,
818 * but to reduce the chance of a test failure due to a pipe handle
819 * leak in the test code, we'll use a different pipe name for each server.
823 SetLastError(0xdeadbeef);
824 serverThread = CreateThread(NULL, 0, serverThreadMain1, (void *)8, 0, &serverThreadId);
825 ok(serverThread != NULL, "CreateThread failed: %d\n", GetLastError());
826 exercizeServer(PIPENAME "serverThreadMain1", serverThread);
829 SetLastError(0xdeadbeef);
830 serverThread = CreateThread(NULL, 0, serverThreadMain2, 0, 0, &serverThreadId);
831 ok(serverThread != NULL, "CreateThread failed: %d\n", GetLastError());
832 exercizeServer(PIPENAME "serverThreadMain2", serverThread);
835 SetLastError(0xdeadbeef);
836 serverThread = CreateThread(NULL, 0, serverThreadMain3, 0, 0, &serverThreadId);
837 ok(serverThread != NULL, "CreateThread failed: %d\n", GetLastError());
838 exercizeServer(PIPENAME "serverThreadMain3", serverThread);
841 SetLastError(0xdeadbeef);
842 serverThread = CreateThread(NULL, 0, serverThreadMain4, 0, 0, &serverThreadId);
843 ok(serverThread != NULL, "CreateThread failed: %d\n", GetLastError());
844 exercizeServer(PIPENAME "serverThreadMain4", serverThread);
846 ok(SetEvent( alarm_event ), "SetEvent\n");
847 CloseHandle( alarm_event );
848 trace("test_NamedPipe_2 returning\n");
851 static int test_DisconnectNamedPipe(void)
855 static const char obuf[] = "Bit Bucket";
861 SetLastError(0xdeadbeef);
862 hnp = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
863 /* nMaxInstances */ 1,
864 /* nOutBufSize */ 1024,
865 /* nInBufSize */ 1024,
866 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
867 /* lpSecurityAttrib */ NULL);
868 if ((hnp == INVALID_HANDLE_VALUE /* Win98 */ || !hnp /* Win95 */)
869 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) {
871 win_skip("Named pipes are not implemented\n");
875 ok(WriteFile(hnp, obuf, sizeof(obuf), &written, NULL) == 0
876 && GetLastError() == ERROR_PIPE_LISTENING, "WriteFile to not-yet-connected pipe\n");
877 ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL) == 0
878 && GetLastError() == ERROR_PIPE_LISTENING, "ReadFile from not-yet-connected pipe\n");
880 hFile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
881 ok(hFile != INVALID_HANDLE_VALUE, "CreateFile failed\n");
883 /* don't try to do i/o if one side couldn't be opened, as it hangs */
884 if (hFile != INVALID_HANDLE_VALUE) {
886 /* see what happens if server calls DisconnectNamedPipe
887 * when there are bytes in the pipe
890 ok(WriteFile(hFile, obuf, sizeof(obuf), &written, NULL), "WriteFile\n");
891 ok(written == sizeof(obuf), "write file len\n");
892 ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe while messages waiting\n");
893 ok(WriteFile(hFile, obuf, sizeof(obuf), &written, NULL) == 0
894 && GetLastError() == ERROR_PIPE_NOT_CONNECTED, "WriteFile to disconnected pipe\n");
895 ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL) == 0
896 && GetLastError() == ERROR_PIPE_NOT_CONNECTED,
897 "ReadFile from disconnected pipe with bytes waiting\n");
898 ok(!DisconnectNamedPipe(hnp) && GetLastError() == ERROR_PIPE_NOT_CONNECTED,
899 "DisconnectNamedPipe worked twice\n");
900 ret = WaitForSingleObject(hFile, 0);
901 ok(ret == WAIT_TIMEOUT, "WaitForSingleObject returned %X\n", ret);
902 ok(CloseHandle(hFile), "CloseHandle\n");
905 ok(CloseHandle(hnp), "CloseHandle\n");
909 static void test_CreatePipe(void)
911 SECURITY_ATTRIBUTES pipe_attr;
912 HANDLE piperead, pipewrite;
919 pipe_attr.nLength = sizeof(SECURITY_ATTRIBUTES);
920 pipe_attr.bInheritHandle = TRUE;
921 pipe_attr.lpSecurityDescriptor = NULL;
922 ok(CreatePipe(&piperead, &pipewrite, &pipe_attr, 0) != 0, "CreatePipe failed\n");
923 ok(WriteFile(pipewrite,PIPENAME,sizeof(PIPENAME), &written, NULL), "Write to anonymous pipe failed\n");
924 ok(written == sizeof(PIPENAME), "Write to anonymous pipe wrote %d bytes\n", written);
925 ok(ReadFile(piperead,readbuf,sizeof(readbuf),&read, NULL), "Read from non empty pipe failed\n");
926 ok(read == sizeof(PIPENAME), "Read from anonymous pipe got %d bytes\n", read);
927 ok(CloseHandle(pipewrite), "CloseHandle for the write pipe failed\n");
928 ok(CloseHandle(piperead), "CloseHandle for the read pipe failed\n");
930 /* Now write another chunk*/
931 ok(CreatePipe(&piperead, &pipewrite, &pipe_attr, 0) != 0, "CreatePipe failed\n");
932 ok(WriteFile(pipewrite,PIPENAME,sizeof(PIPENAME), &written, NULL), "Write to anonymous pipe failed\n");
933 ok(written == sizeof(PIPENAME), "Write to anonymous pipe wrote %d bytes\n", written);
934 /* and close the write end, read should still succeed*/
935 ok(CloseHandle(pipewrite), "CloseHandle for the Write Pipe failed\n");
936 ok(ReadFile(piperead,readbuf,sizeof(readbuf),&read, NULL), "Read from broken pipe withe with pending data failed\n");
937 ok(read == sizeof(PIPENAME), "Read from anonymous pipe got %d bytes\n", read);
938 /* But now we need to get informed that the pipe is closed */
939 ok(ReadFile(piperead,readbuf,sizeof(readbuf),&read, NULL) == 0, "Broken pipe not detected\n");
940 ok(CloseHandle(piperead), "CloseHandle for the read pipe failed\n");
942 /* Try bigger chunks */
944 buffer = HeapAlloc( GetProcessHeap(), 0, size );
945 for (i = 0; i < size; i++) buffer[i] = i;
946 ok(CreatePipe(&piperead, &pipewrite, &pipe_attr, (size + 24)) != 0, "CreatePipe failed\n");
947 ok(WriteFile(pipewrite, buffer, size, &written, NULL), "Write to anonymous pipe failed\n");
948 ok(written == size, "Write to anonymous pipe wrote %d bytes\n", written);
949 /* and close the write end, read should still succeed*/
950 ok(CloseHandle(pipewrite), "CloseHandle for the Write Pipe failed\n");
951 memset( buffer, 0, size );
952 ok(ReadFile(piperead, buffer, size, &read, NULL), "Read from broken pipe withe with pending data failed\n");
953 ok(read == size, "Read from anonymous pipe got %d bytes\n", read);
954 for (i = 0; i < size; i++) ok( buffer[i] == (BYTE)i, "invalid data %x at %x\n", buffer[i], i );
955 /* But now we need to get informed that the pipe is closed */
956 ok(ReadFile(piperead,readbuf,sizeof(readbuf),&read, NULL) == 0, "Broken pipe not detected\n");
957 ok(CloseHandle(piperead), "CloseHandle for the read pipe failed\n");
958 HeapFree(GetProcessHeap(), 0, buffer);
961 struct named_pipe_client_params
963 DWORD security_flags;
968 #define PIPE_NAME "\\\\.\\pipe\\named_pipe_test"
970 static DWORD CALLBACK named_pipe_client_func(LPVOID p)
972 struct named_pipe_client_params *params = p;
975 const char message[] = "Test";
976 DWORD bytes_read, bytes_written;
978 TOKEN_PRIVILEGES *Privileges = NULL;
984 /* modify the token so we can tell if the pipe impersonation
985 * token reverts to the process token */
986 ret = AdjustTokenPrivileges(params->token, TRUE, NULL, 0, NULL, NULL);
987 ok(ret, "AdjustTokenPrivileges failed with error %d\n", GetLastError());
989 ret = SetThreadToken(NULL, params->token);
990 ok(ret, "SetThreadToken failed with error %d\n", GetLastError());
995 HANDLE process_token;
997 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY|TOKEN_ADJUST_PRIVILEGES, &process_token);
998 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
1000 ret = GetTokenInformation(process_token, TokenPrivileges, NULL, 0, &Size);
1001 ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "GetTokenInformation(TokenPrivileges) failed with %d\n", GetLastError());
1002 Privileges = HeapAlloc(GetProcessHeap(), 0, Size);
1003 ret = GetTokenInformation(process_token, TokenPrivileges, Privileges, Size, &Size);
1004 ok(ret, "GetTokenInformation(TokenPrivileges) failed with %d\n", GetLastError());
1006 ret = AdjustTokenPrivileges(process_token, TRUE, NULL, 0, NULL, NULL);
1007 ok(ret, "AdjustTokenPrivileges failed with error %d\n", GetLastError());
1009 CloseHandle(process_token);
1012 pipe = CreateFile(PIPE_NAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, params->security_flags, NULL);
1013 ok(pipe != INVALID_HANDLE_VALUE, "CreateFile for pipe failed with error %d\n", GetLastError());
1015 ret = WriteFile(pipe, message, sizeof(message), &bytes_written, NULL);
1016 ok(ret, "WriteFile failed with error %d\n", GetLastError());
1018 ret = ReadFile(pipe, &dummy, sizeof(dummy), &bytes_read, NULL);
1019 ok(ret, "ReadFile failed with error %d\n", GetLastError());
1025 ret = RevertToSelf();
1026 ok(ret, "RevertToSelf failed with error %d\n", GetLastError());
1030 ret = AdjustTokenPrivileges(params->token, TRUE, NULL, 0, NULL, NULL);
1031 ok(ret, "AdjustTokenPrivileges failed with error %d\n", GetLastError());
1036 HANDLE process_token;
1038 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &process_token);
1039 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
1041 ret = AdjustTokenPrivileges(process_token, FALSE, Privileges, 0, NULL, NULL);
1042 ok(ret, "AdjustTokenPrivileges failed with error %d\n", GetLastError());
1044 HeapFree(GetProcessHeap(), 0, Privileges);
1046 CloseHandle(process_token);
1049 ret = WriteFile(pipe, message, sizeof(message), &bytes_written, NULL);
1050 ok(ret, "WriteFile failed with error %d\n", GetLastError());
1052 ret = ReadFile(pipe, &dummy, sizeof(dummy), &bytes_read, NULL);
1053 ok(ret, "ReadFile failed with error %d\n", GetLastError());
1060 static HANDLE make_impersonation_token(DWORD Access, SECURITY_IMPERSONATION_LEVEL ImpersonationLevel)
1062 HANDLE ProcessToken;
1063 HANDLE Token = NULL;
1066 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE, &ProcessToken);
1067 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
1069 ret = pDuplicateTokenEx(ProcessToken, Access, NULL, ImpersonationLevel, TokenImpersonation, &Token);
1070 ok(ret, "DuplicateToken failed with error %d\n", GetLastError());
1072 CloseHandle(ProcessToken);
1077 static void test_ImpersonateNamedPipeClient(HANDLE hClientToken, DWORD security_flags, BOOL revert, void (*test_func)(int, HANDLE))
1086 struct named_pipe_client_params params;
1088 DWORD dwBytesWritten;
1089 HANDLE hToken = NULL;
1090 SECURITY_IMPERSONATION_LEVEL ImpersonationLevel;
1093 hPipeServer = CreateNamedPipe(PIPE_NAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT, 1, 100, 100, NMPWAIT_USE_DEFAULT_WAIT, NULL);
1094 ok(hPipeServer != INVALID_HANDLE_VALUE, "CreateNamedPipe failed with error %d\n", GetLastError());
1096 params.security_flags = security_flags;
1097 params.token = hClientToken;
1098 params.revert = revert;
1099 hThread = CreateThread(NULL, 0, named_pipe_client_func, ¶ms, 0, &dwTid);
1100 ok(hThread != NULL, "CreateThread failed with error %d\n", GetLastError());
1102 SetLastError(0xdeadbeef);
1103 ret = ImpersonateNamedPipeClient(hPipeServer);
1104 error = GetLastError();
1105 ok(ret /* win2k3 */ || (error == ERROR_CANNOT_IMPERSONATE),
1106 "ImpersonateNamedPipeClient should have failed with ERROR_CANNOT_IMPERSONATE instead of %d\n", GetLastError());
1108 ret = ConnectNamedPipe(hPipeServer, NULL);
1109 ok(ret || (GetLastError() == ERROR_PIPE_CONNECTED), "ConnectNamedPipe failed with error %d\n", GetLastError());
1111 ret = ReadFile(hPipeServer, buffer, sizeof(buffer), &dwBytesRead, NULL);
1112 ok(ret, "ReadFile failed with error %d\n", GetLastError());
1114 ret = ImpersonateNamedPipeClient(hPipeServer);
1115 ok(ret, "ImpersonateNamedPipeClient failed with error %d\n", GetLastError());
1117 ret = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, FALSE, &hToken);
1118 ok(ret, "OpenThreadToken failed with error %d\n", GetLastError());
1120 (*test_func)(0, hToken);
1122 ImpersonationLevel = 0xdeadbeef; /* to avoid false positives */
1123 ret = GetTokenInformation(hToken, TokenImpersonationLevel, &ImpersonationLevel, sizeof(ImpersonationLevel), &size);
1124 ok(ret, "GetTokenInformation(TokenImpersonationLevel) failed with error %d\n", GetLastError());
1125 ok(ImpersonationLevel == SecurityImpersonation, "ImpersonationLevel should have been SecurityImpersonation(%d) instead of %d\n", SecurityImpersonation, ImpersonationLevel);
1127 CloseHandle(hToken);
1131 ret = WriteFile(hPipeServer, &dummy, sizeof(dummy), &dwBytesWritten, NULL);
1132 ok(ret, "WriteFile failed with error %d\n", GetLastError());
1134 ret = ReadFile(hPipeServer, buffer, sizeof(buffer), &dwBytesRead, NULL);
1135 ok(ret, "ReadFile failed with error %d\n", GetLastError());
1137 ret = ImpersonateNamedPipeClient(hPipeServer);
1138 ok(ret, "ImpersonateNamedPipeClient failed with error %d\n", GetLastError());
1140 ret = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, FALSE, &hToken);
1141 ok(ret, "OpenThreadToken failed with error %d\n", GetLastError());
1143 (*test_func)(1, hToken);
1145 CloseHandle(hToken);
1149 ret = WriteFile(hPipeServer, &dummy, sizeof(dummy), &dwBytesWritten, NULL);
1150 ok(ret, "WriteFile failed with error %d\n", GetLastError());
1152 WaitForSingleObject(hThread, INFINITE);
1154 ret = ImpersonateNamedPipeClient(hPipeServer);
1155 ok(ret, "ImpersonateNamedPipeClient failed with error %d\n", GetLastError());
1159 CloseHandle(hThread);
1160 CloseHandle(hPipeServer);
1163 static BOOL are_all_privileges_disabled(HANDLE hToken)
1166 TOKEN_PRIVILEGES *Privileges = NULL;
1168 BOOL all_privs_disabled = TRUE;
1171 ret = GetTokenInformation(hToken, TokenPrivileges, NULL, 0, &Size);
1172 if (!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1174 Privileges = HeapAlloc(GetProcessHeap(), 0, Size);
1175 ret = GetTokenInformation(hToken, TokenPrivileges, Privileges, Size, &Size);
1178 HeapFree(GetProcessHeap(), 0, Privileges);
1185 for (i = 0; i < Privileges->PrivilegeCount; i++)
1187 if (Privileges->Privileges[i].Attributes & SE_PRIVILEGE_ENABLED)
1189 all_privs_disabled = FALSE;
1194 HeapFree(GetProcessHeap(), 0, Privileges);
1196 return all_privs_disabled;
1199 static DWORD get_privilege_count(HANDLE hToken)
1201 TOKEN_STATISTICS Statistics;
1202 DWORD Size = sizeof(Statistics);
1205 ret = GetTokenInformation(hToken, TokenStatistics, &Statistics, Size, &Size);
1206 ok(ret, "GetTokenInformation(TokenStatistics)\n");
1207 if (!ret) return -1;
1209 return Statistics.PrivilegeCount;
1212 static void test_no_sqos_no_token(int call_index, HANDLE hToken)
1219 priv_count = get_privilege_count(hToken);
1221 ok(priv_count == 0, "privilege count should have been 0 instead of %d\n", priv_count);
1224 priv_count = get_privilege_count(hToken);
1225 ok(priv_count > 0, "privilege count should now be > 0 instead of 0\n");
1226 ok(!are_all_privileges_disabled(hToken), "impersonated token should not have been modified\n");
1229 ok(0, "shouldn't happen\n");
1233 static void test_no_sqos(int call_index, HANDLE hToken)
1238 ok(!are_all_privileges_disabled(hToken), "token should be a copy of the process one\n");
1242 ok(are_all_privileges_disabled(hToken), "impersonated token should have been modified\n");
1245 ok(0, "shouldn't happen\n");
1249 static void test_static_context(int call_index, HANDLE hToken)
1254 ok(!are_all_privileges_disabled(hToken), "token should be a copy of the process one\n");
1257 ok(!are_all_privileges_disabled(hToken), "impersonated token should not have been modified\n");
1260 ok(0, "shouldn't happen\n");
1264 static void test_dynamic_context(int call_index, HANDLE hToken)
1269 ok(!are_all_privileges_disabled(hToken), "token should be a copy of the process one\n");
1273 ok(are_all_privileges_disabled(hToken), "impersonated token should have been modified\n");
1276 ok(0, "shouldn't happen\n");
1280 static void test_dynamic_context_no_token(int call_index, HANDLE hToken)
1285 ok(are_all_privileges_disabled(hToken), "token should be a copy of the process one\n");
1288 ok(!are_all_privileges_disabled(hToken), "process token modification should have been detected and impersonation token updated\n");
1291 ok(0, "shouldn't happen\n");
1295 static void test_no_sqos_revert(int call_index, HANDLE hToken)
1301 priv_count = get_privilege_count(hToken);
1303 ok(priv_count == 0, "privilege count should have been 0 instead of %d\n", priv_count);
1306 priv_count = get_privilege_count(hToken);
1307 ok(priv_count > 0, "privilege count should now be > 0 instead of 0\n");
1308 ok(!are_all_privileges_disabled(hToken), "impersonated token should not have been modified\n");
1311 ok(0, "shouldn't happen\n");
1315 static void test_static_context_revert(int call_index, HANDLE hToken)
1321 ok(are_all_privileges_disabled(hToken), "privileges should have been disabled\n");
1325 ok(are_all_privileges_disabled(hToken), "impersonated token should not have been modified\n");
1328 ok(0, "shouldn't happen\n");
1332 static void test_dynamic_context_revert(int call_index, HANDLE hToken)
1338 ok(are_all_privileges_disabled(hToken), "privileges should have been disabled\n");
1341 ok(!are_all_privileges_disabled(hToken), "impersonated token should now be process token\n");
1344 ok(0, "shouldn't happen\n");
1348 static void test_impersonation(void)
1350 HANDLE hClientToken;
1351 HANDLE hProcessToken;
1354 if( !pDuplicateTokenEx ) {
1355 skip("DuplicateTokenEx not found\n");
1359 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hProcessToken);
1362 skip("couldn't open process token, skipping impersonation tests\n");
1366 if (!get_privilege_count(hProcessToken) || are_all_privileges_disabled(hProcessToken))
1368 skip("token didn't have any privileges or they were all disabled. token not suitable for impersonation tests\n");
1369 CloseHandle(hProcessToken);
1372 CloseHandle(hProcessToken);
1374 test_ImpersonateNamedPipeClient(NULL, 0, FALSE, test_no_sqos_no_token);
1375 hClientToken = make_impersonation_token(TOKEN_IMPERSONATE | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, SecurityImpersonation);
1376 test_ImpersonateNamedPipeClient(hClientToken, 0, FALSE, test_no_sqos);
1377 CloseHandle(hClientToken);
1378 hClientToken = make_impersonation_token(TOKEN_IMPERSONATE | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, SecurityImpersonation);
1379 test_ImpersonateNamedPipeClient(hClientToken,
1380 SECURITY_SQOS_PRESENT | SECURITY_IMPERSONATION, FALSE,
1381 test_static_context);
1382 CloseHandle(hClientToken);
1383 hClientToken = make_impersonation_token(TOKEN_IMPERSONATE | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, SecurityImpersonation);
1384 test_ImpersonateNamedPipeClient(hClientToken,
1385 SECURITY_SQOS_PRESENT | SECURITY_CONTEXT_TRACKING | SECURITY_IMPERSONATION,
1386 FALSE, test_dynamic_context);
1387 CloseHandle(hClientToken);
1388 test_ImpersonateNamedPipeClient(NULL,
1389 SECURITY_SQOS_PRESENT | SECURITY_CONTEXT_TRACKING | SECURITY_IMPERSONATION,
1390 FALSE, test_dynamic_context_no_token);
1392 hClientToken = make_impersonation_token(TOKEN_IMPERSONATE | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, SecurityImpersonation);
1393 test_ImpersonateNamedPipeClient(hClientToken, 0, TRUE, test_no_sqos_revert);
1394 CloseHandle(hClientToken);
1395 hClientToken = make_impersonation_token(TOKEN_IMPERSONATE | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, SecurityImpersonation);
1396 test_ImpersonateNamedPipeClient(hClientToken,
1397 SECURITY_SQOS_PRESENT | SECURITY_IMPERSONATION, TRUE,
1398 test_static_context_revert);
1399 CloseHandle(hClientToken);
1400 hClientToken = make_impersonation_token(TOKEN_IMPERSONATE | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, SecurityImpersonation);
1401 test_ImpersonateNamedPipeClient(hClientToken,
1402 SECURITY_SQOS_PRESENT | SECURITY_CONTEXT_TRACKING | SECURITY_IMPERSONATION,
1403 TRUE, test_dynamic_context_revert);
1404 CloseHandle(hClientToken);
1407 struct overlapped_server_args
1409 HANDLE pipe_created;
1412 static DWORD CALLBACK overlapped_server(LPVOID arg)
1417 struct overlapped_server_args *a = arg;
1421 pipe = CreateNamedPipeA("\\\\.\\pipe\\my pipe", FILE_FLAG_OVERLAPPED | PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE, 1, 0, 0, 100000, NULL);
1422 ok(pipe != NULL, "pipe NULL\n");
1424 ol.hEvent = CreateEventA(0, 1, 0, 0);
1425 ok(ol.hEvent != NULL, "event NULL\n");
1426 ret = ConnectNamedPipe(pipe, &ol);
1427 err = GetLastError();
1428 ok(ret == 0, "ret %d\n", ret);
1429 ok(err == ERROR_IO_PENDING, "gle %d\n", err);
1430 SetEvent(a->pipe_created);
1432 ret = WaitForSingleObjectEx(ol.hEvent, INFINITE, 1);
1433 ok(ret == WAIT_OBJECT_0, "ret %x\n", ret);
1435 ret = GetOverlappedResult(pipe, &ol, &num, 1);
1436 ok(ret == 1, "ret %d\n", ret);
1438 /* This should block */
1439 ret = ReadFile(pipe, buf, sizeof(buf), &num, NULL);
1440 ok(ret == 1, "ret %d\n", ret);
1442 DisconnectNamedPipe(pipe);
1443 CloseHandle(ol.hEvent);
1448 static void test_overlapped(void)
1451 HANDLE thread, pipe;
1453 struct overlapped_server_args args;
1455 args.pipe_created = CreateEventA(0, 1, 0, 0);
1456 thread = CreateThread(NULL, 0, overlapped_server, &args, 0, &tid);
1458 WaitForSingleObject(args.pipe_created, INFINITE);
1459 pipe = CreateFileA("\\\\.\\pipe\\my pipe", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
1460 ok(pipe != INVALID_HANDLE_VALUE, "cf failed\n");
1462 /* Sleep to try to get the ReadFile in the server to occur before the following WriteFile */
1465 ret = WriteFile(pipe, "x", 1, &num, NULL);
1466 ok(ret, "WriteFile failed with error %d\n", GetLastError());
1468 WaitForSingleObject(thread, INFINITE);
1470 CloseHandle(args.pipe_created);
1471 CloseHandle(thread);
1474 static void test_NamedPipeHandleState(void)
1476 HANDLE server, client;
1478 DWORD state, instances, maxCollectionCount, collectDataTimeout;
1479 char userName[MAX_PATH];
1481 server = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX,
1482 /* dwOpenMode */ PIPE_TYPE_BYTE | PIPE_WAIT,
1483 /* nMaxInstances */ 1,
1484 /* nOutBufSize */ 1024,
1485 /* nInBufSize */ 1024,
1486 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
1487 /* lpSecurityAttrib */ NULL);
1488 ok(server != INVALID_HANDLE_VALUE, "cf failed\n");
1489 ret = GetNamedPipeHandleState(server, NULL, NULL, NULL, NULL, NULL, 0);
1491 ok(ret, "GetNamedPipeHandleState failed: %d\n", GetLastError());
1492 ret = GetNamedPipeHandleState(server, &state, &instances, NULL, NULL, NULL,
1495 ok(ret, "GetNamedPipeHandleState failed: %d\n", GetLastError());
1498 ok(state == 0, "unexpected state %08x\n", state);
1499 ok(instances == 1, "expected 1 instances, got %d\n", instances);
1501 /* Some parameters have no meaning, and therefore can't be retrieved,
1504 SetLastError(0xdeadbeef);
1505 ret = GetNamedPipeHandleState(server, &state, &instances,
1506 &maxCollectionCount, &collectDataTimeout, userName,
1507 sizeof(userName) / sizeof(userName[0]));
1509 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
1510 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1511 /* A byte-mode pipe server can't be changed to message mode. */
1512 state = PIPE_READMODE_MESSAGE;
1513 SetLastError(0xdeadbeef);
1514 ret = SetNamedPipeHandleState(server, &state, NULL, NULL);
1516 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
1517 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1519 client = CreateFileA(PIPENAME, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1520 OPEN_EXISTING, 0, NULL);
1521 ok(client != INVALID_HANDLE_VALUE, "cf failed\n");
1523 state = PIPE_READMODE_BYTE;
1524 ret = SetNamedPipeHandleState(client, &state, NULL, NULL);
1526 ok(ret, "SetNamedPipeHandleState failed: %d\n", GetLastError());
1527 /* A byte-mode pipe client can't be changed to message mode, either. */
1528 state = PIPE_READMODE_MESSAGE;
1529 SetLastError(0xdeadbeef);
1530 ret = SetNamedPipeHandleState(server, &state, NULL, NULL);
1532 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
1533 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1535 CloseHandle(client);
1536 CloseHandle(server);
1538 server = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX,
1539 /* dwOpenMode */ PIPE_TYPE_MESSAGE | PIPE_WAIT,
1540 /* nMaxInstances */ 1,
1541 /* nOutBufSize */ 1024,
1542 /* nInBufSize */ 1024,
1543 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
1544 /* lpSecurityAttrib */ NULL);
1545 ok(server != INVALID_HANDLE_VALUE, "cf failed\n");
1546 ret = GetNamedPipeHandleState(server, NULL, NULL, NULL, NULL, NULL, 0);
1548 ok(ret, "GetNamedPipeHandleState failed: %d\n", GetLastError());
1549 ret = GetNamedPipeHandleState(server, &state, &instances, NULL, NULL, NULL,
1552 ok(ret, "GetNamedPipeHandleState failed: %d\n", GetLastError());
1555 ok(state == 0, "unexpected state %08x\n", state);
1556 ok(instances == 1, "expected 1 instances, got %d\n", instances);
1558 /* In contrast to byte-mode pipes, a message-mode pipe server can be
1559 * changed to byte mode.
1561 state = PIPE_READMODE_BYTE;
1562 ret = SetNamedPipeHandleState(server, &state, NULL, NULL);
1564 ok(ret, "SetNamedPipeHandleState failed: %d\n", GetLastError());
1566 client = CreateFileA(PIPENAME, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1567 OPEN_EXISTING, 0, NULL);
1568 ok(client != INVALID_HANDLE_VALUE, "cf failed\n");
1570 state = PIPE_READMODE_MESSAGE;
1571 ret = SetNamedPipeHandleState(client, &state, NULL, NULL);
1573 ok(ret, "SetNamedPipeHandleState failed: %d\n", GetLastError());
1574 /* A message-mode pipe client can also be changed to byte mode.
1576 state = PIPE_READMODE_BYTE;
1577 ret = SetNamedPipeHandleState(client, &state, NULL, NULL);
1579 ok(ret, "SetNamedPipeHandleState failed: %d\n", GetLastError());
1581 CloseHandle(client);
1582 CloseHandle(server);
1589 hmod = GetModuleHandle("advapi32.dll");
1590 pDuplicateTokenEx = (void *) GetProcAddress(hmod, "DuplicateTokenEx");
1592 if (test_DisconnectNamedPipe())
1594 test_CreateNamedPipe_instances_must_match();
1596 test_CreateNamedPipe(PIPE_TYPE_BYTE);
1597 test_CreateNamedPipe(PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE);
1599 test_impersonation();
1601 test_NamedPipeHandleState();