Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Muktadir R Chowdhury | 2bc2df0 | 2016-04-05 16:55:41 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2016 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 | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame^] | 32 | #include "../identity-management-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 | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame^] | 65 | class Fixture : public ndn::tests::IdentityManagementTimeFixture |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 66 | { |
| 67 | public: |
| 68 | Fixture() |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame^] | 69 | : face(io, m_keyChain) |
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 | { |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 110 | const Interest& lastInterest = face.sentInterests.back(); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 111 | lp::Nack nack = makeNack(lastInterest, nackReason); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 112 | face.receive(nack); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 113 | advanceClocks(time::milliseconds(1), 10); |
| 114 | } |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 115 | |
| 116 | public: |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 117 | DummyClientFace face; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 118 | |
| 119 | uint32_t nErrors; |
| 120 | uint32_t lastError; |
| 121 | uint32_t nDatas; |
| 122 | size_t dataSize; |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 123 | std::string dataString; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | BOOST_FIXTURE_TEST_CASE(Timeout, Fixture) |
| 127 | { |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 128 | ValidatorNull nullValidator; |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 129 | SegmentFetcher::fetch(face, Interest("/hello/world", time::milliseconds(100)), |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 130 | nullValidator, |
| 131 | bind(&Fixture::onComplete, this, _1), |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 132 | bind(&Fixture::onError, this, _1)); |
| 133 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 134 | advanceClocks(time::milliseconds(1), 99); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 135 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 136 | BOOST_CHECK_EQUAL(nErrors, 0); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 137 | BOOST_CHECK_EQUAL(nDatas, 0); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 138 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1); |
| 139 | BOOST_CHECK_EQUAL(face.sentData.size(), 0); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 140 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 141 | const Interest& interest = face.sentInterests[0]; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 142 | BOOST_CHECK_EQUAL(interest.getName(), "/hello/world"); |
| 143 | BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true); |
| 144 | BOOST_CHECK_EQUAL(interest.getChildSelector(), 1); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 145 | |
| 146 | advanceClocks(time::milliseconds(1), 2); |
| 147 | |
| 148 | BOOST_CHECK_EQUAL(nErrors, 1); |
| 149 | BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::INTEREST_TIMEOUT)); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 150 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1); |
| 151 | BOOST_CHECK_EQUAL(face.sentData.size(), 0); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | |
| 155 | BOOST_FIXTURE_TEST_CASE(Basic, Fixture) |
| 156 | { |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 157 | ValidatorNull nullValidator; |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 158 | SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)), |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 159 | nullValidator, |
| 160 | bind(&Fixture::onComplete, this, _1), |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 161 | bind(&Fixture::onError, this, _1)); |
| 162 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 163 | advanceClocks(time::milliseconds(1), 10); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 164 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 165 | face.receive(*makeDataSegment("/hello/world/version0", 0, true)); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 166 | advanceClocks(time::milliseconds(1), 10); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 167 | |
| 168 | BOOST_CHECK_EQUAL(nErrors, 0); |
| 169 | BOOST_CHECK_EQUAL(nDatas, 1); |
| 170 | |
| 171 | BOOST_CHECK_EQUAL(dataSize, 14); |
| 172 | |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 173 | const uint8_t buffer[] = "Hello, world!"; |
| 174 | std::string bufferString = std::string(reinterpret_cast<const char*>(buffer)); |
| 175 | |
| 176 | BOOST_CHECK_EQUAL(dataString, bufferString); |
| 177 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 178 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1); |
| 179 | BOOST_CHECK_EQUAL(face.sentData.size(), 0); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 180 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 181 | const Interest& interest = face.sentInterests[0]; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 182 | BOOST_CHECK_EQUAL(interest.getName(), "/hello/world"); |
| 183 | BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true); |
| 184 | BOOST_CHECK_EQUAL(interest.getChildSelector(), 1); |
| 185 | } |
| 186 | |
| 187 | BOOST_FIXTURE_TEST_CASE(NoSegmentInData, Fixture) |
| 188 | { |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 189 | ValidatorNull nullValidator; |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 190 | SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)), |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 191 | nullValidator, |
| 192 | bind(&Fixture::onComplete, this, _1), |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 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 | |
| 197 | const uint8_t buffer[] = "Hello, world!"; |
| 198 | |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 199 | shared_ptr<Data> data = makeData("/hello/world/version0/no-segment"); |
| 200 | |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 201 | data->setContent(buffer, sizeof(buffer)); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 202 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 203 | face.receive(*data); |
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, 1); |
| 207 | BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::DATA_HAS_NO_SEGMENT)); |
| 208 | BOOST_CHECK_EQUAL(nDatas, 0); |
| 209 | } |
| 210 | |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 211 | BOOST_FIXTURE_TEST_CASE(SegmentValidationFailure, Fixture) |
| 212 | { |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 213 | ValidatorFailed failedValidator; |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 214 | SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)), |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 215 | failedValidator, |
| 216 | bind(&Fixture::onComplete, this, _1), |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 217 | bind(&Fixture::onError, this, _1)); |
| 218 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 219 | advanceClocks(time::milliseconds(1), 10); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 220 | face.receive(*makeDataSegment("/hello/world/version0", 0, true)); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 221 | advanceClocks(time::milliseconds(1), 10); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 222 | |
| 223 | BOOST_CHECK_EQUAL(nErrors, 1); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 224 | BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::SEGMENT_VALIDATION_FAIL)); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 225 | BOOST_CHECK_EQUAL(nDatas, 0); |
| 226 | } |
| 227 | |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 228 | BOOST_FIXTURE_TEST_CASE(Triple, Fixture) |
| 229 | { |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 230 | ValidatorNull nullValidator; |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 231 | SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)), |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 232 | nullValidator, |
| 233 | bind(&Fixture::onComplete, this, _1), |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 234 | bind(&Fixture::onError, this, _1)); |
| 235 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 236 | advanceClocks(time::milliseconds(1), 10); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 237 | face.receive(*makeDataSegment("/hello/world/version0", 0, false)); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 238 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 239 | advanceClocks(time::milliseconds(1), 10); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 240 | face.receive(*makeDataSegment("/hello/world/version0", 1, false)); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 241 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 242 | advanceClocks(time::milliseconds(1), 10); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 243 | face.receive(*makeDataSegment("/hello/world/version0", 2, true)); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 244 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 245 | advanceClocks(time::milliseconds(1), 10); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 246 | |
| 247 | BOOST_CHECK_EQUAL(nErrors, 0); |
| 248 | BOOST_CHECK_EQUAL(nDatas, 1); |
| 249 | |
| 250 | BOOST_CHECK_EQUAL(dataSize, 42); |
| 251 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 252 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 3); |
| 253 | BOOST_CHECK_EQUAL(face.sentData.size(), 0); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 254 | |
| 255 | { |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 256 | const Interest& interest = face.sentInterests[0]; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 257 | BOOST_CHECK_EQUAL(interest.getName(), "/hello/world"); |
| 258 | BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true); |
| 259 | BOOST_CHECK_EQUAL(interest.getChildSelector(), 1); |
| 260 | } |
| 261 | |
| 262 | { |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 263 | const Interest& interest = face.sentInterests[1]; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 264 | BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%01"); |
| 265 | BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false); |
| 266 | BOOST_CHECK_EQUAL(interest.getChildSelector(), 0); |
| 267 | } |
| 268 | |
| 269 | { |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 270 | const Interest& interest = face.sentInterests[2]; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 271 | BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%02"); |
| 272 | BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false); |
| 273 | BOOST_CHECK_EQUAL(interest.getChildSelector(), 0); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | BOOST_FIXTURE_TEST_CASE(TripleWithInitialSegmentFetching, Fixture) |
| 278 | { |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 279 | ValidatorNull nullValidator; |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 280 | SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)), |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 281 | nullValidator, |
| 282 | bind(&Fixture::onComplete, this, _1), |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 283 | bind(&Fixture::onError, this, _1)); |
| 284 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 285 | advanceClocks(time::milliseconds(1), 10); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 286 | face.receive(*makeDataSegment("/hello/world/version0", 1, false)); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 287 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 288 | advanceClocks(time::milliseconds(1), 10); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 289 | face.receive(*makeDataSegment("/hello/world/version0", 0, false)); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 290 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 291 | advanceClocks(time::milliseconds(1), 10); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 292 | face.receive(*makeDataSegment("/hello/world/version0", 1, false)); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 293 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 294 | advanceClocks(time::milliseconds(1), 10); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 295 | face.receive(*makeDataSegment("/hello/world/version0", 2, true)); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 296 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 297 | advanceClocks(time::milliseconds(1), 10); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 298 | |
| 299 | BOOST_CHECK_EQUAL(nErrors, 0); |
| 300 | BOOST_CHECK_EQUAL(nDatas, 1); |
| 301 | |
| 302 | BOOST_CHECK_EQUAL(dataSize, 42); |
| 303 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 304 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 4); |
| 305 | BOOST_CHECK_EQUAL(face.sentData.size(), 0); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 306 | |
| 307 | { |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 308 | const Interest& interest = face.sentInterests[0]; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 309 | BOOST_CHECK_EQUAL(interest.getName(), "/hello/world"); |
| 310 | BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true); |
| 311 | BOOST_CHECK_EQUAL(interest.getChildSelector(), 1); |
| 312 | } |
| 313 | |
| 314 | { |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 315 | const Interest& interest = face.sentInterests[1]; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 316 | BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%00"); |
| 317 | BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false); |
| 318 | BOOST_CHECK_EQUAL(interest.getChildSelector(), 0); |
| 319 | } |
| 320 | |
| 321 | { |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 322 | const Interest& interest = face.sentInterests[2]; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 323 | BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%01"); |
| 324 | BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false); |
| 325 | BOOST_CHECK_EQUAL(interest.getChildSelector(), 0); |
| 326 | } |
| 327 | |
| 328 | { |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 329 | const Interest& interest = face.sentInterests[3]; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 330 | BOOST_CHECK_EQUAL(interest.getName(), "/hello/world/version0/%00%02"); |
| 331 | BOOST_CHECK_EQUAL(interest.getMustBeFresh(), false); |
| 332 | BOOST_CHECK_EQUAL(interest.getChildSelector(), 0); |
| 333 | } |
| 334 | } |
| 335 | |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 336 | BOOST_FIXTURE_TEST_CASE(MultipleSegmentFetching, Fixture) |
| 337 | { |
| 338 | ValidatorNull nullValidator; |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 339 | SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)), |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 340 | nullValidator, |
| 341 | bind(&Fixture::onComplete, this, _1), |
| 342 | bind(&Fixture::onError, this, _1)); |
| 343 | |
| 344 | advanceClocks(time::milliseconds(1), 10); |
| 345 | |
| 346 | for (uint64_t i = 0; i < 400; i++) { |
| 347 | advanceClocks(time::milliseconds(1), 10); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 348 | face.receive(*makeDataSegment("/hello/world/version0", i, false)); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 349 | } |
| 350 | advanceClocks(time::milliseconds(1), 10); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 351 | face.receive(*makeDataSegment("/hello/world/version0", 400, true)); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 352 | |
| 353 | advanceClocks(time::milliseconds(1), 10); |
| 354 | |
| 355 | BOOST_CHECK_EQUAL(nErrors, 0); |
| 356 | BOOST_CHECK_EQUAL(nDatas, 1); |
| 357 | } |
| 358 | |
Muktadir R Chowdhury | 2bc2df0 | 2016-04-05 16:55:41 -0500 | [diff] [blame] | 359 | BOOST_FIXTURE_TEST_CASE(DuplicateNack, Fixture) |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 360 | { |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 361 | SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)), |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 362 | make_shared<ValidatorNull>(), |
| 363 | bind(&Fixture::onComplete, this, _1), |
| 364 | bind(&Fixture::onError, this, _1)); |
| 365 | advanceClocks(time::milliseconds(1), 10); |
| 366 | |
| 367 | // receive nack for the original interest |
| 368 | nackLastInterest(lp::NackReason::DUPLICATE); |
| 369 | |
| 370 | // receive nack due to Duplication for the reexpressed interests |
| 371 | for (uint32_t i = 1; i <= SegmentFetcher::MAX_INTEREST_REEXPRESS; ++i) { |
| 372 | nackLastInterest(lp::NackReason::DUPLICATE); |
| 373 | } |
| 374 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 375 | BOOST_CHECK_EQUAL(face.sentInterests.size(), (SegmentFetcher::MAX_INTEREST_REEXPRESS + 1)); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 376 | BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::NACK_ERROR)); |
| 377 | } |
| 378 | |
Muktadir R Chowdhury | 2bc2df0 | 2016-04-05 16:55:41 -0500 | [diff] [blame] | 379 | BOOST_FIXTURE_TEST_CASE(CongestionNack, Fixture) |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 380 | { |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 381 | SegmentFetcher::fetch(face, Interest("/hello/world", time::seconds(1000)), |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 382 | make_shared<ValidatorNull>(), |
| 383 | bind(&Fixture::onComplete, this, _1), |
| 384 | bind(&Fixture::onError, this, _1)); |
| 385 | advanceClocks(time::milliseconds(1), 10); |
| 386 | |
| 387 | // receive nack for the original interest |
| 388 | nackLastInterest(lp::NackReason::CONGESTION); |
| 389 | |
| 390 | // receive nack due to Congestion for the reexpressed interests |
| 391 | for (uint32_t i = 1; i <= SegmentFetcher::MAX_INTEREST_REEXPRESS; ++i) { |
| 392 | nackLastInterest(lp::NackReason::CONGESTION); |
| 393 | } |
| 394 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 395 | BOOST_CHECK_EQUAL(face.sentInterests.size(), (SegmentFetcher::MAX_INTEREST_REEXPRESS + 1)); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 396 | BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::NACK_ERROR)); |
| 397 | } |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 398 | |
Muktadir R Chowdhury | 2bc2df0 | 2016-04-05 16:55:41 -0500 | [diff] [blame] | 399 | BOOST_FIXTURE_TEST_CASE(SegmentZero, Fixture) |
| 400 | { |
| 401 | int nNacks = 2; |
| 402 | |
| 403 | ndn::Name interestName("ndn:/A"); |
| 404 | SegmentFetcher::fetch(face, |
| 405 | Interest(interestName), |
| 406 | make_shared<ValidatorNull>(), |
| 407 | bind(&Fixture::onComplete, this, _1), |
| 408 | bind(&Fixture::onError, this, _1)); |
| 409 | |
| 410 | advanceClocks(time::milliseconds(1), 1000); |
| 411 | |
| 412 | for (uint64_t segmentNo = 0; segmentNo <= 3; segmentNo++) { |
| 413 | if (segmentNo == 1) { |
| 414 | while (nNacks--) { |
| 415 | nackLastInterest(lp::NackReason::CONGESTION); |
| 416 | advanceClocks(time::milliseconds(1), 10); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | auto data = makeDataSegment(interestName, segmentNo, segmentNo == 3); |
| 421 | face.receive(*data); |
| 422 | advanceClocks(time::milliseconds(1), 10); |
| 423 | } |
| 424 | |
| 425 | // Total number of sent interests should be 6: one interest for segment zero and segment one each, |
| 426 | // two re-expressed interests for segment one after getting nack twice, and two interests for |
| 427 | // segment two and three |
| 428 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 6); |
| 429 | |
| 430 | BOOST_CHECK_EQUAL(face.sentInterests[0].getName(), ndn::Name("ndn:/A")); |
| 431 | BOOST_CHECK_EQUAL(face.sentInterests[1].getName(), ndn::Name("ndn:/A/%00%01")); |
| 432 | BOOST_CHECK_EQUAL(face.sentInterests[2].getName(), ndn::Name("ndn:/A/%00%01")); |
| 433 | BOOST_CHECK_EQUAL(face.sentInterests[3].getName(), ndn::Name("ndn:/A/%00%01")); |
| 434 | BOOST_CHECK_EQUAL(face.sentInterests[4].getName(), ndn::Name("ndn:/A/%00%02")); |
| 435 | BOOST_CHECK_EQUAL(face.sentInterests[5].getName(), ndn::Name("ndn:/A/%00%03")); |
| 436 | } |
| 437 | |
| 438 | BOOST_FIXTURE_TEST_CASE(ZeroComponentName, Fixture) |
| 439 | { |
| 440 | SegmentFetcher::fetch(face, Interest("ndn:/"), |
| 441 | make_shared<ValidatorNull>(), |
| 442 | bind(&Fixture::onComplete, this, _1), |
| 443 | bind(&Fixture::onError, this, _1)); |
| 444 | advanceClocks(time::milliseconds(1), 10); |
| 445 | nackLastInterest(lp::NackReason::DUPLICATE); |
| 446 | face.receive(*makeDataSegment("/hello/world", 0, true)); |
| 447 | |
| 448 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 2); |
| 449 | BOOST_CHECK_EQUAL(face.sentInterests[0].getName(), ndn::Name("ndn:/")); |
| 450 | BOOST_CHECK_EQUAL(face.sentInterests[1].getName(), ndn::Name("ndn:/")); |
| 451 | BOOST_REQUIRE_EQUAL(nDatas, 1); |
| 452 | } |
| 453 | |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 454 | BOOST_AUTO_TEST_SUITE_END() |
| 455 | |
| 456 | } // namespace tests |
| 457 | } // namespace util |
| 458 | } // namespace ndn |