blob: a9dd0371d451d7ff0902058813d8123c306a24fc [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");
jaczhifeb86502025-03-06 19:12:27 -0800106 ReadvertisedRoute rr(prefix, 100);
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];
jaczhifeb86502025-03-06 19:12:27 -0800114 BOOST_TEST(RIB_REGISTER_COMMAND_PREFIX.isPrefixOf(sentInterest.getName()));
Nick Gordon94719252016-10-20 20:48:01 +0000115
jaczhifeb86502025-03-06 19:12:27 -0800116 // Parse the sent command Interest parameters to check correctness
117 ControlParameters sentCp(sentInterest.getName().get(RIB_REGISTER_COMMAND_PREFIX.size()).blockFromValue());
118 BOOST_TEST(sentCp.getName() == prefix);
119 BOOST_TEST(sentCp.getOrigin() == ndn::nfd::ROUTE_ORIGIN_CLIENT);
120 BOOST_TEST(sentCp.getCost() == 100);
Nick Gordon94719252016-10-20 20:48:01 +0000121
Davide Pesavento4296fe32025-01-14 23:00:41 -0500122 ndn::nfd::ControlResponse responsePayload = Scenario::makeResponse(sentCp);
Nick Gordon94719252016-10-20 20:48:01 +0000123 auto responseData = makeData(sentInterest.getName());
124 responseData->setContent(responsePayload.wireEncode());
125 face.receive(*responseData);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400126 this->advanceClocks(10_ms);
Nick Gordon94719252016-10-20 20:48:01 +0000127
Davide Pesavento4296fe32025-01-14 23:00:41 -0500128 BOOST_TEST(nSuccessCallbacks == Scenario::nExpectedSuccessCalls);
129 BOOST_TEST(nFailureCallbacks == Scenario::nExpectedFailureCalls);
Nick Gordon94719252016-10-20 20:48:01 +0000130}
131
Davide Pesavento4296fe32025-01-14 23:00:41 -0500132struct WithdrawSuccessScenario
Nick Gordon94719252016-10-20 20:48:01 +0000133{
Davide Pesavento4296fe32025-01-14 23:00:41 -0500134 static ndn::nfd::ControlResponse
Nick Gordon94719252016-10-20 20:48:01 +0000135 makeResponse(const ControlParameters& sentCp)
136 {
137 ControlParameters response;
Nick Gordon94719252016-10-20 20:48:01 +0000138 response.setFaceId(1)
139 .setName(sentCp.getName())
140 .setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT);
141
142 ndn::nfd::ControlResponse responsePayload;
143 responsePayload.setCode(200)
144 .setText("Successfully removed")
145 .setBody(response.wireEncode());
146
147 return responsePayload;
148 }
149
Davide Pesavento4296fe32025-01-14 23:00:41 -0500150 static constexpr int nExpectedSuccessCalls = 1;
151 static constexpr int nExpectedFailureCalls = 0;
Nick Gordon94719252016-10-20 20:48:01 +0000152};
153
Davide Pesavento4296fe32025-01-14 23:00:41 -0500154struct WithdrawFailureScenario
Nick Gordon94719252016-10-20 20:48:01 +0000155{
Davide Pesavento4296fe32025-01-14 23:00:41 -0500156 static ndn::nfd::ControlResponse
157 makeResponse(const ControlParameters& sentCp)
Nick Gordon94719252016-10-20 20:48:01 +0000158 {
Davide Pesavento4296fe32025-01-14 23:00:41 -0500159 return ndn::nfd::ControlResponse(403, "Not authenticated");
Nick Gordon94719252016-10-20 20:48:01 +0000160 }
161
Davide Pesavento4296fe32025-01-14 23:00:41 -0500162 static constexpr int nExpectedSuccessCalls = 0;
163 static constexpr int nExpectedFailureCalls = 1;
Nick Gordon94719252016-10-20 20:48:01 +0000164};
165
Davide Pesaventof56cf632024-01-27 22:22:06 -0500166using WithdrawScenarios = boost::mp11::mp_list<WithdrawSuccessScenario, WithdrawFailureScenario>;
Nick Gordon94719252016-10-20 20:48:01 +0000167
168BOOST_AUTO_TEST_CASE_TEMPLATE(Withdraw, Scenario, WithdrawScenarios)
169{
Nick Gordon94719252016-10-20 20:48:01 +0000170 Name prefix("/ndn/memphis/test");
jaczhifeb86502025-03-06 19:12:27 -0800171 ReadvertisedRoute rr(prefix, 100);
Nick Gordon94719252016-10-20 20:48:01 +0000172
173 dest.withdraw(rr, successCallback, failureCallback);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400174 this->advanceClocks(10_ms);
Nick Gordon94719252016-10-20 20:48:01 +0000175
176 // Retrieve the sent Interest to build the response
177 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
178 const Interest& sentInterest = face.sentInterests[0];
jaczhifeb86502025-03-06 19:12:27 -0800179 BOOST_TEST(RIB_UNREGISTER_COMMAND_PREFIX.isPrefixOf(sentInterest.getName()));
Nick Gordon94719252016-10-20 20:48:01 +0000180
jaczhifeb86502025-03-06 19:12:27 -0800181 // Parse the sent command Interest parameters to check correctness
182 ControlParameters sentCp(sentInterest.getName().get(RIB_UNREGISTER_COMMAND_PREFIX.size()).blockFromValue());
183 BOOST_TEST(sentCp.getName() == prefix);
184 BOOST_TEST(sentCp.getOrigin() == ndn::nfd::ROUTE_ORIGIN_CLIENT);
Nick Gordon94719252016-10-20 20:48:01 +0000185
Davide Pesavento4296fe32025-01-14 23:00:41 -0500186 ndn::nfd::ControlResponse responsePayload = Scenario::makeResponse(sentCp);
Nick Gordon94719252016-10-20 20:48:01 +0000187 auto responseData = makeData(sentInterest.getName());
188 responseData->setContent(responsePayload.wireEncode());
189
190 face.receive(*responseData);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400191 this->advanceClocks(1_ms);
Nick Gordon94719252016-10-20 20:48:01 +0000192
Davide Pesavento4296fe32025-01-14 23:00:41 -0500193 BOOST_TEST(nSuccessCallbacks == Scenario::nExpectedSuccessCalls);
194 BOOST_TEST(nFailureCallbacks == Scenario::nExpectedFailureCalls);
Nick Gordon94719252016-10-20 20:48:01 +0000195}
196
Nick Gordon04262d92017-01-31 12:27:06 -0600197BOOST_AUTO_TEST_CASE(DestinationAvailability)
198{
199 std::vector<bool> availabilityChangeHistory;
Junxiao Shi63b67e22017-03-06 19:46:02 +0000200 Name commandPrefix("/localhost/nlsr");
Nick Gordon04262d92017-01-31 12:27:06 -0600201 Route route;
202
Davide Pesavento4296fe32025-01-14 23:00:41 -0500203 dest.afterAvailabilityChange.connect([&] (bool val) { availabilityChangeHistory.push_back(val); });
Nick Gordon04262d92017-01-31 12:27:06 -0600204 BOOST_CHECK_EQUAL(dest.isAvailable(), false);
205
Junxiao Shi63b67e22017-03-06 19:46:02 +0000206 rib.insert(commandPrefix, route);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400207 this->advanceClocks(100_ms);
Nick Gordon04262d92017-01-31 12:27:06 -0600208 BOOST_CHECK_EQUAL(dest.isAvailable(), true);
Junxiao Shi63b67e22017-03-06 19:46:02 +0000209 BOOST_REQUIRE_EQUAL(availabilityChangeHistory.size(), 1);
Nick Gordon04262d92017-01-31 12:27:06 -0600210 BOOST_CHECK_EQUAL(availabilityChangeHistory.back(), true);
211
Junxiao Shi63b67e22017-03-06 19:46:02 +0000212 rib.erase(commandPrefix, route);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400213 this->advanceClocks(100_ms);
Nick Gordon04262d92017-01-31 12:27:06 -0600214 BOOST_CHECK_EQUAL(dest.isAvailable(), false);
Junxiao Shi63b67e22017-03-06 19:46:02 +0000215 BOOST_REQUIRE_EQUAL(availabilityChangeHistory.size(), 2);
Nick Gordon04262d92017-01-31 12:27:06 -0600216 BOOST_CHECK_EQUAL(availabilityChangeHistory.back(), false);
217}
218
Nick Gordon94719252016-10-20 20:48:01 +0000219BOOST_AUTO_TEST_SUITE_END() // TestNfdRibReadvertiseDestination
Davide Pesaventocaa60cc2024-02-18 18:18:37 -0500220BOOST_AUTO_TEST_SUITE_END() // Rib
Nick Gordon94719252016-10-20 20:48:01 +0000221
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400222} // namespace nfd::tests