blob: a54426bfb01b84b01fd8ab6eb3e3cfdba4e1378d [file] [log] [blame]
Zhuo Lib3558892016-08-12 15:51:12 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Eric Newberrybfd66ad2017-05-24 18:19:26 -07003 * Copyright (c) 2014-2017, 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"
23#include <ndn-cxx/util/dummy-client-face.hpp>
24
25#include <boost/mpl/vector.hpp>
26
27namespace ndn {
28namespace peek {
29namespace tests {
30
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
52class NdnPeekFixture : public UnitTestTimeFixture
53{
54protected:
55 NdnPeekFixture()
56 : face(io)
57 {
58 }
59
60 void
61 initialize(const PeekOptions& opts)
62 {
63 peek = make_unique<NdnPeek>(face, opts);
64 }
65
66protected:
67 boost::asio::io_service io;
68 ndn::util::DummyClientFace face;
69 output_test_stream output;
70 unique_ptr<NdnPeek> peek;
71};
72
73static PeekOptions
74makeDefaultOptions()
75{
76 PeekOptions opt;
77 opt.prefix = "ndn:/peek/test";
78 opt.minSuffixComponents = -1;
79 opt.maxSuffixComponents = -1;
80 opt.interestLifetime = DEFAULT_INTEREST_LIFETIME;
81 opt.timeout = time::milliseconds(200);
82 opt.link = nullptr;
83 opt.isVerbose = false;
84 opt.mustBeFresh = false;
85 opt.wantRightmostChild = false;
86 opt.wantPayloadOnly = false;
87 return opt;
88}
89
90class OutputFull
91{
92public:
93 static PeekOptions
94 makeOptions()
95 {
96 return makeDefaultOptions();
97 }
98
99 static void
100 checkOutput(output_test_stream& output, const Data& data)
101 {
102 const Block& block = data.wireEncode();
103 std::string expected(reinterpret_cast<const char*>(block.wire()), block.size());
104 BOOST_CHECK(output.is_equal(expected));
105 }
106
107 static void
108 checkOutput(output_test_stream& output, const lp::Nack& nack)
109 {
110 const Block& block = nack.getHeader().wireEncode();
111 std::string expected(reinterpret_cast<const char*>(block.wire()), block.size());
112 BOOST_CHECK(output.is_equal(expected));
113 }
114};
115
116class OutputPayloadOnly
117{
118public:
119 static PeekOptions
120 makeOptions()
121 {
122 PeekOptions opt = makeDefaultOptions();
123 opt.wantPayloadOnly = true;
124 return opt;
125 }
126
127 static void
128 checkOutput(output_test_stream& output, const Data& data)
129 {
130 const Block& block = data.getContent();
131 std::string expected(reinterpret_cast<const char*>(block.value()), block.value_size());
132 BOOST_CHECK(output.is_equal(expected));
133 }
134
135 static void
136 checkOutput(output_test_stream& output, const lp::Nack& nack)
137 {
138 std::string expected = boost::lexical_cast<std::string>(nack.getReason()) + '\n';
139 BOOST_CHECK(output.is_equal(expected));
140 }
141};
142
143BOOST_AUTO_TEST_SUITE(Peek)
144BOOST_FIXTURE_TEST_SUITE(TestNdnPeek, NdnPeekFixture)
145
146using OutputChecks = boost::mpl::vector<OutputFull, OutputPayloadOnly>;
147
148BOOST_AUTO_TEST_CASE_TEMPLATE(Default, OutputCheck, OutputChecks)
149{
150 auto options = OutputCheck::makeOptions();
151 initialize(options);
152
153 auto data = makeData(options.prefix);
154 std::string payload = "NdnPeekTest";
155 data->setContent(reinterpret_cast<const uint8_t*>(payload.data()), payload.size());
156
157 {
158 CoutRedirector redir(output);
159 peek->start();
160 this->advanceClocks(io, time::milliseconds(25), 4);
161 face.receive(*data);
162 }
163
164 OutputCheck::checkOutput(output, *data);
165 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
166 BOOST_CHECK_EQUAL(face.sentInterests.back().getMaxSuffixComponents(), -1);
167 BOOST_CHECK_EQUAL(face.sentInterests.back().getMinSuffixComponents(), -1);
168 BOOST_CHECK_EQUAL(face.sentInterests.back().getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Junxiao Shia5b60cc2017-07-26 01:35:48 +0000169 BOOST_CHECK(face.sentInterests.back().getForwardingHint().empty());
Zhuo Lib3558892016-08-12 15:51:12 -0700170 BOOST_CHECK_EQUAL(face.sentInterests.back().getMustBeFresh(), false);
Eric Newberrybfd66ad2017-05-24 18:19:26 -0700171 BOOST_CHECK_EQUAL(face.sentInterests.back().getChildSelector(), DEFAULT_CHILD_SELECTOR);
Zhuo Lib3558892016-08-12 15:51:12 -0700172 BOOST_CHECK(peek->getResultCode() == ResultCode::DATA);
173}
174
175BOOST_AUTO_TEST_CASE_TEMPLATE(Selectors, OutputCheck, OutputChecks)
176{
177 auto options = OutputCheck::makeOptions();
178 options.minSuffixComponents = 1;
179 options.maxSuffixComponents = 1;
180 options.interestLifetime = time::milliseconds(200);
181 options.link = makeLink("/net/ndnsim", {{10, "/telia/terabits"}, {20, "/ucla/cs"}});
182 options.mustBeFresh = true;
183 options.wantRightmostChild = true;
184 initialize(options);
185
186 auto data = makeData(options.prefix);
187 std::string payload = "NdnPeekTest";
188 data->setContent(reinterpret_cast<const uint8_t*>(payload.data()), payload.size());
189
190 {
191 CoutRedirector redir(output);
192 peek->start();
193 this->advanceClocks(io, time::milliseconds(25), 4);
194 face.receive(*data);
195 }
196
197 OutputCheck::checkOutput(output, *data);
198 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
199 BOOST_CHECK_EQUAL(face.sentInterests.back().getMaxSuffixComponents(), 1);
200 BOOST_CHECK_EQUAL(face.sentInterests.back().getMinSuffixComponents(), 1);
201 BOOST_CHECK_EQUAL(face.sentInterests.back().getInterestLifetime(), time::milliseconds(200));
Junxiao Shia5b60cc2017-07-26 01:35:48 +0000202 BOOST_CHECK_EQUAL(face.sentInterests.back().getForwardingHint(), options.link->getDelegationList());
Zhuo Lib3558892016-08-12 15:51:12 -0700203 BOOST_CHECK_EQUAL(face.sentInterests.back().getMustBeFresh(), true);
Eric Newberrybfd66ad2017-05-24 18:19:26 -0700204 BOOST_CHECK_EQUAL(face.sentInterests.back().getChildSelector(), 1);
Zhuo Lib3558892016-08-12 15:51:12 -0700205 BOOST_CHECK(peek->getResultCode() == ResultCode::DATA);
206}
207
208BOOST_AUTO_TEST_CASE_TEMPLATE(ReceiveNackWithReason, OutputCheck, OutputChecks)
209{
210 auto options = OutputCheck::makeOptions();
211 initialize(options);
212 lp::Nack nack;
213
214 {
215 CoutRedirector redir(output);
216 peek->start();
217 this->advanceClocks(io, time::milliseconds(25), 4);
218 nack = makeNack(face.sentInterests.at(0), lp::NackReason::NO_ROUTE);
219 face.receive(nack);
220 }
221
222 OutputCheck::checkOutput(output, nack);
223 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
224 BOOST_CHECK(peek->getResultCode() == ResultCode::NACK);
225}
226
227BOOST_AUTO_TEST_CASE_TEMPLATE(ReceiveNackWithoutReason, OutputCheck, OutputChecks)
228{
229 auto options = OutputCheck::makeOptions();
230 initialize(options);
231 lp::Nack nack;
232
233 {
234 CoutRedirector redir(output);
235 peek->start();
236 this->advanceClocks(io, time::milliseconds(25), 4);
237 nack = makeNack(face.sentInterests.at(0), lp::NackReason::NONE);
238 face.receive(nack);
239 }
240
241 OutputCheck::checkOutput(output, nack);
242 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
243 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
244 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0);
245 BOOST_CHECK(peek->getResultCode() == ResultCode::NACK);
246}
247
248BOOST_AUTO_TEST_CASE(TimeoutDefault)
249{
250 auto options = makeDefaultOptions();
251 initialize(options);
252
253 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 0);
254
255 peek->start();
256 this->advanceClocks(io, time::milliseconds(25), 4);
257
258 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
259 BOOST_CHECK(peek->getResultCode() == ResultCode::TIMEOUT);
260}
261
262BOOST_AUTO_TEST_CASE(TimeoutLessThanLifetime)
263{
264 auto options = makeDefaultOptions();
265 options.interestLifetime = time::milliseconds(200);
266 options.timeout = time::milliseconds(100);
267 initialize(options);
268
269 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 0);
270
271 peek->start();
272 this->advanceClocks(io, time::milliseconds(25), 8);
273
274 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
275 BOOST_CHECK(peek->getResultCode() == ResultCode::TIMEOUT);
276}
277
278BOOST_AUTO_TEST_CASE(TimeoutGreaterThanLifetime)
279{
280 auto options = makeDefaultOptions();
281 options.interestLifetime = time::milliseconds(50);
282 options.timeout = time::milliseconds(200);
283 initialize(options);
284
285 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 0);
286
287 peek->start();
288 this->advanceClocks(io, time::milliseconds(25), 4);
289
290 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
291 BOOST_CHECK(peek->getResultCode() == ResultCode::TIMEOUT);
292}
293
294BOOST_AUTO_TEST_SUITE_END() // TestNdnPeek
295BOOST_AUTO_TEST_SUITE_END() // Peek
296
297} // namespace tests
298} // namespace peek
299} // namespace ndn