test(unit): Extend unit tests
[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
35 static int tools_test( ) {
36         int i;
37         int j;
38         int errors = 0;
39         char* tokens[127];
40         char* buf = "2,Fred,Wilma,Barney,Betty,Dino,Pebbles,Bambam,Mr. Slate,Gazoo";
41         char*   dbuf;                           // duplicated buf since C marks a const string is unumtable
42         char*   hname;
43         uta_ctx_t ctx;                          // context for uta_lookup test
44         void*   if_list;
45
46         
47         // ------------------ tokenise tests -----------------------------------------------------------
48         dbuf = strdup( buf );
49         i = uta_tokenise( dbuf, tokens, 127, ',' );
50         errors += fail_not_equal( i, 10, "unexpected number of tokens returned (comma sep)" );
51         for( j = 0; j < i; j++ ) {
52                 //fprintf( stderr, ">>>> [%d] (%s)\n", j, tokens[j] );
53                 errors += fail_if_nil( tokens[j], "token from buffer" );
54         }
55         errors += fail_not_equal( strcmp( tokens[4], "Betty" ), 0, "4th token wasn't 'Betty'" );
56
57         free( dbuf );
58         dbuf = strdup( buf );
59         i = uta_tokenise( dbuf, tokens, 127, '|' );
60         errors += fail_not_equal( i, 1, "unexpected number of tokens returned (bar sep)" );
61         free( dbuf );
62
63         // ------------ has str tests -----------------------------------------------------------------
64         j = uta_has_str( buf, "Mr. Slate", ',', 1 );                    // should fail (-1) because user should use strcmp in this situation
65         errors += fail_if_true( j >= 0, "test to ensure has str rejects small max" );
66
67         j = uta_has_str( buf, "Mr. Slate", ',', 27 );
68         errors += fail_if_true( j < 0, "has string did not find Mr. Slate" );
69
70         j = uta_has_str( buf, "Mrs. Slate", ',', 27 );
71         errors += fail_if_true( j >= 0, "has string not found Mrs. Slate" );
72         
73         // ------------ host name 2 ip tests ---------------------------------------------------------
74         hname = uta_h2ip( "192.168.1.2" );
75         errors += fail_not_equal( strcmp( hname, "192.168.1.2" ), 0, "h2ip did not return IP address when given address" );
76         errors += fail_if_nil( hname, "h2ip did not return a pointer" );
77
78         hname = uta_h2ip( "yahoo.com" );
79         errors += fail_if_nil( hname, "h2ip did not return a pointer" );
80
81         hname = uta_h2ip( "yahoo.com:1234" );                                                   // should ignore the port
82         errors += fail_if_nil( hname, "h2ip did not return a pointer" );
83
84         // ------------ rtg lookup test -------------------------------------------------------------
85         ctx.rtg_port = 0;
86         ctx.rtg_addr = NULL;
87        
88         i = uta_lookup_rtg( NULL );                                             // ensure it handles a nil context
89         errors += fail_if_true( i, "rtg lookup returned that it found something when not expected to (nil context)" );
90
91         setenv( "RMR_RTG_SVC", "localhost:1234", 1);
92         i = uta_lookup_rtg( &ctx );
93         errors += fail_if_false( i, "rtg lookup returned that it did not find something when expected to" );
94         errors += fail_if_nil( ctx.rtg_addr, "rtg lookup did not return a pointer (with port)" );
95         errors += fail_not_equal( ctx.rtg_port, 1234, "rtg lookup did not capture the port" );
96
97         setenv( "RMR_RTG_SVC", "localhost", 1);                 // test ability to generate default port
98         uta_lookup_rtg( &ctx );
99         errors += fail_if_nil( ctx.rtg_addr, "rtg lookup did not return a pointer (no port)" );
100         errors += fail_not_equal( ctx.rtg_port, 5656, "rtg lookup did not return default port" );
101
102         unsetenv( "RMR_RTG_SVC" );                                              // this should fail as the default name (rtg) will be unknown during testing
103         i = uta_lookup_rtg( &ctx );
104         errors += fail_if_true( i, "rtg lookup returned that it found something when not expected to" );
105
106 /*
107 //==== moved out of generic tools ==========
108         // -------------- test link2 stuff ----------------------------------------------------------
109         i = uta_link2( "bad" );                                 // should fail
110         errors += fail_if_true( i >= 0, "uta_link2 didn't fail when given bad address" );
111
112         i = uta_link2( "nohost:-1234" );
113         errors += fail_if_true( i >= 0, "uta_link2 did not failed when given a bad (negative) port " );
114
115         i = uta_link2( "nohost:1234" );                                 // nn should go off and set things up, but it will never successd, but uta_ call should
116         errors += fail_if_true( i < 0, "uta_link2 failed when not expected to" );
117 */
118
119         // ------------ my ip stuff -----------------------------------------------------------------
120
121         if_list = mk_ip_list( "1235" );
122         errors += fail_if_nil( if_list, "mk_ip_list returned nil pointer" );
123
124         i = has_myip( NULL, NULL, ',', 128 );           // should be false if pointers are nil
125         errors += fail_if_true( i, "has_myip returned true when given nil buffer" );
126
127         i = has_myip( "buffer contents not valid", NULL, ',', 128 );            // should be false if pointers are nil
128         errors += fail_if_true( i, "has_myip returned true when given nil list" );
129
130         i = has_myip( "buffer contents not valid", NULL, ',', 1 );                      // should be false if max < 2
131         errors += fail_if_true( i, "has_myip returned true when given small max value" );
132
133         i = has_myip( "buffer.contents.not.valid", if_list, ',', 128 );         // should be false as there is nothing valid in the list
134         errors += fail_if_true( i, "has_myip returned true when given a buffer with no valid info" );
135
136
137         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
138         if_list = mk_ip_list( "1235" );
139         errors += fail_if_nil( if_list, "mk_ip_list with env set returned nil pointer" );
140
141         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
142         errors += fail_if_false( i, "has_myip did not find IP in middle of list" );
143
144         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
145         errors += fail_if_false( i, "has_myip did not find IP at head of list" );
146
147         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
148         errors += fail_if_false( i, "has_myip did not find IP at tail of list" );
149
150         i = has_myip( "192.168.4.30:1235", if_list, ',', 128 );                                                                                 // should find our ip when only in list
151         errors += fail_if_false( i, "has_myip did not find IP when only one in list" );
152
153         return !!errors;                        // 1 or 0 regardless of count
154 }