blob: 7583f08de74718bfe2dbb85728a3b6db083273bb [file] [log] [blame]
Vince Lehman277ecf02016-02-10 16:37:48 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento60f8cc12018-05-10 22:05:21 -04002/*
3 * Copyright (c) 2014-2018, University of Memphis.
Vince Lehman277ecf02016-02-10 16:37:48 -06004 *
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 Pesavento60f8cc12018-05-10 22:05:21 -040022#include "tests/identity-management-fixture.hpp"
23#include "tests/test-common.hpp"
24
Vince Lehman277ecf02016-02-10 16:37:48 -060025#include <ndn-cxx/lp/packet.hpp>
Junxiao Shic9575a62017-07-01 06:16:02 +000026#include <ndn-cxx/net/ethernet.hpp>
Junxiao Shi20b22972017-05-24 21:04:16 +000027
Vince Lehman277ecf02016-02-10 16:37:48 -060028namespace ndn {
29namespace dump {
30namespace tests {
31
32using namespace ndn::tests;
33
34class StdCoutRedirector
35{
36public:
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
49private:
50 std::streambuf* originalBuffer;
51};
52
Junxiao Shi20b22972017-05-24 21:04:16 +000053class NdnDumpFixture : public IdentityManagementFixture
Vince Lehman277ecf02016-02-10 16:37:48 -060054{
55protected:
56 NdnDumpFixture()
57 {
58 dump.m_dataLinkType = DLT_EN10MB;
59 }
60
Junxiao Shi20b22972017-05-24 21:04:16 +000061 template<typename Packet>
Vince Lehman277ecf02016-02-10 16:37:48 -060062 void
63 receive(const Packet& packet)
64 {
Junxiao Shi20b22972017-05-24 21:04:16 +000065 EncodingBuffer buffer(packet.wireEncode());
Vince Lehman277ecf02016-02-10 16:37:48 -060066 receive(buffer);
67 }
68
69 void
Junxiao Shi20b22972017-05-24 21:04:16 +000070 receive(EncodingBuffer& buffer)
Vince Lehman277ecf02016-02-10 16:37:48 -060071 {
Junxiao Shic9575a62017-07-01 06:16:02 +000072 ethernet::Address host;
Vince Lehman277ecf02016-02-10 16:37:48 -060073
74 // Ethernet header
Davide Pesaventoecd44802018-07-23 23:48:10 -040075 uint16_t type = htons(ethernet::ETHERTYPE_NDN);
76 buffer.prependByteArray(reinterpret_cast<const uint8_t*>(&type), ethernet::TYPE_LEN);
Vince Lehman277ecf02016-02-10 16:37:48 -060077 buffer.prependByteArray(host.data(), host.size());
78 buffer.prependByteArray(host.data(), host.size());
79
Davide Pesaventoecd44802018-07-23 23:48:10 -040080 pcap_pkthdr pkthdr{};
81 pkthdr.caplen = pkthdr.len = buffer.size();
Vince Lehman277ecf02016-02-10 16:37:48 -060082
83 {
84 StdCoutRedirector redirect(output);
Davide Pesaventoecd44802018-07-23 23:48:10 -040085 dump.printPacket(&pkthdr, buffer.buf());
Vince Lehman277ecf02016-02-10 16:37:48 -060086 }
87 }
88
89protected:
Davide Pesaventoecd44802018-07-23 23:48:10 -040090 NdnDump dump;
Vince Lehman277ecf02016-02-10 16:37:48 -060091 boost::test_tools::output_test_stream output;
92};
93
Davide Pesaventoecd44802018-07-23 23:48:10 -040094BOOST_AUTO_TEST_SUITE(Dump)
95BOOST_FIXTURE_TEST_SUITE(TestNdnDump, NdnDumpFixture)
Vince Lehman277ecf02016-02-10 16:37:48 -060096
97BOOST_AUTO_TEST_CASE(CaptureInterest)
98{
Junxiao Shi20b22972017-05-24 21:04:16 +000099 Interest interest("/test");
Vince Lehman277ecf02016-02-10 16:37:48 -0600100 interest.setNonce(0);
101
102 this->receive(interest);
103
Davide Pesaventoecd44802018-07-23 23:48:10 -0400104 const std::string expected = "0.000000 Tunnel Type: EthernetFrame, INTEREST: /test?ndn.Nonce=0\n";
105 BOOST_CHECK(output.is_equal(expected));
Vince Lehman277ecf02016-02-10 16:37:48 -0600106}
107
108BOOST_AUTO_TEST_CASE(CaptureData)
109{
Junxiao Shi20b22972017-05-24 21:04:16 +0000110 Data data("/test");
111 m_keyChain.sign(data);
Vince Lehman277ecf02016-02-10 16:37:48 -0600112
113 this->receive(data);
114
Davide Pesaventoecd44802018-07-23 23:48:10 -0400115 const std::string expected = "0.000000 Tunnel Type: EthernetFrame, DATA: /test\n";
116 BOOST_CHECK(output.is_equal(expected));
Vince Lehman277ecf02016-02-10 16:37:48 -0600117}
118
119BOOST_AUTO_TEST_CASE(CaptureNack)
120{
Junxiao Shi20b22972017-05-24 21:04:16 +0000121 Interest interest("/test");
Vince Lehman277ecf02016-02-10 16:37:48 -0600122 interest.setNonce(0);
Junxiao Shi20b22972017-05-24 21:04:16 +0000123 lp::Nack nack(interest);
124 nack.setReason(lp::NackReason::DUPLICATE);
Vince Lehman277ecf02016-02-10 16:37:48 -0600125 lp::Packet lpPacket(interest.wireEncode());
126 lpPacket.add<lp::NackField>(nack.getHeader());
127
128 this->receive(lpPacket);
129
Davide Pesaventoecd44802018-07-23 23:48:10 -0400130 const std::string expected = "0.000000 Tunnel Type: EthernetFrame, NACK: Duplicate, /test?ndn.Nonce=0\n";
131 BOOST_CHECK(output.is_equal(expected));
Vince Lehman277ecf02016-02-10 16:37:48 -0600132}
133
134BOOST_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 Shi20b22972017-05-24 21:04:16 +0000141 Buffer buffer(data, 4);
Vince Lehman277ecf02016-02-10 16:37:48 -0600142 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 Pesaventoecd44802018-07-23 23:48:10 -0400150 const std::string expected = "0.000000 Tunnel Type: EthernetFrame, NDNLPv2-FRAGMENT\n";
151 BOOST_CHECK(output.is_equal(expected));
Vince Lehman277ecf02016-02-10 16:37:48 -0600152}
153
Davide Pesaventoecd44802018-07-23 23:48:10 -0400154BOOST_AUTO_TEST_CASE(CaptureIdleLpPacket)
Vince Lehman277ecf02016-02-10 16:37:48 -0600155{
156 lp::Packet lpPacket;
157
158 this->receive(lpPacket);
159
Davide Pesaventoecd44802018-07-23 23:48:10 -0400160 const std::string expected = "0.000000 Tunnel Type: EthernetFrame, NDNLPv2-IDLE\n";
161 BOOST_CHECK(output.is_equal(expected));
Vince Lehman277ecf02016-02-10 16:37:48 -0600162}
163
164BOOST_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 Pesaventoecd44802018-07-23 23:48:10 -0400180 const std::string expected = "0.000000 Tunnel Type: EthernetFrame, INCOMPLETE-PACKET, size: 4\n";
181 BOOST_CHECK(output.is_equal(expected));
Vince Lehman277ecf02016-02-10 16:37:48 -0600182}
183
184BOOST_AUTO_TEST_CASE(CaptureUnknownNetworkPacket)
185{
Junxiao Shi20b22972017-05-24 21:04:16 +0000186 EncodingBuffer buffer(encoding::makeEmptyBlock(tlv::Name));
Vince Lehman277ecf02016-02-10 16:37:48 -0600187
188 this->receive(buffer);
189
Davide Pesaventoecd44802018-07-23 23:48:10 -0400190 const std::string expected = "0.000000 Tunnel Type: EthernetFrame, UNKNOWN-NETWORK-PACKET\n";
191 BOOST_CHECK(output.is_equal(expected));
Vince Lehman277ecf02016-02-10 16:37:48 -0600192}
193
Davide Pesaventoecd44802018-07-23 23:48:10 -0400194BOOST_AUTO_TEST_CASE(PcapTraceFile)
Vince Lehman277ecf02016-02-10 16:37:48 -0600195{
196 dump.inputFile = "tests/dump/nack.pcap";
Davide Pesaventoecd44802018-07-23 23:48:10 -0400197 dump.pcapFilter = "";
Vince Lehman277ecf02016-02-10 16:37:48 -0600198
199 {
200 StdCoutRedirector redirect(output);
201 dump.run();
202 }
203
Davide Pesaventoecd44802018-07-23 23:48:10 -0400204 const std::string expected =
Vince Lehman277ecf02016-02-10 16:37:48 -0600205 "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"
206 "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"
207 "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"
208 "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"
209 "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"
210 "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"
211 "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"
212 "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"
213 "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"
214 "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"
215 "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"
216 "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"
217 "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";
Davide Pesaventoecd44802018-07-23 23:48:10 -0400218 BOOST_CHECK(output.is_equal(expected));
Vince Lehman277ecf02016-02-10 16:37:48 -0600219}
220
Davide Pesaventoecd44802018-07-23 23:48:10 -0400221BOOST_AUTO_TEST_SUITE_END() // TestNdnDump
222BOOST_AUTO_TEST_SUITE_END() // Dump
Vince Lehman277ecf02016-02-10 16:37:48 -0600223
224} // namespace tests
225} // namespace dump
226} // namespace ndn