X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=test%2Ftools_static_test.c;h=64da9834468825dfa2779551c8813a82afefe1d4;hb=dfe7b622b128e7bfb4a5e1f7e0afdb84e6001d14;hp=b8ea612ed635cc7fbc67795e874f916267290221;hpb=8790bf0c4f4f08fd05853afa67e211112b344a42;p=ric-plt%2Flib%2Frmr.git diff --git a/test/tools_static_test.c b/test/tools_static_test.c index b8ea612..64da983 100644 --- a/test/tools_static_test.c +++ b/test/tools_static_test.c @@ -31,6 +31,38 @@ Date: 3 April 2019 */ +/* + Returns an interface name that is valid in this environment (keeps us from + having to know/guess a name to test with. +*/ +static char* get_ifname( ) { + if_addrs_t* l; + struct ifaddrs *ifs; // pointer to head + struct ifaddrs *ele; // pointer into the list + char* rstr = NULL; // return string + + + if( (l = (if_addrs_t *) malloc( sizeof( if_addrs_t ) )) == NULL ) { + return NULL; + } + memset( l, 0, sizeof( if_addrs_t ) ); + l->addrs = (char **) malloc( sizeof( char* ) * 128 ); + if( l->addrs == NULL ) { + free( l ); + return NULL; + } + + getifaddrs( &ifs ); + for( ele = ifs; ele; ele = ele->ifa_next ) { + if( ele && strcmp( ele->ifa_name, "lo" ) ) { + rstr = strdup( ele->ifa_name ); + break; + } + } + + free( l ); + return rstr; +} static int tools_test( ) { int i; @@ -40,6 +72,7 @@ static int tools_test( ) { char* buf = "2,Fred,Wilma,Barney,Betty,Dino,Pebbles,Bambam,Mr. Slate,Gazoo"; char* dbuf; // duplicated buf since C marks a const string is unumtable char* hname; + char* ip; // ip address string uta_ctx_t ctx; // context for uta_lookup test void* if_list; @@ -74,12 +107,15 @@ static int tools_test( ) { hname = uta_h2ip( "192.168.1.2" ); errors += fail_not_equal( strcmp( hname, "192.168.1.2" ), 0, "h2ip did not return IP address when given address" ); errors += fail_if_nil( hname, "h2ip did not return a pointer" ); + free( hname ); hname = uta_h2ip( "yahoo.com" ); errors += fail_if_nil( hname, "h2ip did not return a pointer" ); + free( hname ); hname = uta_h2ip( "yahoo.com:1234" ); // should ignore the port errors += fail_if_nil( hname, "h2ip did not return a pointer" ); + free( hname ); // ------------ rtg lookup test ------------------------------------------------------------- ctx.rtg_port = 0; @@ -150,5 +186,32 @@ static int tools_test( ) { i = has_myip( "192.168.4.30:1235", if_list, ',', 128 ); // should find our ip when only in list errors += fail_if_false( i, "has_myip did not find IP when only one in list" ); + ip = get_default_ip( NULL ); + errors += fail_not_nil( ip, "get_default_ip returned non-nil pointer when given nil information" ); + + ip = get_default_ip( if_list ); + if( ip ) { + free( ip ); + } else { + errors += fail_if_nil( ip, "get_defaul_ip returned nil pointer when valid pointer expected" ); + } + + ip = get_ifname(); // suss out a valid interface name (not lo) + if( ip ) { + setenv( "RMR_BIND_IF", ip, 1 ); // drive the case where we have a hard set interface; and set known interface in list + free( ip ); + if_list = mk_ip_list( "1235" ); + if( if_list ) { + ip = get_default_ip( if_list ); + errors += fail_if_nil( ip, "get_default_ip did not return valid pointer when list created from interface name" ); + } else { + errors += fail_if_nil( if_list, "mk_ip_list with a specific interface name returned a nil list" ); + } + + free( ip ); + } + +// ------------------------------------------------------------------------------------------------- + return !!errors; // 1 or 0 regardless of count }