752822f862e53fe6a0996c09395a8b8dc48060e6
[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 resolved_ip_count = 10;
40
41     LinkStatusItem link_status( new LinkStatus );
42     HostStatus host_status( "localhost", ping_fail_percentage_limit, link_status );
43     host_status.set_resolved_ip_count( resolved_ip_count );
44
45     host_status.update_ping_statistics( PingStatus_SuccessReply, 1 );
46     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
47
48     host_status.update_ping_statistics( PingStatus_FailureOther, 1 );
49     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
50
51     host_status.update_ping_statistics( PingStatus_FailureOther, 1);
52     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true );
53
54     host_status.update_ping_statistics( PingStatus_SuccessReply, 1 );
55     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true );
56
57     host_status.update_ping_statistics( PingStatus_SuccessReply, 1 );
58     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true );
59
60     host_status.update_ping_statistics( PingStatus_SuccessReply, 1 );
61     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true );
62
63     host_status.update_ping_statistics( PingStatus_SuccessReply, 1 );
64     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true );
65
66     host_status.update_ping_statistics( PingStatus_SuccessReply, 1 );
67     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true );
68
69     host_status.update_ping_statistics( PingStatus_SuccessReply, 1 );
70     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true );
71
72     host_status.update_ping_statistics( PingStatus_SuccessReply, 1 );
73     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true );
74 }
75
76 BOOST_AUTO_TEST_CASE( fail_percentage_50 )
77 {
78     int ping_fail_percentage_limit = 50;
79     int resolved_ip_count = 10;
80
81     LinkStatusItem link_status( new LinkStatus );
82     HostStatus host_status( "localhost", ping_fail_percentage_limit, link_status );
83     host_status.set_resolved_ip_count( resolved_ip_count );
84
85     host_status.update_ping_statistics( PingStatus_SuccessReply, 1 );
86     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
87
88     host_status.update_ping_statistics( PingStatus_FailureOther, 1 );
89     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
90
91     host_status.update_ping_statistics( PingStatus_FailureOther, 1 );
92     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
93
94     host_status.update_ping_statistics( PingStatus_FailureOther, 1 );
95     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
96
97     host_status.update_ping_statistics( PingStatus_FailureOther, 1 );
98     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
99
100     host_status.update_ping_statistics( PingStatus_SuccessReply, 1 );
101     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
102
103     host_status.update_ping_statistics( PingStatus_FailureOther, 1 );
104     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
105
106     host_status.update_ping_statistics( PingStatus_SuccessReply, 1 );
107     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
108
109     host_status.update_ping_statistics( PingStatus_FailureOther, 1 );
110     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true );
111
112     host_status.update_ping_statistics( PingStatus_SuccessReply, 1 );
113     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true );
114 }
115
116 BOOST_AUTO_TEST_CASE( fail_percentage_80 )
117 {
118     int ping_fail_percentage_limit = 80;
119     int resolved_ip_count = 10;
120
121     LinkStatusItem link_status( new LinkStatus );
122     HostStatus host_status( "localhost", ping_fail_percentage_limit, link_status );
123     host_status.set_resolved_ip_count( resolved_ip_count );
124
125     host_status.update_ping_statistics( PingStatus_FailureOther, 1 );
126     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
127
128     host_status.update_ping_statistics( PingStatus_FailureOther, 1 );
129     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
130
131     host_status.update_ping_statistics( PingStatus_FailureOther, 1 );
132     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
133
134     host_status.update_ping_statistics( PingStatus_FailureOther, 1 );
135     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
136
137     host_status.update_ping_statistics( PingStatus_FailureOther, 1 );
138     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
139
140     host_status.update_ping_statistics( PingStatus_FailureOther, 1 );
141     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
142
143     host_status.update_ping_statistics( PingStatus_FailureOther, 1 );
144     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
145
146     host_status.update_ping_statistics( PingStatus_FailureOther, 1 );
147     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
148
149     host_status.update_ping_statistics( PingStatus_FailureOther, 1 );
150     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true );
151
152     host_status.update_ping_statistics( PingStatus_SuccessReply, 1 );
153     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true );
154 }
155
156 BOOST_AUTO_TEST_SUITE_END()