blob: 85624c2ac2e0d727b27879ce6015ec85c86be588 [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 Pesaventoae430302023-05-11 01:42:46 -04003 * Copyright (c) 2014-2023, 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
Nick Gordon94719252016-10-20 20:48:01 +000032#include <ndn-cxx/security/signing-info.hpp>
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040033#include <ndn-cxx/util/dummy-client-face.hpp>
Nick Gordon94719252016-10-20 20:48:01 +000034
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040035namespace nfd::tests {
Nick Gordon94719252016-10-20 20:48:01 +000036
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040037using namespace nfd::rib;
Nick Gordon94719252016-10-20 20:48:01 +000038
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040039class NfdRibReadvertiseDestinationFixture : public GlobalIoTimeFixture, public KeyChainFixture
Nick Gordon94719252016-10-20 20:48:01 +000040{
41public:
42 NfdRibReadvertiseDestinationFixture()
43 : nSuccessCallbacks(0)
44 , nFailureCallbacks(0)
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040045 , face(g_io, m_keyChain, {true, false})
Nick Gordon94719252016-10-20 20:48:01 +000046 , controller(face, m_keyChain)
Yanbiao Lif48d0802018-06-01 03:00:02 -070047 , dest(controller, rib, ndn::nfd::CommandOptions().setPrefix("/localhost/nlsr"))
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040048 , successCallback([this] { nSuccessCallbacks++; })
Davide Pesavento0a71dd32019-03-17 20:36:18 -040049 , failureCallback([this] (const std::string&) { nFailureCallbacks++; })
Nick Gordon94719252016-10-20 20:48:01 +000050 {
51 }
52
53public:
54 uint32_t nSuccessCallbacks;
55 uint32_t nFailureCallbacks;
56
57protected:
Davide Pesaventoae430302023-05-11 01:42:46 -040058 ndn::DummyClientFace face;
Nick Gordon94719252016-10-20 20:48:01 +000059 ndn::nfd::Controller controller;
Nick Gordon04262d92017-01-31 12:27:06 -060060 Rib rib;
Nick Gordon94719252016-10-20 20:48:01 +000061 NfdRibReadvertiseDestination dest;
62 std::function<void()> successCallback;
63 std::function<void(const std::string&)> failureCallback;
64};
65
Junxiao Shi63b67e22017-03-06 19:46:02 +000066BOOST_AUTO_TEST_SUITE(Readvertise)
Nick Gordon94719252016-10-20 20:48:01 +000067BOOST_FIXTURE_TEST_SUITE(TestNfdRibReadvertiseDestination, NfdRibReadvertiseDestinationFixture)
68
69class AdvertiseSuccessScenario
70{
71public:
72 ndn::nfd::ControlResponse
73 makeResponse(const ControlParameters& sentCp)
74 {
75 ControlParameters response;
76
77 response.setFaceId(1)
78 .setName(sentCp.getName())
79 .setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT)
80 .setCost(0)
81 .setFlags(ndn::nfd::ROUTE_FLAG_CHILD_INHERIT);
82
83 ndn::nfd::ControlResponse responsePayload;
84 responsePayload.setCode(200)
85 .setText("Successfully registered.")
86 .setBody(response.wireEncode());
87 return responsePayload;
88 }
89
90 void
91 checkCommandOutcome(NfdRibReadvertiseDestinationFixture* fixture)
92 {
93 BOOST_CHECK_EQUAL(fixture->nSuccessCallbacks, 1);
94 BOOST_CHECK_EQUAL(fixture->nFailureCallbacks, 0);
95 }
96};
97
98class AdvertiseFailureScenario
99{
100public:
101 ndn::nfd::ControlResponse
102 makeResponse(ControlParameters sentCp)
103 {
104 ndn::nfd::ControlResponse responsePayload(403, "Not Authenticated");
105 return responsePayload;
106 }
107
108 void
109 checkCommandOutcome(NfdRibReadvertiseDestinationFixture* fixture)
110 {
111 BOOST_CHECK_EQUAL(fixture->nFailureCallbacks, 1);
112 BOOST_CHECK_EQUAL(fixture->nSuccessCallbacks, 0);
113 }
114};
115
Junxiao Shi63b67e22017-03-06 19:46:02 +0000116using AdvertiseScenarios = boost::mpl::vector<AdvertiseSuccessScenario, AdvertiseFailureScenario>;
Nick Gordon94719252016-10-20 20:48:01 +0000117
118BOOST_AUTO_TEST_CASE_TEMPLATE(Advertise, Scenario, AdvertiseScenarios)
119{
120 Scenario scenario;
121 Name prefix("/ndn/memphis/test");
Junxiao Shifeddc3c2019-01-17 19:06:00 +0000122 ReadvertisedRoute rr(prefix);
Junxiao Shi63b67e22017-03-06 19:46:02 +0000123 const Name RIB_REGISTER_COMMAND_PREFIX("/localhost/nlsr/rib/register");
Nick Gordon94719252016-10-20 20:48:01 +0000124
125 dest.advertise(rr, successCallback, failureCallback);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400126 advanceClocks(100_ms);
Nick Gordon94719252016-10-20 20:48:01 +0000127
128 // Retrieve the sent Interest to build the response
129 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
130 const Interest& sentInterest = face.sentInterests[0];
131 BOOST_CHECK(RIB_REGISTER_COMMAND_PREFIX.isPrefixOf(sentInterest.getName()));
132
133 // Parse the sent command Interest to check correctness.
134 ControlParameters sentCp;
135 BOOST_CHECK_NO_THROW(sentCp.wireDecode(sentInterest.getName().get(RIB_REGISTER_COMMAND_PREFIX.size()).blockFromValue()));
136 BOOST_CHECK_EQUAL(sentCp.getOrigin(), ndn::nfd::ROUTE_ORIGIN_CLIENT);
137 BOOST_CHECK_EQUAL(sentCp.getName(), prefix);
138
139 ndn::nfd::ControlResponse responsePayload = scenario.makeResponse(sentCp);
140 auto responseData = makeData(sentInterest.getName());
141 responseData->setContent(responsePayload.wireEncode());
142 face.receive(*responseData);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400143 this->advanceClocks(10_ms);
Nick Gordon94719252016-10-20 20:48:01 +0000144
145 scenario.checkCommandOutcome(this);
146}
147
148class WithdrawSuccessScenario
149{
150public:
151 ndn::nfd::ControlResponse
152 makeResponse(const ControlParameters& sentCp)
153 {
154 ControlParameters response;
155
156 response.setFaceId(1)
157 .setName(sentCp.getName())
158 .setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT);
159
160 ndn::nfd::ControlResponse responsePayload;
161 responsePayload.setCode(200)
162 .setText("Successfully removed")
163 .setBody(response.wireEncode());
164
165 return responsePayload;
166 }
167
168 void
169 checkCommandOutcome(NfdRibReadvertiseDestinationFixture* fixture)
170 {
171 BOOST_CHECK_EQUAL(fixture->nSuccessCallbacks, 1);
172 BOOST_CHECK_EQUAL(fixture->nFailureCallbacks, 0);
173 }
174};
175
176class WithdrawFailureScenario
177{
178public:
179 ndn::nfd::ControlResponse
180 makeResponse(ControlParameters sentCp)
181 {
182 ndn::nfd::ControlResponse responsePayload(403, "Not authenticated");
183 return responsePayload;
184 }
185
186 void
187 checkCommandOutcome(NfdRibReadvertiseDestinationFixture* fixture)
188 {
189 BOOST_CHECK_EQUAL(fixture->nFailureCallbacks, 1);
190 BOOST_CHECK_EQUAL(fixture->nSuccessCallbacks, 0);
191 }
192};
193
Junxiao Shi63b67e22017-03-06 19:46:02 +0000194using WithdrawScenarios = boost::mpl::vector<WithdrawSuccessScenario, WithdrawFailureScenario>;
Nick Gordon94719252016-10-20 20:48:01 +0000195
196BOOST_AUTO_TEST_CASE_TEMPLATE(Withdraw, Scenario, WithdrawScenarios)
197{
198 Scenario scenario;
199 Name prefix("/ndn/memphis/test");
Junxiao Shifeddc3c2019-01-17 19:06:00 +0000200 ReadvertisedRoute rr(prefix);
Junxiao Shi63b67e22017-03-06 19:46:02 +0000201 const Name RIB_UNREGISTER_COMMAND_PREFIX("/localhost/nlsr/rib/unregister");
Nick Gordon94719252016-10-20 20:48:01 +0000202
203 dest.withdraw(rr, successCallback, failureCallback);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400204 this->advanceClocks(10_ms);
Nick Gordon94719252016-10-20 20:48:01 +0000205
206 // Retrieve the sent Interest to build the response
207 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
208 const Interest& sentInterest = face.sentInterests[0];
209 BOOST_CHECK(RIB_UNREGISTER_COMMAND_PREFIX.isPrefixOf(sentInterest.getName()));
210
211 ControlParameters sentCp;
212 BOOST_CHECK_NO_THROW(sentCp.wireDecode(sentInterest.getName().get(RIB_UNREGISTER_COMMAND_PREFIX.size()).blockFromValue()));
213 BOOST_CHECK_EQUAL(sentCp.getOrigin(), ndn::nfd::ROUTE_ORIGIN_CLIENT);
214 BOOST_CHECK_EQUAL(sentCp.getName(), prefix);
215
216 ndn::nfd::ControlResponse responsePayload = scenario.makeResponse(sentCp);
217 auto responseData = makeData(sentInterest.getName());
218 responseData->setContent(responsePayload.wireEncode());
219
220 face.receive(*responseData);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400221 this->advanceClocks(1_ms);
Nick Gordon94719252016-10-20 20:48:01 +0000222
223 scenario.checkCommandOutcome(this);
224}
225
Nick Gordon04262d92017-01-31 12:27:06 -0600226BOOST_AUTO_TEST_CASE(DestinationAvailability)
227{
228 std::vector<bool> availabilityChangeHistory;
Junxiao Shi63b67e22017-03-06 19:46:02 +0000229 Name commandPrefix("/localhost/nlsr");
Nick Gordon04262d92017-01-31 12:27:06 -0600230 Route route;
231
232 dest.afterAvailabilityChange.connect(
233 std::bind(&std::vector<bool>::push_back, &availabilityChangeHistory, _1));
234 BOOST_CHECK_EQUAL(dest.isAvailable(), false);
235
Junxiao Shi63b67e22017-03-06 19:46:02 +0000236 rib.insert(commandPrefix, route);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400237 this->advanceClocks(100_ms);
Nick Gordon04262d92017-01-31 12:27:06 -0600238 BOOST_CHECK_EQUAL(dest.isAvailable(), true);
Junxiao Shi63b67e22017-03-06 19:46:02 +0000239 BOOST_REQUIRE_EQUAL(availabilityChangeHistory.size(), 1);
Nick Gordon04262d92017-01-31 12:27:06 -0600240 BOOST_CHECK_EQUAL(availabilityChangeHistory.back(), true);
241
Junxiao Shi63b67e22017-03-06 19:46:02 +0000242 rib.erase(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(), false);
Junxiao Shi63b67e22017-03-06 19:46:02 +0000245 BOOST_REQUIRE_EQUAL(availabilityChangeHistory.size(), 2);
Nick Gordon04262d92017-01-31 12:27:06 -0600246 BOOST_CHECK_EQUAL(availabilityChangeHistory.back(), false);
247}
248
Nick Gordon94719252016-10-20 20:48:01 +0000249BOOST_AUTO_TEST_SUITE_END() // TestNfdRibReadvertiseDestination
Junxiao Shi63b67e22017-03-06 19:46:02 +0000250BOOST_AUTO_TEST_SUITE_END() // Readvertise
Nick Gordon94719252016-10-20 20:48:01 +0000251
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400252} // namespace nfd::tests