--- /dev/null
+/*
+ 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)
+