E. Scott Daniels | 2686455 | 2021-02-22 14:42:21 -0500 | [diff] [blame^] | 1 | // : vi ts=4 sw=4 noet : |
| 2 | /* |
| 3 | ================================================================================== |
| 4 | Copyright (c) 2021 Nokia |
| 5 | Copyright (c) 2021 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: alarm_test.c |
| 23 | Abstract: Unit test for common/src/alarm.c functions. |
| 24 | |
| 25 | Author: E. Scott Daniels |
| 26 | Date: 22 February 2021 |
| 27 | */ |
| 28 | |
| 29 | #ifdef KEEP |
| 30 | |
| 31 | #include <unistd.h> |
| 32 | #include <stdio.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <strings.h> |
| 35 | #include <errno.h> |
| 36 | #include <string.h> |
| 37 | #include <stdint.h> |
| 38 | #include <pthread.h> |
| 39 | #include <semaphore.h> |
| 40 | |
| 41 | |
| 42 | #include <unistd.h> |
| 43 | #include <stdio.h> |
| 44 | #include <stdlib.h> |
| 45 | #include <strings.h> |
| 46 | #include <errno.h> |
| 47 | #include <string.h> |
| 48 | #include <stdint.h> |
| 49 | #include <ctype.h> |
| 50 | //#include <sys/epoll.h> |
| 51 | //#include <pthread.h> |
| 52 | //#include <semaphore.h> |
| 53 | |
| 54 | #define DEBUG 1 // must define before pulling in rmr header files |
| 55 | #define PARANOID_CHECKS 1 // must have parinoid testing on to not fail on nil pointer tests |
| 56 | |
| 57 | |
| 58 | // specific test tools in this directory |
| 59 | #undef NNG_UNDER_TEST |
| 60 | #include "test_support.c" // things like fail_if() |
| 61 | #include "test_msg_support.c" |
| 62 | #include "test_gen_rt.c" |
| 63 | |
| 64 | |
| 65 | #include "rmr.h" // things the users see |
| 66 | #include "rmr_agnostic.h" // rmr private things |
| 67 | |
| 68 | #include "rmr_symtab.h" // must pull in for context setup |
| 69 | #include "rmr_agnostic.h" // transport agnostic header |
| 70 | |
| 71 | #include "logging.c" |
| 72 | #include "rt_generic_static.c" |
| 73 | #include "tools_static.c" |
| 74 | #include "symtab.c" |
| 75 | //#include "rmr_si.c" |
| 76 | //#include "mbuf_api.c" |
| 77 | |
| 78 | #include "test_ctx_support.c" // dummy context support (needs symtab defs, so not with others above) |
| 79 | |
| 80 | |
| 81 | //static void gen_rt( uta_ctx_t* ctx ); // defined in sr_si_static_test, but used by a few others (eliminate order requirement below) |
| 82 | |
| 83 | // and finally.... the things under test |
| 84 | #include "alarm.c" |
| 85 | //#include "tools_static_test.c" // local test functions pulled directly because of static nature of things |
| 86 | //#include "symtab_static_test.c" |
| 87 | //#include "ring_static_test.c" |
| 88 | //#include "rt_static_test.c" |
| 89 | //#include "wormhole_static_test.c" |
| 90 | //#include "mbuf_api_static_test.c" |
| 91 | //#include "sr_si_static_test.c" |
| 92 | //#include "lg_buf_static_test.c" |
| 93 | // do NOT include the receive test static must be stand alone |
| 94 | |
| 95 | |
| 96 | #endif |
| 97 | |
| 98 | /* |
| 99 | These tests assume there is a dummy process listening on 127.0.0.1:1986; if it's not there |
| 100 | the tests still pass, but coverage is reduced because the sends never happen. |
| 101 | */ |
| 102 | static int alarm_test( ) { |
| 103 | int errors = 0; // number errors found |
| 104 | uta_ctx_t* ctx; |
| 105 | char* endpt = NULL; |
| 106 | |
| 107 | ctx = mk_dummy_ctx(); |
| 108 | gen_rt( ctx ); |
| 109 | |
| 110 | ctx->my_name = strdup( "private" ); |
| 111 | ctx->my_ip = strdup( "30.4.19.86:2750" ); |
| 112 | |
| 113 | endpt = uta_alarm_endpt(); // check defaults are generated |
| 114 | if( fail_if_nil( endpt, "alarm endpoint did not generate a string for defaults" ) ) { |
| 115 | errors++; |
| 116 | } else { |
| 117 | errors += fail_if_false( strcmp( endpt, "service-ricplt-alarmmanager-rmr:4560" ) == 0, "alarm endpoint default string not expected" ); |
| 118 | free( endpt ); |
| 119 | } |
| 120 | |
| 121 | setenv( "ALARM_MGR_SERVICE_NAME", "127.0.0.1", 1 ); // test to ensure digging from env is good too |
| 122 | setenv( "ALARM_MGR_SERVICE_PORT", "999", 1 ); // connect should fail |
| 123 | endpt = uta_alarm_endpt(); // check defaults are generated |
| 124 | uta_alarm( ctx, ALARM_RAISE, 0, "some info for the alarm" ); // this should fail as the service isn't running |
| 125 | |
| 126 | setenv( "ALARM_MGR_SERVICE_NAME", "127.0.0.1", 1 ); // test to ensure digging from env is good too |
| 127 | setenv( "ALARM_MGR_SERVICE_PORT", "1986", 1 ); |
| 128 | endpt = uta_alarm_endpt(); // check defaults are generated |
| 129 | if( fail_if_nil( endpt, "alarm endpoint did not generate a string when name/port are set in env" ) ) { |
| 130 | errors++; |
| 131 | } else { |
| 132 | errors += fail_if_false( strcmp( endpt, "127.0.0.1:1986" ) == 0, "alarm endpoint string not expected when values are in env" ); |
| 133 | free( endpt ); |
| 134 | } |
| 135 | |
| 136 | |
| 137 | // these functions do not return values; driving for coverage and crash testing |
| 138 | uta_alarm( ctx, ALARM_RAISE, 0, "some info for the alarm" ); |
| 139 | uta_alarm( ctx, ALARM_CLEAR, 0, NULL ); |
| 140 | uta_alarm_send( ctx, NULL ); // ensure nil message doesn't crash us |
| 141 | |
| 142 | |
| 143 | if( ctx ) { |
| 144 | free( ctx->my_name ); |
| 145 | free( ctx->my_ip ); |
| 146 | free( ctx ); |
| 147 | } |
| 148 | |
| 149 | return !!errors; // 1 or 0 regardless of count |
| 150 | } |
| 151 | /* |
| 152 | |
| 153 | int main( ) { |
| 154 | int errors = 0; |
| 155 | |
| 156 | errors += alarm_test(); |
| 157 | exit( !!errors ); |
| 158 | } |
| 159 | */ |