blob: 33b549d29bf5068cd03bfcbdaca4b87c410e8747 [file] [log] [blame]
Vince Lehman4387e782014-06-19 16:57:45 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi52009042018-09-10 12:33:56 +00002/*
Davide Pesaventoae430302023-05-11 01:42:46 -04003 * Copyright (c) 2014-2023, Regents of the University of California,
Alexander Afanasyev7c10b3b2015-01-20 12:24:27 -08004 * 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 Lehman4387e782014-06-19 16:57:45 -050010 *
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 Pesavento1b077f62019-02-19 19:19:44 -050026#ifndef NFD_TESTS_DAEMON_RIB_FIB_UPDATES_COMMON_HPP
27#define NFD_TESTS_DAEMON_RIB_FIB_UPDATES_COMMON_HPP
Davide Pesavento22db5392017-04-14 00:56:43 -040028
Vince Lehman76c751c2014-11-18 17:36:38 -060029#include "rib/fib-updater.hpp"
Junxiao Shidf1dc652019-08-30 19:03:19 +000030#include "common/global.hpp"
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040031
Davide Pesavento1d12d2f2019-03-22 12:44:14 -040032#include "tests/key-chain-fixture.hpp"
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040033#include "tests/daemon/global-io-fixture.hpp"
Davide Pesavento1b077f62019-02-19 19:19:44 -050034#include "tests/daemon/rib/create-route.hpp"
Vince Lehman76c751c2014-11-18 17:36:38 -060035
36#include <ndn-cxx/util/dummy-client-face.hpp>
37
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040038namespace nfd::tests {
Vince Lehman4387e782014-06-19 16:57:45 -050039
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040040using rib::FibUpdate;
41using rib::FibUpdater;
Vince Lehman4387e782014-06-19 16:57:45 -050042
Junxiao Shidf1dc652019-08-30 19:03:19 +000043class MockFibUpdater : public FibUpdater
44{
45public:
46 using FibUpdater::FibUpdater;
47
48 void
49 sortUpdates()
50 {
51 updates.sort([] (const auto& lhs, const auto& rhs) {
52 return std::tie(lhs.name, lhs.faceId, lhs.cost, lhs.action) <
53 std::tie(rhs.name, rhs.faceId, rhs.cost, rhs.action);
54 });
55 }
56
57private:
58 void
59 sendAddNextHopUpdate(const FibUpdate& update,
60 const FibUpdateSuccessCallback& onSuccess,
61 const FibUpdateFailureCallback& onFailure,
62 uint32_t nTimeouts) override
63 {
64 mockUpdate(update, onSuccess, onFailure, nTimeouts);
65 }
66
67 void
68 sendRemoveNextHopUpdate(const FibUpdate& update,
69 const FibUpdateSuccessCallback& onSuccess,
70 const FibUpdateFailureCallback& onFailure,
71 uint32_t nTimeouts) override
72 {
73 mockUpdate(update, onSuccess, onFailure, nTimeouts);
74 }
75
76 void
77 mockUpdate(const FibUpdate& update,
78 const FibUpdateSuccessCallback& onSuccess,
79 const FibUpdateFailureCallback& onFailure,
80 uint32_t nTimeouts)
81 {
82 updates.push_back(update);
83 getGlobalIoService().post([=] {
84 if (mockSuccess) {
85 onUpdateSuccess(update, onSuccess, onFailure);
86 }
87 else {
88 ndn::mgmt::ControlResponse resp(504, "mocked failure");
89 onUpdateError(update, onSuccess, onFailure, resp, nTimeouts);
90 }
91 });
92 }
93
94public:
95 FibUpdateList updates;
96 bool mockSuccess = true;
97};
98
Davide Pesavento14e71f02019-03-28 17:35:25 -040099class FibUpdatesFixture : public GlobalIoFixture, public KeyChainFixture
Vince Lehman4387e782014-06-19 16:57:45 -0500100{
101public:
Vince Lehman76c751c2014-11-18 17:36:38 -0600102 FibUpdatesFixture()
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400103 : face(g_io, m_keyChain)
Junxiao Shi221b6fe2016-07-14 18:21:56 +0000104 , controller(face, m_keyChain)
Vince Lehman76c751c2014-11-18 17:36:38 -0600105 , fibUpdater(rib, controller)
106 {
107 }
108
Vince Lehman4387e782014-06-19 16:57:45 -0500109 void
Davide Pesavento22db5392017-04-14 00:56:43 -0400110 insertRoute(const Name& name, uint64_t faceId,
Davide Pesavento1b077f62019-02-19 19:19:44 -0500111 std::underlying_type_t<ndn::nfd::RouteOrigin> origin,
112 uint64_t cost,
113 std::underlying_type_t<ndn::nfd::RouteFlags> flags)
Vince Lehman4387e782014-06-19 16:57:45 -0500114 {
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400115 auto route = createRoute(faceId, origin, cost, flags);
Vince Lehman4387e782014-06-19 16:57:45 -0500116
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400117 rib::RibUpdate update;
118 update.setAction(rib::RibUpdate::REGISTER)
Vince Lehman76c751c2014-11-18 17:36:38 -0600119 .setName(name)
120 .setRoute(route);
121
Junxiao Shi52009042018-09-10 12:33:56 +0000122 rib.beginApplyUpdate(update, nullptr, nullptr);
Junxiao Shidf1dc652019-08-30 19:03:19 +0000123 pollIo();
Vince Lehman4387e782014-06-19 16:57:45 -0500124 }
125
126 void
Davide Pesavento22db5392017-04-14 00:56:43 -0400127 eraseRoute(const Name& name, uint64_t faceId,
Davide Pesavento1b077f62019-02-19 19:19:44 -0500128 std::underlying_type_t<ndn::nfd::RouteOrigin> origin)
Vince Lehman4387e782014-06-19 16:57:45 -0500129 {
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400130 auto route = createRoute(faceId, origin, 0, 0);
Vince Lehman4387e782014-06-19 16:57:45 -0500131
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400132 rib::RibUpdate update;
133 update.setAction(rib::RibUpdate::UNREGISTER)
Vince Lehman76c751c2014-11-18 17:36:38 -0600134 .setName(name)
135 .setRoute(route);
136
Junxiao Shi52009042018-09-10 12:33:56 +0000137 rib.beginApplyUpdate(update, nullptr, nullptr);
Junxiao Shidf1dc652019-08-30 19:03:19 +0000138 pollIo();
Vince Lehman76c751c2014-11-18 17:36:38 -0600139 }
140
141 void
142 destroyFace(uint64_t faceId)
143 {
Vince Lehman76c751c2014-11-18 17:36:38 -0600144 rib.beginRemoveFace(faceId);
Junxiao Shidf1dc652019-08-30 19:03:19 +0000145 pollIo();
Vince Lehman76c751c2014-11-18 17:36:38 -0600146 }
147
Vince Lehman76c751c2014-11-18 17:36:38 -0600148 const FibUpdater::FibUpdateList&
Junxiao Shidf1dc652019-08-30 19:03:19 +0000149 getFibUpdates() const
Vince Lehman76c751c2014-11-18 17:36:38 -0600150 {
Junxiao Shidf1dc652019-08-30 19:03:19 +0000151 return fibUpdater.updates;
Vince Lehman4387e782014-06-19 16:57:45 -0500152 }
153
Junxiao Shidf1dc652019-08-30 19:03:19 +0000154 const FibUpdater::FibUpdateList&
Vince Lehman4387e782014-06-19 16:57:45 -0500155 getSortedFibUpdates()
156 {
Junxiao Shidf1dc652019-08-30 19:03:19 +0000157 fibUpdater.sortUpdates();
158 return fibUpdater.updates;
Vince Lehman4387e782014-06-19 16:57:45 -0500159 }
160
Vince Lehman76c751c2014-11-18 17:36:38 -0600161 void
162 clearFibUpdates()
163 {
Junxiao Shidf1dc652019-08-30 19:03:19 +0000164 fibUpdater.updates.clear();
Junxiao Shi52009042018-09-10 12:33:56 +0000165 }
166
Vince Lehman4387e782014-06-19 16:57:45 -0500167public:
Davide Pesaventoae430302023-05-11 01:42:46 -0400168 ndn::DummyClientFace face;
Vince Lehman76c751c2014-11-18 17:36:38 -0600169 ndn::nfd::Controller controller;
Vince Lehman76c751c2014-11-18 17:36:38 -0600170
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400171 rib::Rib rib;
Junxiao Shidf1dc652019-08-30 19:03:19 +0000172 MockFibUpdater fibUpdater;
Vince Lehman4387e782014-06-19 16:57:45 -0500173};
174
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400175} // namespace nfd::tests
Davide Pesavento22db5392017-04-14 00:56:43 -0400176
Davide Pesavento1b077f62019-02-19 19:19:44 -0500177#endif // NFD_TESTS_DAEMON_RIB_FIB_UPDATES_COMMON_HPP