blob: ffcab71671a19ebccac7ddcf20e01ee5beee25bf [file] [log] [blame]
Junxiao Shi89c0ea02017-03-06 19:52:05 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento87fc0f82018-04-11 23:43:51 -04002/*
Davide Pesaventob7bfcb92022-05-22 23:55:23 -04003 * Copyright (c) 2014-2022, Regents of the University of California,
Junxiao Shi89c0ea02017-03-06 19:52:05 +00004 * 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 Pesaventocf7db2f2019-03-24 23:17:28 -040028#include "tests/test-common.hpp"
Davide Pesavento1d12d2f2019-03-22 12:44:14 -040029#include "tests/key-chain-fixture.hpp"
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040030#include "tests/daemon/global-io-fixture.hpp"
Davide Pesavento87fc0f82018-04-11 23:43:51 -040031
Junxiao Shi89c0ea02017-03-06 19:52:05 +000032#include <ndn-cxx/util/dummy-client-face.hpp>
Davide Pesavento1d12d2f2019-03-22 12:44:14 -040033
Junxiao Shi89c0ea02017-03-06 19:52:05 +000034#include <boost/range/adaptor/transformed.hpp>
35#include <boost/range/algorithm/copy.hpp>
36
37namespace nfd {
38namespace rib {
39namespace tests {
40
41using namespace nfd::tests;
42
43class DummyReadvertisePolicy : public ReadvertisePolicy
44{
45public:
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040046 std::optional<ReadvertiseAction>
Davide Pesavento0a71dd32019-03-17 20:36:18 -040047 handleNewRoute(const RibRouteRef&) const override
Junxiao Shi89c0ea02017-03-06 19:52:05 +000048 {
49 return this->decision;
50 }
51
52 time::milliseconds
53 getRefreshInterval() const override
54 {
Davide Pesavento0a71dd32019-03-17 20:36:18 -040055 return 1_min;
Junxiao Shi89c0ea02017-03-06 19:52:05 +000056 }
57
58public:
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040059 std::optional<ReadvertiseAction> decision;
Junxiao Shi89c0ea02017-03-06 19:52:05 +000060};
61
62class DummyReadvertiseDestination : public ReadvertiseDestination
63{
64public:
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
104public:
105 struct HistoryEntry
106 {
Davide Pesavento412c9822021-07-02 00:21:05 -0400107 time::steady_clock::time_point timestamp;
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000108 Name prefix;
109 };
110
111 bool shouldSucceed = true;
112 std::vector<HistoryEntry> advertiseHistory;
113 std::vector<HistoryEntry> withdrawHistory;
114};
115
Davide Pesaventocf7db2f2019-03-24 23:17:28 -0400116class ReadvertiseFixture : public GlobalIoTimeFixture, public KeyChainFixture
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000117{
118public:
119 ReadvertiseFixture()
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400120 : m_face(g_io, m_keyChain, {false, false})
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000121 {
Davide Pesavento0a71dd32019-03-17 20:36:18 -0400122 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 Shi89c0ea02017-03-06 19:52:05 +0000127 }
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 Pesaventoe1bdc082018-10-11 21:20:23 -0400135 m_rib.insert(prefix, route);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400136 this->advanceClocks(6_ms);
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000137 }
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 Pesaventoe1bdc082018-10-11 21:20:23 -0400145 m_rib.erase(prefix, route);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400146 this->advanceClocks(6_ms);
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000147 }
148
149 void
150 setDestinationAvailability(bool isAvailable)
151 {
152 destination->setAvailability(isAvailable);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400153 this->advanceClocks(6_ms);
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000154 }
155
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400156protected:
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000157 DummyReadvertisePolicy* policy;
158 DummyReadvertiseDestination* destination;
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000159 unique_ptr<Readvertise> readvertise;
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400160
161private:
162 ndn::util::DummyClientFace m_face;
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400163 Rib m_rib;
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000164};
165
166BOOST_AUTO_TEST_SUITE(Readvertise)
167BOOST_FIXTURE_TEST_SUITE(TestReadvertise, ReadvertiseFixture)
168
169BOOST_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 Pesavento14e71f02019-03-28 17:35:25 -0400184 this->advanceClocks(61_s, 5);
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000185 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
197BOOST_AUTO_TEST_CASE(NoAdvertise)
198{
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400199 policy->decision = std::nullopt;
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000200
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
210BOOST_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(
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400223 [] (const auto& he) { return he.prefix; }),
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000224 std::inserter(advertisedPrefixes, advertisedPrefixes.end()));
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400225 const std::set<Name> expectedPrefixes{"/A", "/B"};
226 BOOST_TEST(advertisedPrefixes == expectedPrefixes, boost::test_tools::per_element());
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000227 destination->advertiseHistory.clear();
228
229 this->setDestinationAvailability(false);
230 this->eraseRoute("/B/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT);
231 BOOST_CHECK_EQUAL(destination->withdrawHistory.size(), 0);
232
233 this->setDestinationAvailability(true);
234 BOOST_CHECK_EQUAL(destination->withdrawHistory.size(), 0);
235 BOOST_CHECK_EQUAL(destination->advertiseHistory.size(), 1);
236 BOOST_CHECK_EQUAL(destination->advertiseHistory.at(0).prefix, "/A");
237}
238
239BOOST_AUTO_TEST_CASE(AdvertiseRetryInterval)
240{
241 destination->shouldSucceed = false;
242
243 policy->decision = ReadvertiseAction{"/A", ndn::security::SigningInfo()};
244 this->insertRoute("/A/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT);
245
Davide Pesavento14e71f02019-03-28 17:35:25 -0400246 this->advanceClocks(10_s, 1_h);
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000247 BOOST_REQUIRE_GT(destination->advertiseHistory.size(), 2);
248
249 // destination->advertise keeps failing, so interval should increase
Davide Pesavento412c9822021-07-02 00:21:05 -0400250 using FloatInterval = time::duration<float, time::steady_clock::duration::period>;
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000251 FloatInterval initialInterval = destination->advertiseHistory[1].timestamp -
252 destination->advertiseHistory[0].timestamp;
253 FloatInterval lastInterval = initialInterval;
254 for (size_t i = 2; i < destination->advertiseHistory.size(); ++i) {
255 FloatInterval interval = destination->advertiseHistory[i].timestamp -
256 destination->advertiseHistory[i - 1].timestamp;
257 BOOST_CHECK_CLOSE(interval.count(), (lastInterval * 2.0).count(), 10.0);
258 lastInterval = interval;
259 }
260
261 destination->shouldSucceed = true;
Davide Pesavento14e71f02019-03-28 17:35:25 -0400262 this->advanceClocks(1_h);
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000263 destination->advertiseHistory.clear();
264
265 // destination->advertise has succeeded, retry interval should reset to initial
266 destination->shouldSucceed = false;
Davide Pesavento14e71f02019-03-28 17:35:25 -0400267 this->advanceClocks(10_s, 300_s);
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000268 BOOST_REQUIRE_GE(destination->advertiseHistory.size(), 2);
269 FloatInterval restartInterval = destination->advertiseHistory[1].timestamp -
270 destination->advertiseHistory[0].timestamp;
271 BOOST_CHECK_CLOSE(restartInterval.count(), initialInterval.count(), 10.0);
272}
273
274BOOST_AUTO_TEST_CASE(ChangeDuringRetry)
275{
276 destination->shouldSucceed = false;
277 policy->decision = ReadvertiseAction{"/A", ndn::security::SigningInfo()};
278 this->insertRoute("/A/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400279 this->advanceClocks(10_s, 300_s);
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000280 BOOST_CHECK_GT(destination->advertiseHistory.size(), 0);
281 BOOST_CHECK_EQUAL(destination->withdrawHistory.size(), 0);
282
283 // destination->advertise has been failing, but we want to withdraw
284 destination->advertiseHistory.clear();
285 destination->withdrawHistory.clear();
286 this->eraseRoute("/A/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400287 this->advanceClocks(10_s, 300_s);
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000288 BOOST_CHECK_EQUAL(destination->advertiseHistory.size(), 0); // don't try to advertise
289 BOOST_CHECK_GT(destination->withdrawHistory.size(), 0); // try to withdraw
290
291 // destination->withdraw has been failing, but we want to advertise
292 destination->advertiseHistory.clear();
293 destination->withdrawHistory.clear();
294 this->insertRoute("/A/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400295 this->advanceClocks(10_s, 300_s);
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000296 BOOST_CHECK_GT(destination->advertiseHistory.size(), 0); // try to advertise
297 BOOST_CHECK_EQUAL(destination->withdrawHistory.size(), 0); // don't try to withdraw
298}
299
300BOOST_AUTO_TEST_SUITE_END() // TestReadvertise
301BOOST_AUTO_TEST_SUITE_END() // Readvertise
302
303} // namespace tests
304} // namespace rib
305} // namespace nfd