Test: bring aboard TcpHeader test case.
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Sat, 18 Feb 2012 02:46:14 +0000 (00:46 -0200)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Sat, 18 Feb 2012 02:46:14 +0000 (00:46 -0200)
test/CMakeLists.test_tcpheader.txt [new file with mode: 0644]
test/CMakeLists.txt
test/test_tcpheader.cpp [new file with mode: 0644]

diff --git a/test/CMakeLists.test_tcpheader.txt b/test/CMakeLists.test_tcpheader.txt
new file mode 100644 (file)
index 0000000..3df0853
--- /dev/null
@@ -0,0 +1,16 @@
+# compiler: creates the binaries
+add_executable(test_tcpheader
+    test_tcpheader.cpp
+    ${CMAKE_SOURCE_DIR}/src/tcp/tcpheader.cpp
+    ${CMAKE_SOURCE_DIR}/src/host/messagepayload.cpp
+)
+
+# linker: link the program against the libraries
+target_link_libraries(
+    test_tcpheader
+    ${I2NCOMMON_LIBRARIES}
+    ${Boost_LIBRARIES}
+)
+
+# cmake: invocation via "make test"
+add_test(test_tcpheader test_tcpheader)
index d8a020d..885455e 100644 (file)
@@ -35,6 +35,7 @@ include(CMakeLists.test_ipv4header.txt)
 include(CMakeLists.test_ipv6header.txt)
 include(CMakeLists.test_icmpv4header.txt)
 include(CMakeLists.test_icmpv6header.txt)
+include(CMakeLists.test_tcpheader.txt)
 
 # cmake: add a custom "make check" target which automatically builds the binary
 add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} DEPENDS
diff --git a/test/test_tcpheader.cpp b/test/test_tcpheader.cpp
new file mode 100644 (file)
index 0000000..44a2977
--- /dev/null
@@ -0,0 +1,126 @@
+/*
+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 <streambuf>
+
+#include <boost/asio.hpp>
+#include <boost/test/unit_test.hpp>
+
+#include "tcp/tcpheader.h"
+
+BOOST_AUTO_TEST_SUITE( TestTcpHeader )
+
+BOOST_AUTO_TEST_CASE( ack_received )
+{
+    uint8_t raw_segment_stream[] = {
+        0x9f, 0x70, 0x00, 0x50, 0x58, 0xb6, 0x52, 0x49, 0x00, 0x00, 0x00, 0x00,
+        0xa0, 0x02, 0x39, 0x08, 0xf7, 0x27, 0x00, 0x00, 0x02, 0x04, 0x05, 0xb4,
+        0x04, 0x02, 0x08, 0x0a, 0x00, 0x05, 0x59, 0x20, 0x00, 0x00, 0x00, 0x00,
+        0x01, 0x03, 0x03, 0x06
+    };
+
+    std::basic_stringbuf<uint8_t> sb;
+    sb.pubsetbuf( raw_segment_stream, sizeof(raw_segment_stream) );
+    std::basic_istream<uint8_t> is( &sb );
+
+    TcpHeader tcp_header;
+    reinterpret_cast<std::istream&>(is) >> tcp_header;
+
+    BOOST_CHECK_EQUAL( tcp_header.get_source_port(), 40816 );
+    BOOST_CHECK_EQUAL( tcp_header.get_destination_port(), 80 );
+    BOOST_CHECK_EQUAL( tcp_header.get_acknowledgment_number(), 0 );
+    BOOST_CHECK_EQUAL( tcp_header.get_header_length(), 10 ); // 10 words = 40 bytes
+    BOOST_CHECK_EQUAL( tcp_header.get_congestion_window_reduced(), false );
+    BOOST_CHECK_EQUAL( tcp_header.get_ecn_echo(), false );
+    BOOST_CHECK_EQUAL( tcp_header.get_urgent(), false );
+    BOOST_CHECK_EQUAL( tcp_header.get_acknowledgment(), false );
+    BOOST_CHECK_EQUAL( tcp_header.get_push(), false );
+    BOOST_CHECK_EQUAL( tcp_header.get_reset(), false );
+    BOOST_CHECK_EQUAL( tcp_header.get_synchronize(), true );
+    BOOST_CHECK_EQUAL( tcp_header.get_finish(), false );
+    BOOST_CHECK_EQUAL( tcp_header.get_window_size(), 14600 );
+    BOOST_CHECK_EQUAL( tcp_header.get_checksum(), 0xF727 );
+}
+
+BOOST_AUTO_TEST_CASE( rst_received )
+{
+    uint8_t raw_segment_stream[] = {
+            0x00, 0x50, 0xe2, 0xc9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+            0x01, 0x50, 0x14, 0x00, 0x00, 0x1e, 0x34, 0x00, 0x00
+    };
+
+    std::basic_stringbuf<uint8_t> sb;
+    sb.pubsetbuf( raw_segment_stream, sizeof(raw_segment_stream) );
+    std::basic_istream<uint8_t> is( &sb );
+
+    TcpHeader tcp_header;
+    reinterpret_cast<std::istream&>(is) >> tcp_header;
+
+    BOOST_CHECK_EQUAL( tcp_header.get_source_port(), 80 );
+    BOOST_CHECK_EQUAL( tcp_header.get_destination_port(), 58057 );
+    BOOST_CHECK_EQUAL( tcp_header.get_sequence_number(), 0 ); // relative
+    BOOST_CHECK_EQUAL( tcp_header.get_acknowledgment_number(), 1 );
+    BOOST_CHECK_EQUAL( tcp_header.get_header_length(), 5 ); // 5 words = 20 bytes
+    BOOST_CHECK_EQUAL( tcp_header.get_congestion_window_reduced(), false );
+    BOOST_CHECK_EQUAL( tcp_header.get_ecn_echo(), false );
+    BOOST_CHECK_EQUAL( tcp_header.get_urgent(), false );
+    BOOST_CHECK_EQUAL( tcp_header.get_acknowledgment(), true );
+    BOOST_CHECK_EQUAL( tcp_header.get_push(), false );
+    BOOST_CHECK_EQUAL( tcp_header.get_reset(), true );
+    BOOST_CHECK_EQUAL( tcp_header.get_synchronize(), false );
+    BOOST_CHECK_EQUAL( tcp_header.get_finish(), false );
+    BOOST_CHECK_EQUAL( tcp_header.get_window_size(), 0 );
+    BOOST_CHECK_EQUAL( tcp_header.get_checksum(), 0x1E34 );
+}
+
+BOOST_AUTO_TEST_CASE( calculate_tcp_checksum_ipv4 )
+{
+    TcpHeader tcp_header;
+    tcp_header.set_source_port( 58057 );
+    tcp_header.set_destination_port( 80 );
+    tcp_header.set_sequence_number( 0x0001 ); // relative
+    tcp_header.set_acknowledgment_number( 0 );
+    tcp_header.set_header_length( 5 ); // 5 words = 20 bytes
+    tcp_header.set_congestion_window_reduced( false );
+    tcp_header.set_ecn_echo( false );
+    tcp_header.set_urgent( false );
+    tcp_header.set_acknowledgment( true );
+    tcp_header.set_push( false );
+    tcp_header.set_reset( false );
+    tcp_header.set_synchronize( false );
+    tcp_header.set_finish( false );
+    tcp_header.set_window_size( 32768 );
+
+    boost::asio::ip::address_v4 src_addr = boost::asio::ip::address_v4::from_string( "192.168.1.102" );
+    boost::asio::ip::address_v4 dst_addr = boost::asio::ip::address_v4::from_string( "200.147.35.224" );
+
+    uint16_t checksum = tcp_header.calculate_tcp_checksum(
+            src_addr.to_bytes(),
+            dst_addr.to_bytes(),
+            NULL, 0
+    );
+
+    BOOST_CHECK_EQUAL( checksum, 0x9E37 );
+}
+
+BOOST_AUTO_TEST_SUITE_END()