blob: c6fe4716b05c96892f4a7763c76498a5b9d9c3e9 [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/*
Junxiao Shi00e32fe2019-01-14 23:34:32 +00003 * Copyright (c) 2014-2019, 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"
33#include "fw/best-route-strategy.hpp"
34#include "fw/best-route-strategy2.hpp"
Junxiao Shi91f6ee02016-12-29 21:44:44 +000035#include "fw/multicast-strategy.hpp"
36#include "fw/ncc-strategy.hpp"
37
38#include "tests/test-common.hpp"
39#include <boost/mpl/vector.hpp>
40
41namespace nfd {
42namespace fw {
43namespace tests {
44
45using namespace nfd::tests;
46
47BOOST_AUTO_TEST_SUITE(Fw)
48BOOST_AUTO_TEST_SUITE(TestStrategyInstantiation)
49
50template<typename S, bool CanAcceptParameters, uint64_t MinVersion>
51class Test
52{
53public:
54 using Strategy = S;
55
56 static bool
57 canAcceptParameters()
58 {
59 return CanAcceptParameters;
60 }
61
62 static uint64_t
63 getMinVersion()
64 {
65 return MinVersion;
66 }
67
68 static Name
69 getVersionedStrategyName(uint64_t version)
70 {
71 return S::getStrategyName().getPrefix(-1).appendVersion(version);
72 }
73};
74
75using Tests = boost::mpl::vector<
76 Test<AccessStrategy, false, 1>,
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -050077 Test<AsfStrategy, true, 3>,
Junxiao Shi91f6ee02016-12-29 21:44:44 +000078 Test<BestRouteStrategy, false, 1>,
Teng Liangf995f382017-04-04 22:09:39 +000079 Test<BestRouteStrategy2, false, 5>,
Teng Liangf995f382017-04-04 22:09:39 +000080 Test<MulticastStrategy, false, 3>,
Junxiao Shi91f6ee02016-12-29 21:44:44 +000081 Test<NccStrategy, false, 1>
82>;
83
84BOOST_AUTO_TEST_CASE_TEMPLATE(Registration, T, Tests)
85{
86 BOOST_CHECK_EQUAL(Strategy::listRegistered().count(T::Strategy::getStrategyName()), 1);
87}
88
89BOOST_AUTO_TEST_CASE_TEMPLATE(InstanceName, T, Tests)
90{
91 BOOST_REQUIRE(T::Strategy::getStrategyName().at(-1).isVersion());
92 uint64_t maxVersion = T::Strategy::getStrategyName().at(-1).toVersion();
93 BOOST_REQUIRE_LE(T::getMinVersion(), maxVersion);
94
95 Forwarder forwarder;
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
123BOOST_AUTO_TEST_SUITE_END() // TestStrategyInstantiation
124BOOST_AUTO_TEST_SUITE_END() // Fw
125
126} // namespace tests
127} // namespace fw
128} // namespace nfd