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
32 #include "wine/test.h"
34 #define PIPENAME "\\\\.\\PiPe\\tests_pipe.c"
36 #define NB_SERVER_LOOPS 8
38 static HANDLE alarm_event;
39 static BOOL (WINAPI *pDuplicateTokenEx)(HANDLE,DWORD,LPSECURITY_ATTRIBUTES,
40 SECURITY_IMPERSONATION_LEVEL,TOKEN_TYPE,PHANDLE);
43 static void test_CreateNamedPipe(int pipemode)
47 static const char obuf[] = "Bit Bucket";
48 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);
67 if (hnp == INVALID_HANDLE_VALUE && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) {
68 /* Is this the right way to notify user of skipped tests? */
69 ok(hnp == INVALID_HANDLE_VALUE && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED,
70 "CreateNamedPipe not supported on this platform, skipping tests.\n");
73 ok(hnp == INVALID_HANDLE_VALUE && GetLastError() == ERROR_INVALID_NAME,
74 "CreateNamedPipe should fail if name doesn't start with \\\\.\\pipe\n");
76 hnp = CreateNamedPipe(NULL,
77 PIPE_ACCESS_DUPLEX, pipemode | PIPE_WAIT,
78 1, 1024, 1024, NMPWAIT_USE_DEFAULT_WAIT, NULL);
79 ok(hnp == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
80 "CreateNamedPipe should fail if name is NULL\n");
82 hFile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
83 ok(hFile == INVALID_HANDLE_VALUE
84 && GetLastError() == ERROR_FILE_NOT_FOUND,
85 "connecting to nonexistent named pipe should fail with ERROR_FILE_NOT_FOUND\n");
87 /* Functional checks */
89 hnp = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, pipemode | PIPE_WAIT,
90 /* nMaxInstances */ 1,
91 /* nOutBufSize */ 1024,
92 /* nInBufSize */ 1024,
93 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
94 /* lpSecurityAttrib */ NULL);
95 ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
97 ok(WaitNamedPipeA(PIPENAME, 2000), "WaitNamedPipe failed (%d)\n", GetLastError());
99 hFile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
100 ok(hFile != INVALID_HANDLE_VALUE, "CreateFile failed (%d)\n", GetLastError());
102 ok(!WaitNamedPipeA(PIPENAME, 1000), "WaitNamedPipe succeeded\n");
103 ok(GetLastError() == ERROR_SEM_TIMEOUT, "wrong error %u\n", GetLastError());
105 /* don't try to do i/o if one side couldn't be opened, as it hangs */
106 if (hFile != INVALID_HANDLE_VALUE) {
109 /* Make sure we can read and write a few bytes in both directions */
110 memset(ibuf, 0, sizeof(ibuf));
111 ok(WriteFile(hnp, obuf, sizeof(obuf), &written, NULL), "WriteFile\n");
112 ok(written == sizeof(obuf), "write file len 1\n");
113 ok(PeekNamedPipe(hFile, NULL, 0, NULL, &readden, NULL), "Peek\n");
114 ok(readden == sizeof(obuf), "peek 1 got %d bytes\n", readden);
115 ok(ReadFile(hFile, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
116 ok(readden == sizeof(obuf), "read 1 got %d bytes\n", readden);
117 ok(memcmp(obuf, ibuf, written) == 0, "content 1 check\n");
119 memset(ibuf, 0, sizeof(ibuf));
120 ok(WriteFile(hFile, obuf2, sizeof(obuf2), &written, NULL), "WriteFile\n");
121 ok(written == sizeof(obuf2), "write file len 2\n");
122 ok(PeekNamedPipe(hnp, NULL, 0, NULL, &readden, NULL), "Peek\n");
123 ok(readden == sizeof(obuf2), "peek 2 got %d bytes\n", readden);
124 ok(PeekNamedPipe(hnp, (LPVOID)1, 0, NULL, &readden, NULL), "Peek\n");
125 ok(readden == sizeof(obuf2), "peek 2 got %d bytes\n", readden);
126 ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
127 ok(readden == sizeof(obuf2), "read 2 got %d bytes\n", readden);
128 ok(memcmp(obuf2, ibuf, written) == 0, "content 2 check\n");
130 /* Test reading of multiple writes */
131 memset(ibuf, 0, sizeof(ibuf));
132 ok(WriteFile(hnp, obuf, sizeof(obuf), &written, NULL), "WriteFile3a\n");
133 ok(written == sizeof(obuf), "write file len 3a\n");
134 ok(WriteFile(hnp, obuf2, sizeof(obuf2), &written, NULL), " WriteFile3b\n");
135 ok(written == sizeof(obuf2), "write file len 3b\n");
136 ok(PeekNamedPipe(hFile, ibuf, sizeof(ibuf), &readden, &avail, NULL), "Peek3\n");
137 if (pipemode == PIPE_TYPE_BYTE) {
138 if (readden != sizeof(obuf)) /* Linux only returns the first message */
139 ok(readden == sizeof(obuf) + sizeof(obuf2), "peek3 got %d bytes\n", readden);
141 todo_wine ok(readden == sizeof(obuf) + sizeof(obuf2), "peek3 got %d bytes\n", readden);
145 if (readden != sizeof(obuf) + sizeof(obuf2)) /* MacOS returns both messages */
146 ok(readden == sizeof(obuf), "peek3 got %d bytes\n", readden);
148 todo_wine ok(readden == sizeof(obuf), "peek3 got %d bytes\n", readden);
150 if (avail != sizeof(obuf)) /* older Linux kernels only return the first write here */
151 ok(avail == sizeof(obuf) + sizeof(obuf2), "peek3 got %d bytes available\n", avail);
153 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "pipe content 3a check\n");
154 if (pipemode == PIPE_TYPE_BYTE && readden >= sizeof(obuf)+sizeof(obuf2)) {
155 pbuf += sizeof(obuf);
156 ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "pipe content 3b check\n");
158 ok(ReadFile(hFile, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
159 ok(readden == sizeof(obuf) + sizeof(obuf2), "read 3 got %d bytes\n", readden);
161 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 3a check\n");
162 pbuf += sizeof(obuf);
163 ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "content 3b check\n");
165 /* Multiple writes in the reverse direction */
166 memset(ibuf, 0, sizeof(ibuf));
167 ok(WriteFile(hFile, obuf, sizeof(obuf), &written, NULL), "WriteFile4a\n");
168 ok(written == sizeof(obuf), "write file len 4a\n");
169 ok(WriteFile(hFile, obuf2, sizeof(obuf2), &written, NULL), " WriteFile4b\n");
170 ok(written == sizeof(obuf2), "write file len 4b\n");
171 ok(PeekNamedPipe(hnp, ibuf, sizeof(ibuf), &readden, &avail, NULL), "Peek4\n");
172 if (pipemode == PIPE_TYPE_BYTE) {
173 if (readden != sizeof(obuf)) /* Linux only returns the first message */
174 /* should return all 23 bytes */
175 ok(readden == sizeof(obuf) + sizeof(obuf2), "peek4 got %d bytes\n", readden);
177 todo_wine ok(readden == sizeof(obuf) + sizeof(obuf2), "peek4 got %d bytes\n", readden);
181 if (readden != sizeof(obuf) + sizeof(obuf2)) /* MacOS returns both messages */
182 ok(readden == sizeof(obuf), "peek4 got %d bytes\n", readden);
184 todo_wine ok(readden == sizeof(obuf), "peek4 got %d bytes\n", readden);
186 if (avail != sizeof(obuf)) /* older Linux kernels only return the first write here */
187 ok(avail == sizeof(obuf) + sizeof(obuf2), "peek4 got %d bytes available\n", avail);
189 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "pipe content 4a check\n");
190 if (pipemode == PIPE_TYPE_BYTE && readden >= sizeof(obuf)+sizeof(obuf2)) {
191 pbuf += sizeof(obuf);
192 ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "pipe content 4b check\n");
194 ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
195 if (pipemode == PIPE_TYPE_BYTE) {
196 ok(readden == sizeof(obuf) + sizeof(obuf2), "read 4 got %d bytes\n", readden);
200 ok(readden == sizeof(obuf), "read 4 got %d bytes\n", readden);
204 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 4a check\n");
205 if (pipemode == PIPE_TYPE_BYTE) {
206 pbuf += sizeof(obuf);
207 ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "content 4b check\n");
210 /* Test reading of multiple writes after a mode change
211 (CreateFile always creates a byte mode pipe) */
212 lpmode = PIPE_READMODE_MESSAGE;
213 if (pipemode == PIPE_TYPE_BYTE) {
214 /* trying to change the client end of a byte pipe to message mode should fail */
215 ok(!SetNamedPipeHandleState(hFile, &lpmode, NULL, NULL), "Change mode\n");
219 ok(SetNamedPipeHandleState(hFile, &lpmode, NULL, NULL), "Change mode\n");
222 memset(ibuf, 0, sizeof(ibuf));
223 ok(WriteFile(hnp, obuf, sizeof(obuf), &written, NULL), "WriteFile5a\n");
224 ok(written == sizeof(obuf), "write file len 3a\n");
225 ok(WriteFile(hnp, obuf2, sizeof(obuf2), &written, NULL), " WriteFile5b\n");
226 ok(written == sizeof(obuf2), "write file len 3b\n");
227 ok(PeekNamedPipe(hFile, ibuf, sizeof(ibuf), &readden, &avail, NULL), "Peek5\n");
228 if (readden != sizeof(obuf) + sizeof(obuf2)) /* MacOS returns both writes */
229 ok(readden == sizeof(obuf), "peek5 got %d bytes\n", readden);
231 todo_wine ok(readden == sizeof(obuf), "peek5 got %d bytes\n", readden);
232 if (avail != sizeof(obuf)) /* older Linux kernels only return the first write here */
233 ok(avail == sizeof(obuf) + sizeof(obuf2), "peek5 got %d bytes available\n", avail);
235 todo_wine ok(avail == sizeof(obuf) + sizeof(obuf2), "peek5 got %d bytes available\n", avail);
237 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 5a check\n");
238 ok(ReadFile(hFile, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
240 ok(readden == sizeof(obuf), "read 5 got %d bytes\n", readden);
243 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 5a check\n");
245 /* Multiple writes in the reverse direction */
246 /* the write of obuf2 from write4 should still be in the buffer */
247 ok(PeekNamedPipe(hnp, ibuf, sizeof(ibuf), &readden, &avail, NULL), "Peek6a\n");
249 ok(readden == sizeof(obuf2), "peek6a got %d bytes\n", readden);
250 ok(avail == sizeof(obuf2), "peek6a got %d bytes available\n", avail);
253 ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
254 ok(readden == sizeof(obuf2), "read 6a got %d bytes\n", readden);
256 ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "content 6a check\n");
258 memset(ibuf, 0, sizeof(ibuf));
259 ok(WriteFile(hFile, obuf, sizeof(obuf), &written, NULL), "WriteFile6a\n");
260 ok(written == sizeof(obuf), "write file len 6a\n");
261 ok(WriteFile(hFile, obuf2, sizeof(obuf2), &written, NULL), " WriteFile6b\n");
262 ok(written == sizeof(obuf2), "write file len 6b\n");
263 ok(PeekNamedPipe(hnp, ibuf, sizeof(ibuf), &readden, &avail, NULL), "Peek6\n");
264 if (readden != sizeof(obuf) + sizeof(obuf2)) /* MacOS returns both writes */
265 ok(readden == sizeof(obuf), "peek6 got %d bytes\n", readden);
267 todo_wine ok(readden == sizeof(obuf), "peek6 got %d bytes\n", readden);
268 if (avail != sizeof(obuf)) /* older Linux kernels only return the first write here */
269 ok(avail == sizeof(obuf) + sizeof(obuf2), "peek6b got %d bytes available\n", avail);
271 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 6a check\n");
272 ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
274 ok(readden == sizeof(obuf), "read 6b got %d bytes\n", readden);
277 ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 6a check\n");
280 /* Picky conformance tests */
282 /* Verify that you can't connect to pipe again
283 * until server calls DisconnectNamedPipe+ConnectNamedPipe
284 * or creates a new pipe
285 * case 1: other client not yet closed
287 hFile2 = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
288 ok(hFile2 == INVALID_HANDLE_VALUE,
289 "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail\n");
290 ok(GetLastError() == ERROR_PIPE_BUSY,
291 "connecting to named pipe before other client closes should fail with ERROR_PIPE_BUSY\n");
293 ok(CloseHandle(hFile), "CloseHandle\n");
295 /* case 2: other client already closed */
296 hFile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
297 ok(hFile == INVALID_HANDLE_VALUE,
298 "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail\n");
299 ok(GetLastError() == ERROR_PIPE_BUSY,
300 "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail with ERROR_PIPE_BUSY\n");
302 ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe\n");
304 /* case 3: server has called DisconnectNamedPipe but not ConnectNamed Pipe */
305 hFile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
306 ok(hFile == INVALID_HANDLE_VALUE,
307 "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail\n");
308 ok(GetLastError() == ERROR_PIPE_BUSY,
309 "connecting to named pipe after other client closes but before ConnectNamedPipe should fail with ERROR_PIPE_BUSY\n");
311 /* to be complete, we'd call ConnectNamedPipe here and loop,
312 * but by default that's blocking, so we'd either have
313 * to turn on the uncommon nonblocking mode, or
314 * use another thread.
318 ok(CloseHandle(hnp), "CloseHandle\n");
320 trace("test_CreateNamedPipe returning\n");
323 static void test_CreateNamedPipe_instances_must_match(void)
327 /* Check no mismatch */
328 hnp = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
329 /* nMaxInstances */ 2,
330 /* nOutBufSize */ 1024,
331 /* nInBufSize */ 1024,
332 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
333 /* lpSecurityAttrib */ NULL);
334 ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
336 hnp2 = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
337 /* nMaxInstances */ 2,
338 /* nOutBufSize */ 1024,
339 /* nInBufSize */ 1024,
340 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
341 /* lpSecurityAttrib */ NULL);
342 ok(hnp2 != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
344 ok(CloseHandle(hnp), "CloseHandle\n");
345 ok(CloseHandle(hnp2), "CloseHandle\n");
347 /* Check nMaxInstances */
348 hnp = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
349 /* nMaxInstances */ 1,
350 /* nOutBufSize */ 1024,
351 /* nInBufSize */ 1024,
352 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
353 /* lpSecurityAttrib */ NULL);
354 ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
356 hnp2 = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
357 /* nMaxInstances */ 1,
358 /* nOutBufSize */ 1024,
359 /* nInBufSize */ 1024,
360 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
361 /* lpSecurityAttrib */ NULL);
362 ok(hnp2 == INVALID_HANDLE_VALUE
363 && GetLastError() == ERROR_PIPE_BUSY, "nMaxInstances not obeyed\n");
365 ok(CloseHandle(hnp), "CloseHandle\n");
367 /* Check PIPE_ACCESS_* */
368 hnp = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
369 /* nMaxInstances */ 2,
370 /* nOutBufSize */ 1024,
371 /* nInBufSize */ 1024,
372 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
373 /* lpSecurityAttrib */ NULL);
374 ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
376 hnp2 = CreateNamedPipe(PIPENAME, PIPE_ACCESS_INBOUND, PIPE_TYPE_BYTE | PIPE_WAIT,
377 /* nMaxInstances */ 1,
378 /* nOutBufSize */ 1024,
379 /* nInBufSize */ 1024,
380 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
381 /* lpSecurityAttrib */ NULL);
382 ok(hnp2 == INVALID_HANDLE_VALUE
383 && GetLastError() == ERROR_ACCESS_DENIED, "PIPE_ACCESS_* mismatch allowed\n");
385 ok(CloseHandle(hnp), "CloseHandle\n");
390 /** implementation of alarm() */
391 static DWORD CALLBACK alarmThreadMain(LPVOID arg)
393 DWORD timeout = (DWORD) arg;
394 trace("alarmThreadMain\n");
395 if (WaitForSingleObject( alarm_event, timeout ) == WAIT_TIMEOUT)
397 ok(FALSE, "alarm\n");
403 HANDLE hnp = INVALID_HANDLE_VALUE;
405 /** Trivial byte echo server - disconnects after each session */
406 static DWORD CALLBACK serverThreadMain1(LPVOID arg)
410 trace("serverThreadMain1 start\n");
411 /* Set up a simple echo server */
412 hnp = CreateNamedPipe(PIPENAME "serverThreadMain1", PIPE_ACCESS_DUPLEX,
413 PIPE_TYPE_BYTE | PIPE_WAIT,
414 /* nMaxInstances */ 1,
415 /* nOutBufSize */ 1024,
416 /* nInBufSize */ 1024,
417 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
418 /* lpSecurityAttrib */ NULL);
420 ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
421 for (i = 0; i < NB_SERVER_LOOPS; i++) {
427 /* Wait for client to connect */
428 trace("Server calling ConnectNamedPipe...\n");
429 ok(ConnectNamedPipe(hnp, NULL)
430 || GetLastError() == ERROR_PIPE_CONNECTED, "ConnectNamedPipe\n");
431 trace("ConnectNamedPipe returned.\n");
433 /* Echo bytes once */
434 memset(buf, 0, sizeof(buf));
436 trace("Server reading...\n");
437 success = ReadFile(hnp, buf, sizeof(buf), &readden, NULL);
438 trace("Server done reading.\n");
439 ok(success, "ReadFile\n");
440 ok(readden, "short read\n");
442 trace("Server writing...\n");
443 ok(WriteFile(hnp, buf, readden, &written, NULL), "WriteFile\n");
444 trace("Server done writing.\n");
445 ok(written == readden, "write file len\n");
447 /* finish this connection, wait for next one */
448 ok(FlushFileBuffers(hnp), "FlushFileBuffers\n");
449 trace("Server done flushing.\n");
450 ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe\n");
451 trace("Server done disconnecting.\n");
456 /** Trivial byte echo server - closes after each connection */
457 static DWORD CALLBACK serverThreadMain2(LPVOID arg)
462 trace("serverThreadMain2\n");
463 /* Set up a simple echo server */
464 hnp = CreateNamedPipe(PIPENAME "serverThreadMain2", PIPE_ACCESS_DUPLEX,
465 PIPE_TYPE_BYTE | PIPE_WAIT,
466 /* nMaxInstances */ 2,
467 /* nOutBufSize */ 1024,
468 /* nInBufSize */ 1024,
469 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
470 /* lpSecurityAttrib */ NULL);
471 ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
473 for (i = 0; i < NB_SERVER_LOOPS; i++) {
479 /* Wait for client to connect */
480 trace("Server calling ConnectNamedPipe...\n");
481 ok(ConnectNamedPipe(hnp, NULL)
482 || GetLastError() == ERROR_PIPE_CONNECTED, "ConnectNamedPipe\n");
483 trace("ConnectNamedPipe returned.\n");
485 /* Echo bytes once */
486 memset(buf, 0, sizeof(buf));
488 trace("Server reading...\n");
489 success = ReadFile(hnp, buf, sizeof(buf), &readden, NULL);
490 trace("Server done reading.\n");
491 ok(success, "ReadFile\n");
493 trace("Server writing...\n");
494 ok(WriteFile(hnp, buf, readden, &written, NULL), "WriteFile\n");
495 trace("Server done writing.\n");
496 ok(written == readden, "write file len\n");
498 /* finish this connection, wait for next one */
499 ok(FlushFileBuffers(hnp), "FlushFileBuffers\n");
500 ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe\n");
502 /* Set up next echo server */
504 CreateNamedPipe(PIPENAME "serverThreadMain2", PIPE_ACCESS_DUPLEX,
505 PIPE_TYPE_BYTE | PIPE_WAIT,
506 /* nMaxInstances */ 2,
507 /* nOutBufSize */ 1024,
508 /* nInBufSize */ 1024,
509 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
510 /* lpSecurityAttrib */ NULL);
512 ok(hnpNext != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
514 ok(CloseHandle(hnp), "CloseHandle\n");
520 /** Trivial byte echo server - uses overlapped named pipe calls */
521 static DWORD CALLBACK serverThreadMain3(LPVOID arg)
526 trace("serverThreadMain3\n");
527 /* Set up a simple echo server */
528 hnp = CreateNamedPipe(PIPENAME "serverThreadMain3", PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
529 PIPE_TYPE_BYTE | PIPE_WAIT,
530 /* nMaxInstances */ 1,
531 /* nOutBufSize */ 1024,
532 /* nInBufSize */ 1024,
533 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
534 /* lpSecurityAttrib */ NULL);
535 ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
537 hEvent = CreateEvent(NULL, /* security attribute */
538 TRUE, /* manual reset event */
539 FALSE, /* initial state */
541 ok(hEvent != NULL, "CreateEvent\n");
543 for (i = 0; i < NB_SERVER_LOOPS; i++) {
550 int letWFSOEwait = (i & 2);
551 int letGORwait = (i & 1);
554 memset(&oOverlap, 0, sizeof(oOverlap));
555 oOverlap.hEvent = hEvent;
557 /* Wait for client to connect */
558 trace("Server calling overlapped ConnectNamedPipe...\n");
559 success = ConnectNamedPipe(hnp, &oOverlap);
560 err = GetLastError();
561 ok(success || err == ERROR_IO_PENDING
562 || err == ERROR_PIPE_CONNECTED, "overlapped ConnectNamedPipe\n");
563 trace("overlapped ConnectNamedPipe returned.\n");
564 if (!success && (err == ERROR_IO_PENDING) && letWFSOEwait)
565 ok(WaitForSingleObjectEx(hEvent, INFINITE, TRUE) == 0, "wait ConnectNamedPipe\n");
566 success = GetOverlappedResult(hnp, &oOverlap, &dummy, letGORwait);
567 if (!letGORwait && !letWFSOEwait && !success) {
568 ok(GetLastError() == ERROR_IO_INCOMPLETE, "GetOverlappedResult\n");
569 success = GetOverlappedResult(hnp, &oOverlap, &dummy, TRUE);
571 ok(success, "GetOverlappedResult ConnectNamedPipe\n");
572 trace("overlapped ConnectNamedPipe operation complete.\n");
574 /* Echo bytes once */
575 memset(buf, 0, sizeof(buf));
577 trace("Server reading...\n");
578 success = ReadFile(hnp, buf, sizeof(buf), NULL, &oOverlap);
579 trace("Server ReadFile returned...\n");
580 err = GetLastError();
581 ok(success || err == ERROR_IO_PENDING, "overlapped ReadFile\n");
582 trace("overlapped ReadFile returned.\n");
583 if (!success && (err == ERROR_IO_PENDING) && letWFSOEwait)
584 ok(WaitForSingleObjectEx(hEvent, INFINITE, TRUE) == 0, "wait ReadFile\n");
585 success = GetOverlappedResult(hnp, &oOverlap, &readden, letGORwait);
586 if (!letGORwait && !letWFSOEwait && !success) {
587 ok(GetLastError() == ERROR_IO_INCOMPLETE, "GetOverlappedResult\n");
588 success = GetOverlappedResult(hnp, &oOverlap, &readden, TRUE);
590 trace("Server done reading.\n");
591 ok(success, "overlapped ReadFile\n");
593 trace("Server writing...\n");
594 success = WriteFile(hnp, buf, readden, NULL, &oOverlap);
595 trace("Server WriteFile returned...\n");
596 err = GetLastError();
597 ok(success || err == ERROR_IO_PENDING, "overlapped WriteFile\n");
598 trace("overlapped WriteFile returned.\n");
599 if (!success && (err == ERROR_IO_PENDING) && letWFSOEwait)
600 ok(WaitForSingleObjectEx(hEvent, INFINITE, TRUE) == 0, "wait WriteFile\n");
601 success = GetOverlappedResult(hnp, &oOverlap, &written, letGORwait);
602 if (!letGORwait && !letWFSOEwait && !success) {
603 ok(GetLastError() == ERROR_IO_INCOMPLETE, "GetOverlappedResult\n");
604 success = GetOverlappedResult(hnp, &oOverlap, &written, TRUE);
606 trace("Server done writing.\n");
607 ok(success, "overlapped WriteFile\n");
608 ok(written == readden, "write file len\n");
610 /* finish this connection, wait for next one */
611 ok(FlushFileBuffers(hnp), "FlushFileBuffers\n");
612 ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe\n");
617 static void exercizeServer(const char *pipename, HANDLE serverThread)
621 trace("exercizeServer starting\n");
622 for (i = 0; i < NB_SERVER_LOOPS; i++) {
623 HANDLE hFile=INVALID_HANDLE_VALUE;
624 static const char obuf[] = "Bit Bucket";
630 for (loop = 0; loop < 3; loop++) {
632 trace("Client connecting...\n");
633 /* Connect to the server */
634 hFile = CreateFileA(pipename, GENERIC_READ | GENERIC_WRITE, 0,
635 NULL, OPEN_EXISTING, 0, 0);
636 if (hFile != INVALID_HANDLE_VALUE)
638 err = GetLastError();
640 ok(err == ERROR_PIPE_BUSY || err == ERROR_FILE_NOT_FOUND, "connecting to pipe\n");
642 ok(err == ERROR_PIPE_BUSY, "connecting to pipe\n");
643 trace("connect failed, retrying\n");
646 ok(hFile != INVALID_HANDLE_VALUE, "client opening named pipe\n");
648 /* Make sure it can echo */
649 memset(ibuf, 0, sizeof(ibuf));
650 trace("Client writing...\n");
651 ok(WriteFile(hFile, obuf, sizeof(obuf), &written, NULL), "WriteFile to client end of pipe\n");
652 ok(written == sizeof(obuf), "write file len\n");
653 trace("Client reading...\n");
654 ok(ReadFile(hFile, ibuf, sizeof(obuf), &readden, NULL), "ReadFile from client end of pipe\n");
655 ok(readden == sizeof(obuf), "read file len\n");
656 ok(memcmp(obuf, ibuf, written) == 0, "content check\n");
658 trace("Client closing...\n");
659 ok(CloseHandle(hFile), "CloseHandle\n");
662 ok(WaitForSingleObject(serverThread,INFINITE) == WAIT_OBJECT_0, "WaitForSingleObject\n");
664 trace("exercizeServer returning\n");
667 static void test_NamedPipe_2(void)
670 DWORD serverThreadId;
674 trace("test_NamedPipe_2 starting\n");
675 /* Set up a ten second timeout */
676 alarm_event = CreateEvent( NULL, TRUE, FALSE, NULL );
677 alarmThread = CreateThread(NULL, 0, alarmThreadMain, (void *) 10000, 0, &alarmThreadId);
679 /* The servers we're about to exercize do try to clean up carefully,
680 * but to reduce the change of a test failure due to a pipe handle
681 * leak in the test code, we'll use a different pipe name for each server.
685 serverThread = CreateThread(NULL, 0, serverThreadMain1, (void *)8, 0, &serverThreadId);
686 ok(serverThread != INVALID_HANDLE_VALUE, "CreateThread\n");
687 exercizeServer(PIPENAME "serverThreadMain1", serverThread);
690 serverThread = CreateThread(NULL, 0, serverThreadMain2, 0, 0, &serverThreadId);
691 ok(serverThread != INVALID_HANDLE_VALUE, "CreateThread\n");
692 exercizeServer(PIPENAME "serverThreadMain2", serverThread);
694 if( 0 ) /* overlapped pipe server doesn't work yet - it randomly fails */
697 serverThread = CreateThread(NULL, 0, serverThreadMain3, 0, 0, &serverThreadId);
698 ok(serverThread != INVALID_HANDLE_VALUE, "CreateThread\n");
699 exercizeServer(PIPENAME "serverThreadMain3", serverThread);
702 ok(SetEvent( alarm_event ), "SetEvent\n");
703 CloseHandle( alarm_event );
704 trace("test_NamedPipe_2 returning\n");
707 static int test_DisconnectNamedPipe(void)
711 static const char obuf[] = "Bit Bucket";
716 SetLastError(0xdeadbeef);
717 hnp = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
718 /* nMaxInstances */ 1,
719 /* nOutBufSize */ 1024,
720 /* nInBufSize */ 1024,
721 /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
722 /* lpSecurityAttrib */ NULL);
723 if ((hnp == INVALID_HANDLE_VALUE /* Win98 */ || !hnp /* Win95 */)
724 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) {
726 win_skip("Named pipes are not implemented\n");
730 ok(WriteFile(hnp, obuf, sizeof(obuf), &written, NULL) == 0
731 && GetLastError() == ERROR_PIPE_LISTENING, "WriteFile to not-yet-connected pipe\n");
732 ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL) == 0
733 && GetLastError() == ERROR_PIPE_LISTENING, "ReadFile from not-yet-connected pipe\n");
735 hFile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
736 ok(hFile != INVALID_HANDLE_VALUE, "CreateFile failed\n");
738 /* don't try to do i/o if one side couldn't be opened, as it hangs */
739 if (hFile != INVALID_HANDLE_VALUE) {
741 /* see what happens if server calls DisconnectNamedPipe
742 * when there are bytes in the pipe
745 ok(WriteFile(hFile, obuf, sizeof(obuf), &written, NULL), "WriteFile\n");
746 ok(written == sizeof(obuf), "write file len\n");
747 ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe while messages waiting\n");
748 ok(WriteFile(hFile, obuf, sizeof(obuf), &written, NULL) == 0
749 && GetLastError() == ERROR_PIPE_NOT_CONNECTED, "WriteFile to disconnected pipe\n");
750 ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL) == 0
751 && GetLastError() == ERROR_PIPE_NOT_CONNECTED,
752 "ReadFile from disconnected pipe with bytes waiting\n");
753 ok(!DisconnectNamedPipe(hnp) && GetLastError() == ERROR_PIPE_NOT_CONNECTED,
754 "DisconnectNamedPipe worked twice\n");
755 ok(CloseHandle(hFile), "CloseHandle\n");
758 ok(CloseHandle(hnp), "CloseHandle\n");
762 static void test_CreatePipe(void)
764 SECURITY_ATTRIBUTES pipe_attr;
765 HANDLE piperead, pipewrite;
772 pipe_attr.nLength = sizeof(SECURITY_ATTRIBUTES);
773 pipe_attr.bInheritHandle = TRUE;
774 pipe_attr.lpSecurityDescriptor = NULL;
775 ok(CreatePipe(&piperead, &pipewrite, &pipe_attr, 0) != 0, "CreatePipe failed\n");
776 ok(WriteFile(pipewrite,PIPENAME,sizeof(PIPENAME), &written, NULL), "Write to anonymous pipe failed\n");
777 ok(written == sizeof(PIPENAME), "Write to anonymous pipe wrote %d bytes\n", written);
778 ok(ReadFile(piperead,readbuf,sizeof(readbuf),&read, NULL), "Read from non empty pipe failed\n");
779 ok(read == sizeof(PIPENAME), "Read from anonymous pipe got %d bytes\n", read);
780 ok(CloseHandle(pipewrite), "CloseHandle for the write pipe failed\n");
781 ok(CloseHandle(piperead), "CloseHandle for the read pipe failed\n");
783 /* Now write another chunk*/
784 ok(CreatePipe(&piperead, &pipewrite, &pipe_attr, 0) != 0, "CreatePipe failed\n");
785 ok(WriteFile(pipewrite,PIPENAME,sizeof(PIPENAME), &written, NULL), "Write to anonymous pipe failed\n");
786 ok(written == sizeof(PIPENAME), "Write to anonymous pipe wrote %d bytes\n", written);
787 /* and close the write end, read should still succeed*/
788 ok(CloseHandle(pipewrite), "CloseHandle for the Write Pipe failed\n");
789 ok(ReadFile(piperead,readbuf,sizeof(readbuf),&read, NULL), "Read from broken pipe withe with pending data failed\n");
790 ok(read == sizeof(PIPENAME), "Read from anonymous pipe got %d bytes\n", read);
791 /* But now we need to get informed that the pipe is closed */
792 ok(ReadFile(piperead,readbuf,sizeof(readbuf),&read, NULL) == 0, "Broken pipe not detected\n");
793 ok(CloseHandle(piperead), "CloseHandle for the read pipe failed\n");
795 /* Try bigger chunks */
797 buffer = HeapAlloc( GetProcessHeap(), 0, size );
798 for (i = 0; i < size; i++) buffer[i] = i;
799 ok(CreatePipe(&piperead, &pipewrite, &pipe_attr, (size + 24)) != 0, "CreatePipe failed\n");
800 ok(WriteFile(pipewrite, buffer, size, &written, NULL), "Write to anonymous pipe failed\n");
801 ok(written == size, "Write to anonymous pipe wrote %d bytes\n", written);
802 /* and close the write end, read should still succeed*/
803 ok(CloseHandle(pipewrite), "CloseHandle for the Write Pipe failed\n");
804 memset( buffer, 0, size );
805 ok(ReadFile(piperead, buffer, size, &read, NULL), "Read from broken pipe withe with pending data failed\n");
806 ok(read == size, "Read from anonymous pipe got %d bytes\n", read);
807 for (i = 0; i < size; i++) ok( buffer[i] == (BYTE)i, "invalid data %x at %x\n", buffer[i], i );
808 /* But now we need to get informed that the pipe is closed */
809 ok(ReadFile(piperead,readbuf,sizeof(readbuf),&read, NULL) == 0, "Broken pipe not detected\n");
810 ok(CloseHandle(piperead), "CloseHandle for the read pipe failed\n");
811 HeapFree(GetProcessHeap(), 0, buffer);
814 struct named_pipe_client_params
816 DWORD security_flags;
821 #define PIPE_NAME "\\\\.\\pipe\\named_pipe_test"
823 static DWORD CALLBACK named_pipe_client_func(LPVOID p)
825 struct named_pipe_client_params *params = (struct named_pipe_client_params *)p;
828 const char message[] = "Test";
829 DWORD bytes_read, bytes_written;
831 TOKEN_PRIVILEGES *Privileges = NULL;
837 /* modify the token so we can tell if the pipe impersonation
838 * token reverts to the process token */
839 ret = AdjustTokenPrivileges(params->token, TRUE, NULL, 0, NULL, NULL);
840 ok(ret, "AdjustTokenPrivileges failed with error %d\n", GetLastError());
842 ret = SetThreadToken(NULL, params->token);
843 ok(ret, "SetThreadToken failed with error %d\n", GetLastError());
848 HANDLE process_token;
850 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY|TOKEN_ADJUST_PRIVILEGES, &process_token);
851 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
853 ret = GetTokenInformation(process_token, TokenPrivileges, NULL, 0, &Size);
854 ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "GetTokenInformation(TokenPrivileges) failed with %d\n", GetLastError());
855 Privileges = HeapAlloc(GetProcessHeap(), 0, Size);
856 ret = GetTokenInformation(process_token, TokenPrivileges, Privileges, Size, &Size);
857 ok(ret, "GetTokenInformation(TokenPrivileges) failed with %d\n", GetLastError());
859 ret = AdjustTokenPrivileges(process_token, TRUE, NULL, 0, NULL, NULL);
860 ok(ret, "AdjustTokenPrivileges failed with error %d\n", GetLastError());
862 CloseHandle(process_token);
865 pipe = CreateFile(PIPE_NAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, params->security_flags, NULL);
866 ok(pipe != INVALID_HANDLE_VALUE, "CreateFile for pipe failed with error %d\n", GetLastError());
868 ret = WriteFile(pipe, message, sizeof(message), &bytes_written, NULL);
869 ok(ret, "WriteFile failed with error %d\n", GetLastError());
871 ret = ReadFile(pipe, &dummy, sizeof(dummy), &bytes_read, NULL);
872 ok(ret, "ReadFile failed with error %d\n", GetLastError());
878 ret = RevertToSelf();
879 ok(ret, "RevertToSelf failed with error %d\n", GetLastError());
883 ret = AdjustTokenPrivileges(params->token, TRUE, NULL, 0, NULL, NULL);
884 ok(ret, "AdjustTokenPrivileges failed with error %d\n", GetLastError());
889 HANDLE process_token;
891 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &process_token);
892 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
894 ret = AdjustTokenPrivileges(process_token, FALSE, Privileges, 0, NULL, NULL);
895 ok(ret, "AdjustTokenPrivileges failed with error %d\n", GetLastError());
897 HeapFree(GetProcessHeap(), 0, Privileges);
899 CloseHandle(process_token);
902 ret = WriteFile(pipe, message, sizeof(message), &bytes_written, NULL);
903 ok(ret, "WriteFile failed with error %d\n", GetLastError());
905 ret = ReadFile(pipe, &dummy, sizeof(dummy), &bytes_read, NULL);
906 ok(ret, "ReadFile failed with error %d\n", GetLastError());
913 static HANDLE make_impersonation_token(DWORD Access, SECURITY_IMPERSONATION_LEVEL ImpersonationLevel)
919 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE, &ProcessToken);
920 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
922 ret = pDuplicateTokenEx(ProcessToken, Access, NULL, ImpersonationLevel, TokenImpersonation, &Token);
923 ok(ret, "DuplicateToken failed with error %d\n", GetLastError());
925 CloseHandle(ProcessToken);
930 static void test_ImpersonateNamedPipeClient(HANDLE hClientToken, DWORD security_flags, BOOL revert, void (*test_func)(int, HANDLE))
939 struct named_pipe_client_params params;
941 DWORD dwBytesWritten;
942 HANDLE hToken = NULL;
943 SECURITY_IMPERSONATION_LEVEL ImpersonationLevel;
946 hPipeServer = CreateNamedPipe(PIPE_NAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT, 1, 100, 100, NMPWAIT_USE_DEFAULT_WAIT, NULL);
947 ok(hPipeServer != INVALID_HANDLE_VALUE, "CreateNamedPipe failed with error %d\n", GetLastError());
949 params.security_flags = security_flags;
950 params.token = hClientToken;
951 params.revert = revert;
952 hThread = CreateThread(NULL, 0, named_pipe_client_func, ¶ms, 0, &dwTid);
953 ok(hThread != NULL, "CreateThread failed with error %d\n", GetLastError());
955 SetLastError(0xdeadbeef);
956 ret = ImpersonateNamedPipeClient(hPipeServer);
957 error = GetLastError();
958 ok(ret /* win2k3 */ || (error == ERROR_CANNOT_IMPERSONATE),
959 "ImpersonateNamedPipeClient should have failed with ERROR_CANNOT_IMPERSONATE instead of %d\n", GetLastError());
961 ret = ConnectNamedPipe(hPipeServer, NULL);
962 ok(ret || (GetLastError() == ERROR_PIPE_CONNECTED), "ConnectNamedPipe failed with error %d\n", GetLastError());
964 ret = ReadFile(hPipeServer, buffer, sizeof(buffer), &dwBytesRead, NULL);
965 ok(ret, "ReadFile failed with error %d\n", GetLastError());
967 ret = ImpersonateNamedPipeClient(hPipeServer);
968 ok(ret, "ImpersonateNamedPipeClient failed with error %d\n", GetLastError());
970 ret = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, FALSE, &hToken);
971 ok(ret, "OpenThreadToken failed with error %d\n", GetLastError());
973 (*test_func)(0, hToken);
975 ImpersonationLevel = 0xdeadbeef; /* to avoid false positives */
976 ret = GetTokenInformation(hToken, TokenImpersonationLevel, &ImpersonationLevel, sizeof(ImpersonationLevel), &size);
977 ok(ret, "GetTokenInformation(TokenImpersonationLevel) failed with error %d\n", GetLastError());
978 ok(ImpersonationLevel == SecurityImpersonation, "ImpersonationLevel should have been SecurityImpersonation(%d) instead of %d\n", SecurityImpersonation, ImpersonationLevel);
984 ret = WriteFile(hPipeServer, &dummy, sizeof(dummy), &dwBytesWritten, NULL);
985 ok(ret, "WriteFile failed with error %d\n", GetLastError());
987 ret = ReadFile(hPipeServer, buffer, sizeof(buffer), &dwBytesRead, NULL);
988 ok(ret, "ReadFile failed with error %d\n", GetLastError());
990 ret = ImpersonateNamedPipeClient(hPipeServer);
991 ok(ret, "ImpersonateNamedPipeClient failed with error %d\n", GetLastError());
993 ret = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, FALSE, &hToken);
994 ok(ret, "OpenThreadToken failed with error %d\n", GetLastError());
996 (*test_func)(1, hToken);
1002 ret = WriteFile(hPipeServer, &dummy, sizeof(dummy), &dwBytesWritten, NULL);
1003 ok(ret, "WriteFile failed with error %d\n", GetLastError());
1005 WaitForSingleObject(hThread, INFINITE);
1007 ret = ImpersonateNamedPipeClient(hPipeServer);
1008 ok(ret, "ImpersonateNamedPipeClient failed with error %d\n", GetLastError());
1012 CloseHandle(hThread);
1013 CloseHandle(hPipeServer);
1016 static BOOL are_all_privileges_disabled(HANDLE hToken)
1019 TOKEN_PRIVILEGES *Privileges = NULL;
1021 BOOL all_privs_disabled = TRUE;
1024 ret = GetTokenInformation(hToken, TokenPrivileges, NULL, 0, &Size);
1025 if (!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1027 Privileges = HeapAlloc(GetProcessHeap(), 0, Size);
1028 ret = GetTokenInformation(hToken, TokenPrivileges, Privileges, Size, &Size);
1031 HeapFree(GetProcessHeap(), 0, Privileges);
1038 for (i = 0; i < Privileges->PrivilegeCount; i++)
1040 if (Privileges->Privileges[i].Attributes & SE_PRIVILEGE_ENABLED)
1042 all_privs_disabled = FALSE;
1047 HeapFree(GetProcessHeap(), 0, Privileges);
1049 return all_privs_disabled;
1052 static DWORD get_privilege_count(HANDLE hToken)
1054 TOKEN_STATISTICS Statistics;
1055 DWORD Size = sizeof(Statistics);
1058 ret = GetTokenInformation(hToken, TokenStatistics, &Statistics, Size, &Size);
1059 ok(ret, "GetTokenInformation(TokenStatistics)\n");
1060 if (!ret) return -1;
1062 return Statistics.PrivilegeCount;
1065 static void test_no_sqos_no_token(int call_index, HANDLE hToken)
1072 priv_count = get_privilege_count(hToken);
1074 ok(priv_count == 0, "privilege count should have been 0 instead of %d\n", priv_count);
1077 priv_count = get_privilege_count(hToken);
1078 ok(priv_count > 0, "privilege count should now be > 0 instead of 0\n");
1079 ok(!are_all_privileges_disabled(hToken), "impersonated token should not have been modified\n");
1082 ok(0, "shouldn't happen\n");
1086 static void test_no_sqos(int call_index, HANDLE hToken)
1091 ok(!are_all_privileges_disabled(hToken), "token should be a copy of the process one\n");
1095 ok(are_all_privileges_disabled(hToken), "impersonated token should have been modified\n");
1098 ok(0, "shouldn't happen\n");
1102 static void test_static_context(int call_index, HANDLE hToken)
1107 ok(!are_all_privileges_disabled(hToken), "token should be a copy of the process one\n");
1110 ok(!are_all_privileges_disabled(hToken), "impersonated token should not have been modified\n");
1113 ok(0, "shouldn't happen\n");
1117 static void test_dynamic_context(int call_index, HANDLE hToken)
1122 ok(!are_all_privileges_disabled(hToken), "token should be a copy of the process one\n");
1126 ok(are_all_privileges_disabled(hToken), "impersonated token should have been modified\n");
1129 ok(0, "shouldn't happen\n");
1133 static void test_dynamic_context_no_token(int call_index, HANDLE hToken)
1138 ok(are_all_privileges_disabled(hToken), "token should be a copy of the process one\n");
1141 ok(!are_all_privileges_disabled(hToken), "process token modification should have been detected and impersonation token updated\n");
1144 ok(0, "shouldn't happen\n");
1148 static void test_no_sqos_revert(int call_index, HANDLE hToken)
1154 priv_count = get_privilege_count(hToken);
1156 ok(priv_count == 0, "privilege count should have been 0 instead of %d\n", priv_count);
1159 priv_count = get_privilege_count(hToken);
1160 ok(priv_count > 0, "privilege count should now be > 0 instead of 0\n");
1161 ok(!are_all_privileges_disabled(hToken), "impersonated token should not have been modified\n");
1164 ok(0, "shouldn't happen\n");
1168 static void test_static_context_revert(int call_index, HANDLE hToken)
1174 ok(are_all_privileges_disabled(hToken), "privileges should have been disabled\n");
1178 ok(are_all_privileges_disabled(hToken), "impersonated token should not have been modified\n");
1181 ok(0, "shouldn't happen\n");
1185 static void test_dynamic_context_revert(int call_index, HANDLE hToken)
1191 ok(are_all_privileges_disabled(hToken), "privileges should have been disabled\n");
1194 ok(!are_all_privileges_disabled(hToken), "impersonated token should now be process token\n");
1197 ok(0, "shouldn't happen\n");
1201 static void test_impersonation(void)
1203 HANDLE hClientToken;
1204 HANDLE hProcessToken;
1207 if( !pDuplicateTokenEx ) {
1208 skip("DuplicateTokenEx not found\n");
1212 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hProcessToken);
1215 skip("couldn't open process token, skipping impersonation tests\n");
1219 if (!get_privilege_count(hProcessToken) || are_all_privileges_disabled(hProcessToken))
1221 skip("token didn't have any privileges or they were all disabled. token not suitable for impersonation tests\n");
1222 CloseHandle(hProcessToken);
1225 CloseHandle(hProcessToken);
1227 test_ImpersonateNamedPipeClient(NULL, 0, FALSE, test_no_sqos_no_token);
1228 hClientToken = make_impersonation_token(TOKEN_IMPERSONATE | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, SecurityImpersonation);
1229 test_ImpersonateNamedPipeClient(hClientToken, 0, FALSE, test_no_sqos);
1230 CloseHandle(hClientToken);
1231 hClientToken = make_impersonation_token(TOKEN_IMPERSONATE | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, SecurityImpersonation);
1232 test_ImpersonateNamedPipeClient(hClientToken,
1233 SECURITY_SQOS_PRESENT | SECURITY_IMPERSONATION, FALSE,
1234 test_static_context);
1235 CloseHandle(hClientToken);
1236 hClientToken = make_impersonation_token(TOKEN_IMPERSONATE | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, SecurityImpersonation);
1237 test_ImpersonateNamedPipeClient(hClientToken,
1238 SECURITY_SQOS_PRESENT | SECURITY_CONTEXT_TRACKING | SECURITY_IMPERSONATION,
1239 FALSE, test_dynamic_context);
1240 CloseHandle(hClientToken);
1241 test_ImpersonateNamedPipeClient(NULL,
1242 SECURITY_SQOS_PRESENT | SECURITY_CONTEXT_TRACKING | SECURITY_IMPERSONATION,
1243 FALSE, test_dynamic_context_no_token);
1245 hClientToken = make_impersonation_token(TOKEN_IMPERSONATE | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, SecurityImpersonation);
1246 test_ImpersonateNamedPipeClient(hClientToken, 0, TRUE, test_no_sqos_revert);
1247 CloseHandle(hClientToken);
1248 hClientToken = make_impersonation_token(TOKEN_IMPERSONATE | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, SecurityImpersonation);
1249 test_ImpersonateNamedPipeClient(hClientToken,
1250 SECURITY_SQOS_PRESENT | SECURITY_IMPERSONATION, TRUE,
1251 test_static_context_revert);
1252 CloseHandle(hClientToken);
1253 hClientToken = make_impersonation_token(TOKEN_IMPERSONATE | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, SecurityImpersonation);
1254 test_ImpersonateNamedPipeClient(hClientToken,
1255 SECURITY_SQOS_PRESENT | SECURITY_CONTEXT_TRACKING | SECURITY_IMPERSONATION,
1256 TRUE, test_dynamic_context_revert);
1257 CloseHandle(hClientToken);
1260 struct overlapped_server_args
1262 HANDLE pipe_created;
1265 static DWORD CALLBACK overlapped_server(LPVOID arg)
1270 struct overlapped_server_args *a = (struct overlapped_server_args*)arg;
1274 pipe = CreateNamedPipeA("\\\\.\\pipe\\my pipe", FILE_FLAG_OVERLAPPED | PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE, 1, 0, 0, 100000, NULL);
1275 ok(pipe != NULL, "pipe NULL\n");
1277 ol.hEvent = CreateEventA(0, 1, 0, 0);
1278 ok(ol.hEvent != NULL, "event NULL\n");
1279 ret = ConnectNamedPipe(pipe, &ol);
1280 err = GetLastError();
1281 ok(ret == 0, "ret %d\n", ret);
1282 ok(err == ERROR_IO_PENDING, "gle %d\n", err);
1283 SetEvent(a->pipe_created);
1285 ret = WaitForSingleObjectEx(ol.hEvent, INFINITE, 1);
1286 ok(ret == WAIT_OBJECT_0, "ret %x\n", ret);
1288 ret = GetOverlappedResult(pipe, &ol, &num, 1);
1289 ok(ret == 1, "ret %d\n", ret);
1291 /* This should block */
1292 ret = ReadFile(pipe, buf, sizeof(buf), &num, NULL);
1293 ok(ret == 1, "ret %d\n", ret);
1295 DisconnectNamedPipe(pipe);
1296 CloseHandle(ol.hEvent);
1301 static void test_overlapped(void)
1304 HANDLE thread, pipe;
1306 struct overlapped_server_args args;
1308 args.pipe_created = CreateEventA(0, 1, 0, 0);
1309 thread = CreateThread(NULL, 0, overlapped_server, &args, 0, &tid);
1311 WaitForSingleObject(args.pipe_created, INFINITE);
1312 pipe = CreateFileA("\\\\.\\pipe\\my pipe", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
1313 ok(pipe != INVALID_HANDLE_VALUE, "cf failed\n");
1315 /* Sleep to try to get the ReadFile in the server to occur before the following WriteFile */
1318 ret = WriteFile(pipe, "x", 1, &num, NULL);
1319 ok(ret == 1, "ret %d\n", ret);
1321 WaitForSingleObject(thread, INFINITE);
1323 CloseHandle(args.pipe_created);
1324 CloseHandle(thread);
1331 hmod = GetModuleHandle("advapi32.dll");
1332 pDuplicateTokenEx = (void *) GetProcAddress(hmod, "DuplicateTokenEx");
1334 if (test_DisconnectNamedPipe())
1336 test_CreateNamedPipe_instances_must_match();
1338 test_CreateNamedPipe(PIPE_TYPE_BYTE);
1339 test_CreateNamedPipe(PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE);
1341 test_impersonation();