blob: 61cfb62093c77decefe1a8e0fb2c7dfde625571e [file] [log] [blame]
Nick Gordon94719252016-10-20 20:48:01 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Nick Gordon04262d92017-01-31 12:27:06 -06003 * Copyright (c) 2014-2017, Regents of the University of California,
Nick Gordon94719252016-10-20 20:48:01 +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 "tests/test-common.hpp"
27
28#include "rib/readvertise/nfd-rib-readvertise-destination.hpp"
29
30#include <ndn-cxx/util/dummy-client-face.hpp>
31#include <ndn-cxx/security/signing-info.hpp>
32
33#include "tests/identity-management-fixture.hpp"
34
35namespace nfd {
36namespace rib {
37namespace tests {
38
39using namespace nfd::tests;
40
41class NfdRibReadvertiseDestinationFixture : public IdentityManagementTimeFixture
42{
43public:
44 NfdRibReadvertiseDestinationFixture()
45 : nSuccessCallbacks(0)
46 , nFailureCallbacks(0)
47 , face(getGlobalIoService(), m_keyChain, {true, false})
48 , controller(face, m_keyChain)
Nick Gordon04262d92017-01-31 12:27:06 -060049 , dest(controller, Name("/localhop/nfd/rib/readvertise"), rib)
Nick Gordon94719252016-10-20 20:48:01 +000050 , successCallback([this] () {
51 nSuccessCallbacks++;
52 })
53 , failureCallback([this] (const std::string& str) {
54 nFailureCallbacks++;
55 })
56 {
57 }
58
59public:
60 uint32_t nSuccessCallbacks;
61 uint32_t nFailureCallbacks;
62
63protected:
64 ndn::util::DummyClientFace face;
65 ndn::nfd::Controller controller;
Nick Gordon04262d92017-01-31 12:27:06 -060066 Rib rib;
Nick Gordon94719252016-10-20 20:48:01 +000067 NfdRibReadvertiseDestination dest;
68 std::function<void()> successCallback;
69 std::function<void(const std::string&)> failureCallback;
70};
71
72BOOST_FIXTURE_TEST_SUITE(TestNfdRibReadvertiseDestination, NfdRibReadvertiseDestinationFixture)
73
74class AdvertiseSuccessScenario
75{
76public:
77 ndn::nfd::ControlResponse
78 makeResponse(const ControlParameters& sentCp)
79 {
80 ControlParameters response;
81
82 response.setFaceId(1)
83 .setName(sentCp.getName())
84 .setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT)
85 .setCost(0)
86 .setFlags(ndn::nfd::ROUTE_FLAG_CHILD_INHERIT);
87
88 ndn::nfd::ControlResponse responsePayload;
89 responsePayload.setCode(200)
90 .setText("Successfully registered.")
91 .setBody(response.wireEncode());
92 return responsePayload;
93 }
94
95 void
96 checkCommandOutcome(NfdRibReadvertiseDestinationFixture* fixture)
97 {
98 BOOST_CHECK_EQUAL(fixture->nSuccessCallbacks, 1);
99 BOOST_CHECK_EQUAL(fixture->nFailureCallbacks, 0);
100 }
101};
102
103class AdvertiseFailureScenario
104{
105public:
106 ndn::nfd::ControlResponse
107 makeResponse(ControlParameters sentCp)
108 {
109 ndn::nfd::ControlResponse responsePayload(403, "Not Authenticated");
110 return responsePayload;
111 }
112
113 void
114 checkCommandOutcome(NfdRibReadvertiseDestinationFixture* fixture)
115 {
116 BOOST_CHECK_EQUAL(fixture->nFailureCallbacks, 1);
117 BOOST_CHECK_EQUAL(fixture->nSuccessCallbacks, 0);
118 }
119};
120
121typedef boost::mpl::vector<AdvertiseSuccessScenario, AdvertiseFailureScenario> AdvertiseScenarios;
122
123BOOST_AUTO_TEST_CASE_TEMPLATE(Advertise, Scenario, AdvertiseScenarios)
124{
125 Scenario scenario;
126 Name prefix("/ndn/memphis/test");
127 ndn::security::SigningInfo secInfo;
128 std::vector<RibRouteRef> routes;
129 ReadvertisedRoute rr(prefix, secInfo, routes);
130 const Name RIB_REGISTER_COMMAND_PREFIX("/localhost/nfd/rib/register");
131
132 dest.advertise(rr, successCallback, failureCallback);
133 advanceClocks(time::milliseconds(100));
134
135 // Retrieve the sent Interest to build the response
136 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
137 const Interest& sentInterest = face.sentInterests[0];
138 BOOST_CHECK(RIB_REGISTER_COMMAND_PREFIX.isPrefixOf(sentInterest.getName()));
139
140 // Parse the sent command Interest to check correctness.
141 ControlParameters sentCp;
142 BOOST_CHECK_NO_THROW(sentCp.wireDecode(sentInterest.getName().get(RIB_REGISTER_COMMAND_PREFIX.size()).blockFromValue()));
143 BOOST_CHECK_EQUAL(sentCp.getOrigin(), ndn::nfd::ROUTE_ORIGIN_CLIENT);
144 BOOST_CHECK_EQUAL(sentCp.getName(), prefix);
145
146 ndn::nfd::ControlResponse responsePayload = scenario.makeResponse(sentCp);
147 auto responseData = makeData(sentInterest.getName());
148 responseData->setContent(responsePayload.wireEncode());
149 face.receive(*responseData);
150 this->advanceClocks(time::milliseconds(10));
151
152 scenario.checkCommandOutcome(this);
153}
154
155class WithdrawSuccessScenario
156{
157public:
158 ndn::nfd::ControlResponse
159 makeResponse(const ControlParameters& sentCp)
160 {
161 ControlParameters response;
162
163 response.setFaceId(1)
164 .setName(sentCp.getName())
165 .setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT);
166
167 ndn::nfd::ControlResponse responsePayload;
168 responsePayload.setCode(200)
169 .setText("Successfully removed")
170 .setBody(response.wireEncode());
171
172 return responsePayload;
173 }
174
175 void
176 checkCommandOutcome(NfdRibReadvertiseDestinationFixture* fixture)
177 {
178 BOOST_CHECK_EQUAL(fixture->nSuccessCallbacks, 1);
179 BOOST_CHECK_EQUAL(fixture->nFailureCallbacks, 0);
180 }
181};
182
183class WithdrawFailureScenario
184{
185public:
186 ndn::nfd::ControlResponse
187 makeResponse(ControlParameters sentCp)
188 {
189 ndn::nfd::ControlResponse responsePayload(403, "Not authenticated");
190 return responsePayload;
191 }
192
193 void
194 checkCommandOutcome(NfdRibReadvertiseDestinationFixture* fixture)
195 {
196 BOOST_CHECK_EQUAL(fixture->nFailureCallbacks, 1);
197 BOOST_CHECK_EQUAL(fixture->nSuccessCallbacks, 0);
198 }
199};
200
201typedef boost::mpl::vector<WithdrawSuccessScenario, WithdrawFailureScenario> WithdrawScenarios;
202
203BOOST_AUTO_TEST_CASE_TEMPLATE(Withdraw, Scenario, WithdrawScenarios)
204{
205 Scenario scenario;
206 Name prefix("/ndn/memphis/test");
207 ndn::security::SigningInfo secInfo;
208 std::vector<RibRouteRef> routes;
209 ReadvertisedRoute rr(prefix, secInfo, routes);
210 const Name RIB_UNREGISTER_COMMAND_PREFIX("/localhost/nfd/rib/unregister");
211
212 dest.withdraw(rr, successCallback, failureCallback);
213 this->advanceClocks(time::milliseconds(10));
214
215 // Retrieve the sent Interest to build the response
216 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
217 const Interest& sentInterest = face.sentInterests[0];
218 BOOST_CHECK(RIB_UNREGISTER_COMMAND_PREFIX.isPrefixOf(sentInterest.getName()));
219
220 ControlParameters sentCp;
221 BOOST_CHECK_NO_THROW(sentCp.wireDecode(sentInterest.getName().get(RIB_UNREGISTER_COMMAND_PREFIX.size()).blockFromValue()));
222 BOOST_CHECK_EQUAL(sentCp.getOrigin(), ndn::nfd::ROUTE_ORIGIN_CLIENT);
223 BOOST_CHECK_EQUAL(sentCp.getName(), prefix);
224
225 ndn::nfd::ControlResponse responsePayload = scenario.makeResponse(sentCp);
226 auto responseData = makeData(sentInterest.getName());
227 responseData->setContent(responsePayload.wireEncode());
228
229 face.receive(*responseData);
230 this->advanceClocks(time::milliseconds(1));
231
232 scenario.checkCommandOutcome(this);
233}
234
Nick Gordon04262d92017-01-31 12:27:06 -0600235BOOST_AUTO_TEST_CASE(DestinationAvailability)
236{
237 std::vector<bool> availabilityChangeHistory;
238 Name prefix("/localhop/nfd/rib/readvertise");
239 Route route;
240
241 dest.afterAvailabilityChange.connect(
242 std::bind(&std::vector<bool>::push_back, &availabilityChangeHistory, _1));
243 BOOST_CHECK_EQUAL(dest.isAvailable(), false);
244
245 rib.insert(prefix, route);
246 this->advanceClocks(time::milliseconds(100), 1);
247 BOOST_CHECK_EQUAL(dest.isAvailable(), true);
248 BOOST_CHECK_EQUAL(availabilityChangeHistory.size(), 1);
249 BOOST_CHECK_EQUAL(availabilityChangeHistory.back(), true);
250
251 rib.erase(prefix, route);
252
253 this->advanceClocks(time::milliseconds(100), 1);
254 BOOST_CHECK_EQUAL(dest.isAvailable(), false);
255 BOOST_CHECK_EQUAL(availabilityChangeHistory.size(), 2);
256 BOOST_CHECK_EQUAL(availabilityChangeHistory.back(), false);
257}
258
Nick Gordon94719252016-10-20 20:48:01 +0000259BOOST_AUTO_TEST_SUITE_END() // TestNfdRibReadvertiseDestination
260
261} // namespace tests
262} // namespace rib
263} // namespace nfd