blob: ae18eeaf538369e37f1c31da5ab2445a6bab261e [file] [log] [blame]
Junxiao Shi91f6ee02016-12-29 21:44:44 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -05002/*
Davide Pesaventof56cf632024-01-27 22:22:06 -05003 * Copyright (c) 2014-2024, Regents of the University of California,
Junxiao Shi91f6ee02016-12-29 21:44:44 +00004 * 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 Pesavento7922d122021-03-05 22:41:23 -050033#include "fw/best-route-strategy.hpp"
Junxiao Shi91f6ee02016-12-29 21:44:44 +000034#include "fw/multicast-strategy.hpp"
Teng Liang39465c22018-11-12 19:43:04 -070035#include "fw/self-learning-strategy.hpp"
Klaus Schneidercf1d0c02019-08-31 19:05:40 -070036#include "fw/random-strategy.hpp"
Junxiao Shi91f6ee02016-12-29 21:44:44 +000037
38#include "tests/test-common.hpp"
Davide Pesavento29abf4c2021-03-05 18:40:01 -050039
Davide Pesaventof56cf632024-01-27 22:22:06 -050040#include <boost/mp11/list.hpp>
Junxiao Shi91f6ee02016-12-29 21:44:44 +000041
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040042namespace nfd::tests {
43
44using namespace nfd::fw;
Junxiao Shi91f6ee02016-12-29 21:44:44 +000045
Junxiao Shi91f6ee02016-12-29 21:44:44 +000046BOOST_AUTO_TEST_SUITE(Fw)
47BOOST_AUTO_TEST_SUITE(TestStrategyInstantiation)
48
Davide Pesaventof56cf632024-01-27 22:22:06 -050049template<typename S, bool CanAcceptParams, uint64_t MinVer>
50struct Test
Junxiao Shi91f6ee02016-12-29 21:44:44 +000051{
Junxiao Shi91f6ee02016-12-29 21:44:44 +000052 using Strategy = S;
Davide Pesaventof56cf632024-01-27 22:22:06 -050053 static constexpr bool canAcceptParameters = CanAcceptParams;
54 static constexpr uint64_t minVersion = MinVer;
Junxiao Shi91f6ee02016-12-29 21:44:44 +000055
56 static Name
57 getVersionedStrategyName(uint64_t version)
58 {
59 return S::getStrategyName().getPrefix(-1).appendVersion(version);
60 }
61};
62
Davide Pesaventof56cf632024-01-27 22:22:06 -050063using Tests = boost::mp11::mp_list<
Junxiao Shi91f6ee02016-12-29 21:44:44 +000064 Test<AccessStrategy, false, 1>,
Davide Pesavento8e0dfcf2024-02-14 20:38:04 -050065 Test<AsfStrategy, true, 5>,
Ashlesh Gawande1ef93d02022-04-08 00:25:06 -040066 Test<BestRouteStrategy, true, 5>,
Mark Theeranantachai195b78a2024-02-14 20:54:44 -050067 Test<MulticastStrategy, true, 5>,
Klaus Schneidercf1d0c02019-08-31 19:05:40 -070068 Test<SelfLearningStrategy, false, 1>,
69 Test<RandomStrategy, false, 1>
Junxiao Shi91f6ee02016-12-29 21:44:44 +000070>;
71
72BOOST_AUTO_TEST_CASE_TEMPLATE(Registration, T, Tests)
73{
Davide Pesavento8e0dfcf2024-02-14 20:38:04 -050074 BOOST_TEST(Strategy::listRegistered().count(T::Strategy::getStrategyName()) == 1);
Junxiao Shi91f6ee02016-12-29 21:44:44 +000075}
76
77BOOST_AUTO_TEST_CASE_TEMPLATE(InstanceName, T, Tests)
78{
Davide Pesavento8e0dfcf2024-02-14 20:38:04 -050079 BOOST_TEST_REQUIRE(T::Strategy::getStrategyName().at(-1).isVersion());
Junxiao Shi91f6ee02016-12-29 21:44:44 +000080 uint64_t maxVersion = T::Strategy::getStrategyName().at(-1).toVersion();
Davide Pesavento8e0dfcf2024-02-14 20:38:04 -050081 BOOST_TEST_REQUIRE(T::minVersion <= maxVersion);
Junxiao Shi91f6ee02016-12-29 21:44:44 +000082
Davide Pesaventoa4abfb02019-10-06 16:02:56 -040083 FaceTable faceTable;
84 Forwarder forwarder(faceTable);
Davide Pesaventof56cf632024-01-27 22:22:06 -050085 for (auto version = T::minVersion; version <= maxVersion; ++version) {
Junxiao Shi91f6ee02016-12-29 21:44:44 +000086 Name versionedName = T::getVersionedStrategyName(version);
Davide Pesaventof56cf632024-01-27 22:22:06 -050087 auto instance = make_unique<typename T::Strategy>(forwarder, versionedName);
Davide Pesavento8e0dfcf2024-02-14 20:38:04 -050088 BOOST_TEST(instance->getInstanceName() == versionedName);
Junxiao Shi91f6ee02016-12-29 21:44:44 +000089
Davide Pesavento8e0dfcf2024-02-14 20:38:04 -050090 if constexpr (!T::canAcceptParameters) {
Junxiao Shi91f6ee02016-12-29 21:44:44 +000091 Name nameWithParameters = Name(versionedName).append("param");
92 BOOST_CHECK_THROW(typename T::Strategy(forwarder, nameWithParameters), std::invalid_argument);
93 }
94 }
95
Davide Pesavento8e0dfcf2024-02-14 20:38:04 -050096 if constexpr (T::minVersion > 0) {
Junxiao Shi91f6ee02016-12-29 21:44:44 +000097 Name version0Name = T::getVersionedStrategyName(0);
98 BOOST_CHECK_THROW(typename T::Strategy(forwarder, version0Name), std::invalid_argument);
Davide Pesaventof56cf632024-01-27 22:22:06 -050099 Name earlyVersionName = T::getVersionedStrategyName(T::minVersion - 1);
Junxiao Shi91f6ee02016-12-29 21:44:44 +0000100 BOOST_CHECK_THROW(typename T::Strategy(forwarder, earlyVersionName), std::invalid_argument);
101 }
102
103 if (maxVersion < std::numeric_limits<uint64_t>::max()) {
104 Name versionMaxName = T::getVersionedStrategyName(std::numeric_limits<uint64_t>::max());
105 BOOST_CHECK_THROW(typename T::Strategy(forwarder, versionMaxName), std::invalid_argument);
106 Name lateVersionName = T::getVersionedStrategyName(maxVersion + 1);
107 BOOST_CHECK_THROW(typename T::Strategy(forwarder, lateVersionName), std::invalid_argument);
108 }
109}
110
Ashlesh Gawande1ef93d02022-04-08 00:25:06 -0400111template<typename S>
112class SuppressionParametersFixture
113{
114public:
115 std::unique_ptr<S>
Davide Pesaventod7083a52023-10-19 17:51:16 -0400116 checkValidity(std::string_view parameters, bool isCorrect)
Ashlesh Gawande1ef93d02022-04-08 00:25:06 -0400117 {
Davide Pesaventod7083a52023-10-19 17:51:16 -0400118 BOOST_TEST_INFO_SCOPE(parameters);
119 Name strategyName(Name(S::getStrategyName()).append(Name(parameters)));
Ashlesh Gawande1ef93d02022-04-08 00:25:06 -0400120 std::unique_ptr<S> strategy;
Davide Pesaventod7083a52023-10-19 17:51:16 -0400121 if (isCorrect) {
122 strategy = make_unique<S>(m_forwarder, strategyName);
123 BOOST_CHECK(strategy->m_retxSuppression != nullptr);
124 }
125 else {
126 BOOST_CHECK_THROW(make_unique<S>(m_forwarder, strategyName), std::invalid_argument);
Ashlesh Gawande1ef93d02022-04-08 00:25:06 -0400127 }
128 return strategy;
129 }
130
131private:
132 FaceTable m_faceTable;
133 Forwarder m_forwarder{m_faceTable};
134};
135
Davide Pesaventof56cf632024-01-27 22:22:06 -0500136using StrategiesWithRetxSuppressionExponential = boost::mp11::mp_list<
Ashlesh Gawande1ef93d02022-04-08 00:25:06 -0400137 AsfStrategy,
138 BestRouteStrategy,
139 MulticastStrategy
140>;
141
142BOOST_FIXTURE_TEST_CASE_TEMPLATE(SuppressionParameters, S, StrategiesWithRetxSuppressionExponential,
143 SuppressionParametersFixture<S>)
144{
145 auto strategy = this->checkValidity("", true);
146 BOOST_TEST(strategy->m_retxSuppression->m_initialInterval == RetxSuppressionExponential::DEFAULT_INITIAL_INTERVAL);
147 BOOST_TEST(strategy->m_retxSuppression->m_maxInterval == RetxSuppressionExponential::DEFAULT_MAX_INTERVAL);
148 BOOST_TEST(strategy->m_retxSuppression->m_multiplier == RetxSuppressionExponential::DEFAULT_MULTIPLIER);
149
150 strategy = this->checkValidity("/retx-suppression-initial~20", true);
151 BOOST_TEST(strategy->m_retxSuppression->m_initialInterval == 20_ms);
152 BOOST_TEST(strategy->m_retxSuppression->m_maxInterval == RetxSuppressionExponential::DEFAULT_MAX_INTERVAL);
153 BOOST_TEST(strategy->m_retxSuppression->m_multiplier == RetxSuppressionExponential::DEFAULT_MULTIPLIER);
154 this->checkValidity("/retx-suppression-initial~0", false);
155 this->checkValidity("/retx-suppression-initial~20.5", false);
156 this->checkValidity("/retx-suppression-initial~-10", false);
157 this->checkValidity("/retx-suppression-initial~ -5", false);
158 this->checkValidity("/retx-suppression-initial~NaN", false);
159
160 strategy = this->checkValidity("/retx-suppression-max~1000", true);
161 BOOST_TEST(strategy->m_retxSuppression->m_initialInterval == RetxSuppressionExponential::DEFAULT_INITIAL_INTERVAL);
162 BOOST_TEST(strategy->m_retxSuppression->m_maxInterval == 1_s);
163 BOOST_TEST(strategy->m_retxSuppression->m_multiplier == RetxSuppressionExponential::DEFAULT_MULTIPLIER);
164 strategy = this->checkValidity("/retx-suppression-initial~40/retx-suppression-max~500", true);
165 BOOST_TEST(strategy->m_retxSuppression->m_initialInterval == 40_ms);
166 BOOST_TEST(strategy->m_retxSuppression->m_maxInterval == 500_ms);
167 this->checkValidity("/retx-suppression-initial~20/retx-suppression-max~10", false);
168 this->checkValidity("/retx-suppression-max~ 500", false);
169 this->checkValidity("/retx-suppression-max~521.5", false);
170
171 strategy = this->checkValidity("/retx-suppression-multiplier~2.25", true);
172 BOOST_TEST(strategy->m_retxSuppression->m_initialInterval == RetxSuppressionExponential::DEFAULT_INITIAL_INTERVAL);
173 BOOST_TEST(strategy->m_retxSuppression->m_maxInterval == RetxSuppressionExponential::DEFAULT_MAX_INTERVAL);
174 BOOST_TEST(strategy->m_retxSuppression->m_multiplier == 2.25);
175 this->checkValidity("/retx-suppression-multiplier~0", false);
176 this->checkValidity("/retx-suppression-multiplier~0.9", false);
177 this->checkValidity("/retx-suppression-multiplier~-2.1", false);
178 this->checkValidity("/retx-suppression-multiplier~foo", false);
179
180 strategy = this->checkValidity("/retx-suppression-initial~20/retx-suppression-max~500/retx-suppression-multiplier~3",
181 true);
182 BOOST_TEST(strategy->m_retxSuppression->m_initialInterval == 20_ms);
183 BOOST_TEST(strategy->m_retxSuppression->m_maxInterval == 500_ms);
184 BOOST_TEST(strategy->m_retxSuppression->m_multiplier == 3);
185
186 this->checkValidity("/foo~42", true); // unknown parameters are ignored
187}
188
Junxiao Shi91f6ee02016-12-29 21:44:44 +0000189BOOST_AUTO_TEST_SUITE_END() // TestStrategyInstantiation
190BOOST_AUTO_TEST_SUITE_END() // Fw
191
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400192} // namespace nfd::tests