1 // : vi ts=4 sw=4 noet :
3 ==================================================================================
4 Copyright (c) 2019 Nokia
5 Copyright (c) 2018-2019 AT&T Intellectual Property.
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
11 http://www.apache.org/licenses/LICENSE-2.0
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 ==================================================================================
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.
26 Author: E. Scott Daniels
38 #include <semaphore.h>
41 #include "rmr_agnostic.h"
44 int mbuf_api_test( ) {
51 unsigned char src_buf[256];
53 mbuf = (rmr_mbuf_t *) malloc( sizeof( *mbuf ) );
55 fprintf( stderr, "[FAIL] tester cannot allocate memory: mbuf\n" );
59 mbuf->payload = (void *) malloc( sizeof( char ) * 1024 ); // add a dummy payload
60 mbuf->tp_buf = mbuf->payload;
61 mbuf->header = mbuf->payload;
62 mbuf->alloc_len = 1024;
64 // --- test payload field access functions ---------------------------------------------------
65 memset( src_buf, 0, sizeof( src_buf ) );
66 rmr_bytes2payload( mbuf, NULL, strlen( src_buf) ); // errno should be set on return
67 errors += fail_if( errno == 0, "buf copy to payload with nil src returned good errno" );
69 rmr_bytes2payload( NULL, src_buf, strlen( src_buf) ); // errno should be set on return
70 errors += fail_if( errno == 0, "buf copy to payload with nil mbuf returned good errno" );
72 mbuf->state = 1; // force it to something to test that it was set
73 rmr_bytes2payload( mbuf, src_buf, strlen( src_buf) );
74 errors += fail_if( mbuf->state != RMR_OK, "buf copy to payload returned bad state in mbuf" );
76 rmr_bytes2payload( mbuf, src_buf, 8192 ); // bust the limit
77 errors += fail_if( mbuf->state == RMR_OK, "huge buf copy to payload returned good state in mbuf" );
78 errors += fail_if( errno == 0, "huge buf copy to payload returned good state in errno" );
81 snprintf( src_buf, sizeof( src_buf ), "This is some text in the buffer" );
82 rmr_str2payload( mbuf, src_buf ); // this uses bytes2payload, so only one invocation needed
85 // --- test meid field access functions ---------------------------------------------------
87 i = rmr_bytes2meid( NULL, src_buf, RMR_MAX_MEID );
88 errors += fail_if( errno == 0, "(errno) attempt to copy bytes to meid with nil message" );
89 errors += fail_if( i > 0, "(rv) attempt to copy bytes to meid with nil message" );
92 i = rmr_bytes2meid( mbuf, NULL, RMR_MAX_MEID );
93 errors += fail_if( errno == 0, "(errno) attempt to copy bytes to meid with nil source buffer" );
94 errors += fail_if( i > 0, "(rv) attempt to copy bytes to meid with nil message" );
97 i = rmr_bytes2meid( mbuf, src_buf, RMR_MAX_MEID + 1 );
98 errors += fail_if( errno == 0, "(errno) attempt to copy bytes to meid with large source buffer" );
99 errors += fail_if( i != RMR_MAX_MEID, "(rv) attempt to copy bytes to meid with large source buffer" );
102 i = rmr_bytes2meid( mbuf, src_buf, RMR_MAX_MEID );
103 errors += fail_if( errno != 0, "copy bytes to meid; expected errno to be ok" );
104 errors += fail_if( i != RMR_MAX_MEID, "copy bytes to meid; expected return value to be max meid len" );
109 snprintf( src_buf, sizeof( src_buf ), "meid-fits" );
110 i = rmr_str2meid( NULL, src_buf );
111 errors += fail_if( errno == 0, "(errno) attempt to copy string to meid with nil message" );
112 errors += fail_if( i == RMR_OK, "(rv) attempt to copy string to meid with nil message" );
115 i = rmr_str2meid( mbuf, NULL );
116 errors += fail_if( errno == 0, "(errno) attempt to copy string to meid with nil source buffer" );
117 errors += fail_if( i == RMR_OK, "(rv) attempt to copy string to meid with nil message" );
120 i = rmr_str2meid( mbuf, src_buf );
121 errors += fail_if( errno != 0, "copy string to meid; expected errno to be ok" );
122 errors += fail_if( i != RMR_OK, "copy string to meid; expected return value to be RMR_OK" );
125 snprintf( src_buf, sizeof( src_buf ), "meid-should-be-too-large-to-fit-in-the-meid" );
126 i = rmr_str2meid( mbuf, src_buf );
127 errors += fail_if( errno == 0, "(errno) attempt to copy string to meid with large source buffer" );
128 errors += fail_if( i == RMR_OK, "(rv) attempt to copy string to meid with large source buffer" );
131 snprintf( src_buf, sizeof( src_buf ), "test-meid" );
132 rmr_str2meid( mbuf, src_buf );
135 c = rmr_get_meid( NULL, NULL );
136 errors += fail_if( c != NULL, "get meid with nil message buffer" );
137 errors += fail_if( errno == 0, "(errno bad) get meid with nil msg buffer" );
139 c = rmr_get_meid( mbuf, NULL ); // should allocate and return c
140 errors += fail_if( c == NULL, "get meid with nil dest pointer (did not allocate a buffer)" );
141 errors += fail_if( strcmp( c, "test-meid" ) != 0, "did not get expected meid from mbuffer" );
143 c = rmr_get_meid( mbuf, c );
144 errors += fail_if( c == NULL, "get meid with a dest pointer returned no pointer" );
145 errors += fail_if( strcmp( c, "test-meid" ) != 0, "did not get expected meid from mbuffer" );
148 // --- test transaction field access functions ---------------------------------------------------
150 i = rmr_bytes2xact( NULL, src_buf, RMR_MAX_XID );
151 errors += fail_if( errno == 0, "(errno) attempt to copy bytes to xact with nil message" );
152 errors += fail_if( i > 0, "(rv) attempt to copy bytes to xact with nil message" );
155 i = rmr_bytes2xact( mbuf, NULL, RMR_MAX_XID );
156 errors += fail_if( errno == 0, "(errno) attempt to copy bytes to xact with nil source buffer" );
157 errors += fail_if( i > 0, "(rv) attempt to copy bytes to xact with nil message" );
160 i = rmr_bytes2xact( mbuf, src_buf, RMR_MAX_XID + 1 );
161 errors += fail_if( errno == 0, "(errno) attempt to copy bytes to xact with large source buffer" );
162 errors += fail_if( i != RMR_MAX_XID, "(rv) attempt to copy bytes to xact with large source buffer" );
165 i = rmr_bytes2xact( mbuf, src_buf, RMR_MAX_XID );
166 errors += fail_if( errno != 0, "copy bytes to xact; expected errno to be ok" );
167 errors += fail_if( i != RMR_MAX_XID, "copy bytes to xact; expected return value to be max xact len" );
172 snprintf( src_buf, sizeof( src_buf ), "xact-fits" );
173 i = rmr_str2xact( NULL, src_buf );
174 errors += fail_if( errno == 0, "(errno) attempt to copy string to xact with nil message" );
175 errors += fail_if( i == RMR_OK, "(rv) attempt to copy string to xact with nil message" );
178 i = rmr_str2xact( mbuf, NULL );
179 errors += fail_if( errno == 0, "(errno) attempt to copy string to xact with nil source buffer" );
180 errors += fail_if( i == RMR_OK, "(rv) attempt to copy string to xact with nil message" );
183 i = rmr_str2xact( mbuf, src_buf );
184 errors += fail_if( errno != 0, "copy string to xact; expected errno to be ok" );
185 errors += fail_if( i != RMR_OK, "copy string to xact; expected return value to be RMR_OK" );
188 snprintf( src_buf, sizeof( src_buf ), "xact-should-be-too-large-to-fit-in-the-xact" );
189 i = rmr_str2xact( mbuf, src_buf );
190 errors += fail_if( errno == 0, "(errno) attempt to copy string to xact with large source buffer" );
191 errors += fail_if( i == RMR_OK, "(rv) attempt to copy string to xact with large source buffer" );
195 // ------------ trace data tests ----------------------------------------------------------------
196 // CAUTION: to support standalone mbuf api tests, the underlying buffer reallocation functions are NOT used
197 // if this is driven by the mbuf_api_test.c driver
199 mbuf = test_mk_msg( 2048, 0 ); // initially no trace size to force realloc
201 state = TRACE_OFFSET( mbuf->header ) - PAYLOAD_OFFSET( mbuf->header ); // no trace data, payload and trace offset should be the same
202 errors += fail_not_equal( state, 0, "trace offset and payload offset do NOT match when trace data is absent" );
204 state = rmr_get_trlen( mbuf );
205 errors += fail_not_equal( state, 0, "initial trace len reported (a) does not match expected (b)" );
207 state = rmr_set_trace( NULL, src_buf, 100 ); // coverage test on nil check
208 errors += fail_not_equal( state, 0, "set trace with nil msg didn't return expected 0 status" );
210 state = rmr_set_trace( mbuf, src_buf, 0 ); // coverage test on length check
211 errors += fail_not_equal( state, 0, "set trace with 0 len didn't return expected 0 status" );
213 state = rmr_get_trace( NULL, src_buf, 100 ); // coverage test on nil check
214 errors += fail_not_equal( state, 0, "get trace with nil msg didn't return expected 0 status" );
216 state = rmr_get_trace( mbuf, NULL, 100 ); // coverage test on nil check
217 errors += fail_not_equal( state, 0, "get trace with nil dest didn't return expected 0 status" );
219 state = rmr_get_trlen( NULL ); // coverage test on nil check
220 errors += fail_not_equal( state, 0, "get trace length with nil msg didn't return expected 0 status" );
224 state = rmr_set_trace( mbuf, "foo bar was here", 17 ); // should force a realloc
225 errors += fail_not_equal( state, 17, "bytes copied to trace (a) did not match expected size (b)" );
227 state = rmr_get_trace( mbuf, src_buf, 17 );
228 errors += fail_not_equal( state, 17, "bytes retrieved from trace (a) did not match expected size (b)" );
230 state = rmr_get_trlen( mbuf );
231 errors += fail_not_equal( state, 17, "trace len reported (a) does not match expected (b)" );
232 state = strcmp( src_buf, "foo bar was here" );
233 errors+= fail_not_equal( state, 0, "compare of pulled trace info did not match" );
235 state = TRACE_OFFSET( mbuf->header ) - PAYLOAD_OFFSET( mbuf->header ); // when there is a trace area these should NOT be the same
236 errors += fail_if_equal( state, 0, "trace offset and payload offset match when trace data is present" );
239 // second round of trace testing, allocating a message with a trace size that matches
240 mbuf = test_mk_msg( 2048, 17 ); // trace size that matches what we'll stuff in, no realloc
241 state = rmr_get_trlen( mbuf );
242 errors += fail_not_equal( state, 17, "alloc with trace size: initial trace len reported (a) does not match expected (b)" );
245 state = rmr_set_trace( mbuf, "foo bar was here", 17 ); // should force a realloc
246 errors += fail_not_equal( state, 17, "bytes copied to trace (a) did not match expected size (b)" );
248 state = rmr_get_trace( mbuf, src_buf, 17 );
249 errors += fail_not_equal( state, 17, "bytes retrieved from trace (a) did not match expected size (b)" );
250 state = strcmp( src_buf, "foo bar was here" );
251 errors+= fail_not_equal( state, 0, "compare of pulled trace info did not match" );
253 i = rmr_get_trlen( mbuf );
256 // ------------- source field tests ------------------------------------------------------------
257 // we cannot force anything into the message source field, so no way to test the content, but we
258 // can test pointers and expected nils
260 buf = rmr_get_src( NULL, src_buf ); // coverage test for nil msg check
261 errors += fail_not_nil( buf, "rmr_get_src returned a pointer when given a nil message" );
263 buf = rmr_get_src( mbuf, NULL ); // coverage test for nil dst check
264 errors += fail_not_nil( buf, "rmr_get_src returned a pointer when given a nil dest buffer" );
266 buf = rmr_get_src( mbuf, src_buf );
267 errors += fail_not_equalp( buf, src_buf, "rmr_get_src didn't return expexted buffer pointer" );
269 buf = rmr_get_srcip( NULL, NULL );
270 errors += fail_not_nil( buf, "get_srcip did not return nil when given nil pointers" );
272 buf = rmr_get_srcip( mbuf, NULL );
273 errors += fail_not_nil( buf, "get_srcip did not return nil when given nil destination" );
275 buf = rmr_get_srcip( mbuf, src_buf );
276 errors += fail_not_equalp( buf, src_buf, "rmr_get_srcip didn't return expexted buffer pointer" );
278 test_set_ver( mbuf, 2 ); // set older message version to ensure properly handled
279 buf = rmr_get_srcip( mbuf, src_buf );
281 return errors > 0; // overall exit code bad if errors