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 <semaphore.h>
41 #include "rmr_agnostic.h"
43 // ----------- local test support ----------------------------------------------------------
44 #define CLONE 1 // convenience constants for payload realloc tests
50 Drive the send and receive functions. We also drive as much of the route
51 table collector as is possible without a real rtg process running somewhere.
53 Send and receive functions are indirectly exercised from the rmr_nng_static_test
54 module as it tests the user facing send/receive/call/rts functions. These tests
55 should exercise specific cases for the internal functions as they will not
56 specifically be driven elsewhere.
58 This requires the gen_rt funcition that is in the test_gen_rt module and should
59 have been included by the test module(s) which include this.
61 static int sr_nng_test() {
62 uta_ctx_t* ctx; // context needed to test load static rt
63 uta_ctx_t* real_ctx; // real one to force odd situations for error testing
64 int errors = 0; // number errors found
65 rmr_mbuf_t* mbuf; // mbuf to send/receive
66 rmr_mbuf_t* mb2; // second mbuf when needed
70 nng_socket nn_dummy_sock; // dummy needed to drive send
76 //ctx = rmr_init( "tcp:4360", 2048, 0 ); // do NOT call init -- that starts the rtc thread which isn't good here
77 ctx = (uta_ctx_t *) malloc( sizeof( uta_ctx_t ) ); // alloc the context manually
78 memset( ctx, 0, sizeof( uta_ctx_t ) );
80 ctx->mring = NULL; //uta_mk_ring( 128 );
81 ctx->max_plen = RMR_MAX_RCV_BYTES + sizeof( uta_mhdr_t );
82 ctx->max_mlen = ctx->max_plen + sizeof( uta_mhdr_t );
83 ctx->my_name = strdup( "dummy-test" );
84 ctx->my_ip = strdup( "30.4.19.86:1111" );
85 uta_lookup_rtg( ctx );
87 gen_rt( ctx ); // forces a static load with some known info since we don't start the rtc()
88 gen_rt( ctx ); // force a second load to test cloning
90 p = rt_ensure_ep( NULL, "foo" ); // drive for coverage
91 errors += fail_not_nil( p, "rt_ensure_ep did not return nil when given nil route table" );
93 state = rmr_ready( NULL );
94 errors += fail_if_true( state, "reported ready when given a nil context" );
95 state = rmr_ready( ctx );
96 errors += fail_if_false( state, "reported not ready when it should be" );
98 mbuf = rcv_msg( ctx, NULL );
99 errors += fail_if_nil( mbuf, "no mbuf returned on receive test" );
104 mb2 = clone_msg( mbuf );
105 errors += fail_if_nil( mb2, "clone message returned nil pointer" );
106 errors += fail_not_equal( mbuf->flags, mb2->flags, "clone did not duplicate flags" );
107 errors += fail_not_equal( mbuf->alloc_len, mb2->alloc_len, "clone did not dup alloc-len" );
108 errors += fail_not_equal( mbuf->state, mb2->state, "clone did not dup state" );
111 mbuf = rmr_send_msg( NULL, mbuf );
112 errors += fail_if_nil( mbuf, "send with nil context but buffere didn't return buffer" );
114 errors += fail_not_equal( mbuf->state, RMR_ERR_BADARG, "send with buffer but nil context didn't return right state" );
116 mbuf = rmr_rcv_msg( ctx, NULL );
119 size = 2048 - em_hdr_size(); // emulated nng receive allocates 2K buffers -- subtract off header size
120 state = rmr_payload_size( mbuf );
121 errors += fail_not_equal( state, size, "payload size didn't return expected value" ); // receive should always give 4k buffer
123 rmr_free_msg( mbuf );
126 state = xlate_nng_state( NNG_EAGAIN, 99 );
127 errors += fail_if( state == 99, "xlate_nng_state returned default for nng_eagain" );
128 errors += fail_if( errno != EAGAIN, "xlate_nng_state did not set errno to eagain for nng_eagain" );
130 state = xlate_nng_state( NNG_ETIMEDOUT, 99 );
131 errors += fail_if( state == 99, "xlate_nng_state returned default for nng_timeout" );
132 errors += fail_if( errno != EAGAIN, "xlate_nng_state did not set errno to eagain for nng_timeout" );
134 state = xlate_nng_state( NNG_ENOTSUP, 99 );
135 errors += fail_if( state != 99, "xlate_nng_state did not return default for nng_notsup" );
137 state = xlate_nng_state( NNG_ENOTSUP, 99 );
138 errors += fail_if( state != 99, "xlate_nng_state did not return default for nng_notsup" );
139 errors += fail_if( errno == 0, "xlate_nng_state did not set errno (1)" );
141 state = xlate_nng_state( NNG_EINVAL, 99 );
142 errors += fail_if( state != 99, "xlate_nng_state did not return default for nng_inval" );
143 errors += fail_if( errno == 0, "xlate_nng_state did not set errno (2)" );
145 state = xlate_nng_state( NNG_ENOMEM, 99 );
146 errors += fail_if( state != 99, "xlate_nng_state did not return default for nng_nomem" );
147 errors += fail_if( errno == 0, "xlate_nng_state did not set errno (3)" );
149 state = xlate_nng_state( NNG_ESTATE, 99 );
150 errors += fail_if( state != 99, "xlate_nng_state did not return default for nng_state" );
151 errors += fail_if( errno == 0, "xlate_nng_state did not set errno (4)" );
153 state = xlate_nng_state( NNG_ECLOSED, 99 );
154 errors += fail_if( state != 99, "xlate_nng_state did not return default for nng_closed" );
155 errors += fail_if( errno == 0, "xlate_nng_state did not set errno (5)" );
157 state = xlate_nng_state( 999, 99 );
158 errors += fail_if( state != 99, "xlate_nng_state did not return default for unknown error" );
159 errors += fail_if( errno == 0, "xlate_nng_state did not set errno (6)" );
161 // ---- drive rtc in a 'static' (not pthreaded) mode to get some coverage; no 'results' to be verified -----
162 setenv( ENV_RTG_RAW, "1", 1 ); // rtc should expect raw messages (mostly coverage here)
163 setenv( ENV_VERBOSE_FILE, ".ut_rmr_verbose", 1 ); // allow for verbose code in rtc to be driven
164 i = open( ".ut_rmr_verbose", O_RDWR | O_CREAT, 0654 );
166 write( i, "2\n", 2 );
169 ctx->shutdown = 1; // should force rtc to quit on first pass
170 rtc( NULL ); // coverage test with nil pointer
173 setenv( "RMR_RTG_SVC", "4567", 1 ); // drive for edge case coverage to ensure no nil pointer etc
175 setenv( "RMR_RTG_SVC", "tcp:4567", 1 );
177 setenv( "RMR_RTG_SVC", "tcp:4567:error", 1 );
180 // ------------- reallocation tests ------------------------------------------------------------
181 // we use mk_populated_msg() to create a message with mid/sid/plen pushed into the transport
182 // header to simulate a message having been sent and received which is what causes this data
183 // to push into the wire packet.
185 payload_str = "Stand Up and Cheer; OU78-82";
188 mbuf = mk_populated_msg( 1024, 0, 99, 100, strlen( payload_str ) );
189 memcpy( mbuf->payload, payload_str, mbuf->len );
190 mb2 = realloc_payload( mbuf, 512, NO_COPY, NO_CLONE );
191 errors += fail_if_nil( mb2, "realloc_payload (no copy) returned a nil pointer when reallocating with smaller size" );
192 errors += fail_if_false( mbuf == mb2, "non-clone realloc payload (no copy) of smaller size did not return the same buffer" );
194 mb2 = realloc_payload( NULL, 512, NO_COPY, NO_CLONE );
195 errors += fail_not_nil( mb2, "realloc payload did not return nil pointer when passed nil mbuf" );
197 mb2 = realloc_payload( mbuf, 0, NO_COPY, NO_CLONE );
198 errors += fail_not_nil( mb2, "realloc payload did not return nil pointer when passed bad len" );
200 fprintf( stderr, "<TEST> no copy/no clone test starts\n" );
201 mb2 = realloc_payload( mbuf, 2048, NO_COPY, NO_CLONE );
202 errors += fail_if_false( mbuf == mb2, "realloc payload (no copy) of larger size did not return the same msg buffer(1)" );
203 errors += fail_not_equal( mb2->mtype, -1, "realloc payload (no copy) did not reset mtype(a) to expected(b) value" );
204 errors += fail_not_equal( mb2->sub_id, -1, "realloc payload (no copy) did not reset sub-id(a) to expected(b) value" );
205 errors += fail_if_nil( mb2, "realloc payload returned (no copy) a nil pointer when increasing payload len" );
206 errors += fail_not_equal( mb2->len, 0, "realloc payload payload len(a) not expected(b):" );
207 errors += fail_not_equal( rmr_payload_size( mb2), 2048, "realloc payload alloc len(a) not expected(b)" );
209 fprintf( stderr, "<TEST> copy/no clone test starts\n" );
210 mbuf = mk_populated_msg( 1024, 0, 99, 100, strlen( payload_str ) );
211 memcpy( mbuf->payload, payload_str, mbuf->len );
212 mb2 = realloc_payload( mbuf, 2048, COPY, NO_CLONE );
213 errors += fail_if_false( mbuf == mb2, "non-clone realloc payload (copy) of larger size did not return the same msg buffer(2)" );
214 errors += fail_if_nil( mb2, "realloc payload (copy) returned a nil pointer when increasing payload len)" );
215 errors += fail_not_equal( mb2->mtype, 99, "realloc payload (copy) did not reset mtype(a) to expected(b) value" );
216 errors += fail_not_equal( mb2->sub_id, 100, "realloc payload (copy) did not reset sub-id(a) to expected(b) value" );
217 errors += fail_if_equal( mb2->len, 0, "realloc payload (copy) msg len(a) not expected(b)" );
218 errors += fail_not_equal( rmr_payload_size( mb2), 2048, "realloc payload (copy) alloc len(a) not expected(b)" );
219 errors += fail_not_equal( strncmp( payload_str, mb2->payload, strlen( payload_str )), 0, "realloc payload(copy) didn't copy payload" );
221 fprintf( stderr, "<TEST> copy/clone test starts requested buffer smaller than original\n" );
222 mbuf = mk_populated_msg( 1024, 0, 99, 100, strlen( payload_str ) );
223 memcpy( mbuf->payload, payload_str, mbuf->len );
224 mb2 = realloc_payload( mbuf, 512, COPY, CLONE );
225 errors += fail_if_true( mbuf == mb2, "realloc payload (clone+copy) of larger size did not return different message buffers" );
226 errors += fail_if_nil( mb2, "realloc payload (clone+copy) returned a nil pointer when increasing payload len)" );
227 errors += fail_not_equal( mb2->mtype, 99, "realloc payload (clone+copy) did not reset mtype(a) to expected(b) value" );
228 errors += fail_not_equal( mb2->sub_id, 100, "realloc payload (clone+copy) did not reset sub-id(a) to expected(b) value" );
229 errors += fail_not_equal( mb2->len, strlen( payload_str ), "realloc payload (clone+copy) msg len(a) not expected(b)" );
230 errors += fail_not_equal( rmr_payload_size( mb2), 1024, "realloc payload (clone+copy) alloc len(a) not expected(b)" );
231 errors += fail_not_equal( strncmp( payload_str, mb2->payload, strlen( payload_str )), 0, "realloc payload(clone+copy) didn't copy payload" );
233 // with a clone, we must verify that original message looks sane too
234 errors += fail_not_equal( mbuf->mtype, 99, "realloc payload (clone+copy) validation of unchanged mbuf->mtype fails" );
235 errors += fail_not_equal( mbuf->sub_id, 100, "realloc payload (clone+copy) validation of unchanged mbuf->subid fails" );
236 errors += fail_not_equal( mbuf->len, strlen( payload_str ), "realloc payload (clone+copy) validation of unchanged payload len fails" );
237 errors += fail_not_equal( rmr_payload_size( mbuf ), 1024, "realloc payload (clone+copy) validation of unchanged alloc length fails" );
238 errors += fail_not_equal( strncmp( payload_str, mbuf->payload, strlen( payload_str )), 0, "realloc payload(clone+copy) validation of unchanged payload fails" );
241 fprintf( stderr, "<TEST> copy/clone test starts requested buf is larger than original\n" );
242 mbuf = mk_populated_msg( 1024, 0, 99, 100, strlen( payload_str ) );
243 memcpy( mbuf->payload, payload_str, mbuf->len );
244 mb2 = realloc_payload( mbuf, 2048, COPY, CLONE );
245 errors += fail_if_true( mbuf == mb2, "realloc payload(clone+copy/lg) of larger size did not return different message buffers" );
246 errors += fail_if_nil( mb2, "realloc payload (clone+copy/lg) returned a nil pointer when increasing payload len)" );
247 errors += fail_not_equal( mb2->mtype, 99, "realloc payload (clone+copy/lg) did not reset mtype(a) to expected(b) value" );
248 errors += fail_not_equal( mb2->sub_id, 100, "realloc payload (clone+copy/lg) did not reset sub-id(a) to expected(b) value" );
249 errors += fail_not_equal( mb2->len, strlen( payload_str ), "realloc payload (clone+copy/lg) msg len(a) not expected(b)" );
250 errors += fail_not_equal( rmr_payload_size( mb2), 2048, "realloc payload (clone+copy/lg) alloc len(a) not expected(b)" );
251 errors += fail_not_equal( strncmp( payload_str, mb2->payload, strlen( payload_str )), 0, "realloc payload(clone+copy/lg) didn't copy payload" );
253 // with a clone, we must verify that original message looks sane too
254 errors += fail_not_equal( mbuf->mtype, 99, "realloc payload (clone+copy/lg) validation of unchanged mbuf->mtype fails" );
255 errors += fail_not_equal( mbuf->sub_id, 100, "realloc payload (clone+copy/lg) validation of unchanged mbuf->subid fails" );
256 errors += fail_not_equal( mbuf->len, strlen( payload_str ), "realloc payload (clone+copy/lg) validation of unchanged payload len fails" );
257 errors += fail_not_equal( rmr_payload_size( mbuf ), 1024, "realloc payload (clone+copy/lg) validation of unchanged alloc length fails" );
258 errors += fail_not_equal( strncmp( payload_str, mbuf->payload, strlen( payload_str )), 0, "realloc payload(clone+copy/lg) validation of unchanged payload fails" );
260 // original message should be unharmed, and new message should have no type/sid or payload len; total alloc len should be requested enlargement
261 fprintf( stderr, "<TEST> no copy/clone test starts requested buf is larger than original\n" );
262 mbuf = mk_populated_msg( 1024, 0, 99, 100, strlen( payload_str ) );
263 memcpy( mbuf->payload, payload_str, mbuf->len );
264 mb2 = realloc_payload( mbuf, 2048, NO_COPY, CLONE );
265 errors += fail_if_true( mbuf == mb2, "realloc payload (clone+nocopy) of larger size did not return different message buffers" );
266 errors += fail_if_nil( mb2, "realloc payload (clone+nocopy) returned a nil pointer when increasing payload len)" );
267 errors += fail_not_equal( mb2->mtype, -1, "realloc payload (clone+nocopy) did not reset mtype(a) to expected(b) value" );
268 errors += fail_not_equal( mb2->sub_id, -1, "realloc payload (clone+nocopy) did not reset sub-id(a) to expected(b) value" );
269 errors += fail_not_equal( mb2->len, 0, "realloc payload (clone+nocopy) msg len(a) not expected(b)" );
270 errors += fail_not_equal( rmr_payload_size( mb2 ), 2048, "realloc payload (clone+nocopy) alloc len(a) not expected(b)" );
271 errors += fail_if_equal( strncmp( payload_str, mb2->payload, strlen( payload_str )), 0, "realloc payload(clone+nocopy) copied payload when not supposed to" );
273 // with a clone, we must verify that original message looks sane too
274 errors += fail_not_equal( mbuf->mtype, 99, "realloc payload (clone+nocopy) validation of unchanged mbuf->mtype fails" );
275 errors += fail_not_equal( mbuf->sub_id, 100, "realloc payload (clone+nocopy) validation of unchanged mbuf->subid fails" );
276 errors += fail_not_equal( mbuf->len, strlen( payload_str ), "realloc payload (clone+nocopy) validation of unchanged payload len fails" );
277 errors += fail_not_equal( rmr_payload_size( mbuf ), 1024, "realloc payload (clone+nocopy) validation of unchanged alloc length fails" );
278 errors += fail_not_equal( strncmp( payload_str, mbuf->payload, strlen( payload_str )), 0, "realloc payload (clone+nocopy) validation of unchanged payload fails" );