Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | af99f46 | 2015-01-19 21:43:09 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2015 Regents of the University of California. |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 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 Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 25 | #include "util/dummy-client-face.hpp" |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 26 | #include "security/key-chain.hpp" |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 27 | #include "../unit-test-time-fixture.hpp" |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 28 | |
| 29 | namespace ndn { |
| 30 | namespace util { |
| 31 | namespace tests { |
| 32 | |
| 33 | BOOST_AUTO_TEST_SUITE(UtilSegmentFetcher) |
| 34 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 35 | class Fixture : public ndn::tests::UnitTestTimeFixture |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 36 | { |
| 37 | public: |
| 38 | Fixture() |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 39 | : face(makeDummyClientFace(io)) |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 40 | , 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 | |
| 76 | public: |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 77 | shared_ptr<DummyClientFace> face; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 78 | KeyChain keyChain; |
| 79 | |
| 80 | uint32_t nErrors; |
| 81 | uint32_t lastError; |
| 82 | uint32_t nDatas; |
| 83 | size_t dataSize; |
| 84 | }; |
| 85 | |
| 86 | BOOST_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 Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 93 | advanceClocks(time::milliseconds(1), 99); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 94 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 95 | BOOST_CHECK_EQUAL(nErrors, 0); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 96 | BOOST_CHECK_EQUAL(nDatas, 0); |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 97 | BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 1); |
| 98 | BOOST_CHECK_EQUAL(face->sentDatas.size(), 0); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 99 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 100 | const Interest& interest = face->sentInterests[0]; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 101 | BOOST_CHECK_EQUAL(interest.getName(), "/hello/world"); |
| 102 | BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true); |
| 103 | BOOST_CHECK_EQUAL(interest.getChildSelector(), 1); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 104 | |
| 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 Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | |
| 114 | BOOST_FIXTURE_TEST_CASE(Basic, Fixture) |
| 115 | { |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 116 | 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 Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 121 | advanceClocks(time::milliseconds(1), 10); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 122 | |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 123 | face->receive(*makeData("/hello/world/version0", 0, true)); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 124 | advanceClocks(time::milliseconds(1), 10); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 125 | |
| 126 | BOOST_CHECK_EQUAL(nErrors, 0); |
| 127 | BOOST_CHECK_EQUAL(nDatas, 1); |
| 128 | |
| 129 | BOOST_CHECK_EQUAL(dataSize, 14); |
| 130 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 131 | BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 1); |
| 132 | BOOST_CHECK_EQUAL(face->sentDatas.size(), 0); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 133 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 134 | const Interest& interest = face->sentInterests[0]; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 135 | BOOST_CHECK_EQUAL(interest.getName(), "/hello/world"); |
| 136 | BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true); |
| 137 | BOOST_CHECK_EQUAL(interest.getChildSelector(), 1); |
| 138 | } |
| 139 | |
| 140 | BOOST_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 Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 148 | advanceClocks(time::milliseconds(1), 10); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 149 | |
| 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 Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 157 | advanceClocks(time::milliseconds(1), 10); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 158 | |
| 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 | |
| 164 | bool |
| 165 | failValidation(const Data& data) |
| 166 | { |
| 167 | return false; |
| 168 | } |
| 169 | |
| 170 | BOOST_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 Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 178 | advanceClocks(time::milliseconds(1), 10); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 179 | face->receive(*makeData("/hello/world/version0", 0, true)); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 180 | advanceClocks(time::milliseconds(1), 10); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 181 | |
| 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 | |
| 188 | BOOST_FIXTURE_TEST_CASE(Triple, Fixture) |
| 189 | { |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 190 | 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 Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 195 | advanceClocks(time::milliseconds(1), 10); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 196 | face->receive(*makeData("/hello/world/version0", 0, false)); |
| 197 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 198 | advanceClocks(time::milliseconds(1), 10); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 199 | face->receive(*makeData("/hello/world/version0", 1, false)); |
| 200 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 201 | advanceClocks(time::milliseconds(1), 10); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 202 | face->receive(*makeData("/hello/world/version0", 2, true)); |
| 203 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 204 | advanceClocks(time::milliseconds(1), 10); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 205 | |
| 206 | BOOST_CHECK_EQUAL(nErrors, 0); |
| 207 | BOOST_CHECK_EQUAL(nDatas, 1); |
| 208 | |
| 209 | BOOST_CHECK_EQUAL(dataSize, 42); |
| 210 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 211 | BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 3); |
| 212 | BOOST_CHECK_EQUAL(face->sentDatas.size(), 0); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 213 | |
| 214 | { |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 215 | const Interest& interest = face->sentInterests[0]; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 216 | 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 Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 222 | const Interest& interest = face->sentInterests[1]; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 223 | 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 Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 229 | const Interest& interest = face->sentInterests[2]; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 230 | 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 | |
| 236 | BOOST_FIXTURE_TEST_CASE(TripleWithInitialSegmentFetching, Fixture) |
| 237 | { |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 238 | 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 Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 243 | advanceClocks(time::milliseconds(1), 10); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 244 | face->receive(*makeData("/hello/world/version0", 1, false)); |
| 245 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 246 | advanceClocks(time::milliseconds(1), 10); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 247 | face->receive(*makeData("/hello/world/version0", 0, false)); |
| 248 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 249 | advanceClocks(time::milliseconds(1), 10); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 250 | face->receive(*makeData("/hello/world/version0", 1, false)); |
| 251 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 252 | advanceClocks(time::milliseconds(1), 10); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 253 | face->receive(*makeData("/hello/world/version0", 2, true)); |
| 254 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 255 | advanceClocks(time::milliseconds(1), 10); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 256 | |
| 257 | BOOST_CHECK_EQUAL(nErrors, 0); |
| 258 | BOOST_CHECK_EQUAL(nDatas, 1); |
| 259 | |
| 260 | BOOST_CHECK_EQUAL(dataSize, 42); |
| 261 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 262 | BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 4); |
| 263 | BOOST_CHECK_EQUAL(face->sentDatas.size(), 0); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 264 | |
| 265 | { |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 266 | const Interest& interest = face->sentInterests[0]; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 267 | 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 Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 273 | const Interest& interest = face->sentInterests[1]; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 274 | 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 Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 280 | const Interest& interest = face->sentInterests[2]; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 281 | 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 Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 287 | const Interest& interest = face->sentInterests[3]; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 288 | 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 | |
| 295 | BOOST_AUTO_TEST_SUITE_END() |
| 296 | |
| 297 | } // namespace tests |
| 298 | } // namespace util |
| 299 | } // namespace ndn |