Add better support for manual app testing
[ric-plt/lib/rmr.git] / test / tools_static_test.c
1 // : vi ts=4 sw=4 noet :
2 /*
3 ==================================================================================
4             Copyright (c) 2019 Nokia
5             Copyright (c) 2018-2019 AT&T Intellectual Property.
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
11            http://www.apache.org/licenses/LICENSE-2.0
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
34 // ------------ zero termianted buffer ---------------------------------------------------------
35 static int ztbf_test() {
36         int errors = 0;
37         char buf[128];
38         char*   sshort = "Stand up and cheer! Cheer long and loud for old Ohio.";
39         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.";
40         int l1;
41
42         l1 = zt_buf_fill( buf, sshort, 64 );
43         errors += fail_not_equal( l1, strlen( sshort ), "zt_buf_fill of short buf returned unexpected len" );
44         errors += fail_not_equal( l1, strlen( buf ), "zt_buf_fill of short buf returned len did not match strlen" );
45
46         l1 = zt_buf_fill( buf, slong, 64 );
47         errors += fail_if_equal( l1, strlen( slong ), "zt_buf_fill of long buf returned unexpected len" );
48         errors += fail_not_equal( l1, strlen( buf ), "zt_buf_fill of long buf returned len did not match strlen" );
49
50         l1 = zt_buf_fill( buf, sshort, strlen( sshort ) );              // edge case of exact size
51         errors += fail_not_equal( l1, strlen( sshort )-1, "zt_buf_fill exact length edge case failed" );
52
53         l1 = zt_buf_fill( buf, sshort, 1 );                                             // unrealistic edge case
54         errors += fail_not_equal( l1, 0, "zt_buf_fill dest len == 1 test failed" );
55
56         return errors;
57 }
58
59 /*
60         Returns an interface name that is valid in this environment (keeps us from
61         having to know/guess a name to test with.
62 */
63 static char* get_ifname( ) {
64         if_addrs_t* l;
65         struct  ifaddrs *ifs;           // pointer to head
66         struct  ifaddrs *ele;           // pointer into the list
67         char*   rstr = NULL;            // return string
68         char    octs[NI_MAXHOST+1];
69
70
71         if( (l = (if_addrs_t *) malloc( sizeof( if_addrs_t ) )) == NULL ) {
72                 return NULL;
73         }
74         memset( l, 0, sizeof( if_addrs_t ) );
75         l->addrs = (char **) malloc( sizeof( char* ) * 128 );
76         if( l->addrs == NULL ) {
77                 free( l );
78                 return NULL;
79         }
80
81         getifaddrs( &ifs );
82         for( ele = ifs; ele; ele = ele->ifa_next ) {
83                 if( ele && strcmp( ele->ifa_name, "lo" ) ) {
84                         memset( octs, 0, sizeof( octs ) );
85                         getnameinfo( ele->ifa_addr, sizeof( struct sockaddr_in6 ),  octs, NI_MAXHOST, NULL, 0, NI_NUMERICHOST );
86                         if( *octs ) {
87                                 rstr = strdup( ele->ifa_name );
88                                 fprintf( stderr, "<INFO> found interface with address: %s\n", rstr );
89                                 break;
90                         }
91                 }
92         }
93
94         free( l );
95         if( rstr == NULL ) {
96                 fprintf( stderr, "<ERROR> no interface with an address was found!\n" );
97         }
98         return rstr;
99 }
100
101 static int tools_test( ) {
102         int i;
103         int j;
104         int errors = 0;
105         char* tokens[127];
106         char* buf = "2,Fred,Wilma,Barney,Betty,Dino,Pebbles,Bambam,Mr. Slate,Gazoo";
107         char*   dbuf;                           // duplicated buf since C marks a const string is unumtable
108         char*   hname;
109         char*   ip;                                     // ip address string
110         uta_ctx_t ctx;                          // context for uta_lookup test
111         void*   if_list;
112
113
114         uta_dump_env();
115
116         // ------------------ tokenise tests -----------------------------------------------------------
117         dbuf = strdup( buf );
118         i = uta_tokenise( dbuf, tokens, 127, ',' );
119         errors += fail_not_equal( i, 10, "unexpected number of tokens returned (comma sep)" );
120         for( j = 0; j < i; j++ ) {
121                 //fprintf( stderr, ">>>> [%d] (%s)\n", j, tokens[j] );
122                 errors += fail_if_nil( tokens[j], "token from buffer" );
123         }
124         errors += fail_not_equal( strcmp( tokens[4], "Betty" ), 0, "4th token wasn't 'Betty'" );
125
126         free( dbuf );
127         dbuf = strdup( buf );
128         i = uta_tokenise( dbuf, tokens, 127, '|' );
129         errors += fail_not_equal( i, 1, "unexpected number of tokens returned (bar sep)" );
130         free( dbuf );
131
132         // ------------ has str tests -----------------------------------------------------------------
133         j = uta_has_str( buf, "Mr. Slate", ',', 1 );                    // should fail (-1) because user should use strcmp in this situation
134         errors += fail_if_true( j >= 0, "test to ensure has str rejects small max" );
135
136         j = uta_has_str( buf, "Mr. Slate", ',', 27 );
137         errors += fail_if_true( j < 0, "has string did not find Mr. Slate" );
138
139         j = uta_has_str( buf, "Mrs. Slate", ',', 27 );
140         errors += fail_if_true( j >= 0, "has string not found Mrs. Slate" );
141
142         // ------------ host name 2 ip tests ---------------------------------------------------------
143         hname = uta_h2ip( "192.168.1.2" );
144         errors += fail_not_equal( strcmp( hname, "192.168.1.2" ), 0, "h2ip did not return IP address when given address" );
145         errors += fail_if_nil( hname, "h2ip did not return a pointer" );
146         free( hname );
147
148         hname = uta_h2ip( "yahoo.com" );
149         errors += fail_if_nil( hname, "h2ip did not return a pointer" );
150         free( hname );
151
152         hname = uta_h2ip( "yahoo.com:1234" );                                                   // should ignore the port
153         errors += fail_if_nil( hname, "h2ip did not return a pointer" );
154         free( hname );
155
156         // ------------ rtg lookup test -------------------------------------------------------------
157 #ifdef KEEP
158         // pub/sub route table generator is deprecated and should be removed at this point
159         ctx.rtg_port = 0;
160         ctx.rtg_addr = NULL;
161
162         i = uta_lookup_rtg( NULL );                                             // ensure it handles a nil context
163         errors += fail_if_true( i, "rtg lookup returned that it found something when not expected to (nil context)" );
164
165         setenv( "RMR_RTG_SVC", "localhost:1234", 1);
166         i = uta_lookup_rtg( &ctx );
167         errors += fail_if_false( i, "rtg lookup returned that it did not find something when expected to" );
168         errors += fail_if_nil( ctx.rtg_addr, "rtg lookup did not return a pointer (with port)" );
169         errors += fail_not_equal( ctx.rtg_port, 1234, "rtg lookup did not capture the port" );
170
171         setenv( "RMR_RTG_SVC", "localhost", 1);                 // test ability to generate default port
172         uta_lookup_rtg( &ctx );
173         errors += fail_if_nil( ctx.rtg_addr, "rtg lookup did not return a pointer (no port)" );
174         errors += fail_not_equal( ctx.rtg_port, 5656, "rtg lookup did not return default port" );
175
176         unsetenv( "RMR_RTG_SVC" );                                              // this should fail as the default name (rtg) will be unknown during testing
177         i = uta_lookup_rtg( &ctx );
178         errors += fail_if_true( i, "rtg lookup returned that it found something when not expected to" );
179 #endif
180
181         // ------------ my_ip stuff -----------------------------------------------------------------
182
183         if_list = mk_ip_list( "1235" );
184         errors += fail_if_nil( if_list, "mk_ip_list returned nil pointer" );
185
186         i = has_myip( NULL, NULL, ',', 128 );           // should be false if pointers are nil
187         errors += fail_if_true( i, "has_myip returned true when given nil buffer" );
188
189         i = has_myip( "buffer contents not valid", NULL, ',', 128 );            // should be false if pointers are nil
190         errors += fail_if_true( i, "has_myip returned true when given nil list" );
191
192         i = has_myip( "buffer contents not valid", NULL, ',', 1 );                      // should be false if max < 2
193         errors += fail_if_true( i, "has_myip returned true when given small max value" );
194
195         i = has_myip( "buffer.contents.not.valid", if_list, ',', 128 );         // should be false as there is nothing valid in the list
196         errors += fail_if_true( i, "has_myip returned true when given a buffer with no valid info" );
197
198
199         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
200         if_list = mk_ip_list( "1235" );
201         errors += fail_if_nil( if_list, "mk_ip_list with env set returned nil pointer" );
202
203         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
204         errors += fail_if_false( i, "has_myip did not find IP in middle of list" );
205
206         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
207         errors += fail_if_false( i, "has_myip did not find IP at head of list" );
208
209         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
210         errors += fail_if_false( i, "has_myip did not find IP at tail of list" );
211
212         i = has_myip( "192.168.4.30:1235", if_list, ',', 128 );                                                                                 // should find our ip when only in list
213         errors += fail_if_false( i, "has_myip did not find IP when only one in list" );
214
215         ip = get_default_ip( NULL );
216         errors += fail_not_nil( ip, "get_default_ip returned non-nil pointer when given nil information" );
217
218         ip = get_default_ip( if_list );
219         if( ip ) {
220                 free( ip );
221         } else {
222                 errors += fail_if_nil( ip, "get_defaul_ip returned nil pointer when valid pointer expected" );
223         }
224
225         ip = get_ifname();                                                      // suss out a valid interface name (not lo)
226         if( ip ) {
227                 setenv( "RMR_BIND_IF", ip, 1 );                 // drive the case where we have a hard set interface; and set known interface in list
228                 free( ip );
229                 if_list = mk_ip_list( "1235" );
230                 if( if_list ) {
231                         ip = get_default_ip( if_list );
232                         errors += fail_if_nil( ip, "get_default_ip did not return valid pointer when list created from interface name" );
233                 } else {
234                         errors += fail_if_nil( if_list, "mk_ip_list with a specific interface name returned a nil list" );
235                 }
236
237                 free( ip );
238         } else {
239                 fprintf( stderr, "<SKIP> test skipped because no interface with address could be found on system" );
240         }
241
242         errors += ztbf_test();                                  // test the zero term buffer fill function
243
244 // -------------------------------------------------------------------------------------------------
245
246         return !!errors;                        // 1 or 0 regardless of count
247 }