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 | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2014-2018, University of Memphis, |
| 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> |
| 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 | |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 38 | namespace ndn { |
| 39 | namespace dump { |
| 40 | namespace tests { |
| 41 | |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame^] | 42 | namespace endian = boost::endian; |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 43 | using namespace ndn::tests; |
| 44 | |
| 45 | class StdCoutRedirector |
| 46 | { |
| 47 | public: |
| 48 | StdCoutRedirector(std::ostream& os) |
| 49 | { |
| 50 | // Redirect std::cout to the specified output stream |
| 51 | originalBuffer = std::cout.rdbuf(os.rdbuf()); |
| 52 | } |
| 53 | |
| 54 | ~StdCoutRedirector() |
| 55 | { |
| 56 | // Revert state for std::cout |
| 57 | std::cout.rdbuf(originalBuffer); |
| 58 | } |
| 59 | |
| 60 | private: |
| 61 | std::streambuf* originalBuffer; |
| 62 | }; |
| 63 | |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 64 | class NdnDumpFixture : public IdentityManagementFixture |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 65 | { |
| 66 | protected: |
| 67 | NdnDumpFixture() |
| 68 | { |
| 69 | dump.m_dataLinkType = DLT_EN10MB; |
| 70 | } |
| 71 | |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 72 | template<typename Packet> |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 73 | void |
| 74 | receive(const Packet& packet) |
| 75 | { |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 76 | EncodingBuffer buffer(packet.wireEncode()); |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame^] | 77 | receiveEthernet(buffer); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame^] | 81 | receiveEthernet(EncodingBuffer& buffer, uint16_t ethertype = s_ethertypeNdn) |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 82 | { |
Junxiao Shi | c9575a6 | 2017-07-01 06:16:02 +0000 | [diff] [blame] | 83 | ethernet::Address host; |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 84 | |
| 85 | // Ethernet header |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame^] | 86 | buffer.prependByteArray(reinterpret_cast<const uint8_t*>(ðertype), ethernet::TYPE_LEN); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 87 | buffer.prependByteArray(host.data(), host.size()); |
| 88 | buffer.prependByteArray(host.data(), host.size()); |
| 89 | |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame^] | 90 | // pcap header |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 91 | pcap_pkthdr pkthdr{}; |
| 92 | pkthdr.caplen = pkthdr.len = buffer.size(); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 93 | |
| 94 | { |
| 95 | StdCoutRedirector redirect(output); |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 96 | dump.printPacket(&pkthdr, buffer.buf()); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame^] | 100 | void |
| 101 | receiveIp4(EncodingBuffer& buffer, const ip* ipHeader) |
| 102 | { |
| 103 | buffer.prependByteArray(reinterpret_cast<const uint8_t*>(ipHeader), sizeof(ip)); |
| 104 | receiveEthernet(buffer, s_ethertypeIp4); |
| 105 | } |
| 106 | |
| 107 | void |
| 108 | receiveIp6(EncodingBuffer& buffer, const ip6_hdr* ip6Header) |
| 109 | { |
| 110 | buffer.prependByteArray(reinterpret_cast<const uint8_t*>(ip6Header), sizeof(ip6_hdr)); |
| 111 | receiveEthernet(buffer, s_ethertypeIp6); |
| 112 | } |
| 113 | |
| 114 | void |
| 115 | receiveTcp4(EncodingBuffer& buffer, const tcphdr* tcpHeader) |
| 116 | { |
| 117 | buffer.prependByteArray(reinterpret_cast<const uint8_t*>(tcpHeader), sizeof(tcphdr)); |
| 118 | |
| 119 | ip ipHeader{}; |
| 120 | ipHeader.ip_v = 4; |
| 121 | ipHeader.ip_hl = 5; |
| 122 | ipHeader.ip_len = endian::native_to_big(static_cast<uint16_t>(buffer.size() + sizeof(ipHeader))); |
| 123 | ipHeader.ip_ttl = 1; |
| 124 | ipHeader.ip_p = IPPROTO_TCP; |
| 125 | |
| 126 | receiveIp4(buffer, &ipHeader); |
| 127 | } |
| 128 | |
| 129 | void |
| 130 | receiveUdp4(EncodingBuffer& buffer, const udphdr* udpHeader) |
| 131 | { |
| 132 | buffer.prependByteArray(reinterpret_cast<const uint8_t*>(udpHeader), sizeof(udphdr)); |
| 133 | |
| 134 | ip ipHeader{}; |
| 135 | ipHeader.ip_v = 4; |
| 136 | ipHeader.ip_hl = 5; |
| 137 | ipHeader.ip_len = endian::native_to_big(static_cast<uint16_t>(buffer.size() + sizeof(ipHeader))); |
| 138 | ipHeader.ip_ttl = 1; |
| 139 | ipHeader.ip_p = IPPROTO_UDP; |
| 140 | |
| 141 | receiveIp4(buffer, &ipHeader); |
| 142 | } |
| 143 | |
| 144 | void |
| 145 | readFile(const std::string& filename) |
| 146 | { |
| 147 | StdCoutRedirector redirect(output); |
| 148 | dump.inputFile = filename; |
| 149 | dump.run(); |
| 150 | } |
| 151 | |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 152 | protected: |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 153 | NdnDump dump; |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 154 | boost::test_tools::output_test_stream output; |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame^] | 155 | |
| 156 | static const uint16_t s_ethertypeNdn; |
| 157 | static const uint16_t s_ethertypeIp4; |
| 158 | static const uint16_t s_ethertypeIp6; |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 159 | }; |
| 160 | |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame^] | 161 | const uint16_t NdnDumpFixture::s_ethertypeNdn = endian::native_to_big(ethernet::ETHERTYPE_NDN); |
| 162 | const uint16_t NdnDumpFixture::s_ethertypeIp4 = endian::native_to_big(uint16_t(ETHERTYPE_IP)); |
| 163 | const uint16_t NdnDumpFixture::s_ethertypeIp6 = endian::native_to_big(uint16_t(ETHERTYPE_IPV6)); |
| 164 | |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 165 | BOOST_AUTO_TEST_SUITE(Dump) |
| 166 | BOOST_FIXTURE_TEST_SUITE(TestNdnDump, NdnDumpFixture) |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 167 | |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame^] | 168 | BOOST_AUTO_TEST_CASE(Interest) |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 169 | { |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame^] | 170 | ndn::Interest interest("/test"); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 171 | interest.setNonce(0); |
| 172 | |
| 173 | this->receive(interest); |
| 174 | |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame^] | 175 | BOOST_CHECK(output.is_equal("0.000000 Ethernet, INTEREST: /test?ndn.Nonce=0\n")); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 176 | } |
| 177 | |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame^] | 178 | BOOST_AUTO_TEST_CASE(Data) |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 179 | { |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame^] | 180 | ndn::Data data("/test"); |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 181 | m_keyChain.sign(data); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 182 | |
| 183 | this->receive(data); |
| 184 | |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame^] | 185 | BOOST_CHECK(output.is_equal("0.000000 Ethernet, DATA: /test\n")); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 186 | } |
| 187 | |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame^] | 188 | BOOST_AUTO_TEST_CASE(Nack) |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 189 | { |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame^] | 190 | ndn::Interest interest("/test"); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 191 | interest.setNonce(0); |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 192 | lp::Nack nack(interest); |
| 193 | nack.setReason(lp::NackReason::DUPLICATE); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 194 | lp::Packet lpPacket(interest.wireEncode()); |
| 195 | lpPacket.add<lp::NackField>(nack.getHeader()); |
| 196 | |
| 197 | this->receive(lpPacket); |
| 198 | |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame^] | 199 | BOOST_CHECK(output.is_equal("0.000000 Ethernet, NDNLPv2, NACK (Duplicate): /test?ndn.Nonce=0\n")); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 200 | } |
| 201 | |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame^] | 202 | BOOST_AUTO_TEST_CASE(LpFragment) |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 203 | { |
| 204 | const uint8_t data[10] = { |
| 205 | 0x06, 0x08, // Data packet |
| 206 | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 207 | }; |
| 208 | |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 209 | Buffer buffer(data, 4); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 210 | lp::Packet lpPacket; |
| 211 | lpPacket.add<lp::FragmentField>(std::make_pair(buffer.begin(), buffer.end())); |
| 212 | lpPacket.add<lp::FragIndexField>(0); |
| 213 | lpPacket.add<lp::FragCountField>(2); |
| 214 | lpPacket.add<lp::SequenceField>(1000); |
| 215 | |
| 216 | this->receive(lpPacket); |
| 217 | |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame^] | 218 | BOOST_CHECK(output.is_equal("0.000000 Ethernet, NDNLPv2 fragment\n")); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 219 | } |
| 220 | |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame^] | 221 | BOOST_AUTO_TEST_CASE(LpIdle) |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 222 | { |
| 223 | lp::Packet lpPacket; |
| 224 | |
| 225 | this->receive(lpPacket); |
| 226 | |
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 | }; |
| 240 | |
| 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); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 245 | |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame^] | 246 | 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] | 247 | } |
| 248 | |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame^] | 249 | BOOST_AUTO_TEST_CASE(UnsupportedNdnPacket) |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 250 | { |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 251 | EncodingBuffer buffer(encoding::makeEmptyBlock(tlv::Name)); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 252 | |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame^] | 253 | this->receiveEthernet(buffer); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 254 | |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame^] | 255 | BOOST_CHECK(output.is_equal("0.000000 Ethernet, [Unsupported NDN packet type 7]\n")); |
| 256 | } |
| 257 | |
| 258 | BOOST_AUTO_TEST_CASE(UnsupportedEtherType) |
| 259 | { |
| 260 | EncodingBuffer pkt; |
| 261 | uint16_t type = ETHERTYPE_ARP; |
| 262 | endian::native_to_big_inplace(type); |
| 263 | |
| 264 | this->receiveEthernet(pkt, type); |
| 265 | |
| 266 | BOOST_CHECK(output.is_equal("0.000000 [Unsupported ethertype 0x806]\n")); |
| 267 | } |
| 268 | |
| 269 | BOOST_AUTO_TEST_CASE(MalformedIpv4Header) |
| 270 | { |
| 271 | dump.wantTimestamp = false; |
| 272 | |
| 273 | uint8_t theAnswer = 42; |
| 274 | |
| 275 | EncodingBuffer pkt1; |
| 276 | pkt1.prependByte(theAnswer); |
| 277 | this->receiveEthernet(pkt1, s_ethertypeIp4); |
| 278 | BOOST_CHECK(output.is_equal("IP truncated header, length 1\n")); |
| 279 | |
| 280 | ip ipHdr2{}; |
| 281 | ipHdr2.ip_v = 7; |
| 282 | |
| 283 | EncodingBuffer pkt2; |
| 284 | this->receiveIp4(pkt2, &ipHdr2); |
| 285 | BOOST_CHECK(output.is_equal("IP bad version 7\n")); |
| 286 | |
| 287 | ip ipHdr3{}; |
| 288 | ipHdr3.ip_v = 4; |
| 289 | ipHdr3.ip_hl = 2; |
| 290 | |
| 291 | EncodingBuffer pkt3; |
| 292 | this->receiveIp4(pkt3, &ipHdr3); |
| 293 | BOOST_CHECK(output.is_equal("IP bad header length 8\n")); |
| 294 | |
| 295 | ip ipHdr4{}; |
| 296 | ipHdr4.ip_v = 4; |
| 297 | ipHdr4.ip_hl = 5; |
| 298 | ipHdr4.ip_len = 10; |
| 299 | endian::native_to_big_inplace(ipHdr4.ip_len); |
| 300 | |
| 301 | EncodingBuffer pkt4; |
| 302 | this->receiveIp4(pkt4, &ipHdr4); |
| 303 | BOOST_CHECK(output.is_equal("IP bad length 10\n")); |
| 304 | |
| 305 | ip ipHdr5{}; |
| 306 | ipHdr5.ip_v = 4; |
| 307 | ipHdr5.ip_hl = 5; |
| 308 | ipHdr5.ip_len = 1000; |
| 309 | endian::native_to_big_inplace(ipHdr5.ip_len); |
| 310 | |
| 311 | EncodingBuffer pkt5; |
| 312 | this->receiveIp4(pkt5, &ipHdr5); |
| 313 | BOOST_CHECK(output.is_equal("IP truncated packet, 980 bytes missing\n")); |
| 314 | } |
| 315 | |
| 316 | BOOST_AUTO_TEST_CASE(UnsupportedIpProto) |
| 317 | { |
| 318 | dump.wantTimestamp = false; |
| 319 | |
| 320 | ip ipHdr{}; |
| 321 | ipHdr.ip_v = 4; |
| 322 | ipHdr.ip_hl = 5; |
| 323 | ipHdr.ip_len = sizeof(ipHdr); |
| 324 | endian::native_to_big_inplace(ipHdr.ip_len); |
| 325 | ipHdr.ip_p = IPPROTO_SCTP; |
| 326 | |
| 327 | EncodingBuffer pkt; |
| 328 | this->receiveIp4(pkt, &ipHdr); |
| 329 | BOOST_CHECK(output.is_equal("IP 0.0.0.0 > 0.0.0.0, [Unsupported IP proto 132]\n")); |
| 330 | } |
| 331 | |
| 332 | BOOST_AUTO_TEST_CASE(MalformedTcpHeader) |
| 333 | { |
| 334 | dump.wantTimestamp = false; |
| 335 | |
| 336 | tcphdr tcpHdr1{}; |
| 337 | tcpHdr1.th_off = 0x2; |
| 338 | |
| 339 | EncodingBuffer pkt1; |
| 340 | this->receiveTcp4(pkt1, &tcpHdr1); |
| 341 | BOOST_CHECK(output.is_equal("IP 0.0.0.0 > 0.0.0.0, TCP bad header length 8\n")); |
| 342 | |
| 343 | tcphdr tcpHdr2{}; |
| 344 | tcpHdr2.th_off = 0xf; |
| 345 | |
| 346 | EncodingBuffer pkt2; |
| 347 | this->receiveTcp4(pkt2, &tcpHdr2); |
| 348 | BOOST_CHECK(output.is_equal("IP 0.0.0.0 > 0.0.0.0, TCP truncated header, 40 bytes missing\n")); |
| 349 | } |
| 350 | |
| 351 | BOOST_AUTO_TEST_CASE(MalformedUdpHeader) |
| 352 | { |
| 353 | dump.wantTimestamp = false; |
| 354 | |
| 355 | udphdr udpHdr1{}; |
| 356 | udpHdr1.uh_ulen = 3; |
| 357 | endian::native_to_big_inplace(udpHdr1.uh_ulen); |
| 358 | |
| 359 | EncodingBuffer pkt1; |
| 360 | this->receiveUdp4(pkt1, &udpHdr1); |
| 361 | BOOST_CHECK(output.is_equal("IP 0.0.0.0 > 0.0.0.0, UDP bad length 3\n")); |
| 362 | |
| 363 | udphdr udpHdr2{}; |
| 364 | udpHdr2.uh_ulen = 1000; |
| 365 | endian::native_to_big_inplace(udpHdr2.uh_ulen); |
| 366 | |
| 367 | EncodingBuffer pkt2; |
| 368 | this->receiveUdp4(pkt2, &udpHdr2); |
| 369 | BOOST_CHECK(output.is_equal("IP 0.0.0.0 > 0.0.0.0, UDP truncated packet, 992 bytes missing\n")); |
| 370 | } |
| 371 | |
| 372 | BOOST_AUTO_TEST_CASE(InvalidTlvLength) |
| 373 | { |
| 374 | dump.wantTimestamp = false; |
| 375 | this->readFile("tests/dump/invalid-tlv-length.pcap"); |
| 376 | |
| 377 | const std::string expected = |
| 378 | "IP 128.196.203.36 > 128.187.81.12, TCP, length 147, NDNLPv2 invalid packet: " |
| 379 | "TLV-LENGTH of sub-element of type 5 exceeds TLV-VALUE boundary of parent block\n"; |
| 380 | BOOST_CHECK(output.is_equal(expected)); |
| 381 | } |
| 382 | |
| 383 | BOOST_AUTO_TEST_CASE(UnrecognizedLpField) |
| 384 | { |
| 385 | dump.wantTimestamp = false; |
| 386 | this->readFile("tests/dump/unrecognized-lp-field.pcap"); |
| 387 | |
| 388 | const std::string expected = "IP 128.196.203.36 > 128.187.81.12, TCP, length 800, " |
| 389 | "NDNLPv2 invalid packet: unrecognized field 4 cannot be ignored\n"; |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 390 | BOOST_CHECK(output.is_equal(expected)); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 391 | } |
| 392 | |
Davide Pesavento | b5b8f95 | 2018-07-26 14:19:16 -0400 | [diff] [blame] | 393 | BOOST_AUTO_TEST_CASE(NoTimestamp) |
| 394 | { |
| 395 | dump.wantTimestamp = false; |
| 396 | |
| 397 | lp::Packet lpPacket; |
| 398 | this->receive(lpPacket); |
| 399 | |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame^] | 400 | BOOST_CHECK(output.is_equal("Ethernet, NDNLPv2 idle\n")); |
Davide Pesavento | b5b8f95 | 2018-07-26 14:19:16 -0400 | [diff] [blame] | 401 | } |
| 402 | |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame^] | 403 | BOOST_AUTO_TEST_CASE(FromFile) |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 404 | { |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 405 | dump.pcapFilter = ""; |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame^] | 406 | this->readFile("tests/dump/nack.pcap"); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 407 | |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 408 | const std::string expected = |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame^] | 409 | "1456768916.467099 IP 1.0.0.1 > 1.0.0.2, UDP, length 42, " |
Davide Pesavento | d1b1bf6 | 2018-07-26 18:05:08 -0400 | [diff] [blame] | 410 | "INTEREST: /producer/nack/congestion?ndn.MustBeFresh=1&ndn.Nonce=2581361680\n" |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame^] | 411 | "1456768916.567099 IP 1.0.0.1 > 1.0.0.2, UDP, length 41, " |
Davide Pesavento | d1b1bf6 | 2018-07-26 18:05:08 -0400 | [diff] [blame] | 412 | "INTEREST: /producer/nack/duplicate?ndn.MustBeFresh=1&ndn.Nonce=4138343109\n" |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame^] | 413 | "1456768916.667099 IP 1.0.0.1 > 1.0.0.2, UDP, length 41, " |
Davide Pesavento | d1b1bf6 | 2018-07-26 18:05:08 -0400 | [diff] [blame] | 414 | "INTEREST: /producer/nack/no-reason?ndn.MustBeFresh=1&ndn.Nonce=4034910304\n" |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame^] | 415 | "1456768916.767099 IP 1.0.0.2 > 1.0.0.1, UDP, length 55, " |
| 416 | "NDNLPv2, NACK (Congestion): /producer/nack/congestion?ndn.MustBeFresh=1&ndn.Nonce=2581361680\n" |
| 417 | "1456768916.867099 IP 1.0.0.2 > 1.0.0.1, UDP, length 54, " |
| 418 | "NDNLPv2, NACK (Duplicate): /producer/nack/duplicate?ndn.MustBeFresh=1&ndn.Nonce=4138343109\n" |
| 419 | "1456768916.967099 IP 1.0.0.2 > 1.0.0.1, UDP, length 54, " |
| 420 | "NDNLPv2, NACK (None): /producer/nack/no-reason?ndn.MustBeFresh=1&ndn.Nonce=4034910304\n" |
| 421 | "1456768917.067099 IP 1.0.0.1 > 1.0.0.2, TCP, length 42, " |
Davide Pesavento | d1b1bf6 | 2018-07-26 18:05:08 -0400 | [diff] [blame] | 422 | "INTEREST: /producer/nack/congestion?ndn.MustBeFresh=1&ndn.Nonce=3192497423\n" |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame^] | 423 | "1456768917.267099 IP 1.0.0.2 > 1.0.0.1, TCP, length 55, " |
| 424 | "NDNLPv2, NACK (Congestion): /producer/nack/congestion?ndn.MustBeFresh=1&ndn.Nonce=3192497423\n" |
| 425 | "1456768917.367099 IP 1.0.0.1 > 1.0.0.2, TCP, length 82, " |
Davide Pesavento | d1b1bf6 | 2018-07-26 18:05:08 -0400 | [diff] [blame] | 426 | "INTEREST: /producer/nack/duplicate?ndn.MustBeFresh=1&ndn.Nonce=522390724\n" |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame^] | 427 | "1456768917.567099 IP 1.0.0.2 > 1.0.0.1, TCP, length 54, " |
| 428 | "NDNLPv2, NACK (Duplicate): /producer/nack/duplicate?ndn.MustBeFresh=1&ndn.Nonce=522390724\n" |
| 429 | "1456768917.767099 IP 1.0.0.2 > 1.0.0.1, TCP, length 54, " |
| 430 | "NDNLPv2, NACK (None): /producer/nack/no-reason?ndn.MustBeFresh=1&ndn.Nonce=2002441365\n" |
| 431 | "1456768917.967099 IP 1.0.0.1 > 1.0.0.2, TCP, length 41, " |
Davide Pesavento | d1b1bf6 | 2018-07-26 18:05:08 -0400 | [diff] [blame] | 432 | "INTEREST: /producer/nack/no-reason?ndn.MustBeFresh=1&ndn.Nonce=3776824408\n" |
Davide Pesavento | f912653 | 2018-08-04 18:31:06 -0400 | [diff] [blame^] | 433 | "1456768918.067099 IP 1.0.0.2 > 1.0.0.1, TCP, length 54, " |
| 434 | "NDNLPv2, NACK (None): /producer/nack/no-reason?ndn.MustBeFresh=1&ndn.Nonce=3776824408\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 | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 438 | BOOST_AUTO_TEST_SUITE_END() // TestNdnDump |
| 439 | BOOST_AUTO_TEST_SUITE_END() // Dump |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 440 | |
| 441 | } // namespace tests |
| 442 | } // namespace dump |
| 443 | } // namespace ndn |