Junxiao Shi | 91f6ee0 | 2016-12-29 21:44:44 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Ashlesh Gawande | 92e4ea5 | 2017-07-19 11:38:12 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | d7083a5 | 2023-10-19 17:51:16 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2023, Regents of the University of California, |
Junxiao Shi | 91f6ee0 | 2016-12-29 21:44:44 +0000 | [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. |
| 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 | |
| 26 | /** \file |
| 27 | * This test suite tests instantiation logic in strategies. |
| 28 | */ |
| 29 | |
| 30 | // All strategies, sorted alphabetically. |
| 31 | #include "fw/access-strategy.hpp" |
| 32 | #include "fw/asf-strategy.hpp" |
Davide Pesavento | 7922d12 | 2021-03-05 22:41:23 -0500 | [diff] [blame] | 33 | #include "fw/best-route-strategy.hpp" |
Junxiao Shi | 91f6ee0 | 2016-12-29 21:44:44 +0000 | [diff] [blame] | 34 | #include "fw/multicast-strategy.hpp" |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 35 | #include "fw/self-learning-strategy.hpp" |
Klaus Schneider | cf1d0c0 | 2019-08-31 19:05:40 -0700 | [diff] [blame] | 36 | #include "fw/random-strategy.hpp" |
Junxiao Shi | 91f6ee0 | 2016-12-29 21:44:44 +0000 | [diff] [blame] | 37 | |
| 38 | #include "tests/test-common.hpp" |
Davide Pesavento | 29abf4c | 2021-03-05 18:40:01 -0500 | [diff] [blame] | 39 | |
Junxiao Shi | 91f6ee0 | 2016-12-29 21:44:44 +0000 | [diff] [blame] | 40 | #include <boost/mpl/vector.hpp> |
| 41 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 42 | namespace nfd::tests { |
| 43 | |
| 44 | using namespace nfd::fw; |
Junxiao Shi | 91f6ee0 | 2016-12-29 21:44:44 +0000 | [diff] [blame] | 45 | |
Junxiao Shi | 91f6ee0 | 2016-12-29 21:44:44 +0000 | [diff] [blame] | 46 | BOOST_AUTO_TEST_SUITE(Fw) |
| 47 | BOOST_AUTO_TEST_SUITE(TestStrategyInstantiation) |
| 48 | |
| 49 | template<typename S, bool CanAcceptParameters, uint64_t MinVersion> |
| 50 | class Test |
| 51 | { |
| 52 | public: |
| 53 | using Strategy = S; |
| 54 | |
| 55 | static bool |
| 56 | canAcceptParameters() |
| 57 | { |
| 58 | return CanAcceptParameters; |
| 59 | } |
| 60 | |
| 61 | static uint64_t |
| 62 | getMinVersion() |
| 63 | { |
| 64 | return MinVersion; |
| 65 | } |
| 66 | |
| 67 | static Name |
| 68 | getVersionedStrategyName(uint64_t version) |
| 69 | { |
| 70 | return S::getStrategyName().getPrefix(-1).appendVersion(version); |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | using Tests = boost::mpl::vector< |
| 75 | Test<AccessStrategy, false, 1>, |
Saurab Dulal | 432be57 | 2021-01-26 12:09:29 -0600 | [diff] [blame] | 76 | Test<AsfStrategy, true, 4>, |
Ashlesh Gawande | 1ef93d0 | 2022-04-08 00:25:06 -0400 | [diff] [blame] | 77 | Test<BestRouteStrategy, true, 5>, |
| 78 | Test<MulticastStrategy, true, 4>, |
Klaus Schneider | cf1d0c0 | 2019-08-31 19:05:40 -0700 | [diff] [blame] | 79 | Test<SelfLearningStrategy, false, 1>, |
| 80 | Test<RandomStrategy, false, 1> |
Junxiao Shi | 91f6ee0 | 2016-12-29 21:44:44 +0000 | [diff] [blame] | 81 | >; |
| 82 | |
| 83 | BOOST_AUTO_TEST_CASE_TEMPLATE(Registration, T, Tests) |
| 84 | { |
| 85 | BOOST_CHECK_EQUAL(Strategy::listRegistered().count(T::Strategy::getStrategyName()), 1); |
| 86 | } |
| 87 | |
| 88 | BOOST_AUTO_TEST_CASE_TEMPLATE(InstanceName, T, Tests) |
| 89 | { |
| 90 | BOOST_REQUIRE(T::Strategy::getStrategyName().at(-1).isVersion()); |
| 91 | uint64_t maxVersion = T::Strategy::getStrategyName().at(-1).toVersion(); |
| 92 | BOOST_REQUIRE_LE(T::getMinVersion(), maxVersion); |
| 93 | |
Davide Pesavento | a4abfb0 | 2019-10-06 16:02:56 -0400 | [diff] [blame] | 94 | FaceTable faceTable; |
| 95 | Forwarder forwarder(faceTable); |
Junxiao Shi | 91f6ee0 | 2016-12-29 21:44:44 +0000 | [diff] [blame] | 96 | for (uint64_t version = T::getMinVersion(); version <= maxVersion; ++version) { |
| 97 | Name versionedName = T::getVersionedStrategyName(version); |
| 98 | unique_ptr<typename T::Strategy> instance; |
| 99 | BOOST_CHECK_NO_THROW(instance = make_unique<typename T::Strategy>(forwarder, versionedName)); |
| 100 | BOOST_CHECK_EQUAL(instance->getInstanceName(), versionedName); |
| 101 | |
| 102 | if (!T::canAcceptParameters()) { |
| 103 | Name nameWithParameters = Name(versionedName).append("param"); |
| 104 | BOOST_CHECK_THROW(typename T::Strategy(forwarder, nameWithParameters), std::invalid_argument); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | if (T::getMinVersion() > 0) { |
| 109 | Name version0Name = T::getVersionedStrategyName(0); |
| 110 | BOOST_CHECK_THROW(typename T::Strategy(forwarder, version0Name), std::invalid_argument); |
| 111 | Name earlyVersionName = T::getVersionedStrategyName(T::getMinVersion() - 1); |
| 112 | BOOST_CHECK_THROW(typename T::Strategy(forwarder, earlyVersionName), std::invalid_argument); |
| 113 | } |
| 114 | |
| 115 | if (maxVersion < std::numeric_limits<uint64_t>::max()) { |
| 116 | Name versionMaxName = T::getVersionedStrategyName(std::numeric_limits<uint64_t>::max()); |
| 117 | BOOST_CHECK_THROW(typename T::Strategy(forwarder, versionMaxName), std::invalid_argument); |
| 118 | Name lateVersionName = T::getVersionedStrategyName(maxVersion + 1); |
| 119 | BOOST_CHECK_THROW(typename T::Strategy(forwarder, lateVersionName), std::invalid_argument); |
| 120 | } |
| 121 | } |
| 122 | |
Ashlesh Gawande | 1ef93d0 | 2022-04-08 00:25:06 -0400 | [diff] [blame] | 123 | template<typename S> |
| 124 | class SuppressionParametersFixture |
| 125 | { |
| 126 | public: |
| 127 | std::unique_ptr<S> |
Davide Pesavento | d7083a5 | 2023-10-19 17:51:16 -0400 | [diff] [blame] | 128 | checkValidity(std::string_view parameters, bool isCorrect) |
Ashlesh Gawande | 1ef93d0 | 2022-04-08 00:25:06 -0400 | [diff] [blame] | 129 | { |
Davide Pesavento | d7083a5 | 2023-10-19 17:51:16 -0400 | [diff] [blame] | 130 | BOOST_TEST_INFO_SCOPE(parameters); |
| 131 | Name strategyName(Name(S::getStrategyName()).append(Name(parameters))); |
Ashlesh Gawande | 1ef93d0 | 2022-04-08 00:25:06 -0400 | [diff] [blame] | 132 | std::unique_ptr<S> strategy; |
Davide Pesavento | d7083a5 | 2023-10-19 17:51:16 -0400 | [diff] [blame] | 133 | if (isCorrect) { |
| 134 | strategy = make_unique<S>(m_forwarder, strategyName); |
| 135 | BOOST_CHECK(strategy->m_retxSuppression != nullptr); |
| 136 | } |
| 137 | else { |
| 138 | BOOST_CHECK_THROW(make_unique<S>(m_forwarder, strategyName), std::invalid_argument); |
Ashlesh Gawande | 1ef93d0 | 2022-04-08 00:25:06 -0400 | [diff] [blame] | 139 | } |
| 140 | return strategy; |
| 141 | } |
| 142 | |
| 143 | private: |
| 144 | FaceTable m_faceTable; |
| 145 | Forwarder m_forwarder{m_faceTable}; |
| 146 | }; |
| 147 | |
| 148 | using StrategiesWithRetxSuppressionExponential = boost::mpl::vector< |
| 149 | AsfStrategy, |
| 150 | BestRouteStrategy, |
| 151 | MulticastStrategy |
| 152 | >; |
| 153 | |
| 154 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(SuppressionParameters, S, StrategiesWithRetxSuppressionExponential, |
| 155 | SuppressionParametersFixture<S>) |
| 156 | { |
| 157 | auto strategy = this->checkValidity("", true); |
| 158 | BOOST_TEST(strategy->m_retxSuppression->m_initialInterval == RetxSuppressionExponential::DEFAULT_INITIAL_INTERVAL); |
| 159 | BOOST_TEST(strategy->m_retxSuppression->m_maxInterval == RetxSuppressionExponential::DEFAULT_MAX_INTERVAL); |
| 160 | BOOST_TEST(strategy->m_retxSuppression->m_multiplier == RetxSuppressionExponential::DEFAULT_MULTIPLIER); |
| 161 | |
| 162 | strategy = this->checkValidity("/retx-suppression-initial~20", true); |
| 163 | BOOST_TEST(strategy->m_retxSuppression->m_initialInterval == 20_ms); |
| 164 | BOOST_TEST(strategy->m_retxSuppression->m_maxInterval == RetxSuppressionExponential::DEFAULT_MAX_INTERVAL); |
| 165 | BOOST_TEST(strategy->m_retxSuppression->m_multiplier == RetxSuppressionExponential::DEFAULT_MULTIPLIER); |
| 166 | this->checkValidity("/retx-suppression-initial~0", false); |
| 167 | this->checkValidity("/retx-suppression-initial~20.5", false); |
| 168 | this->checkValidity("/retx-suppression-initial~-10", false); |
| 169 | this->checkValidity("/retx-suppression-initial~ -5", false); |
| 170 | this->checkValidity("/retx-suppression-initial~NaN", false); |
| 171 | |
| 172 | strategy = this->checkValidity("/retx-suppression-max~1000", true); |
| 173 | BOOST_TEST(strategy->m_retxSuppression->m_initialInterval == RetxSuppressionExponential::DEFAULT_INITIAL_INTERVAL); |
| 174 | BOOST_TEST(strategy->m_retxSuppression->m_maxInterval == 1_s); |
| 175 | BOOST_TEST(strategy->m_retxSuppression->m_multiplier == RetxSuppressionExponential::DEFAULT_MULTIPLIER); |
| 176 | strategy = this->checkValidity("/retx-suppression-initial~40/retx-suppression-max~500", true); |
| 177 | BOOST_TEST(strategy->m_retxSuppression->m_initialInterval == 40_ms); |
| 178 | BOOST_TEST(strategy->m_retxSuppression->m_maxInterval == 500_ms); |
| 179 | this->checkValidity("/retx-suppression-initial~20/retx-suppression-max~10", false); |
| 180 | this->checkValidity("/retx-suppression-max~ 500", false); |
| 181 | this->checkValidity("/retx-suppression-max~521.5", false); |
| 182 | |
| 183 | strategy = this->checkValidity("/retx-suppression-multiplier~2.25", true); |
| 184 | BOOST_TEST(strategy->m_retxSuppression->m_initialInterval == RetxSuppressionExponential::DEFAULT_INITIAL_INTERVAL); |
| 185 | BOOST_TEST(strategy->m_retxSuppression->m_maxInterval == RetxSuppressionExponential::DEFAULT_MAX_INTERVAL); |
| 186 | BOOST_TEST(strategy->m_retxSuppression->m_multiplier == 2.25); |
| 187 | this->checkValidity("/retx-suppression-multiplier~0", false); |
| 188 | this->checkValidity("/retx-suppression-multiplier~0.9", false); |
| 189 | this->checkValidity("/retx-suppression-multiplier~-2.1", false); |
| 190 | this->checkValidity("/retx-suppression-multiplier~foo", false); |
| 191 | |
| 192 | strategy = this->checkValidity("/retx-suppression-initial~20/retx-suppression-max~500/retx-suppression-multiplier~3", |
| 193 | true); |
| 194 | BOOST_TEST(strategy->m_retxSuppression->m_initialInterval == 20_ms); |
| 195 | BOOST_TEST(strategy->m_retxSuppression->m_maxInterval == 500_ms); |
| 196 | BOOST_TEST(strategy->m_retxSuppression->m_multiplier == 3); |
| 197 | |
| 198 | this->checkValidity("/foo~42", true); // unknown parameters are ignored |
| 199 | } |
| 200 | |
Junxiao Shi | 91f6ee0 | 2016-12-29 21:44:44 +0000 | [diff] [blame] | 201 | BOOST_AUTO_TEST_SUITE_END() // TestStrategyInstantiation |
| 202 | BOOST_AUTO_TEST_SUITE_END() // Fw |
| 203 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 204 | } // namespace nfd::tests |