blob: 415be29cdd2678b9b7134d819c2bf537c8089d41 [file] [log] [blame]
Vince Lehmandbf3f702014-07-15 13:00:43 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi7357ef22016-09-07 02:39:37 +00003 * Copyright (c) 2013-2016 Regents of the University of California.
Vince Lehmandbf3f702014-07-15 13:00:43 -05004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
Junxiao Shi7357ef22016-09-07 02:39:37 +000022#include "mgmt/nfd/rib-entry.hpp"
23#include "mgmt/nfd/control-command.hpp"
Vince Lehmandbf3f702014-07-15 13:00:43 -050024
25#include "boost-test.hpp"
26
27namespace ndn {
28namespace nfd {
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080029namespace tests {
Vince Lehmandbf3f702014-07-15 13:00:43 -050030
Junxiao Shi7357ef22016-09-07 02:39:37 +000031BOOST_AUTO_TEST_SUITE(Mgmt)
32BOOST_AUTO_TEST_SUITE(Nfd)
33BOOST_AUTO_TEST_SUITE(TestRibEntry)
Vince Lehmandbf3f702014-07-15 13:00:43 -050034
35const uint8_t RouteData[] =
36{
37 0x81, 0x10, 0x69, 0x01, 0x01, 0x6f, 0x01, 0x80, 0x6a, 0x01, 0x64, 0x6c, 0x01, 0x02,
38 0x6d, 0x02, 0x27, 0x10
39};
40
41const uint8_t RouteInfiniteExpirationPeriod[] =
42{
43 0x81, 0x0C, 0x69, 0x01, 0x01, 0x6f, 0x01, 0x80, 0x6a, 0x01, 0x64, 0x6c, 0x01, 0x02
44};
45
46const uint8_t RibEntryData[] =
47{
48 // Header + Name
49 0x80, 0x34, 0x07, 0x0e, 0x08, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f,
50 0x08, 0x05, 0x77, 0x6f, 0x72, 0x6c, 0x64,
51 // Route
52 0x81, 0x10, 0x69, 0x01, 0x01, 0x6f, 0x01, 0x80, 0x6a, 0x01, 0x64, 0x6c, 0x01, 0x02,
53 0x6d, 0x02, 0x27, 0x10,
54 // Route
55 0x81, 0x10, 0x69, 0x01, 0x02, 0x6f, 0x01, 0x00, 0x6a, 0x01, 0x20, 0x6c, 0x01, 0x01,
56 0x6d, 0x02, 0x13, 0x88
57};
58
59const uint8_t RibEntryInfiniteExpirationPeriod[] =
60{
61 // Header + Name
62 0x80, 0x30, 0x07, 0x0e, 0x08, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f,
63 0x08, 0x05, 0x77, 0x6f, 0x72, 0x6c, 0x64,
64 // Route
65 0x81, 0x10, 0x69, 0x01, 0x01, 0x6f, 0x01, 0x80, 0x6a, 0x01, 0x64, 0x6c, 0x01, 0x02,
66 0x6d, 0x02, 0x27, 0x10,
67 // Route with no ExpirationPeriod
68 0x81, 0x0C, 0x69, 0x01, 0x02, 0x6f, 0x01, 0x00, 0x6a, 0x01, 0x20, 0x6c, 0x01, 0x01,
69};
70
71BOOST_AUTO_TEST_CASE(RouteEncode)
72{
73 Route route;
74 route.setFaceId(1);
75 route.setOrigin(128);
76 route.setCost(100);
77 route.setFlags(ndn::nfd::ROUTE_FLAG_CAPTURE);
78 route.setExpirationPeriod(time::milliseconds(10000));
79
80 const Block& wire = route.wireEncode();
81
82 BOOST_REQUIRE_EQUAL_COLLECTIONS(RouteData,
83 RouteData + sizeof(RouteData),
84 wire.begin(), wire.end());
85}
86
87BOOST_AUTO_TEST_CASE(RouteDecode)
88{
89 Route route;
90
91 BOOST_REQUIRE_NO_THROW(route.wireDecode(Block(RouteData, sizeof(RouteData))));
92
93 BOOST_REQUIRE_EQUAL(route.getFaceId(), 1);
94 BOOST_REQUIRE_EQUAL(route.getOrigin(), 128);
95 BOOST_REQUIRE_EQUAL(route.getCost(), 100);
96 BOOST_REQUIRE_EQUAL(route.getFlags(), static_cast<uint64_t>(ndn::nfd::ROUTE_FLAG_CAPTURE));
97 BOOST_REQUIRE_EQUAL(route.getExpirationPeriod(), time::milliseconds(10000));
98 BOOST_REQUIRE_EQUAL(route.hasInfiniteExpirationPeriod(), false);
99}
100
101BOOST_AUTO_TEST_CASE(RouteInfiniteExpirationPeriodEncode)
102{
103 Route route;
104 route.setFaceId(1);
105 route.setOrigin(128);
106 route.setCost(100);
107 route.setFlags(ndn::nfd::ROUTE_FLAG_CAPTURE);
108 route.setExpirationPeriod(Route::INFINITE_EXPIRATION_PERIOD);
109
110 const Block& wire = route.wireEncode();
111
112 BOOST_REQUIRE_EQUAL_COLLECTIONS(RouteInfiniteExpirationPeriod,
113 RouteInfiniteExpirationPeriod + sizeof(RouteInfiniteExpirationPeriod),
114 wire.begin(), wire.end());
115}
116
117BOOST_AUTO_TEST_CASE(RouteInfiniteExpirationPeriodDecode)
118{
119 Route route;
120
121 BOOST_REQUIRE_NO_THROW(route.wireDecode(Block(RouteInfiniteExpirationPeriod,
122 sizeof(RouteInfiniteExpirationPeriod))));
123
124 BOOST_REQUIRE_EQUAL(route.getFaceId(), 1);
125 BOOST_REQUIRE_EQUAL(route.getOrigin(), 128);
126 BOOST_REQUIRE_EQUAL(route.getCost(), 100);
127 BOOST_REQUIRE_EQUAL(route.getFlags(), static_cast<uint64_t>(ndn::nfd::ROUTE_FLAG_CAPTURE));
128 BOOST_REQUIRE_EQUAL(route.getExpirationPeriod(), Route::INFINITE_EXPIRATION_PERIOD);
129 BOOST_REQUIRE_EQUAL(route.hasInfiniteExpirationPeriod(), true);
130}
131
132BOOST_AUTO_TEST_CASE(RouteOutputStream)
133{
134 Route route;
135 route.setFaceId(1);
136 route.setOrigin(128);
137 route.setCost(100);
138 route.setFlags(ndn::nfd::ROUTE_FLAG_CAPTURE);
139 route.setExpirationPeriod(time::milliseconds(10000));
140
141 std::ostringstream os;
142 os << route;
143
144 BOOST_CHECK_EQUAL(os.str(), "Route(FaceId: 1, Origin: 128, Cost: 100, "
145 "Flags: 2, ExpirationPeriod: 10000 milliseconds)");
146}
147
148BOOST_AUTO_TEST_CASE(RibEntryEncode)
149{
150 RibEntry entry;
151 entry.setName("/hello/world");
152
153 Route route1;
154 route1.setFaceId(1);
155 route1.setOrigin(128);
156 route1.setCost(100);
157 route1.setFlags(ndn::nfd::ROUTE_FLAG_CAPTURE);
158 route1.setExpirationPeriod(time::milliseconds(10000));
159 entry.addRoute(route1);
160
161 Route route2;
162 route2.setFaceId(2);
163 route2.setOrigin(0);
164 route2.setCost(32);
165 route2.setFlags(ndn::nfd::ROUTE_FLAG_CHILD_INHERIT);
166 route2.setExpirationPeriod(time::milliseconds(5000));
167 entry.addRoute(route2);
168
169 const Block& wire = entry.wireEncode();
170
171 BOOST_CHECK_EQUAL_COLLECTIONS(RibEntryData,
172 RibEntryData + sizeof(RibEntryData),
173 wire.begin(), wire.end());
174}
175
176BOOST_AUTO_TEST_CASE(RibEntryDecode)
177{
178 RibEntry entry;
179 BOOST_REQUIRE_NO_THROW(entry.wireDecode(Block(RibEntryData,
180 sizeof(RibEntryData))));
181
182 BOOST_CHECK_EQUAL(entry.getName(), "/hello/world");
183 BOOST_CHECK_EQUAL(entry.getRoutes().size(), 2);
184
185 std::list<Route> routes = entry.getRoutes();
186
187 std::list<Route>::const_iterator it = routes.begin();
188 BOOST_CHECK_EQUAL(it->getFaceId(), 1);
189 BOOST_CHECK_EQUAL(it->getOrigin(), 128);
190 BOOST_CHECK_EQUAL(it->getCost(), 100);
191 BOOST_CHECK_EQUAL(it->getFlags(), static_cast<uint64_t>(ndn::nfd::ROUTE_FLAG_CAPTURE));
192 BOOST_CHECK_EQUAL(it->getExpirationPeriod(), time::milliseconds(10000));
193 BOOST_CHECK_EQUAL(it->hasInfiniteExpirationPeriod(), false);
194
195 ++it;
196 BOOST_CHECK_EQUAL(it->getFaceId(), 2);
197 BOOST_CHECK_EQUAL(it->getOrigin(), 0);
198 BOOST_CHECK_EQUAL(it->getCost(), 32);
199 BOOST_CHECK_EQUAL(it->getFlags(), static_cast<uint64_t>(ndn::nfd::ROUTE_FLAG_CHILD_INHERIT));
200 BOOST_CHECK_EQUAL(it->getExpirationPeriod(), time::milliseconds(5000));
201 BOOST_CHECK_EQUAL(it->hasInfiniteExpirationPeriod(), false);
202}
203
204BOOST_AUTO_TEST_CASE(RibEntryInfiniteExpirationPeriodEncode)
205{
206 RibEntry entry;
207 entry.setName("/hello/world");
208
209 Route route1;
210 route1.setFaceId(1);
211 route1.setOrigin(128);
212 route1.setCost(100);
213 route1.setFlags(ndn::nfd::ROUTE_FLAG_CAPTURE);
214 route1.setExpirationPeriod(time::milliseconds(10000));
215 entry.addRoute(route1);
216
217 Route route2;
218 route2.setFaceId(2);
219 route2.setOrigin(0);
220 route2.setCost(32);
221 route2.setFlags(ndn::nfd::ROUTE_FLAG_CHILD_INHERIT);
222 route2.setExpirationPeriod(Route::INFINITE_EXPIRATION_PERIOD);
223 entry.addRoute(route2);
224
225 const Block& wire = entry.wireEncode();
226
227 BOOST_CHECK_EQUAL_COLLECTIONS(RibEntryInfiniteExpirationPeriod,
228 RibEntryInfiniteExpirationPeriod +
229 sizeof(RibEntryInfiniteExpirationPeriod),
230 wire.begin(), wire.end());
231}
232
233BOOST_AUTO_TEST_CASE(RibEntryInfiniteExpirationPeriodDecode)
234{
235 RibEntry entry;
236 BOOST_REQUIRE_NO_THROW(entry.wireDecode(Block(RibEntryInfiniteExpirationPeriod,
237 sizeof(RibEntryInfiniteExpirationPeriod))));
238
239 BOOST_CHECK_EQUAL(entry.getName(), "/hello/world");
240 BOOST_CHECK_EQUAL(entry.getRoutes().size(), 2);
241
242 std::list<Route> routes = entry.getRoutes();
243
244 std::list<Route>::const_iterator it = routes.begin();
245 BOOST_CHECK_EQUAL(it->getFaceId(), 1);
246 BOOST_CHECK_EQUAL(it->getOrigin(), 128);
247 BOOST_CHECK_EQUAL(it->getCost(), 100);
248 BOOST_CHECK_EQUAL(it->getFlags(), static_cast<uint64_t>(ndn::nfd::ROUTE_FLAG_CAPTURE));
249 BOOST_CHECK_EQUAL(it->getExpirationPeriod(), time::milliseconds(10000));
250 BOOST_CHECK_EQUAL(it->hasInfiniteExpirationPeriod(), false);
251
252 ++it;
253 BOOST_CHECK_EQUAL(it->getFaceId(), 2);
254 BOOST_CHECK_EQUAL(it->getOrigin(), 0);
255 BOOST_CHECK_EQUAL(it->getCost(), 32);
256 BOOST_CHECK_EQUAL(it->getFlags(), static_cast<uint64_t>(ndn::nfd::ROUTE_FLAG_CHILD_INHERIT));
257 BOOST_CHECK_EQUAL(it->getExpirationPeriod(), Route::INFINITE_EXPIRATION_PERIOD);
258 BOOST_CHECK_EQUAL(it->hasInfiniteExpirationPeriod(), true);
259}
260
261BOOST_AUTO_TEST_CASE(RibEntryClear)
262{
263 RibEntry entry;
264 entry.setName("/hello/world");
265
266 Route route1;
267 route1.setFaceId(1);
268 route1.setOrigin(128);
269 route1.setCost(100);
270 route1.setFlags(ndn::nfd::ROUTE_FLAG_CAPTURE);
271 route1.setExpirationPeriod(time::milliseconds(10000));
272 entry.addRoute(route1);
273 BOOST_REQUIRE_EQUAL(entry.getRoutes().size(), 1);
274
275 std::list<Route> routes = entry.getRoutes();
276
277 std::list<Route>::const_iterator it = routes.begin();
278 BOOST_CHECK_EQUAL(it->getFaceId(), 1);
279 BOOST_CHECK_EQUAL(it->getOrigin(), 128);
280 BOOST_CHECK_EQUAL(it->getCost(), 100);
281 BOOST_CHECK_EQUAL(it->getFlags(), static_cast<uint64_t>(ndn::nfd::ROUTE_FLAG_CAPTURE));
282 BOOST_CHECK_EQUAL(it->getExpirationPeriod(), time::milliseconds(10000));
283 BOOST_CHECK_EQUAL(it->hasInfiniteExpirationPeriod(), false);
284
285 entry.clearRoutes();
286 BOOST_CHECK_EQUAL(entry.getRoutes().size(), 0);
287
288 Route route2;
289 route2.setFaceId(2);
290 route2.setOrigin(0);
291 route2.setCost(32);
292 route2.setFlags(ndn::nfd::ROUTE_FLAG_CHILD_INHERIT);
293 route2.setExpirationPeriod(Route::INFINITE_EXPIRATION_PERIOD);
294 entry.addRoute(route2);
295 BOOST_REQUIRE_EQUAL(entry.getRoutes().size(), 1);
296
297 routes = entry.getRoutes();
298
299 it = routes.begin();
300 BOOST_CHECK_EQUAL(it->getFaceId(), 2);
301 BOOST_CHECK_EQUAL(it->getOrigin(), 0);
302 BOOST_CHECK_EQUAL(it->getCost(), 32);
303 BOOST_CHECK_EQUAL(it->getFlags(), static_cast<uint64_t>(ndn::nfd::ROUTE_FLAG_CHILD_INHERIT));
304 BOOST_CHECK_EQUAL(it->getExpirationPeriod(), Route::INFINITE_EXPIRATION_PERIOD);
305 BOOST_CHECK_EQUAL(it->hasInfiniteExpirationPeriod(), true);
306}
307
308BOOST_AUTO_TEST_CASE(RibEntryOutputStream)
309{
310 RibEntry entry;
311 entry.setName("/hello/world");
312
313 Route route1;
314 route1.setFaceId(1);
315 route1.setOrigin(128);
316 route1.setCost(100);
317 route1.setFlags(ndn::nfd::ROUTE_FLAG_CAPTURE);
318 route1.setExpirationPeriod(time::milliseconds(10000));
319 entry.addRoute(route1);
320
321 Route route2;
322 route2.setFaceId(2);
323 route2.setOrigin(0);
324 route2.setCost(32);
325 route2.setFlags(ndn::nfd::ROUTE_FLAG_CHILD_INHERIT);
326 route2.setExpirationPeriod(Route::INFINITE_EXPIRATION_PERIOD);
327 entry.addRoute(route2);
328
329 std::ostringstream os;
330 os << entry;
331
332 BOOST_CHECK_EQUAL(os.str(), "RibEntry{\n"
333 " Name: /hello/world\n"
334 " Route(FaceId: 1, Origin: 128, Cost: 100, "
335 "Flags: 2, ExpirationPeriod: 10000 milliseconds)\n"
336 " Route(FaceId: 2, Origin: 0, Cost: 32, "
337 "Flags: 1, ExpirationPeriod: Infinity)\n"
338 "}");
339}
340
Junxiao Shi7357ef22016-09-07 02:39:37 +0000341BOOST_AUTO_TEST_SUITE_END() // TestRibEntry
342BOOST_AUTO_TEST_SUITE_END() // Nfd
343BOOST_AUTO_TEST_SUITE_END() // Mgmt
Vince Lehmandbf3f702014-07-15 13:00:43 -0500344
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800345} // namespace tests
Vince Lehmandbf3f702014-07-15 13:00:43 -0500346} // namespace nfd
347} // namespace ndn