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