blob: 469cb1a389f492b94200becb3ab4084899359cfb [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"
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070027#include "tests/daemon/fw/dummy-strategy.hpp"
Junxiao Shibb5105f2014-03-03 12:06:45 -070028
29#include "tests/test-common.hpp"
30
31namespace nfd {
32namespace tests {
33
Junxiao Shi4370fde2016-02-24 12:20:46 -070034BOOST_AUTO_TEST_SUITE(Table)
35BOOST_FIXTURE_TEST_SUITE(TestStrategyChoice, BaseFixture)
Junxiao Shibb5105f2014-03-03 12:06:45 -070036
37using fw::Strategy;
38
Steve DiBenedetto77c87512014-10-06 14:18:22 -060039BOOST_AUTO_TEST_CASE(Get)
40{
41 Forwarder forwarder;
42 Name nameP("ndn:/strategy/P");
43 shared_ptr<Strategy> strategyP = make_shared<DummyStrategy>(ref(forwarder), nameP);
44
45 StrategyChoice& table = forwarder.getStrategyChoice();
46
47 // install
48 BOOST_CHECK_EQUAL(table.install(strategyP), true);
49
50 BOOST_CHECK(table.insert("ndn:/", nameP));
51 // { '/'=>P }
52
Junxiao Shi838c4f12014-11-03 18:55:24 -070053 auto getRoot = table.get("ndn:/");
54 BOOST_CHECK_EQUAL(getRoot.first, true);
55 BOOST_CHECK_EQUAL(getRoot.second, nameP);
56
57 auto getA = table.get("ndn:/A");
58 BOOST_CHECK_EQUAL(getA.first, false);
Steve DiBenedetto77c87512014-10-06 14:18:22 -060059}
60
Junxiao Shi4370fde2016-02-24 12:20:46 -070061BOOST_AUTO_TEST_CASE(FindEffectiveStrategy)
Junxiao Shibb5105f2014-03-03 12:06:45 -070062{
63 Forwarder forwarder;
64 Name nameP("ndn:/strategy/P");
65 Name nameQ("ndn:/strategy/Q");
66 Name nameZ("ndn:/strategy/Z");
Alexander Afanasyevf6980282014-05-13 18:28:40 -070067 shared_ptr<Strategy> strategyP = make_shared<DummyStrategy>(ref(forwarder), nameP);
68 shared_ptr<Strategy> strategyQ = make_shared<DummyStrategy>(ref(forwarder), nameQ);
Junxiao Shibb5105f2014-03-03 12:06:45 -070069
70 StrategyChoice& table = forwarder.getStrategyChoice();
71
72 // install
73 BOOST_CHECK_EQUAL(table.install(strategyP), true);
74 BOOST_CHECK_EQUAL(table.install(strategyQ), true);
75 BOOST_CHECK_EQUAL(table.install(strategyQ), false);
76
Junxiao Shie349ea12014-03-12 01:32:42 -070077 BOOST_CHECK(table.insert("ndn:/", nameP));
Junxiao Shibb5105f2014-03-03 12:06:45 -070078 // { '/'=>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
Junxiao Shie349ea12014-03-12 01:32:42 -070084 BOOST_CHECK(table.insert("ndn:/A/B", nameP));
Junxiao Shibb5105f2014-03-03 12:06:45 -070085 // { '/'=>P, '/A/B'=>P }
86
87 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameP);
88 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameP);
89 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameP);
90 // same instance
91 BOOST_CHECK_EQUAL(&table.findEffectiveStrategy("ndn:/"), strategyP.get());
92 BOOST_CHECK_EQUAL(&table.findEffectiveStrategy("ndn:/A"), strategyP.get());
93 BOOST_CHECK_EQUAL(&table.findEffectiveStrategy("ndn:/A/B"), strategyP.get());
94
95 table.erase("ndn:/A"); // no effect
96 // { '/'=>P, '/A/B'=>P }
97
98 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameP);
99 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameP);
100 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameP);
101
Junxiao Shie349ea12014-03-12 01:32:42 -0700102 BOOST_CHECK(table.insert("ndn:/A", nameQ));
Junxiao Shibb5105f2014-03-03 12:06:45 -0700103 // { '/'=>P, '/A/B'=>P, '/A'=>Q }
104
105 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameP);
106 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameQ);
107 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameP);
108
109 table.erase("ndn:/A/B");
110 // { '/'=>P, '/A'=>Q }
111
112 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameP);
113 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameQ);
114 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameQ);
115
Junxiao Shie349ea12014-03-12 01:32:42 -0700116 BOOST_CHECK(!table.insert("ndn:/", nameZ)); // non existent strategy
Junxiao Shibb5105f2014-03-03 12:06:45 -0700117
Junxiao Shie349ea12014-03-12 01:32:42 -0700118 BOOST_CHECK(table.insert("ndn:/", nameQ));
119 BOOST_CHECK(table.insert("ndn:/A", nameP));
Junxiao Shibb5105f2014-03-03 12:06:45 -0700120 // { '/'=>Q, '/A'=>P }
121
122 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameQ);
123 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameP);
124 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameP);
125 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/D") .getName(), nameQ);
126}
127
Junxiao Shi4370fde2016-02-24 12:20:46 -0700128BOOST_AUTO_TEST_CASE(FindEffectiveStrategyWithPitEntry)
129{
130 Forwarder forwarder;
131 Name nameP("ndn:/strategy/P");
132 Name nameQ("ndn:/strategy/Q");
133 shared_ptr<Strategy> strategyP = make_shared<DummyStrategy>(ref(forwarder), nameP);
134 shared_ptr<Strategy> strategyQ = make_shared<DummyStrategy>(ref(forwarder), nameQ);
135 StrategyChoice& table = forwarder.getStrategyChoice();
136 table.install(strategyP);
137 table.install(strategyQ);
138
139 shared_ptr<Data> dataABC = makeData("/A/B/C");
140 Name fullName = dataABC->getFullName();
141
142 BOOST_CHECK(table.insert("/A", nameP));
143 BOOST_CHECK(table.insert(fullName, nameQ));
144
145 Pit& pit = forwarder.getPit();
146 shared_ptr<Interest> interestAB = makeInterest("/A/B");
147 shared_ptr<pit::Entry> pitAB = pit.insert(*interestAB).first;
148 shared_ptr<Interest> interestFull = makeInterest(fullName);
149 shared_ptr<pit::Entry> pitFull = pit.insert(*interestFull).first;
150
151 BOOST_CHECK_EQUAL(table.findEffectiveStrategy(*pitAB).getName(), nameP);
152 BOOST_CHECK_EQUAL(table.findEffectiveStrategy(*pitFull).getName(), nameQ);
153}
154
155BOOST_AUTO_TEST_CASE(FindEffectiveStrategyWithMeasurementsEntry)
156{
157 Forwarder forwarder;
158 Name nameP("ndn:/strategy/P");
159 Name nameQ("ndn:/strategy/Q");
160 shared_ptr<Strategy> strategyP = make_shared<DummyStrategy>(ref(forwarder), nameP);
161 shared_ptr<Strategy> strategyQ = make_shared<DummyStrategy>(ref(forwarder), nameQ);
162 StrategyChoice& table = forwarder.getStrategyChoice();
163 table.install(strategyP);
164 table.install(strategyQ);
165
166 BOOST_CHECK(table.insert("/A", nameP));
167 BOOST_CHECK(table.insert("/A/B/C", nameQ));
168
169 Measurements& measurements = forwarder.getMeasurements();
170 shared_ptr<measurements::Entry> mAB = measurements.get("/A/B");
171 shared_ptr<measurements::Entry> mABCD = measurements.get("/A/B/C/D");
172
173 BOOST_CHECK_EQUAL(table.findEffectiveStrategy(*mAB).getName(), nameP);
174 BOOST_CHECK_EQUAL(table.findEffectiveStrategy(*mABCD).getName(), nameQ);
175}
176
Junxiao Shib5888d22014-05-26 07:35:22 -0700177//XXX BOOST_CONCEPT_ASSERT((ForwardIterator<std::vector<int>::iterator>))
178// is also failing. There might be a problem with ForwardIterator concept checking.
179//BOOST_CONCEPT_ASSERT((ForwardIterator<StrategyChoice::const_iterator>));
180
181BOOST_AUTO_TEST_CASE(Enumerate)
182{
Junxiao Shib5888d22014-05-26 07:35:22 -0700183 Forwarder forwarder;
184 Name nameP("ndn:/strategy/P");
185 Name nameQ("ndn:/strategy/Q");
186 shared_ptr<Strategy> strategyP = make_shared<DummyStrategy>(ref(forwarder), nameP);
187 shared_ptr<Strategy> strategyQ = make_shared<DummyStrategy>(ref(forwarder), nameQ);
188
189 StrategyChoice& table = forwarder.getStrategyChoice();
190 table.install(strategyP);
191 table.install(strategyQ);
192
193 table.insert("ndn:/", nameP);
194 table.insert("ndn:/A/B", nameQ);
195 table.insert("ndn:/A/B/C", nameP);
196 table.insert("ndn:/D", nameP);
197 table.insert("ndn:/E", nameQ);
198
199 BOOST_CHECK_EQUAL(table.size(), 5);
200
201 std::map<Name, Name> map; // namespace=>strategyName
202 for (StrategyChoice::const_iterator it = table.begin(); it != table.end(); ++it) {
203 map[it->getPrefix()] = it->getStrategyName();
204 }
205 BOOST_CHECK_EQUAL(map.size(), 5);
206 BOOST_CHECK_EQUAL(map["ndn:/"], nameP);
207 BOOST_CHECK_EQUAL(map["ndn:/A/B"], nameQ);
208 BOOST_CHECK_EQUAL(map["ndn:/A/B/C"], nameP);
209 BOOST_CHECK_EQUAL(map["ndn:/D"], nameP);
210 BOOST_CHECK_EQUAL(map["ndn:/E"], nameQ);
211 BOOST_CHECK_EQUAL(map.size(), 5);
212}
213
Junxiao Shie349ea12014-03-12 01:32:42 -0700214class PStrategyInfo : public fw::StrategyInfo
215{
Junxiao Shi39ef2612014-11-29 20:35:19 -0700216public:
217 static constexpr int
218 getTypeId()
219 {
220 return 10;
221 }
Junxiao Shie349ea12014-03-12 01:32:42 -0700222};
223
224BOOST_AUTO_TEST_CASE(ClearStrategyInfo)
225{
226 Forwarder forwarder;
227 Name nameP("ndn:/strategy/P");
228 Name nameQ("ndn:/strategy/Q");
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700229 shared_ptr<Strategy> strategyP = make_shared<DummyStrategy>(ref(forwarder), nameP);
230 shared_ptr<Strategy> strategyQ = make_shared<DummyStrategy>(ref(forwarder), nameQ);
Junxiao Shie349ea12014-03-12 01:32:42 -0700231
232 StrategyChoice& table = forwarder.getStrategyChoice();
233 Measurements& measurements = forwarder.getMeasurements();
234
235 // install
236 table.install(strategyP);
237 table.install(strategyQ);
238
239 BOOST_CHECK(table.insert("ndn:/", nameP));
240 // { '/'=>P }
241 measurements.get("ndn:/") ->getOrCreateStrategyInfo<PStrategyInfo>();
242 measurements.get("ndn:/A") ->getOrCreateStrategyInfo<PStrategyInfo>();
243 measurements.get("ndn:/A/B") ->getOrCreateStrategyInfo<PStrategyInfo>();
244 measurements.get("ndn:/A/C") ->getOrCreateStrategyInfo<PStrategyInfo>();
245
246 BOOST_CHECK(table.insert("ndn:/A/B", nameP));
247 // { '/'=>P, '/A/B'=>P }
248 BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/") ->getStrategyInfo<PStrategyInfo>()));
249 BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/A") ->getStrategyInfo<PStrategyInfo>()));
250 BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/A/B")->getStrategyInfo<PStrategyInfo>()));
251 BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/A/C")->getStrategyInfo<PStrategyInfo>()));
252
253 BOOST_CHECK(table.insert("ndn:/A", nameQ));
254 // { '/'=>P, '/A/B'=>P, '/A'=>Q }
255 BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/") ->getStrategyInfo<PStrategyInfo>()));
256 BOOST_CHECK(!static_cast<bool>(measurements.get("ndn:/A") ->getStrategyInfo<PStrategyInfo>()));
257 BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/A/B")->getStrategyInfo<PStrategyInfo>()));
258 BOOST_CHECK(!static_cast<bool>(measurements.get("ndn:/A/C")->getStrategyInfo<PStrategyInfo>()));
259
260 table.erase("ndn:/A/B");
261 // { '/'=>P, '/A'=>Q }
262 BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/") ->getStrategyInfo<PStrategyInfo>()));
263 BOOST_CHECK(!static_cast<bool>(measurements.get("ndn:/A") ->getStrategyInfo<PStrategyInfo>()));
264 BOOST_CHECK(!static_cast<bool>(measurements.get("ndn:/A/B")->getStrategyInfo<PStrategyInfo>()));
265 BOOST_CHECK(!static_cast<bool>(measurements.get("ndn:/A/C")->getStrategyInfo<PStrategyInfo>()));
266}
267
Junxiao Shiee5a4442014-07-27 17:13:43 -0700268BOOST_AUTO_TEST_CASE(EraseNameTreeEntry)
269{
270 Forwarder forwarder;
271 NameTree& nameTree = forwarder.getNameTree();
272 StrategyChoice& table = forwarder.getStrategyChoice();
273
274 Name nameP("ndn:/strategy/P");
275 Name nameQ("ndn:/strategy/Q");
276 shared_ptr<Strategy> strategyP = make_shared<DummyStrategy>(ref(forwarder), nameP);
277 shared_ptr<Strategy> strategyQ = make_shared<DummyStrategy>(ref(forwarder), nameQ);
278 table.install(strategyP);
279 table.install(strategyQ);
280
281 table.insert("ndn:/", nameP);
282
283 size_t nNameTreeEntriesBefore = nameTree.size();
284
285 table.insert("ndn:/A/B", nameQ);
286 table.erase("ndn:/A/B");
287 BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore);
288}
289
Junxiao Shie93d6a32014-09-07 16:13:22 -0700290BOOST_AUTO_TEST_CASE(Versioning)
291{
292 Forwarder forwarder;
293 Name nameP("ndn:/strategy/P");
294 Name nameP1("ndn:/strategy/P/%FD%01");
295 Name nameP2("ndn:/strategy/P/%FD%02");
296 Name name3("ndn:/%FD%03");
297 Name name4("ndn:/%FD%04");
298 Name nameQ("ndn:/strategy/Q");
299 Name nameQ5("ndn:/strategy/Q/%FD%05");
300 shared_ptr<Strategy> strategyP1 = make_shared<DummyStrategy>(ref(forwarder), nameP1);
301 shared_ptr<Strategy> strategyP2 = make_shared<DummyStrategy>(ref(forwarder), nameP2);
302 shared_ptr<Strategy> strategy3 = make_shared<DummyStrategy>(ref(forwarder), name3);
303 shared_ptr<Strategy> strategy4 = make_shared<DummyStrategy>(ref(forwarder), name4);
304 shared_ptr<Strategy> strategyQ = make_shared<DummyStrategy>(ref(forwarder), nameQ);
305 shared_ptr<Strategy> strategyQ5 = make_shared<DummyStrategy>(ref(forwarder), nameQ5);
306
307 StrategyChoice& table = forwarder.getStrategyChoice();
308
309 // install
310 BOOST_CHECK_EQUAL(table.install(strategyP1), true);
311 BOOST_CHECK_EQUAL(table.install(strategyP1), false);
312 BOOST_CHECK_EQUAL(table.hasStrategy(nameP, false), true);
313 BOOST_CHECK_EQUAL(table.hasStrategy(nameP, true), false);
314 BOOST_CHECK_EQUAL(table.hasStrategy(nameP1, true), true);
315
316 BOOST_CHECK_EQUAL(table.install(strategyP2), true);
317 BOOST_CHECK_EQUAL(table.install(strategy3), true);
318 BOOST_CHECK_EQUAL(table.install(strategy4), true);
319 BOOST_CHECK_EQUAL(table.install(strategyQ), true);
320 BOOST_CHECK_EQUAL(table.install(strategyQ5), true);
321
322 BOOST_CHECK(table.insert("ndn:/", nameQ));
323 // exact match, { '/'=>Q }
324 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/").getName(), nameQ);
325
326 BOOST_CHECK(table.insert("ndn:/", nameQ));
327 BOOST_CHECK(table.insert("ndn:/", nameP));
328 // { '/'=>P2 }
329 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/").getName(), nameP2);
330
331 BOOST_CHECK(table.insert("ndn:/", nameQ));
332 BOOST_CHECK(table.insert("ndn:/", nameP1));
333 // { '/'=>P1 }
334 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/").getName(), nameP1);
335
336 BOOST_CHECK(table.insert("ndn:/", nameQ));
337 BOOST_CHECK(table.insert("ndn:/", nameP2));
338 // { '/'=>P2 }
339 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/").getName(), nameP2);
340
341 BOOST_CHECK(table.insert("ndn:/", nameQ));
342 BOOST_CHECK(! table.insert("ndn:/", "ndn:/strategy/A"));
343 // not installed
344 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/").getName(), nameQ);
345
346 BOOST_CHECK(table.insert("ndn:/", nameQ));
347 BOOST_CHECK(! table.insert("ndn:/", "ndn:/strategy/Z"));
348 // not installed
349 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/").getName(), nameQ);
350
351 BOOST_CHECK(table.insert("ndn:/", nameP1));
352 BOOST_CHECK(table.insert("ndn:/", "ndn:/"));
353 // match one component longer only, { '/'=>4 }
354 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/").getName(), name4);
355}
356
Junxiao Shi4370fde2016-02-24 12:20:46 -0700357BOOST_AUTO_TEST_SUITE_END() // TestStrategyChoice
358BOOST_AUTO_TEST_SUITE_END() // Table
Junxiao Shibb5105f2014-03-03 12:06:45 -0700359
360} // namespace tests
361} // namespace nfd