X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=test%2Ftools_static_test.c;h=6f95fe56ee32138201762bd4c7ded9c5b504aaf7;hb=refs%2Fchanges%2F15%2F5115%2F2;hp=66ddeb669a02c9b2a0f0e1747e85cf460c7a4ffa;hpb=8dd46415b94b33fa960bdd5732e909ffc4859520;p=ric-plt%2Flib%2Frmr.git diff --git a/test/tools_static_test.c b/test/tools_static_test.c index 66ddeb6..6f95fe5 100644 --- a/test/tools_static_test.c +++ b/test/tools_static_test.c @@ -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, @@ -31,6 +31,63 @@ Date: 3 April 2019 */ +// ------------ zero termianted buffer --------------------------------------------------------- +static int ztbf_test() { + int errors = 0; + char buf[128]; + char* sshort = "Stand up and cheer! Cheer long and loud for old Ohio."; + char* slong = "Now is the time for the bobcat in the forest to make its way back to Court St for a round of pints at the Pub."; + int l1; + + l1 = zt_buf_fill( buf, sshort, 64 ); + errors += fail_not_equal( l1, strlen( sshort ), "zt_buf_fill of short buf returned unexpected len" ); + errors += fail_not_equal( l1, strlen( buf ), "zt_buf_fill of short buf returned len did not match strlen" ); + + l1 = zt_buf_fill( buf, slong, 64 ); + errors += fail_if_equal( l1, strlen( slong ), "zt_buf_fill of long buf returned unexpected len" ); + errors += fail_not_equal( l1, strlen( buf ), "zt_buf_fill of long buf returned len did not match strlen" ); + + l1 = zt_buf_fill( buf, sshort, strlen( sshort ) ); // edge case of exact size + errors += fail_not_equal( l1, strlen( sshort )-1, "zt_buf_fill exact length edge case failed" ); + + l1 = zt_buf_fill( buf, sshort, 1 ); // unrealistic edge case + errors += fail_not_equal( l1, 0, "zt_buf_fill dest len == 1 test failed" ); + + return errors; +} + +/* + 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 +97,13 @@ 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; - + + uta_dump_env(); + // ------------------ tokenise tests ----------------------------------------------------------- dbuf = strdup( buf ); i = uta_tokenise( dbuf, tokens, 127, ',' ); @@ -69,22 +129,27 @@ 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 ------------------------------------------------------------- +#ifdef KEEP + // pub/sub route table generator is deprecated and should be removed at this point 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)" ); @@ -102,21 +167,9 @@ static int tools_test( ) { unsetenv( "RMR_RTG_SVC" ); // this should fail as the default name (rtg) will be unknown during testing i = uta_lookup_rtg( &ctx ); errors += fail_if_true( i, "rtg lookup returned that it found something when not expected to" ); +#endif -/* -//==== moved out of generic tools ========== - // -------------- test link2 stuff ---------------------------------------------------------- - i = uta_link2( "bad" ); // should fail - errors += fail_if_true( i >= 0, "uta_link2 didn't fail when given bad address" ); - - i = uta_link2( "nohost:-1234" ); - errors += fail_if_true( i >= 0, "uta_link2 did not failed when given a bad (negative) port " ); - - i = uta_link2( "nohost:1234" ); // nn should go off and set things up, but it will never successd, but uta_ call should - errors += fail_if_true( i < 0, "uta_link2 failed when not expected to" ); -*/ - - // ------------ my ip stuff ----------------------------------------------------------------- + // ------------ my_ip stuff ----------------------------------------------------------------- if_list = mk_ip_list( "1235" ); errors += fail_if_nil( if_list, "mk_ip_list returned nil pointer" ); @@ -150,5 +203,34 @@ 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 ); + } + + errors += ztbf_test(); // test the zero term buffer fill function + +// ------------------------------------------------------------------------------------------------- + return !!errors; // 1 or 0 regardless of count }