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