blob: 7f83dfc51a5eddcd21b83703e33e9943dd46bb96 [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 Pesaventocaa60cc2024-02-18 18:18:37 -05003 * Copyright (c) 2014-2024, 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
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040037namespace nfd::tests {
Junxiao Shi89c0ea02017-03-06 19:52:05 +000038
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040039using namespace nfd::rib;
Junxiao Shi89c0ea02017-03-06 19:52:05 +000040
41class DummyReadvertisePolicy : public ReadvertisePolicy
42{
43public:
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040044 std::optional<ReadvertiseAction>
Davide Pesavento0a71dd32019-03-17 20:36:18 -040045 handleNewRoute(const RibRouteRef&) const override
Junxiao Shi89c0ea02017-03-06 19:52:05 +000046 {
47 return this->decision;
48 }
49
50 time::milliseconds
51 getRefreshInterval() const override
52 {
Davide Pesavento0a71dd32019-03-17 20:36:18 -040053 return 1_min;
Junxiao Shi89c0ea02017-03-06 19:52:05 +000054 }
55
56public:
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040057 std::optional<ReadvertiseAction> decision;
Junxiao Shi89c0ea02017-03-06 19:52:05 +000058};
59
60class DummyReadvertiseDestination : public ReadvertiseDestination
61{
62public:
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
102public:
103 struct HistoryEntry
104 {
Davide Pesavento412c9822021-07-02 00:21:05 -0400105 time::steady_clock::time_point timestamp;
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000106 Name prefix;
107 };
108
109 bool shouldSucceed = true;
110 std::vector<HistoryEntry> advertiseHistory;
111 std::vector<HistoryEntry> withdrawHistory;
112};
113
Davide Pesaventocf7db2f2019-03-24 23:17:28 -0400114class ReadvertiseFixture : public GlobalIoTimeFixture, public KeyChainFixture
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000115{
116public:
117 ReadvertiseFixture()
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400118 : m_face(g_io, m_keyChain, {false, false})
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000119 {
Davide Pesavento0a71dd32019-03-17 20:36:18 -0400120 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 Shi89c0ea02017-03-06 19:52:05 +0000125 }
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 Pesaventoe1bdc082018-10-11 21:20:23 -0400133 m_rib.insert(prefix, route);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400134 this->advanceClocks(6_ms);
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000135 }
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 Pesaventoe1bdc082018-10-11 21:20:23 -0400143 m_rib.erase(prefix, route);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400144 this->advanceClocks(6_ms);
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000145 }
146
147 void
148 setDestinationAvailability(bool isAvailable)
149 {
150 destination->setAvailability(isAvailable);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400151 this->advanceClocks(6_ms);
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000152 }
153
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400154protected:
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000155 DummyReadvertisePolicy* policy;
156 DummyReadvertiseDestination* destination;
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000157 unique_ptr<Readvertise> readvertise;
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400158
159private:
Davide Pesaventoae430302023-05-11 01:42:46 -0400160 ndn::DummyClientFace m_face;
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400161 Rib m_rib;
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000162};
163
Davide Pesaventocaa60cc2024-02-18 18:18:37 -0500164BOOST_AUTO_TEST_SUITE(Rib)
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000165BOOST_FIXTURE_TEST_SUITE(TestReadvertise, ReadvertiseFixture)
166
167BOOST_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 Pesavento14e71f02019-03-28 17:35:25 -0400182 this->advanceClocks(61_s, 5);
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000183 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
195BOOST_AUTO_TEST_CASE(NoAdvertise)
196{
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400197 policy->decision = std::nullopt;
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000198
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
208BOOST_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 Pesaventoa3a7a4e2022-05-29 16:06:22 -0400221 [] (const auto& he) { return he.prefix; }),
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000222 std::inserter(advertisedPrefixes, advertisedPrefixes.end()));
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400223 const std::set<Name> expectedPrefixes{"/A", "/B"};
224 BOOST_TEST(advertisedPrefixes == expectedPrefixes, boost::test_tools::per_element());
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000225 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
237BOOST_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 Pesavento14e71f02019-03-28 17:35:25 -0400244 this->advanceClocks(10_s, 1_h);
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000245 BOOST_REQUIRE_GT(destination->advertiseHistory.size(), 2);
246
247 // destination->advertise keeps failing, so interval should increase
Davide Pesavento412c9822021-07-02 00:21:05 -0400248 using FloatInterval = time::duration<float, time::steady_clock::duration::period>;
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000249 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 Pesavento14e71f02019-03-28 17:35:25 -0400260 this->advanceClocks(1_h);
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000261 destination->advertiseHistory.clear();
262
263 // destination->advertise has succeeded, retry interval should reset to initial
264 destination->shouldSucceed = false;
Davide Pesavento14e71f02019-03-28 17:35:25 -0400265 this->advanceClocks(10_s, 300_s);
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000266 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
272BOOST_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 Pesavento14e71f02019-03-28 17:35:25 -0400277 this->advanceClocks(10_s, 300_s);
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000278 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 Pesavento14e71f02019-03-28 17:35:25 -0400285 this->advanceClocks(10_s, 300_s);
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000286 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 Pesavento14e71f02019-03-28 17:35:25 -0400293 this->advanceClocks(10_s, 300_s);
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000294 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
298BOOST_AUTO_TEST_SUITE_END() // TestReadvertise
Davide Pesaventocaa60cc2024-02-18 18:18:37 -0500299BOOST_AUTO_TEST_SUITE_END() // Rib
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000300
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400301} // namespace nfd::tests