From: Guilherme Maciel Ferreira Date: Mon, 13 Feb 2012 00:28:47 +0000 (-0200) Subject: Test: bring aboard LinkStatus test case. X-Git-Tag: v1.3~11^2~14 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=c5db9dfddee73a9d41806648dd8d180a03c07cb1;p=pingcheck Test: bring aboard LinkStatus test case. --- diff --git a/src/link/linkstatus.h b/src/link/linkstatus.h index 24f7830..a8f0ca6 100644 --- a/src/link/linkstatus.h +++ b/src/link/linkstatus.h @@ -65,7 +65,11 @@ private: NotificationStatus_Reported }; +#ifdef UNDER_TEST +public: +#else private: +#endif void add_host_up( const std::string &host_address ); void add_host_down( const std::string &host_address ); diff --git a/test/CMakeLists.test_linkstatus.txt b/test/CMakeLists.test_linkstatus.txt new file mode 100644 index 0000000..df6b5da --- /dev/null +++ b/test/CMakeLists.test_linkstatus.txt @@ -0,0 +1,15 @@ +# compiler: creates the binaries +add_executable(test_linkstatus + test_linkstatus + ${CMAKE_SOURCE_DIR}/src/link/linkstatus.cpp +) + +# linker: link the program against the libraries +target_link_libraries( + test_linkstatus + ${I2NCOMMON_LIBRARIES} + ${Boost_LIBRARIES} +) + +# cmake: invocation via "make test" +add_test(test_linkstatus test_linkstatus) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index aaba1f6..d8a020d 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -30,6 +30,7 @@ include(CMakeLists.test_configurationoptions.txt) include(CMakeLists.test_messagepayload.txt) include(CMakeLists.test_pingprotocol.txt) include(CMakeLists.test_hoststatus.txt) +include(CMakeLists.test_linkstatus.txt) include(CMakeLists.test_ipv4header.txt) include(CMakeLists.test_ipv6header.txt) include(CMakeLists.test_icmpv4header.txt) @@ -43,6 +44,7 @@ add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} DEPENDS test_messagepayload test_pingprotocol test_hoststatus + test_linkstatus test_ipv4header test_ipv6header test_icmpv4header diff --git a/test/mock_statusnotifiercommand.h b/test/mock_statusnotifiercommand.h new file mode 100644 index 0000000..7583888 --- /dev/null +++ b/test/mock_statusnotifiercommand.h @@ -0,0 +1,95 @@ +/* +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. +*/ +#ifndef STATUS_NOTIFIER_COMMAND_H +#define STATUS_NOTIFIER_COMMAND_H + +#include + +#include + +#define UNDER_TEST + +//----------------------------------------------------------------------------- +// StatusNotifierCommand +//----------------------------------------------------------------------------- + +/** + * @brief This class provides methods to handle status-notifier-cmd + * configuration. + */ +class StatusNotifierCommand +{ +public: + static const std::string StatusToken; + +public: + explicit StatusNotifierCommand( const std::string &status_notifier_cmd ); + virtual ~StatusNotifierCommand(); + + bool set_token_value( + const std::string &token, + const std::string &value + ); + std::string get_token_value() const; + + bool execute() const; +}; + +//----------------------------------------------------------------------------- +// StatusNotifierCommandItem +//----------------------------------------------------------------------------- + +typedef boost::scoped_ptr StatusNotifierCommandItem; + +//----------------------------------------------------------------------------- + +const std::string StatusNotifierCommand::StatusToken("${status}"); + +//----------------------------------------------------------------------------- + +StatusNotifierCommand::StatusNotifierCommand( + const std::string & +) +{ +} + +StatusNotifierCommand::~StatusNotifierCommand() +{ +} + +bool StatusNotifierCommand::set_token_value( + const std::string &, + const std::string &value +) +{ + return true; +} + +std::string StatusNotifierCommand::get_token_value() const +{ + return std::string(); +} + +bool StatusNotifierCommand::execute() const +{ + return true; +} + +#endif // STATUS_NOTIFIER_COMMAND_H diff --git a/test/test_linkstatus.cpp b/test/test_linkstatus.cpp new file mode 100644 index 0000000..87f6bec --- /dev/null +++ b/test/test_linkstatus.cpp @@ -0,0 +1,102 @@ +/* +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 "mock_statusnotifiercommand.h" + +#include "link/linkstatus.h" + +BOOST_AUTO_TEST_SUITE( TestLinkStatus ) + +BOOST_AUTO_TEST_CASE( is_link_up_enough_time ) +{ + int hosts_down_limit = 2; + int link_up_interval_in_min = 0; + int link_down_interval_in_min = 1; + + std::string status_notifier_cmd = "none"; + + LinkStatus link_status( + hosts_down_limit, + link_up_interval_in_min, + link_down_interval_in_min, + status_notifier_cmd + ); + + link_status.notify_host_up( "www.intra2net.com" ); + BOOST_CHECK_EQUAL( link_status.is_link_up_enough_time(), true ); +} + +BOOST_AUTO_TEST_CASE( is_link_down_enough_time ) +{ + int hosts_down_limit = 2; + int link_up_interval_in_min = 1; + int link_down_interval_in_min = 0; + + std::string status_notifier_cmd = "none"; + + LinkStatus link_status( + hosts_down_limit, + link_up_interval_in_min, + link_down_interval_in_min, + status_notifier_cmd + ); + + link_status.notify_host_down( "www.intra2net.com" ); + BOOST_CHECK_EQUAL( link_status.is_link_down_enough_time(), true ); +} + +BOOST_AUTO_TEST_CASE( exceeded_host_down_limit ) +{ + int hosts_down_limit = 2; + int link_up_interval_in_min = 1; + int link_down_interval_in_min = 1; + + std::string status_notifier_cmd = "none"; + + LinkStatus link_status( + hosts_down_limit, + link_up_interval_in_min, + link_down_interval_in_min, + status_notifier_cmd + ); + + link_status.notify_host_down( "www.intra2net.com" ); + BOOST_CHECK_EQUAL( link_status.exceeded_host_down_limit(), false ); + + link_status.notify_host_down( "www.ufsc.br" ); + BOOST_CHECK_EQUAL( link_status.exceeded_host_down_limit(), false ); + + link_status.notify_host_down( "www.google.com" ); + BOOST_CHECK_EQUAL( link_status.exceeded_host_down_limit(), true ); + + link_status.notify_host_down( "www.fazenda.gov.br" ); + BOOST_CHECK_EQUAL( link_status.exceeded_host_down_limit(), true ); +} + +BOOST_AUTO_TEST_SUITE_END()