blob: ffcc40266562c15339aebf581c0cfa9f5e7d9f19 [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 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
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 Afanasyeve4f8c3b2016-06-23 16:03:48 -070065class Fixture : public ndn::tests::IdentityManagementTimeFixture
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070066{
67public:
68 Fixture()
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070069 : face(io, m_keyChain)
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
119 uint32_t nErrors;
120 uint32_t lastError;
121 uint32_t nDatas;
122 size_t dataSize;
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500123 std::string dataString;
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700124};
125
126BOOST_FIXTURE_TEST_CASE(Timeout, Fixture)
127{
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500128 ValidatorNull nullValidator;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800129 SegmentFetcher::fetch(face, Interest("/hello/world", time::milliseconds(100)),
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500130 nullValidator,
131 bind(&Fixture::onComplete, this, _1),
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700132 bind(&Fixture::onError, this, _1));
133
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800134 advanceClocks(time::milliseconds(1), 99);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700135
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800136 BOOST_CHECK_EQUAL(nErrors, 0);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700137 BOOST_CHECK_EQUAL(nDatas, 0);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800138 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
139 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700140
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800141 const Interest& interest = face.sentInterests[0];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700142 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world");
143 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true);
144 BOOST_CHECK_EQUAL(interest.getChildSelector(), 1);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800145
146 advanceClocks(time::milliseconds(1), 2);
147
148 BOOST_CHECK_EQUAL(nErrors, 1);
149 BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::INTEREST_TIMEOUT));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800150 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
151 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700152}
153
154
155BOOST_FIXTURE_TEST_CASE(Basic, Fixture)
156{
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500157 ValidatorNull nullValidator;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800158 SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)),
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500159 nullValidator,
160 bind(&Fixture::onComplete, this, _1),
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700161 bind(&Fixture::onError, this, _1));
162
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800163 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700164
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800165 face.receive(*makeDataSegment("/hello/world/version0", 0, true));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800166 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700167
168 BOOST_CHECK_EQUAL(nErrors, 0);
169 BOOST_CHECK_EQUAL(nDatas, 1);
170
171 BOOST_CHECK_EQUAL(dataSize, 14);
172
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500173 const uint8_t buffer[] = "Hello, world!";
174 std::string bufferString = std::string(reinterpret_cast<const char*>(buffer));
175
176 BOOST_CHECK_EQUAL(dataString, bufferString);
177
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800178 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
179 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700180
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800181 const Interest& interest = face.sentInterests[0];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700182 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world");
183 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true);
184 BOOST_CHECK_EQUAL(interest.getChildSelector(), 1);
185}
186
187BOOST_FIXTURE_TEST_CASE(NoSegmentInData, Fixture)
188{
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500189 ValidatorNull nullValidator;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800190 SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)),
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500191 nullValidator,
192 bind(&Fixture::onComplete, this, _1),
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700193 bind(&Fixture::onError, this, _1));
194
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800195 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700196
197 const uint8_t buffer[] = "Hello, world!";
198
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500199 shared_ptr<Data> data = makeData("/hello/world/version0/no-segment");
200
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700201 data->setContent(buffer, sizeof(buffer));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700202
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800203 face.receive(*data);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800204 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700205
206 BOOST_CHECK_EQUAL(nErrors, 1);
207 BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::DATA_HAS_NO_SEGMENT));
208 BOOST_CHECK_EQUAL(nDatas, 0);
209}
210
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700211BOOST_FIXTURE_TEST_CASE(SegmentValidationFailure, Fixture)
212{
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500213 ValidatorFailed failedValidator;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800214 SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)),
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500215 failedValidator,
216 bind(&Fixture::onComplete, this, _1),
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700217 bind(&Fixture::onError, this, _1));
218
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800219 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800220 face.receive(*makeDataSegment("/hello/world/version0", 0, true));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800221 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700222
223 BOOST_CHECK_EQUAL(nErrors, 1);
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500224 BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::SEGMENT_VALIDATION_FAIL));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700225 BOOST_CHECK_EQUAL(nDatas, 0);
226}
227
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700228BOOST_FIXTURE_TEST_CASE(Triple, Fixture)
229{
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500230 ValidatorNull nullValidator;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800231 SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)),
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500232 nullValidator,
233 bind(&Fixture::onComplete, this, _1),
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700234 bind(&Fixture::onError, this, _1));
235
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800236 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800237 face.receive(*makeDataSegment("/hello/world/version0", 0, false));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700238
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800239 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800240 face.receive(*makeDataSegment("/hello/world/version0", 1, false));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700241
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800242 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800243 face.receive(*makeDataSegment("/hello/world/version0", 2, true));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700244
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800245 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700246
247 BOOST_CHECK_EQUAL(nErrors, 0);
248 BOOST_CHECK_EQUAL(nDatas, 1);
249
250 BOOST_CHECK_EQUAL(dataSize, 42);
251
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800252 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 3);
253 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700254
255 {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800256 const Interest& interest = face.sentInterests[0];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700257 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world");
258 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true);
259 BOOST_CHECK_EQUAL(interest.getChildSelector(), 1);
260 }
261
262 {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800263 const Interest& interest = face.sentInterests[1];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700264 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%01");
265 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false);
266 BOOST_CHECK_EQUAL(interest.getChildSelector(), 0);
267 }
268
269 {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800270 const Interest& interest = face.sentInterests[2];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700271 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%02");
272 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false);
273 BOOST_CHECK_EQUAL(interest.getChildSelector(), 0);
274 }
275}
276
277BOOST_FIXTURE_TEST_CASE(TripleWithInitialSegmentFetching, Fixture)
278{
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500279 ValidatorNull nullValidator;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800280 SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)),
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500281 nullValidator,
282 bind(&Fixture::onComplete, this, _1),
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700283 bind(&Fixture::onError, this, _1));
284
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800285 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800286 face.receive(*makeDataSegment("/hello/world/version0", 1, false));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700287
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800288 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800289 face.receive(*makeDataSegment("/hello/world/version0", 0, false));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700290
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800291 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800292 face.receive(*makeDataSegment("/hello/world/version0", 1, false));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700293
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800294 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800295 face.receive(*makeDataSegment("/hello/world/version0", 2, true));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700296
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800297 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700298
299 BOOST_CHECK_EQUAL(nErrors, 0);
300 BOOST_CHECK_EQUAL(nDatas, 1);
301
302 BOOST_CHECK_EQUAL(dataSize, 42);
303
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800304 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 4);
305 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700306
307 {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800308 const Interest& interest = face.sentInterests[0];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700309 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world");
310 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true);
311 BOOST_CHECK_EQUAL(interest.getChildSelector(), 1);
312 }
313
314 {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800315 const Interest& interest = face.sentInterests[1];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700316 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%00");
317 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false);
318 BOOST_CHECK_EQUAL(interest.getChildSelector(), 0);
319 }
320
321 {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800322 const Interest& interest = face.sentInterests[2];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700323 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%01");
324 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false);
325 BOOST_CHECK_EQUAL(interest.getChildSelector(), 0);
326 }
327
328 {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800329 const Interest& interest = face.sentInterests[3];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700330 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%02");
331 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false);
332 BOOST_CHECK_EQUAL(interest.getChildSelector(), 0);
333 }
334}
335
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500336BOOST_FIXTURE_TEST_CASE(MultipleSegmentFetching, Fixture)
337{
338 ValidatorNull nullValidator;
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 nullValidator,
341 bind(&Fixture::onComplete, this, _1),
342 bind(&Fixture::onError, this, _1));
343
344 advanceClocks(time::milliseconds(1), 10);
345
346 for (uint64_t i = 0; i < 400; i++) {
347 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800348 face.receive(*makeDataSegment("/hello/world/version0", i, false));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500349 }
350 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800351 face.receive(*makeDataSegment("/hello/world/version0", 400, true));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500352
353 advanceClocks(time::milliseconds(1), 10);
354
355 BOOST_CHECK_EQUAL(nErrors, 0);
356 BOOST_CHECK_EQUAL(nDatas, 1);
357}
358
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500359BOOST_FIXTURE_TEST_CASE(DuplicateNack, Fixture)
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500360{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800361 SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)),
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500362 make_shared<ValidatorNull>(),
363 bind(&Fixture::onComplete, this, _1),
364 bind(&Fixture::onError, this, _1));
365 advanceClocks(time::milliseconds(1), 10);
366
367 // receive nack for the original interest
368 nackLastInterest(lp::NackReason::DUPLICATE);
369
370 // receive nack due to Duplication for the reexpressed interests
371 for (uint32_t i = 1; i <= SegmentFetcher::MAX_INTEREST_REEXPRESS; ++i) {
372 nackLastInterest(lp::NackReason::DUPLICATE);
373 }
374
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800375 BOOST_CHECK_EQUAL(face.sentInterests.size(), (SegmentFetcher::MAX_INTEREST_REEXPRESS + 1));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500376 BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::NACK_ERROR));
377}
378
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500379BOOST_FIXTURE_TEST_CASE(CongestionNack, Fixture)
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500380{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800381 SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)),
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500382 make_shared<ValidatorNull>(),
383 bind(&Fixture::onComplete, this, _1),
384 bind(&Fixture::onError, this, _1));
385 advanceClocks(time::milliseconds(1), 10);
386
387 // receive nack for the original interest
388 nackLastInterest(lp::NackReason::CONGESTION);
389
390 // receive nack due to Congestion for the reexpressed interests
391 for (uint32_t i = 1; i <= SegmentFetcher::MAX_INTEREST_REEXPRESS; ++i) {
392 nackLastInterest(lp::NackReason::CONGESTION);
393 }
394
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800395 BOOST_CHECK_EQUAL(face.sentInterests.size(), (SegmentFetcher::MAX_INTEREST_REEXPRESS + 1));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500396 BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::NACK_ERROR));
397}
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700398
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500399BOOST_FIXTURE_TEST_CASE(SegmentZero, Fixture)
400{
401 int nNacks = 2;
402
403 ndn::Name interestName("ndn:/A");
404 SegmentFetcher::fetch(face,
405 Interest(interestName),
406 make_shared<ValidatorNull>(),
407 bind(&Fixture::onComplete, this, _1),
408 bind(&Fixture::onError, this, _1));
409
410 advanceClocks(time::milliseconds(1), 1000);
411
412 for (uint64_t segmentNo = 0; segmentNo <= 3; segmentNo++) {
413 if (segmentNo == 1) {
414 while (nNacks--) {
415 nackLastInterest(lp::NackReason::CONGESTION);
416 advanceClocks(time::milliseconds(1), 10);
417 }
418 }
419
420 auto data = makeDataSegment(interestName, segmentNo, segmentNo == 3);
421 face.receive(*data);
422 advanceClocks(time::milliseconds(1), 10);
423 }
424
425 // Total number of sent interests should be 6: one interest for segment zero and segment one each,
426 // two re-expressed interests for segment one after getting nack twice, and two interests for
427 // segment two and three
428 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 6);
429
430 BOOST_CHECK_EQUAL(face.sentInterests[0].getName(), ndn::Name("ndn:/A"));
431 BOOST_CHECK_EQUAL(face.sentInterests[1].getName(), ndn::Name("ndn:/A/%00%01"));
432 BOOST_CHECK_EQUAL(face.sentInterests[2].getName(), ndn::Name("ndn:/A/%00%01"));
433 BOOST_CHECK_EQUAL(face.sentInterests[3].getName(), ndn::Name("ndn:/A/%00%01"));
434 BOOST_CHECK_EQUAL(face.sentInterests[4].getName(), ndn::Name("ndn:/A/%00%02"));
435 BOOST_CHECK_EQUAL(face.sentInterests[5].getName(), ndn::Name("ndn:/A/%00%03"));
436}
437
438BOOST_FIXTURE_TEST_CASE(ZeroComponentName, Fixture)
439{
440 SegmentFetcher::fetch(face, Interest("ndn:/"),
441 make_shared<ValidatorNull>(),
442 bind(&Fixture::onComplete, this, _1),
443 bind(&Fixture::onError, this, _1));
444 advanceClocks(time::milliseconds(1), 10);
445 nackLastInterest(lp::NackReason::DUPLICATE);
446 face.receive(*makeDataSegment("/hello/world", 0, true));
447
448 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 2);
449 BOOST_CHECK_EQUAL(face.sentInterests[0].getName(), ndn::Name("ndn:/"));
450 BOOST_CHECK_EQUAL(face.sentInterests[1].getName(), ndn::Name("ndn:/"));
451 BOOST_REQUIRE_EQUAL(nDatas, 1);
452}
453
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700454BOOST_AUTO_TEST_SUITE_END()
455
456} // namespace tests
457} // namespace util
458} // namespace ndn