Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Davide Pesavento | d2ea235 | 2017-03-12 20:38:09 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, 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 | |
Junxiao Shi | cbc8e94 | 2016-09-06 03:17:45 +0000 | [diff] [blame] | 29 | #include <ndn-cxx/lp/tags.hpp> |
Junxiao Shi | 25c6ce4 | 2016-09-09 13:49:59 +0000 | [diff] [blame] | 30 | #include <ndn-cxx/mgmt/nfd/face-event-notification.hpp> |
Davide Pesavento | d2ea235 | 2017-03-12 20:38:09 -0400 | [diff] [blame] | 31 | #include <ndn-cxx/mgmt/nfd/face-status.hpp> |
| 32 | #include <ndn-cxx/mgmt/nfd/rib-entry.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) |
Junxiao Shi | 221b6fe | 2016-07-14 18:21:56 +0000 | [diff] [blame] | 52 | : m_commands(m_face.sentInterests) |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 53 | , m_status(status) |
Junxiao Shi | 221b6fe | 2016-07-14 18:21:56 +0000 | [diff] [blame] | 54 | , m_manager(m_dispatcher, m_face, m_keyChain) |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 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 | |
Junxiao Shi | 221b6fe | 2016-07-14 18:21:56 +0000 | [diff] [blame] | 115 | m_face.getIoService().post([this, data] { m_face.receive(*data); }); |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | Name commandPrefix("/localhost/nfd/fib/add-nexthop"); |
Davide Pesavento | e94804b | 2016-09-19 17:23:21 +0000 | [diff] [blame] | 119 | for (const auto& command : m_commands) { |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 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 | |
Davide Pesavento | e94804b | 2016-09-19 17:23:21 +0000 | [diff] [blame] | 131 | void |
| 132 | clearRib() |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 133 | { |
| 134 | while (!m_rib.empty()) { |
Davide Pesavento | e94804b | 2016-09-19 17:23:21 +0000 | [diff] [blame] | 135 | m_rib.erase(m_rib.begin()->first, *m_rib.begin()->second->begin()); |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
| 139 | public: |
| 140 | ControlParameters |
| 141 | makeRegisterParameters(const Name& name, uint64_t id = 0, |
Davide Pesavento | d2ea235 | 2017-03-12 20:38:09 -0400 | [diff] [blame] | 142 | time::milliseconds expiry = time::milliseconds::max()) |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 143 | { |
| 144 | return ControlParameters() |
| 145 | .setName(name) |
| 146 | .setFaceId(id) |
| 147 | .setOrigin(128) |
| 148 | .setCost(10) |
| 149 | .setFlags(0) |
| 150 | .setExpirationPeriod(expiry); |
| 151 | } |
| 152 | |
| 153 | ControlParameters |
| 154 | makeUnregisterParameters(const Name& name, uint64_t id = 0) |
| 155 | { |
| 156 | return ControlParameters() |
| 157 | .setName(name) |
| 158 | .setFaceId(id) |
| 159 | .setOrigin(128); |
| 160 | } |
| 161 | |
| 162 | void |
| 163 | onSendBatchFromQueue(const RibUpdateBatch& batch) |
| 164 | { |
| 165 | BOOST_ASSERT(batch.begin() != batch.end()); |
| 166 | RibUpdate update = *(batch.begin()); |
| 167 | |
| 168 | // Simulate a successful response from NFD |
| 169 | FibUpdater& updater = m_manager.m_fibUpdater; |
| 170 | m_manager.m_rib.onFibUpdateSuccess(batch, updater.m_inheritedRoutes, |
| 171 | bind(&RibManager::onRibUpdateSuccess, &m_manager, update)); |
| 172 | } |
| 173 | |
| 174 | public: |
| 175 | enum class CheckCommandResult { |
| 176 | OK, |
| 177 | OUT_OF_BOUNDARY, |
| 178 | WRONG_FORMAT, |
| 179 | WRONG_VERB, |
| 180 | WRONG_PARAMS_FORMAT, |
| 181 | WRONG_PARAMS_NAME, |
| 182 | WRONG_PARAMS_FACE |
Davide Pesavento | d2ea235 | 2017-03-12 20:38:09 -0400 | [diff] [blame] | 183 | }; |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 184 | |
| 185 | CheckCommandResult |
Davide Pesavento | d2ea235 | 2017-03-12 20:38:09 -0400 | [diff] [blame] | 186 | checkCommand(size_t idx, const char* verbStr, const ControlParameters& expectedParams) const |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 187 | { |
| 188 | if (idx > m_commands.size()) { |
| 189 | return CheckCommandResult::OUT_OF_BOUNDARY; |
| 190 | } |
| 191 | const auto& name = m_commands[idx].getName(); |
| 192 | |
| 193 | if (!FIB_COMMAND_PREFIX.isPrefixOf(name) || FIB_COMMAND_PREFIX.size() >= name.size()) { |
| 194 | return CheckCommandResult::WRONG_FORMAT; |
| 195 | } |
| 196 | const auto& verb = name[FIB_COMMAND_PREFIX.size()]; |
| 197 | |
| 198 | Name::Component expectedVerb(verbStr); |
| 199 | if (verb != expectedVerb) { |
| 200 | return CheckCommandResult::WRONG_VERB; |
| 201 | } |
| 202 | |
| 203 | ControlParameters parameters; |
| 204 | try { |
| 205 | Block rawParameters = name[FIB_COMMAND_PREFIX.size() + 1].blockFromValue(); |
| 206 | parameters.wireDecode(rawParameters); |
| 207 | } |
| 208 | catch (...) { |
| 209 | return CheckCommandResult::WRONG_PARAMS_FORMAT; |
| 210 | } |
| 211 | |
| 212 | if (parameters.getName() != expectedParams.getName()) { |
| 213 | return CheckCommandResult::WRONG_PARAMS_NAME; |
| 214 | } |
| 215 | |
| 216 | if (parameters.getFaceId() != expectedParams.getFaceId()) { |
| 217 | return CheckCommandResult::WRONG_PARAMS_FACE; |
| 218 | } |
| 219 | |
| 220 | return CheckCommandResult::OK; |
| 221 | } |
| 222 | |
| 223 | public: |
Davide Pesavento | d2ea235 | 2017-03-12 20:38:09 -0400 | [diff] [blame] | 224 | static const Name FIB_COMMAND_PREFIX; |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 225 | std::vector<Interest>& m_commands; |
| 226 | ConfigurationStatus m_status; |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 227 | |
| 228 | protected: |
| 229 | RibManager m_manager; |
| 230 | Rib& m_rib; |
| 231 | }; |
| 232 | |
| 233 | const Name RibManagerFixture::FIB_COMMAND_PREFIX("/localhost/nfd/fib"); |
| 234 | |
| 235 | std::ostream& |
Davide Pesavento | d2ea235 | 2017-03-12 20:38:09 -0400 | [diff] [blame] | 236 | operator<<(std::ostream& os, RibManagerFixture::CheckCommandResult result) |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 237 | { |
| 238 | switch (result) { |
| 239 | case RibManagerFixture::CheckCommandResult::OK: |
Davide Pesavento | d2ea235 | 2017-03-12 20:38:09 -0400 | [diff] [blame] | 240 | return os << "OK"; |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 241 | case RibManagerFixture::CheckCommandResult::OUT_OF_BOUNDARY: |
Davide Pesavento | d2ea235 | 2017-03-12 20:38:09 -0400 | [diff] [blame] | 242 | return os << "OUT_OF_BOUNDARY"; |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 243 | case RibManagerFixture::CheckCommandResult::WRONG_FORMAT: |
Davide Pesavento | d2ea235 | 2017-03-12 20:38:09 -0400 | [diff] [blame] | 244 | return os << "WRONG_FORMAT"; |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 245 | case RibManagerFixture::CheckCommandResult::WRONG_VERB: |
Davide Pesavento | d2ea235 | 2017-03-12 20:38:09 -0400 | [diff] [blame] | 246 | return os << "WRONG_VERB"; |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 247 | case RibManagerFixture::CheckCommandResult::WRONG_PARAMS_FORMAT: |
Davide Pesavento | d2ea235 | 2017-03-12 20:38:09 -0400 | [diff] [blame] | 248 | return os << "WRONG_PARAMS_FORMAT"; |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 249 | case RibManagerFixture::CheckCommandResult::WRONG_PARAMS_NAME: |
Davide Pesavento | d2ea235 | 2017-03-12 20:38:09 -0400 | [diff] [blame] | 250 | return os << "WRONG_PARAMS_NAME"; |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 251 | case RibManagerFixture::CheckCommandResult::WRONG_PARAMS_FACE: |
Davide Pesavento | d2ea235 | 2017-03-12 20:38:09 -0400 | [diff] [blame] | 252 | return os << "WRONG_PARAMS_FACE"; |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 253 | }; |
| 254 | |
Davide Pesavento | d2ea235 | 2017-03-12 20:38:09 -0400 | [diff] [blame] | 255 | return os << static_cast<int>(result); |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 256 | } |
| 257 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 258 | BOOST_AUTO_TEST_SUITE(TestRibManager) |
| 259 | |
| 260 | class AddTopPrefixFixture : public RibManagerFixture |
| 261 | { |
| 262 | public: |
| 263 | AddTopPrefixFixture() |
| 264 | : RibManagerFixture({true, true}, false) |
| 265 | { |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 266 | } |
| 267 | }; |
| 268 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 269 | BOOST_FIXTURE_TEST_CASE(AddTopPrefix, AddTopPrefixFixture) |
Steve DiBenedetto | cd4ee5f | 2014-12-08 16:09:11 -0700 | [diff] [blame] | 270 | { |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 271 | BOOST_CHECK_EQUAL(m_rib.size(), 2); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 272 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 273 | std::vector<Name> ribEntryNames; |
| 274 | for (auto&& entry : m_rib) { |
| 275 | ribEntryNames.push_back(entry.first); |
| 276 | } |
| 277 | BOOST_CHECK_EQUAL(ribEntryNames[0], "/localhop/nfd"); |
| 278 | BOOST_CHECK_EQUAL(ribEntryNames[1], "/localhost/nfd"); |
Steve DiBenedetto | cd4ee5f | 2014-12-08 16:09:11 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 281 | class UnauthorizedRibManagerFixture : public RibManagerFixture |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 282 | { |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 283 | public: |
| 284 | UnauthorizedRibManagerFixture() |
| 285 | : RibManagerFixture({false, false}, true) |
| 286 | { |
| 287 | } |
| 288 | }; |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 289 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 290 | class LocalhostAuthorizedRibManagerFixture : public RibManagerFixture |
| 291 | { |
| 292 | public: |
| 293 | LocalhostAuthorizedRibManagerFixture() |
| 294 | : RibManagerFixture({true, false}, true) |
| 295 | { |
| 296 | } |
| 297 | }; |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 298 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 299 | class LocalhopAuthorizedRibManagerFixture : public RibManagerFixture |
| 300 | { |
| 301 | public: |
| 302 | LocalhopAuthorizedRibManagerFixture() |
| 303 | : RibManagerFixture({false, true}, true) |
| 304 | { |
| 305 | } |
| 306 | }; |
| 307 | |
| 308 | class AuthorizedRibManagerFixture : public RibManagerFixture |
| 309 | { |
| 310 | public: |
| 311 | AuthorizedRibManagerFixture() |
| 312 | : RibManagerFixture({true, true}, true) |
| 313 | { |
| 314 | } |
| 315 | }; |
| 316 | |
Davide Pesavento | d2ea235 | 2017-03-12 20:38:09 -0400 | [diff] [blame] | 317 | using AllFixtures = boost::mpl::vector< |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 318 | UnauthorizedRibManagerFixture, |
| 319 | LocalhostAuthorizedRibManagerFixture, |
| 320 | LocalhopAuthorizedRibManagerFixture, |
| 321 | AuthorizedRibManagerFixture |
Davide Pesavento | d2ea235 | 2017-03-12 20:38:09 -0400 | [diff] [blame] | 322 | >; |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 323 | |
| 324 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(CommandAuthorization, T, AllFixtures, T) |
| 325 | { |
| 326 | auto parameters = this->makeRegisterParameters("/test-authorization", 9527); |
| 327 | auto commandHost = this->makeControlCommandRequest("/localhost/nfd/rib/register", parameters); |
| 328 | auto commandHop = this->makeControlCommandRequest("/localhop/nfd/rib/register", parameters); |
| 329 | auto successResp = this->makeResponse(200, "Success", parameters); |
| 330 | auto failureResp = ControlResponse(403, "authorization rejected"); |
| 331 | |
| 332 | BOOST_CHECK_EQUAL(this->m_responses.size(), 0); |
| 333 | this->receiveInterest(commandHost); |
| 334 | this->receiveInterest(commandHop); |
| 335 | |
| 336 | auto nExpectedResponses = this->m_status.isLocalhopConfigured ? 2 : 1; |
| 337 | auto expectedLocalhostResponse = this->m_status.isLocalhostConfigured ? successResp : failureResp; |
| 338 | auto expectedLocalhopResponse = this->m_status.isLocalhopConfigured ? successResp : failureResp; |
| 339 | |
| 340 | BOOST_REQUIRE_EQUAL(this->m_responses.size(), nExpectedResponses); |
| 341 | BOOST_CHECK_EQUAL(this->checkResponse(0, commandHost->getName(), expectedLocalhostResponse), |
| 342 | ManagerCommonFixture::CheckResponseResult::OK); |
| 343 | if (nExpectedResponses == 2) { |
| 344 | BOOST_CHECK_EQUAL(this->checkResponse(1, commandHop->getName(), expectedLocalhopResponse), |
| 345 | ManagerCommonFixture::CheckResponseResult::OK); |
| 346 | } |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 347 | } |
| 348 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 349 | BOOST_FIXTURE_TEST_SUITE(RegisterUnregister, LocalhostAuthorizedRibManagerFixture) |
| 350 | |
| 351 | BOOST_AUTO_TEST_CASE(Basic) |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 352 | { |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 353 | auto paramsRegister = makeRegisterParameters("/test-register-unregister", 9527); |
| 354 | auto paramsUnregister = makeUnregisterParameters("/test-register-unregister", 9527); |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 355 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 356 | auto setInFaceId = [] (shared_ptr<Interest> commandInterest) { |
| 357 | commandInterest->setTag(make_shared<lp::IncomingFaceIdTag>(1234)); |
| 358 | }; |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 359 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 360 | auto commandRegister = makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister, setInFaceId); |
| 361 | auto commandUnregister = makeControlCommandRequest("/localhost/nfd/rib/unregister", paramsUnregister, setInFaceId); |
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 | receiveInterest(commandRegister); |
| 364 | receiveInterest(commandUnregister); |
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 | BOOST_REQUIRE_EQUAL(m_responses.size(), 2); |
| 367 | BOOST_CHECK_EQUAL(checkResponse(0, commandRegister->getName(), makeResponse(200, "Success", paramsRegister)), |
| 368 | CheckResponseResult::OK); |
| 369 | BOOST_CHECK_EQUAL(checkResponse(1, commandUnregister->getName(), makeResponse(200, "Success", paramsUnregister)), |
| 370 | CheckResponseResult::OK); |
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 | BOOST_REQUIRE_EQUAL(m_commands.size(), 2); |
| 373 | BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK); |
| 374 | BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK); |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 375 | } |
| 376 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 377 | BOOST_AUTO_TEST_CASE(SelfOperation) |
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 | auto paramsRegister = makeRegisterParameters("/test-self-register-unregister"); |
| 380 | auto paramsUnregister = makeUnregisterParameters("/test-self-register-unregister"); |
| 381 | BOOST_CHECK_EQUAL(paramsRegister.getFaceId(), 0); |
| 382 | BOOST_CHECK_EQUAL(paramsUnregister.getFaceId(), 0); |
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 | const uint64_t inFaceId = 9527; |
| 385 | auto setInFaceId = [&inFaceId] (shared_ptr<Interest> commandInterest) { |
| 386 | commandInterest->setTag(make_shared<lp::IncomingFaceIdTag>(inFaceId)); |
| 387 | }; |
| 388 | auto commandRegister = makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister, setInFaceId); |
| 389 | auto commandUnregister = makeControlCommandRequest("/localhost/nfd/rib/unregister", paramsUnregister, setInFaceId); |
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 | receiveInterest(commandRegister); |
| 392 | receiveInterest(commandUnregister); |
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 | paramsRegister.setFaceId(inFaceId); |
| 395 | paramsUnregister.setFaceId(inFaceId); |
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 | BOOST_REQUIRE_EQUAL(m_responses.size(), 2); |
| 398 | BOOST_CHECK_EQUAL(checkResponse(0, commandRegister->getName(), makeResponse(200, "Success", paramsRegister)), |
| 399 | CheckResponseResult::OK); |
| 400 | BOOST_CHECK_EQUAL(checkResponse(1, commandUnregister->getName(), makeResponse(200, "Success", paramsUnregister)), |
| 401 | CheckResponseResult::OK); |
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 | BOOST_REQUIRE_EQUAL(m_commands.size(), 2); |
| 404 | BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK); |
| 405 | BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK); |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 406 | } |
| 407 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 408 | BOOST_AUTO_TEST_CASE(Expiration) |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 409 | { |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 410 | auto paramsRegister = makeRegisterParameters("/test-expiry", 9527, time::milliseconds(50)); |
| 411 | auto paramsUnregister = makeRegisterParameters("/test-expiry", 9527); |
| 412 | receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister)); |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 413 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 414 | advanceClocks(time::milliseconds(55)); |
| 415 | BOOST_REQUIRE_EQUAL(m_commands.size(), 2); // the registered route has expired |
| 416 | BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK); |
| 417 | BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK); |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 418 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 419 | m_commands.clear(); |
| 420 | paramsRegister.setExpirationPeriod(time::milliseconds(100)); |
| 421 | receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister)); |
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 | advanceClocks(time::milliseconds(55)); |
| 424 | BOOST_REQUIRE_EQUAL(m_commands.size(), 1); // the registered route is still active |
| 425 | BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK); |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 428 | BOOST_AUTO_TEST_SUITE_END() // RegisterUnregister |
| 429 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 430 | BOOST_FIXTURE_TEST_CASE(RibDataset, UnauthorizedRibManagerFixture) |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 431 | { |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 432 | uint64_t faceId = 0; |
Davide Pesavento | d2ea235 | 2017-03-12 20:38:09 -0400 | [diff] [blame] | 433 | auto generateRoute = [&faceId] () -> Route { |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 434 | Route route; |
| 435 | route.faceId = ++faceId; |
Davide Pesavento | d2ea235 | 2017-03-12 20:38:09 -0400 | [diff] [blame] | 436 | route.cost = route.faceId * 10; |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 437 | route.expires = time::steady_clock::TimePoint::max(); |
| 438 | return route; |
| 439 | }; |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 440 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 441 | const size_t nEntries = 108; |
| 442 | std::set<Name> actualPrefixes; |
| 443 | for (size_t i = 0; i < nEntries; ++i) { |
| 444 | Name prefix = Name("/test-dataset").appendNumber(i); |
| 445 | actualPrefixes.insert(prefix); |
| 446 | m_rib.insert(prefix, generateRoute()); |
| 447 | if (i & 0x1) { |
| 448 | m_rib.insert(prefix, generateRoute()); |
| 449 | m_rib.insert(prefix, generateRoute()); |
| 450 | } |
| 451 | } |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 452 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 453 | receiveInterest(makeInterest("/localhost/nfd/rib/list")); |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 454 | |
Davide Pesavento | d2ea235 | 2017-03-12 20:38:09 -0400 | [diff] [blame] | 455 | Block content = concatenateResponses(); |
| 456 | content.parse(); |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 457 | BOOST_REQUIRE_EQUAL(content.elements().size(), nEntries); |
| 458 | |
Davide Pesavento | d2ea235 | 2017-03-12 20:38:09 -0400 | [diff] [blame] | 459 | std::vector<ndn::nfd::RibEntry> receivedRecords, expectedRecords; |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 460 | for (size_t idx = 0; idx < nEntries; ++idx) { |
Davide Pesavento | d2ea235 | 2017-03-12 20:38:09 -0400 | [diff] [blame] | 461 | ndn::nfd::RibEntry decodedEntry(content.elements()[idx]); |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 462 | receivedRecords.push_back(decodedEntry); |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 463 | actualPrefixes.erase(decodedEntry.getName()); |
| 464 | |
| 465 | auto matchedEntryIt = m_rib.find(decodedEntry.getName()); |
| 466 | BOOST_REQUIRE(matchedEntryIt != m_rib.end()); |
| 467 | |
| 468 | auto matchedEntry = matchedEntryIt->second; |
| 469 | BOOST_REQUIRE(matchedEntry != nullptr); |
| 470 | |
Davide Pesavento | d2ea235 | 2017-03-12 20:38:09 -0400 | [diff] [blame] | 471 | expectedRecords.emplace_back(); |
| 472 | expectedRecords.back().setName(matchedEntry->getName()); |
| 473 | for (const auto& route : matchedEntry->getRoutes()) { |
| 474 | expectedRecords.back().addRoute(ndn::nfd::Route() |
| 475 | .setFaceId(route.faceId) |
| 476 | .setOrigin(route.origin) |
| 477 | .setCost(route.cost) |
| 478 | .setFlags(route.flags)); |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 479 | } |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | BOOST_CHECK_EQUAL(actualPrefixes.size(), 0); |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 483 | BOOST_CHECK_EQUAL_COLLECTIONS(receivedRecords.begin(), receivedRecords.end(), |
| 484 | expectedRecords.begin(), expectedRecords.end()); |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 485 | } |
| 486 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 487 | BOOST_FIXTURE_TEST_SUITE(FaceMonitor, LocalhostAuthorizedRibManagerFixture) |
| 488 | |
| 489 | BOOST_AUTO_TEST_CASE(FetchActiveFacesEvent) |
Vince Lehman | cd16c83 | 2014-07-23 15:14:55 -0700 | [diff] [blame] | 490 | { |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 491 | BOOST_CHECK_EQUAL(m_commands.size(), 0); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 492 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 493 | advanceClocks(time::seconds(301)); // RibManager::ACTIVE_FACE_FETCH_INTERVAL = 300s |
| 494 | BOOST_REQUIRE_EQUAL(m_commands.size(), 2); |
| 495 | BOOST_CHECK_EQUAL(m_commands[0].getName(), "/localhost/nfd/faces/events"); |
| 496 | BOOST_CHECK_EQUAL(m_commands[1].getName(), "/localhost/nfd/faces/list"); |
Vince Lehman | cd16c83 | 2014-07-23 15:14:55 -0700 | [diff] [blame] | 497 | } |
| 498 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 499 | BOOST_AUTO_TEST_CASE(RemoveInvalidFaces) |
Vince Lehman | 281ded7 | 2014-08-21 12:17:08 -0500 | [diff] [blame] | 500 | { |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 501 | auto parameters1 = makeRegisterParameters("/test-remove-invalid-faces-1"); |
| 502 | auto parameters2 = makeRegisterParameters("/test-remove-invalid-faces-2"); |
| 503 | receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1.setFaceId(1))); |
| 504 | receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1.setFaceId(2))); |
| 505 | receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters2.setFaceId(2))); |
| 506 | BOOST_REQUIRE_EQUAL(m_rib.size(), 3); |
Vince Lehman | 281ded7 | 2014-08-21 12:17:08 -0500 | [diff] [blame] | 507 | |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 508 | ndn::nfd::FaceStatus status; |
| 509 | status.setFaceId(1); |
Weiwei Liu | 6e21cdb | 2016-09-29 15:16:23 -0700 | [diff] [blame] | 510 | std::vector<ndn::nfd::FaceStatus> activeFaces; |
| 511 | activeFaces.push_back(status); |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 512 | |
Weiwei Liu | 6e21cdb | 2016-09-29 15:16:23 -0700 | [diff] [blame] | 513 | m_manager.removeInvalidFaces(activeFaces); |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 514 | advanceClocks(time::milliseconds(100)); |
| 515 | BOOST_REQUIRE_EQUAL(m_rib.size(), 1); |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 516 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 517 | auto it1 = m_rib.find("/test-remove-invalid-faces-1"); |
| 518 | auto it2 = m_rib.find("/test-remove-invalid-faces-2"); |
| 519 | BOOST_CHECK(it2 == m_rib.end()); |
| 520 | BOOST_REQUIRE(it1 != m_rib.end()); |
| 521 | BOOST_CHECK(it1->second->hasFaceId(1)); |
| 522 | BOOST_CHECK(!it1->second->hasFaceId(2)); |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 523 | } |
| 524 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 525 | BOOST_AUTO_TEST_CASE(OnNotification) |
Vince Lehman | 7c7d33a | 2015-01-20 17:40:26 -0600 | [diff] [blame] | 526 | { |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 527 | auto parameters1 = makeRegisterParameters("/test-face-event-notification-1", 1); |
| 528 | auto parameters2 = makeRegisterParameters("/test-face-event-notification-2", 1); |
| 529 | receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1)); |
| 530 | receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters2)); |
| 531 | BOOST_REQUIRE_EQUAL(m_rib.size(), 2); |
Vince Lehman | 7c7d33a | 2015-01-20 17:40:26 -0600 | [diff] [blame] | 532 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 533 | auto makeNotification = [] (ndn::nfd::FaceEventKind eventKind, uint64_t faceId) -> ndn::nfd::FaceEventNotification { |
| 534 | ndn::nfd::FaceEventNotification notification; |
| 535 | notification.setKind(eventKind).setFaceId(faceId); |
| 536 | return notification; |
| 537 | }; |
Vince Lehman | 7c7d33a | 2015-01-20 17:40:26 -0600 | [diff] [blame] | 538 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 539 | m_manager.onNotification(makeNotification(ndn::nfd::FaceEventKind::FACE_EVENT_DESTROYED, 1)); |
| 540 | advanceClocks(time::milliseconds(100)); |
| 541 | BOOST_CHECK_EQUAL(m_rib.size(), 0); |
Vince Lehman | 7c7d33a | 2015-01-20 17:40:26 -0600 | [diff] [blame] | 542 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 543 | m_manager.onNotification(makeNotification(ndn::nfd::FaceEventKind::FACE_EVENT_CREATED, 2)); |
| 544 | advanceClocks(time::milliseconds(100)); |
| 545 | BOOST_CHECK_EQUAL(m_rib.size(), 0); |
Vince Lehman | 7c7d33a | 2015-01-20 17:40:26 -0600 | [diff] [blame] | 546 | } |
| 547 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 548 | BOOST_AUTO_TEST_SUITE_END() // FaceMonitor |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 549 | BOOST_AUTO_TEST_SUITE_END() // TestRibManager |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 550 | |
| 551 | } // namespace tests |
| 552 | } // namespace rib |
| 553 | } // namespace nfd |