blob: 332c2d27ab956e45024290b52c0b4b7d123aec66 [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
53class NdnPeekFixture : public UnitTestTimeFixture
54{
55protected:
56 NdnPeekFixture()
57 : face(io)
58 {
59 }
60
61 void
62 initialize(const PeekOptions& opts)
63 {
64 peek = make_unique<NdnPeek>(face, opts);
65 }
66
67protected:
68 boost::asio::io_service io;
69 ndn::util::DummyClientFace face;
70 output_test_stream output;
71 unique_ptr<NdnPeek> peek;
72};
73
74static PeekOptions
75makeDefaultOptions()
76{
77 PeekOptions opt;
Junxiao Shib54aabd2018-04-16 19:36:24 +000078 opt.name = "ndn:/peek/test";
Zhuo Lib3558892016-08-12 15:51:12 -070079 opt.interestLifetime = DEFAULT_INTEREST_LIFETIME;
Junxiao Shib54aabd2018-04-16 19:36:24 +000080 opt.timeout = 200_ms;
Zhuo Lib3558892016-08-12 15:51:12 -070081 return opt;
82}
83
84class OutputFull
85{
86public:
87 static PeekOptions
88 makeOptions()
89 {
90 return makeDefaultOptions();
91 }
92
93 static void
94 checkOutput(output_test_stream& output, const Data& data)
95 {
96 const Block& block = data.wireEncode();
97 std::string expected(reinterpret_cast<const char*>(block.wire()), block.size());
98 BOOST_CHECK(output.is_equal(expected));
99 }
100
101 static void
102 checkOutput(output_test_stream& output, const lp::Nack& nack)
103 {
104 const Block& block = nack.getHeader().wireEncode();
105 std::string expected(reinterpret_cast<const char*>(block.wire()), block.size());
106 BOOST_CHECK(output.is_equal(expected));
107 }
108};
109
110class OutputPayloadOnly
111{
112public:
113 static PeekOptions
114 makeOptions()
115 {
116 PeekOptions opt = makeDefaultOptions();
117 opt.wantPayloadOnly = true;
118 return opt;
119 }
120
121 static void
122 checkOutput(output_test_stream& output, const Data& data)
123 {
124 const Block& block = data.getContent();
125 std::string expected(reinterpret_cast<const char*>(block.value()), block.value_size());
126 BOOST_CHECK(output.is_equal(expected));
127 }
128
129 static void
130 checkOutput(output_test_stream& output, const lp::Nack& nack)
131 {
132 std::string expected = boost::lexical_cast<std::string>(nack.getReason()) + '\n';
133 BOOST_CHECK(output.is_equal(expected));
134 }
135};
136
137BOOST_AUTO_TEST_SUITE(Peek)
138BOOST_FIXTURE_TEST_SUITE(TestNdnPeek, NdnPeekFixture)
139
140using OutputChecks = boost::mpl::vector<OutputFull, OutputPayloadOnly>;
141
142BOOST_AUTO_TEST_CASE_TEMPLATE(Default, OutputCheck, OutputChecks)
143{
144 auto options = OutputCheck::makeOptions();
145 initialize(options);
146
Junxiao Shib54aabd2018-04-16 19:36:24 +0000147 auto data = makeData(options.name);
Zhuo Lib3558892016-08-12 15:51:12 -0700148 std::string payload = "NdnPeekTest";
149 data->setContent(reinterpret_cast<const uint8_t*>(payload.data()), payload.size());
150
151 {
152 CoutRedirector redir(output);
153 peek->start();
Junxiao Shib54aabd2018-04-16 19:36:24 +0000154 this->advanceClocks(io, 25_ms, 4);
Zhuo Lib3558892016-08-12 15:51:12 -0700155 face.receive(*data);
156 }
157
158 OutputCheck::checkOutput(output, *data);
159 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
Junxiao Shib54aabd2018-04-16 19:36:24 +0000160 BOOST_CHECK_EQUAL(face.sentInterests.back().getCanBePrefix(), false);
Zhuo Lib3558892016-08-12 15:51:12 -0700161 BOOST_CHECK_EQUAL(face.sentInterests.back().getMustBeFresh(), false);
Junxiao Shib54aabd2018-04-16 19:36:24 +0000162 BOOST_CHECK(face.sentInterests.back().getForwardingHint().empty());
163 BOOST_CHECK_EQUAL(face.sentInterests.back().getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Zhuo Lib3558892016-08-12 15:51:12 -0700164 BOOST_CHECK(peek->getResultCode() == ResultCode::DATA);
165}
166
Junxiao Shib54aabd2018-04-16 19:36:24 +0000167BOOST_AUTO_TEST_CASE_TEMPLATE(NonDefault, OutputCheck, OutputChecks)
Zhuo Lib3558892016-08-12 15:51:12 -0700168{
169 auto options = OutputCheck::makeOptions();
Junxiao Shib54aabd2018-04-16 19:36:24 +0000170 options.canBePrefix = true;
Zhuo Lib3558892016-08-12 15:51:12 -0700171 options.mustBeFresh = true;
Junxiao Shib54aabd2018-04-16 19:36:24 +0000172 options.interestLifetime = 200_ms;
Zhuo Lib3558892016-08-12 15:51:12 -0700173 initialize(options);
174
Junxiao Shib54aabd2018-04-16 19:36:24 +0000175 auto data = makeData(Name(options.name).append("suffix"));
Junxiao Shi96192952019-05-22 15:45:12 +0000176 data->setFreshnessPeriod(1_s);
Zhuo Lib3558892016-08-12 15:51:12 -0700177 std::string payload = "NdnPeekTest";
178 data->setContent(reinterpret_cast<const uint8_t*>(payload.data()), payload.size());
179
180 {
181 CoutRedirector redir(output);
182 peek->start();
Junxiao Shib54aabd2018-04-16 19:36:24 +0000183 this->advanceClocks(io, 25_ms, 4);
Zhuo Lib3558892016-08-12 15:51:12 -0700184 face.receive(*data);
185 }
186
187 OutputCheck::checkOutput(output, *data);
188 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
Junxiao Shib54aabd2018-04-16 19:36:24 +0000189 BOOST_CHECK_EQUAL(face.sentInterests.back().getCanBePrefix(), true);
Zhuo Lib3558892016-08-12 15:51:12 -0700190 BOOST_CHECK_EQUAL(face.sentInterests.back().getMustBeFresh(), true);
Junxiao Shib54aabd2018-04-16 19:36:24 +0000191 BOOST_CHECK(face.sentInterests.back().getForwardingHint().empty());
192 BOOST_CHECK_EQUAL(face.sentInterests.back().getInterestLifetime(), 200_ms);
Zhuo Lib3558892016-08-12 15:51:12 -0700193 BOOST_CHECK(peek->getResultCode() == ResultCode::DATA);
194}
195
196BOOST_AUTO_TEST_CASE_TEMPLATE(ReceiveNackWithReason, OutputCheck, OutputChecks)
197{
198 auto options = OutputCheck::makeOptions();
199 initialize(options);
200 lp::Nack nack;
201
202 {
203 CoutRedirector redir(output);
204 peek->start();
Junxiao Shib54aabd2018-04-16 19:36:24 +0000205 this->advanceClocks(io, 25_ms, 4);
Zhuo Lib3558892016-08-12 15:51:12 -0700206 nack = makeNack(face.sentInterests.at(0), lp::NackReason::NO_ROUTE);
207 face.receive(nack);
208 }
209
210 OutputCheck::checkOutput(output, nack);
211 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
212 BOOST_CHECK(peek->getResultCode() == ResultCode::NACK);
213}
214
215BOOST_AUTO_TEST_CASE_TEMPLATE(ReceiveNackWithoutReason, OutputCheck, OutputChecks)
216{
217 auto options = OutputCheck::makeOptions();
218 initialize(options);
219 lp::Nack nack;
220
221 {
222 CoutRedirector redir(output);
223 peek->start();
Junxiao Shib54aabd2018-04-16 19:36:24 +0000224 this->advanceClocks(io, 25_ms, 4);
Zhuo Lib3558892016-08-12 15:51:12 -0700225 nack = makeNack(face.sentInterests.at(0), lp::NackReason::NONE);
226 face.receive(nack);
227 }
228
229 OutputCheck::checkOutput(output, nack);
230 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
231 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
232 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0);
233 BOOST_CHECK(peek->getResultCode() == ResultCode::NACK);
234}
235
236BOOST_AUTO_TEST_CASE(TimeoutDefault)
237{
238 auto options = makeDefaultOptions();
239 initialize(options);
240
241 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 0);
242
243 peek->start();
Junxiao Shib54aabd2018-04-16 19:36:24 +0000244 this->advanceClocks(io, 25_ms, 4);
Zhuo Lib3558892016-08-12 15:51:12 -0700245
246 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
247 BOOST_CHECK(peek->getResultCode() == ResultCode::TIMEOUT);
248}
249
250BOOST_AUTO_TEST_CASE(TimeoutLessThanLifetime)
251{
252 auto options = makeDefaultOptions();
Junxiao Shib54aabd2018-04-16 19:36:24 +0000253 options.interestLifetime = 200_ms;
254 options.timeout = 100_ms;
Zhuo Lib3558892016-08-12 15:51:12 -0700255 initialize(options);
256
257 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 0);
258
259 peek->start();
Junxiao Shib54aabd2018-04-16 19:36:24 +0000260 this->advanceClocks(io, 25_ms, 8);
Zhuo Lib3558892016-08-12 15:51:12 -0700261
262 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
263 BOOST_CHECK(peek->getResultCode() == ResultCode::TIMEOUT);
264}
265
266BOOST_AUTO_TEST_CASE(TimeoutGreaterThanLifetime)
267{
268 auto options = makeDefaultOptions();
Junxiao Shib54aabd2018-04-16 19:36:24 +0000269 options.interestLifetime = 50_ms;
270 options.timeout = 200_ms;
Zhuo Lib3558892016-08-12 15:51:12 -0700271 initialize(options);
272
273 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 0);
274
275 peek->start();
Junxiao Shib54aabd2018-04-16 19:36:24 +0000276 this->advanceClocks(io, 25_ms, 4);
Zhuo Lib3558892016-08-12 15:51:12 -0700277
278 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
279 BOOST_CHECK(peek->getResultCode() == ResultCode::TIMEOUT);
280}
281
282BOOST_AUTO_TEST_SUITE_END() // TestNdnPeek
283BOOST_AUTO_TEST_SUITE_END() // Peek
284
285} // namespace tests
286} // namespace peek
287} // namespace ndn