--- /dev/null
+/*
+The software in this package is distributed under the GNU General
+Public License version 2 (with a special exception described below).
+
+A copy of GNU General Public License (GPL) is included in this distribution,
+in the file COPYING.GPL.
+
+As a special exception, if other files instantiate templates or use macros
+or inline functions from this file, or you compile this file and link it
+with other works to produce a work based on this file, this file
+does not by itself cause the resulting work to be covered
+by the GNU General Public License.
+
+However the source code for this file must still be made available
+in accordance with section (3) of the GNU General Public License.
+
+This exception does not invalidate any other reasons why a work based
+on this file might be covered by the GNU General Public License.
+*/
+
+#define BOOST_TEST_MAIN
+#define BOOST_TEST_DYN_LINK
+
+#include <algorithm>
+
+#include <boost/program_options.hpp>
+#include <boost/shared_ptr.hpp>
+#include <boost/test/unit_test.hpp>
+
+#include "host/pingprotocol.h"
+#include "config/configurationoptions.h"
+
+//------------------------------------------------------------------------------
+
+typedef boost::shared_ptr<boost::program_options::option_description> option_description_item;
+
+bool option_present(
+ const std::vector< option_description_item > &options,
+ const std::string &option_to_find
+)
+{
+ std::vector< option_description_item >::const_iterator first = options.begin();
+ std::vector< option_description_item >::const_iterator last = options.end();
+
+ while ( first != last )
+ {
+ if ( (*first)->long_name() == option_to_find ) {
+ return true;
+ }
+ first++;
+ }
+ return false;
+}
+
+void option_insert(
+ const std::string &key,
+ const boost::any &value,
+ boost::program_options::variables_map &vm
+)
+{
+ boost::program_options::variable_value var_value( value, true );
+
+ vm.insert( std::pair<std::string, boost::program_options::variable_value>( key, var_value ) );
+}
+
+void option_clear_and_insert(
+ const std::string &key,
+ const boost::any &value,
+ boost::program_options::variables_map &vm
+)
+{
+ vm.clear();
+
+ option_insert( key, value, vm );
+}
+
+//------------------------------------------------------------------------------
+
+BOOST_AUTO_TEST_SUITE( TestConfigurationOptions )
+
+BOOST_AUTO_TEST_CASE( get_generic_options )
+{
+ ConfigurationOptions config_options;
+ std::vector< option_description_item > options = config_options.get_generic_options().options();
+
+ // if this assert fails, you must add or remove one of the options in the
+ // test bellow
+ BOOST_CHECK_EQUAL( options.size(), 4 ); // help, version, daemon and config-file
+
+ BOOST_CHECK_EQUAL( option_present( options, "help" ), true );
+ BOOST_CHECK_EQUAL( option_present( options, "config-file" ), true );
+ BOOST_CHECK_EQUAL( option_present( options, "daemon" ), true );
+ BOOST_CHECK_EQUAL( option_present( options, "version" ), true );
+}
+
+BOOST_AUTO_TEST_CASE( get_configuration_options )
+{
+ ConfigurationOptions config_options;
+ std::vector< option_description_item > options = config_options.get_configuration_options().options();
+
+ // if this assert fails, you must add or remove one of the options in the
+ // test bellow
+ BOOST_CHECK_EQUAL( options.size(), 11 );
+
+ BOOST_CHECK_EQUAL( option_present( options, "hosts-down-limit" ), true );
+ BOOST_CHECK_EQUAL( option_present( options, "link-down-interval" ), true );
+ BOOST_CHECK_EQUAL( option_present( options, "link-up-interval" ), true );
+ BOOST_CHECK_EQUAL( option_present( options, "nameserver" ), true );
+ BOOST_CHECK_EQUAL( option_present( options, "ping-fail-limit" ), true );
+ BOOST_CHECK_EQUAL( option_present( options, "source-network-interface" ), true );
+ BOOST_CHECK_EQUAL( option_present( options, "status-notifier-cmd" ), true );
+ BOOST_CHECK_EQUAL( option_present( options, "host.name" ), true );
+ BOOST_CHECK_EQUAL( option_present( options, "host.port" ), true );
+ BOOST_CHECK_EQUAL( option_present( options, "host.ping-protocol" ), true );
+ BOOST_CHECK_EQUAL( option_present( options, "host.interval" ), true );
+}
+
+BOOST_AUTO_TEST_CASE( parse_generic_options )
+{
+ boost::program_options::variables_map vm;
+
+ option_insert( "config-file", boost::any( std::string("pingcheck.conf") ), vm );
+ option_insert( "daemon", boost::any(), vm );
+
+ ConfigurationOptions config_options;
+ Configuration configuration;
+ config_options.parse_generic_options( vm, &configuration );
+
+ BOOST_CHECK_EQUAL( configuration.get_config_file_name(), "pingcheck.conf" );
+ BOOST_CHECK_EQUAL( configuration.get_daemon(), true );
+}
+
+BOOST_AUTO_TEST_CASE( parse_configuration_options )
+{
+ boost::program_options::variables_map vm;
+
+ option_insert( "hosts-down-limit", boost::any( 0 ), vm );
+ option_insert( "link-down-interval", boost::any( 30 ), vm );
+ option_insert( "link-up-interval", boost::any( 40 ), vm );
+ option_insert( "nameserver", boost::any( std::string("localhost") ), vm );
+ option_insert( "ping-fail-limit", boost::any( 50 ), vm );
+ option_insert( "source-network-interface", boost::any( std::string("wlan1") ), vm );
+ option_insert( "status-notifier-cmd", boost::any( std::string("/tmp/command.sh") ), vm );
+
+ ConfigurationOptions config_options;
+ Configuration configuration;
+ config_options.parse_configuration_options( vm, &configuration );
+
+ BOOST_CHECK_EQUAL( configuration.get_hosts_down_limit(), 0 );
+ BOOST_CHECK_EQUAL( configuration.get_link_down_interval_in_min(), 30 );
+ BOOST_CHECK_EQUAL( configuration.get_link_up_interval_in_min(), 40 );
+ BOOST_CHECK_EQUAL( configuration.get_nameserver(), "localhost" );
+ BOOST_CHECK_EQUAL( configuration.get_ping_fail_limit(), 50 );
+ BOOST_CHECK_EQUAL( configuration.get_source_network_interface(), "wlan1" );
+ BOOST_CHECK_EQUAL( configuration.get_status_notifier_cmd(), "/tmp/command.sh" );
+}
+
+BOOST_AUTO_TEST_CASE( parse_hosts_options )
+{
+ std::vector<std::string> hosts_names;
+ std::vector<int> hosts_ports;
+ std::vector<std::string> hosts_ping_protocols;
+ std::vector<int> hosts_intervals;
+ // host 1
+ hosts_names.push_back( "www.fazenda.gov.br" );
+ hosts_ports.push_back( 61 );
+ hosts_ping_protocols.push_back( "TCP" );
+ hosts_intervals.push_back( 71 );
+ // host 2
+ hosts_names.push_back( "www.intra2net.de" );
+ hosts_ports.push_back( 62 );
+ hosts_ping_protocols.push_back( "ICMP" );
+ hosts_intervals.push_back( 72 );
+ // host 3
+ hosts_names.push_back( "www.ufsc.br" );
+ hosts_ports.push_back( 63 );
+ hosts_ping_protocols.push_back( "ICMPv6" );
+ hosts_intervals.push_back( 73 );
+ // host 4
+ hosts_names.push_back( "www.google.com" );
+ hosts_ports.push_back( 64 );
+ hosts_ping_protocols.push_back( "TCP_IPv6" );
+ hosts_intervals.push_back( 74 );
+
+ ConfigurationOptions config_options;
+ Configuration configuration;
+ boost::program_options::variables_map vm;
+ option_insert( "host.name", boost::any( hosts_names ), vm );
+ option_insert( "host.port", boost::any( hosts_ports ), vm );
+ option_insert( "host.ping-protocol", boost::any( hosts_ping_protocols ), vm );
+ option_insert( "host.interval", boost::any( hosts_intervals ), vm );
+
+ config_options.parse_configuration_options( vm, &configuration );
+
+ // host 1
+ HostItem host1 = configuration.get_hosts().at(0);
+ BOOST_CHECK_EQUAL( host1->get_address(), "www.fazenda.gov.br" );
+ BOOST_CHECK_EQUAL( host1->get_port(), 61 );
+ BOOST_CHECK_EQUAL( host1->get_ping_protocol(), PingProtocol_TCP );
+ BOOST_CHECK_EQUAL( host1->get_interval_in_sec(), 71 );
+ // host 2
+ HostItem host2 = configuration.get_hosts().at(1);
+ BOOST_CHECK_EQUAL( host2->get_address(), "www.intra2net.de" );
+ BOOST_CHECK_EQUAL( host2->get_port(), 62 );
+ BOOST_CHECK_EQUAL( host2->get_ping_protocol(), PingProtocol_ICMP );
+ BOOST_CHECK_EQUAL( host2->get_interval_in_sec(), 72 );
+ // host 3
+ HostItem host3 = configuration.get_hosts().at(2);
+ BOOST_CHECK_EQUAL( host3->get_address(), "www.ufsc.br" );
+ BOOST_CHECK_EQUAL( host3->get_port(), 63 );
+ BOOST_CHECK_EQUAL( host3->get_ping_protocol(), PingProtocol_ICMPv6 );
+ BOOST_CHECK_EQUAL( host3->get_interval_in_sec(), 73 );
+ // host 4
+ HostItem host4 = configuration.get_hosts().at(3);
+ BOOST_CHECK_EQUAL( host4->get_address(), "www.google.com" );
+ BOOST_CHECK_EQUAL( host4->get_port(), 64 );
+ BOOST_CHECK_EQUAL( host4->get_ping_protocol(), PingProtocol_TCP_IPv6 );
+ BOOST_CHECK_EQUAL( host4->get_interval_in_sec(), 74 );
+}
+
+BOOST_AUTO_TEST_CASE( halt_on_generic_options )
+{
+ ConfigurationOptions config_options;
+ boost::program_options::variables_map vm;
+ boost::any value;
+
+ option_clear_and_insert( "config-file", value, vm );
+ BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
+
+ option_clear_and_insert( "daemon", value, vm );
+ BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
+
+ option_clear_and_insert( "help", value, vm );
+ BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), true );
+
+ option_clear_and_insert( "version", value, vm );
+ BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), true );
+
+ option_clear_and_insert( "hosts-down-limit", value, vm );
+ BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
+
+ option_clear_and_insert( "link-down-interval", value, vm );
+ BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
+
+ option_clear_and_insert( "link-up-interval", value, vm );
+ BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
+
+ option_clear_and_insert( "nameserver", value, vm );
+ BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
+
+ option_clear_and_insert( "ping-fail-limit", value, vm );
+ BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
+
+ option_clear_and_insert( "source-network-interface", value, vm );
+ BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
+
+ option_clear_and_insert( "status-notifier-cmd", value, vm );
+ BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
+
+ option_clear_and_insert( "host.name", value, vm );
+ BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
+
+ option_clear_and_insert( "host.port", value, vm );
+ BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
+
+ option_clear_and_insert( "host.ping-protocol", value, vm );
+ BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
+
+ option_clear_and_insert( "host.interval", value, vm );
+ BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
+}
+
+BOOST_AUTO_TEST_SUITE_END()