blob: e734f372f7fdb9293c3bb039de98427050ada0b9 [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 Pesaventob5b8f952018-07-26 14:19:16 -0400194BOOST_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 Pesaventoecd44802018-07-23 23:48:10 -0400205BOOST_AUTO_TEST_CASE(PcapTraceFile)
Vince Lehman277ecf02016-02-10 16:37:48 -0600206{
207 dump.inputFile = "tests/dump/nack.pcap";
Davide Pesaventoecd44802018-07-23 23:48:10 -0400208 dump.pcapFilter = "";
Vince Lehman277ecf02016-02-10 16:37:48 -0600209
210 {
211 StdCoutRedirector redirect(output);
212 dump.run();
213 }
214
Davide Pesaventoecd44802018-07-23 23:48:10 -0400215 const std::string expected =
Davide Pesaventod1b1bf62018-07-26 18:05:08 -0400216 "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
245BOOST_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
261BOOST_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 Pesaventoecd44802018-07-23 23:48:10 -0400273 BOOST_CHECK(output.is_equal(expected));
Vince Lehman277ecf02016-02-10 16:37:48 -0600274}
275
Davide Pesaventoecd44802018-07-23 23:48:10 -0400276BOOST_AUTO_TEST_SUITE_END() // TestNdnDump
277BOOST_AUTO_TEST_SUITE_END() // Dump
Vince Lehman277ecf02016-02-10 16:37:48 -0600278
279} // namespace tests
280} // namespace dump
281} // namespace ndn