os/ossrv/genericopenlibs/cstdlib/TSTLIB/TNETDB.C
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/TSTLIB/TNETDB.C	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,257 @@
     1.4 +/*
     1.5 +* Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description:
    1.18 +* Test of NETDB.H functions - you need a real Internet connection for this!
    1.19 +* 
    1.20 +*
    1.21 +*/
    1.22 +
    1.23 +
    1.24 +
    1.25 +#include <stdio.h>
    1.26 +#include <errno.h>
    1.27 +#include <stdlib.h>
    1.28 +#include <string.h>
    1.29 +#include <unistd.h>
    1.30 +#include <sys/types.h>
    1.31 +#include <sys/socket.h>
    1.32 +#include <sys/ioctl.h>
    1.33 +#include <libc/netinet/in.h>
    1.34 +#include <libc/arpa/inet.h>
    1.35 +#include <netdb.h>
    1.36 +#include <errno.h>
    1.37 +
    1.38 +#include "CTEST.H"	/* includes C interface to EPOC32 threads, and SpawnPosixServer */
    1.39 +
    1.40 +test_Data;
    1.41 +
    1.42 +#define N_ADDRESSES	3
    1.43 +unsigned long addresses[N_ADDRESSES][3] =
    1.44 +	{
    1.45 +		{ 121, 0x112233, 0x79112233 },	/* class A 121.xxxxxxxx */
    1.46 +		{ 33771, 0x1122, 0x83eb1122 },	/* class B 131.235.xxxx */
    1.47 +		{ 12747009,  90, 0xc281015a }	/* class C 194.129.1.xx */
    1.48 +	};
    1.49 +
    1.50 +/**
    1.51 +@SYMTestCaseID          SYSLIB-STDLIB-CT-1072
    1.52 +@SYMTestCaseDesc	    Tests for ARPA net functions
    1.53 +@SYMTestPriority 	    High
    1.54 +@SYMTestActions  	    Tests for all basic network functions 
    1.55 +@SYMTestExpectedResults Test must not fail
    1.56 +@SYMREQ                 REQ0000
    1.57 +*/
    1.58 +void testArpa()
    1.59 +	{
    1.60 +	char* cp;
    1.61 +	char* cp2;
    1.62 +	struct in_addr iaddr;
    1.63 +	unsigned long ul1, ul2;
    1.64 +	int err;
    1.65 +	int i;
    1.66 +
    1.67 +	test_Next("ARPA/INET.H functions");
    1.68 +
    1.69 +	iaddr.s_addr=11;
    1.70 +	cp="16.33.50.67";
    1.71 +	err=inet_aton(cp, &iaddr);
    1.72 +	test(err==1);
    1.73 +	test(iaddr.s_addr==htonl(0x10213243));
    1.74 +	test(iaddr.s_addr==inet_addr(cp));
    1.75 +	cp2=inet_ntoa(iaddr);
    1.76 +	test(strcmp(cp2,cp)==0);
    1.77 +
    1.78 +	iaddr.s_addr=11;
    1.79 +	err=inet_aton("16.rubbish.67", &iaddr);
    1.80 +	test(err==0);
    1.81 +
    1.82 +	for (i=0;i<N_ADDRESSES;i++)
    1.83 +		{
    1.84 +		iaddr=inet_makeaddr(addresses[i][0], addresses[i][1]);
    1.85 +		test(iaddr.s_addr==ntohl(addresses[i][2]));
    1.86 +		ul1=inet_netof(iaddr);
    1.87 +		ul2=inet_lnaof(iaddr);
    1.88 +		test(ul1==addresses[i][0]);
    1.89 +		test(ul2==addresses[i][1]);
    1.90 +		}
    1.91 +	}
    1.92 +
    1.93 +#define N_NAMES	3
    1.94 +const char* names[N_NAMES][3] = 
    1.95 +	{
    1.96 +		{ "phoenix.doc.ic.ac.uk",  "146.169.1.160",    "phoenix.doc.ic.ac.uk" },
    1.97 +		{ "httpsmtp.test.intra",   "192.168.20.11",   "httpsmtp.test.intra" },
    1.98 +		{ "unix.sri.com",          "128.18.30.211",    "unix.sri.com" }
    1.99 +	};
   1.100 +
   1.101 +int close_console=0;
   1.102 +
   1.103 +/**
   1.104 +@SYMTestCaseID          SYSLIB-STDLIB-CT-1073
   1.105 +@SYMTestCaseDesc	    Tests for ARPA net functions
   1.106 +@SYMTestPriority 	    High
   1.107 +@SYMTestActions  	    Tests for get host by name and host by address functions
   1.108 +@SYMTestExpectedResults Test must not fail
   1.109 +@SYMREQ                 REQ0000
   1.110 +*/
   1.111 +void testNetDB()
   1.112 +	{
   1.113 +	char hostname[128];
   1.114 +	struct in_addr addr, *addrp;
   1.115 +	int err, i;
   1.116 +	struct hostent *hp;
   1.117 +
   1.118 +	test_Next("Get Host Name");
   1.119 +
   1.120 +	err=gethostname(hostname,sizeof(hostname));
   1.121 +	test(err==0);
   1.122 +	printf("  hostname = >%s<\n", hostname);
   1.123 +
   1.124 +	test_Next("Get Host By Name");
   1.125 +
   1.126 +	for (i=0; i<N_NAMES; i++)
   1.127 +		{
   1.128 +		hp=gethostbyname(names[i][0]);
   1.129 +		test_ok(hp!=0);
   1.130 +		addrp=(struct in_addr*)(hp->h_addr_list[0]);
   1.131 +		printf("  %-30s => address %-15s\n", hp->h_name, inet_ntoa(*addrp));
   1.132 +		test(strcasecmp(hp->h_name,names[i][0])==0);
   1.133 +		test(addrp->s_addr==inet_addr(names[i][1]));
   1.134 +		}
   1.135 +
   1.136 +	hp=gethostbyname("nosuchname.symbian.com");
   1.137 +	test_errno(hp==0,ENOENT);
   1.138 +	test(errno==HOST_NOT_FOUND);
   1.139 +
   1.140 +	test_Next("Get Address of \"\"");
   1.141 +	hp=gethostbyname("");
   1.142 +	test_ok(hp!=0);
   1.143 +	addrp=(struct in_addr*)(hp->h_addr_list[0]);
   1.144 +	printf("  %-30s => address %-15s\n", hp->h_name, inet_ntoa(*addrp));
   1.145 +
   1.146 +	test_Next("Get Host By Addr");
   1.147 +
   1.148 +	for (i=0; i<N_NAMES; i++)
   1.149 +		{
   1.150 +		addr.s_addr=inet_addr(names[i][1]);
   1.151 +		hp=gethostbyaddr((const char *)&addr,sizeof(addr),AF_INET);
   1.152 +		test_ok(hp!=0);
   1.153 +		addrp=(struct in_addr*)(hp->h_addr_list[0]);
   1.154 +		printf("  address %-15s => %s\n", inet_ntoa(*addrp), hp->h_name);
   1.155 +		test(addrp->s_addr==addr.s_addr);
   1.156 +		test(strcasecmp(hp->h_name,names[i][2])==0);
   1.157 +		}
   1.158 +
   1.159 +	addr.s_addr=inet_addr("10.11.199.255");
   1.160 +	hp=gethostbyaddr((const char *)&addr,sizeof(addr),AF_INET);
   1.161 +	test_errno(hp==0,ENOENT);
   1.162 +	test(errno==HOST_NOT_FOUND);
   1.163 +	
   1.164 +/*
   1.165 +	struct sockaddr_in testaddr;
   1.166 +	int fd;
   1.167 +	test_Next("Connect to the Imperial College Echo server");
   1.168 +
   1.169 +	fd=socket(AF_INET, SOCK_STREAM, 0);
   1.170 +	test_ok(fd>=0);
   1.171 +	testaddr.sin_family=AF_INET;
   1.172 +	testaddr.sin_addr.s_addr=inet_addr("193.63.255.1");
   1.173 +	testaddr.sin_port=htons(7);	// echo
   1.174 +	err=connect(fd,(struct sockaddr*)&testaddr, sizeof(testaddr));
   1.175 +	test(err==0);
   1.176 +	close(fd);
   1.177 +*/
   1.178 +
   1.179 +	test_Next("Get Address of roundrobin.test.intra which has multiple address");
   1.180 +	hp=gethostbyname("roundrobin.test.intra");	
   1.181 +	test_ok(hp!=0);
   1.182 +
   1.183 +	if (hp)
   1.184 +		{
   1.185 +		if (hp->h_addr_list)
   1.186 +			{
   1.187 +			int Index = 0;
   1.188 +			while (hp->h_addr_list[Index])
   1.189 +				{
   1.190 +				addrp = (struct in_addr*)(hp->h_addr_list[Index]);
   1.191 +				printf("  %-30s => address %-15s\n", hp->h_name, inet_ntoa(*addrp));
   1.192 +				Index++;
   1.193 +				}
   1.194 +			}
   1.195 +		}
   1.196 +
   1.197 +	test_Next("Get Host name of 192.168.255.4 which has multiple host name");
   1.198 +	addr.s_addr=inet_addr("192.168.255.4");
   1.199 +	hp=gethostbyaddr((const char *)&addr,sizeof(addr),AF_INET);
   1.200 +	test_ok(hp!=0);
   1.201 +
   1.202 +	if (hp)
   1.203 +		{
   1.204 +		addrp=(struct in_addr*)(hp->h_addr_list[0]);
   1.205 +		printf("  address %-15s => %s\n", inet_ntoa(*addrp), hp->h_name);
   1.206 +
   1.207 +		if (hp->h_aliases)
   1.208 +			{
   1.209 +			int Index = 0;
   1.210 +			while (hp->h_aliases[Index])
   1.211 +				{
   1.212 +				printf("  address %-15s => %s\n", inet_ntoa(*addrp), hp->h_aliases[Index]);
   1.213 +				Index++;
   1.214 +				}
   1.215 +			}
   1.216 +		}
   1.217 +
   1.218 +	if (close_console)
   1.219 +		{
   1.220 +		test_Close();
   1.221 +		close(0);
   1.222 +		close(1);
   1.223 +		close(2);
   1.224 +		}
   1.225 +	}
   1.226 +
   1.227 +
   1.228 +int main(int argc, char *argv[])
   1.229 +	{
   1.230 +	void* client;
   1.231 +	int err;
   1.232 +
   1.233 +	test_Title("NETDB.H Functionality");
   1.234 +
   1.235 +	err=CommInit(0);	/* ensure a workable comms environment */
   1.236 +	test(err==0);
   1.237 +
   1.238 +	testArpa();	/* doesn't use the MSystemInterface so only tested once */
   1.239 +
   1.240 +	if (argc==1)
   1.241 +		{
   1.242 +		/* Run the test(s) without a CPosixServer first */		
   1.243 +		testNetDB();
   1.244 +		}
   1.245 +
   1.246 +	test_Next("Do it all using the CPosixServer (for them, not me)");
   1.247 +	close_console=1;
   1.248 +
   1.249 +	start_posix_server();	/* calls SpawnPosixServer from C++ code */
   1.250 +
   1.251 +	client=create_thread(testNetDB, "TNETDB NetDB");
   1.252 +	test(client!=0);
   1.253 +	start_thread(client);
   1.254 +	err=wait_for_thread(client);
   1.255 +	test(err==0);
   1.256 +
   1.257 +	test_Close();
   1.258 +	exit(0);
   1.259 +	return 0;
   1.260 +	}