(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 rizwank   1.1 
 28               #ifndef STANDALONE
 29               #include "wine/test.h"
 30               #else
 31               #include <assert.h>
 32               #define START_TEST(name) main(int argc, char **argv)
 33               #define ok(condition, msg) \
 34               	do { \
 35               		if(!(condition)) \
 36               		{ \
 37               			fprintf(stderr,"failed at %d\n",__LINE__); \
 38               			exit(0); \
 39               		} \
 40               	} while(0)
 41               
 42               #define todo_wine
 43               #endif
 44               
 45 cs130_tom 1.4 #define NUM_CLIENTS 5
 46               
 47               struct TestParams {
 48 cs130_tom 1.5 	int serverSock;
 49               	int serverType;
 50 cs130_tom 1.4 	int serverPort;
 51               	int clientPort[NUM_CLIENTS];
 52               };
 53               
 54               struct ClientParams {
 55               	struct TestParams *test;
 56               	int clientNum; // 1...NUM_CLIENTS
 57               };
 58               
 59               static void test_Startup(void);
 60               void BlockingClient();
 61               void BlockingServer();
 62               static void test_ClientServerBlocking_1(void);
 63               static void test_Startup(void);
 64 rizwank   1.1 
 65 cs130_tom 1.5 // StartNetworkApp creates socket sock of type type and returns assigned port number in addr.
 66               void StartNetworkApp(int type, SOCKET *sock, SOCKADDR_IN *addr)
 67 rizwank   1.1 {
 68 cs130_tom 1.5 	SOCKADDR_IN tmpAddr;
 69               	int tmpAddrSize;
 70 cs130_tom 1.4 
 71 cs130_tom 1.5 	*sock = socket(AF_INET, type, 0);
 72               	if (*sock == INVALID_SOCKET) {
 73 cs130_tom 1.4 		ok( 0 , "Error in socket()");
 74               		WSACleanup();
 75               		exit(0);
 76               	}
 77               	trace("socket() ok\n");
 78               
 79 cs130_tom 1.5 	addr->sin_family = AF_INET;
 80               	addr->sin_addr.s_addr = INADDR_ANY;
 81               	addr->sin_port = htons(0);
 82 cs130_tom 1.4 
 83 cs130_tom 1.5 	if( bind(*sock, (const SOCKADDR *) addr, sizeof(*addr)) ) {
 84               		ok( 0 , "Error binding client to socket");
 85               		WSACleanup();
 86               		exit(0);
 87               	}
 88 cs130_tom 1.4 
 89 cs130_tom 1.5 	// get port number
 90               	tmpAddrSize = sizeof(tmpAddr);
 91               	getsockname(*sock, (SOCKADDR *) &tmpAddr, &tmpAddrSize);
 92               	addr->sin_port = tmpAddr.sin_port;
 93               }
 94 cs130_tom 1.4 
 95 cs130_tom 1.5 void BlockingClient()
 96               {
 97               	SOCKET sock;
 98               	SOCKADDR_IN client;
 99               	StartNetworkApp(SOCK_STREAM, &sock, &client);
100               
101               	// network code here
102 cs130_tom 1.4 
103 cs130_tom 1.5 	trace("blocking client done\n");
104 rizwank   1.1 }
105               
106 cs130_tom 1.4 void BlockingServer()
107 rizwank   1.3 {
108 cs130_tom 1.5 	SOCKET sock;
109               	SOCKADDR_IN server;
110               	StartNetworkApp(SOCK_STREAM, &sock, &server);
111               
112               	// network code here
113               
114               	trace("blocking server done\n");
115 rizwank   1.3 }
116               
117 cs130_tom 1.4 static void test_ClientServerBlocking_1(void)
118 rizwank   1.3 {
119 cs130_tom 1.4   HANDLE Thread1, Thread2;
120                 DWORD ThreadId1, ThreadId2;
121                 Thread1 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &BlockingClient, NULL, 0, &ThreadId1);
122                 Thread2 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &BlockingServer, NULL, 0, &ThreadId2);
123 cs130_tom 1.5 	trace("test_ClientServerBlocking_1 done\n");
124 rizwank   1.3 }
125               
126 cs130_tom 1.4 static void test_Startup(void)
127 rizwank   1.3 {
128 cs130_tom 1.4 	// initialize application
129               	WSADATA wsaData;
130                 int wsastartup_result = WSAStartup(MAKEWORD(1,1), &wsaData);
131               	if ( (LOBYTE(wsaData.wVersion) != 1) && (HIBYTE(wsaData.wVersion) != 1) )
132               	{
133               		ok( 0 , "WSAStartup returns an incompatible sockets version");
134               		WSACleanup();
135               		exit(0);
136               	}
137               
138                  ok((wsastartup_result == NO_ERROR), "Error in WSAStartup()");
139 rizwank   1.3 }
140               
141 cs130_tom 1.4 
142 rizwank   1.1 START_TEST(wsock32_main)
143               {
144 cs130_tom 1.4   trace("test 1 of 2:\n");
145                 test_Startup();
146                 trace("test 2 of 2:\n");
147                 test_ClientServerBlocking_1();
148                 trace("all tests done\n");
149 rizwank   1.1 }

Rizwan Kassim
Powered by
ViewCVS 0.9.2