ReactOS  0.4.15-dev-489-g75a0787
nonblocking.c
Go to the documentation of this file.
1 /*
2 * PROJECT: ReactOS api tests
3 * LICENSE: GPL - See COPYING in the top level directory
4 * PURPOSE: Test for nonblocking sockets
5 * PROGRAMMERS: Peter Hater
6 */
7 
8 #include "ws2_32.h"
9 
10 #define SVR_PORT 5000
11 #define WAIT_TIMEOUT_ 10000
12 #define EXIT_FLAGS (FD_ACCEPT|FD_CONNECT)
13 
14 START_TEST(nonblocking)
15 {
16  WSADATA WsaData;
18  ClientSocket = INVALID_SOCKET;
19  struct sockaddr_in server_addr_in;
20  struct sockaddr_in addr_remote;
21  struct sockaddr_in addr_con_loc;
22  int nConRes, err;
23  int addrsize;
24  SOCKET sockaccept;
25  ULONG ulValue = 1;
26  DWORD dwFlags = 0, dwLen, dwAddrLen;
27  fd_set readfds, writefds, exceptfds;
28  struct timeval tval = { 0 };
29  char address[100];
30 
32  {
33  skip("ROSTESTS-247: Skipping ws2_32_apitest:nonblocking because it times out on testbot\n");
34  return;
35  }
36 
37  if (WSAStartup(MAKEWORD(2, 2), &WsaData) != 0)
38  {
39  skip("WSAStartup failed\n");
40  return;
41  }
42 
44  ClientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
45 
47  {
48  skip("ERROR: Server socket creation failed\n");
49  return;
50  }
51  if (ClientSocket == INVALID_SOCKET)
52  {
53  skip("ERROR: Client socket creation failed\n");
55  return;
56  }
57  server_addr_in.sin_family = AF_INET;
58  server_addr_in.sin_addr.s_addr = INADDR_ANY;
59  server_addr_in.sin_port = htons(SVR_PORT);
60 
61  // Server initialization.
62  trace("Initializing server and client connections ...\n");
63  err = bind(ServerSocket, (struct sockaddr*)&server_addr_in, sizeof(server_addr_in));
64  ok(err == 0, "ERROR: server bind failed\n");
65  err = ioctlsocket(ServerSocket, FIONBIO, &ulValue);
66  ok(err == 0, "ERROR: server ioctlsocket FIONBIO failed\n");
67 
68  // Client initialization.
69  err = ioctlsocket(ClientSocket, FIONBIO, &ulValue);
70  ok(err == 0, "ERROR: client ioctlsocket FIONBIO failed\n");
71 
72  // listen
73  trace("Starting server listening mode ...\n");
74  err = listen(ServerSocket, 2);
75  ok(err == 0, "ERROR: cannot initialize server listen\n");
76 
77  trace("Starting client to server connection ...\n");
78  // connect
79  server_addr_in.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
80  server_addr_in.sin_port = htons(SVR_PORT);
81  nConRes = connect(ClientSocket, (struct sockaddr*)&server_addr_in, sizeof(server_addr_in));
82  ok(nConRes == SOCKET_ERROR, "ERROR: client connect() result is not SOCKET_ERROR\n");
83  ok(WSAGetLastError() == WSAEWOULDBLOCK, "ERROR: client connect() last error is not WSAEWOULDBLOCK\n");
84  FD_ZERO(&readfds);
85  FD_ZERO(&writefds);
86  FD_ZERO(&exceptfds);
87  FD_SET(ServerSocket, &readfds);
88 
89  while (dwFlags != EXIT_FLAGS)
90  {
91  addrsize = sizeof(addr_con_loc);
92  err = getsockname(ClientSocket, (struct sockaddr*)&addr_con_loc, &addrsize);
93  if (err == 0)
94  {// client connected
95  dwLen = sizeof(addr_con_loc);
96  dwAddrLen = sizeof(address);
97  err = WSAAddressToStringA((PSOCKADDR)&addr_con_loc, dwLen, NULL, address, &dwAddrLen);
98  if (err == 0)
99  {
100  trace("Event FD_CONNECT...\n");
101  dwFlags |= FD_CONNECT;
102  err = recv(ClientSocket, address, dwAddrLen, 0);
103  ok(err == -1, "ERROR: error reading data from connected socket, error %d\n", WSAGetLastError());
104  ok(WSAGetLastError() == WSAEWOULDBLOCK, "ERROR: client connect() last error is not WSAEWOULDBLOCK\n");
105  err = send(ClientSocket, address, dwAddrLen, 0);
106  ok(err == dwAddrLen, "ERROR: error writing data to connected socket, error %d %d\n", err, WSAGetLastError());
107  }
108  else
109  {
110  trace("WSAAddressToStringA failed %d\n", WSAGetLastError());
111  }
112  }
113 
114  err = select(1, &readfds, &writefds, &exceptfds, &tval);
115  if (err == 1 && FD_ISSET(ServerSocket, &readfds))
116  {// connection ready to be accepted
117  trace("Event FD_ACCEPT...\n");
118  addrsize = sizeof(addr_remote);
119  sockaccept = accept(ServerSocket, (struct sockaddr*)&addr_remote, &addrsize);
120  ok(sockaccept != INVALID_SOCKET, "ERROR: Connection accept function failed, error %d\n", WSAGetLastError());
121  dwFlags |= FD_ACCEPT;
122  dwLen = sizeof(addr_remote);
123  dwAddrLen = sizeof(address);
124  err = WSAAddressToStringA((PSOCKADDR)&addr_remote, dwLen, NULL, address, &dwAddrLen);
125  ok(err == 0, "WSAAddressToStringA, error %d\n", WSAGetLastError());
126  ok(dwAddrLen > 7, "len <= 7\n");
127  err = send(sockaccept, address, dwAddrLen, 0);
128  ok(err == dwAddrLen, "ERROR: error sending data on accepted socket, error %d\n", WSAGetLastError());
129  }
130  }
131 
132  closesocket(sockaccept);
134  closesocket(ClientSocket);
135 
136  WSACleanup();
137 }
Definition: winsock.h:66
#define SOCKET_ERROR
Definition: winsock.h:333
SOCKET WSAAPI socket(IN INT af, IN INT type, IN INT protocol)
Definition: socklife.c:143
u_short sin_port
Definition: winsock.h:511
#define FD_ACCEPT
Definition: winsock.h:408
#define htonl(x)
Definition: module.h:212
#define MAKEWORD(a, b)
Definition: typedefs.h:247
#define FD_CONNECT
Definition: winsock.h:409
INT WSAAPI bind(IN SOCKET s, IN CONST struct sockaddr *name, IN INT namelen)
Definition: socklife.c:36
#define closesocket
Definition: precomp.h:57
INT WSAAPI WSACleanup(VOID)
Definition: startup.c:60
#define WSAEWOULDBLOCK
Definition: winerror.h:1948
#define INADDR_ANY
Definition: StrAddr.c:4
int winetest_interactive
#define FD_ZERO(set)
Definition: winsock.h:96
#define FD_SET(fd, set)
Definition: winsock.h:89
INT WSAAPI connect(IN SOCKET s, IN CONST struct sockaddr *name, IN INT namelen)
Definition: sockctrl.c:23
#define FD_ISSET(fd, set)
Definition: winsock.h:100
INT WSAAPI select(IN INT s, IN OUT LPFD_SET readfds, IN OUT LPFD_SET writefds, IN OUT LPFD_SET exceptfds, IN CONST struct timeval *timeout)
Definition: select.c:41
INT WINAPI WSAStartup(IN WORD wVersionRequested, OUT LPWSADATA lpWSAData)
Definition: startup.c:113
smooth NULL
Definition: ftsmooth.c:416
INT WSAAPI WSAGetLastError(VOID)
Definition: dllmain.c:112
#define trace
Definition: atltest.h:70
#define INVALID_SOCKET
Definition: winsock.h:332
INT WSAAPI send(IN SOCKET s, IN CONST CHAR FAR *buf, IN INT len, IN INT flags)
Definition: send.c:23
unsigned long DWORD
Definition: ntddk_ex.h:95
GLuint address
Definition: glext.h:9393
_In_ PCCERT_CONTEXT _In_ DWORD dwFlags
Definition: wincrypt.h:1175
#define err(...)
START_TEST(nonblocking)
Definition: nonblocking.c:14
SOCKET WSAAPI accept(IN SOCKET s, OUT LPSOCKADDR addr, OUT INT FAR *addrlen)
Definition: socklife.c:23
#define FIONBIO
Definition: winsock.h:149
struct in_addr sin_addr
Definition: winsock.h:512
#define ioctlsocket
Definition: precomp.h:58
#define ok(value,...)
Definition: atltest.h:57
#define EXIT_FLAGS
Definition: nonblocking.c:12
SOCKET ServerSocket
Definition: socket.c:3
INT WSAAPI listen(IN SOCKET s, IN INT backlog)
Definition: sockctrl.c:123
#define skip(...)
Definition: atltest.h:64
unsigned int ULONG
Definition: retypes.h:1
#define AF_INET
Definition: tcpip.h:117
#define INADDR_LOOPBACK
Definition: winsock.h:312
#define SVR_PORT
Definition: nonblocking.c:10
UINT_PTR SOCKET
Definition: winsock.h:47
#define SOCK_STREAM
Definition: tcpip.h:118
#define htons(x)
Definition: module.h:213
INT WSAAPI WSAAddressToStringA(IN LPSOCKADDR lpsaAddress, IN DWORD dwAddressLength, IN LPWSAPROTOCOL_INFOA lpProtocolInfo, OUT LPSTR lpszAddressString, IN OUT LPDWORD lpdwAddressStringLength)
Definition: rnr.c:23
INT WSAAPI recv(IN SOCKET s, OUT CHAR FAR *buf, IN INT len, IN INT flags)
Definition: recv.c:23
short sin_family
Definition: winsock.h:510
INT WSAAPI getsockname(IN SOCKET s, OUT LPSOCKADDR name, IN OUT INT FAR *namelen)
Definition: sockctrl.c:213