blob: 02ef85b9fb5d0186620350fcd40ccff6ceed6d50 [file] [log] [blame]
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -08003 * Copyright (c) 2013-2017 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"
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050023#include "security/validator-null.hpp"
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -080024#include "lp/nack-header.hpp"
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050025#include "data.hpp"
26#include "encoding/block.hpp"
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070027
28#include "boost-test.hpp"
Junxiao Shia60d9362014-11-12 09:38:21 -070029#include "util/dummy-client-face.hpp"
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070030#include "../identity-management-time-fixture.hpp"
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050031#include "../make-interest-data.hpp"
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -080032#include "../../dummy-validator.hpp"
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070033
34namespace ndn {
35namespace util {
36namespace tests {
37
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000038using namespace ndn::tests;
39
Junxiao Shid5827ce2016-07-14 20:49:37 +000040BOOST_AUTO_TEST_SUITE(Util)
41BOOST_AUTO_TEST_SUITE(TestSegmentFetcher)
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070042
Alexander Afanasyev80782e02017-01-04 13:16:54 -080043class Fixture : public IdentityManagementTimeFixture
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070044{
45public:
46 Fixture()
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070047 : face(io, m_keyChain)
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070048 , nErrors(0)
Junxiao Shid5827ce2016-07-14 20:49:37 +000049 , nData(0)
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070050 , dataSize(0)
51 {
52 }
53
54 shared_ptr<Data>
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050055 makeDataSegment(const Name& baseName, uint64_t segment, bool isFinal)
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070056 {
57 const uint8_t buffer[] = "Hello, world!";
58
Junxiao Shid5827ce2016-07-14 20:49:37 +000059 auto data = make_shared<Data>(Name(baseName).appendSegment(segment));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070060 data->setContent(buffer, sizeof(buffer));
61
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050062 if (isFinal) {
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070063 data->setFinalBlockId(data->getName()[-1]);
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050064 }
65 data = signData(data);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070066
67 return data;
68 }
69
70 void
71 onError(uint32_t errorCode)
72 {
73 ++nErrors;
74 lastError = errorCode;
75 }
76
77 void
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050078 onComplete(const ConstBufferPtr& data)
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070079 {
Junxiao Shid5827ce2016-07-14 20:49:37 +000080 ++nData;
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070081 dataSize = data->size();
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050082 dataString = std::string(reinterpret_cast<const char*>(data->get()));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070083 }
84
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050085 void
86 nackLastInterest(lp::NackReason nackReason)
87 {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080088 const Interest& lastInterest = face.sentInterests.back();
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050089 lp::Nack nack = makeNack(lastInterest, nackReason);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080090 face.receive(nack);
Junxiao Shid5827ce2016-07-14 20:49:37 +000091 advanceClocks(time::milliseconds(10));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050092 }
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070093
94public:
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080095 DummyClientFace face;
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070096
97 uint32_t nErrors;
98 uint32_t lastError;
Junxiao Shid5827ce2016-07-14 20:49:37 +000099 uint32_t nData;
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700100 size_t dataSize;
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500101 std::string dataString;
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700102};
103
104BOOST_FIXTURE_TEST_CASE(Timeout, Fixture)
105{
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500106 ValidatorNull nullValidator;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800107 SegmentFetcher::fetch(face, Interest("/hello/world", time::milliseconds(100)),
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500108 nullValidator,
109 bind(&Fixture::onComplete, this, _1),
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700110 bind(&Fixture::onError, this, _1));
111
Junxiao Shid5827ce2016-07-14 20:49:37 +0000112 advanceClocks(time::milliseconds(1));
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
Junxiao Shid5827ce2016-07-14 20:49:37 +0000120 advanceClocks(time::milliseconds(98));
121 BOOST_CHECK_EQUAL(nErrors, 0);
122 BOOST_CHECK_EQUAL(nData, 0);
123 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800124
Junxiao Shid5827ce2016-07-14 20:49:37 +0000125 advanceClocks(time::milliseconds(1), 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
132
133BOOST_FIXTURE_TEST_CASE(Basic, Fixture)
134{
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500135 ValidatorNull nullValidator;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800136 SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)),
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500137 nullValidator,
138 bind(&Fixture::onComplete, this, _1),
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700139 bind(&Fixture::onError, this, _1));
140
Junxiao Shid5827ce2016-07-14 20:49:37 +0000141 advanceClocks(time::milliseconds(10));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700142
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800143 face.receive(*makeDataSegment("/hello/world/version0", 0, true));
Junxiao Shid5827ce2016-07-14 20:49:37 +0000144 advanceClocks(time::milliseconds(10));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700145
146 BOOST_CHECK_EQUAL(nErrors, 0);
Junxiao Shid5827ce2016-07-14 20:49:37 +0000147 BOOST_CHECK_EQUAL(nData, 1);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700148
149 BOOST_CHECK_EQUAL(dataSize, 14);
150
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500151 const uint8_t buffer[] = "Hello, world!";
152 std::string bufferString = std::string(reinterpret_cast<const char*>(buffer));
153
154 BOOST_CHECK_EQUAL(dataString, bufferString);
155
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800156 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
157 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700158
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800159 const Interest& interest = face.sentInterests[0];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700160 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world");
161 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true);
162 BOOST_CHECK_EQUAL(interest.getChildSelector(), 1);
163}
164
165BOOST_FIXTURE_TEST_CASE(NoSegmentInData, Fixture)
166{
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500167 ValidatorNull nullValidator;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800168 SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)),
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500169 nullValidator,
170 bind(&Fixture::onComplete, this, _1),
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700171 bind(&Fixture::onError, this, _1));
172
Junxiao Shid5827ce2016-07-14 20:49:37 +0000173 advanceClocks(time::milliseconds(10));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700174
175 const uint8_t buffer[] = "Hello, world!";
176
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500177 shared_ptr<Data> data = makeData("/hello/world/version0/no-segment");
178
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700179 data->setContent(buffer, sizeof(buffer));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700180
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800181 face.receive(*data);
Junxiao Shid5827ce2016-07-14 20:49:37 +0000182 advanceClocks(time::milliseconds(10));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700183
184 BOOST_CHECK_EQUAL(nErrors, 1);
185 BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::DATA_HAS_NO_SEGMENT));
Junxiao Shid5827ce2016-07-14 20:49:37 +0000186 BOOST_CHECK_EQUAL(nData, 0);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700187}
188
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700189BOOST_FIXTURE_TEST_CASE(SegmentValidationFailure, Fixture)
190{
Junxiao Shi0f3f0b42016-07-14 13:26:37 +0000191 DummyRejectValidator rejectValidator;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800192 SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)),
Junxiao Shi0f3f0b42016-07-14 13:26:37 +0000193 rejectValidator,
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500194 bind(&Fixture::onComplete, this, _1),
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700195 bind(&Fixture::onError, this, _1));
196
Junxiao Shid5827ce2016-07-14 20:49:37 +0000197 advanceClocks(time::milliseconds(10));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800198 face.receive(*makeDataSegment("/hello/world/version0", 0, true));
Junxiao Shid5827ce2016-07-14 20:49:37 +0000199 advanceClocks(time::milliseconds(10));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700200
201 BOOST_CHECK_EQUAL(nErrors, 1);
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500202 BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::SEGMENT_VALIDATION_FAIL));
Junxiao Shid5827ce2016-07-14 20:49:37 +0000203 BOOST_CHECK_EQUAL(nData, 0);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700204}
205
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700206BOOST_FIXTURE_TEST_CASE(Triple, Fixture)
207{
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500208 ValidatorNull nullValidator;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800209 SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)),
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500210 nullValidator,
211 bind(&Fixture::onComplete, this, _1),
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700212 bind(&Fixture::onError, this, _1));
213
Junxiao Shid5827ce2016-07-14 20:49:37 +0000214 advanceClocks(time::milliseconds(10));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800215 face.receive(*makeDataSegment("/hello/world/version0", 0, false));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700216
Junxiao Shid5827ce2016-07-14 20:49:37 +0000217 advanceClocks(time::milliseconds(10));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800218 face.receive(*makeDataSegment("/hello/world/version0", 1, false));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700219
Junxiao Shid5827ce2016-07-14 20:49:37 +0000220 advanceClocks(time::milliseconds(10));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800221 face.receive(*makeDataSegment("/hello/world/version0", 2, true));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700222
Junxiao Shid5827ce2016-07-14 20:49:37 +0000223 advanceClocks(time::milliseconds(10));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700224
225 BOOST_CHECK_EQUAL(nErrors, 0);
Junxiao Shid5827ce2016-07-14 20:49:37 +0000226 BOOST_CHECK_EQUAL(nData, 1);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700227
228 BOOST_CHECK_EQUAL(dataSize, 42);
229
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800230 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 3);
231 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700232
233 {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800234 const Interest& interest = face.sentInterests[0];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700235 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world");
236 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true);
237 BOOST_CHECK_EQUAL(interest.getChildSelector(), 1);
238 }
239
240 {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800241 const Interest& interest = face.sentInterests[1];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700242 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%01");
243 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false);
244 BOOST_CHECK_EQUAL(interest.getChildSelector(), 0);
245 }
246
247 {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800248 const Interest& interest = face.sentInterests[2];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700249 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%02");
250 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false);
251 BOOST_CHECK_EQUAL(interest.getChildSelector(), 0);
252 }
253}
254
255BOOST_FIXTURE_TEST_CASE(TripleWithInitialSegmentFetching, Fixture)
256{
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500257 ValidatorNull nullValidator;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800258 SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)),
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500259 nullValidator,
260 bind(&Fixture::onComplete, this, _1),
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700261 bind(&Fixture::onError, this, _1));
262
Junxiao Shid5827ce2016-07-14 20:49:37 +0000263 advanceClocks(time::milliseconds(10));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800264 face.receive(*makeDataSegment("/hello/world/version0", 1, false));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700265
Junxiao Shid5827ce2016-07-14 20:49:37 +0000266 advanceClocks(time::milliseconds(10));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800267 face.receive(*makeDataSegment("/hello/world/version0", 0, false));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700268
Junxiao Shid5827ce2016-07-14 20:49:37 +0000269 advanceClocks(time::milliseconds(10));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800270 face.receive(*makeDataSegment("/hello/world/version0", 1, false));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700271
Junxiao Shid5827ce2016-07-14 20:49:37 +0000272 advanceClocks(time::milliseconds(10));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800273 face.receive(*makeDataSegment("/hello/world/version0", 2, true));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700274
Junxiao Shid5827ce2016-07-14 20:49:37 +0000275 advanceClocks(time::milliseconds(10));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700276
277 BOOST_CHECK_EQUAL(nErrors, 0);
Junxiao Shid5827ce2016-07-14 20:49:37 +0000278 BOOST_CHECK_EQUAL(nData, 1);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700279
280 BOOST_CHECK_EQUAL(dataSize, 42);
281
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800282 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 4);
283 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700284
285 {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800286 const Interest& interest = face.sentInterests[0];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700287 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world");
288 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true);
289 BOOST_CHECK_EQUAL(interest.getChildSelector(), 1);
290 }
291
292 {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800293 const Interest& interest = face.sentInterests[1];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700294 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%00");
295 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false);
296 BOOST_CHECK_EQUAL(interest.getChildSelector(), 0);
297 }
298
299 {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800300 const Interest& interest = face.sentInterests[2];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700301 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%01");
302 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false);
303 BOOST_CHECK_EQUAL(interest.getChildSelector(), 0);
304 }
305
306 {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800307 const Interest& interest = face.sentInterests[3];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700308 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%02");
309 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false);
310 BOOST_CHECK_EQUAL(interest.getChildSelector(), 0);
311 }
312}
313
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500314BOOST_FIXTURE_TEST_CASE(MultipleSegmentFetching, Fixture)
315{
316 ValidatorNull nullValidator;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800317 SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)),
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500318 nullValidator,
319 bind(&Fixture::onComplete, this, _1),
320 bind(&Fixture::onError, this, _1));
321
Junxiao Shid5827ce2016-07-14 20:49:37 +0000322 advanceClocks(time::milliseconds(10));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500323
324 for (uint64_t i = 0; i < 400; i++) {
Junxiao Shid5827ce2016-07-14 20:49:37 +0000325 advanceClocks(time::milliseconds(10));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800326 face.receive(*makeDataSegment("/hello/world/version0", i, false));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500327 }
Junxiao Shid5827ce2016-07-14 20:49:37 +0000328 advanceClocks(time::milliseconds(10));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800329 face.receive(*makeDataSegment("/hello/world/version0", 400, true));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500330
Junxiao Shid5827ce2016-07-14 20:49:37 +0000331 advanceClocks(time::milliseconds(10));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500332
333 BOOST_CHECK_EQUAL(nErrors, 0);
Junxiao Shid5827ce2016-07-14 20:49:37 +0000334 BOOST_CHECK_EQUAL(nData, 1);
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500335}
336
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500337BOOST_FIXTURE_TEST_CASE(DuplicateNack, Fixture)
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500338{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800339 SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)),
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500340 make_shared<ValidatorNull>(),
341 bind(&Fixture::onComplete, this, _1),
342 bind(&Fixture::onError, this, _1));
Junxiao Shid5827ce2016-07-14 20:49:37 +0000343 advanceClocks(time::milliseconds(10));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500344
345 // receive nack for the original interest
346 nackLastInterest(lp::NackReason::DUPLICATE);
347
348 // receive nack due to Duplication for the reexpressed interests
349 for (uint32_t i = 1; i <= SegmentFetcher::MAX_INTEREST_REEXPRESS; ++i) {
350 nackLastInterest(lp::NackReason::DUPLICATE);
351 }
352
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800353 BOOST_CHECK_EQUAL(face.sentInterests.size(), (SegmentFetcher::MAX_INTEREST_REEXPRESS + 1));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500354 BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::NACK_ERROR));
355}
356
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500357BOOST_FIXTURE_TEST_CASE(CongestionNack, Fixture)
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500358{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800359 SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)),
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500360 make_shared<ValidatorNull>(),
361 bind(&Fixture::onComplete, this, _1),
362 bind(&Fixture::onError, this, _1));
Junxiao Shid5827ce2016-07-14 20:49:37 +0000363 advanceClocks(time::milliseconds(10));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500364
365 // receive nack for the original interest
366 nackLastInterest(lp::NackReason::CONGESTION);
367
368 // receive nack due to Congestion for the reexpressed interests
369 for (uint32_t i = 1; i <= SegmentFetcher::MAX_INTEREST_REEXPRESS; ++i) {
370 nackLastInterest(lp::NackReason::CONGESTION);
371 }
372
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800373 BOOST_CHECK_EQUAL(face.sentInterests.size(), (SegmentFetcher::MAX_INTEREST_REEXPRESS + 1));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500374 BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::NACK_ERROR));
375}
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700376
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500377BOOST_FIXTURE_TEST_CASE(SegmentZero, Fixture)
378{
379 int nNacks = 2;
380
381 ndn::Name interestName("ndn:/A");
382 SegmentFetcher::fetch(face,
383 Interest(interestName),
384 make_shared<ValidatorNull>(),
385 bind(&Fixture::onComplete, this, _1),
386 bind(&Fixture::onError, this, _1));
387
Junxiao Shid5827ce2016-07-14 20:49:37 +0000388 advanceClocks(time::milliseconds(1000));
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500389
390 for (uint64_t segmentNo = 0; segmentNo <= 3; segmentNo++) {
391 if (segmentNo == 1) {
392 while (nNacks--) {
393 nackLastInterest(lp::NackReason::CONGESTION);
Junxiao Shid5827ce2016-07-14 20:49:37 +0000394 advanceClocks(time::milliseconds(10));
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500395 }
396 }
397
398 auto data = makeDataSegment(interestName, segmentNo, segmentNo == 3);
399 face.receive(*data);
Junxiao Shid5827ce2016-07-14 20:49:37 +0000400 advanceClocks(time::milliseconds(10));
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500401 }
402
403 // Total number of sent interests should be 6: one interest for segment zero and segment one each,
404 // two re-expressed interests for segment one after getting nack twice, and two interests for
405 // segment two and three
406 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 6);
407
408 BOOST_CHECK_EQUAL(face.sentInterests[0].getName(), ndn::Name("ndn:/A"));
409 BOOST_CHECK_EQUAL(face.sentInterests[1].getName(), ndn::Name("ndn:/A/%00%01"));
410 BOOST_CHECK_EQUAL(face.sentInterests[2].getName(), ndn::Name("ndn:/A/%00%01"));
411 BOOST_CHECK_EQUAL(face.sentInterests[3].getName(), ndn::Name("ndn:/A/%00%01"));
412 BOOST_CHECK_EQUAL(face.sentInterests[4].getName(), ndn::Name("ndn:/A/%00%02"));
413 BOOST_CHECK_EQUAL(face.sentInterests[5].getName(), ndn::Name("ndn:/A/%00%03"));
414}
415
416BOOST_FIXTURE_TEST_CASE(ZeroComponentName, Fixture)
417{
418 SegmentFetcher::fetch(face, Interest("ndn:/"),
419 make_shared<ValidatorNull>(),
420 bind(&Fixture::onComplete, this, _1),
421 bind(&Fixture::onError, this, _1));
Junxiao Shid5827ce2016-07-14 20:49:37 +0000422 advanceClocks(time::milliseconds(10));
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500423 nackLastInterest(lp::NackReason::DUPLICATE);
424 face.receive(*makeDataSegment("/hello/world", 0, true));
425
426 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 2);
427 BOOST_CHECK_EQUAL(face.sentInterests[0].getName(), ndn::Name("ndn:/"));
428 BOOST_CHECK_EQUAL(face.sentInterests[1].getName(), ndn::Name("ndn:/"));
Junxiao Shid5827ce2016-07-14 20:49:37 +0000429 BOOST_REQUIRE_EQUAL(nData, 1);
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500430}
431
Junxiao Shid5827ce2016-07-14 20:49:37 +0000432BOOST_AUTO_TEST_SUITE_END() // TestSegmentFetcher
433BOOST_AUTO_TEST_SUITE_END() // Util
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700434
435} // namespace tests
436} // namespace util
437} // namespace ndn