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 "../src/common/include/rmr.h"
39 #include "../src/common/include/rmr_agnostic.h"
42 int mbuf_api_test( ) {
47 unsigned char src_buf[256];
49 mbuf = (rmr_mbuf_t *) malloc( sizeof( *mbuf ) );
51 fprintf( stderr, "[FAIL] tester cannot allocate memory: mbuf\n" );
55 mbuf->payload = (void *) malloc( sizeof( char ) * 1024 ); // add a dummy payload
56 mbuf->tp_buf = mbuf->payload;
57 mbuf->header = mbuf->payload;
58 mbuf->alloc_len = 1024;
60 memset( src_buf, 0, sizeof( src_buf ) );
61 rmr_bytes2payload( mbuf, NULL, strlen( src_buf) ); // errno should be set on return
62 errors += fail_if( errno == 0, "buf copy to payload with nil src returned good errno" );
64 rmr_bytes2payload( NULL, src_buf, strlen( src_buf) ); // errno should be set on return
65 errors += fail_if( errno == 0, "buf copy to payload with nil mbuf returned good errno" );
67 mbuf->state = 1; // force it to something to test that it was set
68 rmr_bytes2payload( mbuf, src_buf, strlen( src_buf) );
69 errors += fail_if( mbuf->state != RMR_OK, "buf copy to payload returned bad state in mbuf" );
71 rmr_bytes2payload( mbuf, src_buf, 8192 ); // bust the limit
72 errors += fail_if( mbuf->state == RMR_OK, "huge buf copy to payload returned good state in mbuf" );
73 errors += fail_if( errno == 0, "huge buf copy to payload returned good state in errno" );
76 snprintf( src_buf, sizeof( src_buf ), "This is some text in the buffer" );
77 rmr_str2payload( mbuf, src_buf ); // this uses bytes2payload, so only one invocation needed
80 i = rmr_bytes2meid( NULL, src_buf, RMR_MAX_MEID );
81 errors += fail_if( errno == 0, "(errno) attempt to copy bytes to meid with nil message" );
82 errors += fail_if( i > 0, "(rv) attempt to copy bytes to meid with nil message" );
85 i = rmr_bytes2meid( mbuf, NULL, RMR_MAX_MEID );
86 errors += fail_if( errno == 0, "(errno) attempt to copy bytes to meid with nil source buffer" );
87 errors += fail_if( i > 0, "(rv) attempt to copy bytes to meid with nil message" );
90 i = rmr_bytes2meid( mbuf, src_buf, RMR_MAX_MEID + 1 );
91 errors += fail_if( errno == 0, "(errno) attempt to copy bytes to meid with large source buffer" );
92 errors += fail_if( i != RMR_MAX_MEID, "(rv) attempt to copy bytes to meid with large source buffer" );
95 i = rmr_bytes2meid( mbuf, src_buf, RMR_MAX_MEID );
96 errors += fail_if( errno != 0, "copy bytes to meid; expected errno to be ok" );
97 errors += fail_if( i != RMR_MAX_MEID, "copy bytes to meid; expected return value to be max meid len" );
102 snprintf( src_buf, sizeof( src_buf ), "meid-fits" );
103 i = rmr_str2meid( NULL, src_buf );
104 errors += fail_if( errno == 0, "(errno) attempt to copy string to meid with nil message" );
105 errors += fail_if( i == RMR_OK, "(rv) attempt to copy string to meid with nil message" );
108 i = rmr_str2meid( mbuf, NULL );
109 errors += fail_if( errno == 0, "(errno) attempt to copy string to meid with nil source buffer" );
110 errors += fail_if( i == RMR_OK, "(rv) attempt to copy string to meid with nil message" );
113 i = rmr_str2meid( mbuf, src_buf );
114 errors += fail_if( errno != 0, "copy string to meid; expected errno to be ok" );
115 errors += fail_if( i != RMR_OK, "copy string to meid; expected return value to be RMR_OK" );
118 snprintf( src_buf, sizeof( src_buf ), "meid-should-be-too-large-to-fit-in-the-meid" );
119 i = rmr_str2meid( mbuf, src_buf );
120 errors += fail_if( errno == 0, "(errno) attempt to copy string to meid with large source buffer" );
121 errors += fail_if( i == RMR_OK, "(rv) attempt to copy string to meid with large source buffer" );
125 i = rmr_bytes2xact( NULL, src_buf, RMR_MAX_XID );
126 errors += fail_if( errno == 0, "(errno) attempt to copy bytes to xact with nil message" );
127 errors += fail_if( i > 0, "(rv) attempt to copy bytes to xact with nil message" );
130 i = rmr_bytes2xact( mbuf, NULL, RMR_MAX_XID );
131 errors += fail_if( errno == 0, "(errno) attempt to copy bytes to xact with nil source buffer" );
132 errors += fail_if( i > 0, "(rv) attempt to copy bytes to xact with nil message" );
135 i = rmr_bytes2xact( mbuf, src_buf, RMR_MAX_XID + 1 );
136 errors += fail_if( errno == 0, "(errno) attempt to copy bytes to xact with large source buffer" );
137 errors += fail_if( i != RMR_MAX_XID, "(rv) attempt to copy bytes to xact with large source buffer" );
140 i = rmr_bytes2xact( mbuf, src_buf, RMR_MAX_XID );
141 errors += fail_if( errno != 0, "copy bytes to xact; expected errno to be ok" );
142 errors += fail_if( i != RMR_MAX_XID, "copy bytes to xact; expected return value to be max xact len" );
147 snprintf( src_buf, sizeof( src_buf ), "xact-fits" );
148 i = rmr_str2xact( NULL, src_buf );
149 errors += fail_if( errno == 0, "(errno) attempt to copy string to xact with nil message" );
150 errors += fail_if( i == RMR_OK, "(rv) attempt to copy string to xact with nil message" );
153 i = rmr_str2xact( mbuf, NULL );
154 errors += fail_if( errno == 0, "(errno) attempt to copy string to xact with nil source buffer" );
155 errors += fail_if( i == RMR_OK, "(rv) attempt to copy string to xact with nil message" );
158 i = rmr_str2xact( mbuf, src_buf );
159 errors += fail_if( errno != 0, "copy string to xact; expected errno to be ok" );
160 errors += fail_if( i != RMR_OK, "copy string to xact; expected return value to be RMR_OK" );
163 snprintf( src_buf, sizeof( src_buf ), "xact-should-be-too-large-to-fit-in-the-xact" );
164 i = rmr_str2xact( mbuf, src_buf );
165 errors += fail_if( errno == 0, "(errno) attempt to copy string to xact with large source buffer" );
166 errors += fail_if( i == RMR_OK, "(rv) attempt to copy string to xact with large source buffer" );
169 snprintf( src_buf, sizeof( src_buf ), "test-meid" );
170 rmr_str2meid( mbuf, src_buf );
173 c = rmr_get_meid( NULL, NULL );
174 errors += fail_if( c != NULL, "get meid with nil message buffer" );
175 errors += fail_if( errno == 0, "(errno bad) get meid with nil msg buffer" );
178 c = rmr_get_meid( mbuf, NULL ); // should allocate and return c
179 errors += fail_if( c == NULL, "get meid with nil dest pointer (did not allocate a buffer)" );
180 errors += fail_if( strcmp( c, "test-meid" ) != 0, "did not get expected meid from mbuffer" );
182 c = rmr_get_meid( mbuf, c );
183 errors += fail_if( c == NULL, "get meid with a dest pointer returned no pointer" );
184 errors += fail_if( strcmp( c, "test-meid" ) != 0, "did not get expected meid from mbuffer" );
187 return errors > 0; // overall exit code bad if errors