6d5e319ba7380ea38dc264179513baaaeeb4c475
[reactos.git] / rostests / apitests / ws2_32 / WSAAsync.c
1 /*
2 * PROJECT: ReactOS api tests
3 * LICENSE: GPL - See COPYING in the top level directory
4 * PURPOSE: Test for WSAAsync
5 * PROGRAMMERS: Miroslav Mastny
6 */
7
8 #include <apitest.h>
9
10 #include <stdio.h>
11 #include <windows.h>
12 #include <winsock2.h>
13
14 #define SVR_PORT 5000
15 #define WAIT_TIMEOUT_ 10000
16 #define EXIT_FLAGS (FD_ACCEPT|FD_CONNECT)
17
18 START_TEST(WSAAsync)
19 {
20 WSADATA WsaData;
21 SOCKET ServerSocket = INVALID_SOCKET,
22 ClientSocket = INVALID_SOCKET;
23 WSAEVENT ServerEvent = WSA_INVALID_EVENT,
24 ClientEvent = WSA_INVALID_EVENT;
25 struct hostent *ent = NULL;
26 struct sockaddr_in server_addr_in;
27 struct sockaddr_in addr_remote;
28 struct sockaddr_in addr_con_loc;
29 int nConRes;
30 int addrsize, len;
31 WSAEVENT fEvents[2];
32 SOCKET fSockets[2];
33 SOCKET sockaccept;
34 WSANETWORKEVENTS WsaNetworkEvents;
35 ULONG ulValue = 1;
36 DWORD dwWait;
37 DWORD dwFlags = 0;
38
39 if (WSAStartup(MAKEWORD(2, 2), &WsaData) != 0)
40 {
41 skip("WSAStartup failed\n");
42 return;
43 }
44
45 ServerSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
46 ClientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
47 ServerEvent = WSACreateEvent();
48 ClientEvent = WSACreateEvent();
49
50 if (ServerSocket == INVALID_SOCKET)
51 {
52 skip("ERROR: Server socket creation failed\n");
53 return;
54 }
55 if (ClientSocket == INVALID_SOCKET)
56 {
57 skip("ERROR: Client socket creation failed\n");
58 closesocket(ServerSocket);
59 return;
60 }
61 if (ServerEvent == WSA_INVALID_EVENT)
62 {
63 skip("ERROR: Server WSAEvent creation failed\n");
64 closesocket(ClientSocket);
65 closesocket(ServerSocket);
66 return;
67 }
68 if (ClientEvent == WSA_INVALID_EVENT)
69 {
70 skip("ERROR: Client WSAEvent creation failed\n");
71 WSACloseEvent(ServerEvent);
72 closesocket(ClientSocket);
73 closesocket(ServerSocket);
74 return;
75 }
76 ent = gethostbyname("127.0.0.1");
77 if (ent == NULL)
78 {
79 ok(ent != NULL, "ERROR: gethostbyname '127.0.0.1' failed, trying 'localhost'\n");
80 ent = gethostbyname("localhost");
81
82 if (ent == NULL)
83 {
84 skip("ERROR: gethostbyname 'localhost' failed\n");
85 goto done;
86 }
87 }
88
89 server_addr_in.sin_family = AF_INET;
90 server_addr_in.sin_port = htons(SVR_PORT);
91 memcpy(&server_addr_in.sin_addr.S_un.S_addr, ent->h_addr_list[0], 4);
92
93 // server inialialization
94 trace("Initializing server and client connections ...\n");
95 ok(bind(ServerSocket, (struct sockaddr*)&server_addr_in, sizeof(server_addr_in)) == 0, "ERROR: server bind failed\n");
96 ok(ioctlsocket(ServerSocket, FIONBIO, &ulValue) == 0, "ERROR: server ioctlsocket FIONBIO failed\n");
97 ok(WSAEventSelect(ServerSocket, ServerEvent, FD_ACCEPT | FD_CLOSE) == 0, "ERROR: server accept EventSelect failed\n");
98
99 // client inialialization
100 ok(WSAEventSelect(ClientSocket, ClientEvent, FD_CONNECT | FD_CLOSE) == 0, "ERROR: client EventSelect failed\n");
101 ok(ioctlsocket(ClientSocket, FIONBIO, &ulValue) == 0, "ERROR: client ioctlsocket FIONBIO failed\n");
102
103 // listen
104 trace("Starting server listening mode ...\n");
105 ok(listen(ServerSocket, SOMAXCONN) == 0, "ERROR: cannot initialize server listen\n");
106
107 trace("Starting client to server connection ...\n");
108 // connect
109 nConRes = connect(ClientSocket, (struct sockaddr*)&server_addr_in, sizeof(server_addr_in));
110 ok(nConRes == SOCKET_ERROR, "ERROR: client connect() result is not SOCKET_ERROR\n");
111 ok(WSAGetLastError() == WSAEWOULDBLOCK, "ERROR: client connect() last error is not WSAEWOULDBLOCK\n");
112
113 fSockets[0] = ServerSocket;
114 fSockets[1] = ClientSocket;
115
116 fEvents[0] = ServerEvent;
117 fEvents[1] = ClientEvent;
118
119 while (dwFlags != EXIT_FLAGS)
120 {
121 dwWait = WaitForMultipleObjects(2, fEvents, FALSE, WAIT_TIMEOUT_);
122
123 ok(dwWait == WAIT_OBJECT_0 || // server socket event
124 dwWait == WAIT_OBJECT_0+1, // client socket event
125 "Unknown event received %ld\n", dwWait);
126 if (dwWait != WAIT_OBJECT_0 && dwWait != WAIT_OBJECT_0+1)
127 {
128 skip("ERROR: Connection timeout\n");
129 break;
130 }
131
132 WSAEnumNetworkEvents(fSockets[dwWait-WAIT_OBJECT_0], fEvents[dwWait-WAIT_OBJECT_0], &WsaNetworkEvents);
133
134 if ((WsaNetworkEvents.lNetworkEvents & FD_ACCEPT) != 0)
135 {// connection accepted
136 trace("Event FD_ACCEPT...\n");
137 ok(WsaNetworkEvents.iErrorCode[FD_ACCEPT_BIT] == 0, "Error on accept %d\n", WsaNetworkEvents.iErrorCode[FD_ACCEPT_BIT]);
138 if (WsaNetworkEvents.iErrorCode[FD_ACCEPT_BIT] == 0)
139 {
140 addrsize = sizeof(addr_remote);
141 sockaccept = accept(fSockets[dwWait - WAIT_OBJECT_0], (struct sockaddr*)&addr_remote, &addrsize);
142 ok(sockaccept != INVALID_SOCKET, "ERROR: Connection accept function failed, error %d\n", WSAGetLastError());
143 dwFlags |= FD_ACCEPT;
144 }
145 }
146
147 if ((WsaNetworkEvents.lNetworkEvents & FD_CONNECT) != 0)
148 {// client connected
149 trace("Event FD_CONNECT...\n");
150 ok(WsaNetworkEvents.iErrorCode[FD_CONNECT_BIT] == 0, "Error on connect %d\n", WsaNetworkEvents.iErrorCode[FD_CONNECT_BIT]);
151 if (WsaNetworkEvents.iErrorCode[FD_CONNECT_BIT] == 0)
152 {
153 len = sizeof(addr_con_loc);
154 ok(getsockname(fSockets[dwWait - WAIT_OBJECT_0], (struct sockaddr*)&addr_con_loc, &len) == 0, "\n");
155 dwFlags |= FD_CONNECT;
156 }
157 }
158 }
159
160 done:
161 WSACloseEvent(ServerEvent);
162 WSACloseEvent(ClientEvent);
163 closesocket(ServerSocket);
164 closesocket(ClientSocket);
165
166 WSACleanup();
167 }