Update pingcheck to work with cmake 3.28
[pingcheck] / test / test_ipv4header.cpp
1 /*
2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
4
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
7
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.
13
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.
16
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.
19 */
20
21 #define BOOST_TEST_MAIN
22 #define BOOST_TEST_DYN_LINK
23
24 #include <streambuf>
25
26 #include <boost/test/unit_test.hpp>
27
28 #include "ip/ipv4header.h"
29
30 BOOST_AUTO_TEST_SUITE( TestIpv4Header )
31
32 BOOST_AUTO_TEST_CASE( icmp_packet )
33 {
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
37     };
38
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 );
42
43     Ipv4Header ip_header;
44     reinterpret_cast<std::istream&>(is) >> ip_header;
45
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") );
58 }
59
60 BOOST_AUTO_TEST_CASE( tcp_packet )
61 {
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
65     };
66
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 );
70
71     Ipv4Header ip_header;
72     reinterpret_cast<std::istream&>(is) >> ip_header;
73
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" ) );
86 }
87
88 BOOST_AUTO_TEST_CASE( udp_packet )
89 {
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
93     };
94
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 );
98
99     Ipv4Header ip_header;
100     reinterpret_cast<std::istream&>(is) >> ip_header;
101
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" ) );
114 }
115
116 BOOST_AUTO_TEST_SUITE_END()