blob: 4b319cb4d353dfae739a9bad2278000f9ffdae26 [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/*
Alexander Afanasyev4400e422021-02-17 11:17:33 -05003 * Copyright (c) 2014-2021, 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
Junxiao Shi91f6ee02016-12-29 21:44:44 +000040#include <boost/mpl/vector.hpp>
41
42namespace nfd {
43namespace fw {
44namespace tests {
45
Junxiao Shi91f6ee02016-12-29 21:44:44 +000046BOOST_AUTO_TEST_SUITE(Fw)
47BOOST_AUTO_TEST_SUITE(TestStrategyInstantiation)
48
49template<typename S, bool CanAcceptParameters, uint64_t MinVersion>
50class Test
51{
52public:
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
74using Tests = boost::mpl::vector<
75 Test<AccessStrategy, false, 1>,
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -050076 Test<AsfStrategy, true, 3>,
Davide Pesavento7922d122021-03-05 22:41:23 -050077 Test<BestRouteStrategy, false, 5>,
Alexander Afanasyev4400e422021-02-17 11:17:33 -050078 Test<MulticastStrategy, false, 4>,
Klaus Schneidercf1d0c02019-08-31 19:05:40 -070079 Test<SelfLearningStrategy, false, 1>,
80 Test<RandomStrategy, false, 1>
Junxiao Shi91f6ee02016-12-29 21:44:44 +000081>;
82
83BOOST_AUTO_TEST_CASE_TEMPLATE(Registration, T, Tests)
84{
85 BOOST_CHECK_EQUAL(Strategy::listRegistered().count(T::Strategy::getStrategyName()), 1);
86}
87
88BOOST_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 Pesaventoa4abfb02019-10-06 16:02:56 -040094 FaceTable faceTable;
95 Forwarder forwarder(faceTable);
Junxiao Shi91f6ee02016-12-29 21:44:44 +000096 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
123BOOST_AUTO_TEST_SUITE_END() // TestStrategyInstantiation
124BOOST_AUTO_TEST_SUITE_END() // Fw
125
126} // namespace tests
127} // namespace fw
128} // namespace nfd