blob: cc7b2790ff05896362497f5698ef382301ceedeb [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
52 BOOST_CHECK_EQUAL(*table.get("ndn:/"), nameP);
53}
54
Junxiao Shibb5105f2014-03-03 12:06:45 -070055BOOST_AUTO_TEST_CASE(Effective)
56{
57 Forwarder forwarder;
58 Name nameP("ndn:/strategy/P");
59 Name nameQ("ndn:/strategy/Q");
60 Name nameZ("ndn:/strategy/Z");
Alexander Afanasyevf6980282014-05-13 18:28:40 -070061 shared_ptr<Strategy> strategyP = make_shared<DummyStrategy>(ref(forwarder), nameP);
62 shared_ptr<Strategy> strategyQ = make_shared<DummyStrategy>(ref(forwarder), nameQ);
Junxiao Shibb5105f2014-03-03 12:06:45 -070063
64 StrategyChoice& table = forwarder.getStrategyChoice();
65
66 // install
67 BOOST_CHECK_EQUAL(table.install(strategyP), true);
68 BOOST_CHECK_EQUAL(table.install(strategyQ), true);
69 BOOST_CHECK_EQUAL(table.install(strategyQ), false);
70
Junxiao Shie349ea12014-03-12 01:32:42 -070071 BOOST_CHECK(table.insert("ndn:/", nameP));
Junxiao Shibb5105f2014-03-03 12:06:45 -070072 // { '/'=>P }
73
74 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameP);
75 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameP);
76 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameP);
77
Junxiao Shie349ea12014-03-12 01:32:42 -070078 BOOST_CHECK(table.insert("ndn:/A/B", nameP));
Junxiao Shibb5105f2014-03-03 12:06:45 -070079 // { '/'=>P, '/A/B'=>P }
80
81 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameP);
82 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameP);
83 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameP);
84 // same instance
85 BOOST_CHECK_EQUAL(&table.findEffectiveStrategy("ndn:/"), strategyP.get());
86 BOOST_CHECK_EQUAL(&table.findEffectiveStrategy("ndn:/A"), strategyP.get());
87 BOOST_CHECK_EQUAL(&table.findEffectiveStrategy("ndn:/A/B"), strategyP.get());
88
89 table.erase("ndn:/A"); // no effect
90 // { '/'=>P, '/A/B'=>P }
91
92 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameP);
93 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameP);
94 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameP);
95
Junxiao Shie349ea12014-03-12 01:32:42 -070096 BOOST_CHECK(table.insert("ndn:/A", nameQ));
Junxiao Shibb5105f2014-03-03 12:06:45 -070097 // { '/'=>P, '/A/B'=>P, '/A'=>Q }
98
99 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameP);
100 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameQ);
101 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameP);
102
103 table.erase("ndn:/A/B");
104 // { '/'=>P, '/A'=>Q }
105
106 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameP);
107 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameQ);
108 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameQ);
109
Junxiao Shie349ea12014-03-12 01:32:42 -0700110 BOOST_CHECK(!table.insert("ndn:/", nameZ)); // non existent strategy
Junxiao Shibb5105f2014-03-03 12:06:45 -0700111
Junxiao Shie349ea12014-03-12 01:32:42 -0700112 BOOST_CHECK(table.insert("ndn:/", nameQ));
113 BOOST_CHECK(table.insert("ndn:/A", nameP));
Junxiao Shibb5105f2014-03-03 12:06:45 -0700114 // { '/'=>Q, '/A'=>P }
115
116 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameQ);
117 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameP);
118 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameP);
119 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/D") .getName(), nameQ);
120}
121
Junxiao Shib5888d22014-05-26 07:35:22 -0700122//XXX BOOST_CONCEPT_ASSERT((ForwardIterator<std::vector<int>::iterator>))
123// is also failing. There might be a problem with ForwardIterator concept checking.
124//BOOST_CONCEPT_ASSERT((ForwardIterator<StrategyChoice::const_iterator>));
125
126BOOST_AUTO_TEST_CASE(Enumerate)
127{
128
129 Forwarder forwarder;
130 Name nameP("ndn:/strategy/P");
131 Name nameQ("ndn:/strategy/Q");
132 shared_ptr<Strategy> strategyP = make_shared<DummyStrategy>(ref(forwarder), nameP);
133 shared_ptr<Strategy> strategyQ = make_shared<DummyStrategy>(ref(forwarder), nameQ);
134
135 StrategyChoice& table = forwarder.getStrategyChoice();
136 table.install(strategyP);
137 table.install(strategyQ);
138
139 table.insert("ndn:/", nameP);
140 table.insert("ndn:/A/B", nameQ);
141 table.insert("ndn:/A/B/C", nameP);
142 table.insert("ndn:/D", nameP);
143 table.insert("ndn:/E", nameQ);
144
145 BOOST_CHECK_EQUAL(table.size(), 5);
146
147 std::map<Name, Name> map; // namespace=>strategyName
148 for (StrategyChoice::const_iterator it = table.begin(); it != table.end(); ++it) {
149 map[it->getPrefix()] = it->getStrategyName();
150 }
151 BOOST_CHECK_EQUAL(map.size(), 5);
152 BOOST_CHECK_EQUAL(map["ndn:/"], nameP);
153 BOOST_CHECK_EQUAL(map["ndn:/A/B"], nameQ);
154 BOOST_CHECK_EQUAL(map["ndn:/A/B/C"], nameP);
155 BOOST_CHECK_EQUAL(map["ndn:/D"], nameP);
156 BOOST_CHECK_EQUAL(map["ndn:/E"], nameQ);
157 BOOST_CHECK_EQUAL(map.size(), 5);
158}
159
Junxiao Shie349ea12014-03-12 01:32:42 -0700160class PStrategyInfo : public fw::StrategyInfo
161{
162};
163
164BOOST_AUTO_TEST_CASE(ClearStrategyInfo)
165{
166 Forwarder forwarder;
167 Name nameP("ndn:/strategy/P");
168 Name nameQ("ndn:/strategy/Q");
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700169 shared_ptr<Strategy> strategyP = make_shared<DummyStrategy>(ref(forwarder), nameP);
170 shared_ptr<Strategy> strategyQ = make_shared<DummyStrategy>(ref(forwarder), nameQ);
Junxiao Shie349ea12014-03-12 01:32:42 -0700171
172 StrategyChoice& table = forwarder.getStrategyChoice();
173 Measurements& measurements = forwarder.getMeasurements();
174
175 // install
176 table.install(strategyP);
177 table.install(strategyQ);
178
179 BOOST_CHECK(table.insert("ndn:/", nameP));
180 // { '/'=>P }
181 measurements.get("ndn:/") ->getOrCreateStrategyInfo<PStrategyInfo>();
182 measurements.get("ndn:/A") ->getOrCreateStrategyInfo<PStrategyInfo>();
183 measurements.get("ndn:/A/B") ->getOrCreateStrategyInfo<PStrategyInfo>();
184 measurements.get("ndn:/A/C") ->getOrCreateStrategyInfo<PStrategyInfo>();
185
186 BOOST_CHECK(table.insert("ndn:/A/B", nameP));
187 // { '/'=>P, '/A/B'=>P }
188 BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/") ->getStrategyInfo<PStrategyInfo>()));
189 BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/A") ->getStrategyInfo<PStrategyInfo>()));
190 BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/A/B")->getStrategyInfo<PStrategyInfo>()));
191 BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/A/C")->getStrategyInfo<PStrategyInfo>()));
192
193 BOOST_CHECK(table.insert("ndn:/A", nameQ));
194 // { '/'=>P, '/A/B'=>P, '/A'=>Q }
195 BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/") ->getStrategyInfo<PStrategyInfo>()));
196 BOOST_CHECK(!static_cast<bool>(measurements.get("ndn:/A") ->getStrategyInfo<PStrategyInfo>()));
197 BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/A/B")->getStrategyInfo<PStrategyInfo>()));
198 BOOST_CHECK(!static_cast<bool>(measurements.get("ndn:/A/C")->getStrategyInfo<PStrategyInfo>()));
199
200 table.erase("ndn:/A/B");
201 // { '/'=>P, '/A'=>Q }
202 BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/") ->getStrategyInfo<PStrategyInfo>()));
203 BOOST_CHECK(!static_cast<bool>(measurements.get("ndn:/A") ->getStrategyInfo<PStrategyInfo>()));
204 BOOST_CHECK(!static_cast<bool>(measurements.get("ndn:/A/B")->getStrategyInfo<PStrategyInfo>()));
205 BOOST_CHECK(!static_cast<bool>(measurements.get("ndn:/A/C")->getStrategyInfo<PStrategyInfo>()));
206}
207
Junxiao Shiee5a4442014-07-27 17:13:43 -0700208BOOST_AUTO_TEST_CASE(EraseNameTreeEntry)
209{
210 Forwarder forwarder;
211 NameTree& nameTree = forwarder.getNameTree();
212 StrategyChoice& table = forwarder.getStrategyChoice();
213
214 Name nameP("ndn:/strategy/P");
215 Name nameQ("ndn:/strategy/Q");
216 shared_ptr<Strategy> strategyP = make_shared<DummyStrategy>(ref(forwarder), nameP);
217 shared_ptr<Strategy> strategyQ = make_shared<DummyStrategy>(ref(forwarder), nameQ);
218 table.install(strategyP);
219 table.install(strategyQ);
220
221 table.insert("ndn:/", nameP);
222
223 size_t nNameTreeEntriesBefore = nameTree.size();
224
225 table.insert("ndn:/A/B", nameQ);
226 table.erase("ndn:/A/B");
227 BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore);
228}
229
Junxiao Shie93d6a32014-09-07 16:13:22 -0700230BOOST_AUTO_TEST_CASE(Versioning)
231{
232 Forwarder forwarder;
233 Name nameP("ndn:/strategy/P");
234 Name nameP1("ndn:/strategy/P/%FD%01");
235 Name nameP2("ndn:/strategy/P/%FD%02");
236 Name name3("ndn:/%FD%03");
237 Name name4("ndn:/%FD%04");
238 Name nameQ("ndn:/strategy/Q");
239 Name nameQ5("ndn:/strategy/Q/%FD%05");
240 shared_ptr<Strategy> strategyP1 = make_shared<DummyStrategy>(ref(forwarder), nameP1);
241 shared_ptr<Strategy> strategyP2 = make_shared<DummyStrategy>(ref(forwarder), nameP2);
242 shared_ptr<Strategy> strategy3 = make_shared<DummyStrategy>(ref(forwarder), name3);
243 shared_ptr<Strategy> strategy4 = make_shared<DummyStrategy>(ref(forwarder), name4);
244 shared_ptr<Strategy> strategyQ = make_shared<DummyStrategy>(ref(forwarder), nameQ);
245 shared_ptr<Strategy> strategyQ5 = make_shared<DummyStrategy>(ref(forwarder), nameQ5);
246
247 StrategyChoice& table = forwarder.getStrategyChoice();
248
249 // install
250 BOOST_CHECK_EQUAL(table.install(strategyP1), true);
251 BOOST_CHECK_EQUAL(table.install(strategyP1), false);
252 BOOST_CHECK_EQUAL(table.hasStrategy(nameP, false), true);
253 BOOST_CHECK_EQUAL(table.hasStrategy(nameP, true), false);
254 BOOST_CHECK_EQUAL(table.hasStrategy(nameP1, true), true);
255
256 BOOST_CHECK_EQUAL(table.install(strategyP2), true);
257 BOOST_CHECK_EQUAL(table.install(strategy3), true);
258 BOOST_CHECK_EQUAL(table.install(strategy4), true);
259 BOOST_CHECK_EQUAL(table.install(strategyQ), true);
260 BOOST_CHECK_EQUAL(table.install(strategyQ5), true);
261
262 BOOST_CHECK(table.insert("ndn:/", nameQ));
263 // exact match, { '/'=>Q }
264 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/").getName(), nameQ);
265
266 BOOST_CHECK(table.insert("ndn:/", nameQ));
267 BOOST_CHECK(table.insert("ndn:/", nameP));
268 // { '/'=>P2 }
269 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/").getName(), nameP2);
270
271 BOOST_CHECK(table.insert("ndn:/", nameQ));
272 BOOST_CHECK(table.insert("ndn:/", nameP1));
273 // { '/'=>P1 }
274 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/").getName(), nameP1);
275
276 BOOST_CHECK(table.insert("ndn:/", nameQ));
277 BOOST_CHECK(table.insert("ndn:/", nameP2));
278 // { '/'=>P2 }
279 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/").getName(), nameP2);
280
281 BOOST_CHECK(table.insert("ndn:/", nameQ));
282 BOOST_CHECK(! table.insert("ndn:/", "ndn:/strategy/A"));
283 // not installed
284 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/").getName(), nameQ);
285
286 BOOST_CHECK(table.insert("ndn:/", nameQ));
287 BOOST_CHECK(! table.insert("ndn:/", "ndn:/strategy/Z"));
288 // not installed
289 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/").getName(), nameQ);
290
291 BOOST_CHECK(table.insert("ndn:/", nameP1));
292 BOOST_CHECK(table.insert("ndn:/", "ndn:/"));
293 // match one component longer only, { '/'=>4 }
294 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/").getName(), name4);
295}
296
Junxiao Shibb5105f2014-03-03 12:06:45 -0700297BOOST_AUTO_TEST_SUITE_END()
298
299} // namespace tests
300} // namespace nfd