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