blob: d2d8bb2f1c08ce78617469f7ed3510dc929c6a51 [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 Pesavento3dade002019-03-19 11:29:56 -06003 * Copyright (c) 2014-2019, 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
Junxiao Shi89c0ea02017-03-06 19:52:05 +000028#include "tests/identity-management-fixture.hpp"
Davide Pesavento87fc0f82018-04-11 23:43:51 -040029
Junxiao Shi89c0ea02017-03-06 19:52:05 +000030#include <ndn-cxx/util/dummy-client-face.hpp>
31#include <boost/range/adaptor/transformed.hpp>
32#include <boost/range/algorithm/copy.hpp>
33
34namespace nfd {
35namespace rib {
36namespace tests {
37
38using namespace nfd::tests;
39
40class DummyReadvertisePolicy : public ReadvertisePolicy
41{
42public:
Davide Pesavento87fc0f82018-04-11 23:43:51 -040043 optional<ReadvertiseAction>
Davide Pesavento0a71dd32019-03-17 20:36:18 -040044 handleNewRoute(const RibRouteRef&) const override
Junxiao Shi89c0ea02017-03-06 19:52:05 +000045 {
46 return this->decision;
47 }
48
49 time::milliseconds
50 getRefreshInterval() const override
51 {
Davide Pesavento0a71dd32019-03-17 20:36:18 -040052 return 1_min;
Junxiao Shi89c0ea02017-03-06 19:52:05 +000053 }
54
55public:
Davide Pesavento87fc0f82018-04-11 23:43:51 -040056 optional<ReadvertiseAction> decision;
Junxiao Shi89c0ea02017-03-06 19:52:05 +000057};
58
59class DummyReadvertiseDestination : public ReadvertiseDestination
60{
61public:
62 DummyReadvertiseDestination()
63 {
64 this->setAvailability(true);
65 }
66
67 void
68 advertise(const ReadvertisedRoute& rr,
69 std::function<void()> successCb,
70 std::function<void(const std::string&)> failureCb) override
71 {
72 advertiseHistory.push_back({time::steady_clock::now(), rr.prefix});
73 if (shouldSucceed) {
74 successCb();
75 }
76 else {
77 failureCb("FAKE-FAILURE");
78 }
79 }
80
81 void
82 withdraw(const ReadvertisedRoute& rr,
83 std::function<void()> successCb,
84 std::function<void(const std::string&)> failureCb) override
85 {
86 withdrawHistory.push_back({time::steady_clock::now(), rr.prefix});
87 if (shouldSucceed) {
88 successCb();
89 }
90 else {
91 failureCb("FAKE-FAILURE");
92 }
93 }
94
95 void
96 setAvailability(bool isAvailable)
97 {
98 this->ReadvertiseDestination::setAvailability(isAvailable);
99 }
100
101public:
102 struct HistoryEntry
103 {
104 time::steady_clock::TimePoint timestamp;
105 Name prefix;
106 };
107
108 bool shouldSucceed = true;
109 std::vector<HistoryEntry> advertiseHistory;
110 std::vector<HistoryEntry> withdrawHistory;
111};
112
113class ReadvertiseFixture : public IdentityManagementTimeFixture
114{
115public:
116 ReadvertiseFixture()
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400117 : m_face(g_io, m_keyChain, {false, false})
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000118 {
Davide Pesavento0a71dd32019-03-17 20:36:18 -0400119 auto policyPtr = make_unique<DummyReadvertisePolicy>();
120 policy = policyPtr.get();
121 auto destinationPtr = make_unique<DummyReadvertiseDestination>();
122 destination = destinationPtr.get();
123 readvertise = make_unique<Readvertise>(m_rib, std::move(policyPtr), std::move(destinationPtr));
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000124 }
125
126 void
127 insertRoute(const Name& prefix, uint64_t faceId, ndn::nfd::RouteOrigin origin)
128 {
129 Route route;
130 route.faceId = faceId;
131 route.origin = origin;
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400132 m_rib.insert(prefix, route);
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000133 this->advanceClocks(time::milliseconds(6));
134 }
135
136 void
137 eraseRoute(const Name& prefix, uint64_t faceId, ndn::nfd::RouteOrigin origin)
138 {
139 Route route;
140 route.faceId = faceId;
141 route.origin = origin;
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400142 m_rib.erase(prefix, route);
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000143 this->advanceClocks(time::milliseconds(6));
144 }
145
146 void
147 setDestinationAvailability(bool isAvailable)
148 {
149 destination->setAvailability(isAvailable);
150 this->advanceClocks(time::milliseconds(6));
151 }
152
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400153protected:
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000154 DummyReadvertisePolicy* policy;
155 DummyReadvertiseDestination* destination;
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000156 unique_ptr<Readvertise> readvertise;
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400157
158private:
159 ndn::util::DummyClientFace m_face;
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400160 Rib m_rib;
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000161};
162
163BOOST_AUTO_TEST_SUITE(Readvertise)
164BOOST_FIXTURE_TEST_SUITE(TestReadvertise, ReadvertiseFixture)
165
166BOOST_AUTO_TEST_CASE(AddRemoveRoute)
167{
168 policy->decision = ReadvertiseAction{"/A", ndn::security::SigningInfo()};
169
170 // advertising /A
171 this->insertRoute("/A/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT);
172 BOOST_CHECK_EQUAL(destination->advertiseHistory.size(), 1);
173 BOOST_CHECK_EQUAL(destination->advertiseHistory.at(0).prefix, "/A");
174
175 // /A is already advertised
176 this->insertRoute("/A/2", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT);
177 BOOST_CHECK_EQUAL(destination->advertiseHistory.size(), 1);
178
179 // refresh every 60 seconds
180 destination->advertiseHistory.clear();
181 this->advanceClocks(time::seconds(61), 5);
182 BOOST_CHECK_EQUAL(destination->advertiseHistory.size(), 5);
183
184 // /A is still needed by /A/2 route
185 this->eraseRoute("/A/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT);
186 BOOST_CHECK_EQUAL(destination->withdrawHistory.size(), 0);
187
188 // withdrawing /A
189 this->eraseRoute("/A/2", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT);
190 BOOST_CHECK_EQUAL(destination->withdrawHistory.size(), 1);
191 BOOST_CHECK_EQUAL(destination->withdrawHistory.at(0).prefix, "/A");
192}
193
194BOOST_AUTO_TEST_CASE(NoAdvertise)
195{
Davide Pesavento87fc0f82018-04-11 23:43:51 -0400196 policy->decision = nullopt;
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000197
198 this->insertRoute("/A/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT);
199 this->insertRoute("/A/2", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT);
200 this->eraseRoute("/A/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT);
201 this->eraseRoute("/A/2", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT);
202
203 BOOST_CHECK_EQUAL(destination->advertiseHistory.size(), 0);
204 BOOST_CHECK_EQUAL(destination->withdrawHistory.size(), 0);
205}
206
207BOOST_AUTO_TEST_CASE(DestinationAvailability)
208{
209 this->setDestinationAvailability(false);
210
211 policy->decision = ReadvertiseAction{"/A", ndn::security::SigningInfo()};
212 this->insertRoute("/A/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT);
213 policy->decision = ReadvertiseAction{"/B", ndn::security::SigningInfo()};
214 this->insertRoute("/B/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT);
215 BOOST_CHECK_EQUAL(destination->advertiseHistory.size(), 0);
216
217 this->setDestinationAvailability(true);
218 std::set<Name> advertisedPrefixes;
219 boost::copy(destination->advertiseHistory | boost::adaptors::transformed(
220 [] (const DummyReadvertiseDestination::HistoryEntry& he) { return he.prefix; }),
221 std::inserter(advertisedPrefixes, advertisedPrefixes.end()));
222 std::set<Name> expectedPrefixes{"/A", "/B"};
223 BOOST_CHECK_EQUAL_COLLECTIONS(advertisedPrefixes.begin(), advertisedPrefixes.end(),
224 expectedPrefixes.begin(), expectedPrefixes.end());
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
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
244 this->advanceClocks(time::seconds(10), time::seconds(3600));
245 BOOST_REQUIRE_GT(destination->advertiseHistory.size(), 2);
246
247 // destination->advertise keeps failing, so interval should increase
248 using FloatInterval = time::duration<float, time::steady_clock::Duration::period>;
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;
260 this->advanceClocks(time::seconds(3600));
261 destination->advertiseHistory.clear();
262
263 // destination->advertise has succeeded, retry interval should reset to initial
264 destination->shouldSucceed = false;
265 this->advanceClocks(time::seconds(10), time::seconds(300));
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
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);
277 this->advanceClocks(time::seconds(10), time::seconds(300));
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);
285 this->advanceClocks(time::seconds(10), time::seconds(300));
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);
293 this->advanceClocks(time::seconds(10), time::seconds(300));
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
298BOOST_AUTO_TEST_SUITE_END() // TestReadvertise
299BOOST_AUTO_TEST_SUITE_END() // Readvertise
300
301} // namespace tests
302} // namespace rib
303} // namespace nfd