blob: 450839126c45b41676417dd8691d1fdaed16c40f [file] [log] [blame]
Junxiao Shibb5105f2014-03-03 12:06:45 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi4370fde2016-02-24 12:20:46 -07003 * Copyright (c) 2014-2016, Regents of the University of California,
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -08004 * 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.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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/>.
Junxiao Shiee5a4442014-07-27 17:13:43 -070024 */
Junxiao Shibb5105f2014-03-03 12:06:45 -070025
26#include "table/strategy-choice.hpp"
Junxiao Shibb5105f2014-03-03 12:06:45 -070027
28#include "tests/test-common.hpp"
Junxiao Shia49a1ab2016-07-15 18:24:36 +000029#include "../fw/dummy-strategy.hpp"
Junxiao Shibb5105f2014-03-03 12:06:45 -070030
31namespace nfd {
32namespace tests {
33
Junxiao Shi18739c42016-12-22 08:03:00 +000034class StrategyChoiceFixture : public BaseFixture
35{
36protected:
37 StrategyChoiceFixture()
38 : sc(forwarder.getStrategyChoice())
Junxiao Shi0e4a1f12016-12-24 02:39:01 +000039 , strategyNameP("/strategy-choice-P/%FD%00")
40 , strategyNameQ("/strategy-choice-Q/%FD%00")
Junxiao Shi18739c42016-12-22 08:03:00 +000041 {
Junxiao Shi0e4a1f12016-12-24 02:39:01 +000042 DummyStrategy::registerAs(strategyNameP);
43 DummyStrategy::registerAs(strategyNameQ);
Junxiao Shi18739c42016-12-22 08:03:00 +000044 }
45
46 Name
47 insertAndGet(const Name& prefix, const Name& strategyName)
48 {
49 BOOST_REQUIRE(sc.insert(prefix, strategyName));
50 bool isFound;
51 Name foundName;
52 std::tie(isFound, foundName) = sc.get(prefix);
53 BOOST_REQUIRE(isFound);
54 return foundName;
55 }
56
Junxiao Shi0e4a1f12016-12-24 02:39:01 +000057 template<typename Q>
58 Name
59 findInstanceName(const Q& query)
60 {
61 return sc.findEffectiveStrategy(query).getInstanceName();
62 }
63
Junxiao Shi18739c42016-12-22 08:03:00 +000064protected:
65 Forwarder forwarder;
66 StrategyChoice& sc;
Junxiao Shi0e4a1f12016-12-24 02:39:01 +000067
68 const Name strategyNameP;
69 const Name strategyNameQ;
Junxiao Shi18739c42016-12-22 08:03:00 +000070};
71
Junxiao Shi4370fde2016-02-24 12:20:46 -070072BOOST_AUTO_TEST_SUITE(Table)
Junxiao Shi18739c42016-12-22 08:03:00 +000073BOOST_FIXTURE_TEST_SUITE(TestStrategyChoice, StrategyChoiceFixture)
Junxiao Shibb5105f2014-03-03 12:06:45 -070074
75using fw::Strategy;
76
Junxiao Shi7f566dd2016-12-27 02:28:31 +000077BOOST_AUTO_TEST_CASE(Versioning)
78{
79 const Name strategyNameV("/strategy-choice-V");
80 const Name strategyNameV0("/strategy-choice-V/%FD%00");
81 const Name strategyNameV1("/strategy-choice-V/%FD%01");
82 const Name strategyNameV2("/strategy-choice-V/%FD%02");
83 const Name strategyNameV3("/strategy-choice-V/%FD%03");
84 const Name strategyNameV4("/strategy-choice-V/%FD%04");
85 const Name strategyNameV5("/strategy-choice-V/%FD%05");
86
87 VersionedDummyStrategy<1>::registerAs(strategyNameV1);
88 VersionedDummyStrategy<3>::registerAs(strategyNameV3);
89 VersionedDummyStrategy<4>::registerAs(strategyNameV4);
90
91 // unversioned: choose latest version
92 BOOST_CHECK_EQUAL(this->insertAndGet("/A", strategyNameV), strategyNameV4);
93
94 // exact version: choose same version
95 BOOST_CHECK_EQUAL(this->insertAndGet("/B", strategyNameV1), strategyNameV1);
96 BOOST_CHECK_EQUAL(this->insertAndGet("/C", strategyNameV3), strategyNameV3);
97 BOOST_CHECK_EQUAL(this->insertAndGet("/D", strategyNameV4), strategyNameV4);
98
99 // lower version: choose next higher version
100 // BOOST_CHECK_EQUAL(this->insertAndGet("/E", strategyNameV0), strategyNameV1);
101 // BOOST_CHECK_EQUAL(this->insertAndGet("/F", strategyNameV2), strategyNameV3);
102
103 // higher version: failure
104 BOOST_CHECK_EQUAL(sc.insert("/G", strategyNameV5), false);
105}
106
Junxiao Shi18739c42016-12-22 08:03:00 +0000107BOOST_AUTO_TEST_CASE(Parameters)
108{
Junxiao Shi18739c42016-12-22 08:03:00 +0000109 // no parameters
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000110 BOOST_CHECK_EQUAL(this->insertAndGet("/A", strategyNameP), strategyNameP);
Junxiao Shi18739c42016-12-22 08:03:00 +0000111
112 // one parameter
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000113 Name oneParamName = Name(strategyNameP).append("param");
Junxiao Shi18739c42016-12-22 08:03:00 +0000114 BOOST_CHECK_EQUAL(this->insertAndGet("/B", oneParamName), oneParamName);
115
116 // two parameters
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000117 Name twoParamName = Name(strategyNameP).append("x").append("y");
Junxiao Shi18739c42016-12-22 08:03:00 +0000118 BOOST_CHECK_EQUAL(this->insertAndGet("/C", twoParamName), twoParamName);
119
120 // parameter without version is disallowed
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000121 Name oneParamUnversioned = strategyNameP.getPrefix(-1).append("param");
Junxiao Shi18739c42016-12-22 08:03:00 +0000122 BOOST_CHECK_EQUAL(sc.insert("/D", oneParamUnversioned), false);
123}
124
Steve DiBenedetto77c87512014-10-06 14:18:22 -0600125BOOST_AUTO_TEST_CASE(Get)
126{
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000127 BOOST_CHECK(sc.insert("/", strategyNameP));
Steve DiBenedetto77c87512014-10-06 14:18:22 -0600128 // { '/'=>P }
129
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000130 auto getRoot = sc.get("/");
Junxiao Shi838c4f12014-11-03 18:55:24 -0700131 BOOST_CHECK_EQUAL(getRoot.first, true);
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000132 BOOST_CHECK_EQUAL(getRoot.second, strategyNameP);
Junxiao Shi838c4f12014-11-03 18:55:24 -0700133
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000134 auto getA = sc.get("/A");
Junxiao Shi838c4f12014-11-03 18:55:24 -0700135 BOOST_CHECK_EQUAL(getA.first, false);
Steve DiBenedetto77c87512014-10-06 14:18:22 -0600136}
137
Junxiao Shi4370fde2016-02-24 12:20:46 -0700138BOOST_AUTO_TEST_CASE(FindEffectiveStrategy)
Junxiao Shibb5105f2014-03-03 12:06:45 -0700139{
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000140 const Name strategyNameZ("/strategy-choice-Z/%FD%00"); // unregistered strategyName
Junxiao Shibb5105f2014-03-03 12:06:45 -0700141
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000142 BOOST_CHECK(sc.insert("/", strategyNameP));
Junxiao Shibb5105f2014-03-03 12:06:45 -0700143 // { '/'=>P }
144
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000145 BOOST_CHECK_EQUAL(this->findInstanceName("/"), strategyNameP);
146 BOOST_CHECK_EQUAL(this->findInstanceName("/A"), strategyNameP);
147 BOOST_CHECK_EQUAL(this->findInstanceName("/A/B"), strategyNameP);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700148
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000149 BOOST_CHECK(sc.insert("/A/B", strategyNameP));
Junxiao Shibb5105f2014-03-03 12:06:45 -0700150 // { '/'=>P, '/A/B'=>P }
151
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000152 BOOST_CHECK_EQUAL(this->findInstanceName("/"), strategyNameP);
153 BOOST_CHECK_EQUAL(this->findInstanceName("/A"), strategyNameP);
154 BOOST_CHECK_EQUAL(this->findInstanceName("/A/B"), strategyNameP);
155 // same entry, same instance
156 BOOST_CHECK_EQUAL(&sc.findEffectiveStrategy("/"), &sc.findEffectiveStrategy("/A"));
157 // different entries, distinct instances
158 BOOST_CHECK_NE(&sc.findEffectiveStrategy("/"), &sc.findEffectiveStrategy("/A/B"));
Junxiao Shibb5105f2014-03-03 12:06:45 -0700159
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000160 sc.erase("/A"); // no effect
Junxiao Shibb5105f2014-03-03 12:06:45 -0700161 // { '/'=>P, '/A/B'=>P }
162
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000163 BOOST_CHECK_EQUAL(this->findInstanceName("/"), strategyNameP);
164 BOOST_CHECK_EQUAL(this->findInstanceName("/A"), strategyNameP);
165 BOOST_CHECK_EQUAL(this->findInstanceName("/A/B"), strategyNameP);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700166
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000167 BOOST_CHECK(sc.insert("/A", strategyNameQ));
Junxiao Shibb5105f2014-03-03 12:06:45 -0700168 // { '/'=>P, '/A/B'=>P, '/A'=>Q }
169
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000170 BOOST_CHECK_EQUAL(this->findInstanceName("/"), strategyNameP);
171 BOOST_CHECK_EQUAL(this->findInstanceName("/A"), strategyNameQ);
172 BOOST_CHECK_EQUAL(this->findInstanceName("/A/B"), strategyNameP);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700173
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000174 sc.erase("/A/B");
Junxiao Shibb5105f2014-03-03 12:06:45 -0700175 // { '/'=>P, '/A'=>Q }
176
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000177 BOOST_CHECK_EQUAL(this->findInstanceName("/"), strategyNameP);
178 BOOST_CHECK_EQUAL(this->findInstanceName("/A"), strategyNameQ);
179 BOOST_CHECK_EQUAL(this->findInstanceName("/A/B"), strategyNameQ);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700180
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000181 BOOST_CHECK(!sc.insert("/", strategyNameZ)); // non existent strategy
Junxiao Shibb5105f2014-03-03 12:06:45 -0700182
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000183 BOOST_CHECK(sc.insert("/", strategyNameQ));
184 BOOST_CHECK(sc.insert("/A", strategyNameP));
Junxiao Shibb5105f2014-03-03 12:06:45 -0700185 // { '/'=>Q, '/A'=>P }
186
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000187 BOOST_CHECK_EQUAL(this->findInstanceName("/"), strategyNameQ);
188 BOOST_CHECK_EQUAL(this->findInstanceName("/A"), strategyNameP);
189 BOOST_CHECK_EQUAL(this->findInstanceName("/A/B"), strategyNameP);
190 BOOST_CHECK_EQUAL(this->findInstanceName("/D"), strategyNameQ);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700191}
192
Junxiao Shi4370fde2016-02-24 12:20:46 -0700193BOOST_AUTO_TEST_CASE(FindEffectiveStrategyWithPitEntry)
194{
Junxiao Shi4370fde2016-02-24 12:20:46 -0700195 shared_ptr<Data> dataABC = makeData("/A/B/C");
196 Name fullName = dataABC->getFullName();
197
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000198 BOOST_CHECK(sc.insert("/A", strategyNameP));
199 BOOST_CHECK(sc.insert(fullName, strategyNameQ));
Junxiao Shi4370fde2016-02-24 12:20:46 -0700200
201 Pit& pit = forwarder.getPit();
202 shared_ptr<Interest> interestAB = makeInterest("/A/B");
203 shared_ptr<pit::Entry> pitAB = pit.insert(*interestAB).first;
204 shared_ptr<Interest> interestFull = makeInterest(fullName);
205 shared_ptr<pit::Entry> pitFull = pit.insert(*interestFull).first;
206
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000207 BOOST_CHECK_EQUAL(this->findInstanceName(*pitAB), strategyNameP);
208 BOOST_CHECK_EQUAL(this->findInstanceName(*pitFull), strategyNameQ);
Junxiao Shi4370fde2016-02-24 12:20:46 -0700209}
210
211BOOST_AUTO_TEST_CASE(FindEffectiveStrategyWithMeasurementsEntry)
212{
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000213 BOOST_CHECK(sc.insert("/A", strategyNameP));
214 BOOST_CHECK(sc.insert("/A/B/C", strategyNameQ));
Junxiao Shi4370fde2016-02-24 12:20:46 -0700215
216 Measurements& measurements = forwarder.getMeasurements();
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000217 measurements::Entry& mAB = measurements.get("/A/B");
218 measurements::Entry& mABCD = measurements.get("/A/B/C/D");
Junxiao Shi4370fde2016-02-24 12:20:46 -0700219
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000220 BOOST_CHECK_EQUAL(this->findInstanceName(mAB), strategyNameP);
221 BOOST_CHECK_EQUAL(this->findInstanceName(mABCD), strategyNameQ);
222}
223
224BOOST_AUTO_TEST_CASE(Erase)
225{
226 NameTree& nameTree = forwarder.getNameTree();
227
228 sc.insert("/", strategyNameP);
229
230 size_t nNameTreeEntriesBefore = nameTree.size();
231
232 sc.insert("/A/B", strategyNameQ);
233 sc.erase("/A/B");
234 BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore);
Junxiao Shi4370fde2016-02-24 12:20:46 -0700235}
236
Junxiao Shib5888d22014-05-26 07:35:22 -0700237BOOST_AUTO_TEST_CASE(Enumerate)
238{
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000239 sc.insert("/", strategyNameP);
240 sc.insert("/A/B", strategyNameQ);
241 sc.insert("/A/B/C", strategyNameP);
242 sc.insert("/D", strategyNameP);
243 sc.insert("/E", strategyNameQ);
Junxiao Shib5888d22014-05-26 07:35:22 -0700244
Junxiao Shi18739c42016-12-22 08:03:00 +0000245 BOOST_CHECK_EQUAL(sc.size(), 5);
Junxiao Shib5888d22014-05-26 07:35:22 -0700246
247 std::map<Name, Name> map; // namespace=>strategyName
Junxiao Shi18739c42016-12-22 08:03:00 +0000248 for (StrategyChoice::const_iterator it = sc.begin(); it != sc.end(); ++it) {
Junxiao Shi4cb74312016-12-25 20:48:47 +0000249 map[it->getPrefix()] = it->getStrategyInstanceName();
Junxiao Shib5888d22014-05-26 07:35:22 -0700250 }
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000251
252 BOOST_CHECK_EQUAL(map.at("/"), strategyNameP);
253 BOOST_CHECK_EQUAL(map.at("/A/B"), strategyNameQ);
254 BOOST_CHECK_EQUAL(map.at("/A/B/C"), strategyNameP);
255 BOOST_CHECK_EQUAL(map.at("/D"), strategyNameP);
256 BOOST_CHECK_EQUAL(map.at("/E"), strategyNameQ);
Junxiao Shib5888d22014-05-26 07:35:22 -0700257 BOOST_CHECK_EQUAL(map.size(), 5);
258}
259
Junxiao Shie349ea12014-03-12 01:32:42 -0700260class PStrategyInfo : public fw::StrategyInfo
261{
Junxiao Shi39ef2612014-11-29 20:35:19 -0700262public:
263 static constexpr int
264 getTypeId()
265 {
266 return 10;
267 }
Junxiao Shie349ea12014-03-12 01:32:42 -0700268};
269
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000270BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(ClearStrategyInfo, 2)
Junxiao Shie349ea12014-03-12 01:32:42 -0700271BOOST_AUTO_TEST_CASE(ClearStrategyInfo)
272{
Junxiao Shie349ea12014-03-12 01:32:42 -0700273 Measurements& measurements = forwarder.getMeasurements();
274
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000275 BOOST_CHECK(sc.insert("/", strategyNameP));
Junxiao Shie349ea12014-03-12 01:32:42 -0700276 // { '/'=>P }
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000277 measurements.get("/").insertStrategyInfo<PStrategyInfo>();
278 measurements.get("/A").insertStrategyInfo<PStrategyInfo>();
279 measurements.get("/A/B").insertStrategyInfo<PStrategyInfo>();
280 measurements.get("/A/C").insertStrategyInfo<PStrategyInfo>();
Junxiao Shie349ea12014-03-12 01:32:42 -0700281
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000282 BOOST_CHECK(sc.insert("/A/B", strategyNameP));
Junxiao Shie349ea12014-03-12 01:32:42 -0700283 // { '/'=>P, '/A/B'=>P }
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000284 BOOST_CHECK(measurements.get("/").getStrategyInfo<PStrategyInfo>() != nullptr);
285 BOOST_CHECK(measurements.get("/A").getStrategyInfo<PStrategyInfo>() != nullptr);
286 BOOST_CHECK(measurements.get("/A/B").getStrategyInfo<PStrategyInfo>() != nullptr); // expected failure
287 BOOST_CHECK(measurements.get("/A/C").getStrategyInfo<PStrategyInfo>() != nullptr);
Junxiao Shie349ea12014-03-12 01:32:42 -0700288
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000289 BOOST_CHECK(sc.insert("/A", strategyNameQ));
Junxiao Shie349ea12014-03-12 01:32:42 -0700290 // { '/'=>P, '/A/B'=>P, '/A'=>Q }
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000291 BOOST_CHECK(measurements.get("/").getStrategyInfo<PStrategyInfo>() != nullptr);
292 BOOST_CHECK(measurements.get("/A").getStrategyInfo<PStrategyInfo>() == nullptr);
293 BOOST_CHECK(measurements.get("/A/B").getStrategyInfo<PStrategyInfo>() != nullptr); // expected failure
294 BOOST_CHECK(measurements.get("/A/C").getStrategyInfo<PStrategyInfo>() == nullptr);
Junxiao Shie349ea12014-03-12 01:32:42 -0700295
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000296 sc.erase("/A/B");
Junxiao Shie349ea12014-03-12 01:32:42 -0700297 // { '/'=>P, '/A'=>Q }
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000298 BOOST_CHECK(measurements.get("/").getStrategyInfo<PStrategyInfo>() != nullptr);
299 BOOST_CHECK(measurements.get("/A").getStrategyInfo<PStrategyInfo>() == nullptr);
300 BOOST_CHECK(measurements.get("/A/B").getStrategyInfo<PStrategyInfo>() == nullptr);
301 BOOST_CHECK(measurements.get("/A/C").getStrategyInfo<PStrategyInfo>() == nullptr);
Junxiao Shie93d6a32014-09-07 16:13:22 -0700302}
303
Junxiao Shi4370fde2016-02-24 12:20:46 -0700304BOOST_AUTO_TEST_SUITE_END() // TestStrategyChoice
305BOOST_AUTO_TEST_SUITE_END() // Table
Junxiao Shibb5105f2014-03-03 12:06:45 -0700306
307} // namespace tests
308} // namespace nfd