blob: e371fa5988ba34429ac7f0c1d5c864bd17a50506 [file] [log] [blame]
Vince Lehman277ecf02016-02-10 16:37:48 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi20b22972017-05-24 21:04:16 +00003 * Copyright (c) 2014-2017, 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
Vince Lehman277ecf02016-02-10 16:37:48 -060022#include <ndn-cxx/lp/packet.hpp>
Junxiao Shic9575a62017-07-01 06:16:02 +000023#include <ndn-cxx/net/ethernet.hpp>
Vince Lehman277ecf02016-02-10 16:37:48 -060024#include <ndn-cxx/security/key-chain.hpp>
Vince Lehman277ecf02016-02-10 16:37:48 -060025
Junxiao Shi20b22972017-05-24 21:04:16 +000026#include "tests/test-common.hpp"
27#include "tests/identity-management-fixture.hpp"
28#include <boost/test/output_test_stream.hpp>
29
Vince Lehman277ecf02016-02-10 16:37:48 -060030namespace ndn {
31namespace dump {
32namespace tests {
33
34using namespace ndn::tests;
35
36class StdCoutRedirector
37{
38public:
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
51private:
52 std::streambuf* originalBuffer;
53};
54
Junxiao Shi20b22972017-05-24 21:04:16 +000055class NdnDumpFixture : public IdentityManagementFixture
Vince Lehman277ecf02016-02-10 16:37:48 -060056{
57protected:
58 NdnDumpFixture()
59 {
60 dump.m_dataLinkType = DLT_EN10MB;
61 }
62
Junxiao Shi20b22972017-05-24 21:04:16 +000063 template<typename Packet>
Vince Lehman277ecf02016-02-10 16:37:48 -060064 void
65 receive(const Packet& packet)
66 {
Junxiao Shi20b22972017-05-24 21:04:16 +000067 EncodingBuffer buffer(packet.wireEncode());
Vince Lehman277ecf02016-02-10 16:37:48 -060068 receive(buffer);
69 }
70
71 void
Junxiao Shi20b22972017-05-24 21:04:16 +000072 receive(EncodingBuffer& buffer)
Vince Lehman277ecf02016-02-10 16:37:48 -060073 {
Junxiao Shic9575a62017-07-01 06:16:02 +000074 ethernet::Address host;
Vince Lehman277ecf02016-02-10 16:37:48 -060075
76 // Ethernet header
Junxiao Shic9575a62017-07-01 06:16:02 +000077 uint16_t frameType = htons(ethernet::ETHERTYPE_NDN);
78 buffer.prependByteArray(reinterpret_cast<const uint8_t*>(&frameType), ethernet::TYPE_LEN);
Vince Lehman277ecf02016-02-10 16:37:48 -060079 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
91protected:
92 Ndndump dump;
93 boost::test_tools::output_test_stream output;
94};
95
96BOOST_FIXTURE_TEST_SUITE(NdnDump, NdnDumpFixture)
97
98BOOST_AUTO_TEST_CASE(CaptureInterest)
99{
Junxiao Shi20b22972017-05-24 21:04:16 +0000100 Interest interest("/test");
Vince Lehman277ecf02016-02-10 16:37:48 -0600101 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
111BOOST_AUTO_TEST_CASE(CaptureData)
112{
Junxiao Shi20b22972017-05-24 21:04:16 +0000113 Data data("/test");
114 m_keyChain.sign(data);
Vince Lehman277ecf02016-02-10 16:37:48 -0600115
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
123BOOST_AUTO_TEST_CASE(CaptureNack)
124{
Junxiao Shi20b22972017-05-24 21:04:16 +0000125 Interest interest("/test");
Vince Lehman277ecf02016-02-10 16:37:48 -0600126 interest.setNonce(0);
127
Junxiao Shi20b22972017-05-24 21:04:16 +0000128 lp::Nack nack(interest);
129 nack.setReason(lp::NackReason::DUPLICATE);
Vince Lehman277ecf02016-02-10 16:37:48 -0600130
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
142BOOST_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 Shi20b22972017-05-24 21:04:16 +0000149 Buffer buffer(data, 4);
Vince Lehman277ecf02016-02-10 16:37:48 -0600150
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
165BOOST_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
177BOOST_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
199BOOST_AUTO_TEST_CASE(CaptureUnknownNetworkPacket)
200{
Junxiao Shi20b22972017-05-24 21:04:16 +0000201 EncodingBuffer buffer(encoding::makeEmptyBlock(tlv::Name));
Vince Lehman277ecf02016-02-10 16:37:48 -0600202
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
211BOOST_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
240BOOST_AUTO_TEST_SUITE_END()
241
242} // namespace tests
243} // namespace dump
244} // namespace ndn