blob: 090bd5a1063abbc99c24a1212370b4f656b3020d [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 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 Pesavento87fc0f82018-04-11 23:43:51 -040046 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 Pesavento87fc0f82018-04-11 23:43:51 -040059 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 {
107 time::steady_clock::TimePoint timestamp;
108 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);
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000136 this->advanceClocks(time::milliseconds(6));
137 }
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);
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000146 this->advanceClocks(time::milliseconds(6));
147 }
148
149 void
150 setDestinationAvailability(bool isAvailable)
151 {
152 destination->setAvailability(isAvailable);
153 this->advanceClocks(time::milliseconds(6));
154 }
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();
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