2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
8 As a special exception, if other files instantiate templates or use macros
9 or inline functions from this file, or you compile this file and link it
10 with other works to produce a work based on this file, this file
11 does not by itself cause the resulting work to be covered
12 by the GNU General Public License.
14 However the source code for this file must still be made available
15 in accordance with section (3) of the GNU General Public License.
17 This exception does not invalidate any other reasons why a work based
18 on this file might be covered by the GNU General Public License.
21 #define BOOST_TEST_MAIN
22 #define BOOST_TEST_DYN_LINK
26 #include <boost/test/unit_test.hpp>
28 #include "ip/ipv4header.h"
30 BOOST_AUTO_TEST_SUITE( TestIpv4Header )
32 BOOST_AUTO_TEST_CASE( icmp_packet )
34 uint8_t raw_packet_stream[] = {
35 0x45, 0x00, 0x00, 0x54, 0x0f, 0x12, 0x00, 0x00, 0x33, 0x01, 0xea,
36 0x2e, 0xc8, 0x93, 0x03, 0xc7, 0xc0, 0xa8, 0x01, 0x66
39 std::basic_stringbuf<uint8_t> sb;
40 sb.pubsetbuf( raw_packet_stream, sizeof(raw_packet_stream) );
41 std::basic_istream<uint8_t> is( &sb );
44 reinterpret_cast<std::istream&>(is) >> ip_header;
46 BOOST_CHECK_EQUAL( ip_header.get_version(), 4 );
47 BOOST_CHECK_EQUAL( ip_header.get_header_length(), 20 );
48 BOOST_CHECK_EQUAL( ip_header.get_total_length(), 84 );
49 BOOST_CHECK_EQUAL( ip_header.get_identification(), 0x0F12 );
50 BOOST_CHECK_EQUAL( ip_header.dont_fragment(), false );
51 BOOST_CHECK_EQUAL( ip_header.more_fragments(), false );
52 BOOST_CHECK_EQUAL( ip_header.get_fragment_offset(), 0 );
53 BOOST_CHECK_EQUAL( ip_header.get_time_to_live(), 51 );
54 BOOST_CHECK_EQUAL( ip_header.get_protocol(), 0x01 ); // ICMP
55 BOOST_CHECK_EQUAL( ip_header.get_header_checksum(), 0xEA2E );
56 BOOST_CHECK_EQUAL( ip_header.get_source_address(), boost::asio::ip::address_v4::from_string("200.147.3.199") );
57 BOOST_CHECK_EQUAL( ip_header.get_destination_address(), boost::asio::ip::address_v4::from_string("192.168.1.102") );
60 BOOST_AUTO_TEST_CASE( tcp_packet )
62 uint8_t raw_packet_stream[] = {
63 0x45, 0x00, 0x00, 0x34, 0x93, 0x2a, 0x40, 0x00, 0x40, 0x06, 0x08,
64 0x57, 0xc0, 0xa8, 0x01, 0x66, 0x08, 0x14, 0xd5, 0x20
67 std::basic_stringbuf<uint8_t> sb;
68 sb.pubsetbuf( raw_packet_stream, sizeof(raw_packet_stream) );
69 std::basic_istream<uint8_t> is( &sb );
72 reinterpret_cast<std::istream&>(is) >> ip_header;
74 BOOST_CHECK_EQUAL( ip_header.get_version(), 4 );
75 BOOST_CHECK_EQUAL( ip_header.get_header_length(), 20 );
76 BOOST_CHECK_EQUAL( ip_header.get_total_length(), 52 );
77 BOOST_CHECK_EQUAL( ip_header.get_identification(), 0x932A );
78 BOOST_CHECK_EQUAL( ip_header.dont_fragment(), true );
79 BOOST_CHECK_EQUAL( ip_header.more_fragments(), false );
80 BOOST_CHECK_EQUAL( ip_header.get_fragment_offset(), 0 );
81 BOOST_CHECK_EQUAL( ip_header.get_time_to_live(), 64 );
82 BOOST_CHECK_EQUAL( ip_header.get_protocol(), 0x06 ); // TCP
83 BOOST_CHECK_EQUAL( ip_header.get_header_checksum(), 0x0857 );
84 BOOST_CHECK_EQUAL( ip_header.get_source_address(), boost::asio::ip::address_v4::from_string("192.168.1.102") );
85 BOOST_CHECK_EQUAL( ip_header.get_destination_address(), boost::asio::ip::address_v4::from_string( "8.20.213.32" ) );
88 BOOST_AUTO_TEST_CASE( udp_packet )
90 uint8_t raw_packet_stream[] = {
91 0x45, 0x00, 0x00, 0x49, 0x1c, 0xd9, 0x40, 0x00, 0x40, 0x11, 0x9a,
92 0x13, 0xc0, 0xa8, 0x01, 0x66, 0xc0, 0xa8, 0x01, 0x01
95 std::basic_stringbuf<uint8_t> sb;
96 sb.pubsetbuf( raw_packet_stream, sizeof(raw_packet_stream) );
97 std::basic_istream<uint8_t> is( &sb );
100 reinterpret_cast<std::istream&>(is) >> ip_header;
102 BOOST_CHECK_EQUAL( ip_header.get_version(), 4 );
103 BOOST_CHECK_EQUAL( ip_header.get_header_length(), 20 );
104 BOOST_CHECK_EQUAL( ip_header.get_total_length(), 73 );
105 BOOST_CHECK_EQUAL( ip_header.get_identification(), 0x1CD9 );
106 BOOST_CHECK_EQUAL( ip_header.dont_fragment(), true );
107 BOOST_CHECK_EQUAL( ip_header.more_fragments(), false );
108 BOOST_CHECK_EQUAL( ip_header.get_fragment_offset(), 0 );
109 BOOST_CHECK_EQUAL( ip_header.get_time_to_live(), 64 );
110 BOOST_CHECK_EQUAL( ip_header.get_protocol(), 0x11 ); // UDP
111 BOOST_CHECK_EQUAL( ip_header.get_header_checksum(), 0x9A13 );
112 BOOST_CHECK_EQUAL( ip_header.get_source_address(), boost::asio::ip::address_v4::from_string("192.168.1.102") );
113 BOOST_CHECK_EQUAL( ip_header.get_destination_address(), boost::asio::ip::address_v4::from_string( "192.168.1.1" ) );
116 BOOST_AUTO_TEST_SUITE_END()