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.
20 Christian Herdtweck, Intra2net AG 2015
23 #define BOOST_TEST_MAIN
24 #define BOOST_TEST_DYN_LINK
29 #include <boost/test/unit_test.hpp>
30 #include <boost/asio.hpp>
32 #include "icmp/icmppacketfactory.h"
33 #include "icmp/icmptype.h"
34 #include "tools/pcap.h"
36 //------------------------------------------------------------------------------
37 // helper function and consts
38 //------------------------------------------------------------------------------
40 const int DUMP_MODE_NO_DUMP = 0;
42 IcmpPacketItem read_packet(const std::string &file_name);
45 IcmpPacketItem read_packet(const std::string &file_name)
47 std::stringstream full_name;
48 full_name << DATA_DIR_STRING << "/" << file_name;
49 BOOST_TEST_MESSAGE( "Opening file " << full_name.str() );
50 std::ifstream file_stream(full_name.str().c_str(), std::ios::in |
53 BOOST_REQUIRE( file_stream );
55 // recognized pcap file?
56 bool is_pcap = consume_single_packet_pcap(file_stream);
60 BOOST_FAIL( "Failed to recognize/consume pcap header!" );
63 IcmpPacketItem packet = IcmpPacketFactory::create_icmp_packet(
64 boost::asio::ip::icmp::v4(), file_stream, DUMP_MODE_NO_DUMP);
67 // managed to create packet from rest of file contents?
68 BOOST_REQUIRE( packet );
69 //BOOST_REQUIRE( packet->check_integrity() ); not implemented yet
73 //------------------------------------------------------------------------------
75 //------------------------------------------------------------------------------
77 BOOST_AUTO_TEST_SUITE( TestIcmpPacket )
79 BOOST_AUTO_TEST_CASE( EchoRequest )
81 std::string request_file = "icmp_echorequest.pcap";
84 IcmpPacketItem packet = read_packet(request_file);
85 BOOST_REQUIRE( packet );
86 BOOST_CHECK_EQUAL( packet->get_type_v4(), Icmpv4Type_EchoRequest );
87 BOOST_CHECK_EQUAL( packet->get_source_address().to_string(),
89 BOOST_CHECK_EQUAL( packet->get_destination_address().to_string(),
91 BOOST_CHECK_EQUAL( packet->get_icmp_code(), 0 );
92 BOOST_CHECK_EQUAL( packet->get_icmp_size(), 20 );
96 BOOST_AUTO_TEST_CASE( EchoReply )
98 std::string reply_file = "icmp_echoreply.pcap";
101 IcmpPacketItem packet = read_packet(reply_file);
102 BOOST_REQUIRE( packet );
103 BOOST_CHECK_EQUAL( packet->get_type_v4(), Icmpv4Type_EchoReply );
104 BOOST_CHECK_EQUAL( packet->get_source_address().to_string(),
106 BOOST_CHECK_EQUAL( packet->get_destination_address().to_string(),
108 BOOST_CHECK_EQUAL( packet->get_icmp_code(), 0 );
109 BOOST_CHECK_EQUAL( packet->get_icmp_size(), 20 );
110 BOOST_CHECK( packet->match_echo_reply(0xaf36, 1,
111 boost::asio::ip::address_v4::from_string("38.100.128.10")) );
114 BOOST_AUTO_TEST_CASE( TimeExceeded )
116 std::string file_name = "icmp_timeexceeded.pcap";
119 IcmpPacketItem packet = read_packet(file_name);
120 BOOST_REQUIRE( packet );
121 BOOST_CHECK_EQUAL( packet->get_type_v4(), Icmpv4Type_TimeExceeded );
122 BOOST_CHECK_EQUAL( packet->get_source_address().to_string(),
124 BOOST_CHECK_EQUAL( packet->get_destination_address().to_string(),
126 BOOST_CHECK_EQUAL( packet->get_icmp_code(), 0 );
127 BOOST_CHECK_EQUAL( packet->get_icmp_size(), 48 );
128 BOOST_CHECK( packet->match_time_exceeded(0x4ae3, 1,
129 boost::asio::ip::address_v4::from_string("216.218.186.2")) );
132 BOOST_AUTO_TEST_CASE( DestinationUnreachable )
134 std::string file_name = "icmp_destinationunreachable.pcap";
137 IcmpPacketItem packet = read_packet(file_name);
138 BOOST_REQUIRE( packet );
139 BOOST_CHECK_EQUAL( packet->get_type_v4(), Icmpv4Type_DestinationUnreachable );
140 BOOST_CHECK_EQUAL( packet->get_source_address().to_string(),
142 BOOST_CHECK_EQUAL( packet->get_destination_address().to_string(),
144 BOOST_CHECK_EQUAL( packet->get_icmp_code(), 3 );
145 BOOST_CHECK_EQUAL( packet->get_icmp_size(), 92 );
146 BOOST_CHECK( packet->match_destination_unreachable(0x077b, 1,
147 boost::asio::ip::address_v4::from_string("85.214.66.2")) );
150 BOOST_AUTO_TEST_SUITE_END()
151 // (created using vim -- the world's best text editor)