blob: 5897981abaee7b37432bd442c5942f253945d036 [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"
30#include "../fw/install-strategy.hpp"
Junxiao Shibb5105f2014-03-03 12:06:45 -070031
32namespace nfd {
33namespace tests {
34
Junxiao Shi4370fde2016-02-24 12:20:46 -070035BOOST_AUTO_TEST_SUITE(Table)
36BOOST_FIXTURE_TEST_SUITE(TestStrategyChoice, BaseFixture)
Junxiao Shibb5105f2014-03-03 12:06:45 -070037
38using fw::Strategy;
39
Steve DiBenedetto77c87512014-10-06 14:18:22 -060040BOOST_AUTO_TEST_CASE(Get)
41{
42 Forwarder forwarder;
43 Name nameP("ndn:/strategy/P");
Junxiao Shia49a1ab2016-07-15 18:24:36 +000044 install<DummyStrategy>(forwarder, nameP);
Steve DiBenedetto77c87512014-10-06 14:18:22 -060045
46 StrategyChoice& table = forwarder.getStrategyChoice();
47
Steve DiBenedetto77c87512014-10-06 14:18:22 -060048 BOOST_CHECK(table.insert("ndn:/", nameP));
49 // { '/'=>P }
50
Junxiao Shi838c4f12014-11-03 18:55:24 -070051 auto getRoot = table.get("ndn:/");
52 BOOST_CHECK_EQUAL(getRoot.first, true);
53 BOOST_CHECK_EQUAL(getRoot.second, nameP);
54
55 auto getA = table.get("ndn:/A");
56 BOOST_CHECK_EQUAL(getA.first, false);
Steve DiBenedetto77c87512014-10-06 14:18:22 -060057}
58
Junxiao Shi4370fde2016-02-24 12:20:46 -070059BOOST_AUTO_TEST_CASE(FindEffectiveStrategy)
Junxiao Shibb5105f2014-03-03 12:06:45 -070060{
61 Forwarder forwarder;
62 Name nameP("ndn:/strategy/P");
63 Name nameQ("ndn:/strategy/Q");
64 Name nameZ("ndn:/strategy/Z");
Junxiao Shia49a1ab2016-07-15 18:24:36 +000065 install<DummyStrategy>(forwarder, nameP);
66 install<DummyStrategy>(forwarder, nameQ);
Junxiao Shibb5105f2014-03-03 12:06:45 -070067
68 StrategyChoice& table = forwarder.getStrategyChoice();
69
Junxiao Shie349ea12014-03-12 01:32:42 -070070 BOOST_CHECK(table.insert("ndn:/", nameP));
Junxiao Shibb5105f2014-03-03 12:06:45 -070071 // { '/'=>P }
72
73 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameP);
74 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameP);
75 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameP);
76
Junxiao Shie349ea12014-03-12 01:32:42 -070077 BOOST_CHECK(table.insert("ndn:/A/B", nameP));
Junxiao Shibb5105f2014-03-03 12:06:45 -070078 // { '/'=>P, '/A/B'=>P }
79
80 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameP);
81 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameP);
82 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameP);
83 // same instance
Junxiao Shia49a1ab2016-07-15 18:24:36 +000084 BOOST_CHECK_EQUAL(&table.findEffectiveStrategy("ndn:/"),
85 &table.findEffectiveStrategy("ndn:/A/B"));
Junxiao Shibb5105f2014-03-03 12:06:45 -070086
87 table.erase("ndn:/A"); // no effect
88 // { '/'=>P, '/A/B'=>P }
89
90 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameP);
91 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameP);
92 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameP);
93
Junxiao Shie349ea12014-03-12 01:32:42 -070094 BOOST_CHECK(table.insert("ndn:/A", nameQ));
Junxiao Shibb5105f2014-03-03 12:06:45 -070095 // { '/'=>P, '/A/B'=>P, '/A'=>Q }
96
97 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameP);
98 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameQ);
99 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameP);
100
101 table.erase("ndn:/A/B");
102 // { '/'=>P, '/A'=>Q }
103
104 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameP);
105 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameQ);
106 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameQ);
107
Junxiao Shie349ea12014-03-12 01:32:42 -0700108 BOOST_CHECK(!table.insert("ndn:/", nameZ)); // non existent strategy
Junxiao Shibb5105f2014-03-03 12:06:45 -0700109
Junxiao Shie349ea12014-03-12 01:32:42 -0700110 BOOST_CHECK(table.insert("ndn:/", nameQ));
111 BOOST_CHECK(table.insert("ndn:/A", nameP));
Junxiao Shibb5105f2014-03-03 12:06:45 -0700112 // { '/'=>Q, '/A'=>P }
113
114 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameQ);
115 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameP);
116 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameP);
117 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/D") .getName(), nameQ);
118}
119
Junxiao Shi4370fde2016-02-24 12:20:46 -0700120BOOST_AUTO_TEST_CASE(FindEffectiveStrategyWithPitEntry)
121{
122 Forwarder forwarder;
123 Name nameP("ndn:/strategy/P");
124 Name nameQ("ndn:/strategy/Q");
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000125 install<DummyStrategy>(forwarder, nameP);
126 install<DummyStrategy>(forwarder, nameQ);
Junxiao Shi4370fde2016-02-24 12:20:46 -0700127
128 shared_ptr<Data> dataABC = makeData("/A/B/C");
129 Name fullName = dataABC->getFullName();
130
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000131 StrategyChoice& table = forwarder.getStrategyChoice();
132
Junxiao Shi4370fde2016-02-24 12:20:46 -0700133 BOOST_CHECK(table.insert("/A", nameP));
134 BOOST_CHECK(table.insert(fullName, nameQ));
135
136 Pit& pit = forwarder.getPit();
137 shared_ptr<Interest> interestAB = makeInterest("/A/B");
138 shared_ptr<pit::Entry> pitAB = pit.insert(*interestAB).first;
139 shared_ptr<Interest> interestFull = makeInterest(fullName);
140 shared_ptr<pit::Entry> pitFull = pit.insert(*interestFull).first;
141
142 BOOST_CHECK_EQUAL(table.findEffectiveStrategy(*pitAB).getName(), nameP);
143 BOOST_CHECK_EQUAL(table.findEffectiveStrategy(*pitFull).getName(), nameQ);
144}
145
146BOOST_AUTO_TEST_CASE(FindEffectiveStrategyWithMeasurementsEntry)
147{
148 Forwarder forwarder;
149 Name nameP("ndn:/strategy/P");
150 Name nameQ("ndn:/strategy/Q");
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000151 install<DummyStrategy>(forwarder, nameP);
152 install<DummyStrategy>(forwarder, nameQ);
153
Junxiao Shi4370fde2016-02-24 12:20:46 -0700154 StrategyChoice& table = forwarder.getStrategyChoice();
Junxiao Shi4370fde2016-02-24 12:20:46 -0700155
156 BOOST_CHECK(table.insert("/A", nameP));
157 BOOST_CHECK(table.insert("/A/B/C", nameQ));
158
159 Measurements& measurements = forwarder.getMeasurements();
160 shared_ptr<measurements::Entry> mAB = measurements.get("/A/B");
161 shared_ptr<measurements::Entry> mABCD = measurements.get("/A/B/C/D");
162
163 BOOST_CHECK_EQUAL(table.findEffectiveStrategy(*mAB).getName(), nameP);
164 BOOST_CHECK_EQUAL(table.findEffectiveStrategy(*mABCD).getName(), nameQ);
165}
166
Junxiao Shib5888d22014-05-26 07:35:22 -0700167//XXX BOOST_CONCEPT_ASSERT((ForwardIterator<std::vector<int>::iterator>))
168// is also failing. There might be a problem with ForwardIterator concept checking.
169//BOOST_CONCEPT_ASSERT((ForwardIterator<StrategyChoice::const_iterator>));
170
171BOOST_AUTO_TEST_CASE(Enumerate)
172{
Junxiao Shib5888d22014-05-26 07:35:22 -0700173 Forwarder forwarder;
174 Name nameP("ndn:/strategy/P");
175 Name nameQ("ndn:/strategy/Q");
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000176 install<DummyStrategy>(forwarder, nameP);
177 install<DummyStrategy>(forwarder, nameQ);
Junxiao Shib5888d22014-05-26 07:35:22 -0700178
179 StrategyChoice& table = forwarder.getStrategyChoice();
Junxiao Shib5888d22014-05-26 07:35:22 -0700180
181 table.insert("ndn:/", nameP);
182 table.insert("ndn:/A/B", nameQ);
183 table.insert("ndn:/A/B/C", nameP);
184 table.insert("ndn:/D", nameP);
185 table.insert("ndn:/E", nameQ);
186
187 BOOST_CHECK_EQUAL(table.size(), 5);
188
189 std::map<Name, Name> map; // namespace=>strategyName
190 for (StrategyChoice::const_iterator it = table.begin(); it != table.end(); ++it) {
191 map[it->getPrefix()] = it->getStrategyName();
192 }
193 BOOST_CHECK_EQUAL(map.size(), 5);
194 BOOST_CHECK_EQUAL(map["ndn:/"], nameP);
195 BOOST_CHECK_EQUAL(map["ndn:/A/B"], nameQ);
196 BOOST_CHECK_EQUAL(map["ndn:/A/B/C"], nameP);
197 BOOST_CHECK_EQUAL(map["ndn:/D"], nameP);
198 BOOST_CHECK_EQUAL(map["ndn:/E"], nameQ);
199 BOOST_CHECK_EQUAL(map.size(), 5);
200}
201
Junxiao Shie349ea12014-03-12 01:32:42 -0700202class PStrategyInfo : public fw::StrategyInfo
203{
Junxiao Shi39ef2612014-11-29 20:35:19 -0700204public:
205 static constexpr int
206 getTypeId()
207 {
208 return 10;
209 }
Junxiao Shie349ea12014-03-12 01:32:42 -0700210};
211
212BOOST_AUTO_TEST_CASE(ClearStrategyInfo)
213{
214 Forwarder forwarder;
215 Name nameP("ndn:/strategy/P");
216 Name nameQ("ndn:/strategy/Q");
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000217 install<DummyStrategy>(forwarder, nameP);
218 install<DummyStrategy>(forwarder, nameQ);
Junxiao Shie349ea12014-03-12 01:32:42 -0700219
220 StrategyChoice& table = forwarder.getStrategyChoice();
221 Measurements& measurements = forwarder.getMeasurements();
222
Junxiao Shie349ea12014-03-12 01:32:42 -0700223 BOOST_CHECK(table.insert("ndn:/", nameP));
224 // { '/'=>P }
225 measurements.get("ndn:/") ->getOrCreateStrategyInfo<PStrategyInfo>();
226 measurements.get("ndn:/A") ->getOrCreateStrategyInfo<PStrategyInfo>();
227 measurements.get("ndn:/A/B") ->getOrCreateStrategyInfo<PStrategyInfo>();
228 measurements.get("ndn:/A/C") ->getOrCreateStrategyInfo<PStrategyInfo>();
229
230 BOOST_CHECK(table.insert("ndn:/A/B", nameP));
231 // { '/'=>P, '/A/B'=>P }
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000232 BOOST_CHECK(measurements.get("ndn:/") ->getStrategyInfo<PStrategyInfo>() != nullptr);
233 BOOST_CHECK(measurements.get("ndn:/A") ->getStrategyInfo<PStrategyInfo>() != nullptr);
234 BOOST_CHECK(measurements.get("ndn:/A/B")->getStrategyInfo<PStrategyInfo>() != nullptr);
235 BOOST_CHECK(measurements.get("ndn:/A/C")->getStrategyInfo<PStrategyInfo>() != nullptr);
Junxiao Shie349ea12014-03-12 01:32:42 -0700236
237 BOOST_CHECK(table.insert("ndn:/A", nameQ));
238 // { '/'=>P, '/A/B'=>P, '/A'=>Q }
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000239 BOOST_CHECK(measurements.get("ndn:/") ->getStrategyInfo<PStrategyInfo>() != nullptr);
240 BOOST_CHECK(measurements.get("ndn:/A") ->getStrategyInfo<PStrategyInfo>() == nullptr);
241 BOOST_CHECK(measurements.get("ndn:/A/B")->getStrategyInfo<PStrategyInfo>() != nullptr);
242 BOOST_CHECK(measurements.get("ndn:/A/C")->getStrategyInfo<PStrategyInfo>() == nullptr);
Junxiao Shie349ea12014-03-12 01:32:42 -0700243
244 table.erase("ndn:/A/B");
245 // { '/'=>P, '/A'=>Q }
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000246 BOOST_CHECK(measurements.get("ndn:/") ->getStrategyInfo<PStrategyInfo>() != nullptr);
247 BOOST_CHECK(measurements.get("ndn:/A") ->getStrategyInfo<PStrategyInfo>() == nullptr);
248 BOOST_CHECK(measurements.get("ndn:/A/B")->getStrategyInfo<PStrategyInfo>() == nullptr);
249 BOOST_CHECK(measurements.get("ndn:/A/C")->getStrategyInfo<PStrategyInfo>() == nullptr);
Junxiao Shie349ea12014-03-12 01:32:42 -0700250}
251
Junxiao Shiee5a4442014-07-27 17:13:43 -0700252BOOST_AUTO_TEST_CASE(EraseNameTreeEntry)
253{
254 Forwarder forwarder;
Junxiao Shiee5a4442014-07-27 17:13:43 -0700255 Name nameP("ndn:/strategy/P");
256 Name nameQ("ndn:/strategy/Q");
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000257 install<DummyStrategy>(forwarder, nameP);
258 install<DummyStrategy>(forwarder, nameQ);
259
260 NameTree& nameTree = forwarder.getNameTree();
261 StrategyChoice& table = forwarder.getStrategyChoice();
Junxiao Shiee5a4442014-07-27 17:13:43 -0700262
263 table.insert("ndn:/", nameP);
264
265 size_t nNameTreeEntriesBefore = nameTree.size();
266
267 table.insert("ndn:/A/B", nameQ);
268 table.erase("ndn:/A/B");
269 BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore);
270}
271
Junxiao Shie93d6a32014-09-07 16:13:22 -0700272BOOST_AUTO_TEST_CASE(Versioning)
273{
274 Forwarder forwarder;
275 Name nameP("ndn:/strategy/P");
276 Name nameP1("ndn:/strategy/P/%FD%01");
277 Name nameP2("ndn:/strategy/P/%FD%02");
278 Name name3("ndn:/%FD%03");
279 Name name4("ndn:/%FD%04");
280 Name nameQ("ndn:/strategy/Q");
281 Name nameQ5("ndn:/strategy/Q/%FD%05");
Junxiao Shie93d6a32014-09-07 16:13:22 -0700282
283 StrategyChoice& table = forwarder.getStrategyChoice();
284
285 // install
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000286 auto strategyP1 = make_unique<DummyStrategy>(ref(forwarder), nameP1);
287 Strategy* instanceP1 = strategyP1.get();
288 auto strategyP1b = make_unique<DummyStrategy>(ref(forwarder), nameP1);
289 auto strategyP2 = make_unique<DummyStrategy>(ref(forwarder), nameP2);
290 auto strategy3 = make_unique<DummyStrategy>(ref(forwarder), name3);
291 auto strategy4 = make_unique<DummyStrategy>(ref(forwarder), name4);
292 auto strategyQ = make_unique<DummyStrategy>(ref(forwarder), nameQ);
293 auto strategyQ5 = make_unique<DummyStrategy>(ref(forwarder), nameQ5);
294
295 bool isInstalled = false;
296 Strategy* installed = nullptr;
297
298 std::tie(isInstalled, installed) = table.install(std::move(strategyP1));
299 BOOST_CHECK_EQUAL(isInstalled, true);
300 BOOST_CHECK_EQUAL(installed, instanceP1);
301 std::tie(isInstalled, installed) = table.install(std::move(strategyP1b));
302 BOOST_CHECK_EQUAL(isInstalled, false);
303 BOOST_CHECK_EQUAL(installed, instanceP1);
304
Junxiao Shie93d6a32014-09-07 16:13:22 -0700305 BOOST_CHECK_EQUAL(table.hasStrategy(nameP, false), true);
306 BOOST_CHECK_EQUAL(table.hasStrategy(nameP, true), false);
307 BOOST_CHECK_EQUAL(table.hasStrategy(nameP1, true), true);
308
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000309 BOOST_CHECK_EQUAL(table.install(std::move(strategyP2)).first, true);
310 BOOST_CHECK_EQUAL(table.install(std::move(strategy3)).first, true);
311 BOOST_CHECK_EQUAL(table.install(std::move(strategy4)).first, true);
312 BOOST_CHECK_EQUAL(table.install(std::move(strategyQ)).first, true);
313 BOOST_CHECK_EQUAL(table.install(std::move(strategyQ5)).first, true);
Junxiao Shie93d6a32014-09-07 16:13:22 -0700314
315 BOOST_CHECK(table.insert("ndn:/", nameQ));
316 // exact match, { '/'=>Q }
317 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/").getName(), nameQ);
318
319 BOOST_CHECK(table.insert("ndn:/", nameQ));
320 BOOST_CHECK(table.insert("ndn:/", nameP));
321 // { '/'=>P2 }
322 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/").getName(), nameP2);
323
324 BOOST_CHECK(table.insert("ndn:/", nameQ));
325 BOOST_CHECK(table.insert("ndn:/", nameP1));
326 // { '/'=>P1 }
327 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/").getName(), nameP1);
328
329 BOOST_CHECK(table.insert("ndn:/", nameQ));
330 BOOST_CHECK(table.insert("ndn:/", nameP2));
331 // { '/'=>P2 }
332 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/").getName(), nameP2);
333
334 BOOST_CHECK(table.insert("ndn:/", nameQ));
335 BOOST_CHECK(! table.insert("ndn:/", "ndn:/strategy/A"));
336 // not installed
337 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/").getName(), nameQ);
338
339 BOOST_CHECK(table.insert("ndn:/", nameQ));
340 BOOST_CHECK(! table.insert("ndn:/", "ndn:/strategy/Z"));
341 // not installed
342 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/").getName(), nameQ);
343
344 BOOST_CHECK(table.insert("ndn:/", nameP1));
345 BOOST_CHECK(table.insert("ndn:/", "ndn:/"));
346 // match one component longer only, { '/'=>4 }
347 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/").getName(), name4);
348}
349
Junxiao Shi4370fde2016-02-24 12:20:46 -0700350BOOST_AUTO_TEST_SUITE_END() // TestStrategyChoice
351BOOST_AUTO_TEST_SUITE_END() // Table
Junxiao Shibb5105f2014-03-03 12:06:45 -0700352
353} // namespace tests
354} // namespace nfd