Initial commit of RMR Library
[ric-plt/lib/rmr.git] / test / tools_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_testh.c
24         Abstract:       Unit tests for the RMr tools module.
25         Author:         E. Scott Daniels
26         Date:           21 January 2019
27 */
28
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <netdb.h>
33 #include <errno.h>
34 #include <string.h>
35 #include <errno.h>
36 #include <pthread.h>
37 #include <ctype.h>
38
39 /*
40 #include <nanomsg/nn.h>
41 #include <nanomsg/tcp.h>
42 #include <nanomsg/pair.h>
43 #include <nanomsg/pipeline.h>
44 #include <nanomsg/pubsub.h>
45 */
46
47 #include "../src/common/include/rmr.h"
48 #include "../src/common/include/rmr_agnostic.h"
49 #include "test_support.c"               // our private library of test tools
50
51 // ===== dummy context for tools testing so we don't have to pull in all of the nano/nng specific stuff =====
52 struct uta_ctx {
53         char*   my_name;                        // dns name of this host to set in sender field of a message
54         int     shutdown;                               // thread notification if we need to tell them to stop
55         int max_mlen;                           // max message length payload+header
56         int     max_plen;                               // max payload length
57         int     flags;                                  // CTXFL_ constants
58         int nrtele;                                     // number of elements in the routing table
59         int send_retries;                       // number of retries send_msg() should attempt if eagain/timeout indicated by nng
60         //nng_socket    nn_sock;                // our general listen socket
61         route_table_t* rtable;          // the active route table
62         route_table_t* old_rtable;      // the previously used rt, sits here to allow for draining
63         route_table_t* new_rtable;      // route table under construction
64         if_addrs_t*     ip_list;                // list manager of the IP addresses that are on our known interfaces
65         void*   mring;                          // ring where msgs are queued while waiting for a call response msg
66         
67         char*   rtg_addr;                       // addr/port of the route table generation publisher
68         int             rtg_port;                       // the port that the rtg listens on
69         
70         wh_mgt_t*       wormholes;              // management of user opened wormholes
71         //epoll_stuff_t*        eps;            // epoll information needed for the rcv with timeout call
72
73         //pthread_t     rtc_th;                 // thread info for the rtc listener
74 };
75
76
77 #include "../src/common/src/tools_static.c"
78
79
80 int main( ) {
81         int i;
82         int j;
83         int errors = 0;
84         char* tokens[127];
85         char* buf = "2,Fred,Wilma,Barney,Betty,Dino,Pebbles,Bambam,Mr. Slate,Gazoo";
86         char*   dbuf;                           // duplicated buf since C marks a const string is unumtable
87         char*   hname;
88         uta_ctx_t ctx;                          // context for uta_lookup test
89         void*   if_list;
90
91         
92         // ------------------ tokenise tests -----------------------------------------------------------
93         dbuf = strdup( buf );
94         i = uta_tokenise( dbuf, tokens, 127, ',' );
95         errors += fail_not_equal( i, 10, "unexpected number of tokens returned (comma sep)" );
96         for( j = 0; j < i; j++ ) {
97                 //fprintf( stderr, ">>>> [%d] (%s)\n", j, tokens[j] );
98                 errors += fail_if_nil( tokens[j], "token from buffer" );
99         }
100         errors += fail_not_equal( strcmp( tokens[4], "Betty" ), 0, "4th token wasn't 'Betty'" );
101
102         free( dbuf );
103         dbuf = strdup( buf );
104         i = uta_tokenise( dbuf, tokens, 127, '|' );
105         errors += fail_not_equal( i, 1, "unexpected number of tokens returned (bar sep)" );
106         free( dbuf );
107
108         // ------------ has str tests -----------------------------------------------------------------
109         j = uta_has_str( buf, "Mr. Slate", ',', 1 );                    // should fail (-1) because user should use strcmp in this situation
110         errors += fail_if_true( j >= 0, "test to ensure has str rejects small max" );
111
112         j = uta_has_str( buf, "Mr. Slate", ',', 27 );
113         errors += fail_if_true( j < 0, "has string did not find Mr. Slate" );
114
115         j = uta_has_str( buf, "Mrs. Slate", ',', 27 );
116         errors += fail_if_true( j >= 0, "has string not found Mrs. Slate" );
117         
118         // ------------ host name 2 ip tests ---------------------------------------------------------
119         hname = uta_h2ip( "192.168.1.2" );
120         errors += fail_not_equal( strcmp( hname, "192.168.1.2" ), 0, "h2ip did not return IP address when given address" );
121         errors += fail_if_nil( hname, "h2ip did not return a pointer" );
122
123         hname = uta_h2ip( "yahoo.com" );
124         errors += fail_if_nil( hname, "h2ip did not return a pointer" );
125
126         hname = uta_h2ip( "yahoo.com:1234" );                                                   // should ignore the port
127         errors += fail_if_nil( hname, "h2ip did not return a pointer" );
128
129         // ------------ rtg lookup test -------------------------------------------------------------
130         ctx.rtg_port = 0;
131         ctx.rtg_addr = NULL;
132        
133         i = uta_lookup_rtg( NULL );                                             // ensure it handles a nil context
134         errors += fail_if_true( i, "rtg lookup returned that it found something when not expected to (nil context)" );
135
136         setenv( "RMR_RTG_SVC", "localhost:1234", 1);
137         i = uta_lookup_rtg( &ctx );
138         errors += fail_if_false( i, "rtg lookup returned that it did not find something when expected to" );
139         errors += fail_if_nil( ctx.rtg_addr, "rtg lookup did not return a pointer (with port)" );
140         errors += fail_not_equal( ctx.rtg_port, 1234, "rtg lookup did not capture the port" );
141
142         setenv( "RMR_RTG_SVC", "localhost", 1);                 // test ability to generate default port
143         uta_lookup_rtg( &ctx );
144         errors += fail_if_nil( ctx.rtg_addr, "rtg lookup did not return a pointer (no port)" );
145         errors += fail_not_equal( ctx.rtg_port, 5656, "rtg lookup did not return default port" );
146
147         unsetenv( "RMR_RTG_SVC" );                                              // this should fail as the default name (rtg) will be unknown during testing
148         i = uta_lookup_rtg( &ctx );
149         errors += fail_if_true( i, "rtg lookup returned that it found something when not expected to" );
150
151 /*
152 //==== moved out of generic tools ==========
153         // -------------- test link2 stuff ----------------------------------------------------------
154         i = uta_link2( "bad" );                                 // should fail
155         errors += fail_if_true( i >= 0, "uta_link2 didn't fail when given bad address" );
156
157         i = uta_link2( "nohost:-1234" );
158         errors += fail_if_true( i >= 0, "uta_link2 did not failed when given a bad (negative) port " );
159
160         i = uta_link2( "nohost:1234" );                                 // nn should go off and set things up, but it will never successd, but uta_ call should
161         errors += fail_if_true( i < 0, "uta_link2 failed when not expected to" );
162 */
163
164         // ------------ my ip stuff -----------------------------------------------------------------
165
166         if_list = mk_ip_list( "1235" );
167         errors += fail_if_nil( if_list, "mk_ip_list returned nil pointer" );
168
169         i = has_myip( NULL, NULL, ',', 128 );           // should be false if pointers are nil
170         errors += fail_if_true( i, "has_myip returned true when given nil buffer" );
171
172         i = has_myip( "buffer contents not valid", NULL, ',', 128 );            // should be false if pointers are nil
173         errors += fail_if_true( i, "has_myip returned true when given nil list" );
174
175         i = has_myip( "buffer contents not valid", NULL, ',', 1 );                      // should be false if max < 2
176         errors += fail_if_true( i, "has_myip returned true when given small max value" );
177
178         i = has_myip( "buffer.contents.not.valid", if_list, ',', 128 );         // should be false as there is nothing valid in the list
179         errors += fail_if_true( i, "has_myip returned true when given a buffer with no valid info" );
180
181
182         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
183         if_list = mk_ip_list( "1235" );
184         errors += fail_if_nil( if_list, "mk_ip_list with env set returned nil pointer" );
185
186         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
187         errors += fail_if_false( i, "has_myip did not find IP in middle of list" );
188
189         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
190         errors += fail_if_false( i, "has_myip did not find IP at head of list" );
191
192         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
193         errors += fail_if_false( i, "has_myip did not find IP at tail of list" );
194
195         i = has_myip( "192.168.4.30:1235", if_list, ',', 128 );                                                                                 // should find our ip when only in list
196         errors += fail_if_false( i, "has_myip did not find IP when only one in list" );
197
198         return errors > 0;                      // overall exit code bad if errors
199 }