blob: 39a4a5d3d8d2e0a0444cfa21207a2dd6010ceb53 [file] [log] [blame]
Vince Lehman72446ec2014-07-09 10:50:02 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * 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
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 "rib/rib-manager.hpp"
Junxiao Shi376f7372014-11-17 18:03:31 -070027#include <ndn-cxx/management/nfd-face-status.hpp>
Vince Lehmancd16c832014-07-23 15:14:55 -070028#include "rib/rib-status-publisher-common.hpp"
Vince Lehman72446ec2014-07-09 10:50:02 -050029
Junxiao Shi376f7372014-11-17 18:03:31 -070030#include "tests/test-common.hpp"
31#include "tests/limited-io.hpp"
32#include <ndn-cxx/util/dummy-client-face.hpp>
Vince Lehman26b215c2014-08-17 15:00:41 -050033
Vince Lehman72446ec2014-07-09 10:50:02 -050034namespace nfd {
35namespace rib {
36namespace tests {
37
38class RibManagerFixture : public nfd::tests::BaseFixture
39{
40public:
41 RibManagerFixture()
42 : COMMAND_PREFIX("/localhost/nfd/rib")
43 , ADD_NEXTHOP_VERB("add-nexthop")
44 , REMOVE_NEXTHOP_VERB("remove-nexthop")
45 {
Junxiao Shi376f7372014-11-17 18:03:31 -070046 face = ndn::util::makeDummyClientFace();
Vince Lehman72446ec2014-07-09 10:50:02 -050047
48 manager = make_shared<RibManager>(ndn::ref(*face));
49 manager->registerWithNfd();
50
51 face->processEvents(time::milliseconds(1));
Junxiao Shi376f7372014-11-17 18:03:31 -070052 face->sentInterests.clear();
Vince Lehman72446ec2014-07-09 10:50:02 -050053 }
54
55 ~RibManagerFixture()
56 {
57 manager.reset();
58 face.reset();
59 }
60
61 void extractParameters(Interest& interest, Name::Component& verb,
62 ControlParameters& extractedParameters)
63 {
64 const Name& name = interest.getName();
65 verb = name[COMMAND_PREFIX.size()];
66 const Name::Component& parameterComponent = name[COMMAND_PREFIX.size() + 1];
67
68 Block rawParameters = parameterComponent.blockFromValue();
69 extractedParameters.wireDecode(rawParameters);
70 }
71
72 void receiveCommandInterest(Name& name, ControlParameters& parameters)
73 {
Steve DiBenedettocd4ee5f2014-12-08 16:09:11 -070074 receiveCommandInterest(name.append(parameters.wireEncode()));
75 }
Vince Lehman72446ec2014-07-09 10:50:02 -050076
Steve DiBenedettocd4ee5f2014-12-08 16:09:11 -070077 void receiveCommandInterest(const Name& name)
78 {
Vince Lehman72446ec2014-07-09 10:50:02 -050079 Interest command(name);
80
81 face->receive(command);
82 face->processEvents(time::milliseconds(1));
83 }
84
85public:
86 shared_ptr<RibManager> manager;
Junxiao Shi376f7372014-11-17 18:03:31 -070087 shared_ptr<ndn::util::DummyClientFace> face;
Vince Lehman72446ec2014-07-09 10:50:02 -050088
89 const Name COMMAND_PREFIX;
90 const Name::Component ADD_NEXTHOP_VERB;
91 const Name::Component REMOVE_NEXTHOP_VERB;
92};
93
94class AuthorizedRibManager : public RibManagerFixture
95{
96public:
97 AuthorizedRibManager()
98 {
99 ConfigFile config;
100 manager->setConfigFile(config);
101
102 const std::string CONFIG_STRING =
103 "rib\n"
104 "{\n"
105 " localhost_security\n"
106 " {\n"
107 " trust-anchor\n"
108 " {\n"
109 " type any\n"
110 " }\n"
111 " }"
112 "}";
113
114 config.parse(CONFIG_STRING, true, "test-rib");
115 }
116};
117
118typedef RibManagerFixture UnauthorizedRibManager;
119
Vince Lehmancd16c832014-07-23 15:14:55 -0700120BOOST_FIXTURE_TEST_SUITE(RibManager, RibManagerFixture)
Vince Lehman72446ec2014-07-09 10:50:02 -0500121
Steve DiBenedettocd4ee5f2014-12-08 16:09:11 -0700122BOOST_FIXTURE_TEST_CASE(ShortName, AuthorizedRibManager)
123{
124 Name commandName("/localhost/nfd/rib");
125 receiveCommandInterest(commandName);
126 // TODO verify error response
127}
128
Vince Lehman72446ec2014-07-09 10:50:02 -0500129BOOST_FIXTURE_TEST_CASE(Basic, AuthorizedRibManager)
130{
131 ControlParameters parameters;
132 parameters
133 .setName("/hello")
134 .setFaceId(1)
135 .setCost(10)
136 .setFlags(0)
137 .setOrigin(128)
138 .setExpirationPeriod(ndn::time::milliseconds::max());
139
140 Name commandName("/localhost/nfd/rib/register");
141
142 receiveCommandInterest(commandName, parameters);
143
Junxiao Shi376f7372014-11-17 18:03:31 -0700144 BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 1);
Vince Lehman72446ec2014-07-09 10:50:02 -0500145}
146
147BOOST_FIXTURE_TEST_CASE(Register, AuthorizedRibManager)
148{
149 ControlParameters parameters;
150 parameters
151 .setName("/hello")
152 .setFaceId(1)
153 .setCost(10)
154 .setFlags(0)
155 .setOrigin(128)
156 .setExpirationPeriod(ndn::time::milliseconds::max());
157
158 Name commandName("/localhost/nfd/rib/register");
159
160 receiveCommandInterest(commandName, parameters);
161
Junxiao Shi376f7372014-11-17 18:03:31 -0700162 BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 1);
Vince Lehman72446ec2014-07-09 10:50:02 -0500163
Junxiao Shi376f7372014-11-17 18:03:31 -0700164 Interest& request = face->sentInterests[0];
Vince Lehman72446ec2014-07-09 10:50:02 -0500165
166 ControlParameters extractedParameters;
167 Name::Component verb;
168 extractParameters(request, verb, extractedParameters);
169
170 BOOST_CHECK_EQUAL(verb, ADD_NEXTHOP_VERB);
171 BOOST_CHECK_EQUAL(extractedParameters.getName(), parameters.getName());
172 BOOST_CHECK_EQUAL(extractedParameters.getFaceId(), parameters.getFaceId());
173 BOOST_CHECK_EQUAL(extractedParameters.getCost(), parameters.getCost());
174}
175
176BOOST_FIXTURE_TEST_CASE(Unregister, AuthorizedRibManager)
177{
178 ControlParameters addParameters;
179 addParameters
180 .setName("/hello")
181 .setFaceId(1)
182 .setCost(10)
183 .setFlags(0)
184 .setOrigin(128)
185 .setExpirationPeriod(ndn::time::milliseconds::max());
186
187 Name registerName("/localhost/nfd/rib/register");
188
189 receiveCommandInterest(registerName, addParameters);
Junxiao Shi376f7372014-11-17 18:03:31 -0700190 face->sentInterests.clear();
Vince Lehman72446ec2014-07-09 10:50:02 -0500191
192 ControlParameters removeParameters;
193 removeParameters
194 .setName("/hello")
195 .setFaceId(1)
196 .setOrigin(128);
197
198 Name unregisterName("/localhost/nfd/rib/unregister");
199
200 receiveCommandInterest(unregisterName, removeParameters);
201
Junxiao Shi376f7372014-11-17 18:03:31 -0700202 BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 1);
Vince Lehman72446ec2014-07-09 10:50:02 -0500203
Junxiao Shi376f7372014-11-17 18:03:31 -0700204 Interest& request = face->sentInterests[0];
Vince Lehman72446ec2014-07-09 10:50:02 -0500205
206 ControlParameters extractedParameters;
207 Name::Component verb;
208 extractParameters(request, verb, extractedParameters);
209
210 BOOST_CHECK_EQUAL(verb, REMOVE_NEXTHOP_VERB);
211 BOOST_CHECK_EQUAL(extractedParameters.getName(), removeParameters.getName());
212 BOOST_CHECK_EQUAL(extractedParameters.getFaceId(), removeParameters.getFaceId());
213}
214
215BOOST_FIXTURE_TEST_CASE(UnauthorizedCommand, UnauthorizedRibManager)
216{
217 ControlParameters parameters;
218 parameters
219 .setName("/hello")
220 .setFaceId(1)
221 .setCost(10)
222 .setFlags(0)
223 .setOrigin(128)
224 .setExpirationPeriod(ndn::time::milliseconds::max());
225
226 Name commandName("/localhost/nfd/rib/register");
227
Junxiao Shi376f7372014-11-17 18:03:31 -0700228 BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 0);
Vince Lehman72446ec2014-07-09 10:50:02 -0500229
230 receiveCommandInterest(commandName, parameters);
231
Junxiao Shi376f7372014-11-17 18:03:31 -0700232 BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 0);
Vince Lehman72446ec2014-07-09 10:50:02 -0500233}
234
Vince Lehmancd16c832014-07-23 15:14:55 -0700235BOOST_FIXTURE_TEST_CASE(RibStatusRequest, AuthorizedRibManager)
236{
237 FaceEntry entry;
238 Name name("/");
239 entry.faceId = 1;
240 entry.origin = 128;
241 entry.cost = 32;
242 entry.flags = ndn::nfd::ROUTE_FLAG_CAPTURE;
243
244 ControlParameters parameters;
245 parameters
246 .setName(name)
247 .setFaceId(entry.faceId)
248 .setOrigin(entry.origin)
249 .setCost(entry.cost)
250 .setFlags(entry.flags)
251 .setExpirationPeriod(ndn::time::milliseconds::max());
252
253 Name commandName("/localhost/nfd/rib/register");
254
Junxiao Shi376f7372014-11-17 18:03:31 -0700255 BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 0);
Vince Lehmancd16c832014-07-23 15:14:55 -0700256
257 receiveCommandInterest(commandName, parameters);
Junxiao Shi376f7372014-11-17 18:03:31 -0700258 face->sentInterests.clear();
259 face->sentDatas.clear();
Vince Lehmancd16c832014-07-23 15:14:55 -0700260
261 face->receive(Interest("/localhost/nfd/rib/list"));
262 face->processEvents(time::milliseconds(1));
263
Junxiao Shi376f7372014-11-17 18:03:31 -0700264 BOOST_REQUIRE_EQUAL(face->sentDatas.size(), 1);
265 RibStatusPublisherFixture::decodeRibEntryBlock(face->sentDatas[0], name, entry);
Vince Lehmancd16c832014-07-23 15:14:55 -0700266}
267
Vince Lehman281ded72014-08-21 12:17:08 -0500268BOOST_FIXTURE_TEST_CASE(CancelExpirationEvent, AuthorizedRibManager)
269{
270 // Register face
271 ControlParameters addParameters;
272 addParameters
273 .setName("/expire")
274 .setFaceId(1)
275 .setCost(10)
276 .setFlags(0)
277 .setOrigin(128)
278 .setExpirationPeriod(ndn::time::milliseconds(500));
279
280 Name registerName("/localhost/nfd/rib/register");
281
282 receiveCommandInterest(registerName, addParameters);
Junxiao Shi376f7372014-11-17 18:03:31 -0700283 face->sentInterests.clear();
Vince Lehman281ded72014-08-21 12:17:08 -0500284
285 // Unregister face
286 ControlParameters removeParameters;
287 removeParameters
288 .setName("/expire")
289 .setFaceId(1)
290 .setOrigin(128);
291
292 Name unregisterName("/localhost/nfd/rib/unregister");
293
294 receiveCommandInterest(unregisterName, removeParameters);
295
296 // Reregister face
297 Name reRegisterName("/localhost/nfd/rib/register");
298 addParameters.setExpirationPeriod(ndn::time::milliseconds::max());
299 receiveCommandInterest(reRegisterName, addParameters);
300
301 nfd::tests::LimitedIo limitedIo;
302 limitedIo.run(nfd::tests::LimitedIo::UNLIMITED_OPS, time::seconds(1));
303
304 BOOST_REQUIRE_EQUAL(manager->m_managedRib.size(), 1);
305}
306
Vince Lehman26b215c2014-08-17 15:00:41 -0500307BOOST_FIXTURE_TEST_CASE(RemoveInvalidFaces, AuthorizedRibManager)
308{
309 // Register valid face
310 ControlParameters validParameters;
311 validParameters
312 .setName("/test")
313 .setFaceId(1);
314
315 Name validName("/localhost/nfd/rib/register");
316 receiveCommandInterest(validName, validParameters);
317
318 // Register invalid face
319 ControlParameters invalidParameters;
320 invalidParameters
321 .setName("/test")
322 .setFaceId(2);
323
324 Name invalidName("/localhost/nfd/rib/register");
325 receiveCommandInterest(invalidName, invalidParameters);
326
327 BOOST_REQUIRE_EQUAL(manager->m_managedRib.size(), 2);
328
329 // Receive status with only faceId: 1
330 ndn::nfd::FaceStatus status;
331 status.setFaceId(1);
332
333 shared_ptr<Data> data = nfd::tests::makeData("/localhost/nfd/faces/list");
334 data->setContent(status.wireEncode());
335
336 shared_ptr<ndn::OBufferStream> buffer = make_shared<ndn::OBufferStream>();
337 buffer->write(reinterpret_cast<const char*>(data->getContent().value()),
338 data->getContent().value_size());
339
340 manager->removeInvalidFaces(buffer);
341
342 // Run scheduler
343 nfd::tests::LimitedIo limitedIo;
344 limitedIo.run(nfd::tests::LimitedIo::UNLIMITED_OPS, time::seconds(1));
345
346 BOOST_REQUIRE_EQUAL(manager->m_managedRib.size(), 1);
347
348 Rib::const_iterator it = manager->m_managedRib.find("/test");
349 BOOST_REQUIRE(it != manager->m_managedRib.end());
350
351 shared_ptr<RibEntry> entry = it->second;
352 BOOST_CHECK_EQUAL(entry->hasFaceId(1), true);
353 BOOST_CHECK_EQUAL(entry->hasFaceId(2), false);
354}
355
Vince Lehman72446ec2014-07-09 10:50:02 -0500356BOOST_AUTO_TEST_SUITE_END()
357
358} // namespace tests
359} // namespace rib
360} // namespace nfd