enhance(API): Add multi-threaded call
[ric-plt/lib/rmr.git] / test / wormhole_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:       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     i;
53         void* p;
54
55         rmr_mbuf_t*     mbuf;           // mbuf to send to peer
56         int             whid = -1;
57         int             last_whid;
58
59         ctx = (uta_ctx_t *) malloc( sizeof( uta_ctx_t ) );
60         if( ctx == NULL ) {
61                 fail_if_nil( ctx, "could not allocate dummy context" );
62                 return 1;
63         }
64         memset( ctx, 0, sizeof( *ctx ) );
65         ctx->my_name = strdup( "tester" );
66
67         gen_rt( ctx );
68
69         whid = rmr_wh_open( NULL, NULL );
70         errors += fail_not_equal( whid, -1, "call to wh_open with invalid values did not return bad whid" );
71
72
73         whid = rmr_wh_open( ctx, NULL );
74         errors += fail_not_equal( whid, -1, "call to wh_open with invalid target did not return bad whid" );
75
76         whid = rmr_wh_open( ctx, "" );
77         errors += fail_not_equal( whid, -1, "call to wh_open with empty target did not return bad whid" );
78
79         whid = rmr_wh_open( ctx, "localhost:89219" );
80         errors += fail_if_equal( whid, -1, "call to wh_open with valid target failed" );
81
82         rmr_wh_close( ctx, 4 );                                 // test for coverage only; [5] should have nil pointer
83         rmr_wh_close( ctx, 50 );                                        // test for coverage only; more than allocated reference
84
85         last_whid = whid;
86         whid = rmr_wh_open( ctx, "localhost:89219" );
87         errors += fail_not_equal( whid, last_whid, "call to wh_open with duplicate target did not return the same whid" );
88
89         for( i = 0; i < 20; i++ ) {                                                                                             // test ability to extend the table
90                 snprintf( wbuf, sizeof( wbuf ), "localhost:864%02d", i );                       // new address for each so whid is different
91                 whid = rmr_wh_open( ctx, wbuf );
92                 snprintf( wbuf, sizeof( wbuf ), "call to wh_open failed for iteration = %d", i );
93                 errors += fail_if_equal( whid, -1, wbuf );
94                 if( i ) {
95                         snprintf( wbuf, sizeof( wbuf ), "call to wh_open for iteration = %d returned same whid: %d", i, whid );
96                         errors += fail_if_equal( whid, last_whid, wbuf );
97                 }
98
99                 last_whid = whid;
100         }
101
102         rmr_wh_close( ctx, 3 );         // close one, then open a new one to verify that hole is found
103         whid = rmr_wh_open( ctx, "localhost:21961" );
104         errors += fail_not_equal( whid, 3, "attempt to fill in a hole didn't return expected" );
105
106         p = rmr_wh_send_msg( NULL, 0, NULL );                   // tests for coverage
107         fail_not_nil( p, "wh_send_msg returned a pointer when given nil context and message" );
108
109         p = rmr_wh_send_msg( ctx, 0, NULL );
110         fail_not_nil( p, "wh_send_msg returned a pointer when given nil message with valid context" );
111
112         mbuf = rmr_alloc_msg( ctx, 2048 );                      // get an muf to pass round
113         errors += fail_if_nil( mbuf, "unable to allocate mbuf for send tests (giving up on send tests)" );
114
115         mbuf->state = 0;
116         mbuf = rmr_wh_send_msg( NULL, 0, mbuf );
117         if( mbuf ) {
118                 fail_if_equal( mbuf->state, 0, "wh_send_msg returned a zero state when given a nil context" );
119         }
120         fail_if_nil( mbuf, "wh_send_msg returned a nil message buffer when given a nil context"  );
121
122         while( mbuf ) {
123                 if( !(mbuf = rmr_wh_send_msg( ctx, 50, mbuf )) ) {              // test for coverage
124                         errors += fail_if_nil( mbuf, "send didn't return an mbuf (skip rest of send tests)" );
125                         break;
126                 }
127
128                 mbuf = rmr_wh_send_msg( ctx, 4, mbuf );
129                 errors += fail_not_equal( mbuf->state, RMR_OK, "valid wormhole send failed" );
130                 errors += fail_not_equal( errno, 0, "errno after valid wormhole send was not 0" );
131
132                 rmr_wh_close( ctx, 4 );
133                 mbuf = rmr_wh_send_msg( ctx, 4, mbuf );
134                 rmr_wh_send_msg( ctx, 4, mbuf );
135                 errors += fail_not_equal( mbuf->state, RMR_ERR_WHID, "send on closed wormhole didn't set correct state in msg" );
136
137                 break;
138         }
139
140
141         // WARNING:  these tests destroy the context, so they MUST be last
142         if( mbuf ) {                    // only if we got an mbuf
143                 errno = 0;
144                 mbuf->header = NULL;
145                 mbuf = rmr_wh_send_msg( ctx, 5, mbuf );         // coverage test on mbuf header check
146                 errors += fail_not_equal( errno, EBADMSG, "wh_send didn't set errno after bad mbuf send" );
147                 errors += fail_not_equal( mbuf->state, RMR_ERR_NOHDR, "send with bad header did now set msg state correctly" );
148
149                 errno = 0;
150                 wh_nuke( ctx );
151                 ctx->wormholes = NULL;
152                 mbuf = rmr_wh_send_msg( ctx, 4, mbuf );         // coverage test on mbuf header check
153                 errors += fail_not_equal( errno, EINVAL, "wh_send didn't set errno after send without wormole reference" );
154                 errors += fail_not_equal( mbuf->state, RMR_ERR_NOWHOPEN, "wh_send didn't set msg state after send without wormole reference" );
155
156                 rmr_free_msg( mbuf );
157         }
158
159         if( ctx ) {
160                 free( ctx->my_name );
161                 free( ctx );
162         }
163
164         return !!errors;                        // 1 or 0 regardless of count
165 }