blob: fae7b49315eea95319bfd48611fac3c5c370df47 [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 Shi18739c42016-12-22 08:03:00 +000077BOOST_AUTO_TEST_CASE(Parameters)
78{
Junxiao Shi18739c42016-12-22 08:03:00 +000079 // no parameters
Junxiao Shi0e4a1f12016-12-24 02:39:01 +000080 BOOST_CHECK_EQUAL(this->insertAndGet("/A", strategyNameP), strategyNameP);
Junxiao Shi18739c42016-12-22 08:03:00 +000081
82 // one parameter
Junxiao Shi0e4a1f12016-12-24 02:39:01 +000083 Name oneParamName = Name(strategyNameP).append("param");
Junxiao Shi18739c42016-12-22 08:03:00 +000084 BOOST_CHECK_EQUAL(this->insertAndGet("/B", oneParamName), oneParamName);
85
86 // two parameters
Junxiao Shi0e4a1f12016-12-24 02:39:01 +000087 Name twoParamName = Name(strategyNameP).append("x").append("y");
Junxiao Shi18739c42016-12-22 08:03:00 +000088 BOOST_CHECK_EQUAL(this->insertAndGet("/C", twoParamName), twoParamName);
89
90 // parameter without version is disallowed
Junxiao Shi0e4a1f12016-12-24 02:39:01 +000091 Name oneParamUnversioned = strategyNameP.getPrefix(-1).append("param");
Junxiao Shi18739c42016-12-22 08:03:00 +000092 BOOST_CHECK_EQUAL(sc.insert("/D", oneParamUnversioned), false);
93}
94
Steve DiBenedetto77c87512014-10-06 14:18:22 -060095BOOST_AUTO_TEST_CASE(Get)
96{
Junxiao Shi0e4a1f12016-12-24 02:39:01 +000097 BOOST_CHECK(sc.insert("/", strategyNameP));
Steve DiBenedetto77c87512014-10-06 14:18:22 -060098 // { '/'=>P }
99
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000100 auto getRoot = sc.get("/");
Junxiao Shi838c4f12014-11-03 18:55:24 -0700101 BOOST_CHECK_EQUAL(getRoot.first, true);
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000102 BOOST_CHECK_EQUAL(getRoot.second, strategyNameP);
Junxiao Shi838c4f12014-11-03 18:55:24 -0700103
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000104 auto getA = sc.get("/A");
Junxiao Shi838c4f12014-11-03 18:55:24 -0700105 BOOST_CHECK_EQUAL(getA.first, false);
Steve DiBenedetto77c87512014-10-06 14:18:22 -0600106}
107
Junxiao Shi4370fde2016-02-24 12:20:46 -0700108BOOST_AUTO_TEST_CASE(FindEffectiveStrategy)
Junxiao Shibb5105f2014-03-03 12:06:45 -0700109{
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000110 const Name strategyNameZ("/strategy-choice-Z/%FD%00"); // unregistered strategyName
Junxiao Shibb5105f2014-03-03 12:06:45 -0700111
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000112 BOOST_CHECK(sc.insert("/", strategyNameP));
Junxiao Shibb5105f2014-03-03 12:06:45 -0700113 // { '/'=>P }
114
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000115 BOOST_CHECK_EQUAL(this->findInstanceName("/"), strategyNameP);
116 BOOST_CHECK_EQUAL(this->findInstanceName("/A"), strategyNameP);
117 BOOST_CHECK_EQUAL(this->findInstanceName("/A/B"), strategyNameP);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700118
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000119 BOOST_CHECK(sc.insert("/A/B", strategyNameP));
Junxiao Shibb5105f2014-03-03 12:06:45 -0700120 // { '/'=>P, '/A/B'=>P }
121
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000122 BOOST_CHECK_EQUAL(this->findInstanceName("/"), strategyNameP);
123 BOOST_CHECK_EQUAL(this->findInstanceName("/A"), strategyNameP);
124 BOOST_CHECK_EQUAL(this->findInstanceName("/A/B"), strategyNameP);
125 // same entry, same instance
126 BOOST_CHECK_EQUAL(&sc.findEffectiveStrategy("/"), &sc.findEffectiveStrategy("/A"));
127 // different entries, distinct instances
128 BOOST_CHECK_NE(&sc.findEffectiveStrategy("/"), &sc.findEffectiveStrategy("/A/B"));
Junxiao Shibb5105f2014-03-03 12:06:45 -0700129
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000130 sc.erase("/A"); // no effect
Junxiao Shibb5105f2014-03-03 12:06:45 -0700131 // { '/'=>P, '/A/B'=>P }
132
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000133 BOOST_CHECK_EQUAL(this->findInstanceName("/"), strategyNameP);
134 BOOST_CHECK_EQUAL(this->findInstanceName("/A"), strategyNameP);
135 BOOST_CHECK_EQUAL(this->findInstanceName("/A/B"), strategyNameP);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700136
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000137 BOOST_CHECK(sc.insert("/A", strategyNameQ));
Junxiao Shibb5105f2014-03-03 12:06:45 -0700138 // { '/'=>P, '/A/B'=>P, '/A'=>Q }
139
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000140 BOOST_CHECK_EQUAL(this->findInstanceName("/"), strategyNameP);
141 BOOST_CHECK_EQUAL(this->findInstanceName("/A"), strategyNameQ);
142 BOOST_CHECK_EQUAL(this->findInstanceName("/A/B"), strategyNameP);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700143
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000144 sc.erase("/A/B");
Junxiao Shibb5105f2014-03-03 12:06:45 -0700145 // { '/'=>P, '/A'=>Q }
146
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000147 BOOST_CHECK_EQUAL(this->findInstanceName("/"), strategyNameP);
148 BOOST_CHECK_EQUAL(this->findInstanceName("/A"), strategyNameQ);
149 BOOST_CHECK_EQUAL(this->findInstanceName("/A/B"), strategyNameQ);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700150
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000151 BOOST_CHECK(!sc.insert("/", strategyNameZ)); // non existent strategy
Junxiao Shibb5105f2014-03-03 12:06:45 -0700152
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000153 BOOST_CHECK(sc.insert("/", strategyNameQ));
154 BOOST_CHECK(sc.insert("/A", strategyNameP));
Junxiao Shibb5105f2014-03-03 12:06:45 -0700155 // { '/'=>Q, '/A'=>P }
156
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000157 BOOST_CHECK_EQUAL(this->findInstanceName("/"), strategyNameQ);
158 BOOST_CHECK_EQUAL(this->findInstanceName("/A"), strategyNameP);
159 BOOST_CHECK_EQUAL(this->findInstanceName("/A/B"), strategyNameP);
160 BOOST_CHECK_EQUAL(this->findInstanceName("/D"), strategyNameQ);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700161}
162
Junxiao Shi4370fde2016-02-24 12:20:46 -0700163BOOST_AUTO_TEST_CASE(FindEffectiveStrategyWithPitEntry)
164{
Junxiao Shi4370fde2016-02-24 12:20:46 -0700165 shared_ptr<Data> dataABC = makeData("/A/B/C");
166 Name fullName = dataABC->getFullName();
167
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000168 BOOST_CHECK(sc.insert("/A", strategyNameP));
169 BOOST_CHECK(sc.insert(fullName, strategyNameQ));
Junxiao Shi4370fde2016-02-24 12:20:46 -0700170
171 Pit& pit = forwarder.getPit();
172 shared_ptr<Interest> interestAB = makeInterest("/A/B");
173 shared_ptr<pit::Entry> pitAB = pit.insert(*interestAB).first;
174 shared_ptr<Interest> interestFull = makeInterest(fullName);
175 shared_ptr<pit::Entry> pitFull = pit.insert(*interestFull).first;
176
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000177 BOOST_CHECK_EQUAL(this->findInstanceName(*pitAB), strategyNameP);
178 BOOST_CHECK_EQUAL(this->findInstanceName(*pitFull), strategyNameQ);
Junxiao Shi4370fde2016-02-24 12:20:46 -0700179}
180
181BOOST_AUTO_TEST_CASE(FindEffectiveStrategyWithMeasurementsEntry)
182{
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000183 BOOST_CHECK(sc.insert("/A", strategyNameP));
184 BOOST_CHECK(sc.insert("/A/B/C", strategyNameQ));
Junxiao Shi4370fde2016-02-24 12:20:46 -0700185
186 Measurements& measurements = forwarder.getMeasurements();
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000187 measurements::Entry& mAB = measurements.get("/A/B");
188 measurements::Entry& mABCD = measurements.get("/A/B/C/D");
Junxiao Shi4370fde2016-02-24 12:20:46 -0700189
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000190 BOOST_CHECK_EQUAL(this->findInstanceName(mAB), strategyNameP);
191 BOOST_CHECK_EQUAL(this->findInstanceName(mABCD), strategyNameQ);
192}
193
194BOOST_AUTO_TEST_CASE(Erase)
195{
196 NameTree& nameTree = forwarder.getNameTree();
197
198 sc.insert("/", strategyNameP);
199
200 size_t nNameTreeEntriesBefore = nameTree.size();
201
202 sc.insert("/A/B", strategyNameQ);
203 sc.erase("/A/B");
204 BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore);
Junxiao Shi4370fde2016-02-24 12:20:46 -0700205}
206
Junxiao Shib5888d22014-05-26 07:35:22 -0700207//XXX BOOST_CONCEPT_ASSERT((ForwardIterator<std::vector<int>::iterator>))
208// is also failing. There might be a problem with ForwardIterator concept checking.
209//BOOST_CONCEPT_ASSERT((ForwardIterator<StrategyChoice::const_iterator>));
210
211BOOST_AUTO_TEST_CASE(Enumerate)
212{
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000213 sc.insert("/", strategyNameP);
214 sc.insert("/A/B", strategyNameQ);
215 sc.insert("/A/B/C", strategyNameP);
216 sc.insert("/D", strategyNameP);
217 sc.insert("/E", strategyNameQ);
Junxiao Shib5888d22014-05-26 07:35:22 -0700218
Junxiao Shi18739c42016-12-22 08:03:00 +0000219 BOOST_CHECK_EQUAL(sc.size(), 5);
Junxiao Shib5888d22014-05-26 07:35:22 -0700220
221 std::map<Name, Name> map; // namespace=>strategyName
Junxiao Shi18739c42016-12-22 08:03:00 +0000222 for (StrategyChoice::const_iterator it = sc.begin(); it != sc.end(); ++it) {
Junxiao Shib5888d22014-05-26 07:35:22 -0700223 map[it->getPrefix()] = it->getStrategyName();
224 }
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000225
226 BOOST_CHECK_EQUAL(map.at("/"), strategyNameP);
227 BOOST_CHECK_EQUAL(map.at("/A/B"), strategyNameQ);
228 BOOST_CHECK_EQUAL(map.at("/A/B/C"), strategyNameP);
229 BOOST_CHECK_EQUAL(map.at("/D"), strategyNameP);
230 BOOST_CHECK_EQUAL(map.at("/E"), strategyNameQ);
Junxiao Shib5888d22014-05-26 07:35:22 -0700231 BOOST_CHECK_EQUAL(map.size(), 5);
232}
233
Junxiao Shie349ea12014-03-12 01:32:42 -0700234class PStrategyInfo : public fw::StrategyInfo
235{
Junxiao Shi39ef2612014-11-29 20:35:19 -0700236public:
237 static constexpr int
238 getTypeId()
239 {
240 return 10;
241 }
Junxiao Shie349ea12014-03-12 01:32:42 -0700242};
243
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000244BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(ClearStrategyInfo, 2)
Junxiao Shie349ea12014-03-12 01:32:42 -0700245BOOST_AUTO_TEST_CASE(ClearStrategyInfo)
246{
Junxiao Shie349ea12014-03-12 01:32:42 -0700247 Measurements& measurements = forwarder.getMeasurements();
248
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000249 BOOST_CHECK(sc.insert("/", strategyNameP));
Junxiao Shie349ea12014-03-12 01:32:42 -0700250 // { '/'=>P }
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000251 measurements.get("/").insertStrategyInfo<PStrategyInfo>();
252 measurements.get("/A").insertStrategyInfo<PStrategyInfo>();
253 measurements.get("/A/B").insertStrategyInfo<PStrategyInfo>();
254 measurements.get("/A/C").insertStrategyInfo<PStrategyInfo>();
Junxiao Shie349ea12014-03-12 01:32:42 -0700255
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000256 BOOST_CHECK(sc.insert("/A/B", strategyNameP));
Junxiao Shie349ea12014-03-12 01:32:42 -0700257 // { '/'=>P, '/A/B'=>P }
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000258 BOOST_CHECK(measurements.get("/").getStrategyInfo<PStrategyInfo>() != nullptr);
259 BOOST_CHECK(measurements.get("/A").getStrategyInfo<PStrategyInfo>() != nullptr);
260 BOOST_CHECK(measurements.get("/A/B").getStrategyInfo<PStrategyInfo>() != nullptr); // expected failure
261 BOOST_CHECK(measurements.get("/A/C").getStrategyInfo<PStrategyInfo>() != nullptr);
Junxiao Shie349ea12014-03-12 01:32:42 -0700262
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000263 BOOST_CHECK(sc.insert("/A", strategyNameQ));
Junxiao Shie349ea12014-03-12 01:32:42 -0700264 // { '/'=>P, '/A/B'=>P, '/A'=>Q }
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000265 BOOST_CHECK(measurements.get("/").getStrategyInfo<PStrategyInfo>() != nullptr);
266 BOOST_CHECK(measurements.get("/A").getStrategyInfo<PStrategyInfo>() == nullptr);
267 BOOST_CHECK(measurements.get("/A/B").getStrategyInfo<PStrategyInfo>() != nullptr); // expected failure
268 BOOST_CHECK(measurements.get("/A/C").getStrategyInfo<PStrategyInfo>() == nullptr);
Junxiao Shie349ea12014-03-12 01:32:42 -0700269
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000270 sc.erase("/A/B");
Junxiao Shie349ea12014-03-12 01:32:42 -0700271 // { '/'=>P, '/A'=>Q }
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000272 BOOST_CHECK(measurements.get("/").getStrategyInfo<PStrategyInfo>() != nullptr);
273 BOOST_CHECK(measurements.get("/A").getStrategyInfo<PStrategyInfo>() == nullptr);
274 BOOST_CHECK(measurements.get("/A/B").getStrategyInfo<PStrategyInfo>() == nullptr);
275 BOOST_CHECK(measurements.get("/A/C").getStrategyInfo<PStrategyInfo>() == nullptr);
Junxiao Shie93d6a32014-09-07 16:13:22 -0700276}
277
Junxiao Shi4370fde2016-02-24 12:20:46 -0700278BOOST_AUTO_TEST_SUITE_END() // TestStrategyChoice
279BOOST_AUTO_TEST_SUITE_END() // Table
Junxiao Shibb5105f2014-03-03 12:06:45 -0700280
281} // namespace tests
282} // namespace nfd