blob: 42301144976cadaa6a4ee151022a1e0ea022a204 [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 Pesaventoc2442be2025-01-11 17:25:40 -05003 * Copyright (c) 2014-2025, 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
Davide Pesavento5d642632023-10-03 00:36:08 -040036#include <boost/asio/defer.hpp>
37
Vince Lehman76c751c2014-11-18 17:36:38 -060038#include <ndn-cxx/util/dummy-client-face.hpp>
39
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040040namespace nfd::tests {
Vince Lehman4387e782014-06-19 16:57:45 -050041
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040042using rib::FibUpdate;
43using rib::FibUpdater;
Vince Lehman4387e782014-06-19 16:57:45 -050044
Junxiao Shidf1dc652019-08-30 19:03:19 +000045class MockFibUpdater : public FibUpdater
46{
47public:
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
59private:
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 Pesavento5d642632023-10-03 00:36:08 -040085 boost::asio::defer(getGlobalIoService(), [=] {
Junxiao Shidf1dc652019-08-30 19:03:19 +000086 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
96public:
97 FibUpdateList updates;
98 bool mockSuccess = true;
99};
100
Davide Pesavento14e71f02019-03-28 17:35:25 -0400101class FibUpdatesFixture : public GlobalIoFixture, public KeyChainFixture
Vince Lehman4387e782014-06-19 16:57:45 -0500102{
103public:
104 void
Davide Pesavento22db5392017-04-14 00:56:43 -0400105 insertRoute(const Name& name, uint64_t faceId,
Davide Pesavento1b077f62019-02-19 19:19:44 -0500106 std::underlying_type_t<ndn::nfd::RouteOrigin> origin,
107 uint64_t cost,
108 std::underlying_type_t<ndn::nfd::RouteFlags> flags)
Vince Lehman4387e782014-06-19 16:57:45 -0500109 {
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400110 auto route = createRoute(faceId, origin, cost, flags);
Davide Pesaventoc2442be2025-01-11 17:25:40 -0500111 rib.beginApplyUpdate({rib::RibUpdate::REGISTER, name, route}, nullptr, nullptr);
Junxiao Shidf1dc652019-08-30 19:03:19 +0000112 pollIo();
Vince Lehman4387e782014-06-19 16:57:45 -0500113 }
114
115 void
Davide Pesavento22db5392017-04-14 00:56:43 -0400116 eraseRoute(const Name& name, uint64_t faceId,
Davide Pesavento1b077f62019-02-19 19:19:44 -0500117 std::underlying_type_t<ndn::nfd::RouteOrigin> origin)
Vince Lehman4387e782014-06-19 16:57:45 -0500118 {
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400119 auto route = createRoute(faceId, origin, 0, 0);
Davide Pesaventoc2442be2025-01-11 17:25:40 -0500120 rib.beginApplyUpdate({rib::RibUpdate::UNREGISTER, name, route}, nullptr, nullptr);
Junxiao Shidf1dc652019-08-30 19:03:19 +0000121 pollIo();
Vince Lehman76c751c2014-11-18 17:36:38 -0600122 }
123
124 void
125 destroyFace(uint64_t faceId)
126 {
Vince Lehman76c751c2014-11-18 17:36:38 -0600127 rib.beginRemoveFace(faceId);
Junxiao Shidf1dc652019-08-30 19:03:19 +0000128 pollIo();
Vince Lehman76c751c2014-11-18 17:36:38 -0600129 }
130
Vince Lehman76c751c2014-11-18 17:36:38 -0600131 const FibUpdater::FibUpdateList&
Junxiao Shidf1dc652019-08-30 19:03:19 +0000132 getFibUpdates() const
Vince Lehman76c751c2014-11-18 17:36:38 -0600133 {
Junxiao Shidf1dc652019-08-30 19:03:19 +0000134 return fibUpdater.updates;
Vince Lehman4387e782014-06-19 16:57:45 -0500135 }
136
Junxiao Shidf1dc652019-08-30 19:03:19 +0000137 const FibUpdater::FibUpdateList&
Vince Lehman4387e782014-06-19 16:57:45 -0500138 getSortedFibUpdates()
139 {
Junxiao Shidf1dc652019-08-30 19:03:19 +0000140 fibUpdater.sortUpdates();
141 return fibUpdater.updates;
Vince Lehman4387e782014-06-19 16:57:45 -0500142 }
143
Vince Lehman76c751c2014-11-18 17:36:38 -0600144 void
145 clearFibUpdates()
146 {
Junxiao Shidf1dc652019-08-30 19:03:19 +0000147 fibUpdater.updates.clear();
Junxiao Shi52009042018-09-10 12:33:56 +0000148 }
149
Vince Lehman4387e782014-06-19 16:57:45 -0500150public:
Davide Pesaventoc2442be2025-01-11 17:25:40 -0500151 ndn::DummyClientFace face{g_io, m_keyChain};
152 ndn::nfd::Controller controller{face, m_keyChain};
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400153 rib::Rib rib;
Davide Pesaventoc2442be2025-01-11 17:25:40 -0500154 MockFibUpdater fibUpdater{rib, controller};
Vince Lehman4387e782014-06-19 16:57:45 -0500155};
156
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400157} // namespace nfd::tests
Davide Pesavento22db5392017-04-14 00:56:43 -0400158
Davide Pesavento1b077f62019-02-19 19:19:44 -0500159#endif // NFD_TESTS_DAEMON_RIB_FIB_UPDATES_COMMON_HPP