From: Guilherme Maciel Ferreira Date: Mon, 6 Feb 2012 02:13:22 +0000 (-0200) Subject: Test: bring aboard ConfigurationOptions test case. X-Git-Tag: v1.3~11^2~17 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=33695392250ad31852dc54c59d1cd9ee8e440480;p=pingcheck Test: bring aboard ConfigurationOptions test case. --- diff --git a/src/config/configurationoptions.cpp b/src/config/configurationoptions.cpp index dd98445..f392ff1 100644 --- a/src/config/configurationoptions.cpp +++ b/src/config/configurationoptions.cpp @@ -200,7 +200,6 @@ bool ConfigurationOptions::parse_generic_options( generic_option->parse( vm, configuration ); } - return false; } diff --git a/test/CMakeLists.test_configurationoptions.txt b/test/CMakeLists.test_configurationoptions.txt new file mode 100644 index 0000000..86b7cfa --- /dev/null +++ b/test/CMakeLists.test_configurationoptions.txt @@ -0,0 +1,34 @@ +# compiler: creates the binaries +add_executable(test_configurationoptions + test_configurationoptions.cpp + ${CMAKE_SOURCE_DIR}/src/config/configurationoptions.cpp + ${CMAKE_SOURCE_DIR}/src/config/configuration.cpp + ${CMAKE_SOURCE_DIR}/src/config/host.cpp + ${CMAKE_SOURCE_DIR}/src/config/option/daemonoption.cpp + ${CMAKE_SOURCE_DIR}/src/config/option/configfileoption.cpp + ${CMAKE_SOURCE_DIR}/src/config/option/configurationoption.cpp + ${CMAKE_SOURCE_DIR}/src/config/option/hostconfigurationoption.cpp + ${CMAKE_SOURCE_DIR}/src/config/option/hostpingintervaloption.cpp + ${CMAKE_SOURCE_DIR}/src/config/option/hostpingprotocoloption.cpp + ${CMAKE_SOURCE_DIR}/src/config/option/hostportoption.cpp + ${CMAKE_SOURCE_DIR}/src/config/option/hostnameoption.cpp + ${CMAKE_SOURCE_DIR}/src/config/option/hostsdownlimitoption.cpp + ${CMAKE_SOURCE_DIR}/src/config/option/linkdownintervaloption.cpp + ${CMAKE_SOURCE_DIR}/src/config/option/linkupintervaloption.cpp + ${CMAKE_SOURCE_DIR}/src/config/option/nameserveroption.cpp + ${CMAKE_SOURCE_DIR}/src/config/option/pingfaillimitoption.cpp + ${CMAKE_SOURCE_DIR}/src/config/option/sourcenetworkinterfaceoption.cpp + ${CMAKE_SOURCE_DIR}/src/config/option/statusnotifiercmdoption.cpp + ${CMAKE_SOURCE_DIR}/src/config/option/versionoption.cpp + ${CMAKE_SOURCE_DIR}/src/host/pingprotocol.cpp +) + +# linker: link the program against the libraries +target_link_libraries( + test_configurationoptions + ${I2NCOMMON_LIBRARIES} + ${Boost_LIBRARIES} +) + +# cmake: invocation via "make test" +add_test(test_configurationoptions test_configurationoptions) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index c02aac5..d832c8b 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -26,6 +26,7 @@ enable_testing() # cmake: inclusion of each test case cmake file include(CMakeLists.test_configurationcommandline.txt) include(CMakeLists.test_configurationfile.txt) +include(CMakeLists.test_configurationoptions.txt) include(CMakeLists.test_messagepayload.txt) include(CMakeLists.test_hoststatus.txt) include(CMakeLists.test_ipv4header.txt) @@ -36,6 +37,8 @@ include(CMakeLists.test_icmpv6header.txt) # cmake: add a custom "make check" target which automatically builds the binary add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} DEPENDS test_configurationcommandline + test_configurationfile + test_configurationoptions test_messagepayload test_hoststatus test_ipv4header diff --git a/test/test_configurationoptions.cpp b/test/test_configurationoptions.cpp new file mode 100644 index 0000000..c7dd972 --- /dev/null +++ b/test/test_configurationoptions.cpp @@ -0,0 +1,273 @@ +/* +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 + +#include +#include +#include + +#include "host/pingprotocol.h" +#include "config/configurationoptions.h" + +//------------------------------------------------------------------------------ + +typedef boost::shared_ptr 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( 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 hosts_names; + std::vector hosts_ports; + std::vector hosts_ping_protocols; + std::vector 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()