Test: bring aboard HostStatusAnalyzer test case.
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Fri, 20 Jan 2012 10:21:06 +0000 (08:21 -0200)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Fri, 20 Jan 2012 10:21:06 +0000 (08:21 -0200)
test/CMakeLists.txt
test/test_hoststatusanalyzer.cpp [new file with mode: 0644]

index 39a13a3..c1a392a 100644 (file)
@@ -23,15 +23,22 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src)
 # compiler: source code from test cases
 set(TESTS_SOURCES
     test_messagepayload.cpp
+    test_hoststatusanalyzer.cpp
     test_ipv4header.cpp
     test_ipv6header.cpp
     test_icmpv4header.cpp
     test_icmpv6header.cpp
 )
 
+# compiler: source code from mock objects
+set(MOCK_SOURCES
+    mock_linkstatusanalyzer.cpp
+)
+
 # compiler: source code to be tested
 set(SOURCES
     ${CMAKE_SOURCE_DIR}/src/host/messagepayload.cpp
+    ${CMAKE_SOURCE_DIR}/src/host/hoststatusanalyzer.cpp
     ${CMAKE_SOURCE_DIR}/src/ip/ipv4header.cpp
     ${CMAKE_SOURCE_DIR}/src/ip/ipv6header.cpp
     ${CMAKE_SOURCE_DIR}/src/icmp/icmpv4header.cpp
@@ -44,7 +51,7 @@ set(SOURCES
 )
 
 # compiler: creates the binary
-add_executable(test_${TARGET} ${SOURCES} ${TESTS_SOURCES})
+add_executable(test_${TARGET} ${SOURCES} ${MOCK_SOURCES} ${TESTS_SOURCES})
 
 # cmake: add a custom "make check" target which automatically builds the binary
 add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} DEPENDS test_${TARGET})
diff --git a/test/test_hoststatusanalyzer.cpp b/test/test_hoststatusanalyzer.cpp
new file mode 100644 (file)
index 0000000..d795998
--- /dev/null
@@ -0,0 +1,157 @@
+/*
+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_DYN_LINK
+#define BOOST_TEST_MESSAGE usage
+
+#include <streambuf>
+
+#include <boost/asio.hpp>
+#include <boost/test/unit_test.hpp>
+
+#include "mock_linkstatusanalyzer.h"
+
+#include "host/hoststatusanalyzer.h"
+
+BOOST_AUTO_TEST_SUITE( TestHostStatusAnalyzer )
+
+BOOST_AUTO_TEST_CASE( fail_percentage_10 )
+{
+    int ping_fail_percentage_limit = 10;
+    int resolved_ip_count = 10;
+
+    LinkStatusAnalyzerItem link_status( new LinkStatusAnalyzer );
+    HostStatusAnalyzer host_status( "localhost", ping_fail_percentage_limit, link_status );
+    host_status.set_resolved_ip_count( resolved_ip_count );
+
+    host_status.update_ping_statistics( true );
+    BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
+
+    host_status.update_ping_statistics( false );
+    BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
+
+    host_status.update_ping_statistics( false );
+    BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true );
+
+    host_status.update_ping_statistics( true );
+    BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true );
+
+    host_status.update_ping_statistics( true );
+    BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true );
+
+    host_status.update_ping_statistics( true );
+    BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true );
+
+    host_status.update_ping_statistics( true );
+    BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true );
+
+    host_status.update_ping_statistics( true );
+    BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true );
+
+    host_status.update_ping_statistics( true );
+    BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true );
+
+    host_status.update_ping_statistics( true );
+    BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true );
+}
+
+BOOST_AUTO_TEST_CASE( fail_percentage_50 )
+{
+    int ping_fail_percentage_limit = 50;
+    int resolved_ip_count = 10;
+
+    LinkStatusAnalyzerItem link_status( new LinkStatusAnalyzer );
+    HostStatusAnalyzer host_status( "localhost", ping_fail_percentage_limit, link_status );
+    host_status.set_resolved_ip_count( resolved_ip_count );
+
+    host_status.update_ping_statistics( true );
+    BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
+
+    host_status.update_ping_statistics( false );
+    BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
+
+    host_status.update_ping_statistics( false );
+    BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
+
+    host_status.update_ping_statistics( false );
+    BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
+
+    host_status.update_ping_statistics( false );
+    BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
+
+    host_status.update_ping_statistics( true );
+    BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
+
+    host_status.update_ping_statistics( false );
+    BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
+
+    host_status.update_ping_statistics( true );
+    BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
+
+    host_status.update_ping_statistics( false );
+    BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true );
+
+    host_status.update_ping_statistics( true );
+    BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true );
+}
+
+BOOST_AUTO_TEST_CASE( fail_percentage_80 )
+{
+    int ping_fail_percentage_limit = 80;
+    int resolved_ip_count = 10;
+
+    LinkStatusAnalyzerItem link_status( new LinkStatusAnalyzer );
+    HostStatusAnalyzer host_status( "localhost", ping_fail_percentage_limit, link_status );
+    host_status.set_resolved_ip_count( resolved_ip_count );
+
+    host_status.update_ping_statistics( false );
+    BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
+
+    host_status.update_ping_statistics( false );
+    BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
+
+    host_status.update_ping_statistics( false );
+    BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
+
+    host_status.update_ping_statistics( false );
+    BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
+
+    host_status.update_ping_statistics( false );
+    BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
+
+    host_status.update_ping_statistics( false );
+    BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
+
+    host_status.update_ping_statistics( false );
+    BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
+
+    host_status.update_ping_statistics( false );
+    BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
+
+    host_status.update_ping_statistics( false );
+    BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true );
+
+    host_status.update_ping_statistics( true );
+    BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true );
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+