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 std::vector< option_description_item >::const_iterator first = options.begin();
43 std::vector< option_description_item >::const_iterator last = options.end();
45 while ( first != last )
47 if ( (*first)->long_name() == option_to_find ) {
56 const std::string &key,
57 const boost::any &value,
58 boost::program_options::variables_map &vm
61 boost::program_options::variable_value var_value( value, true );
63 vm.insert( std::pair<std::string, boost::program_options::variable_value>( key, var_value ) );
66 void option_clear_and_insert(
67 const std::string &key,
68 const boost::any &value,
69 boost::program_options::variables_map &vm
74 option_insert( key, value, vm );
77 //------------------------------------------------------------------------------
79 BOOST_AUTO_TEST_SUITE( TestConfigurationOptions )
81 BOOST_AUTO_TEST_CASE( get_generic_options )
83 ConfigurationOptions config_options;
84 std::vector< option_description_item > options = config_options.get_generic_options().options();
86 // if this assert fails, you must add or remove one of the options in the
88 BOOST_CHECK_EQUAL( options.size(), 4 ); // help, version, daemon and config-file
90 BOOST_CHECK_EQUAL( option_present( options, "help" ), true );
91 BOOST_CHECK_EQUAL( option_present( options, "config-file" ), true );
92 BOOST_CHECK_EQUAL( option_present( options, "daemon" ), true );
93 BOOST_CHECK_EQUAL( option_present( options, "version" ), true );
96 BOOST_AUTO_TEST_CASE( get_configuration_options )
98 ConfigurationOptions config_options;
99 std::vector< option_description_item > options = config_options.get_configuration_options().options();
101 // if this assert fails, you must add or remove one of the options in the
103 BOOST_CHECK_EQUAL( options.size(), 11 );
105 BOOST_CHECK_EQUAL( option_present( options, "hosts-down-limit" ), true );
106 BOOST_CHECK_EQUAL( option_present( options, "link-down-interval" ), true );
107 BOOST_CHECK_EQUAL( option_present( options, "link-up-interval" ), true );
108 BOOST_CHECK_EQUAL( option_present( options, "nameserver" ), true );
109 BOOST_CHECK_EQUAL( option_present( options, "ping-fail-limit" ), true );
110 BOOST_CHECK_EQUAL( option_present( options, "source-network-interface" ), true );
111 BOOST_CHECK_EQUAL( option_present( options, "status-notifier-cmd" ), true );
112 BOOST_CHECK_EQUAL( option_present( options, "host.name" ), true );
113 BOOST_CHECK_EQUAL( option_present( options, "host.port" ), true );
114 BOOST_CHECK_EQUAL( option_present( options, "host.ping-protocol" ), true );
115 BOOST_CHECK_EQUAL( option_present( options, "host.interval" ), true );
118 BOOST_AUTO_TEST_CASE( parse_generic_options )
120 boost::program_options::variables_map vm;
122 option_insert( "config-file", boost::any( std::string("pingcheck.conf") ), vm );
123 option_insert( "daemon", boost::any(), vm );
125 ConfigurationOptions config_options;
126 Configuration configuration;
127 config_options.parse_generic_options( vm, &configuration );
129 BOOST_CHECK_EQUAL( configuration.get_config_file_name(), "pingcheck.conf" );
130 BOOST_CHECK_EQUAL( configuration.get_daemon(), true );
133 BOOST_AUTO_TEST_CASE( parse_configuration_options )
135 boost::program_options::variables_map vm;
137 option_insert( "hosts-down-limit", boost::any( 0 ), vm );
138 option_insert( "link-down-interval", boost::any( 30 ), vm );
139 option_insert( "link-up-interval", boost::any( 40 ), vm );
140 option_insert( "nameserver", boost::any( std::string("localhost") ), vm );
141 option_insert( "ping-fail-limit", boost::any( 50 ), vm );
142 option_insert( "source-network-interface", boost::any( std::string("wlan1") ), vm );
143 option_insert( "status-notifier-cmd", boost::any( std::string("/tmp/command.sh") ), vm );
145 ConfigurationOptions config_options;
146 Configuration configuration;
147 config_options.parse_configuration_options( vm, &configuration );
149 BOOST_CHECK_EQUAL( configuration.get_hosts_down_limit(), 0 );
150 BOOST_CHECK_EQUAL( configuration.get_link_down_interval_in_min(), 30 );
151 BOOST_CHECK_EQUAL( configuration.get_link_up_interval_in_min(), 40 );
152 BOOST_CHECK_EQUAL( configuration.get_nameserver(), "localhost" );
153 BOOST_CHECK_EQUAL( configuration.get_ping_fail_limit(), 50 );
154 BOOST_CHECK_EQUAL( configuration.get_source_network_interface(), "wlan1" );
155 BOOST_CHECK_EQUAL( configuration.get_status_notifier_cmd(), "/tmp/command.sh" );
158 BOOST_AUTO_TEST_CASE( parse_hosts_options )
160 std::vector<std::string> hosts_names;
161 std::vector<int> hosts_ports;
162 std::vector<std::string> hosts_ping_protocols;
163 std::vector<int> hosts_intervals;
165 hosts_names.push_back( "www.fazenda.gov.br" );
166 hosts_ports.push_back( 61 );
167 hosts_ping_protocols.push_back( "TCP" );
168 hosts_intervals.push_back( 71 );
170 hosts_names.push_back( "www.intra2net.de" );
171 hosts_ports.push_back( 62 );
172 hosts_ping_protocols.push_back( "ICMP" );
173 hosts_intervals.push_back( 72 );
175 hosts_names.push_back( "www.ufsc.br" );
176 hosts_ports.push_back( 63 );
177 hosts_ping_protocols.push_back( "ICMPv6" );
178 hosts_intervals.push_back( 73 );
180 hosts_names.push_back( "www.google.com" );
181 hosts_ports.push_back( 64 );
182 hosts_ping_protocols.push_back( "TCP_IPv6" );
183 hosts_intervals.push_back( 74 );
185 ConfigurationOptions config_options;
186 Configuration configuration;
187 boost::program_options::variables_map vm;
188 option_insert( "host.name", boost::any( hosts_names ), vm );
189 option_insert( "host.port", boost::any( hosts_ports ), vm );
190 option_insert( "host.ping-protocol", boost::any( hosts_ping_protocols ), vm );
191 option_insert( "host.interval", boost::any( hosts_intervals ), vm );
193 config_options.parse_configuration_options( vm, &configuration );
196 HostItem host1 = configuration.get_hosts().at(0);
197 BOOST_CHECK_EQUAL( host1->get_address(), "www.fazenda.gov.br" );
198 BOOST_CHECK_EQUAL( host1->get_port(), 61 );
199 BOOST_CHECK_EQUAL( host1->get_ping_protocol(), PingProtocol_TCP );
200 BOOST_CHECK_EQUAL( host1->get_interval_in_sec(), 71 );
202 HostItem host2 = configuration.get_hosts().at(1);
203 BOOST_CHECK_EQUAL( host2->get_address(), "www.intra2net.de" );
204 BOOST_CHECK_EQUAL( host2->get_port(), 62 );
205 BOOST_CHECK_EQUAL( host2->get_ping_protocol(), PingProtocol_ICMP );
206 BOOST_CHECK_EQUAL( host2->get_interval_in_sec(), 72 );
208 HostItem host3 = configuration.get_hosts().at(2);
209 BOOST_CHECK_EQUAL( host3->get_address(), "www.ufsc.br" );
210 BOOST_CHECK_EQUAL( host3->get_port(), 63 );
211 BOOST_CHECK_EQUAL( host3->get_ping_protocol(), PingProtocol_ICMPv6 );
212 BOOST_CHECK_EQUAL( host3->get_interval_in_sec(), 73 );
214 HostItem host4 = configuration.get_hosts().at(3);
215 BOOST_CHECK_EQUAL( host4->get_address(), "www.google.com" );
216 BOOST_CHECK_EQUAL( host4->get_port(), 64 );
217 BOOST_CHECK_EQUAL( host4->get_ping_protocol(), PingProtocol_TCP_IPv6 );
218 BOOST_CHECK_EQUAL( host4->get_interval_in_sec(), 74 );
221 BOOST_AUTO_TEST_CASE( halt_on_generic_options )
223 ConfigurationOptions config_options;
224 boost::program_options::variables_map vm;
227 option_clear_and_insert( "config-file", value, vm );
228 BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
230 option_clear_and_insert( "daemon", value, vm );
231 BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
233 option_clear_and_insert( "help", value, vm );
234 BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), true );
236 option_clear_and_insert( "version", value, vm );
237 BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), true );
239 option_clear_and_insert( "hosts-down-limit", value, vm );
240 BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
242 option_clear_and_insert( "link-down-interval", value, vm );
243 BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
245 option_clear_and_insert( "link-up-interval", value, vm );
246 BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
248 option_clear_and_insert( "nameserver", value, vm );
249 BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
251 option_clear_and_insert( "ping-fail-limit", value, vm );
252 BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
254 option_clear_and_insert( "source-network-interface", value, vm );
255 BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
257 option_clear_and_insert( "status-notifier-cmd", value, vm );
258 BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
260 option_clear_and_insert( "host.name", value, vm );
261 BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
263 option_clear_and_insert( "host.port", value, vm );
264 BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
266 option_clear_and_insert( "host.ping-protocol", value, vm );
267 BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
269 option_clear_and_insert( "host.interval", value, vm );
270 BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
273 BOOST_AUTO_TEST_SUITE_END()