Yanbiao Li | d7c9636 | 2015-01-30 23:58:24 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2015, Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | #include "rib/auto-prefix-propagator.hpp" |
| 27 | |
| 28 | #include "tests/test-common.hpp" |
| 29 | #include "tests/identity-management-fixture.hpp" |
| 30 | #include <ndn-cxx/util/dummy-client-face.hpp> |
| 31 | |
| 32 | namespace nfd { |
| 33 | namespace rib { |
| 34 | namespace tests { |
| 35 | |
| 36 | NFD_LOG_INIT("AutoPrefixPropagatorTest"); |
| 37 | |
| 38 | const Name TEST_LINK_LOCAL_NFD_PREFIX("/localhop/nfd"); |
| 39 | const time::milliseconds TEST_PREFIX_PROPAGATION_TIMEOUT(1000); |
| 40 | |
| 41 | class AutoPrefixPropagatorFixture : public nfd::tests::IdentityManagementFixture |
| 42 | , public nfd::tests::UnitTestTimeFixture |
| 43 | { |
| 44 | public: |
| 45 | AutoPrefixPropagatorFixture() |
| 46 | : m_face(ndn::util::makeDummyClientFace(nfd::tests::UnitTestTimeFixture::g_io, {true, true})) |
| 47 | , m_controller(*m_face, m_keyChain) |
| 48 | , m_propagator(m_controller, m_keyChain, m_rib) |
| 49 | , m_requests(m_face->sentInterests) |
| 50 | , m_entries(m_propagator.m_propagatedEntries) |
| 51 | { |
| 52 | m_propagator.enable(); |
| 53 | m_propagator.m_controlParameters |
| 54 | .setCost(15) |
| 55 | .setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT)// set origin to client. |
| 56 | .setFaceId(0);// the remote hub will take the input face as the faceId. |
| 57 | m_propagator.m_commandOptions |
| 58 | .setPrefix(TEST_LINK_LOCAL_NFD_PREFIX) |
| 59 | .setTimeout(TEST_PREFIX_PROPAGATION_TIMEOUT); |
| 60 | } |
| 61 | |
| 62 | public: // helpers for test |
| 63 | bool |
| 64 | insertEntryToRib(const Name& name, const uint64_t& faceId = 0) |
| 65 | { |
| 66 | if (m_rib.find(name) != m_rib.end()) { |
| 67 | NFD_LOG_INFO("RIB entry already exists: " << name); |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | Route route; |
| 72 | route.faceId = faceId; |
| 73 | m_rib.insert(name, route); |
| 74 | advanceClocks(time::milliseconds(1)); |
| 75 | |
| 76 | return m_rib.find(name) != m_rib.end(); // return whether afterInserEntry will be triggered |
| 77 | } |
| 78 | |
| 79 | bool |
| 80 | eraseEntryFromRib(const Name& name) |
| 81 | { |
| 82 | if (m_rib.find(name) == m_rib.end()) { |
| 83 | NFD_LOG_INFO("RIB entry does not exist: " << name); |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | std::vector<Route> routeList; |
| 88 | std::copy(m_rib.find(name)->second->begin(), m_rib.find(name)->second->end(), |
| 89 | std::back_inserter(routeList)); |
| 90 | for (auto&& route : routeList) { |
| 91 | m_rib.erase(name, route); |
| 92 | } |
| 93 | advanceClocks(time::milliseconds(1)); |
| 94 | |
| 95 | return m_rib.find(name) == m_rib.end(); // return whether afterEraseEntry will be triggered |
| 96 | } |
| 97 | |
| 98 | void |
| 99 | connectToHub() |
| 100 | { |
| 101 | insertEntryToRib("/localhop/nfd"); |
| 102 | } |
| 103 | |
| 104 | void |
| 105 | disconnectFromHub() |
| 106 | { |
| 107 | eraseEntryFromRib("/localhop/nfd"); |
| 108 | } |
| 109 | |
| 110 | public: // helpers for check |
| 111 | enum class CheckRequestResult { |
| 112 | OK, |
| 113 | OUT_OF_BOUNDARY, |
| 114 | INVALID_N_COMPONENTS, |
| 115 | WRONG_COMMAND_PREFIX, |
| 116 | WRONG_VERB, |
| 117 | INVALID_PARAMETERS, |
| 118 | WRONG_REGISTERING_PREFIX |
| 119 | }; |
| 120 | |
| 121 | /** |
| 122 | * @brief check a request at specified index |
| 123 | * |
| 124 | * @param idx the index of the specified request in m_requests |
| 125 | * @param verb the expected verb of request |
| 126 | * @param registeringPrefix the expected registering prefix of request |
| 127 | |
| 128 | * @retval OK the specified request has a valid name, the right verb, a valid ControlParameters |
| 129 | * and the right registering prefix |
| 130 | * @retval OUT_OF_BOUNDARY the specified index out of boundary |
| 131 | * @retval INVALID_N_COMPONENTS the number of components of the request name is invalid |
| 132 | * @retval WRONG_COMMAND_PREFIX the command prefix of the request is wrong |
| 133 | * @retval WRONG_VERB the command verb of the request is wrong |
| 134 | * @retval INVALID_PARAMETERS no valid parameters can be decoded from the request's name |
| 135 | * @retval WRONG_REGISTERING_PREFIX the registering prefix of the request is wrong |
| 136 | */ |
| 137 | CheckRequestResult |
| 138 | checkRequest(size_t idx, const std::string& verb, const Name& registeringPrefix) |
| 139 | { |
| 140 | Name requestName; |
| 141 | try { |
| 142 | requestName = m_requests.at(idx).getName(); |
| 143 | } |
| 144 | catch (const std::out_of_range&) { |
| 145 | return CheckRequestResult::OUT_OF_BOUNDARY; |
| 146 | } |
| 147 | |
| 148 | if (requestName.size() < 5) { |
| 149 | return CheckRequestResult::INVALID_N_COMPONENTS; |
| 150 | } |
| 151 | |
| 152 | if (requestName.getPrefix(2) != TEST_LINK_LOCAL_NFD_PREFIX) { |
| 153 | return CheckRequestResult::WRONG_COMMAND_PREFIX; |
| 154 | } |
| 155 | |
| 156 | if (requestName.get(3) != Name::Component(verb)) { |
| 157 | return CheckRequestResult::WRONG_VERB; |
| 158 | } |
| 159 | |
| 160 | ControlParameters parameters; |
| 161 | try { |
| 162 | parameters.wireDecode(requestName.get(4).blockFromValue()); |
| 163 | } |
| 164 | catch (const tlv::Error&) { |
| 165 | return CheckRequestResult::INVALID_PARAMETERS; |
| 166 | } |
| 167 | |
| 168 | if (parameters.getName() != registeringPrefix) { |
| 169 | return CheckRequestResult::WRONG_REGISTERING_PREFIX; |
| 170 | } |
| 171 | |
| 172 | return CheckRequestResult::OK; |
| 173 | } |
| 174 | |
| 175 | protected: |
| 176 | shared_ptr<ndn::util::DummyClientFace> m_face; |
| 177 | ndn::nfd::Controller m_controller; |
| 178 | Rib m_rib; |
| 179 | AutoPrefixPropagator m_propagator; |
| 180 | std::vector<Interest>& m_requests; // store sent out requests |
| 181 | AutoPrefixPropagator::PropagatedEntryList& m_entries; // store propagated entries |
| 182 | }; |
| 183 | |
| 184 | std::ostream& |
| 185 | operator<<(std::ostream &os, const AutoPrefixPropagatorFixture::CheckRequestResult& result) |
| 186 | { |
| 187 | switch (result) { |
| 188 | case AutoPrefixPropagatorFixture::CheckRequestResult::OK: |
| 189 | os << "OK"; |
| 190 | break; |
| 191 | case AutoPrefixPropagatorFixture::CheckRequestResult::OUT_OF_BOUNDARY: |
| 192 | os << "OUT_OF_BOUNDARY"; |
| 193 | break; |
| 194 | case AutoPrefixPropagatorFixture::CheckRequestResult::INVALID_N_COMPONENTS: |
| 195 | os << "INVALID_N_COMPONENTS"; |
| 196 | break; |
| 197 | case AutoPrefixPropagatorFixture::CheckRequestResult::WRONG_COMMAND_PREFIX: |
| 198 | os << "WRONG_COMMAND_PREFIX"; |
| 199 | break; |
| 200 | case AutoPrefixPropagatorFixture::CheckRequestResult::WRONG_VERB: |
| 201 | os << "WRONG_VERB"; |
| 202 | break; |
| 203 | case AutoPrefixPropagatorFixture::CheckRequestResult::INVALID_PARAMETERS: |
| 204 | os << "INVALID_PARAMETERS"; |
| 205 | break; |
| 206 | case AutoPrefixPropagatorFixture::CheckRequestResult::WRONG_REGISTERING_PREFIX: |
| 207 | os << "WRONG_REGISTERING_PREFIX"; |
| 208 | break; |
| 209 | default: |
| 210 | break; |
| 211 | } |
| 212 | |
| 213 | return os; |
| 214 | } |
| 215 | |
| 216 | BOOST_AUTO_TEST_SUITE(Rib) |
| 217 | |
| 218 | BOOST_FIXTURE_TEST_SUITE(TestAutoPrefixPropagator, AutoPrefixPropagatorFixture) |
| 219 | |
| 220 | BOOST_AUTO_TEST_CASE(EnableDisable) |
| 221 | { |
| 222 | connectToHub(); |
| 223 | BOOST_REQUIRE(addIdentity("/test/A")); |
| 224 | |
| 225 | auto testPropagateRevokeBasic = [this] () -> bool { |
| 226 | m_propagator.m_propagatedEntries.clear(); |
| 227 | |
| 228 | if (!insertEntryToRib("/test/A/app")) { |
| 229 | return false; |
| 230 | } |
| 231 | m_entries["/test/A"].succeed(nullptr); |
| 232 | if (!eraseEntryFromRib("/test/A/app")) { |
| 233 | return false; |
| 234 | } |
| 235 | return true; |
| 236 | }; |
| 237 | |
| 238 | m_propagator.disable(); |
| 239 | BOOST_REQUIRE(testPropagateRevokeBasic()); |
| 240 | BOOST_CHECK(m_requests.empty()); |
| 241 | |
| 242 | m_propagator.enable(); |
| 243 | BOOST_REQUIRE(testPropagateRevokeBasic()); |
| 244 | BOOST_REQUIRE_EQUAL(m_requests.size(), 2); |
| 245 | BOOST_CHECK_EQUAL(checkRequest(0, "register", "/test/A"), CheckRequestResult::OK); |
| 246 | BOOST_CHECK_EQUAL(checkRequest(1, "unregister", "/test/A"), CheckRequestResult::OK); |
| 247 | } |
| 248 | |
| 249 | BOOST_AUTO_TEST_CASE(LoadConfiguration) |
| 250 | { |
| 251 | ConfigFile config; |
| 252 | config.addSectionHandler("auto_prefix_propagate", |
| 253 | bind(&AutoPrefixPropagator::loadConfig, &m_propagator, _1)); |
| 254 | |
| 255 | const std::string CONFIG_STRING = |
| 256 | "auto_prefix_propagate\n" |
| 257 | "{\n" |
| 258 | " cost 11\n" |
| 259 | " timeout 22\n" |
| 260 | " refresh_interval 33\n" |
| 261 | " base_retry_wait 44\n" |
| 262 | " max_retry_wait 55\n" |
| 263 | "}"; |
| 264 | config.parse(CONFIG_STRING, true, "test-auto-prefix-propagator"); |
| 265 | |
| 266 | BOOST_CHECK_EQUAL(m_propagator.m_controlParameters.getCost(), 11); |
| 267 | BOOST_CHECK_EQUAL(m_propagator.m_controlParameters.getOrigin(), ndn::nfd::ROUTE_ORIGIN_CLIENT); |
| 268 | BOOST_CHECK_EQUAL(m_propagator.m_controlParameters.getFaceId(), 0); |
| 269 | |
| 270 | BOOST_CHECK_EQUAL(m_propagator.m_commandOptions.getPrefix(), TEST_LINK_LOCAL_NFD_PREFIX); |
| 271 | BOOST_CHECK_EQUAL(m_propagator.m_commandOptions.getTimeout(), time::milliseconds(22)); |
| 272 | |
| 273 | BOOST_CHECK_EQUAL(m_propagator.m_refreshInterval, time::seconds(33)); |
| 274 | BOOST_CHECK_EQUAL(m_propagator.m_baseRetryWait, time::seconds(44)); |
| 275 | BOOST_CHECK_EQUAL(m_propagator.m_maxRetryWait, time::seconds(55)); |
| 276 | } |
| 277 | |
| 278 | BOOST_AUTO_TEST_SUITE(Helpers) |
| 279 | |
| 280 | BOOST_AUTO_TEST_CASE(GetPrefixPropagationParameters) |
| 281 | { |
| 282 | BOOST_REQUIRE(addIdentity("/test/A")); |
| 283 | BOOST_REQUIRE(addIdentity("/test/A/B")); |
| 284 | BOOST_REQUIRE(addIdentity("/test/C/nrd")); |
| 285 | |
| 286 | auto parameters1 = m_propagator.getPrefixPropagationParameters("/none/A/B/app"); |
| 287 | auto parameters2 = m_propagator.getPrefixPropagationParameters("/test/A/B/app"); |
| 288 | auto parameters3 = m_propagator.getPrefixPropagationParameters("/test/C/D/app"); |
| 289 | |
| 290 | BOOST_CHECK(!parameters1.isValid); |
| 291 | |
| 292 | BOOST_CHECK(parameters2.isValid); |
| 293 | BOOST_CHECK_EQUAL(parameters2.parameters.getName(), "/test/A"); |
| 294 | BOOST_CHECK_EQUAL(parameters2.options.getSigningInfo().getSignerName(), "/test/A"); |
| 295 | |
| 296 | BOOST_CHECK(parameters3.isValid); |
| 297 | BOOST_CHECK_EQUAL(parameters3.parameters.getName(), "/test/C"); |
| 298 | BOOST_CHECK_EQUAL(parameters3.options.getSigningInfo().getSignerName(), "/test/C/nrd"); |
| 299 | } |
| 300 | |
| 301 | BOOST_AUTO_TEST_CASE(CheckCurrentPropagatedPrefix) |
| 302 | { |
| 303 | BOOST_REQUIRE(addIdentity("/test/A")); |
| 304 | BOOST_REQUIRE(addIdentity("/test/B/nrd")); |
| 305 | BOOST_REQUIRE(addIdentity("/test/A/B")); |
| 306 | |
| 307 | BOOST_CHECK(!m_propagator.doesCurrentPropagatedPrefixWork("/test/E")); // does not exist |
| 308 | BOOST_CHECK(!m_propagator.doesCurrentPropagatedPrefixWork("/test/A/B")); // has a better option |
| 309 | BOOST_CHECK(m_propagator.doesCurrentPropagatedPrefixWork("/test/A")); |
| 310 | BOOST_CHECK(m_propagator.doesCurrentPropagatedPrefixWork("/test/B")); |
| 311 | } |
| 312 | |
| 313 | BOOST_AUTO_TEST_CASE(RedoPropagation) |
| 314 | { |
| 315 | connectToHub(); |
| 316 | BOOST_REQUIRE(addIdentity("/test/A")); |
| 317 | BOOST_REQUIRE(addIdentity("/test/B")); |
| 318 | BOOST_REQUIRE(addIdentity("/test/B/C")); |
| 319 | BOOST_REQUIRE(insertEntryToRib("/test/A/app")); |
| 320 | BOOST_REQUIRE(insertEntryToRib("/test/B/C/app")); |
| 321 | BOOST_REQUIRE(insertEntryToRib("/test/B/D/app")); |
| 322 | |
| 323 | auto testRedoPropagation = [this] (const Name& signingIdentity) { |
| 324 | m_requests.clear(); |
| 325 | |
| 326 | m_entries[signingIdentity].setSigningIdentity(signingIdentity); |
| 327 | |
| 328 | auto parameters = m_propagator.m_controlParameters; |
| 329 | auto options = m_propagator.m_commandOptions; |
| 330 | ndn::security::SigningInfo info(ndn::security::SigningInfo::SIGNER_TYPE_ID, signingIdentity); |
| 331 | m_propagator.redoPropagation(m_entries.find(signingIdentity), |
| 332 | parameters.setName(signingIdentity), |
| 333 | options.setSigningInfo(info), |
| 334 | time::seconds(0)); |
| 335 | advanceClocks(time::milliseconds(1)); |
| 336 | }; |
| 337 | |
| 338 | testRedoPropagation("/test/A"); // current propagated prefix still works |
| 339 | BOOST_REQUIRE_EQUAL(m_requests.size(), 1); |
| 340 | BOOST_CHECK_EQUAL(checkRequest(0, "register", "/test/A"), CheckRequestResult::OK); |
| 341 | BOOST_CHECK(m_entries.find("test/A") != m_entries.end()); |
| 342 | |
| 343 | BOOST_CHECK_NO_THROW(m_keyChain.deleteIdentity("/test/B")); |
| 344 | testRedoPropagation("/test/B"); // signingIdentity no longer exists |
| 345 | BOOST_REQUIRE_EQUAL(m_requests.size(), 1); |
| 346 | BOOST_CHECK_EQUAL(checkRequest(0, "register", "/test/B/C"), CheckRequestResult::OK); |
| 347 | BOOST_CHECK(m_entries.find("/test/B") == m_entries.end()); |
| 348 | BOOST_CHECK(m_entries.find("/test/B/C") != m_entries.end()); |
| 349 | |
| 350 | testRedoPropagation("/test/B"); // no alternative identity |
| 351 | BOOST_CHECK(m_requests.empty()); |
| 352 | BOOST_CHECK(m_entries.find("/test/B") == m_entries.end()); |
| 353 | |
| 354 | m_entries["/test/B/C"].succeed(nullptr); |
| 355 | testRedoPropagation("/test/B"); // alternative identity has been propagated |
| 356 | BOOST_CHECK(m_requests.empty()); |
| 357 | BOOST_CHECK(m_entries.find("/test/B") == m_entries.end()); |
| 358 | |
| 359 | BOOST_REQUIRE(addIdentity("/test/B/")); |
| 360 | testRedoPropagation("/test/B/C"); // better option exists: /test/B |
| 361 | BOOST_REQUIRE_EQUAL(m_requests.size(), 1); |
| 362 | BOOST_CHECK_EQUAL(checkRequest(0, "register", "/test/B"), CheckRequestResult::OK); |
| 363 | BOOST_CHECK(m_entries.find("test/B/C") == m_entries.end()); |
| 364 | } |
| 365 | |
| 366 | BOOST_AUTO_TEST_SUITE_END() // Helpers |
| 367 | |
| 368 | BOOST_AUTO_TEST_SUITE(PropagateRevokeSemantics) |
| 369 | |
| 370 | BOOST_AUTO_TEST_CASE(Basic) |
| 371 | { |
| 372 | connectToHub(); // ensure connectivity to the hub |
| 373 | BOOST_REQUIRE(addIdentity("/test/A")); |
| 374 | |
| 375 | BOOST_REQUIRE(insertEntryToRib("/test/A/app")); // ensure afterInsertEntry signal emitted |
| 376 | m_entries["/test/A"].succeed(nullptr); // ensure there is a valid entry inserted |
| 377 | BOOST_REQUIRE(eraseEntryFromRib("/test/A/app")); // ensure afterEraseEntry signal emitted |
| 378 | |
| 379 | BOOST_REQUIRE_EQUAL(m_requests.size(), 2); |
| 380 | BOOST_CHECK_EQUAL(checkRequest(0, "register", "/test/A"), CheckRequestResult::OK); |
| 381 | BOOST_CHECK_EQUAL(checkRequest(1, "unregister", "/test/A"), CheckRequestResult::OK); |
| 382 | } |
| 383 | |
| 384 | BOOST_AUTO_TEST_CASE(LocalPrefix) |
| 385 | { |
| 386 | connectToHub(); |
| 387 | BOOST_REQUIRE(addIdentity("/localhost/A")); |
| 388 | |
| 389 | BOOST_REQUIRE(insertEntryToRib("/localhost/A/app")); |
| 390 | BOOST_CHECK(m_requests.empty()); |
| 391 | |
| 392 | m_propagator.m_propagatedEntries["/localhost/A"].succeed(nullptr); |
| 393 | BOOST_REQUIRE(eraseEntryFromRib("/localhost/A/app")); |
| 394 | BOOST_CHECK(m_requests.empty()); |
| 395 | } |
| 396 | |
| 397 | BOOST_AUTO_TEST_CASE(InvalidPropagationParameters) |
| 398 | { |
| 399 | connectToHub(); |
| 400 | |
| 401 | // no identity can be found |
| 402 | BOOST_CHECK(!m_propagator.getPrefixPropagationParameters("/test/A/app").isValid); |
| 403 | |
| 404 | BOOST_REQUIRE(insertEntryToRib("/test/A/app")); |
| 405 | BOOST_CHECK(m_requests.empty()); |
| 406 | |
| 407 | m_entries["/test/A"].succeed(nullptr); |
| 408 | BOOST_REQUIRE(eraseEntryFromRib("/test/A/app")); |
| 409 | BOOST_CHECK(m_requests.empty()); |
| 410 | } |
| 411 | |
| 412 | BOOST_AUTO_TEST_CASE(NoHubConnectivity) |
| 413 | { |
| 414 | BOOST_REQUIRE(addIdentity("/test/A")); |
| 415 | |
| 416 | BOOST_REQUIRE(insertEntryToRib("/test/A/app")); |
| 417 | BOOST_CHECK(m_requests.empty()); |
| 418 | |
| 419 | m_entries["/test/A"].succeed(nullptr); |
| 420 | BOOST_REQUIRE(eraseEntryFromRib("/test/A/app")); |
| 421 | BOOST_CHECK(m_requests.empty()); |
| 422 | } |
| 423 | |
| 424 | BOOST_AUTO_TEST_CASE(PropagatedEntryExists) |
| 425 | { |
| 426 | connectToHub(); |
| 427 | BOOST_REQUIRE(addIdentity("/test/A")); |
| 428 | BOOST_REQUIRE(insertEntryToRib("/test/A/app1")); |
| 429 | |
| 430 | m_requests.clear(); |
| 431 | BOOST_REQUIRE(insertEntryToRib("/test/A/app2")); // /test/A has been propagated by /test/A/app1 |
| 432 | BOOST_CHECK(m_requests.empty()); |
| 433 | } |
| 434 | |
| 435 | BOOST_AUTO_TEST_CASE(PropagatedEntryShouldKeep) |
| 436 | { |
| 437 | connectToHub(); |
| 438 | BOOST_REQUIRE(addIdentity("/test/A")); |
| 439 | BOOST_REQUIRE(insertEntryToRib("/test/A/app1")); |
| 440 | BOOST_REQUIRE(insertEntryToRib("/test/A/app2")); |
| 441 | |
| 442 | m_requests.clear(); |
| 443 | BOOST_REQUIRE(eraseEntryFromRib("/test/A/app2")); // /test/A should be kept for /test/A/app1 |
| 444 | BOOST_CHECK(m_requests.empty()); |
| 445 | } |
| 446 | |
| 447 | BOOST_AUTO_TEST_CASE(BackOffRetryPolicy) |
| 448 | { |
| 449 | m_propagator.m_commandOptions.setTimeout(time::milliseconds(1)); |
| 450 | m_propagator.m_baseRetryWait = time::seconds(1); |
| 451 | m_propagator.m_maxRetryWait = time::seconds(2); |
| 452 | |
| 453 | connectToHub(); |
| 454 | BOOST_REQUIRE(addIdentity("/test/A")); |
| 455 | BOOST_REQUIRE(insertEntryToRib("/test/A/app")); |
| 456 | |
| 457 | BOOST_REQUIRE_EQUAL(m_requests.size(), 1); |
| 458 | BOOST_CHECK_EQUAL(checkRequest(0, "register", "/test/A"), CheckRequestResult::OK); |
| 459 | |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame^] | 460 | advanceClocks(time::milliseconds(10), time::milliseconds(1050)); // wait for the 1st retry |
Yanbiao Li | d7c9636 | 2015-01-30 23:58:24 -0800 | [diff] [blame] | 461 | BOOST_REQUIRE_EQUAL(m_requests.size(), 2); |
| 462 | BOOST_CHECK_EQUAL(checkRequest(1, "register", "/test/A"), CheckRequestResult::OK); |
| 463 | |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame^] | 464 | advanceClocks(time::milliseconds(10), time::milliseconds(2050)); // wait for the 2nd retry, 2 times |
Yanbiao Li | d7c9636 | 2015-01-30 23:58:24 -0800 | [diff] [blame] | 465 | BOOST_REQUIRE_EQUAL(m_requests.size(), 3); |
| 466 | BOOST_CHECK_EQUAL(checkRequest(2, "register", "/test/A"), CheckRequestResult::OK); |
| 467 | |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame^] | 468 | advanceClocks(time::milliseconds(10), time::milliseconds(2050)); // wait for the 3rd retry, reach the upper bound |
Yanbiao Li | d7c9636 | 2015-01-30 23:58:24 -0800 | [diff] [blame] | 469 | BOOST_REQUIRE_EQUAL(m_requests.size(), 4); |
| 470 | BOOST_CHECK_EQUAL(checkRequest(3, "register", "/test/A"), CheckRequestResult::OK); |
| 471 | } |
| 472 | |
| 473 | BOOST_AUTO_TEST_SUITE_END() // PropagateRevokeSemantics |
| 474 | |
| 475 | BOOST_AUTO_TEST_SUITE(PropagatedEntryStateChanges) |
| 476 | |
| 477 | BOOST_AUTO_TEST_CASE(AfterRibInsert) |
| 478 | { |
| 479 | BOOST_REQUIRE(addIdentity("/test/A")); |
| 480 | |
| 481 | auto testAfterRibInsert = [this] (const Name& ribEntryPrefix) { |
| 482 | m_requests.clear(); |
| 483 | m_propagator.m_propagatedEntries.clear(); // ensure entry does not exist |
| 484 | |
| 485 | auto propagateParameters = m_propagator.getPrefixPropagationParameters(ribEntryPrefix); |
| 486 | m_propagator.afterRibInsert(propagateParameters.parameters, propagateParameters.options); |
| 487 | advanceClocks(time::milliseconds(1)); |
| 488 | }; |
| 489 | |
| 490 | testAfterRibInsert("/test/A/app1"); |
| 491 | BOOST_CHECK(m_requests.empty()); // no connectivity now |
| 492 | BOOST_CHECK(m_entries.find("/test/A") != m_entries.end()); |
| 493 | |
| 494 | connectToHub(); |
| 495 | testAfterRibInsert("/test/A/app2"); |
| 496 | BOOST_REQUIRE_EQUAL(m_requests.size(), 1); |
| 497 | BOOST_CHECK_EQUAL(checkRequest(0, "register", "/test/A"), CheckRequestResult::OK); |
| 498 | BOOST_CHECK(m_entries.find("/test/A") != m_entries.end()); |
| 499 | } |
| 500 | |
| 501 | BOOST_AUTO_TEST_CASE(AfterRibErase) |
| 502 | { |
| 503 | BOOST_REQUIRE(addIdentity("/test/A")); |
| 504 | |
| 505 | auto testAfterRibInsert = [this] (const Name& localUnregPrefix) { |
| 506 | m_requests.clear(); |
| 507 | |
| 508 | auto propagateParameters = m_propagator.getPrefixPropagationParameters(localUnregPrefix); |
| 509 | m_propagator.afterRibErase(propagateParameters.parameters.unsetCost(), |
| 510 | propagateParameters.options); |
| 511 | advanceClocks(time::milliseconds(1)); |
| 512 | }; |
| 513 | |
| 514 | m_entries["/test/A"].succeed(nullptr); |
| 515 | testAfterRibInsert("/test/A/app"); |
| 516 | BOOST_CHECK(m_requests.empty()); // no connectivity |
| 517 | BOOST_CHECK(m_entries.find("/test/A") == m_entries.end()); // has been erased |
| 518 | |
| 519 | connectToHub(); |
| 520 | m_entries["/test/A"].fail(nullptr); |
| 521 | testAfterRibInsert("/test/A/app"); |
| 522 | BOOST_CHECK(m_requests.empty()); // previous propagation has not succeeded |
| 523 | BOOST_CHECK(m_entries.find("/test/A") == m_entries.end()); // has been erased |
| 524 | |
| 525 | m_entries["/test/A"].succeed(nullptr); |
| 526 | testAfterRibInsert("/test/A/app"); |
| 527 | BOOST_REQUIRE_EQUAL(m_requests.size(), 1); |
| 528 | BOOST_CHECK_EQUAL(checkRequest(0, "unregister", "/test/A"), CheckRequestResult::OK); |
| 529 | BOOST_CHECK(m_entries.find("/test/A") == m_entries.end()); // has been erased |
| 530 | } |
| 531 | |
| 532 | BOOST_AUTO_TEST_CASE(AfterHubConnectDisconnect) |
| 533 | { |
| 534 | BOOST_REQUIRE(addIdentity("/test/A")); |
| 535 | BOOST_REQUIRE(addIdentity("/test/B")); |
| 536 | BOOST_REQUIRE(addIdentity("/test/C")); |
| 537 | BOOST_REQUIRE(insertEntryToRib("/test/A/app")); |
| 538 | BOOST_REQUIRE(insertEntryToRib("/test/B/app")); |
| 539 | |
| 540 | // recorder the prefixes that will be propagated in order |
| 541 | std::vector<Name> propagatedPrefixes; |
| 542 | |
| 543 | BOOST_CHECK(m_requests.empty()); // no request because there is no connectivity to the hub now |
| 544 | BOOST_REQUIRE_EQUAL(m_entries.size(), 2); // valid entries will be kept |
| 545 | |
| 546 | connectToHub(); // 2 cached entries will be processed |
| 547 | for (auto&& entry : m_entries) { |
| 548 | propagatedPrefixes.push_back(entry.first); |
| 549 | } |
| 550 | |
| 551 | BOOST_REQUIRE(insertEntryToRib("/test/C/app")); // will be processed directly |
| 552 | propagatedPrefixes.push_back("/test/C"); |
| 553 | BOOST_REQUIRE_EQUAL(m_entries.size(), 3); |
| 554 | |
| 555 | BOOST_REQUIRE(m_entries["/test/A"].isPropagating()); |
| 556 | BOOST_REQUIRE(m_entries["/test/B"].isPropagating()); |
| 557 | BOOST_REQUIRE(m_entries["/test/C"].isPropagating()); |
| 558 | |
| 559 | disconnectFromHub(); // all 3 entries are initialized |
| 560 | BOOST_CHECK(m_entries["/test/A"].isNew()); |
| 561 | BOOST_CHECK(m_entries["/test/B"].isNew()); |
| 562 | BOOST_CHECK(m_entries["/test/C"].isNew()); |
| 563 | |
| 564 | connectToHub(); // all 3 entries will be processed |
| 565 | for (auto&& entry : m_entries) { |
| 566 | propagatedPrefixes.push_back(entry.first); |
| 567 | } |
| 568 | |
| 569 | BOOST_REQUIRE_EQUAL(m_requests.size(), 6); |
| 570 | BOOST_REQUIRE_EQUAL(propagatedPrefixes.size(), 6); |
| 571 | BOOST_CHECK_EQUAL(checkRequest(0, "register", propagatedPrefixes[0]), CheckRequestResult::OK); |
| 572 | BOOST_CHECK_EQUAL(checkRequest(1, "register", propagatedPrefixes[1]), CheckRequestResult::OK); |
| 573 | BOOST_CHECK_EQUAL(checkRequest(2, "register", propagatedPrefixes[2]), CheckRequestResult::OK); |
| 574 | BOOST_CHECK_EQUAL(checkRequest(3, "register", propagatedPrefixes[3]), CheckRequestResult::OK); |
| 575 | BOOST_CHECK_EQUAL(checkRequest(4, "register", propagatedPrefixes[4]), CheckRequestResult::OK); |
| 576 | BOOST_CHECK_EQUAL(checkRequest(5, "register", propagatedPrefixes[5]), CheckRequestResult::OK); |
| 577 | } |
| 578 | |
| 579 | BOOST_AUTO_TEST_CASE(AfterPropagateSucceed) |
| 580 | { |
| 581 | bool wasRefreshEventTriggered = false; |
| 582 | auto testAfterPropagateSucceed = [&] (const Name& ribEntryPrefix) { |
| 583 | m_requests.clear(); |
| 584 | wasRefreshEventTriggered = false; |
| 585 | |
| 586 | auto propagateParameters = m_propagator.getPrefixPropagationParameters(ribEntryPrefix); |
| 587 | m_propagator.afterPropagateSucceed(propagateParameters.parameters, propagateParameters.options, |
| 588 | [&]{ wasRefreshEventTriggered = true; }); |
| 589 | advanceClocks(time::milliseconds(1)); |
| 590 | }; |
| 591 | |
| 592 | BOOST_REQUIRE(addIdentity("/test/A")); |
| 593 | m_propagator.m_refreshInterval = time::seconds(0); // event will be executed at once |
| 594 | m_entries["/test/A"].startPropagation(); // set to be in PROPAGATING state |
| 595 | |
| 596 | testAfterPropagateSucceed("/test/A/app"); // will trigger refresh event |
| 597 | BOOST_CHECK(wasRefreshEventTriggered); |
| 598 | BOOST_CHECK(m_requests.empty()); |
| 599 | |
| 600 | m_entries.erase(m_entries.find("/test/A")); // set to be in RELEASED state |
| 601 | testAfterPropagateSucceed("/test/A/app"); // will call startRevocation |
| 602 | BOOST_CHECK(!wasRefreshEventTriggered); |
| 603 | BOOST_REQUIRE_EQUAL(m_requests.size(), 1); |
| 604 | BOOST_CHECK_EQUAL(checkRequest(0, "unregister", "/test/A"), CheckRequestResult::OK); |
| 605 | } |
| 606 | |
| 607 | BOOST_AUTO_TEST_CASE(AfterPropagateFail) |
| 608 | { |
| 609 | bool wasRetryEventTriggered = false; |
| 610 | auto testAfterPropagateFail = [&] (const Name& ribEntryPrefix) { |
| 611 | m_requests.clear(); |
| 612 | wasRetryEventTriggered = false; |
| 613 | |
| 614 | auto propagateParameters = m_propagator.getPrefixPropagationParameters(ribEntryPrefix); |
| 615 | m_propagator.afterPropagateFail(400, "test", propagateParameters.parameters, propagateParameters.options, |
| 616 | time::seconds(0), [&]{ wasRetryEventTriggered = true; }); |
| 617 | advanceClocks(time::milliseconds(1)); |
| 618 | }; |
| 619 | |
| 620 | BOOST_REQUIRE(addIdentity("/test/A")); |
| 621 | m_entries["/test/A"].startPropagation(); // set to be in PROPAGATING state |
| 622 | |
| 623 | testAfterPropagateFail("/test/A/app"); // will trigger retry event |
| 624 | BOOST_CHECK(wasRetryEventTriggered); |
| 625 | BOOST_CHECK(m_requests.empty()); |
| 626 | |
| 627 | m_entries.erase(m_entries.find("/test/A")); // set to be in RELEASED state |
| 628 | testAfterPropagateFail("/test/A/app"); // will do nothing |
| 629 | BOOST_CHECK(!wasRetryEventTriggered); |
| 630 | BOOST_CHECK(m_requests.empty()); |
| 631 | } |
| 632 | |
| 633 | BOOST_AUTO_TEST_CASE(AfterRevokeSucceed) |
| 634 | { |
| 635 | auto testAfterRevokeSucceed = [&] (const Name& ribEntryPrefix) { |
| 636 | m_requests.clear(); |
| 637 | auto propagateParameters = m_propagator.getPrefixPropagationParameters(ribEntryPrefix); |
| 638 | m_propagator.afterRevokeSucceed(propagateParameters.parameters, |
| 639 | propagateParameters.options, |
| 640 | time::seconds(0)); |
| 641 | advanceClocks(time::milliseconds(1)); |
| 642 | }; |
| 643 | |
| 644 | BOOST_REQUIRE(addIdentity("/test/A")); |
| 645 | |
| 646 | testAfterRevokeSucceed("/test/A/app"); // in RELEASED state |
| 647 | BOOST_CHECK(m_requests.empty()); |
| 648 | |
| 649 | m_entries["/test/A"].fail(nullptr); // in PROPAGATE_FAIL state |
| 650 | testAfterRevokeSucceed("/test/A/app"); |
| 651 | BOOST_CHECK(m_requests.empty()); |
| 652 | |
| 653 | m_entries["/test/A"].succeed(nullptr); // in PROPAGATED state |
| 654 | testAfterRevokeSucceed("/test/A/app"); |
| 655 | BOOST_REQUIRE_EQUAL(m_requests.size(), 1); |
| 656 | BOOST_CHECK_EQUAL(checkRequest(0, "register", "/test/A"), CheckRequestResult::OK); |
| 657 | |
| 658 | m_entries["/test/A"].startPropagation(); // in PROPAGATING state |
| 659 | testAfterRevokeSucceed("/test/A/app"); |
| 660 | BOOST_REQUIRE_EQUAL(m_requests.size(), 1); |
| 661 | BOOST_CHECK_EQUAL(checkRequest(0, "register", "/test/A"), CheckRequestResult::OK); |
| 662 | } |
| 663 | |
| 664 | BOOST_AUTO_TEST_SUITE_END() // PropagatedEntryStateChanges |
| 665 | |
| 666 | BOOST_AUTO_TEST_SUITE_END() // TestAutoPrefixPropagator |
| 667 | BOOST_AUTO_TEST_SUITE_END() // Rib |
| 668 | |
| 669 | } // namespace tests |
| 670 | } // namespace rib |
| 671 | } // namespace nfd |