blob: afef2f36a2b067ddbb45078d85ea036c39d1fb91 [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 {
74 name.append(parameters.wireEncode());
75
76 Interest command(name);
77
78 face->receive(command);
79 face->processEvents(time::milliseconds(1));
80 }
81
82public:
83 shared_ptr<RibManager> manager;
Junxiao Shi376f7372014-11-17 18:03:31 -070084 shared_ptr<ndn::util::DummyClientFace> face;
Vince Lehman72446ec2014-07-09 10:50:02 -050085
86 const Name COMMAND_PREFIX;
87 const Name::Component ADD_NEXTHOP_VERB;
88 const Name::Component REMOVE_NEXTHOP_VERB;
89};
90
91class AuthorizedRibManager : public RibManagerFixture
92{
93public:
94 AuthorizedRibManager()
95 {
96 ConfigFile config;
97 manager->setConfigFile(config);
98
99 const std::string CONFIG_STRING =
100 "rib\n"
101 "{\n"
102 " localhost_security\n"
103 " {\n"
104 " trust-anchor\n"
105 " {\n"
106 " type any\n"
107 " }\n"
108 " }"
109 "}";
110
111 config.parse(CONFIG_STRING, true, "test-rib");
112 }
113};
114
115typedef RibManagerFixture UnauthorizedRibManager;
116
Vince Lehmancd16c832014-07-23 15:14:55 -0700117BOOST_FIXTURE_TEST_SUITE(RibManager, RibManagerFixture)
Vince Lehman72446ec2014-07-09 10:50:02 -0500118
119BOOST_FIXTURE_TEST_CASE(Basic, AuthorizedRibManager)
120{
121 ControlParameters parameters;
122 parameters
123 .setName("/hello")
124 .setFaceId(1)
125 .setCost(10)
126 .setFlags(0)
127 .setOrigin(128)
128 .setExpirationPeriod(ndn::time::milliseconds::max());
129
130 Name commandName("/localhost/nfd/rib/register");
131
132 receiveCommandInterest(commandName, parameters);
133
Junxiao Shi376f7372014-11-17 18:03:31 -0700134 BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 1);
Vince Lehman72446ec2014-07-09 10:50:02 -0500135}
136
137BOOST_FIXTURE_TEST_CASE(Register, AuthorizedRibManager)
138{
139 ControlParameters parameters;
140 parameters
141 .setName("/hello")
142 .setFaceId(1)
143 .setCost(10)
144 .setFlags(0)
145 .setOrigin(128)
146 .setExpirationPeriod(ndn::time::milliseconds::max());
147
148 Name commandName("/localhost/nfd/rib/register");
149
150 receiveCommandInterest(commandName, parameters);
151
Junxiao Shi376f7372014-11-17 18:03:31 -0700152 BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 1);
Vince Lehman72446ec2014-07-09 10:50:02 -0500153
Junxiao Shi376f7372014-11-17 18:03:31 -0700154 Interest& request = face->sentInterests[0];
Vince Lehman72446ec2014-07-09 10:50:02 -0500155
156 ControlParameters extractedParameters;
157 Name::Component verb;
158 extractParameters(request, verb, extractedParameters);
159
160 BOOST_CHECK_EQUAL(verb, ADD_NEXTHOP_VERB);
161 BOOST_CHECK_EQUAL(extractedParameters.getName(), parameters.getName());
162 BOOST_CHECK_EQUAL(extractedParameters.getFaceId(), parameters.getFaceId());
163 BOOST_CHECK_EQUAL(extractedParameters.getCost(), parameters.getCost());
164}
165
166BOOST_FIXTURE_TEST_CASE(Unregister, AuthorizedRibManager)
167{
168 ControlParameters addParameters;
169 addParameters
170 .setName("/hello")
171 .setFaceId(1)
172 .setCost(10)
173 .setFlags(0)
174 .setOrigin(128)
175 .setExpirationPeriod(ndn::time::milliseconds::max());
176
177 Name registerName("/localhost/nfd/rib/register");
178
179 receiveCommandInterest(registerName, addParameters);
Junxiao Shi376f7372014-11-17 18:03:31 -0700180 face->sentInterests.clear();
Vince Lehman72446ec2014-07-09 10:50:02 -0500181
182 ControlParameters removeParameters;
183 removeParameters
184 .setName("/hello")
185 .setFaceId(1)
186 .setOrigin(128);
187
188 Name unregisterName("/localhost/nfd/rib/unregister");
189
190 receiveCommandInterest(unregisterName, removeParameters);
191
Junxiao Shi376f7372014-11-17 18:03:31 -0700192 BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 1);
Vince Lehman72446ec2014-07-09 10:50:02 -0500193
Junxiao Shi376f7372014-11-17 18:03:31 -0700194 Interest& request = face->sentInterests[0];
Vince Lehman72446ec2014-07-09 10:50:02 -0500195
196 ControlParameters extractedParameters;
197 Name::Component verb;
198 extractParameters(request, verb, extractedParameters);
199
200 BOOST_CHECK_EQUAL(verb, REMOVE_NEXTHOP_VERB);
201 BOOST_CHECK_EQUAL(extractedParameters.getName(), removeParameters.getName());
202 BOOST_CHECK_EQUAL(extractedParameters.getFaceId(), removeParameters.getFaceId());
203}
204
205BOOST_FIXTURE_TEST_CASE(UnauthorizedCommand, UnauthorizedRibManager)
206{
207 ControlParameters parameters;
208 parameters
209 .setName("/hello")
210 .setFaceId(1)
211 .setCost(10)
212 .setFlags(0)
213 .setOrigin(128)
214 .setExpirationPeriod(ndn::time::milliseconds::max());
215
216 Name commandName("/localhost/nfd/rib/register");
217
Junxiao Shi376f7372014-11-17 18:03:31 -0700218 BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 0);
Vince Lehman72446ec2014-07-09 10:50:02 -0500219
220 receiveCommandInterest(commandName, parameters);
221
Junxiao Shi376f7372014-11-17 18:03:31 -0700222 BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 0);
Vince Lehman72446ec2014-07-09 10:50:02 -0500223}
224
Vince Lehmancd16c832014-07-23 15:14:55 -0700225BOOST_FIXTURE_TEST_CASE(RibStatusRequest, AuthorizedRibManager)
226{
227 FaceEntry entry;
228 Name name("/");
229 entry.faceId = 1;
230 entry.origin = 128;
231 entry.cost = 32;
232 entry.flags = ndn::nfd::ROUTE_FLAG_CAPTURE;
233
234 ControlParameters parameters;
235 parameters
236 .setName(name)
237 .setFaceId(entry.faceId)
238 .setOrigin(entry.origin)
239 .setCost(entry.cost)
240 .setFlags(entry.flags)
241 .setExpirationPeriod(ndn::time::milliseconds::max());
242
243 Name commandName("/localhost/nfd/rib/register");
244
Junxiao Shi376f7372014-11-17 18:03:31 -0700245 BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 0);
Vince Lehmancd16c832014-07-23 15:14:55 -0700246
247 receiveCommandInterest(commandName, parameters);
Junxiao Shi376f7372014-11-17 18:03:31 -0700248 face->sentInterests.clear();
249 face->sentDatas.clear();
Vince Lehmancd16c832014-07-23 15:14:55 -0700250
251 face->receive(Interest("/localhost/nfd/rib/list"));
252 face->processEvents(time::milliseconds(1));
253
Junxiao Shi376f7372014-11-17 18:03:31 -0700254 BOOST_REQUIRE_EQUAL(face->sentDatas.size(), 1);
255 RibStatusPublisherFixture::decodeRibEntryBlock(face->sentDatas[0], name, entry);
Vince Lehmancd16c832014-07-23 15:14:55 -0700256}
257
Vince Lehman281ded72014-08-21 12:17:08 -0500258BOOST_FIXTURE_TEST_CASE(CancelExpirationEvent, AuthorizedRibManager)
259{
260 // Register face
261 ControlParameters addParameters;
262 addParameters
263 .setName("/expire")
264 .setFaceId(1)
265 .setCost(10)
266 .setFlags(0)
267 .setOrigin(128)
268 .setExpirationPeriod(ndn::time::milliseconds(500));
269
270 Name registerName("/localhost/nfd/rib/register");
271
272 receiveCommandInterest(registerName, addParameters);
Junxiao Shi376f7372014-11-17 18:03:31 -0700273 face->sentInterests.clear();
Vince Lehman281ded72014-08-21 12:17:08 -0500274
275 // Unregister face
276 ControlParameters removeParameters;
277 removeParameters
278 .setName("/expire")
279 .setFaceId(1)
280 .setOrigin(128);
281
282 Name unregisterName("/localhost/nfd/rib/unregister");
283
284 receiveCommandInterest(unregisterName, removeParameters);
285
286 // Reregister face
287 Name reRegisterName("/localhost/nfd/rib/register");
288 addParameters.setExpirationPeriod(ndn::time::milliseconds::max());
289 receiveCommandInterest(reRegisterName, addParameters);
290
291 nfd::tests::LimitedIo limitedIo;
292 limitedIo.run(nfd::tests::LimitedIo::UNLIMITED_OPS, time::seconds(1));
293
294 BOOST_REQUIRE_EQUAL(manager->m_managedRib.size(), 1);
295}
296
Vince Lehman26b215c2014-08-17 15:00:41 -0500297BOOST_FIXTURE_TEST_CASE(RemoveInvalidFaces, AuthorizedRibManager)
298{
299 // Register valid face
300 ControlParameters validParameters;
301 validParameters
302 .setName("/test")
303 .setFaceId(1);
304
305 Name validName("/localhost/nfd/rib/register");
306 receiveCommandInterest(validName, validParameters);
307
308 // Register invalid face
309 ControlParameters invalidParameters;
310 invalidParameters
311 .setName("/test")
312 .setFaceId(2);
313
314 Name invalidName("/localhost/nfd/rib/register");
315 receiveCommandInterest(invalidName, invalidParameters);
316
317 BOOST_REQUIRE_EQUAL(manager->m_managedRib.size(), 2);
318
319 // Receive status with only faceId: 1
320 ndn::nfd::FaceStatus status;
321 status.setFaceId(1);
322
323 shared_ptr<Data> data = nfd::tests::makeData("/localhost/nfd/faces/list");
324 data->setContent(status.wireEncode());
325
326 shared_ptr<ndn::OBufferStream> buffer = make_shared<ndn::OBufferStream>();
327 buffer->write(reinterpret_cast<const char*>(data->getContent().value()),
328 data->getContent().value_size());
329
330 manager->removeInvalidFaces(buffer);
331
332 // Run scheduler
333 nfd::tests::LimitedIo limitedIo;
334 limitedIo.run(nfd::tests::LimitedIo::UNLIMITED_OPS, time::seconds(1));
335
336 BOOST_REQUIRE_EQUAL(manager->m_managedRib.size(), 1);
337
338 Rib::const_iterator it = manager->m_managedRib.find("/test");
339 BOOST_REQUIRE(it != manager->m_managedRib.end());
340
341 shared_ptr<RibEntry> entry = it->second;
342 BOOST_CHECK_EQUAL(entry->hasFaceId(1), true);
343 BOOST_CHECK_EQUAL(entry->hasFaceId(2), false);
344}
345
Vince Lehman72446ec2014-07-09 10:50:02 -0500346BOOST_AUTO_TEST_SUITE_END()
347
348} // namespace tests
349} // namespace rib
350} // namespace nfd