blob: 2f1e27a54ae3656bba367d884e83f55b906f8702 [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/*
Davide Pesavento1d2d3372025-01-14 12:04:12 -05003 * Copyright (c) 2014-2025, 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
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040028#include "tests/test-common.hpp"
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040029#include "tests/key-chain-fixture.hpp"
30#include "tests/daemon/global-io-fixture.hpp"
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040031
Davide Pesavento1d2d3372025-01-14 12:04:12 -050032#include <ndn-cxx/mgmt/nfd/control-command.hpp>
Nick Gordon94719252016-10-20 20:48:01 +000033#include <ndn-cxx/security/signing-info.hpp>
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040034#include <ndn-cxx/util/dummy-client-face.hpp>
Nick Gordon94719252016-10-20 20:48:01 +000035
Davide Pesaventof56cf632024-01-27 22:22:06 -050036#include <boost/mp11/list.hpp>
37
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040038namespace nfd::tests {
Nick Gordon94719252016-10-20 20:48:01 +000039
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040040using namespace nfd::rib;
Nick Gordon94719252016-10-20 20:48:01 +000041
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040042class NfdRibReadvertiseDestinationFixture : public GlobalIoTimeFixture, public KeyChainFixture
Nick Gordon94719252016-10-20 20:48:01 +000043{
Davide Pesavento1d2d3372025-01-14 12:04:12 -050044protected:
Nick Gordon94719252016-10-20 20:48:01 +000045 NfdRibReadvertiseDestinationFixture()
46 : nSuccessCallbacks(0)
47 , nFailureCallbacks(0)
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040048 , face(g_io, m_keyChain, {true, false})
Nick Gordon94719252016-10-20 20:48:01 +000049 , controller(face, m_keyChain)
Yanbiao Lif48d0802018-06-01 03:00:02 -070050 , dest(controller, rib, ndn::nfd::CommandOptions().setPrefix("/localhost/nlsr"))
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040051 , successCallback([this] { nSuccessCallbacks++; })
Davide Pesavento0a71dd32019-03-17 20:36:18 -040052 , failureCallback([this] (const std::string&) { nFailureCallbacks++; })
Nick Gordon94719252016-10-20 20:48:01 +000053 {
54 }
55
56public:
57 uint32_t nSuccessCallbacks;
58 uint32_t nFailureCallbacks;
59
60protected:
Davide Pesavento1d2d3372025-01-14 12:04:12 -050061 static inline const Name RIB_REGISTER_COMMAND_PREFIX = Name("/localhost/nlsr")
62 .append(ndn::nfd::RibRegisterCommand::getName());
63 static inline const Name RIB_UNREGISTER_COMMAND_PREFIX = Name("/localhost/nlsr")
64 .append(ndn::nfd::RibUnregisterCommand::getName());
65
Davide Pesaventoae430302023-05-11 01:42:46 -040066 ndn::DummyClientFace face;
Nick Gordon94719252016-10-20 20:48:01 +000067 ndn::nfd::Controller controller;
Nick Gordon04262d92017-01-31 12:27:06 -060068 Rib rib;
Nick Gordon94719252016-10-20 20:48:01 +000069 NfdRibReadvertiseDestination dest;
70 std::function<void()> successCallback;
71 std::function<void(const std::string&)> failureCallback;
72};
73
Davide Pesaventocaa60cc2024-02-18 18:18:37 -050074BOOST_AUTO_TEST_SUITE(Rib)
Nick Gordon94719252016-10-20 20:48:01 +000075BOOST_FIXTURE_TEST_SUITE(TestNfdRibReadvertiseDestination, NfdRibReadvertiseDestinationFixture)
76
77class AdvertiseSuccessScenario
78{
79public:
80 ndn::nfd::ControlResponse
81 makeResponse(const ControlParameters& sentCp)
82 {
83 ControlParameters response;
84
85 response.setFaceId(1)
86 .setName(sentCp.getName())
87 .setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT)
88 .setCost(0)
89 .setFlags(ndn::nfd::ROUTE_FLAG_CHILD_INHERIT);
90
91 ndn::nfd::ControlResponse responsePayload;
92 responsePayload.setCode(200)
93 .setText("Successfully registered.")
94 .setBody(response.wireEncode());
95 return responsePayload;
96 }
97
98 void
99 checkCommandOutcome(NfdRibReadvertiseDestinationFixture* fixture)
100 {
101 BOOST_CHECK_EQUAL(fixture->nSuccessCallbacks, 1);
102 BOOST_CHECK_EQUAL(fixture->nFailureCallbacks, 0);
103 }
104};
105
106class AdvertiseFailureScenario
107{
108public:
109 ndn::nfd::ControlResponse
110 makeResponse(ControlParameters sentCp)
111 {
112 ndn::nfd::ControlResponse responsePayload(403, "Not Authenticated");
113 return responsePayload;
114 }
115
116 void
117 checkCommandOutcome(NfdRibReadvertiseDestinationFixture* fixture)
118 {
119 BOOST_CHECK_EQUAL(fixture->nFailureCallbacks, 1);
120 BOOST_CHECK_EQUAL(fixture->nSuccessCallbacks, 0);
121 }
122};
123
Davide Pesaventof56cf632024-01-27 22:22:06 -0500124using AdvertiseScenarios = boost::mp11::mp_list<AdvertiseSuccessScenario, AdvertiseFailureScenario>;
Nick Gordon94719252016-10-20 20:48:01 +0000125
126BOOST_AUTO_TEST_CASE_TEMPLATE(Advertise, Scenario, AdvertiseScenarios)
127{
128 Scenario scenario;
129 Name prefix("/ndn/memphis/test");
Junxiao Shifeddc3c2019-01-17 19:06:00 +0000130 ReadvertisedRoute rr(prefix);
Nick Gordon94719252016-10-20 20:48:01 +0000131
132 dest.advertise(rr, successCallback, failureCallback);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400133 advanceClocks(100_ms);
Nick Gordon94719252016-10-20 20:48:01 +0000134
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);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400150 this->advanceClocks(10_ms);
Nick Gordon94719252016-10-20 20:48:01 +0000151
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
Davide Pesaventof56cf632024-01-27 22:22:06 -0500201using WithdrawScenarios = boost::mp11::mp_list<WithdrawSuccessScenario, WithdrawFailureScenario>;
Nick Gordon94719252016-10-20 20:48:01 +0000202
203BOOST_AUTO_TEST_CASE_TEMPLATE(Withdraw, Scenario, WithdrawScenarios)
204{
205 Scenario scenario;
206 Name prefix("/ndn/memphis/test");
Junxiao Shifeddc3c2019-01-17 19:06:00 +0000207 ReadvertisedRoute rr(prefix);
Nick Gordon94719252016-10-20 20:48:01 +0000208
209 dest.withdraw(rr, successCallback, failureCallback);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400210 this->advanceClocks(10_ms);
Nick Gordon94719252016-10-20 20:48:01 +0000211
212 // Retrieve the sent Interest to build the response
213 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
214 const Interest& sentInterest = face.sentInterests[0];
215 BOOST_CHECK(RIB_UNREGISTER_COMMAND_PREFIX.isPrefixOf(sentInterest.getName()));
216
217 ControlParameters sentCp;
218 BOOST_CHECK_NO_THROW(sentCp.wireDecode(sentInterest.getName().get(RIB_UNREGISTER_COMMAND_PREFIX.size()).blockFromValue()));
219 BOOST_CHECK_EQUAL(sentCp.getOrigin(), ndn::nfd::ROUTE_ORIGIN_CLIENT);
220 BOOST_CHECK_EQUAL(sentCp.getName(), prefix);
221
222 ndn::nfd::ControlResponse responsePayload = scenario.makeResponse(sentCp);
223 auto responseData = makeData(sentInterest.getName());
224 responseData->setContent(responsePayload.wireEncode());
225
226 face.receive(*responseData);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400227 this->advanceClocks(1_ms);
Nick Gordon94719252016-10-20 20:48:01 +0000228
229 scenario.checkCommandOutcome(this);
230}
231
Nick Gordon04262d92017-01-31 12:27:06 -0600232BOOST_AUTO_TEST_CASE(DestinationAvailability)
233{
234 std::vector<bool> availabilityChangeHistory;
Junxiao Shi63b67e22017-03-06 19:46:02 +0000235 Name commandPrefix("/localhost/nlsr");
Nick Gordon04262d92017-01-31 12:27:06 -0600236 Route route;
237
238 dest.afterAvailabilityChange.connect(
239 std::bind(&std::vector<bool>::push_back, &availabilityChangeHistory, _1));
240 BOOST_CHECK_EQUAL(dest.isAvailable(), false);
241
Junxiao Shi63b67e22017-03-06 19:46:02 +0000242 rib.insert(commandPrefix, route);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400243 this->advanceClocks(100_ms);
Nick Gordon04262d92017-01-31 12:27:06 -0600244 BOOST_CHECK_EQUAL(dest.isAvailable(), true);
Junxiao Shi63b67e22017-03-06 19:46:02 +0000245 BOOST_REQUIRE_EQUAL(availabilityChangeHistory.size(), 1);
Nick Gordon04262d92017-01-31 12:27:06 -0600246 BOOST_CHECK_EQUAL(availabilityChangeHistory.back(), true);
247
Junxiao Shi63b67e22017-03-06 19:46:02 +0000248 rib.erase(commandPrefix, route);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400249 this->advanceClocks(100_ms);
Nick Gordon04262d92017-01-31 12:27:06 -0600250 BOOST_CHECK_EQUAL(dest.isAvailable(), false);
Junxiao Shi63b67e22017-03-06 19:46:02 +0000251 BOOST_REQUIRE_EQUAL(availabilityChangeHistory.size(), 2);
Nick Gordon04262d92017-01-31 12:27:06 -0600252 BOOST_CHECK_EQUAL(availabilityChangeHistory.back(), false);
253}
254
Nick Gordon94719252016-10-20 20:48:01 +0000255BOOST_AUTO_TEST_SUITE_END() // TestNfdRibReadvertiseDestination
Davide Pesaventocaa60cc2024-02-18 18:18:37 -0500256BOOST_AUTO_TEST_SUITE_END() // Rib
Nick Gordon94719252016-10-20 20:48:01 +0000257
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400258} // namespace nfd::tests