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