2016-11-10 15:13:19 +00:00
|
|
|
/*
|
|
|
|
* PROJECT: ReactOS api tests
|
|
|
|
* LICENSE: GPL - See COPYING in the top level directory
|
|
|
|
* PURPOSE: Test for nonblocking sockets
|
2021-09-13 01:33:14 +00:00
|
|
|
* PROGRAMMERS: Peter Hater
|
2016-11-10 15:13:19 +00:00
|
|
|
*/
|
|
|
|
|
2017-12-21 12:36:04 +00:00
|
|
|
#include "ws2_32.h"
|
2016-11-10 15:13:19 +00:00
|
|
|
|
|
|
|
#define SVR_PORT 5000
|
|
|
|
#define WAIT_TIMEOUT_ 10000
|
|
|
|
#define EXIT_FLAGS (FD_ACCEPT|FD_CONNECT)
|
|
|
|
|
|
|
|
START_TEST(nonblocking)
|
|
|
|
{
|
|
|
|
WSADATA WsaData;
|
|
|
|
SOCKET ServerSocket = INVALID_SOCKET,
|
|
|
|
ClientSocket = INVALID_SOCKET;
|
|
|
|
struct sockaddr_in server_addr_in;
|
|
|
|
struct sockaddr_in addr_remote;
|
|
|
|
struct sockaddr_in addr_con_loc;
|
|
|
|
int nConRes, err;
|
|
|
|
int addrsize;
|
|
|
|
SOCKET sockaccept;
|
|
|
|
ULONG ulValue = 1;
|
|
|
|
DWORD dwFlags = 0, dwLen, dwAddrLen;
|
|
|
|
fd_set readfds, writefds, exceptfds;
|
|
|
|
struct timeval tval = { 0 };
|
|
|
|
char address[100];
|
|
|
|
|
2016-11-13 14:54:06 +00:00
|
|
|
if (!winetest_interactive)
|
|
|
|
{
|
|
|
|
skip("ROSTESTS-247: Skipping ws2_32_apitest:nonblocking because it times out on testbot\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-10 15:13:19 +00:00
|
|
|
if (WSAStartup(MAKEWORD(2, 2), &WsaData) != 0)
|
|
|
|
{
|
|
|
|
skip("WSAStartup failed\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ServerSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
|
|
|
ClientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
|
|
|
|
|
|
|
if (ServerSocket == INVALID_SOCKET)
|
|
|
|
{
|
|
|
|
skip("ERROR: Server socket creation failed\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (ClientSocket == INVALID_SOCKET)
|
|
|
|
{
|
|
|
|
skip("ERROR: Client socket creation failed\n");
|
|
|
|
closesocket(ServerSocket);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
server_addr_in.sin_family = AF_INET;
|
|
|
|
server_addr_in.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
server_addr_in.sin_port = htons(SVR_PORT);
|
|
|
|
|
2018-12-12 19:24:07 +00:00
|
|
|
// Server initialization.
|
2016-11-10 15:13:19 +00:00
|
|
|
trace("Initializing server and client connections ...\n");
|
|
|
|
err = bind(ServerSocket, (struct sockaddr*)&server_addr_in, sizeof(server_addr_in));
|
|
|
|
ok(err == 0, "ERROR: server bind failed\n");
|
|
|
|
err = ioctlsocket(ServerSocket, FIONBIO, &ulValue);
|
|
|
|
ok(err == 0, "ERROR: server ioctlsocket FIONBIO failed\n");
|
|
|
|
|
2018-12-12 19:24:07 +00:00
|
|
|
// Client initialization.
|
2016-11-10 15:13:19 +00:00
|
|
|
err = ioctlsocket(ClientSocket, FIONBIO, &ulValue);
|
|
|
|
ok(err == 0, "ERROR: client ioctlsocket FIONBIO failed\n");
|
|
|
|
|
|
|
|
// listen
|
|
|
|
trace("Starting server listening mode ...\n");
|
|
|
|
err = listen(ServerSocket, 2);
|
|
|
|
ok(err == 0, "ERROR: cannot initialize server listen\n");
|
|
|
|
|
|
|
|
trace("Starting client to server connection ...\n");
|
|
|
|
// connect
|
|
|
|
server_addr_in.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
|
|
|
server_addr_in.sin_port = htons(SVR_PORT);
|
|
|
|
nConRes = connect(ClientSocket, (struct sockaddr*)&server_addr_in, sizeof(server_addr_in));
|
|
|
|
ok(nConRes == SOCKET_ERROR, "ERROR: client connect() result is not SOCKET_ERROR\n");
|
|
|
|
ok(WSAGetLastError() == WSAEWOULDBLOCK, "ERROR: client connect() last error is not WSAEWOULDBLOCK\n");
|
|
|
|
FD_ZERO(&readfds);
|
|
|
|
FD_ZERO(&writefds);
|
|
|
|
FD_ZERO(&exceptfds);
|
|
|
|
FD_SET(ServerSocket, &readfds);
|
|
|
|
|
|
|
|
while (dwFlags != EXIT_FLAGS)
|
|
|
|
{
|
|
|
|
addrsize = sizeof(addr_con_loc);
|
|
|
|
err = getsockname(ClientSocket, (struct sockaddr*)&addr_con_loc, &addrsize);
|
|
|
|
if (err == 0)
|
|
|
|
{// client connected
|
|
|
|
dwLen = sizeof(addr_con_loc);
|
|
|
|
dwAddrLen = sizeof(address);
|
|
|
|
err = WSAAddressToStringA((PSOCKADDR)&addr_con_loc, dwLen, NULL, address, &dwAddrLen);
|
|
|
|
if (err == 0)
|
|
|
|
{
|
|
|
|
trace("Event FD_CONNECT...\n");
|
|
|
|
dwFlags |= FD_CONNECT;
|
|
|
|
err = recv(ClientSocket, address, dwAddrLen, 0);
|
|
|
|
ok(err == -1, "ERROR: error reading data from connected socket, error %d\n", WSAGetLastError());
|
|
|
|
ok(WSAGetLastError() == WSAEWOULDBLOCK, "ERROR: client connect() last error is not WSAEWOULDBLOCK\n");
|
|
|
|
err = send(ClientSocket, address, dwAddrLen, 0);
|
|
|
|
ok(err == dwAddrLen, "ERROR: error writing data to connected socket, error %d %d\n", err, WSAGetLastError());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
trace("WSAAddressToStringA failed %d\n", WSAGetLastError());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = select(1, &readfds, &writefds, &exceptfds, &tval);
|
|
|
|
if (err == 1 && FD_ISSET(ServerSocket, &readfds))
|
|
|
|
{// connection ready to be accepted
|
|
|
|
trace("Event FD_ACCEPT...\n");
|
|
|
|
addrsize = sizeof(addr_remote);
|
|
|
|
sockaccept = accept(ServerSocket, (struct sockaddr*)&addr_remote, &addrsize);
|
|
|
|
ok(sockaccept != INVALID_SOCKET, "ERROR: Connection accept function failed, error %d\n", WSAGetLastError());
|
|
|
|
dwFlags |= FD_ACCEPT;
|
|
|
|
dwLen = sizeof(addr_remote);
|
|
|
|
dwAddrLen = sizeof(address);
|
|
|
|
err = WSAAddressToStringA((PSOCKADDR)&addr_remote, dwLen, NULL, address, &dwAddrLen);
|
|
|
|
ok(err == 0, "WSAAddressToStringA, error %d\n", WSAGetLastError());
|
|
|
|
ok(dwAddrLen > 7, "len <= 7\n");
|
|
|
|
err = send(sockaccept, address, dwAddrLen, 0);
|
|
|
|
ok(err == dwAddrLen, "ERROR: error sending data on accepted socket, error %d\n", WSAGetLastError());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
closesocket(sockaccept);
|
|
|
|
closesocket(ServerSocket);
|
|
|
|
closesocket(ClientSocket);
|
|
|
|
|
|
|
|
WSACleanup();
|
|
|
|
}
|