2003-02-19 23:06:36 +01:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2003-09-06 01:08:26 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdarg.h>
|
2003-02-19 23:06:36 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <time.h>
|
2003-09-06 01:08:26 +02:00
|
|
|
|
|
|
|
#include <windef.h>
|
|
|
|
#include <winbase.h>
|
|
|
|
#include <winsock.h>
|
2003-02-19 23:06:36 +01:00
|
|
|
|
|
|
|
#ifndef STANDALONE
|
|
|
|
#include "wine/test.h"
|
|
|
|
#else
|
|
|
|
#include <assert.h>
|
|
|
|
#define START_TEST(name) main(int argc, char **argv)
|
2003-05-16 00:52:41 +02:00
|
|
|
#define ok(condition, msg) \
|
|
|
|
do { \
|
|
|
|
if(!(condition)) \
|
|
|
|
{ \
|
|
|
|
fprintf(stderr,"failed at %d\n",__LINE__); \
|
|
|
|
exit(0); \
|
|
|
|
} \
|
|
|
|
} while(0)
|
|
|
|
|
2003-02-25 04:56:43 +01:00
|
|
|
#define todo_wine
|
2003-02-19 23:06:36 +01:00
|
|
|
#endif
|
|
|
|
|
2003-02-25 04:56:43 +01:00
|
|
|
#include <wtypes.h>
|
2003-02-19 23:06:36 +01:00
|
|
|
#include <winerror.h>
|
|
|
|
|
|
|
|
#define PIPENAME "\\\\.\\PiPe\\tests_" __FILE__
|
|
|
|
|
2003-05-16 00:52:41 +02:00
|
|
|
void test_CreateNamedPipe(void)
|
2003-02-19 23:06:36 +01:00
|
|
|
{
|
|
|
|
HANDLE hnp;
|
|
|
|
HANDLE hFile;
|
|
|
|
const char obuf[] = "Bit Bucket";
|
|
|
|
char ibuf[32];
|
|
|
|
DWORD written;
|
2003-05-16 00:52:41 +02:00
|
|
|
DWORD readden;
|
2003-02-19 23:06:36 +01:00
|
|
|
|
2003-05-20 05:58:35 +02:00
|
|
|
trace("test_CreateNamedPipe starting\n");
|
2003-02-19 23:06:36 +01:00
|
|
|
/* Bad parameter checks */
|
2003-05-16 00:52:41 +02:00
|
|
|
hnp = CreateNamedPipe("not a named pipe", PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
|
2003-02-19 23:06:36 +01:00
|
|
|
/* 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? */
|
2003-05-16 00:52:41 +02:00
|
|
|
ok(hnp == INVALID_HANDLE_VALUE && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED,
|
|
|
|
"CreateNamedPipe not supported on this platform, skipping tests.");
|
2003-02-19 23:06:36 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
ok(hnp == INVALID_HANDLE_VALUE && GetLastError() == ERROR_INVALID_NAME,
|
|
|
|
"CreateNamedPipe should fail if name doesn't start with \\\\.\\pipe");
|
|
|
|
|
2003-05-16 00:52:41 +02:00
|
|
|
hnp = CreateNamedPipe(NULL,
|
|
|
|
PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
|
2003-02-19 23:06:36 +01:00
|
|
|
1, 1024, 1024, NMPWAIT_USE_DEFAULT_WAIT, NULL);
|
|
|
|
ok(hnp == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
|
|
|
|
"CreateNamedPipe should fail if name is NULL");
|
|
|
|
|
2003-05-16 00:52:41 +02:00
|
|
|
hFile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
|
|
|
|
ok(hFile == INVALID_HANDLE_VALUE
|
|
|
|
&& GetLastError() == ERROR_FILE_NOT_FOUND,
|
|
|
|
"connecting to nonexistent named pipe should fail with ERROR_FILE_NOT_FOUND");
|
2003-02-25 04:56:43 +01:00
|
|
|
|
2003-02-19 23:06:36 +01:00
|
|
|
/* Functional checks */
|
|
|
|
|
2003-05-16 00:52:41 +02:00
|
|
|
hnp = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
|
|
|
|
/* nMaxInstances */ 1,
|
|
|
|
/* nOutBufSize */ 1024,
|
|
|
|
/* nInBufSize */ 1024,
|
|
|
|
/* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
|
|
|
|
/* lpSecurityAttrib */ NULL);
|
|
|
|
ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed");
|
|
|
|
|
|
|
|
hFile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
|
|
|
|
ok(hFile != INVALID_HANDLE_VALUE, "CreateFile failed");
|
|
|
|
|
|
|
|
/* 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));
|
|
|
|
ok(WriteFile(hnp, obuf, sizeof(obuf), &written, NULL), "WriteFile");
|
|
|
|
ok(written == sizeof(obuf), "write file len");
|
|
|
|
ok(ReadFile(hFile, ibuf, sizeof(obuf), &readden, NULL), "ReadFile");
|
|
|
|
ok(readden == sizeof(obuf), "read file len");
|
|
|
|
ok(memcmp(obuf, ibuf, written) == 0, "content check");
|
|
|
|
|
|
|
|
memset(ibuf, 0, sizeof(ibuf));
|
|
|
|
ok(WriteFile(hFile, obuf, sizeof(obuf), &written, NULL), "WriteFile");
|
|
|
|
ok(written == sizeof(obuf), "write file len");
|
|
|
|
ok(ReadFile(hnp, ibuf, sizeof(obuf), &readden, NULL), "ReadFile");
|
|
|
|
ok(readden == sizeof(obuf), "read file len");
|
|
|
|
ok(memcmp(obuf, ibuf, written) == 0, "content check");
|
|
|
|
|
|
|
|
/* 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,
|
|
|
|
"connecting to named pipe after other client closes but before DisconnectNamedPipe should fail");
|
|
|
|
ok(GetLastError() == ERROR_PIPE_BUSY,
|
|
|
|
"connecting to named pipe before other client closes should fail with ERROR_PIPE_BUSY");
|
|
|
|
|
|
|
|
ok(CloseHandle(hFile), "CloseHandle");
|
|
|
|
|
|
|
|
/* case 2: other client already closed */
|
|
|
|
hFile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
|
|
|
|
ok(hFile == INVALID_HANDLE_VALUE,
|
|
|
|
"connecting to named pipe after other client closes but before DisconnectNamedPipe should fail");
|
|
|
|
ok(GetLastError() == ERROR_PIPE_BUSY,
|
|
|
|
"connecting to named pipe after other client closes but before DisconnectNamedPipe should fail with ERROR_PIPE_BUSY");
|
|
|
|
|
|
|
|
ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe");
|
|
|
|
|
|
|
|
/* 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,
|
|
|
|
"connecting to named pipe after other client closes but before DisconnectNamedPipe should fail");
|
|
|
|
ok(GetLastError() == ERROR_PIPE_BUSY,
|
|
|
|
"connecting to named pipe after other client closes but before ConnectNamedPipe should fail with ERROR_PIPE_BUSY");
|
|
|
|
|
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
ok(CloseHandle(hnp), "CloseHandle");
|
|
|
|
|
2003-05-20 05:58:35 +02:00
|
|
|
trace("test_CreateNamedPipe returning\n");
|
2003-05-16 00:52:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_CreateNamedPipe_instances_must_match(void)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed");
|
|
|
|
|
|
|
|
hnp2 = CreateNamedPipe(PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT,
|
|
|
|
/* nMaxInstances */ 2,
|
|
|
|
/* nOutBufSize */ 1024,
|
|
|
|
/* nInBufSize */ 1024,
|
|
|
|
/* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
|
|
|
|
/* lpSecurityAttrib */ NULL);
|
|
|
|
ok(hnp2 != INVALID_HANDLE_VALUE, "CreateNamedPipe failed");
|
|
|
|
|
|
|
|
ok(CloseHandle(hnp), "CloseHandle");
|
|
|
|
ok(CloseHandle(hnp2), "CloseHandle");
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed");
|
|
|
|
|
|
|
|
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
|
|
|
|
&& GetLastError() == ERROR_PIPE_BUSY, "nMaxInstances not obeyed");
|
|
|
|
|
|
|
|
ok(CloseHandle(hnp), "CloseHandle");
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed");
|
|
|
|
|
|
|
|
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
|
|
|
|
&& GetLastError() == ERROR_ACCESS_DENIED, "PIPE_ACCESS_* mismatch allowed");
|
|
|
|
|
|
|
|
ok(CloseHandle(hnp), "CloseHandle");
|
|
|
|
|
|
|
|
/* etc, etc */
|
|
|
|
}
|
|
|
|
|
|
|
|
/** implementation of alarm() */
|
|
|
|
static DWORD CALLBACK alarmThreadMain(LPVOID arg)
|
|
|
|
{
|
|
|
|
DWORD timeout = (DWORD) arg;
|
2003-05-20 05:58:35 +02:00
|
|
|
trace("alarmThreadMain\n");
|
2003-05-16 00:52:41 +02:00
|
|
|
Sleep(timeout);
|
|
|
|
ok(FALSE, "alarm");
|
|
|
|
ExitProcess(1);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE hnp = INVALID_HANDLE_VALUE;
|
|
|
|
|
|
|
|
/** Trivial byte echo server - disconnects after each session */
|
|
|
|
static DWORD CALLBACK serverThreadMain1(LPVOID arg)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2003-05-20 05:58:35 +02:00
|
|
|
trace("serverThreadMain1 start\n");
|
2003-05-16 00:52:41 +02:00
|
|
|
/* 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);
|
|
|
|
|
|
|
|
ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed");
|
|
|
|
for (i = 0; ; i++) {
|
|
|
|
char buf[512];
|
|
|
|
DWORD written;
|
|
|
|
DWORD readden;
|
|
|
|
DWORD success;
|
|
|
|
|
|
|
|
/* Wait for client to connect */
|
2003-05-20 05:58:35 +02:00
|
|
|
trace("Server calling ConnectNamedPipe...\n");
|
2003-05-16 00:52:41 +02:00
|
|
|
ok(ConnectNamedPipe(hnp, NULL)
|
|
|
|
|| GetLastError() == ERROR_PIPE_CONNECTED, "ConnectNamedPipe");
|
2003-05-20 05:58:35 +02:00
|
|
|
trace("ConnectNamedPipe returned.\n");
|
2003-05-16 00:52:41 +02:00
|
|
|
|
|
|
|
/* Echo bytes once */
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
|
2003-05-20 05:58:35 +02:00
|
|
|
trace("Server reading...\n");
|
2003-05-16 00:52:41 +02:00
|
|
|
success = ReadFile(hnp, buf, sizeof(buf), &readden, NULL);
|
2003-05-20 05:58:35 +02:00
|
|
|
trace("Server done reading.\n");
|
2003-05-16 00:52:41 +02:00
|
|
|
ok(success, "ReadFile");
|
|
|
|
ok(readden, "short read");
|
|
|
|
|
2003-05-20 05:58:35 +02:00
|
|
|
trace("Server writing...\n");
|
2003-05-16 00:52:41 +02:00
|
|
|
ok(WriteFile(hnp, buf, readden, &written, NULL), "WriteFile");
|
2003-05-20 05:58:35 +02:00
|
|
|
trace("Server done writing.\n");
|
2003-05-16 00:52:41 +02:00
|
|
|
ok(written == readden, "write file len");
|
|
|
|
|
|
|
|
/* finish this connection, wait for next one */
|
|
|
|
ok(FlushFileBuffers(hnp), "FlushFileBuffers");
|
2003-05-20 05:58:35 +02:00
|
|
|
trace("Server done flushing.\n");
|
2003-05-16 00:52:41 +02:00
|
|
|
ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe");
|
2003-05-20 05:58:35 +02:00
|
|
|
trace("Server done disconnecting.\n");
|
2003-05-16 00:52:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Trivial byte echo server - closes after each connection */
|
|
|
|
static DWORD CALLBACK serverThreadMain2(LPVOID arg)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
HANDLE hnpNext = 0;
|
|
|
|
|
2003-05-20 05:58:35 +02:00
|
|
|
trace("serverThreadMain2\n");
|
2003-05-16 00:52:41 +02:00
|
|
|
/* 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);
|
|
|
|
ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed");
|
|
|
|
|
|
|
|
for (i = 0; ; i++) {
|
|
|
|
char buf[512];
|
|
|
|
DWORD written;
|
|
|
|
DWORD readden;
|
|
|
|
DWORD success;
|
|
|
|
|
|
|
|
/* Wait for client to connect */
|
2003-05-20 05:58:35 +02:00
|
|
|
trace("Server calling ConnectNamedPipe...\n");
|
2003-05-16 00:52:41 +02:00
|
|
|
ok(ConnectNamedPipe(hnp, NULL)
|
|
|
|
|| GetLastError() == ERROR_PIPE_CONNECTED, "ConnectNamedPipe");
|
2003-05-20 05:58:35 +02:00
|
|
|
trace("ConnectNamedPipe returned.\n");
|
2003-05-16 00:52:41 +02:00
|
|
|
|
|
|
|
/* Echo bytes once */
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
|
2003-05-20 05:58:35 +02:00
|
|
|
trace("Server reading...\n");
|
2003-05-16 00:52:41 +02:00
|
|
|
success = ReadFile(hnp, buf, sizeof(buf), &readden, NULL);
|
2003-05-20 05:58:35 +02:00
|
|
|
trace("Server done reading.\n");
|
2003-05-16 00:52:41 +02:00
|
|
|
ok(success, "ReadFile");
|
|
|
|
|
2003-05-20 05:58:35 +02:00
|
|
|
trace("Server writing...\n");
|
2003-05-16 00:52:41 +02:00
|
|
|
ok(WriteFile(hnp, buf, readden, &written, NULL), "WriteFile");
|
2003-05-20 05:58:35 +02:00
|
|
|
trace("Server done writing.\n");
|
2003-05-16 00:52:41 +02:00
|
|
|
ok(written == readden, "write file len");
|
|
|
|
|
|
|
|
/* finish this connection, wait for next one */
|
|
|
|
ok(FlushFileBuffers(hnp), "FlushFileBuffers");
|
|
|
|
ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe");
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
|
|
|
ok(hnpNext != INVALID_HANDLE_VALUE, "CreateNamedPipe failed");
|
|
|
|
|
|
|
|
ok(CloseHandle(hnp), "CloseHandle");
|
|
|
|
hnp = hnpNext;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Trivial byte echo server - uses overlapped named pipe calls */
|
|
|
|
static DWORD CALLBACK serverThreadMain3(LPVOID arg)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
HANDLE hEvent;
|
|
|
|
|
2003-05-20 05:58:35 +02:00
|
|
|
trace("serverThreadMain3\n");
|
2003-05-16 00:52:41 +02:00
|
|
|
/* Set up a simple echo server */
|
|
|
|
hnp = CreateNamedPipe(PIPENAME "serverThreadMain3", PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
|
|
|
|
PIPE_TYPE_BYTE | PIPE_WAIT,
|
2003-02-19 23:06:36 +01:00
|
|
|
/* nMaxInstances */ 1,
|
|
|
|
/* nOutBufSize */ 1024,
|
|
|
|
/* nInBufSize */ 1024,
|
|
|
|
/* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
|
|
|
|
/* lpSecurityAttrib */ NULL);
|
|
|
|
ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed");
|
|
|
|
|
2003-05-16 00:52:41 +02:00
|
|
|
hEvent = CreateEvent(NULL, /* security attribute */
|
|
|
|
TRUE, /* manual reset event */
|
|
|
|
FALSE, /* initial state */
|
|
|
|
NULL); /* name */
|
|
|
|
ok(hEvent != NULL, "CreateEvent");
|
|
|
|
|
|
|
|
for (i = 0; ; i++) {
|
|
|
|
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 */
|
2003-05-20 05:58:35 +02:00
|
|
|
trace("Server calling overlapped ConnectNamedPipe...\n");
|
2003-05-16 00:52:41 +02:00
|
|
|
success = ConnectNamedPipe(hnp, &oOverlap);
|
|
|
|
err = GetLastError();
|
|
|
|
ok(success || err == ERROR_IO_PENDING
|
|
|
|
|| err == ERROR_PIPE_CONNECTED, "overlapped ConnectNamedPipe");
|
2003-05-20 05:58:35 +02:00
|
|
|
trace("overlapped ConnectNamedPipe returned.\n");
|
2003-05-16 00:52:41 +02:00
|
|
|
if (!success && (err == ERROR_IO_PENDING) && letWFSOEwait)
|
|
|
|
ok(WaitForSingleObjectEx(hEvent, INFINITE, TRUE) == 0, "wait ConnectNamedPipe");
|
|
|
|
success = GetOverlappedResult(hnp, &oOverlap, &dummy, letGORwait);
|
|
|
|
if (!letGORwait && !letWFSOEwait && !success) {
|
|
|
|
ok(GetLastError() == ERROR_IO_INCOMPLETE, "GetOverlappedResult");
|
|
|
|
success = GetOverlappedResult(hnp, &oOverlap, &dummy, TRUE);
|
|
|
|
}
|
|
|
|
ok(success, "GetOverlappedResult ConnectNamedPipe");
|
2003-05-20 05:58:35 +02:00
|
|
|
trace("overlapped ConnectNamedPipe operation complete.\n");
|
2003-05-16 00:52:41 +02:00
|
|
|
|
|
|
|
/* Echo bytes once */
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
|
2003-05-20 05:58:35 +02:00
|
|
|
trace("Server reading...\n");
|
2003-05-16 00:52:41 +02:00
|
|
|
success = ReadFile(hnp, buf, sizeof(buf), NULL, &oOverlap);
|
2003-05-20 05:58:35 +02:00
|
|
|
trace("Server ReadFile returned...\n");
|
2003-05-16 00:52:41 +02:00
|
|
|
err = GetLastError();
|
|
|
|
ok(success || err == ERROR_IO_PENDING, "overlapped ReadFile");
|
2003-05-20 05:58:35 +02:00
|
|
|
trace("overlapped ReadFile returned.\n");
|
2003-05-16 00:52:41 +02:00
|
|
|
if (!success && (err == ERROR_IO_PENDING) && letWFSOEwait)
|
|
|
|
ok(WaitForSingleObjectEx(hEvent, INFINITE, TRUE) == 0, "wait ReadFile");
|
|
|
|
success = GetOverlappedResult(hnp, &oOverlap, &readden, letGORwait);
|
|
|
|
if (!letGORwait && !letWFSOEwait && !success) {
|
|
|
|
ok(GetLastError() == ERROR_IO_INCOMPLETE, "GetOverlappedResult");
|
|
|
|
success = GetOverlappedResult(hnp, &oOverlap, &readden, TRUE);
|
|
|
|
}
|
2003-05-20 05:58:35 +02:00
|
|
|
trace("Server done reading.\n");
|
2003-05-16 00:52:41 +02:00
|
|
|
ok(success, "overlapped ReadFile");
|
|
|
|
|
2003-05-20 05:58:35 +02:00
|
|
|
trace("Server writing...\n");
|
2003-05-16 00:52:41 +02:00
|
|
|
success = WriteFile(hnp, buf, readden, NULL, &oOverlap);
|
2003-05-20 05:58:35 +02:00
|
|
|
trace("Server WriteFile returned...\n");
|
2003-05-16 00:52:41 +02:00
|
|
|
err = GetLastError();
|
|
|
|
ok(success || err == ERROR_IO_PENDING, "overlapped WriteFile");
|
2003-05-20 05:58:35 +02:00
|
|
|
trace("overlapped WriteFile returned.\n");
|
2003-05-16 00:52:41 +02:00
|
|
|
if (!success && (err == ERROR_IO_PENDING) && letWFSOEwait)
|
|
|
|
ok(WaitForSingleObjectEx(hEvent, INFINITE, TRUE) == 0, "wait WriteFile");
|
|
|
|
success = GetOverlappedResult(hnp, &oOverlap, &written, letGORwait);
|
|
|
|
if (!letGORwait && !letWFSOEwait && !success) {
|
|
|
|
ok(GetLastError() == ERROR_IO_INCOMPLETE, "GetOverlappedResult");
|
|
|
|
success = GetOverlappedResult(hnp, &oOverlap, &written, TRUE);
|
|
|
|
}
|
2003-05-20 05:58:35 +02:00
|
|
|
trace("Server done writing.\n");
|
2003-05-16 00:52:41 +02:00
|
|
|
ok(success, "overlapped WriteFile");
|
|
|
|
ok(written == readden, "write file len");
|
|
|
|
|
|
|
|
/* finish this connection, wait for next one */
|
|
|
|
ok(FlushFileBuffers(hnp), "FlushFileBuffers");
|
|
|
|
ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void exercizeServer(const char *pipename, HANDLE serverThread)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2003-05-20 05:58:35 +02:00
|
|
|
trace("exercizeServer starting\n");
|
2003-05-16 00:52:41 +02:00
|
|
|
for (i = 0; i < 8; i++) {
|
2003-05-21 20:23:20 +02:00
|
|
|
HANDLE hFile=INVALID_HANDLE_VALUE;
|
2003-05-16 00:52:41 +02:00
|
|
|
const char obuf[] = "Bit Bucket";
|
|
|
|
char ibuf[32];
|
|
|
|
DWORD written;
|
|
|
|
DWORD readden;
|
|
|
|
int loop;
|
|
|
|
|
|
|
|
for (loop = 0; loop < 3; loop++) {
|
|
|
|
DWORD err;
|
2003-05-20 05:58:35 +02:00
|
|
|
trace("Client connecting...\n");
|
2003-05-16 00:52:41 +02:00
|
|
|
/* 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)
|
|
|
|
ok(err == ERROR_PIPE_BUSY || err == ERROR_FILE_NOT_FOUND, "connecting to pipe");
|
|
|
|
else
|
|
|
|
ok(err == ERROR_PIPE_BUSY, "connecting to pipe");
|
2003-05-20 05:58:35 +02:00
|
|
|
trace("connect failed, retrying\n");
|
2003-05-16 00:52:41 +02:00
|
|
|
Sleep(200);
|
|
|
|
}
|
|
|
|
ok(hFile != INVALID_HANDLE_VALUE, "client opening named pipe");
|
|
|
|
|
|
|
|
/* Make sure it can echo */
|
|
|
|
memset(ibuf, 0, sizeof(ibuf));
|
2003-05-20 05:58:35 +02:00
|
|
|
trace("Client writing...\n");
|
2003-05-16 00:52:41 +02:00
|
|
|
ok(WriteFile(hFile, obuf, sizeof(obuf), &written, NULL), "WriteFile to client end of pipe");
|
|
|
|
ok(written == sizeof(obuf), "write file len");
|
2003-05-20 05:58:35 +02:00
|
|
|
trace("Client reading...\n");
|
2003-05-16 00:52:41 +02:00
|
|
|
ok(ReadFile(hFile, ibuf, sizeof(obuf), &readden, NULL), "ReadFile from client end of pipe");
|
|
|
|
ok(readden == sizeof(obuf), "read file len");
|
|
|
|
ok(memcmp(obuf, ibuf, written) == 0, "content check");
|
|
|
|
|
2003-05-20 05:58:35 +02:00
|
|
|
trace("Client closing...\n");
|
2003-05-16 00:52:41 +02:00
|
|
|
ok(CloseHandle(hFile), "CloseHandle");
|
|
|
|
}
|
|
|
|
|
|
|
|
ok(TerminateThread(serverThread, 0), "TerminateThread");
|
|
|
|
CloseHandle(hnp);
|
2003-05-20 05:58:35 +02:00
|
|
|
trace("exercizeServer returning\n");
|
2003-05-16 00:52:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_NamedPipe_2(void)
|
|
|
|
{
|
|
|
|
HANDLE serverThread;
|
|
|
|
DWORD serverThreadId;
|
|
|
|
HANDLE alarmThread;
|
|
|
|
DWORD alarmThreadId;
|
|
|
|
|
2003-05-20 05:58:35 +02:00
|
|
|
trace("test_NamedPipe_2 starting\n");
|
2003-05-16 00:52:41 +02:00
|
|
|
/* Set up a ten second timeout */
|
|
|
|
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 */
|
|
|
|
serverThread = CreateThread(NULL, 0, serverThreadMain1, 0, 0, &serverThreadId);
|
|
|
|
ok(serverThread != INVALID_HANDLE_VALUE, "CreateThread");
|
|
|
|
exercizeServer(PIPENAME "serverThreadMain1", serverThread);
|
|
|
|
|
|
|
|
/* Try server #2 */
|
|
|
|
serverThread = CreateThread(NULL, 0, serverThreadMain2, 0, 0, &serverThreadId);
|
|
|
|
ok(serverThread != INVALID_HANDLE_VALUE, "CreateThread");
|
|
|
|
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);
|
|
|
|
ok(serverThread != INVALID_HANDLE_VALUE, "CreateThread");
|
|
|
|
exercizeServer(PIPENAME "serverThreadMain3", serverThread);
|
|
|
|
}
|
|
|
|
|
|
|
|
ok(TerminateThread(alarmThread, 0), "TerminateThread");
|
2003-05-20 05:58:35 +02:00
|
|
|
trace("test_NamedPipe_2 returning\n");
|
2003-05-16 00:52:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_DisconnectNamedPipe(void)
|
|
|
|
{
|
|
|
|
HANDLE hnp;
|
|
|
|
HANDLE hFile;
|
|
|
|
const char obuf[] = "Bit Bucket";
|
|
|
|
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);
|
|
|
|
ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed");
|
|
|
|
|
|
|
|
ok(WriteFile(hnp, obuf, sizeof(obuf), &written, NULL) == 0
|
|
|
|
&& GetLastError() == ERROR_PIPE_LISTENING, "WriteFile to not-yet-connected pipe");
|
|
|
|
ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL) == 0
|
|
|
|
&& GetLastError() == ERROR_PIPE_LISTENING, "ReadFile from not-yet-connected pipe");
|
|
|
|
|
|
|
|
hFile = CreateFileA(PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
|
2003-05-15 06:22:45 +02:00
|
|
|
ok(hFile != INVALID_HANDLE_VALUE, "CreateFile failed");
|
2003-02-19 23:06:36 +01:00
|
|
|
|
|
|
|
/* don't try to do i/o if one side couldn't be opened, as it hangs */
|
|
|
|
if (hFile != INVALID_HANDLE_VALUE) {
|
2003-05-16 00:52:41 +02:00
|
|
|
|
|
|
|
/* see what happens if server calls DisconnectNamedPipe
|
|
|
|
* when there are bytes in the pipe
|
|
|
|
*/
|
|
|
|
|
|
|
|
ok(WriteFile(hFile, obuf, sizeof(obuf), &written, NULL), "WriteFile");
|
|
|
|
ok(written == sizeof(obuf), "write file len");
|
|
|
|
ok(DisconnectNamedPipe(hnp), "DisconnectNamedPipe while messages waiting");
|
|
|
|
ok(WriteFile(hFile, obuf, sizeof(obuf), &written, NULL) == 0
|
|
|
|
&& GetLastError() == ERROR_PIPE_NOT_CONNECTED, "WriteFile to disconnected pipe");
|
|
|
|
ok(ReadFile(hnp, ibuf, sizeof(ibuf), &readden, NULL) == 0
|
|
|
|
&& GetLastError() == ERROR_PIPE_NOT_CONNECTED,
|
|
|
|
"ReadFile from disconnected pipe with bytes waiting");
|
|
|
|
ok(CloseHandle(hFile), "CloseHandle");
|
2003-02-19 23:06:36 +01:00
|
|
|
}
|
|
|
|
|
2003-02-25 04:56:43 +01:00
|
|
|
ok(CloseHandle(hnp), "CloseHandle");
|
2003-05-16 00:52:41 +02:00
|
|
|
|
2003-02-19 23:06:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
START_TEST(pipe)
|
|
|
|
{
|
2003-05-20 05:58:35 +02:00
|
|
|
trace("test 1 of 4:\n");
|
2003-05-16 00:52:41 +02:00
|
|
|
test_DisconnectNamedPipe();
|
2003-05-20 05:58:35 +02:00
|
|
|
trace("test 2 of 4:\n");
|
2003-05-16 00:52:41 +02:00
|
|
|
test_CreateNamedPipe_instances_must_match();
|
2003-05-20 05:58:35 +02:00
|
|
|
trace("test 3 of 4:\n");
|
2003-05-16 00:52:41 +02:00
|
|
|
test_NamedPipe_2();
|
2003-05-20 05:58:35 +02:00
|
|
|
trace("test 4 of 4:\n");
|
2003-05-16 00:52:41 +02:00
|
|
|
test_CreateNamedPipe();
|
2003-05-20 05:58:35 +02:00
|
|
|
trace("all tests done\n");
|
2003-02-19 23:06:36 +01:00
|
|
|
}
|