blob: 1930d142d0f6f8e3667b00b87235015faca15704 [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 Pesavento1b077f62019-02-19 19:19:44 -05003 * Copyright (c) 2014-2019, 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
Vince Lehman4387e782014-06-19 16:57:45 -050038namespace nfd {
39namespace rib {
40namespace tests {
41
Davide Pesavento14e71f02019-03-28 17:35:25 -040042using namespace nfd::tests;
Vince Lehman4387e782014-06-19 16:57:45 -050043
Junxiao Shidf1dc652019-08-30 19:03:19 +000044class MockFibUpdater : public FibUpdater
45{
46public:
47 using FibUpdater::FibUpdater;
48
49 void
50 sortUpdates()
51 {
52 updates.sort([] (const auto& lhs, const auto& rhs) {
53 return std::tie(lhs.name, lhs.faceId, lhs.cost, lhs.action) <
54 std::tie(rhs.name, rhs.faceId, rhs.cost, rhs.action);
55 });
56 }
57
58private:
59 void
60 sendAddNextHopUpdate(const FibUpdate& update,
61 const FibUpdateSuccessCallback& onSuccess,
62 const FibUpdateFailureCallback& onFailure,
63 uint32_t nTimeouts) override
64 {
65 mockUpdate(update, onSuccess, onFailure, nTimeouts);
66 }
67
68 void
69 sendRemoveNextHopUpdate(const FibUpdate& update,
70 const FibUpdateSuccessCallback& onSuccess,
71 const FibUpdateFailureCallback& onFailure,
72 uint32_t nTimeouts) override
73 {
74 mockUpdate(update, onSuccess, onFailure, nTimeouts);
75 }
76
77 void
78 mockUpdate(const FibUpdate& update,
79 const FibUpdateSuccessCallback& onSuccess,
80 const FibUpdateFailureCallback& onFailure,
81 uint32_t nTimeouts)
82 {
83 updates.push_back(update);
84 getGlobalIoService().post([=] {
85 if (mockSuccess) {
86 onUpdateSuccess(update, onSuccess, onFailure);
87 }
88 else {
89 ndn::mgmt::ControlResponse resp(504, "mocked failure");
90 onUpdateError(update, onSuccess, onFailure, resp, nTimeouts);
91 }
92 });
93 }
94
95public:
96 FibUpdateList updates;
97 bool mockSuccess = true;
98};
99
Davide Pesavento14e71f02019-03-28 17:35:25 -0400100class FibUpdatesFixture : public GlobalIoFixture, public KeyChainFixture
Vince Lehman4387e782014-06-19 16:57:45 -0500101{
102public:
Vince Lehman76c751c2014-11-18 17:36:38 -0600103 FibUpdatesFixture()
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400104 : face(g_io, m_keyChain)
Junxiao Shi221b6fe2016-07-14 18:21:56 +0000105 , controller(face, m_keyChain)
Vince Lehman76c751c2014-11-18 17:36:38 -0600106 , fibUpdater(rib, controller)
107 {
108 }
109
Vince Lehman4387e782014-06-19 16:57:45 -0500110 void
Davide Pesavento22db5392017-04-14 00:56:43 -0400111 insertRoute(const Name& name, uint64_t faceId,
Davide Pesavento1b077f62019-02-19 19:19:44 -0500112 std::underlying_type_t<ndn::nfd::RouteOrigin> origin,
113 uint64_t cost,
114 std::underlying_type_t<ndn::nfd::RouteFlags> flags)
Vince Lehman4387e782014-06-19 16:57:45 -0500115 {
Vince Lehman76c751c2014-11-18 17:36:38 -0600116 Route route = createRoute(faceId, origin, cost, flags);
Vince Lehman4387e782014-06-19 16:57:45 -0500117
Vince Lehman76c751c2014-11-18 17:36:38 -0600118 RibUpdate update;
119 update.setAction(RibUpdate::REGISTER)
120 .setName(name)
121 .setRoute(route);
122
Junxiao Shi52009042018-09-10 12:33:56 +0000123 rib.beginApplyUpdate(update, nullptr, nullptr);
Junxiao Shidf1dc652019-08-30 19:03:19 +0000124 pollIo();
Vince Lehman4387e782014-06-19 16:57:45 -0500125 }
126
127 void
Davide Pesavento22db5392017-04-14 00:56:43 -0400128 eraseRoute(const Name& name, uint64_t faceId,
Davide Pesavento1b077f62019-02-19 19:19:44 -0500129 std::underlying_type_t<ndn::nfd::RouteOrigin> origin)
Vince Lehman4387e782014-06-19 16:57:45 -0500130 {
Vince Lehman76c751c2014-11-18 17:36:38 -0600131 Route route = createRoute(faceId, origin, 0, 0);
Vince Lehman4387e782014-06-19 16:57:45 -0500132
Vince Lehman76c751c2014-11-18 17:36:38 -0600133 RibUpdate update;
134 update.setAction(RibUpdate::UNREGISTER)
135 .setName(name)
136 .setRoute(route);
137
Junxiao Shi52009042018-09-10 12:33:56 +0000138 rib.beginApplyUpdate(update, nullptr, nullptr);
Junxiao Shidf1dc652019-08-30 19:03:19 +0000139 pollIo();
Vince Lehman76c751c2014-11-18 17:36:38 -0600140 }
141
142 void
143 destroyFace(uint64_t faceId)
144 {
Vince Lehman76c751c2014-11-18 17:36:38 -0600145 rib.beginRemoveFace(faceId);
Junxiao Shidf1dc652019-08-30 19:03:19 +0000146 pollIo();
Vince Lehman76c751c2014-11-18 17:36:38 -0600147 }
148
Vince Lehman76c751c2014-11-18 17:36:38 -0600149 const FibUpdater::FibUpdateList&
Junxiao Shidf1dc652019-08-30 19:03:19 +0000150 getFibUpdates() const
Vince Lehman76c751c2014-11-18 17:36:38 -0600151 {
Junxiao Shidf1dc652019-08-30 19:03:19 +0000152 return fibUpdater.updates;
Vince Lehman4387e782014-06-19 16:57:45 -0500153 }
154
Junxiao Shidf1dc652019-08-30 19:03:19 +0000155 const FibUpdater::FibUpdateList&
Vince Lehman4387e782014-06-19 16:57:45 -0500156 getSortedFibUpdates()
157 {
Junxiao Shidf1dc652019-08-30 19:03:19 +0000158 fibUpdater.sortUpdates();
159 return fibUpdater.updates;
Vince Lehman4387e782014-06-19 16:57:45 -0500160 }
161
Vince Lehman76c751c2014-11-18 17:36:38 -0600162 void
163 clearFibUpdates()
164 {
Junxiao Shidf1dc652019-08-30 19:03:19 +0000165 fibUpdater.updates.clear();
Junxiao Shi52009042018-09-10 12:33:56 +0000166 }
167
Vince Lehman4387e782014-06-19 16:57:45 -0500168public:
Junxiao Shi221b6fe2016-07-14 18:21:56 +0000169 ndn::util::DummyClientFace face;
Vince Lehman76c751c2014-11-18 17:36:38 -0600170 ndn::nfd::Controller controller;
Vince Lehman76c751c2014-11-18 17:36:38 -0600171
Davide Pesavento97210d52016-10-14 15:45:48 +0200172 Rib rib;
Junxiao Shidf1dc652019-08-30 19:03:19 +0000173 MockFibUpdater fibUpdater;
Vince Lehman4387e782014-06-19 16:57:45 -0500174};
175
176} // namespace tests
177} // namespace rib
178} // namespace nfd
Davide Pesavento22db5392017-04-14 00:56:43 -0400179
Davide Pesavento1b077f62019-02-19 19:19:44 -0500180#endif // NFD_TESTS_DAEMON_RIB_FIB_UPDATES_COMMON_HPP