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 Mnemonic: test_tools.c
23 Abstract: Functions for test applications to make their life a bit easier.
24 This file is probably compiled to a .o, and then included on
25 the cc command for the test.
26 Author: E. Scott Daniels
30 #ifndef _test_support_c
31 #define _test_support_c
41 #define BAD 1 // these are exit codes unless user overrides
46 Snag the optional positional parameter at pp, return defval if not there.
48 static char* snag_pp( int pp, int argc, char** argv, char* defval ) {
58 Signal handler -- inside of the tests we will exit cleanly for hup/temp/intr
59 signals so that the coverage stuff will generate the needed data files. If
60 we inter/term the process they don't drive.
63 void sig_clean_exit( int sign ) {
64 fprintf( stderr, "signal trapped for clean exit: %d\n", sign );
69 Setup all of the signal handling for signals that we want to force a clean exit:
70 term, intr, hup, quit, usr1/2 alarm, etc. All others we'll let default.
72 static void set_signals( void ) {
74 int sig_list[] = { SIGINT, SIGQUIT, SIGILL, SIGALRM, SIGTERM, SIGUSR1 , SIGUSR2 };
76 int nele; // number of elements in the list
78 nele = (int) ( sizeof( sig_list )/sizeof( int ) ); // convert raw size to the number of elements
79 for( i = 0; i < nele; i ++ ) {
80 memset( &sa, 0, sizeof( sa ) );
81 sa.sa_handler = sig_clean_exit;
82 sigaction( sig_list[i], &sa, NULL );
87 static int fail_if_nil( void* p, char* what ) {
89 fprintf( stderr, "<FAIL> %s: pointer was nil\n", what );
91 return p ? GOOD : BAD;
94 static int fail_not_nil( void* p, char* what ) {
96 fprintf( stderr, "<FAIL> %s: pointer was not nil\n", what );
98 return !p ? GOOD : BAD;
101 static int fail_if_false( int bv, char* what ) {
103 fprintf( stderr, "<FAIL> %s: expected true, boolean test was false (%d)\n", what, bv );
106 return bv ? GOOD : BAD;
109 static int fail_if_true( int bv, char* what ) {
111 fprintf( stderr, "<FAIL> %s: expected false, boolean test was true (%d)\n", what, bv );
113 return bv ? BAD : GOOD;
117 Same as fail_if_true(), but reads easier in the test code.
119 static int fail_if( int bv, char* what ) {
122 fprintf( stderr, "<FAIL> %s: expected false, boolean test was true (%d)\n", what, bv );
124 return bv ? BAD : GOOD;
127 static int fail_not_equal( int a, int b, char* what ) {
129 fprintf( stderr, "<FAIL> %s: values were not equal a=%d b=%d\n", what, a, b );
131 return a == b ? GOOD : BAD; // user may override good/bad so do NOT return a==b directly!
134 static int fail_if_equal( int a, int b, char* what ) {
136 fprintf( stderr, "<FAIL> %s values were equal a=%d b=%d\n", what, a, b );
138 return a != b ? GOOD : BAD; // user may override good/bad so do NOT return a==b directly!
141 static int fail_not_equalp( void* a, void* b, char* what ) {
143 fprintf( stderr, "<FAIL> %s: pointers were not equal a=%p b=%p\n", what, a, b );
145 return a == b ? GOOD : BAD; // user may override good/bad so do NOT return a==b directly!
148 static int fail_if_equalp( void* a, void* b, char* what ) {
150 fprintf( stderr, "<FAIL> %s pointers were equal a=%p b=%p\n", what, a, b );
152 return a != b ? GOOD : BAD; // user may override good/bad so do NOT return a==b directly!
158 Dummy message allocator for testing without sr_static functions
164 static rmr_mbuf_t* test_mk_msg( int len, int tr_len ) {
169 alen = sizeof( *hdr ) + tr_len + len;
171 new_msg = (rmr_mbuf_t *) malloc( sizeof *new_msg );
172 new_msg->tp_buf = (void *) malloc( alen );
173 memset( new_msg->tp_buf, 0, alen );
175 hdr = (uta_mhdr_t*) new_msg->tp_buf;
177 SET_HDR_TR_LEN( hdr, tr_len );
178 hdr->rmr_ver = htonl( MSG_VER );
179 strcpy( hdr->src, "dummyhost:1111" );
180 strcpy( hdr->srcip, "30.4.19.86:1111" );
182 new_msg->header = new_msg->tp_buf;
183 new_msg->payload = new_msg->header + PAYLOAD_OFFSET( hdr );
184 new_msg->alloc_len = alen;
190 static void test_set_ver( rmr_mbuf_t* msg, int ver ) {
193 hdr = (uta_mhdr_t*) msg->tp_buf;
194 hdr->rmr_ver = htonl( ver );
195 strcpy( hdr->src, "dummyhost-v2:1111" );
196 strcpy( hdr->srcip, "30.4.19.86:2222" );