(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.7 #define NUM_CLIENTS 1
 47 cs130_tom  1.6 // amount of data to transfer from each client to server
 48 cs130_tom  1.15 #define TEST_DATA_SIZE 100000
 49 cs130_tom  1.4  
 50 cs130_tom  1.7  int clientsDone = 0;
 51 cs130_tom  1.12 int sizeofSOCKADDR_IN = sizeof(SOCKADDR_IN);
 52 cs130_tom  1.15 char *testData;
 53 cs130_tom  1.7  
 54 cs130_tom  1.4  struct TestParams {
 55 cs130_tom  1.5  	int serverSock;
 56                 	int serverType;
 57 cs130_tom  1.4  	int serverPort;
 58                 	int clientPort[NUM_CLIENTS];
 59                 };
 60                 
 61                 struct ClientParams {
 62                 	struct TestParams *test;
 63                 	int clientNum; // 1...NUM_CLIENTS
 64                 };
 65                 
 66 cs130_tom  1.16 struct Thread {
 67                 	HANDLE Handle;
 68                 	DWORD ID;
 69                 };
 70                 
 71 cs130_tom  1.12 struct ServerThread {
 72 cs130_doug 1.13 	HANDLE ServerThread;
 73                 	DWORD ServerThreadID;
 74                 	SOCKET ConnectedSocket; // socket to communicate with client
 75 cs130_tom  1.12 	SOCKADDR_IN Client; // client info
 76                 };
 77                 
 78 cs130_tom  1.16 extern int close(int);
 79                 
 80 cs130_tom  1.4  static void test_Startup(void);
 81 cs130_tom  1.6  void BlockingClient(int *serverPort);
 82                 void BlockingServer(int *port);
 83 cs130_tom  1.4  static void test_ClientServerBlocking_1(void);
 84                 static void test_Startup(void);
 85 rizwank    1.1  
 86 cs130_tom  1.5  // StartNetworkApp creates socket sock of type type and returns assigned port number in addr.
 87                 void StartNetworkApp(int type, SOCKET *sock, SOCKADDR_IN *addr)
 88 rizwank    1.1  {
 89 cs130_tom  1.5  	SOCKADDR_IN tmpAddr;
 90                 	int tmpAddrSize;
 91 cs130_tom  1.4  
 92 cs130_tom  1.5  	*sock = socket(AF_INET, type, 0);
 93                 	if (*sock == INVALID_SOCKET) {
 94 cs130_tom  1.4  		ok( 0 , "Error in socket()");
 95                 		WSACleanup();
 96                 		exit(0);
 97                 	}
 98                 	trace("socket() ok\n");
 99                 
100 cs130_tom  1.5  	addr->sin_family = AF_INET;
101                 	addr->sin_addr.s_addr = INADDR_ANY;
102                 	addr->sin_port = htons(0);
103 cs130_tom  1.4  
104 cs130_tom  1.5  	if( bind(*sock, (const SOCKADDR *) addr, sizeof(*addr)) ) {
105                 		ok( 0 , "Error binding client to socket");
106                 		WSACleanup();
107                 		exit(0);
108                 	}
109 cs130_tom  1.4  
110 cs130_tom  1.5  	// get port number
111                 	tmpAddrSize = sizeof(tmpAddr);
112                 	getsockname(*sock, (SOCKADDR *) &tmpAddr, &tmpAddrSize);
113                 	addr->sin_port = tmpAddr.sin_port;
114                 }
115 cs130_tom  1.4  
116 cs130_tom  1.6  void BlockingClient(int *serverPort)
117 cs130_tom  1.5  {
118                 	SOCKET sock;
119 cs130_tom  1.6  	SOCKADDR_IN client, server;
120                 	HOSTENT *hp;
121 cs130_tom  1.16 	int connectError;
122                 	int totCharsReceived = 0;
123                 	int numCharsReceived;
124                 	int memSame;
125                 	char buf[1001];
126 cs130_tom  1.12 	char* msg="Some message to send to server";
127                 
128 cs130_tom  1.5  	StartNetworkApp(SOCK_STREAM, &sock, &client);
129                 
130 cs130_tom  1.6  	hp = gethostbyname("localhost");
131                 
132 cs130_tom  1.17 	while(*serverPort == 0)
133                 		;
134 cs130_tom  1.6  
135 cs130_tom  1.5  	// network code here
136 cs130_tom  1.6  	server.sin_family = AF_INET;
137                 	server.sin_addr = *(struct in_addr *) hp->h_addr;
138                 	server.sin_port = *serverPort;
139 cs130_tom  1.4  
140 cs130_tom  1.16 	connectError = connect(sock, (struct sockaddr *)&server, sizeof(struct sockaddr));
141                 	ok( !connectError , "client cannot connect to host\n");
142                 	if(connectError) {
143 cs130_tom  1.12 		WSACleanup();
144                 		exit(0);
145                 	}
146 cs130_tom  1.17 	printf("client starting data transfer\n");
147 cs130_tom  1.16 	while( totCharsReceived < TEST_DATA_SIZE ) {
148                 		numCharsReceived = recv(sock, buf, 1000, 0);
149                 		ok( numCharsReceived > 0, "socket was closed unexpectedly" );
150                 
151                 		memSame = ! memcmp(buf,testData+totCharsReceived,numCharsReceived);
152                 		ok( memSame, "data integrity lost during transfer" );
153                 
154                 		totCharsReceived += numCharsReceived;
155 cs130_tom  1.17 		printf("received %d chars\n", numCharsReceived);
156 cs130_tom  1.12 	}
157 cs130_tom  1.17 	printf("client data transfer completed\n");
158 cs130_tom  1.12 
159 cs130_tom  1.5  	trace("blocking client done\n");
160 cs130_tom  1.7  	clientsDone++;
161 rizwank    1.1  }
162                 
163 cs130_tom  1.17 void ProcessConnection(struct ServerThread *t)
164 cs130_doug 1.8  {
165                 	// this will handle all connections to the server, it's in its own function to allow for multithreading
166 cs130_tom  1.10 	int bClosed;
167 cs130_tom  1.17 	int totCharsSent = 0;
168                 	int numCharsSent;
169                 	const int charsPerSend = 2000;
170                 
171                 	printf("server starting data transfer\n");
172                 	while( totCharsSent < TEST_DATA_SIZE ) {
173                 		numCharsSent = send(t->ConnectedSocket, testData+totCharsSent, charsPerSend, 0);
174                 		//ok( numCharsSent != SOCKET_ERROR, "socket error" );
175                 		if(numCharsSent == SOCKET_ERROR) {
176                 			printf("error code: %d",WSAGetLastError());
177                 		}
178                 		totCharsSent += numCharsSent;
179                 		printf("sent %d block\n", numCharsSent);
180                 	}
181                 	printf("server data transfer completed\n");
182                 
183                 	bClosed = close(t->ConnectedSocket);
184 cs130_tom  1.10 	//ok(bClosed,"Error closing socket");
185 cs130_doug 1.8  }
186                 
187 cs130_doug 1.11 void BlockingServer(int *port) // listens for incoming connections and accepts up to NUM_CLIENTS connections at once
188 rizwank    1.3  {
189 cs130_tom  1.12 	struct ServerThread *Threads;
190 cs130_doug 1.11 	int ThreadIndex = 0;
191                 
192                 	SOCKET sock;
193                 	SOCKADDR_IN server;
194 cs130_tom  1.12 	int ListenReturn;
195 cs130_tom  1.10 
196 cs130_tom  1.5  	StartNetworkApp(SOCK_STREAM, &sock, &server);
197 cs130_tom  1.9  
198 cs130_tom  1.12 	Threads = malloc(sizeof(struct ServerThread) * NUM_CLIENTS);
199                 	memset(Threads, 0, sizeof(struct ServerThread) * NUM_CLIENTS);
200                 
201 cs130_tom  1.17 	ListenReturn = listen(sock, NUM_CLIENTS);
202 cs130_doug 1.11 	ok(ListenReturn != SOCKET_ERROR, "error listening on socket");
203 cs130_tom  1.17 
204                 	*port = server.sin_port;
205                 
206                 	for (ThreadIndex = 0; ThreadIndex < NUM_CLIENTS; ThreadIndex++) {
207 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
208                 			ok(Threads[ThreadIndex].ConnectedSocket != INVALID_SOCKET, "error accepting socket");
209 cs130_doug 1.11 
210 cs130_tom  1.17 			Threads[ThreadIndex].ServerThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &ProcessConnection, &Threads[ThreadIndex], 0, &Threads[ThreadIndex].ServerThreadID);
211 cs130_doug 1.8  	}
212 cs130_tom  1.5  
213                 	// network code here
214 cs130_tom  1.17 
215                 	while (clientsDone != NUM_CLIENTS)
216                 		;
217 cs130_tom  1.5  
218 cs130_doug 1.13 	free(Threads);
219 cs130_tom  1.5  	trace("blocking server done\n");
220 rizwank    1.3  }
221                 
222 cs130_tom  1.4  static void test_ClientServerBlocking_1(void)
223 rizwank    1.3  {
224 cs130_tom  1.9  	int ThreadIndex = 0;
225 cs130_doug 1.8  	int serverPort = 0; // I think the server port would work better as a #DEFINE rather than a variable that gets passed around everywhere
226 cs130_tom  1.16 	struct Thread ServerThread;
227                 	struct Thread *ClientThreads;
228 cs130_tom  1.9  
229 cs130_tom  1.16 	ServerThread.Handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &BlockingClient, &serverPort, 0, &ServerThread.ID);
230 cs130_tom  1.9  
231 cs130_tom  1.16 	ClientThreads = malloc(sizeof(struct Thread) * NUM_CLIENTS);
232                 	memset(ClientThreads, 0, sizeof(struct Thread) * NUM_CLIENTS);
233 cs130_tom  1.9  
234                 	for(ThreadIndex = 0; ThreadIndex < NUM_CLIENTS; ThreadIndex++) {
235 cs130_tom  1.16 		ClientThreads[ThreadIndex].Handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &BlockingServer, &serverPort, 0, &ClientThreads[ThreadIndex].ID);
236 cs130_tom  1.9  	}
237                 
238 cs130_tom  1.5  	trace("test_ClientServerBlocking_1 done\n");
239 rizwank    1.3  }
240                 
241 cs130_tom  1.4  static void test_Startup(void)
242 rizwank    1.3  {
243 cs130_tom  1.4  	// initialize application
244                 	WSADATA wsaData;
245 cs130_tom  1.14   int wsastartup_result;
246                 	int versionOK;
247                 
248                 	wsastartup_result = WSAStartup(MAKEWORD(1,1), &wsaData);
249                 	versionOK = (LOBYTE(wsaData.wVersion) == 1) && (HIBYTE(wsaData.wVersion) == 1);
250                 
251                 	ok( versionOK , "WSAStartup returns an incompatible sockets version");
252                 	if ( !versionOK ) {
253 cs130_tom  1.4  		WSACleanup();
254                 		exit(0);
255                 	}
256                 
257                    ok((wsastartup_result == NO_ERROR), "Error in WSAStartup()");
258 rizwank    1.3  }
259 cs130_tom  1.4  
260 rizwank    1.1  START_TEST(wsock32_main)
261                 {
262 cs130_tom  1.15 	testData = malloc(TEST_DATA_SIZE);
263 cs130_tom  1.4    trace("test 1 of 2:\n");
264                   test_Startup();
265                   trace("test 2 of 2:\n");
266                   test_ClientServerBlocking_1();
267                   trace("all tests done\n");
268 cs130_tom  1.7  	while (clientsDone != NUM_CLIENTS)
269                 		;
270 rizwank    1.1  }

Rizwan Kassim
Powered by
ViewCVS 0.9.2