1 // : vi ts=4 sw=4 noet :
3 ==================================================================================
4 Copyright (c) 2019 Nokia
5 Copyright (c) 2018-2019 AT&T Intellectual Property.
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
11 http://www.apache.org/licenses/LICENSE-2.0
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 ==================================================================================
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.
26 Author: E. Scott Daniels
38 #include "../src/common/include/rmr.h"
39 #include "../src/common/include/rmr_agnostic.h"
42 Generate a simple route table (for all but direct route table testing).
44 static void gen_rt( uta_ctx_t* ctx ) {
46 char* rt_stuff; // strings for the route table
49 "newrt|end\n" // end of table check before start of table found
50 "# comment to drive full comment test\n"
51 "\n" // handle blank lines
52 " \n" // handle blank lines
53 "mse|4|10|localhost:4561\n" // entry before start message
54 "rte|4|localhost:4561\n" // entry before start message
55 "newrt|start\n" // false start to drive detection
56 "xxx|badentry to drive default case"
58 "rte|0|localhost:4560,localhost:4562\n" // these are legitimate entries for our testing
59 "rte|1|localhost:4562;localhost:4561,localhost:4569\n"
60 "rte|2|localhost:4562| 10\n" // new subid at end
61 "mse|4|10|localhost:4561\n" // new msg/subid specifier rec
62 "mse|4|localhost:4561\n" // new mse entry with less than needed fields
63 " rte| 5 |localhost:4563 #garbage comment\n" // tests white space cleanup
64 "rte|6|localhost:4562\n"
67 fd = open( "utesting.rt", O_WRONLY | O_CREAT, 0600 );
69 fprintf( stderr, "<BUGGERED> unable to open file for testing route table gen\n" );
73 setenv( "RMR_SEED_RT", "utesting.rt", 1 );
74 write( fd, rt_stuff, strlen( rt_stuff ) );
76 read_static_rt( ctx, 0 );
77 unlink( "utesting.rt" );
82 Drive the send and receive functions. We also drive as much of the route
83 table collector as is possible without a real rtg process running somewhere.
85 Send and receive functions are indirectly exercised from the rmr_nng_static_test
86 module as it tests the user facing send/receive/call/rts functions. These tests
87 should exercise specific cases for the internal functions as they will not
88 specifically be driven elsewhere.
90 static int sr_nng_test() {
91 uta_ctx_t* ctx; // context needed to test load static rt
92 uta_ctx_t* real_ctx; // real one to force odd situations for error testing
93 int errors = 0; // number errors found
94 rmr_mbuf_t* mbuf; // mbuf to send/receive
95 rmr_mbuf_t* mb2; // error capturing msg buf
99 nng_socket nn_dummy_sock; // dummy needed to drive send
104 //ctx = rmr_init( "tcp:4360", 2048, 0 ); // do NOT call init -- that starts the rtc thread which isn't good here
105 ctx = (uta_ctx_t *) malloc( sizeof( uta_ctx_t ) ); // alloc the context manually
106 memset( ctx, 0, sizeof( uta_ctx_t ) );
108 ctx->mring = NULL; //uta_mk_ring( 128 );
109 ctx->max_plen = RMR_MAX_RCV_BYTES + sizeof( uta_mhdr_t );
110 ctx->max_mlen = ctx->max_plen + sizeof( uta_mhdr_t );
111 ctx->my_name = strdup( "dummy-test" );
112 uta_lookup_rtg( ctx );
114 gen_rt( ctx ); // forces a static load with some known info since we don't start the rtc()
115 gen_rt( ctx ); // force a second load to test cloning
117 p = rt_ensure_ep( NULL, "foo" ); // drive for coverage
118 errors += fail_not_nil( p, "rt_ensure_ep did not return nil when given nil route table" );
120 state = rmr_ready( NULL );
121 errors += fail_if_true( state, "reported ready when given a nil context" );
122 state = rmr_ready( ctx );
123 errors += fail_if_false( state, "reported not ready when it should be" );
125 mbuf = rcv_msg( ctx, NULL );
126 errors += fail_if_nil( mbuf, "no mbuf returned on receive test" );
131 mb2 = clone_msg( mbuf );
132 errors += fail_if_nil( mb2, "clone message returned nil pointer" );
133 errors += fail_not_equal( mbuf->flags, mb2->flags, "clone did not duplicate flags" );
134 errors += fail_not_equal( mbuf->alloc_len, mb2->alloc_len, "clone did not dup alloc-len" );
135 errors += fail_not_equal( mbuf->state, mb2->state, "clone did not dup state" );
138 mbuf = rmr_send_msg( NULL, mbuf );
139 errors += fail_if_nil( mbuf, "send with nil context but buffere didn't return buffer" );
141 errors += fail_not_equal( mbuf->state, RMR_ERR_BADARG, "send with buffer but nil context didn't return right state" );
143 mbuf = rmr_rcv_msg( ctx, NULL );
146 size = 2048 - sizeof( uta_mhdr_t ); // emulated nng receive allocates 2K payloads
147 state = rmr_payload_size( mbuf );
148 errors += fail_not_equal( state, size, "payload size didn't return expected value" ); // receive should always give 4k buffer
150 rmr_free_msg( mbuf );
153 state = xlate_nng_state( NNG_EAGAIN, 99 );
154 errors += fail_if( state == 99, "xlate_nng_state returned default for nng_eagain" );
155 errors += fail_if( errno != EAGAIN, "xlate_nng_state did not set errno to eagain for nng_eagain" );
157 state = xlate_nng_state( NNG_ETIMEDOUT, 99 );
158 errors += fail_if( state == 99, "xlate_nng_state returned default for nng_timeout" );
159 errors += fail_if( errno != EAGAIN, "xlate_nng_state did not set errno to eagain for nng_timeout" );
161 state = xlate_nng_state( NNG_ENOTSUP, 99 );
162 errors += fail_if( state != 99, "xlate_nng_state did not return default for nng_notsup" );
164 state = xlate_nng_state( NNG_ENOTSUP, 99 );
165 errors += fail_if( state != 99, "xlate_nng_state did not return default for nng_notsup" );
166 errors += fail_if( errno == 0, "xlate_nng_state did not set errno (1)" );
168 state = xlate_nng_state( NNG_EINVAL, 99 );
169 errors += fail_if( state != 99, "xlate_nng_state did not return default for nng_inval" );
170 errors += fail_if( errno == 0, "xlate_nng_state did not set errno (2)" );
172 state = xlate_nng_state( NNG_ENOMEM, 99 );
173 errors += fail_if( state != 99, "xlate_nng_state did not return default for nng_nomem" );
174 errors += fail_if( errno == 0, "xlate_nng_state did not set errno (3)" );
176 state = xlate_nng_state( NNG_ESTATE, 99 );
177 errors += fail_if( state != 99, "xlate_nng_state did not return default for nng_state" );
178 errors += fail_if( errno == 0, "xlate_nng_state did not set errno (4)" );
180 state = xlate_nng_state( NNG_ECLOSED, 99 );
181 errors += fail_if( state != 99, "xlate_nng_state did not return default for nng_closed" );
182 errors += fail_if( errno == 0, "xlate_nng_state did not set errno (5)" );
184 state = xlate_nng_state( 999, 99 );
185 errors += fail_if( state != 99, "xlate_nng_state did not return default for unknown error" );
186 errors += fail_if( errno == 0, "xlate_nng_state did not set errno (6)" );
188 // ---- drive rtc in a 'static' (not pthreaded) mode to get some coverage; no 'results' to be verified -----
189 setenv( ENV_RTG_RAW, "1", 1 ); // rtc should expect raw messages (mostly coverage here)
190 setenv( ENV_VERBOSE_FILE, ".ut_rmr_verbose", 1 ); // allow for verbose code in rtc to be driven
191 i = open( ".ut_rmr_verbose", O_RDWR | O_CREAT, 0654 );
193 write( i, "2\n", 2 );
196 ctx->shutdown = 1; // should force rtc to quit on first pass
197 rtc( NULL ); // coverage test with nil pointer