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