blob: 7817d51e2d1e0ee5612c5387eae718c7feae1d83 [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{
167};
168
169BOOST_AUTO_TEST_CASE(ClearStrategyInfo)
170{
171 Forwarder forwarder;
172 Name nameP("ndn:/strategy/P");
173 Name nameQ("ndn:/strategy/Q");
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700174 shared_ptr<Strategy> strategyP = make_shared<DummyStrategy>(ref(forwarder), nameP);
175 shared_ptr<Strategy> strategyQ = make_shared<DummyStrategy>(ref(forwarder), nameQ);
Junxiao Shie349ea12014-03-12 01:32:42 -0700176
177 StrategyChoice& table = forwarder.getStrategyChoice();
178 Measurements& measurements = forwarder.getMeasurements();
179
180 // install
181 table.install(strategyP);
182 table.install(strategyQ);
183
184 BOOST_CHECK(table.insert("ndn:/", nameP));
185 // { '/'=>P }
186 measurements.get("ndn:/") ->getOrCreateStrategyInfo<PStrategyInfo>();
187 measurements.get("ndn:/A") ->getOrCreateStrategyInfo<PStrategyInfo>();
188 measurements.get("ndn:/A/B") ->getOrCreateStrategyInfo<PStrategyInfo>();
189 measurements.get("ndn:/A/C") ->getOrCreateStrategyInfo<PStrategyInfo>();
190
191 BOOST_CHECK(table.insert("ndn:/A/B", nameP));
192 // { '/'=>P, '/A/B'=>P }
193 BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/") ->getStrategyInfo<PStrategyInfo>()));
194 BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/A") ->getStrategyInfo<PStrategyInfo>()));
195 BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/A/B")->getStrategyInfo<PStrategyInfo>()));
196 BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/A/C")->getStrategyInfo<PStrategyInfo>()));
197
198 BOOST_CHECK(table.insert("ndn:/A", nameQ));
199 // { '/'=>P, '/A/B'=>P, '/A'=>Q }
200 BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/") ->getStrategyInfo<PStrategyInfo>()));
201 BOOST_CHECK(!static_cast<bool>(measurements.get("ndn:/A") ->getStrategyInfo<PStrategyInfo>()));
202 BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/A/B")->getStrategyInfo<PStrategyInfo>()));
203 BOOST_CHECK(!static_cast<bool>(measurements.get("ndn:/A/C")->getStrategyInfo<PStrategyInfo>()));
204
205 table.erase("ndn:/A/B");
206 // { '/'=>P, '/A'=>Q }
207 BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/") ->getStrategyInfo<PStrategyInfo>()));
208 BOOST_CHECK(!static_cast<bool>(measurements.get("ndn:/A") ->getStrategyInfo<PStrategyInfo>()));
209 BOOST_CHECK(!static_cast<bool>(measurements.get("ndn:/A/B")->getStrategyInfo<PStrategyInfo>()));
210 BOOST_CHECK(!static_cast<bool>(measurements.get("ndn:/A/C")->getStrategyInfo<PStrategyInfo>()));
211}
212
Junxiao Shiee5a4442014-07-27 17:13:43 -0700213BOOST_AUTO_TEST_CASE(EraseNameTreeEntry)
214{
215 Forwarder forwarder;
216 NameTree& nameTree = forwarder.getNameTree();
217 StrategyChoice& table = forwarder.getStrategyChoice();
218
219 Name nameP("ndn:/strategy/P");
220 Name nameQ("ndn:/strategy/Q");
221 shared_ptr<Strategy> strategyP = make_shared<DummyStrategy>(ref(forwarder), nameP);
222 shared_ptr<Strategy> strategyQ = make_shared<DummyStrategy>(ref(forwarder), nameQ);
223 table.install(strategyP);
224 table.install(strategyQ);
225
226 table.insert("ndn:/", nameP);
227
228 size_t nNameTreeEntriesBefore = nameTree.size();
229
230 table.insert("ndn:/A/B", nameQ);
231 table.erase("ndn:/A/B");
232 BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore);
233}
234
Junxiao Shie93d6a32014-09-07 16:13:22 -0700235BOOST_AUTO_TEST_CASE(Versioning)
236{
237 Forwarder forwarder;
238 Name nameP("ndn:/strategy/P");
239 Name nameP1("ndn:/strategy/P/%FD%01");
240 Name nameP2("ndn:/strategy/P/%FD%02");
241 Name name3("ndn:/%FD%03");
242 Name name4("ndn:/%FD%04");
243 Name nameQ("ndn:/strategy/Q");
244 Name nameQ5("ndn:/strategy/Q/%FD%05");
245 shared_ptr<Strategy> strategyP1 = make_shared<DummyStrategy>(ref(forwarder), nameP1);
246 shared_ptr<Strategy> strategyP2 = make_shared<DummyStrategy>(ref(forwarder), nameP2);
247 shared_ptr<Strategy> strategy3 = make_shared<DummyStrategy>(ref(forwarder), name3);
248 shared_ptr<Strategy> strategy4 = make_shared<DummyStrategy>(ref(forwarder), name4);
249 shared_ptr<Strategy> strategyQ = make_shared<DummyStrategy>(ref(forwarder), nameQ);
250 shared_ptr<Strategy> strategyQ5 = make_shared<DummyStrategy>(ref(forwarder), nameQ5);
251
252 StrategyChoice& table = forwarder.getStrategyChoice();
253
254 // install
255 BOOST_CHECK_EQUAL(table.install(strategyP1), true);
256 BOOST_CHECK_EQUAL(table.install(strategyP1), false);
257 BOOST_CHECK_EQUAL(table.hasStrategy(nameP, false), true);
258 BOOST_CHECK_EQUAL(table.hasStrategy(nameP, true), false);
259 BOOST_CHECK_EQUAL(table.hasStrategy(nameP1, true), true);
260
261 BOOST_CHECK_EQUAL(table.install(strategyP2), true);
262 BOOST_CHECK_EQUAL(table.install(strategy3), true);
263 BOOST_CHECK_EQUAL(table.install(strategy4), true);
264 BOOST_CHECK_EQUAL(table.install(strategyQ), true);
265 BOOST_CHECK_EQUAL(table.install(strategyQ5), true);
266
267 BOOST_CHECK(table.insert("ndn:/", nameQ));
268 // exact match, { '/'=>Q }
269 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/").getName(), nameQ);
270
271 BOOST_CHECK(table.insert("ndn:/", nameQ));
272 BOOST_CHECK(table.insert("ndn:/", nameP));
273 // { '/'=>P2 }
274 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/").getName(), nameP2);
275
276 BOOST_CHECK(table.insert("ndn:/", nameQ));
277 BOOST_CHECK(table.insert("ndn:/", nameP1));
278 // { '/'=>P1 }
279 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/").getName(), nameP1);
280
281 BOOST_CHECK(table.insert("ndn:/", nameQ));
282 BOOST_CHECK(table.insert("ndn:/", nameP2));
283 // { '/'=>P2 }
284 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/").getName(), nameP2);
285
286 BOOST_CHECK(table.insert("ndn:/", nameQ));
287 BOOST_CHECK(! table.insert("ndn:/", "ndn:/strategy/A"));
288 // not installed
289 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/").getName(), nameQ);
290
291 BOOST_CHECK(table.insert("ndn:/", nameQ));
292 BOOST_CHECK(! table.insert("ndn:/", "ndn:/strategy/Z"));
293 // not installed
294 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/").getName(), nameQ);
295
296 BOOST_CHECK(table.insert("ndn:/", nameP1));
297 BOOST_CHECK(table.insert("ndn:/", "ndn:/"));
298 // match one component longer only, { '/'=>4 }
299 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/").getName(), name4);
300}
301
Junxiao Shibb5105f2014-03-03 12:06:45 -0700302BOOST_AUTO_TEST_SUITE_END()
303
304} // namespace tests
305} // namespace nfd