(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                #define TRANSFER_SIZE 1000000
 49 cs130_tom  1.4 
 50 cs130_tom  1.7 int clientsDone = 0;
 51                
 52 cs130_tom  1.4 struct TestParams {
 53 cs130_tom  1.5 	int serverSock;
 54                	int serverType;
 55 cs130_tom  1.4 	int serverPort;
 56                	int clientPort[NUM_CLIENTS];
 57                };
 58                
 59                struct ClientParams {
 60                	struct TestParams *test;
 61                	int clientNum; // 1...NUM_CLIENTS
 62                };
 63                
 64                static void test_Startup(void);
 65 cs130_tom  1.6 void BlockingClient(int *serverPort);
 66                void BlockingServer(int *port);
 67 cs130_tom  1.4 static void test_ClientServerBlocking_1(void);
 68                static void test_Startup(void);
 69 rizwank    1.1 
 70 cs130_tom  1.5 // StartNetworkApp creates socket sock of type type and returns assigned port number in addr.
 71                void StartNetworkApp(int type, SOCKET *sock, SOCKADDR_IN *addr)
 72 rizwank    1.1 {
 73 cs130_tom  1.5 	SOCKADDR_IN tmpAddr;
 74                	int tmpAddrSize;
 75 cs130_tom  1.4 
 76 cs130_tom  1.5 	*sock = socket(AF_INET, type, 0);
 77                	if (*sock == INVALID_SOCKET) {
 78 cs130_tom  1.4 		ok( 0 , "Error in socket()");
 79                		WSACleanup();
 80                		exit(0);
 81                	}
 82                	trace("socket() ok\n");
 83                
 84 cs130_tom  1.5 	addr->sin_family = AF_INET;
 85                	addr->sin_addr.s_addr = INADDR_ANY;
 86                	addr->sin_port = htons(0);
 87 cs130_tom  1.4 
 88 cs130_tom  1.5 	if( bind(*sock, (const SOCKADDR *) addr, sizeof(*addr)) ) {
 89                		ok( 0 , "Error binding client to socket");
 90                		WSACleanup();
 91                		exit(0);
 92                	}
 93 cs130_tom  1.4 
 94 cs130_tom  1.5 	// get port number
 95                	tmpAddrSize = sizeof(tmpAddr);
 96                	getsockname(*sock, (SOCKADDR *) &tmpAddr, &tmpAddrSize);
 97                	addr->sin_port = tmpAddr.sin_port;
 98                }
 99 cs130_tom  1.4 
100 cs130_tom  1.6 void BlockingClient(int *serverPort)
101 cs130_tom  1.5 {
102                	SOCKET sock;
103 cs130_tom  1.6 	SOCKADDR_IN client, server;
104                	HOSTENT *hp;
105 cs130_tom  1.5 	StartNetworkApp(SOCK_STREAM, &sock, &client);
106                
107 cs130_tom  1.6 	hp = gethostbyname("localhost");
108                
109                	while(*serverPort == 0) ;
110                
111 cs130_tom  1.5 	// network code here
112 cs130_tom  1.6 	server.sin_family = AF_INET;
113                	server.sin_addr = *(struct in_addr *) hp->h_addr;
114                	server.sin_port = *serverPort;
115 cs130_tom  1.4 
116 cs130_tom  1.5 	trace("blocking client done\n");
117 cs130_tom  1.7 	clientsDone++;
118 rizwank    1.1 }
119                
120 cs130_doug 1.11 void ProcessConnection(SOCKET *ConnectedSocket)
121 cs130_doug 1.8  {
122                 	// this will handle all connections to the server, it's in its own function to allow for multithreading
123 cs130_tom  1.10 	int bClosed;
124                 	bClosed = close(ConnectedSocket);
125                 	//ok(bClosed,"Error closing socket");
126 cs130_doug 1.8  }
127                 
128 cs130_doug 1.11 void BlockingServer(int *port) // listens for incoming connections and accepts up to NUM_CLIENTS connections at once
129 rizwank    1.3  {
130 cs130_doug 1.11 	HANDLE* ServerThreads; // the handles for the threads that process connections
131                 	DWORD* ServerThreadIDs; // the thread ids for the threads that process connections
132                 	SOCKET* ConnectedSockets; // the threads created by accept() that get sent to the processing function
133                 	int ThreadIndex = 0;
134                 
135                 	SOCKET sock;
136                 	SOCKADDR_IN server;
137                 	Handle* ServerThreads;
138 cs130_tom  1.10 	DWORD* ServerThreadIDs;
139                 	int ThreadIndex = 0;
140                 
141 cs130_tom  1.5  	StartNetworkApp(SOCK_STREAM, &sock, &server);
142 cs130_tom  1.6  	*port = server.sin_port;
143 cs130_tom  1.9  
144 cs130_tom  1.10 	ServerThreads = malloc(sizeof(HANDLE) * NUM_CLIENTS);
145                 	memset(ServerThreads, 0, sizeof(HANDLE) * NUM_CLIENTS);
146                 
147 cs130_doug 1.8  	ServerThreadIDs = malloc(sizeof(DWORD) * NUM_CLIENTS);
148                 	memset(ServerThreadIDs, 0, sizeof(DWORD) * NUM_CLIENTS);
149                 
150 cs130_doug 1.11 	ConnectedSockets = malloc(sizeof(DWORD) * NUM_CLIENTS);
151                 	memset(ConnectedSockets, 0, sizeof(DWORD) * NUM_CLIENTS);
152                 
153 cs130_doug 1.8  	//SOCKADDR_IN RemoteAddress;
154                 
155 cs130_doug 1.11 	ListenReturn = listen(sock, 5);
156                 	ok(ListenReturn != SOCKET_ERROR, "error listening on socket");
157                 	
158                 	for (ThreadIndex = 0; ThreadIndex < NUM_CLIENTS; ThreadIndex++)
159 cs130_doug 1.8  	{
160 cs130_doug 1.11 			ConnectedSocket = accept(sock); // this can be modified to include the address of the remote socket
161                 			ok(ConnectedSocket != INVALID_SOCKET, "error accepting socket");
162                 
163                 			ServerThreads[ThreadIndex] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &ProcessConnection, &ConnectedSockets[ThreadIndex], 0, &ServerThreadIDs[ThreadIndex]);
164 cs130_doug 1.8  	}
165 cs130_tom  1.5  
166                 	// network code here
167                 
168 cs130_doug 1.8  	free(ServerThreads);
169                 	free(ServerThreadIDs);
170 cs130_doug 1.11 	free(ConnectedSockets);
171 cs130_tom  1.5  	trace("blocking server done\n");
172 rizwank    1.3  }
173                 
174 cs130_tom  1.4  static void test_ClientServerBlocking_1(void)
175 rizwank    1.3  {
176 cs130_tom  1.9  	int ThreadIndex = 0;
177 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
178 cs130_tom  1.10 	HANDLE ServerThread;
179                 	DWORD ServerThreadId;
180 cs130_tom  1.9  	DWORD *ClientThreadIds;
181                 	HANDLE *ClientThreads;
182                 
183                 	ServerThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &BlockingClient, &serverPort, 0, &ServerThreadId);
184                 
185 cs130_tom  1.10 	ClientThreads = malloc(sizeof(HANDLE) * NUM_CLIENTS);
186                 	memset(ClientThreads, 0, sizeof(HANDLE) * NUM_CLIENTS);
187 cs130_tom  1.9  	
188                 	ClientThreadIds = malloc(sizeof(DWORD) * NUM_CLIENTS);
189                 	memset(ClientThreadIds, 0, sizeof(DWORD) * NUM_CLIENTS);
190                 
191                 	for(ThreadIndex = 0; ThreadIndex < NUM_CLIENTS; ThreadIndex++) {
192                 		ClientThreads[ThreadIndex] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &BlockingServer, &serverPort, 0, &ClientThreadIds[ThreadIndex]);
193                 	}
194                 
195 cs130_tom  1.5  	trace("test_ClientServerBlocking_1 done\n");
196 rizwank    1.3  }
197                 
198 cs130_tom  1.4  static void test_Startup(void)
199 rizwank    1.3  {
200 cs130_tom  1.4  	// initialize application
201                 	WSADATA wsaData;
202                   int wsastartup_result = WSAStartup(MAKEWORD(1,1), &wsaData);
203                 	if ( (LOBYTE(wsaData.wVersion) != 1) && (HIBYTE(wsaData.wVersion) != 1) )
204                 	{
205                 		ok( 0 , "WSAStartup returns an incompatible sockets version");
206                 		WSACleanup();
207                 		exit(0);
208                 	}
209                 
210                    ok((wsastartup_result == NO_ERROR), "Error in WSAStartup()");
211 rizwank    1.3  }
212                 
213 cs130_tom  1.4  
214 rizwank    1.1  START_TEST(wsock32_main)
215                 {
216 cs130_tom  1.4    trace("test 1 of 2:\n");
217                   test_Startup();
218                   trace("test 2 of 2:\n");
219                   test_ClientServerBlocking_1();
220                   trace("all tests done\n");
221 cs130_tom  1.7  	while (clientsDone != NUM_CLIENTS)
222                 		;
223 rizwank    1.1  }

Rizwan Kassim
Powered by
ViewCVS 0.9.2