blob: 462e759cb52aeebdca3b8111b4b63a0348a4f5b9 [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
Junxiao Shid5827ce2016-07-14 20:49:37 +000041BOOST_AUTO_TEST_SUITE(Util)
42BOOST_AUTO_TEST_SUITE(TestSegmentFetcher)
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070043
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000044class Fixture : public IdentityManagementTimeFixture
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070045{
46public:
47 Fixture()
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070048 : face(io, m_keyChain)
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070049 , nErrors(0)
Junxiao Shid5827ce2016-07-14 20:49:37 +000050 , nData(0)
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070051 , dataSize(0)
52 {
53 }
54
55 shared_ptr<Data>
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050056 makeDataSegment(const Name& baseName, uint64_t segment, bool isFinal)
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070057 {
58 const uint8_t buffer[] = "Hello, world!";
59
Junxiao Shid5827ce2016-07-14 20:49:37 +000060 auto data = make_shared<Data>(Name(baseName).appendSegment(segment));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070061 data->setContent(buffer, sizeof(buffer));
62
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050063 if (isFinal) {
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070064 data->setFinalBlockId(data->getName()[-1]);
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050065 }
66 data = signData(data);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070067
68 return data;
69 }
70
71 void
72 onError(uint32_t errorCode)
73 {
74 ++nErrors;
75 lastError = errorCode;
76 }
77
78 void
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050079 onComplete(const ConstBufferPtr& data)
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070080 {
Junxiao Shid5827ce2016-07-14 20:49:37 +000081 ++nData;
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070082 dataSize = data->size();
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050083 dataString = std::string(reinterpret_cast<const char*>(data->get()));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070084 }
85
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050086 void
87 nackLastInterest(lp::NackReason nackReason)
88 {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080089 const Interest& lastInterest = face.sentInterests.back();
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050090 lp::Nack nack = makeNack(lastInterest, nackReason);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080091 face.receive(nack);
Junxiao Shid5827ce2016-07-14 20:49:37 +000092 advanceClocks(time::milliseconds(10));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050093 }
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070094
95public:
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080096 DummyClientFace face;
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070097
98 uint32_t nErrors;
99 uint32_t lastError;
Junxiao Shid5827ce2016-07-14 20:49:37 +0000100 uint32_t nData;
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700101 size_t dataSize;
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500102 std::string dataString;
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700103};
104
105BOOST_FIXTURE_TEST_CASE(Timeout, Fixture)
106{
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500107 ValidatorNull nullValidator;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800108 SegmentFetcher::fetch(face, Interest("/hello/world", time::milliseconds(100)),
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500109 nullValidator,
110 bind(&Fixture::onComplete, this, _1),
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700111 bind(&Fixture::onError, this, _1));
112
Junxiao Shid5827ce2016-07-14 20:49:37 +0000113 advanceClocks(time::milliseconds(1));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800114 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700115
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800116 const Interest& interest = face.sentInterests[0];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700117 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world");
118 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true);
119 BOOST_CHECK_EQUAL(interest.getChildSelector(), 1);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800120
Junxiao Shid5827ce2016-07-14 20:49:37 +0000121 advanceClocks(time::milliseconds(98));
122 BOOST_CHECK_EQUAL(nErrors, 0);
123 BOOST_CHECK_EQUAL(nData, 0);
124 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800125
Junxiao Shid5827ce2016-07-14 20:49:37 +0000126 advanceClocks(time::milliseconds(1), 2);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800127 BOOST_CHECK_EQUAL(nErrors, 1);
128 BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::INTEREST_TIMEOUT));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800129 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
130 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700131}
132
133
134BOOST_FIXTURE_TEST_CASE(Basic, Fixture)
135{
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500136 ValidatorNull nullValidator;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800137 SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)),
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500138 nullValidator,
139 bind(&Fixture::onComplete, this, _1),
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700140 bind(&Fixture::onError, this, _1));
141
Junxiao Shid5827ce2016-07-14 20:49:37 +0000142 advanceClocks(time::milliseconds(10));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700143
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800144 face.receive(*makeDataSegment("/hello/world/version0", 0, true));
Junxiao Shid5827ce2016-07-14 20:49:37 +0000145 advanceClocks(time::milliseconds(10));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700146
147 BOOST_CHECK_EQUAL(nErrors, 0);
Junxiao Shid5827ce2016-07-14 20:49:37 +0000148 BOOST_CHECK_EQUAL(nData, 1);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700149
150 BOOST_CHECK_EQUAL(dataSize, 14);
151
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500152 const uint8_t buffer[] = "Hello, world!";
153 std::string bufferString = std::string(reinterpret_cast<const char*>(buffer));
154
155 BOOST_CHECK_EQUAL(dataString, bufferString);
156
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800157 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
158 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700159
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800160 const Interest& interest = face.sentInterests[0];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700161 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world");
162 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true);
163 BOOST_CHECK_EQUAL(interest.getChildSelector(), 1);
164}
165
166BOOST_FIXTURE_TEST_CASE(NoSegmentInData, Fixture)
167{
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500168 ValidatorNull nullValidator;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800169 SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)),
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500170 nullValidator,
171 bind(&Fixture::onComplete, this, _1),
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700172 bind(&Fixture::onError, this, _1));
173
Junxiao Shid5827ce2016-07-14 20:49:37 +0000174 advanceClocks(time::milliseconds(10));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700175
176 const uint8_t buffer[] = "Hello, world!";
177
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500178 shared_ptr<Data> data = makeData("/hello/world/version0/no-segment");
179
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700180 data->setContent(buffer, sizeof(buffer));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700181
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800182 face.receive(*data);
Junxiao Shid5827ce2016-07-14 20:49:37 +0000183 advanceClocks(time::milliseconds(10));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700184
185 BOOST_CHECK_EQUAL(nErrors, 1);
186 BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::DATA_HAS_NO_SEGMENT));
Junxiao Shid5827ce2016-07-14 20:49:37 +0000187 BOOST_CHECK_EQUAL(nData, 0);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700188}
189
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700190BOOST_FIXTURE_TEST_CASE(SegmentValidationFailure, Fixture)
191{
Junxiao Shi0f3f0b42016-07-14 13:26:37 +0000192 DummyRejectValidator rejectValidator;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800193 SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)),
Junxiao Shi0f3f0b42016-07-14 13:26:37 +0000194 rejectValidator,
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500195 bind(&Fixture::onComplete, this, _1),
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700196 bind(&Fixture::onError, this, _1));
197
Junxiao Shid5827ce2016-07-14 20:49:37 +0000198 advanceClocks(time::milliseconds(10));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800199 face.receive(*makeDataSegment("/hello/world/version0", 0, true));
Junxiao Shid5827ce2016-07-14 20:49:37 +0000200 advanceClocks(time::milliseconds(10));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700201
202 BOOST_CHECK_EQUAL(nErrors, 1);
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500203 BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::SEGMENT_VALIDATION_FAIL));
Junxiao Shid5827ce2016-07-14 20:49:37 +0000204 BOOST_CHECK_EQUAL(nData, 0);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700205}
206
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700207BOOST_FIXTURE_TEST_CASE(Triple, Fixture)
208{
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500209 ValidatorNull nullValidator;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800210 SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)),
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500211 nullValidator,
212 bind(&Fixture::onComplete, this, _1),
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700213 bind(&Fixture::onError, this, _1));
214
Junxiao Shid5827ce2016-07-14 20:49:37 +0000215 advanceClocks(time::milliseconds(10));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800216 face.receive(*makeDataSegment("/hello/world/version0", 0, false));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700217
Junxiao Shid5827ce2016-07-14 20:49:37 +0000218 advanceClocks(time::milliseconds(10));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800219 face.receive(*makeDataSegment("/hello/world/version0", 1, false));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700220
Junxiao Shid5827ce2016-07-14 20:49:37 +0000221 advanceClocks(time::milliseconds(10));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800222 face.receive(*makeDataSegment("/hello/world/version0", 2, true));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700223
Junxiao Shid5827ce2016-07-14 20:49:37 +0000224 advanceClocks(time::milliseconds(10));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700225
226 BOOST_CHECK_EQUAL(nErrors, 0);
Junxiao Shid5827ce2016-07-14 20:49:37 +0000227 BOOST_CHECK_EQUAL(nData, 1);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700228
229 BOOST_CHECK_EQUAL(dataSize, 42);
230
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800231 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 3);
232 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700233
234 {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800235 const Interest& interest = face.sentInterests[0];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700236 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world");
237 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true);
238 BOOST_CHECK_EQUAL(interest.getChildSelector(), 1);
239 }
240
241 {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800242 const Interest& interest = face.sentInterests[1];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700243 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%01");
244 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false);
245 BOOST_CHECK_EQUAL(interest.getChildSelector(), 0);
246 }
247
248 {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800249 const Interest& interest = face.sentInterests[2];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700250 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%02");
251 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false);
252 BOOST_CHECK_EQUAL(interest.getChildSelector(), 0);
253 }
254}
255
256BOOST_FIXTURE_TEST_CASE(TripleWithInitialSegmentFetching, Fixture)
257{
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500258 ValidatorNull nullValidator;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800259 SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)),
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500260 nullValidator,
261 bind(&Fixture::onComplete, this, _1),
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700262 bind(&Fixture::onError, this, _1));
263
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", 1, false));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700266
Junxiao Shid5827ce2016-07-14 20:49:37 +0000267 advanceClocks(time::milliseconds(10));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800268 face.receive(*makeDataSegment("/hello/world/version0", 0, false));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700269
Junxiao Shid5827ce2016-07-14 20:49:37 +0000270 advanceClocks(time::milliseconds(10));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800271 face.receive(*makeDataSegment("/hello/world/version0", 1, false));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700272
Junxiao Shid5827ce2016-07-14 20:49:37 +0000273 advanceClocks(time::milliseconds(10));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800274 face.receive(*makeDataSegment("/hello/world/version0", 2, true));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700275
Junxiao Shid5827ce2016-07-14 20:49:37 +0000276 advanceClocks(time::milliseconds(10));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700277
278 BOOST_CHECK_EQUAL(nErrors, 0);
Junxiao Shid5827ce2016-07-14 20:49:37 +0000279 BOOST_CHECK_EQUAL(nData, 1);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700280
281 BOOST_CHECK_EQUAL(dataSize, 42);
282
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800283 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 4);
284 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700285
286 {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800287 const Interest& interest = face.sentInterests[0];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700288 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world");
289 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true);
290 BOOST_CHECK_EQUAL(interest.getChildSelector(), 1);
291 }
292
293 {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800294 const Interest& interest = face.sentInterests[1];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700295 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%00");
296 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false);
297 BOOST_CHECK_EQUAL(interest.getChildSelector(), 0);
298 }
299
300 {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800301 const Interest& interest = face.sentInterests[2];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700302 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%01");
303 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false);
304 BOOST_CHECK_EQUAL(interest.getChildSelector(), 0);
305 }
306
307 {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800308 const Interest& interest = face.sentInterests[3];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700309 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%02");
310 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false);
311 BOOST_CHECK_EQUAL(interest.getChildSelector(), 0);
312 }
313}
314
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500315BOOST_FIXTURE_TEST_CASE(MultipleSegmentFetching, Fixture)
316{
317 ValidatorNull nullValidator;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800318 SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)),
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500319 nullValidator,
320 bind(&Fixture::onComplete, this, _1),
321 bind(&Fixture::onError, this, _1));
322
Junxiao Shid5827ce2016-07-14 20:49:37 +0000323 advanceClocks(time::milliseconds(10));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500324
325 for (uint64_t i = 0; i < 400; i++) {
Junxiao Shid5827ce2016-07-14 20:49:37 +0000326 advanceClocks(time::milliseconds(10));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800327 face.receive(*makeDataSegment("/hello/world/version0", i, false));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500328 }
Junxiao Shid5827ce2016-07-14 20:49:37 +0000329 advanceClocks(time::milliseconds(10));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800330 face.receive(*makeDataSegment("/hello/world/version0", 400, true));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500331
Junxiao Shid5827ce2016-07-14 20:49:37 +0000332 advanceClocks(time::milliseconds(10));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500333
334 BOOST_CHECK_EQUAL(nErrors, 0);
Junxiao Shid5827ce2016-07-14 20:49:37 +0000335 BOOST_CHECK_EQUAL(nData, 1);
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500336}
337
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500338BOOST_FIXTURE_TEST_CASE(DuplicateNack, Fixture)
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500339{
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 make_shared<ValidatorNull>(),
342 bind(&Fixture::onComplete, this, _1),
343 bind(&Fixture::onError, this, _1));
Junxiao Shid5827ce2016-07-14 20:49:37 +0000344 advanceClocks(time::milliseconds(10));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500345
346 // receive nack for the original interest
347 nackLastInterest(lp::NackReason::DUPLICATE);
348
349 // receive nack due to Duplication for the reexpressed interests
350 for (uint32_t i = 1; i <= SegmentFetcher::MAX_INTEREST_REEXPRESS; ++i) {
351 nackLastInterest(lp::NackReason::DUPLICATE);
352 }
353
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800354 BOOST_CHECK_EQUAL(face.sentInterests.size(), (SegmentFetcher::MAX_INTEREST_REEXPRESS + 1));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500355 BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::NACK_ERROR));
356}
357
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500358BOOST_FIXTURE_TEST_CASE(CongestionNack, Fixture)
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500359{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800360 SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)),
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500361 make_shared<ValidatorNull>(),
362 bind(&Fixture::onComplete, this, _1),
363 bind(&Fixture::onError, this, _1));
Junxiao Shid5827ce2016-07-14 20:49:37 +0000364 advanceClocks(time::milliseconds(10));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500365
366 // receive nack for the original interest
367 nackLastInterest(lp::NackReason::CONGESTION);
368
369 // receive nack due to Congestion for the reexpressed interests
370 for (uint32_t i = 1; i <= SegmentFetcher::MAX_INTEREST_REEXPRESS; ++i) {
371 nackLastInterest(lp::NackReason::CONGESTION);
372 }
373
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800374 BOOST_CHECK_EQUAL(face.sentInterests.size(), (SegmentFetcher::MAX_INTEREST_REEXPRESS + 1));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500375 BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::NACK_ERROR));
376}
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700377
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500378BOOST_FIXTURE_TEST_CASE(SegmentZero, Fixture)
379{
380 int nNacks = 2;
381
382 ndn::Name interestName("ndn:/A");
383 SegmentFetcher::fetch(face,
384 Interest(interestName),
385 make_shared<ValidatorNull>(),
386 bind(&Fixture::onComplete, this, _1),
387 bind(&Fixture::onError, this, _1));
388
Junxiao Shid5827ce2016-07-14 20:49:37 +0000389 advanceClocks(time::milliseconds(1000));
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500390
391 for (uint64_t segmentNo = 0; segmentNo <= 3; segmentNo++) {
392 if (segmentNo == 1) {
393 while (nNacks--) {
394 nackLastInterest(lp::NackReason::CONGESTION);
Junxiao Shid5827ce2016-07-14 20:49:37 +0000395 advanceClocks(time::milliseconds(10));
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500396 }
397 }
398
399 auto data = makeDataSegment(interestName, segmentNo, segmentNo == 3);
400 face.receive(*data);
Junxiao Shid5827ce2016-07-14 20:49:37 +0000401 advanceClocks(time::milliseconds(10));
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500402 }
403
404 // Total number of sent interests should be 6: one interest for segment zero and segment one each,
405 // two re-expressed interests for segment one after getting nack twice, and two interests for
406 // segment two and three
407 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 6);
408
409 BOOST_CHECK_EQUAL(face.sentInterests[0].getName(), ndn::Name("ndn:/A"));
410 BOOST_CHECK_EQUAL(face.sentInterests[1].getName(), ndn::Name("ndn:/A/%00%01"));
411 BOOST_CHECK_EQUAL(face.sentInterests[2].getName(), ndn::Name("ndn:/A/%00%01"));
412 BOOST_CHECK_EQUAL(face.sentInterests[3].getName(), ndn::Name("ndn:/A/%00%01"));
413 BOOST_CHECK_EQUAL(face.sentInterests[4].getName(), ndn::Name("ndn:/A/%00%02"));
414 BOOST_CHECK_EQUAL(face.sentInterests[5].getName(), ndn::Name("ndn:/A/%00%03"));
415}
416
417BOOST_FIXTURE_TEST_CASE(ZeroComponentName, Fixture)
418{
419 SegmentFetcher::fetch(face, Interest("ndn:/"),
420 make_shared<ValidatorNull>(),
421 bind(&Fixture::onComplete, this, _1),
422 bind(&Fixture::onError, this, _1));
Junxiao Shid5827ce2016-07-14 20:49:37 +0000423 advanceClocks(time::milliseconds(10));
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500424 nackLastInterest(lp::NackReason::DUPLICATE);
425 face.receive(*makeDataSegment("/hello/world", 0, true));
426
427 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 2);
428 BOOST_CHECK_EQUAL(face.sentInterests[0].getName(), ndn::Name("ndn:/"));
429 BOOST_CHECK_EQUAL(face.sentInterests[1].getName(), ndn::Name("ndn:/"));
Junxiao Shid5827ce2016-07-14 20:49:37 +0000430 BOOST_REQUIRE_EQUAL(nData, 1);
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500431}
432
Junxiao Shid5827ce2016-07-14 20:49:37 +0000433BOOST_AUTO_TEST_SUITE_END() // TestSegmentFetcher
434BOOST_AUTO_TEST_SUITE_END() // Util
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700435
436} // namespace tests
437} // namespace util
438} // namespace ndn