os/ossrv/genericopenlibs/cstdlib/TSTLIB/TNETDB.C
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     5 * under the terms of "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     8 *
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    11 *
    12 * Contributors:
    13 *
    14 * Description:
    15 * Test of NETDB.H functions - you need a real Internet connection for this!
    16 * 
    17 *
    18 */
    19 
    20 
    21 
    22 #include <stdio.h>
    23 #include <errno.h>
    24 #include <stdlib.h>
    25 #include <string.h>
    26 #include <unistd.h>
    27 #include <sys/types.h>
    28 #include <sys/socket.h>
    29 #include <sys/ioctl.h>
    30 #include <libc/netinet/in.h>
    31 #include <libc/arpa/inet.h>
    32 #include <netdb.h>
    33 #include <errno.h>
    34 
    35 #include "CTEST.H"	/* includes C interface to EPOC32 threads, and SpawnPosixServer */
    36 
    37 test_Data;
    38 
    39 #define N_ADDRESSES	3
    40 unsigned long addresses[N_ADDRESSES][3] =
    41 	{
    42 		{ 121, 0x112233, 0x79112233 },	/* class A 121.xxxxxxxx */
    43 		{ 33771, 0x1122, 0x83eb1122 },	/* class B 131.235.xxxx */
    44 		{ 12747009,  90, 0xc281015a }	/* class C 194.129.1.xx */
    45 	};
    46 
    47 /**
    48 @SYMTestCaseID          SYSLIB-STDLIB-CT-1072
    49 @SYMTestCaseDesc	    Tests for ARPA net functions
    50 @SYMTestPriority 	    High
    51 @SYMTestActions  	    Tests for all basic network functions 
    52 @SYMTestExpectedResults Test must not fail
    53 @SYMREQ                 REQ0000
    54 */
    55 void testArpa()
    56 	{
    57 	char* cp;
    58 	char* cp2;
    59 	struct in_addr iaddr;
    60 	unsigned long ul1, ul2;
    61 	int err;
    62 	int i;
    63 
    64 	test_Next("ARPA/INET.H functions");
    65 
    66 	iaddr.s_addr=11;
    67 	cp="16.33.50.67";
    68 	err=inet_aton(cp, &iaddr);
    69 	test(err==1);
    70 	test(iaddr.s_addr==htonl(0x10213243));
    71 	test(iaddr.s_addr==inet_addr(cp));
    72 	cp2=inet_ntoa(iaddr);
    73 	test(strcmp(cp2,cp)==0);
    74 
    75 	iaddr.s_addr=11;
    76 	err=inet_aton("16.rubbish.67", &iaddr);
    77 	test(err==0);
    78 
    79 	for (i=0;i<N_ADDRESSES;i++)
    80 		{
    81 		iaddr=inet_makeaddr(addresses[i][0], addresses[i][1]);
    82 		test(iaddr.s_addr==ntohl(addresses[i][2]));
    83 		ul1=inet_netof(iaddr);
    84 		ul2=inet_lnaof(iaddr);
    85 		test(ul1==addresses[i][0]);
    86 		test(ul2==addresses[i][1]);
    87 		}
    88 	}
    89 
    90 #define N_NAMES	3
    91 const char* names[N_NAMES][3] = 
    92 	{
    93 		{ "phoenix.doc.ic.ac.uk",  "146.169.1.160",    "phoenix.doc.ic.ac.uk" },
    94 		{ "httpsmtp.test.intra",   "192.168.20.11",   "httpsmtp.test.intra" },
    95 		{ "unix.sri.com",          "128.18.30.211",    "unix.sri.com" }
    96 	};
    97 
    98 int close_console=0;
    99 
   100 /**
   101 @SYMTestCaseID          SYSLIB-STDLIB-CT-1073
   102 @SYMTestCaseDesc	    Tests for ARPA net functions
   103 @SYMTestPriority 	    High
   104 @SYMTestActions  	    Tests for get host by name and host by address functions
   105 @SYMTestExpectedResults Test must not fail
   106 @SYMREQ                 REQ0000
   107 */
   108 void testNetDB()
   109 	{
   110 	char hostname[128];
   111 	struct in_addr addr, *addrp;
   112 	int err, i;
   113 	struct hostent *hp;
   114 
   115 	test_Next("Get Host Name");
   116 
   117 	err=gethostname(hostname,sizeof(hostname));
   118 	test(err==0);
   119 	printf("  hostname = >%s<\n", hostname);
   120 
   121 	test_Next("Get Host By Name");
   122 
   123 	for (i=0; i<N_NAMES; i++)
   124 		{
   125 		hp=gethostbyname(names[i][0]);
   126 		test_ok(hp!=0);
   127 		addrp=(struct in_addr*)(hp->h_addr_list[0]);
   128 		printf("  %-30s => address %-15s\n", hp->h_name, inet_ntoa(*addrp));
   129 		test(strcasecmp(hp->h_name,names[i][0])==0);
   130 		test(addrp->s_addr==inet_addr(names[i][1]));
   131 		}
   132 
   133 	hp=gethostbyname("nosuchname.symbian.com");
   134 	test_errno(hp==0,ENOENT);
   135 	test(errno==HOST_NOT_FOUND);
   136 
   137 	test_Next("Get Address of \"\"");
   138 	hp=gethostbyname("");
   139 	test_ok(hp!=0);
   140 	addrp=(struct in_addr*)(hp->h_addr_list[0]);
   141 	printf("  %-30s => address %-15s\n", hp->h_name, inet_ntoa(*addrp));
   142 
   143 	test_Next("Get Host By Addr");
   144 
   145 	for (i=0; i<N_NAMES; i++)
   146 		{
   147 		addr.s_addr=inet_addr(names[i][1]);
   148 		hp=gethostbyaddr((const char *)&addr,sizeof(addr),AF_INET);
   149 		test_ok(hp!=0);
   150 		addrp=(struct in_addr*)(hp->h_addr_list[0]);
   151 		printf("  address %-15s => %s\n", inet_ntoa(*addrp), hp->h_name);
   152 		test(addrp->s_addr==addr.s_addr);
   153 		test(strcasecmp(hp->h_name,names[i][2])==0);
   154 		}
   155 
   156 	addr.s_addr=inet_addr("10.11.199.255");
   157 	hp=gethostbyaddr((const char *)&addr,sizeof(addr),AF_INET);
   158 	test_errno(hp==0,ENOENT);
   159 	test(errno==HOST_NOT_FOUND);
   160 	
   161 /*
   162 	struct sockaddr_in testaddr;
   163 	int fd;
   164 	test_Next("Connect to the Imperial College Echo server");
   165 
   166 	fd=socket(AF_INET, SOCK_STREAM, 0);
   167 	test_ok(fd>=0);
   168 	testaddr.sin_family=AF_INET;
   169 	testaddr.sin_addr.s_addr=inet_addr("193.63.255.1");
   170 	testaddr.sin_port=htons(7);	// echo
   171 	err=connect(fd,(struct sockaddr*)&testaddr, sizeof(testaddr));
   172 	test(err==0);
   173 	close(fd);
   174 */
   175 
   176 	test_Next("Get Address of roundrobin.test.intra which has multiple address");
   177 	hp=gethostbyname("roundrobin.test.intra");	
   178 	test_ok(hp!=0);
   179 
   180 	if (hp)
   181 		{
   182 		if (hp->h_addr_list)
   183 			{
   184 			int Index = 0;
   185 			while (hp->h_addr_list[Index])
   186 				{
   187 				addrp = (struct in_addr*)(hp->h_addr_list[Index]);
   188 				printf("  %-30s => address %-15s\n", hp->h_name, inet_ntoa(*addrp));
   189 				Index++;
   190 				}
   191 			}
   192 		}
   193 
   194 	test_Next("Get Host name of 192.168.255.4 which has multiple host name");
   195 	addr.s_addr=inet_addr("192.168.255.4");
   196 	hp=gethostbyaddr((const char *)&addr,sizeof(addr),AF_INET);
   197 	test_ok(hp!=0);
   198 
   199 	if (hp)
   200 		{
   201 		addrp=(struct in_addr*)(hp->h_addr_list[0]);
   202 		printf("  address %-15s => %s\n", inet_ntoa(*addrp), hp->h_name);
   203 
   204 		if (hp->h_aliases)
   205 			{
   206 			int Index = 0;
   207 			while (hp->h_aliases[Index])
   208 				{
   209 				printf("  address %-15s => %s\n", inet_ntoa(*addrp), hp->h_aliases[Index]);
   210 				Index++;
   211 				}
   212 			}
   213 		}
   214 
   215 	if (close_console)
   216 		{
   217 		test_Close();
   218 		close(0);
   219 		close(1);
   220 		close(2);
   221 		}
   222 	}
   223 
   224 
   225 int main(int argc, char *argv[])
   226 	{
   227 	void* client;
   228 	int err;
   229 
   230 	test_Title("NETDB.H Functionality");
   231 
   232 	err=CommInit(0);	/* ensure a workable comms environment */
   233 	test(err==0);
   234 
   235 	testArpa();	/* doesn't use the MSystemInterface so only tested once */
   236 
   237 	if (argc==1)
   238 		{
   239 		/* Run the test(s) without a CPosixServer first */		
   240 		testNetDB();
   241 		}
   242 
   243 	test_Next("Do it all using the CPosixServer (for them, not me)");
   244 	close_console=1;
   245 
   246 	start_posix_server();	/* calls SpawnPosixServer from C++ code */
   247 
   248 	client=create_thread(testNetDB, "TNETDB NetDB");
   249 	test(client!=0);
   250 	start_thread(client);
   251 	err=wait_for_thread(client);
   252 	test(err==0);
   253 
   254 	test_Close();
   255 	exit(0);
   256 	return 0;
   257 	}