Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 2 | /* |
Muktadir Chowdhury | 1c109b4 | 2018-01-10 08:36:00 +0000 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 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" |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 23 | |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 24 | #include "data.hpp" |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 25 | #include "lp/nack.hpp" |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 26 | #include "util/dummy-client-face.hpp" |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 27 | |
| 28 | #include "boost-test.hpp" |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 29 | #include "dummy-validator.hpp" |
| 30 | #include "make-interest-data.hpp" |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 31 | #include "../identity-management-time-fixture.hpp" |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 32 | |
| 33 | namespace ndn { |
| 34 | namespace util { |
| 35 | namespace tests { |
| 36 | |
Junxiao Shi | 0f3f0b4 | 2016-07-14 13:26:37 +0000 | [diff] [blame] | 37 | using namespace ndn::tests; |
| 38 | |
Junxiao Shi | d5827ce | 2016-07-14 20:49:37 +0000 | [diff] [blame] | 39 | BOOST_AUTO_TEST_SUITE(Util) |
| 40 | BOOST_AUTO_TEST_SUITE(TestSegmentFetcher) |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 41 | |
Alexander Afanasyev | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame] | 42 | class Fixture : public IdentityManagementTimeFixture |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 43 | { |
| 44 | public: |
| 45 | Fixture() |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 46 | : face(io, m_keyChain) |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 47 | { |
| 48 | } |
| 49 | |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 50 | static shared_ptr<Data> |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 51 | makeDataSegment(const Name& baseName, uint64_t segment, bool isFinal) |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 52 | { |
| 53 | const uint8_t buffer[] = "Hello, world!"; |
| 54 | |
Junxiao Shi | d5827ce | 2016-07-14 20:49:37 +0000 | [diff] [blame] | 55 | auto data = make_shared<Data>(Name(baseName).appendSegment(segment)); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 56 | data->setContent(buffer, sizeof(buffer)); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 57 | if (isFinal) { |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 58 | data->setFinalBlock(data->getName()[-1]); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 59 | } |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 60 | |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 61 | return signData(data); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void |
| 65 | onError(uint32_t errorCode) |
| 66 | { |
| 67 | ++nErrors; |
| 68 | lastError = errorCode; |
| 69 | } |
| 70 | |
| 71 | void |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 72 | onComplete(const ConstBufferPtr& data) |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 73 | { |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame^] | 74 | ++nCompletions; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 75 | dataSize = data->size(); |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 76 | dataString = std::string(data->get<char>()); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 79 | void |
| 80 | nackLastInterest(lp::NackReason nackReason) |
| 81 | { |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 82 | const Interest& lastInterest = face.sentInterests.back(); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 83 | lp::Nack nack = makeNack(lastInterest, nackReason); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 84 | face.receive(nack); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 85 | advanceClocks(10_ms); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 86 | } |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 87 | |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame^] | 88 | void |
| 89 | connectSignals(shared_ptr<SegmentFetcher> fetcher) |
| 90 | { |
| 91 | fetcher->onComplete.connect(bind(&Fixture::onComplete, this, _1)); |
| 92 | fetcher->onError.connect(bind(&Fixture::onError, this, _1)); |
| 93 | } |
| 94 | |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 95 | public: |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 96 | DummyClientFace face; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 97 | |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame^] | 98 | int nErrors = 0; |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 99 | uint32_t lastError = 0; |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame^] | 100 | int nCompletions = 0; |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 101 | size_t dataSize = 0; |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 102 | std::string dataString; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | BOOST_FIXTURE_TEST_CASE(Timeout, Fixture) |
| 106 | { |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 107 | DummyValidator acceptValidator; |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame^] | 108 | shared_ptr<SegmentFetcher> fetcher = SegmentFetcher::start(face, Interest("/hello/world", 100_ms), |
| 109 | acceptValidator); |
| 110 | connectSignals(fetcher); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 111 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 112 | advanceClocks(1_ms); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 113 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 114 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 115 | const Interest& interest = face.sentInterests[0]; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 116 | BOOST_CHECK_EQUAL(interest.getName(), "/hello/world"); |
| 117 | BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true); |
| 118 | BOOST_CHECK_EQUAL(interest.getChildSelector(), 1); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 119 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 120 | advanceClocks(98_ms); |
Junxiao Shi | d5827ce | 2016-07-14 20:49:37 +0000 | [diff] [blame] | 121 | BOOST_CHECK_EQUAL(nErrors, 0); |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame^] | 122 | BOOST_CHECK_EQUAL(nCompletions, 0); |
Junxiao Shi | d5827ce | 2016-07-14 20:49:37 +0000 | [diff] [blame] | 123 | BOOST_CHECK_EQUAL(face.sentData.size(), 0); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 124 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 125 | advanceClocks(1_ms, 2); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 126 | BOOST_CHECK_EQUAL(nErrors, 1); |
| 127 | BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::INTEREST_TIMEOUT)); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 128 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1); |
| 129 | BOOST_CHECK_EQUAL(face.sentData.size(), 0); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 132 | BOOST_FIXTURE_TEST_CASE(Basic, Fixture) |
| 133 | { |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 134 | DummyValidator acceptValidator; |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame^] | 135 | size_t nAfterSegmentReceived = 0; |
| 136 | size_t nAfterSegmentValidated = 0; |
| 137 | shared_ptr<SegmentFetcher> fetcher = SegmentFetcher::start(face, Interest("/hello/world", 1000_s), |
| 138 | acceptValidator); |
| 139 | connectSignals(fetcher); |
| 140 | fetcher->afterSegmentReceived.connect(bind([&nAfterSegmentReceived] { ++nAfterSegmentReceived; })); |
| 141 | fetcher->afterSegmentValidated.connect(bind([&nAfterSegmentValidated] { ++nAfterSegmentValidated; })); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 142 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 143 | advanceClocks(10_ms); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 144 | |
| 145 | for (uint64_t i = 0; i < 400; i++) { |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 146 | advanceClocks(10_ms); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 147 | face.receive(*makeDataSegment("/hello/world/version0", i, false)); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 148 | } |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 149 | advanceClocks(10_ms); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 150 | face.receive(*makeDataSegment("/hello/world/version0", 400, true)); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 151 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 152 | advanceClocks(10_ms); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 153 | |
| 154 | BOOST_CHECK_EQUAL(nErrors, 0); |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame^] | 155 | BOOST_CHECK_EQUAL(nCompletions, 1); |
| 156 | BOOST_CHECK_EQUAL(nAfterSegmentReceived, 401); |
| 157 | BOOST_CHECK_EQUAL(nAfterSegmentValidated, 401); |
| 158 | } |
| 159 | |
| 160 | BOOST_FIXTURE_TEST_CASE(MissingSegmentNum, Fixture) |
| 161 | { |
| 162 | DummyValidator acceptValidator; |
| 163 | shared_ptr<SegmentFetcher> fetcher = SegmentFetcher::start(face, Interest("/hello/world", 1000_s), |
| 164 | acceptValidator); |
| 165 | connectSignals(fetcher); |
| 166 | |
| 167 | advanceClocks(10_ms); |
| 168 | |
| 169 | const uint8_t buffer[] = "Hello, world!"; |
| 170 | auto data = makeData("/hello/world/version0/no-segment"); |
| 171 | data->setContent(buffer, sizeof(buffer)); |
| 172 | |
| 173 | face.receive(*data); |
| 174 | advanceClocks(10_ms); |
| 175 | |
| 176 | BOOST_CHECK_EQUAL(nErrors, 1); |
| 177 | BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::DATA_HAS_NO_SEGMENT)); |
| 178 | BOOST_CHECK_EQUAL(nCompletions, 0); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 179 | } |
| 180 | |
Muktadir R Chowdhury | 2bc2df0 | 2016-04-05 16:55:41 -0500 | [diff] [blame] | 181 | BOOST_FIXTURE_TEST_CASE(DuplicateNack, Fixture) |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 182 | { |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame^] | 183 | DummyValidator acceptValidator; |
| 184 | shared_ptr<SegmentFetcher> fetcher = SegmentFetcher::start(face, Interest("/hello/world", 1000_s), |
| 185 | acceptValidator); |
| 186 | connectSignals(fetcher); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 187 | advanceClocks(10_ms); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 188 | |
| 189 | // receive nack for the original interest |
| 190 | nackLastInterest(lp::NackReason::DUPLICATE); |
| 191 | |
| 192 | // receive nack due to Duplication for the reexpressed interests |
| 193 | for (uint32_t i = 1; i <= SegmentFetcher::MAX_INTEREST_REEXPRESS; ++i) { |
| 194 | nackLastInterest(lp::NackReason::DUPLICATE); |
| 195 | } |
| 196 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 197 | BOOST_CHECK_EQUAL(face.sentInterests.size(), (SegmentFetcher::MAX_INTEREST_REEXPRESS + 1)); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 198 | BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::NACK_ERROR)); |
| 199 | } |
| 200 | |
Muktadir R Chowdhury | 2bc2df0 | 2016-04-05 16:55:41 -0500 | [diff] [blame] | 201 | BOOST_FIXTURE_TEST_CASE(CongestionNack, Fixture) |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 202 | { |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame^] | 203 | DummyValidator acceptValidator; |
| 204 | shared_ptr<SegmentFetcher> fetcher = SegmentFetcher::start(face, Interest("/hello/world", 1000_s), |
| 205 | acceptValidator); |
| 206 | connectSignals(fetcher); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 207 | advanceClocks(10_ms); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 208 | |
| 209 | // receive nack for the original interest |
| 210 | nackLastInterest(lp::NackReason::CONGESTION); |
| 211 | |
| 212 | // receive nack due to Congestion for the reexpressed interests |
| 213 | for (uint32_t i = 1; i <= SegmentFetcher::MAX_INTEREST_REEXPRESS; ++i) { |
| 214 | nackLastInterest(lp::NackReason::CONGESTION); |
| 215 | } |
| 216 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 217 | BOOST_CHECK_EQUAL(face.sentInterests.size(), (SegmentFetcher::MAX_INTEREST_REEXPRESS + 1)); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 218 | BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::NACK_ERROR)); |
| 219 | } |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 220 | |
Muktadir R Chowdhury | 2bc2df0 | 2016-04-05 16:55:41 -0500 | [diff] [blame] | 221 | BOOST_FIXTURE_TEST_CASE(SegmentZero, Fixture) |
| 222 | { |
| 223 | int nNacks = 2; |
| 224 | |
| 225 | ndn::Name interestName("ndn:/A"); |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame^] | 226 | DummyValidator acceptValidator; |
| 227 | shared_ptr<SegmentFetcher> fetcher = SegmentFetcher::start(face, Interest(interestName), |
| 228 | acceptValidator); |
| 229 | connectSignals(fetcher); |
Muktadir R Chowdhury | 2bc2df0 | 2016-04-05 16:55:41 -0500 | [diff] [blame] | 230 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 231 | advanceClocks(1000_ms); |
Muktadir R Chowdhury | 2bc2df0 | 2016-04-05 16:55:41 -0500 | [diff] [blame] | 232 | |
| 233 | for (uint64_t segmentNo = 0; segmentNo <= 3; segmentNo++) { |
| 234 | if (segmentNo == 1) { |
| 235 | while (nNacks--) { |
| 236 | nackLastInterest(lp::NackReason::CONGESTION); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 237 | advanceClocks(10_ms); |
Muktadir R Chowdhury | 2bc2df0 | 2016-04-05 16:55:41 -0500 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
| 241 | auto data = makeDataSegment(interestName, segmentNo, segmentNo == 3); |
| 242 | face.receive(*data); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 243 | advanceClocks(10_ms); |
Muktadir R Chowdhury | 2bc2df0 | 2016-04-05 16:55:41 -0500 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | // Total number of sent interests should be 6: one interest for segment zero and segment one each, |
| 247 | // two re-expressed interests for segment one after getting nack twice, and two interests for |
| 248 | // segment two and three |
| 249 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 6); |
| 250 | |
| 251 | BOOST_CHECK_EQUAL(face.sentInterests[0].getName(), ndn::Name("ndn:/A")); |
| 252 | BOOST_CHECK_EQUAL(face.sentInterests[1].getName(), ndn::Name("ndn:/A/%00%01")); |
| 253 | BOOST_CHECK_EQUAL(face.sentInterests[2].getName(), ndn::Name("ndn:/A/%00%01")); |
| 254 | BOOST_CHECK_EQUAL(face.sentInterests[3].getName(), ndn::Name("ndn:/A/%00%01")); |
| 255 | BOOST_CHECK_EQUAL(face.sentInterests[4].getName(), ndn::Name("ndn:/A/%00%02")); |
| 256 | BOOST_CHECK_EQUAL(face.sentInterests[5].getName(), ndn::Name("ndn:/A/%00%03")); |
| 257 | } |
| 258 | |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame^] | 259 | BOOST_FIXTURE_TEST_CASE(SingleSegment, Fixture) |
Muktadir R Chowdhury | 2bc2df0 | 2016-04-05 16:55:41 -0500 | [diff] [blame] | 260 | { |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame^] | 261 | DummyValidator acceptValidator; |
| 262 | shared_ptr<SegmentFetcher> fetcher = SegmentFetcher::start(face, Interest("ndn:/", 1000_s), |
| 263 | acceptValidator); |
| 264 | connectSignals(fetcher); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 265 | advanceClocks(10_ms); |
Muktadir R Chowdhury | 2bc2df0 | 2016-04-05 16:55:41 -0500 | [diff] [blame] | 266 | nackLastInterest(lp::NackReason::DUPLICATE); |
| 267 | face.receive(*makeDataSegment("/hello/world", 0, true)); |
| 268 | |
| 269 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 2); |
| 270 | BOOST_CHECK_EQUAL(face.sentInterests[0].getName(), ndn::Name("ndn:/")); |
| 271 | BOOST_CHECK_EQUAL(face.sentInterests[1].getName(), ndn::Name("ndn:/")); |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame^] | 272 | BOOST_REQUIRE_EQUAL(nCompletions, 1); |
Muktadir R Chowdhury | 2bc2df0 | 2016-04-05 16:55:41 -0500 | [diff] [blame] | 273 | } |
| 274 | |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame^] | 275 | BOOST_FIXTURE_TEST_CASE(ValidationFailure, Fixture) |
Muktadir Chowdhury | 1c109b4 | 2018-01-10 08:36:00 +0000 | [diff] [blame] | 276 | { |
| 277 | DummyValidator validator; |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame^] | 278 | validator.getPolicy().setResultCallback([] (const Name& name) { |
| 279 | return name.at(-1).toSegment() % 2 == 0; |
Muktadir Chowdhury | 1c109b4 | 2018-01-10 08:36:00 +0000 | [diff] [blame] | 280 | }); |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame^] | 281 | shared_ptr<SegmentFetcher> fetcher = SegmentFetcher::start(face, Interest("/hello/world", 1000_s), |
| 282 | validator); |
| 283 | connectSignals(fetcher); |
Muktadir Chowdhury | 1c109b4 | 2018-01-10 08:36:00 +0000 | [diff] [blame] | 284 | |
| 285 | auto data1 = makeDataSegment("/hello/world", 0, false); |
| 286 | auto data2 = makeDataSegment("/hello/world", 1, true); |
| 287 | |
| 288 | size_t nRecvSegments = 0; |
| 289 | fetcher->afterSegmentReceived.connect([&nRecvSegments] (const Data& receivedSegment) { |
| 290 | ++nRecvSegments; |
| 291 | }); |
| 292 | |
| 293 | size_t nValidatedSegments = 0; |
| 294 | fetcher->afterSegmentValidated.connect([&nValidatedSegments] (const Data& validatedSegment) { |
| 295 | ++nValidatedSegments; |
| 296 | }); |
| 297 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 298 | advanceClocks(10_ms, 10); |
Muktadir Chowdhury | 1c109b4 | 2018-01-10 08:36:00 +0000 | [diff] [blame] | 299 | |
| 300 | face.receive(*data1); |
| 301 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 302 | advanceClocks(10_ms, 10); |
Muktadir Chowdhury | 1c109b4 | 2018-01-10 08:36:00 +0000 | [diff] [blame] | 303 | |
| 304 | face.receive(*data2); |
| 305 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 306 | advanceClocks(10_ms, 10); |
Muktadir Chowdhury | 1c109b4 | 2018-01-10 08:36:00 +0000 | [diff] [blame] | 307 | |
| 308 | BOOST_CHECK_EQUAL(nRecvSegments, 2); |
| 309 | BOOST_CHECK_EQUAL(nValidatedSegments, 1); |
| 310 | BOOST_CHECK_EQUAL(nErrors, 1); |
| 311 | } |
| 312 | |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame^] | 313 | // Tests deprecated `fetch` API that uses callbacks instead of signals. This test case will be |
| 314 | // removed when this API is removed. |
| 315 | BOOST_FIXTURE_TEST_CASE(DeprecatedFetch, Fixture) |
| 316 | { |
| 317 | DummyValidator acceptValidator; |
| 318 | SegmentFetcher::fetch(face, Interest("/hello/world", 1000_s), |
| 319 | acceptValidator, |
| 320 | bind(&Fixture::onComplete, this, _1), |
| 321 | bind(&Fixture::onError, this, _1)); |
| 322 | |
| 323 | advanceClocks(10_ms); |
| 324 | |
| 325 | face.receive(*makeDataSegment("/hello/world/version0", 0, true)); |
| 326 | advanceClocks(10_ms); |
| 327 | |
| 328 | BOOST_CHECK_EQUAL(nErrors, 0); |
| 329 | BOOST_CHECK_EQUAL(nCompletions, 1); |
| 330 | |
| 331 | BOOST_CHECK_EQUAL(dataSize, 14); |
| 332 | |
| 333 | const uint8_t buffer[] = "Hello, world!"; |
| 334 | std::string bufferString(reinterpret_cast<const char*>(buffer)); |
| 335 | |
| 336 | BOOST_CHECK_EQUAL(dataString, bufferString); |
| 337 | |
| 338 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1); |
| 339 | BOOST_CHECK_EQUAL(face.sentData.size(), 0); |
| 340 | |
| 341 | const Interest& interest = face.sentInterests[0]; |
| 342 | BOOST_CHECK_EQUAL(interest.getName(), "/hello/world"); |
| 343 | BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true); |
| 344 | BOOST_CHECK_EQUAL(interest.getChildSelector(), 1); |
| 345 | } |
| 346 | |
| 347 | // Tests deprecated `fetch` API that uses callbacks instead of signals (with an accepting shared_ptr |
| 348 | // Validator). This test case will be removed when this API is removed. |
| 349 | BOOST_FIXTURE_TEST_CASE(DeprecatedFetchSharedPtrComplete, Fixture) |
| 350 | { |
| 351 | auto acceptValidator = make_shared<DummyValidator>(true); |
| 352 | SegmentFetcher::fetch(face, Interest("/hello/world", 1000_s), |
| 353 | acceptValidator, |
| 354 | bind(&Fixture::onComplete, this, _1), |
| 355 | bind(&Fixture::onError, this, _1)); |
| 356 | |
| 357 | weak_ptr<DummyValidator> weakValidator = acceptValidator; |
| 358 | BOOST_CHECK(!weakValidator.expired()); |
| 359 | acceptValidator.reset(); |
| 360 | BOOST_CHECK(!weakValidator.expired()); |
| 361 | |
| 362 | advanceClocks(10_ms); |
| 363 | |
| 364 | BOOST_CHECK(!weakValidator.expired()); |
| 365 | face.receive(*makeDataSegment("/hello/world/version0", 0, true)); |
| 366 | |
| 367 | advanceClocks(10_ms); |
| 368 | |
| 369 | BOOST_CHECK(weakValidator.expired()); |
| 370 | BOOST_CHECK_EQUAL(nErrors, 0); |
| 371 | BOOST_CHECK_EQUAL(nCompletions, 1); |
| 372 | |
| 373 | BOOST_CHECK_EQUAL(dataSize, 14); |
| 374 | |
| 375 | const uint8_t buffer[] = "Hello, world!"; |
| 376 | std::string bufferString(reinterpret_cast<const char*>(buffer)); |
| 377 | |
| 378 | BOOST_CHECK_EQUAL(dataString, bufferString); |
| 379 | |
| 380 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1); |
| 381 | BOOST_CHECK_EQUAL(face.sentData.size(), 0); |
| 382 | |
| 383 | const Interest& interest = face.sentInterests[0]; |
| 384 | BOOST_CHECK_EQUAL(interest.getName(), "/hello/world"); |
| 385 | BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true); |
| 386 | BOOST_CHECK_EQUAL(interest.getChildSelector(), 1); |
| 387 | } |
| 388 | |
| 389 | // Tests deprecated `fetch` API that uses callbacks instead of signals (with a rejecting shared_ptr |
| 390 | // Validator). This test case will be removed when this API is removed. |
| 391 | BOOST_FIXTURE_TEST_CASE(DeprecatedFetchSharedPtrError, Fixture) |
| 392 | { |
| 393 | auto acceptValidator = make_shared<DummyValidator>(false); |
| 394 | SegmentFetcher::fetch(face, Interest("/hello/world", 1000_s), |
| 395 | acceptValidator, |
| 396 | bind(&Fixture::onComplete, this, _1), |
| 397 | bind(&Fixture::onError, this, _1)); |
| 398 | |
| 399 | weak_ptr<DummyValidator> weakValidator = acceptValidator; |
| 400 | BOOST_CHECK(!weakValidator.expired()); |
| 401 | acceptValidator.reset(); |
| 402 | BOOST_CHECK(!weakValidator.expired()); |
| 403 | |
| 404 | advanceClocks(10_ms); |
| 405 | |
| 406 | BOOST_CHECK(!weakValidator.expired()); |
| 407 | face.receive(*makeDataSegment("/hello/world/version0", 0, true)); |
| 408 | |
| 409 | advanceClocks(10_ms); |
| 410 | |
| 411 | BOOST_CHECK(weakValidator.expired()); |
| 412 | BOOST_CHECK_EQUAL(nErrors, 1); |
| 413 | BOOST_CHECK_EQUAL(nCompletions, 0); |
| 414 | |
| 415 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1); |
| 416 | BOOST_CHECK_EQUAL(face.sentData.size(), 0); |
| 417 | |
| 418 | const Interest& interest = face.sentInterests[0]; |
| 419 | BOOST_CHECK_EQUAL(interest.getName(), "/hello/world"); |
| 420 | BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true); |
| 421 | BOOST_CHECK_EQUAL(interest.getChildSelector(), 1); |
| 422 | } |
| 423 | |
Junxiao Shi | d5827ce | 2016-07-14 20:49:37 +0000 | [diff] [blame] | 424 | BOOST_AUTO_TEST_SUITE_END() // TestSegmentFetcher |
| 425 | BOOST_AUTO_TEST_SUITE_END() // Util |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 426 | |
| 427 | } // namespace tests |
| 428 | } // namespace util |
| 429 | } // namespace ndn |