1 rizwank 1.1 /*
|
2 cs130_tom 1.26 * Unit tests for 32-bit WinSock 1.1 functions in Wine
|
3 rizwank 1.1 *
|
4 rizwank 1.3 * Copyright (c) 2005 Thomas Kho, Fredy Garcia, Douglas Rosenberg
|
5 cs130_tom 1.30 * standalone boilerplate copyright (c) 2004,2005 Dan Kegel
|
6 rizwank 1.1 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 #include <stdio.h>
23
|
24 rizwank 1.3 #include <windows.h>
|
25 cs130_tom 1.4 #include <winsock.h>
26 #include <wtypes.h>
27 #include <winerror.h>
|
28 rizwank 1.1
|
29 cs130_tom 1.30 /* To build outside Wine tree, compile with cl -DSTANDALONE -D_X86_ wsock32_main.c wsock32.lib */
|
30 rizwank 1.1 #ifndef STANDALONE
31 #include "wine/test.h"
32 #else
|
33 cs130_tom 1.30 #include <stdarg.h>
34 #include <stdio.h>
|
35 rizwank 1.1 #define START_TEST(name) main(int argc, char **argv)
36 #define ok(condition, msg) \
37 do { \
38 if(!(condition)) \
39 { \
40 fprintf(stderr,"failed at %d\n",__LINE__); \
|
41 cs130_tom 1.30 exit(1); \
|
42 rizwank 1.1 } \
43 } while(0)
44
45 #define todo_wine
|
46 cs130_tom 1.30 static void trace(const char *s, ...)
47 {
48 va_list elipsis;
49 va_start (elipsis, s);
50 vprintf(s, elipsis);
51 va_end(elipsis);
52 }
|
53 rizwank 1.1 #endif
54
|
55 cs130_tom 1.31 /* clients threads to create */
|
56 cs130_tom 1.22 #define NUM_CLIENTS 64
|
57 cs130_tom 1.19
|
58 cs130_tom 1.31 /* amount of data to transfer from each client to server */
|
59 cs130_tom 1.32 #define TEST_DATA_SIZE 145244
|
60 cs130_tom 1.4
|
61 cs130_tom 1.31 /* max time (seconds) to run test.
62 On a 650 Mhz Linux system with tcpdump running, it takes 8 seconds. */
|
63 cs130_tom 1.30 #define TEST_TIMEOUT 20
|
64 cs130_tom 1.19
|
65 cs130_tom 1.31 /* we often pass this size by reference */
|
66 cs130_tom 1.12 int sizeofSOCKADDR_IN = sizeof(SOCKADDR_IN);
|
67 cs130_tom 1.19
|
68 cs130_tom 1.31 /* global test data; server sends it to client, then client verifies it */
|
69 cs130_tom 1.24 char *gTestData;
|
70 cs130_tom 1.4
|
71 cs130_tom 1.25 struct ThreadInfo {
|
72 cs130_tom 1.16 HANDLE Handle;
73 DWORD ID;
74 };
75
|
76 cs130_tom 1.29 struct BlockingServerConnection {
77 struct ThreadInfo serverThread;
|
78 cs130_tom 1.31 SOCKET connectedSocket; /* socket to communicate with client */
79 SOCKADDR_IN clientAddr; /* client info */
|
80 cs130_tom 1.12 };
81
|
82 cs130_tom 1.4 static void test_Startup(void);
|
83 cs130_tom 1.23 static void test_ClientServerBlocking_1(void);
|
84 cs130_tom 1.22 static void test_Cleanup(void);
|
85 cs130_tom 1.23
|
86 cs130_tom 1.28 static void BlockingClient(int *serverPort);
87 static int BlockingServer_Init(int type, SOCKET *sock, SOCKADDR_IN *addr);
88 static void BlockingServer(SOCKET *sock);
|
89 cs130_tom 1.29 static struct BlockingServerConnection * BlockingServerConnection_New(SOCKET sock, SOCKADDR_IN clientAddr);
90 static void BlockingServerConnection_Run(struct BlockingServerConnection *t);
91 static void BlockingServerConnection_Delete(struct BlockingServerConnection *c);
|
92 cs130_tom 1.23
|
93 cs130_tom 1.25 static void BlockingClient(int *serverPort)
|
94 cs130_tom 1.5 {
95 SOCKET sock;
|
96 cs130_tom 1.26 SOCKADDR_IN server;
|
97 cs130_tom 1.6 HOSTENT *hp;
|
98 cs130_tom 1.16 int connectError;
99 int totCharsReceived = 0;
100 int numCharsReceived;
101 int memSame;
102 char buf[1001];
|
103 cs130_tom 1.12
|
104 cs130_tom 1.31 /* create socket */
|
105 cs130_tom 1.26 sock = socket(AF_INET, SOCK_STREAM, 0);
106 ok( sock != INVALID_SOCKET , "Error in socket()\n");
107 if (sock == INVALID_SOCKET) {
108 WSACleanup();
109 exit(0);
110 }
|
111 cs130_tom 1.5
|
112 cs130_tom 1.6 hp = gethostbyname("localhost");
113
114 server.sin_family = AF_INET;
115 server.sin_addr = *(struct in_addr *) hp->h_addr;
116 server.sin_port = *serverPort;
|
117 cs130_tom 1.4
|
118 cs130_tom 1.31 /* connect to server */
|
119 cs130_tom 1.16 connectError = connect(sock, (struct sockaddr *)&server, sizeof(struct sockaddr));
120 ok( !connectError , "client cannot connect to host\n");
121 if(connectError) {
|
122 cs130_tom 1.12 WSACleanup();
123 exit(0);
124 }
|
125 cs130_tom 1.19
|
126 cs130_tom 1.31 /* start receiving data from server */
|
127 cs130_tom 1.16 while( totCharsReceived < TEST_DATA_SIZE ) {
128 numCharsReceived = recv(sock, buf, 1000, 0);
|
129 cs130_tom 1.24 ok( numCharsReceived > 0, "socket was closed unexpectedly\n" );
|
130 cs130_tom 1.19
|
131 cs130_tom 1.31 /* check received data againt global test data */
|
132 cs130_tom 1.24 memSame = ! memcmp(buf,gTestData+totCharsReceived,numCharsReceived);
133 ok( memSame, "data integrity lost during transfer\n" );
|
134 cs130_tom 1.19 totCharsReceived += numCharsReceived;
|
135 cs130_tom 1.12 }
|
136 rizwank 1.1 }
137
|
138 cs130_tom 1.28 static int BlockingServer_Init(int type, SOCKET *sock, SOCKADDR_IN *addr)
|
139 cs130_doug 1.8 {
|
140 cs130_tom 1.31 /* BlockingServer_Init creates socket sock of type type and returns assigned port number in addr.
141 returns server port number */
|
142 cs130_tom 1.29
|
143 cs130_tom 1.27 SOCKADDR_IN tmpAddr;
144 int bindOK;
145 int listenReturn;
|
146 cs130_tom 1.17
|
147 cs130_tom 1.31 /* create socket */
|
148 cs130_tom 1.27 *sock = socket(AF_INET, type, 0);
149 ok( *sock != INVALID_SOCKET , "Error in socket()\n");
150 if (*sock == INVALID_SOCKET) {
151 WSACleanup();
152 exit(0);
153 }
|
154 cs130_tom 1.25
|
155 cs130_tom 1.27 addr->sin_family = AF_INET;
156 addr->sin_addr.s_addr = INADDR_ANY;
157 addr->sin_port = htons(0);
|
158 cs130_tom 1.25
|
159 cs130_tom 1.31 /* bind socket to port */
|
160 cs130_tom 1.27 bindOK = !bind(*sock, (const SOCKADDR *) addr, sizeofSOCKADDR_IN);
161 ok( bindOK , "Error binding client to socket\n");
162 if( !bindOK ) {
163 WSACleanup();
164 exit(0);
|
165 cs130_tom 1.17 }
166
|
167 cs130_tom 1.31 /* get port number */
|
168 cs130_tom 1.27 getsockname(*sock, (SOCKADDR *) &tmpAddr, &sizeofSOCKADDR_IN);
169 addr->sin_port = tmpAddr.sin_port;
170
|
171 cs130_tom 1.31 /* listen on port */
|
172 cs130_tom 1.27 listenReturn = listen(*sock, NUM_CLIENTS);
173 ok(listenReturn != SOCKET_ERROR, "error listening on socket\n");
|
174 cs130_tom 1.28
175 return addr->sin_port;
|
176 cs130_doug 1.8 }
177
|
178 cs130_tom 1.31 static void BlockingServer(SOCKET *sock)
|
179 rizwank 1.3 {
|
180 cs130_tom 1.31 /* listens for incoming connections and accepts up to NUM_CLIENTS connections at once */
|
181 cs130_tom 1.29 struct BlockingServerConnection *connections[NUM_CLIENTS];
182 int connIndex = 0;
183 SOCKET tmpSock;
184 SOCKADDR_IN tmpSockAddr;
|
185 cs130_tom 1.12
|
186 cs130_tom 1.31 /* we require one connection from each client thread */
|
187 cs130_tom 1.29 for (connIndex = 0; connIndex < NUM_CLIENTS; connIndex++) {
|
188 cs130_tom 1.31 /* accept connection */
|
189 cs130_tom 1.29 tmpSock = accept(*sock, (SOCKADDR *) &tmpSockAddr, &sizeofSOCKADDR_IN);
190 ok(tmpSock != INVALID_SOCKET, "error accepting socket\n");
|
191 cs130_tom 1.19
|
192 cs130_tom 1.31 /* handle new connection */
|
193 cs130_tom 1.29 connections[connIndex] = BlockingServerConnection_New(tmpSock, tmpSockAddr);
|
194 cs130_doug 1.8 }
|
195 cs130_tom 1.5
|
196 cs130_tom 1.31 /* clean up connections */
|
197 cs130_tom 1.29 for(connIndex = 0; connIndex < NUM_CLIENTS; connIndex++) {
198 BlockingServerConnection_Delete(connections[connIndex]);
|
199 cs130_tom 1.24 }
|
200 cs130_tom 1.29 }
201
202 static struct BlockingServerConnection * BlockingServerConnection_New(SOCKET sock, SOCKADDR_IN clientAddr)
203 {
204 struct BlockingServerConnection *connection;
205 connection = malloc(sizeof(struct BlockingServerConnection));
206 memset(connection, 0, sizeof(struct BlockingServerConnection));
|
207 cs130_tom 1.5
|
208 cs130_tom 1.29 connection->connectedSocket = sock;
209 connection->clientAddr = clientAddr;
210
|
211 cs130_tom 1.31 /* spawn thread to handle sending data */
|
212 cs130_tom 1.29 connection->serverThread.Handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &BlockingServerConnection_Run, connection, 0, &connection->serverThread.ID);
213
214 return connection;
|
215 cs130_tom 1.27 }
216
|
217 cs130_tom 1.29 static void BlockingServerConnection_Run(struct BlockingServerConnection *connection)
|
218 cs130_tom 1.27 {
|
219 cs130_tom 1.31 /* BlockingServerConnection_Run handles data transfer */
|
220 cs130_tom 1.27 int bClosed;
221 int totCharsSent = 0;
222 int numCharsSent;
223 const int charsPerSend = 2000;
224
|
225 cs130_tom 1.31 /* loop and send data */
|
226 cs130_tom 1.27 while( totCharsSent < TEST_DATA_SIZE ) {
|
227 cs130_tom 1.29 numCharsSent = send(connection->connectedSocket, gTestData+totCharsSent, (totCharsSent + charsPerSend <= TEST_DATA_SIZE) ? charsPerSend : TEST_DATA_SIZE - totCharsSent, 0);
|
228 cs130_tom 1.27 ok( numCharsSent != SOCKET_ERROR, "socket error\n" );
229
|
230 cs130_tom 1.31 /* pass if send buffer is full */
|
231 cs130_tom 1.27 if(numCharsSent == 0) {
232 Sleep(100);
233 }
234
235 totCharsSent += numCharsSent;
236 }
237
|
238 cs130_tom 1.29 bClosed = !closesocket(connection->connectedSocket);
|
239 cs130_tom 1.27 ok(bClosed,"Error closing socket\n");
|
240 rizwank 1.3 }
241
|
242 cs130_tom 1.29 static void BlockingServerConnection_Delete(struct BlockingServerConnection *c)
243 {
|
244 cs130_tom 1.31 /* wait for client to receive data before cleaning up */
|
245 cs130_tom 1.29 WaitForSingleObject(c->serverThread.Handle, INFINITE);
246
247 free(c);
248 }
249
|
250 cs130_tom 1.28 static void test_ClientServerBlocking_1(void)
|
251 rizwank 1.3 {
|
252 cs130_tom 1.28 struct ThreadInfo serverThread;
|
253 cs130_tom 1.29 struct ThreadInfo *clientThreads;
|
254 cs130_tom 1.28 DWORD waitStatus;
255 SOCKET sock;
256 SOCKADDR_IN server;
257 int serverPort;
|
258 cs130_tom 1.25 int threadIndex = 0;
|
259 cs130_tom 1.9
|
260 cs130_tom 1.31 /* create socket, bind server and start listening */
|
261 cs130_tom 1.28 serverPort = BlockingServer_Init(SOCK_STREAM, &sock, &server);
262
|
263 cs130_tom 1.31 /* start server thread */
|
264 cs130_tom 1.28 trace("starting server thread\n");
265 serverThread.Handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &BlockingServer, &sock, 0, &serverThread.ID);
266
|
267 cs130_tom 1.31 /* start client threads */
|
268 cs130_tom 1.25 clientThreads = malloc(sizeof(struct ThreadInfo) * NUM_CLIENTS);
269 memset(clientThreads, 0, sizeof(struct ThreadInfo) * NUM_CLIENTS);
|
270 cs130_tom 1.18
|
271 cs130_tom 1.25 for(threadIndex = 0; threadIndex < NUM_CLIENTS; threadIndex++) {
|
272 cs130_tom 1.28 clientThreads[threadIndex].Handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &BlockingClient, (void *) &serverPort, 0, &clientThreads[threadIndex].ID);
|
273 cs130_tom 1.9 }
|
274 cs130_tom 1.20 trace("%d clients started\n", NUM_CLIENTS);
|
275 cs130_tom 1.9
|
276 cs130_tom 1.31 /* server thread needs to end before cleaning up */
|
277 cs130_tom 1.28 waitStatus = WaitForSingleObject(serverThread.Handle, TEST_TIMEOUT * 1000);
278 ok( waitStatus != WAIT_TIMEOUT, "test did not complete in time\n" );
279
|
280 cs130_tom 1.31 /* wait for all clients to receive data before cleaning up */
|
281 cs130_tom 1.25 for(threadIndex = 0; threadIndex < NUM_CLIENTS; threadIndex++) {
282 WaitForSingleObject(clientThreads[threadIndex].Handle, INFINITE);
|
283 cs130_tom 1.24 }
|
284 cs130_tom 1.19
|
285 cs130_tom 1.25 free(clientThreads);
|
286 rizwank 1.3 }
287
|
288 cs130_tom 1.4 static void test_Startup(void)
|
289 rizwank 1.3 {
|
290 cs130_tom 1.32 /* generate test data and initialize application */
|
291 cs130_tom 1.4 WSADATA wsaData;
|
292 cs130_tom 1.22 int wsastartup_result;
|
293 cs130_tom 1.14 int versionOK;
|
294 cs130_tom 1.32 int i;
295
296 srand(1);
297
298 /* fill out test data */
299 gTestData = malloc(TEST_DATA_SIZE);
300 for(i = 0; i < (TEST_DATA_SIZE/sizeof(int)); i++) {
301 *(((int *) gTestData) + i) = rand();
302 }
|
303 cs130_tom 1.14
|
304 cs130_tom 1.31 /* check for compatible winsock version */
|
305 cs130_tom 1.14 wsastartup_result = WSAStartup(MAKEWORD(1,1), &wsaData);
|
306 cs130_tom 1.32 ok((wsastartup_result == NO_ERROR), "Error in WSAStartup()\n");
307
|
308 cs130_tom 1.14 versionOK = (LOBYTE(wsaData.wVersion) == 1) && (HIBYTE(wsaData.wVersion) == 1);
|
309 cs130_tom 1.24 ok( versionOK , "WSAStartup returns an incompatible sockets version\n");
|
310 cs130_tom 1.14 if ( !versionOK ) {
|
311 cs130_tom 1.4 WSACleanup();
312 exit(0);
313 }
314
|
315 cs130_tom 1.22 trace("startup ok\n");
316 }
317
318 static void test_Cleanup(void)
319 {
320 int cleanupOK;
321
322 cleanupOK = ! WSACleanup();
|
323 cs130_tom 1.32 ok( cleanupOK , "error in WSACleanup()\n");
324
325 free(gTestData);
|
326 cs130_tom 1.22
327 trace("cleanup ok\n");
|
328 rizwank 1.3 }
|
329 cs130_tom 1.4
|
330 rizwank 1.1 START_TEST(wsock32_main)
331 {
|
332 cs130_tom 1.22 const int numTests = 3;
|
333 cs130_tom 1.32
|
334 cs130_tom 1.22 trace("test 1 of %d:\n", numTests);
335 test_Startup();
336
337 trace("test 2 of %d:\n", numTests);
338 test_ClientServerBlocking_1();
339
340 trace("test 3 of %d:\n", numTests);
341 test_Cleanup();
342
|
343 cs130_tom 1.30 trace("all " __FILE__ " tests done\n");
|
344 rizwank 1.1 }
|