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

  1 rizwank 1.1 /*
  2 rizwank 1.3  * Unit tests for 32-bit socket functions in Wine
  3 rizwank 1.1  *
  4 rizwank 1.3  * Copyright (c) 2005 Thomas Kho, Fredy Garcia, Douglas Rosenberg
  5 rizwank 1.1  *
  6              * This library is free software; you can redistribute it and/or
  7              * modify it under the terms of the GNU Lesser General Public
  8              * License as published by the Free Software Foundation; either
  9              * version 2.1 of the License, or (at your option) any later version.
 10              *
 11              * This library is distributed in the hope that it will be useful,
 12              * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13              * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 14              * Lesser General Public License for more details.
 15              *
 16              * You should have received a copy of the GNU Lesser General Public
 17              * License along with this library; if not, write to the Free Software
 18              * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 19              */
 20             
 21             #include <stdio.h>
 22             
 23 rizwank 1.3 #include <windows.h>
 24 cs130_tom 1.4 #include <winsock.h>
 25               #include <wtypes.h>
 26               #include <winerror.h>
 27 cs130_doug 1.8 #include <string.h>
 28 rizwank    1.1 
 29                #ifndef STANDALONE
 30                #include "wine/test.h"
 31                #else
 32                #include <assert.h>
 33                #define START_TEST(name) main(int argc, char **argv)
 34                #define ok(condition, msg) \
 35                	do { \
 36                		if(!(condition)) \
 37                		{ \
 38                			fprintf(stderr,"failed at %d\n",__LINE__); \
 39                			exit(0); \
 40                		} \
 41                	} while(0)
 42                
 43                #define todo_wine
 44                #endif
 45                
 46 cs130_tom  1.19 // clients threads to create
 47 cs130_tom  1.22 #define NUM_CLIENTS 64
 48 cs130_tom  1.19 
 49 cs130_tom  1.6  // amount of data to transfer from each client to server
 50 cs130_tom  1.19 #define TEST_DATA_SIZE 145243
 51 cs130_tom  1.4  
 52 cs130_tom  1.19 // tracks number of clients that have successfull transferred data
 53 cs130_tom  1.21 volatile int clientsDone = 0;
 54 cs130_tom  1.19 
 55                 // we often pass this size by reference
 56 cs130_tom  1.12 int sizeofSOCKADDR_IN = sizeof(SOCKADDR_IN);
 57 cs130_tom  1.19 
 58                 // global test data; server sends it to client, then client verifies it
 59 cs130_tom  1.15 char *testData;
 60 cs130_tom  1.7  
 61 cs130_tom  1.4  struct TestParams {
 62 cs130_tom  1.5  	int serverSock;
 63                 	int serverType;
 64 cs130_tom  1.4  	int serverPort;
 65                 	int clientPort[NUM_CLIENTS];
 66                 };
 67                 
 68                 struct ClientParams {
 69                 	struct TestParams *test;
 70                 	int clientNum; // 1...NUM_CLIENTS
 71                 };
 72                 
 73 cs130_tom  1.20 struct MyThread {
 74 cs130_tom  1.16 	HANDLE Handle;
 75                 	DWORD ID;
 76                 };
 77                 
 78 cs130_tom  1.12 struct ServerThread {
 79 cs130_doug 1.13 	HANDLE ServerThread;
 80                 	DWORD ServerThreadID;
 81                 	SOCKET ConnectedSocket; // socket to communicate with client
 82 cs130_tom  1.12 	SOCKADDR_IN Client; // client info
 83                 };
 84                 
 85 cs130_tom  1.4  static void test_Startup(void);
 86 cs130_tom  1.22 static void test_Cleanup(void);
 87 cs130_tom  1.20 void BlockingClient(volatile int *serverPort);
 88                 void BlockingServer(volatile int *port);
 89 cs130_tom  1.4  static void test_ClientServerBlocking_1(void);
 90                 static void test_Startup(void);
 91 rizwank    1.1  
 92 cs130_tom  1.5  // StartNetworkApp creates socket sock of type type and returns assigned port number in addr.
 93                 void StartNetworkApp(int type, SOCKET *sock, SOCKADDR_IN *addr)
 94 rizwank    1.1  {
 95 cs130_tom  1.5  	SOCKADDR_IN tmpAddr;
 96                 	int tmpAddrSize;
 97 cs130_tom  1.19 	int bindOK;
 98 cs130_tom  1.4  
 99 cs130_tom  1.19 	// create socket
100 cs130_tom  1.5  	*sock = socket(AF_INET, type, 0);
101 cs130_tom  1.19 	ok( *sock != INVALID_SOCKET , "Error in socket()");
102 cs130_tom  1.5  	if (*sock == INVALID_SOCKET) {
103 cs130_tom  1.4  		WSACleanup();
104                 		exit(0);
105                 	}
106                 
107 cs130_tom  1.5  	addr->sin_family = AF_INET;
108                 	addr->sin_addr.s_addr = INADDR_ANY;
109                 	addr->sin_port = htons(0);
110 cs130_tom  1.4  
111 cs130_tom  1.19 	// bind socket to port
112                 	bindOK = !bind(*sock, (const SOCKADDR *) addr, sizeof(*addr));
113                 	ok( bindOK , "Error binding client to socket");
114                 	if( !bindOK ) {
115 cs130_tom  1.5  		WSACleanup();
116                 		exit(0);
117                 	}
118 cs130_tom  1.4  
119 cs130_tom  1.5  	// get port number
120                 	tmpAddrSize = sizeof(tmpAddr);
121                 	getsockname(*sock, (SOCKADDR *) &tmpAddr, &tmpAddrSize);
122                 	addr->sin_port = tmpAddr.sin_port;
123                 }
124 cs130_tom  1.4  
125 cs130_tom  1.20 void BlockingClient(volatile int *serverPort)
126 cs130_tom  1.5  {
127                 	SOCKET sock;
128 cs130_tom  1.6  	SOCKADDR_IN client, server;
129                 	HOSTENT *hp;
130 cs130_tom  1.16 	int connectError;
131                 	int totCharsReceived = 0;
132                 	int numCharsReceived;
133                 	int memSame;
134 cs130_tom  1.19 	int yieldCounter = 0;
135 cs130_tom  1.16 	char buf[1001];
136 cs130_tom  1.12 
137 cs130_tom  1.5  	StartNetworkApp(SOCK_STREAM, &sock, &client);
138                 
139 cs130_tom  1.20 	//trace("client port %d\n",ntohs(client.sin_port));
140                 
141 cs130_tom  1.6  	hp = gethostbyname("localhost");
142                 
143 cs130_tom  1.19 	// yield until server determines its random port number
144 cs130_tom  1.17 	while(*serverPort == 0)
145 cs130_tom  1.19 		SwitchToThread();
146 cs130_tom  1.6  
147                 	server.sin_family = AF_INET;
148                 	server.sin_addr = *(struct in_addr *) hp->h_addr;
149                 	server.sin_port = *serverPort;
150 cs130_tom  1.4  
151 cs130_tom  1.19 	// connect to server
152 cs130_tom  1.16 	connectError = connect(sock, (struct sockaddr *)&server, sizeof(struct sockaddr));
153                 	ok( !connectError , "client cannot connect to host\n");
154                 	if(connectError) {
155 cs130_tom  1.12 		WSACleanup();
156                 		exit(0);
157                 	}
158 cs130_tom  1.19 
159                 	// start receiving data from server
160 cs130_tom  1.16 	while( totCharsReceived < TEST_DATA_SIZE ) {
161                 		numCharsReceived = recv(sock, buf, 1000, 0);
162                 		ok( numCharsReceived > 0, "socket was closed unexpectedly" );
163 cs130_tom  1.19 		
164                 		// check received data againt global test data
165 cs130_tom  1.16 		memSame = ! memcmp(buf,testData+totCharsReceived,numCharsReceived);
166 cs130_tom  1.19 		ok( memSame, "data integrity lost during transfer" );
167                 		totCharsReceived += numCharsReceived;
168 cs130_tom  1.18 
169 cs130_tom  1.19 		// yield to our other threads
170                 		if(yieldCounter % 20 == 0) {
171                 			//SwitchToThread();
172 cs130_tom  1.18 		}
173 cs130_tom  1.19 		yieldCounter++;
174 cs130_tom  1.12 	}
175                 
176 cs130_tom  1.22 	//trace("client done\n");
177 cs130_tom  1.7  	clientsDone++;
178 rizwank    1.1  }
179                 
180 cs130_tom  1.17 void ProcessConnection(struct ServerThread *t)
181 cs130_doug 1.8  {
182                 	// this will handle all connections to the server, it's in its own function to allow for multithreading
183 cs130_tom  1.10 	int bClosed;
184 cs130_tom  1.17 	int totCharsSent = 0;
185                 	int numCharsSent;
186 cs130_tom  1.19 	int yieldCounter = 0;
187 cs130_tom  1.17 	const int charsPerSend = 2000;
188                 
189 cs130_tom  1.19 	// loop and send data
190 cs130_tom  1.17 	while( totCharsSent < TEST_DATA_SIZE ) {
191 cs130_tom  1.18 		numCharsSent = send(t->ConnectedSocket, testData+totCharsSent, (totCharsSent + charsPerSend <= TEST_DATA_SIZE) ? charsPerSend : TEST_DATA_SIZE - totCharsSent, 0);
192 cs130_tom  1.19 		ok( numCharsSent != SOCKET_ERROR, "socket error" );
193 cs130_tom  1.17 		if(numCharsSent == SOCKET_ERROR) {
194                 			printf("error code: %d",WSAGetLastError());
195                 		}
196                 		totCharsSent += numCharsSent;
197 cs130_tom  1.19 
198                 		// yield to our other threads
199                 		if(yieldCounter % 20 == 0) {
200                 			//SwitchToThread();
201                 		}
202                 		yieldCounter++;
203 cs130_tom  1.17 	}
204                 
205 cs130_tom  1.19 	bClosed = !closesocket(t->ConnectedSocket);
206                 	ok(bClosed,"Error closing socket");
207 cs130_doug 1.8  }
208                 
209 cs130_tom  1.20 void BlockingServer(volatile int *port) // listens for incoming connections and accepts up to NUM_CLIENTS connections at once
210 rizwank    1.3  {
211 cs130_tom  1.12 	struct ServerThread *Threads;
212 cs130_doug 1.11 	int ThreadIndex = 0;
213                 
214                 	SOCKET sock;
215                 	SOCKADDR_IN server;
216 cs130_tom  1.12 	int ListenReturn;
217 cs130_tom  1.10 
218 cs130_tom  1.5  	StartNetworkApp(SOCK_STREAM, &sock, &server);
219 cs130_tom  1.9  
220 cs130_tom  1.19 	// allocate enough space to keep track of NUM_CLIENTS connections
221 cs130_tom  1.12 	Threads = malloc(sizeof(struct ServerThread) * NUM_CLIENTS);
222                 	memset(Threads, 0, sizeof(struct ServerThread) * NUM_CLIENTS);
223                 
224 cs130_tom  1.19 	// listen on port
225 cs130_tom  1.17 	ListenReturn = listen(sock, NUM_CLIENTS);
226 cs130_doug 1.11 	ok(ListenReturn != SOCKET_ERROR, "error listening on socket");
227 cs130_tom  1.17 
228 cs130_tom  1.19 	// set the port parameter; clients now know we're ready to accept connections
229 cs130_tom  1.17 	*port = server.sin_port;
230                 
231 cs130_tom  1.19 	// we require one connection from each client thread
232 cs130_tom  1.17 	for (ThreadIndex = 0; ThreadIndex < NUM_CLIENTS; ThreadIndex++) {
233 cs130_tom  1.19 			// accept connection
234 cs130_tom  1.14 			Threads[ThreadIndex].ConnectedSocket = accept(sock, (SOCKADDR *) &Threads[ThreadIndex].Client, &sizeofSOCKADDR_IN); // this can be modified to include the address of the remote socket
235                 			ok(Threads[ThreadIndex].ConnectedSocket != INVALID_SOCKET, "error accepting socket");
236 cs130_tom  1.19 
237                 			// spawn thread to handle sending data
238 cs130_tom  1.17 			Threads[ThreadIndex].ServerThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &ProcessConnection, &Threads[ThreadIndex], 0, &Threads[ThreadIndex].ServerThreadID);
239 cs130_doug 1.8  	}
240 cs130_tom  1.5  
241 cs130_tom  1.19 	// wait for all clients to receive data before cleaning up
242 cs130_tom  1.17 	while (clientsDone != NUM_CLIENTS)
243 cs130_tom  1.20 		SwitchToThread();
244 cs130_tom  1.5  
245 cs130_doug 1.13 	free(Threads);
246 rizwank    1.3  }
247                 
248 cs130_tom  1.4  static void test_ClientServerBlocking_1(void)
249 rizwank    1.3  {
250 cs130_tom  1.9  	int ThreadIndex = 0;
251 cs130_tom  1.20 	// tell the compiler not to optimize code relating to serverPort
252                 	volatile int serverPort = 0;
253                 	struct MyThread ServerThread;
254                 	struct MyThread *ClientThreads;
255 cs130_tom  1.9  
256 cs130_tom  1.20 	ClientThreads = malloc(sizeof(struct MyThread) * NUM_CLIENTS);
257                 	memset(ClientThreads, 0, sizeof(struct MyThread) * NUM_CLIENTS);
258 cs130_tom  1.19 	
259                 	trace("starting main server thread\n");
260 cs130_tom  1.20 	ClientThreads[ThreadIndex].Handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &BlockingServer, (void *) &serverPort, 0, &ClientThreads[ThreadIndex].ID);
261 cs130_tom  1.18 
262 cs130_tom  1.9  	for(ThreadIndex = 0; ThreadIndex < NUM_CLIENTS; ThreadIndex++) {
263 cs130_tom  1.20 		ServerThread.Handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &BlockingClient, (void *) &serverPort, 0, &ServerThread.ID);
264 cs130_tom  1.9  	}
265 cs130_tom  1.20 	trace("%d clients started\n", NUM_CLIENTS);
266 cs130_tom  1.9  
267 cs130_tom  1.20 	// wait for all clients to receive data before cleaning up
268 cs130_tom  1.19 	while (clientsDone != NUM_CLIENTS)
269 cs130_tom  1.20 		SwitchToThread();
270 cs130_tom  1.19 
271                 	free(ClientThreads);
272                 
273 cs130_tom  1.5  	trace("test_ClientServerBlocking_1 done\n");
274 rizwank    1.3  }
275                 
276 cs130_tom  1.4  static void test_Startup(void)
277 rizwank    1.3  {
278 cs130_tom  1.4  	// initialize application
279                 	WSADATA wsaData;
280 cs130_tom  1.22 	int wsastartup_result;
281 cs130_tom  1.14 	int versionOK;
282                 
283 cs130_tom  1.19 	// check for compatible winsock version
284 cs130_tom  1.14 	wsastartup_result = WSAStartup(MAKEWORD(1,1), &wsaData);
285                 	versionOK = (LOBYTE(wsaData.wVersion) == 1) && (HIBYTE(wsaData.wVersion) == 1);
286                 
287                 	ok( versionOK , "WSAStartup returns an incompatible sockets version");
288                 	if ( !versionOK ) {
289 cs130_tom  1.4  		WSACleanup();
290                 		exit(0);
291                 	}
292                 
293 cs130_tom  1.22 	ok((wsastartup_result == NO_ERROR), "Error in WSAStartup()");
294                 	trace("startup ok\n");
295                 }
296                 
297                 
298                 static void test_Cleanup(void)
299                 {
300                 	int cleanupOK;
301                 
302                 	cleanupOK = ! WSACleanup();
303                 
304                 	ok( cleanupOK , "error in WSACleanup()");
305                 	trace("cleanup ok\n");
306 rizwank    1.3  }
307 cs130_tom  1.4  
308 rizwank    1.1  START_TEST(wsock32_main)
309                 {
310 cs130_tom  1.22 	const int numTests = 3;
311 cs130_tom  1.15 	testData = malloc(TEST_DATA_SIZE);
312 cs130_tom  1.22   
313                 	trace("test 1 of %d:\n", numTests);
314                 	test_Startup();
315                   
316                 	trace("test 2 of %d:\n", numTests);
317                 	test_ClientServerBlocking_1();
318                 	
319                 	trace("test 3 of %d:\n", numTests);
320                 	test_Cleanup();
321                 
322 cs130_tom  1.4    trace("all tests done\n");
323 cs130_tom  1.20 
324                 	// wait for all clients to receive data before cleaning up
325 cs130_tom  1.7  	while (clientsDone != NUM_CLIENTS)
326 cs130_tom  1.20 		SwitchToThread();
327                 
328 cs130_tom  1.19 	free(testData);
329 rizwank    1.1  }

Rizwan Kassim
Powered by
ViewCVS 0.9.2