blob: e3fd5b37153d3ceab8d82d867e6eec27aadf4b0f [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/*
3 * Copyright (c) 2014-2018, 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"));
Zhuo Lib3558892016-08-12 15:51:12 -0700176 std::string payload = "NdnPeekTest";
177 data->setContent(reinterpret_cast<const uint8_t*>(payload.data()), payload.size());
178
179 {
180 CoutRedirector redir(output);
181 peek->start();
Junxiao Shib54aabd2018-04-16 19:36:24 +0000182 this->advanceClocks(io, 25_ms, 4);
Zhuo Lib3558892016-08-12 15:51:12 -0700183 face.receive(*data);
184 }
185
186 OutputCheck::checkOutput(output, *data);
187 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
Junxiao Shib54aabd2018-04-16 19:36:24 +0000188 BOOST_CHECK_EQUAL(face.sentInterests.back().getCanBePrefix(), true);
Zhuo Lib3558892016-08-12 15:51:12 -0700189 BOOST_CHECK_EQUAL(face.sentInterests.back().getMustBeFresh(), true);
Junxiao Shib54aabd2018-04-16 19:36:24 +0000190 BOOST_CHECK(face.sentInterests.back().getForwardingHint().empty());
191 BOOST_CHECK_EQUAL(face.sentInterests.back().getInterestLifetime(), 200_ms);
Zhuo Lib3558892016-08-12 15:51:12 -0700192 BOOST_CHECK(peek->getResultCode() == ResultCode::DATA);
193}
194
195BOOST_AUTO_TEST_CASE_TEMPLATE(ReceiveNackWithReason, OutputCheck, OutputChecks)
196{
197 auto options = OutputCheck::makeOptions();
198 initialize(options);
199 lp::Nack nack;
200
201 {
202 CoutRedirector redir(output);
203 peek->start();
Junxiao Shib54aabd2018-04-16 19:36:24 +0000204 this->advanceClocks(io, 25_ms, 4);
Zhuo Lib3558892016-08-12 15:51:12 -0700205 nack = makeNack(face.sentInterests.at(0), lp::NackReason::NO_ROUTE);
206 face.receive(nack);
207 }
208
209 OutputCheck::checkOutput(output, nack);
210 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
211 BOOST_CHECK(peek->getResultCode() == ResultCode::NACK);
212}
213
214BOOST_AUTO_TEST_CASE_TEMPLATE(ReceiveNackWithoutReason, OutputCheck, OutputChecks)
215{
216 auto options = OutputCheck::makeOptions();
217 initialize(options);
218 lp::Nack nack;
219
220 {
221 CoutRedirector redir(output);
222 peek->start();
Junxiao Shib54aabd2018-04-16 19:36:24 +0000223 this->advanceClocks(io, 25_ms, 4);
Zhuo Lib3558892016-08-12 15:51:12 -0700224 nack = makeNack(face.sentInterests.at(0), lp::NackReason::NONE);
225 face.receive(nack);
226 }
227
228 OutputCheck::checkOutput(output, nack);
229 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
230 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
231 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0);
232 BOOST_CHECK(peek->getResultCode() == ResultCode::NACK);
233}
234
235BOOST_AUTO_TEST_CASE(TimeoutDefault)
236{
237 auto options = makeDefaultOptions();
238 initialize(options);
239
240 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 0);
241
242 peek->start();
Junxiao Shib54aabd2018-04-16 19:36:24 +0000243 this->advanceClocks(io, 25_ms, 4);
Zhuo Lib3558892016-08-12 15:51:12 -0700244
245 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
246 BOOST_CHECK(peek->getResultCode() == ResultCode::TIMEOUT);
247}
248
249BOOST_AUTO_TEST_CASE(TimeoutLessThanLifetime)
250{
251 auto options = makeDefaultOptions();
Junxiao Shib54aabd2018-04-16 19:36:24 +0000252 options.interestLifetime = 200_ms;
253 options.timeout = 100_ms;
Zhuo Lib3558892016-08-12 15:51:12 -0700254 initialize(options);
255
256 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 0);
257
258 peek->start();
Junxiao Shib54aabd2018-04-16 19:36:24 +0000259 this->advanceClocks(io, 25_ms, 8);
Zhuo Lib3558892016-08-12 15:51:12 -0700260
261 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
262 BOOST_CHECK(peek->getResultCode() == ResultCode::TIMEOUT);
263}
264
265BOOST_AUTO_TEST_CASE(TimeoutGreaterThanLifetime)
266{
267 auto options = makeDefaultOptions();
Junxiao Shib54aabd2018-04-16 19:36:24 +0000268 options.interestLifetime = 50_ms;
269 options.timeout = 200_ms;
Zhuo Lib3558892016-08-12 15:51:12 -0700270 initialize(options);
271
272 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 0);
273
274 peek->start();
Junxiao Shib54aabd2018-04-16 19:36:24 +0000275 this->advanceClocks(io, 25_ms, 4);
Zhuo Lib3558892016-08-12 15:51:12 -0700276
277 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
278 BOOST_CHECK(peek->getResultCode() == ResultCode::TIMEOUT);
279}
280
281BOOST_AUTO_TEST_SUITE_END() // TestNdnPeek
282BOOST_AUTO_TEST_SUITE_END() // Peek
283
284} // namespace tests
285} // namespace peek
286} // namespace ndn