added new unittest for IcmpPacket
authorChristian Herdtweck <christian.herdtweck@intra2net.com>
Wed, 18 Mar 2015 18:05:48 +0000 (19:05 +0100)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Wed, 18 Mar 2015 18:05:48 +0000 (19:05 +0100)
test/CMakeLists.test_icmppacket.txt [new file with mode: 0644]
test/CMakeLists.txt
test/test_icmppacket.cpp [new file with mode: 0644]
test/test_icmppacket_echorequest.pcap [new file with mode: 0644]

diff --git a/test/CMakeLists.test_icmppacket.txt b/test/CMakeLists.test_icmppacket.txt
new file mode 100644 (file)
index 0000000..4f548b9
--- /dev/null
@@ -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)
index bf5eee0..3351090 100644 (file)
@@ -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 (file)
index 0000000..2b6dde2
--- /dev/null
@@ -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 <fstream>
+#include <ios>
+#include <boost/test/unit_test.hpp>
+#include <boost/asio.hpp>
+
+#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<uint8_t>(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 (file)
index 0000000..bd34339
Binary files /dev/null and b/test/test_icmppacket_echorequest.pcap differ