blob: 494c76b8cc98c8c58c3de1523c71cb7d58bc591f [file] [log] [blame]
Junxiao Shibb5105f2014-03-03 12:06:45 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shib5888d22014-05-26 07:35:22 -07003 * Copyright (c) 2014, 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
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
34BOOST_FIXTURE_TEST_SUITE(TableStrategyChoice, BaseFixture)
35
36using fw::Strategy;
37
Steve DiBenedetto77c87512014-10-06 14:18:22 -060038BOOST_AUTO_TEST_CASE(Get)
39{
40 Forwarder forwarder;
41 Name nameP("ndn:/strategy/P");
42 shared_ptr<Strategy> strategyP = make_shared<DummyStrategy>(ref(forwarder), nameP);
43
44 StrategyChoice& table = forwarder.getStrategyChoice();
45
46 // install
47 BOOST_CHECK_EQUAL(table.install(strategyP), true);
48
49 BOOST_CHECK(table.insert("ndn:/", nameP));
50 // { '/'=>P }
51
Junxiao Shi838c4f12014-11-03 18:55:24 -070052 auto getRoot = table.get("ndn:/");
53 BOOST_CHECK_EQUAL(getRoot.first, true);
54 BOOST_CHECK_EQUAL(getRoot.second, nameP);
55
56 auto getA = table.get("ndn:/A");
57 BOOST_CHECK_EQUAL(getA.first, false);
Steve DiBenedetto77c87512014-10-06 14:18:22 -060058}
59
Junxiao Shibb5105f2014-03-03 12:06:45 -070060BOOST_AUTO_TEST_CASE(Effective)
61{
62 Forwarder forwarder;
63 Name nameP("ndn:/strategy/P");
64 Name nameQ("ndn:/strategy/Q");
65 Name nameZ("ndn:/strategy/Z");
Alexander Afanasyevf6980282014-05-13 18:28:40 -070066 shared_ptr<Strategy> strategyP = make_shared<DummyStrategy>(ref(forwarder), nameP);
67 shared_ptr<Strategy> strategyQ = make_shared<DummyStrategy>(ref(forwarder), nameQ);
Junxiao Shibb5105f2014-03-03 12:06:45 -070068
69 StrategyChoice& table = forwarder.getStrategyChoice();
70
71 // install
72 BOOST_CHECK_EQUAL(table.install(strategyP), true);
73 BOOST_CHECK_EQUAL(table.install(strategyQ), true);
74 BOOST_CHECK_EQUAL(table.install(strategyQ), false);
75
Junxiao Shie349ea12014-03-12 01:32:42 -070076 BOOST_CHECK(table.insert("ndn:/", nameP));
Junxiao Shibb5105f2014-03-03 12:06:45 -070077 // { '/'=>P }
78
79 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameP);
80 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameP);
81 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameP);
82
Junxiao Shie349ea12014-03-12 01:32:42 -070083 BOOST_CHECK(table.insert("ndn:/A/B", nameP));
Junxiao Shibb5105f2014-03-03 12:06:45 -070084 // { '/'=>P, '/A/B'=>P }
85
86 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameP);
87 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameP);
88 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameP);
89 // same instance
90 BOOST_CHECK_EQUAL(&table.findEffectiveStrategy("ndn:/"), strategyP.get());
91 BOOST_CHECK_EQUAL(&table.findEffectiveStrategy("ndn:/A"), strategyP.get());
92 BOOST_CHECK_EQUAL(&table.findEffectiveStrategy("ndn:/A/B"), strategyP.get());
93
94 table.erase("ndn:/A"); // no effect
95 // { '/'=>P, '/A/B'=>P }
96
97 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameP);
98 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameP);
99 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameP);
100
Junxiao Shie349ea12014-03-12 01:32:42 -0700101 BOOST_CHECK(table.insert("ndn:/A", nameQ));
Junxiao Shibb5105f2014-03-03 12:06:45 -0700102 // { '/'=>P, '/A/B'=>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(), nameP);
107
108 table.erase("ndn:/A/B");
109 // { '/'=>P, '/A'=>Q }
110
111 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameP);
112 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameQ);
113 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameQ);
114
Junxiao Shie349ea12014-03-12 01:32:42 -0700115 BOOST_CHECK(!table.insert("ndn:/", nameZ)); // non existent strategy
Junxiao Shibb5105f2014-03-03 12:06:45 -0700116
Junxiao Shie349ea12014-03-12 01:32:42 -0700117 BOOST_CHECK(table.insert("ndn:/", nameQ));
118 BOOST_CHECK(table.insert("ndn:/A", nameP));
Junxiao Shibb5105f2014-03-03 12:06:45 -0700119 // { '/'=>Q, '/A'=>P }
120
121 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameQ);
122 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameP);
123 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameP);
124 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/D") .getName(), nameQ);
125}
126
Junxiao Shib5888d22014-05-26 07:35:22 -0700127//XXX BOOST_CONCEPT_ASSERT((ForwardIterator<std::vector<int>::iterator>))
128// is also failing. There might be a problem with ForwardIterator concept checking.
129//BOOST_CONCEPT_ASSERT((ForwardIterator<StrategyChoice::const_iterator>));
130
131BOOST_AUTO_TEST_CASE(Enumerate)
132{
133
134 Forwarder forwarder;
135 Name nameP("ndn:/strategy/P");
136 Name nameQ("ndn:/strategy/Q");
137 shared_ptr<Strategy> strategyP = make_shared<DummyStrategy>(ref(forwarder), nameP);
138 shared_ptr<Strategy> strategyQ = make_shared<DummyStrategy>(ref(forwarder), nameQ);
139
140 StrategyChoice& table = forwarder.getStrategyChoice();
141 table.install(strategyP);
142 table.install(strategyQ);
143
144 table.insert("ndn:/", nameP);
145 table.insert("ndn:/A/B", nameQ);
146 table.insert("ndn:/A/B/C", nameP);
147 table.insert("ndn:/D", nameP);
148 table.insert("ndn:/E", nameQ);
149
150 BOOST_CHECK_EQUAL(table.size(), 5);
151
152 std::map<Name, Name> map; // namespace=>strategyName
153 for (StrategyChoice::const_iterator it = table.begin(); it != table.end(); ++it) {
154 map[it->getPrefix()] = it->getStrategyName();
155 }
156 BOOST_CHECK_EQUAL(map.size(), 5);
157 BOOST_CHECK_EQUAL(map["ndn:/"], nameP);
158 BOOST_CHECK_EQUAL(map["ndn:/A/B"], nameQ);
159 BOOST_CHECK_EQUAL(map["ndn:/A/B/C"], nameP);
160 BOOST_CHECK_EQUAL(map["ndn:/D"], nameP);
161 BOOST_CHECK_EQUAL(map["ndn:/E"], nameQ);
162 BOOST_CHECK_EQUAL(map.size(), 5);
163}
164
Junxiao Shie349ea12014-03-12 01:32:42 -0700165class PStrategyInfo : public fw::StrategyInfo
166{
Junxiao Shi39ef2612014-11-29 20:35:19 -0700167public:
168 static constexpr int
169 getTypeId()
170 {
171 return 10;
172 }
Junxiao Shie349ea12014-03-12 01:32:42 -0700173};
174
175BOOST_AUTO_TEST_CASE(ClearStrategyInfo)
176{
177 Forwarder forwarder;
178 Name nameP("ndn:/strategy/P");
179 Name nameQ("ndn:/strategy/Q");
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700180 shared_ptr<Strategy> strategyP = make_shared<DummyStrategy>(ref(forwarder), nameP);
181 shared_ptr<Strategy> strategyQ = make_shared<DummyStrategy>(ref(forwarder), nameQ);
Junxiao Shie349ea12014-03-12 01:32:42 -0700182
183 StrategyChoice& table = forwarder.getStrategyChoice();
184 Measurements& measurements = forwarder.getMeasurements();
185
186 // install
187 table.install(strategyP);
188 table.install(strategyQ);
189
190 BOOST_CHECK(table.insert("ndn:/", nameP));
191 // { '/'=>P }
192 measurements.get("ndn:/") ->getOrCreateStrategyInfo<PStrategyInfo>();
193 measurements.get("ndn:/A") ->getOrCreateStrategyInfo<PStrategyInfo>();
194 measurements.get("ndn:/A/B") ->getOrCreateStrategyInfo<PStrategyInfo>();
195 measurements.get("ndn:/A/C") ->getOrCreateStrategyInfo<PStrategyInfo>();
196
197 BOOST_CHECK(table.insert("ndn:/A/B", nameP));
198 // { '/'=>P, '/A/B'=>P }
199 BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/") ->getStrategyInfo<PStrategyInfo>()));
200 BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/A") ->getStrategyInfo<PStrategyInfo>()));
201 BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/A/B")->getStrategyInfo<PStrategyInfo>()));
202 BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/A/C")->getStrategyInfo<PStrategyInfo>()));
203
204 BOOST_CHECK(table.insert("ndn:/A", nameQ));
205 // { '/'=>P, '/A/B'=>P, '/A'=>Q }
206 BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/") ->getStrategyInfo<PStrategyInfo>()));
207 BOOST_CHECK(!static_cast<bool>(measurements.get("ndn:/A") ->getStrategyInfo<PStrategyInfo>()));
208 BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/A/B")->getStrategyInfo<PStrategyInfo>()));
209 BOOST_CHECK(!static_cast<bool>(measurements.get("ndn:/A/C")->getStrategyInfo<PStrategyInfo>()));
210
211 table.erase("ndn:/A/B");
212 // { '/'=>P, '/A'=>Q }
213 BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/") ->getStrategyInfo<PStrategyInfo>()));
214 BOOST_CHECK(!static_cast<bool>(measurements.get("ndn:/A") ->getStrategyInfo<PStrategyInfo>()));
215 BOOST_CHECK(!static_cast<bool>(measurements.get("ndn:/A/B")->getStrategyInfo<PStrategyInfo>()));
216 BOOST_CHECK(!static_cast<bool>(measurements.get("ndn:/A/C")->getStrategyInfo<PStrategyInfo>()));
217}
218
Junxiao Shiee5a4442014-07-27 17:13:43 -0700219BOOST_AUTO_TEST_CASE(EraseNameTreeEntry)
220{
221 Forwarder forwarder;
222 NameTree& nameTree = forwarder.getNameTree();
223 StrategyChoice& table = forwarder.getStrategyChoice();
224
225 Name nameP("ndn:/strategy/P");
226 Name nameQ("ndn:/strategy/Q");
227 shared_ptr<Strategy> strategyP = make_shared<DummyStrategy>(ref(forwarder), nameP);
228 shared_ptr<Strategy> strategyQ = make_shared<DummyStrategy>(ref(forwarder), nameQ);
229 table.install(strategyP);
230 table.install(strategyQ);
231
232 table.insert("ndn:/", nameP);
233
234 size_t nNameTreeEntriesBefore = nameTree.size();
235
236 table.insert("ndn:/A/B", nameQ);
237 table.erase("ndn:/A/B");
238 BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore);
239}
240
Junxiao Shie93d6a32014-09-07 16:13:22 -0700241BOOST_AUTO_TEST_CASE(Versioning)
242{
243 Forwarder forwarder;
244 Name nameP("ndn:/strategy/P");
245 Name nameP1("ndn:/strategy/P/%FD%01");
246 Name nameP2("ndn:/strategy/P/%FD%02");
247 Name name3("ndn:/%FD%03");
248 Name name4("ndn:/%FD%04");
249 Name nameQ("ndn:/strategy/Q");
250 Name nameQ5("ndn:/strategy/Q/%FD%05");
251 shared_ptr<Strategy> strategyP1 = make_shared<DummyStrategy>(ref(forwarder), nameP1);
252 shared_ptr<Strategy> strategyP2 = make_shared<DummyStrategy>(ref(forwarder), nameP2);
253 shared_ptr<Strategy> strategy3 = make_shared<DummyStrategy>(ref(forwarder), name3);
254 shared_ptr<Strategy> strategy4 = make_shared<DummyStrategy>(ref(forwarder), name4);
255 shared_ptr<Strategy> strategyQ = make_shared<DummyStrategy>(ref(forwarder), nameQ);
256 shared_ptr<Strategy> strategyQ5 = make_shared<DummyStrategy>(ref(forwarder), nameQ5);
257
258 StrategyChoice& table = forwarder.getStrategyChoice();
259
260 // install
261 BOOST_CHECK_EQUAL(table.install(strategyP1), true);
262 BOOST_CHECK_EQUAL(table.install(strategyP1), false);
263 BOOST_CHECK_EQUAL(table.hasStrategy(nameP, false), true);
264 BOOST_CHECK_EQUAL(table.hasStrategy(nameP, true), false);
265 BOOST_CHECK_EQUAL(table.hasStrategy(nameP1, true), true);
266
267 BOOST_CHECK_EQUAL(table.install(strategyP2), true);
268 BOOST_CHECK_EQUAL(table.install(strategy3), true);
269 BOOST_CHECK_EQUAL(table.install(strategy4), true);
270 BOOST_CHECK_EQUAL(table.install(strategyQ), true);
271 BOOST_CHECK_EQUAL(table.install(strategyQ5), true);
272
273 BOOST_CHECK(table.insert("ndn:/", nameQ));
274 // exact match, { '/'=>Q }
275 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/").getName(), nameQ);
276
277 BOOST_CHECK(table.insert("ndn:/", nameQ));
278 BOOST_CHECK(table.insert("ndn:/", nameP));
279 // { '/'=>P2 }
280 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/").getName(), nameP2);
281
282 BOOST_CHECK(table.insert("ndn:/", nameQ));
283 BOOST_CHECK(table.insert("ndn:/", nameP1));
284 // { '/'=>P1 }
285 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/").getName(), nameP1);
286
287 BOOST_CHECK(table.insert("ndn:/", nameQ));
288 BOOST_CHECK(table.insert("ndn:/", nameP2));
289 // { '/'=>P2 }
290 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/").getName(), nameP2);
291
292 BOOST_CHECK(table.insert("ndn:/", nameQ));
293 BOOST_CHECK(! table.insert("ndn:/", "ndn:/strategy/A"));
294 // not installed
295 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/").getName(), nameQ);
296
297 BOOST_CHECK(table.insert("ndn:/", nameQ));
298 BOOST_CHECK(! table.insert("ndn:/", "ndn:/strategy/Z"));
299 // not installed
300 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/").getName(), nameQ);
301
302 BOOST_CHECK(table.insert("ndn:/", nameP1));
303 BOOST_CHECK(table.insert("ndn:/", "ndn:/"));
304 // match one component longer only, { '/'=>4 }
305 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/").getName(), name4);
306}
307
Junxiao Shibb5105f2014-03-03 12:06:45 -0700308BOOST_AUTO_TEST_SUITE_END()
309
310} // namespace tests
311} // namespace nfd