enhance(API): Add source IP support to msg header
[ric-plt/lib/rmr.git] / test / sr_nano_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         Mmemonic:       sr_nano_static_test.c
23         Abstract:       Test the send/receive funcitons. These are meant to be included at compile
24                                 time by the test driver.
25
26         Author:         E. Scott Daniels
27         Date:           3 April 2019
28 */
29
30 #include <unistd.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <strings.h>
34 #include <errno.h>
35 #include <string.h>
36 #include <stdint.h>
37 #include <pthread.h>
38 #include <semaphore.h>
39
40 #include "rmr.h"
41 #include "rmr_agnostic.h"
42
43 /*
44         Generate a simple route table (for all but direct route table testing).
45         This table contains multiple tables inasmuch as a second update set of
46         records follows the initial set. 
47 */
48 static void gen_rt( uta_ctx_t* ctx ) {
49         int             fd;
50         char*   rt_stuff;               // strings for the route table
51
52         rt_stuff =
53                 "newrt|end\n"                                                           // end of table check before start of table found
54                 "# comment to drive full comment test\n"
55                 "\n"                                                                            // handle blank lines
56                 "   \n"                                                                         // handle blank lines
57             "mse|4|10|localhost:4561\n"                                 // entry before start message
58             "rte|4|localhost:4561\n"                                    // entry before start message
59                 "newrt|start\n"                                                         // false start to drive detection
60                 "xxx|badentry to drive default case"
61                 "newrt|start\n"
62             "rte|0|localhost:4560,localhost:4562\n"                                     // these are legitimate entries for our testing
63             "rte|1|localhost:4562;localhost:4561,localhost:4569\n"
64             "rte|2|localhost:4562| 10\n"                                                                // new subid at end
65             "mse|4|10|localhost:4561\n"                                                                 // new msg/subid specifier rec
66             "mse|4|localhost:4561\n"                                                                    // new mse entry with less than needed fields
67                 "   rte|   5   |localhost:4563    #garbage comment\n"           // tests white space cleanup
68             "rte|6|localhost:4562\n"
69                 "newrt|end\n";
70
71         fd = open( "utesting.rt", O_WRONLY | O_CREAT, 0600 );
72         if( fd < 0 ) {
73                 fprintf( stderr, "<BUGGERED> unable to open file for testing route table gen\n" );
74                 return;
75         }
76
77         setenv( "RMR_SEED_RT", "utesting.rt", 1 );
78         write( fd, rt_stuff, strlen( rt_stuff ) );
79         close( fd );
80         read_static_rt( ctx, 0 );
81         unlink( "utesting.rt" );
82 }
83
84
85 /*
86         Drive the send and receive functions.  We also drive as much of the route
87         table collector as is possible without a real rtg process running somewhere.
88
89         Send and receive functions are indirectly exercised from the rmr_nano_static_test
90         module as it tests the user facing send/receive/call/rts functions. These tests
91         should exercise specific cases for the internal functions as they will not
92         specifically be driven elsewhere.
93 */
94 static int sr_nano_test() {
95         int errors = 0;                 // number errors found
96
97         uta_ctx_t* ctx;                         // context needed to test load static rt
98         uta_ctx_t*      real_ctx;       // real one to force odd situations for error testing
99         rmr_mbuf_t*     mbuf;           // mbuf to send/receive
100         rmr_mbuf_t*     mb2;            // error capturing msg buf
101         int             whid = -1;
102         int             last_whid;
103         int     state;
104         int nn_dummy_sock;                                      // dummy needed to drive send
105         int             size;
106         int             i;
107         void*   p;
108
109         //ctx = rmr_init( "tcp:4360", 2048, 0 );                                // do NOT call init -- that starts the rtc thread which isn't good here
110         ctx = (uta_ctx_t *) malloc( sizeof( uta_ctx_t ) );              // alloc the context manually
111         memset( ctx, 0, sizeof( uta_ctx_t ) );
112
113         ctx->mring = NULL;              //uta_mk_ring( 128 );
114         ctx->max_plen = RMR_MAX_RCV_BYTES + sizeof( uta_mhdr_t );
115         ctx->max_mlen = ctx->max_plen + sizeof( uta_mhdr_t );
116         ctx->my_name = strdup( "dummy-test" );
117         ctx->my_ip = strdup( "30.4.19.86:1111" );
118         uta_lookup_rtg( ctx );
119
120         gen_rt( ctx );                                                          // forces a static load with some known info since we don't start the rtc()
121         gen_rt( ctx );                                                          // force a second load to test cloning
122
123         p = rt_ensure_ep( NULL, "foo" );                                // drive for coverage
124         errors += fail_not_nil( p,  "rt_ensure_ep did not return nil when given nil route table" );
125
126         state = rmr_ready( NULL );
127         errors += fail_if_true( state, "reported ready when given a nil context" );
128         state = rmr_ready( ctx );
129         errors += fail_if_false( state, "reported not ready when it should be" );
130
131         mbuf = rcv_msg( ctx, NULL );
132         errors += fail_if_nil( mbuf, "no mbuf returned on receive test" );
133
134         mbuf->len = 10;
135         mbuf->mtype = 1;
136
137         mb2 = clone_msg( mbuf );
138         errors += fail_if_nil( mb2, "clone message returned nil pointer" );
139         //errors += fail_not_equal( mbuf->flags, mb2->flags, "clone did not duplicate flags" );
140         errors += fail_not_equal( mbuf->alloc_len, mb2->alloc_len, "clone did not dup alloc-len" );
141         errors += fail_not_equal( mbuf->state, mb2->state, "clone did not dup state" );
142         rmr_free_msg( mb2 );
143
144         mbuf = rmr_send_msg( NULL, mbuf );
145         errors += fail_if_nil( mbuf, "send with nil context but buffere didn't return buffer" );
146         if( mbuf ) {
147                 errors += fail_not_equal( mbuf->state, RMR_ERR_BADARG, "send with buffer but nil context didn't return right state" );
148         } else {
149                 mbuf = rmr_rcv_msg( ctx, NULL );
150         }
151
152         size = 4096;
153         state = rmr_payload_size( mbuf );
154         errors += fail_not_equal( state, size, "payload size (b) didn't return expected value (a)" );   // receive should always give 4k buffer
155
156         rmr_free_msg( mbuf );
157
158
159         // ---- direct message read into payload (no rmr header) -------------------------
160         mbuf = rcv_payload( ctx, NULL );
161         errors += fail_if_nil( mbuf, "rcv_payload did not return a message buffer when given a nil messge" );
162         if( mbuf ) {
163                 errors += fail_if_true( mbuf->len <= 0, "rcv_payload did not return a buffer with payload length set when given a nil messge" );
164                 errors += fail_not_equal( mbuf->state, 0, "rcv_payload did not return a buffer with good state when given a nil messge" );
165         }
166
167         mbuf = rcv_payload( ctx, NULL );
168         errors += fail_if_nil( mbuf, "rcv_payload did not return a message buffer" );
169         if( mbuf ) {
170                 errors += fail_if_true( mbuf->len <= 0, "rcv_payload did not return a buffer with payload length set" );
171                 errors += fail_not_equal( mbuf->state, 0, "rcv_payload did not return a buffer with good state" );
172         }
173
174         // ---- drive rtc in a 'static' (not pthreaded) mode to get some coverage; no 'results' to be verified -----
175         setenv( ENV_RTG_RAW, "1", 1 );                                                          // rtc should expect raw messages (mostly coverage here)
176         setenv( ENV_VERBOSE_FILE, ".ut_rmr_verbose", 1 );                       // allow for verbose code in rtc to be driven
177         i = open( ".ut_rmr_verbose", O_RDWR | O_CREAT, 0654 );
178         if( i >= 0 ) {
179                 write( i, "2\n", 2 );
180                 close( i );
181         }
182         ctx->shutdown = 1;                      // should force rtc to quit on first pass
183         rtc( NULL );                            // coverage test with nil pointer
184         rtc( ctx );
185
186
187         // --- drive the route table things which are nanomsg specific ------
188
189         return !!errors;
190 }