(file) Return to wsock32_main.c CVS log (file) (dir) Up to [RizwankCVS] / wine4 / wine / dlls / wsock32 / tests

  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.19 // clients threads to create
 56 cs130_tom 1.22 #define NUM_CLIENTS 64
 57 cs130_tom 1.19 
 58 cs130_tom 1.6  // amount of data to transfer from each client to server
 59 cs130_tom 1.19 #define TEST_DATA_SIZE 145243
 60 cs130_tom 1.4  
 61 cs130_tom 1.30 // max time (seconds) to run test.  
 62                // On a 650 Mhz Linux system with tcpdump running, it takes 8 seconds.
 63                #define TEST_TIMEOUT 20
 64 cs130_tom 1.19 
 65                // we often pass this size by reference
 66 cs130_tom 1.12 int sizeofSOCKADDR_IN = sizeof(SOCKADDR_IN);
 67 cs130_tom 1.19 
 68                // 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.25 	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.26 	// create socket
105                	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.19 	// 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                	// 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                		// 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.29 	// BlockingServer_Init creates socket sock of type type and returns assigned port number in addr.
141                 	// returns server port number
142                 
143 cs130_tom  1.27 	SOCKADDR_IN tmpAddr;
144                 	int bindOK;
145                 	int listenReturn;
146 cs130_tom  1.17 
147 cs130_tom  1.27 	// create socket
148                 	*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.27 	// bind socket to port
160                 	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.27 	// get port number
168                 	getsockname(*sock, (SOCKADDR *) &tmpAddr, &sizeofSOCKADDR_IN);
169                 	addr->sin_port = tmpAddr.sin_port;
170                 
171                 	// listen on port
172                 	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.28 static void BlockingServer(SOCKET *sock) // listens for incoming connections and accepts up to NUM_CLIENTS connections at once
179 rizwank    1.3  {
180 cs130_tom  1.29 	struct BlockingServerConnection *connections[NUM_CLIENTS];
181                 	int connIndex = 0;
182                 	SOCKET tmpSock;
183                 	SOCKADDR_IN tmpSockAddr;
184 cs130_tom  1.12 
185 cs130_tom  1.19 	// we require one connection from each client thread
186 cs130_tom  1.29 	for (connIndex = 0; connIndex < NUM_CLIENTS; connIndex++) {
187 cs130_tom  1.19 			// accept connection
188 cs130_tom  1.29 			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.29 			// handle new connection
192                 			connections[connIndex] = BlockingServerConnection_New(tmpSock, tmpSockAddr);
193 cs130_doug 1.8  	}
194 cs130_tom  1.5  
195 cs130_tom  1.29 	// clean up connections
196                 	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                 	// spawn thread to handle sending data
211                 	connection->serverThread.Handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &BlockingServerConnection_Run, connection, 0, &connection->serverThread.ID);
212                 
213                 	return connection;
214 cs130_tom  1.27 }
215                 
216 cs130_tom  1.29 static void BlockingServerConnection_Run(struct BlockingServerConnection *connection)
217 cs130_tom  1.27 {
218                 	// this will handle all connections to the server, it's in its own function to allow for multithreading
219                 	int bClosed;
220                 	int totCharsSent = 0;
221                 	int numCharsSent;
222                 	const int charsPerSend = 2000;
223                 
224                 	// loop and send data
225                 	while( totCharsSent < TEST_DATA_SIZE ) {
226 cs130_tom  1.29 		numCharsSent = send(connection->connectedSocket, gTestData+totCharsSent, (totCharsSent + charsPerSend <= TEST_DATA_SIZE) ? charsPerSend : TEST_DATA_SIZE - totCharsSent, 0);
227 cs130_tom  1.27 		ok( numCharsSent != SOCKET_ERROR, "socket error\n" );
228                 
229                 		// pass if send buffer is full
230                 		if(numCharsSent == 0) {
231                 			Sleep(100);
232                 		}
233                 
234                 		totCharsSent += numCharsSent;
235                 	}
236                 
237 cs130_tom  1.29 	bClosed = !closesocket(connection->connectedSocket);
238 cs130_tom  1.27 	ok(bClosed,"Error closing socket\n");
239 rizwank    1.3  }
240                 
241 cs130_tom  1.29 static void BlockingServerConnection_Delete(struct BlockingServerConnection *c)
242                 {
243                 	// wait for client to receive data before cleaning up
244                 	WaitForSingleObject(c->serverThread.Handle, INFINITE);
245                 
246                 	free(c);
247                 }
248                 
249 cs130_tom  1.28 static void test_ClientServerBlocking_1(void)
250 rizwank    1.3  {
251 cs130_tom  1.28 	struct ThreadInfo serverThread;
252 cs130_tom  1.29 	struct ThreadInfo *clientThreads;
253 cs130_tom  1.28 	DWORD waitStatus;
254                 	SOCKET sock;
255                 	SOCKADDR_IN server;
256                 	int serverPort;
257 cs130_tom  1.25 	int threadIndex = 0;
258 cs130_tom  1.9  
259 cs130_tom  1.28 	// create socket, bind server and start listening
260                 	serverPort = BlockingServer_Init(SOCK_STREAM, &sock, &server);
261                 
262                 	// start server thread
263                 	trace("starting server thread\n");
264                 	serverThread.Handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &BlockingServer, &sock, 0, &serverThread.ID);
265                 
266                 	// start client threads
267 cs130_tom  1.25 	clientThreads = malloc(sizeof(struct ThreadInfo) * NUM_CLIENTS);
268                 	memset(clientThreads, 0, sizeof(struct ThreadInfo) * NUM_CLIENTS);
269 cs130_tom  1.18 
270 cs130_tom  1.25 	for(threadIndex = 0; threadIndex < NUM_CLIENTS; threadIndex++) {
271 cs130_tom  1.28 		clientThreads[threadIndex].Handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &BlockingClient, (void *) &serverPort, 0, &clientThreads[threadIndex].ID);
272 cs130_tom  1.9  	}
273 cs130_tom  1.20 	trace("%d clients started\n", NUM_CLIENTS);
274 cs130_tom  1.9  
275 cs130_tom  1.28 	// server thread needs to end before cleaning up
276                 	waitStatus = WaitForSingleObject(serverThread.Handle, TEST_TIMEOUT * 1000);
277                 	ok( waitStatus != WAIT_TIMEOUT, "test did not complete in time\n" );
278                 
279 cs130_tom  1.20 	// wait for all clients to receive data before cleaning up
280 cs130_tom  1.25 	for(threadIndex = 0; threadIndex < NUM_CLIENTS; threadIndex++) {
281                 		WaitForSingleObject(clientThreads[threadIndex].Handle, INFINITE);
282 cs130_tom  1.24 	}
283 cs130_tom  1.19 
284 cs130_tom  1.25 	free(clientThreads);
285 rizwank    1.3  }
286                 
287 cs130_tom  1.4  static void test_Startup(void)
288 rizwank    1.3  {
289 cs130_tom  1.4  	// initialize application
290                 	WSADATA wsaData;
291 cs130_tom  1.22 	int wsastartup_result;
292 cs130_tom  1.14 	int versionOK;
293                 
294 cs130_tom  1.19 	// check for compatible winsock version
295 cs130_tom  1.14 	wsastartup_result = WSAStartup(MAKEWORD(1,1), &wsaData);
296                 	versionOK = (LOBYTE(wsaData.wVersion) == 1) && (HIBYTE(wsaData.wVersion) == 1);
297                 
298 cs130_tom  1.24 	ok( versionOK , "WSAStartup returns an incompatible sockets version\n");
299 cs130_tom  1.14 	if ( !versionOK ) {
300 cs130_tom  1.4  		WSACleanup();
301                 		exit(0);
302                 	}
303                 
304 cs130_tom  1.24 	ok((wsastartup_result == NO_ERROR), "Error in WSAStartup()\n");
305 cs130_tom  1.22 	trace("startup ok\n");
306                 }
307                 
308                 static void test_Cleanup(void)
309                 {
310                 	int cleanupOK;
311                 
312                 	cleanupOK = ! WSACleanup();
313                 
314 cs130_tom  1.24 	ok( cleanupOK , "error in WSACleanup()\n");
315 cs130_tom  1.22 	trace("cleanup ok\n");
316 rizwank    1.3  }
317 cs130_tom  1.4  
318 rizwank    1.1  START_TEST(wsock32_main)
319                 {
320 cs130_tom  1.22 	const int numTests = 3;
321 cs130_tom  1.24 	gTestData = malloc(TEST_DATA_SIZE);
322 cs130_tom  1.22   
323                 	trace("test 1 of %d:\n", numTests);
324                 	test_Startup();
325                   
326                 	trace("test 2 of %d:\n", numTests);
327                 	test_ClientServerBlocking_1();
328                 	
329                 	trace("test 3 of %d:\n", numTests);
330                 	test_Cleanup();
331                 
332 cs130_tom  1.30 	trace("all " __FILE__ " tests done\n");
333 cs130_tom  1.20 
334 cs130_tom  1.24 	free(gTestData);
335 rizwank    1.1  }

Rizwan Kassim
Powered by
ViewCVS 0.9.2