From c41527cdfa104c7c9e62d8d4e3dc57c62d5bfb73 Mon Sep 17 00:00:00 2001 From: Guilherme Maciel Ferreira Date: Sat, 18 Feb 2012 00:46:14 -0200 Subject: [PATCH] Test: bring aboard TcpHeader test case. --- test/CMakeLists.test_tcpheader.txt | 16 +++++ test/CMakeLists.txt | 1 + test/test_tcpheader.cpp | 126 ++++++++++++++++++++++++++++++++++++ 3 files changed, 143 insertions(+), 0 deletions(-) create mode 100644 test/CMakeLists.test_tcpheader.txt create mode 100644 test/test_tcpheader.cpp diff --git a/test/CMakeLists.test_tcpheader.txt b/test/CMakeLists.test_tcpheader.txt new file mode 100644 index 0000000..3df0853 --- /dev/null +++ b/test/CMakeLists.test_tcpheader.txt @@ -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) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index d8a020d..885455e 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -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 index 0000000..44a2977 --- /dev/null +++ b/test/test_tcpheader.cpp @@ -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 + +#include +#include + +#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 sb; + sb.pubsetbuf( raw_segment_stream, sizeof(raw_segment_stream) ); + std::basic_istream is( &sb ); + + TcpHeader tcp_header; + reinterpret_cast(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 sb; + sb.pubsetbuf( raw_segment_stream, sizeof(raw_segment_stream) ); + std::basic_istream is( &sb ); + + TcpHeader tcp_header; + reinterpret_cast(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() -- 1.7.1