blob: e6d9988b179436940d69d2f0630f7ee7ee8198ce [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:
Davide Pesavento1d2d3372025-01-14 12:04:12 -050045 static inline const Name RIB_REGISTER_COMMAND_PREFIX = Name("/localhost/nlsr")
46 .append(ndn::nfd::RibRegisterCommand::getName());
47 static inline const Name RIB_UNREGISTER_COMMAND_PREFIX = Name("/localhost/nlsr")
48 .append(ndn::nfd::RibUnregisterCommand::getName());
49
Davide Pesavento4296fe32025-01-14 23:00:41 -050050 ndn::DummyClientFace face{g_io, m_keyChain, {true, false}};
51 ndn::nfd::Controller controller{face, m_keyChain};
Nick Gordon04262d92017-01-31 12:27:06 -060052 Rib rib;
Davide Pesavento4296fe32025-01-14 23:00:41 -050053 NfdRibReadvertiseDestination dest{controller, rib, ndn::nfd::CommandOptions().setPrefix("/localhost/nlsr")};
54
55 std::function<void()> successCallback = [this] { nSuccessCallbacks++; };
56 std::function<void(const std::string&)> failureCallback = [this] (auto&&) { nFailureCallbacks++; };
57
58 int nSuccessCallbacks = 0;
59 int nFailureCallbacks = 0;
Nick Gordon94719252016-10-20 20:48:01 +000060};
61
Davide Pesaventocaa60cc2024-02-18 18:18:37 -050062BOOST_AUTO_TEST_SUITE(Rib)
Nick Gordon94719252016-10-20 20:48:01 +000063BOOST_FIXTURE_TEST_SUITE(TestNfdRibReadvertiseDestination, NfdRibReadvertiseDestinationFixture)
64
Davide Pesavento4296fe32025-01-14 23:00:41 -050065struct AdvertiseSuccessScenario
Nick Gordon94719252016-10-20 20:48:01 +000066{
Davide Pesavento4296fe32025-01-14 23:00:41 -050067 static ndn::nfd::ControlResponse
Nick Gordon94719252016-10-20 20:48:01 +000068 makeResponse(const ControlParameters& sentCp)
69 {
70 ControlParameters response;
Nick Gordon94719252016-10-20 20:48:01 +000071 response.setFaceId(1)
72 .setName(sentCp.getName())
73 .setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT)
74 .setCost(0)
75 .setFlags(ndn::nfd::ROUTE_FLAG_CHILD_INHERIT);
76
77 ndn::nfd::ControlResponse responsePayload;
78 responsePayload.setCode(200)
79 .setText("Successfully registered.")
80 .setBody(response.wireEncode());
Davide Pesavento4296fe32025-01-14 23:00:41 -050081
Nick Gordon94719252016-10-20 20:48:01 +000082 return responsePayload;
83 }
84
Davide Pesavento4296fe32025-01-14 23:00:41 -050085 static constexpr int nExpectedSuccessCalls = 1;
86 static constexpr int nExpectedFailureCalls = 0;
Nick Gordon94719252016-10-20 20:48:01 +000087};
88
Davide Pesavento4296fe32025-01-14 23:00:41 -050089struct AdvertiseFailureScenario
Nick Gordon94719252016-10-20 20:48:01 +000090{
Davide Pesavento4296fe32025-01-14 23:00:41 -050091 static ndn::nfd::ControlResponse
92 makeResponse(const ControlParameters& sentCp)
Nick Gordon94719252016-10-20 20:48:01 +000093 {
Davide Pesavento4296fe32025-01-14 23:00:41 -050094 return ndn::nfd::ControlResponse(403, "Not Authenticated");
Nick Gordon94719252016-10-20 20:48:01 +000095 }
96
Davide Pesavento4296fe32025-01-14 23:00:41 -050097 static constexpr int nExpectedSuccessCalls = 0;
98 static constexpr int nExpectedFailureCalls = 1;
Nick Gordon94719252016-10-20 20:48:01 +000099};
100
Davide Pesaventof56cf632024-01-27 22:22:06 -0500101using AdvertiseScenarios = boost::mp11::mp_list<AdvertiseSuccessScenario, AdvertiseFailureScenario>;
Nick Gordon94719252016-10-20 20:48:01 +0000102
103BOOST_AUTO_TEST_CASE_TEMPLATE(Advertise, Scenario, AdvertiseScenarios)
104{
Nick Gordon94719252016-10-20 20:48:01 +0000105 Name prefix("/ndn/memphis/test");
Junxiao Shifeddc3c2019-01-17 19:06:00 +0000106 ReadvertisedRoute rr(prefix);
Nick Gordon94719252016-10-20 20:48:01 +0000107
108 dest.advertise(rr, successCallback, failureCallback);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400109 advanceClocks(100_ms);
Nick Gordon94719252016-10-20 20:48:01 +0000110
111 // Retrieve the sent Interest to build the response
112 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
113 const Interest& sentInterest = face.sentInterests[0];
114 BOOST_CHECK(RIB_REGISTER_COMMAND_PREFIX.isPrefixOf(sentInterest.getName()));
115
116 // Parse the sent command Interest to check correctness.
117 ControlParameters sentCp;
Davide Pesavento4296fe32025-01-14 23:00:41 -0500118 BOOST_CHECK_NO_THROW(sentCp.wireDecode(sentInterest.getName().get(RIB_REGISTER_COMMAND_PREFIX.size())
119 .blockFromValue()));
Nick Gordon94719252016-10-20 20:48:01 +0000120 BOOST_CHECK_EQUAL(sentCp.getOrigin(), ndn::nfd::ROUTE_ORIGIN_CLIENT);
121 BOOST_CHECK_EQUAL(sentCp.getName(), prefix);
122
Davide Pesavento4296fe32025-01-14 23:00:41 -0500123 ndn::nfd::ControlResponse responsePayload = Scenario::makeResponse(sentCp);
Nick Gordon94719252016-10-20 20:48:01 +0000124 auto responseData = makeData(sentInterest.getName());
125 responseData->setContent(responsePayload.wireEncode());
126 face.receive(*responseData);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400127 this->advanceClocks(10_ms);
Nick Gordon94719252016-10-20 20:48:01 +0000128
Davide Pesavento4296fe32025-01-14 23:00:41 -0500129 BOOST_TEST(nSuccessCallbacks == Scenario::nExpectedSuccessCalls);
130 BOOST_TEST(nFailureCallbacks == Scenario::nExpectedFailureCalls);
Nick Gordon94719252016-10-20 20:48:01 +0000131}
132
Davide Pesavento4296fe32025-01-14 23:00:41 -0500133struct WithdrawSuccessScenario
Nick Gordon94719252016-10-20 20:48:01 +0000134{
Davide Pesavento4296fe32025-01-14 23:00:41 -0500135 static ndn::nfd::ControlResponse
Nick Gordon94719252016-10-20 20:48:01 +0000136 makeResponse(const ControlParameters& sentCp)
137 {
138 ControlParameters response;
Nick Gordon94719252016-10-20 20:48:01 +0000139 response.setFaceId(1)
140 .setName(sentCp.getName())
141 .setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT);
142
143 ndn::nfd::ControlResponse responsePayload;
144 responsePayload.setCode(200)
145 .setText("Successfully removed")
146 .setBody(response.wireEncode());
147
148 return responsePayload;
149 }
150
Davide Pesavento4296fe32025-01-14 23:00:41 -0500151 static constexpr int nExpectedSuccessCalls = 1;
152 static constexpr int nExpectedFailureCalls = 0;
Nick Gordon94719252016-10-20 20:48:01 +0000153};
154
Davide Pesavento4296fe32025-01-14 23:00:41 -0500155struct WithdrawFailureScenario
Nick Gordon94719252016-10-20 20:48:01 +0000156{
Davide Pesavento4296fe32025-01-14 23:00:41 -0500157 static ndn::nfd::ControlResponse
158 makeResponse(const ControlParameters& sentCp)
Nick Gordon94719252016-10-20 20:48:01 +0000159 {
Davide Pesavento4296fe32025-01-14 23:00:41 -0500160 return ndn::nfd::ControlResponse(403, "Not authenticated");
Nick Gordon94719252016-10-20 20:48:01 +0000161 }
162
Davide Pesavento4296fe32025-01-14 23:00:41 -0500163 static constexpr int nExpectedSuccessCalls = 0;
164 static constexpr int nExpectedFailureCalls = 1;
Nick Gordon94719252016-10-20 20:48:01 +0000165};
166
Davide Pesaventof56cf632024-01-27 22:22:06 -0500167using WithdrawScenarios = boost::mp11::mp_list<WithdrawSuccessScenario, WithdrawFailureScenario>;
Nick Gordon94719252016-10-20 20:48:01 +0000168
169BOOST_AUTO_TEST_CASE_TEMPLATE(Withdraw, Scenario, WithdrawScenarios)
170{
Nick Gordon94719252016-10-20 20:48:01 +0000171 Name prefix("/ndn/memphis/test");
Junxiao Shifeddc3c2019-01-17 19:06:00 +0000172 ReadvertisedRoute rr(prefix);
Nick Gordon94719252016-10-20 20:48:01 +0000173
174 dest.withdraw(rr, successCallback, failureCallback);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400175 this->advanceClocks(10_ms);
Nick Gordon94719252016-10-20 20:48:01 +0000176
177 // Retrieve the sent Interest to build the response
178 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
179 const Interest& sentInterest = face.sentInterests[0];
180 BOOST_CHECK(RIB_UNREGISTER_COMMAND_PREFIX.isPrefixOf(sentInterest.getName()));
181
182 ControlParameters sentCp;
Davide Pesavento4296fe32025-01-14 23:00:41 -0500183 BOOST_CHECK_NO_THROW(sentCp.wireDecode(sentInterest.getName().get(RIB_UNREGISTER_COMMAND_PREFIX.size())
184 .blockFromValue()));
Nick Gordon94719252016-10-20 20:48:01 +0000185 BOOST_CHECK_EQUAL(sentCp.getOrigin(), ndn::nfd::ROUTE_ORIGIN_CLIENT);
186 BOOST_CHECK_EQUAL(sentCp.getName(), prefix);
187
Davide Pesavento4296fe32025-01-14 23:00:41 -0500188 ndn::nfd::ControlResponse responsePayload = Scenario::makeResponse(sentCp);
Nick Gordon94719252016-10-20 20:48:01 +0000189 auto responseData = makeData(sentInterest.getName());
190 responseData->setContent(responsePayload.wireEncode());
191
192 face.receive(*responseData);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400193 this->advanceClocks(1_ms);
Nick Gordon94719252016-10-20 20:48:01 +0000194
Davide Pesavento4296fe32025-01-14 23:00:41 -0500195 BOOST_TEST(nSuccessCallbacks == Scenario::nExpectedSuccessCalls);
196 BOOST_TEST(nFailureCallbacks == Scenario::nExpectedFailureCalls);
Nick Gordon94719252016-10-20 20:48:01 +0000197}
198
Nick Gordon04262d92017-01-31 12:27:06 -0600199BOOST_AUTO_TEST_CASE(DestinationAvailability)
200{
201 std::vector<bool> availabilityChangeHistory;
Junxiao Shi63b67e22017-03-06 19:46:02 +0000202 Name commandPrefix("/localhost/nlsr");
Nick Gordon04262d92017-01-31 12:27:06 -0600203 Route route;
204
Davide Pesavento4296fe32025-01-14 23:00:41 -0500205 dest.afterAvailabilityChange.connect([&] (bool val) { availabilityChangeHistory.push_back(val); });
Nick Gordon04262d92017-01-31 12:27:06 -0600206 BOOST_CHECK_EQUAL(dest.isAvailable(), false);
207
Junxiao Shi63b67e22017-03-06 19:46:02 +0000208 rib.insert(commandPrefix, route);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400209 this->advanceClocks(100_ms);
Nick Gordon04262d92017-01-31 12:27:06 -0600210 BOOST_CHECK_EQUAL(dest.isAvailable(), true);
Junxiao Shi63b67e22017-03-06 19:46:02 +0000211 BOOST_REQUIRE_EQUAL(availabilityChangeHistory.size(), 1);
Nick Gordon04262d92017-01-31 12:27:06 -0600212 BOOST_CHECK_EQUAL(availabilityChangeHistory.back(), true);
213
Junxiao Shi63b67e22017-03-06 19:46:02 +0000214 rib.erase(commandPrefix, route);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400215 this->advanceClocks(100_ms);
Nick Gordon04262d92017-01-31 12:27:06 -0600216 BOOST_CHECK_EQUAL(dest.isAvailable(), false);
Junxiao Shi63b67e22017-03-06 19:46:02 +0000217 BOOST_REQUIRE_EQUAL(availabilityChangeHistory.size(), 2);
Nick Gordon04262d92017-01-31 12:27:06 -0600218 BOOST_CHECK_EQUAL(availabilityChangeHistory.back(), false);
219}
220
Nick Gordon94719252016-10-20 20:48:01 +0000221BOOST_AUTO_TEST_SUITE_END() // TestNfdRibReadvertiseDestination
Davide Pesaventocaa60cc2024-02-18 18:18:37 -0500222BOOST_AUTO_TEST_SUITE_END() // Rib
Nick Gordon94719252016-10-20 20:48:01 +0000223
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400224} // namespace nfd::tests