--- /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.
+*/
+
+#define BOOST_TEST_DYN_LINK
+
+#include <streambuf>
+
+#include <boost/test/unit_test.hpp>
+
+#include "ip/ipv4header.h"
+
+BOOST_AUTO_TEST_SUITE( TestIpv4Header )
+
+BOOST_AUTO_TEST_CASE( icmp_packet )
+{
+ uint8_t raw_packet_stream[] = {
+ 0x45, 0x00, 0x00, 0x54, 0x0f, 0x12, 0x00, 0x00, 0x33, 0x01, 0xea,
+ 0x2e, 0xc8, 0x93, 0x03, 0xc7, 0xc0, 0xa8, 0x01, 0x66
+ };
+
+ std::basic_stringbuf<uint8_t> sb;
+ sb.pubsetbuf( raw_packet_stream, sizeof(raw_packet_stream) );
+ std::basic_istream<uint8_t> is( &sb );
+
+ Ipv4Header ip_header;
+ reinterpret_cast<std::istream&>(is) >> ip_header;
+
+ BOOST_CHECK_EQUAL( ip_header.get_version(), 4 );
+ BOOST_CHECK_EQUAL( ip_header.get_header_length(), 20 );
+ BOOST_CHECK_EQUAL( ip_header.get_total_length(), 84 );
+ BOOST_CHECK_EQUAL( ip_header.get_identification(), 0x0F12 );
+ BOOST_CHECK_EQUAL( ip_header.dont_fragment(), false );
+ BOOST_CHECK_EQUAL( ip_header.more_fragments(), false );
+ BOOST_CHECK_EQUAL( ip_header.get_fragment_offset(), 0 );
+ BOOST_CHECK_EQUAL( ip_header.get_time_to_live(), 51 );
+ BOOST_CHECK_EQUAL( ip_header.get_protocol(), 0x01 ); // ICMP
+ BOOST_CHECK_EQUAL( ip_header.get_header_checksum(), 0xEA2E );
+ BOOST_CHECK_EQUAL( ip_header.get_source_address(), boost::asio::ip::address_v4::from_string("200.147.3.199") );
+ BOOST_CHECK_EQUAL( ip_header.get_destination_address(), boost::asio::ip::address_v4::from_string("192.168.1.102") );
+}
+
+BOOST_AUTO_TEST_CASE( tcp_packet )
+{
+ uint8_t raw_packet_stream[] = {
+ 0x45, 0x00, 0x00, 0x34, 0x93, 0x2a, 0x40, 0x00, 0x40, 0x06, 0x08,
+ 0x57, 0xc0, 0xa8, 0x01, 0x66, 0x08, 0x14, 0xd5, 0x20
+ };
+
+ std::basic_stringbuf<uint8_t> sb;
+ sb.pubsetbuf( raw_packet_stream, sizeof(raw_packet_stream) );
+ std::basic_istream<uint8_t> is( &sb );
+
+ Ipv4Header ip_header;
+ reinterpret_cast<std::istream&>(is) >> ip_header;
+
+ BOOST_CHECK_EQUAL( ip_header.get_version(), 4 );
+ BOOST_CHECK_EQUAL( ip_header.get_header_length(), 20 );
+ BOOST_CHECK_EQUAL( ip_header.get_total_length(), 52 );
+ BOOST_CHECK_EQUAL( ip_header.get_identification(), 0x932A );
+ BOOST_CHECK_EQUAL( ip_header.dont_fragment(), true );
+ BOOST_CHECK_EQUAL( ip_header.more_fragments(), false );
+ BOOST_CHECK_EQUAL( ip_header.get_fragment_offset(), 0 );
+ BOOST_CHECK_EQUAL( ip_header.get_time_to_live(), 64 );
+ BOOST_CHECK_EQUAL( ip_header.get_protocol(), 0x06 ); // TCP
+ BOOST_CHECK_EQUAL( ip_header.get_header_checksum(), 0x0857 );
+ BOOST_CHECK_EQUAL( ip_header.get_source_address(), boost::asio::ip::address_v4::from_string("192.168.1.102") );
+ BOOST_CHECK_EQUAL( ip_header.get_destination_address(), boost::asio::ip::address_v4::from_string( "8.20.213.32" ) );
+}
+
+BOOST_AUTO_TEST_CASE( udp_packet )
+{
+ uint8_t raw_packet_stream[] = {
+ 0x45, 0x00, 0x00, 0x49, 0x1c, 0xd9, 0x40, 0x00, 0x40, 0x11, 0x9a,
+ 0x13, 0xc0, 0xa8, 0x01, 0x66, 0xc0, 0xa8, 0x01, 0x01
+ };
+
+ std::basic_stringbuf<uint8_t> sb;
+ sb.pubsetbuf( raw_packet_stream, sizeof(raw_packet_stream) );
+ std::basic_istream<uint8_t> is( &sb );
+
+ Ipv4Header ip_header;
+ reinterpret_cast<std::istream&>(is) >> ip_header;
+
+ BOOST_CHECK_EQUAL( ip_header.get_version(), 4 );
+ BOOST_CHECK_EQUAL( ip_header.get_header_length(), 20 );
+ BOOST_CHECK_EQUAL( ip_header.get_total_length(), 73 );
+ BOOST_CHECK_EQUAL( ip_header.get_identification(), 0x1CD9 );
+ BOOST_CHECK_EQUAL( ip_header.dont_fragment(), true );
+ BOOST_CHECK_EQUAL( ip_header.more_fragments(), false );
+ BOOST_CHECK_EQUAL( ip_header.get_fragment_offset(), 0 );
+ BOOST_CHECK_EQUAL( ip_header.get_time_to_live(), 64 );
+ BOOST_CHECK_EQUAL( ip_header.get_protocol(), 0x11 ); // UDP
+ BOOST_CHECK_EQUAL( ip_header.get_header_checksum(), 0x9A13 );
+ BOOST_CHECK_EQUAL( ip_header.get_source_address(), boost::asio::ip::address_v4::from_string("192.168.1.102") );
+ BOOST_CHECK_EQUAL( ip_header.get_destination_address(), boost::asio::ip::address_v4::from_string( "192.168.1.1" ) );
+}
+
+BOOST_AUTO_TEST_SUITE_END()