From: Guilherme Maciel Ferreira Date: Wed, 1 Feb 2012 01:15:44 +0000 (-0200) Subject: Test: bring aboard ConfigurationCommandLine test case. X-Git-Tag: v1.3~11^2~24 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=5dfcc527a8619d6cc183b8fa7f5216c160bbd1c3;p=pingcheck Test: bring aboard ConfigurationCommandLine test case. --- diff --git a/test/CMakeLists.test_configurationcommandline.txt b/test/CMakeLists.test_configurationcommandline.txt new file mode 100644 index 0000000..bbbaf00 --- /dev/null +++ b/test/CMakeLists.test_configurationcommandline.txt @@ -0,0 +1,36 @@ +# compiler: creates the binaries +add_executable(test_configurationcommandline + test_configurationcommandline.cpp + ${CMAKE_SOURCE_DIR}/src/config/configurationcommandline.cpp + ${CMAKE_SOURCE_DIR}/src/config/configurationinterface.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/hostdownlimitoption.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_configurationcommandline + ${I2NCOMMON_LIBRARIES} + ${Boost_LIBRARIES} +) + +# cmake: invocation via "make test" +add_test(test_configurationcommandline test_configurationcommandline) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 975be0c..2673b52 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -5,7 +5,7 @@ include(FindPkgConfig) set(Boost_USE_STATIC_LIBS OFF) set(Boost_USE_MULTITHREADED ON) set(Boost_USE_STATIC_RUNTIME OFF) -find_package(Boost 1.44 COMPONENTS unit_test_framework system REQUIRED) +find_package(Boost 1.44 COMPONENTS unit_test_framework system program_options REQUIRED) include_directories(${Boost_INCLUDE_DIRS}) link_directories(${Boost_LIBRARY_DIRS}) @@ -24,6 +24,7 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src) enable_testing() # cmake: inclusion of each test case cmake file +include(CMakeLists.test_configurationcommandline.txt) include(CMakeLists.test_messagepayload.txt) include(CMakeLists.test_hoststatus.txt) include(CMakeLists.test_ipv4header.txt) @@ -33,6 +34,7 @@ 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_messagepayload test_hoststatus test_ipv4header diff --git a/test/test_configurationcommandline.cpp b/test/test_configurationcommandline.cpp new file mode 100644 index 0000000..60f8120 --- /dev/null +++ b/test/test_configurationcommandline.cpp @@ -0,0 +1,121 @@ +/* +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 "config/configurationcommandline.h" + +BOOST_AUTO_TEST_SUITE( TestConfigurationCommandLine ) + +BOOST_AUTO_TEST_CASE( normal_options ) +{ + const int argc = 17; + char *argv[argc] = { + "./pingcheck", + "--config-file=conf/config_file.conf", + "--source-network-interface=eth0", + "--nameserver=localhost", + "--hosts-down-limit=2", + "--ping-fail-limit=80", + "--status-notifier-cmd=scripts/notifier_command.sh", + "--link-up-interval=10", + "--link-down-interval=10", + // 1st host1 + "--host.name=www.intra2net.com", + "--host.port=80", + "--host.interval=4000", + "--host.ping-protocol=TCP", + // 2nd host1 + "--host.name=www.ufsc.br", + "--host.port=25", + "--host.interval=1000", + "--host.ping-protocol=ICMP", + }; + boost::program_options::variables_map vm; + Configuration config; + + ConfigurationCommandLine command_line( argc, argv ); + bool command_line_processed = command_line.process( &vm ); + bool command_line_parsed = command_line.parse( vm, &config ); + + BOOST_CHECK_EQUAL( command_line_processed, true ); + BOOST_CHECK_EQUAL( command_line_parsed, true ); + + BOOST_CHECK_EQUAL( config.get_config_file_name(), "conf/config_file.conf" ); + BOOST_CHECK_EQUAL( config.get_source_network_interface(), "eth0" ); + BOOST_CHECK_EQUAL( config.get_nameserver(), "localhost" ); + BOOST_CHECK_EQUAL( config.get_hosts_down_limit(), 2 ); + BOOST_CHECK_EQUAL( config.get_ping_fail_limit(), 80 ); + BOOST_CHECK_EQUAL( config.get_status_notifier_cmd(), "scripts/notifier_command.sh" ); + BOOST_CHECK_EQUAL( config.get_link_up_interval_in_min(), 10 ); + BOOST_CHECK_EQUAL( config.get_link_down_interval_in_min(), 10 ); + + BOOST_CHECK_EQUAL( config.get_hosts().size(), 2 ); + HostItem host1 = config.get_hosts().at(0); + BOOST_CHECK_EQUAL( host1->get_address(), "www.intra2net.com" ); + BOOST_CHECK_EQUAL( host1->get_port(), 80 ); + BOOST_CHECK_EQUAL( host1->get_interval_in_sec(), 4000 ); + BOOST_CHECK_EQUAL( host1->get_ping_protocol(), PingProtocol_TCP ); + HostItem host2 = config.get_hosts().at(1); + BOOST_CHECK_EQUAL( host2->get_address(), "www.ufsc.br" ); + BOOST_CHECK_EQUAL( host2->get_port(), 25 ); + BOOST_CHECK_EQUAL( host2->get_interval_in_sec(), 1000 ); + BOOST_CHECK_EQUAL( host2->get_ping_protocol(), PingProtocol_ICMP ); +} + +BOOST_AUTO_TEST_CASE( version_option ) +{ + char *argv[2] = { "./pingcheck", "--version" }; + const int argc = 2; + boost::program_options::variables_map vm; + Configuration config; + + ConfigurationCommandLine command_line( argc, argv ); + bool command_line_processed = command_line.process( &vm ); + + bool command_line_parsed = command_line.parse( vm, &config ); + + BOOST_CHECK_EQUAL( command_line_processed, true ); + BOOST_CHECK_EQUAL( command_line_parsed, false ); +} + +BOOST_AUTO_TEST_CASE( help_option ) +{ + char *argv[2] = { "./pingcheck", "--help" }; + const int argc = 2; + boost::program_options::variables_map vm; + Configuration config; + + ConfigurationCommandLine command_line( argc, argv ); + bool command_line_processed = command_line.process( &vm ); + + bool command_line_parsed = command_line.parse( vm, &config ); + + BOOST_CHECK_EQUAL( command_line_processed, true ); + BOOST_CHECK_EQUAL( command_line_parsed, false ); +} + +BOOST_AUTO_TEST_SUITE_END()