Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2014-2021, Regents of the University of California, |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | #include "rib/readvertise/readvertise.hpp" |
| 27 | |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 28 | #include "tests/test-common.hpp" |
Davide Pesavento | 1d12d2f | 2019-03-22 12:44:14 -0400 | [diff] [blame] | 29 | #include "tests/key-chain-fixture.hpp" |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 30 | #include "tests/daemon/global-io-fixture.hpp" |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame] | 31 | |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 32 | #include <ndn-cxx/util/dummy-client-face.hpp> |
Davide Pesavento | 1d12d2f | 2019-03-22 12:44:14 -0400 | [diff] [blame] | 33 | |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 34 | #include <boost/range/adaptor/transformed.hpp> |
| 35 | #include <boost/range/algorithm/copy.hpp> |
| 36 | |
| 37 | namespace nfd { |
| 38 | namespace rib { |
| 39 | namespace tests { |
| 40 | |
| 41 | using namespace nfd::tests; |
| 42 | |
| 43 | class DummyReadvertisePolicy : public ReadvertisePolicy |
| 44 | { |
| 45 | public: |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame] | 46 | optional<ReadvertiseAction> |
Davide Pesavento | 0a71dd3 | 2019-03-17 20:36:18 -0400 | [diff] [blame] | 47 | handleNewRoute(const RibRouteRef&) const override |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 48 | { |
| 49 | return this->decision; |
| 50 | } |
| 51 | |
| 52 | time::milliseconds |
| 53 | getRefreshInterval() const override |
| 54 | { |
Davide Pesavento | 0a71dd3 | 2019-03-17 20:36:18 -0400 | [diff] [blame] | 55 | return 1_min; |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | public: |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame] | 59 | optional<ReadvertiseAction> decision; |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | class DummyReadvertiseDestination : public ReadvertiseDestination |
| 63 | { |
| 64 | public: |
| 65 | DummyReadvertiseDestination() |
| 66 | { |
| 67 | this->setAvailability(true); |
| 68 | } |
| 69 | |
| 70 | void |
| 71 | advertise(const ReadvertisedRoute& rr, |
| 72 | std::function<void()> successCb, |
| 73 | std::function<void(const std::string&)> failureCb) override |
| 74 | { |
| 75 | advertiseHistory.push_back({time::steady_clock::now(), rr.prefix}); |
| 76 | if (shouldSucceed) { |
| 77 | successCb(); |
| 78 | } |
| 79 | else { |
| 80 | failureCb("FAKE-FAILURE"); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | void |
| 85 | withdraw(const ReadvertisedRoute& rr, |
| 86 | std::function<void()> successCb, |
| 87 | std::function<void(const std::string&)> failureCb) override |
| 88 | { |
| 89 | withdrawHistory.push_back({time::steady_clock::now(), rr.prefix}); |
| 90 | if (shouldSucceed) { |
| 91 | successCb(); |
| 92 | } |
| 93 | else { |
| 94 | failureCb("FAKE-FAILURE"); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | void |
| 99 | setAvailability(bool isAvailable) |
| 100 | { |
| 101 | this->ReadvertiseDestination::setAvailability(isAvailable); |
| 102 | } |
| 103 | |
| 104 | public: |
| 105 | struct HistoryEntry |
| 106 | { |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame^] | 107 | time::steady_clock::time_point timestamp; |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 108 | Name prefix; |
| 109 | }; |
| 110 | |
| 111 | bool shouldSucceed = true; |
| 112 | std::vector<HistoryEntry> advertiseHistory; |
| 113 | std::vector<HistoryEntry> withdrawHistory; |
| 114 | }; |
| 115 | |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 116 | class ReadvertiseFixture : public GlobalIoTimeFixture, public KeyChainFixture |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 117 | { |
| 118 | public: |
| 119 | ReadvertiseFixture() |
Davide Pesavento | e1bdc08 | 2018-10-11 21:20:23 -0400 | [diff] [blame] | 120 | : m_face(g_io, m_keyChain, {false, false}) |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 121 | { |
Davide Pesavento | 0a71dd3 | 2019-03-17 20:36:18 -0400 | [diff] [blame] | 122 | auto policyPtr = make_unique<DummyReadvertisePolicy>(); |
| 123 | policy = policyPtr.get(); |
| 124 | auto destinationPtr = make_unique<DummyReadvertiseDestination>(); |
| 125 | destination = destinationPtr.get(); |
| 126 | readvertise = make_unique<Readvertise>(m_rib, std::move(policyPtr), std::move(destinationPtr)); |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | void |
| 130 | insertRoute(const Name& prefix, uint64_t faceId, ndn::nfd::RouteOrigin origin) |
| 131 | { |
| 132 | Route route; |
| 133 | route.faceId = faceId; |
| 134 | route.origin = origin; |
Davide Pesavento | e1bdc08 | 2018-10-11 21:20:23 -0400 | [diff] [blame] | 135 | m_rib.insert(prefix, route); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 136 | this->advanceClocks(6_ms); |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | void |
| 140 | eraseRoute(const Name& prefix, uint64_t faceId, ndn::nfd::RouteOrigin origin) |
| 141 | { |
| 142 | Route route; |
| 143 | route.faceId = faceId; |
| 144 | route.origin = origin; |
Davide Pesavento | e1bdc08 | 2018-10-11 21:20:23 -0400 | [diff] [blame] | 145 | m_rib.erase(prefix, route); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 146 | this->advanceClocks(6_ms); |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | void |
| 150 | setDestinationAvailability(bool isAvailable) |
| 151 | { |
| 152 | destination->setAvailability(isAvailable); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 153 | this->advanceClocks(6_ms); |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Davide Pesavento | e1bdc08 | 2018-10-11 21:20:23 -0400 | [diff] [blame] | 156 | protected: |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 157 | DummyReadvertisePolicy* policy; |
| 158 | DummyReadvertiseDestination* destination; |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 159 | unique_ptr<Readvertise> readvertise; |
Davide Pesavento | e1bdc08 | 2018-10-11 21:20:23 -0400 | [diff] [blame] | 160 | |
| 161 | private: |
| 162 | ndn::util::DummyClientFace m_face; |
Davide Pesavento | e1bdc08 | 2018-10-11 21:20:23 -0400 | [diff] [blame] | 163 | Rib m_rib; |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 164 | }; |
| 165 | |
| 166 | BOOST_AUTO_TEST_SUITE(Readvertise) |
| 167 | BOOST_FIXTURE_TEST_SUITE(TestReadvertise, ReadvertiseFixture) |
| 168 | |
| 169 | BOOST_AUTO_TEST_CASE(AddRemoveRoute) |
| 170 | { |
| 171 | policy->decision = ReadvertiseAction{"/A", ndn::security::SigningInfo()}; |
| 172 | |
| 173 | // advertising /A |
| 174 | this->insertRoute("/A/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT); |
| 175 | BOOST_CHECK_EQUAL(destination->advertiseHistory.size(), 1); |
| 176 | BOOST_CHECK_EQUAL(destination->advertiseHistory.at(0).prefix, "/A"); |
| 177 | |
| 178 | // /A is already advertised |
| 179 | this->insertRoute("/A/2", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT); |
| 180 | BOOST_CHECK_EQUAL(destination->advertiseHistory.size(), 1); |
| 181 | |
| 182 | // refresh every 60 seconds |
| 183 | destination->advertiseHistory.clear(); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 184 | this->advanceClocks(61_s, 5); |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 185 | BOOST_CHECK_EQUAL(destination->advertiseHistory.size(), 5); |
| 186 | |
| 187 | // /A is still needed by /A/2 route |
| 188 | this->eraseRoute("/A/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT); |
| 189 | BOOST_CHECK_EQUAL(destination->withdrawHistory.size(), 0); |
| 190 | |
| 191 | // withdrawing /A |
| 192 | this->eraseRoute("/A/2", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT); |
| 193 | BOOST_CHECK_EQUAL(destination->withdrawHistory.size(), 1); |
| 194 | BOOST_CHECK_EQUAL(destination->withdrawHistory.at(0).prefix, "/A"); |
| 195 | } |
| 196 | |
| 197 | BOOST_AUTO_TEST_CASE(NoAdvertise) |
| 198 | { |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame] | 199 | policy->decision = nullopt; |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 200 | |
| 201 | this->insertRoute("/A/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT); |
| 202 | this->insertRoute("/A/2", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT); |
| 203 | this->eraseRoute("/A/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT); |
| 204 | this->eraseRoute("/A/2", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT); |
| 205 | |
| 206 | BOOST_CHECK_EQUAL(destination->advertiseHistory.size(), 0); |
| 207 | BOOST_CHECK_EQUAL(destination->withdrawHistory.size(), 0); |
| 208 | } |
| 209 | |
| 210 | BOOST_AUTO_TEST_CASE(DestinationAvailability) |
| 211 | { |
| 212 | this->setDestinationAvailability(false); |
| 213 | |
| 214 | policy->decision = ReadvertiseAction{"/A", ndn::security::SigningInfo()}; |
| 215 | this->insertRoute("/A/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT); |
| 216 | policy->decision = ReadvertiseAction{"/B", ndn::security::SigningInfo()}; |
| 217 | this->insertRoute("/B/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT); |
| 218 | BOOST_CHECK_EQUAL(destination->advertiseHistory.size(), 0); |
| 219 | |
| 220 | this->setDestinationAvailability(true); |
| 221 | std::set<Name> advertisedPrefixes; |
| 222 | boost::copy(destination->advertiseHistory | boost::adaptors::transformed( |
| 223 | [] (const DummyReadvertiseDestination::HistoryEntry& he) { return he.prefix; }), |
| 224 | std::inserter(advertisedPrefixes, advertisedPrefixes.end())); |
| 225 | std::set<Name> expectedPrefixes{"/A", "/B"}; |
| 226 | BOOST_CHECK_EQUAL_COLLECTIONS(advertisedPrefixes.begin(), advertisedPrefixes.end(), |
| 227 | expectedPrefixes.begin(), expectedPrefixes.end()); |
| 228 | destination->advertiseHistory.clear(); |
| 229 | |
| 230 | this->setDestinationAvailability(false); |
| 231 | this->eraseRoute("/B/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT); |
| 232 | BOOST_CHECK_EQUAL(destination->withdrawHistory.size(), 0); |
| 233 | |
| 234 | this->setDestinationAvailability(true); |
| 235 | BOOST_CHECK_EQUAL(destination->withdrawHistory.size(), 0); |
| 236 | BOOST_CHECK_EQUAL(destination->advertiseHistory.size(), 1); |
| 237 | BOOST_CHECK_EQUAL(destination->advertiseHistory.at(0).prefix, "/A"); |
| 238 | } |
| 239 | |
| 240 | BOOST_AUTO_TEST_CASE(AdvertiseRetryInterval) |
| 241 | { |
| 242 | destination->shouldSucceed = false; |
| 243 | |
| 244 | policy->decision = ReadvertiseAction{"/A", ndn::security::SigningInfo()}; |
| 245 | this->insertRoute("/A/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT); |
| 246 | |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 247 | this->advanceClocks(10_s, 1_h); |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 248 | BOOST_REQUIRE_GT(destination->advertiseHistory.size(), 2); |
| 249 | |
| 250 | // destination->advertise keeps failing, so interval should increase |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame^] | 251 | using FloatInterval = time::duration<float, time::steady_clock::duration::period>; |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 252 | FloatInterval initialInterval = destination->advertiseHistory[1].timestamp - |
| 253 | destination->advertiseHistory[0].timestamp; |
| 254 | FloatInterval lastInterval = initialInterval; |
| 255 | for (size_t i = 2; i < destination->advertiseHistory.size(); ++i) { |
| 256 | FloatInterval interval = destination->advertiseHistory[i].timestamp - |
| 257 | destination->advertiseHistory[i - 1].timestamp; |
| 258 | BOOST_CHECK_CLOSE(interval.count(), (lastInterval * 2.0).count(), 10.0); |
| 259 | lastInterval = interval; |
| 260 | } |
| 261 | |
| 262 | destination->shouldSucceed = true; |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 263 | this->advanceClocks(1_h); |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 264 | destination->advertiseHistory.clear(); |
| 265 | |
| 266 | // destination->advertise has succeeded, retry interval should reset to initial |
| 267 | destination->shouldSucceed = false; |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 268 | this->advanceClocks(10_s, 300_s); |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 269 | BOOST_REQUIRE_GE(destination->advertiseHistory.size(), 2); |
| 270 | FloatInterval restartInterval = destination->advertiseHistory[1].timestamp - |
| 271 | destination->advertiseHistory[0].timestamp; |
| 272 | BOOST_CHECK_CLOSE(restartInterval.count(), initialInterval.count(), 10.0); |
| 273 | } |
| 274 | |
| 275 | BOOST_AUTO_TEST_CASE(ChangeDuringRetry) |
| 276 | { |
| 277 | destination->shouldSucceed = false; |
| 278 | policy->decision = ReadvertiseAction{"/A", ndn::security::SigningInfo()}; |
| 279 | this->insertRoute("/A/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 280 | this->advanceClocks(10_s, 300_s); |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 281 | BOOST_CHECK_GT(destination->advertiseHistory.size(), 0); |
| 282 | BOOST_CHECK_EQUAL(destination->withdrawHistory.size(), 0); |
| 283 | |
| 284 | // destination->advertise has been failing, but we want to withdraw |
| 285 | destination->advertiseHistory.clear(); |
| 286 | destination->withdrawHistory.clear(); |
| 287 | this->eraseRoute("/A/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 288 | this->advanceClocks(10_s, 300_s); |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 289 | BOOST_CHECK_EQUAL(destination->advertiseHistory.size(), 0); // don't try to advertise |
| 290 | BOOST_CHECK_GT(destination->withdrawHistory.size(), 0); // try to withdraw |
| 291 | |
| 292 | // destination->withdraw has been failing, but we want to advertise |
| 293 | destination->advertiseHistory.clear(); |
| 294 | destination->withdrawHistory.clear(); |
| 295 | this->insertRoute("/A/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 296 | this->advanceClocks(10_s, 300_s); |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 297 | BOOST_CHECK_GT(destination->advertiseHistory.size(), 0); // try to advertise |
| 298 | BOOST_CHECK_EQUAL(destination->withdrawHistory.size(), 0); // don't try to withdraw |
| 299 | } |
| 300 | |
| 301 | BOOST_AUTO_TEST_SUITE_END() // TestReadvertise |
| 302 | BOOST_AUTO_TEST_SUITE_END() // Readvertise |
| 303 | |
| 304 | } // namespace tests |
| 305 | } // namespace rib |
| 306 | } // namespace nfd |