pipe.c 31.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Unit tests for named pipe functions in Wine
 *
 * Copyright (c) 2002 Dan Kegel
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

21 22
#include <assert.h>
#include <stdarg.h>
23 24
#include <stdio.h>
#include <time.h>
25 26 27 28

#include <windef.h>
#include <winbase.h>
#include <winsock.h>
29
#include <wtypes.h>
30 31
#include <winerror.h>

32 33
#include "wine/test.h"

34
#define PIPENAME "\\\\.\\PiPe\\tests_pipe.c"
35

36 37 38 39
#define NB_SERVER_LOOPS 8

static HANDLE alarm_event;

40
static void test_CreateNamedPipe(int pipemode)
41 42 43
{
    HANDLE hnp;
    HANDLE hFile;
44 45
    static const char obuf[] = "Bit Bucket";
    static const char obuf2[] = "More bits";
46
    char ibuf[32], *pbuf;
47
    DWORD written;
Dan Kegel's avatar
Dan Kegel committed
48
    DWORD readden;
49 50
    DWORD avail;
    DWORD lpmode;
51

52 53 54 55
    if (pipemode == PIPE_TYPE_BYTE)
        trace("test_CreateNamedPipe starting in byte mode\n");
    else
        trace("test_CreateNamedPipe starting in message mode\n");
56
    /* Bad parameter checks */
57
    hnp = CreateNamedPipe("not a named pipe", PIPE_ACCESS_DUPLEX, pipemode | PIPE_WAIT,
58 59 60 61 62 63 64 65
        /* nMaxInstances */ 1,
        /* nOutBufSize */ 1024,
        /* nInBufSize */ 1024,
        /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
        /* lpSecurityAttrib */ NULL);

    if (hnp == INVALID_HANDLE_VALUE && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) {
        /* Is this the right way to notify user of skipped tests? */
Dan Kegel's avatar
Dan Kegel committed
66
        ok(hnp == INVALID_HANDLE_VALUE && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED,
67
            "CreateNamedPipe not supported on this platform, skipping tests.\n");
68 69 70
        return;
    }
    ok(hnp == INVALID_HANDLE_VALUE && GetLastError() == ERROR_INVALID_NAME,
71
        "CreateNamedPipe should fail if name doesn't start with \\\\.\\pipe\n");
72

Dan Kegel's avatar
Dan Kegel committed
73
    hnp = CreateNamedPipe(NULL,
74
        PIPE_ACCESS_DUPLEX, pipemode | PIPE_WAIT,
75 76
        1, 1024, 1024, NMPWAIT_USE_DEFAULT_WAIT, NULL);
    ok(hnp == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
77
        "CreateNamedPipe should fail if name is NULL\n");
78

Dan Kegel's avatar
Dan Kegel committed
79 80 81
    hFile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
    ok(hFile == INVALID_HANDLE_VALUE
        && GetLastError() == ERROR_FILE_NOT_FOUND,
82
        "connecting to nonexistent named pipe should fail with ERROR_FILE_NOT_FOUND\n");
83

84 85
    /* Functional checks */

86
    hnp = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, pipemode | PIPE_WAIT,
Dan Kegel's avatar
Dan Kegel committed
87 88 89 90 91
        /* nMaxInstances */ 1,
        /* nOutBufSize */ 1024,
        /* nInBufSize */ 1024,
        /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
        /* lpSecurityAttrib */ NULL);
92
    ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
Dan Kegel's avatar
Dan Kegel committed
93

94
    ok(WaitNamedPipeA(PIPENAME, 2000), "WaitNamedPipe failed (%08lx)\n", GetLastError());
95

Dan Kegel's avatar
Dan Kegel committed
96
    hFile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
97
    ok(hFile != INVALID_HANDLE_VALUE, "CreateFile failed (%08lx)\n", GetLastError());
Dan Kegel's avatar
Dan Kegel committed
98 99 100 101 102 103 104

    /* don't try to do i/o if one side couldn't be opened, as it hangs */
    if (hFile != INVALID_HANDLE_VALUE) {
        HANDLE hFile2;

        /* Make sure we can read and write a few bytes in both directions */
        memset(ibuf, 0, sizeof(ibuf));
105
        ok(WriteFile(hnp, obuf, sizeof(obuf), &written, NULL), "WriteFile\n");
106 107 108 109 110 111
        ok(written == sizeof(obuf), "write file len 1\n");
        ok(PeekNamedPipe(hFile, NULL, 0, NULL, &readden, NULL), "Peek\n");
        ok(readden == sizeof(obuf), "peek 1 got %ld bytes\n", readden);
        ok(ReadFile(hFile, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
        ok(readden == sizeof(obuf), "read 1 got %ld bytes\n", readden);
        ok(memcmp(obuf, ibuf, written) == 0, "content 1 check\n");
Dan Kegel's avatar
Dan Kegel committed
112 113

        memset(ibuf, 0, sizeof(ibuf));
114 115 116 117
        ok(WriteFile(hFile, obuf2, sizeof(obuf2), &written, NULL), "WriteFile\n");
        ok(written == sizeof(obuf2), "write file len 2\n");
        ok(PeekNamedPipe(hnp, NULL, 0, NULL, &readden, NULL), "Peek\n");
        ok(readden == sizeof(obuf2), "peek 2 got %ld bytes\n", readden);
118 119
        ok(PeekNamedPipe(hnp, (LPVOID)1, 0, NULL, &readden, NULL), "Peek\n");
        ok(readden == sizeof(obuf2), "peek 2 got %ld bytes\n", readden);
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
        ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
        ok(readden == sizeof(obuf2), "read 2 got %ld bytes\n", readden);
        ok(memcmp(obuf2, ibuf, written) == 0, "content 2 check\n");

        /* Test reading of multiple writes */
        memset(ibuf, 0, sizeof(ibuf));
        ok(WriteFile(hnp, obuf, sizeof(obuf), &written, NULL), "WriteFile3a\n");
        ok(written == sizeof(obuf), "write file len 3a\n");
        ok(WriteFile(hnp, obuf2, sizeof(obuf2), &written, NULL), " WriteFile3b\n");
        ok(written == sizeof(obuf2), "write file len 3b\n");
        ok(PeekNamedPipe(hFile, ibuf, sizeof(ibuf), &readden, &avail, NULL), "Peek3\n");
        if (pipemode == PIPE_TYPE_BYTE) {
            todo_wine {
                /* should return all 23 bytes */
                ok(readden == sizeof(obuf) + sizeof(obuf2), "peek3 got %ld bytes\n", readden);
            }
        }
        else
            ok(readden == sizeof(obuf), "peek3 got %ld bytes\n", readden);
139
        if (avail != sizeof(obuf)) /* older Linux kernels only return the first write here */
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
            ok(avail == sizeof(obuf) + sizeof(obuf2), "peek3 got %ld bytes available\n", avail);
        pbuf = ibuf;
        ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "pipe content 3a check\n");
        if (pipemode == PIPE_TYPE_BYTE) {
            todo_wine {
                pbuf += sizeof(obuf);
                ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "pipe content 3b check\n");
            }
        }
        ok(ReadFile(hFile, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
        ok(readden == sizeof(obuf) + sizeof(obuf2), "read 3 got %ld bytes\n", readden);
        pbuf = ibuf;
        ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 3a check\n");
        pbuf += sizeof(obuf);
        ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "content 3b check\n");

        /* Multiple writes in the reverse direction */
        memset(ibuf, 0, sizeof(ibuf));
        ok(WriteFile(hFile, obuf, sizeof(obuf), &written, NULL), "WriteFile4a\n");
        ok(written == sizeof(obuf), "write file len 4a\n");
        ok(WriteFile(hFile, obuf2, sizeof(obuf2), &written, NULL), " WriteFile4b\n");
        ok(written == sizeof(obuf2), "write file len 4b\n");
        ok(PeekNamedPipe(hnp, ibuf, sizeof(ibuf), &readden, &avail, NULL), "Peek4\n");
        if (pipemode == PIPE_TYPE_BYTE) {
            todo_wine {
                /* should return all 23 bytes */
                ok(readden == sizeof(obuf) + sizeof(obuf2), "peek4 got %ld bytes\n", readden);
            }
        }
        else
            ok(readden == sizeof(obuf), "peek4 got %ld bytes\n", readden);
171
        if (avail != sizeof(obuf)) /* older Linux kernels only return the first write here */
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
            ok(avail == sizeof(obuf) + sizeof(obuf2), "peek4 got %ld bytes available\n", avail);
        pbuf = ibuf;
        ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "pipe content 4a check\n");
        if (pipemode == PIPE_TYPE_BYTE) {
            todo_wine {
                pbuf += sizeof(obuf);
                ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "pipe content 4b check\n");
            }
        }
        ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
        if (pipemode == PIPE_TYPE_BYTE) {
            ok(readden == sizeof(obuf) + sizeof(obuf2), "read 4 got %ld bytes\n", readden);
        }
        else {
            todo_wine {
                ok(readden == sizeof(obuf), "read 4 got %ld bytes\n", readden);
            }
        }
        pbuf = ibuf;
        ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 4a check\n");
        if (pipemode == PIPE_TYPE_BYTE) {
            pbuf += sizeof(obuf);
            ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "content 4b check\n");
        }

        /* Test reading of multiple writes after a mode change
          (CreateFile always creates a byte mode pipe) */
        lpmode = PIPE_READMODE_MESSAGE;
        if (pipemode == PIPE_TYPE_BYTE) {
            /* trying to change the client end of a byte pipe to message mode should fail */
            ok(!SetNamedPipeHandleState(hFile, &lpmode, NULL, NULL), "Change mode\n");
        }
        else {
            todo_wine {
                ok(SetNamedPipeHandleState(hFile, &lpmode, NULL, NULL), "Change mode\n");
            }
        
            memset(ibuf, 0, sizeof(ibuf));
            ok(WriteFile(hnp, obuf, sizeof(obuf), &written, NULL), "WriteFile5a\n");
            ok(written == sizeof(obuf), "write file len 3a\n");
            ok(WriteFile(hnp, obuf2, sizeof(obuf2), &written, NULL), " WriteFile5b\n");
            ok(written == sizeof(obuf2), "write file len 3b\n");
            ok(PeekNamedPipe(hFile, ibuf, sizeof(ibuf), &readden, &avail, NULL), "Peek5\n");
            ok(readden == sizeof(obuf), "peek5 got %ld bytes\n", readden);
216
            if (avail != sizeof(obuf)) /* older Linux kernels only return the first write here */
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
                ok(avail == sizeof(obuf) + sizeof(obuf2), "peek5 got %ld bytes available\n", avail);
            pbuf = ibuf;
            ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 5a check\n");
            ok(ReadFile(hFile, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
            todo_wine {
                ok(readden == sizeof(obuf), "read 5 got %ld bytes\n", readden);
            }
            pbuf = ibuf;
            ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 5a check\n");
    
            /* Multiple writes in the reverse direction */
            /* the write of obuf2 from write4 should still be in the buffer */
            ok(PeekNamedPipe(hnp, ibuf, sizeof(ibuf), &readden, &avail, NULL), "Peek6a\n");
            todo_wine {
                ok(readden == sizeof(obuf2), "peek6a got %ld bytes\n", readden);
                ok(avail == sizeof(obuf2), "peek6a got %ld bytes available\n", avail);
            }
            if (avail > 0) {
                ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
                ok(readden == sizeof(obuf2), "read 6a got %ld bytes\n", readden);
                pbuf = ibuf;
                ok(memcmp(obuf2, pbuf, sizeof(obuf2)) == 0, "content 6a check\n");
            }
            memset(ibuf, 0, sizeof(ibuf));
            ok(WriteFile(hFile, obuf, sizeof(obuf), &written, NULL), "WriteFile6a\n");
            ok(written == sizeof(obuf), "write file len 6a\n");
            ok(WriteFile(hFile, obuf2, sizeof(obuf2), &written, NULL), " WriteFile6b\n");
            ok(written == sizeof(obuf2), "write file len 6b\n");
            ok(PeekNamedPipe(hnp, ibuf, sizeof(ibuf), &readden, &avail, NULL), "Peek6\n");
            ok(readden == sizeof(obuf), "peek6 got %ld bytes\n", readden);
247
            if (avail != sizeof(obuf)) /* older Linux kernels only return the first write here */
248 249 250 251 252 253 254 255 256 257
                ok(avail == sizeof(obuf) + sizeof(obuf2), "peek6b got %ld bytes available\n", avail);
            pbuf = ibuf;
            ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 6a check\n");
            ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL), "ReadFile\n");
            todo_wine {
                ok(readden == sizeof(obuf), "read 6b got %ld bytes\n", readden);
            }
            pbuf = ibuf;
            ok(memcmp(obuf, pbuf, sizeof(obuf)) == 0, "content 6a check\n");
        }
Dan Kegel's avatar
Dan Kegel committed
258 259 260 261 262 263 264 265 266 267

        /* Picky conformance tests */

        /* Verify that you can't connect to pipe again
         * until server calls DisconnectNamedPipe+ConnectNamedPipe
         * or creates a new pipe
         * case 1: other client not yet closed
         */
        hFile2 = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
        ok(hFile2 == INVALID_HANDLE_VALUE,
268
            "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail\n");
Dan Kegel's avatar
Dan Kegel committed
269
        ok(GetLastError() == ERROR_PIPE_BUSY,
270
            "connecting to named pipe before other client closes should fail with ERROR_PIPE_BUSY\n");
Dan Kegel's avatar
Dan Kegel committed
271

272
        ok(CloseHandle(hFile), "CloseHandle\n");
Dan Kegel's avatar
Dan Kegel committed
273 274 275 276

        /* case 2: other client already closed */
        hFile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
        ok(hFile == INVALID_HANDLE_VALUE,
277
            "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail\n");
Dan Kegel's avatar
Dan Kegel committed
278
        ok(GetLastError() == ERROR_PIPE_BUSY,
279
            "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail with ERROR_PIPE_BUSY\n");
Dan Kegel's avatar
Dan Kegel committed
280

281
        ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe\n");
Dan Kegel's avatar
Dan Kegel committed
282 283 284 285

        /* case 3: server has called DisconnectNamedPipe but not ConnectNamed Pipe */
        hFile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
        ok(hFile == INVALID_HANDLE_VALUE,
286
            "connecting to named pipe after other client closes but before DisconnectNamedPipe should fail\n");
Dan Kegel's avatar
Dan Kegel committed
287
        ok(GetLastError() == ERROR_PIPE_BUSY,
288
            "connecting to named pipe after other client closes but before ConnectNamedPipe should fail with ERROR_PIPE_BUSY\n");
Dan Kegel's avatar
Dan Kegel committed
289 290 291 292 293 294 295 296

        /* to be complete, we'd call ConnectNamedPipe here and loop,
         * but by default that's blocking, so we'd either have
         * to turn on the uncommon nonblocking mode, or
         * use another thread.
         */
    }

297
    ok(CloseHandle(hnp), "CloseHandle\n");
Dan Kegel's avatar
Dan Kegel committed
298

299
    trace("test_CreateNamedPipe returning\n");
Dan Kegel's avatar
Dan Kegel committed
300 301
}

302
static void test_CreateNamedPipe_instances_must_match(void)
Dan Kegel's avatar
Dan Kegel committed
303 304 305 306 307 308 309 310 311 312
{
    HANDLE hnp, hnp2;

    /* Check no mismatch */
    hnp = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
        /* nMaxInstances */ 2,
        /* nOutBufSize */ 1024,
        /* nInBufSize */ 1024,
        /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
        /* lpSecurityAttrib */ NULL);
313
    ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
Dan Kegel's avatar
Dan Kegel committed
314 315 316 317 318 319 320

    hnp2 = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
        /* nMaxInstances */ 2,
        /* nOutBufSize */ 1024,
        /* nInBufSize */ 1024,
        /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
        /* lpSecurityAttrib */ NULL);
321
    ok(hnp2 != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
Dan Kegel's avatar
Dan Kegel committed
322

323 324
    ok(CloseHandle(hnp), "CloseHandle\n");
    ok(CloseHandle(hnp2), "CloseHandle\n");
Dan Kegel's avatar
Dan Kegel committed
325 326 327 328 329 330 331 332

    /* Check nMaxInstances */
    hnp = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
        /* nMaxInstances */ 1,
        /* nOutBufSize */ 1024,
        /* nInBufSize */ 1024,
        /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
        /* lpSecurityAttrib */ NULL);
333
    ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
Dan Kegel's avatar
Dan Kegel committed
334 335 336 337 338 339 340 341

    hnp2 = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
        /* nMaxInstances */ 1,
        /* nOutBufSize */ 1024,
        /* nInBufSize */ 1024,
        /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
        /* lpSecurityAttrib */ NULL);
    ok(hnp2 == INVALID_HANDLE_VALUE
342
        && GetLastError() == ERROR_PIPE_BUSY, "nMaxInstances not obeyed\n");
Dan Kegel's avatar
Dan Kegel committed
343

344
    ok(CloseHandle(hnp), "CloseHandle\n");
Dan Kegel's avatar
Dan Kegel committed
345 346 347 348 349 350 351 352

    /* Check PIPE_ACCESS_* */
    hnp = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
        /* nMaxInstances */ 2,
        /* nOutBufSize */ 1024,
        /* nInBufSize */ 1024,
        /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
        /* lpSecurityAttrib */ NULL);
353
    ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
Dan Kegel's avatar
Dan Kegel committed
354 355 356 357 358 359 360 361

    hnp2 = CreateNamedPipe(PIPENAME, PIPE_ACCESS_INBOUND, PIPE_TYPE_BYTE | PIPE_WAIT,
        /* nMaxInstances */ 1,
        /* nOutBufSize */ 1024,
        /* nInBufSize */ 1024,
        /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
        /* lpSecurityAttrib */ NULL);
    ok(hnp2 == INVALID_HANDLE_VALUE
362
        && GetLastError() == ERROR_ACCESS_DENIED, "PIPE_ACCESS_* mismatch allowed\n");
Dan Kegel's avatar
Dan Kegel committed
363

364
    ok(CloseHandle(hnp), "CloseHandle\n");
Dan Kegel's avatar
Dan Kegel committed
365 366 367 368 369 370 371 372

    /* etc, etc */
}

/** implementation of alarm() */
static DWORD CALLBACK alarmThreadMain(LPVOID arg)
{
    DWORD timeout = (DWORD) arg;
373
    trace("alarmThreadMain\n");
374 375 376 377 378
    if (WaitForSingleObject( alarm_event, timeout ) == WAIT_TIMEOUT)
    {
        ok(FALSE, "alarm\n");
        ExitProcess(1);
    }
Dan Kegel's avatar
Dan Kegel committed
379 380 381 382 383 384 385 386 387 388
    return 1;
}

HANDLE hnp = INVALID_HANDLE_VALUE;

/** Trivial byte echo server - disconnects after each session */
static DWORD CALLBACK serverThreadMain1(LPVOID arg)
{
    int i;

389
    trace("serverThreadMain1 start\n");
Dan Kegel's avatar
Dan Kegel committed
390 391 392 393 394 395 396 397 398
    /* Set up a simple echo server */
    hnp = CreateNamedPipe(PIPENAME "serverThreadMain1", PIPE_ACCESS_DUPLEX,
        PIPE_TYPE_BYTE | PIPE_WAIT,
        /* nMaxInstances */ 1,
        /* nOutBufSize */ 1024,
        /* nInBufSize */ 1024,
        /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
        /* lpSecurityAttrib */ NULL);

399
    ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
400
    for (i = 0; i < NB_SERVER_LOOPS; i++) {
Dan Kegel's avatar
Dan Kegel committed
401 402 403 404 405 406
        char buf[512];
        DWORD written;
        DWORD readden;
        DWORD success;

        /* Wait for client to connect */
407
        trace("Server calling ConnectNamedPipe...\n");
Dan Kegel's avatar
Dan Kegel committed
408
        ok(ConnectNamedPipe(hnp, NULL)
409
            || GetLastError() == ERROR_PIPE_CONNECTED, "ConnectNamedPipe\n");
410
        trace("ConnectNamedPipe returned.\n");
Dan Kegel's avatar
Dan Kegel committed
411 412 413 414

        /* Echo bytes once */
        memset(buf, 0, sizeof(buf));

415
        trace("Server reading...\n");
Dan Kegel's avatar
Dan Kegel committed
416
        success = ReadFile(hnp, buf, sizeof(buf), &readden, NULL);
417
        trace("Server done reading.\n");
418 419
        ok(success, "ReadFile\n");
        ok(readden, "short read\n");
Dan Kegel's avatar
Dan Kegel committed
420

421
        trace("Server writing...\n");
422
        ok(WriteFile(hnp, buf, readden, &written, NULL), "WriteFile\n");
423
        trace("Server done writing.\n");
424
        ok(written == readden, "write file len\n");
Dan Kegel's avatar
Dan Kegel committed
425 426

        /* finish this connection, wait for next one */
427
        ok(FlushFileBuffers(hnp), "FlushFileBuffers\n");
428
        trace("Server done flushing.\n");
429
        ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe\n");
430
        trace("Server done disconnecting.\n");
Dan Kegel's avatar
Dan Kegel committed
431
    }
432
    return 0;
Dan Kegel's avatar
Dan Kegel committed
433 434 435 436 437 438 439 440
}

/** Trivial byte echo server - closes after each connection */
static DWORD CALLBACK serverThreadMain2(LPVOID arg)
{
    int i;
    HANDLE hnpNext = 0;

441
    trace("serverThreadMain2\n");
Dan Kegel's avatar
Dan Kegel committed
442 443 444 445 446 447 448 449
    /* Set up a simple echo server */
    hnp = CreateNamedPipe(PIPENAME "serverThreadMain2", PIPE_ACCESS_DUPLEX,
        PIPE_TYPE_BYTE | PIPE_WAIT,
        /* nMaxInstances */ 2,
        /* nOutBufSize */ 1024,
        /* nInBufSize */ 1024,
        /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
        /* lpSecurityAttrib */ NULL);
450
    ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
Dan Kegel's avatar
Dan Kegel committed
451

452
    for (i = 0; i < NB_SERVER_LOOPS; i++) {
Dan Kegel's avatar
Dan Kegel committed
453 454 455 456 457 458
        char buf[512];
        DWORD written;
        DWORD readden;
        DWORD success;

        /* Wait for client to connect */
459
        trace("Server calling ConnectNamedPipe...\n");
Dan Kegel's avatar
Dan Kegel committed
460
        ok(ConnectNamedPipe(hnp, NULL)
461
            || GetLastError() == ERROR_PIPE_CONNECTED, "ConnectNamedPipe\n");
462
        trace("ConnectNamedPipe returned.\n");
Dan Kegel's avatar
Dan Kegel committed
463 464 465 466

        /* Echo bytes once */
        memset(buf, 0, sizeof(buf));

467
        trace("Server reading...\n");
Dan Kegel's avatar
Dan Kegel committed
468
        success = ReadFile(hnp, buf, sizeof(buf), &readden, NULL);
469
        trace("Server done reading.\n");
470
        ok(success, "ReadFile\n");
Dan Kegel's avatar
Dan Kegel committed
471

472
        trace("Server writing...\n");
473
        ok(WriteFile(hnp, buf, readden, &written, NULL), "WriteFile\n");
474
        trace("Server done writing.\n");
475
        ok(written == readden, "write file len\n");
Dan Kegel's avatar
Dan Kegel committed
476 477

        /* finish this connection, wait for next one */
478 479
        ok(FlushFileBuffers(hnp), "FlushFileBuffers\n");
        ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe\n");
Dan Kegel's avatar
Dan Kegel committed
480 481 482 483 484 485 486 487 488 489 490

        /* Set up next echo server */
        hnpNext =
            CreateNamedPipe(PIPENAME "serverThreadMain2", PIPE_ACCESS_DUPLEX,
            PIPE_TYPE_BYTE | PIPE_WAIT,
            /* nMaxInstances */ 2,
            /* nOutBufSize */ 1024,
            /* nInBufSize */ 1024,
            /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
            /* lpSecurityAttrib */ NULL);

491
        ok(hnpNext != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
Dan Kegel's avatar
Dan Kegel committed
492

493
        ok(CloseHandle(hnp), "CloseHandle\n");
Dan Kegel's avatar
Dan Kegel committed
494 495
        hnp = hnpNext;
    }
496
    return 0;
Dan Kegel's avatar
Dan Kegel committed
497 498 499 500 501 502 503 504
}

/** Trivial byte echo server - uses overlapped named pipe calls */
static DWORD CALLBACK serverThreadMain3(LPVOID arg)
{
    int i;
    HANDLE hEvent;

505
    trace("serverThreadMain3\n");
Dan Kegel's avatar
Dan Kegel committed
506 507 508
    /* Set up a simple echo server */
    hnp = CreateNamedPipe(PIPENAME "serverThreadMain3", PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
        PIPE_TYPE_BYTE | PIPE_WAIT,
509 510 511 512 513
        /* nMaxInstances */ 1,
        /* nOutBufSize */ 1024,
        /* nInBufSize */ 1024,
        /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
        /* lpSecurityAttrib */ NULL);
514
    ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
515

Dan Kegel's avatar
Dan Kegel committed
516 517 518 519
    hEvent = CreateEvent(NULL,  /* security attribute */
        TRUE,                   /* manual reset event */
        FALSE,                  /* initial state */
        NULL);                  /* name */
520
    ok(hEvent != NULL, "CreateEvent\n");
Dan Kegel's avatar
Dan Kegel committed
521

522
    for (i = 0; i < NB_SERVER_LOOPS; i++) {
Dan Kegel's avatar
Dan Kegel committed
523 524 525 526 527 528 529 530 531 532 533 534 535 536
        char buf[512];
        DWORD written;
        DWORD readden;
        DWORD dummy;
        DWORD success;
        OVERLAPPED oOverlap;
        int letWFSOEwait = (i & 2);
        int letGORwait = (i & 1);
	DWORD err;

        memset(&oOverlap, 0, sizeof(oOverlap));
        oOverlap.hEvent = hEvent;

        /* Wait for client to connect */
537
        trace("Server calling overlapped ConnectNamedPipe...\n");
Dan Kegel's avatar
Dan Kegel committed
538 539 540
        success = ConnectNamedPipe(hnp, &oOverlap);
        err = GetLastError();
        ok(success || err == ERROR_IO_PENDING
541
            || err == ERROR_PIPE_CONNECTED, "overlapped ConnectNamedPipe\n");
542
        trace("overlapped ConnectNamedPipe returned.\n");
Dan Kegel's avatar
Dan Kegel committed
543
        if (!success && (err == ERROR_IO_PENDING) && letWFSOEwait)
544
            ok(WaitForSingleObjectEx(hEvent, INFINITE, TRUE) == 0, "wait ConnectNamedPipe\n");
Dan Kegel's avatar
Dan Kegel committed
545 546
        success = GetOverlappedResult(hnp, &oOverlap, &dummy, letGORwait);
	if (!letGORwait && !letWFSOEwait && !success) {
547
	    ok(GetLastError() == ERROR_IO_INCOMPLETE, "GetOverlappedResult\n");
Dan Kegel's avatar
Dan Kegel committed
548 549
	    success = GetOverlappedResult(hnp, &oOverlap, &dummy, TRUE);
	}
550
	ok(success, "GetOverlappedResult ConnectNamedPipe\n");
551
        trace("overlapped ConnectNamedPipe operation complete.\n");
Dan Kegel's avatar
Dan Kegel committed
552 553 554 555

        /* Echo bytes once */
        memset(buf, 0, sizeof(buf));

556
        trace("Server reading...\n");
Dan Kegel's avatar
Dan Kegel committed
557
        success = ReadFile(hnp, buf, sizeof(buf), NULL, &oOverlap);
558
        trace("Server ReadFile returned...\n");
Dan Kegel's avatar
Dan Kegel committed
559
        err = GetLastError();
560
        ok(success || err == ERROR_IO_PENDING, "overlapped ReadFile\n");
561
        trace("overlapped ReadFile returned.\n");
Dan Kegel's avatar
Dan Kegel committed
562
        if (!success && (err == ERROR_IO_PENDING) && letWFSOEwait)
563
            ok(WaitForSingleObjectEx(hEvent, INFINITE, TRUE) == 0, "wait ReadFile\n");
Dan Kegel's avatar
Dan Kegel committed
564 565
        success = GetOverlappedResult(hnp, &oOverlap, &readden, letGORwait);
	if (!letGORwait && !letWFSOEwait && !success) {
566
	    ok(GetLastError() == ERROR_IO_INCOMPLETE, "GetOverlappedResult\n");
Dan Kegel's avatar
Dan Kegel committed
567 568
	    success = GetOverlappedResult(hnp, &oOverlap, &readden, TRUE);
	}
569
        trace("Server done reading.\n");
570
        ok(success, "overlapped ReadFile\n");
Dan Kegel's avatar
Dan Kegel committed
571

572
        trace("Server writing...\n");
Dan Kegel's avatar
Dan Kegel committed
573
        success = WriteFile(hnp, buf, readden, NULL, &oOverlap);
574
        trace("Server WriteFile returned...\n");
Dan Kegel's avatar
Dan Kegel committed
575
        err = GetLastError();
576
        ok(success || err == ERROR_IO_PENDING, "overlapped WriteFile\n");
577
        trace("overlapped WriteFile returned.\n");
Dan Kegel's avatar
Dan Kegel committed
578
        if (!success && (err == ERROR_IO_PENDING) && letWFSOEwait)
579
            ok(WaitForSingleObjectEx(hEvent, INFINITE, TRUE) == 0, "wait WriteFile\n");
Dan Kegel's avatar
Dan Kegel committed
580 581
        success = GetOverlappedResult(hnp, &oOverlap, &written, letGORwait);
	if (!letGORwait && !letWFSOEwait && !success) {
582
	    ok(GetLastError() == ERROR_IO_INCOMPLETE, "GetOverlappedResult\n");
Dan Kegel's avatar
Dan Kegel committed
583 584
	    success = GetOverlappedResult(hnp, &oOverlap, &written, TRUE);
	}
585
        trace("Server done writing.\n");
586 587
        ok(success, "overlapped WriteFile\n");
        ok(written == readden, "write file len\n");
Dan Kegel's avatar
Dan Kegel committed
588 589

        /* finish this connection, wait for next one */
590 591
        ok(FlushFileBuffers(hnp), "FlushFileBuffers\n");
        ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe\n");
Dan Kegel's avatar
Dan Kegel committed
592
    }
593
    return 0;
Dan Kegel's avatar
Dan Kegel committed
594 595 596 597 598 599
}

static void exercizeServer(const char *pipename, HANDLE serverThread)
{
    int i;

600
    trace("exercizeServer starting\n");
601
    for (i = 0; i < NB_SERVER_LOOPS; i++) {
602
        HANDLE hFile=INVALID_HANDLE_VALUE;
603
        static const char obuf[] = "Bit Bucket";
Dan Kegel's avatar
Dan Kegel committed
604 605 606 607 608 609 610
        char ibuf[32];
        DWORD written;
        DWORD readden;
        int loop;

        for (loop = 0; loop < 3; loop++) {
	    DWORD err;
611
            trace("Client connecting...\n");
Dan Kegel's avatar
Dan Kegel committed
612 613 614 615 616 617 618
            /* Connect to the server */
            hFile = CreateFileA(pipename, GENERIC_READ | GENERIC_WRITE, 0,
                NULL, OPEN_EXISTING, 0, 0);
            if (hFile != INVALID_HANDLE_VALUE)
                break;
	    err = GetLastError();
	    if (loop == 0)
619
	        ok(err == ERROR_PIPE_BUSY || err == ERROR_FILE_NOT_FOUND, "connecting to pipe\n");
Dan Kegel's avatar
Dan Kegel committed
620
	    else
621
	        ok(err == ERROR_PIPE_BUSY, "connecting to pipe\n");
622
            trace("connect failed, retrying\n");
Dan Kegel's avatar
Dan Kegel committed
623 624
            Sleep(200);
        }
625
        ok(hFile != INVALID_HANDLE_VALUE, "client opening named pipe\n");
Dan Kegel's avatar
Dan Kegel committed
626 627 628

        /* Make sure it can echo */
        memset(ibuf, 0, sizeof(ibuf));
629
        trace("Client writing...\n");
630 631
        ok(WriteFile(hFile, obuf, sizeof(obuf), &written, NULL), "WriteFile to client end of pipe\n");
        ok(written == sizeof(obuf), "write file len\n");
632
        trace("Client reading...\n");
633 634 635
        ok(ReadFile(hFile, ibuf, sizeof(obuf), &readden, NULL), "ReadFile from client end of pipe\n");
        ok(readden == sizeof(obuf), "read file len\n");
        ok(memcmp(obuf, ibuf, written) == 0, "content check\n");
Dan Kegel's avatar
Dan Kegel committed
636

637
        trace("Client closing...\n");
638
        ok(CloseHandle(hFile), "CloseHandle\n");
Dan Kegel's avatar
Dan Kegel committed
639 640
    }

641
    ok(WaitForSingleObject(serverThread,INFINITE) == WAIT_OBJECT_0, "WaitForSingleObject\n");
Dan Kegel's avatar
Dan Kegel committed
642
    CloseHandle(hnp);
643
    trace("exercizeServer returning\n");
Dan Kegel's avatar
Dan Kegel committed
644 645
}

646
static void test_NamedPipe_2(void)
Dan Kegel's avatar
Dan Kegel committed
647 648 649 650 651 652
{
    HANDLE serverThread;
    DWORD serverThreadId;
    HANDLE alarmThread;
    DWORD alarmThreadId;

653
    trace("test_NamedPipe_2 starting\n");
Dan Kegel's avatar
Dan Kegel committed
654
    /* Set up a ten second timeout */
655
    alarm_event = CreateEvent( NULL, TRUE, FALSE, NULL );
Dan Kegel's avatar
Dan Kegel committed
656 657 658 659 660 661 662 663
    alarmThread = CreateThread(NULL, 0, alarmThreadMain, (void *) 10000, 0, &alarmThreadId);

    /* The servers we're about to exercize do try to clean up carefully,
     * but to reduce the change of a test failure due to a pipe handle
     * leak in the test code, we'll use a different pipe name for each server.
     */

    /* Try server #1 */
664
    serverThread = CreateThread(NULL, 0, serverThreadMain1, (void *)8, 0, &serverThreadId);
665
    ok(serverThread != INVALID_HANDLE_VALUE, "CreateThread\n");
Dan Kegel's avatar
Dan Kegel committed
666 667 668 669
    exercizeServer(PIPENAME "serverThreadMain1", serverThread);

    /* Try server #2 */
    serverThread = CreateThread(NULL, 0, serverThreadMain2, 0, 0, &serverThreadId);
670
    ok(serverThread != INVALID_HANDLE_VALUE, "CreateThread\n");
Dan Kegel's avatar
Dan Kegel committed
671 672 673 674 675 676
    exercizeServer(PIPENAME "serverThreadMain2", serverThread);

    if( 0 ) /* overlapped pipe server doesn't work yet - it randomly fails */
    {
    /* Try server #3 */
    serverThread = CreateThread(NULL, 0, serverThreadMain3, 0, 0, &serverThreadId);
677
    ok(serverThread != INVALID_HANDLE_VALUE, "CreateThread\n");
Dan Kegel's avatar
Dan Kegel committed
678 679 680
    exercizeServer(PIPENAME "serverThreadMain3", serverThread);
    }

681 682
    ok(SetEvent( alarm_event ), "SetEvent\n");
    CloseHandle( alarm_event );
683
    trace("test_NamedPipe_2 returning\n");
Dan Kegel's avatar
Dan Kegel committed
684 685
}

686
static int test_DisconnectNamedPipe(void)
Dan Kegel's avatar
Dan Kegel committed
687 688 689
{
    HANDLE hnp;
    HANDLE hFile;
690
    static const char obuf[] = "Bit Bucket";
Dan Kegel's avatar
Dan Kegel committed
691 692 693 694 695 696 697 698 699 700
    char ibuf[32];
    DWORD written;
    DWORD readden;

    hnp = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
        /* nMaxInstances */ 1,
        /* nOutBufSize */ 1024,
        /* nInBufSize */ 1024,
        /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
        /* lpSecurityAttrib */ NULL);
701 702 703 704
    if (INVALID_HANDLE_VALUE == hnp) {
        trace ("Seems we have no named pipes.\n");
        return 1;
    }
Dan Kegel's avatar
Dan Kegel committed
705 706

    ok(WriteFile(hnp, obuf, sizeof(obuf), &written, NULL) == 0
707
        && GetLastError() == ERROR_PIPE_LISTENING, "WriteFile to not-yet-connected pipe\n");
Dan Kegel's avatar
Dan Kegel committed
708
    ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL) == 0
709
        && GetLastError() == ERROR_PIPE_LISTENING, "ReadFile from not-yet-connected pipe\n");
Dan Kegel's avatar
Dan Kegel committed
710 711

    hFile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
712
    ok(hFile != INVALID_HANDLE_VALUE, "CreateFile failed\n");
713 714 715

    /* don't try to do i/o if one side couldn't be opened, as it hangs */
    if (hFile != INVALID_HANDLE_VALUE) {
Dan Kegel's avatar
Dan Kegel committed
716 717 718 719 720

        /* see what happens if server calls DisconnectNamedPipe
         * when there are bytes in the pipe
         */

721 722 723
        ok(WriteFile(hFile, obuf, sizeof(obuf), &written, NULL), "WriteFile\n");
        ok(written == sizeof(obuf), "write file len\n");
        ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe while messages waiting\n");
Dan Kegel's avatar
Dan Kegel committed
724
        ok(WriteFile(hFile, obuf, sizeof(obuf), &written, NULL) == 0
725
            && GetLastError() == ERROR_PIPE_NOT_CONNECTED, "WriteFile to disconnected pipe\n");
Dan Kegel's avatar
Dan Kegel committed
726 727
        ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL) == 0
            && GetLastError() == ERROR_PIPE_NOT_CONNECTED,
728 729
            "ReadFile from disconnected pipe with bytes waiting\n");
        ok(CloseHandle(hFile), "CloseHandle\n");
730 731
    }

732
    ok(CloseHandle(hnp), "CloseHandle\n");
Dan Kegel's avatar
Dan Kegel committed
733

734
    return 0;
735
}
Uwe Bonnes's avatar
Uwe Bonnes committed
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
static void test_CreatePipe(void)
{
    SECURITY_ATTRIBUTES pipe_attr;
    HANDLE piperead, pipewrite;
    DWORD written;
    DWORD read;
    char readbuf[32];

    pipe_attr.nLength = sizeof(SECURITY_ATTRIBUTES); 
    pipe_attr.bInheritHandle = TRUE; 
    pipe_attr.lpSecurityDescriptor = NULL; 
    ok(CreatePipe(&piperead, &pipewrite, &pipe_attr, 0) != 0, "CreatePipe failed\n");
    ok(WriteFile(pipewrite,PIPENAME,sizeof(PIPENAME), &written, NULL), "Write to anonymous pipe failed\n");
    ok(written == sizeof(PIPENAME), "Write to anonymous pipe wrote %ld bytes instead of %d\n", written,sizeof(PIPENAME));
    ok(ReadFile(piperead,readbuf,sizeof(readbuf),&read, NULL), "Read from non empty pipe failed\n");
    ok(read == sizeof(PIPENAME), "Read from  anonymous pipe got %ld bytes instead of %d\n", read, sizeof(PIPENAME));

    /* Now write another chunk*/
    ok(CreatePipe(&piperead, &pipewrite, &pipe_attr, 0) != 0, "CreatePipe failed\n");
    ok(WriteFile(pipewrite,PIPENAME,sizeof(PIPENAME), &written, NULL), "Write to anonymous pipe failed\n");
    ok(written == sizeof(PIPENAME), "Write to anonymous pipe wrote %ld bytes instead of %d\n", written,sizeof(PIPENAME));
    /* and close the write end, read should still succeed*/
    ok(CloseHandle(pipewrite), "CloseHandle for the Write Pipe failed\n");
    ok(ReadFile(piperead,readbuf,sizeof(readbuf),&read, NULL), "Read from broken pipe withe with pending data failed\n");
    ok(read == sizeof(PIPENAME), "Read from  anonymous pipe got %ld bytes instead of %d\n", read, sizeof(PIPENAME));
    /* But now we need to get informed that the pipe is closed */
762
    ok(ReadFile(piperead,readbuf,sizeof(readbuf),&read, NULL) == 0, "Broken pipe not detected\n");
Uwe Bonnes's avatar
Uwe Bonnes committed
763
}
764 765 766

START_TEST(pipe)
{
Uwe Bonnes's avatar
Uwe Bonnes committed
767
    trace("test 1 of 6:\n");
768 769
    if (test_DisconnectNamedPipe())
        return;
Uwe Bonnes's avatar
Uwe Bonnes committed
770
    trace("test 2 of 6:\n");
Dan Kegel's avatar
Dan Kegel committed
771
    test_CreateNamedPipe_instances_must_match();
Uwe Bonnes's avatar
Uwe Bonnes committed
772
    trace("test 3 of 6:\n");
Dan Kegel's avatar
Dan Kegel committed
773
    test_NamedPipe_2();
Uwe Bonnes's avatar
Uwe Bonnes committed
774
    trace("test 4 of 6:\n");
775
    test_CreateNamedPipe(PIPE_TYPE_BYTE);
Uwe Bonnes's avatar
Uwe Bonnes committed
776
    trace("test 5 of 6\n");
777
    test_CreateNamedPipe(PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE);
Uwe Bonnes's avatar
Uwe Bonnes committed
778 779
    trace("test 6 of 6\n");
    test_CreatePipe();
780
    trace("all tests done\n");
781
}