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" |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 23 | #include "security/validator-null.hpp" |
| 24 | #include "security/validator.hpp" |
| 25 | #include "data.hpp" |
| 26 | #include "encoding/block.hpp" |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 27 | |
| 28 | #include "boost-test.hpp" |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 29 | #include "util/dummy-client-face.hpp" |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 30 | #include "security/key-chain.hpp" |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 31 | #include "lp/nack-header.hpp" |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 32 | #include "../unit-test-time-fixture.hpp" |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 33 | #include "../make-interest-data.hpp" |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 34 | |
| 35 | namespace ndn { |
| 36 | namespace util { |
| 37 | namespace tests { |
| 38 | |
| 39 | BOOST_AUTO_TEST_SUITE(UtilSegmentFetcher) |
| 40 | |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 41 | class ValidatorFailed : public Validator |
| 42 | { |
| 43 | protected: |
| 44 | virtual void |
| 45 | checkPolicy(const Data& data, |
| 46 | int nSteps, |
| 47 | const OnDataValidated& onValidated, |
| 48 | const OnDataValidationFailed& onValidationFailed, |
| 49 | std::vector<shared_ptr<ValidationRequest>>& nextSteps) |
| 50 | { |
| 51 | onValidationFailed(data.shared_from_this(), "Data validation failed."); |
| 52 | } |
| 53 | |
| 54 | virtual void |
| 55 | checkPolicy(const Interest& interest, |
| 56 | int nSteps, |
| 57 | const OnInterestValidated& onValidated, |
| 58 | const OnInterestValidationFailed& onValidationFailed, |
| 59 | std::vector<shared_ptr<ValidationRequest>>& nextSteps) |
| 60 | { |
| 61 | onValidationFailed(interest.shared_from_this(), "Interest validation failed."); |
| 62 | } |
| 63 | }; |
| 64 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 65 | class Fixture : public ndn::tests::UnitTestTimeFixture |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 66 | { |
| 67 | public: |
| 68 | Fixture() |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 69 | : face(makeDummyClientFace(io)) |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 70 | , nErrors(0) |
| 71 | , nDatas(0) |
| 72 | , dataSize(0) |
| 73 | { |
| 74 | } |
| 75 | |
| 76 | shared_ptr<Data> |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 77 | makeDataSegment(const Name& baseName, uint64_t segment, bool isFinal) |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 78 | { |
| 79 | const uint8_t buffer[] = "Hello, world!"; |
| 80 | |
| 81 | shared_ptr<Data> data = make_shared<Data>(Name(baseName).appendSegment(segment)); |
| 82 | data->setContent(buffer, sizeof(buffer)); |
| 83 | |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 84 | if (isFinal) { |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 85 | data->setFinalBlockId(data->getName()[-1]); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 86 | } |
| 87 | data = signData(data); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 88 | |
| 89 | return data; |
| 90 | } |
| 91 | |
| 92 | void |
| 93 | onError(uint32_t errorCode) |
| 94 | { |
| 95 | ++nErrors; |
| 96 | lastError = errorCode; |
| 97 | } |
| 98 | |
| 99 | void |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 100 | onComplete(const ConstBufferPtr& data) |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 101 | { |
| 102 | ++nDatas; |
| 103 | dataSize = data->size(); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 104 | dataString = std::string(reinterpret_cast<const char*>(data->get())); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 107 | void |
| 108 | nackLastInterest(lp::NackReason nackReason) |
| 109 | { |
| 110 | const Interest& lastInterest = face->sentInterests.back(); |
| 111 | lp::Nack nack = makeNack(lastInterest, nackReason); |
| 112 | face->receive(nack); |
| 113 | advanceClocks(time::milliseconds(1), 10); |
| 114 | } |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 115 | |
| 116 | public: |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 117 | shared_ptr<DummyClientFace> face; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 118 | KeyChain keyChain; |
| 119 | |
| 120 | uint32_t nErrors; |
| 121 | uint32_t lastError; |
| 122 | uint32_t nDatas; |
| 123 | size_t dataSize; |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 124 | std::string dataString; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | BOOST_FIXTURE_TEST_CASE(Timeout, Fixture) |
| 128 | { |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 129 | ValidatorNull nullValidator; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 130 | SegmentFetcher::fetch(*face, Interest("/hello/world", time::milliseconds(100)), |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 131 | nullValidator, |
| 132 | bind(&Fixture::onComplete, this, _1), |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 133 | bind(&Fixture::onError, this, _1)); |
| 134 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 135 | advanceClocks(time::milliseconds(1), 99); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 136 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 137 | BOOST_CHECK_EQUAL(nErrors, 0); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 138 | BOOST_CHECK_EQUAL(nDatas, 0); |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 139 | BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 1); |
| 140 | BOOST_CHECK_EQUAL(face->sentDatas.size(), 0); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 141 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 142 | const Interest& interest = face->sentInterests[0]; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 143 | BOOST_CHECK_EQUAL(interest.getName(), "/hello/world"); |
| 144 | BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true); |
| 145 | BOOST_CHECK_EQUAL(interest.getChildSelector(), 1); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 146 | |
| 147 | advanceClocks(time::milliseconds(1), 2); |
| 148 | |
| 149 | BOOST_CHECK_EQUAL(nErrors, 1); |
| 150 | BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::INTEREST_TIMEOUT)); |
| 151 | BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 1); |
| 152 | BOOST_CHECK_EQUAL(face->sentDatas.size(), 0); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | |
| 156 | BOOST_FIXTURE_TEST_CASE(Basic, Fixture) |
| 157 | { |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 158 | ValidatorNull nullValidator; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 159 | SegmentFetcher::fetch(*face, Interest("/hello/world", time::seconds(1000)), |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 160 | nullValidator, |
| 161 | bind(&Fixture::onComplete, this, _1), |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 162 | bind(&Fixture::onError, this, _1)); |
| 163 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 164 | advanceClocks(time::milliseconds(1), 10); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 165 | |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 166 | face->receive(*makeDataSegment("/hello/world/version0", 0, true)); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 167 | advanceClocks(time::milliseconds(1), 10); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 168 | |
| 169 | BOOST_CHECK_EQUAL(nErrors, 0); |
| 170 | BOOST_CHECK_EQUAL(nDatas, 1); |
| 171 | |
| 172 | BOOST_CHECK_EQUAL(dataSize, 14); |
| 173 | |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 174 | const uint8_t buffer[] = "Hello, world!"; |
| 175 | std::string bufferString = std::string(reinterpret_cast<const char*>(buffer)); |
| 176 | |
| 177 | BOOST_CHECK_EQUAL(dataString, bufferString); |
| 178 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 179 | BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 1); |
| 180 | BOOST_CHECK_EQUAL(face->sentDatas.size(), 0); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 181 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 182 | const Interest& interest = face->sentInterests[0]; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 183 | BOOST_CHECK_EQUAL(interest.getName(), "/hello/world"); |
| 184 | BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true); |
| 185 | BOOST_CHECK_EQUAL(interest.getChildSelector(), 1); |
| 186 | } |
| 187 | |
| 188 | BOOST_FIXTURE_TEST_CASE(NoSegmentInData, Fixture) |
| 189 | { |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 190 | ValidatorNull nullValidator; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 191 | SegmentFetcher::fetch(*face, Interest("/hello/world", time::seconds(1000)), |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 192 | nullValidator, |
| 193 | bind(&Fixture::onComplete, this, _1), |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 194 | bind(&Fixture::onError, this, _1)); |
| 195 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 196 | advanceClocks(time::milliseconds(1), 10); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 197 | |
| 198 | const uint8_t buffer[] = "Hello, world!"; |
| 199 | |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 200 | shared_ptr<Data> data = makeData("/hello/world/version0/no-segment"); |
| 201 | |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 202 | data->setContent(buffer, sizeof(buffer)); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 203 | |
| 204 | face->receive(*data); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 205 | advanceClocks(time::milliseconds(1), 10); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 206 | |
| 207 | BOOST_CHECK_EQUAL(nErrors, 1); |
| 208 | BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::DATA_HAS_NO_SEGMENT)); |
| 209 | BOOST_CHECK_EQUAL(nDatas, 0); |
| 210 | } |
| 211 | |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 212 | BOOST_FIXTURE_TEST_CASE(SegmentValidationFailure, Fixture) |
| 213 | { |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 214 | ValidatorFailed failedValidator; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 215 | SegmentFetcher::fetch(*face, Interest("/hello/world", time::seconds(1000)), |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 216 | failedValidator, |
| 217 | bind(&Fixture::onComplete, this, _1), |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 218 | bind(&Fixture::onError, this, _1)); |
| 219 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 220 | advanceClocks(time::milliseconds(1), 10); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 221 | face->receive(*makeDataSegment("/hello/world/version0", 0, true)); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 222 | advanceClocks(time::milliseconds(1), 10); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 223 | |
| 224 | BOOST_CHECK_EQUAL(nErrors, 1); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 225 | BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::SEGMENT_VALIDATION_FAIL)); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 226 | BOOST_CHECK_EQUAL(nDatas, 0); |
| 227 | } |
| 228 | |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 229 | BOOST_FIXTURE_TEST_CASE(Triple, Fixture) |
| 230 | { |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 231 | ValidatorNull nullValidator; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 232 | SegmentFetcher::fetch(*face, Interest("/hello/world", time::seconds(1000)), |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 233 | nullValidator, |
| 234 | bind(&Fixture::onComplete, this, _1), |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 235 | bind(&Fixture::onError, this, _1)); |
| 236 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 237 | advanceClocks(time::milliseconds(1), 10); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 238 | face->receive(*makeDataSegment("/hello/world/version0", 0, false)); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 239 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 240 | advanceClocks(time::milliseconds(1), 10); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 241 | face->receive(*makeDataSegment("/hello/world/version0", 1, false)); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 242 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 243 | advanceClocks(time::milliseconds(1), 10); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 244 | face->receive(*makeDataSegment("/hello/world/version0", 2, true)); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 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 | |
| 248 | BOOST_CHECK_EQUAL(nErrors, 0); |
| 249 | BOOST_CHECK_EQUAL(nDatas, 1); |
| 250 | |
| 251 | BOOST_CHECK_EQUAL(dataSize, 42); |
| 252 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 253 | BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 3); |
| 254 | BOOST_CHECK_EQUAL(face->sentDatas.size(), 0); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 255 | |
| 256 | { |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 257 | const Interest& interest = face->sentInterests[0]; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 258 | BOOST_CHECK_EQUAL(interest.getName(), "/hello/world"); |
| 259 | BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true); |
| 260 | BOOST_CHECK_EQUAL(interest.getChildSelector(), 1); |
| 261 | } |
| 262 | |
| 263 | { |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 264 | const Interest& interest = face->sentInterests[1]; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 265 | BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%01"); |
| 266 | BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false); |
| 267 | BOOST_CHECK_EQUAL(interest.getChildSelector(), 0); |
| 268 | } |
| 269 | |
| 270 | { |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 271 | const Interest& interest = face->sentInterests[2]; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 272 | BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%02"); |
| 273 | BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false); |
| 274 | BOOST_CHECK_EQUAL(interest.getChildSelector(), 0); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | BOOST_FIXTURE_TEST_CASE(TripleWithInitialSegmentFetching, Fixture) |
| 279 | { |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 280 | ValidatorNull nullValidator; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 281 | SegmentFetcher::fetch(*face, Interest("/hello/world", time::seconds(1000)), |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 282 | nullValidator, |
| 283 | bind(&Fixture::onComplete, this, _1), |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 284 | bind(&Fixture::onError, this, _1)); |
| 285 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 286 | advanceClocks(time::milliseconds(1), 10); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 287 | face->receive(*makeDataSegment("/hello/world/version0", 1, false)); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 288 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 289 | advanceClocks(time::milliseconds(1), 10); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 290 | face->receive(*makeDataSegment("/hello/world/version0", 0, false)); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 291 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 292 | advanceClocks(time::milliseconds(1), 10); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 293 | face->receive(*makeDataSegment("/hello/world/version0", 1, false)); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 294 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 295 | advanceClocks(time::milliseconds(1), 10); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 296 | face->receive(*makeDataSegment("/hello/world/version0", 2, true)); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 297 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 298 | advanceClocks(time::milliseconds(1), 10); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 299 | |
| 300 | BOOST_CHECK_EQUAL(nErrors, 0); |
| 301 | BOOST_CHECK_EQUAL(nDatas, 1); |
| 302 | |
| 303 | BOOST_CHECK_EQUAL(dataSize, 42); |
| 304 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 305 | BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 4); |
| 306 | BOOST_CHECK_EQUAL(face->sentDatas.size(), 0); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 307 | |
| 308 | { |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 309 | const Interest& interest = face->sentInterests[0]; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 310 | BOOST_CHECK_EQUAL(interest.getName(), "/hello/world"); |
| 311 | BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true); |
| 312 | BOOST_CHECK_EQUAL(interest.getChildSelector(), 1); |
| 313 | } |
| 314 | |
| 315 | { |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 316 | const Interest& interest = face->sentInterests[1]; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 317 | BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%00"); |
| 318 | BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false); |
| 319 | BOOST_CHECK_EQUAL(interest.getChildSelector(), 0); |
| 320 | } |
| 321 | |
| 322 | { |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 323 | const Interest& interest = face->sentInterests[2]; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 324 | BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%01"); |
| 325 | BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false); |
| 326 | BOOST_CHECK_EQUAL(interest.getChildSelector(), 0); |
| 327 | } |
| 328 | |
| 329 | { |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 330 | const Interest& interest = face->sentInterests[3]; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 331 | BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%02"); |
| 332 | BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false); |
| 333 | BOOST_CHECK_EQUAL(interest.getChildSelector(), 0); |
| 334 | } |
| 335 | } |
| 336 | |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 337 | BOOST_FIXTURE_TEST_CASE(MultipleSegmentFetching, Fixture) |
| 338 | { |
| 339 | ValidatorNull nullValidator; |
| 340 | SegmentFetcher::fetch(*face, Interest("/hello/world", time::seconds(1000)), |
| 341 | nullValidator, |
| 342 | bind(&Fixture::onComplete, this, _1), |
| 343 | bind(&Fixture::onError, this, _1)); |
| 344 | |
| 345 | advanceClocks(time::milliseconds(1), 10); |
| 346 | |
| 347 | for (uint64_t i = 0; i < 400; i++) { |
| 348 | advanceClocks(time::milliseconds(1), 10); |
| 349 | face->receive(*makeDataSegment("/hello/world/version0", i, false)); |
| 350 | } |
| 351 | advanceClocks(time::milliseconds(1), 10); |
| 352 | face->receive(*makeDataSegment("/hello/world/version0", 400, true)); |
| 353 | |
| 354 | advanceClocks(time::milliseconds(1), 10); |
| 355 | |
| 356 | BOOST_CHECK_EQUAL(nErrors, 0); |
| 357 | BOOST_CHECK_EQUAL(nDatas, 1); |
| 358 | } |
| 359 | |
| 360 | BOOST_FIXTURE_TEST_CASE(SegmentFetcherDuplicateNack, Fixture) |
| 361 | { |
| 362 | SegmentFetcher::fetch(*face, Interest("/hello/world", time::seconds(1000)), |
| 363 | make_shared<ValidatorNull>(), |
| 364 | bind(&Fixture::onComplete, this, _1), |
| 365 | bind(&Fixture::onError, this, _1)); |
| 366 | advanceClocks(time::milliseconds(1), 10); |
| 367 | |
| 368 | // receive nack for the original interest |
| 369 | nackLastInterest(lp::NackReason::DUPLICATE); |
| 370 | |
| 371 | // receive nack due to Duplication for the reexpressed interests |
| 372 | for (uint32_t i = 1; i <= SegmentFetcher::MAX_INTEREST_REEXPRESS; ++i) { |
| 373 | nackLastInterest(lp::NackReason::DUPLICATE); |
| 374 | } |
| 375 | |
| 376 | BOOST_CHECK_EQUAL(face->sentInterests.size(), (SegmentFetcher::MAX_INTEREST_REEXPRESS + 1)); |
| 377 | BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::NACK_ERROR)); |
| 378 | } |
| 379 | |
| 380 | BOOST_FIXTURE_TEST_CASE(SegmentFetcherCongestionNack, Fixture) |
| 381 | { |
| 382 | SegmentFetcher::fetch(*face, Interest("/hello/world", time::seconds(1000)), |
| 383 | make_shared<ValidatorNull>(), |
| 384 | bind(&Fixture::onComplete, this, _1), |
| 385 | bind(&Fixture::onError, this, _1)); |
| 386 | advanceClocks(time::milliseconds(1), 10); |
| 387 | |
| 388 | // receive nack for the original interest |
| 389 | nackLastInterest(lp::NackReason::CONGESTION); |
| 390 | |
| 391 | // receive nack due to Congestion for the reexpressed interests |
| 392 | for (uint32_t i = 1; i <= SegmentFetcher::MAX_INTEREST_REEXPRESS; ++i) { |
| 393 | nackLastInterest(lp::NackReason::CONGESTION); |
| 394 | } |
| 395 | |
| 396 | BOOST_CHECK_EQUAL(face->sentInterests.size(), (SegmentFetcher::MAX_INTEREST_REEXPRESS + 1)); |
| 397 | BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::NACK_ERROR)); |
| 398 | } |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 399 | |
| 400 | BOOST_AUTO_TEST_SUITE_END() |
| 401 | |
| 402 | } // namespace tests |
| 403 | } // namespace util |
| 404 | } // namespace ndn |