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