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 |
Junxiao Shi | c9575a6 | 2017-07-01 06:16:02 +0000 | [diff] [blame] | 75 | uint16_t frameType = htons(ethernet::ETHERTYPE_NDN); |
| 76 | buffer.prependByteArray(reinterpret_cast<const uint8_t*>(&frameType), 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 | |
| 80 | pcap_pkthdr header{}; |
| 81 | header.len = buffer.size(); |
| 82 | |
| 83 | { |
| 84 | StdCoutRedirector redirect(output); |
| 85 | dump.onCapturedPacket(&header, buffer.buf()); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | protected: |
| 90 | Ndndump dump; |
| 91 | boost::test_tools::output_test_stream output; |
| 92 | }; |
| 93 | |
| 94 | BOOST_FIXTURE_TEST_SUITE(NdnDump, NdnDumpFixture) |
| 95 | |
| 96 | BOOST_AUTO_TEST_CASE(CaptureInterest) |
| 97 | { |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 98 | Interest interest("/test"); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 99 | interest.setNonce(0); |
| 100 | |
| 101 | this->receive(interest); |
| 102 | |
| 103 | const std::string expectedOutput = |
| 104 | "0.000000 Tunnel Type: EthernetFrame, INTEREST: /test?ndn.Nonce=0\n"; |
| 105 | |
| 106 | BOOST_CHECK(output.is_equal(expectedOutput)); |
| 107 | } |
| 108 | |
| 109 | BOOST_AUTO_TEST_CASE(CaptureData) |
| 110 | { |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 111 | Data data("/test"); |
| 112 | m_keyChain.sign(data); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 113 | |
| 114 | this->receive(data); |
| 115 | |
| 116 | const std::string expectedOutput = "0.000000 Tunnel Type: EthernetFrame, DATA: /test\n"; |
| 117 | |
| 118 | BOOST_CHECK(output.is_equal(expectedOutput)); |
| 119 | } |
| 120 | |
| 121 | BOOST_AUTO_TEST_CASE(CaptureNack) |
| 122 | { |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 123 | Interest interest("/test"); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 124 | interest.setNonce(0); |
| 125 | |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 126 | lp::Nack nack(interest); |
| 127 | nack.setReason(lp::NackReason::DUPLICATE); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 128 | |
| 129 | lp::Packet lpPacket(interest.wireEncode()); |
| 130 | lpPacket.add<lp::NackField>(nack.getHeader()); |
| 131 | |
| 132 | this->receive(lpPacket); |
| 133 | |
| 134 | const std::string expectedOutput = |
| 135 | "0.000000 Tunnel Type: EthernetFrame, NACK: Duplicate, /test?ndn.Nonce=0\n"; |
| 136 | |
| 137 | BOOST_CHECK(output.is_equal(expectedOutput)); |
| 138 | } |
| 139 | |
| 140 | BOOST_AUTO_TEST_CASE(CaptureLpFragment) |
| 141 | { |
| 142 | const uint8_t data[10] = { |
| 143 | 0x06, 0x08, // Data packet |
| 144 | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 145 | }; |
| 146 | |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 147 | Buffer buffer(data, 4); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 148 | |
| 149 | lp::Packet lpPacket; |
| 150 | lpPacket.add<lp::FragmentField>(std::make_pair(buffer.begin(), buffer.end())); |
| 151 | lpPacket.add<lp::FragIndexField>(0); |
| 152 | lpPacket.add<lp::FragCountField>(2); |
| 153 | lpPacket.add<lp::SequenceField>(1000); |
| 154 | |
| 155 | this->receive(lpPacket); |
| 156 | |
| 157 | const std::string expectedOutput = |
| 158 | "0.000000 Tunnel Type: EthernetFrame, NDNLPv2-FRAGMENT\n"; |
| 159 | |
| 160 | BOOST_CHECK(output.is_equal(expectedOutput)); |
| 161 | } |
| 162 | |
| 163 | BOOST_AUTO_TEST_CASE(CaptureIdlePacket) |
| 164 | { |
| 165 | lp::Packet lpPacket; |
| 166 | |
| 167 | this->receive(lpPacket); |
| 168 | |
| 169 | const std::string expectedOutput = |
| 170 | "0.000000 Tunnel Type: EthernetFrame, NDNLPv2-IDLE\n"; |
| 171 | |
| 172 | BOOST_CHECK(output.is_equal(expectedOutput)); |
| 173 | } |
| 174 | |
| 175 | BOOST_AUTO_TEST_CASE(CaptureIncompletePacket) |
| 176 | { |
| 177 | const uint8_t interest[] = { |
| 178 | 0x05, 0x0E, // Interest |
| 179 | 0x07, 0x06, // Name |
| 180 | 0x08, 0x04, // NameComponent |
| 181 | 0x74, 0x65, 0x73, 0x74, |
| 182 | 0x0a, 0x04, // Nonce |
| 183 | 0x00, 0x00, 0x00, 0x01 |
| 184 | }; |
| 185 | |
| 186 | EncodingBuffer buffer; |
| 187 | buffer.prependByteArray(interest, 4); |
| 188 | |
| 189 | this->receive(buffer); |
| 190 | |
| 191 | const std::string expectedOutput = |
| 192 | "0.000000 Tunnel Type: EthernetFrame, INCOMPLETE-PACKET, size: 4\n"; |
| 193 | |
| 194 | BOOST_CHECK(output.is_equal(expectedOutput)); |
| 195 | } |
| 196 | |
| 197 | BOOST_AUTO_TEST_CASE(CaptureUnknownNetworkPacket) |
| 198 | { |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 199 | EncodingBuffer buffer(encoding::makeEmptyBlock(tlv::Name)); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 200 | |
| 201 | this->receive(buffer); |
| 202 | |
| 203 | const std::string expectedOutput = |
| 204 | "0.000000 Tunnel Type: EthernetFrame, UNKNOWN-NETWORK-PACKET\n"; |
| 205 | |
| 206 | BOOST_CHECK(output.is_equal(expectedOutput)); |
| 207 | } |
| 208 | |
| 209 | BOOST_AUTO_TEST_CASE(DumpPcapTrace) |
| 210 | { |
| 211 | dump.inputFile = "tests/dump/nack.pcap"; |
| 212 | dump.pcapProgram = ""; |
| 213 | |
| 214 | { |
| 215 | StdCoutRedirector redirect(output); |
| 216 | dump.run(); |
| 217 | } |
| 218 | |
| 219 | const std::string expectedOutput = |
| 220 | "1456768916.467099 From: 1.0.0.1, To: 1.0.0.2, Tunnel Type: UDP, INTEREST: /producer/nack/congestion?ndn.MustBeFresh=1&ndn.Nonce=2581361680\n" |
| 221 | "1456768916.567099 From: 1.0.0.1, To: 1.0.0.2, Tunnel Type: UDP, INTEREST: /producer/nack/duplicate?ndn.MustBeFresh=1&ndn.Nonce=4138343109\n" |
| 222 | "1456768916.667099 From: 1.0.0.1, To: 1.0.0.2, Tunnel Type: UDP, INTEREST: /producer/nack/no-reason?ndn.MustBeFresh=1&ndn.Nonce=4034910304\n" |
| 223 | "1456768916.767099 From: 1.0.0.2, To: 1.0.0.1, Tunnel Type: UDP, 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, NACK: Duplicate, /producer/nack/duplicate?ndn.MustBeFresh=1&ndn.Nonce=4138343109\n" |
| 225 | "1456768916.967099 From: 1.0.0.2, To: 1.0.0.1, Tunnel Type: UDP, NACK: None, /producer/nack/no-reason?ndn.MustBeFresh=1&ndn.Nonce=4034910304\n" |
| 226 | "1456768917.067099 From: 1.0.0.1, To: 1.0.0.2, Tunnel Type: TCP, INTEREST: /producer/nack/congestion?ndn.MustBeFresh=1&ndn.Nonce=3192497423\n" |
| 227 | "1456768917.267099 From: 1.0.0.2, To: 1.0.0.1, Tunnel Type: TCP, NACK: Congestion, /producer/nack/congestion?ndn.MustBeFresh=1&ndn.Nonce=3192497423\n" |
| 228 | "1456768917.367099 From: 1.0.0.1, To: 1.0.0.2, Tunnel Type: TCP, INTEREST: /producer/nack/duplicate?ndn.MustBeFresh=1&ndn.Nonce=522390724\n" |
| 229 | "1456768917.567099 From: 1.0.0.2, To: 1.0.0.1, Tunnel Type: TCP, NACK: Duplicate, /producer/nack/duplicate?ndn.MustBeFresh=1&ndn.Nonce=522390724\n" |
| 230 | "1456768917.767099 From: 1.0.0.2, To: 1.0.0.1, Tunnel Type: TCP, NACK: None, /producer/nack/no-reason?ndn.MustBeFresh=1&ndn.Nonce=2002441365\n" |
| 231 | "1456768917.967099 From: 1.0.0.1, To: 1.0.0.2, Tunnel Type: TCP, INTEREST: /producer/nack/no-reason?ndn.MustBeFresh=1&ndn.Nonce=3776824408\n" |
| 232 | "1456768918.067099 From: 1.0.0.2, To: 1.0.0.1, Tunnel Type: TCP, NACK: None, /producer/nack/no-reason?ndn.MustBeFresh=1&ndn.Nonce=3776824408\n"; |
| 233 | |
| 234 | BOOST_CHECK(output.is_equal(expectedOutput)); |
| 235 | } |
| 236 | |
| 237 | |
| 238 | BOOST_AUTO_TEST_SUITE_END() |
| 239 | |
| 240 | } // namespace tests |
| 241 | } // namespace dump |
| 242 | } // namespace ndn |