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