blob: 593517f75bb2788e52866924589b299c1c3d28f1 [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"
27
28#include "tests/test-common.hpp"
Junxiao Shi15b12e72014-08-09 19:56:24 -070029#include "tests/dummy-client-face.hpp"
Vince Lehmancd16c832014-07-23 15:14:55 -070030#include "rib/rib-status-publisher-common.hpp"
Vince Lehman72446ec2014-07-09 10:50:02 -050031
32namespace nfd {
33namespace rib {
34namespace tests {
35
36class RibManagerFixture : public nfd::tests::BaseFixture
37{
38public:
39 RibManagerFixture()
40 : COMMAND_PREFIX("/localhost/nfd/rib")
41 , ADD_NEXTHOP_VERB("add-nexthop")
42 , REMOVE_NEXTHOP_VERB("remove-nexthop")
43 {
Junxiao Shi15b12e72014-08-09 19:56:24 -070044 face = nfd::tests::makeDummyClientFace();
Vince Lehman72446ec2014-07-09 10:50:02 -050045
46 manager = make_shared<RibManager>(ndn::ref(*face));
47 manager->registerWithNfd();
48
49 face->processEvents(time::milliseconds(1));
50 face->m_sentInterests.clear();
Vince Lehmancd613c52014-07-30 14:34:49 -050051
52 manager->activeFaces.insert(1);
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 Shi15b12e72014-08-09 19:56:24 -070084 shared_ptr<nfd::tests::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
134 BOOST_REQUIRE_EQUAL(face->m_sentInterests.size(), 1);
135}
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
152 BOOST_REQUIRE_EQUAL(face->m_sentInterests.size(), 1);
153
154 Interest& request = face->m_sentInterests[0];
155
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);
180 face->m_sentInterests.clear();
181
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
192 BOOST_REQUIRE_EQUAL(face->m_sentInterests.size(), 1);
193
194 Interest& request = face->m_sentInterests[0];
195
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
218 BOOST_REQUIRE_EQUAL(face->m_sentInterests.size(), 0);
219
220 receiveCommandInterest(commandName, parameters);
221
222 BOOST_REQUIRE_EQUAL(face->m_sentInterests.size(), 0);
223}
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
245 BOOST_REQUIRE_EQUAL(face->m_sentInterests.size(), 0);
246
247 receiveCommandInterest(commandName, parameters);
248 face->m_sentInterests.clear();
249 face->m_sentDatas.clear();
250
251 face->receive(Interest("/localhost/nfd/rib/list"));
252 face->processEvents(time::milliseconds(1));
253
254 BOOST_REQUIRE_EQUAL(face->m_sentDatas.size(), 1);
255 RibStatusPublisherFixture::decodeRibEntryBlock(face->m_sentDatas[0], name, entry);
256}
257
Vince Lehman72446ec2014-07-09 10:50:02 -0500258BOOST_AUTO_TEST_SUITE_END()
259
260} // namespace tests
261} // namespace rib
262} // namespace nfd