(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.20 #define NUM_CLIENTS 1500
 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.20 void BlockingClient(volatile int *serverPort);
 87                 void BlockingServer(volatile int *port);
 88 cs130_tom  1.4  static void test_ClientServerBlocking_1(void);
 89                 static void test_Startup(void);
 90 rizwank    1.1  
 91 cs130_tom  1.5  // StartNetworkApp creates socket sock of type type and returns assigned port number in addr.
 92                 void StartNetworkApp(int type, SOCKET *sock, SOCKADDR_IN *addr)
 93 rizwank    1.1  {
 94 cs130_tom  1.5  	SOCKADDR_IN tmpAddr;
 95                 	int tmpAddrSize;
 96 cs130_tom  1.19 	int bindOK;
 97 cs130_tom  1.4  
 98 cs130_tom  1.19 	// create socket
 99 cs130_tom  1.5  	*sock = socket(AF_INET, type, 0);
100 cs130_tom  1.19 	ok( *sock != INVALID_SOCKET , "Error in socket()");
101 cs130_tom  1.5  	if (*sock == INVALID_SOCKET) {
102 cs130_tom  1.4  		WSACleanup();
103                 		exit(0);
104                 	}
105                 
106 cs130_tom  1.5  	addr->sin_family = AF_INET;
107                 	addr->sin_addr.s_addr = INADDR_ANY;
108                 	addr->sin_port = htons(0);
109 cs130_tom  1.4  
110 cs130_tom  1.19 	// bind socket to port
111                 	bindOK = !bind(*sock, (const SOCKADDR *) addr, sizeof(*addr));
112                 	ok( bindOK , "Error binding client to socket");
113                 	if( !bindOK ) {
114 cs130_tom  1.5  		WSACleanup();
115                 		exit(0);
116                 	}
117 cs130_tom  1.4  
118 cs130_tom  1.5  	// get port number
119                 	tmpAddrSize = sizeof(tmpAddr);
120                 	getsockname(*sock, (SOCKADDR *) &tmpAddr, &tmpAddrSize);
121                 	addr->sin_port = tmpAddr.sin_port;
122                 }
123 cs130_tom  1.4  
124 cs130_tom  1.20 void BlockingClient(volatile int *serverPort)
125 cs130_tom  1.5  {
126                 	SOCKET sock;
127 cs130_tom  1.6  	SOCKADDR_IN client, server;
128                 	HOSTENT *hp;
129 cs130_tom  1.16 	int connectError;
130                 	int totCharsReceived = 0;
131                 	int numCharsReceived;
132                 	int memSame;
133 cs130_tom  1.19 	int yieldCounter = 0;
134 cs130_tom  1.16 	char buf[1001];
135 cs130_tom  1.12 
136 cs130_tom  1.5  	StartNetworkApp(SOCK_STREAM, &sock, &client);
137                 
138 cs130_tom  1.20 	//trace("client port %d\n",ntohs(client.sin_port));
139                 
140 cs130_tom  1.6  	hp = gethostbyname("localhost");
141                 
142 cs130_tom  1.19 	// yield until server determines its random port number
143 cs130_tom  1.17 	while(*serverPort == 0)
144 cs130_tom  1.19 		SwitchToThread();
145 cs130_tom  1.6  
146                 	server.sin_family = AF_INET;
147                 	server.sin_addr = *(struct in_addr *) hp->h_addr;
148                 	server.sin_port = *serverPort;
149 cs130_tom  1.4  
150 cs130_tom  1.19 	// connect to server
151 cs130_tom  1.16 	connectError = connect(sock, (struct sockaddr *)&server, sizeof(struct sockaddr));
152                 	ok( !connectError , "client cannot connect to host\n");
153                 	if(connectError) {
154 cs130_tom  1.12 		WSACleanup();
155                 		exit(0);
156                 	}
157 cs130_tom  1.19 
158                 	// start receiving data from server
159 cs130_tom  1.16 	while( totCharsReceived < TEST_DATA_SIZE ) {
160                 		numCharsReceived = recv(sock, buf, 1000, 0);
161                 		ok( numCharsReceived > 0, "socket was closed unexpectedly" );
162 cs130_tom  1.19 		
163                 		// check received data againt global test data
164 cs130_tom  1.16 		memSame = ! memcmp(buf,testData+totCharsReceived,numCharsReceived);
165 cs130_tom  1.19 		ok( memSame, "data integrity lost during transfer" );
166                 		totCharsReceived += numCharsReceived;
167 cs130_tom  1.18 
168 cs130_tom  1.19 		// yield to our other threads
169                 		if(yieldCounter % 20 == 0) {
170                 			//SwitchToThread();
171 cs130_tom  1.18 		}
172 cs130_tom  1.19 		yieldCounter++;
173 cs130_tom  1.12 	}
174                 
175 cs130_tom  1.19 	trace("client done\n");
176 cs130_tom  1.7  	clientsDone++;
177 rizwank    1.1  }
178                 
179 cs130_tom  1.17 void ProcessConnection(struct ServerThread *t)
180 cs130_doug 1.8  {
181                 	// this will handle all connections to the server, it's in its own function to allow for multithreading
182 cs130_tom  1.10 	int bClosed;
183 cs130_tom  1.17 	int totCharsSent = 0;
184                 	int numCharsSent;
185 cs130_tom  1.19 	int yieldCounter = 0;
186 cs130_tom  1.17 	const int charsPerSend = 2000;
187                 
188 cs130_tom  1.19 	// loop and send data
189 cs130_tom  1.17 	while( totCharsSent < TEST_DATA_SIZE ) {
190 cs130_tom  1.18 		numCharsSent = send(t->ConnectedSocket, testData+totCharsSent, (totCharsSent + charsPerSend <= TEST_DATA_SIZE) ? charsPerSend : TEST_DATA_SIZE - totCharsSent, 0);
191 cs130_tom  1.19 		ok( numCharsSent != SOCKET_ERROR, "socket error" );
192 cs130_tom  1.17 		if(numCharsSent == SOCKET_ERROR) {
193                 			printf("error code: %d",WSAGetLastError());
194                 		}
195                 		totCharsSent += numCharsSent;
196 cs130_tom  1.19 
197                 		// yield to our other threads
198                 		if(yieldCounter % 20 == 0) {
199                 			//SwitchToThread();
200                 		}
201                 		yieldCounter++;
202 cs130_tom  1.17 	}
203                 
204 cs130_tom  1.19 	bClosed = !closesocket(t->ConnectedSocket);
205                 	ok(bClosed,"Error closing socket");
206 cs130_doug 1.8  }
207                 
208 cs130_tom  1.20 void BlockingServer(volatile int *port) // listens for incoming connections and accepts up to NUM_CLIENTS connections at once
209 rizwank    1.3  {
210 cs130_tom  1.12 	struct ServerThread *Threads;
211 cs130_doug 1.11 	int ThreadIndex = 0;
212                 
213                 	SOCKET sock;
214                 	SOCKADDR_IN server;
215 cs130_tom  1.12 	int ListenReturn;
216 cs130_tom  1.10 
217 cs130_tom  1.5  	StartNetworkApp(SOCK_STREAM, &sock, &server);
218 cs130_tom  1.9  
219 cs130_tom  1.19 	// allocate enough space to keep track of NUM_CLIENTS connections
220 cs130_tom  1.12 	Threads = malloc(sizeof(struct ServerThread) * NUM_CLIENTS);
221                 	memset(Threads, 0, sizeof(struct ServerThread) * NUM_CLIENTS);
222                 
223 cs130_tom  1.19 	// listen on port
224 cs130_tom  1.17 	ListenReturn = listen(sock, NUM_CLIENTS);
225 cs130_doug 1.11 	ok(ListenReturn != SOCKET_ERROR, "error listening on socket");
226 cs130_tom  1.17 
227 cs130_tom  1.19 	// set the port parameter; clients now know we're ready to accept connections
228 cs130_tom  1.17 	*port = server.sin_port;
229                 
230 cs130_tom  1.19 	// we require one connection from each client thread
231 cs130_tom  1.17 	for (ThreadIndex = 0; ThreadIndex < NUM_CLIENTS; ThreadIndex++) {
232 cs130_tom  1.19 			// accept connection
233 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
234                 			ok(Threads[ThreadIndex].ConnectedSocket != INVALID_SOCKET, "error accepting socket");
235 cs130_tom  1.19 
236                 			// spawn thread to handle sending data
237 cs130_tom  1.17 			Threads[ThreadIndex].ServerThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &ProcessConnection, &Threads[ThreadIndex], 0, &Threads[ThreadIndex].ServerThreadID);
238 cs130_doug 1.8  	}
239 cs130_tom  1.5  
240 cs130_tom  1.19 	// wait for all clients to receive data before cleaning up
241 cs130_tom  1.17 	while (clientsDone != NUM_CLIENTS)
242 cs130_tom  1.20 		SwitchToThread();
243 cs130_tom  1.5  
244 cs130_doug 1.13 	free(Threads);
245 rizwank    1.3  }
246                 
247 cs130_tom  1.4  static void test_ClientServerBlocking_1(void)
248 rizwank    1.3  {
249 cs130_tom  1.9  	int ThreadIndex = 0;
250 cs130_tom  1.20 	// tell the compiler not to optimize code relating to serverPort
251                 	volatile int serverPort = 0;
252                 	struct MyThread ServerThread;
253                 	struct MyThread *ClientThreads;
254 cs130_tom  1.9  
255 cs130_tom  1.20 	ClientThreads = malloc(sizeof(struct MyThread) * NUM_CLIENTS);
256                 	memset(ClientThreads, 0, sizeof(struct MyThread) * NUM_CLIENTS);
257 cs130_tom  1.19 	
258                 	trace("starting main server thread\n");
259 cs130_tom  1.20 	ClientThreads[ThreadIndex].Handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &BlockingServer, (void *) &serverPort, 0, &ClientThreads[ThreadIndex].ID);
260 cs130_tom  1.18 
261 cs130_tom  1.9  	for(ThreadIndex = 0; ThreadIndex < NUM_CLIENTS; ThreadIndex++) {
262 cs130_tom  1.20 		ServerThread.Handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &BlockingClient, (void *) &serverPort, 0, &ServerThread.ID);
263 cs130_tom  1.9  	}
264 cs130_tom  1.20 	trace("%d clients started\n", NUM_CLIENTS);
265 cs130_tom  1.9  
266 cs130_tom  1.20 	// wait for all clients to receive data before cleaning up
267 cs130_tom  1.19 	while (clientsDone != NUM_CLIENTS)
268 cs130_tom  1.20 		SwitchToThread();
269 cs130_tom  1.19 
270                 	free(ClientThreads);
271                 
272 cs130_tom  1.5  	trace("test_ClientServerBlocking_1 done\n");
273 rizwank    1.3  }
274                 
275 cs130_tom  1.4  static void test_Startup(void)
276 rizwank    1.3  {
277 cs130_tom  1.4  	// initialize application
278                 	WSADATA wsaData;
279 cs130_tom  1.14   int wsastartup_result;
280                 	int versionOK;
281                 
282 cs130_tom  1.19 	// check for compatible winsock version
283 cs130_tom  1.14 	wsastartup_result = WSAStartup(MAKEWORD(1,1), &wsaData);
284                 	versionOK = (LOBYTE(wsaData.wVersion) == 1) && (HIBYTE(wsaData.wVersion) == 1);
285                 
286                 	ok( versionOK , "WSAStartup returns an incompatible sockets version");
287                 	if ( !versionOK ) {
288 cs130_tom  1.4  		WSACleanup();
289                 		exit(0);
290                 	}
291                 
292                    ok((wsastartup_result == NO_ERROR), "Error in WSAStartup()");
293 cs130_tom  1.19 	 trace("startup ok\n");
294 rizwank    1.3  }
295 cs130_tom  1.4  
296 rizwank    1.1  START_TEST(wsock32_main)
297                 {
298 cs130_tom  1.15 	testData = malloc(TEST_DATA_SIZE);
299 cs130_tom  1.4    trace("test 1 of 2:\n");
300                   test_Startup();
301                   trace("test 2 of 2:\n");
302                   test_ClientServerBlocking_1();
303                   trace("all tests done\n");
304 cs130_tom  1.20 
305                 	// wait for all clients to receive data before cleaning up
306 cs130_tom  1.7  	while (clientsDone != NUM_CLIENTS)
307 cs130_tom  1.20 		SwitchToThread();
308                 
309 cs130_tom  1.19 	free(testData);
310 rizwank    1.1  }

Rizwan Kassim
Powered by
ViewCVS 0.9.2