Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 60f8cc1 | 2018-05-10 22:05:21 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 5998428 | 2022-02-16 22:41:03 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2014-2022, University of Memphis, |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 4 | * University Pierre & Marie Curie, Sorbonne University. |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 5 | * |
| 6 | * This file is part of ndn-tools (Named Data Networking Essential Tools). |
| 7 | * See AUTHORS.md for complete list of ndn-tools authors and contributors. |
| 8 | * |
| 9 | * ndn-tools is free software: you can redistribute it and/or modify it under the terms |
| 10 | * of the GNU General Public License as published by the Free Software Foundation, |
| 11 | * either version 3 of the License, or (at your option) any later version. |
| 12 | * |
| 13 | * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 14 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 15 | * PURPOSE. See the GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along with |
| 18 | * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include "tools/dump/ndndump.hpp" |
| 22 | |
Davide Pesavento | 60f8cc1 | 2018-05-10 22:05:21 -0400 | [diff] [blame] | 23 | #include "tests/test-common.hpp" |
| 24 | |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 25 | #include <net/ethernet.h> |
| 26 | #include <netinet/ip.h> |
| 27 | #include <netinet/ip6.h> |
| 28 | #include <netinet/tcp.h> |
| 29 | #include <netinet/udp.h> |
| 30 | |
| 31 | #include <boost/endian/conversion.hpp> |
Davide Pesavento | a8600b0 | 2019-12-22 18:52:36 -0500 | [diff] [blame] | 32 | #if BOOST_VERSION >= 105900 |
| 33 | #include <boost/test/tools/output_test_stream.hpp> |
| 34 | #else |
| 35 | #include <boost/test/output_test_stream.hpp> |
| 36 | #endif |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 37 | |
| 38 | #include <ndn-cxx/encoding/encoding-buffer.hpp> |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 39 | #include <ndn-cxx/lp/packet.hpp> |
Junxiao Shi | c9575a6 | 2017-07-01 06:16:02 +0000 | [diff] [blame] | 40 | #include <ndn-cxx/net/ethernet.hpp> |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 41 | |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 42 | namespace ndn { |
| 43 | namespace dump { |
| 44 | namespace tests { |
| 45 | |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 46 | namespace endian = boost::endian; |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 47 | using namespace ndn::tests; |
| 48 | |
| 49 | class StdCoutRedirector |
| 50 | { |
| 51 | public: |
| 52 | StdCoutRedirector(std::ostream& os) |
| 53 | { |
| 54 | // Redirect std::cout to the specified output stream |
| 55 | originalBuffer = std::cout.rdbuf(os.rdbuf()); |
| 56 | } |
| 57 | |
| 58 | ~StdCoutRedirector() |
| 59 | { |
| 60 | // Revert state for std::cout |
| 61 | std::cout.rdbuf(originalBuffer); |
| 62 | } |
| 63 | |
| 64 | private: |
| 65 | std::streambuf* originalBuffer; |
| 66 | }; |
| 67 | |
Davide Pesavento | 6677762 | 2020-10-09 18:46:03 -0400 | [diff] [blame] | 68 | class NdnDumpFixture |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 69 | { |
| 70 | protected: |
| 71 | NdnDumpFixture() |
| 72 | { |
| 73 | dump.m_dataLinkType = DLT_EN10MB; |
| 74 | } |
| 75 | |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 76 | template<typename Packet> |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 77 | void |
| 78 | receive(const Packet& packet) |
| 79 | { |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 80 | EncodingBuffer buffer(packet.wireEncode()); |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 81 | receiveEthernet(buffer); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 85 | receiveEthernet(EncodingBuffer& buffer, uint16_t ethertype = s_ethertypeNdn) |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 86 | { |
Junxiao Shi | c9575a6 | 2017-07-01 06:16:02 +0000 | [diff] [blame] | 87 | ethernet::Address host; |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 88 | |
| 89 | // Ethernet header |
Davide Pesavento | 5998428 | 2022-02-16 22:41:03 -0500 | [diff] [blame^] | 90 | buffer.prependBytes({reinterpret_cast<const uint8_t*>(ðertype), ethernet::TYPE_LEN}); |
| 91 | buffer.prependBytes(host); |
| 92 | buffer.prependBytes(host); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 93 | |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 94 | // pcap header |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 95 | pcap_pkthdr pkthdr{}; |
| 96 | pkthdr.caplen = pkthdr.len = buffer.size(); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 97 | |
| 98 | { |
| 99 | StdCoutRedirector redirect(output); |
Davide Pesavento | 5998428 | 2022-02-16 22:41:03 -0500 | [diff] [blame^] | 100 | dump.printPacket(&pkthdr, buffer.data()); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 104 | void |
| 105 | receiveIp4(EncodingBuffer& buffer, const ip* ipHeader) |
| 106 | { |
Davide Pesavento | 5998428 | 2022-02-16 22:41:03 -0500 | [diff] [blame^] | 107 | buffer.prependBytes({reinterpret_cast<const uint8_t*>(ipHeader), sizeof(ip)}); |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 108 | receiveEthernet(buffer, s_ethertypeIp4); |
| 109 | } |
| 110 | |
| 111 | void |
| 112 | receiveIp6(EncodingBuffer& buffer, const ip6_hdr* ip6Header) |
| 113 | { |
Davide Pesavento | 5998428 | 2022-02-16 22:41:03 -0500 | [diff] [blame^] | 114 | buffer.prependBytes({reinterpret_cast<const uint8_t*>(ip6Header), sizeof(ip6_hdr)}); |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 115 | receiveEthernet(buffer, s_ethertypeIp6); |
| 116 | } |
| 117 | |
| 118 | void |
| 119 | receiveTcp4(EncodingBuffer& buffer, const tcphdr* tcpHeader) |
| 120 | { |
Davide Pesavento | 5998428 | 2022-02-16 22:41:03 -0500 | [diff] [blame^] | 121 | buffer.prependBytes({reinterpret_cast<const uint8_t*>(tcpHeader), sizeof(tcphdr)}); |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 122 | |
| 123 | ip ipHeader{}; |
| 124 | ipHeader.ip_v = 4; |
| 125 | ipHeader.ip_hl = 5; |
| 126 | ipHeader.ip_len = endian::native_to_big(static_cast<uint16_t>(buffer.size() + sizeof(ipHeader))); |
| 127 | ipHeader.ip_ttl = 1; |
| 128 | ipHeader.ip_p = IPPROTO_TCP; |
| 129 | |
| 130 | receiveIp4(buffer, &ipHeader); |
| 131 | } |
| 132 | |
| 133 | void |
| 134 | receiveUdp4(EncodingBuffer& buffer, const udphdr* udpHeader) |
| 135 | { |
Davide Pesavento | 5998428 | 2022-02-16 22:41:03 -0500 | [diff] [blame^] | 136 | buffer.prependBytes({reinterpret_cast<const uint8_t*>(udpHeader), sizeof(udphdr)}); |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 137 | |
| 138 | ip ipHeader{}; |
| 139 | ipHeader.ip_v = 4; |
| 140 | ipHeader.ip_hl = 5; |
| 141 | ipHeader.ip_len = endian::native_to_big(static_cast<uint16_t>(buffer.size() + sizeof(ipHeader))); |
| 142 | ipHeader.ip_ttl = 1; |
| 143 | ipHeader.ip_p = IPPROTO_UDP; |
| 144 | |
| 145 | receiveIp4(buffer, &ipHeader); |
| 146 | } |
| 147 | |
| 148 | void |
| 149 | readFile(const std::string& filename) |
| 150 | { |
| 151 | StdCoutRedirector redirect(output); |
| 152 | dump.inputFile = filename; |
| 153 | dump.run(); |
| 154 | } |
| 155 | |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 156 | protected: |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 157 | NdnDump dump; |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 158 | boost::test_tools::output_test_stream output; |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 159 | |
| 160 | static const uint16_t s_ethertypeNdn; |
| 161 | static const uint16_t s_ethertypeIp4; |
| 162 | static const uint16_t s_ethertypeIp6; |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 163 | }; |
| 164 | |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 165 | const uint16_t NdnDumpFixture::s_ethertypeNdn = endian::native_to_big(ethernet::ETHERTYPE_NDN); |
| 166 | const uint16_t NdnDumpFixture::s_ethertypeIp4 = endian::native_to_big(uint16_t(ETHERTYPE_IP)); |
| 167 | const uint16_t NdnDumpFixture::s_ethertypeIp6 = endian::native_to_big(uint16_t(ETHERTYPE_IPV6)); |
| 168 | |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 169 | BOOST_AUTO_TEST_SUITE(Dump) |
| 170 | BOOST_FIXTURE_TEST_SUITE(TestNdnDump, NdnDumpFixture) |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 171 | |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 172 | BOOST_AUTO_TEST_CASE(Interest) |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 173 | { |
Davide Pesavento | 327fb4b | 2019-10-14 19:01:27 -0400 | [diff] [blame] | 174 | auto interest = makeInterest("/test", false, DEFAULT_INTEREST_LIFETIME, 1); |
| 175 | this->receive(*interest); |
Davide Pesavento | 4ab5eed | 2020-03-12 20:50:04 -0400 | [diff] [blame] | 176 | BOOST_CHECK(output.is_equal("0.000000 Ethernet, INTEREST: /test?Nonce=00000001\n")); |
Davide Pesavento | 327fb4b | 2019-10-14 19:01:27 -0400 | [diff] [blame] | 177 | this->receive(interest->setCanBePrefix(true)); |
Davide Pesavento | 4ab5eed | 2020-03-12 20:50:04 -0400 | [diff] [blame] | 178 | BOOST_CHECK(output.is_equal("0.000000 Ethernet, INTEREST: /test?CanBePrefix&Nonce=00000001\n")); |
Davide Pesavento | 327fb4b | 2019-10-14 19:01:27 -0400 | [diff] [blame] | 179 | this->receive(interest->setInterestLifetime(50_ms)); |
Davide Pesavento | 4ab5eed | 2020-03-12 20:50:04 -0400 | [diff] [blame] | 180 | BOOST_CHECK(output.is_equal("0.000000 Ethernet, INTEREST: /test?CanBePrefix&Nonce=00000001&Lifetime=50\n")); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 181 | } |
| 182 | |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 183 | BOOST_AUTO_TEST_CASE(Data) |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 184 | { |
Davide Pesavento | 327fb4b | 2019-10-14 19:01:27 -0400 | [diff] [blame] | 185 | auto data = makeData("/test"); |
| 186 | this->receive(*data); |
| 187 | BOOST_CHECK(output.is_equal("0.000000 Ethernet, DATA: /test\n")); |
| 188 | this->receive(data->setContentType(tlv::ContentType_Key)); |
| 189 | BOOST_CHECK(output.is_equal("0.000000 Ethernet, DATA: /test\n")); |
| 190 | this->receive(data->setFreshnessPeriod(42_h)); |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 191 | BOOST_CHECK(output.is_equal("0.000000 Ethernet, DATA: /test\n")); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 192 | } |
| 193 | |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 194 | BOOST_AUTO_TEST_CASE(Nack) |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 195 | { |
Davide Pesavento | 327fb4b | 2019-10-14 19:01:27 -0400 | [diff] [blame] | 196 | auto interest = makeInterest("/test", false, DEFAULT_INTEREST_LIFETIME, 1); |
Junxiao Shi | 20d5a0b | 2018-08-14 09:26:11 -0600 | [diff] [blame] | 197 | auto nack = makeNack(*interest, lp::NackReason::DUPLICATE); |
| 198 | lp::Packet lpPacket(interest->wireEncode()); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 199 | lpPacket.add<lp::NackField>(nack.getHeader()); |
| 200 | |
| 201 | this->receive(lpPacket); |
Davide Pesavento | 4ab5eed | 2020-03-12 20:50:04 -0400 | [diff] [blame] | 202 | BOOST_CHECK(output.is_equal("0.000000 Ethernet, NDNLPv2, NACK (Duplicate): /test?Nonce=00000001\n")); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 203 | } |
| 204 | |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 205 | BOOST_AUTO_TEST_CASE(LpFragment) |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 206 | { |
| 207 | const uint8_t data[10] = { |
| 208 | 0x06, 0x08, // Data packet |
| 209 | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 210 | }; |
| 211 | |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 212 | Buffer buffer(data, 4); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 213 | lp::Packet lpPacket; |
| 214 | lpPacket.add<lp::FragmentField>(std::make_pair(buffer.begin(), buffer.end())); |
| 215 | lpPacket.add<lp::FragIndexField>(0); |
| 216 | lpPacket.add<lp::FragCountField>(2); |
| 217 | lpPacket.add<lp::SequenceField>(1000); |
| 218 | |
| 219 | this->receive(lpPacket); |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 220 | BOOST_CHECK(output.is_equal("0.000000 Ethernet, NDNLPv2 fragment\n")); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 221 | } |
| 222 | |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 223 | BOOST_AUTO_TEST_CASE(LpIdle) |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 224 | { |
| 225 | lp::Packet lpPacket; |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 226 | this->receive(lpPacket); |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 227 | BOOST_CHECK(output.is_equal("0.000000 Ethernet, NDNLPv2 idle\n")); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 228 | } |
| 229 | |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 230 | BOOST_AUTO_TEST_CASE(IncompleteNdnPacket) |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 231 | { |
| 232 | const uint8_t interest[] = { |
| 233 | 0x05, 0x0E, // Interest |
| 234 | 0x07, 0x06, // Name |
| 235 | 0x08, 0x04, // NameComponent |
| 236 | 0x74, 0x65, 0x73, 0x74, |
| 237 | 0x0a, 0x04, // Nonce |
| 238 | 0x00, 0x00, 0x00, 0x01 |
| 239 | }; |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 240 | EncodingBuffer buffer; |
Davide Pesavento | 5998428 | 2022-02-16 22:41:03 -0500 | [diff] [blame^] | 241 | buffer.prependBytes(make_span(interest).first(4)); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 242 | |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 243 | this->receiveEthernet(buffer); |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 244 | BOOST_CHECK(output.is_equal("0.000000 Ethernet, NDN truncated packet, length 4\n")); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 245 | } |
| 246 | |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 247 | BOOST_AUTO_TEST_CASE(UnsupportedNdnPacket) |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 248 | { |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 249 | EncodingBuffer buffer(encoding::makeEmptyBlock(tlv::Name)); |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 250 | this->receiveEthernet(buffer); |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 251 | BOOST_CHECK(output.is_equal("0.000000 Ethernet, [Unsupported NDN packet type 7]\n")); |
| 252 | } |
| 253 | |
| 254 | BOOST_AUTO_TEST_CASE(UnsupportedEtherType) |
| 255 | { |
| 256 | EncodingBuffer pkt; |
| 257 | uint16_t type = ETHERTYPE_ARP; |
| 258 | endian::native_to_big_inplace(type); |
| 259 | |
| 260 | this->receiveEthernet(pkt, type); |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 261 | BOOST_CHECK(output.is_equal("0.000000 [Unsupported ethertype 0x806]\n")); |
| 262 | } |
| 263 | |
| 264 | BOOST_AUTO_TEST_CASE(MalformedIpv4Header) |
| 265 | { |
| 266 | dump.wantTimestamp = false; |
| 267 | |
| 268 | uint8_t theAnswer = 42; |
| 269 | |
| 270 | EncodingBuffer pkt1; |
Davide Pesavento | 5998428 | 2022-02-16 22:41:03 -0500 | [diff] [blame^] | 271 | pkt1.prependBytes({theAnswer}); |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 272 | this->receiveEthernet(pkt1, s_ethertypeIp4); |
| 273 | BOOST_CHECK(output.is_equal("IP truncated header, length 1\n")); |
| 274 | |
| 275 | ip ipHdr2{}; |
| 276 | ipHdr2.ip_v = 7; |
| 277 | |
| 278 | EncodingBuffer pkt2; |
| 279 | this->receiveIp4(pkt2, &ipHdr2); |
| 280 | BOOST_CHECK(output.is_equal("IP bad version 7\n")); |
| 281 | |
| 282 | ip ipHdr3{}; |
| 283 | ipHdr3.ip_v = 4; |
| 284 | ipHdr3.ip_hl = 2; |
| 285 | |
| 286 | EncodingBuffer pkt3; |
| 287 | this->receiveIp4(pkt3, &ipHdr3); |
| 288 | BOOST_CHECK(output.is_equal("IP bad header length 8\n")); |
| 289 | |
| 290 | ip ipHdr4{}; |
| 291 | ipHdr4.ip_v = 4; |
| 292 | ipHdr4.ip_hl = 5; |
| 293 | ipHdr4.ip_len = 10; |
| 294 | endian::native_to_big_inplace(ipHdr4.ip_len); |
| 295 | |
| 296 | EncodingBuffer pkt4; |
| 297 | this->receiveIp4(pkt4, &ipHdr4); |
| 298 | BOOST_CHECK(output.is_equal("IP bad length 10\n")); |
| 299 | |
| 300 | ip ipHdr5{}; |
| 301 | ipHdr5.ip_v = 4; |
| 302 | ipHdr5.ip_hl = 5; |
| 303 | ipHdr5.ip_len = 1000; |
| 304 | endian::native_to_big_inplace(ipHdr5.ip_len); |
| 305 | |
| 306 | EncodingBuffer pkt5; |
| 307 | this->receiveIp4(pkt5, &ipHdr5); |
| 308 | BOOST_CHECK(output.is_equal("IP truncated packet, 980 bytes missing\n")); |
| 309 | } |
| 310 | |
Davide Pesavento | 0ca7e69 | 2018-08-05 02:21:06 -0400 | [diff] [blame] | 311 | BOOST_AUTO_TEST_CASE(MalformedIpv6Header) |
| 312 | { |
| 313 | dump.wantTimestamp = false; |
| 314 | |
| 315 | uint8_t theAnswer = 42; |
| 316 | |
| 317 | EncodingBuffer pkt1; |
Davide Pesavento | 5998428 | 2022-02-16 22:41:03 -0500 | [diff] [blame^] | 318 | pkt1.prependBytes({theAnswer}); |
Davide Pesavento | 0ca7e69 | 2018-08-05 02:21:06 -0400 | [diff] [blame] | 319 | this->receiveEthernet(pkt1, s_ethertypeIp6); |
| 320 | BOOST_CHECK(output.is_equal("IP6 truncated header, length 1\n")); |
| 321 | |
| 322 | ip6_hdr ip6Hdr2{}; |
| 323 | ip6Hdr2.ip6_vfc = 10 << 4; |
| 324 | |
| 325 | EncodingBuffer pkt2; |
| 326 | this->receiveIp6(pkt2, &ip6Hdr2); |
| 327 | BOOST_CHECK(output.is_equal("IP6 bad version 10\n")); |
| 328 | |
| 329 | ip6_hdr ip6Hdr3{}; |
| 330 | ip6Hdr3.ip6_vfc = 6 << 4; |
| 331 | ip6Hdr3.ip6_plen = 1400; |
| 332 | endian::native_to_big_inplace(ip6Hdr3.ip6_plen); |
| 333 | |
| 334 | EncodingBuffer pkt3; |
| 335 | this->receiveIp6(pkt3, &ip6Hdr3); |
| 336 | BOOST_CHECK(output.is_equal("IP6 truncated payload, 1400 bytes missing\n")); |
| 337 | } |
| 338 | |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 339 | BOOST_AUTO_TEST_CASE(UnsupportedIpProto) |
| 340 | { |
| 341 | dump.wantTimestamp = false; |
| 342 | |
| 343 | ip ipHdr{}; |
| 344 | ipHdr.ip_v = 4; |
| 345 | ipHdr.ip_hl = 5; |
| 346 | ipHdr.ip_len = sizeof(ipHdr); |
| 347 | endian::native_to_big_inplace(ipHdr.ip_len); |
| 348 | ipHdr.ip_p = IPPROTO_SCTP; |
| 349 | |
Davide Pesavento | 0ca7e69 | 2018-08-05 02:21:06 -0400 | [diff] [blame] | 350 | EncodingBuffer pkt1; |
| 351 | this->receiveIp4(pkt1, &ipHdr); |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 352 | BOOST_CHECK(output.is_equal("IP 0.0.0.0 > 0.0.0.0, [Unsupported IP proto 132]\n")); |
Davide Pesavento | 0ca7e69 | 2018-08-05 02:21:06 -0400 | [diff] [blame] | 353 | |
| 354 | ip6_hdr ip6Hdr{}; |
| 355 | ip6Hdr.ip6_vfc = 6 << 4; |
| 356 | ip6Hdr.ip6_nxt = IPPROTO_NONE; |
| 357 | |
| 358 | EncodingBuffer pkt2; |
| 359 | this->receiveIp6(pkt2, &ip6Hdr); |
| 360 | BOOST_CHECK(output.is_equal("IP6 :: > ::, [No next header]\n")); |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | BOOST_AUTO_TEST_CASE(MalformedTcpHeader) |
| 364 | { |
| 365 | dump.wantTimestamp = false; |
| 366 | |
| 367 | tcphdr tcpHdr1{}; |
Davide Pesavento | 7b9837b | 2019-02-23 19:07:50 -0500 | [diff] [blame] | 368 | tcpHdr1.TH_OFF = 0x2; |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 369 | |
| 370 | EncodingBuffer pkt1; |
| 371 | this->receiveTcp4(pkt1, &tcpHdr1); |
| 372 | BOOST_CHECK(output.is_equal("IP 0.0.0.0 > 0.0.0.0, TCP bad header length 8\n")); |
| 373 | |
| 374 | tcphdr tcpHdr2{}; |
Davide Pesavento | 7b9837b | 2019-02-23 19:07:50 -0500 | [diff] [blame] | 375 | tcpHdr2.TH_OFF = 0xf; |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 376 | |
| 377 | EncodingBuffer pkt2; |
| 378 | this->receiveTcp4(pkt2, &tcpHdr2); |
| 379 | BOOST_CHECK(output.is_equal("IP 0.0.0.0 > 0.0.0.0, TCP truncated header, 40 bytes missing\n")); |
| 380 | } |
| 381 | |
| 382 | BOOST_AUTO_TEST_CASE(MalformedUdpHeader) |
| 383 | { |
| 384 | dump.wantTimestamp = false; |
| 385 | |
| 386 | udphdr udpHdr1{}; |
Davide Pesavento | 7b9837b | 2019-02-23 19:07:50 -0500 | [diff] [blame] | 387 | udpHdr1.UH_LEN = 3; |
| 388 | endian::native_to_big_inplace(udpHdr1.UH_LEN); |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 389 | |
| 390 | EncodingBuffer pkt1; |
| 391 | this->receiveUdp4(pkt1, &udpHdr1); |
| 392 | BOOST_CHECK(output.is_equal("IP 0.0.0.0 > 0.0.0.0, UDP bad length 3\n")); |
| 393 | |
| 394 | udphdr udpHdr2{}; |
Davide Pesavento | 7b9837b | 2019-02-23 19:07:50 -0500 | [diff] [blame] | 395 | udpHdr2.UH_LEN = 1000; |
| 396 | endian::native_to_big_inplace(udpHdr2.UH_LEN); |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 397 | |
| 398 | EncodingBuffer pkt2; |
| 399 | this->receiveUdp4(pkt2, &udpHdr2); |
| 400 | BOOST_CHECK(output.is_equal("IP 0.0.0.0 > 0.0.0.0, UDP truncated packet, 992 bytes missing\n")); |
| 401 | } |
| 402 | |
| 403 | BOOST_AUTO_TEST_CASE(InvalidTlvLength) |
| 404 | { |
| 405 | dump.wantTimestamp = false; |
| 406 | this->readFile("tests/dump/invalid-tlv-length.pcap"); |
| 407 | |
| 408 | const std::string expected = |
| 409 | "IP 128.196.203.36 > 128.187.81.12, TCP, length 147, NDNLPv2 invalid packet: " |
| 410 | "TLV-LENGTH of sub-element of type 5 exceeds TLV-VALUE boundary of parent block\n"; |
| 411 | BOOST_CHECK(output.is_equal(expected)); |
| 412 | } |
| 413 | |
| 414 | BOOST_AUTO_TEST_CASE(UnrecognizedLpField) |
| 415 | { |
| 416 | dump.wantTimestamp = false; |
| 417 | this->readFile("tests/dump/unrecognized-lp-field.pcap"); |
| 418 | |
Davide Pesavento | 327fb4b | 2019-10-14 19:01:27 -0400 | [diff] [blame] | 419 | const std::string expected = |
| 420 | "IP 128.196.203.36 > 128.187.81.12, TCP, length 800, " |
| 421 | "NDNLPv2 invalid packet: unrecognized field 4 cannot be ignored\n"; |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 422 | BOOST_CHECK(output.is_equal(expected)); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 423 | } |
| 424 | |
Davide Pesavento | b5b8f95 | 2018-07-26 14:19:16 -0400 | [diff] [blame] | 425 | BOOST_AUTO_TEST_CASE(NoTimestamp) |
| 426 | { |
| 427 | dump.wantTimestamp = false; |
| 428 | |
| 429 | lp::Packet lpPacket; |
| 430 | this->receive(lpPacket); |
| 431 | |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 432 | BOOST_CHECK(output.is_equal("Ethernet, NDNLPv2 idle\n")); |
Davide Pesavento | b5b8f95 | 2018-07-26 14:19:16 -0400 | [diff] [blame] | 433 | } |
| 434 | |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 435 | BOOST_AUTO_TEST_CASE(FromFile) |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 436 | { |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 437 | dump.pcapFilter = ""; |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame] | 438 | this->readFile("tests/dump/nack.pcap"); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 439 | |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 440 | const std::string expected = |
Davide Pesavento | 327fb4b | 2019-10-14 19:01:27 -0400 | [diff] [blame] | 441 | "1571091605.129263 IP 127.0.0.1 > 127.0.0.1, TCP, length 36, " |
Davide Pesavento | 4ab5eed | 2020-03-12 20:50:04 -0400 | [diff] [blame] | 442 | "INTEREST: /producer/nack/no-route?Nonce=827bcac4\n" |
Davide Pesavento | 327fb4b | 2019-10-14 19:01:27 -0400 | [diff] [blame] | 443 | "1571091605.129702 IP 127.0.0.1 > 127.0.0.1, TCP, length 49, " |
Davide Pesavento | 4ab5eed | 2020-03-12 20:50:04 -0400 | [diff] [blame] | 444 | "NDNLPv2, NACK (NoRoute): /producer/nack/no-route?Nonce=827bcac4\n"; |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 445 | BOOST_CHECK(output.is_equal(expected)); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 446 | } |
| 447 | |
Davide Pesavento | 0ca7e69 | 2018-08-05 02:21:06 -0400 | [diff] [blame] | 448 | BOOST_AUTO_TEST_CASE(LinuxSllTcp4) |
| 449 | { |
| 450 | dump.wantTimestamp = false; |
| 451 | this->readFile("tests/dump/linux-sll-tcp4.pcap"); |
| 452 | |
| 453 | const std::string expected = |
Davide Pesavento | 4ab5eed | 2020-03-12 20:50:04 -0400 | [diff] [blame] | 454 | "IP 162.211.64.84 > 131.179.196.46, TCP, length 41, INTEREST: /ndn/edu/arizona/ping/8202?Nonce=cf062c3f\n" |
Davide Pesavento | 327fb4b | 2019-10-14 19:01:27 -0400 | [diff] [blame] | 455 | "IP 131.179.196.46 > 162.211.64.84, TCP, length 403, DATA: /ndn/edu/arizona/ping/8202\n"; |
Davide Pesavento | 0ca7e69 | 2018-08-05 02:21:06 -0400 | [diff] [blame] | 456 | BOOST_CHECK(output.is_equal(expected)); |
| 457 | } |
| 458 | |
| 459 | BOOST_AUTO_TEST_CASE(LinuxSllUdp4) |
| 460 | { |
| 461 | dump.wantTimestamp = false; |
| 462 | this->readFile("tests/dump/linux-sll-udp4.pcap"); |
| 463 | |
| 464 | const std::string expected = |
Davide Pesavento | 4ab5eed | 2020-03-12 20:50:04 -0400 | [diff] [blame] | 465 | "IP 162.211.64.84 > 131.179.196.46, UDP, length 42, INTEREST: /ndn/edu/arizona/ping/31044?Nonce=f33c0bbd\n" |
Davide Pesavento | 327fb4b | 2019-10-14 19:01:27 -0400 | [diff] [blame] | 466 | "IP 131.179.196.46 > 162.211.64.84, UDP, length 404, DATA: /ndn/edu/arizona/ping/31044\n"; |
Davide Pesavento | 0ca7e69 | 2018-08-05 02:21:06 -0400 | [diff] [blame] | 467 | BOOST_CHECK(output.is_equal(expected)); |
| 468 | } |
| 469 | |
| 470 | BOOST_AUTO_TEST_CASE(LinuxSllTcp6) |
| 471 | { |
| 472 | dump.wantTimestamp = false; |
| 473 | this->readFile("tests/dump/linux-sll-tcp6.pcap"); |
| 474 | |
| 475 | const std::string expected = |
| 476 | "IP6 2602:fff6:d:b317::39f8 > 2001:660:3302:282c:160::163, TCP, length 42, " |
Davide Pesavento | 4ab5eed | 2020-03-12 20:50:04 -0400 | [diff] [blame] | 477 | "INTEREST: /ndn/edu/arizona/ping/19573?Nonce=7b9e5b2e\n" |
Davide Pesavento | 0ca7e69 | 2018-08-05 02:21:06 -0400 | [diff] [blame] | 478 | "IP6 2001:660:3302:282c:160::163 > 2602:fff6:d:b317::39f8, TCP, length 404, " |
| 479 | "DATA: /ndn/edu/arizona/ping/19573\n" |
| 480 | "IP6 2001:660:3302:282c:160::163 > 2602:fff6:d:b317::39f8, TCP, length 56, " |
Davide Pesavento | 327fb4b | 2019-10-14 19:01:27 -0400 | [diff] [blame] | 481 | "invalid network packet: Unrecognized element of critical type 9\n"; |
Davide Pesavento | 0ca7e69 | 2018-08-05 02:21:06 -0400 | [diff] [blame] | 482 | BOOST_CHECK(output.is_equal(expected)); |
| 483 | } |
| 484 | |
| 485 | BOOST_AUTO_TEST_CASE(LinuxSllUdp6) |
| 486 | { |
| 487 | dump.wantTimestamp = false; |
| 488 | this->readFile("tests/dump/linux-sll-udp6.pcap"); |
| 489 | |
| 490 | const std::string expected = |
| 491 | "IP6 2602:fff6:d:b317::39f8 > 2001:660:3302:282c:160::163, UDP, length 39, " |
Davide Pesavento | 4ab5eed | 2020-03-12 20:50:04 -0400 | [diff] [blame] | 492 | "INTEREST: /ndn/edu/arizona/ping/18?Nonce=7e351222\n" |
Davide Pesavento | 0ca7e69 | 2018-08-05 02:21:06 -0400 | [diff] [blame] | 493 | "IP6 2001:660:3302:282c:160::163 > 2602:fff6:d:b317::39f8, UDP, length 401, " |
| 494 | "DATA: /ndn/edu/arizona/ping/18\n" |
| 495 | "IP6 2001:660:3302:282c:160::163 > 2602:fff6:d:b317::39f8, UDP, length 56, " |
Davide Pesavento | 327fb4b | 2019-10-14 19:01:27 -0400 | [diff] [blame] | 496 | "invalid network packet: Unrecognized element of critical type 9\n"; |
Davide Pesavento | 0ca7e69 | 2018-08-05 02:21:06 -0400 | [diff] [blame] | 497 | BOOST_CHECK(output.is_equal(expected)); |
| 498 | } |
| 499 | |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 500 | BOOST_AUTO_TEST_SUITE_END() // TestNdnDump |
| 501 | BOOST_AUTO_TEST_SUITE_END() // Dump |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 502 | |
| 503 | } // namespace tests |
| 504 | } // namespace dump |
| 505 | } // namespace ndn |