blob: 14949b80c6f21d3178937f83f425a5ea20cffcb2 [file] [log] [blame]
Junxiao Shi89c0ea02017-03-06 19:52:05 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2017, Regents of the University of California,
4 * 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
28#include "tests/test-common.hpp"
29#include "tests/identity-management-fixture.hpp"
30#include <ndn-cxx/mgmt/nfd/controller.hpp>
31#include <ndn-cxx/util/dummy-client-face.hpp>
32#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:
44 ndn::optional<ReadvertiseAction>
45 handleNewRoute(const RibRouteRef& route) const override
46 {
47 return this->decision;
48 }
49
50 time::milliseconds
51 getRefreshInterval() const override
52 {
53 return time::seconds(60);
54 }
55
56public:
57 ndn::optional<ReadvertiseAction> decision;
58};
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
114class ReadvertiseFixture : public IdentityManagementTimeFixture
115{
116public:
117 ReadvertiseFixture()
118 : face(getGlobalIoService(), m_keyChain, {false, false})
119 , controller(face, m_keyChain)
120 {
121 auto policyUnique = make_unique<DummyReadvertisePolicy>();
122 policy = policyUnique.get();
123 auto destinationUnique = make_unique<DummyReadvertiseDestination>();
124 destination = destinationUnique.get();
125 readvertise.reset(new Readvertise(rib, std::move(policyUnique), std::move(destinationUnique)));
126 }
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;
134 rib.insert(prefix, route);
135 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;
144 rib.erase(prefix, route);
145 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
155public:
156 ndn::KeyChain m_keyChain;
157 ndn::util::DummyClientFace face;
158 ndn::nfd::Controller controller;
159 DummyReadvertisePolicy* policy;
160 DummyReadvertiseDestination* destination;
161 Rib rib;
162 unique_ptr<Readvertise> readvertise;
163};
164
165BOOST_AUTO_TEST_SUITE(Readvertise)
166BOOST_FIXTURE_TEST_SUITE(TestReadvertise, ReadvertiseFixture)
167
168BOOST_AUTO_TEST_CASE(AddRemoveRoute)
169{
170 policy->decision = ReadvertiseAction{"/A", ndn::security::SigningInfo()};
171
172 // advertising /A
173 this->insertRoute("/A/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT);
174 BOOST_CHECK_EQUAL(destination->advertiseHistory.size(), 1);
175 BOOST_CHECK_EQUAL(destination->advertiseHistory.at(0).prefix, "/A");
176
177 // /A is already advertised
178 this->insertRoute("/A/2", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT);
179 BOOST_CHECK_EQUAL(destination->advertiseHistory.size(), 1);
180
181 // refresh every 60 seconds
182 destination->advertiseHistory.clear();
183 this->advanceClocks(time::seconds(61), 5);
184 BOOST_CHECK_EQUAL(destination->advertiseHistory.size(), 5);
185
186 // /A is still needed by /A/2 route
187 this->eraseRoute("/A/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT);
188 BOOST_CHECK_EQUAL(destination->withdrawHistory.size(), 0);
189
190 // withdrawing /A
191 this->eraseRoute("/A/2", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT);
192 BOOST_CHECK_EQUAL(destination->withdrawHistory.size(), 1);
193 BOOST_CHECK_EQUAL(destination->withdrawHistory.at(0).prefix, "/A");
194}
195
196BOOST_AUTO_TEST_CASE(NoAdvertise)
197{
198 policy->decision = ndn::nullopt;
199
200 this->insertRoute("/A/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT);
201 this->insertRoute("/A/2", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT);
202 this->eraseRoute("/A/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT);
203 this->eraseRoute("/A/2", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT);
204
205 BOOST_CHECK_EQUAL(destination->advertiseHistory.size(), 0);
206 BOOST_CHECK_EQUAL(destination->withdrawHistory.size(), 0);
207}
208
209BOOST_AUTO_TEST_CASE(DestinationAvailability)
210{
211 this->setDestinationAvailability(false);
212
213 policy->decision = ReadvertiseAction{"/A", ndn::security::SigningInfo()};
214 this->insertRoute("/A/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT);
215 policy->decision = ReadvertiseAction{"/B", ndn::security::SigningInfo()};
216 this->insertRoute("/B/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT);
217 BOOST_CHECK_EQUAL(destination->advertiseHistory.size(), 0);
218
219 this->setDestinationAvailability(true);
220 std::set<Name> advertisedPrefixes;
221 boost::copy(destination->advertiseHistory | boost::adaptors::transformed(
222 [] (const DummyReadvertiseDestination::HistoryEntry& he) { return he.prefix; }),
223 std::inserter(advertisedPrefixes, advertisedPrefixes.end()));
224 std::set<Name> expectedPrefixes{"/A", "/B"};
225 BOOST_CHECK_EQUAL_COLLECTIONS(advertisedPrefixes.begin(), advertisedPrefixes.end(),
226 expectedPrefixes.begin(), expectedPrefixes.end());
227 destination->advertiseHistory.clear();
228
229 this->setDestinationAvailability(false);
230 this->eraseRoute("/B/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT);
231 BOOST_CHECK_EQUAL(destination->withdrawHistory.size(), 0);
232
233 this->setDestinationAvailability(true);
234 BOOST_CHECK_EQUAL(destination->withdrawHistory.size(), 0);
235 BOOST_CHECK_EQUAL(destination->advertiseHistory.size(), 1);
236 BOOST_CHECK_EQUAL(destination->advertiseHistory.at(0).prefix, "/A");
237}
238
239BOOST_AUTO_TEST_CASE(AdvertiseRetryInterval)
240{
241 destination->shouldSucceed = false;
242
243 policy->decision = ReadvertiseAction{"/A", ndn::security::SigningInfo()};
244 this->insertRoute("/A/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT);
245
246 this->advanceClocks(time::seconds(10), time::seconds(3600));
247 BOOST_REQUIRE_GT(destination->advertiseHistory.size(), 2);
248
249 // destination->advertise keeps failing, so interval should increase
250 using FloatInterval = time::duration<float, time::steady_clock::Duration::period>;
251 FloatInterval initialInterval = destination->advertiseHistory[1].timestamp -
252 destination->advertiseHistory[0].timestamp;
253 FloatInterval lastInterval = initialInterval;
254 for (size_t i = 2; i < destination->advertiseHistory.size(); ++i) {
255 FloatInterval interval = destination->advertiseHistory[i].timestamp -
256 destination->advertiseHistory[i - 1].timestamp;
257 BOOST_CHECK_CLOSE(interval.count(), (lastInterval * 2.0).count(), 10.0);
258 lastInterval = interval;
259 }
260
261 destination->shouldSucceed = true;
262 this->advanceClocks(time::seconds(3600));
263 destination->advertiseHistory.clear();
264
265 // destination->advertise has succeeded, retry interval should reset to initial
266 destination->shouldSucceed = false;
267 this->advanceClocks(time::seconds(10), time::seconds(300));
268 BOOST_REQUIRE_GE(destination->advertiseHistory.size(), 2);
269 FloatInterval restartInterval = destination->advertiseHistory[1].timestamp -
270 destination->advertiseHistory[0].timestamp;
271 BOOST_CHECK_CLOSE(restartInterval.count(), initialInterval.count(), 10.0);
272}
273
274BOOST_AUTO_TEST_CASE(ChangeDuringRetry)
275{
276 destination->shouldSucceed = false;
277 policy->decision = ReadvertiseAction{"/A", ndn::security::SigningInfo()};
278 this->insertRoute("/A/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT);
279 this->advanceClocks(time::seconds(10), time::seconds(300));
280 BOOST_CHECK_GT(destination->advertiseHistory.size(), 0);
281 BOOST_CHECK_EQUAL(destination->withdrawHistory.size(), 0);
282
283 // destination->advertise has been failing, but we want to withdraw
284 destination->advertiseHistory.clear();
285 destination->withdrawHistory.clear();
286 this->eraseRoute("/A/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT);
287 this->advanceClocks(time::seconds(10), time::seconds(300));
288 BOOST_CHECK_EQUAL(destination->advertiseHistory.size(), 0); // don't try to advertise
289 BOOST_CHECK_GT(destination->withdrawHistory.size(), 0); // try to withdraw
290
291 // destination->withdraw has been failing, but we want to advertise
292 destination->advertiseHistory.clear();
293 destination->withdrawHistory.clear();
294 this->insertRoute("/A/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT);
295 this->advanceClocks(time::seconds(10), time::seconds(300));
296 BOOST_CHECK_GT(destination->advertiseHistory.size(), 0); // try to advertise
297 BOOST_CHECK_EQUAL(destination->withdrawHistory.size(), 0); // don't try to withdraw
298}
299
300BOOST_AUTO_TEST_SUITE_END() // TestReadvertise
301BOOST_AUTO_TEST_SUITE_END() // Readvertise
302
303} // namespace tests
304} // namespace rib
305} // namespace nfd