1eb5fc8792f1b105875230a94b81c4438097e564
[pingcheck] / test / test_hoststatus.cpp
1 /*
2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
4
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
7
8 As a special exception, if other files instantiate templates or use macros
9 or inline functions from this file, or you compile this file and link it
10 with other works to produce a work based on this file, this file
11 does not by itself cause the resulting work to be covered
12 by the GNU General Public License.
13
14 However the source code for this file must still be made available
15 in accordance with section (3) of the GNU General Public License.
16
17 This exception does not invalidate any other reasons why a work based
18 on this file might be covered by the GNU General Public License.
19 */
20
21 #define BOOST_TEST_MAIN
22 #define BOOST_TEST_DYN_LINK
23
24 #include <streambuf>
25
26 #include <boost/asio.hpp>
27 #include <boost/test/unit_test.hpp>
28
29 #include "mock_linkstatus.h"
30
31 #include "host/hoststatus.h"
32 #include "host/pingstatus.h"
33
34 BOOST_AUTO_TEST_SUITE( TestHostStatus )
35
36 BOOST_AUTO_TEST_CASE( fail_percentage_10 )
37 {
38     int ping_fail_percentage_limit = 10;
39     int ping_congestion_percentage_limit = 75;
40     int ping_congestion_duration_thresh = 5;
41     int n_parallel_pings = 1;
42     int resolved_ip_count = 10;
43
44     LinkStatusItem link_status( new LinkStatus );
45     HostStatus host_status( "localhost",
46                             ping_fail_percentage_limit,
47                             ping_congestion_percentage_limit,
48                             ping_congestion_duration_thresh,
49                             n_parallel_pings,
50                             link_status );
51     host_status.set_resolved_ip_count( resolved_ip_count );
52
53     host_status.update_ping_statistics( PingStatus_SuccessReply, 1 );
54     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
55
56     host_status.update_ping_statistics( PingStatus_FailureOther, 1 );
57     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
58
59     host_status.update_ping_statistics( PingStatus_FailureOther, 1);
60     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true );
61
62     host_status.update_ping_statistics( PingStatus_SuccessReply, 1 );
63     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true );
64
65     host_status.update_ping_statistics( PingStatus_SuccessReply, 1 );
66     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true );
67
68     host_status.update_ping_statistics( PingStatus_SuccessReply, 1 );
69     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true );
70
71     host_status.update_ping_statistics( PingStatus_SuccessReply, 1 );
72     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true );
73
74     host_status.update_ping_statistics( PingStatus_SuccessReply, 1 );
75     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true );
76
77     host_status.update_ping_statistics( PingStatus_SuccessReply, 1 );
78     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true );
79
80     host_status.update_ping_statistics( PingStatus_SuccessReply, 1 );
81     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true );
82 }
83
84 BOOST_AUTO_TEST_CASE( fail_percentage_50 )
85 {
86     int ping_fail_percentage_limit = 50;
87     int ping_congestion_percentage_limit = 75;
88     int ping_congestion_duration_thresh = 5;
89     int n_parallel_pings = 1;
90     int resolved_ip_count = 10;
91
92     LinkStatusItem link_status( new LinkStatus );
93     HostStatus host_status( "localhost",
94                             ping_fail_percentage_limit,
95                             ping_congestion_percentage_limit,
96                             ping_congestion_duration_thresh,
97                             n_parallel_pings,
98                             link_status );
99     host_status.set_resolved_ip_count( resolved_ip_count );
100
101     host_status.update_ping_statistics( PingStatus_SuccessReply, 1 );
102     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
103
104     host_status.update_ping_statistics( PingStatus_FailureOther, 1 );
105     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
106
107     host_status.update_ping_statistics( PingStatus_FailureOther, 1 );
108     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
109
110     host_status.update_ping_statistics( PingStatus_FailureOther, 1 );
111     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
112
113     host_status.update_ping_statistics( PingStatus_FailureOther, 1 );
114     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
115
116     host_status.update_ping_statistics( PingStatus_SuccessReply, 1 );
117     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
118
119     host_status.update_ping_statistics( PingStatus_FailureOther, 1 );
120     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
121
122     host_status.update_ping_statistics( PingStatus_SuccessReply, 1 );
123     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
124
125     host_status.update_ping_statistics( PingStatus_FailureOther, 1 );
126     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true );
127
128     host_status.update_ping_statistics( PingStatus_SuccessReply, 1 );
129     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true );
130 }
131
132 BOOST_AUTO_TEST_CASE( fail_percentage_80 )
133 {
134     int ping_fail_percentage_limit = 80;
135     int ping_congestion_percentage_limit = 75;
136     int ping_congestion_duration_thresh = 5;
137     int n_parallel_pings = 1;
138     int resolved_ip_count = 10;
139
140     LinkStatusItem link_status( new LinkStatus );
141     HostStatus host_status( "localhost",
142                             ping_fail_percentage_limit,
143                             ping_congestion_percentage_limit,
144                             ping_congestion_duration_thresh,
145                             n_parallel_pings,
146                             link_status );
147     host_status.set_resolved_ip_count( resolved_ip_count );
148
149     host_status.update_ping_statistics( PingStatus_FailureOther, 1 );
150     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
151
152     host_status.update_ping_statistics( PingStatus_FailureOther, 1 );
153     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
154
155     host_status.update_ping_statistics( PingStatus_FailureOther, 1 );
156     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
157
158     host_status.update_ping_statistics( PingStatus_FailureOther, 1 );
159     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
160
161     host_status.update_ping_statistics( PingStatus_FailureOther, 1 );
162     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
163
164     host_status.update_ping_statistics( PingStatus_FailureOther, 1 );
165     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
166
167     host_status.update_ping_statistics( PingStatus_FailureOther, 1 );
168     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
169
170     host_status.update_ping_statistics( PingStatus_FailureOther, 1 );
171     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
172
173     host_status.update_ping_statistics( PingStatus_FailureOther, 1 );
174     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true );
175
176     host_status.update_ping_statistics( PingStatus_SuccessReply, 1 );
177     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true );
178 }
179
180 BOOST_AUTO_TEST_SUITE_END()