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