test(unit): Extend unit tests
[ric-plt/lib/rmr.git] / test / sr_nng_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_nng_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
38 #include "../src/common/include/rmr.h"
39 #include "../src/common/include/rmr_agnostic.h"
40
41 /*
42         Generate a simple route table (for all but direct route table testing).
43 */
44 static void gen_rt( uta_ctx_t* ctx ) {
45         int             fd;
46         char*   rt_stuff;               // strings for the route table
47
48         rt_stuff = 
49                 "newrt|start\n"                                                         // false start to drive detection 
50                 "xxx|badentry to drive default case"
51                 "newrt|start\n"
52         "rte|0|localhost:4560,localhost:4562\n"
53         "rte|1|localhost:4562;localhost:4561,localhost:4569\n"
54         "rte|2|localhost:4562\n"
55             "rte|4|localhost:4561\n"
56                 "rte|5|localhost:4563\n"
57         "rte|6|localhost:4562\n"
58                 "newrt|end\n";
59
60         fd = open( "utesting.rt", O_WRONLY | O_CREAT, 0600 );
61         if( fd < 0 ) {
62                 fprintf( stderr, "<BUGGERED> unable to open file for testing route table gen\n" );
63                 return;
64         }
65
66         setenv( "RMR_SEED_RT", "utesting.rt", 1 );
67         write( fd, rt_stuff, strlen( rt_stuff ) );
68         close( fd );
69         read_static_rt( ctx, 0 );
70         unlink( "utesting.rt" );
71 }
72
73
74 /*
75         Drive the send and receive functions.  We also drive as much of the route
76         table collector as is possible without a real rtg process running somewhere.
77
78         Send and receive functions are indirectly exercised from the rmr_nng_static_test
79         module as it tests the user facing send/receive/call/rts functions. These tests
80         should exercise specific cases for the internal functions as they will not 
81         specifically be driven elsewhere.
82 */
83 static int sr_nng_test() {
84         uta_ctx_t* ctx;                         // context needed to test load static rt
85         uta_ctx_t*      real_ctx;       // real one to force odd situations for error testing
86         int errors = 0;                 // number errors found
87         rmr_mbuf_t*     mbuf;           // mbuf to send/receive
88         rmr_mbuf_t*     mb2;            // error capturing msg buf
89         int             whid = -1;
90         int             last_whid;
91         int     state;
92         nng_socket nn_dummy_sock;                                       // dummy needed to drive send
93         int             size;
94         int             i;
95
96         //ctx = rmr_init( "tcp:4360", 2048, 0 );                                // do NOT call init -- that starts the rtc thread which isn't good here
97         ctx = (uta_ctx_t *) malloc( sizeof( uta_ctx_t ) );              // alloc the context manually
98         memset( ctx, 0, sizeof( uta_ctx_t ) );
99
100         ctx->mring = NULL;              //uta_mk_ring( 128 );
101         ctx->max_plen = RMR_MAX_RCV_BYTES + sizeof( uta_mhdr_t );
102         ctx->max_mlen = ctx->max_plen + sizeof( uta_mhdr_t );
103         ctx->my_name = strdup( "dummy-test" );
104         uta_lookup_rtg( ctx );
105
106         gen_rt( ctx );                                                          // forces a static load with some known info since we don't start the rtc()
107
108         state = rmr_ready( NULL );
109         errors += fail_if_true( state, "reported ready when given a nil context" );
110         state = rmr_ready( ctx );
111         errors += fail_if_false( state, "reported not ready when it should be" );
112
113         mbuf = rcv_msg( ctx, NULL );
114         errors += fail_if_nil( mbuf, "no mbuf returned on receive test" );
115
116         mbuf->len = 10;
117         mbuf->mtype = 1;
118
119         mb2 = clone_msg( mbuf );
120         errors += fail_if_nil( mb2, "clone message returned nil pointer" );
121         errors += fail_not_equal( mbuf->flags, mb2->flags, "clone did not duplicate flags" );
122         errors += fail_not_equal( mbuf->alloc_len, mb2->alloc_len, "clone did not dup alloc-len" );
123         errors += fail_not_equal( mbuf->state, mb2->state, "clone did not dup state" );
124         rmr_free_msg( mb2 );    
125
126         mbuf = rmr_send_msg( NULL, mbuf );
127         errors += fail_if_nil( mbuf, "send with nil context but buffere didn't return buffer" );
128         if( mbuf ) {
129                 errors += fail_not_equal( mbuf->state, RMR_ERR_BADARG, "send with buffer but nil context didn't return right state" );
130         } else {
131                 mbuf = rmr_rcv_msg( ctx, NULL );
132         }
133
134         size = 2048 - sizeof( uta_mhdr_t );             // emulated nng receive allocates 2K payloads
135         state = rmr_payload_size( mbuf );
136         errors += fail_not_equal( state, size, "payload size didn't return expected value" );   // receive should always give 4k buffer
137
138         rmr_free_msg( mbuf );
139
140
141         state = xlate_nng_state( NNG_EAGAIN, 99 );
142         errors += fail_if( state == 99, "xlate_nng_state returned default for nng_eagain" );
143         errors += fail_if( errno != EAGAIN, "xlate_nng_state did not set errno to eagain for nng_eagain" );
144
145         state = xlate_nng_state( NNG_ETIMEDOUT, 99 );
146         errors += fail_if( state == 99, "xlate_nng_state returned default for nng_timeout" );
147         errors += fail_if( errno != EAGAIN, "xlate_nng_state did not set errno to eagain for nng_timeout" );
148
149         state = xlate_nng_state( NNG_ENOTSUP, 99 );
150         errors += fail_if( state != 99, "xlate_nng_state did not return  default for nng_notsup" );
151
152         state = xlate_nng_state( NNG_ENOTSUP, 99 );
153         errors += fail_if( state != 99, "xlate_nng_state did not return  default for nng_notsup" );
154         errors += fail_if( errno == 0, "xlate_nng_state did not set errno (1)" );
155
156         state = xlate_nng_state( NNG_EINVAL, 99 );
157         errors += fail_if( state != 99, "xlate_nng_state did not return  default for nng_inval" );
158         errors += fail_if( errno == 0, "xlate_nng_state did not set errno (2)" );
159
160         state = xlate_nng_state( NNG_ENOMEM, 99 );
161         errors += fail_if( state != 99, "xlate_nng_state did not return  default for nng_nomem" );
162         errors += fail_if( errno == 0, "xlate_nng_state did not set errno (3)" );
163
164         state = xlate_nng_state( NNG_ESTATE, 99 );
165         errors += fail_if( state != 99, "xlate_nng_state did not return  default for nng_state" );
166         errors += fail_if( errno == 0, "xlate_nng_state did not set errno (4)" );
167
168         state = xlate_nng_state( NNG_ECLOSED, 99 );
169         errors += fail_if( state != 99, "xlate_nng_state did not return  default for nng_closed" );
170         errors += fail_if( errno == 0, "xlate_nng_state did not set errno (5)" );
171
172         state = xlate_nng_state( 999, 99 );
173         errors += fail_if( state != 99, "xlate_nng_state did not return  default for unknown error" );
174         errors += fail_if( errno == 0, "xlate_nng_state did not set errno (6)" );
175
176         // ---- drive rtc in a 'static' (not pthreaded) mode -----
177         setenv( "ENV_VERBOSE_FILE", ".ut_rmr_verbose", 1 );                     // allow for verbose code in rtc to be driven
178         i = open( ".rmr_verbose", O_CREAT, 0664 );
179         if( i >= 0 ) {
180                 write( i, "0\n", 2 );
181                 close( i );
182         }
183         ctx->shutdown = 1;                      // should force rtc to quit on first pass
184         rtc( ctx );
185         
186
187         return !!errors;
188 }