blob: 1ff0f853c91043cb04bb4cfdf0374407752dc212 [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/*
3 * Copyright (c) 2014-2018, 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>
Junxiao Shi89c0ea02017-03-06 19:52:05 +000044 handleNewRoute(const RibRouteRef& route) const override
45 {
46 return this->decision;
47 }
48
49 time::milliseconds
50 getRefreshInterval() const override
51 {
52 return time::seconds(60);
53 }
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})
118 , m_scheduler(g_io)
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000119 {
120 auto policyUnique = make_unique<DummyReadvertisePolicy>();
121 policy = policyUnique.get();
122 auto destinationUnique = make_unique<DummyReadvertiseDestination>();
123 destination = destinationUnique.get();
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400124 readvertise = make_unique<Readvertise>(m_rib, m_scheduler,
125 std::move(policyUnique), std::move(destinationUnique));
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000126 }
127
128 void
129 insertRoute(const Name& prefix, uint64_t faceId, ndn::nfd::RouteOrigin origin)
130 {
131 Route route;
132 route.faceId = faceId;
133 route.origin = origin;
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400134 m_rib.insert(prefix, route);
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000135 this->advanceClocks(time::milliseconds(6));
136 }
137
138 void
139 eraseRoute(const Name& prefix, uint64_t faceId, ndn::nfd::RouteOrigin origin)
140 {
141 Route route;
142 route.faceId = faceId;
143 route.origin = origin;
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400144 m_rib.erase(prefix, route);
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000145 this->advanceClocks(time::milliseconds(6));
146 }
147
148 void
149 setDestinationAvailability(bool isAvailable)
150 {
151 destination->setAvailability(isAvailable);
152 this->advanceClocks(time::milliseconds(6));
153 }
154
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400155protected:
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000156 DummyReadvertisePolicy* policy;
157 DummyReadvertiseDestination* destination;
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000158 unique_ptr<Readvertise> readvertise;
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400159
160private:
161 ndn::util::DummyClientFace m_face;
162 ndn::util::Scheduler m_scheduler;
163 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();
184 this->advanceClocks(time::seconds(61), 5);
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
197BOOST_AUTO_TEST_CASE(NoAdvertise)
198{
Davide Pesavento87fc0f82018-04-11 23:43:51 -0400199 policy->decision = 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(
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
240BOOST_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
247 this->advanceClocks(time::seconds(10), time::seconds(3600));
248 BOOST_REQUIRE_GT(destination->advertiseHistory.size(), 2);
249
250 // destination->advertise keeps failing, so interval should increase
251 using FloatInterval = time::duration<float, time::steady_clock::Duration::period>;
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;
263 this->advanceClocks(time::seconds(3600));
264 destination->advertiseHistory.clear();
265
266 // destination->advertise has succeeded, retry interval should reset to initial
267 destination->shouldSucceed = false;
268 this->advanceClocks(time::seconds(10), time::seconds(300));
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
275BOOST_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);
280 this->advanceClocks(time::seconds(10), time::seconds(300));
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);
288 this->advanceClocks(time::seconds(10), time::seconds(300));
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);
296 this->advanceClocks(time::seconds(10), time::seconds(300));
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
301BOOST_AUTO_TEST_SUITE_END() // TestReadvertise
302BOOST_AUTO_TEST_SUITE_END() // Readvertise
303
304} // namespace tests
305} // namespace rib
306} // namespace nfd