blob: 90789a0088bebb7cff2fb2b85ceba5a9ec4fec9b [file] [log] [blame]
Yanbiao Li6704a4a2015-08-19 16:30:16 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yanbiao Lidf846e52016-01-30 21:53:47 -08003 * Copyright (c) 2014-2016, Regents of the University of California,
Yanbiao Li6704a4a2015-08-19 16:30:16 -07004 * 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.
10 *
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 */
25
26#include "mgmt/strategy-choice-manager.hpp"
Yanbiao Lidf846e52016-01-30 21:53:47 -080027#include "nfd-manager-common-fixture.hpp"
Yanbiao Li6704a4a2015-08-19 16:30:16 -070028
29#include "face/face.hpp"
30#include "face/internal-face.hpp"
31#include "table/name-tree.hpp"
32#include "table/strategy-choice.hpp"
33#include "fw/strategy.hpp"
34#include "tests/daemon/face/dummy-face.hpp"
35#include "tests/daemon/fw/dummy-strategy.hpp"
36
37#include <ndn-cxx/util/random.hpp>
38#include <ndn-cxx/management/nfd-strategy-choice.hpp>
39
40namespace nfd {
41namespace tests {
42
Yanbiao Lidf846e52016-01-30 21:53:47 -080043class StrategyChoiceManagerFixture : public NfdManagerCommonFixture
Yanbiao Li6704a4a2015-08-19 16:30:16 -070044{
45public:
46 StrategyChoiceManagerFixture()
47 : m_strategyChoice(m_forwarder.getStrategyChoice())
48 , m_manager(m_strategyChoice, m_dispatcher, m_validator)
49 {
Yanbiao Lidf846e52016-01-30 21:53:47 -080050 setPrivilege("strategy-choice");
Yanbiao Li6704a4a2015-08-19 16:30:16 -070051 }
52
53public:
54 void
55 installStrategy(const Name& strategy)
56 {
57 m_strategyChoice.install(make_shared<DummyStrategy>(ref(m_forwarder), strategy));
58 }
59
60 const Name&
61 findStrategy(const Name& name)
62 {
63 return m_strategyChoice.findEffectiveStrategy(name).getName();
64 }
65
66 ControlParameters
67 makeParameters(const Name& name, const Name& strategy)
68 {
69 return ControlParameters().setName(name).setStrategy(strategy);
70 }
71
72protected:
73 StrategyChoice& m_strategyChoice;
74 StrategyChoiceManager m_manager;
75};
76
77BOOST_FIXTURE_TEST_SUITE(Mgmt, StrategyChoiceManagerFixture)
78BOOST_AUTO_TEST_SUITE(TestStrategyChoiceManager)
79
80BOOST_AUTO_TEST_CASE(SetStrategy)
81{
82 auto testSetStrategy = [this] (const ControlParameters& parameters) -> Name {
83 m_responses.clear();
84 auto command = makeControlCommandRequest("/localhost/nfd/strategy-choice/set", parameters);
85 receiveInterest(command);
86 return command->getName();
87 };
88
89 installStrategy("/localhost/nfd/strategy/test-strategy-a");
90 installStrategy("/localhost/nfd/strategy/test-strategy-c/%FD%01"); // version 1
91 installStrategy("/localhost/nfd/strategy/test-strategy-c/%FD%02"); // version 2
92
93 auto parametersA = makeParameters("test", "/localhost/nfd/strategy/test-strategy-a");
94 auto parametersB = makeParameters("test", "/localhost/nfd/strategy/test-strategy-b");
95 auto parametersC1 = makeParameters("test", "/localhost/nfd/strategy/test-strategy-c/%FD%01");
96 auto parametersC = makeParameters("test", "/localhost/nfd/strategy/test-strategy-c");
97
98 auto commandNameA = testSetStrategy(parametersA); // succeed
99 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
100 BOOST_CHECK_EQUAL(checkResponse(0, commandNameA, makeResponse(200, "OK", parametersA)),
101 CheckResponseResult::OK);
102 BOOST_CHECK_EQUAL(findStrategy("/test"), "/localhost/nfd/strategy/test-strategy-a");
103
104 auto commandNameB = testSetStrategy(parametersB); // not installed
105 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
106 BOOST_CHECK_EQUAL(checkResponse(0, commandNameB, ControlResponse(504, "Unsupported strategy")),
107 CheckResponseResult::OK);
108
109 auto commandNameC1 = testSetStrategy(parametersC1); // specified version
110 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
111 BOOST_CHECK_EQUAL(checkResponse(0, commandNameC1, makeResponse(200, "OK", parametersC1)),
112 CheckResponseResult::OK);
113 BOOST_CHECK_EQUAL(findStrategy("/test"), "/localhost/nfd/strategy/test-strategy-c/%FD%01");
114
115 auto commandNameC = testSetStrategy(parametersC); // latest version
116 parametersC.setStrategy("/localhost/nfd/strategy/test-strategy-c/%FD%02"); // change to latest
117 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
118 BOOST_CHECK_EQUAL(checkResponse(0, commandNameC, makeResponse(200, "OK", parametersC)),
119 CheckResponseResult::OK);
120 BOOST_CHECK_EQUAL(findStrategy("/test"), "/localhost/nfd/strategy/test-strategy-c/%FD%02");
121}
122
123BOOST_AUTO_TEST_CASE(UnsetStrategy)
124{
125 auto testUnsetStrategy = [this] (const ControlParameters& parameters) -> Name {
126 m_responses.clear();
127 auto command = makeControlCommandRequest("/localhost/nfd/strategy-choice/unset", parameters);
128 receiveInterest(command);
129 return command->getName();
130 };
131
132 installStrategy("/localhost/nfd/strategy/test-strategy-a");
133 installStrategy("/localhost/nfd/strategy/test-strategy-b");
134 installStrategy("/localhost/nfd/strategy/test-strategy-c");
135
136 BOOST_CHECK(m_strategyChoice.insert("ndn:/", "/localhost/nfd/strategy/test-strategy-a")); // root
137 BOOST_CHECK(m_strategyChoice.insert("/test", "/localhost/nfd/strategy/test-strategy-b")); // test
138 BOOST_CHECK_EQUAL(findStrategy("/"), "/localhost/nfd/strategy/test-strategy-a");
139
140 auto parametersRoot = ControlParameters().setName("ndn:/"); // root prefix
141 auto parametersNone = ControlParameters().setName("/none"); // no such entry
142 auto parametersTest = ControlParameters().setName("/test"); // has such entry
143
144 BOOST_CHECK_EQUAL(findStrategy("/"), "/localhost/nfd/strategy/test-strategy-a"); // root
145 auto commandRootName = testUnsetStrategy(parametersRoot);
146 auto expectedResponse = ControlResponse(400, "failed in validating parameters");
147 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
148 BOOST_CHECK_EQUAL(checkResponse(0, commandRootName, expectedResponse), CheckResponseResult::OK);
149 BOOST_CHECK_EQUAL(findStrategy("/"), "/localhost/nfd/strategy/test-strategy-a"); // keep as root
150
151 BOOST_CHECK_EQUAL(findStrategy("/none"), "/localhost/nfd/strategy/test-strategy-a"); // root
152 auto commandNoneName = testUnsetStrategy(parametersNone);
153 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
154 BOOST_CHECK_EQUAL(checkResponse(0, commandNoneName, makeResponse(200, "OK", parametersNone)),
155 CheckResponseResult::OK);
156 BOOST_CHECK_EQUAL(findStrategy("/none"), "/localhost/nfd/strategy/test-strategy-a"); // root
157
158 BOOST_CHECK_EQUAL(findStrategy("/test"), "/localhost/nfd/strategy/test-strategy-b"); // self
159 auto commandTestName = testUnsetStrategy(parametersTest);
160 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
161 BOOST_CHECK_EQUAL(checkResponse(0, commandTestName, makeResponse(200, "OK", parametersTest)),
162 CheckResponseResult::OK);
163 BOOST_CHECK_EQUAL(findStrategy("/test"), "/localhost/nfd/strategy/test-strategy-a"); // parent
164}
165
166// @todo Remove when ndn::nfd::StrategyChoice implements operator!= and operator<<
167class StrategyChoice : public ndn::nfd::StrategyChoice
168{
169public:
170 StrategyChoice() = default;
171
172 StrategyChoice(const ndn::nfd::StrategyChoice& entry)
173 : ndn::nfd::StrategyChoice(entry)
174 {
175 }
176};
177
178bool
179operator!=(const StrategyChoice& left, const StrategyChoice& right)
180{
181 return left.getName() != right.getName() || left.getStrategy() != right.getStrategy();
182}
183
184std::ostream&
185operator<<(std::ostream &os, const StrategyChoice& entry)
186{
187 os << "[ " << entry.getName() << ", " << entry.getStrategy() << " ]";
188 return os;
189}
190
191BOOST_AUTO_TEST_CASE(ListChoices)
192{
193 size_t nPreInsertedStrategies = m_strategyChoice.size(); // the best-route strategy
194 std::set<Name> actualNames, actualStrategies;
195 for (auto&& entry : m_strategyChoice) {
196 actualNames.insert(entry.getPrefix());
197 actualStrategies.insert(entry.getStrategyName());
198 }
199
200 size_t nEntries = 1024;
201 for (size_t i = 0 ; i < nEntries ; i++) {
202 auto name = Name("test-name").appendSegment(i);
203 auto strategy = Name("test-strategy").appendSegment(ndn::random::generateWord64());
204 auto entry = ndn::nfd::StrategyChoice().setName(name).setStrategy(strategy);
205 actualNames.insert(name);
206 actualStrategies.insert(strategy);
207 installStrategy(strategy);
208 m_strategyChoice.insert(name, strategy);
209 }
210 nEntries += nPreInsertedStrategies;
211
212 receiveInterest(makeInterest("/localhost/nfd/strategy-choice/list"));
213
214 Block content;
215 BOOST_CHECK_NO_THROW(content = concatenateResponses());
216 BOOST_CHECK_NO_THROW(content.parse());
217 BOOST_CHECK_EQUAL(content.elements().size(), nEntries);
218
219 std::vector<StrategyChoice> receivedRecords, expectedRecords;
220 for (size_t idx = 0; idx < nEntries; ++idx) {
221 BOOST_TEST_MESSAGE("processing element: " << idx);
222
223 StrategyChoice decodedEntry;
224 BOOST_REQUIRE_NO_THROW(decodedEntry.wireDecode(content.elements()[idx]));
225 receivedRecords.push_back(decodedEntry);
226
227 actualNames.erase(decodedEntry.getName());
228 actualStrategies.erase(decodedEntry.getStrategy());
229
230 auto result = m_strategyChoice.get(decodedEntry.getName());
231 BOOST_REQUIRE(result.first);
232
233 auto record = StrategyChoice().setName(decodedEntry.getName()).setStrategy(result.second);
234 expectedRecords.push_back(record);
235 }
236
237 BOOST_CHECK_EQUAL(actualNames.size(), 0);
238 BOOST_CHECK_EQUAL(actualStrategies.size(), 0);
239
240 BOOST_CHECK_EQUAL_COLLECTIONS(receivedRecords.begin(), receivedRecords.end(),
241 expectedRecords.begin(), expectedRecords.end());
242}
243
244BOOST_AUTO_TEST_SUITE_END() // TestStrategyChoiceManager
245BOOST_AUTO_TEST_SUITE_END() // Mgmt
246
247} // namespace tests
248} // namespace nfd