From ee48e2b240436fe743f606757d7ac8097358e232 Mon Sep 17 00:00:00 2001 From: Guilherme Maciel Ferreira Date: Tue, 10 Jan 2012 07:18:33 -0200 Subject: [PATCH] Test: bring aboard Icmpv4Header test case. --- test/CMakeLists.txt | 7 +++ test/test_icmpv4header.cpp | 107 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+), 0 deletions(-) create mode 100644 test/test_icmpv4header.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 79a5935..f6af8c6 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -25,6 +25,7 @@ set(TESTS_SOURCES test_messagepayload.cpp test_ipv4header.cpp test_ipv6header.cpp + test_icmpv4header.cpp ) # compiler: source code to be tested @@ -32,6 +33,12 @@ set(SOURCES ${CMAKE_SOURCE_DIR}/src/host/messagepayload.cpp ${CMAKE_SOURCE_DIR}/src/ip/ipv4header.cpp ${CMAKE_SOURCE_DIR}/src/ip/ipv6header.cpp + ${CMAKE_SOURCE_DIR}/src/icmp/icmpv4header.cpp + ${CMAKE_SOURCE_DIR}/src/icmp/icmpmessage.cpp + ${CMAKE_SOURCE_DIR}/src/icmp/icmpdestinationunreachablemessage.cpp + ${CMAKE_SOURCE_DIR}/src/icmp/icmpechoreplymessage.cpp + ${CMAKE_SOURCE_DIR}/src/icmp/icmpechorequestmessage.cpp + ${CMAKE_SOURCE_DIR}/src/icmp/icmpgenericmessage.cpp ) # compiler: creates the binary diff --git a/test/test_icmpv4header.cpp b/test/test_icmpv4header.cpp new file mode 100644 index 0000000..4fe3940 --- /dev/null +++ b/test/test_icmpv4header.cpp @@ -0,0 +1,107 @@ +/* +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 + +#include + +#include "icmp/icmptype.h" +#include "icmp/icmpv4header.h" + +BOOST_AUTO_TEST_SUITE( TestIcmpv4Header ) + +BOOST_AUTO_TEST_CASE( echo_reply ) +{ + uint8_t raw_packet_stream[] = { + 0x00, 0x00, 0x78, 0x7d, 0x09, 0x0f, 0x00, 0x01, 0xc7, 0x85, 0x0b, 0x4f, + 0xb3, 0x9a, 0x0d, 0x00, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, + 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, + 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, + 0x34, 0x35, 0x36, 0x37 + }; + + std::basic_stringbuf sb; + sb.pubsetbuf( raw_packet_stream, sizeof(raw_packet_stream) ); + std::basic_istream is( &sb ); + + Icmpv4Header icmp_header; + reinterpret_cast(is) >> icmp_header; + + BOOST_CHECK_EQUAL( icmp_header.get_type(), Icmpv4Type_EchoReply ); + BOOST_CHECK_EQUAL( icmp_header.get_code(), 0 ); + BOOST_CHECK_EQUAL( icmp_header.get_checksum(), 0x787D ); + BOOST_CHECK_EQUAL( icmp_header.get_identifier(), 0x090F ); + BOOST_CHECK_EQUAL( icmp_header.get_sequence_number(), 1 ); +} + +BOOST_AUTO_TEST_CASE( echo_request ) +{ + uint8_t raw_packet_stream[] = { + 0x08, 0x00, 0x28, 0xd1, 0x09, 0x21, 0x00, 0x02, 0xaa, 0x86, 0x0b, 0x4f, + 0x22, 0x33, 0x03, 0x00, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, + 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, + 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, + 0x34, 0x35, 0x36, 0x37 + }; + + std::basic_stringbuf sb; + sb.pubsetbuf( raw_packet_stream, sizeof(raw_packet_stream) ); + std::basic_istream is( &sb ); + + Icmpv4Header icmp_header; + reinterpret_cast(is) >> icmp_header; + + BOOST_CHECK_EQUAL( icmp_header.get_type(), Icmpv4Type_EchoRequest ); + BOOST_CHECK_EQUAL( icmp_header.get_code(), 0 ); + BOOST_CHECK_EQUAL( icmp_header.get_checksum(), 0x28D1 ); + BOOST_CHECK_EQUAL( icmp_header.get_identifier(), 0x0921 ); + BOOST_CHECK_EQUAL( icmp_header.get_sequence_number(), 2 ); +} + +BOOST_AUTO_TEST_CASE( destination_unreachable ) +{ + uint8_t raw_packet_stream[] = { + 0x03, 0x01, 0xfc, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x45, 0x00, 0x00, 0x54, + 0x00, 0x00, 0x40, 0x00, 0x40, 0x01, 0xf9, 0x99, 0xc0, 0xa8, 0x01, 0x66, + 0x7f, 0x00, 0x00, 0x01, 0x08, 0x00, 0x37, 0xd4, 0x0b, 0x6e, 0x00, 0x02, + 0xef, 0x8b, 0x0b, 0x4f, 0xc5, 0xdd, 0x09, 0x00, 0x08, 0x09, 0x0a, 0x0b, + 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, + 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37 + }; + + std::basic_stringbuf sb; + sb.pubsetbuf( raw_packet_stream, sizeof(raw_packet_stream) ); + std::basic_istream is( &sb ); + + Icmpv4Header icmp_header; + reinterpret_cast(is) >> icmp_header; + + BOOST_CHECK_EQUAL( icmp_header.get_type(), Icmpv4Type_DestinationUnreachable ); + BOOST_CHECK_EQUAL( icmp_header.get_code(), 1 ); + BOOST_CHECK_EQUAL( icmp_header.get_checksum(), 0xFCFE ); +} + +BOOST_AUTO_TEST_SUITE_END() -- 1.7.1