Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 1 | // : vi ts=4 sw=4 noet : |
| 2 | /* |
| 3 | ================================================================================== |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 4 | Copyright (c) 2019 Nokia |
| 5 | Copyright (c) 2018-2019 AT&T Intellectual Property. |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 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 | |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 11 | http://www.apache.org/licenses/LICENSE-2.0 |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 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 | 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 |
| 27 | Date: 6 January 2019 |
| 28 | */ |
| 29 | |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 30 | #ifndef _test_support_c |
| 31 | #define _test_support_c |
| 32 | |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 33 | #include <signal.h> |
| 34 | #include <string.h> |
| 35 | #include <stdio.h> |
| 36 | #include <stdlib.h> |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 37 | #include <fcntl.h> |
| 38 | #include <unistd.h> |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 39 | |
| 40 | #ifndef BAD |
| 41 | #define BAD 1 // these are exit codes unless user overrides |
| 42 | #define GOOD 0 |
| 43 | #endif |
| 44 | |
| 45 | /* |
| 46 | Snag the optional positional parameter at pp, return defval if not there. |
| 47 | */ |
| 48 | static char* snag_pp( int pp, int argc, char** argv, char* defval ) { |
| 49 | |
| 50 | if( pp < argc ) { |
| 51 | return argv[pp]; |
| 52 | } |
| 53 | |
| 54 | return defval; |
| 55 | } |
| 56 | |
| 57 | /* |
| 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. |
| 61 | */ |
| 62 | |
| 63 | void sig_clean_exit( int sign ) { |
| 64 | fprintf( stderr, "signal trapped for clean exit: %d\n", sign ); |
| 65 | exit( 0 ); |
| 66 | } |
| 67 | |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 68 | /* |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 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. |
| 71 | */ |
| 72 | static void set_signals( void ) { |
| 73 | struct sigaction sa; |
| 74 | int sig_list[] = { SIGINT, SIGQUIT, SIGILL, SIGALRM, SIGTERM, SIGUSR1 , SIGUSR2 }; |
| 75 | int i; |
| 76 | int nele; // number of elements in the list |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 77 | |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 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 ); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | |
| 87 | static int fail_if_nil( void* p, char* what ) { |
| 88 | if( !p ) { |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 89 | fprintf( stderr, "<FAIL> %s: pointer was nil\n", what ); |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 90 | } |
| 91 | return p ? GOOD : BAD; |
| 92 | } |
| 93 | |
| 94 | static int fail_not_nil( void* p, char* what ) { |
| 95 | if( p ) { |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 96 | fprintf( stderr, "<FAIL> %s: pointer was not nil\n", what ); |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 97 | } |
| 98 | return !p ? GOOD : BAD; |
| 99 | } |
| 100 | |
| 101 | static int fail_if_false( int bv, char* what ) { |
| 102 | if( !bv ) { |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 103 | fprintf( stderr, "<FAIL> %s: expected true, boolean test was false (%d)\n", what, bv ); |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | return bv ? GOOD : BAD; |
| 107 | } |
| 108 | |
| 109 | static int fail_if_true( int bv, char* what ) { |
| 110 | if( bv ) { |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 111 | fprintf( stderr, "<FAIL> %s: expected false, boolean test was true (%d)\n", what, bv ); |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 112 | } |
| 113 | return bv ? BAD : GOOD; |
| 114 | } |
| 115 | |
| 116 | /* |
| 117 | Same as fail_if_true(), but reads easier in the test code. |
| 118 | */ |
| 119 | static int fail_if( int bv, char* what ) { |
| 120 | |
| 121 | if( bv ) { |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 122 | fprintf( stderr, "<FAIL> %s: expected false, boolean test was true (%d)\n", what, bv ); |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 123 | } |
| 124 | return bv ? BAD : GOOD; |
| 125 | } |
| 126 | |
| 127 | static int fail_not_equal( int a, int b, char* what ) { |
| 128 | if( a != b ) { |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 129 | fprintf( stderr, "<FAIL> %s: values were not equal a=%d b=%d\n", what, a, b ); |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 130 | } |
| 131 | return a == b ? GOOD : BAD; // user may override good/bad so do NOT return a==b directly! |
| 132 | } |
| 133 | |
| 134 | static int fail_if_equal( int a, int b, char* what ) { |
| 135 | if( a == b ) { |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 136 | fprintf( stderr, "<FAIL> %s values were equal a=%d b=%d\n", what, a, b ); |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 137 | } |
| 138 | return a != b ? GOOD : BAD; // user may override good/bad so do NOT return a==b directly! |
| 139 | } |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 140 | |
E. Scott Daniels | d710957 | 2019-04-18 14:01:16 +0000 | [diff] [blame] | 141 | |
| 142 | #ifndef NO_DUMMY_RMR |
| 143 | /* |
| 144 | Dummy message allocator for testing without sr_static functions |
| 145 | */ |
| 146 | static rmr_mbuf_t* test_mk_msg( int len, int tr_len ) { |
| 147 | rmr_mbuf_t* new_msg; |
| 148 | uta_mhdr_t* hdr; |
| 149 | size_t alen; |
| 150 | |
| 151 | alen = sizeof( *hdr ) + tr_len + len; |
| 152 | |
| 153 | new_msg = (rmr_mbuf_t *) malloc( sizeof *new_msg ); |
| 154 | new_msg->tp_buf = (void *) malloc( alen ); |
| 155 | memset( new_msg->tp_buf, 0, alen ); |
| 156 | |
| 157 | hdr = (uta_mhdr_t*) new_msg->tp_buf; |
| 158 | SET_HDR_LEN( hdr ); |
| 159 | SET_HDR_TR_LEN( hdr, tr_len ); |
| 160 | |
| 161 | new_msg->header = new_msg->tp_buf; |
| 162 | new_msg->payload = new_msg->header + PAYLOAD_OFFSET( hdr ); |
| 163 | new_msg->alloc_len = alen; |
| 164 | new_msg->len = 0; |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 165 | |
E. Scott Daniels | d710957 | 2019-04-18 14:01:16 +0000 | [diff] [blame] | 166 | return new_msg; |
| 167 | } |
| 168 | #endif |
| 169 | |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 170 | #endif |