Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, 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 | |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 22 | #include <ndn-cxx/lp/packet.hpp> |
Junxiao Shi | c9575a6 | 2017-07-01 06:16:02 +0000 | [diff] [blame^] | 23 | #include <ndn-cxx/net/ethernet.hpp> |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 24 | #include <ndn-cxx/security/key-chain.hpp> |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 25 | |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 26 | #include "tests/test-common.hpp" |
| 27 | #include "tests/identity-management-fixture.hpp" |
| 28 | #include <boost/test/output_test_stream.hpp> |
| 29 | |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 30 | namespace ndn { |
| 31 | namespace dump { |
| 32 | namespace tests { |
| 33 | |
| 34 | using namespace ndn::tests; |
| 35 | |
| 36 | class StdCoutRedirector |
| 37 | { |
| 38 | public: |
| 39 | StdCoutRedirector(std::ostream& os) |
| 40 | { |
| 41 | // Redirect std::cout to the specified output stream |
| 42 | originalBuffer = std::cout.rdbuf(os.rdbuf()); |
| 43 | } |
| 44 | |
| 45 | ~StdCoutRedirector() |
| 46 | { |
| 47 | // Revert state for std::cout |
| 48 | std::cout.rdbuf(originalBuffer); |
| 49 | } |
| 50 | |
| 51 | private: |
| 52 | std::streambuf* originalBuffer; |
| 53 | }; |
| 54 | |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 55 | class NdnDumpFixture : public IdentityManagementFixture |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 56 | { |
| 57 | protected: |
| 58 | NdnDumpFixture() |
| 59 | { |
| 60 | dump.m_dataLinkType = DLT_EN10MB; |
| 61 | } |
| 62 | |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 63 | template<typename Packet> |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 64 | void |
| 65 | receive(const Packet& packet) |
| 66 | { |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 67 | EncodingBuffer buffer(packet.wireEncode()); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 68 | receive(buffer); |
| 69 | } |
| 70 | |
| 71 | void |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 72 | receive(EncodingBuffer& buffer) |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 73 | { |
Junxiao Shi | c9575a6 | 2017-07-01 06:16:02 +0000 | [diff] [blame^] | 74 | ethernet::Address host; |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 75 | |
| 76 | // Ethernet header |
Junxiao Shi | c9575a6 | 2017-07-01 06:16:02 +0000 | [diff] [blame^] | 77 | uint16_t frameType = htons(ethernet::ETHERTYPE_NDN); |
| 78 | buffer.prependByteArray(reinterpret_cast<const uint8_t*>(&frameType), ethernet::TYPE_LEN); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 79 | buffer.prependByteArray(host.data(), host.size()); |
| 80 | buffer.prependByteArray(host.data(), host.size()); |
| 81 | |
| 82 | pcap_pkthdr header{}; |
| 83 | header.len = buffer.size(); |
| 84 | |
| 85 | { |
| 86 | StdCoutRedirector redirect(output); |
| 87 | dump.onCapturedPacket(&header, buffer.buf()); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | protected: |
| 92 | Ndndump dump; |
| 93 | boost::test_tools::output_test_stream output; |
| 94 | }; |
| 95 | |
| 96 | BOOST_FIXTURE_TEST_SUITE(NdnDump, NdnDumpFixture) |
| 97 | |
| 98 | BOOST_AUTO_TEST_CASE(CaptureInterest) |
| 99 | { |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 100 | Interest interest("/test"); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 101 | interest.setNonce(0); |
| 102 | |
| 103 | this->receive(interest); |
| 104 | |
| 105 | const std::string expectedOutput = |
| 106 | "0.000000 Tunnel Type: EthernetFrame, INTEREST: /test?ndn.Nonce=0\n"; |
| 107 | |
| 108 | BOOST_CHECK(output.is_equal(expectedOutput)); |
| 109 | } |
| 110 | |
| 111 | BOOST_AUTO_TEST_CASE(CaptureData) |
| 112 | { |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 113 | Data data("/test"); |
| 114 | m_keyChain.sign(data); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 115 | |
| 116 | this->receive(data); |
| 117 | |
| 118 | const std::string expectedOutput = "0.000000 Tunnel Type: EthernetFrame, DATA: /test\n"; |
| 119 | |
| 120 | BOOST_CHECK(output.is_equal(expectedOutput)); |
| 121 | } |
| 122 | |
| 123 | BOOST_AUTO_TEST_CASE(CaptureNack) |
| 124 | { |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 125 | Interest interest("/test"); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 126 | interest.setNonce(0); |
| 127 | |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 128 | lp::Nack nack(interest); |
| 129 | nack.setReason(lp::NackReason::DUPLICATE); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 130 | |
| 131 | lp::Packet lpPacket(interest.wireEncode()); |
| 132 | lpPacket.add<lp::NackField>(nack.getHeader()); |
| 133 | |
| 134 | this->receive(lpPacket); |
| 135 | |
| 136 | const std::string expectedOutput = |
| 137 | "0.000000 Tunnel Type: EthernetFrame, NACK: Duplicate, /test?ndn.Nonce=0\n"; |
| 138 | |
| 139 | BOOST_CHECK(output.is_equal(expectedOutput)); |
| 140 | } |
| 141 | |
| 142 | BOOST_AUTO_TEST_CASE(CaptureLpFragment) |
| 143 | { |
| 144 | const uint8_t data[10] = { |
| 145 | 0x06, 0x08, // Data packet |
| 146 | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 147 | }; |
| 148 | |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 149 | Buffer buffer(data, 4); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 150 | |
| 151 | lp::Packet lpPacket; |
| 152 | lpPacket.add<lp::FragmentField>(std::make_pair(buffer.begin(), buffer.end())); |
| 153 | lpPacket.add<lp::FragIndexField>(0); |
| 154 | lpPacket.add<lp::FragCountField>(2); |
| 155 | lpPacket.add<lp::SequenceField>(1000); |
| 156 | |
| 157 | this->receive(lpPacket); |
| 158 | |
| 159 | const std::string expectedOutput = |
| 160 | "0.000000 Tunnel Type: EthernetFrame, NDNLPv2-FRAGMENT\n"; |
| 161 | |
| 162 | BOOST_CHECK(output.is_equal(expectedOutput)); |
| 163 | } |
| 164 | |
| 165 | BOOST_AUTO_TEST_CASE(CaptureIdlePacket) |
| 166 | { |
| 167 | lp::Packet lpPacket; |
| 168 | |
| 169 | this->receive(lpPacket); |
| 170 | |
| 171 | const std::string expectedOutput = |
| 172 | "0.000000 Tunnel Type: EthernetFrame, NDNLPv2-IDLE\n"; |
| 173 | |
| 174 | BOOST_CHECK(output.is_equal(expectedOutput)); |
| 175 | } |
| 176 | |
| 177 | BOOST_AUTO_TEST_CASE(CaptureIncompletePacket) |
| 178 | { |
| 179 | const uint8_t interest[] = { |
| 180 | 0x05, 0x0E, // Interest |
| 181 | 0x07, 0x06, // Name |
| 182 | 0x08, 0x04, // NameComponent |
| 183 | 0x74, 0x65, 0x73, 0x74, |
| 184 | 0x0a, 0x04, // Nonce |
| 185 | 0x00, 0x00, 0x00, 0x01 |
| 186 | }; |
| 187 | |
| 188 | EncodingBuffer buffer; |
| 189 | buffer.prependByteArray(interest, 4); |
| 190 | |
| 191 | this->receive(buffer); |
| 192 | |
| 193 | const std::string expectedOutput = |
| 194 | "0.000000 Tunnel Type: EthernetFrame, INCOMPLETE-PACKET, size: 4\n"; |
| 195 | |
| 196 | BOOST_CHECK(output.is_equal(expectedOutput)); |
| 197 | } |
| 198 | |
| 199 | BOOST_AUTO_TEST_CASE(CaptureUnknownNetworkPacket) |
| 200 | { |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 201 | EncodingBuffer buffer(encoding::makeEmptyBlock(tlv::Name)); |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 202 | |
| 203 | this->receive(buffer); |
| 204 | |
| 205 | const std::string expectedOutput = |
| 206 | "0.000000 Tunnel Type: EthernetFrame, UNKNOWN-NETWORK-PACKET\n"; |
| 207 | |
| 208 | BOOST_CHECK(output.is_equal(expectedOutput)); |
| 209 | } |
| 210 | |
| 211 | BOOST_AUTO_TEST_CASE(DumpPcapTrace) |
| 212 | { |
| 213 | dump.inputFile = "tests/dump/nack.pcap"; |
| 214 | dump.pcapProgram = ""; |
| 215 | |
| 216 | { |
| 217 | StdCoutRedirector redirect(output); |
| 218 | dump.run(); |
| 219 | } |
| 220 | |
| 221 | const std::string expectedOutput = |
| 222 | "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" |
| 223 | "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" |
| 224 | "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" |
| 225 | "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" |
| 226 | "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" |
| 227 | "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" |
| 228 | "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" |
| 229 | "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" |
| 230 | "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" |
| 231 | "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" |
| 232 | "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" |
| 233 | "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" |
| 234 | "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"; |
| 235 | |
| 236 | BOOST_CHECK(output.is_equal(expectedOutput)); |
| 237 | } |
| 238 | |
| 239 | |
| 240 | BOOST_AUTO_TEST_SUITE_END() |
| 241 | |
| 242 | } // namespace tests |
| 243 | } // namespace dump |
| 244 | } // namespace ndn |