From 1669ea2674c640789608a1134eae166a3214b8ed Mon Sep 17 00:00:00 2001 From: Christian Herdtweck Date: Wed, 18 Mar 2015 19:05:48 +0100 Subject: [PATCH] added new unittest for IcmpPacket --- test/CMakeLists.test_icmppacket.txt | 16 ++++ test/CMakeLists.txt | 2 + test/test_icmppacket.cpp | 123 +++++++++++++++++++++++++++++++++ test/test_icmppacket_echorequest.pcap | Bin 0 -> 94 bytes 4 files changed, 141 insertions(+), 0 deletions(-) create mode 100644 test/CMakeLists.test_icmppacket.txt create mode 100644 test/test_icmppacket.cpp create mode 100644 test/test_icmppacket_echorequest.pcap diff --git a/test/CMakeLists.test_icmppacket.txt b/test/CMakeLists.test_icmppacket.txt new file mode 100644 index 0000000..4f548b9 --- /dev/null +++ b/test/CMakeLists.test_icmppacket.txt @@ -0,0 +1,16 @@ +# compiler: creates the binaries +add_executable(test_icmppacket + test_icmppacket.cpp + ${CMAKE_SOURCE_DIR}/src/boost_assert_handler.cpp + ${CMAKE_SOURCE_DIR}/src/icmp/icmppacket.cpp +) + +# linker: link the program against the libraries +target_link_libraries( + test_icmppacket + ${I2NCOMMON_LIBRARIES} + ${Boost_LIBRARIES} +) + +# cmake: invocation via "make test" +add_test(test_icmppacket test_icmppacket) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index bf5eee0..3351090 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -37,6 +37,7 @@ include(CMakeLists.test_ipv4header.txt) include(CMakeLists.test_ipv6header.txt) include(CMakeLists.test_icmpv4header.txt) include(CMakeLists.test_icmpv6header.txt) +include(CMakeLists.test_icmppacket.txt) include(CMakeLists.test_tcpheader.txt) # cmake: add a custom "make check" target which automatically builds the binary @@ -53,5 +54,6 @@ add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} DEPENDS test_ipv6header test_icmpv4header test_icmpv6header + test_icmppacket test_tcpheader ) diff --git a/test/test_icmppacket.cpp b/test/test_icmppacket.cpp new file mode 100644 index 0000000..2b6dde2 --- /dev/null +++ b/test/test_icmppacket.cpp @@ -0,0 +1,123 @@ +/* + 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. + + Christian Herdtweck, Intra2net AG 2015 + */ + +#define BOOST_TEST_MAIN +#define BOOST_TEST_DYN_LINK + +#include +#include +#include +#include + +#include "icmp/icmppacketfactory.h" +#include "icmp/icmptype.h" + +//------------------------------------------------------------------------------ +// helper function and consts +//------------------------------------------------------------------------------ + +bool check_and_consume_pcap_headers(std::istream &input_stream); +IcmpPacketItem read_packet(const std::string &file_name); + +const uint8_t pcap_magic_bytes[] = {0xa1, 0xb2, 0xc3, 0xd4}; +const std::streamsize n_magic_bytes = 4; +const std::streamsize pcap_file_header_size = 24; +const std::streamsize pcap_packet_header_size = 16; + +const int DUMP_MODE_NO_DUMP = 0; + +bool check_and_consume_pcap_headers(std::ifstream &input_stream) +{ + int read_idx; + for (read_idx=0; read_idx<4; ++read_idx) + { + int val = input_stream.get(); + if (static_cast(val) != pcap_magic_bytes[3-read_idx]) + return false; + } + + std::streamsize to_consume = pcap_file_header_size + pcap_packet_header_size + - n_magic_bytes; + char *buf = new char[to_consume]; + input_stream.read(buf, to_consume); + delete[] buf; + + return true; +} + +IcmpPacketItem read_packet(const std::string &file_name) +{ + std::ifstream file_stream(file_name.c_str(), std::ios::in | + std::ios::binary); + // could open? + BOOST_REQUIRE( file_stream ); + + // recognized pcap file? + bool is_pcap = check_and_consume_pcap_headers(file_stream); + if ( !is_pcap ) + { + file_stream.close(); + BOOST_FAIL( "Failed to recognize/consume pcap header!" ); + } + + IcmpPacketItem packet = IcmpPacketFactory::create_icmp_packet( + boost::asio::ip::icmp::v4(), file_stream, DUMP_MODE_NO_DUMP); + file_stream.close(); + + // managed to create packet from rest of file contents? + BOOST_REQUIRE( packet ); + //BOOST_REQUIRE( packet->check_integrity() ); not implemented yet + return packet; +} + +//------------------------------------------------------------------------------ +// TEST SUITE +//------------------------------------------------------------------------------ + +BOOST_AUTO_TEST_SUITE( TestIcmpPacket ) + +// just read a pcap file to see if helpers work +BOOST_AUTO_TEST_CASE( TestInternal ) +{ + std::string file_name = "test_icmppacket_echorequest.pcap"; + IcmpPacketItem packet = read_packet(file_name); + BOOST_CHECK( packet ); +} + + +BOOST_AUTO_TEST_CASE( EchoRequest ) +{ + std::string file_name = "test_icmppacket_echorequest.pcap"; + IcmpPacketItem packet = read_packet(file_name); + BOOST_REQUIRE( packet ); + BOOST_CHECK_EQUAL( packet->get_type_v4(), Icmpv4Type_EchoRequest ); + BOOST_CHECK_EQUAL( packet->get_source_address().to_string(), + "172.16.1.141"); + BOOST_CHECK_EQUAL( packet->get_destination_address().to_string(), + "38.100.128.10"); + BOOST_CHECK_EQUAL( packet->get_icmp_code(), 0 ); + BOOST_CHECK_EQUAL( packet->get_icmp_size(), 20 ); +} + +BOOST_AUTO_TEST_SUITE_END() +// (created using vim -- the world's best text editor) + diff --git a/test/test_icmppacket_echorequest.pcap b/test/test_icmppacket_echorequest.pcap new file mode 100644 index 0000000000000000000000000000000000000000..bd34339aedf65095761ef6900f95b824b71e397a GIT binary patch literal 94 zcmca|c+)~A1{MYw`2U}Qff2}=w1YFWV*?9=8ITRaEhf_i8~!kXl!b9HxH2$kaHTsi lI56HxS|h;NtCrHh#li5$f4vz4V?kzKx^8Z2adBdLDggdT7jOUo literal 0 HcmV?d00001 -- 1.7.1