Nick Gordon | 9471925 | 2016-10-20 20:48:01 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | e1bdc08 | 2018-10-11 21:20:23 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | ae43030 | 2023-05-11 01:42:46 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2023, Regents of the University of California, |
Nick Gordon | 9471925 | 2016-10-20 20:48:01 +0000 | [diff] [blame] | 4 | * 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 Gordon | 9471925 | 2016-10-20 20:48:01 +0000 | [diff] [blame] | 26 | #include "rib/readvertise/nfd-rib-readvertise-destination.hpp" |
| 27 | |
Davide Pesavento | e1bdc08 | 2018-10-11 21:20:23 -0400 | [diff] [blame] | 28 | #include "tests/test-common.hpp" |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 29 | #include "tests/key-chain-fixture.hpp" |
| 30 | #include "tests/daemon/global-io-fixture.hpp" |
Davide Pesavento | e1bdc08 | 2018-10-11 21:20:23 -0400 | [diff] [blame] | 31 | |
Nick Gordon | 9471925 | 2016-10-20 20:48:01 +0000 | [diff] [blame] | 32 | #include <ndn-cxx/security/signing-info.hpp> |
Davide Pesavento | e1bdc08 | 2018-10-11 21:20:23 -0400 | [diff] [blame] | 33 | #include <ndn-cxx/util/dummy-client-face.hpp> |
Nick Gordon | 9471925 | 2016-10-20 20:48:01 +0000 | [diff] [blame] | 34 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 35 | namespace nfd::tests { |
Nick Gordon | 9471925 | 2016-10-20 20:48:01 +0000 | [diff] [blame] | 36 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 37 | using namespace nfd::rib; |
Nick Gordon | 9471925 | 2016-10-20 20:48:01 +0000 | [diff] [blame] | 38 | |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 39 | class NfdRibReadvertiseDestinationFixture : public GlobalIoTimeFixture, public KeyChainFixture |
Nick Gordon | 9471925 | 2016-10-20 20:48:01 +0000 | [diff] [blame] | 40 | { |
| 41 | public: |
| 42 | NfdRibReadvertiseDestinationFixture() |
| 43 | : nSuccessCallbacks(0) |
| 44 | , nFailureCallbacks(0) |
Davide Pesavento | e1bdc08 | 2018-10-11 21:20:23 -0400 | [diff] [blame] | 45 | , face(g_io, m_keyChain, {true, false}) |
Nick Gordon | 9471925 | 2016-10-20 20:48:01 +0000 | [diff] [blame] | 46 | , controller(face, m_keyChain) |
Yanbiao Li | f48d080 | 2018-06-01 03:00:02 -0700 | [diff] [blame] | 47 | , dest(controller, rib, ndn::nfd::CommandOptions().setPrefix("/localhost/nlsr")) |
Davide Pesavento | e1bdc08 | 2018-10-11 21:20:23 -0400 | [diff] [blame] | 48 | , successCallback([this] { nSuccessCallbacks++; }) |
Davide Pesavento | 0a71dd3 | 2019-03-17 20:36:18 -0400 | [diff] [blame] | 49 | , failureCallback([this] (const std::string&) { nFailureCallbacks++; }) |
Nick Gordon | 9471925 | 2016-10-20 20:48:01 +0000 | [diff] [blame] | 50 | { |
| 51 | } |
| 52 | |
| 53 | public: |
| 54 | uint32_t nSuccessCallbacks; |
| 55 | uint32_t nFailureCallbacks; |
| 56 | |
| 57 | protected: |
Davide Pesavento | ae43030 | 2023-05-11 01:42:46 -0400 | [diff] [blame] | 58 | ndn::DummyClientFace face; |
Nick Gordon | 9471925 | 2016-10-20 20:48:01 +0000 | [diff] [blame] | 59 | ndn::nfd::Controller controller; |
Nick Gordon | 04262d9 | 2017-01-31 12:27:06 -0600 | [diff] [blame] | 60 | Rib rib; |
Nick Gordon | 9471925 | 2016-10-20 20:48:01 +0000 | [diff] [blame] | 61 | NfdRibReadvertiseDestination dest; |
| 62 | std::function<void()> successCallback; |
| 63 | std::function<void(const std::string&)> failureCallback; |
| 64 | }; |
| 65 | |
Junxiao Shi | 63b67e2 | 2017-03-06 19:46:02 +0000 | [diff] [blame] | 66 | BOOST_AUTO_TEST_SUITE(Readvertise) |
Nick Gordon | 9471925 | 2016-10-20 20:48:01 +0000 | [diff] [blame] | 67 | BOOST_FIXTURE_TEST_SUITE(TestNfdRibReadvertiseDestination, NfdRibReadvertiseDestinationFixture) |
| 68 | |
| 69 | class AdvertiseSuccessScenario |
| 70 | { |
| 71 | public: |
| 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 | |
| 98 | class AdvertiseFailureScenario |
| 99 | { |
| 100 | public: |
| 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 Shi | 63b67e2 | 2017-03-06 19:46:02 +0000 | [diff] [blame] | 116 | using AdvertiseScenarios = boost::mpl::vector<AdvertiseSuccessScenario, AdvertiseFailureScenario>; |
Nick Gordon | 9471925 | 2016-10-20 20:48:01 +0000 | [diff] [blame] | 117 | |
| 118 | BOOST_AUTO_TEST_CASE_TEMPLATE(Advertise, Scenario, AdvertiseScenarios) |
| 119 | { |
| 120 | Scenario scenario; |
| 121 | Name prefix("/ndn/memphis/test"); |
Junxiao Shi | feddc3c | 2019-01-17 19:06:00 +0000 | [diff] [blame] | 122 | ReadvertisedRoute rr(prefix); |
Junxiao Shi | 63b67e2 | 2017-03-06 19:46:02 +0000 | [diff] [blame] | 123 | const Name RIB_REGISTER_COMMAND_PREFIX("/localhost/nlsr/rib/register"); |
Nick Gordon | 9471925 | 2016-10-20 20:48:01 +0000 | [diff] [blame] | 124 | |
| 125 | dest.advertise(rr, successCallback, failureCallback); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 126 | advanceClocks(100_ms); |
Nick Gordon | 9471925 | 2016-10-20 20:48:01 +0000 | [diff] [blame] | 127 | |
| 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 Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 143 | this->advanceClocks(10_ms); |
Nick Gordon | 9471925 | 2016-10-20 20:48:01 +0000 | [diff] [blame] | 144 | |
| 145 | scenario.checkCommandOutcome(this); |
| 146 | } |
| 147 | |
| 148 | class WithdrawSuccessScenario |
| 149 | { |
| 150 | public: |
| 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 | |
| 176 | class WithdrawFailureScenario |
| 177 | { |
| 178 | public: |
| 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 Shi | 63b67e2 | 2017-03-06 19:46:02 +0000 | [diff] [blame] | 194 | using WithdrawScenarios = boost::mpl::vector<WithdrawSuccessScenario, WithdrawFailureScenario>; |
Nick Gordon | 9471925 | 2016-10-20 20:48:01 +0000 | [diff] [blame] | 195 | |
| 196 | BOOST_AUTO_TEST_CASE_TEMPLATE(Withdraw, Scenario, WithdrawScenarios) |
| 197 | { |
| 198 | Scenario scenario; |
| 199 | Name prefix("/ndn/memphis/test"); |
Junxiao Shi | feddc3c | 2019-01-17 19:06:00 +0000 | [diff] [blame] | 200 | ReadvertisedRoute rr(prefix); |
Junxiao Shi | 63b67e2 | 2017-03-06 19:46:02 +0000 | [diff] [blame] | 201 | const Name RIB_UNREGISTER_COMMAND_PREFIX("/localhost/nlsr/rib/unregister"); |
Nick Gordon | 9471925 | 2016-10-20 20:48:01 +0000 | [diff] [blame] | 202 | |
| 203 | dest.withdraw(rr, successCallback, failureCallback); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 204 | this->advanceClocks(10_ms); |
Nick Gordon | 9471925 | 2016-10-20 20:48:01 +0000 | [diff] [blame] | 205 | |
| 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 Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 221 | this->advanceClocks(1_ms); |
Nick Gordon | 9471925 | 2016-10-20 20:48:01 +0000 | [diff] [blame] | 222 | |
| 223 | scenario.checkCommandOutcome(this); |
| 224 | } |
| 225 | |
Nick Gordon | 04262d9 | 2017-01-31 12:27:06 -0600 | [diff] [blame] | 226 | BOOST_AUTO_TEST_CASE(DestinationAvailability) |
| 227 | { |
| 228 | std::vector<bool> availabilityChangeHistory; |
Junxiao Shi | 63b67e2 | 2017-03-06 19:46:02 +0000 | [diff] [blame] | 229 | Name commandPrefix("/localhost/nlsr"); |
Nick Gordon | 04262d9 | 2017-01-31 12:27:06 -0600 | [diff] [blame] | 230 | 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 Shi | 63b67e2 | 2017-03-06 19:46:02 +0000 | [diff] [blame] | 236 | rib.insert(commandPrefix, route); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 237 | this->advanceClocks(100_ms); |
Nick Gordon | 04262d9 | 2017-01-31 12:27:06 -0600 | [diff] [blame] | 238 | BOOST_CHECK_EQUAL(dest.isAvailable(), true); |
Junxiao Shi | 63b67e2 | 2017-03-06 19:46:02 +0000 | [diff] [blame] | 239 | BOOST_REQUIRE_EQUAL(availabilityChangeHistory.size(), 1); |
Nick Gordon | 04262d9 | 2017-01-31 12:27:06 -0600 | [diff] [blame] | 240 | BOOST_CHECK_EQUAL(availabilityChangeHistory.back(), true); |
| 241 | |
Junxiao Shi | 63b67e2 | 2017-03-06 19:46:02 +0000 | [diff] [blame] | 242 | rib.erase(commandPrefix, route); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 243 | this->advanceClocks(100_ms); |
Nick Gordon | 04262d9 | 2017-01-31 12:27:06 -0600 | [diff] [blame] | 244 | BOOST_CHECK_EQUAL(dest.isAvailable(), false); |
Junxiao Shi | 63b67e2 | 2017-03-06 19:46:02 +0000 | [diff] [blame] | 245 | BOOST_REQUIRE_EQUAL(availabilityChangeHistory.size(), 2); |
Nick Gordon | 04262d9 | 2017-01-31 12:27:06 -0600 | [diff] [blame] | 246 | BOOST_CHECK_EQUAL(availabilityChangeHistory.back(), false); |
| 247 | } |
| 248 | |
Nick Gordon | 9471925 | 2016-10-20 20:48:01 +0000 | [diff] [blame] | 249 | BOOST_AUTO_TEST_SUITE_END() // TestNfdRibReadvertiseDestination |
Junxiao Shi | 63b67e2 | 2017-03-06 19:46:02 +0000 | [diff] [blame] | 250 | BOOST_AUTO_TEST_SUITE_END() // Readvertise |
Nick Gordon | 9471925 | 2016-10-20 20:48:01 +0000 | [diff] [blame] | 251 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 252 | } // namespace nfd::tests |