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: wormhole_static.c
23 Abstract: Specific tests for wormhole. This module is included directly by
24 the test driver at compile time.
26 Author: E. Scott Daniels
38 #include <semaphore.h>
41 #include "rmr_agnostic.h"
45 Note that the last tests in this function destroy the context and message so
46 any tests added MUST be ahead of those tests.
48 static int worm_test( ) {
49 uta_ctx_t* ctx; // context needed to test load static rt
51 int errors = 0; // number errors found
56 rmr_mbuf_t* mbuf; // mbuf to send to peer
61 ctx->my_name = strdup( "tester" );
62 ctx->my_ip = strdup( "30.4.19.86:1111" );
66 whid = rmr_wh_open( NULL, NULL );
67 errors += fail_not_equal( whid, -1, "call to wh_open with invalid values did not return bad whid" );
70 whid = rmr_wh_open( ctx, NULL );
71 errors += fail_not_equal( whid, -1, "call to wh_open with invalid target did not return bad whid" );
73 whid = rmr_wh_open( ctx, "" );
74 errors += fail_not_equal( whid, -1, "call to wh_open with empty target did not return bad whid" );
76 whid = rmr_wh_open( ctx, "localhost:89219" );
77 errors += fail_if_equal( whid, -1, "call to wh_open with valid target failed" );
79 rmr_wh_close( ctx, 4 ); // test for coverage only; [5] should have nil pointer
80 rmr_wh_close( ctx, 50 ); // test for coverage only; more than allocated reference
83 whid = rmr_wh_open( ctx, "localhost:89219" );
84 errors += fail_not_equal( whid, last_whid, "call to wh_open with duplicate target did not return the same whid" );
86 for( i = 0; i < 20; i++ ) { // test ability to extend the table
87 snprintf( wbuf, sizeof( wbuf ), "localhost:864%02d", i ); // new address for each so whid is different
88 whid = rmr_wh_open( ctx, wbuf );
89 snprintf( wbuf, sizeof( wbuf ), "call to wh_open failed for iteration = %d", i );
90 errors += fail_if_equal( whid, -1, wbuf );
92 snprintf( wbuf, sizeof( wbuf ), "call to wh_open for iteration = %d returned same whid: %d", i, whid );
93 errors += fail_if_equal( whid, last_whid, wbuf );
99 rmr_wh_close( ctx, 3 ); // close one, then open a new one to verify that hole is found
100 whid = rmr_wh_open( ctx, "localhost:21961" );
101 errors += fail_not_equal( whid, 3, "attempt to fill in a hole didn't return expected" );
103 p = rmr_wh_send_msg( NULL, 0, NULL ); // tests for coverage
104 fail_not_nil( p, "wh_send_msg returned a pointer when given nil context and message" );
106 p = rmr_wh_send_msg( ctx, 0, NULL );
107 fail_not_nil( p, "wh_send_msg returned a pointer when given nil message with valid context" );
109 mbuf = rmr_alloc_msg( ctx, 2048 ); // get an muf to pass round
110 errors += fail_if_nil( mbuf, "unable to allocate mbuf for send tests (giving up on send tests)" );
113 mbuf = rmr_wh_send_msg( NULL, 0, mbuf );
115 fail_if_equal( mbuf->state, 0, "wh_send_msg returned a zero state when given a nil context" );
117 fail_if_nil( mbuf, "wh_send_msg returned a nil message buffer when given a nil context" );
121 if( !(mbuf = rmr_wh_send_msg( ctx, 50, mbuf )) ) { // test for coverage
122 errors += fail_if_nil( mbuf, "send didn't return an mbuf (skip rest of send tests)" );
126 mbuf = rmr_wh_send_msg( ctx, 4, mbuf );
127 errors += fail_not_equal( mbuf->state, RMR_OK, "valid wormhole send failed" );
128 errors += fail_not_equal( errno, 0, "errno after valid wormhole send was not 0" );
130 rmr_wh_close( ctx, 4 );
131 mbuf = rmr_wh_send_msg( ctx, 4, mbuf );
132 rmr_wh_send_msg( ctx, 4, mbuf );
133 errors += fail_not_equal( mbuf->state, RMR_ERR_WHID, "send on closed wormhole didn't set correct state in msg" );
138 // ---- wormhole state -----------
139 state = rmr_wh_state( NULL, 0 );
140 errors += fail_if_equal( state, RMR_OK, "wh_state with bad context did not return error" );
142 state = rmr_wh_state( ctx, -1 );
143 errors += fail_if_equal( state, RMR_OK, "wh_state with bad whid did not return error" );
145 whid = rmr_wh_open( ctx, "localhost:9219" );
146 if( ! fail_if_equal( whid, -1, "skipping some wh_state tests: no whid returned" ) ) {
147 state = rmr_wh_state( ctx, whid );
148 errors += fail_not_equal( state, RMR_OK, "wh_state on an open wh did not return OK" );
150 rmr_wh_close( ctx, whid );
151 state = rmr_wh_state( ctx, whid );
152 errors += fail_if_equal( state, RMR_OK, "wh_state on a closed wh returned OK" );
156 // -----------------------------------------------------------------------
157 // WARNING: these tests destroy the context, so they MUST be last
158 if( mbuf ) { // only if we got an mbuf
161 mbuf = rmr_wh_send_msg( ctx, 5, mbuf ); // coverage test on mbuf header check
162 errors += fail_not_equal( errno, EBADMSG, "wh_send didn't set errno after bad mbuf send" );
163 errors += fail_not_equal( mbuf->state, RMR_ERR_NOHDR, "send with bad header did now set msg state correctly" );
167 ctx->wormholes = NULL;
168 mbuf = rmr_wh_send_msg( ctx, 4, mbuf ); // coverage test on mbuf header check
169 errors += fail_not_equal( errno, EINVAL, "wh_send didn't set errno after send without wormole reference" );
170 errors += fail_not_equal( mbuf->state, RMR_ERR_NOWHOPEN, "wh_send didn't set msg state after send without wormole reference" );
172 rmr_free_msg( mbuf );
176 free( ctx->my_name );
180 return !!errors; // 1 or 0 regardless of count