Add transport provider status to message buffer
[ric-plt/lib/rmr.git] / test / mbuf_api_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:       mbuf_api_static_test.c
23         Abstract:       Test the message buffer  funcitons. These are meant to be included at compile
24                                 time by the test driver.
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 int mbuf_api_test( ) {
45         unsigned char* c;
46         int i;
47         int state;
48         int errors = 0;
49         char*   buf;
50         void*   ptr;
51         rmr_mbuf_t*     mbuf;
52         unsigned char src_buf[256];
53
54         mbuf = (rmr_mbuf_t *) malloc( sizeof( *mbuf ) );
55         if( mbuf == NULL ) {
56                 fprintf( stderr, "[FAIL] tester cannot allocate memory: mbuf\n" );
57                 exit( 1 );
58         }
59
60         mbuf->payload = (void *) malloc( sizeof( char ) * 1024 );               // add a dummy payload
61         mbuf->tp_buf = mbuf->payload;
62         mbuf->header = mbuf->payload;
63         mbuf->alloc_len = 1024;
64
65         // --- test payload field  access functions ---------------------------------------------------
66         memset( src_buf, 0, sizeof( src_buf ) );
67         rmr_bytes2payload( mbuf, NULL, strlen( src_buf) );                              // errno should be set on return
68         errors += fail_if( errno == 0, "buf copy to payload with nil src returned good errno" );
69
70         rmr_bytes2payload( NULL, src_buf, strlen( src_buf) );                   // errno should be set on return
71         errors += fail_if( errno == 0, "buf copy to payload with nil mbuf returned good errno" );
72
73         mbuf->state = 1;                                                                                        // force it to something to test that it was set
74         rmr_bytes2payload( mbuf, src_buf, strlen( src_buf) );
75         errors += fail_if( mbuf->state != RMR_OK, "buf copy to payload returned bad state in mbuf" );
76
77         rmr_bytes2payload( mbuf, src_buf, 8192 );                                               // bust the limit
78         errors += fail_if( mbuf->state == RMR_OK, "huge buf copy to payload returned good state in mbuf" );
79         errors += fail_if( errno == 0, "huge buf copy to payload returned good state in errno" );
80
81
82         snprintf( src_buf, sizeof( src_buf ), "This is some text in the buffer" );
83         rmr_str2payload( mbuf, src_buf );                                                       // this uses bytes2payload, so only one invocation needed
84
85
86         // --- test meid field  access functions ---------------------------------------------------
87         errno = 0;
88         i = rmr_bytes2meid( NULL, src_buf, RMR_MAX_MEID );
89         errors += fail_if( errno == 0, "(errno) attempt to copy bytes to meid with nil message" );
90         errors += fail_if( i > 0, "(rv) attempt to copy bytes to meid with nil message" );
91
92         errno = 0;
93         i = rmr_bytes2meid( mbuf, NULL, RMR_MAX_MEID );
94         errors += fail_if( errno == 0, "(errno) attempt to copy bytes to meid with nil source buffer" );
95         errors += fail_if( i > 0, "(rv) attempt to copy bytes to meid with nil message" );
96
97         errno = 0;
98         i = rmr_bytes2meid( mbuf, src_buf, RMR_MAX_MEID + 1 );
99         errors += fail_if( errno == 0, "(errno) attempt to copy bytes to meid with large source buffer" );
100         errors += fail_if( i != RMR_MAX_MEID, "(rv) attempt to copy bytes to meid with large source buffer" );
101
102         errno = 0;
103         i = rmr_bytes2meid( mbuf, src_buf, RMR_MAX_MEID  );
104         errors += fail_if( errno != 0, "copy bytes to meid; expected errno to be ok" );
105         errors += fail_if( i != RMR_MAX_MEID, "copy bytes to meid; expected return value to be max meid len" );
106
107
108
109         errno = 0;
110         snprintf( src_buf, sizeof( src_buf ), "meid-fits" );
111         i = rmr_str2meid( NULL, src_buf );
112         errors += fail_if( errno == 0, "(errno) attempt to copy string to meid with nil message" );
113         errors += fail_if( i == RMR_OK, "(rv) attempt to copy string to meid with nil message" );
114
115         errno = 0;
116         i = rmr_str2meid( mbuf, NULL );
117         errors += fail_if( errno == 0, "(errno) attempt to copy string to meid with nil source buffer" );
118         errors += fail_if( i == RMR_OK, "(rv) attempt to copy string to meid with nil message" );
119
120         errno = 0;
121         i = rmr_str2meid( mbuf, src_buf );
122         errors += fail_if( errno != 0, "copy string to meid; expected errno to be ok" );
123         errors += fail_if( i != RMR_OK, "copy string to meid; expected return value to be RMR_OK" );
124
125         errno = 0;
126         snprintf( src_buf, sizeof( src_buf ), "meid-should-be-too-large-to-fit-in-the-meid" );
127         i = rmr_str2meid( mbuf, src_buf );
128         errors += fail_if( errno == 0, "(errno) attempt to copy string to meid with large source buffer" );
129         errors += fail_if( i == RMR_OK, "(rv) attempt to copy string to meid with large source buffer" );
130
131
132         snprintf( src_buf, sizeof( src_buf ), "test-meid" );
133         rmr_str2meid( mbuf, src_buf );
134
135         errno = 0;
136         c = rmr_get_meid( NULL, NULL );
137         errors += fail_if( c != NULL, "get meid with nil message buffer" );
138         errors += fail_if( errno == 0, "(errno bad) get meid with nil msg buffer" );
139
140         c = rmr_get_meid( mbuf, NULL );                 // should allocate and return c
141         errors += fail_if( c == NULL, "get meid with nil dest pointer (did not allocate a buffer)" );
142         errors += fail_if( strcmp( c, "test-meid" ) != 0, "did not get expected meid from mbuffer" );
143
144         c = rmr_get_meid( mbuf, c );
145         errors += fail_if( c == NULL, "get meid with a dest pointer returned no pointer" );
146         errors += fail_if( strcmp( c, "test-meid" ) != 0, "did not get expected meid from mbuffer" );
147
148
149         // --- test transaction field  access functions ---------------------------------------------------
150         errno = 0;
151         i = rmr_bytes2xact( NULL, src_buf, RMR_MAX_XID );
152         errors += fail_if( errno == 0, "(errno) attempt to copy bytes to xact with nil message" );
153         errors += fail_if( i > 0, "(rv) attempt to copy bytes to xact with nil message" );
154
155         errno = 0;
156         i = rmr_bytes2xact( mbuf, NULL, RMR_MAX_XID );
157         errors += fail_if( errno == 0, "(errno) attempt to copy bytes to xact with nil source buffer" );
158         errors += fail_if( i > 0, "(rv) attempt to copy bytes to xact with nil message" );
159
160         errno = 0;
161         i = rmr_bytes2xact( mbuf, src_buf, RMR_MAX_XID + 1 );
162         errors += fail_if( errno == 0, "(errno) attempt to copy bytes to xact with large source buffer" );
163         errors += fail_if( i != RMR_MAX_XID, "(rv) attempt to copy bytes to xact with large source buffer" );
164
165         errno = 0;
166         i = rmr_bytes2xact( mbuf, src_buf, RMR_MAX_XID  );
167         errors += fail_if( errno != 0, "copy bytes to xact; expected errno to be ok" );
168         errors += fail_if( i != RMR_MAX_XID, "copy bytes to xact; expected return value to be max xact len" );
169
170
171
172         errno = 0;
173         snprintf( src_buf, sizeof( src_buf ), "xact-fits" );
174         i = rmr_str2xact( NULL, src_buf );
175         errors += fail_if( errno == 0, "(errno) attempt to copy string to xact with nil message" );
176         errors += fail_if( i == RMR_OK, "(rv) attempt to copy string to xact with nil message" );
177
178         errno = 0;
179         i = rmr_str2xact( mbuf, NULL );
180         errors += fail_if( errno == 0, "(errno) attempt to copy string to xact with nil source buffer" );
181         errors += fail_if( i == RMR_OK, "(rv) attempt to copy string to xact with nil message" );
182
183         errno = 0;
184         i = rmr_str2xact( mbuf, src_buf );
185         errors += fail_if( errno != 0, "copy string to xact; expected errno to be ok" );
186         errors += fail_if( i != RMR_OK, "copy string to xact; expected return value to be RMR_OK" );
187
188         errno = 0;
189         snprintf( src_buf, sizeof( src_buf ), "xact-should-be-too-large-to-fit-in-the-xact" );
190         i = rmr_str2xact( mbuf, src_buf );
191         errors += fail_if( errno == 0, "(errno) attempt to copy string to xact with large source buffer" );
192         errors += fail_if( i == RMR_OK, "(rv) attempt to copy string to xact with large source buffer" );
193
194
195
196         // ------------ trace data tests ----------------------------------------------------------------
197         // CAUTION: to support standalone mbuf api tests, the underlying buffer reallocation functions are NOT used
198         //                      if this is driven by the mbuf_api_test.c driver
199
200         mbuf = test_mk_msg( 2048, 0 );          // initially no trace size to force realloc
201
202         state = TRACE_OFFSET( mbuf->header ) - PAYLOAD_OFFSET( mbuf->header );          // no trace data, payload and trace offset should be the same
203         errors += fail_not_equal( state, 0, "trace offset and payload offset do NOT match when trace data is absent" );
204
205         state = rmr_get_trlen( mbuf );
206         errors += fail_not_equal( state, 0, "initial trace len reported (a) does not match expected (b)" );
207
208         state = rmr_set_trace( NULL, src_buf, 100 );                            // coverage test on nil check
209         errors += fail_not_equal( state, 0, "set trace with nil msg didn't return expected 0 status" );
210
211         state = rmr_set_trace( mbuf, src_buf, 0 );                              // coverage test on length check
212         errors += fail_not_equal( state, 0, "set trace with 0 len didn't return expected 0 status" );
213
214         state = rmr_get_trace( NULL, src_buf, 100 );                            // coverage test on nil check
215         errors += fail_not_equal( state, 0, "get trace with nil msg didn't return expected 0 status" );
216
217         state = rmr_get_trace( mbuf, NULL, 100 );                                       // coverage test on nil check
218         errors += fail_not_equal( state, 0, "get trace with nil dest didn't return expected 0 status" );
219
220         state = rmr_get_trlen( NULL );                                                          // coverage test on nil check
221         errors += fail_not_equal( state, 0, "get trace length with nil msg didn't return expected 0 status" );
222
223         ptr = rmr_trace_ref( NULL, NULL );
224         errors += fail_not_nil( ptr, "trace ref returned non-nil pointer when given nil message" );
225
226         mbuf = test_mk_msg( 2048, 0 );          // initially no trace size to force realloc
227         ptr = rmr_trace_ref( mbuf, NULL );
228         errors += fail_not_nil( ptr, "trace ref returned non-nil pointer when given a message without trace data" );
229
230         i = 100;                                                                // ensure that i is reset when there is no trace data
231         ptr = rmr_trace_ref( mbuf, &i );
232         errors += fail_not_nil( ptr, "trace ref returned non-nil pointer when given a message without trace data" );
233         errors += fail_not_equal( i, 0, "trace ref returned non zero size (a) when no trace info in message" );
234
235         i = 100;
236         mbuf = test_mk_msg( 2048, 113 );                // alloc with a trace data area
237         ptr = rmr_trace_ref( mbuf, &i );
238         errors += fail_if_nil( ptr, "trace ref returned nil pointer when given a message with trace data" );
239         errors += fail_not_equal( i, 113, "trace ref returned bad size (a), expected (b)" );
240
241
242         src_buf[0] = 0;
243         state = rmr_set_trace( mbuf, "foo bar was here", 17 );          // should force a realloc
244         errors += fail_not_equal( state, 17, "bytes copied to trace (a) did not match expected size (b)" );
245
246         state = rmr_get_trace( mbuf, src_buf, 17 );
247         errors += fail_not_equal( state, 17, "bytes retrieved from trace (a) did not match expected size (b)" );
248
249         state = rmr_get_trlen( mbuf );
250         errors += fail_not_equal( state, 17, "trace len reported (a) does not match expected (b)" );
251         state = strcmp( src_buf, "foo bar was here" );
252         errors+= fail_not_equal( state, 0, "compare of pulled trace info did not match" );
253
254         state = TRACE_OFFSET( mbuf->header ) - PAYLOAD_OFFSET( mbuf->header );          // when there is a trace area these should NOT be the same
255         errors += fail_if_equal( state, 0, "trace offset and payload offset match when trace data is present" );
256
257
258                                                                                         // second round of trace testing, allocating a message with a trace size that matches
259         mbuf = test_mk_msg( 2048, 17 );                 // trace size that matches what we'll stuff in, no realloc
260         state = rmr_get_trlen( mbuf );
261         errors += fail_not_equal( state, 17, "alloc with trace size: initial trace len reported (a) does not match expected (b)" );
262
263         src_buf[0] = 0;
264         state = rmr_set_trace( mbuf, "foo bar was here", 17 );          // should force a realloc
265         errors += fail_not_equal( state, 17, "bytes copied to trace (a) did not match expected size (b)" );
266
267         state = rmr_get_trace( mbuf, src_buf, 17 );
268         errors += fail_not_equal( state, 17, "bytes retrieved from trace (a) did not match expected size (b)" );
269         state = strcmp( src_buf, "foo bar was here" );
270         errors+= fail_not_equal( state, 0, "compare of pulled trace info did not match" );
271
272         i = rmr_get_trlen( mbuf );
273
274
275         // ------------- source field tests ------------------------------------------------------------
276         // we cannot force anything into the message source field, so no way to test the content, but we
277         // can test pointers and expected nils
278
279         buf = rmr_get_src( NULL, src_buf );                                     // coverage test for nil msg check
280         errors += fail_not_nil( buf, "rmr_get_src returned a pointer when given a nil message" );
281
282         buf = rmr_get_src( mbuf, NULL );                                        // coverage test for nil dst check
283         errors += fail_not_nil( buf, "rmr_get_src returned a pointer when given a nil dest buffer" );
284
285         buf = rmr_get_src( mbuf, src_buf );
286         errors += fail_not_equalp( buf, src_buf, "rmr_get_src didn't return expexted buffer pointer" );
287
288         buf = rmr_get_srcip( NULL, NULL );
289         errors += fail_not_nil( buf, "get_srcip did not return nil when given nil pointers" );
290
291         buf = rmr_get_srcip( mbuf, NULL );
292         errors += fail_not_nil( buf, "get_srcip did not return nil when given nil destination" );
293
294         buf = rmr_get_srcip( mbuf, src_buf );
295         errors += fail_not_equalp( buf, src_buf, "rmr_get_srcip didn't return expexted buffer pointer" );
296
297         test_set_ver( mbuf, 2 );                                                        // set older message version to ensure properly handled
298         buf = rmr_get_srcip( mbuf, src_buf );
299
300         return errors > 0;                      // overall exit code bad if errors
301 }