Test: bring aboard Ipv4Header test case.
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Sun, 8 Jan 2012 14:41:03 +0000 (12:41 -0200)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Sun, 8 Jan 2012 14:41:03 +0000 (12:41 -0200)
test/CMakeLists.txt
test/test_ipv4header.cpp [new file with mode: 0644]

index 03b5fe9..5d9d9a5 100644 (file)
@@ -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 REQUIRED)
+find_package(Boost 1.44 COMPONENTS unit_test_framework system REQUIRED)
 include_directories(${Boost_INCLUDE_DIRS})
 link_directories(${Boost_LIBRARY_DIRS})
 
@@ -23,11 +23,13 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src)
 # compiler: source code from test cases
 set(TESTS_SOURCES
     test_messagepayload.cpp
+    test_ipv4header.cpp
 )
 
 # compiler: source code to be tested
 set(SOURCES
     ${CMAKE_SOURCE_DIR}/src/host/messagepayload.cpp
+    ${CMAKE_SOURCE_DIR}/src/ip/ipv4header.cpp
 )
 
 # compiler: creates the binary
@@ -44,5 +46,5 @@ add_test(test_${TARGET} test_${TARGET})
 target_link_libraries(
     test_${TARGET}
     ${I2NCOMMON_LIBRARIES}
-    ${Boost_UNIT_TEST_FRAMEWORK_LIBRARIES}
+    ${Boost_LIBRARIES}
 )
diff --git a/test/test_ipv4header.cpp b/test/test_ipv4header.cpp
new file mode 100644 (file)
index 0000000..9931d65
--- /dev/null
@@ -0,0 +1,115 @@
+/*
+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
+
+#include <streambuf>
+
+#include <boost/test/unit_test.hpp>
+
+#include "ip/ipv4header.h"
+
+BOOST_AUTO_TEST_SUITE( TestIpv4Header )
+
+BOOST_AUTO_TEST_CASE( icmp_packet )
+{
+    uint8_t raw_packet_stream[] = {
+        0x45, 0x00, 0x00, 0x54, 0x0f, 0x12, 0x00, 0x00, 0x33, 0x01, 0xea,
+        0x2e, 0xc8, 0x93, 0x03, 0xc7, 0xc0, 0xa8, 0x01, 0x66
+    };
+
+    std::basic_stringbuf<uint8_t> sb;
+    sb.pubsetbuf( raw_packet_stream, sizeof(raw_packet_stream) );
+    std::basic_istream<uint8_t> is( &sb );
+
+    Ipv4Header ip_header;
+    reinterpret_cast<std::istream&>(is) >> ip_header;
+
+    BOOST_CHECK_EQUAL( ip_header.get_version(), 4 );
+    BOOST_CHECK_EQUAL( ip_header.get_header_length(), 20 );
+    BOOST_CHECK_EQUAL( ip_header.get_total_length(), 84 );
+    BOOST_CHECK_EQUAL( ip_header.get_identification(), 0x0F12 );
+    BOOST_CHECK_EQUAL( ip_header.dont_fragment(), false );
+    BOOST_CHECK_EQUAL( ip_header.more_fragments(), false );
+    BOOST_CHECK_EQUAL( ip_header.get_fragment_offset(), 0 );
+    BOOST_CHECK_EQUAL( ip_header.get_time_to_live(), 51 );
+    BOOST_CHECK_EQUAL( ip_header.get_protocol(), 0x01 ); // ICMP
+    BOOST_CHECK_EQUAL( ip_header.get_header_checksum(), 0xEA2E );
+    BOOST_CHECK_EQUAL( ip_header.get_source_address(), boost::asio::ip::address_v4::from_string("200.147.3.199") );
+    BOOST_CHECK_EQUAL( ip_header.get_destination_address(), boost::asio::ip::address_v4::from_string("192.168.1.102") );
+}
+
+BOOST_AUTO_TEST_CASE( tcp_packet )
+{
+    uint8_t raw_packet_stream[] = {
+        0x45, 0x00, 0x00, 0x34, 0x93, 0x2a, 0x40, 0x00, 0x40, 0x06, 0x08,
+        0x57, 0xc0, 0xa8, 0x01, 0x66, 0x08, 0x14, 0xd5, 0x20
+    };
+
+    std::basic_stringbuf<uint8_t> sb;
+    sb.pubsetbuf( raw_packet_stream, sizeof(raw_packet_stream) );
+    std::basic_istream<uint8_t> is( &sb );
+
+    Ipv4Header ip_header;
+    reinterpret_cast<std::istream&>(is) >> ip_header;
+
+    BOOST_CHECK_EQUAL( ip_header.get_version(), 4 );
+    BOOST_CHECK_EQUAL( ip_header.get_header_length(), 20 );
+    BOOST_CHECK_EQUAL( ip_header.get_total_length(), 52 );
+    BOOST_CHECK_EQUAL( ip_header.get_identification(), 0x932A );
+    BOOST_CHECK_EQUAL( ip_header.dont_fragment(), true );
+    BOOST_CHECK_EQUAL( ip_header.more_fragments(), false );
+    BOOST_CHECK_EQUAL( ip_header.get_fragment_offset(), 0 );
+    BOOST_CHECK_EQUAL( ip_header.get_time_to_live(), 64 );
+    BOOST_CHECK_EQUAL( ip_header.get_protocol(), 0x06 ); // TCP
+    BOOST_CHECK_EQUAL( ip_header.get_header_checksum(), 0x0857 );
+    BOOST_CHECK_EQUAL( ip_header.get_source_address(), boost::asio::ip::address_v4::from_string("192.168.1.102") );
+    BOOST_CHECK_EQUAL( ip_header.get_destination_address(), boost::asio::ip::address_v4::from_string( "8.20.213.32" ) );
+}
+
+BOOST_AUTO_TEST_CASE( udp_packet )
+{
+    uint8_t raw_packet_stream[] = {
+        0x45, 0x00, 0x00, 0x49, 0x1c, 0xd9, 0x40, 0x00, 0x40, 0x11, 0x9a,
+        0x13, 0xc0, 0xa8, 0x01, 0x66, 0xc0, 0xa8, 0x01, 0x01
+    };
+
+    std::basic_stringbuf<uint8_t> sb;
+    sb.pubsetbuf( raw_packet_stream, sizeof(raw_packet_stream) );
+    std::basic_istream<uint8_t> is( &sb );
+
+    Ipv4Header ip_header;
+    reinterpret_cast<std::istream&>(is) >> ip_header;
+
+    BOOST_CHECK_EQUAL( ip_header.get_version(), 4 );
+    BOOST_CHECK_EQUAL( ip_header.get_header_length(), 20 );
+    BOOST_CHECK_EQUAL( ip_header.get_total_length(), 73 );
+    BOOST_CHECK_EQUAL( ip_header.get_identification(), 0x1CD9 );
+    BOOST_CHECK_EQUAL( ip_header.dont_fragment(), true );
+    BOOST_CHECK_EQUAL( ip_header.more_fragments(), false );
+    BOOST_CHECK_EQUAL( ip_header.get_fragment_offset(), 0 );
+    BOOST_CHECK_EQUAL( ip_header.get_time_to_live(), 64 );
+    BOOST_CHECK_EQUAL( ip_header.get_protocol(), 0x11 ); // UDP
+    BOOST_CHECK_EQUAL( ip_header.get_header_checksum(), 0x9A13 );
+    BOOST_CHECK_EQUAL( ip_header.get_source_address(), boost::asio::ip::address_v4::from_string("192.168.1.102") );
+    BOOST_CHECK_EQUAL( ip_header.get_destination_address(), boost::asio::ip::address_v4::from_string( "192.168.1.1" ) );
+}
+
+BOOST_AUTO_TEST_SUITE_END()