blob: b0884eb4a4b4bb4a5db6b14b9d3559d994a991d1 [file] [log] [blame]
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyevaf99f462015-01-19 21:43:09 -08003 * Copyright (c) 2013-2015 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"
23
24#include "boost-test.hpp"
Junxiao Shia60d9362014-11-12 09:38:21 -070025#include "util/dummy-client-face.hpp"
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070026#include "security/key-chain.hpp"
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080027#include "../unit-test-time-fixture.hpp"
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070028
29namespace ndn {
30namespace util {
31namespace tests {
32
33BOOST_AUTO_TEST_SUITE(UtilSegmentFetcher)
34
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080035class Fixture : public ndn::tests::UnitTestTimeFixture
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070036{
37public:
38 Fixture()
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080039 : face(makeDummyClientFace(io))
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070040 , nErrors(0)
41 , nDatas(0)
42 , dataSize(0)
43 {
44 }
45
46 shared_ptr<Data>
47 makeData(const Name& baseName, uint64_t segment, bool isFinal)
48 {
49 const uint8_t buffer[] = "Hello, world!";
50
51 shared_ptr<Data> data = make_shared<Data>(Name(baseName).appendSegment(segment));
52 data->setContent(buffer, sizeof(buffer));
53
54 if (isFinal)
55 data->setFinalBlockId(data->getName()[-1]);
56 keyChain.sign(*data);
57
58 return data;
59 }
60
61 void
62 onError(uint32_t errorCode)
63 {
64 ++nErrors;
65 lastError = errorCode;
66 }
67
68 void
69 onData(const ConstBufferPtr& data)
70 {
71 ++nDatas;
72 dataSize = data->size();
73 }
74
75
76public:
Junxiao Shia60d9362014-11-12 09:38:21 -070077 shared_ptr<DummyClientFace> face;
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070078 KeyChain keyChain;
79
80 uint32_t nErrors;
81 uint32_t lastError;
82 uint32_t nDatas;
83 size_t dataSize;
84};
85
86BOOST_FIXTURE_TEST_CASE(Timeout, Fixture)
87{
88 SegmentFetcher::fetch(*face, Interest("/hello/world", time::milliseconds(100)),
89 DontVerifySegment(),
90 bind(&Fixture::onData, this, _1),
91 bind(&Fixture::onError, this, _1));
92
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080093 advanceClocks(time::milliseconds(1), 99);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070094
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080095 BOOST_CHECK_EQUAL(nErrors, 0);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070096 BOOST_CHECK_EQUAL(nDatas, 0);
Junxiao Shia60d9362014-11-12 09:38:21 -070097 BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 1);
98 BOOST_CHECK_EQUAL(face->sentDatas.size(), 0);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -070099
Junxiao Shia60d9362014-11-12 09:38:21 -0700100 const Interest& interest = face->sentInterests[0];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700101 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world");
102 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true);
103 BOOST_CHECK_EQUAL(interest.getChildSelector(), 1);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800104
105 advanceClocks(time::milliseconds(1), 2);
106
107 BOOST_CHECK_EQUAL(nErrors, 1);
108 BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::INTEREST_TIMEOUT));
109 BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 1);
110 BOOST_CHECK_EQUAL(face->sentDatas.size(), 0);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700111}
112
113
114BOOST_FIXTURE_TEST_CASE(Basic, Fixture)
115{
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700116 SegmentFetcher::fetch(*face, Interest("/hello/world", time::seconds(1000)),
117 DontVerifySegment(),
118 bind(&Fixture::onData, this, _1),
119 bind(&Fixture::onError, this, _1));
120
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800121 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700122
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700123 face->receive(*makeData("/hello/world/version0", 0, true));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800124 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700125
126 BOOST_CHECK_EQUAL(nErrors, 0);
127 BOOST_CHECK_EQUAL(nDatas, 1);
128
129 BOOST_CHECK_EQUAL(dataSize, 14);
130
Junxiao Shia60d9362014-11-12 09:38:21 -0700131 BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 1);
132 BOOST_CHECK_EQUAL(face->sentDatas.size(), 0);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700133
Junxiao Shia60d9362014-11-12 09:38:21 -0700134 const Interest& interest = face->sentInterests[0];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700135 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world");
136 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true);
137 BOOST_CHECK_EQUAL(interest.getChildSelector(), 1);
138}
139
140BOOST_FIXTURE_TEST_CASE(NoSegmentInData, Fixture)
141{
142
143 SegmentFetcher::fetch(*face, Interest("/hello/world", time::seconds(1000)),
144 DontVerifySegment(),
145 bind(&Fixture::onData, this, _1),
146 bind(&Fixture::onError, this, _1));
147
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800148 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700149
150 const uint8_t buffer[] = "Hello, world!";
151
152 shared_ptr<Data> data = make_shared<Data>("/hello/world/version0/no-segment");
153 data->setContent(buffer, sizeof(buffer));
154 keyChain.sign(*data);
155
156 face->receive(*data);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800157 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700158
159 BOOST_CHECK_EQUAL(nErrors, 1);
160 BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::DATA_HAS_NO_SEGMENT));
161 BOOST_CHECK_EQUAL(nDatas, 0);
162}
163
164bool
165failValidation(const Data& data)
166{
167 return false;
168}
169
170BOOST_FIXTURE_TEST_CASE(SegmentValidationFailure, Fixture)
171{
172
173 SegmentFetcher::fetch(*face, Interest("/hello/world", time::seconds(1000)),
174 &failValidation,
175 bind(&Fixture::onData, this, _1),
176 bind(&Fixture::onError, this, _1));
177
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800178 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700179 face->receive(*makeData("/hello/world/version0", 0, true));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800180 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700181
182 BOOST_CHECK_EQUAL(nErrors, 1);
183 BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::SEGMENT_VERIFICATION_FAIL));
184 BOOST_CHECK_EQUAL(nDatas, 0);
185}
186
187
188BOOST_FIXTURE_TEST_CASE(Triple, Fixture)
189{
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700190 SegmentFetcher::fetch(*face, Interest("/hello/world", time::seconds(1000)),
191 DontVerifySegment(),
192 bind(&Fixture::onData, this, _1),
193 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 face->receive(*makeData("/hello/world/version0", 0, false));
197
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800198 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700199 face->receive(*makeData("/hello/world/version0", 1, false));
200
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800201 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700202 face->receive(*makeData("/hello/world/version0", 2, true));
203
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, 0);
207 BOOST_CHECK_EQUAL(nDatas, 1);
208
209 BOOST_CHECK_EQUAL(dataSize, 42);
210
Junxiao Shia60d9362014-11-12 09:38:21 -0700211 BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 3);
212 BOOST_CHECK_EQUAL(face->sentDatas.size(), 0);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700213
214 {
Junxiao Shia60d9362014-11-12 09:38:21 -0700215 const Interest& interest = face->sentInterests[0];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700216 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world");
217 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true);
218 BOOST_CHECK_EQUAL(interest.getChildSelector(), 1);
219 }
220
221 {
Junxiao Shia60d9362014-11-12 09:38:21 -0700222 const Interest& interest = face->sentInterests[1];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700223 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%01");
224 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false);
225 BOOST_CHECK_EQUAL(interest.getChildSelector(), 0);
226 }
227
228 {
Junxiao Shia60d9362014-11-12 09:38:21 -0700229 const Interest& interest = face->sentInterests[2];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700230 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%02");
231 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false);
232 BOOST_CHECK_EQUAL(interest.getChildSelector(), 0);
233 }
234}
235
236BOOST_FIXTURE_TEST_CASE(TripleWithInitialSegmentFetching, Fixture)
237{
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700238 SegmentFetcher::fetch(*face, Interest("/hello/world", time::seconds(1000)),
239 DontVerifySegment(),
240 bind(&Fixture::onData, this, _1),
241 bind(&Fixture::onError, this, _1));
242
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800243 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700244 face->receive(*makeData("/hello/world/version0", 1, false));
245
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800246 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700247 face->receive(*makeData("/hello/world/version0", 0, false));
248
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800249 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700250 face->receive(*makeData("/hello/world/version0", 1, false));
251
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800252 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700253 face->receive(*makeData("/hello/world/version0", 2, true));
254
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800255 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700256
257 BOOST_CHECK_EQUAL(nErrors, 0);
258 BOOST_CHECK_EQUAL(nDatas, 1);
259
260 BOOST_CHECK_EQUAL(dataSize, 42);
261
Junxiao Shia60d9362014-11-12 09:38:21 -0700262 BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 4);
263 BOOST_CHECK_EQUAL(face->sentDatas.size(), 0);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700264
265 {
Junxiao Shia60d9362014-11-12 09:38:21 -0700266 const Interest& interest = face->sentInterests[0];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700267 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world");
268 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true);
269 BOOST_CHECK_EQUAL(interest.getChildSelector(), 1);
270 }
271
272 {
Junxiao Shia60d9362014-11-12 09:38:21 -0700273 const Interest& interest = face->sentInterests[1];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700274 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%00");
275 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false);
276 BOOST_CHECK_EQUAL(interest.getChildSelector(), 0);
277 }
278
279 {
Junxiao Shia60d9362014-11-12 09:38:21 -0700280 const Interest& interest = face->sentInterests[2];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700281 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%01");
282 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false);
283 BOOST_CHECK_EQUAL(interest.getChildSelector(), 0);
284 }
285
286 {
Junxiao Shia60d9362014-11-12 09:38:21 -0700287 const Interest& interest = face->sentInterests[3];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700288 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%02");
289 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false);
290 BOOST_CHECK_EQUAL(interest.getChildSelector(), 0);
291 }
292}
293
294
295BOOST_AUTO_TEST_SUITE_END()
296
297} // namespace tests
298} // namespace util
299} // namespace ndn