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 | /* |
Junxiao Shi | feddc3c | 2019-01-17 19:06:00 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, 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 | |
Nick Gordon | 9471925 | 2016-10-20 20:48:01 +0000 | [diff] [blame] | 35 | namespace nfd { |
| 36 | namespace rib { |
| 37 | namespace tests { |
| 38 | |
| 39 | using namespace nfd::tests; |
| 40 | |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 41 | class NfdRibReadvertiseDestinationFixture : public GlobalIoTimeFixture, public KeyChainFixture |
Nick Gordon | 9471925 | 2016-10-20 20:48:01 +0000 | [diff] [blame] | 42 | { |
| 43 | public: |
| 44 | NfdRibReadvertiseDestinationFixture() |
| 45 | : nSuccessCallbacks(0) |
| 46 | , nFailureCallbacks(0) |
Davide Pesavento | e1bdc08 | 2018-10-11 21:20:23 -0400 | [diff] [blame] | 47 | , face(g_io, m_keyChain, {true, false}) |
Nick Gordon | 9471925 | 2016-10-20 20:48:01 +0000 | [diff] [blame] | 48 | , controller(face, m_keyChain) |
Yanbiao Li | f48d080 | 2018-06-01 03:00:02 -0700 | [diff] [blame] | 49 | , dest(controller, rib, ndn::nfd::CommandOptions().setPrefix("/localhost/nlsr")) |
Davide Pesavento | e1bdc08 | 2018-10-11 21:20:23 -0400 | [diff] [blame] | 50 | , successCallback([this] { nSuccessCallbacks++; }) |
Davide Pesavento | 0a71dd3 | 2019-03-17 20:36:18 -0400 | [diff] [blame] | 51 | , failureCallback([this] (const std::string&) { nFailureCallbacks++; }) |
Nick Gordon | 9471925 | 2016-10-20 20:48:01 +0000 | [diff] [blame] | 52 | { |
| 53 | } |
| 54 | |
| 55 | public: |
| 56 | uint32_t nSuccessCallbacks; |
| 57 | uint32_t nFailureCallbacks; |
| 58 | |
| 59 | protected: |
| 60 | ndn::util::DummyClientFace face; |
| 61 | ndn::nfd::Controller controller; |
Nick Gordon | 04262d9 | 2017-01-31 12:27:06 -0600 | [diff] [blame] | 62 | Rib rib; |
Nick Gordon | 9471925 | 2016-10-20 20:48:01 +0000 | [diff] [blame] | 63 | NfdRibReadvertiseDestination dest; |
| 64 | std::function<void()> successCallback; |
| 65 | std::function<void(const std::string&)> failureCallback; |
| 66 | }; |
| 67 | |
Junxiao Shi | 63b67e2 | 2017-03-06 19:46:02 +0000 | [diff] [blame] | 68 | BOOST_AUTO_TEST_SUITE(Readvertise) |
Nick Gordon | 9471925 | 2016-10-20 20:48:01 +0000 | [diff] [blame] | 69 | BOOST_FIXTURE_TEST_SUITE(TestNfdRibReadvertiseDestination, NfdRibReadvertiseDestinationFixture) |
| 70 | |
| 71 | class AdvertiseSuccessScenario |
| 72 | { |
| 73 | public: |
| 74 | ndn::nfd::ControlResponse |
| 75 | makeResponse(const ControlParameters& sentCp) |
| 76 | { |
| 77 | ControlParameters response; |
| 78 | |
| 79 | response.setFaceId(1) |
| 80 | .setName(sentCp.getName()) |
| 81 | .setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT) |
| 82 | .setCost(0) |
| 83 | .setFlags(ndn::nfd::ROUTE_FLAG_CHILD_INHERIT); |
| 84 | |
| 85 | ndn::nfd::ControlResponse responsePayload; |
| 86 | responsePayload.setCode(200) |
| 87 | .setText("Successfully registered.") |
| 88 | .setBody(response.wireEncode()); |
| 89 | return responsePayload; |
| 90 | } |
| 91 | |
| 92 | void |
| 93 | checkCommandOutcome(NfdRibReadvertiseDestinationFixture* fixture) |
| 94 | { |
| 95 | BOOST_CHECK_EQUAL(fixture->nSuccessCallbacks, 1); |
| 96 | BOOST_CHECK_EQUAL(fixture->nFailureCallbacks, 0); |
| 97 | } |
| 98 | }; |
| 99 | |
| 100 | class AdvertiseFailureScenario |
| 101 | { |
| 102 | public: |
| 103 | ndn::nfd::ControlResponse |
| 104 | makeResponse(ControlParameters sentCp) |
| 105 | { |
| 106 | ndn::nfd::ControlResponse responsePayload(403, "Not Authenticated"); |
| 107 | return responsePayload; |
| 108 | } |
| 109 | |
| 110 | void |
| 111 | checkCommandOutcome(NfdRibReadvertiseDestinationFixture* fixture) |
| 112 | { |
| 113 | BOOST_CHECK_EQUAL(fixture->nFailureCallbacks, 1); |
| 114 | BOOST_CHECK_EQUAL(fixture->nSuccessCallbacks, 0); |
| 115 | } |
| 116 | }; |
| 117 | |
Junxiao Shi | 63b67e2 | 2017-03-06 19:46:02 +0000 | [diff] [blame] | 118 | using AdvertiseScenarios = boost::mpl::vector<AdvertiseSuccessScenario, AdvertiseFailureScenario>; |
Nick Gordon | 9471925 | 2016-10-20 20:48:01 +0000 | [diff] [blame] | 119 | |
| 120 | BOOST_AUTO_TEST_CASE_TEMPLATE(Advertise, Scenario, AdvertiseScenarios) |
| 121 | { |
| 122 | Scenario scenario; |
| 123 | Name prefix("/ndn/memphis/test"); |
Junxiao Shi | feddc3c | 2019-01-17 19:06:00 +0000 | [diff] [blame] | 124 | ReadvertisedRoute rr(prefix); |
Junxiao Shi | 63b67e2 | 2017-03-06 19:46:02 +0000 | [diff] [blame] | 125 | const Name RIB_REGISTER_COMMAND_PREFIX("/localhost/nlsr/rib/register"); |
Nick Gordon | 9471925 | 2016-10-20 20:48:01 +0000 | [diff] [blame] | 126 | |
| 127 | dest.advertise(rr, successCallback, failureCallback); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame^] | 128 | advanceClocks(100_ms); |
Nick Gordon | 9471925 | 2016-10-20 20:48:01 +0000 | [diff] [blame] | 129 | |
| 130 | // Retrieve the sent Interest to build the response |
| 131 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1); |
| 132 | const Interest& sentInterest = face.sentInterests[0]; |
| 133 | BOOST_CHECK(RIB_REGISTER_COMMAND_PREFIX.isPrefixOf(sentInterest.getName())); |
| 134 | |
| 135 | // Parse the sent command Interest to check correctness. |
| 136 | ControlParameters sentCp; |
| 137 | BOOST_CHECK_NO_THROW(sentCp.wireDecode(sentInterest.getName().get(RIB_REGISTER_COMMAND_PREFIX.size()).blockFromValue())); |
| 138 | BOOST_CHECK_EQUAL(sentCp.getOrigin(), ndn::nfd::ROUTE_ORIGIN_CLIENT); |
| 139 | BOOST_CHECK_EQUAL(sentCp.getName(), prefix); |
| 140 | |
| 141 | ndn::nfd::ControlResponse responsePayload = scenario.makeResponse(sentCp); |
| 142 | auto responseData = makeData(sentInterest.getName()); |
| 143 | responseData->setContent(responsePayload.wireEncode()); |
| 144 | face.receive(*responseData); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame^] | 145 | this->advanceClocks(10_ms); |
Nick Gordon | 9471925 | 2016-10-20 20:48:01 +0000 | [diff] [blame] | 146 | |
| 147 | scenario.checkCommandOutcome(this); |
| 148 | } |
| 149 | |
| 150 | class WithdrawSuccessScenario |
| 151 | { |
| 152 | public: |
| 153 | ndn::nfd::ControlResponse |
| 154 | makeResponse(const ControlParameters& sentCp) |
| 155 | { |
| 156 | ControlParameters response; |
| 157 | |
| 158 | response.setFaceId(1) |
| 159 | .setName(sentCp.getName()) |
| 160 | .setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT); |
| 161 | |
| 162 | ndn::nfd::ControlResponse responsePayload; |
| 163 | responsePayload.setCode(200) |
| 164 | .setText("Successfully removed") |
| 165 | .setBody(response.wireEncode()); |
| 166 | |
| 167 | return responsePayload; |
| 168 | } |
| 169 | |
| 170 | void |
| 171 | checkCommandOutcome(NfdRibReadvertiseDestinationFixture* fixture) |
| 172 | { |
| 173 | BOOST_CHECK_EQUAL(fixture->nSuccessCallbacks, 1); |
| 174 | BOOST_CHECK_EQUAL(fixture->nFailureCallbacks, 0); |
| 175 | } |
| 176 | }; |
| 177 | |
| 178 | class WithdrawFailureScenario |
| 179 | { |
| 180 | public: |
| 181 | ndn::nfd::ControlResponse |
| 182 | makeResponse(ControlParameters sentCp) |
| 183 | { |
| 184 | ndn::nfd::ControlResponse responsePayload(403, "Not authenticated"); |
| 185 | return responsePayload; |
| 186 | } |
| 187 | |
| 188 | void |
| 189 | checkCommandOutcome(NfdRibReadvertiseDestinationFixture* fixture) |
| 190 | { |
| 191 | BOOST_CHECK_EQUAL(fixture->nFailureCallbacks, 1); |
| 192 | BOOST_CHECK_EQUAL(fixture->nSuccessCallbacks, 0); |
| 193 | } |
| 194 | }; |
| 195 | |
Junxiao Shi | 63b67e2 | 2017-03-06 19:46:02 +0000 | [diff] [blame] | 196 | using WithdrawScenarios = boost::mpl::vector<WithdrawSuccessScenario, WithdrawFailureScenario>; |
Nick Gordon | 9471925 | 2016-10-20 20:48:01 +0000 | [diff] [blame] | 197 | |
| 198 | BOOST_AUTO_TEST_CASE_TEMPLATE(Withdraw, Scenario, WithdrawScenarios) |
| 199 | { |
| 200 | Scenario scenario; |
| 201 | Name prefix("/ndn/memphis/test"); |
Junxiao Shi | feddc3c | 2019-01-17 19:06:00 +0000 | [diff] [blame] | 202 | ReadvertisedRoute rr(prefix); |
Junxiao Shi | 63b67e2 | 2017-03-06 19:46:02 +0000 | [diff] [blame] | 203 | const Name RIB_UNREGISTER_COMMAND_PREFIX("/localhost/nlsr/rib/unregister"); |
Nick Gordon | 9471925 | 2016-10-20 20:48:01 +0000 | [diff] [blame] | 204 | |
| 205 | dest.withdraw(rr, successCallback, failureCallback); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame^] | 206 | this->advanceClocks(10_ms); |
Nick Gordon | 9471925 | 2016-10-20 20:48:01 +0000 | [diff] [blame] | 207 | |
| 208 | // Retrieve the sent Interest to build the response |
| 209 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1); |
| 210 | const Interest& sentInterest = face.sentInterests[0]; |
| 211 | BOOST_CHECK(RIB_UNREGISTER_COMMAND_PREFIX.isPrefixOf(sentInterest.getName())); |
| 212 | |
| 213 | ControlParameters sentCp; |
| 214 | BOOST_CHECK_NO_THROW(sentCp.wireDecode(sentInterest.getName().get(RIB_UNREGISTER_COMMAND_PREFIX.size()).blockFromValue())); |
| 215 | BOOST_CHECK_EQUAL(sentCp.getOrigin(), ndn::nfd::ROUTE_ORIGIN_CLIENT); |
| 216 | BOOST_CHECK_EQUAL(sentCp.getName(), prefix); |
| 217 | |
| 218 | ndn::nfd::ControlResponse responsePayload = scenario.makeResponse(sentCp); |
| 219 | auto responseData = makeData(sentInterest.getName()); |
| 220 | responseData->setContent(responsePayload.wireEncode()); |
| 221 | |
| 222 | face.receive(*responseData); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame^] | 223 | this->advanceClocks(1_ms); |
Nick Gordon | 9471925 | 2016-10-20 20:48:01 +0000 | [diff] [blame] | 224 | |
| 225 | scenario.checkCommandOutcome(this); |
| 226 | } |
| 227 | |
Nick Gordon | 04262d9 | 2017-01-31 12:27:06 -0600 | [diff] [blame] | 228 | BOOST_AUTO_TEST_CASE(DestinationAvailability) |
| 229 | { |
| 230 | std::vector<bool> availabilityChangeHistory; |
Junxiao Shi | 63b67e2 | 2017-03-06 19:46:02 +0000 | [diff] [blame] | 231 | Name commandPrefix("/localhost/nlsr"); |
Nick Gordon | 04262d9 | 2017-01-31 12:27:06 -0600 | [diff] [blame] | 232 | Route route; |
| 233 | |
| 234 | dest.afterAvailabilityChange.connect( |
| 235 | std::bind(&std::vector<bool>::push_back, &availabilityChangeHistory, _1)); |
| 236 | BOOST_CHECK_EQUAL(dest.isAvailable(), false); |
| 237 | |
Junxiao Shi | 63b67e2 | 2017-03-06 19:46:02 +0000 | [diff] [blame] | 238 | rib.insert(commandPrefix, route); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame^] | 239 | this->advanceClocks(100_ms); |
Nick Gordon | 04262d9 | 2017-01-31 12:27:06 -0600 | [diff] [blame] | 240 | BOOST_CHECK_EQUAL(dest.isAvailable(), true); |
Junxiao Shi | 63b67e2 | 2017-03-06 19:46:02 +0000 | [diff] [blame] | 241 | BOOST_REQUIRE_EQUAL(availabilityChangeHistory.size(), 1); |
Nick Gordon | 04262d9 | 2017-01-31 12:27:06 -0600 | [diff] [blame] | 242 | BOOST_CHECK_EQUAL(availabilityChangeHistory.back(), true); |
| 243 | |
Junxiao Shi | 63b67e2 | 2017-03-06 19:46:02 +0000 | [diff] [blame] | 244 | rib.erase(commandPrefix, route); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame^] | 245 | this->advanceClocks(100_ms); |
Nick Gordon | 04262d9 | 2017-01-31 12:27:06 -0600 | [diff] [blame] | 246 | BOOST_CHECK_EQUAL(dest.isAvailable(), false); |
Junxiao Shi | 63b67e2 | 2017-03-06 19:46:02 +0000 | [diff] [blame] | 247 | BOOST_REQUIRE_EQUAL(availabilityChangeHistory.size(), 2); |
Nick Gordon | 04262d9 | 2017-01-31 12:27:06 -0600 | [diff] [blame] | 248 | BOOST_CHECK_EQUAL(availabilityChangeHistory.back(), false); |
| 249 | } |
| 250 | |
Nick Gordon | 9471925 | 2016-10-20 20:48:01 +0000 | [diff] [blame] | 251 | BOOST_AUTO_TEST_SUITE_END() // TestNfdRibReadvertiseDestination |
Junxiao Shi | 63b67e2 | 2017-03-06 19:46:02 +0000 | [diff] [blame] | 252 | BOOST_AUTO_TEST_SUITE_END() // Readvertise |
Nick Gordon | 9471925 | 2016-10-20 20:48:01 +0000 | [diff] [blame] | 253 | |
| 254 | } // namespace tests |
| 255 | } // namespace rib |
| 256 | } // namespace nfd |