blob: 21ab9a5445d2d06fc727ed88be3238ca0ae89f7a [file] [log] [blame]
Junxiao Shi91f6ee02016-12-29 21:44:44 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2016, Regents of the University of California,
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"
33#include "fw/best-route-strategy.hpp"
34#include "fw/best-route-strategy2.hpp"
35#include "fw/client-control-strategy.hpp"
36#include "fw/multicast-strategy.hpp"
37#include "fw/ncc-strategy.hpp"
38
39#include "tests/test-common.hpp"
40#include <boost/mpl/vector.hpp>
41
42namespace nfd {
43namespace fw {
44namespace tests {
45
46using namespace nfd::tests;
47
48BOOST_AUTO_TEST_SUITE(Fw)
49BOOST_AUTO_TEST_SUITE(TestStrategyInstantiation)
50
51template<typename S, bool CanAcceptParameters, uint64_t MinVersion>
52class Test
53{
54public:
55 using Strategy = S;
56
57 static bool
58 canAcceptParameters()
59 {
60 return CanAcceptParameters;
61 }
62
63 static uint64_t
64 getMinVersion()
65 {
66 return MinVersion;
67 }
68
69 static Name
70 getVersionedStrategyName(uint64_t version)
71 {
72 return S::getStrategyName().getPrefix(-1).appendVersion(version);
73 }
74};
75
76using Tests = boost::mpl::vector<
77 Test<AccessStrategy, false, 1>,
78 Test<AsfStrategy, true, 1>,
79 Test<BestRouteStrategy, false, 1>,
80 Test<BestRouteStrategy2, false, 4>,
81 Test<ClientControlStrategy, false, 2>,
82 Test<MulticastStrategy, false, 1>,
83 Test<NccStrategy, false, 1>
84>;
85
86BOOST_AUTO_TEST_CASE_TEMPLATE(Registration, T, Tests)
87{
88 BOOST_CHECK_EQUAL(Strategy::listRegistered().count(T::Strategy::getStrategyName()), 1);
89}
90
91BOOST_AUTO_TEST_CASE_TEMPLATE(InstanceName, T, Tests)
92{
93 BOOST_REQUIRE(T::Strategy::getStrategyName().at(-1).isVersion());
94 uint64_t maxVersion = T::Strategy::getStrategyName().at(-1).toVersion();
95 BOOST_REQUIRE_LE(T::getMinVersion(), maxVersion);
96
97 Forwarder forwarder;
98 for (uint64_t version = T::getMinVersion(); version <= maxVersion; ++version) {
99 Name versionedName = T::getVersionedStrategyName(version);
100 unique_ptr<typename T::Strategy> instance;
101 BOOST_CHECK_NO_THROW(instance = make_unique<typename T::Strategy>(forwarder, versionedName));
102 BOOST_CHECK_EQUAL(instance->getInstanceName(), versionedName);
103
104 if (!T::canAcceptParameters()) {
105 Name nameWithParameters = Name(versionedName).append("param");
106 BOOST_CHECK_THROW(typename T::Strategy(forwarder, nameWithParameters), std::invalid_argument);
107 }
108 }
109
110 if (T::getMinVersion() > 0) {
111 Name version0Name = T::getVersionedStrategyName(0);
112 BOOST_CHECK_THROW(typename T::Strategy(forwarder, version0Name), std::invalid_argument);
113 Name earlyVersionName = T::getVersionedStrategyName(T::getMinVersion() - 1);
114 BOOST_CHECK_THROW(typename T::Strategy(forwarder, earlyVersionName), std::invalid_argument);
115 }
116
117 if (maxVersion < std::numeric_limits<uint64_t>::max()) {
118 Name versionMaxName = T::getVersionedStrategyName(std::numeric_limits<uint64_t>::max());
119 BOOST_CHECK_THROW(typename T::Strategy(forwarder, versionMaxName), std::invalid_argument);
120 Name lateVersionName = T::getVersionedStrategyName(maxVersion + 1);
121 BOOST_CHECK_THROW(typename T::Strategy(forwarder, lateVersionName), std::invalid_argument);
122 }
123}
124
125BOOST_AUTO_TEST_SUITE_END() // TestStrategyInstantiation
126BOOST_AUTO_TEST_SUITE_END() // Fw
127
128} // namespace tests
129} // namespace fw
130} // namespace nfd