blob: e8f4ca33825434b3bff7fd81601dc639b54ad029 [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/>.
24 **/
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 Shibb5105f2014-03-03 12:06:45 -0700191BOOST_AUTO_TEST_SUITE_END()
192
193} // namespace tests
194} // namespace nfd