Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
Alexander Afanasyev | 7c10b3b | 2015-01-20 12:24:27 -0800 | [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. |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 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 | |
| 26 | #include "rib/rib-manager.hpp" |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 27 | #include "manager-common-fixture.hpp" |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 28 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 29 | #include <ndn-cxx/management/nfd-rib-entry.hpp> |
| 30 | #include <ndn-cxx/management/nfd-face-status.hpp> |
| 31 | #include <ndn-cxx/management/nfd-face-event-notification.hpp> |
| 32 | #include <ndn-cxx/util/random.hpp> |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 33 | |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 34 | namespace nfd { |
| 35 | namespace rib { |
| 36 | namespace tests { |
| 37 | |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 38 | using namespace nfd::tests; |
| 39 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 40 | struct ConfigurationStatus |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 41 | { |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 42 | bool isLocalhostConfigured; |
| 43 | bool isLocalhopConfigured; |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 44 | }; |
| 45 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 46 | class RibManagerFixture : public ManagerCommonFixture |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 47 | { |
| 48 | public: |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 49 | explicit |
| 50 | RibManagerFixture(const ConfigurationStatus& status, |
| 51 | bool shouldClearRib) |
| 52 | : m_commands(m_face->sentInterests) |
| 53 | , m_status(status) |
| 54 | , m_manager(m_dispatcher, *m_face, m_keyChain) |
| 55 | , m_rib(m_manager.m_rib) |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 56 | { |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 57 | m_rib.m_onSendBatchFromQueue = bind(&RibManagerFixture::onSendBatchFromQueue, this, _1); |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 58 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 59 | const std::string prefix = "rib\n{\n"; |
| 60 | const std::string suffix = "}"; |
| 61 | const std::string localhostSection = " localhost_security\n" |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 62 | " {\n" |
| 63 | " trust-anchor\n" |
| 64 | " {\n" |
| 65 | " type any\n" |
| 66 | " }\n" |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 67 | " }\n"; |
| 68 | const std::string localhopSection = " localhop_security\n" |
| 69 | " {\n" |
| 70 | " trust-anchor\n" |
| 71 | " {\n" |
| 72 | " type any\n" |
| 73 | " }\n" |
| 74 | " }\n"; |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 75 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 76 | std::string ribSection = ""; |
| 77 | if (m_status.isLocalhostConfigured) { |
| 78 | ribSection += localhostSection; |
| 79 | } |
| 80 | if (m_status.isLocalhopConfigured) { |
| 81 | ribSection += localhopSection; |
| 82 | } |
| 83 | const std::string CONFIG_STR = prefix + ribSection + suffix; |
| 84 | |
| 85 | ConfigFile config; |
| 86 | m_manager.setConfigFile(config); |
| 87 | config.parse(CONFIG_STR, true, "test-rib"); |
| 88 | |
| 89 | registerWithNfd(); |
| 90 | |
| 91 | if (shouldClearRib) { |
| 92 | clearRib(); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | private: |
| 97 | void |
| 98 | registerWithNfd() |
| 99 | { |
| 100 | m_manager.registerWithNfd(); |
| 101 | advanceClocks(time::milliseconds(1)); |
| 102 | |
| 103 | auto replyFibAddCommand = [this] (const Interest& interest) { |
| 104 | nfd::ControlParameters params(interest.getName().get(-5).blockFromValue()); |
Junxiao Shi | fde3f54 | 2016-07-10 19:54:53 +0000 | [diff] [blame^] | 105 | BOOST_CHECK(params.getName() == "/localhost/nfd/rib" || params.getName() == "/localhop/nfd/rib"); |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 106 | params.setFaceId(1).setCost(0); |
| 107 | nfd::ControlResponse resp; |
| 108 | |
| 109 | resp.setCode(200).setBody(params.wireEncode()); |
| 110 | shared_ptr<Data> data = make_shared<Data>(interest.getName()); |
| 111 | data->setContent(resp.wireEncode()); |
| 112 | |
| 113 | m_keyChain.sign(*data, ndn::security::SigningInfo(ndn::security::SigningInfo::SIGNER_TYPE_SHA256)); |
| 114 | |
| 115 | m_face->getIoService().post([this, data] { m_face->receive(*data); }); |
| 116 | }; |
| 117 | |
| 118 | Name commandPrefix("/localhost/nfd/fib/add-nexthop"); |
| 119 | for (auto&& command : m_commands) { |
| 120 | if (commandPrefix.isPrefixOf(command.getName())) { |
| 121 | replyFibAddCommand(command); |
| 122 | advanceClocks(time::milliseconds(1)); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | // clear commands and responses |
| 127 | m_responses.clear(); |
| 128 | m_commands.clear(); |
| 129 | } |
| 130 | |
| 131 | void clearRib() |
| 132 | { |
| 133 | while (!m_rib.empty()) { |
| 134 | auto& name = m_rib.begin()->first; |
| 135 | auto& routes = m_rib.begin()->second->getRoutes(); |
| 136 | while (!routes.empty()) { |
| 137 | m_rib.erase(name, *routes.begin()); |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | public: |
| 143 | ControlParameters |
| 144 | makeRegisterParameters(const Name& name, uint64_t id = 0, |
| 145 | const time::milliseconds expiry = time::milliseconds::max()) |
| 146 | { |
| 147 | return ControlParameters() |
| 148 | .setName(name) |
| 149 | .setFaceId(id) |
| 150 | .setOrigin(128) |
| 151 | .setCost(10) |
| 152 | .setFlags(0) |
| 153 | .setExpirationPeriod(expiry); |
| 154 | } |
| 155 | |
| 156 | ControlParameters |
| 157 | makeUnregisterParameters(const Name& name, uint64_t id = 0) |
| 158 | { |
| 159 | return ControlParameters() |
| 160 | .setName(name) |
| 161 | .setFaceId(id) |
| 162 | .setOrigin(128); |
| 163 | } |
| 164 | |
| 165 | void |
| 166 | onSendBatchFromQueue(const RibUpdateBatch& batch) |
| 167 | { |
| 168 | BOOST_ASSERT(batch.begin() != batch.end()); |
| 169 | RibUpdate update = *(batch.begin()); |
| 170 | |
| 171 | // Simulate a successful response from NFD |
| 172 | FibUpdater& updater = m_manager.m_fibUpdater; |
| 173 | m_manager.m_rib.onFibUpdateSuccess(batch, updater.m_inheritedRoutes, |
| 174 | bind(&RibManager::onRibUpdateSuccess, &m_manager, update)); |
| 175 | } |
| 176 | |
| 177 | public: |
| 178 | enum class CheckCommandResult { |
| 179 | OK, |
| 180 | OUT_OF_BOUNDARY, |
| 181 | WRONG_FORMAT, |
| 182 | WRONG_VERB, |
| 183 | WRONG_PARAMS_FORMAT, |
| 184 | WRONG_PARAMS_NAME, |
| 185 | WRONG_PARAMS_FACE |
| 186 | }; |
| 187 | |
| 188 | CheckCommandResult |
| 189 | checkCommand(size_t idx, const char* verbStr, ControlParameters expectedParams) |
| 190 | { |
| 191 | if (idx > m_commands.size()) { |
| 192 | return CheckCommandResult::OUT_OF_BOUNDARY; |
| 193 | } |
| 194 | const auto& name = m_commands[idx].getName(); |
| 195 | |
| 196 | if (!FIB_COMMAND_PREFIX.isPrefixOf(name) || FIB_COMMAND_PREFIX.size() >= name.size()) { |
| 197 | return CheckCommandResult::WRONG_FORMAT; |
| 198 | } |
| 199 | const auto& verb = name[FIB_COMMAND_PREFIX.size()]; |
| 200 | |
| 201 | Name::Component expectedVerb(verbStr); |
| 202 | if (verb != expectedVerb) { |
| 203 | return CheckCommandResult::WRONG_VERB; |
| 204 | } |
| 205 | |
| 206 | ControlParameters parameters; |
| 207 | try { |
| 208 | Block rawParameters = name[FIB_COMMAND_PREFIX.size() + 1].blockFromValue(); |
| 209 | parameters.wireDecode(rawParameters); |
| 210 | } |
| 211 | catch (...) { |
| 212 | return CheckCommandResult::WRONG_PARAMS_FORMAT; |
| 213 | } |
| 214 | |
| 215 | if (parameters.getName() != expectedParams.getName()) { |
| 216 | return CheckCommandResult::WRONG_PARAMS_NAME; |
| 217 | } |
| 218 | |
| 219 | if (parameters.getFaceId() != expectedParams.getFaceId()) { |
| 220 | return CheckCommandResult::WRONG_PARAMS_FACE; |
| 221 | } |
| 222 | |
| 223 | return CheckCommandResult::OK; |
| 224 | } |
| 225 | |
| 226 | public: |
| 227 | std::vector<Interest>& m_commands; |
| 228 | ConfigurationStatus m_status; |
| 229 | static const Name FIB_COMMAND_PREFIX; |
| 230 | |
| 231 | protected: |
| 232 | RibManager m_manager; |
| 233 | Rib& m_rib; |
| 234 | }; |
| 235 | |
| 236 | const Name RibManagerFixture::FIB_COMMAND_PREFIX("/localhost/nfd/fib"); |
| 237 | |
| 238 | std::ostream& |
| 239 | operator<<(std::ostream& os, const RibManagerFixture::CheckCommandResult& result) |
| 240 | { |
| 241 | switch (result) { |
| 242 | case RibManagerFixture::CheckCommandResult::OK: |
| 243 | os << "OK"; |
| 244 | break; |
| 245 | case RibManagerFixture::CheckCommandResult::OUT_OF_BOUNDARY: |
| 246 | os << "OUT_OF_BOUNDARY"; |
| 247 | break; |
| 248 | case RibManagerFixture::CheckCommandResult::WRONG_FORMAT: |
| 249 | os << "WRONG_FORMAT"; |
| 250 | break; |
| 251 | case RibManagerFixture::CheckCommandResult::WRONG_VERB: |
| 252 | os << "WRONG_VERB"; |
| 253 | break; |
| 254 | case RibManagerFixture::CheckCommandResult::WRONG_PARAMS_FORMAT: |
| 255 | os << "WRONG_COST"; |
| 256 | break; |
| 257 | case RibManagerFixture::CheckCommandResult::WRONG_PARAMS_NAME: |
| 258 | os << "WRONG_PARAMS_NAME"; |
| 259 | break; |
| 260 | case RibManagerFixture::CheckCommandResult::WRONG_PARAMS_FACE: |
| 261 | os << "WRONG_PARAMS_FACE"; |
| 262 | break; |
| 263 | default: |
| 264 | break; |
| 265 | }; |
| 266 | |
| 267 | return os; |
| 268 | } |
| 269 | |
| 270 | BOOST_AUTO_TEST_SUITE(Rib) |
| 271 | BOOST_AUTO_TEST_SUITE(TestRibManager) |
| 272 | |
| 273 | class AddTopPrefixFixture : public RibManagerFixture |
| 274 | { |
| 275 | public: |
| 276 | AddTopPrefixFixture() |
| 277 | : RibManagerFixture({true, true}, false) |
| 278 | { |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 279 | } |
| 280 | }; |
| 281 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 282 | BOOST_FIXTURE_TEST_CASE(AddTopPrefix, AddTopPrefixFixture) |
Steve DiBenedetto | cd4ee5f | 2014-12-08 16:09:11 -0700 | [diff] [blame] | 283 | { |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 284 | BOOST_CHECK_EQUAL(m_rib.size(), 2); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 285 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 286 | std::vector<Name> ribEntryNames; |
| 287 | for (auto&& entry : m_rib) { |
| 288 | ribEntryNames.push_back(entry.first); |
| 289 | } |
| 290 | BOOST_CHECK_EQUAL(ribEntryNames[0], "/localhop/nfd"); |
| 291 | BOOST_CHECK_EQUAL(ribEntryNames[1], "/localhost/nfd"); |
Steve DiBenedetto | cd4ee5f | 2014-12-08 16:09:11 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 294 | class UnauthorizedRibManagerFixture : public RibManagerFixture |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 295 | { |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 296 | public: |
| 297 | UnauthorizedRibManagerFixture() |
| 298 | : RibManagerFixture({false, false}, true) |
| 299 | { |
| 300 | } |
| 301 | }; |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 302 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 303 | class LocalhostAuthorizedRibManagerFixture : public RibManagerFixture |
| 304 | { |
| 305 | public: |
| 306 | LocalhostAuthorizedRibManagerFixture() |
| 307 | : RibManagerFixture({true, false}, true) |
| 308 | { |
| 309 | } |
| 310 | }; |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 311 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 312 | class LocalhopAuthorizedRibManagerFixture : public RibManagerFixture |
| 313 | { |
| 314 | public: |
| 315 | LocalhopAuthorizedRibManagerFixture() |
| 316 | : RibManagerFixture({false, true}, true) |
| 317 | { |
| 318 | } |
| 319 | }; |
| 320 | |
| 321 | class AuthorizedRibManagerFixture : public RibManagerFixture |
| 322 | { |
| 323 | public: |
| 324 | AuthorizedRibManagerFixture() |
| 325 | : RibManagerFixture({true, true}, true) |
| 326 | { |
| 327 | } |
| 328 | }; |
| 329 | |
| 330 | typedef boost::mpl::vector< |
| 331 | UnauthorizedRibManagerFixture, |
| 332 | LocalhostAuthorizedRibManagerFixture, |
| 333 | LocalhopAuthorizedRibManagerFixture, |
| 334 | AuthorizedRibManagerFixture |
| 335 | > AllFixtures; |
| 336 | |
| 337 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(CommandAuthorization, T, AllFixtures, T) |
| 338 | { |
| 339 | auto parameters = this->makeRegisterParameters("/test-authorization", 9527); |
| 340 | auto commandHost = this->makeControlCommandRequest("/localhost/nfd/rib/register", parameters); |
| 341 | auto commandHop = this->makeControlCommandRequest("/localhop/nfd/rib/register", parameters); |
| 342 | auto successResp = this->makeResponse(200, "Success", parameters); |
| 343 | auto failureResp = ControlResponse(403, "authorization rejected"); |
| 344 | |
| 345 | BOOST_CHECK_EQUAL(this->m_responses.size(), 0); |
| 346 | this->receiveInterest(commandHost); |
| 347 | this->receiveInterest(commandHop); |
| 348 | |
| 349 | auto nExpectedResponses = this->m_status.isLocalhopConfigured ? 2 : 1; |
| 350 | auto expectedLocalhostResponse = this->m_status.isLocalhostConfigured ? successResp : failureResp; |
| 351 | auto expectedLocalhopResponse = this->m_status.isLocalhopConfigured ? successResp : failureResp; |
| 352 | |
| 353 | BOOST_REQUIRE_EQUAL(this->m_responses.size(), nExpectedResponses); |
| 354 | BOOST_CHECK_EQUAL(this->checkResponse(0, commandHost->getName(), expectedLocalhostResponse), |
| 355 | ManagerCommonFixture::CheckResponseResult::OK); |
| 356 | if (nExpectedResponses == 2) { |
| 357 | BOOST_CHECK_EQUAL(this->checkResponse(1, commandHop->getName(), expectedLocalhopResponse), |
| 358 | ManagerCommonFixture::CheckResponseResult::OK); |
| 359 | } |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 360 | } |
| 361 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 362 | BOOST_FIXTURE_TEST_SUITE(RegisterUnregister, LocalhostAuthorizedRibManagerFixture) |
| 363 | |
| 364 | BOOST_AUTO_TEST_CASE(Basic) |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 365 | { |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 366 | auto paramsRegister = makeRegisterParameters("/test-register-unregister", 9527); |
| 367 | auto paramsUnregister = makeUnregisterParameters("/test-register-unregister", 9527); |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 368 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 369 | auto setInFaceId = [] (shared_ptr<Interest> commandInterest) { |
| 370 | commandInterest->setTag(make_shared<lp::IncomingFaceIdTag>(1234)); |
| 371 | }; |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 372 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 373 | auto commandRegister = makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister, setInFaceId); |
| 374 | auto commandUnregister = makeControlCommandRequest("/localhost/nfd/rib/unregister", paramsUnregister, setInFaceId); |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 375 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 376 | receiveInterest(commandRegister); |
| 377 | receiveInterest(commandUnregister); |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 378 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 379 | BOOST_REQUIRE_EQUAL(m_responses.size(), 2); |
| 380 | BOOST_CHECK_EQUAL(checkResponse(0, commandRegister->getName(), makeResponse(200, "Success", paramsRegister)), |
| 381 | CheckResponseResult::OK); |
| 382 | BOOST_CHECK_EQUAL(checkResponse(1, commandUnregister->getName(), makeResponse(200, "Success", paramsUnregister)), |
| 383 | CheckResponseResult::OK); |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 384 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 385 | BOOST_REQUIRE_EQUAL(m_commands.size(), 2); |
| 386 | BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK); |
| 387 | BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK); |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 388 | } |
| 389 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 390 | BOOST_AUTO_TEST_CASE(SelfOperation) |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 391 | { |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 392 | auto paramsRegister = makeRegisterParameters("/test-self-register-unregister"); |
| 393 | auto paramsUnregister = makeUnregisterParameters("/test-self-register-unregister"); |
| 394 | BOOST_CHECK_EQUAL(paramsRegister.getFaceId(), 0); |
| 395 | BOOST_CHECK_EQUAL(paramsUnregister.getFaceId(), 0); |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 396 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 397 | const uint64_t inFaceId = 9527; |
| 398 | auto setInFaceId = [&inFaceId] (shared_ptr<Interest> commandInterest) { |
| 399 | commandInterest->setTag(make_shared<lp::IncomingFaceIdTag>(inFaceId)); |
| 400 | }; |
| 401 | auto commandRegister = makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister, setInFaceId); |
| 402 | auto commandUnregister = makeControlCommandRequest("/localhost/nfd/rib/unregister", paramsUnregister, setInFaceId); |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 403 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 404 | receiveInterest(commandRegister); |
| 405 | receiveInterest(commandUnregister); |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 406 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 407 | paramsRegister.setFaceId(inFaceId); |
| 408 | paramsUnregister.setFaceId(inFaceId); |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 409 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 410 | BOOST_REQUIRE_EQUAL(m_responses.size(), 2); |
| 411 | BOOST_CHECK_EQUAL(checkResponse(0, commandRegister->getName(), makeResponse(200, "Success", paramsRegister)), |
| 412 | CheckResponseResult::OK); |
| 413 | BOOST_CHECK_EQUAL(checkResponse(1, commandUnregister->getName(), makeResponse(200, "Success", paramsUnregister)), |
| 414 | CheckResponseResult::OK); |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 415 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 416 | BOOST_REQUIRE_EQUAL(m_commands.size(), 2); |
| 417 | BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK); |
| 418 | BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK); |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 419 | } |
| 420 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 421 | BOOST_AUTO_TEST_CASE(Expiration) |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 422 | { |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 423 | auto paramsRegister = makeRegisterParameters("/test-expiry", 9527, time::milliseconds(50)); |
| 424 | auto paramsUnregister = makeRegisterParameters("/test-expiry", 9527); |
| 425 | receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister)); |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 426 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 427 | advanceClocks(time::milliseconds(55)); |
| 428 | BOOST_REQUIRE_EQUAL(m_commands.size(), 2); // the registered route has expired |
| 429 | BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK); |
| 430 | BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK); |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 431 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 432 | m_commands.clear(); |
| 433 | paramsRegister.setExpirationPeriod(time::milliseconds(100)); |
| 434 | receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister)); |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 435 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 436 | advanceClocks(time::milliseconds(55)); |
| 437 | BOOST_REQUIRE_EQUAL(m_commands.size(), 1); // the registered route is still active |
| 438 | BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK); |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 439 | } |
| 440 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 441 | BOOST_AUTO_TEST_SUITE_END() // RegisterUnregister |
| 442 | |
| 443 | // @todo Remove when ndn::nfd::RibEntry implements operator!= |
| 444 | class RibEntry : public ndn::nfd::RibEntry |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 445 | { |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 446 | public: |
| 447 | RibEntry() = default; |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 448 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 449 | RibEntry(const ndn::nfd::RibEntry& entry) |
| 450 | : ndn::nfd::RibEntry(entry) |
| 451 | { |
| 452 | } |
| 453 | }; |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 454 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 455 | bool |
| 456 | operator!=(const RibEntry& left, const RibEntry& right) |
| 457 | { |
| 458 | if (left.getName() != right.getName()) { |
| 459 | return true; |
| 460 | } |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 461 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 462 | auto leftRoutes = left.getRoutes(); |
| 463 | auto rightRoutes = right.getRoutes(); |
| 464 | if (leftRoutes.size() != rightRoutes.size()) { |
| 465 | return true; |
| 466 | } |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 467 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 468 | for (auto&& route : leftRoutes) { |
| 469 | auto hitEntry = |
| 470 | std::find_if(rightRoutes.begin(), rightRoutes.end(), [&] (const ndn::nfd::Route& record) { |
| 471 | return route.getFaceId() == record.getFaceId() && |
| 472 | route.getCost() == record.getCost() && |
| 473 | route.getOrigin() == record.getOrigin() && |
| 474 | route.getFlags() == record.getFlags() && |
| 475 | route.getExpirationPeriod() == record.getExpirationPeriod(); |
| 476 | }); |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 477 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 478 | if (hitEntry == rightRoutes.end()) { |
| 479 | return true; |
| 480 | } |
| 481 | } |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 482 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 483 | return false; |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 484 | } |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 485 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 486 | BOOST_FIXTURE_TEST_CASE(RibDataset, UnauthorizedRibManagerFixture) |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 487 | { |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 488 | uint64_t faceId = 0; |
| 489 | auto generateRoute = [&faceId] () -> Route { |
| 490 | Route route; |
| 491 | route.faceId = ++faceId; |
| 492 | route.cost = ndn::random::generateWord64(); |
| 493 | route.expires = time::steady_clock::TimePoint::max(); |
| 494 | return route; |
| 495 | }; |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 496 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 497 | const size_t nEntries = 108; |
| 498 | std::set<Name> actualPrefixes; |
| 499 | for (size_t i = 0; i < nEntries; ++i) { |
| 500 | Name prefix = Name("/test-dataset").appendNumber(i); |
| 501 | actualPrefixes.insert(prefix); |
| 502 | m_rib.insert(prefix, generateRoute()); |
| 503 | if (i & 0x1) { |
| 504 | m_rib.insert(prefix, generateRoute()); |
| 505 | m_rib.insert(prefix, generateRoute()); |
| 506 | } |
| 507 | } |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 508 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 509 | receiveInterest(makeInterest("/localhost/nfd/rib/list")); |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 510 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 511 | Block content; |
| 512 | BOOST_CHECK_NO_THROW(content = concatenateResponses()); |
| 513 | BOOST_CHECK_NO_THROW(content.parse()); |
| 514 | BOOST_REQUIRE_EQUAL(content.elements().size(), nEntries); |
| 515 | |
| 516 | std::vector<RibEntry> receivedRecords, expectedRecords; |
| 517 | for (size_t idx = 0; idx < nEntries; ++idx) { |
| 518 | BOOST_TEST_MESSAGE("processing element: " << idx); |
| 519 | |
| 520 | RibEntry decodedEntry; |
| 521 | BOOST_REQUIRE_NO_THROW(decodedEntry.wireDecode(content.elements()[idx])); |
| 522 | receivedRecords.push_back(decodedEntry); |
| 523 | |
| 524 | actualPrefixes.erase(decodedEntry.getName()); |
| 525 | |
| 526 | auto matchedEntryIt = m_rib.find(decodedEntry.getName()); |
| 527 | BOOST_REQUIRE(matchedEntryIt != m_rib.end()); |
| 528 | |
| 529 | auto matchedEntry = matchedEntryIt->second; |
| 530 | BOOST_REQUIRE(matchedEntry != nullptr); |
| 531 | |
| 532 | RibEntry record; |
| 533 | record.setName(matchedEntry->getName()); |
| 534 | const auto& routes = matchedEntry->getRoutes(); |
| 535 | for (auto&& route : routes) { |
| 536 | ndn::nfd::Route routeRecord; |
| 537 | routeRecord.setFaceId(route.faceId); |
| 538 | routeRecord.setOrigin(route.origin); |
| 539 | routeRecord.setFlags(route.flags); |
| 540 | routeRecord.setCost(route.cost); |
| 541 | record.addRoute(routeRecord); |
| 542 | } |
| 543 | expectedRecords.push_back(record); |
| 544 | } |
| 545 | |
| 546 | BOOST_CHECK_EQUAL(actualPrefixes.size(), 0); |
| 547 | |
| 548 | BOOST_CHECK_EQUAL_COLLECTIONS(receivedRecords.begin(), receivedRecords.end(), |
| 549 | expectedRecords.begin(), expectedRecords.end()); |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 550 | } |
| 551 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 552 | BOOST_FIXTURE_TEST_SUITE(FaceMonitor, LocalhostAuthorizedRibManagerFixture) |
| 553 | |
| 554 | BOOST_AUTO_TEST_CASE(FetchActiveFacesEvent) |
Vince Lehman | cd16c83 | 2014-07-23 15:14:55 -0700 | [diff] [blame] | 555 | { |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 556 | BOOST_CHECK_EQUAL(m_commands.size(), 0); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 557 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 558 | advanceClocks(time::seconds(301)); // RibManager::ACTIVE_FACE_FETCH_INTERVAL = 300s |
| 559 | BOOST_REQUIRE_EQUAL(m_commands.size(), 2); |
| 560 | BOOST_CHECK_EQUAL(m_commands[0].getName(), "/localhost/nfd/faces/events"); |
| 561 | BOOST_CHECK_EQUAL(m_commands[1].getName(), "/localhost/nfd/faces/list"); |
Vince Lehman | cd16c83 | 2014-07-23 15:14:55 -0700 | [diff] [blame] | 562 | } |
| 563 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 564 | BOOST_AUTO_TEST_CASE(RemoveInvalidFaces) |
Vince Lehman | 281ded7 | 2014-08-21 12:17:08 -0500 | [diff] [blame] | 565 | { |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 566 | auto parameters1 = makeRegisterParameters("/test-remove-invalid-faces-1"); |
| 567 | auto parameters2 = makeRegisterParameters("/test-remove-invalid-faces-2"); |
| 568 | receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1.setFaceId(1))); |
| 569 | receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1.setFaceId(2))); |
| 570 | receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters2.setFaceId(2))); |
| 571 | BOOST_REQUIRE_EQUAL(m_rib.size(), 3); |
Vince Lehman | 281ded7 | 2014-08-21 12:17:08 -0500 | [diff] [blame] | 572 | |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 573 | ndn::nfd::FaceStatus status; |
| 574 | status.setFaceId(1); |
| 575 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 576 | auto data = makeData("/localhost/nfd/faces/list"); |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 577 | data->setContent(status.wireEncode()); |
| 578 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 579 | auto buffer = make_shared<ndn::OBufferStream>(); |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 580 | buffer->write(reinterpret_cast<const char*>(data->getContent().value()), |
| 581 | data->getContent().value_size()); |
| 582 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 583 | m_manager.removeInvalidFaces(buffer); |
| 584 | advanceClocks(time::milliseconds(100)); |
| 585 | BOOST_REQUIRE_EQUAL(m_rib.size(), 1); |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 586 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 587 | auto it1 = m_rib.find("/test-remove-invalid-faces-1"); |
| 588 | auto it2 = m_rib.find("/test-remove-invalid-faces-2"); |
| 589 | BOOST_CHECK(it2 == m_rib.end()); |
| 590 | BOOST_REQUIRE(it1 != m_rib.end()); |
| 591 | BOOST_CHECK(it1->second->hasFaceId(1)); |
| 592 | BOOST_CHECK(!it1->second->hasFaceId(2)); |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 593 | } |
| 594 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 595 | BOOST_AUTO_TEST_CASE(OnNotification) |
Vince Lehman | 7c7d33a | 2015-01-20 17:40:26 -0600 | [diff] [blame] | 596 | { |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 597 | auto parameters1 = makeRegisterParameters("/test-face-event-notification-1", 1); |
| 598 | auto parameters2 = makeRegisterParameters("/test-face-event-notification-2", 1); |
| 599 | receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1)); |
| 600 | receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters2)); |
| 601 | BOOST_REQUIRE_EQUAL(m_rib.size(), 2); |
Vince Lehman | 7c7d33a | 2015-01-20 17:40:26 -0600 | [diff] [blame] | 602 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 603 | auto makeNotification = [] (ndn::nfd::FaceEventKind eventKind, uint64_t faceId) -> ndn::nfd::FaceEventNotification { |
| 604 | ndn::nfd::FaceEventNotification notification; |
| 605 | notification.setKind(eventKind).setFaceId(faceId); |
| 606 | return notification; |
| 607 | }; |
Vince Lehman | 7c7d33a | 2015-01-20 17:40:26 -0600 | [diff] [blame] | 608 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 609 | m_manager.onNotification(makeNotification(ndn::nfd::FaceEventKind::FACE_EVENT_DESTROYED, 1)); |
| 610 | advanceClocks(time::milliseconds(100)); |
| 611 | BOOST_CHECK_EQUAL(m_rib.size(), 0); |
Vince Lehman | 7c7d33a | 2015-01-20 17:40:26 -0600 | [diff] [blame] | 612 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 613 | m_manager.onNotification(makeNotification(ndn::nfd::FaceEventKind::FACE_EVENT_CREATED, 2)); |
| 614 | advanceClocks(time::milliseconds(100)); |
| 615 | BOOST_CHECK_EQUAL(m_rib.size(), 0); |
Vince Lehman | 7c7d33a | 2015-01-20 17:40:26 -0600 | [diff] [blame] | 616 | } |
| 617 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 618 | BOOST_AUTO_TEST_SUITE_END() // FaceMonitor |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 619 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 620 | BOOST_AUTO_TEST_SUITE_END() // TestRibManager |
| 621 | BOOST_AUTO_TEST_SUITE_END() // Rib |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 622 | |
| 623 | } // namespace tests |
| 624 | } // namespace rib |
| 625 | } // namespace nfd |