E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 1 | // : vi ts=4 sw=4 noet : |
| 2 | /* |
| 3 | ================================================================================== |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 4 | Copyright (c) 2019 Nokia |
| 5 | Copyright (c) 2018-2019 AT&T Intellectual Property. |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 6 | |
| 7 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | you may not use this file except in compliance with the License. |
| 9 | You may obtain a copy of the License at |
| 10 | |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 11 | http://www.apache.org/licenses/LICENSE-2.0 |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 12 | |
| 13 | Unless required by applicable law or agreed to in writing, software |
| 14 | distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | See the License for the specific language governing permissions and |
| 17 | limitations under the License. |
| 18 | ================================================================================== |
| 19 | */ |
| 20 | |
| 21 | |
| 22 | /* |
| 23 | Mnemonic: tools_static_test.c |
| 24 | Abstract: Unit tests for the RMr tools module. This file is a static include |
| 25 | that is pulle in at compile time by the test driver. The driver is |
| 26 | expected to include necessary rmr*.h and test_support files before |
| 27 | including this file. In addition, a context struct, or dummy, must |
| 28 | be provided based on the type of testing being done. |
| 29 | |
| 30 | Author: E. Scott Daniels |
| 31 | Date: 3 April 2019 |
| 32 | */ |
| 33 | |
E. Scott Daniels | 68d09fa | 2019-06-03 19:45:12 +0000 | [diff] [blame] | 34 | /* |
| 35 | Returns an interface name that is valid in this environment (keeps us from |
| 36 | having to know/guess a name to test with. |
| 37 | */ |
| 38 | static char* get_ifname( ) { |
| 39 | if_addrs_t* l; |
| 40 | struct ifaddrs *ifs; // pointer to head |
| 41 | struct ifaddrs *ele; // pointer into the list |
| 42 | char* rstr = NULL; // return string |
| 43 | |
| 44 | |
| 45 | if( (l = (if_addrs_t *) malloc( sizeof( if_addrs_t ) )) == NULL ) { |
| 46 | return NULL; |
| 47 | } |
| 48 | memset( l, 0, sizeof( if_addrs_t ) ); |
| 49 | l->addrs = (char **) malloc( sizeof( char* ) * 128 ); |
| 50 | if( l->addrs == NULL ) { |
| 51 | free( l ); |
| 52 | return NULL; |
| 53 | } |
| 54 | |
| 55 | getifaddrs( &ifs ); |
| 56 | for( ele = ifs; ele; ele = ele->ifa_next ) { |
| 57 | if( ele && strcmp( ele->ifa_name, "lo" ) ) { |
| 58 | rstr = strdup( ele->ifa_name ); |
| 59 | break; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | free( l ); |
| 64 | return rstr; |
| 65 | } |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 66 | |
| 67 | static int tools_test( ) { |
| 68 | int i; |
| 69 | int j; |
| 70 | int errors = 0; |
| 71 | char* tokens[127]; |
| 72 | char* buf = "2,Fred,Wilma,Barney,Betty,Dino,Pebbles,Bambam,Mr. Slate,Gazoo"; |
| 73 | char* dbuf; // duplicated buf since C marks a const string is unumtable |
| 74 | char* hname; |
E. Scott Daniels | 68d09fa | 2019-06-03 19:45:12 +0000 | [diff] [blame] | 75 | char* ip; // ip address string |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 76 | uta_ctx_t ctx; // context for uta_lookup test |
| 77 | void* if_list; |
| 78 | |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 79 | |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 80 | // ------------------ tokenise tests ----------------------------------------------------------- |
| 81 | dbuf = strdup( buf ); |
| 82 | i = uta_tokenise( dbuf, tokens, 127, ',' ); |
| 83 | errors += fail_not_equal( i, 10, "unexpected number of tokens returned (comma sep)" ); |
| 84 | for( j = 0; j < i; j++ ) { |
| 85 | //fprintf( stderr, ">>>> [%d] (%s)\n", j, tokens[j] ); |
| 86 | errors += fail_if_nil( tokens[j], "token from buffer" ); |
| 87 | } |
| 88 | errors += fail_not_equal( strcmp( tokens[4], "Betty" ), 0, "4th token wasn't 'Betty'" ); |
| 89 | |
| 90 | free( dbuf ); |
| 91 | dbuf = strdup( buf ); |
| 92 | i = uta_tokenise( dbuf, tokens, 127, '|' ); |
| 93 | errors += fail_not_equal( i, 1, "unexpected number of tokens returned (bar sep)" ); |
| 94 | free( dbuf ); |
| 95 | |
| 96 | // ------------ has str tests ----------------------------------------------------------------- |
| 97 | j = uta_has_str( buf, "Mr. Slate", ',', 1 ); // should fail (-1) because user should use strcmp in this situation |
| 98 | errors += fail_if_true( j >= 0, "test to ensure has str rejects small max" ); |
| 99 | |
| 100 | j = uta_has_str( buf, "Mr. Slate", ',', 27 ); |
| 101 | errors += fail_if_true( j < 0, "has string did not find Mr. Slate" ); |
| 102 | |
| 103 | j = uta_has_str( buf, "Mrs. Slate", ',', 27 ); |
| 104 | errors += fail_if_true( j >= 0, "has string not found Mrs. Slate" ); |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 105 | |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 106 | // ------------ host name 2 ip tests --------------------------------------------------------- |
| 107 | hname = uta_h2ip( "192.168.1.2" ); |
| 108 | errors += fail_not_equal( strcmp( hname, "192.168.1.2" ), 0, "h2ip did not return IP address when given address" ); |
| 109 | errors += fail_if_nil( hname, "h2ip did not return a pointer" ); |
E. Scott Daniels | 6511ac7 | 2019-08-27 10:17:21 -0400 | [diff] [blame] | 110 | free( hname ); |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 111 | |
| 112 | hname = uta_h2ip( "yahoo.com" ); |
| 113 | errors += fail_if_nil( hname, "h2ip did not return a pointer" ); |
E. Scott Daniels | 6511ac7 | 2019-08-27 10:17:21 -0400 | [diff] [blame] | 114 | free( hname ); |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 115 | |
| 116 | hname = uta_h2ip( "yahoo.com:1234" ); // should ignore the port |
| 117 | errors += fail_if_nil( hname, "h2ip did not return a pointer" ); |
E. Scott Daniels | 6511ac7 | 2019-08-27 10:17:21 -0400 | [diff] [blame] | 118 | free( hname ); |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 119 | |
| 120 | // ------------ rtg lookup test ------------------------------------------------------------- |
| 121 | ctx.rtg_port = 0; |
| 122 | ctx.rtg_addr = NULL; |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 123 | |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 124 | i = uta_lookup_rtg( NULL ); // ensure it handles a nil context |
| 125 | errors += fail_if_true( i, "rtg lookup returned that it found something when not expected to (nil context)" ); |
| 126 | |
| 127 | setenv( "RMR_RTG_SVC", "localhost:1234", 1); |
| 128 | i = uta_lookup_rtg( &ctx ); |
| 129 | errors += fail_if_false( i, "rtg lookup returned that it did not find something when expected to" ); |
| 130 | errors += fail_if_nil( ctx.rtg_addr, "rtg lookup did not return a pointer (with port)" ); |
| 131 | errors += fail_not_equal( ctx.rtg_port, 1234, "rtg lookup did not capture the port" ); |
| 132 | |
| 133 | setenv( "RMR_RTG_SVC", "localhost", 1); // test ability to generate default port |
| 134 | uta_lookup_rtg( &ctx ); |
| 135 | errors += fail_if_nil( ctx.rtg_addr, "rtg lookup did not return a pointer (no port)" ); |
| 136 | errors += fail_not_equal( ctx.rtg_port, 5656, "rtg lookup did not return default port" ); |
| 137 | |
| 138 | unsetenv( "RMR_RTG_SVC" ); // this should fail as the default name (rtg) will be unknown during testing |
| 139 | i = uta_lookup_rtg( &ctx ); |
| 140 | errors += fail_if_true( i, "rtg lookup returned that it found something when not expected to" ); |
| 141 | |
| 142 | /* |
| 143 | //==== moved out of generic tools ========== |
| 144 | // -------------- test link2 stuff ---------------------------------------------------------- |
| 145 | i = uta_link2( "bad" ); // should fail |
| 146 | errors += fail_if_true( i >= 0, "uta_link2 didn't fail when given bad address" ); |
| 147 | |
| 148 | i = uta_link2( "nohost:-1234" ); |
| 149 | errors += fail_if_true( i >= 0, "uta_link2 did not failed when given a bad (negative) port " ); |
| 150 | |
| 151 | i = uta_link2( "nohost:1234" ); // nn should go off and set things up, but it will never successd, but uta_ call should |
| 152 | errors += fail_if_true( i < 0, "uta_link2 failed when not expected to" ); |
| 153 | */ |
| 154 | |
| 155 | // ------------ my ip stuff ----------------------------------------------------------------- |
| 156 | |
| 157 | if_list = mk_ip_list( "1235" ); |
| 158 | errors += fail_if_nil( if_list, "mk_ip_list returned nil pointer" ); |
| 159 | |
| 160 | i = has_myip( NULL, NULL, ',', 128 ); // should be false if pointers are nil |
| 161 | errors += fail_if_true( i, "has_myip returned true when given nil buffer" ); |
| 162 | |
| 163 | i = has_myip( "buffer contents not valid", NULL, ',', 128 ); // should be false if pointers are nil |
| 164 | errors += fail_if_true( i, "has_myip returned true when given nil list" ); |
| 165 | |
| 166 | i = has_myip( "buffer contents not valid", NULL, ',', 1 ); // should be false if max < 2 |
| 167 | errors += fail_if_true( i, "has_myip returned true when given small max value" ); |
| 168 | |
| 169 | i = has_myip( "buffer.contents.not.valid", if_list, ',', 128 ); // should be false as there is nothing valid in the list |
| 170 | errors += fail_if_true( i, "has_myip returned true when given a buffer with no valid info" ); |
| 171 | |
| 172 | |
| 173 | setenv( "RMR_BIND_IF", "192.168.4.30", 1 ); // drive the case where we have a hard set interface; and set known interface in list |
| 174 | if_list = mk_ip_list( "1235" ); |
| 175 | errors += fail_if_nil( if_list, "mk_ip_list with env set returned nil pointer" ); |
| 176 | |
| 177 | i = has_myip( "192.168.1.2:1235,192.168.4.30:1235,192.168.2.19:4567", if_list, ',', 128 ); // should find our ip in middle |
| 178 | errors += fail_if_false( i, "has_myip did not find IP in middle of list" ); |
| 179 | |
| 180 | i = has_myip( "192.168.4.30:1235,192.168.2.19:4567,192.168.2.19:2222", if_list, ',', 128 ); // should find our ip at head |
| 181 | errors += fail_if_false( i, "has_myip did not find IP at head of list" ); |
| 182 | |
| 183 | i = has_myip( "192.168.23.45:4444,192.168.1.2:1235,192.168.4.30:1235", if_list, ',', 128 ); // should find our ip at end |
| 184 | errors += fail_if_false( i, "has_myip did not find IP at tail of list" ); |
| 185 | |
| 186 | i = has_myip( "192.168.4.30:1235", if_list, ',', 128 ); // should find our ip when only in list |
| 187 | errors += fail_if_false( i, "has_myip did not find IP when only one in list" ); |
| 188 | |
E. Scott Daniels | 68d09fa | 2019-06-03 19:45:12 +0000 | [diff] [blame] | 189 | ip = get_default_ip( NULL ); |
| 190 | errors += fail_not_nil( ip, "get_default_ip returned non-nil pointer when given nil information" ); |
| 191 | |
| 192 | ip = get_default_ip( if_list ); |
| 193 | if( ip ) { |
E. Scott Daniels | 6511ac7 | 2019-08-27 10:17:21 -0400 | [diff] [blame] | 194 | free( ip ); |
E. Scott Daniels | 68d09fa | 2019-06-03 19:45:12 +0000 | [diff] [blame] | 195 | } else { |
| 196 | errors += fail_if_nil( ip, "get_defaul_ip returned nil pointer when valid pointer expected" ); |
| 197 | } |
| 198 | |
| 199 | ip = get_ifname(); // suss out a valid interface name (not lo) |
| 200 | if( ip ) { |
| 201 | setenv( "RMR_BIND_IF", ip, 1 ); // drive the case where we have a hard set interface; and set known interface in list |
| 202 | free( ip ); |
| 203 | if_list = mk_ip_list( "1235" ); |
| 204 | if( if_list ) { |
| 205 | ip = get_default_ip( if_list ); |
| 206 | errors += fail_if_nil( ip, "get_default_ip did not return valid pointer when list created from interface name" ); |
| 207 | } else { |
| 208 | errors += fail_if_nil( if_list, "mk_ip_list with a specific interface name returned a nil list" ); |
| 209 | } |
E. Scott Daniels | 6511ac7 | 2019-08-27 10:17:21 -0400 | [diff] [blame] | 210 | |
| 211 | free( ip ); |
E. Scott Daniels | 68d09fa | 2019-06-03 19:45:12 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | // ------------------------------------------------------------------------------------------------- |
| 215 | |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 216 | return !!errors; // 1 or 0 regardless of count |
| 217 | } |