blob: 336b1de96895b6a95652e71b5153437b98af81ad [file] [log] [blame]
Zhuo Lib3558892016-08-12 15:51:12 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shib54aabd2018-04-16 19:36:24 +00002/*
Junxiao Shifd2d1012022-01-11 18:20:38 +00003 * Copyright (c) 2014-2022, Arizona Board of Regents.
Zhuo Lib3558892016-08-12 15:51:12 -07004 *
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/peek/ndnpeek/ndnpeek.hpp"
21
22#include "tests/test-common.hpp"
Davide Pesavento66777622020-10-09 18:46:03 -040023#include "tests/io-fixture.hpp"
Davide Pesaventobf28cd72017-08-13 17:22:47 -040024
Zhuo Lib3558892016-08-12 15:51:12 -070025#include <ndn-cxx/util/dummy-client-face.hpp>
26
27#include <boost/mpl/vector.hpp>
Davide Pesaventoa8600b02019-12-22 18:52:36 -050028#if BOOST_VERSION >= 105900
29#include <boost/test/tools/output_test_stream.hpp>
30#else
31#include <boost/test/output_test_stream.hpp>
32#endif
Zhuo Lib3558892016-08-12 15:51:12 -070033
34namespace ndn {
35namespace peek {
36namespace tests {
37
38using namespace ndn::tests;
39using boost::test_tools::output_test_stream;
40
41class CoutRedirector : noncopyable
42{
43public:
44 explicit
45 CoutRedirector(std::ostream& destination)
46 {
47 m_originalBuf = std::cout.rdbuf(destination.rdbuf());
48 }
49
50 ~CoutRedirector()
51 {
52 std::cout.rdbuf(m_originalBuf);
53 }
54
55private:
56 std::streambuf* m_originalBuf;
57};
58
Davide Pesavento96028232019-08-17 01:26:13 -040059static PeekOptions
60makeDefaultOptions()
61{
62 PeekOptions opt;
63 opt.name = "/peek/test";
64 return opt;
65}
66
Davide Pesavento66777622020-10-09 18:46:03 -040067class NdnPeekFixture : public IoFixture
Zhuo Lib3558892016-08-12 15:51:12 -070068{
69protected:
Zhuo Lib3558892016-08-12 15:51:12 -070070 void
Davide Pesavento96028232019-08-17 01:26:13 -040071 initialize(const PeekOptions& opts = makeDefaultOptions())
Zhuo Lib3558892016-08-12 15:51:12 -070072 {
73 peek = make_unique<NdnPeek>(face, opts);
74 }
75
76protected:
Davide Pesavento66777622020-10-09 18:46:03 -040077 ndn::util::DummyClientFace face{m_io};
Zhuo Lib3558892016-08-12 15:51:12 -070078 output_test_stream output;
79 unique_ptr<NdnPeek> peek;
80};
81
Zhuo Lib3558892016-08-12 15:51:12 -070082class OutputFull
83{
84public:
85 static PeekOptions
86 makeOptions()
87 {
88 return makeDefaultOptions();
89 }
90
91 static void
92 checkOutput(output_test_stream& output, const Data& data)
93 {
94 const Block& block = data.wireEncode();
95 std::string expected(reinterpret_cast<const char*>(block.wire()), block.size());
96 BOOST_CHECK(output.is_equal(expected));
97 }
98
99 static void
100 checkOutput(output_test_stream& output, const lp::Nack& nack)
101 {
102 const Block& block = nack.getHeader().wireEncode();
103 std::string expected(reinterpret_cast<const char*>(block.wire()), block.size());
104 BOOST_CHECK(output.is_equal(expected));
105 }
106};
107
108class OutputPayloadOnly
109{
110public:
111 static PeekOptions
112 makeOptions()
113 {
114 PeekOptions opt = makeDefaultOptions();
115 opt.wantPayloadOnly = true;
116 return opt;
117 }
118
119 static void
120 checkOutput(output_test_stream& output, const Data& data)
121 {
122 const Block& block = data.getContent();
123 std::string expected(reinterpret_cast<const char*>(block.value()), block.value_size());
124 BOOST_CHECK(output.is_equal(expected));
125 }
126
127 static void
128 checkOutput(output_test_stream& output, const lp::Nack& nack)
129 {
130 std::string expected = boost::lexical_cast<std::string>(nack.getReason()) + '\n';
131 BOOST_CHECK(output.is_equal(expected));
132 }
133};
134
135BOOST_AUTO_TEST_SUITE(Peek)
136BOOST_FIXTURE_TEST_SUITE(TestNdnPeek, NdnPeekFixture)
137
138using OutputChecks = boost::mpl::vector<OutputFull, OutputPayloadOnly>;
139
140BOOST_AUTO_TEST_CASE_TEMPLATE(Default, OutputCheck, OutputChecks)
141{
142 auto options = OutputCheck::makeOptions();
143 initialize(options);
144
Junxiao Shib54aabd2018-04-16 19:36:24 +0000145 auto data = makeData(options.name);
Davide Pesavento65d11552019-06-09 19:15:50 -0400146 const std::string payload = "NdnPeekTest";
Zhuo Lib3558892016-08-12 15:51:12 -0700147 data->setContent(reinterpret_cast<const uint8_t*>(payload.data()), payload.size());
148
149 {
150 CoutRedirector redir(output);
151 peek->start();
Davide Pesavento66777622020-10-09 18:46:03 -0400152 this->advanceClocks(25_ms, 4);
Zhuo Lib3558892016-08-12 15:51:12 -0700153 face.receive(*data);
154 }
155
156 OutputCheck::checkOutput(output, *data);
157 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
Junxiao Shifd2d1012022-01-11 18:20:38 +0000158 const auto& interest = face.sentInterests.back();
159 BOOST_CHECK_EQUAL(interest.getCanBePrefix(), false);
160 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false);
161 BOOST_CHECK_EQUAL(interest.getForwardingHint().empty(), true);
162 BOOST_CHECK_EQUAL(interest.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
163 BOOST_CHECK(interest.getHopLimit() == nullopt);
164 BOOST_CHECK(!interest.hasApplicationParameters());
Davide Pesavento87434be2019-07-25 19:04:23 -0400165 BOOST_CHECK(peek->getResult() == NdnPeek::Result::DATA);
Zhuo Lib3558892016-08-12 15:51:12 -0700166}
167
Junxiao Shib54aabd2018-04-16 19:36:24 +0000168BOOST_AUTO_TEST_CASE_TEMPLATE(NonDefault, OutputCheck, OutputChecks)
Zhuo Lib3558892016-08-12 15:51:12 -0700169{
170 auto options = OutputCheck::makeOptions();
Junxiao Shib54aabd2018-04-16 19:36:24 +0000171 options.canBePrefix = true;
Zhuo Lib3558892016-08-12 15:51:12 -0700172 options.mustBeFresh = true;
Junxiao Shifd2d1012022-01-11 18:20:38 +0000173 options.forwardingHint.emplace_back("/fh");
Junxiao Shib54aabd2018-04-16 19:36:24 +0000174 options.interestLifetime = 200_ms;
Davide Pesaventoc214e072019-08-17 01:33:28 -0400175 options.hopLimit = 64;
Zhuo Lib3558892016-08-12 15:51:12 -0700176 initialize(options);
177
Junxiao Shib54aabd2018-04-16 19:36:24 +0000178 auto data = makeData(Name(options.name).append("suffix"));
Junxiao Shi96192952019-05-22 15:45:12 +0000179 data->setFreshnessPeriod(1_s);
Davide Pesavento65d11552019-06-09 19:15:50 -0400180 const std::string payload = "NdnPeekTest";
Zhuo Lib3558892016-08-12 15:51:12 -0700181 data->setContent(reinterpret_cast<const uint8_t*>(payload.data()), payload.size());
182
183 {
184 CoutRedirector redir(output);
185 peek->start();
Davide Pesavento66777622020-10-09 18:46:03 -0400186 this->advanceClocks(25_ms, 4);
Zhuo Lib3558892016-08-12 15:51:12 -0700187 face.receive(*data);
188 }
189
190 OutputCheck::checkOutput(output, *data);
191 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
Junxiao Shifd2d1012022-01-11 18:20:38 +0000192 const auto& interest = face.sentInterests.back();
193 BOOST_CHECK_EQUAL(interest.getCanBePrefix(), true);
194 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true);
195 BOOST_TEST(interest.getForwardingHint() == std::vector<Name>({"/fh"}),
196 boost::test_tools::per_element());
197 BOOST_CHECK_EQUAL(interest.getInterestLifetime(), 200_ms);
198 BOOST_CHECK(interest.getHopLimit() == 64);
199 BOOST_CHECK(!interest.hasApplicationParameters());
Davide Pesavento87434be2019-07-25 19:04:23 -0400200 BOOST_CHECK(peek->getResult() == NdnPeek::Result::DATA);
Zhuo Lib3558892016-08-12 15:51:12 -0700201}
202
203BOOST_AUTO_TEST_CASE_TEMPLATE(ReceiveNackWithReason, OutputCheck, OutputChecks)
204{
205 auto options = OutputCheck::makeOptions();
206 initialize(options);
207 lp::Nack nack;
208
209 {
210 CoutRedirector redir(output);
211 peek->start();
Davide Pesavento66777622020-10-09 18:46:03 -0400212 this->advanceClocks(25_ms, 4);
Zhuo Lib3558892016-08-12 15:51:12 -0700213 nack = makeNack(face.sentInterests.at(0), lp::NackReason::NO_ROUTE);
214 face.receive(nack);
215 }
216
217 OutputCheck::checkOutput(output, nack);
218 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
Davide Pesavento87434be2019-07-25 19:04:23 -0400219 BOOST_CHECK(peek->getResult() == NdnPeek::Result::NACK);
Zhuo Lib3558892016-08-12 15:51:12 -0700220}
221
222BOOST_AUTO_TEST_CASE_TEMPLATE(ReceiveNackWithoutReason, OutputCheck, OutputChecks)
223{
224 auto options = OutputCheck::makeOptions();
225 initialize(options);
226 lp::Nack nack;
227
228 {
229 CoutRedirector redir(output);
230 peek->start();
Davide Pesavento66777622020-10-09 18:46:03 -0400231 this->advanceClocks(25_ms, 4);
Zhuo Lib3558892016-08-12 15:51:12 -0700232 nack = makeNack(face.sentInterests.at(0), lp::NackReason::NONE);
233 face.receive(nack);
234 }
235
236 OutputCheck::checkOutput(output, nack);
237 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
Davide Pesavento87434be2019-07-25 19:04:23 -0400238 BOOST_CHECK(peek->getResult() == NdnPeek::Result::NACK);
Zhuo Lib3558892016-08-12 15:51:12 -0700239}
240
Davide Pesavento96028232019-08-17 01:26:13 -0400241BOOST_AUTO_TEST_CASE(ApplicationParameters)
242{
243 auto options = makeDefaultOptions();
244 options.applicationParameters = make_shared<Buffer>("hello", 5);
245 initialize(options);
246
247 peek->start();
Davide Pesavento66777622020-10-09 18:46:03 -0400248 this->advanceClocks(25_ms, 4);
Davide Pesavento96028232019-08-17 01:26:13 -0400249
250 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
251 BOOST_CHECK_EQUAL(face.sentInterests.back().getCanBePrefix(), false);
252 BOOST_CHECK_EQUAL(face.sentInterests.back().getMustBeFresh(), false);
253 BOOST_CHECK_EQUAL(face.sentInterests.back().getForwardingHint().empty(), true);
254 BOOST_CHECK_EQUAL(face.sentInterests.back().getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
255 BOOST_CHECK_EQUAL(face.sentInterests.back().hasApplicationParameters(), true);
256 BOOST_CHECK_EQUAL(face.sentInterests.back().getApplicationParameters(), "2405 68656C6C6F"_block);
257}
258
Davide Pesavento65d11552019-06-09 19:15:50 -0400259BOOST_AUTO_TEST_CASE(NoTimeout)
Zhuo Lib3558892016-08-12 15:51:12 -0700260{
261 auto options = makeDefaultOptions();
Davide Pesavento65d11552019-06-09 19:15:50 -0400262 options.interestLifetime = 1_s;
263 options.timeout = nullopt;
Zhuo Lib3558892016-08-12 15:51:12 -0700264 initialize(options);
265
Davide Pesavento65d11552019-06-09 19:15:50 -0400266 BOOST_CHECK_EQUAL(face.sentInterests.size(), 0);
Zhuo Lib3558892016-08-12 15:51:12 -0700267
268 peek->start();
Davide Pesavento66777622020-10-09 18:46:03 -0400269 this->advanceClocks(100_ms, 9);
Zhuo Lib3558892016-08-12 15:51:12 -0700270 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
Davide Pesavento65d11552019-06-09 19:15:50 -0400271 BOOST_CHECK_EQUAL(face.getNPendingInterests(), 1);
Davide Pesavento87434be2019-07-25 19:04:23 -0400272 BOOST_CHECK(peek->getResult() == NdnPeek::Result::UNKNOWN);
Davide Pesavento65d11552019-06-09 19:15:50 -0400273
Davide Pesavento66777622020-10-09 18:46:03 -0400274 this->advanceClocks(100_ms, 2);
Davide Pesavento65d11552019-06-09 19:15:50 -0400275 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
276 BOOST_CHECK_EQUAL(face.getNPendingInterests(), 0);
Davide Pesavento87434be2019-07-25 19:04:23 -0400277 BOOST_CHECK(peek->getResult() == NdnPeek::Result::TIMEOUT);
Zhuo Lib3558892016-08-12 15:51:12 -0700278}
279
280BOOST_AUTO_TEST_CASE(TimeoutLessThanLifetime)
281{
282 auto options = makeDefaultOptions();
Junxiao Shib54aabd2018-04-16 19:36:24 +0000283 options.interestLifetime = 200_ms;
284 options.timeout = 100_ms;
Zhuo Lib3558892016-08-12 15:51:12 -0700285 initialize(options);
286
Davide Pesavento65d11552019-06-09 19:15:50 -0400287 BOOST_CHECK_EQUAL(face.sentInterests.size(), 0);
Zhuo Lib3558892016-08-12 15:51:12 -0700288
289 peek->start();
Davide Pesavento66777622020-10-09 18:46:03 -0400290 this->advanceClocks(25_ms, 6);
Zhuo Lib3558892016-08-12 15:51:12 -0700291
292 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
Davide Pesavento65d11552019-06-09 19:15:50 -0400293 BOOST_CHECK_EQUAL(face.getNPendingInterests(), 0);
Davide Pesavento87434be2019-07-25 19:04:23 -0400294 BOOST_CHECK(peek->getResult() == NdnPeek::Result::TIMEOUT);
Zhuo Lib3558892016-08-12 15:51:12 -0700295}
296
297BOOST_AUTO_TEST_CASE(TimeoutGreaterThanLifetime)
298{
299 auto options = makeDefaultOptions();
Junxiao Shib54aabd2018-04-16 19:36:24 +0000300 options.interestLifetime = 50_ms;
301 options.timeout = 200_ms;
Zhuo Lib3558892016-08-12 15:51:12 -0700302 initialize(options);
303
Davide Pesavento65d11552019-06-09 19:15:50 -0400304 BOOST_CHECK_EQUAL(face.sentInterests.size(), 0);
Zhuo Lib3558892016-08-12 15:51:12 -0700305
306 peek->start();
Davide Pesavento66777622020-10-09 18:46:03 -0400307 this->advanceClocks(25_ms, 4);
Zhuo Lib3558892016-08-12 15:51:12 -0700308
309 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
Davide Pesavento65d11552019-06-09 19:15:50 -0400310 BOOST_CHECK_EQUAL(face.getNPendingInterests(), 0);
Davide Pesavento87434be2019-07-25 19:04:23 -0400311 BOOST_CHECK(peek->getResult() == NdnPeek::Result::TIMEOUT);
Zhuo Lib3558892016-08-12 15:51:12 -0700312}
313
Davide Pesavento96028232019-08-17 01:26:13 -0400314BOOST_AUTO_TEST_CASE(OversizedPacket)
315{
316 auto options = makeDefaultOptions();
317 options.applicationParameters = make_shared<Buffer>(MAX_NDN_PACKET_SIZE);
318 initialize(options);
319
320 peek->start();
Davide Pesavento66777622020-10-09 18:46:03 -0400321 BOOST_CHECK_THROW(this->advanceClocks(1_ms, 10), Face::OversizedPacketError);
Davide Pesavento96028232019-08-17 01:26:13 -0400322
323 BOOST_CHECK_EQUAL(face.sentInterests.size(), 0);
324}
325
Zhuo Lib3558892016-08-12 15:51:12 -0700326BOOST_AUTO_TEST_SUITE_END() // TestNdnPeek
327BOOST_AUTO_TEST_SUITE_END() // Peek
328
329} // namespace tests
330} // namespace peek
331} // namespace ndn