blob: d8f2bf505e36b2e9899881dddaf0439718347941 [file] [log] [blame]
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -08002/*
Muktadir Chowdhury1c109b42018-01-10 08:36:00 +00003 * Copyright (c) 2013-2018 Regents of the University of California.
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#include "util/segment-fetcher.hpp"
Davide Pesavento5d0b0102017-10-07 13:43:16 -040023
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050024#include "data.hpp"
Davide Pesavento5d0b0102017-10-07 13:43:16 -040025#include "lp/nack.hpp"
Junxiao Shi2bea5c42017-08-14 20:10:32 +000026#include "util/dummy-client-face.hpp"
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070027
28#include "boost-test.hpp"
Junxiao Shi2bea5c42017-08-14 20:10:32 +000029#include "dummy-validator.hpp"
30#include "make-interest-data.hpp"
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070031#include "../identity-management-time-fixture.hpp"
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070032
33namespace ndn {
34namespace util {
35namespace tests {
36
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000037using namespace ndn::tests;
38
Junxiao Shid5827ce2016-07-14 20:49:37 +000039BOOST_AUTO_TEST_SUITE(Util)
40BOOST_AUTO_TEST_SUITE(TestSegmentFetcher)
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070041
Alexander Afanasyev80782e02017-01-04 13:16:54 -080042class Fixture : public IdentityManagementTimeFixture
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070043{
44public:
45 Fixture()
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070046 : face(io, m_keyChain)
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070047 {
48 }
49
Davide Pesavento5d0b0102017-10-07 13:43:16 -040050 static shared_ptr<Data>
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050051 makeDataSegment(const Name& baseName, uint64_t segment, bool isFinal)
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070052 {
53 const uint8_t buffer[] = "Hello, world!";
54
Junxiao Shid5827ce2016-07-14 20:49:37 +000055 auto data = make_shared<Data>(Name(baseName).appendSegment(segment));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070056 data->setContent(buffer, sizeof(buffer));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050057 if (isFinal) {
Junxiao Shiebfe4a22018-04-01 23:53:40 +000058 data->setFinalBlock(data->getName()[-1]);
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050059 }
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070060
Davide Pesavento5d0b0102017-10-07 13:43:16 -040061 return signData(data);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070062 }
63
64 void
65 onError(uint32_t errorCode)
66 {
67 ++nErrors;
68 lastError = errorCode;
69 }
70
71 void
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050072 onComplete(const ConstBufferPtr& data)
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070073 {
Eric Newberrycc910cd2018-05-06 17:01:40 -070074 ++nCompletions;
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070075 dataSize = data->size();
Davide Pesavento5d0b0102017-10-07 13:43:16 -040076 dataString = std::string(data->get<char>());
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070077 }
78
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050079 void
80 nackLastInterest(lp::NackReason nackReason)
81 {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080082 const Interest& lastInterest = face.sentInterests.back();
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050083 lp::Nack nack = makeNack(lastInterest, nackReason);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080084 face.receive(nack);
Davide Pesavento0f830802018-01-16 23:58:58 -050085 advanceClocks(10_ms);
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050086 }
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070087
Eric Newberrycc910cd2018-05-06 17:01:40 -070088 void
89 connectSignals(shared_ptr<SegmentFetcher> fetcher)
90 {
91 fetcher->onComplete.connect(bind(&Fixture::onComplete, this, _1));
92 fetcher->onError.connect(bind(&Fixture::onError, this, _1));
93 }
94
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070095public:
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080096 DummyClientFace face;
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070097
Eric Newberrycc910cd2018-05-06 17:01:40 -070098 int nErrors = 0;
Davide Pesavento5d0b0102017-10-07 13:43:16 -040099 uint32_t lastError = 0;
Eric Newberrycc910cd2018-05-06 17:01:40 -0700100 int nCompletions = 0;
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400101 size_t dataSize = 0;
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500102 std::string dataString;
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700103};
104
105BOOST_FIXTURE_TEST_CASE(Timeout, Fixture)
106{
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -0800107 DummyValidator acceptValidator;
Eric Newberrycc910cd2018-05-06 17:01:40 -0700108 shared_ptr<SegmentFetcher> fetcher = SegmentFetcher::start(face, Interest("/hello/world", 100_ms),
109 acceptValidator);
110 connectSignals(fetcher);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700111
Davide Pesavento0f830802018-01-16 23:58:58 -0500112 advanceClocks(1_ms);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800113 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700114
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800115 const Interest& interest = face.sentInterests[0];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700116 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world");
117 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true);
118 BOOST_CHECK_EQUAL(interest.getChildSelector(), 1);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800119
Davide Pesavento0f830802018-01-16 23:58:58 -0500120 advanceClocks(98_ms);
Junxiao Shid5827ce2016-07-14 20:49:37 +0000121 BOOST_CHECK_EQUAL(nErrors, 0);
Eric Newberrycc910cd2018-05-06 17:01:40 -0700122 BOOST_CHECK_EQUAL(nCompletions, 0);
Junxiao Shid5827ce2016-07-14 20:49:37 +0000123 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800124
Davide Pesavento0f830802018-01-16 23:58:58 -0500125 advanceClocks(1_ms, 2);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800126 BOOST_CHECK_EQUAL(nErrors, 1);
127 BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::INTEREST_TIMEOUT));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800128 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
129 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700130}
131
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700132BOOST_FIXTURE_TEST_CASE(Basic, Fixture)
133{
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -0800134 DummyValidator acceptValidator;
Eric Newberrycc910cd2018-05-06 17:01:40 -0700135 size_t nAfterSegmentReceived = 0;
136 size_t nAfterSegmentValidated = 0;
137 shared_ptr<SegmentFetcher> fetcher = SegmentFetcher::start(face, Interest("/hello/world", 1000_s),
138 acceptValidator);
139 connectSignals(fetcher);
140 fetcher->afterSegmentReceived.connect(bind([&nAfterSegmentReceived] { ++nAfterSegmentReceived; }));
141 fetcher->afterSegmentValidated.connect(bind([&nAfterSegmentValidated] { ++nAfterSegmentValidated; }));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500142
Davide Pesavento0f830802018-01-16 23:58:58 -0500143 advanceClocks(10_ms);
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500144
145 for (uint64_t i = 0; i < 400; i++) {
Davide Pesavento0f830802018-01-16 23:58:58 -0500146 advanceClocks(10_ms);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800147 face.receive(*makeDataSegment("/hello/world/version0", i, false));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500148 }
Davide Pesavento0f830802018-01-16 23:58:58 -0500149 advanceClocks(10_ms);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800150 face.receive(*makeDataSegment("/hello/world/version0", 400, true));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500151
Davide Pesavento0f830802018-01-16 23:58:58 -0500152 advanceClocks(10_ms);
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500153
154 BOOST_CHECK_EQUAL(nErrors, 0);
Eric Newberrycc910cd2018-05-06 17:01:40 -0700155 BOOST_CHECK_EQUAL(nCompletions, 1);
156 BOOST_CHECK_EQUAL(nAfterSegmentReceived, 401);
157 BOOST_CHECK_EQUAL(nAfterSegmentValidated, 401);
158}
159
160BOOST_FIXTURE_TEST_CASE(MissingSegmentNum, Fixture)
161{
162 DummyValidator acceptValidator;
163 shared_ptr<SegmentFetcher> fetcher = SegmentFetcher::start(face, Interest("/hello/world", 1000_s),
164 acceptValidator);
165 connectSignals(fetcher);
166
167 advanceClocks(10_ms);
168
169 const uint8_t buffer[] = "Hello, world!";
170 auto data = makeData("/hello/world/version0/no-segment");
171 data->setContent(buffer, sizeof(buffer));
172
173 face.receive(*data);
174 advanceClocks(10_ms);
175
176 BOOST_CHECK_EQUAL(nErrors, 1);
177 BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::DATA_HAS_NO_SEGMENT));
178 BOOST_CHECK_EQUAL(nCompletions, 0);
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500179}
180
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500181BOOST_FIXTURE_TEST_CASE(DuplicateNack, Fixture)
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500182{
Eric Newberrycc910cd2018-05-06 17:01:40 -0700183 DummyValidator acceptValidator;
184 shared_ptr<SegmentFetcher> fetcher = SegmentFetcher::start(face, Interest("/hello/world", 1000_s),
185 acceptValidator);
186 connectSignals(fetcher);
Davide Pesavento0f830802018-01-16 23:58:58 -0500187 advanceClocks(10_ms);
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500188
189 // receive nack for the original interest
190 nackLastInterest(lp::NackReason::DUPLICATE);
191
192 // receive nack due to Duplication for the reexpressed interests
193 for (uint32_t i = 1; i <= SegmentFetcher::MAX_INTEREST_REEXPRESS; ++i) {
194 nackLastInterest(lp::NackReason::DUPLICATE);
195 }
196
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800197 BOOST_CHECK_EQUAL(face.sentInterests.size(), (SegmentFetcher::MAX_INTEREST_REEXPRESS + 1));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500198 BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::NACK_ERROR));
199}
200
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500201BOOST_FIXTURE_TEST_CASE(CongestionNack, Fixture)
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500202{
Eric Newberrycc910cd2018-05-06 17:01:40 -0700203 DummyValidator acceptValidator;
204 shared_ptr<SegmentFetcher> fetcher = SegmentFetcher::start(face, Interest("/hello/world", 1000_s),
205 acceptValidator);
206 connectSignals(fetcher);
Davide Pesavento0f830802018-01-16 23:58:58 -0500207 advanceClocks(10_ms);
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500208
209 // receive nack for the original interest
210 nackLastInterest(lp::NackReason::CONGESTION);
211
212 // receive nack due to Congestion for the reexpressed interests
213 for (uint32_t i = 1; i <= SegmentFetcher::MAX_INTEREST_REEXPRESS; ++i) {
214 nackLastInterest(lp::NackReason::CONGESTION);
215 }
216
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800217 BOOST_CHECK_EQUAL(face.sentInterests.size(), (SegmentFetcher::MAX_INTEREST_REEXPRESS + 1));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500218 BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::NACK_ERROR));
219}
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700220
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500221BOOST_FIXTURE_TEST_CASE(SegmentZero, Fixture)
222{
223 int nNacks = 2;
224
225 ndn::Name interestName("ndn:/A");
Eric Newberrycc910cd2018-05-06 17:01:40 -0700226 DummyValidator acceptValidator;
227 shared_ptr<SegmentFetcher> fetcher = SegmentFetcher::start(face, Interest(interestName),
228 acceptValidator);
229 connectSignals(fetcher);
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500230
Davide Pesavento0f830802018-01-16 23:58:58 -0500231 advanceClocks(1000_ms);
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500232
233 for (uint64_t segmentNo = 0; segmentNo <= 3; segmentNo++) {
234 if (segmentNo == 1) {
235 while (nNacks--) {
236 nackLastInterest(lp::NackReason::CONGESTION);
Davide Pesavento0f830802018-01-16 23:58:58 -0500237 advanceClocks(10_ms);
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500238 }
239 }
240
241 auto data = makeDataSegment(interestName, segmentNo, segmentNo == 3);
242 face.receive(*data);
Davide Pesavento0f830802018-01-16 23:58:58 -0500243 advanceClocks(10_ms);
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500244 }
245
246 // Total number of sent interests should be 6: one interest for segment zero and segment one each,
247 // two re-expressed interests for segment one after getting nack twice, and two interests for
248 // segment two and three
249 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 6);
250
251 BOOST_CHECK_EQUAL(face.sentInterests[0].getName(), ndn::Name("ndn:/A"));
252 BOOST_CHECK_EQUAL(face.sentInterests[1].getName(), ndn::Name("ndn:/A/%00%01"));
253 BOOST_CHECK_EQUAL(face.sentInterests[2].getName(), ndn::Name("ndn:/A/%00%01"));
254 BOOST_CHECK_EQUAL(face.sentInterests[3].getName(), ndn::Name("ndn:/A/%00%01"));
255 BOOST_CHECK_EQUAL(face.sentInterests[4].getName(), ndn::Name("ndn:/A/%00%02"));
256 BOOST_CHECK_EQUAL(face.sentInterests[5].getName(), ndn::Name("ndn:/A/%00%03"));
257}
258
Eric Newberrycc910cd2018-05-06 17:01:40 -0700259BOOST_FIXTURE_TEST_CASE(SingleSegment, Fixture)
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500260{
Eric Newberrycc910cd2018-05-06 17:01:40 -0700261 DummyValidator acceptValidator;
262 shared_ptr<SegmentFetcher> fetcher = SegmentFetcher::start(face, Interest("ndn:/", 1000_s),
263 acceptValidator);
264 connectSignals(fetcher);
Davide Pesavento0f830802018-01-16 23:58:58 -0500265 advanceClocks(10_ms);
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500266 nackLastInterest(lp::NackReason::DUPLICATE);
267 face.receive(*makeDataSegment("/hello/world", 0, true));
268
269 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 2);
270 BOOST_CHECK_EQUAL(face.sentInterests[0].getName(), ndn::Name("ndn:/"));
271 BOOST_CHECK_EQUAL(face.sentInterests[1].getName(), ndn::Name("ndn:/"));
Eric Newberrycc910cd2018-05-06 17:01:40 -0700272 BOOST_REQUIRE_EQUAL(nCompletions, 1);
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500273}
274
Eric Newberrycc910cd2018-05-06 17:01:40 -0700275BOOST_FIXTURE_TEST_CASE(ValidationFailure, Fixture)
Muktadir Chowdhury1c109b42018-01-10 08:36:00 +0000276{
277 DummyValidator validator;
Eric Newberrycc910cd2018-05-06 17:01:40 -0700278 validator.getPolicy().setResultCallback([] (const Name& name) {
279 return name.at(-1).toSegment() % 2 == 0;
Muktadir Chowdhury1c109b42018-01-10 08:36:00 +0000280 });
Eric Newberrycc910cd2018-05-06 17:01:40 -0700281 shared_ptr<SegmentFetcher> fetcher = SegmentFetcher::start(face, Interest("/hello/world", 1000_s),
282 validator);
283 connectSignals(fetcher);
Muktadir Chowdhury1c109b42018-01-10 08:36:00 +0000284
285 auto data1 = makeDataSegment("/hello/world", 0, false);
286 auto data2 = makeDataSegment("/hello/world", 1, true);
287
288 size_t nRecvSegments = 0;
289 fetcher->afterSegmentReceived.connect([&nRecvSegments] (const Data& receivedSegment) {
290 ++nRecvSegments;
291 });
292
293 size_t nValidatedSegments = 0;
294 fetcher->afterSegmentValidated.connect([&nValidatedSegments] (const Data& validatedSegment) {
295 ++nValidatedSegments;
296 });
297
Davide Pesavento0f830802018-01-16 23:58:58 -0500298 advanceClocks(10_ms, 10);
Muktadir Chowdhury1c109b42018-01-10 08:36:00 +0000299
300 face.receive(*data1);
301
Davide Pesavento0f830802018-01-16 23:58:58 -0500302 advanceClocks(10_ms, 10);
Muktadir Chowdhury1c109b42018-01-10 08:36:00 +0000303
304 face.receive(*data2);
305
Davide Pesavento0f830802018-01-16 23:58:58 -0500306 advanceClocks(10_ms, 10);
Muktadir Chowdhury1c109b42018-01-10 08:36:00 +0000307
308 BOOST_CHECK_EQUAL(nRecvSegments, 2);
309 BOOST_CHECK_EQUAL(nValidatedSegments, 1);
310 BOOST_CHECK_EQUAL(nErrors, 1);
311}
312
Eric Newberrycc910cd2018-05-06 17:01:40 -0700313// Tests deprecated `fetch` API that uses callbacks instead of signals. This test case will be
314// removed when this API is removed.
315BOOST_FIXTURE_TEST_CASE(DeprecatedFetch, Fixture)
316{
317 DummyValidator acceptValidator;
318 SegmentFetcher::fetch(face, Interest("/hello/world", 1000_s),
319 acceptValidator,
320 bind(&Fixture::onComplete, this, _1),
321 bind(&Fixture::onError, this, _1));
322
323 advanceClocks(10_ms);
324
325 face.receive(*makeDataSegment("/hello/world/version0", 0, true));
326 advanceClocks(10_ms);
327
328 BOOST_CHECK_EQUAL(nErrors, 0);
329 BOOST_CHECK_EQUAL(nCompletions, 1);
330
331 BOOST_CHECK_EQUAL(dataSize, 14);
332
333 const uint8_t buffer[] = "Hello, world!";
334 std::string bufferString(reinterpret_cast<const char*>(buffer));
335
336 BOOST_CHECK_EQUAL(dataString, bufferString);
337
338 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
339 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
340
341 const Interest& interest = face.sentInterests[0];
342 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world");
343 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true);
344 BOOST_CHECK_EQUAL(interest.getChildSelector(), 1);
345}
346
347// Tests deprecated `fetch` API that uses callbacks instead of signals (with an accepting shared_ptr
348// Validator). This test case will be removed when this API is removed.
349BOOST_FIXTURE_TEST_CASE(DeprecatedFetchSharedPtrComplete, Fixture)
350{
351 auto acceptValidator = make_shared<DummyValidator>(true);
352 SegmentFetcher::fetch(face, Interest("/hello/world", 1000_s),
353 acceptValidator,
354 bind(&Fixture::onComplete, this, _1),
355 bind(&Fixture::onError, this, _1));
356
357 weak_ptr<DummyValidator> weakValidator = acceptValidator;
358 BOOST_CHECK(!weakValidator.expired());
359 acceptValidator.reset();
360 BOOST_CHECK(!weakValidator.expired());
361
362 advanceClocks(10_ms);
363
364 BOOST_CHECK(!weakValidator.expired());
365 face.receive(*makeDataSegment("/hello/world/version0", 0, true));
366
367 advanceClocks(10_ms);
368
369 BOOST_CHECK(weakValidator.expired());
370 BOOST_CHECK_EQUAL(nErrors, 0);
371 BOOST_CHECK_EQUAL(nCompletions, 1);
372
373 BOOST_CHECK_EQUAL(dataSize, 14);
374
375 const uint8_t buffer[] = "Hello, world!";
376 std::string bufferString(reinterpret_cast<const char*>(buffer));
377
378 BOOST_CHECK_EQUAL(dataString, bufferString);
379
380 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
381 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
382
383 const Interest& interest = face.sentInterests[0];
384 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world");
385 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true);
386 BOOST_CHECK_EQUAL(interest.getChildSelector(), 1);
387}
388
389// Tests deprecated `fetch` API that uses callbacks instead of signals (with a rejecting shared_ptr
390// Validator). This test case will be removed when this API is removed.
391BOOST_FIXTURE_TEST_CASE(DeprecatedFetchSharedPtrError, Fixture)
392{
393 auto acceptValidator = make_shared<DummyValidator>(false);
394 SegmentFetcher::fetch(face, Interest("/hello/world", 1000_s),
395 acceptValidator,
396 bind(&Fixture::onComplete, this, _1),
397 bind(&Fixture::onError, this, _1));
398
399 weak_ptr<DummyValidator> weakValidator = acceptValidator;
400 BOOST_CHECK(!weakValidator.expired());
401 acceptValidator.reset();
402 BOOST_CHECK(!weakValidator.expired());
403
404 advanceClocks(10_ms);
405
406 BOOST_CHECK(!weakValidator.expired());
407 face.receive(*makeDataSegment("/hello/world/version0", 0, true));
408
409 advanceClocks(10_ms);
410
411 BOOST_CHECK(weakValidator.expired());
412 BOOST_CHECK_EQUAL(nErrors, 1);
413 BOOST_CHECK_EQUAL(nCompletions, 0);
414
415 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
416 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
417
418 const Interest& interest = face.sentInterests[0];
419 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world");
420 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true);
421 BOOST_CHECK_EQUAL(interest.getChildSelector(), 1);
422}
423
Junxiao Shid5827ce2016-07-14 20:49:37 +0000424BOOST_AUTO_TEST_SUITE_END() // TestSegmentFetcher
425BOOST_AUTO_TEST_SUITE_END() // Util
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700426
427} // namespace tests
428} // namespace util
429} // namespace ndn