The change to fix a bug2
[ric-plt/lib/rmr.git] / test / tools_static_test.c
index 66ddeb6..64da983 100644 (file)
@@ -1,14 +1,14 @@
 // : vi ts=4 sw=4 noet :
 /*
 ==================================================================================
-        Copyright (c) 2019 Nokia 
-        Copyright (c) 2018-2019 AT&T Intellectual Property.
+           Copyright (c) 2019 Nokia
+           Copyright (c) 2018-2019 AT&T Intellectual Property.
 
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
 
-       http://www.apache.org/licenses/LICENSE-2.0
+          http://www.apache.org/licenses/LICENSE-2.0
 
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
        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,10 +72,11 @@ 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;
 
-       
+
        // ------------------ tokenise tests -----------------------------------------------------------
        dbuf = strdup( buf );
        i = uta_tokenise( dbuf, tokens, 127, ',' );
@@ -69,22 +102,25 @@ static int tools_test( ) {
 
        j = uta_has_str( buf, "Mrs. Slate", ',', 27 );
        errors += fail_if_true( j >= 0, "has string not found Mrs. Slate" );
-       
+
        // ------------ host name 2 ip tests ---------------------------------------------------------
        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;
        ctx.rtg_addr = NULL;
-       
+
        i = uta_lookup_rtg( NULL );                                             // ensure it handles a nil context
        errors += fail_if_true( i, "rtg lookup returned that it found something when not expected to (nil context)" );
 
@@ -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
 }