blob: 4018386147400f58d96f6991d87e2086f98984cf [file] [log] [blame]
Vince Lehmandbf3f702014-07-15 13:00:43 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Spyridon Mastorakis429634f2015-02-19 17:35:33 -08003 * Copyright (c) 2013-2015 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
22#include "management/nfd-rib-entry.hpp"
23#include "management/nfd-control-command.hpp"
24
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
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080031BOOST_AUTO_TEST_SUITE(ManagementNfdRibEntry)
Vince Lehmandbf3f702014-07-15 13:00:43 -050032
33const uint8_t RouteData[] =
34{
35 0x81, 0x10, 0x69, 0x01, 0x01, 0x6f, 0x01, 0x80, 0x6a, 0x01, 0x64, 0x6c, 0x01, 0x02,
36 0x6d, 0x02, 0x27, 0x10
37};
38
39const uint8_t RouteInfiniteExpirationPeriod[] =
40{
41 0x81, 0x0C, 0x69, 0x01, 0x01, 0x6f, 0x01, 0x80, 0x6a, 0x01, 0x64, 0x6c, 0x01, 0x02
42};
43
44const uint8_t RibEntryData[] =
45{
46 // Header + Name
47 0x80, 0x34, 0x07, 0x0e, 0x08, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f,
48 0x08, 0x05, 0x77, 0x6f, 0x72, 0x6c, 0x64,
49 // Route
50 0x81, 0x10, 0x69, 0x01, 0x01, 0x6f, 0x01, 0x80, 0x6a, 0x01, 0x64, 0x6c, 0x01, 0x02,
51 0x6d, 0x02, 0x27, 0x10,
52 // Route
53 0x81, 0x10, 0x69, 0x01, 0x02, 0x6f, 0x01, 0x00, 0x6a, 0x01, 0x20, 0x6c, 0x01, 0x01,
54 0x6d, 0x02, 0x13, 0x88
55};
56
57const uint8_t RibEntryInfiniteExpirationPeriod[] =
58{
59 // Header + Name
60 0x80, 0x30, 0x07, 0x0e, 0x08, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f,
61 0x08, 0x05, 0x77, 0x6f, 0x72, 0x6c, 0x64,
62 // Route
63 0x81, 0x10, 0x69, 0x01, 0x01, 0x6f, 0x01, 0x80, 0x6a, 0x01, 0x64, 0x6c, 0x01, 0x02,
64 0x6d, 0x02, 0x27, 0x10,
65 // Route with no ExpirationPeriod
66 0x81, 0x0C, 0x69, 0x01, 0x02, 0x6f, 0x01, 0x00, 0x6a, 0x01, 0x20, 0x6c, 0x01, 0x01,
67};
68
69BOOST_AUTO_TEST_CASE(RouteEncode)
70{
71 Route route;
72 route.setFaceId(1);
73 route.setOrigin(128);
74 route.setCost(100);
75 route.setFlags(ndn::nfd::ROUTE_FLAG_CAPTURE);
76 route.setExpirationPeriod(time::milliseconds(10000));
77
78 const Block& wire = route.wireEncode();
79
80 BOOST_REQUIRE_EQUAL_COLLECTIONS(RouteData,
81 RouteData + sizeof(RouteData),
82 wire.begin(), wire.end());
83}
84
85BOOST_AUTO_TEST_CASE(RouteDecode)
86{
87 Route route;
88
89 BOOST_REQUIRE_NO_THROW(route.wireDecode(Block(RouteData, sizeof(RouteData))));
90
91 BOOST_REQUIRE_EQUAL(route.getFaceId(), 1);
92 BOOST_REQUIRE_EQUAL(route.getOrigin(), 128);
93 BOOST_REQUIRE_EQUAL(route.getCost(), 100);
94 BOOST_REQUIRE_EQUAL(route.getFlags(), static_cast<uint64_t>(ndn::nfd::ROUTE_FLAG_CAPTURE));
95 BOOST_REQUIRE_EQUAL(route.getExpirationPeriod(), time::milliseconds(10000));
96 BOOST_REQUIRE_EQUAL(route.hasInfiniteExpirationPeriod(), false);
97}
98
99BOOST_AUTO_TEST_CASE(RouteInfiniteExpirationPeriodEncode)
100{
101 Route route;
102 route.setFaceId(1);
103 route.setOrigin(128);
104 route.setCost(100);
105 route.setFlags(ndn::nfd::ROUTE_FLAG_CAPTURE);
106 route.setExpirationPeriod(Route::INFINITE_EXPIRATION_PERIOD);
107
108 const Block& wire = route.wireEncode();
109
110 BOOST_REQUIRE_EQUAL_COLLECTIONS(RouteInfiniteExpirationPeriod,
111 RouteInfiniteExpirationPeriod + sizeof(RouteInfiniteExpirationPeriod),
112 wire.begin(), wire.end());
113}
114
115BOOST_AUTO_TEST_CASE(RouteInfiniteExpirationPeriodDecode)
116{
117 Route route;
118
119 BOOST_REQUIRE_NO_THROW(route.wireDecode(Block(RouteInfiniteExpirationPeriod,
120 sizeof(RouteInfiniteExpirationPeriod))));
121
122 BOOST_REQUIRE_EQUAL(route.getFaceId(), 1);
123 BOOST_REQUIRE_EQUAL(route.getOrigin(), 128);
124 BOOST_REQUIRE_EQUAL(route.getCost(), 100);
125 BOOST_REQUIRE_EQUAL(route.getFlags(), static_cast<uint64_t>(ndn::nfd::ROUTE_FLAG_CAPTURE));
126 BOOST_REQUIRE_EQUAL(route.getExpirationPeriod(), Route::INFINITE_EXPIRATION_PERIOD);
127 BOOST_REQUIRE_EQUAL(route.hasInfiniteExpirationPeriod(), true);
128}
129
130BOOST_AUTO_TEST_CASE(RouteOutputStream)
131{
132 Route route;
133 route.setFaceId(1);
134 route.setOrigin(128);
135 route.setCost(100);
136 route.setFlags(ndn::nfd::ROUTE_FLAG_CAPTURE);
137 route.setExpirationPeriod(time::milliseconds(10000));
138
139 std::ostringstream os;
140 os << route;
141
142 BOOST_CHECK_EQUAL(os.str(), "Route(FaceId: 1, Origin: 128, Cost: 100, "
143 "Flags: 2, ExpirationPeriod: 10000 milliseconds)");
144}
145
146BOOST_AUTO_TEST_CASE(RibEntryEncode)
147{
148 RibEntry entry;
149 entry.setName("/hello/world");
150
151 Route route1;
152 route1.setFaceId(1);
153 route1.setOrigin(128);
154 route1.setCost(100);
155 route1.setFlags(ndn::nfd::ROUTE_FLAG_CAPTURE);
156 route1.setExpirationPeriod(time::milliseconds(10000));
157 entry.addRoute(route1);
158
159 Route route2;
160 route2.setFaceId(2);
161 route2.setOrigin(0);
162 route2.setCost(32);
163 route2.setFlags(ndn::nfd::ROUTE_FLAG_CHILD_INHERIT);
164 route2.setExpirationPeriod(time::milliseconds(5000));
165 entry.addRoute(route2);
166
167 const Block& wire = entry.wireEncode();
168
169 BOOST_CHECK_EQUAL_COLLECTIONS(RibEntryData,
170 RibEntryData + sizeof(RibEntryData),
171 wire.begin(), wire.end());
172}
173
174BOOST_AUTO_TEST_CASE(RibEntryDecode)
175{
176 RibEntry entry;
177 BOOST_REQUIRE_NO_THROW(entry.wireDecode(Block(RibEntryData,
178 sizeof(RibEntryData))));
179
180 BOOST_CHECK_EQUAL(entry.getName(), "/hello/world");
181 BOOST_CHECK_EQUAL(entry.getRoutes().size(), 2);
182
183 std::list<Route> routes = entry.getRoutes();
184
185 std::list<Route>::const_iterator it = routes.begin();
186 BOOST_CHECK_EQUAL(it->getFaceId(), 1);
187 BOOST_CHECK_EQUAL(it->getOrigin(), 128);
188 BOOST_CHECK_EQUAL(it->getCost(), 100);
189 BOOST_CHECK_EQUAL(it->getFlags(), static_cast<uint64_t>(ndn::nfd::ROUTE_FLAG_CAPTURE));
190 BOOST_CHECK_EQUAL(it->getExpirationPeriod(), time::milliseconds(10000));
191 BOOST_CHECK_EQUAL(it->hasInfiniteExpirationPeriod(), false);
192
193 ++it;
194 BOOST_CHECK_EQUAL(it->getFaceId(), 2);
195 BOOST_CHECK_EQUAL(it->getOrigin(), 0);
196 BOOST_CHECK_EQUAL(it->getCost(), 32);
197 BOOST_CHECK_EQUAL(it->getFlags(), static_cast<uint64_t>(ndn::nfd::ROUTE_FLAG_CHILD_INHERIT));
198 BOOST_CHECK_EQUAL(it->getExpirationPeriod(), time::milliseconds(5000));
199 BOOST_CHECK_EQUAL(it->hasInfiniteExpirationPeriod(), false);
200}
201
202BOOST_AUTO_TEST_CASE(RibEntryInfiniteExpirationPeriodEncode)
203{
204 RibEntry entry;
205 entry.setName("/hello/world");
206
207 Route route1;
208 route1.setFaceId(1);
209 route1.setOrigin(128);
210 route1.setCost(100);
211 route1.setFlags(ndn::nfd::ROUTE_FLAG_CAPTURE);
212 route1.setExpirationPeriod(time::milliseconds(10000));
213 entry.addRoute(route1);
214
215 Route route2;
216 route2.setFaceId(2);
217 route2.setOrigin(0);
218 route2.setCost(32);
219 route2.setFlags(ndn::nfd::ROUTE_FLAG_CHILD_INHERIT);
220 route2.setExpirationPeriod(Route::INFINITE_EXPIRATION_PERIOD);
221 entry.addRoute(route2);
222
223 const Block& wire = entry.wireEncode();
224
225 BOOST_CHECK_EQUAL_COLLECTIONS(RibEntryInfiniteExpirationPeriod,
226 RibEntryInfiniteExpirationPeriod +
227 sizeof(RibEntryInfiniteExpirationPeriod),
228 wire.begin(), wire.end());
229}
230
231BOOST_AUTO_TEST_CASE(RibEntryInfiniteExpirationPeriodDecode)
232{
233 RibEntry entry;
234 BOOST_REQUIRE_NO_THROW(entry.wireDecode(Block(RibEntryInfiniteExpirationPeriod,
235 sizeof(RibEntryInfiniteExpirationPeriod))));
236
237 BOOST_CHECK_EQUAL(entry.getName(), "/hello/world");
238 BOOST_CHECK_EQUAL(entry.getRoutes().size(), 2);
239
240 std::list<Route> routes = entry.getRoutes();
241
242 std::list<Route>::const_iterator it = routes.begin();
243 BOOST_CHECK_EQUAL(it->getFaceId(), 1);
244 BOOST_CHECK_EQUAL(it->getOrigin(), 128);
245 BOOST_CHECK_EQUAL(it->getCost(), 100);
246 BOOST_CHECK_EQUAL(it->getFlags(), static_cast<uint64_t>(ndn::nfd::ROUTE_FLAG_CAPTURE));
247 BOOST_CHECK_EQUAL(it->getExpirationPeriod(), time::milliseconds(10000));
248 BOOST_CHECK_EQUAL(it->hasInfiniteExpirationPeriod(), false);
249
250 ++it;
251 BOOST_CHECK_EQUAL(it->getFaceId(), 2);
252 BOOST_CHECK_EQUAL(it->getOrigin(), 0);
253 BOOST_CHECK_EQUAL(it->getCost(), 32);
254 BOOST_CHECK_EQUAL(it->getFlags(), static_cast<uint64_t>(ndn::nfd::ROUTE_FLAG_CHILD_INHERIT));
255 BOOST_CHECK_EQUAL(it->getExpirationPeriod(), Route::INFINITE_EXPIRATION_PERIOD);
256 BOOST_CHECK_EQUAL(it->hasInfiniteExpirationPeriod(), true);
257}
258
259BOOST_AUTO_TEST_CASE(RibEntryClear)
260{
261 RibEntry entry;
262 entry.setName("/hello/world");
263
264 Route route1;
265 route1.setFaceId(1);
266 route1.setOrigin(128);
267 route1.setCost(100);
268 route1.setFlags(ndn::nfd::ROUTE_FLAG_CAPTURE);
269 route1.setExpirationPeriod(time::milliseconds(10000));
270 entry.addRoute(route1);
271 BOOST_REQUIRE_EQUAL(entry.getRoutes().size(), 1);
272
273 std::list<Route> routes = entry.getRoutes();
274
275 std::list<Route>::const_iterator it = routes.begin();
276 BOOST_CHECK_EQUAL(it->getFaceId(), 1);
277 BOOST_CHECK_EQUAL(it->getOrigin(), 128);
278 BOOST_CHECK_EQUAL(it->getCost(), 100);
279 BOOST_CHECK_EQUAL(it->getFlags(), static_cast<uint64_t>(ndn::nfd::ROUTE_FLAG_CAPTURE));
280 BOOST_CHECK_EQUAL(it->getExpirationPeriod(), time::milliseconds(10000));
281 BOOST_CHECK_EQUAL(it->hasInfiniteExpirationPeriod(), false);
282
283 entry.clearRoutes();
284 BOOST_CHECK_EQUAL(entry.getRoutes().size(), 0);
285
286 Route route2;
287 route2.setFaceId(2);
288 route2.setOrigin(0);
289 route2.setCost(32);
290 route2.setFlags(ndn::nfd::ROUTE_FLAG_CHILD_INHERIT);
291 route2.setExpirationPeriod(Route::INFINITE_EXPIRATION_PERIOD);
292 entry.addRoute(route2);
293 BOOST_REQUIRE_EQUAL(entry.getRoutes().size(), 1);
294
295 routes = entry.getRoutes();
296
297 it = routes.begin();
298 BOOST_CHECK_EQUAL(it->getFaceId(), 2);
299 BOOST_CHECK_EQUAL(it->getOrigin(), 0);
300 BOOST_CHECK_EQUAL(it->getCost(), 32);
301 BOOST_CHECK_EQUAL(it->getFlags(), static_cast<uint64_t>(ndn::nfd::ROUTE_FLAG_CHILD_INHERIT));
302 BOOST_CHECK_EQUAL(it->getExpirationPeriod(), Route::INFINITE_EXPIRATION_PERIOD);
303 BOOST_CHECK_EQUAL(it->hasInfiniteExpirationPeriod(), true);
304}
305
306BOOST_AUTO_TEST_CASE(RibEntryOutputStream)
307{
308 RibEntry entry;
309 entry.setName("/hello/world");
310
311 Route route1;
312 route1.setFaceId(1);
313 route1.setOrigin(128);
314 route1.setCost(100);
315 route1.setFlags(ndn::nfd::ROUTE_FLAG_CAPTURE);
316 route1.setExpirationPeriod(time::milliseconds(10000));
317 entry.addRoute(route1);
318
319 Route route2;
320 route2.setFaceId(2);
321 route2.setOrigin(0);
322 route2.setCost(32);
323 route2.setFlags(ndn::nfd::ROUTE_FLAG_CHILD_INHERIT);
324 route2.setExpirationPeriod(Route::INFINITE_EXPIRATION_PERIOD);
325 entry.addRoute(route2);
326
327 std::ostringstream os;
328 os << entry;
329
330 BOOST_CHECK_EQUAL(os.str(), "RibEntry{\n"
331 " Name: /hello/world\n"
332 " Route(FaceId: 1, Origin: 128, Cost: 100, "
333 "Flags: 2, ExpirationPeriod: 10000 milliseconds)\n"
334 " Route(FaceId: 2, Origin: 0, Cost: 32, "
335 "Flags: 1, ExpirationPeriod: Infinity)\n"
336 "}");
337}
338
339BOOST_AUTO_TEST_SUITE_END()
340
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800341} // namespace tests
Vince Lehmandbf3f702014-07-15 13:00:43 -0500342} // namespace nfd
343} // namespace ndn