CI: Add silent cmake SonarCloud scan
[ric-plt/lib/rmr.git] / test / alarm_static_test.c
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
30 /*
31         These tests assume there is a dummy process listening on 127.0.0.1:1986; if it's not there
32         the tests still pass, but coverage is reduced because the sends never happen.
33 */
34 static int alarm_test( ) {
35         int errors = 0;                         // number errors found
36         uta_ctx_t* ctx;
37         uta_ctx_t* pctx;                        // tests  into rtable functions need a second context
38         char*   endpt = NULL;
39
40         ctx = mk_dummy_ctx();
41         gen_rt( ctx );
42
43         ctx->my_name = strdup( "private" );
44         ctx->my_ip = strdup( "30.4.19.86:2750" );
45
46         endpt = uta_alarm_endpt();                                              // check defaults are generated
47         if( fail_if_nil( endpt, "alarm endpoint did not generate a string for defaults" ) ) {
48                 errors++;
49         } else {
50                 errors += fail_if_false( strcmp( endpt, "service-ricplt-alarmmanager-rmr:4560" ) == 0, "alarm endpoint default string not expected" );
51                 free( endpt );
52         }
53
54         setenv( "ALARM_MGR_SERVICE_NAME", "127.0.0.1", 1 );                             // test to ensure digging from env is good too
55         setenv( "ALARM_MGR_SERVICE_PORT", "999", 1 );                                   // connect should fail
56         endpt = uta_alarm_endpt();                                              // check defaults are generated
57         uta_alarm( ctx, ALARM_RAISE, 0, "some info for the alarm" );    // this should fail as the service isn't running
58
59         setenv( "ALARM_MGR_SERVICE_NAME", "127.0.0.1", 1 );                             // test to ensure digging from env is good too
60         setenv( "ALARM_MGR_SERVICE_PORT", "1986", 1 );
61         endpt = uta_alarm_endpt();                                              // check defaults are generated
62         if( fail_if_nil( endpt, "alarm endpoint did not generate a string when name/port are set in env" ) ) {
63                 errors++;
64         } else {
65                 errors += fail_if_false( strcmp( endpt, "127.0.0.1:1986" ) == 0, "alarm endpoint string not expected when values are in env" );
66                 free( endpt );
67         }
68
69
70         // these functions do not return values; driving for coverage and crash testing
71         uta_alarm( ctx, ALARM_RAISE, 0, "some info for the alarm" );
72         uta_alarm( ctx, ALARM_CLEAR, 0, NULL );
73         uta_alarm_send( ctx, NULL );                                                                    // ensure nil message doesn't crash us
74
75
76         // ------ drive the alarm if dropping function in the route table code --------------------------------
77
78         pctx = mk_dummy_ctx();                          // grab a private context for rt to use
79
80         /*
81                 These tests don't return anything that we can check; driving just to cover the lines and ensure
82                 we don't segfault or something bad like that.
83         */
84         ctx->dcount - 0;
85         alarm_if_drops( ctx,  pctx );                   // should do nothing; no drops indicated
86
87         ctx->dcount = 1024;                                             // make it look like we dropped things
88         alarm_if_drops( ctx,  pctx );                   // should drive the code block to send alarm and put is in dropping mode
89
90         ctx->dcount = 1028;                                             // make it look like we are still  dropping
91         alarm_if_drops( ctx,  pctx );                   // drive the just reset time to clear block
92
93         alarm_if_drops( ctx,  pctx );                   // drive the check to see if past the clear time (it's not) to reset timer
94
95         fprintf( stderr, "<TEST> pausing 65 seconds before driving last alarm if drops call\n" );
96         sleep( 65 );                                                    // we must pause for longer than the timer so we can drive last block
97         alarm_if_drops( ctx,  pctx );                   // should appear that we're not dropping and reset the alarm
98
99
100         // -------------------------- tidy the house ---------------------------------------------------------
101         if( ctx ) {
102                 free( ctx->my_name );
103                 free( ctx->my_ip );
104                 free( ctx );
105         }
106
107         if( pctx ) {
108                 free( pctx->my_name );
109                 free( pctx->my_ip );
110                 free( pctx );
111         }
112
113         return errors;
114 }