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 /* global test data; server sends it to client, then client verifies it */
|
66 cs130_tom 1.24 char *gTestData;
|
67 cs130_tom 1.4
|
68 cs130_tom 1.25 struct ThreadInfo {
|
69 cs130_tom 1.16 HANDLE Handle;
70 DWORD ID;
71 };
72
|
73 cs130_tom 1.29 struct BlockingServerConnection {
74 struct ThreadInfo serverThread;
|
75 cs130_tom 1.31 SOCKET connectedSocket; /* socket to communicate with client */
76 SOCKADDR_IN clientAddr; /* client info */
|
77 cs130_tom 1.12 };
78
|
79 cs130_tom 1.4 static void test_Startup(void);
|
80 cs130_tom 1.23 static void test_ClientServerBlocking_1(void);
|
81 cs130_tom 1.22 static void test_Cleanup(void);
|
82 cs130_tom 1.23
|
83 cs130_tom 1.28 static void BlockingClient(int *serverPort);
84 static int BlockingServer_Init(int type, SOCKET *sock, SOCKADDR_IN *addr);
85 static void BlockingServer(SOCKET *sock);
|
86 cs130_tom 1.29 static struct BlockingServerConnection * BlockingServerConnection_New(SOCKET sock, SOCKADDR_IN clientAddr);
87 static void BlockingServerConnection_Run(struct BlockingServerConnection *t);
88 static void BlockingServerConnection_Delete(struct BlockingServerConnection *c);
|
89 cs130_tom 1.23
|
90 cs130_tom 1.25 static void BlockingClient(int *serverPort)
|
91 cs130_tom 1.5 {
92 SOCKET sock;
|
93 cs130_tom 1.26 SOCKADDR_IN server;
|
94 cs130_tom 1.6 HOSTENT *hp;
|
95 cs130_tom 1.16 int connectError;
96 int totCharsReceived = 0;
97 int numCharsReceived;
98 int memSame;
99 char buf[1001];
|
100 cs130_tom 1.12
|
101 cs130_tom 1.31 /* create socket */
|
102 cs130_tom 1.26 sock = socket(AF_INET, SOCK_STREAM, 0);
103 ok( sock != INVALID_SOCKET , "Error in socket()\n");
104 if (sock == INVALID_SOCKET) {
105 WSACleanup();
106 exit(0);
107 }
|
108 cs130_tom 1.5
|
109 cs130_tom 1.6 hp = gethostbyname("localhost");
110
111 server.sin_family = AF_INET;
112 server.sin_addr = *(struct in_addr *) hp->h_addr;
113 server.sin_port = *serverPort;
|
114 cs130_tom 1.4
|
115 cs130_tom 1.31 /* connect to server */
|
116 cs130_tom 1.16 connectError = connect(sock, (struct sockaddr *)&server, sizeof(struct sockaddr));
117 ok( !connectError , "client cannot connect to host\n");
118 if(connectError) {
|
119 cs130_tom 1.12 WSACleanup();
120 exit(0);
121 }
|
122 cs130_tom 1.19
|
123 cs130_tom 1.31 /* start receiving data from server */
|
124 cs130_tom 1.16 while( totCharsReceived < TEST_DATA_SIZE ) {
125 numCharsReceived = recv(sock, buf, 1000, 0);
|
126 cs130_tom 1.24 ok( numCharsReceived > 0, "socket was closed unexpectedly\n" );
|
127 cs130_tom 1.19
|
128 cs130_tom 1.31 /* check received data againt global test data */
|
129 cs130_tom 1.24 memSame = ! memcmp(buf,gTestData+totCharsReceived,numCharsReceived);
130 ok( memSame, "data integrity lost during transfer\n" );
|
131 cs130_tom 1.19 totCharsReceived += numCharsReceived;
|
132 cs130_tom 1.12 }
|
133 rizwank 1.1 }
134
|
135 cs130_tom 1.28 static int BlockingServer_Init(int type, SOCKET *sock, SOCKADDR_IN *addr)
|
136 cs130_doug 1.8 {
|
137 cs130_tom 1.33 /* BlockingServer_Init creates socket sock of type type and
138 returns assigned port number in addr. */
|
139 cs130_tom 1.29
|
140 cs130_tom 1.27 SOCKADDR_IN tmpAddr;
141 int bindOK;
142 int listenReturn;
|
143 cs130_tom 1.33 int sizeofSOCKADDR_IN = sizeof(SOCKADDR_IN);
|
144 cs130_tom 1.17
|
145 cs130_tom 1.31 /* create socket */
|
146 cs130_tom 1.27 *sock = socket(AF_INET, type, 0);
147 ok( *sock != INVALID_SOCKET , "Error in socket()\n");
148 if (*sock == INVALID_SOCKET) {
149 WSACleanup();
150 exit(0);
151 }
|
152 cs130_tom 1.25
|
153 cs130_tom 1.27 addr->sin_family = AF_INET;
154 addr->sin_addr.s_addr = INADDR_ANY;
155 addr->sin_port = htons(0);
|
156 cs130_tom 1.25
|
157 cs130_tom 1.31 /* bind socket to port */
|
158 cs130_tom 1.33 bindOK = !bind(*sock, (const SOCKADDR *) addr, sizeof(SOCKADDR_IN));
|
159 cs130_tom 1.27 ok( bindOK , "Error binding client to socket\n");
160 if( !bindOK ) {
161 WSACleanup();
162 exit(0);
|
163 cs130_tom 1.17 }
164
|
165 cs130_tom 1.31 /* get port number */
|
166 cs130_tom 1.27 getsockname(*sock, (SOCKADDR *) &tmpAddr, &sizeofSOCKADDR_IN);
167 addr->sin_port = tmpAddr.sin_port;
168
|
169 cs130_tom 1.31 /* listen on port */
|
170 cs130_tom 1.27 listenReturn = listen(*sock, NUM_CLIENTS);
171 ok(listenReturn != SOCKET_ERROR, "error listening on socket\n");
|
172 cs130_tom 1.28
173 return addr->sin_port;
|
174 cs130_doug 1.8 }
175
|
176 cs130_tom 1.31 static void BlockingServer(SOCKET *sock)
|
177 rizwank 1.3 {
|
178 cs130_tom 1.31 /* listens for incoming connections and accepts up to NUM_CLIENTS connections at once */
|
179 cs130_tom 1.29 struct BlockingServerConnection *connections[NUM_CLIENTS];
180 int connIndex = 0;
181 SOCKET tmpSock;
182 SOCKADDR_IN tmpSockAddr;
|
183 cs130_tom 1.33 int sizeofSOCKADDR_IN = sizeof(SOCKADDR_IN);
|
184 cs130_tom 1.12
|
185 cs130_tom 1.31 /* we require one connection from each client thread */
|
186 cs130_tom 1.29 for (connIndex = 0; connIndex < NUM_CLIENTS; connIndex++) {
|
187 cs130_tom 1.33 /* accept connection */
188 tmpSock = accept(*sock, (SOCKADDR *) &tmpSockAddr, &sizeofSOCKADDR_IN);
189 ok(tmpSock != INVALID_SOCKET, "error accepting socket\n");
|
190 cs130_tom 1.19
|
191 cs130_tom 1.33 /* handle new connection */
192 connections[connIndex] = BlockingServerConnection_New(tmpSock, tmpSockAddr);
|
193 cs130_doug 1.8 }
|
194 cs130_tom 1.5
|
195 cs130_tom 1.31 /* clean up connections */
|
196 cs130_tom 1.29 for(connIndex = 0; connIndex < NUM_CLIENTS; connIndex++) {
197 BlockingServerConnection_Delete(connections[connIndex]);
|
198 cs130_tom 1.24 }
|
199 cs130_tom 1.29 }
200
201 static struct BlockingServerConnection * BlockingServerConnection_New(SOCKET sock, SOCKADDR_IN clientAddr)
202 {
203 struct BlockingServerConnection *connection;
204 connection = malloc(sizeof(struct BlockingServerConnection));
205 memset(connection, 0, sizeof(struct BlockingServerConnection));
|
206 cs130_tom 1.5
|
207 cs130_tom 1.29 connection->connectedSocket = sock;
208 connection->clientAddr = clientAddr;
209
|
210 cs130_tom 1.31 /* spawn thread to handle sending data */
|
211 cs130_tom 1.33 connection->serverThread.Handle = CreateThread(NULL, 0,
212 (LPTHREAD_START_ROUTINE) &BlockingServerConnection_Run, connection, 0,
213 &connection->serverThread.ID);
|
214 cs130_tom 1.29
215 return connection;
|
216 cs130_tom 1.27 }
217
|
218 cs130_tom 1.29 static void BlockingServerConnection_Run(struct BlockingServerConnection *connection)
|
219 cs130_tom 1.27 {
|
220 cs130_tom 1.31 /* BlockingServerConnection_Run handles data transfer */
|
221 cs130_tom 1.27 int bClosed;
222 int totCharsSent = 0;
223 int numCharsSent;
|
224 cs130_tom 1.33 int charsToSend;
|
225 cs130_tom 1.27 const int charsPerSend = 2000;
226
|
227 cs130_tom 1.31 /* loop and send data */
|
228 cs130_tom 1.27 while( totCharsSent < TEST_DATA_SIZE ) {
|
229 cs130_tom 1.33 if (totCharsSent + charsPerSend <= TEST_DATA_SIZE) {
230 charsToSend = charsPerSend;
231 } else {
232 charsToSend = TEST_DATA_SIZE - totCharsSent;
233 }
234
235 numCharsSent = send(connection->connectedSocket, &gTestData[totCharsSent], charsToSend, 0);
|
236 cs130_tom 1.27 ok( numCharsSent != SOCKET_ERROR, "socket error\n" );
237
|
238 cs130_tom 1.31 /* pass if send buffer is full */
|
239 cs130_tom 1.27 if(numCharsSent == 0) {
240 Sleep(100);
241 }
242
243 totCharsSent += numCharsSent;
244 }
245
|
246 cs130_tom 1.29 bClosed = !closesocket(connection->connectedSocket);
|
247 cs130_tom 1.27 ok(bClosed,"Error closing socket\n");
|
248 rizwank 1.3 }
249
|
250 cs130_tom 1.29 static void BlockingServerConnection_Delete(struct BlockingServerConnection *c)
251 {
|
252 cs130_tom 1.31 /* wait for client to receive data before cleaning up */
|
253 cs130_tom 1.29 WaitForSingleObject(c->serverThread.Handle, INFINITE);
254
255 free(c);
256 }
257
|
258 cs130_tom 1.28 static void test_ClientServerBlocking_1(void)
|
259 rizwank 1.3 {
|
260 cs130_tom 1.28 struct ThreadInfo serverThread;
|
261 cs130_tom 1.29 struct ThreadInfo *clientThreads;
|
262 cs130_tom 1.28 DWORD waitStatus;
263 SOCKET sock;
264 SOCKADDR_IN server;
265 int serverPort;
|
266 cs130_tom 1.25 int threadIndex = 0;
|
267 cs130_tom 1.9
|
268 cs130_tom 1.31 /* create socket, bind server and start listening */
|
269 cs130_tom 1.28 serverPort = BlockingServer_Init(SOCK_STREAM, &sock, &server);
270
|
271 cs130_tom 1.31 /* start server thread */
|
272 cs130_tom 1.28 trace("starting server thread\n");
|
273 cs130_tom 1.33 serverThread.Handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &BlockingServer,
274 &sock, 0, &serverThread.ID);
|
275 cs130_tom 1.28
|
276 cs130_tom 1.31 /* start client threads */
|
277 cs130_tom 1.25 clientThreads = malloc(sizeof(struct ThreadInfo) * NUM_CLIENTS);
278 memset(clientThreads, 0, sizeof(struct ThreadInfo) * NUM_CLIENTS);
|
279 cs130_tom 1.18
|
280 cs130_tom 1.25 for(threadIndex = 0; threadIndex < NUM_CLIENTS; threadIndex++) {
|
281 cs130_tom 1.33 clientThreads[threadIndex].Handle = CreateThread(NULL, 0,
282 (LPTHREAD_START_ROUTINE) &BlockingClient, (void *) &serverPort, 0,
283 &clientThreads[threadIndex].ID);
|
284 cs130_tom 1.9 }
|
285 cs130_tom 1.20 trace("%d clients started\n", NUM_CLIENTS);
|
286 cs130_tom 1.9
|
287 cs130_tom 1.31 /* server thread needs to end before cleaning up */
|
288 cs130_tom 1.28 waitStatus = WaitForSingleObject(serverThread.Handle, TEST_TIMEOUT * 1000);
289 ok( waitStatus != WAIT_TIMEOUT, "test did not complete in time\n" );
290
|
291 cs130_tom 1.31 /* wait for all clients to receive data before cleaning up */
|
292 cs130_tom 1.25 for(threadIndex = 0; threadIndex < NUM_CLIENTS; threadIndex++) {
293 WaitForSingleObject(clientThreads[threadIndex].Handle, INFINITE);
|
294 cs130_tom 1.24 }
|
295 cs130_tom 1.19
|
296 cs130_tom 1.25 free(clientThreads);
|
297 rizwank 1.3 }
298
|
299 cs130_tom 1.4 static void test_Startup(void)
|
300 rizwank 1.3 {
|
301 cs130_tom 1.32 /* generate test data and initialize application */
|
302 cs130_tom 1.4 WSADATA wsaData;
|
303 cs130_tom 1.22 int wsastartup_result;
|
304 cs130_tom 1.14 int versionOK;
|
305 cs130_tom 1.33 int i;
306 char testPattern[11] = { 'A', 'O', 'E', 'U', 'I', 'D', 0x00, 0x05, 0x10, 0x15, 0x20 };
|
307 cs130_tom 1.32
308 /* fill out test data */
309 gTestData = malloc(TEST_DATA_SIZE);
|
310 cs130_tom 1.33 for(i = 0; i < TEST_DATA_SIZE; i++) {
311 gTestData[i] = testPattern[i%11];
|
312 cs130_tom 1.32 }
|
313 cs130_tom 1.14
|
314 cs130_tom 1.31 /* check for compatible winsock version */
|
315 cs130_tom 1.14 wsastartup_result = WSAStartup(MAKEWORD(1,1), &wsaData);
|
316 cs130_tom 1.32 ok((wsastartup_result == NO_ERROR), "Error in WSAStartup()\n");
317
|
318 cs130_tom 1.14 versionOK = (LOBYTE(wsaData.wVersion) == 1) && (HIBYTE(wsaData.wVersion) == 1);
|
319 cs130_tom 1.24 ok( versionOK , "WSAStartup returns an incompatible sockets version\n");
|
320 cs130_tom 1.14 if ( !versionOK ) {
|
321 cs130_tom 1.4 WSACleanup();
322 exit(0);
323 }
324
|
325 cs130_tom 1.22 trace("startup ok\n");
326 }
327
328 static void test_Cleanup(void)
329 {
330 int cleanupOK;
331
332 cleanupOK = ! WSACleanup();
|
333 cs130_tom 1.32 ok( cleanupOK , "error in WSACleanup()\n");
334
335 free(gTestData);
|
336 cs130_tom 1.22
337 trace("cleanup ok\n");
|
338 rizwank 1.3 }
|
339 cs130_tom 1.4
|
340 rizwank 1.1 START_TEST(wsock32_main)
341 {
|
342 cs130_tom 1.33 static const int numTests = 3;
|
343 cs130_tom 1.32
|
344 cs130_tom 1.22 trace("test 1 of %d:\n", numTests);
345 test_Startup();
346
347 trace("test 2 of %d:\n", numTests);
348 test_ClientServerBlocking_1();
349
350 trace("test 3 of %d:\n", numTests);
351 test_Cleanup();
352
|
353 cs130_tom 1.30 trace("all " __FILE__ " tests done\n");
|
354 rizwank 1.1 }
|