blob: 8a51b8e06d73d7fd6d13f6a7e6b02910bd3da0f6 [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/*
Davide Pesavento3653dae2023-03-13 17:44:22 -04003 * Copyright (c) 2014-2023, 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
Davide Pesaventoa8600b02019-12-22 18:52:36 -050027#include <boost/test/tools/output_test_stream.hpp>
Zhuo Lib3558892016-08-12 15:51:12 -070028
Davide Pesaventob3570c62022-02-19 19:19:00 -050029namespace ndn::peek::tests {
Zhuo Lib3558892016-08-12 15:51:12 -070030
31using namespace ndn::tests;
32using boost::test_tools::output_test_stream;
33
34class CoutRedirector : noncopyable
35{
36public:
37 explicit
38 CoutRedirector(std::ostream& destination)
39 {
40 m_originalBuf = std::cout.rdbuf(destination.rdbuf());
41 }
42
43 ~CoutRedirector()
44 {
45 std::cout.rdbuf(m_originalBuf);
46 }
47
48private:
49 std::streambuf* m_originalBuf;
50};
51
Davide Pesavento96028232019-08-17 01:26:13 -040052static PeekOptions
53makeDefaultOptions()
54{
55 PeekOptions opt;
56 opt.name = "/peek/test";
57 return opt;
58}
59
Davide Pesavento66777622020-10-09 18:46:03 -040060class NdnPeekFixture : public IoFixture
Zhuo Lib3558892016-08-12 15:51:12 -070061{
62protected:
Zhuo Lib3558892016-08-12 15:51:12 -070063 void
Davide Pesavento96028232019-08-17 01:26:13 -040064 initialize(const PeekOptions& opts = makeDefaultOptions())
Zhuo Lib3558892016-08-12 15:51:12 -070065 {
66 peek = make_unique<NdnPeek>(face, opts);
67 }
68
69protected:
Junxiao Shi869d73e2023-08-10 22:52:26 +000070 DummyClientFace face{m_io};
Zhuo Lib3558892016-08-12 15:51:12 -070071 output_test_stream output;
72 unique_ptr<NdnPeek> peek;
73};
74
Zhuo Lib3558892016-08-12 15:51:12 -070075class OutputFull
76{
77public:
78 static PeekOptions
79 makeOptions()
80 {
81 return makeDefaultOptions();
82 }
83
84 static void
85 checkOutput(output_test_stream& output, const Data& data)
86 {
87 const Block& block = data.wireEncode();
Davide Pesavento242d5062022-03-11 16:34:23 -050088 std::string expected(reinterpret_cast<const char*>(block.data()), block.size());
Zhuo Lib3558892016-08-12 15:51:12 -070089 BOOST_CHECK(output.is_equal(expected));
90 }
91
92 static void
93 checkOutput(output_test_stream& output, const lp::Nack& nack)
94 {
95 const Block& block = nack.getHeader().wireEncode();
Davide Pesavento242d5062022-03-11 16:34:23 -050096 std::string expected(reinterpret_cast<const char*>(block.data()), block.size());
Zhuo Lib3558892016-08-12 15:51:12 -070097 BOOST_CHECK(output.is_equal(expected));
98 }
99};
100
101class OutputPayloadOnly
102{
103public:
104 static PeekOptions
105 makeOptions()
106 {
107 PeekOptions opt = makeDefaultOptions();
108 opt.wantPayloadOnly = true;
109 return opt;
110 }
111
112 static void
113 checkOutput(output_test_stream& output, const Data& data)
114 {
115 const Block& block = data.getContent();
116 std::string expected(reinterpret_cast<const char*>(block.value()), block.value_size());
117 BOOST_CHECK(output.is_equal(expected));
118 }
119
120 static void
121 checkOutput(output_test_stream& output, const lp::Nack& nack)
122 {
123 std::string expected = boost::lexical_cast<std::string>(nack.getReason()) + '\n';
124 BOOST_CHECK(output.is_equal(expected));
125 }
126};
127
128BOOST_AUTO_TEST_SUITE(Peek)
129BOOST_FIXTURE_TEST_SUITE(TestNdnPeek, NdnPeekFixture)
130
Davide Pesavento2ab04a22023-09-15 22:08:22 -0400131using OutputChecks = std::tuple<OutputFull, OutputPayloadOnly>;
Zhuo Lib3558892016-08-12 15:51:12 -0700132
133BOOST_AUTO_TEST_CASE_TEMPLATE(Default, OutputCheck, OutputChecks)
134{
135 auto options = OutputCheck::makeOptions();
136 initialize(options);
137
Junxiao Shib54aabd2018-04-16 19:36:24 +0000138 auto data = makeData(options.name);
Davide Pesavento59984282022-02-16 22:41:03 -0500139 data->setContent({'n', 'd', 'n', 'p', 'e', 'e', 'k'});
Zhuo Lib3558892016-08-12 15:51:12 -0700140
141 {
142 CoutRedirector redir(output);
143 peek->start();
Davide Pesavento66777622020-10-09 18:46:03 -0400144 this->advanceClocks(25_ms, 4);
Zhuo Lib3558892016-08-12 15:51:12 -0700145 face.receive(*data);
146 }
147
148 OutputCheck::checkOutput(output, *data);
149 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
Junxiao Shifd2d1012022-01-11 18:20:38 +0000150 const auto& interest = face.sentInterests.back();
151 BOOST_CHECK_EQUAL(interest.getCanBePrefix(), false);
152 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false);
153 BOOST_CHECK_EQUAL(interest.getForwardingHint().empty(), true);
154 BOOST_CHECK_EQUAL(interest.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesavento3653dae2023-03-13 17:44:22 -0400155 BOOST_CHECK(interest.getHopLimit() == std::nullopt);
Junxiao Shifd2d1012022-01-11 18:20:38 +0000156 BOOST_CHECK(!interest.hasApplicationParameters());
Davide Pesavento87434be2019-07-25 19:04:23 -0400157 BOOST_CHECK(peek->getResult() == NdnPeek::Result::DATA);
Zhuo Lib3558892016-08-12 15:51:12 -0700158}
159
Junxiao Shib54aabd2018-04-16 19:36:24 +0000160BOOST_AUTO_TEST_CASE_TEMPLATE(NonDefault, OutputCheck, OutputChecks)
Zhuo Lib3558892016-08-12 15:51:12 -0700161{
162 auto options = OutputCheck::makeOptions();
Junxiao Shib54aabd2018-04-16 19:36:24 +0000163 options.canBePrefix = true;
Zhuo Lib3558892016-08-12 15:51:12 -0700164 options.mustBeFresh = true;
Junxiao Shifd2d1012022-01-11 18:20:38 +0000165 options.forwardingHint.emplace_back("/fh");
Junxiao Shib54aabd2018-04-16 19:36:24 +0000166 options.interestLifetime = 200_ms;
Davide Pesaventoc214e072019-08-17 01:33:28 -0400167 options.hopLimit = 64;
Zhuo Lib3558892016-08-12 15:51:12 -0700168 initialize(options);
169
Junxiao Shib54aabd2018-04-16 19:36:24 +0000170 auto data = makeData(Name(options.name).append("suffix"));
Junxiao Shi96192952019-05-22 15:45:12 +0000171 data->setFreshnessPeriod(1_s);
Davide Pesavento59984282022-02-16 22:41:03 -0500172 data->setContent({'n', 'd', 'n', 'p', 'e', 'e', 'k'});
Zhuo Lib3558892016-08-12 15:51:12 -0700173
174 {
175 CoutRedirector redir(output);
176 peek->start();
Davide Pesavento66777622020-10-09 18:46:03 -0400177 this->advanceClocks(25_ms, 4);
Zhuo Lib3558892016-08-12 15:51:12 -0700178 face.receive(*data);
179 }
180
181 OutputCheck::checkOutput(output, *data);
182 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
Junxiao Shifd2d1012022-01-11 18:20:38 +0000183 const auto& interest = face.sentInterests.back();
184 BOOST_CHECK_EQUAL(interest.getCanBePrefix(), true);
185 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true);
186 BOOST_TEST(interest.getForwardingHint() == std::vector<Name>({"/fh"}),
187 boost::test_tools::per_element());
188 BOOST_CHECK_EQUAL(interest.getInterestLifetime(), 200_ms);
189 BOOST_CHECK(interest.getHopLimit() == 64);
190 BOOST_CHECK(!interest.hasApplicationParameters());
Davide Pesavento87434be2019-07-25 19:04:23 -0400191 BOOST_CHECK(peek->getResult() == NdnPeek::Result::DATA);
Zhuo Lib3558892016-08-12 15:51:12 -0700192}
193
194BOOST_AUTO_TEST_CASE_TEMPLATE(ReceiveNackWithReason, OutputCheck, OutputChecks)
195{
196 auto options = OutputCheck::makeOptions();
197 initialize(options);
198 lp::Nack nack;
199
200 {
201 CoutRedirector redir(output);
202 peek->start();
Davide Pesavento66777622020-10-09 18:46:03 -0400203 this->advanceClocks(25_ms, 4);
Zhuo Lib3558892016-08-12 15:51:12 -0700204 nack = makeNack(face.sentInterests.at(0), lp::NackReason::NO_ROUTE);
205 face.receive(nack);
206 }
207
208 OutputCheck::checkOutput(output, nack);
209 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
Davide Pesavento87434be2019-07-25 19:04:23 -0400210 BOOST_CHECK(peek->getResult() == NdnPeek::Result::NACK);
Zhuo Lib3558892016-08-12 15:51:12 -0700211}
212
213BOOST_AUTO_TEST_CASE_TEMPLATE(ReceiveNackWithoutReason, OutputCheck, OutputChecks)
214{
215 auto options = OutputCheck::makeOptions();
216 initialize(options);
217 lp::Nack nack;
218
219 {
220 CoutRedirector redir(output);
221 peek->start();
Davide Pesavento66777622020-10-09 18:46:03 -0400222 this->advanceClocks(25_ms, 4);
Zhuo Lib3558892016-08-12 15:51:12 -0700223 nack = makeNack(face.sentInterests.at(0), lp::NackReason::NONE);
224 face.receive(nack);
225 }
226
227 OutputCheck::checkOutput(output, nack);
228 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
Davide Pesavento87434be2019-07-25 19:04:23 -0400229 BOOST_CHECK(peek->getResult() == NdnPeek::Result::NACK);
Zhuo Lib3558892016-08-12 15:51:12 -0700230}
231
Davide Pesavento96028232019-08-17 01:26:13 -0400232BOOST_AUTO_TEST_CASE(ApplicationParameters)
233{
234 auto options = makeDefaultOptions();
235 options.applicationParameters = make_shared<Buffer>("hello", 5);
236 initialize(options);
237
238 peek->start();
Davide Pesavento66777622020-10-09 18:46:03 -0400239 this->advanceClocks(25_ms, 4);
Davide Pesavento96028232019-08-17 01:26:13 -0400240
241 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
242 BOOST_CHECK_EQUAL(face.sentInterests.back().getCanBePrefix(), false);
243 BOOST_CHECK_EQUAL(face.sentInterests.back().getMustBeFresh(), false);
244 BOOST_CHECK_EQUAL(face.sentInterests.back().getForwardingHint().empty(), true);
245 BOOST_CHECK_EQUAL(face.sentInterests.back().getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
246 BOOST_CHECK_EQUAL(face.sentInterests.back().hasApplicationParameters(), true);
247 BOOST_CHECK_EQUAL(face.sentInterests.back().getApplicationParameters(), "2405 68656C6C6F"_block);
248}
249
Davide Pesavento65d11552019-06-09 19:15:50 -0400250BOOST_AUTO_TEST_CASE(NoTimeout)
Zhuo Lib3558892016-08-12 15:51:12 -0700251{
252 auto options = makeDefaultOptions();
Davide Pesavento65d11552019-06-09 19:15:50 -0400253 options.interestLifetime = 1_s;
Davide Pesavento242d5062022-03-11 16:34:23 -0500254 options.timeout = std::nullopt;
Zhuo Lib3558892016-08-12 15:51:12 -0700255 initialize(options);
256
Davide Pesavento65d11552019-06-09 19:15:50 -0400257 BOOST_CHECK_EQUAL(face.sentInterests.size(), 0);
Zhuo Lib3558892016-08-12 15:51:12 -0700258
259 peek->start();
Davide Pesavento66777622020-10-09 18:46:03 -0400260 this->advanceClocks(100_ms, 9);
Zhuo Lib3558892016-08-12 15:51:12 -0700261 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
Davide Pesavento65d11552019-06-09 19:15:50 -0400262 BOOST_CHECK_EQUAL(face.getNPendingInterests(), 1);
Davide Pesavento87434be2019-07-25 19:04:23 -0400263 BOOST_CHECK(peek->getResult() == NdnPeek::Result::UNKNOWN);
Davide Pesavento65d11552019-06-09 19:15:50 -0400264
Davide Pesavento66777622020-10-09 18:46:03 -0400265 this->advanceClocks(100_ms, 2);
Davide Pesavento65d11552019-06-09 19:15:50 -0400266 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
267 BOOST_CHECK_EQUAL(face.getNPendingInterests(), 0);
Davide Pesavento87434be2019-07-25 19:04:23 -0400268 BOOST_CHECK(peek->getResult() == NdnPeek::Result::TIMEOUT);
Zhuo Lib3558892016-08-12 15:51:12 -0700269}
270
271BOOST_AUTO_TEST_CASE(TimeoutLessThanLifetime)
272{
273 auto options = makeDefaultOptions();
Junxiao Shib54aabd2018-04-16 19:36:24 +0000274 options.interestLifetime = 200_ms;
275 options.timeout = 100_ms;
Zhuo Lib3558892016-08-12 15:51:12 -0700276 initialize(options);
277
Davide Pesavento65d11552019-06-09 19:15:50 -0400278 BOOST_CHECK_EQUAL(face.sentInterests.size(), 0);
Zhuo Lib3558892016-08-12 15:51:12 -0700279
280 peek->start();
Davide Pesavento66777622020-10-09 18:46:03 -0400281 this->advanceClocks(25_ms, 6);
Zhuo Lib3558892016-08-12 15:51:12 -0700282
283 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
Davide Pesavento65d11552019-06-09 19:15:50 -0400284 BOOST_CHECK_EQUAL(face.getNPendingInterests(), 0);
Davide Pesavento87434be2019-07-25 19:04:23 -0400285 BOOST_CHECK(peek->getResult() == NdnPeek::Result::TIMEOUT);
Zhuo Lib3558892016-08-12 15:51:12 -0700286}
287
288BOOST_AUTO_TEST_CASE(TimeoutGreaterThanLifetime)
289{
290 auto options = makeDefaultOptions();
Junxiao Shib54aabd2018-04-16 19:36:24 +0000291 options.interestLifetime = 50_ms;
292 options.timeout = 200_ms;
Zhuo Lib3558892016-08-12 15:51:12 -0700293 initialize(options);
294
Davide Pesavento65d11552019-06-09 19:15:50 -0400295 BOOST_CHECK_EQUAL(face.sentInterests.size(), 0);
Zhuo Lib3558892016-08-12 15:51:12 -0700296
297 peek->start();
Davide Pesavento66777622020-10-09 18:46:03 -0400298 this->advanceClocks(25_ms, 4);
Zhuo Lib3558892016-08-12 15:51:12 -0700299
300 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
Davide Pesavento65d11552019-06-09 19:15:50 -0400301 BOOST_CHECK_EQUAL(face.getNPendingInterests(), 0);
Davide Pesavento87434be2019-07-25 19:04:23 -0400302 BOOST_CHECK(peek->getResult() == NdnPeek::Result::TIMEOUT);
Zhuo Lib3558892016-08-12 15:51:12 -0700303}
304
Davide Pesavento96028232019-08-17 01:26:13 -0400305BOOST_AUTO_TEST_CASE(OversizedPacket)
306{
307 auto options = makeDefaultOptions();
308 options.applicationParameters = make_shared<Buffer>(MAX_NDN_PACKET_SIZE);
309 initialize(options);
310
311 peek->start();
Davide Pesavento66777622020-10-09 18:46:03 -0400312 BOOST_CHECK_THROW(this->advanceClocks(1_ms, 10), Face::OversizedPacketError);
Davide Pesavento96028232019-08-17 01:26:13 -0400313
314 BOOST_CHECK_EQUAL(face.sentInterests.size(), 0);
315}
316
Zhuo Lib3558892016-08-12 15:51:12 -0700317BOOST_AUTO_TEST_SUITE_END() // TestNdnPeek
318BOOST_AUTO_TEST_SUITE_END() // Peek
319
Davide Pesaventob3570c62022-02-19 19:19:00 -0500320} // namespace ndn::peek::tests