blob: 15cc105276da1d512e9ea4088fd92d25b5a9c705 [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
Davide Pesavento1d12d2f2019-03-22 12:44:14 -040028#include "tests/key-chain-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>
Davide Pesavento1d12d2f2019-03-22 12:44:14 -040031
Junxiao Shi89c0ea02017-03-06 19:52:05 +000032#include <boost/range/adaptor/transformed.hpp>
33#include <boost/range/algorithm/copy.hpp>
34
35namespace nfd {
36namespace rib {
37namespace tests {
38
39using namespace nfd::tests;
40
41class DummyReadvertisePolicy : public ReadvertisePolicy
42{
43public:
Davide Pesavento87fc0f82018-04-11 23:43:51 -040044 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 Pesavento87fc0f82018-04-11 23:43:51 -040057 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 {
105 time::steady_clock::TimePoint timestamp;
106 Name prefix;
107 };
108
109 bool shouldSucceed = true;
110 std::vector<HistoryEntry> advertiseHistory;
111 std::vector<HistoryEntry> withdrawHistory;
112};
113
Davide Pesavento1d12d2f2019-03-22 12:44:14 -0400114class ReadvertiseFixture : public UnitTestTimeFixture, 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);
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000134 this->advanceClocks(time::milliseconds(6));
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 Pesaventoe1bdc082018-10-11 21:20:23 -0400143 m_rib.erase(prefix, route);
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000144 this->advanceClocks(time::milliseconds(6));
145 }
146
147 void
148 setDestinationAvailability(bool isAvailable)
149 {
150 destination->setAvailability(isAvailable);
151 this->advanceClocks(time::milliseconds(6));
152 }
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:
160 ndn::util::DummyClientFace m_face;
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400161 Rib m_rib;
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000162};
163
164BOOST_AUTO_TEST_SUITE(Readvertise)
165BOOST_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();
182 this->advanceClocks(time::seconds(61), 5);
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
195BOOST_AUTO_TEST_CASE(NoAdvertise)
196{
Davide Pesavento87fc0f82018-04-11 23:43:51 -0400197 policy->decision = 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(
221 [] (const DummyReadvertiseDestination::HistoryEntry& he) { return he.prefix; }),
222 std::inserter(advertisedPrefixes, advertisedPrefixes.end()));
223 std::set<Name> expectedPrefixes{"/A", "/B"};
224 BOOST_CHECK_EQUAL_COLLECTIONS(advertisedPrefixes.begin(), advertisedPrefixes.end(),
225 expectedPrefixes.begin(), expectedPrefixes.end());
226 destination->advertiseHistory.clear();
227
228 this->setDestinationAvailability(false);
229 this->eraseRoute("/B/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT);
230 BOOST_CHECK_EQUAL(destination->withdrawHistory.size(), 0);
231
232 this->setDestinationAvailability(true);
233 BOOST_CHECK_EQUAL(destination->withdrawHistory.size(), 0);
234 BOOST_CHECK_EQUAL(destination->advertiseHistory.size(), 1);
235 BOOST_CHECK_EQUAL(destination->advertiseHistory.at(0).prefix, "/A");
236}
237
238BOOST_AUTO_TEST_CASE(AdvertiseRetryInterval)
239{
240 destination->shouldSucceed = false;
241
242 policy->decision = ReadvertiseAction{"/A", ndn::security::SigningInfo()};
243 this->insertRoute("/A/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT);
244
245 this->advanceClocks(time::seconds(10), time::seconds(3600));
246 BOOST_REQUIRE_GT(destination->advertiseHistory.size(), 2);
247
248 // destination->advertise keeps failing, so interval should increase
249 using FloatInterval = time::duration<float, time::steady_clock::Duration::period>;
250 FloatInterval initialInterval = destination->advertiseHistory[1].timestamp -
251 destination->advertiseHistory[0].timestamp;
252 FloatInterval lastInterval = initialInterval;
253 for (size_t i = 2; i < destination->advertiseHistory.size(); ++i) {
254 FloatInterval interval = destination->advertiseHistory[i].timestamp -
255 destination->advertiseHistory[i - 1].timestamp;
256 BOOST_CHECK_CLOSE(interval.count(), (lastInterval * 2.0).count(), 10.0);
257 lastInterval = interval;
258 }
259
260 destination->shouldSucceed = true;
261 this->advanceClocks(time::seconds(3600));
262 destination->advertiseHistory.clear();
263
264 // destination->advertise has succeeded, retry interval should reset to initial
265 destination->shouldSucceed = false;
266 this->advanceClocks(time::seconds(10), time::seconds(300));
267 BOOST_REQUIRE_GE(destination->advertiseHistory.size(), 2);
268 FloatInterval restartInterval = destination->advertiseHistory[1].timestamp -
269 destination->advertiseHistory[0].timestamp;
270 BOOST_CHECK_CLOSE(restartInterval.count(), initialInterval.count(), 10.0);
271}
272
273BOOST_AUTO_TEST_CASE(ChangeDuringRetry)
274{
275 destination->shouldSucceed = false;
276 policy->decision = ReadvertiseAction{"/A", ndn::security::SigningInfo()};
277 this->insertRoute("/A/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT);
278 this->advanceClocks(time::seconds(10), time::seconds(300));
279 BOOST_CHECK_GT(destination->advertiseHistory.size(), 0);
280 BOOST_CHECK_EQUAL(destination->withdrawHistory.size(), 0);
281
282 // destination->advertise has been failing, but we want to withdraw
283 destination->advertiseHistory.clear();
284 destination->withdrawHistory.clear();
285 this->eraseRoute("/A/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT);
286 this->advanceClocks(time::seconds(10), time::seconds(300));
287 BOOST_CHECK_EQUAL(destination->advertiseHistory.size(), 0); // don't try to advertise
288 BOOST_CHECK_GT(destination->withdrawHistory.size(), 0); // try to withdraw
289
290 // destination->withdraw has been failing, but we want to advertise
291 destination->advertiseHistory.clear();
292 destination->withdrawHistory.clear();
293 this->insertRoute("/A/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT);
294 this->advanceClocks(time::seconds(10), time::seconds(300));
295 BOOST_CHECK_GT(destination->advertiseHistory.size(), 0); // try to advertise
296 BOOST_CHECK_EQUAL(destination->withdrawHistory.size(), 0); // don't try to withdraw
297}
298
299BOOST_AUTO_TEST_SUITE_END() // TestReadvertise
300BOOST_AUTO_TEST_SUITE_END() // Readvertise
301
302} // namespace tests
303} // namespace rib
304} // namespace nfd