/* 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. Christian Herdtweck, Intra2net AG 2015 */ #define BOOST_TEST_MAIN #define BOOST_TEST_DYN_LINK #include #include #include #include #include #include "icmp/icmppacketfactory.h" #include "icmp/icmptype.h" #include "tools/pcap.h" //------------------------------------------------------------------------------ // helper function //------------------------------------------------------------------------------ IcmpPacketItem read_packet(const std::string &file_name); IcmpPacketItem read_packet(const std::string &file_name) { std::stringstream full_name; full_name << DATA_DIR_STRING << "/" << file_name; BOOST_TEST_MESSAGE( "Opening file " << full_name.str() ); std::ifstream file_stream(full_name.str().c_str(), std::ios::in | std::ios::binary); // could open? BOOST_REQUIRE( file_stream ); // recognized pcap file? bool is_pcap = consume_single_packet_pcap(file_stream); if ( !is_pcap ) { file_stream.close(); BOOST_FAIL( "Failed to recognize/consume pcap header!" ); } IcmpPacketFactory::PacketDumpMode = DUMP_NEVER; IcmpPacketItem packet = IcmpPacketFactory::create_icmp_packet( boost::asio::ip::icmp::v4(), file_stream); file_stream.close(); // managed to create packet from rest of file contents? BOOST_REQUIRE( packet ); //BOOST_REQUIRE( packet->check_integrity() ); not implemented yet return packet; } //------------------------------------------------------------------------------ // TEST SUITE //------------------------------------------------------------------------------ BOOST_AUTO_TEST_SUITE( TestIcmpPacket ) BOOST_AUTO_TEST_CASE( EchoRequest ) { std::string request_file = "icmp_echorequest.pcap"; // read request file IcmpPacketItem packet = read_packet(request_file); BOOST_REQUIRE( packet ); BOOST_CHECK_EQUAL( packet->get_type_v4(), Icmpv4Type_EchoRequest ); BOOST_CHECK_EQUAL( packet->get_source_address().to_string(), "11.22.33.44"); BOOST_CHECK_EQUAL( packet->get_destination_address().to_string(), "38.100.128.10"); BOOST_CHECK_EQUAL( packet->get_icmp_code(), 0 ); BOOST_CHECK_EQUAL( packet->get_icmp_size(), 20 ); } BOOST_AUTO_TEST_CASE( EchoReply ) { std::string reply_file = "icmp_echoreply.pcap"; // read reply file IcmpPacketItem packet = read_packet(reply_file); BOOST_REQUIRE( packet ); BOOST_CHECK_EQUAL( packet->get_type_v4(), Icmpv4Type_EchoReply ); BOOST_CHECK_EQUAL( packet->get_source_address().to_string(), "38.100.128.10"); BOOST_CHECK_EQUAL( packet->get_destination_address().to_string(), "11.22.33.44"); BOOST_CHECK_EQUAL( packet->get_icmp_code(), 0 ); BOOST_CHECK_EQUAL( packet->get_icmp_size(), 20 ); BOOST_CHECK( packet->match_echo_reply(0xaf36, 1, boost::asio::ip::address_v4::from_string("38.100.128.10")) ); } BOOST_AUTO_TEST_CASE( TimeExceeded ) { std::string file_name = "icmp_timeexceeded.pcap"; // read file IcmpPacketItem packet = read_packet(file_name); BOOST_REQUIRE( packet ); BOOST_CHECK_EQUAL( packet->get_type_v4(), Icmpv4Type_TimeExceeded ); BOOST_CHECK_EQUAL( packet->get_source_address().to_string(), "193.186.7.1"); BOOST_CHECK_EQUAL( packet->get_destination_address().to_string(), "11.22.33.44"); BOOST_CHECK_EQUAL( packet->get_icmp_code(), 0 ); BOOST_CHECK_EQUAL( packet->get_icmp_size(), 48 ); BOOST_CHECK( packet->match_time_exceeded(0x4ae3, 1, boost::asio::ip::address_v4::from_string("216.218.186.2")) ); } BOOST_AUTO_TEST_CASE( DestinationUnreachable ) { std::string file_name = "icmp_destinationunreachable.pcap"; // read file IcmpPacketItem packet = read_packet(file_name); BOOST_REQUIRE( packet ); BOOST_CHECK_EQUAL( packet->get_type_v4(), Icmpv4Type_DestinationUnreachable ); BOOST_CHECK_EQUAL( packet->get_source_address().to_string(), "11.22.33.254"); BOOST_CHECK_EQUAL( packet->get_destination_address().to_string(), "11.22.33.44"); BOOST_CHECK_EQUAL( packet->get_icmp_code(), 3 ); BOOST_CHECK_EQUAL( packet->get_icmp_size(), 92 ); BOOST_CHECK( packet->match_destination_unreachable(0x077b, 1, boost::asio::ip::address_v4::from_string("85.214.66.2")) ); } BOOST_AUTO_TEST_SUITE_END()