blob: 15d7831cb92b27faa0a652ab00ee67fbb99dba54 [file] [log] [blame]
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2014 Regents of the University of California.
4 *
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{
190 KeyChain keyChain;
191
192 SegmentFetcher::fetch(*face, Interest("/hello/world", time::seconds(1000)),
193 DontVerifySegment(),
194 bind(&Fixture::onData, this, _1),
195 bind(&Fixture::onError, this, _1));
196
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800197 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700198 face->receive(*makeData("/hello/world/version0", 0, false));
199
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800200 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700201 face->receive(*makeData("/hello/world/version0", 1, false));
202
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800203 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700204 face->receive(*makeData("/hello/world/version0", 2, true));
205
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800206 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700207
208 BOOST_CHECK_EQUAL(nErrors, 0);
209 BOOST_CHECK_EQUAL(nDatas, 1);
210
211 BOOST_CHECK_EQUAL(dataSize, 42);
212
Junxiao Shia60d9362014-11-12 09:38:21 -0700213 BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 3);
214 BOOST_CHECK_EQUAL(face->sentDatas.size(), 0);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700215
216 {
Junxiao Shia60d9362014-11-12 09:38:21 -0700217 const Interest& interest = face->sentInterests[0];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700218 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world");
219 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true);
220 BOOST_CHECK_EQUAL(interest.getChildSelector(), 1);
221 }
222
223 {
Junxiao Shia60d9362014-11-12 09:38:21 -0700224 const Interest& interest = face->sentInterests[1];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700225 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%01");
226 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false);
227 BOOST_CHECK_EQUAL(interest.getChildSelector(), 0);
228 }
229
230 {
Junxiao Shia60d9362014-11-12 09:38:21 -0700231 const Interest& interest = face->sentInterests[2];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700232 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%02");
233 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false);
234 BOOST_CHECK_EQUAL(interest.getChildSelector(), 0);
235 }
236}
237
238BOOST_FIXTURE_TEST_CASE(TripleWithInitialSegmentFetching, Fixture)
239{
240 KeyChain keyChain;
241
242 SegmentFetcher::fetch(*face, Interest("/hello/world", time::seconds(1000)),
243 DontVerifySegment(),
244 bind(&Fixture::onData, this, _1),
245 bind(&Fixture::onError, this, _1));
246
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800247 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700248 face->receive(*makeData("/hello/world/version0", 1, false));
249
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800250 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700251 face->receive(*makeData("/hello/world/version0", 0, false));
252
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800253 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700254 face->receive(*makeData("/hello/world/version0", 1, false));
255
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800256 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700257 face->receive(*makeData("/hello/world/version0", 2, true));
258
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800259 advanceClocks(time::milliseconds(1), 10);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700260
261 BOOST_CHECK_EQUAL(nErrors, 0);
262 BOOST_CHECK_EQUAL(nDatas, 1);
263
264 BOOST_CHECK_EQUAL(dataSize, 42);
265
Junxiao Shia60d9362014-11-12 09:38:21 -0700266 BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 4);
267 BOOST_CHECK_EQUAL(face->sentDatas.size(), 0);
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700268
269 {
Junxiao Shia60d9362014-11-12 09:38:21 -0700270 const Interest& interest = face->sentInterests[0];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700271 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world");
272 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true);
273 BOOST_CHECK_EQUAL(interest.getChildSelector(), 1);
274 }
275
276 {
Junxiao Shia60d9362014-11-12 09:38:21 -0700277 const Interest& interest = face->sentInterests[1];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700278 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%00");
279 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false);
280 BOOST_CHECK_EQUAL(interest.getChildSelector(), 0);
281 }
282
283 {
Junxiao Shia60d9362014-11-12 09:38:21 -0700284 const Interest& interest = face->sentInterests[2];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700285 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%01");
286 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false);
287 BOOST_CHECK_EQUAL(interest.getChildSelector(), 0);
288 }
289
290 {
Junxiao Shia60d9362014-11-12 09:38:21 -0700291 const Interest& interest = face->sentInterests[3];
Alexander Afanasyevf3cfab52014-08-17 22:15:25 -0700292 BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%02");
293 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false);
294 BOOST_CHECK_EQUAL(interest.getChildSelector(), 0);
295 }
296}
297
298
299BOOST_AUTO_TEST_SUITE_END()
300
301} // namespace tests
302} // namespace util
303} // namespace ndn