blob: 8fbbb45efd70813ebc7a4b79c230a7e8f0d0d853 [file] [log] [blame]
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -05003 * Copyright (c) 2013-2016 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"
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000024#include "../../dummy-validator.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 Afanasyevf3cfab52014-08-17 22:15:25 -070030#include "security/key-chain.hpp"
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050031#include "lp/nack-header.hpp"
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070032#include "../identity-management-time-fixture.hpp"
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050033#include "../make-interest-data.hpp"
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070034
35namespace ndn {
36namespace util {
37namespace tests {
38
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000039using namespace ndn::tests;
40
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070041BOOST_AUTO_TEST_SUITE(UtilSegmentFetcher)
42
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000043class 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)
49 , nDatas(0)
50 , 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
59 shared_ptr<Data> data = make_shared<Data>(Name(baseName).appendSegment(segment));
60 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 {
80 ++nDatas;
81 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);
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050091 advanceClocks(time::milliseconds(1), 10);
92 }
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;
99 uint32_t nDatas;
100 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
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800112 advanceClocks(time::milliseconds(1), 99);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700113
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800114 BOOST_CHECK_EQUAL(nErrors, 0);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700115 BOOST_CHECK_EQUAL(nDatas, 0);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800116 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
117 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700118
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800119 const Interest& interest = face.sentInterests[0];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700120 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world");
121 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true);
122 BOOST_CHECK_EQUAL(interest.getChildSelector(), 1);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800123
124 advanceClocks(time::milliseconds(1), 2);
125
126 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
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800141 advanceClocks(time::milliseconds(1), 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));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800144 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700145
146 BOOST_CHECK_EQUAL(nErrors, 0);
147 BOOST_CHECK_EQUAL(nDatas, 1);
148
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
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800173 advanceClocks(time::milliseconds(1), 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);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800182 advanceClocks(time::milliseconds(1), 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));
186 BOOST_CHECK_EQUAL(nDatas, 0);
187}
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
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800197 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800198 face.receive(*makeDataSegment("/hello/world/version0", 0, true));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800199 advanceClocks(time::milliseconds(1), 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));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700203 BOOST_CHECK_EQUAL(nDatas, 0);
204}
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
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800214 advanceClocks(time::milliseconds(1), 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
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800217 advanceClocks(time::milliseconds(1), 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
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800220 advanceClocks(time::milliseconds(1), 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
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800223 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700224
225 BOOST_CHECK_EQUAL(nErrors, 0);
226 BOOST_CHECK_EQUAL(nDatas, 1);
227
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
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800263 advanceClocks(time::milliseconds(1), 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
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800266 advanceClocks(time::milliseconds(1), 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
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800269 advanceClocks(time::milliseconds(1), 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
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800272 advanceClocks(time::milliseconds(1), 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
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800275 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700276
277 BOOST_CHECK_EQUAL(nErrors, 0);
278 BOOST_CHECK_EQUAL(nDatas, 1);
279
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
322 advanceClocks(time::milliseconds(1), 10);
323
324 for (uint64_t i = 0; i < 400; i++) {
325 advanceClocks(time::milliseconds(1), 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 }
328 advanceClocks(time::milliseconds(1), 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
331 advanceClocks(time::milliseconds(1), 10);
332
333 BOOST_CHECK_EQUAL(nErrors, 0);
334 BOOST_CHECK_EQUAL(nDatas, 1);
335}
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));
343 advanceClocks(time::milliseconds(1), 10);
344
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));
363 advanceClocks(time::milliseconds(1), 10);
364
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
388 advanceClocks(time::milliseconds(1), 1000);
389
390 for (uint64_t segmentNo = 0; segmentNo <= 3; segmentNo++) {
391 if (segmentNo == 1) {
392 while (nNacks--) {
393 nackLastInterest(lp::NackReason::CONGESTION);
394 advanceClocks(time::milliseconds(1), 10);
395 }
396 }
397
398 auto data = makeDataSegment(interestName, segmentNo, segmentNo == 3);
399 face.receive(*data);
400 advanceClocks(time::milliseconds(1), 10);
401 }
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));
422 advanceClocks(time::milliseconds(1), 10);
423 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:/"));
429 BOOST_REQUIRE_EQUAL(nDatas, 1);
430}
431
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700432BOOST_AUTO_TEST_SUITE_END()
433
434} // namespace tests
435} // namespace util
436} // namespace ndn