blob: 48164bd2edf5e40cc18e753fbffb084b7b44ef2e [file] [log] [blame]
Nick Gordon94719252016-10-20 20:48:01 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoe1bdc082018-10-11 21:20:23 -04002/*
3 * Copyright (c) 2014-2018, 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
Nick Gordon94719252016-10-20 20:48:01 +000026#include "rib/readvertise/nfd-rib-readvertise-destination.hpp"
27
Junxiao Shi63b67e22017-03-06 19:46:02 +000028#include "tests/identity-management-fixture.hpp"
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040029#include "tests/test-common.hpp"
30
Nick Gordon94719252016-10-20 20:48:01 +000031#include <ndn-cxx/security/signing-info.hpp>
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040032#include <ndn-cxx/util/dummy-client-face.hpp>
Nick Gordon94719252016-10-20 20:48:01 +000033
Nick Gordon94719252016-10-20 20:48:01 +000034namespace nfd {
35namespace rib {
36namespace tests {
37
38using namespace nfd::tests;
39
40class NfdRibReadvertiseDestinationFixture : public IdentityManagementTimeFixture
41{
42public:
43 NfdRibReadvertiseDestinationFixture()
44 : nSuccessCallbacks(0)
45 , nFailureCallbacks(0)
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040046 , face(g_io, m_keyChain, {true, false})
47 , scheduler(g_io)
Nick Gordon94719252016-10-20 20:48:01 +000048 , controller(face, m_keyChain)
Yanbiao Lif48d0802018-06-01 03:00:02 -070049 , dest(controller, rib, ndn::nfd::CommandOptions().setPrefix("/localhost/nlsr"))
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040050 , successCallback([this] { nSuccessCallbacks++; })
Yanbiao Lif48d0802018-06-01 03:00:02 -070051 , failureCallback([this] (const std::string& str) { nFailureCallbacks++; })
Nick Gordon94719252016-10-20 20:48:01 +000052 {
53 }
54
55public:
56 uint32_t nSuccessCallbacks;
57 uint32_t nFailureCallbacks;
58
59protected:
60 ndn::util::DummyClientFace face;
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040061 ndn::util::Scheduler scheduler;
Nick Gordon94719252016-10-20 20:48:01 +000062 ndn::nfd::Controller controller;
Nick Gordon04262d92017-01-31 12:27:06 -060063 Rib rib;
Nick Gordon94719252016-10-20 20:48:01 +000064 NfdRibReadvertiseDestination dest;
65 std::function<void()> successCallback;
66 std::function<void(const std::string&)> failureCallback;
67};
68
Junxiao Shi63b67e22017-03-06 19:46:02 +000069BOOST_AUTO_TEST_SUITE(Readvertise)
Nick Gordon94719252016-10-20 20:48:01 +000070BOOST_FIXTURE_TEST_SUITE(TestNfdRibReadvertiseDestination, NfdRibReadvertiseDestinationFixture)
71
72class AdvertiseSuccessScenario
73{
74public:
75 ndn::nfd::ControlResponse
76 makeResponse(const ControlParameters& sentCp)
77 {
78 ControlParameters response;
79
80 response.setFaceId(1)
81 .setName(sentCp.getName())
82 .setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT)
83 .setCost(0)
84 .setFlags(ndn::nfd::ROUTE_FLAG_CHILD_INHERIT);
85
86 ndn::nfd::ControlResponse responsePayload;
87 responsePayload.setCode(200)
88 .setText("Successfully registered.")
89 .setBody(response.wireEncode());
90 return responsePayload;
91 }
92
93 void
94 checkCommandOutcome(NfdRibReadvertiseDestinationFixture* fixture)
95 {
96 BOOST_CHECK_EQUAL(fixture->nSuccessCallbacks, 1);
97 BOOST_CHECK_EQUAL(fixture->nFailureCallbacks, 0);
98 }
99};
100
101class AdvertiseFailureScenario
102{
103public:
104 ndn::nfd::ControlResponse
105 makeResponse(ControlParameters sentCp)
106 {
107 ndn::nfd::ControlResponse responsePayload(403, "Not Authenticated");
108 return responsePayload;
109 }
110
111 void
112 checkCommandOutcome(NfdRibReadvertiseDestinationFixture* fixture)
113 {
114 BOOST_CHECK_EQUAL(fixture->nFailureCallbacks, 1);
115 BOOST_CHECK_EQUAL(fixture->nSuccessCallbacks, 0);
116 }
117};
118
Junxiao Shi63b67e22017-03-06 19:46:02 +0000119using AdvertiseScenarios = boost::mpl::vector<AdvertiseSuccessScenario, AdvertiseFailureScenario>;
Nick Gordon94719252016-10-20 20:48:01 +0000120
121BOOST_AUTO_TEST_CASE_TEMPLATE(Advertise, Scenario, AdvertiseScenarios)
122{
123 Scenario scenario;
124 Name prefix("/ndn/memphis/test");
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400125 ReadvertisedRoute rr(prefix, scheduler);
Junxiao Shi63b67e22017-03-06 19:46:02 +0000126 const Name RIB_REGISTER_COMMAND_PREFIX("/localhost/nlsr/rib/register");
Nick Gordon94719252016-10-20 20:48:01 +0000127
128 dest.advertise(rr, successCallback, failureCallback);
129 advanceClocks(time::milliseconds(100));
130
131 // Retrieve the sent Interest to build the response
132 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
133 const Interest& sentInterest = face.sentInterests[0];
134 BOOST_CHECK(RIB_REGISTER_COMMAND_PREFIX.isPrefixOf(sentInterest.getName()));
135
136 // Parse the sent command Interest to check correctness.
137 ControlParameters sentCp;
138 BOOST_CHECK_NO_THROW(sentCp.wireDecode(sentInterest.getName().get(RIB_REGISTER_COMMAND_PREFIX.size()).blockFromValue()));
139 BOOST_CHECK_EQUAL(sentCp.getOrigin(), ndn::nfd::ROUTE_ORIGIN_CLIENT);
140 BOOST_CHECK_EQUAL(sentCp.getName(), prefix);
141
142 ndn::nfd::ControlResponse responsePayload = scenario.makeResponse(sentCp);
143 auto responseData = makeData(sentInterest.getName());
144 responseData->setContent(responsePayload.wireEncode());
145 face.receive(*responseData);
146 this->advanceClocks(time::milliseconds(10));
147
148 scenario.checkCommandOutcome(this);
149}
150
151class WithdrawSuccessScenario
152{
153public:
154 ndn::nfd::ControlResponse
155 makeResponse(const ControlParameters& sentCp)
156 {
157 ControlParameters response;
158
159 response.setFaceId(1)
160 .setName(sentCp.getName())
161 .setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT);
162
163 ndn::nfd::ControlResponse responsePayload;
164 responsePayload.setCode(200)
165 .setText("Successfully removed")
166 .setBody(response.wireEncode());
167
168 return responsePayload;
169 }
170
171 void
172 checkCommandOutcome(NfdRibReadvertiseDestinationFixture* fixture)
173 {
174 BOOST_CHECK_EQUAL(fixture->nSuccessCallbacks, 1);
175 BOOST_CHECK_EQUAL(fixture->nFailureCallbacks, 0);
176 }
177};
178
179class WithdrawFailureScenario
180{
181public:
182 ndn::nfd::ControlResponse
183 makeResponse(ControlParameters sentCp)
184 {
185 ndn::nfd::ControlResponse responsePayload(403, "Not authenticated");
186 return responsePayload;
187 }
188
189 void
190 checkCommandOutcome(NfdRibReadvertiseDestinationFixture* fixture)
191 {
192 BOOST_CHECK_EQUAL(fixture->nFailureCallbacks, 1);
193 BOOST_CHECK_EQUAL(fixture->nSuccessCallbacks, 0);
194 }
195};
196
Junxiao Shi63b67e22017-03-06 19:46:02 +0000197using WithdrawScenarios = boost::mpl::vector<WithdrawSuccessScenario, WithdrawFailureScenario>;
Nick Gordon94719252016-10-20 20:48:01 +0000198
199BOOST_AUTO_TEST_CASE_TEMPLATE(Withdraw, Scenario, WithdrawScenarios)
200{
201 Scenario scenario;
202 Name prefix("/ndn/memphis/test");
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400203 ReadvertisedRoute rr(prefix, scheduler);
Junxiao Shi63b67e22017-03-06 19:46:02 +0000204 const Name RIB_UNREGISTER_COMMAND_PREFIX("/localhost/nlsr/rib/unregister");
Nick Gordon94719252016-10-20 20:48:01 +0000205
206 dest.withdraw(rr, successCallback, failureCallback);
207 this->advanceClocks(time::milliseconds(10));
208
209 // Retrieve the sent Interest to build the response
210 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
211 const Interest& sentInterest = face.sentInterests[0];
212 BOOST_CHECK(RIB_UNREGISTER_COMMAND_PREFIX.isPrefixOf(sentInterest.getName()));
213
214 ControlParameters sentCp;
215 BOOST_CHECK_NO_THROW(sentCp.wireDecode(sentInterest.getName().get(RIB_UNREGISTER_COMMAND_PREFIX.size()).blockFromValue()));
216 BOOST_CHECK_EQUAL(sentCp.getOrigin(), ndn::nfd::ROUTE_ORIGIN_CLIENT);
217 BOOST_CHECK_EQUAL(sentCp.getName(), prefix);
218
219 ndn::nfd::ControlResponse responsePayload = scenario.makeResponse(sentCp);
220 auto responseData = makeData(sentInterest.getName());
221 responseData->setContent(responsePayload.wireEncode());
222
223 face.receive(*responseData);
224 this->advanceClocks(time::milliseconds(1));
225
226 scenario.checkCommandOutcome(this);
227}
228
Nick Gordon04262d92017-01-31 12:27:06 -0600229BOOST_AUTO_TEST_CASE(DestinationAvailability)
230{
231 std::vector<bool> availabilityChangeHistory;
Junxiao Shi63b67e22017-03-06 19:46:02 +0000232 Name commandPrefix("/localhost/nlsr");
Nick Gordon04262d92017-01-31 12:27:06 -0600233 Route route;
234
235 dest.afterAvailabilityChange.connect(
236 std::bind(&std::vector<bool>::push_back, &availabilityChangeHistory, _1));
237 BOOST_CHECK_EQUAL(dest.isAvailable(), false);
238
Junxiao Shi63b67e22017-03-06 19:46:02 +0000239 rib.insert(commandPrefix, route);
240 this->advanceClocks(time::milliseconds(100));
Nick Gordon04262d92017-01-31 12:27:06 -0600241 BOOST_CHECK_EQUAL(dest.isAvailable(), true);
Junxiao Shi63b67e22017-03-06 19:46:02 +0000242 BOOST_REQUIRE_EQUAL(availabilityChangeHistory.size(), 1);
Nick Gordon04262d92017-01-31 12:27:06 -0600243 BOOST_CHECK_EQUAL(availabilityChangeHistory.back(), true);
244
Junxiao Shi63b67e22017-03-06 19:46:02 +0000245 rib.erase(commandPrefix, route);
246 this->advanceClocks(time::milliseconds(100));
Nick Gordon04262d92017-01-31 12:27:06 -0600247 BOOST_CHECK_EQUAL(dest.isAvailable(), false);
Junxiao Shi63b67e22017-03-06 19:46:02 +0000248 BOOST_REQUIRE_EQUAL(availabilityChangeHistory.size(), 2);
Nick Gordon04262d92017-01-31 12:27:06 -0600249 BOOST_CHECK_EQUAL(availabilityChangeHistory.back(), false);
250}
251
Nick Gordon94719252016-10-20 20:48:01 +0000252BOOST_AUTO_TEST_SUITE_END() // TestNfdRibReadvertiseDestination
Junxiao Shi63b67e22017-03-06 19:46:02 +0000253BOOST_AUTO_TEST_SUITE_END() // Readvertise
Nick Gordon94719252016-10-20 20:48:01 +0000254
255} // namespace tests
256} // namespace rib
257} // namespace nfd