blob: d80f1ae91cb97b7806b3b6cca9940ca798a7a25d [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"
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050023#include "data.hpp"
24#include "encoding/block.hpp"
Junxiao Shi2bea5c42017-08-14 20:10:32 +000025#include "lp/nack-header.hpp"
26#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 , nErrors(0)
Junxiao Shid5827ce2016-07-14 20:49:37 +000048 , nData(0)
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070049 , dataSize(0)
50 {
51 }
52
53 shared_ptr<Data>
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050054 makeDataSegment(const Name& baseName, uint64_t segment, bool isFinal)
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070055 {
56 const uint8_t buffer[] = "Hello, world!";
57
Junxiao Shid5827ce2016-07-14 20:49:37 +000058 auto data = make_shared<Data>(Name(baseName).appendSegment(segment));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070059 data->setContent(buffer, sizeof(buffer));
60
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050061 if (isFinal) {
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070062 data->setFinalBlockId(data->getName()[-1]);
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050063 }
64 data = signData(data);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070065
66 return data;
67 }
68
69 void
70 onError(uint32_t errorCode)
71 {
72 ++nErrors;
73 lastError = errorCode;
74 }
75
76 void
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050077 onComplete(const ConstBufferPtr& data)
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070078 {
Junxiao Shid5827ce2016-07-14 20:49:37 +000079 ++nData;
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070080 dataSize = data->size();
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050081 dataString = std::string(reinterpret_cast<const char*>(data->get()));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070082 }
83
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050084 void
85 nackLastInterest(lp::NackReason nackReason)
86 {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080087 const Interest& lastInterest = face.sentInterests.back();
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050088 lp::Nack nack = makeNack(lastInterest, nackReason);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080089 face.receive(nack);
Junxiao Shid5827ce2016-07-14 20:49:37 +000090 advanceClocks(time::milliseconds(10));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -050091 }
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070092
93public:
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080094 DummyClientFace face;
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070095
96 uint32_t nErrors;
97 uint32_t lastError;
Junxiao Shid5827ce2016-07-14 20:49:37 +000098 uint32_t nData;
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070099 size_t dataSize;
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500100 std::string dataString;
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700101};
102
103BOOST_FIXTURE_TEST_CASE(Timeout, Fixture)
104{
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -0800105 DummyValidator acceptValidator;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800106 SegmentFetcher::fetch(face, Interest("/hello/world", time::milliseconds(100)),
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -0800107 acceptValidator,
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500108 bind(&Fixture::onComplete, this, _1),
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700109 bind(&Fixture::onError, this, _1));
110
Junxiao Shid5827ce2016-07-14 20:49:37 +0000111 advanceClocks(time::milliseconds(1));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800112 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700113
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800114 const Interest& interest = face.sentInterests[0];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700115 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world");
116 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true);
117 BOOST_CHECK_EQUAL(interest.getChildSelector(), 1);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800118
Junxiao Shid5827ce2016-07-14 20:49:37 +0000119 advanceClocks(time::milliseconds(98));
120 BOOST_CHECK_EQUAL(nErrors, 0);
121 BOOST_CHECK_EQUAL(nData, 0);
122 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800123
Junxiao Shid5827ce2016-07-14 20:49:37 +0000124 advanceClocks(time::milliseconds(1), 2);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800125 BOOST_CHECK_EQUAL(nErrors, 1);
126 BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::INTEREST_TIMEOUT));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800127 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
128 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700129}
130
131
132BOOST_FIXTURE_TEST_CASE(Basic, Fixture)
133{
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -0800134 DummyValidator acceptValidator;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800135 SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)),
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -0800136 acceptValidator,
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500137 bind(&Fixture::onComplete, this, _1),
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700138 bind(&Fixture::onError, this, _1));
139
Junxiao Shid5827ce2016-07-14 20:49:37 +0000140 advanceClocks(time::milliseconds(10));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700141
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800142 face.receive(*makeDataSegment("/hello/world/version0", 0, true));
Junxiao Shid5827ce2016-07-14 20:49:37 +0000143 advanceClocks(time::milliseconds(10));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700144
145 BOOST_CHECK_EQUAL(nErrors, 0);
Junxiao Shid5827ce2016-07-14 20:49:37 +0000146 BOOST_CHECK_EQUAL(nData, 1);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700147
148 BOOST_CHECK_EQUAL(dataSize, 14);
149
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500150 const uint8_t buffer[] = "Hello, world!";
151 std::string bufferString = std::string(reinterpret_cast<const char*>(buffer));
152
153 BOOST_CHECK_EQUAL(dataString, bufferString);
154
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800155 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
156 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700157
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800158 const Interest& interest = face.sentInterests[0];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700159 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world");
160 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true);
161 BOOST_CHECK_EQUAL(interest.getChildSelector(), 1);
162}
163
164BOOST_FIXTURE_TEST_CASE(NoSegmentInData, Fixture)
165{
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -0800166 DummyValidator acceptValidator;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800167 SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)),
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -0800168 acceptValidator,
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500169 bind(&Fixture::onComplete, this, _1),
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700170 bind(&Fixture::onError, this, _1));
171
Junxiao Shid5827ce2016-07-14 20:49:37 +0000172 advanceClocks(time::milliseconds(10));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700173
174 const uint8_t buffer[] = "Hello, world!";
175
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500176 shared_ptr<Data> data = makeData("/hello/world/version0/no-segment");
177
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700178 data->setContent(buffer, sizeof(buffer));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700179
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800180 face.receive(*data);
Junxiao Shid5827ce2016-07-14 20:49:37 +0000181 advanceClocks(time::milliseconds(10));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700182
183 BOOST_CHECK_EQUAL(nErrors, 1);
184 BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::DATA_HAS_NO_SEGMENT));
Junxiao Shid5827ce2016-07-14 20:49:37 +0000185 BOOST_CHECK_EQUAL(nData, 0);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700186}
187
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700188BOOST_FIXTURE_TEST_CASE(SegmentValidationFailure, Fixture)
189{
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -0800190 DummyValidator rejectValidator(false);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800191 SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)),
Junxiao Shi0f3f0b42016-07-14 13:26:37 +0000192 rejectValidator,
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500193 bind(&Fixture::onComplete, this, _1),
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700194 bind(&Fixture::onError, this, _1));
195
Junxiao Shid5827ce2016-07-14 20:49:37 +0000196 advanceClocks(time::milliseconds(10));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800197 face.receive(*makeDataSegment("/hello/world/version0", 0, true));
Junxiao Shid5827ce2016-07-14 20:49:37 +0000198 advanceClocks(time::milliseconds(10));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700199
200 BOOST_CHECK_EQUAL(nErrors, 1);
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500201 BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::SEGMENT_VALIDATION_FAIL));
Junxiao Shid5827ce2016-07-14 20:49:37 +0000202 BOOST_CHECK_EQUAL(nData, 0);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700203}
204
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700205BOOST_FIXTURE_TEST_CASE(Triple, Fixture)
206{
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -0800207 DummyValidator acceptValidator;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800208 SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)),
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -0800209 acceptValidator,
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500210 bind(&Fixture::onComplete, this, _1),
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700211 bind(&Fixture::onError, this, _1));
212
Junxiao Shid5827ce2016-07-14 20:49:37 +0000213 advanceClocks(time::milliseconds(10));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800214 face.receive(*makeDataSegment("/hello/world/version0", 0, false));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700215
Junxiao Shid5827ce2016-07-14 20:49:37 +0000216 advanceClocks(time::milliseconds(10));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800217 face.receive(*makeDataSegment("/hello/world/version0", 1, false));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700218
Junxiao Shid5827ce2016-07-14 20:49:37 +0000219 advanceClocks(time::milliseconds(10));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800220 face.receive(*makeDataSegment("/hello/world/version0", 2, true));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700221
Junxiao Shid5827ce2016-07-14 20:49:37 +0000222 advanceClocks(time::milliseconds(10));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700223
224 BOOST_CHECK_EQUAL(nErrors, 0);
Junxiao Shid5827ce2016-07-14 20:49:37 +0000225 BOOST_CHECK_EQUAL(nData, 1);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700226
227 BOOST_CHECK_EQUAL(dataSize, 42);
228
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800229 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 3);
230 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700231
232 {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800233 const Interest& interest = face.sentInterests[0];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700234 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world");
235 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true);
236 BOOST_CHECK_EQUAL(interest.getChildSelector(), 1);
237 }
238
239 {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800240 const Interest& interest = face.sentInterests[1];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700241 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%01");
242 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false);
243 BOOST_CHECK_EQUAL(interest.getChildSelector(), 0);
244 }
245
246 {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800247 const Interest& interest = face.sentInterests[2];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700248 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%02");
249 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false);
250 BOOST_CHECK_EQUAL(interest.getChildSelector(), 0);
251 }
252}
253
254BOOST_FIXTURE_TEST_CASE(TripleWithInitialSegmentFetching, Fixture)
255{
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -0800256 DummyValidator acceptValidator;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800257 SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)),
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -0800258 acceptValidator,
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500259 bind(&Fixture::onComplete, this, _1),
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700260 bind(&Fixture::onError, this, _1));
261
Junxiao Shid5827ce2016-07-14 20:49:37 +0000262 advanceClocks(time::milliseconds(10));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800263 face.receive(*makeDataSegment("/hello/world/version0", 1, false));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700264
Junxiao Shid5827ce2016-07-14 20:49:37 +0000265 advanceClocks(time::milliseconds(10));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800266 face.receive(*makeDataSegment("/hello/world/version0", 0, false));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700267
Junxiao Shid5827ce2016-07-14 20:49:37 +0000268 advanceClocks(time::milliseconds(10));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800269 face.receive(*makeDataSegment("/hello/world/version0", 1, false));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700270
Junxiao Shid5827ce2016-07-14 20:49:37 +0000271 advanceClocks(time::milliseconds(10));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800272 face.receive(*makeDataSegment("/hello/world/version0", 2, true));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700273
Junxiao Shid5827ce2016-07-14 20:49:37 +0000274 advanceClocks(time::milliseconds(10));
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700275
276 BOOST_CHECK_EQUAL(nErrors, 0);
Junxiao Shid5827ce2016-07-14 20:49:37 +0000277 BOOST_CHECK_EQUAL(nData, 1);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700278
279 BOOST_CHECK_EQUAL(dataSize, 42);
280
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800281 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 4);
282 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700283
284 {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800285 const Interest& interest = face.sentInterests[0];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700286 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world");
287 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true);
288 BOOST_CHECK_EQUAL(interest.getChildSelector(), 1);
289 }
290
291 {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800292 const Interest& interest = face.sentInterests[1];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700293 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%00");
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[2];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700300 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%01");
301 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false);
302 BOOST_CHECK_EQUAL(interest.getChildSelector(), 0);
303 }
304
305 {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800306 const Interest& interest = face.sentInterests[3];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700307 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%02");
308 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false);
309 BOOST_CHECK_EQUAL(interest.getChildSelector(), 0);
310 }
311}
312
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500313BOOST_FIXTURE_TEST_CASE(MultipleSegmentFetching, Fixture)
314{
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -0800315 DummyValidator acceptValidator;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800316 SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)),
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -0800317 acceptValidator,
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500318 bind(&Fixture::onComplete, this, _1),
319 bind(&Fixture::onError, this, _1));
320
Junxiao Shid5827ce2016-07-14 20:49:37 +0000321 advanceClocks(time::milliseconds(10));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500322
323 for (uint64_t i = 0; i < 400; i++) {
Junxiao Shid5827ce2016-07-14 20:49:37 +0000324 advanceClocks(time::milliseconds(10));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800325 face.receive(*makeDataSegment("/hello/world/version0", i, false));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500326 }
Junxiao Shid5827ce2016-07-14 20:49:37 +0000327 advanceClocks(time::milliseconds(10));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800328 face.receive(*makeDataSegment("/hello/world/version0", 400, true));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500329
Junxiao Shid5827ce2016-07-14 20:49:37 +0000330 advanceClocks(time::milliseconds(10));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500331
332 BOOST_CHECK_EQUAL(nErrors, 0);
Junxiao Shid5827ce2016-07-14 20:49:37 +0000333 BOOST_CHECK_EQUAL(nData, 1);
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500334}
335
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500336BOOST_FIXTURE_TEST_CASE(DuplicateNack, Fixture)
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500337{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800338 SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)),
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -0800339 make_shared<DummyValidator>(true),
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500340 bind(&Fixture::onComplete, this, _1),
341 bind(&Fixture::onError, this, _1));
Junxiao Shid5827ce2016-07-14 20:49:37 +0000342 advanceClocks(time::milliseconds(10));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500343
344 // receive nack for the original interest
345 nackLastInterest(lp::NackReason::DUPLICATE);
346
347 // receive nack due to Duplication for the reexpressed interests
348 for (uint32_t i = 1; i <= SegmentFetcher::MAX_INTEREST_REEXPRESS; ++i) {
349 nackLastInterest(lp::NackReason::DUPLICATE);
350 }
351
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800352 BOOST_CHECK_EQUAL(face.sentInterests.size(), (SegmentFetcher::MAX_INTEREST_REEXPRESS + 1));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500353 BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::NACK_ERROR));
354}
355
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500356BOOST_FIXTURE_TEST_CASE(CongestionNack, Fixture)
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500357{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800358 SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)),
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -0800359 make_shared<DummyValidator>(true),
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500360 bind(&Fixture::onComplete, this, _1),
361 bind(&Fixture::onError, this, _1));
Junxiao Shid5827ce2016-07-14 20:49:37 +0000362 advanceClocks(time::milliseconds(10));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500363
364 // receive nack for the original interest
365 nackLastInterest(lp::NackReason::CONGESTION);
366
367 // receive nack due to Congestion for the reexpressed interests
368 for (uint32_t i = 1; i <= SegmentFetcher::MAX_INTEREST_REEXPRESS; ++i) {
369 nackLastInterest(lp::NackReason::CONGESTION);
370 }
371
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800372 BOOST_CHECK_EQUAL(face.sentInterests.size(), (SegmentFetcher::MAX_INTEREST_REEXPRESS + 1));
Muktadir R Chowdhuryf58f8f42015-09-02 11:56:49 -0500373 BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::NACK_ERROR));
374}
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700375
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500376BOOST_FIXTURE_TEST_CASE(SegmentZero, Fixture)
377{
378 int nNacks = 2;
379
380 ndn::Name interestName("ndn:/A");
381 SegmentFetcher::fetch(face,
382 Interest(interestName),
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -0800383 make_shared<DummyValidator>(true),
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500384 bind(&Fixture::onComplete, this, _1),
385 bind(&Fixture::onError, this, _1));
386
Junxiao Shid5827ce2016-07-14 20:49:37 +0000387 advanceClocks(time::milliseconds(1000));
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500388
389 for (uint64_t segmentNo = 0; segmentNo <= 3; segmentNo++) {
390 if (segmentNo == 1) {
391 while (nNacks--) {
392 nackLastInterest(lp::NackReason::CONGESTION);
Junxiao Shid5827ce2016-07-14 20:49:37 +0000393 advanceClocks(time::milliseconds(10));
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500394 }
395 }
396
397 auto data = makeDataSegment(interestName, segmentNo, segmentNo == 3);
398 face.receive(*data);
Junxiao Shid5827ce2016-07-14 20:49:37 +0000399 advanceClocks(time::milliseconds(10));
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500400 }
401
402 // Total number of sent interests should be 6: one interest for segment zero and segment one each,
403 // two re-expressed interests for segment one after getting nack twice, and two interests for
404 // segment two and three
405 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 6);
406
407 BOOST_CHECK_EQUAL(face.sentInterests[0].getName(), ndn::Name("ndn:/A"));
408 BOOST_CHECK_EQUAL(face.sentInterests[1].getName(), ndn::Name("ndn:/A/%00%01"));
409 BOOST_CHECK_EQUAL(face.sentInterests[2].getName(), ndn::Name("ndn:/A/%00%01"));
410 BOOST_CHECK_EQUAL(face.sentInterests[3].getName(), ndn::Name("ndn:/A/%00%01"));
411 BOOST_CHECK_EQUAL(face.sentInterests[4].getName(), ndn::Name("ndn:/A/%00%02"));
412 BOOST_CHECK_EQUAL(face.sentInterests[5].getName(), ndn::Name("ndn:/A/%00%03"));
413}
414
415BOOST_FIXTURE_TEST_CASE(ZeroComponentName, Fixture)
416{
417 SegmentFetcher::fetch(face, Interest("ndn:/"),
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -0800418 make_shared<DummyValidator>(true),
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500419 bind(&Fixture::onComplete, this, _1),
420 bind(&Fixture::onError, this, _1));
Junxiao Shid5827ce2016-07-14 20:49:37 +0000421 advanceClocks(time::milliseconds(10));
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500422 nackLastInterest(lp::NackReason::DUPLICATE);
423 face.receive(*makeDataSegment("/hello/world", 0, true));
424
425 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 2);
426 BOOST_CHECK_EQUAL(face.sentInterests[0].getName(), ndn::Name("ndn:/"));
427 BOOST_CHECK_EQUAL(face.sentInterests[1].getName(), ndn::Name("ndn:/"));
Junxiao Shid5827ce2016-07-14 20:49:37 +0000428 BOOST_REQUIRE_EQUAL(nData, 1);
Muktadir R Chowdhury2bc2df02016-04-05 16:55:41 -0500429}
430
Junxiao Shid5827ce2016-07-14 20:49:37 +0000431BOOST_AUTO_TEST_SUITE_END() // TestSegmentFetcher
432BOOST_AUTO_TEST_SUITE_END() // Util
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700433
434} // namespace tests
435} // namespace util
436} // namespace ndn