blob: dfc2cd8de132d735e686460a728a7d0545d40a13 [file] [log] [blame]
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -08002/*
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"
Davide Pesavento5d0b0102017-10-07 13:43:16 -040023
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050024#include "data.hpp"
Davide Pesavento5d0b0102017-10-07 13:43:16 -040025#include "lp/nack.hpp"
Junxiao Shi2bea5c42017-08-14 20:10:32 +000026#include "util/dummy-client-face.hpp"
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070027
28#include "boost-test.hpp"
Junxiao Shi2bea5c42017-08-14 20:10:32 +000029#include "dummy-validator.hpp"
30#include "make-interest-data.hpp"
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070031#include "../identity-management-time-fixture.hpp"
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070032
33namespace ndn {
34namespace util {
35namespace tests {
36
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000037using namespace ndn::tests;
38
Junxiao Shid5827ce2016-07-14 20:49:37 +000039BOOST_AUTO_TEST_SUITE(Util)
40BOOST_AUTO_TEST_SUITE(TestSegmentFetcher)
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070041
Alexander Afanasyev80782e02017-01-04 13:16:54 -080042class Fixture : public IdentityManagementTimeFixture
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070043{
44public:
45 Fixture()
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070046 : face(io, m_keyChain)
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070047 {
48 }
49
Davide Pesavento5d0b0102017-10-07 13:43:16 -040050 static shared_ptr<Data>
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050051 makeDataSegment(const Name& baseName, uint64_t segment, bool isFinal)
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070052 {
53 const uint8_t buffer[] = "Hello, world!";
54
Junxiao Shid5827ce2016-07-14 20:49:37 +000055 auto data = make_shared<Data>(Name(baseName).appendSegment(segment));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070056 data->setContent(buffer, sizeof(buffer));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050057 if (isFinal) {
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070058 data->setFinalBlockId(data->getName()[-1]);
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050059 }
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070060
Davide Pesavento5d0b0102017-10-07 13:43:16 -040061 return signData(data);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070062 }
63
64 void
65 onError(uint32_t errorCode)
66 {
67 ++nErrors;
68 lastError = errorCode;
69 }
70
71 void
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050072 onComplete(const ConstBufferPtr& data)
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070073 {
Junxiao Shid5827ce2016-07-14 20:49:37 +000074 ++nData;
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070075 dataSize = data->size();
Davide Pesavento5d0b0102017-10-07 13:43:16 -040076 dataString = std::string(data->get<char>());
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070077 }
78
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050079 void
80 nackLastInterest(lp::NackReason nackReason)
81 {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080082 const Interest& lastInterest = face.sentInterests.back();
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050083 lp::Nack nack = makeNack(lastInterest, nackReason);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080084 face.receive(nack);
Junxiao Shid5827ce2016-07-14 20:49:37 +000085 advanceClocks(time::milliseconds(10));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050086 }
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070087
88public:
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080089 DummyClientFace face;
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070090
Davide Pesavento5d0b0102017-10-07 13:43:16 -040091 uint32_t nErrors = 0;
92 uint32_t lastError = 0;
93 uint32_t nData = 0;
94 size_t dataSize = 0;
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050095 std::string dataString;
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070096};
97
98BOOST_FIXTURE_TEST_CASE(Timeout, Fixture)
99{
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -0800100 DummyValidator acceptValidator;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800101 SegmentFetcher::fetch(face, Interest("/hello/world", time::milliseconds(100)),
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -0800102 acceptValidator,
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500103 bind(&Fixture::onComplete, this, _1),
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700104 bind(&Fixture::onError, this, _1));
105
Junxiao Shid5827ce2016-07-14 20:49:37 +0000106 advanceClocks(time::milliseconds(1));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800107 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700108
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800109 const Interest& interest = face.sentInterests[0];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700110 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world");
111 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true);
112 BOOST_CHECK_EQUAL(interest.getChildSelector(), 1);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800113
Junxiao Shid5827ce2016-07-14 20:49:37 +0000114 advanceClocks(time::milliseconds(98));
115 BOOST_CHECK_EQUAL(nErrors, 0);
116 BOOST_CHECK_EQUAL(nData, 0);
117 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800118
Junxiao Shid5827ce2016-07-14 20:49:37 +0000119 advanceClocks(time::milliseconds(1), 2);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800120 BOOST_CHECK_EQUAL(nErrors, 1);
121 BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::INTEREST_TIMEOUT));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800122 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
123 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700124}
125
126
127BOOST_FIXTURE_TEST_CASE(Basic, Fixture)
128{
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -0800129 DummyValidator acceptValidator;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800130 SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)),
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -0800131 acceptValidator,
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500132 bind(&Fixture::onComplete, this, _1),
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700133 bind(&Fixture::onError, this, _1));
134
Junxiao Shid5827ce2016-07-14 20:49:37 +0000135 advanceClocks(time::milliseconds(10));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700136
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800137 face.receive(*makeDataSegment("/hello/world/version0", 0, true));
Junxiao Shid5827ce2016-07-14 20:49:37 +0000138 advanceClocks(time::milliseconds(10));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700139
140 BOOST_CHECK_EQUAL(nErrors, 0);
Junxiao Shid5827ce2016-07-14 20:49:37 +0000141 BOOST_CHECK_EQUAL(nData, 1);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700142
143 BOOST_CHECK_EQUAL(dataSize, 14);
144
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500145 const uint8_t buffer[] = "Hello, world!";
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400146 std::string bufferString(reinterpret_cast<const char*>(buffer));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500147
148 BOOST_CHECK_EQUAL(dataString, bufferString);
149
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
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800153 const Interest& interest = face.sentInterests[0];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700154 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world");
155 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true);
156 BOOST_CHECK_EQUAL(interest.getChildSelector(), 1);
157}
158
159BOOST_FIXTURE_TEST_CASE(NoSegmentInData, Fixture)
160{
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -0800161 DummyValidator acceptValidator;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800162 SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)),
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -0800163 acceptValidator,
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500164 bind(&Fixture::onComplete, this, _1),
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700165 bind(&Fixture::onError, this, _1));
166
Junxiao Shid5827ce2016-07-14 20:49:37 +0000167 advanceClocks(time::milliseconds(10));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700168
169 const uint8_t buffer[] = "Hello, world!";
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400170 auto data = makeData("/hello/world/version0/no-segment");
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700171 data->setContent(buffer, sizeof(buffer));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700172
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800173 face.receive(*data);
Junxiao Shid5827ce2016-07-14 20:49:37 +0000174 advanceClocks(time::milliseconds(10));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700175
176 BOOST_CHECK_EQUAL(nErrors, 1);
177 BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::DATA_HAS_NO_SEGMENT));
Junxiao Shid5827ce2016-07-14 20:49:37 +0000178 BOOST_CHECK_EQUAL(nData, 0);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700179}
180
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700181BOOST_FIXTURE_TEST_CASE(SegmentValidationFailure, Fixture)
182{
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -0800183 DummyValidator rejectValidator(false);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800184 SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)),
Junxiao Shi0f3f0b42016-07-14 13:26:37 +0000185 rejectValidator,
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500186 bind(&Fixture::onComplete, this, _1),
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700187 bind(&Fixture::onError, this, _1));
188
Junxiao Shid5827ce2016-07-14 20:49:37 +0000189 advanceClocks(time::milliseconds(10));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800190 face.receive(*makeDataSegment("/hello/world/version0", 0, true));
Junxiao Shid5827ce2016-07-14 20:49:37 +0000191 advanceClocks(time::milliseconds(10));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700192
193 BOOST_CHECK_EQUAL(nErrors, 1);
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500194 BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::SEGMENT_VALIDATION_FAIL));
Junxiao Shid5827ce2016-07-14 20:49:37 +0000195 BOOST_CHECK_EQUAL(nData, 0);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700196}
197
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700198BOOST_FIXTURE_TEST_CASE(Triple, Fixture)
199{
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -0800200 DummyValidator acceptValidator;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800201 SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)),
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -0800202 acceptValidator,
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500203 bind(&Fixture::onComplete, this, _1),
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700204 bind(&Fixture::onError, this, _1));
205
Junxiao Shid5827ce2016-07-14 20:49:37 +0000206 advanceClocks(time::milliseconds(10));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800207 face.receive(*makeDataSegment("/hello/world/version0", 0, false));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700208
Junxiao Shid5827ce2016-07-14 20:49:37 +0000209 advanceClocks(time::milliseconds(10));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800210 face.receive(*makeDataSegment("/hello/world/version0", 1, false));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700211
Junxiao Shid5827ce2016-07-14 20:49:37 +0000212 advanceClocks(time::milliseconds(10));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800213 face.receive(*makeDataSegment("/hello/world/version0", 2, true));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700214
Junxiao Shid5827ce2016-07-14 20:49:37 +0000215 advanceClocks(time::milliseconds(10));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700216
217 BOOST_CHECK_EQUAL(nErrors, 0);
Junxiao Shid5827ce2016-07-14 20:49:37 +0000218 BOOST_CHECK_EQUAL(nData, 1);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700219
220 BOOST_CHECK_EQUAL(dataSize, 42);
221
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800222 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 3);
223 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700224
225 {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800226 const Interest& interest = face.sentInterests[0];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700227 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world");
228 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true);
229 BOOST_CHECK_EQUAL(interest.getChildSelector(), 1);
230 }
231
232 {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800233 const Interest& interest = face.sentInterests[1];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700234 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%01");
235 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false);
236 BOOST_CHECK_EQUAL(interest.getChildSelector(), 0);
237 }
238
239 {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800240 const Interest& interest = face.sentInterests[2];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700241 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%02");
242 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false);
243 BOOST_CHECK_EQUAL(interest.getChildSelector(), 0);
244 }
245}
246
247BOOST_FIXTURE_TEST_CASE(TripleWithInitialSegmentFetching, Fixture)
248{
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -0800249 DummyValidator acceptValidator;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800250 SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)),
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -0800251 acceptValidator,
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500252 bind(&Fixture::onComplete, this, _1),
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700253 bind(&Fixture::onError, this, _1));
254
Junxiao Shid5827ce2016-07-14 20:49:37 +0000255 advanceClocks(time::milliseconds(10));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800256 face.receive(*makeDataSegment("/hello/world/version0", 1, false));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700257
Junxiao Shid5827ce2016-07-14 20:49:37 +0000258 advanceClocks(time::milliseconds(10));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800259 face.receive(*makeDataSegment("/hello/world/version0", 0, false));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700260
Junxiao Shid5827ce2016-07-14 20:49:37 +0000261 advanceClocks(time::milliseconds(10));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800262 face.receive(*makeDataSegment("/hello/world/version0", 1, false));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700263
Junxiao Shid5827ce2016-07-14 20:49:37 +0000264 advanceClocks(time::milliseconds(10));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800265 face.receive(*makeDataSegment("/hello/world/version0", 2, true));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700266
Junxiao Shid5827ce2016-07-14 20:49:37 +0000267 advanceClocks(time::milliseconds(10));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700268
269 BOOST_CHECK_EQUAL(nErrors, 0);
Junxiao Shid5827ce2016-07-14 20:49:37 +0000270 BOOST_CHECK_EQUAL(nData, 1);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700271
272 BOOST_CHECK_EQUAL(dataSize, 42);
273
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800274 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 4);
275 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700276
277 {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800278 const Interest& interest = face.sentInterests[0];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700279 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world");
280 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true);
281 BOOST_CHECK_EQUAL(interest.getChildSelector(), 1);
282 }
283
284 {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800285 const Interest& interest = face.sentInterests[1];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700286 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%00");
287 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false);
288 BOOST_CHECK_EQUAL(interest.getChildSelector(), 0);
289 }
290
291 {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800292 const Interest& interest = face.sentInterests[2];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700293 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%01");
294 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false);
295 BOOST_CHECK_EQUAL(interest.getChildSelector(), 0);
296 }
297
298 {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800299 const Interest& interest = face.sentInterests[3];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700300 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%02");
301 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false);
302 BOOST_CHECK_EQUAL(interest.getChildSelector(), 0);
303 }
304}
305
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500306BOOST_FIXTURE_TEST_CASE(MultipleSegmentFetching, Fixture)
307{
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -0800308 DummyValidator acceptValidator;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800309 SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)),
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -0800310 acceptValidator,
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500311 bind(&Fixture::onComplete, this, _1),
312 bind(&Fixture::onError, this, _1));
313
Junxiao Shid5827ce2016-07-14 20:49:37 +0000314 advanceClocks(time::milliseconds(10));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500315
316 for (uint64_t i = 0; i < 400; i++) {
Junxiao Shid5827ce2016-07-14 20:49:37 +0000317 advanceClocks(time::milliseconds(10));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800318 face.receive(*makeDataSegment("/hello/world/version0", i, false));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500319 }
Junxiao Shid5827ce2016-07-14 20:49:37 +0000320 advanceClocks(time::milliseconds(10));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800321 face.receive(*makeDataSegment("/hello/world/version0", 400, true));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500322
Junxiao Shid5827ce2016-07-14 20:49:37 +0000323 advanceClocks(time::milliseconds(10));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500324
325 BOOST_CHECK_EQUAL(nErrors, 0);
Junxiao Shid5827ce2016-07-14 20:49:37 +0000326 BOOST_CHECK_EQUAL(nData, 1);
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500327}
328
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500329BOOST_FIXTURE_TEST_CASE(DuplicateNack, Fixture)
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500330{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800331 SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)),
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -0800332 make_shared<DummyValidator>(true),
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500333 bind(&Fixture::onComplete, this, _1),
334 bind(&Fixture::onError, this, _1));
Junxiao Shid5827ce2016-07-14 20:49:37 +0000335 advanceClocks(time::milliseconds(10));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500336
337 // receive nack for the original interest
338 nackLastInterest(lp::NackReason::DUPLICATE);
339
340 // receive nack due to Duplication for the reexpressed interests
341 for (uint32_t i = 1; i <= SegmentFetcher::MAX_INTEREST_REEXPRESS; ++i) {
342 nackLastInterest(lp::NackReason::DUPLICATE);
343 }
344
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800345 BOOST_CHECK_EQUAL(face.sentInterests.size(), (SegmentFetcher::MAX_INTEREST_REEXPRESS + 1));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500346 BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::NACK_ERROR));
347}
348
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500349BOOST_FIXTURE_TEST_CASE(CongestionNack, Fixture)
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500350{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800351 SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)),
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -0800352 make_shared<DummyValidator>(true),
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500353 bind(&Fixture::onComplete, this, _1),
354 bind(&Fixture::onError, this, _1));
Junxiao Shid5827ce2016-07-14 20:49:37 +0000355 advanceClocks(time::milliseconds(10));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500356
357 // receive nack for the original interest
358 nackLastInterest(lp::NackReason::CONGESTION);
359
360 // receive nack due to Congestion for the reexpressed interests
361 for (uint32_t i = 1; i <= SegmentFetcher::MAX_INTEREST_REEXPRESS; ++i) {
362 nackLastInterest(lp::NackReason::CONGESTION);
363 }
364
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800365 BOOST_CHECK_EQUAL(face.sentInterests.size(), (SegmentFetcher::MAX_INTEREST_REEXPRESS + 1));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500366 BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::NACK_ERROR));
367}
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700368
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500369BOOST_FIXTURE_TEST_CASE(SegmentZero, Fixture)
370{
371 int nNacks = 2;
372
373 ndn::Name interestName("ndn:/A");
374 SegmentFetcher::fetch(face,
375 Interest(interestName),
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -0800376 make_shared<DummyValidator>(true),
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500377 bind(&Fixture::onComplete, this, _1),
378 bind(&Fixture::onError, this, _1));
379
Junxiao Shid5827ce2016-07-14 20:49:37 +0000380 advanceClocks(time::milliseconds(1000));
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500381
382 for (uint64_t segmentNo = 0; segmentNo <= 3; segmentNo++) {
383 if (segmentNo == 1) {
384 while (nNacks--) {
385 nackLastInterest(lp::NackReason::CONGESTION);
Junxiao Shid5827ce2016-07-14 20:49:37 +0000386 advanceClocks(time::milliseconds(10));
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500387 }
388 }
389
390 auto data = makeDataSegment(interestName, segmentNo, segmentNo == 3);
391 face.receive(*data);
Junxiao Shid5827ce2016-07-14 20:49:37 +0000392 advanceClocks(time::milliseconds(10));
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500393 }
394
395 // Total number of sent interests should be 6: one interest for segment zero and segment one each,
396 // two re-expressed interests for segment one after getting nack twice, and two interests for
397 // segment two and three
398 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 6);
399
400 BOOST_CHECK_EQUAL(face.sentInterests[0].getName(), ndn::Name("ndn:/A"));
401 BOOST_CHECK_EQUAL(face.sentInterests[1].getName(), ndn::Name("ndn:/A/%00%01"));
402 BOOST_CHECK_EQUAL(face.sentInterests[2].getName(), ndn::Name("ndn:/A/%00%01"));
403 BOOST_CHECK_EQUAL(face.sentInterests[3].getName(), ndn::Name("ndn:/A/%00%01"));
404 BOOST_CHECK_EQUAL(face.sentInterests[4].getName(), ndn::Name("ndn:/A/%00%02"));
405 BOOST_CHECK_EQUAL(face.sentInterests[5].getName(), ndn::Name("ndn:/A/%00%03"));
406}
407
408BOOST_FIXTURE_TEST_CASE(ZeroComponentName, Fixture)
409{
410 SegmentFetcher::fetch(face, Interest("ndn:/"),
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -0800411 make_shared<DummyValidator>(true),
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500412 bind(&Fixture::onComplete, this, _1),
413 bind(&Fixture::onError, this, _1));
Junxiao Shid5827ce2016-07-14 20:49:37 +0000414 advanceClocks(time::milliseconds(10));
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500415 nackLastInterest(lp::NackReason::DUPLICATE);
416 face.receive(*makeDataSegment("/hello/world", 0, true));
417
418 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 2);
419 BOOST_CHECK_EQUAL(face.sentInterests[0].getName(), ndn::Name("ndn:/"));
420 BOOST_CHECK_EQUAL(face.sentInterests[1].getName(), ndn::Name("ndn:/"));
Junxiao Shid5827ce2016-07-14 20:49:37 +0000421 BOOST_REQUIRE_EQUAL(nData, 1);
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500422}
423
Junxiao Shid5827ce2016-07-14 20:49:37 +0000424BOOST_AUTO_TEST_SUITE_END() // TestSegmentFetcher
425BOOST_AUTO_TEST_SUITE_END() // Util
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700426
427} // namespace tests
428} // namespace util
429} // namespace ndn