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