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