blob: 73624393c7094a48a7d9a9136750af30c81528e2 [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"
24#include "security/validator.hpp"
25#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 Afanasyevd3a55b22014-11-18 19:23:28 -080032#include "../unit-test-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
39BOOST_AUTO_TEST_SUITE(UtilSegmentFetcher)
40
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050041class ValidatorFailed : public Validator
42{
43protected:
44 virtual void
45 checkPolicy(const Data& data,
46 int nSteps,
47 const OnDataValidated& onValidated,
48 const OnDataValidationFailed& onValidationFailed,
49 std::vector<shared_ptr<ValidationRequest>>& nextSteps)
50 {
51 onValidationFailed(data.shared_from_this(), "Data validation failed.");
52 }
53
54 virtual void
55 checkPolicy(const Interest& interest,
56 int nSteps,
57 const OnInterestValidated& onValidated,
58 const OnInterestValidationFailed& onValidationFailed,
59 std::vector<shared_ptr<ValidationRequest>>& nextSteps)
60 {
61 onValidationFailed(interest.shared_from_this(), "Interest validation failed.");
62 }
63};
64
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080065class Fixture : public ndn::tests::UnitTestTimeFixture
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070066{
67public:
68 Fixture()
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080069 : face(io)
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070070 , nErrors(0)
71 , nDatas(0)
72 , dataSize(0)
73 {
74 }
75
76 shared_ptr<Data>
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050077 makeDataSegment(const Name& baseName, uint64_t segment, bool isFinal)
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070078 {
79 const uint8_t buffer[] = "Hello, world!";
80
81 shared_ptr<Data> data = make_shared<Data>(Name(baseName).appendSegment(segment));
82 data->setContent(buffer, sizeof(buffer));
83
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050084 if (isFinal) {
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070085 data->setFinalBlockId(data->getName()[-1]);
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050086 }
87 data = signData(data);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070088
89 return data;
90 }
91
92 void
93 onError(uint32_t errorCode)
94 {
95 ++nErrors;
96 lastError = errorCode;
97 }
98
99 void
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500100 onComplete(const ConstBufferPtr& data)
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700101 {
102 ++nDatas;
103 dataSize = data->size();
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500104 dataString = std::string(reinterpret_cast<const char*>(data->get()));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700105 }
106
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500107 void
108 nackLastInterest(lp::NackReason nackReason)
109 {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800110 const Interest& lastInterest = face.sentInterests.back();
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500111 lp::Nack nack = makeNack(lastInterest, nackReason);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800112 face.receive(nack);
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500113 advanceClocks(time::milliseconds(1), 10);
114 }
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700115
116public:
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800117 DummyClientFace face;
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700118 KeyChain keyChain;
119
120 uint32_t nErrors;
121 uint32_t lastError;
122 uint32_t nDatas;
123 size_t dataSize;
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500124 std::string dataString;
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700125};
126
127BOOST_FIXTURE_TEST_CASE(Timeout, Fixture)
128{
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500129 ValidatorNull nullValidator;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800130 SegmentFetcher::fetch(face, Interest("/hello/world", time::milliseconds(100)),
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500131 nullValidator,
132 bind(&Fixture::onComplete, this, _1),
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700133 bind(&Fixture::onError, this, _1));
134
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800135 advanceClocks(time::milliseconds(1), 99);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700136
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800137 BOOST_CHECK_EQUAL(nErrors, 0);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700138 BOOST_CHECK_EQUAL(nDatas, 0);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800139 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
140 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700141
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800142 const Interest& interest = face.sentInterests[0];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700143 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world");
144 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true);
145 BOOST_CHECK_EQUAL(interest.getChildSelector(), 1);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800146
147 advanceClocks(time::milliseconds(1), 2);
148
149 BOOST_CHECK_EQUAL(nErrors, 1);
150 BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::INTEREST_TIMEOUT));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800151 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
152 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700153}
154
155
156BOOST_FIXTURE_TEST_CASE(Basic, Fixture)
157{
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500158 ValidatorNull nullValidator;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800159 SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)),
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500160 nullValidator,
161 bind(&Fixture::onComplete, this, _1),
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700162 bind(&Fixture::onError, this, _1));
163
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800164 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700165
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800166 face.receive(*makeDataSegment("/hello/world/version0", 0, true));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800167 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700168
169 BOOST_CHECK_EQUAL(nErrors, 0);
170 BOOST_CHECK_EQUAL(nDatas, 1);
171
172 BOOST_CHECK_EQUAL(dataSize, 14);
173
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500174 const uint8_t buffer[] = "Hello, world!";
175 std::string bufferString = std::string(reinterpret_cast<const char*>(buffer));
176
177 BOOST_CHECK_EQUAL(dataString, bufferString);
178
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800179 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
180 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700181
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800182 const Interest& interest = face.sentInterests[0];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700183 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world");
184 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true);
185 BOOST_CHECK_EQUAL(interest.getChildSelector(), 1);
186}
187
188BOOST_FIXTURE_TEST_CASE(NoSegmentInData, Fixture)
189{
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500190 ValidatorNull nullValidator;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800191 SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)),
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500192 nullValidator,
193 bind(&Fixture::onComplete, this, _1),
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700194 bind(&Fixture::onError, this, _1));
195
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800196 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700197
198 const uint8_t buffer[] = "Hello, world!";
199
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500200 shared_ptr<Data> data = makeData("/hello/world/version0/no-segment");
201
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700202 data->setContent(buffer, sizeof(buffer));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700203
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800204 face.receive(*data);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800205 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700206
207 BOOST_CHECK_EQUAL(nErrors, 1);
208 BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::DATA_HAS_NO_SEGMENT));
209 BOOST_CHECK_EQUAL(nDatas, 0);
210}
211
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700212BOOST_FIXTURE_TEST_CASE(SegmentValidationFailure, Fixture)
213{
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500214 ValidatorFailed failedValidator;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800215 SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)),
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500216 failedValidator,
217 bind(&Fixture::onComplete, this, _1),
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700218 bind(&Fixture::onError, this, _1));
219
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", 0, true));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800222 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700223
224 BOOST_CHECK_EQUAL(nErrors, 1);
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500225 BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::SEGMENT_VALIDATION_FAIL));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700226 BOOST_CHECK_EQUAL(nDatas, 0);
227}
228
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700229BOOST_FIXTURE_TEST_CASE(Triple, Fixture)
230{
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500231 ValidatorNull nullValidator;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800232 SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)),
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500233 nullValidator,
234 bind(&Fixture::onComplete, this, _1),
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700235 bind(&Fixture::onError, this, _1));
236
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800237 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800238 face.receive(*makeDataSegment("/hello/world/version0", 0, false));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700239
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800240 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800241 face.receive(*makeDataSegment("/hello/world/version0", 1, false));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700242
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800243 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800244 face.receive(*makeDataSegment("/hello/world/version0", 2, true));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700245
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800246 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700247
248 BOOST_CHECK_EQUAL(nErrors, 0);
249 BOOST_CHECK_EQUAL(nDatas, 1);
250
251 BOOST_CHECK_EQUAL(dataSize, 42);
252
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800253 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 3);
254 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700255
256 {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800257 const Interest& interest = face.sentInterests[0];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700258 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world");
259 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true);
260 BOOST_CHECK_EQUAL(interest.getChildSelector(), 1);
261 }
262
263 {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800264 const Interest& interest = face.sentInterests[1];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700265 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%01");
266 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false);
267 BOOST_CHECK_EQUAL(interest.getChildSelector(), 0);
268 }
269
270 {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800271 const Interest& interest = face.sentInterests[2];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700272 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%02");
273 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false);
274 BOOST_CHECK_EQUAL(interest.getChildSelector(), 0);
275 }
276}
277
278BOOST_FIXTURE_TEST_CASE(TripleWithInitialSegmentFetching, Fixture)
279{
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500280 ValidatorNull nullValidator;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800281 SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)),
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500282 nullValidator,
283 bind(&Fixture::onComplete, this, _1),
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700284 bind(&Fixture::onError, this, _1));
285
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800286 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800287 face.receive(*makeDataSegment("/hello/world/version0", 1, false));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700288
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800289 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800290 face.receive(*makeDataSegment("/hello/world/version0", 0, false));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700291
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800292 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800293 face.receive(*makeDataSegment("/hello/world/version0", 1, false));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700294
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800295 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800296 face.receive(*makeDataSegment("/hello/world/version0", 2, true));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700297
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800298 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700299
300 BOOST_CHECK_EQUAL(nErrors, 0);
301 BOOST_CHECK_EQUAL(nDatas, 1);
302
303 BOOST_CHECK_EQUAL(dataSize, 42);
304
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800305 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 4);
306 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700307
308 {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800309 const Interest& interest = face.sentInterests[0];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700310 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world");
311 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true);
312 BOOST_CHECK_EQUAL(interest.getChildSelector(), 1);
313 }
314
315 {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800316 const Interest& interest = face.sentInterests[1];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700317 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%00");
318 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false);
319 BOOST_CHECK_EQUAL(interest.getChildSelector(), 0);
320 }
321
322 {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800323 const Interest& interest = face.sentInterests[2];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700324 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%01");
325 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false);
326 BOOST_CHECK_EQUAL(interest.getChildSelector(), 0);
327 }
328
329 {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800330 const Interest& interest = face.sentInterests[3];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700331 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%02");
332 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false);
333 BOOST_CHECK_EQUAL(interest.getChildSelector(), 0);
334 }
335}
336
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500337BOOST_FIXTURE_TEST_CASE(MultipleSegmentFetching, Fixture)
338{
339 ValidatorNull nullValidator;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800340 SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)),
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500341 nullValidator,
342 bind(&Fixture::onComplete, this, _1),
343 bind(&Fixture::onError, this, _1));
344
345 advanceClocks(time::milliseconds(1), 10);
346
347 for (uint64_t i = 0; i < 400; i++) {
348 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800349 face.receive(*makeDataSegment("/hello/world/version0", i, false));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500350 }
351 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800352 face.receive(*makeDataSegment("/hello/world/version0", 400, true));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500353
354 advanceClocks(time::milliseconds(1), 10);
355
356 BOOST_CHECK_EQUAL(nErrors, 0);
357 BOOST_CHECK_EQUAL(nDatas, 1);
358}
359
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500360BOOST_FIXTURE_TEST_CASE(DuplicateNack, Fixture)
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500361{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800362 SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)),
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500363 make_shared<ValidatorNull>(),
364 bind(&Fixture::onComplete, this, _1),
365 bind(&Fixture::onError, this, _1));
366 advanceClocks(time::milliseconds(1), 10);
367
368 // receive nack for the original interest
369 nackLastInterest(lp::NackReason::DUPLICATE);
370
371 // receive nack due to Duplication for the reexpressed interests
372 for (uint32_t i = 1; i <= SegmentFetcher::MAX_INTEREST_REEXPRESS; ++i) {
373 nackLastInterest(lp::NackReason::DUPLICATE);
374 }
375
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800376 BOOST_CHECK_EQUAL(face.sentInterests.size(), (SegmentFetcher::MAX_INTEREST_REEXPRESS + 1));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500377 BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::NACK_ERROR));
378}
379
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500380BOOST_FIXTURE_TEST_CASE(CongestionNack, Fixture)
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500381{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800382 SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)),
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500383 make_shared<ValidatorNull>(),
384 bind(&Fixture::onComplete, this, _1),
385 bind(&Fixture::onError, this, _1));
386 advanceClocks(time::milliseconds(1), 10);
387
388 // receive nack for the original interest
389 nackLastInterest(lp::NackReason::CONGESTION);
390
391 // receive nack due to Congestion for the reexpressed interests
392 for (uint32_t i = 1; i <= SegmentFetcher::MAX_INTEREST_REEXPRESS; ++i) {
393 nackLastInterest(lp::NackReason::CONGESTION);
394 }
395
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800396 BOOST_CHECK_EQUAL(face.sentInterests.size(), (SegmentFetcher::MAX_INTEREST_REEXPRESS + 1));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500397 BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::NACK_ERROR));
398}
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700399
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500400BOOST_FIXTURE_TEST_CASE(SegmentZero, Fixture)
401{
402 int nNacks = 2;
403
404 ndn::Name interestName("ndn:/A");
405 SegmentFetcher::fetch(face,
406 Interest(interestName),
407 make_shared<ValidatorNull>(),
408 bind(&Fixture::onComplete, this, _1),
409 bind(&Fixture::onError, this, _1));
410
411 advanceClocks(time::milliseconds(1), 1000);
412
413 for (uint64_t segmentNo = 0; segmentNo <= 3; segmentNo++) {
414 if (segmentNo == 1) {
415 while (nNacks--) {
416 nackLastInterest(lp::NackReason::CONGESTION);
417 advanceClocks(time::milliseconds(1), 10);
418 }
419 }
420
421 auto data = makeDataSegment(interestName, segmentNo, segmentNo == 3);
422 face.receive(*data);
423 advanceClocks(time::milliseconds(1), 10);
424 }
425
426 // Total number of sent interests should be 6: one interest for segment zero and segment one each,
427 // two re-expressed interests for segment one after getting nack twice, and two interests for
428 // segment two and three
429 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 6);
430
431 BOOST_CHECK_EQUAL(face.sentInterests[0].getName(), ndn::Name("ndn:/A"));
432 BOOST_CHECK_EQUAL(face.sentInterests[1].getName(), ndn::Name("ndn:/A/%00%01"));
433 BOOST_CHECK_EQUAL(face.sentInterests[2].getName(), ndn::Name("ndn:/A/%00%01"));
434 BOOST_CHECK_EQUAL(face.sentInterests[3].getName(), ndn::Name("ndn:/A/%00%01"));
435 BOOST_CHECK_EQUAL(face.sentInterests[4].getName(), ndn::Name("ndn:/A/%00%02"));
436 BOOST_CHECK_EQUAL(face.sentInterests[5].getName(), ndn::Name("ndn:/A/%00%03"));
437}
438
439BOOST_FIXTURE_TEST_CASE(ZeroComponentName, Fixture)
440{
441 SegmentFetcher::fetch(face, Interest("ndn:/"),
442 make_shared<ValidatorNull>(),
443 bind(&Fixture::onComplete, this, _1),
444 bind(&Fixture::onError, this, _1));
445 advanceClocks(time::milliseconds(1), 10);
446 nackLastInterest(lp::NackReason::DUPLICATE);
447 face.receive(*makeDataSegment("/hello/world", 0, true));
448
449 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 2);
450 BOOST_CHECK_EQUAL(face.sentInterests[0].getName(), ndn::Name("ndn:/"));
451 BOOST_CHECK_EQUAL(face.sentInterests[1].getName(), ndn::Name("ndn:/"));
452 BOOST_REQUIRE_EQUAL(nDatas, 1);
453}
454
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700455BOOST_AUTO_TEST_SUITE_END()
456
457} // namespace tests
458} // namespace util
459} // namespace ndn