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

Rizwan Kassim
Powered by
ViewCVS 0.9.2