blob: 3d72664ccdb4eb1ee6eda57739eec0c179c34f8d [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 Shi96192952019-05-22 15:45:12 +00003 * Copyright (c) 2014-2019, 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 Pesaventobf28cd72017-08-13 17:22:47 -040023
Zhuo Lib3558892016-08-12 15:51:12 -070024#include <ndn-cxx/util/dummy-client-face.hpp>
25
26#include <boost/mpl/vector.hpp>
27
28namespace ndn {
29namespace peek {
30namespace tests {
31
32using namespace ndn::tests;
33using boost::test_tools::output_test_stream;
34
35class CoutRedirector : noncopyable
36{
37public:
38 explicit
39 CoutRedirector(std::ostream& destination)
40 {
41 m_originalBuf = std::cout.rdbuf(destination.rdbuf());
42 }
43
44 ~CoutRedirector()
45 {
46 std::cout.rdbuf(m_originalBuf);
47 }
48
49private:
50 std::streambuf* m_originalBuf;
51};
52
Davide Pesavento96028232019-08-17 01:26:13 -040053static PeekOptions
54makeDefaultOptions()
55{
56 PeekOptions opt;
57 opt.name = "/peek/test";
58 return opt;
59}
60
Zhuo Lib3558892016-08-12 15:51:12 -070061class NdnPeekFixture : public UnitTestTimeFixture
62{
63protected:
Zhuo Lib3558892016-08-12 15:51:12 -070064 void
Davide Pesavento96028232019-08-17 01:26:13 -040065 initialize(const PeekOptions& opts = makeDefaultOptions())
Zhuo Lib3558892016-08-12 15:51:12 -070066 {
67 peek = make_unique<NdnPeek>(face, opts);
68 }
69
70protected:
71 boost::asio::io_service io;
Davide Pesavento65d11552019-06-09 19:15:50 -040072 ndn::util::DummyClientFace face{io};
Zhuo Lib3558892016-08-12 15:51:12 -070073 output_test_stream output;
74 unique_ptr<NdnPeek> peek;
75};
76
Zhuo Lib3558892016-08-12 15:51:12 -070077class OutputFull
78{
79public:
80 static PeekOptions
81 makeOptions()
82 {
83 return makeDefaultOptions();
84 }
85
86 static void
87 checkOutput(output_test_stream& output, const Data& data)
88 {
89 const Block& block = data.wireEncode();
90 std::string expected(reinterpret_cast<const char*>(block.wire()), block.size());
91 BOOST_CHECK(output.is_equal(expected));
92 }
93
94 static void
95 checkOutput(output_test_stream& output, const lp::Nack& nack)
96 {
97 const Block& block = nack.getHeader().wireEncode();
98 std::string expected(reinterpret_cast<const char*>(block.wire()), block.size());
99 BOOST_CHECK(output.is_equal(expected));
100 }
101};
102
103class OutputPayloadOnly
104{
105public:
106 static PeekOptions
107 makeOptions()
108 {
109 PeekOptions opt = makeDefaultOptions();
110 opt.wantPayloadOnly = true;
111 return opt;
112 }
113
114 static void
115 checkOutput(output_test_stream& output, const Data& data)
116 {
117 const Block& block = data.getContent();
118 std::string expected(reinterpret_cast<const char*>(block.value()), block.value_size());
119 BOOST_CHECK(output.is_equal(expected));
120 }
121
122 static void
123 checkOutput(output_test_stream& output, const lp::Nack& nack)
124 {
125 std::string expected = boost::lexical_cast<std::string>(nack.getReason()) + '\n';
126 BOOST_CHECK(output.is_equal(expected));
127 }
128};
129
130BOOST_AUTO_TEST_SUITE(Peek)
131BOOST_FIXTURE_TEST_SUITE(TestNdnPeek, NdnPeekFixture)
132
133using OutputChecks = boost::mpl::vector<OutputFull, OutputPayloadOnly>;
134
135BOOST_AUTO_TEST_CASE_TEMPLATE(Default, OutputCheck, OutputChecks)
136{
137 auto options = OutputCheck::makeOptions();
138 initialize(options);
139
Junxiao Shib54aabd2018-04-16 19:36:24 +0000140 auto data = makeData(options.name);
Davide Pesavento65d11552019-06-09 19:15:50 -0400141 const std::string payload = "NdnPeekTest";
Zhuo Lib3558892016-08-12 15:51:12 -0700142 data->setContent(reinterpret_cast<const uint8_t*>(payload.data()), payload.size());
143
144 {
145 CoutRedirector redir(output);
146 peek->start();
Junxiao Shib54aabd2018-04-16 19:36:24 +0000147 this->advanceClocks(io, 25_ms, 4);
Zhuo Lib3558892016-08-12 15:51:12 -0700148 face.receive(*data);
149 }
150
151 OutputCheck::checkOutput(output, *data);
152 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
Junxiao Shib54aabd2018-04-16 19:36:24 +0000153 BOOST_CHECK_EQUAL(face.sentInterests.back().getCanBePrefix(), false);
Zhuo Lib3558892016-08-12 15:51:12 -0700154 BOOST_CHECK_EQUAL(face.sentInterests.back().getMustBeFresh(), false);
Davide Pesavento65d11552019-06-09 19:15:50 -0400155 BOOST_CHECK_EQUAL(face.sentInterests.back().getForwardingHint().empty(), true);
Junxiao Shib54aabd2018-04-16 19:36:24 +0000156 BOOST_CHECK_EQUAL(face.sentInterests.back().getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesaventoc214e072019-08-17 01:33:28 -0400157 BOOST_CHECK(face.sentInterests.back().getHopLimit() == nullopt);
158 BOOST_CHECK(!face.sentInterests.back().hasApplicationParameters());
Davide Pesavento87434be2019-07-25 19:04:23 -0400159 BOOST_CHECK(peek->getResult() == NdnPeek::Result::DATA);
Zhuo Lib3558892016-08-12 15:51:12 -0700160}
161
Junxiao Shib54aabd2018-04-16 19:36:24 +0000162BOOST_AUTO_TEST_CASE_TEMPLATE(NonDefault, OutputCheck, OutputChecks)
Zhuo Lib3558892016-08-12 15:51:12 -0700163{
164 auto options = OutputCheck::makeOptions();
Junxiao Shib54aabd2018-04-16 19:36:24 +0000165 options.canBePrefix = true;
Zhuo Lib3558892016-08-12 15:51:12 -0700166 options.mustBeFresh = true;
Junxiao Shib54aabd2018-04-16 19:36:24 +0000167 options.interestLifetime = 200_ms;
Davide Pesaventoc214e072019-08-17 01:33:28 -0400168 options.hopLimit = 64;
Zhuo Lib3558892016-08-12 15:51:12 -0700169 initialize(options);
170
Junxiao Shib54aabd2018-04-16 19:36:24 +0000171 auto data = makeData(Name(options.name).append("suffix"));
Junxiao Shi96192952019-05-22 15:45:12 +0000172 data->setFreshnessPeriod(1_s);
Davide Pesavento65d11552019-06-09 19:15:50 -0400173 const std::string payload = "NdnPeekTest";
Zhuo Lib3558892016-08-12 15:51:12 -0700174 data->setContent(reinterpret_cast<const uint8_t*>(payload.data()), payload.size());
175
176 {
177 CoutRedirector redir(output);
178 peek->start();
Junxiao Shib54aabd2018-04-16 19:36:24 +0000179 this->advanceClocks(io, 25_ms, 4);
Zhuo Lib3558892016-08-12 15:51:12 -0700180 face.receive(*data);
181 }
182
183 OutputCheck::checkOutput(output, *data);
184 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
Junxiao Shib54aabd2018-04-16 19:36:24 +0000185 BOOST_CHECK_EQUAL(face.sentInterests.back().getCanBePrefix(), true);
Zhuo Lib3558892016-08-12 15:51:12 -0700186 BOOST_CHECK_EQUAL(face.sentInterests.back().getMustBeFresh(), true);
Davide Pesavento65d11552019-06-09 19:15:50 -0400187 BOOST_CHECK_EQUAL(face.sentInterests.back().getForwardingHint().empty(), true);
Junxiao Shib54aabd2018-04-16 19:36:24 +0000188 BOOST_CHECK_EQUAL(face.sentInterests.back().getInterestLifetime(), 200_ms);
Davide Pesaventoc214e072019-08-17 01:33:28 -0400189 BOOST_CHECK(face.sentInterests.back().getHopLimit() == 64);
190 BOOST_CHECK(!face.sentInterests.back().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();
Junxiao Shib54aabd2018-04-16 19:36:24 +0000203 this->advanceClocks(io, 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();
Junxiao Shib54aabd2018-04-16 19:36:24 +0000222 this->advanceClocks(io, 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();
239 this->advanceClocks(io, 25_ms, 4);
240
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;
254 options.timeout = 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 Pesavento65d11552019-06-09 19:15:50 -0400260 this->advanceClocks(io, 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
265 this->advanceClocks(io, 100_ms, 2);
266 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 Pesavento65d11552019-06-09 19:15:50 -0400281 this->advanceClocks(io, 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();
Junxiao Shib54aabd2018-04-16 19:36:24 +0000298 this->advanceClocks(io, 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();
312 BOOST_CHECK_THROW(this->advanceClocks(io, 1_ms, 10), Face::OversizedPacketError);
313
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
320} // namespace tests
321} // namespace peek
322} // namespace ndn