Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | ae43030 | 2023-05-11 01:42:46 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2023, 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 | 4387e78 | 2014-06-19 16:57:45 -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 | |
Davide Pesavento | 1b077f6 | 2019-02-19 19:19:44 -0500 | [diff] [blame] | 26 | #ifndef NFD_TESTS_DAEMON_RIB_FIB_UPDATES_COMMON_HPP |
| 27 | #define NFD_TESTS_DAEMON_RIB_FIB_UPDATES_COMMON_HPP |
Davide Pesavento | 22db539 | 2017-04-14 00:56:43 -0400 | [diff] [blame] | 28 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 29 | #include "rib/fib-updater.hpp" |
Junxiao Shi | df1dc65 | 2019-08-30 19:03:19 +0000 | [diff] [blame] | 30 | #include "common/global.hpp" |
Davide Pesavento | e1bdc08 | 2018-10-11 21:20:23 -0400 | [diff] [blame] | 31 | |
Davide Pesavento | 1d12d2f | 2019-03-22 12:44:14 -0400 | [diff] [blame] | 32 | #include "tests/key-chain-fixture.hpp" |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 33 | #include "tests/daemon/global-io-fixture.hpp" |
Davide Pesavento | 1b077f6 | 2019-02-19 19:19:44 -0500 | [diff] [blame] | 34 | #include "tests/daemon/rib/create-route.hpp" |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 35 | |
Davide Pesavento | 5d64263 | 2023-10-03 00:36:08 -0400 | [diff] [blame] | 36 | #include <boost/asio/defer.hpp> |
| 37 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 38 | #include <ndn-cxx/util/dummy-client-face.hpp> |
| 39 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 40 | namespace nfd::tests { |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 41 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 42 | using rib::FibUpdate; |
| 43 | using rib::FibUpdater; |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 44 | |
Junxiao Shi | df1dc65 | 2019-08-30 19:03:19 +0000 | [diff] [blame] | 45 | class MockFibUpdater : public FibUpdater |
| 46 | { |
| 47 | public: |
| 48 | using FibUpdater::FibUpdater; |
| 49 | |
| 50 | void |
| 51 | sortUpdates() |
| 52 | { |
| 53 | updates.sort([] (const auto& lhs, const auto& rhs) { |
| 54 | return std::tie(lhs.name, lhs.faceId, lhs.cost, lhs.action) < |
| 55 | std::tie(rhs.name, rhs.faceId, rhs.cost, rhs.action); |
| 56 | }); |
| 57 | } |
| 58 | |
| 59 | private: |
| 60 | void |
| 61 | sendAddNextHopUpdate(const FibUpdate& update, |
| 62 | const FibUpdateSuccessCallback& onSuccess, |
| 63 | const FibUpdateFailureCallback& onFailure, |
| 64 | uint32_t nTimeouts) override |
| 65 | { |
| 66 | mockUpdate(update, onSuccess, onFailure, nTimeouts); |
| 67 | } |
| 68 | |
| 69 | void |
| 70 | sendRemoveNextHopUpdate(const FibUpdate& update, |
| 71 | const FibUpdateSuccessCallback& onSuccess, |
| 72 | const FibUpdateFailureCallback& onFailure, |
| 73 | uint32_t nTimeouts) override |
| 74 | { |
| 75 | mockUpdate(update, onSuccess, onFailure, nTimeouts); |
| 76 | } |
| 77 | |
| 78 | void |
| 79 | mockUpdate(const FibUpdate& update, |
| 80 | const FibUpdateSuccessCallback& onSuccess, |
| 81 | const FibUpdateFailureCallback& onFailure, |
| 82 | uint32_t nTimeouts) |
| 83 | { |
| 84 | updates.push_back(update); |
Davide Pesavento | 5d64263 | 2023-10-03 00:36:08 -0400 | [diff] [blame] | 85 | boost::asio::defer(getGlobalIoService(), [=] { |
Junxiao Shi | df1dc65 | 2019-08-30 19:03:19 +0000 | [diff] [blame] | 86 | if (mockSuccess) { |
| 87 | onUpdateSuccess(update, onSuccess, onFailure); |
| 88 | } |
| 89 | else { |
| 90 | ndn::mgmt::ControlResponse resp(504, "mocked failure"); |
| 91 | onUpdateError(update, onSuccess, onFailure, resp, nTimeouts); |
| 92 | } |
| 93 | }); |
| 94 | } |
| 95 | |
| 96 | public: |
| 97 | FibUpdateList updates; |
| 98 | bool mockSuccess = true; |
| 99 | }; |
| 100 | |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 101 | class FibUpdatesFixture : public GlobalIoFixture, public KeyChainFixture |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 102 | { |
| 103 | public: |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 104 | FibUpdatesFixture() |
Davide Pesavento | e1bdc08 | 2018-10-11 21:20:23 -0400 | [diff] [blame] | 105 | : face(g_io, m_keyChain) |
Junxiao Shi | 221b6fe | 2016-07-14 18:21:56 +0000 | [diff] [blame] | 106 | , controller(face, m_keyChain) |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 107 | , fibUpdater(rib, controller) |
| 108 | { |
| 109 | } |
| 110 | |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 111 | void |
Davide Pesavento | 22db539 | 2017-04-14 00:56:43 -0400 | [diff] [blame] | 112 | insertRoute(const Name& name, uint64_t faceId, |
Davide Pesavento | 1b077f6 | 2019-02-19 19:19:44 -0500 | [diff] [blame] | 113 | std::underlying_type_t<ndn::nfd::RouteOrigin> origin, |
| 114 | uint64_t cost, |
| 115 | std::underlying_type_t<ndn::nfd::RouteFlags> flags) |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 116 | { |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 117 | auto route = createRoute(faceId, origin, cost, flags); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 118 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 119 | rib::RibUpdate update; |
| 120 | update.setAction(rib::RibUpdate::REGISTER) |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 121 | .setName(name) |
| 122 | .setRoute(route); |
| 123 | |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 124 | rib.beginApplyUpdate(update, nullptr, nullptr); |
Junxiao Shi | df1dc65 | 2019-08-30 19:03:19 +0000 | [diff] [blame] | 125 | pollIo(); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | void |
Davide Pesavento | 22db539 | 2017-04-14 00:56:43 -0400 | [diff] [blame] | 129 | eraseRoute(const Name& name, uint64_t faceId, |
Davide Pesavento | 1b077f6 | 2019-02-19 19:19:44 -0500 | [diff] [blame] | 130 | std::underlying_type_t<ndn::nfd::RouteOrigin> origin) |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 131 | { |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 132 | auto route = createRoute(faceId, origin, 0, 0); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 133 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 134 | rib::RibUpdate update; |
| 135 | update.setAction(rib::RibUpdate::UNREGISTER) |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 136 | .setName(name) |
| 137 | .setRoute(route); |
| 138 | |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 139 | rib.beginApplyUpdate(update, nullptr, nullptr); |
Junxiao Shi | df1dc65 | 2019-08-30 19:03:19 +0000 | [diff] [blame] | 140 | pollIo(); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | void |
| 144 | destroyFace(uint64_t faceId) |
| 145 | { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 146 | rib.beginRemoveFace(faceId); |
Junxiao Shi | df1dc65 | 2019-08-30 19:03:19 +0000 | [diff] [blame] | 147 | pollIo(); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 148 | } |
| 149 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 150 | const FibUpdater::FibUpdateList& |
Junxiao Shi | df1dc65 | 2019-08-30 19:03:19 +0000 | [diff] [blame] | 151 | getFibUpdates() const |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 152 | { |
Junxiao Shi | df1dc65 | 2019-08-30 19:03:19 +0000 | [diff] [blame] | 153 | return fibUpdater.updates; |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 154 | } |
| 155 | |
Junxiao Shi | df1dc65 | 2019-08-30 19:03:19 +0000 | [diff] [blame] | 156 | const FibUpdater::FibUpdateList& |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 157 | getSortedFibUpdates() |
| 158 | { |
Junxiao Shi | df1dc65 | 2019-08-30 19:03:19 +0000 | [diff] [blame] | 159 | fibUpdater.sortUpdates(); |
| 160 | return fibUpdater.updates; |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 161 | } |
| 162 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 163 | void |
| 164 | clearFibUpdates() |
| 165 | { |
Junxiao Shi | df1dc65 | 2019-08-30 19:03:19 +0000 | [diff] [blame] | 166 | fibUpdater.updates.clear(); |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 169 | public: |
Davide Pesavento | ae43030 | 2023-05-11 01:42:46 -0400 | [diff] [blame] | 170 | ndn::DummyClientFace face; |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 171 | ndn::nfd::Controller controller; |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 172 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 173 | rib::Rib rib; |
Junxiao Shi | df1dc65 | 2019-08-30 19:03:19 +0000 | [diff] [blame] | 174 | MockFibUpdater fibUpdater; |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 175 | }; |
| 176 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 177 | } // namespace nfd::tests |
Davide Pesavento | 22db539 | 2017-04-14 00:56:43 -0400 | [diff] [blame] | 178 | |
Davide Pesavento | 1b077f6 | 2019-02-19 19:19:44 -0500 | [diff] [blame] | 179 | #endif // NFD_TESTS_DAEMON_RIB_FIB_UPDATES_COMMON_HPP |