blob: b360bd904ef7a0f8944ee5bb490e1bd009167acc [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>
23#include <ndn-cxx/security/key-chain.hpp>
24#include <ndn-cxx/util/ethernet.hpp>
25
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 Shi20b22972017-05-24 21:04:16 +000074 util::ethernet::Address host;
Vince Lehman277ecf02016-02-10 16:37:48 -060075
76 // Ethernet header
Junxiao Shi20b22972017-05-24 21:04:16 +000077 uint16_t frameType = htons(util::ethernet::ETHERTYPE_NDN);
Vince Lehman277ecf02016-02-10 16:37:48 -060078 buffer.prependByteArray(reinterpret_cast<const uint8_t*>(&frameType),
Junxiao Shi20b22972017-05-24 21:04:16 +000079 util::ethernet::TYPE_LEN);
Vince Lehman277ecf02016-02-10 16:37:48 -060080 buffer.prependByteArray(host.data(), host.size());
81 buffer.prependByteArray(host.data(), host.size());
82
83 pcap_pkthdr header{};
84 header.len = buffer.size();
85
86 {
87 StdCoutRedirector redirect(output);
88 dump.onCapturedPacket(&header, buffer.buf());
89 }
90 }
91
92protected:
93 Ndndump dump;
94 boost::test_tools::output_test_stream output;
95};
96
97BOOST_FIXTURE_TEST_SUITE(NdnDump, NdnDumpFixture)
98
99BOOST_AUTO_TEST_CASE(CaptureInterest)
100{
Junxiao Shi20b22972017-05-24 21:04:16 +0000101 Interest interest("/test");
Vince Lehman277ecf02016-02-10 16:37:48 -0600102 interest.setNonce(0);
103
104 this->receive(interest);
105
106 const std::string expectedOutput =
107 "0.000000 Tunnel Type: EthernetFrame, INTEREST: /test?ndn.Nonce=0\n";
108
109 BOOST_CHECK(output.is_equal(expectedOutput));
110}
111
112BOOST_AUTO_TEST_CASE(CaptureData)
113{
Junxiao Shi20b22972017-05-24 21:04:16 +0000114 Data data("/test");
115 m_keyChain.sign(data);
Vince Lehman277ecf02016-02-10 16:37:48 -0600116
117 this->receive(data);
118
119 const std::string expectedOutput = "0.000000 Tunnel Type: EthernetFrame, DATA: /test\n";
120
121 BOOST_CHECK(output.is_equal(expectedOutput));
122}
123
124BOOST_AUTO_TEST_CASE(CaptureNack)
125{
Junxiao Shi20b22972017-05-24 21:04:16 +0000126 Interest interest("/test");
Vince Lehman277ecf02016-02-10 16:37:48 -0600127 interest.setNonce(0);
128
Junxiao Shi20b22972017-05-24 21:04:16 +0000129 lp::Nack nack(interest);
130 nack.setReason(lp::NackReason::DUPLICATE);
Vince Lehman277ecf02016-02-10 16:37:48 -0600131
132 lp::Packet lpPacket(interest.wireEncode());
133 lpPacket.add<lp::NackField>(nack.getHeader());
134
135 this->receive(lpPacket);
136
137 const std::string expectedOutput =
138 "0.000000 Tunnel Type: EthernetFrame, NACK: Duplicate, /test?ndn.Nonce=0\n";
139
140 BOOST_CHECK(output.is_equal(expectedOutput));
141}
142
143BOOST_AUTO_TEST_CASE(CaptureLpFragment)
144{
145 const uint8_t data[10] = {
146 0x06, 0x08, // Data packet
147 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
148 };
149
Junxiao Shi20b22972017-05-24 21:04:16 +0000150 Buffer buffer(data, 4);
Vince Lehman277ecf02016-02-10 16:37:48 -0600151
152 lp::Packet lpPacket;
153 lpPacket.add<lp::FragmentField>(std::make_pair(buffer.begin(), buffer.end()));
154 lpPacket.add<lp::FragIndexField>(0);
155 lpPacket.add<lp::FragCountField>(2);
156 lpPacket.add<lp::SequenceField>(1000);
157
158 this->receive(lpPacket);
159
160 const std::string expectedOutput =
161 "0.000000 Tunnel Type: EthernetFrame, NDNLPv2-FRAGMENT\n";
162
163 BOOST_CHECK(output.is_equal(expectedOutput));
164}
165
166BOOST_AUTO_TEST_CASE(CaptureIdlePacket)
167{
168 lp::Packet lpPacket;
169
170 this->receive(lpPacket);
171
172 const std::string expectedOutput =
173 "0.000000 Tunnel Type: EthernetFrame, NDNLPv2-IDLE\n";
174
175 BOOST_CHECK(output.is_equal(expectedOutput));
176}
177
178BOOST_AUTO_TEST_CASE(CaptureIncompletePacket)
179{
180 const uint8_t interest[] = {
181 0x05, 0x0E, // Interest
182 0x07, 0x06, // Name
183 0x08, 0x04, // NameComponent
184 0x74, 0x65, 0x73, 0x74,
185 0x0a, 0x04, // Nonce
186 0x00, 0x00, 0x00, 0x01
187 };
188
189 EncodingBuffer buffer;
190 buffer.prependByteArray(interest, 4);
191
192 this->receive(buffer);
193
194 const std::string expectedOutput =
195 "0.000000 Tunnel Type: EthernetFrame, INCOMPLETE-PACKET, size: 4\n";
196
197 BOOST_CHECK(output.is_equal(expectedOutput));
198}
199
200BOOST_AUTO_TEST_CASE(CaptureUnknownNetworkPacket)
201{
Junxiao Shi20b22972017-05-24 21:04:16 +0000202 EncodingBuffer buffer(encoding::makeEmptyBlock(tlv::Name));
Vince Lehman277ecf02016-02-10 16:37:48 -0600203
204 this->receive(buffer);
205
206 const std::string expectedOutput =
207 "0.000000 Tunnel Type: EthernetFrame, UNKNOWN-NETWORK-PACKET\n";
208
209 BOOST_CHECK(output.is_equal(expectedOutput));
210}
211
212BOOST_AUTO_TEST_CASE(DumpPcapTrace)
213{
214 dump.inputFile = "tests/dump/nack.pcap";
215 dump.pcapProgram = "";
216
217 {
218 StdCoutRedirector redirect(output);
219 dump.run();
220 }
221
222 const std::string expectedOutput =
223 "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"
224 "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"
225 "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"
226 "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"
227 "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"
228 "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"
229 "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"
230 "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"
231 "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"
232 "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"
233 "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"
234 "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"
235 "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";
236
237 BOOST_CHECK(output.is_equal(expectedOutput));
238}
239
240
241BOOST_AUTO_TEST_SUITE_END()
242
243} // namespace tests
244} // namespace dump
245} // namespace ndn