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