Update pingcheck to work with cmake 3.28
[pingcheck] / test / test_tcpheader.cpp
CommitLineData
c41527cd
GMF
1/*
2The software in this package is distributed under the GNU General
3Public License version 2 (with a special exception described below).
4
5A copy of GNU General Public License (GPL) is included in this distribution,
6in the file COPYING.GPL.
7
8As a special exception, if other files instantiate templates or use macros
9or inline functions from this file, or you compile this file and link it
10with other works to produce a work based on this file, this file
11does not by itself cause the resulting work to be covered
12by the GNU General Public License.
13
14However the source code for this file must still be made available
15in accordance with section (3) of the GNU General Public License.
16
17This exception does not invalidate any other reasons why a work based
18on 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/asio.hpp>
27#include <boost/test/unit_test.hpp>
28
29#include "tcp/tcpheader.h"
30
31BOOST_AUTO_TEST_SUITE( TestTcpHeader )
32
33BOOST_AUTO_TEST_CASE( ack_received )
34{
35 uint8_t raw_segment_stream[] = {
36 0x9f, 0x70, 0x00, 0x50, 0x58, 0xb6, 0x52, 0x49, 0x00, 0x00, 0x00, 0x00,
37 0xa0, 0x02, 0x39, 0x08, 0xf7, 0x27, 0x00, 0x00, 0x02, 0x04, 0x05, 0xb4,
38 0x04, 0x02, 0x08, 0x0a, 0x00, 0x05, 0x59, 0x20, 0x00, 0x00, 0x00, 0x00,
39 0x01, 0x03, 0x03, 0x06
40 };
41
42 std::basic_stringbuf<uint8_t> sb;
43 sb.pubsetbuf( raw_segment_stream, sizeof(raw_segment_stream) );
44 std::basic_istream<uint8_t> is( &sb );
45
46 TcpHeader tcp_header;
47 reinterpret_cast<std::istream&>(is) >> tcp_header;
48
49 BOOST_CHECK_EQUAL( tcp_header.get_source_port(), 40816 );
50 BOOST_CHECK_EQUAL( tcp_header.get_destination_port(), 80 );
51 BOOST_CHECK_EQUAL( tcp_header.get_acknowledgment_number(), 0 );
52 BOOST_CHECK_EQUAL( tcp_header.get_header_length(), 10 ); // 10 words = 40 bytes
53 BOOST_CHECK_EQUAL( tcp_header.get_congestion_window_reduced(), false );
54 BOOST_CHECK_EQUAL( tcp_header.get_ecn_echo(), false );
55 BOOST_CHECK_EQUAL( tcp_header.get_urgent(), false );
56 BOOST_CHECK_EQUAL( tcp_header.get_acknowledgment(), false );
57 BOOST_CHECK_EQUAL( tcp_header.get_push(), false );
58 BOOST_CHECK_EQUAL( tcp_header.get_reset(), false );
59 BOOST_CHECK_EQUAL( tcp_header.get_synchronize(), true );
60 BOOST_CHECK_EQUAL( tcp_header.get_finish(), false );
61 BOOST_CHECK_EQUAL( tcp_header.get_window_size(), 14600 );
62 BOOST_CHECK_EQUAL( tcp_header.get_checksum(), 0xF727 );
63}
64
65BOOST_AUTO_TEST_CASE( rst_received )
66{
67 uint8_t raw_segment_stream[] = {
68 0x00, 0x50, 0xe2, 0xc9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
69 0x01, 0x50, 0x14, 0x00, 0x00, 0x1e, 0x34, 0x00, 0x00
70 };
71
72 std::basic_stringbuf<uint8_t> sb;
73 sb.pubsetbuf( raw_segment_stream, sizeof(raw_segment_stream) );
74 std::basic_istream<uint8_t> is( &sb );
75
76 TcpHeader tcp_header;
77 reinterpret_cast<std::istream&>(is) >> tcp_header;
78
79 BOOST_CHECK_EQUAL( tcp_header.get_source_port(), 80 );
80 BOOST_CHECK_EQUAL( tcp_header.get_destination_port(), 58057 );
81 BOOST_CHECK_EQUAL( tcp_header.get_sequence_number(), 0 ); // relative
82 BOOST_CHECK_EQUAL( tcp_header.get_acknowledgment_number(), 1 );
83 BOOST_CHECK_EQUAL( tcp_header.get_header_length(), 5 ); // 5 words = 20 bytes
84 BOOST_CHECK_EQUAL( tcp_header.get_congestion_window_reduced(), false );
85 BOOST_CHECK_EQUAL( tcp_header.get_ecn_echo(), false );
86 BOOST_CHECK_EQUAL( tcp_header.get_urgent(), false );
87 BOOST_CHECK_EQUAL( tcp_header.get_acknowledgment(), true );
88 BOOST_CHECK_EQUAL( tcp_header.get_push(), false );
89 BOOST_CHECK_EQUAL( tcp_header.get_reset(), true );
90 BOOST_CHECK_EQUAL( tcp_header.get_synchronize(), false );
91 BOOST_CHECK_EQUAL( tcp_header.get_finish(), false );
92 BOOST_CHECK_EQUAL( tcp_header.get_window_size(), 0 );
93 BOOST_CHECK_EQUAL( tcp_header.get_checksum(), 0x1E34 );
94}
95
96BOOST_AUTO_TEST_CASE( calculate_tcp_checksum_ipv4 )
97{
98 TcpHeader tcp_header;
99 tcp_header.set_source_port( 58057 );
100 tcp_header.set_destination_port( 80 );
101 tcp_header.set_sequence_number( 0x0001 ); // relative
102 tcp_header.set_acknowledgment_number( 0 );
103 tcp_header.set_header_length( 5 ); // 5 words = 20 bytes
104 tcp_header.set_congestion_window_reduced( false );
105 tcp_header.set_ecn_echo( false );
106 tcp_header.set_urgent( false );
107 tcp_header.set_acknowledgment( true );
108 tcp_header.set_push( false );
109 tcp_header.set_reset( false );
110 tcp_header.set_synchronize( false );
111 tcp_header.set_finish( false );
112 tcp_header.set_window_size( 32768 );
113
114 boost::asio::ip::address_v4 src_addr = boost::asio::ip::address_v4::from_string( "192.168.1.102" );
115 boost::asio::ip::address_v4 dst_addr = boost::asio::ip::address_v4::from_string( "200.147.35.224" );
116
117 uint16_t checksum = tcp_header.calculate_tcp_checksum(
118 src_addr.to_bytes(),
119 dst_addr.to_bytes(),
120 NULL, 0
121 );
122
123 BOOST_CHECK_EQUAL( checksum, 0x9E37 );
124}
125
126BOOST_AUTO_TEST_SUITE_END()