Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Teng Liang | 6308644 | 2018-02-25 20:39:42 -0700 | [diff] [blame] | 2 | /* |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2024, Regents of the University of California, |
Davide Pesavento | b84bd3a | 2016-04-22 02:21:45 +0200 | [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. |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [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/>. |
Junxiao Shi | 82e7f58 | 2014-09-07 15:15:40 -0700 | [diff] [blame] | 24 | */ |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 25 | |
Junxiao Shi | 82e7f58 | 2014-09-07 15:15:40 -0700 | [diff] [blame] | 26 | #ifndef NFD_TESTS_DAEMON_FW_DUMMY_STRATEGY_HPP |
| 27 | #define NFD_TESTS_DAEMON_FW_DUMMY_STRATEGY_HPP |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 28 | |
| 29 | #include "fw/strategy.hpp" |
| 30 | |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 31 | #include <limits> |
| 32 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 33 | namespace nfd::tests { |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 34 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 35 | /** |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 36 | * \brief Forwarding strategy for unit testing. |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 37 | * |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 38 | * Triggers are recorded but do nothing. |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 39 | * |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 40 | * DummyStrategy registers itself as `/dummy-strategy/<max-version>`, so that it can be instantiated |
| 41 | * with any version number. Aliases can be created with the registerAs() function. |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 42 | */ |
| 43 | class DummyStrategy : public fw::Strategy |
| 44 | { |
| 45 | public: |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 46 | static void |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 47 | registerAs(const Name& strategyName); |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 48 | |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 49 | static Name |
| 50 | getStrategyName(uint64_t version = std::numeric_limits<uint64_t>::max()); |
| 51 | |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 52 | /** |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 53 | * \brief Constructor. |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 54 | * |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 55 | * \p name is recorded unchanged as getInstanceName(), and will not automatically |
| 56 | * gain a version number when instantiated without a version number. |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 57 | */ |
| 58 | explicit |
| 59 | DummyStrategy(Forwarder& forwarder, const Name& name = getStrategyName()); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 60 | |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 61 | /** |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 62 | * \brief After receive Interest trigger. |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 63 | * |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 64 | * If interestOutFace is not null, \p interest is forwarded to that face; |
| 65 | * otherwise, rejectPendingInterest() is invoked. |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 66 | */ |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 67 | void |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 68 | afterReceiveInterest(const Interest& interest, const FaceEndpoint& ingress, |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 69 | const shared_ptr<pit::Entry>& pitEntry) override; |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 70 | |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 71 | void |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 72 | afterContentStoreHit(const Data& data, const FaceEndpoint& ingress, |
| 73 | const shared_ptr<pit::Entry>& pitEntry) override; |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 74 | |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 75 | void |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 76 | beforeSatisfyInterest(const Data& data, const FaceEndpoint& ingress, |
| 77 | const shared_ptr<pit::Entry>& pitEntry) override; |
Teng Liang | 85a3663 | 2018-03-21 05:59:34 -0700 | [diff] [blame] | 78 | |
| 79 | void |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 80 | afterReceiveData(const Data& data, const FaceEndpoint& ingress, |
| 81 | const shared_ptr<pit::Entry>& pitEntry) override; |
Teng Liang | 43bb231 | 2018-03-26 04:16:42 -0700 | [diff] [blame] | 82 | |
| 83 | void |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 84 | afterReceiveNack(const lp::Nack& nack, const FaceEndpoint& ingress, |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 85 | const shared_ptr<pit::Entry>& pitEntry) override; |
| 86 | |
Ju Pan | 2feb459 | 2019-09-16 20:56:38 +0000 | [diff] [blame] | 87 | void |
| 88 | afterNewNextHop(const fib::NextHop& nextHop, const shared_ptr<pit::Entry>& pitEntry) override; |
| 89 | |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 90 | protected: |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 91 | /** \brief Register an alias. |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 92 | * \tparam S subclass of DummyStrategy |
| 93 | */ |
| 94 | template<typename S> |
| 95 | static void |
| 96 | registerAsImpl(const Name& strategyName) |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 97 | { |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 98 | if (!fw::Strategy::canCreate(strategyName)) { |
| 99 | fw::Strategy::registerType<S>(strategyName); |
| 100 | } |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 103 | public: |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 104 | int afterReceiveInterest_count = 0; |
| 105 | int afterContentStoreHit_count = 0; |
| 106 | int beforeSatisfyInterest_count = 0; |
| 107 | int afterReceiveData_count = 0; |
| 108 | int afterReceiveNack_count = 0; |
Junxiao Shi | 15e98b0 | 2016-08-12 11:21:44 +0000 | [diff] [blame] | 109 | |
Ju Pan | 2feb459 | 2019-09-16 20:56:38 +0000 | [diff] [blame] | 110 | // a collection of names of PIT entries that afterNewNextHop() was called on |
| 111 | std::vector<Name> afterNewNextHopCalls; |
| 112 | |
Junxiao Shi | 15e98b0 | 2016-08-12 11:21:44 +0000 | [diff] [blame] | 113 | shared_ptr<Face> interestOutFace; |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 114 | }; |
| 115 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 116 | /** |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 117 | * \brief DummyStrategy with specific version. |
Junxiao Shi | 7f566dd | 2016-12-27 02:28:31 +0000 | [diff] [blame] | 118 | */ |
| 119 | template<uint64_t VERSION> |
| 120 | class VersionedDummyStrategy : public DummyStrategy |
| 121 | { |
| 122 | public: |
| 123 | static void |
| 124 | registerAs(const Name& strategyName) |
| 125 | { |
| 126 | DummyStrategy::registerAsImpl<VersionedDummyStrategy<VERSION>>(strategyName); |
| 127 | } |
| 128 | |
| 129 | static Name |
| 130 | getStrategyName() |
| 131 | { |
| 132 | return DummyStrategy::getStrategyName(VERSION); |
| 133 | } |
| 134 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 135 | /** \brief Constructor. |
Junxiao Shi | 7f566dd | 2016-12-27 02:28:31 +0000 | [diff] [blame] | 136 | * |
| 137 | * The strategy instance name is taken from \p name ; if it does not contain a version component, |
| 138 | * \p VERSION will be appended. |
| 139 | */ |
| 140 | explicit |
| 141 | VersionedDummyStrategy(Forwarder& forwarder, const Name& name = getStrategyName()) |
| 142 | : DummyStrategy(forwarder, Strategy::makeInstanceName(name, getStrategyName())) |
| 143 | { |
| 144 | } |
| 145 | }; |
| 146 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 147 | } // namespace nfd::tests |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 148 | |
Junxiao Shi | 82e7f58 | 2014-09-07 15:15:40 -0700 | [diff] [blame] | 149 | #endif // NFD_TESTS_DAEMON_FW_DUMMY_STRATEGY_HPP |