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