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