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 | /* |
| 3 | * Copyright (c) 2014-2018, University of Memphis. |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-tools (Named Data Networking Essential Tools). |
| 6 | * See AUTHORS.md for complete list of ndn-tools authors and contributors. |
| 7 | * |
| 8 | * ndn-tools is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include "tools/dump/ndndump.hpp" |
| 21 | |
Davide Pesavento | 60f8cc1 | 2018-05-10 22:05:21 -0400 | [diff] [blame] | 22 | #include "tests/identity-management-fixture.hpp" |
| 23 | #include "tests/test-common.hpp" |
| 24 | |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 25 | #include <ndn-cxx/lp/packet.hpp> |
Junxiao Shi | c9575a6 | 2017-07-01 06:16:02 +0000 | [diff] [blame] | 26 | #include <ndn-cxx/net/ethernet.hpp> |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 27 | |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 28 | namespace ndn { |
| 29 | namespace dump { |
| 30 | namespace tests { |
| 31 | |
| 32 | using namespace ndn::tests; |
| 33 | |
| 34 | class StdCoutRedirector |
| 35 | { |
| 36 | public: |
| 37 | StdCoutRedirector(std::ostream& os) |
| 38 | { |
| 39 | // Redirect std::cout to the specified output stream |
| 40 | originalBuffer = std::cout.rdbuf(os.rdbuf()); |
| 41 | } |
| 42 | |
| 43 | ~StdCoutRedirector() |
| 44 | { |
| 45 | // Revert state for std::cout |
| 46 | std::cout.rdbuf(originalBuffer); |
| 47 | } |
| 48 | |
| 49 | private: |
| 50 | std::streambuf* originalBuffer; |
| 51 | }; |
| 52 | |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 53 | class NdnDumpFixture : public IdentityManagementFixture |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 54 | { |
| 55 | protected: |
| 56 | NdnDumpFixture() |
| 57 | { |
| 58 | dump.m_dataLinkType = DLT_EN10MB; |
| 59 | } |
| 60 | |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 61 | template<typename Packet> |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 62 | void |
| 63 | receive(const Packet& packet) |
| 64 | { |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 65 | EncodingBuffer buffer(packet.wireEncode()); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 66 | receive(buffer); |
| 67 | } |
| 68 | |
| 69 | void |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 70 | receive(EncodingBuffer& buffer) |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 71 | { |
Junxiao Shi | c9575a6 | 2017-07-01 06:16:02 +0000 | [diff] [blame] | 72 | ethernet::Address host; |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 73 | |
| 74 | // Ethernet header |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 75 | uint16_t type = htons(ethernet::ETHERTYPE_NDN); |
| 76 | buffer.prependByteArray(reinterpret_cast<const uint8_t*>(&type), ethernet::TYPE_LEN); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 77 | buffer.prependByteArray(host.data(), host.size()); |
| 78 | buffer.prependByteArray(host.data(), host.size()); |
| 79 | |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 80 | pcap_pkthdr pkthdr{}; |
| 81 | pkthdr.caplen = pkthdr.len = buffer.size(); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 82 | |
| 83 | { |
| 84 | StdCoutRedirector redirect(output); |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 85 | dump.printPacket(&pkthdr, buffer.buf()); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
| 89 | protected: |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 90 | NdnDump dump; |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 91 | boost::test_tools::output_test_stream output; |
| 92 | }; |
| 93 | |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 94 | BOOST_AUTO_TEST_SUITE(Dump) |
| 95 | BOOST_FIXTURE_TEST_SUITE(TestNdnDump, NdnDumpFixture) |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 96 | |
| 97 | BOOST_AUTO_TEST_CASE(CaptureInterest) |
| 98 | { |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 99 | Interest interest("/test"); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 100 | interest.setNonce(0); |
| 101 | |
| 102 | this->receive(interest); |
| 103 | |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 104 | const std::string expected = "0.000000 Tunnel Type: EthernetFrame, INTEREST: /test?ndn.Nonce=0\n"; |
| 105 | BOOST_CHECK(output.is_equal(expected)); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | BOOST_AUTO_TEST_CASE(CaptureData) |
| 109 | { |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 110 | Data data("/test"); |
| 111 | m_keyChain.sign(data); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 112 | |
| 113 | this->receive(data); |
| 114 | |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 115 | const std::string expected = "0.000000 Tunnel Type: EthernetFrame, DATA: /test\n"; |
| 116 | BOOST_CHECK(output.is_equal(expected)); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | BOOST_AUTO_TEST_CASE(CaptureNack) |
| 120 | { |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 121 | Interest interest("/test"); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 122 | interest.setNonce(0); |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 123 | lp::Nack nack(interest); |
| 124 | nack.setReason(lp::NackReason::DUPLICATE); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 125 | lp::Packet lpPacket(interest.wireEncode()); |
| 126 | lpPacket.add<lp::NackField>(nack.getHeader()); |
| 127 | |
| 128 | this->receive(lpPacket); |
| 129 | |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 130 | const std::string expected = "0.000000 Tunnel Type: EthernetFrame, NACK: Duplicate, /test?ndn.Nonce=0\n"; |
| 131 | BOOST_CHECK(output.is_equal(expected)); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | BOOST_AUTO_TEST_CASE(CaptureLpFragment) |
| 135 | { |
| 136 | const uint8_t data[10] = { |
| 137 | 0x06, 0x08, // Data packet |
| 138 | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 139 | }; |
| 140 | |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 141 | Buffer buffer(data, 4); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 142 | lp::Packet lpPacket; |
| 143 | lpPacket.add<lp::FragmentField>(std::make_pair(buffer.begin(), buffer.end())); |
| 144 | lpPacket.add<lp::FragIndexField>(0); |
| 145 | lpPacket.add<lp::FragCountField>(2); |
| 146 | lpPacket.add<lp::SequenceField>(1000); |
| 147 | |
| 148 | this->receive(lpPacket); |
| 149 | |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 150 | const std::string expected = "0.000000 Tunnel Type: EthernetFrame, NDNLPv2-FRAGMENT\n"; |
| 151 | BOOST_CHECK(output.is_equal(expected)); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 152 | } |
| 153 | |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 154 | BOOST_AUTO_TEST_CASE(CaptureIdleLpPacket) |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 155 | { |
| 156 | lp::Packet lpPacket; |
| 157 | |
| 158 | this->receive(lpPacket); |
| 159 | |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 160 | const std::string expected = "0.000000 Tunnel Type: EthernetFrame, NDNLPv2-IDLE\n"; |
| 161 | BOOST_CHECK(output.is_equal(expected)); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | BOOST_AUTO_TEST_CASE(CaptureIncompletePacket) |
| 165 | { |
| 166 | const uint8_t interest[] = { |
| 167 | 0x05, 0x0E, // Interest |
| 168 | 0x07, 0x06, // Name |
| 169 | 0x08, 0x04, // NameComponent |
| 170 | 0x74, 0x65, 0x73, 0x74, |
| 171 | 0x0a, 0x04, // Nonce |
| 172 | 0x00, 0x00, 0x00, 0x01 |
| 173 | }; |
| 174 | |
| 175 | EncodingBuffer buffer; |
| 176 | buffer.prependByteArray(interest, 4); |
| 177 | |
| 178 | this->receive(buffer); |
| 179 | |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 180 | const std::string expected = "0.000000 Tunnel Type: EthernetFrame, INCOMPLETE-PACKET, size: 4\n"; |
| 181 | BOOST_CHECK(output.is_equal(expected)); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | BOOST_AUTO_TEST_CASE(CaptureUnknownNetworkPacket) |
| 185 | { |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 186 | EncodingBuffer buffer(encoding::makeEmptyBlock(tlv::Name)); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 187 | |
| 188 | this->receive(buffer); |
| 189 | |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 190 | const std::string expected = "0.000000 Tunnel Type: EthernetFrame, UNKNOWN-NETWORK-PACKET\n"; |
| 191 | BOOST_CHECK(output.is_equal(expected)); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 192 | } |
| 193 | |
Davide Pesavento | b5b8f95 | 2018-07-26 14:19:16 -0400 | [diff] [blame] | 194 | BOOST_AUTO_TEST_CASE(NoTimestamp) |
| 195 | { |
| 196 | dump.wantTimestamp = false; |
| 197 | |
| 198 | lp::Packet lpPacket; |
| 199 | this->receive(lpPacket); |
| 200 | |
| 201 | const std::string expected = "Tunnel Type: EthernetFrame, NDNLPv2-IDLE\n"; |
| 202 | BOOST_CHECK(output.is_equal(expected)); |
| 203 | } |
| 204 | |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 205 | BOOST_AUTO_TEST_CASE(PcapTraceFile) |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 206 | { |
| 207 | dump.inputFile = "tests/dump/nack.pcap"; |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 208 | dump.pcapFilter = ""; |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 209 | |
| 210 | { |
| 211 | StdCoutRedirector redirect(output); |
| 212 | dump.run(); |
| 213 | } |
| 214 | |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 215 | const std::string expected = |
Davide Pesavento | d1b1bf6 | 2018-07-26 18:05:08 -0400 | [diff] [blame] | 216 | "1456768916.467099 From: 1.0.0.1, To: 1.0.0.2, Tunnel Type: UDP, " |
| 217 | "INTEREST: /producer/nack/congestion?ndn.MustBeFresh=1&ndn.Nonce=2581361680\n" |
| 218 | "1456768916.567099 From: 1.0.0.1, To: 1.0.0.2, Tunnel Type: UDP, " |
| 219 | "INTEREST: /producer/nack/duplicate?ndn.MustBeFresh=1&ndn.Nonce=4138343109\n" |
| 220 | "1456768916.667099 From: 1.0.0.1, To: 1.0.0.2, Tunnel Type: UDP, " |
| 221 | "INTEREST: /producer/nack/no-reason?ndn.MustBeFresh=1&ndn.Nonce=4034910304\n" |
| 222 | "1456768916.767099 From: 1.0.0.2, To: 1.0.0.1, Tunnel Type: UDP, " |
| 223 | "NACK: Congestion, /producer/nack/congestion?ndn.MustBeFresh=1&ndn.Nonce=2581361680\n" |
| 224 | "1456768916.867099 From: 1.0.0.2, To: 1.0.0.1, Tunnel Type: UDP, " |
| 225 | "NACK: Duplicate, /producer/nack/duplicate?ndn.MustBeFresh=1&ndn.Nonce=4138343109\n" |
| 226 | "1456768916.967099 From: 1.0.0.2, To: 1.0.0.1, Tunnel Type: UDP, " |
| 227 | "NACK: None, /producer/nack/no-reason?ndn.MustBeFresh=1&ndn.Nonce=4034910304\n" |
| 228 | "1456768917.067099 From: 1.0.0.1, To: 1.0.0.2, Tunnel Type: TCP, " |
| 229 | "INTEREST: /producer/nack/congestion?ndn.MustBeFresh=1&ndn.Nonce=3192497423\n" |
| 230 | "1456768917.267099 From: 1.0.0.2, To: 1.0.0.1, Tunnel Type: TCP, " |
| 231 | "NACK: Congestion, /producer/nack/congestion?ndn.MustBeFresh=1&ndn.Nonce=3192497423\n" |
| 232 | "1456768917.367099 From: 1.0.0.1, To: 1.0.0.2, Tunnel Type: TCP, " |
| 233 | "INTEREST: /producer/nack/duplicate?ndn.MustBeFresh=1&ndn.Nonce=522390724\n" |
| 234 | "1456768917.567099 From: 1.0.0.2, To: 1.0.0.1, Tunnel Type: TCP, " |
| 235 | "NACK: Duplicate, /producer/nack/duplicate?ndn.MustBeFresh=1&ndn.Nonce=522390724\n" |
| 236 | "1456768917.767099 From: 1.0.0.2, To: 1.0.0.1, Tunnel Type: TCP, " |
| 237 | "NACK: None, /producer/nack/no-reason?ndn.MustBeFresh=1&ndn.Nonce=2002441365\n" |
| 238 | "1456768917.967099 From: 1.0.0.1, To: 1.0.0.2, Tunnel Type: TCP, " |
| 239 | "INTEREST: /producer/nack/no-reason?ndn.MustBeFresh=1&ndn.Nonce=3776824408\n" |
| 240 | "1456768918.067099 From: 1.0.0.2, To: 1.0.0.1, Tunnel Type: TCP, " |
| 241 | "NACK: None, /producer/nack/no-reason?ndn.MustBeFresh=1&ndn.Nonce=3776824408\n"; |
| 242 | BOOST_CHECK(output.is_equal(expected)); |
| 243 | } |
| 244 | |
| 245 | BOOST_AUTO_TEST_CASE(InvalidTlvLength) |
| 246 | { |
| 247 | dump.inputFile = "tests/dump/invalid-tlv-length.pcap"; |
| 248 | dump.wantTimestamp = false; |
| 249 | |
| 250 | { |
| 251 | StdCoutRedirector redirect(output); |
| 252 | dump.run(); |
| 253 | } |
| 254 | |
| 255 | const std::string expected = |
| 256 | "From: 128.196.203.36, To: 128.187.81.12, Tunnel Type: TCP, INVALID-NDNLPv2-PACKET: " |
| 257 | "TLV-LENGTH of sub-element of type 5 exceeds TLV-VALUE boundary of parent block\n"; |
| 258 | BOOST_CHECK(output.is_equal(expected)); |
| 259 | } |
| 260 | |
| 261 | BOOST_AUTO_TEST_CASE(UnrecognizedLpField) |
| 262 | { |
| 263 | dump.inputFile = "tests/dump/unrecognized-lp-field.pcap"; |
| 264 | dump.wantTimestamp = false; |
| 265 | |
| 266 | { |
| 267 | StdCoutRedirector redirect(output); |
| 268 | dump.run(); |
| 269 | } |
| 270 | |
| 271 | const std::string expected = "From: 128.196.203.36, To: 128.187.81.12, Tunnel Type: TCP, " |
| 272 | "INVALID-NDNLPv2-PACKET: unrecognized field 4 cannot be ignored\n"; |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 273 | BOOST_CHECK(output.is_equal(expected)); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 274 | } |
| 275 | |
Davide Pesavento | ecd4480 | 2018-07-23 23:48:10 -0400 | [diff] [blame] | 276 | BOOST_AUTO_TEST_SUITE_END() // TestNdnDump |
| 277 | BOOST_AUTO_TEST_SUITE_END() // Dump |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 278 | |
| 279 | } // namespace tests |
| 280 | } // namespace dump |
| 281 | } // namespace ndn |