2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
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.
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.
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.
21 #define BOOST_TEST_MAIN
22 #define BOOST_TEST_DYN_LINK
26 #include <boost/program_options.hpp>
27 #include <boost/shared_ptr.hpp>
28 #include <boost/test/unit_test.hpp>
30 #include "host/pingprotocol.h"
31 #include "config/configurationoptions.h"
33 //------------------------------------------------------------------------------
35 typedef boost::shared_ptr<boost::program_options::option_description> option_description_item;
38 const std::vector< option_description_item > &options,
39 const std::string &option_to_find
42 const std::string &key,
43 const boost::any &value,
44 boost::program_options::variables_map &vm
46 void option_clear_and_insert(
47 const std::string &key,
48 const boost::any &value,
49 boost::program_options::variables_map &vm
52 //------------------------------------------------------------------------------
55 const std::vector< option_description_item > &options,
56 const std::string &option_to_find
59 std::vector< option_description_item >::const_iterator first = options.begin();
60 std::vector< option_description_item >::const_iterator last = options.end();
62 while ( first != last )
64 if ( (*first)->long_name() == option_to_find ) {
73 const std::string &key,
74 const boost::any &value,
75 boost::program_options::variables_map &vm
78 boost::program_options::variable_value var_value( value, true );
80 vm.insert( std::pair<std::string, boost::program_options::variable_value>( key, var_value ) );
83 void option_clear_and_insert(
84 const std::string &key,
85 const boost::any &value,
86 boost::program_options::variables_map &vm
91 option_insert( key, value, vm );
94 //------------------------------------------------------------------------------
96 BOOST_AUTO_TEST_SUITE( TestConfigurationOptions )
98 BOOST_AUTO_TEST_CASE( get_generic_options )
100 ConfigurationOptions config_options;
101 std::vector< option_description_item > options = config_options.get_generic_options().options();
103 // if this assert fails, you must add or remove one of the options in the
105 BOOST_CHECK_EQUAL( options.size(), 4 ); // help, version, daemon and config-file
107 BOOST_CHECK_EQUAL( option_present( options, "help" ), true );
108 BOOST_CHECK_EQUAL( option_present( options, "config-file" ), true );
109 BOOST_CHECK_EQUAL( option_present( options, "daemon" ), true );
110 BOOST_CHECK_EQUAL( option_present( options, "version" ), true );
113 BOOST_AUTO_TEST_CASE( get_configuration_options )
115 ConfigurationOptions config_options;
116 std::vector< option_description_item > options = config_options.get_configuration_options().options();
118 // if this assert fails, you must add or remove one of the options in the
120 BOOST_CHECK_EQUAL( options.size(), 11 );
122 BOOST_CHECK_EQUAL( option_present( options, "hosts-down-limit" ), true );
123 BOOST_CHECK_EQUAL( option_present( options, "link-down-interval" ), true );
124 BOOST_CHECK_EQUAL( option_present( options, "link-up-interval" ), true );
125 BOOST_CHECK_EQUAL( option_present( options, "nameserver" ), true );
126 BOOST_CHECK_EQUAL( option_present( options, "ping-fail-limit" ), true );
127 BOOST_CHECK_EQUAL( option_present( options, "source-network-interface" ), true );
128 BOOST_CHECK_EQUAL( option_present( options, "status-notifier-cmd" ), true );
129 BOOST_CHECK_EQUAL( option_present( options, "host.name" ), true );
130 BOOST_CHECK_EQUAL( option_present( options, "host.port" ), true );
131 BOOST_CHECK_EQUAL( option_present( options, "host.ping-protocol" ), true );
132 BOOST_CHECK_EQUAL( option_present( options, "host.interval" ), true );
135 BOOST_AUTO_TEST_CASE( parse_generic_options )
137 boost::program_options::variables_map vm;
139 option_insert( "config-file", boost::any( std::string("pingcheck.conf") ), vm );
140 option_insert( "daemon", boost::any(), vm );
142 ConfigurationOptions config_options;
143 Configuration configuration;
144 config_options.parse_generic_options( vm, &configuration );
146 BOOST_CHECK_EQUAL( configuration.get_config_file_name(), "pingcheck.conf" );
147 BOOST_CHECK_EQUAL( configuration.get_daemon(), true );
150 BOOST_AUTO_TEST_CASE( parse_configuration_options )
152 boost::program_options::variables_map vm;
154 option_insert( "hosts-down-limit", boost::any( 0 ), vm );
155 option_insert( "link-down-interval", boost::any( 30 ), vm );
156 option_insert( "link-up-interval", boost::any( 40 ), vm );
157 option_insert( "nameserver", boost::any( std::string("localhost") ), vm );
158 option_insert( "ping-fail-limit", boost::any( 50 ), vm );
159 option_insert( "source-network-interface", boost::any( std::string("wlan1") ), vm );
160 option_insert( "status-notifier-cmd", boost::any( std::string("/tmp/command.sh") ), vm );
162 ConfigurationOptions config_options;
163 Configuration configuration;
164 config_options.parse_configuration_options( vm, &configuration );
166 BOOST_CHECK_EQUAL( configuration.get_hosts_down_limit(), 0 );
167 BOOST_CHECK_EQUAL( configuration.get_link_down_interval_in_min(), 30 );
168 BOOST_CHECK_EQUAL( configuration.get_link_up_interval_in_min(), 40 );
169 BOOST_CHECK_EQUAL( configuration.get_nameserver(), "localhost" );
170 BOOST_CHECK_EQUAL( configuration.get_ping_fail_limit(), 50 );
171 BOOST_CHECK_EQUAL( configuration.get_source_network_interface(), "wlan1" );
172 BOOST_CHECK_EQUAL( configuration.get_status_notifier_cmd(), "/tmp/command.sh" );
175 BOOST_AUTO_TEST_CASE( parse_hosts_options )
177 std::vector<std::string> hosts_names;
178 std::vector<int> hosts_ports;
179 std::vector<std::string> hosts_ping_protocols;
180 std::vector<int> hosts_intervals;
182 hosts_names.push_back( "www.fazenda.gov.br" );
183 hosts_ports.push_back( 61 );
184 hosts_ping_protocols.push_back( "TCP" );
185 hosts_intervals.push_back( 71 );
187 hosts_names.push_back( "www.intra2net.de" );
188 hosts_ports.push_back( 62 );
189 hosts_ping_protocols.push_back( "ICMP" );
190 hosts_intervals.push_back( 72 );
192 hosts_names.push_back( "www.ufsc.br" );
193 hosts_ports.push_back( 63 );
194 hosts_ping_protocols.push_back( "ICMPv6" );
195 hosts_intervals.push_back( 73 );
197 hosts_names.push_back( "www.google.com" );
198 hosts_ports.push_back( 64 );
199 hosts_ping_protocols.push_back( "TCP_IPv6" );
200 hosts_intervals.push_back( 74 );
202 ConfigurationOptions config_options;
203 Configuration configuration;
204 boost::program_options::variables_map vm;
205 option_insert( "host.name", boost::any( hosts_names ), vm );
206 option_insert( "host.port", boost::any( hosts_ports ), vm );
207 option_insert( "host.ping-protocol", boost::any( hosts_ping_protocols ), vm );
208 option_insert( "host.interval", boost::any( hosts_intervals ), vm );
210 config_options.parse_configuration_options( vm, &configuration );
213 HostItem host1 = configuration.get_hosts().at(0);
214 BOOST_CHECK_EQUAL( host1->get_address(), "www.fazenda.gov.br" );
215 BOOST_CHECK_EQUAL( host1->get_port(), 61 );
216 BOOST_CHECK_EQUAL( host1->get_ping_protocol(), PingProtocol_TCP );
217 BOOST_CHECK_EQUAL( host1->get_interval_in_sec(), 71 );
219 HostItem host2 = configuration.get_hosts().at(1);
220 BOOST_CHECK_EQUAL( host2->get_address(), "www.intra2net.de" );
221 BOOST_CHECK_EQUAL( host2->get_port(), 62 );
222 BOOST_CHECK_EQUAL( host2->get_ping_protocol(), PingProtocol_ICMP );
223 BOOST_CHECK_EQUAL( host2->get_interval_in_sec(), 72 );
225 HostItem host3 = configuration.get_hosts().at(2);
226 BOOST_CHECK_EQUAL( host3->get_address(), "www.ufsc.br" );
227 BOOST_CHECK_EQUAL( host3->get_port(), 63 );
228 BOOST_CHECK_EQUAL( host3->get_ping_protocol(), PingProtocol_ICMPv6 );
229 BOOST_CHECK_EQUAL( host3->get_interval_in_sec(), 73 );
231 HostItem host4 = configuration.get_hosts().at(3);
232 BOOST_CHECK_EQUAL( host4->get_address(), "www.google.com" );
233 BOOST_CHECK_EQUAL( host4->get_port(), 64 );
234 BOOST_CHECK_EQUAL( host4->get_ping_protocol(), PingProtocol_TCP_IPv6 );
235 BOOST_CHECK_EQUAL( host4->get_interval_in_sec(), 74 );
238 BOOST_AUTO_TEST_CASE( halt_on_generic_options )
240 ConfigurationOptions config_options;
241 boost::program_options::variables_map vm;
244 option_clear_and_insert( "config-file", value, vm );
245 BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
247 option_clear_and_insert( "daemon", value, vm );
248 BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
250 option_clear_and_insert( "help", value, vm );
251 BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), true );
253 option_clear_and_insert( "version", value, vm );
254 BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), true );
256 option_clear_and_insert( "hosts-down-limit", value, vm );
257 BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
259 option_clear_and_insert( "link-down-interval", value, vm );
260 BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
262 option_clear_and_insert( "link-up-interval", value, vm );
263 BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
265 option_clear_and_insert( "nameserver", value, vm );
266 BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
268 option_clear_and_insert( "ping-fail-limit", value, vm );
269 BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
271 option_clear_and_insert( "source-network-interface", value, vm );
272 BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
274 option_clear_and_insert( "status-notifier-cmd", value, vm );
275 BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
277 option_clear_and_insert( "host.name", value, vm );
278 BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
280 option_clear_and_insert( "host.port", value, vm );
281 BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
283 option_clear_and_insert( "host.ping-protocol", value, vm );
284 BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
286 option_clear_and_insert( "host.interval", value, vm );
287 BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
290 BOOST_AUTO_TEST_SUITE_END()