feat(API): Add trace data functions
[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
38 #include "../src/common/include/rmr.h"
39 #include "../src/common/include/rmr_agnostic.h"
40
41
42 int mbuf_api_test( ) {
43         unsigned char* c;
44         int i;
45         int state;
46         int errors = 0;
47         rmr_mbuf_t*     mbuf;
48         unsigned char src_buf[256];
49
50         mbuf = (rmr_mbuf_t *) malloc( sizeof( *mbuf ) );
51         if( mbuf == NULL ) {
52                 fprintf( stderr, "[FAIL] tester cannot allocate memory: mbuf\n" );
53                 exit( 1 );
54         }
55
56         mbuf->payload = (void *) malloc( sizeof( char ) * 1024 );               // add a dummy payload
57         mbuf->tp_buf = mbuf->payload;
58         mbuf->header = mbuf->payload;
59         mbuf->alloc_len = 1024;
60
61         // --- test payload field  access functions ---------------------------------------------------
62         memset( src_buf, 0, sizeof( src_buf ) );
63         rmr_bytes2payload( mbuf, NULL, strlen( src_buf) );                              // errno should be set on return
64         errors += fail_if( errno == 0, "buf copy to payload with nil src returned good errno" );
65
66         rmr_bytes2payload( NULL, src_buf, strlen( src_buf) );                   // errno should be set on return
67         errors += fail_if( errno == 0, "buf copy to payload with nil mbuf returned good errno" );
68
69         mbuf->state = 1;                                                                                        // force it to something to test that it was set
70         rmr_bytes2payload( mbuf, src_buf, strlen( src_buf) );
71         errors += fail_if( mbuf->state != RMR_OK, "buf copy to payload returned bad state in mbuf" );
72
73         rmr_bytes2payload( mbuf, src_buf, 8192 );                                               // bust the limit
74         errors += fail_if( mbuf->state == RMR_OK, "huge buf copy to payload returned good state in mbuf" );
75         errors += fail_if( errno == 0, "huge buf copy to payload returned good state in errno" );
76         
77
78         snprintf( src_buf, sizeof( src_buf ), "This is some text in the buffer" );
79         rmr_str2payload( mbuf, src_buf );                                                       // this uses bytes2payload, so only one invocation needed
80
81
82         // --- test meid field  access functions ---------------------------------------------------
83         errno = 0;
84         i = rmr_bytes2meid( NULL, src_buf, RMR_MAX_MEID );
85         errors += fail_if( errno == 0, "(errno) attempt to copy bytes to meid with nil message" );
86         errors += fail_if( i > 0, "(rv) attempt to copy bytes to meid with nil message" );
87
88         errno = 0;
89         i = rmr_bytes2meid( mbuf, NULL, RMR_MAX_MEID );
90         errors += fail_if( errno == 0, "(errno) attempt to copy bytes to meid with nil source buffer" );
91         errors += fail_if( i > 0, "(rv) attempt to copy bytes to meid with nil message" );
92
93         errno = 0;
94         i = rmr_bytes2meid( mbuf, src_buf, RMR_MAX_MEID + 1 );
95         errors += fail_if( errno == 0, "(errno) attempt to copy bytes to meid with large source buffer" );
96         errors += fail_if( i != RMR_MAX_MEID, "(rv) attempt to copy bytes to meid with large source buffer" );
97
98         errno = 0;
99         i = rmr_bytes2meid( mbuf, src_buf, RMR_MAX_MEID  );
100         errors += fail_if( errno != 0, "copy bytes to meid; expected errno to be ok" );
101         errors += fail_if( i != RMR_MAX_MEID, "copy bytes to meid; expected return value to be max meid len" );
102
103
104
105         errno = 0;
106         snprintf( src_buf, sizeof( src_buf ), "meid-fits" );
107         i = rmr_str2meid( NULL, src_buf );
108         errors += fail_if( errno == 0, "(errno) attempt to copy string to meid with nil message" );
109         errors += fail_if( i == RMR_OK, "(rv) attempt to copy string to meid with nil message" );
110
111         errno = 0;
112         i = rmr_str2meid( mbuf, NULL );
113         errors += fail_if( errno == 0, "(errno) attempt to copy string to meid with nil source buffer" );
114         errors += fail_if( i == RMR_OK, "(rv) attempt to copy string to meid with nil message" );
115
116         errno = 0;
117         i = rmr_str2meid( mbuf, src_buf );
118         errors += fail_if( errno != 0, "copy string to meid; expected errno to be ok" );
119         errors += fail_if( i != RMR_OK, "copy string to meid; expected return value to be RMR_OK" );
120
121         errno = 0;
122         snprintf( src_buf, sizeof( src_buf ), "meid-should-be-too-large-to-fit-in-the-meid" );
123         i = rmr_str2meid( mbuf, src_buf );
124         errors += fail_if( errno == 0, "(errno) attempt to copy string to meid with large source buffer" );
125         errors += fail_if( i == RMR_OK, "(rv) attempt to copy string to meid with large source buffer" );
126
127
128         snprintf( src_buf, sizeof( src_buf ), "test-meid" );
129         rmr_str2meid( mbuf, src_buf );
130
131         errno = 0;
132         c = rmr_get_meid( NULL, NULL );
133         errors += fail_if( c != NULL, "get meid with nil message buffer" );
134         errors += fail_if( errno == 0, "(errno bad) get meid with nil msg buffer" ); 
135         
136         c = rmr_get_meid( mbuf, NULL );                 // should allocate and return c
137         errors += fail_if( c == NULL, "get meid with nil dest pointer (did not allocate a buffer)" );
138         errors += fail_if( strcmp( c, "test-meid" ) != 0, "did not get expected meid from mbuffer" );
139
140         c = rmr_get_meid( mbuf, c );
141         errors += fail_if( c == NULL, "get meid with a dest pointer returned no pointer" );
142         errors += fail_if( strcmp( c, "test-meid" ) != 0, "did not get expected meid from mbuffer" );
143
144
145         // --- test transaction field  access functions ---------------------------------------------------
146         errno = 0;
147         i = rmr_bytes2xact( NULL, src_buf, RMR_MAX_XID );
148         errors += fail_if( errno == 0, "(errno) attempt to copy bytes to xact with nil message" );
149         errors += fail_if( i > 0, "(rv) attempt to copy bytes to xact with nil message" );
150
151         errno = 0;
152         i = rmr_bytes2xact( mbuf, NULL, RMR_MAX_XID );
153         errors += fail_if( errno == 0, "(errno) attempt to copy bytes to xact with nil source buffer" );
154         errors += fail_if( i > 0, "(rv) attempt to copy bytes to xact with nil message" );
155
156         errno = 0;
157         i = rmr_bytes2xact( mbuf, src_buf, RMR_MAX_XID + 1 );
158         errors += fail_if( errno == 0, "(errno) attempt to copy bytes to xact with large source buffer" );
159         errors += fail_if( i != RMR_MAX_XID, "(rv) attempt to copy bytes to xact with large source buffer" );
160
161         errno = 0;
162         i = rmr_bytes2xact( mbuf, src_buf, RMR_MAX_XID  );
163         errors += fail_if( errno != 0, "copy bytes to xact; expected errno to be ok" );
164         errors += fail_if( i != RMR_MAX_XID, "copy bytes to xact; expected return value to be max xact len" );
165
166
167
168         errno = 0;
169         snprintf( src_buf, sizeof( src_buf ), "xact-fits" );
170         i = rmr_str2xact( NULL, src_buf );
171         errors += fail_if( errno == 0, "(errno) attempt to copy string to xact with nil message" );
172         errors += fail_if( i == RMR_OK, "(rv) attempt to copy string to xact with nil message" );
173
174         errno = 0;
175         i = rmr_str2xact( mbuf, NULL );
176         errors += fail_if( errno == 0, "(errno) attempt to copy string to xact with nil source buffer" );
177         errors += fail_if( i == RMR_OK, "(rv) attempt to copy string to xact with nil message" );
178
179         errno = 0;
180         i = rmr_str2xact( mbuf, src_buf );
181         errors += fail_if( errno != 0, "copy string to xact; expected errno to be ok" );
182         errors += fail_if( i != RMR_OK, "copy string to xact; expected return value to be RMR_OK" );
183
184         errno = 0;
185         snprintf( src_buf, sizeof( src_buf ), "xact-should-be-too-large-to-fit-in-the-xact" );
186         i = rmr_str2xact( mbuf, src_buf );
187         errors += fail_if( errno == 0, "(errno) attempt to copy string to xact with large source buffer" );
188         errors += fail_if( i == RMR_OK, "(rv) attempt to copy string to xact with large source buffer" );
189
190
191
192         // ------------ trace data tests ----------------------------------------------------------------
193         // CAUTION: to support standalone mbuf api tests, the underlying buffer reallocation functions are NOT used
194         //                      if this is driven by the mbuf_api_test.c driver
195
196         mbuf = test_mk_msg( 2048, 0 );          // initially no trace size to force realloc
197
198         state = TRACE_OFFSET( mbuf->header ) - PAYLOAD_OFFSET( mbuf->header );          // no trace data, payload and trace offset should be the same
199         errors += fail_not_equal( state, 0, "trace offset and payload offset do NOT match when trace data is absent" );
200
201         state = rmr_get_trlen( mbuf );
202         errors += fail_not_equal( state, 0, "initial trace len reported (a) does not match expected (b)" );
203
204         state = rmr_set_trace( NULL, src_buf, 100 );                            // coverage test on nil check
205         errors += fail_not_equal( state, 0, "set trace with nil msg didn't return expected 0 status" );
206
207         state = rmr_set_trace( mbuf, src_buf, 0 );                              // coverage test on length check
208         errors += fail_not_equal( state, 0, "set trace with 0 len didn't return expected 0 status" );
209
210         state = rmr_get_trace( NULL, src_buf, 100 );                            // coverage test on nil check
211         errors += fail_not_equal( state, 0, "get trace with nil msg didn't return expected 0 status" );
212
213         state = rmr_get_trace( mbuf, NULL, 100 );                                       // coverage test on nil check
214         errors += fail_not_equal( state, 0, "get trace with nil dest didn't return expected 0 status" );
215
216         state = rmr_get_trlen( NULL );                                                          // coverage test on nil check
217         errors += fail_not_equal( state, 0, "get trace length with nil msg didn't return expected 0 status" );
218
219         
220         src_buf[0] = 0;
221         state = rmr_set_trace( mbuf, "foo bar was here", 17 );          // should force a realloc
222         errors += fail_not_equal( state, 17, "bytes copied to trace (a) did not match expected size (b)" );
223
224         state = rmr_get_trace( mbuf, src_buf, 17 );
225         errors += fail_not_equal( state, 17, "bytes retrieved from trace (a) did not match expected size (b)" );
226
227         state = rmr_get_trlen( mbuf );
228         errors += fail_not_equal( state, 17, "trace len reported (a) does not match expected (b)" );
229         state = strcmp( src_buf, "foo bar was here" );
230         errors+= fail_not_equal( state, 0, "compare of pulled trace info did not match" );
231
232         state = TRACE_OFFSET( mbuf->header ) - PAYLOAD_OFFSET( mbuf->header );          // when there is a trace area these should NOT be the same
233         errors += fail_if_equal( state, 0, "trace offset and payload offset match when trace data is present" );
234
235
236                                                                                         // second round of trace testing, allocating a message with a trace size that matches
237         mbuf = test_mk_msg( 2048, 17 );                 // trace size that matches what we'll stuff in, no realloc
238         state = rmr_get_trlen( mbuf );
239         errors += fail_not_equal( state, 17, "alloc with trace size: initial trace len reported (a) does not match expected (b)" );
240
241         src_buf[0] = 0;
242         state = rmr_set_trace( mbuf, "foo bar was here", 17 );          // should force a realloc
243         errors += fail_not_equal( state, 17, "bytes copied to trace (a) did not match expected size (b)" );
244
245         state = rmr_get_trace( mbuf, src_buf, 17 );
246         errors += fail_not_equal( state, 17, "bytes retrieved from trace (a) did not match expected size (b)" );
247         state = strcmp( src_buf, "foo bar was here" );
248         errors+= fail_not_equal( state, 0, "compare of pulled trace info did not match" );
249
250         i = rmr_get_trlen( mbuf );
251         
252
253         return errors > 0;                      // overall exit code bad if errors
254 }