sl@0: /* sl@0: * Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * Test of NETDB.H functions - you need a real Internet connection for this! sl@0: * sl@0: * sl@0: */ sl@0: sl@0: sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #include "CTEST.H" /* includes C interface to EPOC32 threads, and SpawnPosixServer */ sl@0: sl@0: test_Data; sl@0: sl@0: #define N_ADDRESSES 3 sl@0: unsigned long addresses[N_ADDRESSES][3] = sl@0: { sl@0: { 121, 0x112233, 0x79112233 }, /* class A 121.xxxxxxxx */ sl@0: { 33771, 0x1122, 0x83eb1122 }, /* class B 131.235.xxxx */ sl@0: { 12747009, 90, 0xc281015a } /* class C 194.129.1.xx */ sl@0: }; sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-STDLIB-CT-1072 sl@0: @SYMTestCaseDesc Tests for ARPA net functions sl@0: @SYMTestPriority High sl@0: @SYMTestActions Tests for all basic network functions sl@0: @SYMTestExpectedResults Test must not fail sl@0: @SYMREQ REQ0000 sl@0: */ sl@0: void testArpa() sl@0: { sl@0: char* cp; sl@0: char* cp2; sl@0: struct in_addr iaddr; sl@0: unsigned long ul1, ul2; sl@0: int err; sl@0: int i; sl@0: sl@0: test_Next("ARPA/INET.H functions"); sl@0: sl@0: iaddr.s_addr=11; sl@0: cp="16.33.50.67"; sl@0: err=inet_aton(cp, &iaddr); sl@0: test(err==1); sl@0: test(iaddr.s_addr==htonl(0x10213243)); sl@0: test(iaddr.s_addr==inet_addr(cp)); sl@0: cp2=inet_ntoa(iaddr); sl@0: test(strcmp(cp2,cp)==0); sl@0: sl@0: iaddr.s_addr=11; sl@0: err=inet_aton("16.rubbish.67", &iaddr); sl@0: test(err==0); sl@0: sl@0: for (i=0;i%s<\n", hostname); sl@0: sl@0: test_Next("Get Host By Name"); sl@0: sl@0: for (i=0; ih_addr_list[0]); sl@0: printf(" %-30s => address %-15s\n", hp->h_name, inet_ntoa(*addrp)); sl@0: test(strcasecmp(hp->h_name,names[i][0])==0); sl@0: test(addrp->s_addr==inet_addr(names[i][1])); sl@0: } sl@0: sl@0: hp=gethostbyname("nosuchname.symbian.com"); sl@0: test_errno(hp==0,ENOENT); sl@0: test(errno==HOST_NOT_FOUND); sl@0: sl@0: test_Next("Get Address of \"\""); sl@0: hp=gethostbyname(""); sl@0: test_ok(hp!=0); sl@0: addrp=(struct in_addr*)(hp->h_addr_list[0]); sl@0: printf(" %-30s => address %-15s\n", hp->h_name, inet_ntoa(*addrp)); sl@0: sl@0: test_Next("Get Host By Addr"); sl@0: sl@0: for (i=0; ih_addr_list[0]); sl@0: printf(" address %-15s => %s\n", inet_ntoa(*addrp), hp->h_name); sl@0: test(addrp->s_addr==addr.s_addr); sl@0: test(strcasecmp(hp->h_name,names[i][2])==0); sl@0: } sl@0: sl@0: addr.s_addr=inet_addr("10.11.199.255"); sl@0: hp=gethostbyaddr((const char *)&addr,sizeof(addr),AF_INET); sl@0: test_errno(hp==0,ENOENT); sl@0: test(errno==HOST_NOT_FOUND); sl@0: sl@0: /* sl@0: struct sockaddr_in testaddr; sl@0: int fd; sl@0: test_Next("Connect to the Imperial College Echo server"); sl@0: sl@0: fd=socket(AF_INET, SOCK_STREAM, 0); sl@0: test_ok(fd>=0); sl@0: testaddr.sin_family=AF_INET; sl@0: testaddr.sin_addr.s_addr=inet_addr("193.63.255.1"); sl@0: testaddr.sin_port=htons(7); // echo sl@0: err=connect(fd,(struct sockaddr*)&testaddr, sizeof(testaddr)); sl@0: test(err==0); sl@0: close(fd); sl@0: */ sl@0: sl@0: test_Next("Get Address of roundrobin.test.intra which has multiple address"); sl@0: hp=gethostbyname("roundrobin.test.intra"); sl@0: test_ok(hp!=0); sl@0: sl@0: if (hp) sl@0: { sl@0: if (hp->h_addr_list) sl@0: { sl@0: int Index = 0; sl@0: while (hp->h_addr_list[Index]) sl@0: { sl@0: addrp = (struct in_addr*)(hp->h_addr_list[Index]); sl@0: printf(" %-30s => address %-15s\n", hp->h_name, inet_ntoa(*addrp)); sl@0: Index++; sl@0: } sl@0: } sl@0: } sl@0: sl@0: test_Next("Get Host name of 192.168.255.4 which has multiple host name"); sl@0: addr.s_addr=inet_addr("192.168.255.4"); sl@0: hp=gethostbyaddr((const char *)&addr,sizeof(addr),AF_INET); sl@0: test_ok(hp!=0); sl@0: sl@0: if (hp) sl@0: { sl@0: addrp=(struct in_addr*)(hp->h_addr_list[0]); sl@0: printf(" address %-15s => %s\n", inet_ntoa(*addrp), hp->h_name); sl@0: sl@0: if (hp->h_aliases) sl@0: { sl@0: int Index = 0; sl@0: while (hp->h_aliases[Index]) sl@0: { sl@0: printf(" address %-15s => %s\n", inet_ntoa(*addrp), hp->h_aliases[Index]); sl@0: Index++; sl@0: } sl@0: } sl@0: } sl@0: sl@0: if (close_console) sl@0: { sl@0: test_Close(); sl@0: close(0); sl@0: close(1); sl@0: close(2); sl@0: } sl@0: } sl@0: sl@0: sl@0: int main(int argc, char *argv[]) sl@0: { sl@0: void* client; sl@0: int err; sl@0: sl@0: test_Title("NETDB.H Functionality"); sl@0: sl@0: err=CommInit(0); /* ensure a workable comms environment */ sl@0: test(err==0); sl@0: sl@0: testArpa(); /* doesn't use the MSystemInterface so only tested once */ sl@0: sl@0: if (argc==1) sl@0: { sl@0: /* Run the test(s) without a CPosixServer first */ sl@0: testNetDB(); sl@0: } sl@0: sl@0: test_Next("Do it all using the CPosixServer (for them, not me)"); sl@0: close_console=1; sl@0: sl@0: start_posix_server(); /* calls SpawnPosixServer from C++ code */ sl@0: sl@0: client=create_thread(testNetDB, "TNETDB NetDB"); sl@0: test(client!=0); sl@0: start_thread(client); sl@0: err=wait_for_thread(client); sl@0: test(err==0); sl@0: sl@0: test_Close(); sl@0: exit(0); sl@0: return 0; sl@0: }