(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.8 void ProcessConnection(SOCKET ConnectedSocket)
121                {
122                	// this will handle all connections to the server, it's in its own function to allow for multithreading
123                	close(ConnectedSocket);
124                }
125                
126 cs130_tom  1.6 void BlockingServer(int *port)
127 rizwank    1.3 {
128 cs130_tom  1.5 	SOCKET sock;
129                	SOCKADDR_IN server;
130                	StartNetworkApp(SOCK_STREAM, &sock, &server);
131 cs130_tom  1.6 	*port = server.sin_port;
132 cs130_doug 1.8 	
133                	Handle* ServerThreads;
134                	ServerThreads = malloc(sizeof(Handle) * NUM_CLIENTS);
135                	memset(ServerThreads, 0, sizeof(Handle) * NUM_CLIENTS)
136                	
137                	DWORD* ServerThreadIDs;
138                	ServerThreadIDs = malloc(sizeof(DWORD) * NUM_CLIENTS);
139                	memset(ServerThreadIDs, 0, sizeof(DWORD) * NUM_CLIENTS);
140                
141                	int ThreadIndex = 0;
142                
143                	//SOCKADDR_IN RemoteAddress;
144                
145                	// condition for how long we want the test to run goes here
146                	{
147                			ok(listen(sock, 5) != SOCKET_ERROR, "error listening on socket");
148                			ok(INVALID_SOCK != (ConnectedSocket = accept(sock)), "error accepting socket"); // this can be modified to include the address of the remote socket
149                			ServerThreads[ThreadIndex] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &ProcessConnection, ConnectedSocket, 0, &ServerThreadIDs[ThreadIndex]);
150                			// the line above needs to be cleaned up, it currently copies the connected socket into the called function
151                			// and will then overwrite the local variable.  I'm thinking maybe it needs an array of sockets to handle this
152                			// and then it can pass the address of the socket.
153 cs130_doug 1.8 	}
154 cs130_tom  1.5 
155                	// network code here
156                
157 cs130_doug 1.8 	free(ServerThreads);
158                	free(ServerThreadIDs);
159 cs130_tom  1.5 	trace("blocking server done\n");
160 rizwank    1.3 }
161                
162 cs130_tom  1.4 static void test_ClientServerBlocking_1(void)
163 rizwank    1.3 {
164 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
165 cs130_tom  1.4   HANDLE Thread1, Thread2;
166                  DWORD ThreadId1, ThreadId2;
167 cs130_tom  1.6   Thread1 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &BlockingClient, &serverPort, 0, &ThreadId1);
168                  Thread2 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &BlockingServer, &serverPort, 0, &ThreadId2);
169 cs130_tom  1.5 	trace("test_ClientServerBlocking_1 done\n");
170 rizwank    1.3 }
171                
172 cs130_tom  1.4 static void test_Startup(void)
173 rizwank    1.3 {
174 cs130_tom  1.4 	// initialize application
175                	WSADATA wsaData;
176                  int wsastartup_result = WSAStartup(MAKEWORD(1,1), &wsaData);
177                	if ( (LOBYTE(wsaData.wVersion) != 1) && (HIBYTE(wsaData.wVersion) != 1) )
178                	{
179                		ok( 0 , "WSAStartup returns an incompatible sockets version");
180                		WSACleanup();
181                		exit(0);
182                	}
183                
184                   ok((wsastartup_result == NO_ERROR), "Error in WSAStartup()");
185 rizwank    1.3 }
186                
187 cs130_tom  1.4 
188 rizwank    1.1 START_TEST(wsock32_main)
189                {
190 cs130_tom  1.4   trace("test 1 of 2:\n");
191                  test_Startup();
192                  trace("test 2 of 2:\n");
193                  test_ClientServerBlocking_1();
194                  trace("all tests done\n");
195 cs130_tom  1.7 	while (clientsDone != NUM_CLIENTS)
196                		;
197 rizwank    1.1 }

Rizwan Kassim
Powered by
ViewCVS 0.9.2