CI: Add silent cmake SonarCloud scan
[ric-plt/lib/rmr.git] / test / wormhole_static_test.c
1 // : vi ts=4 sw=4 noet :
2 /*
3 ==================================================================================
4             Copyright (c) 2019-2021 Nokia
5             Copyright (c) 2018-2021 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:       wormhole_static.c
23         Abstract:       Specific tests for wormhole. This module is included directly by
24                                 the test driver at compile time.
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 /*
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.
47 */
48 static int worm_test( ) {
49         uta_ctx_t* ctx;                 // context needed to test load static rt
50         char    wbuf[1024];
51         int errors = 0;                 // number errors found
52         int             state = 0;
53         int     i;
54         void* p;
55
56         rmr_mbuf_t*     mbuf;           // mbuf to send to peer
57         rmr_mbuf_t*     resp;           // response message from a wormhole call
58         int             whid = -1;
59         int             last_whid;
60
61         ctx = mk_dummy_ctx();
62         ctx->my_name = strdup( "tester" );
63         ctx->my_ip = strdup( "30.4.19.86:1111" );
64
65         gen_rt( ctx );
66
67         // ---- must run these checks before wormhole(s) are opened --------------------------
68         mbuf = rmr_alloc_msg( ctx, 2048 );                      // get an muf to pass round, no need to init for first test
69         resp = rmr_wh_call( ctx, 0, mbuf, 1, 10 );
70         errors += fail_if_nil( resp, "wormhole call given valid context and msg, before wormholes open  didn't return message pointer" );
71         if( resp ) {
72         errors += fail_if_equal( resp->state, RMR_OK, "wormhole call given valid context and msg, before wormholes open returned ok state" );
73         }
74
75         rmr_wh_close( NULL, 0 );                        // drive for coverage; nothing to vet
76         rmr_wh_close( ctx, 0 );
77         wh_init( NULL );
78
79         // ----- end must run before opening wormholes ---------------------------------------
80
81         whid = rmr_wh_open( NULL, NULL );
82         errors += fail_not_equal( whid, -1, "call to wh_open with invalid values did not return bad whid" );
83
84
85         whid = rmr_wh_open( ctx, NULL );
86         errors += fail_not_equal( whid, -1, "call to wh_open with invalid target did not return bad whid" );
87
88         whid = rmr_wh_open( ctx, "" );
89         errors += fail_not_equal( whid, -1, "call to wh_open with empty target did not return bad whid" );
90
91         whid = rmr_wh_open( ctx, "localhost:89219" );
92         errors += fail_if_equal( whid, -1, "call to wh_open with valid target failed" );
93
94         i = wh_init( ctx );                                     // already initialised, ensure no harm
95         errors += fail_not_equal( i, 1, "second call to wh_init() didn't return true" );
96
97         rmr_wh_close( ctx, 4 );                                 // test for coverage only; [5] should have nil pointer
98         rmr_wh_close( ctx, 50 );                                        // test for coverage only; more than allocated reference
99
100         last_whid = whid;
101         whid = rmr_wh_open( ctx, "localhost:89219" );
102         errors += fail_not_equal( whid, last_whid, "call to wh_open with duplicate target did not return the same whid" );
103
104         for( i = 0; i < 20; i++ ) {                                                                                             // test ability to extend the table
105                 snprintf( wbuf, sizeof( wbuf ), "localhost:864%02d", i );                       // new address for each so whid is different
106                 whid = rmr_wh_open( ctx, wbuf );
107                 snprintf( wbuf, sizeof( wbuf ), "call to wh_open failed for iteration = %d", i );
108                 errors += fail_if_equal( whid, -1, wbuf );
109                 if( i ) {
110                         snprintf( wbuf, sizeof( wbuf ), "call to wh_open for iteration = %d returned same whid: %d", i, whid );
111                         errors += fail_if_equal( whid, last_whid, wbuf );
112                 }
113
114                 last_whid = whid;
115         }
116
117         rmr_wh_close( ctx, 3 );         // close one, then open a new one to verify that hole is found
118         whid = rmr_wh_open( ctx, "localhost:21961" );
119         errors += fail_not_equal( whid, 3, "attempt to fill in a hole didn't return expected" );
120
121         p = rmr_wh_send_msg( NULL, 0, NULL );                   // tests for coverage
122         fail_not_nil( p, "wh_send_msg returned a pointer when given nil context and message" );
123
124         p = rmr_wh_send_msg( ctx, 0, NULL );
125         fail_not_nil( p, "wh_send_msg returned a pointer when given nil message with valid context" );
126
127         mbuf = rmr_alloc_msg( ctx, 2048 );                      // get an muf to pass round
128         errors += fail_if_nil( mbuf, "unable to allocate mbuf for send tests (giving up on send tests)" );
129
130         mbuf->state = 0;
131         mbuf = rmr_wh_send_msg( NULL, 0, mbuf );
132         if( mbuf ) {
133                 fail_if_equal( mbuf->state, 0, "wh_send_msg returned a zero state when given a nil context" );
134         }
135         fail_if_nil( mbuf, "wh_send_msg returned a nil message buffer when given a nil context"  );
136
137
138         while( mbuf ) {
139                 if( !(mbuf = rmr_wh_send_msg( ctx, 50, mbuf )) ) {              // test for coverage
140                         errors += fail_if_nil( mbuf, "send didn't return an mbuf (skip rest of send tests)" );
141                         break;
142                 }
143
144                 mbuf = rmr_wh_send_msg( ctx, 4, mbuf );
145                 errors += fail_not_equal( mbuf->state, RMR_OK, "valid wormhole send failed" );
146                 errors += fail_not_equal( errno, 0, "errno after valid wormhole send was not 0" );
147
148                 rmr_wh_close( ctx, 4 );
149                 mbuf = rmr_wh_send_msg( ctx, 4, mbuf );
150                 rmr_wh_send_msg( ctx, 4, mbuf );
151                 errors += fail_not_equal( mbuf->state, RMR_ERR_WHID, "send on closed wormhole didn't set correct state in msg" );
152
153                 break;
154         }
155
156         // ---- wormhole state -----------
157         state = rmr_wh_state( NULL, 0 );
158         errors += fail_if_equal( state, RMR_OK, "wh_state with bad context did not return error" );
159
160         state = rmr_wh_state( ctx, -1 );
161         errors += fail_if_equal( state, RMR_OK, "wh_state with bad whid did not return error" );
162
163         whid = rmr_wh_open( ctx, "localhost:9219" );
164         if( ! fail_if_equal( whid, -1, "skipping some wh_state tests: no whid returned" ) ) {
165                 state = rmr_wh_state( ctx, whid );
166                 errors += fail_not_equal( state, RMR_OK, "wh_state on an open wh did not return OK" );
167
168                 rmr_wh_close( ctx, whid );
169                 state = rmr_wh_state( ctx, whid );
170                 errors += fail_if_equal( state, RMR_OK, "wh_state on a closed wh returned OK" );
171                 whid = -1;
172         }
173
174         // ----- wormhole call --------------------------------------------------
175         mbuf = rmr_alloc_msg( ctx, 2048 );                              // ensure we have a buffer to pass
176         resp = rmr_wh_call( NULL, 0, NULL, 1, 10 );     // ensure no msg when msg is nil
177         errors += fail_not_nil( resp, "wormhole call given nil context and msg didn't return nil message" );
178
179         resp = rmr_wh_call( NULL, 0, mbuf, 1, 10 );      // ensure invalid state when context is bad
180         errors += fail_if_nil( resp, "wormhole call given nil context and valid msg didn't return message pointer" );
181         if( resp != NULL ) {
182                 errors += fail_if_equal( resp->state, RMR_OK, "wormhole call given nil context and valid message returned OK state" );
183         }
184
185         resp = rmr_wh_call( ctx, 2000, mbuf, 1, 10 );        // invalid wormhole id
186         errors += fail_if_nil( resp, "wormhole call given bad whid returned no msg" );
187         if( resp ) {
188                 errors += fail_if_equal( resp->state, RMR_OK, "wormhole call given bad whid returned good state" );
189         }   
190
191         whid = rmr_wh_open( ctx, "localhost:9219" );    // ensure one is open
192         resp = rmr_wh_call( ctx, whid, mbuf, 1, 1 );
193         errors += fail_if_nil( resp, "wormhole call given valid info returned nil response" );
194
195
196         // -----------------------------------------------------------------------
197         // WARNING:  these tests destroy the context, so they MUST be last
198         if( mbuf ) {                    // only if we got an mbuf
199                 errno = 0;
200                 mbuf->header = NULL;
201                 mbuf = rmr_wh_send_msg( ctx, 5, mbuf );         // coverage test on mbuf header check
202                 errors += fail_not_equal( errno, EBADMSG, "wh_send didn't set errno after bad mbuf send" );
203                 errors += fail_not_equal( mbuf->state, RMR_ERR_NOHDR, "send with bad header did now set msg state correctly" );
204
205                 errno = 0;
206                 wh_nuke( NULL );                                // coverage only
207                 wh_nuke( ctx );
208                 ctx->wormholes = NULL;
209                 mbuf = rmr_wh_send_msg( ctx, 4, mbuf );         // coverage test on mbuf header check
210                 errors += fail_not_equal( errno, EINVAL, "wh_send didn't set errno after send without wormole reference" );
211                 errors += fail_not_equal( mbuf->state, RMR_ERR_NOWHOPEN, "wh_send didn't set msg state after send without wormole reference" );
212
213                 rmr_free_msg( mbuf );
214         }
215
216         if( ctx ) {
217                 free( ctx->my_name );
218                 free( ctx );
219         }
220
221         return errors;
222 }