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 | ae43030 | 2023-05-11 01:42:46 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2023, 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 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 37 | namespace nfd::tests { |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 38 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 39 | using namespace nfd::rib; |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 40 | |
| 41 | class DummyReadvertisePolicy : public ReadvertisePolicy |
| 42 | { |
| 43 | public: |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 44 | std::optional<ReadvertiseAction> |
Davide Pesavento | 0a71dd3 | 2019-03-17 20:36:18 -0400 | [diff] [blame] | 45 | handleNewRoute(const RibRouteRef&) const override |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 46 | { |
| 47 | return this->decision; |
| 48 | } |
| 49 | |
| 50 | time::milliseconds |
| 51 | getRefreshInterval() const override |
| 52 | { |
Davide Pesavento | 0a71dd3 | 2019-03-17 20:36:18 -0400 | [diff] [blame] | 53 | return 1_min; |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | public: |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 57 | std::optional<ReadvertiseAction> decision; |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | class DummyReadvertiseDestination : public ReadvertiseDestination |
| 61 | { |
| 62 | public: |
| 63 | DummyReadvertiseDestination() |
| 64 | { |
| 65 | this->setAvailability(true); |
| 66 | } |
| 67 | |
| 68 | void |
| 69 | advertise(const ReadvertisedRoute& rr, |
| 70 | std::function<void()> successCb, |
| 71 | std::function<void(const std::string&)> failureCb) override |
| 72 | { |
| 73 | advertiseHistory.push_back({time::steady_clock::now(), rr.prefix}); |
| 74 | if (shouldSucceed) { |
| 75 | successCb(); |
| 76 | } |
| 77 | else { |
| 78 | failureCb("FAKE-FAILURE"); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | void |
| 83 | withdraw(const ReadvertisedRoute& rr, |
| 84 | std::function<void()> successCb, |
| 85 | std::function<void(const std::string&)> failureCb) override |
| 86 | { |
| 87 | withdrawHistory.push_back({time::steady_clock::now(), rr.prefix}); |
| 88 | if (shouldSucceed) { |
| 89 | successCb(); |
| 90 | } |
| 91 | else { |
| 92 | failureCb("FAKE-FAILURE"); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | void |
| 97 | setAvailability(bool isAvailable) |
| 98 | { |
| 99 | this->ReadvertiseDestination::setAvailability(isAvailable); |
| 100 | } |
| 101 | |
| 102 | public: |
| 103 | struct HistoryEntry |
| 104 | { |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 105 | time::steady_clock::time_point timestamp; |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 106 | Name prefix; |
| 107 | }; |
| 108 | |
| 109 | bool shouldSucceed = true; |
| 110 | std::vector<HistoryEntry> advertiseHistory; |
| 111 | std::vector<HistoryEntry> withdrawHistory; |
| 112 | }; |
| 113 | |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 114 | class ReadvertiseFixture : public GlobalIoTimeFixture, public KeyChainFixture |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 115 | { |
| 116 | public: |
| 117 | ReadvertiseFixture() |
Davide Pesavento | e1bdc08 | 2018-10-11 21:20:23 -0400 | [diff] [blame] | 118 | : m_face(g_io, m_keyChain, {false, false}) |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 119 | { |
Davide Pesavento | 0a71dd3 | 2019-03-17 20:36:18 -0400 | [diff] [blame] | 120 | auto policyPtr = make_unique<DummyReadvertisePolicy>(); |
| 121 | policy = policyPtr.get(); |
| 122 | auto destinationPtr = make_unique<DummyReadvertiseDestination>(); |
| 123 | destination = destinationPtr.get(); |
| 124 | readvertise = make_unique<Readvertise>(m_rib, std::move(policyPtr), std::move(destinationPtr)); |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void |
| 128 | insertRoute(const Name& prefix, uint64_t faceId, ndn::nfd::RouteOrigin origin) |
| 129 | { |
| 130 | Route route; |
| 131 | route.faceId = faceId; |
| 132 | route.origin = origin; |
Davide Pesavento | e1bdc08 | 2018-10-11 21:20:23 -0400 | [diff] [blame] | 133 | m_rib.insert(prefix, route); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 134 | this->advanceClocks(6_ms); |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | void |
| 138 | eraseRoute(const Name& prefix, uint64_t faceId, ndn::nfd::RouteOrigin origin) |
| 139 | { |
| 140 | Route route; |
| 141 | route.faceId = faceId; |
| 142 | route.origin = origin; |
Davide Pesavento | e1bdc08 | 2018-10-11 21:20:23 -0400 | [diff] [blame] | 143 | m_rib.erase(prefix, route); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 144 | this->advanceClocks(6_ms); |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | void |
| 148 | setDestinationAvailability(bool isAvailable) |
| 149 | { |
| 150 | destination->setAvailability(isAvailable); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 151 | this->advanceClocks(6_ms); |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Davide Pesavento | e1bdc08 | 2018-10-11 21:20:23 -0400 | [diff] [blame] | 154 | protected: |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 155 | DummyReadvertisePolicy* policy; |
| 156 | DummyReadvertiseDestination* destination; |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 157 | unique_ptr<Readvertise> readvertise; |
Davide Pesavento | e1bdc08 | 2018-10-11 21:20:23 -0400 | [diff] [blame] | 158 | |
| 159 | private: |
Davide Pesavento | ae43030 | 2023-05-11 01:42:46 -0400 | [diff] [blame] | 160 | ndn::DummyClientFace m_face; |
Davide Pesavento | e1bdc08 | 2018-10-11 21:20:23 -0400 | [diff] [blame] | 161 | Rib m_rib; |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 162 | }; |
| 163 | |
| 164 | BOOST_AUTO_TEST_SUITE(Readvertise) |
| 165 | BOOST_FIXTURE_TEST_SUITE(TestReadvertise, ReadvertiseFixture) |
| 166 | |
| 167 | BOOST_AUTO_TEST_CASE(AddRemoveRoute) |
| 168 | { |
| 169 | policy->decision = ReadvertiseAction{"/A", ndn::security::SigningInfo()}; |
| 170 | |
| 171 | // advertising /A |
| 172 | this->insertRoute("/A/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT); |
| 173 | BOOST_CHECK_EQUAL(destination->advertiseHistory.size(), 1); |
| 174 | BOOST_CHECK_EQUAL(destination->advertiseHistory.at(0).prefix, "/A"); |
| 175 | |
| 176 | // /A is already advertised |
| 177 | this->insertRoute("/A/2", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT); |
| 178 | BOOST_CHECK_EQUAL(destination->advertiseHistory.size(), 1); |
| 179 | |
| 180 | // refresh every 60 seconds |
| 181 | destination->advertiseHistory.clear(); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 182 | this->advanceClocks(61_s, 5); |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 183 | BOOST_CHECK_EQUAL(destination->advertiseHistory.size(), 5); |
| 184 | |
| 185 | // /A is still needed by /A/2 route |
| 186 | this->eraseRoute("/A/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT); |
| 187 | BOOST_CHECK_EQUAL(destination->withdrawHistory.size(), 0); |
| 188 | |
| 189 | // withdrawing /A |
| 190 | this->eraseRoute("/A/2", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT); |
| 191 | BOOST_CHECK_EQUAL(destination->withdrawHistory.size(), 1); |
| 192 | BOOST_CHECK_EQUAL(destination->withdrawHistory.at(0).prefix, "/A"); |
| 193 | } |
| 194 | |
| 195 | BOOST_AUTO_TEST_CASE(NoAdvertise) |
| 196 | { |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 197 | policy->decision = std::nullopt; |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 198 | |
| 199 | this->insertRoute("/A/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT); |
| 200 | this->insertRoute("/A/2", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT); |
| 201 | this->eraseRoute("/A/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT); |
| 202 | this->eraseRoute("/A/2", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT); |
| 203 | |
| 204 | BOOST_CHECK_EQUAL(destination->advertiseHistory.size(), 0); |
| 205 | BOOST_CHECK_EQUAL(destination->withdrawHistory.size(), 0); |
| 206 | } |
| 207 | |
| 208 | BOOST_AUTO_TEST_CASE(DestinationAvailability) |
| 209 | { |
| 210 | this->setDestinationAvailability(false); |
| 211 | |
| 212 | policy->decision = ReadvertiseAction{"/A", ndn::security::SigningInfo()}; |
| 213 | this->insertRoute("/A/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT); |
| 214 | policy->decision = ReadvertiseAction{"/B", ndn::security::SigningInfo()}; |
| 215 | this->insertRoute("/B/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT); |
| 216 | BOOST_CHECK_EQUAL(destination->advertiseHistory.size(), 0); |
| 217 | |
| 218 | this->setDestinationAvailability(true); |
| 219 | std::set<Name> advertisedPrefixes; |
| 220 | boost::copy(destination->advertiseHistory | boost::adaptors::transformed( |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 221 | [] (const auto& he) { return he.prefix; }), |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 222 | std::inserter(advertisedPrefixes, advertisedPrefixes.end())); |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 223 | const std::set<Name> expectedPrefixes{"/A", "/B"}; |
| 224 | BOOST_TEST(advertisedPrefixes == expectedPrefixes, boost::test_tools::per_element()); |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 225 | destination->advertiseHistory.clear(); |
| 226 | |
| 227 | this->setDestinationAvailability(false); |
| 228 | this->eraseRoute("/B/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT); |
| 229 | BOOST_CHECK_EQUAL(destination->withdrawHistory.size(), 0); |
| 230 | |
| 231 | this->setDestinationAvailability(true); |
| 232 | BOOST_CHECK_EQUAL(destination->withdrawHistory.size(), 0); |
| 233 | BOOST_CHECK_EQUAL(destination->advertiseHistory.size(), 1); |
| 234 | BOOST_CHECK_EQUAL(destination->advertiseHistory.at(0).prefix, "/A"); |
| 235 | } |
| 236 | |
| 237 | BOOST_AUTO_TEST_CASE(AdvertiseRetryInterval) |
| 238 | { |
| 239 | destination->shouldSucceed = false; |
| 240 | |
| 241 | policy->decision = ReadvertiseAction{"/A", ndn::security::SigningInfo()}; |
| 242 | this->insertRoute("/A/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT); |
| 243 | |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 244 | this->advanceClocks(10_s, 1_h); |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 245 | BOOST_REQUIRE_GT(destination->advertiseHistory.size(), 2); |
| 246 | |
| 247 | // destination->advertise keeps failing, so interval should increase |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 248 | using FloatInterval = time::duration<float, time::steady_clock::duration::period>; |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 249 | FloatInterval initialInterval = destination->advertiseHistory[1].timestamp - |
| 250 | destination->advertiseHistory[0].timestamp; |
| 251 | FloatInterval lastInterval = initialInterval; |
| 252 | for (size_t i = 2; i < destination->advertiseHistory.size(); ++i) { |
| 253 | FloatInterval interval = destination->advertiseHistory[i].timestamp - |
| 254 | destination->advertiseHistory[i - 1].timestamp; |
| 255 | BOOST_CHECK_CLOSE(interval.count(), (lastInterval * 2.0).count(), 10.0); |
| 256 | lastInterval = interval; |
| 257 | } |
| 258 | |
| 259 | destination->shouldSucceed = true; |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 260 | this->advanceClocks(1_h); |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 261 | destination->advertiseHistory.clear(); |
| 262 | |
| 263 | // destination->advertise has succeeded, retry interval should reset to initial |
| 264 | destination->shouldSucceed = false; |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 265 | this->advanceClocks(10_s, 300_s); |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 266 | BOOST_REQUIRE_GE(destination->advertiseHistory.size(), 2); |
| 267 | FloatInterval restartInterval = destination->advertiseHistory[1].timestamp - |
| 268 | destination->advertiseHistory[0].timestamp; |
| 269 | BOOST_CHECK_CLOSE(restartInterval.count(), initialInterval.count(), 10.0); |
| 270 | } |
| 271 | |
| 272 | BOOST_AUTO_TEST_CASE(ChangeDuringRetry) |
| 273 | { |
| 274 | destination->shouldSucceed = false; |
| 275 | policy->decision = ReadvertiseAction{"/A", ndn::security::SigningInfo()}; |
| 276 | this->insertRoute("/A/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 277 | this->advanceClocks(10_s, 300_s); |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 278 | BOOST_CHECK_GT(destination->advertiseHistory.size(), 0); |
| 279 | BOOST_CHECK_EQUAL(destination->withdrawHistory.size(), 0); |
| 280 | |
| 281 | // destination->advertise has been failing, but we want to withdraw |
| 282 | destination->advertiseHistory.clear(); |
| 283 | destination->withdrawHistory.clear(); |
| 284 | this->eraseRoute("/A/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 285 | this->advanceClocks(10_s, 300_s); |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 286 | BOOST_CHECK_EQUAL(destination->advertiseHistory.size(), 0); // don't try to advertise |
| 287 | BOOST_CHECK_GT(destination->withdrawHistory.size(), 0); // try to withdraw |
| 288 | |
| 289 | // destination->withdraw has been failing, but we want to advertise |
| 290 | destination->advertiseHistory.clear(); |
| 291 | destination->withdrawHistory.clear(); |
| 292 | this->insertRoute("/A/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 293 | this->advanceClocks(10_s, 300_s); |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 294 | BOOST_CHECK_GT(destination->advertiseHistory.size(), 0); // try to advertise |
| 295 | BOOST_CHECK_EQUAL(destination->withdrawHistory.size(), 0); // don't try to withdraw |
| 296 | } |
| 297 | |
| 298 | BOOST_AUTO_TEST_SUITE_END() // TestReadvertise |
| 299 | BOOST_AUTO_TEST_SUITE_END() // Readvertise |
| 300 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 301 | } // namespace nfd::tests |