blob: f2ab51a83ebd7fb20c2b6b1f772ab88eaa363004 [file] [log] [blame]
Vince Lehmandbf3f702014-07-15 13:00:43 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento0f830802018-01-16 23:58:58 -05002/*
3 * Copyright (c) 2013-2018 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"
Vince Lehmandbf3f702014-07-15 13:00:43 -050023
24#include "boost-test.hpp"
Davide Pesavento484bbe52017-02-15 00:03:46 -050025#include <boost/lexical_cast.hpp>
Vince Lehmandbf3f702014-07-15 13:00:43 -050026
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
Davide Pesavento484bbe52017-02-15 00:03:46 -050035static Route
36makeRoute()
Vince Lehmandbf3f702014-07-15 13:00:43 -050037{
Davide Pesavento484bbe52017-02-15 00:03:46 -050038 return Route()
39 .setFaceId(1)
Davide Pesaventoe8e48c22017-04-13 00:35:33 -040040 .setOrigin(ROUTE_ORIGIN_NLSR)
Davide Pesavento484bbe52017-02-15 00:03:46 -050041 .setCost(100)
Davide Pesaventoe8e48c22017-04-13 00:35:33 -040042 .setFlags(ROUTE_FLAG_CAPTURE);
Davide Pesavento484bbe52017-02-15 00:03:46 -050043}
Vince Lehmandbf3f702014-07-15 13:00:43 -050044
Davide Pesavento484bbe52017-02-15 00:03:46 -050045static RibEntry
46makeRibEntry()
Vince Lehmandbf3f702014-07-15 13:00:43 -050047{
Davide Pesavento484bbe52017-02-15 00:03:46 -050048 return RibEntry()
49 .setName("/hello/world")
50 .addRoute(makeRoute()
Davide Pesavento0f830802018-01-16 23:58:58 -050051 .setExpirationPeriod(10_s));
Davide Pesavento484bbe52017-02-15 00:03:46 -050052}
Vince Lehmandbf3f702014-07-15 13:00:43 -050053
54BOOST_AUTO_TEST_CASE(RouteEncode)
55{
Davide Pesavento484bbe52017-02-15 00:03:46 -050056 Route route1 = makeRoute();
Davide Pesavento0f830802018-01-16 23:58:58 -050057 route1.setExpirationPeriod(10_s);
Davide Pesavento484bbe52017-02-15 00:03:46 -050058 const Block& wire = route1.wireEncode();
Vince Lehmandbf3f702014-07-15 13:00:43 -050059
Davide Pesavento484bbe52017-02-15 00:03:46 -050060 static const uint8_t expected[] = {
61 0x81, 0x10, 0x69, 0x01, 0x01, 0x6f, 0x01, 0x80, 0x6a, 0x01, 0x64, 0x6c, 0x01, 0x02,
62 0x6d, 0x02, 0x27, 0x10
63 };
64 BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
65 wire.begin(), wire.end());
Vince Lehmandbf3f702014-07-15 13:00:43 -050066
Davide Pesavento484bbe52017-02-15 00:03:46 -050067 Route route2(wire);
68 BOOST_CHECK_EQUAL(route1, route2);
Vince Lehmandbf3f702014-07-15 13:00:43 -050069}
70
Davide Pesavento484bbe52017-02-15 00:03:46 -050071BOOST_AUTO_TEST_CASE(RouteNoExpirationPeriodEncode)
Vince Lehmandbf3f702014-07-15 13:00:43 -050072{
Davide Pesavento484bbe52017-02-15 00:03:46 -050073 Route route1 = makeRoute();
74 const Block& wire = route1.wireEncode();
Vince Lehmandbf3f702014-07-15 13:00:43 -050075
Davide Pesavento484bbe52017-02-15 00:03:46 -050076 static const uint8_t expected[] = {
77 0x81, 0x0C, 0x69, 0x01, 0x01, 0x6f, 0x01, 0x80, 0x6a, 0x01, 0x64, 0x6c, 0x01, 0x02
78 };
79 BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
80 wire.begin(), wire.end());
Vince Lehmandbf3f702014-07-15 13:00:43 -050081
Davide Pesavento484bbe52017-02-15 00:03:46 -050082 Route route2(wire);
83 BOOST_CHECK_EQUAL(route1, route2);
Vince Lehmandbf3f702014-07-15 13:00:43 -050084}
85
Davide Pesavento156c1ea2017-03-19 16:09:33 -040086BOOST_AUTO_TEST_CASE(RouteExpirationPeriod)
87{
88 Route route;
89 BOOST_CHECK_EQUAL(route.hasExpirationPeriod(), false);
90 BOOST_CHECK_EQUAL(route.getExpirationPeriod(), time::milliseconds::max());
91
Davide Pesavento0f830802018-01-16 23:58:58 -050092 route.setExpirationPeriod(1_min);
Davide Pesavento156c1ea2017-03-19 16:09:33 -040093 BOOST_CHECK_EQUAL(route.hasExpirationPeriod(), true);
Davide Pesavento0f830802018-01-16 23:58:58 -050094 BOOST_CHECK_EQUAL(route.getExpirationPeriod(), 1_min);
Davide Pesavento156c1ea2017-03-19 16:09:33 -040095
96 route.setExpirationPeriod(time::milliseconds::max());
97 BOOST_CHECK_EQUAL(route.hasExpirationPeriod(), false);
98 BOOST_CHECK_EQUAL(route.getExpirationPeriod(), time::milliseconds::max());
99
Davide Pesavento0f830802018-01-16 23:58:58 -0500100 route.setExpirationPeriod(1_min);
Davide Pesavento156c1ea2017-03-19 16:09:33 -0400101 BOOST_CHECK_EQUAL(route.hasExpirationPeriod(), true);
102
103 route.unsetExpirationPeriod();
104 BOOST_CHECK_EQUAL(route.hasExpirationPeriod(), false);
105}
106
Davide Pesavento484bbe52017-02-15 00:03:46 -0500107BOOST_AUTO_TEST_CASE(RouteEquality)
Vince Lehmandbf3f702014-07-15 13:00:43 -0500108{
Davide Pesavento484bbe52017-02-15 00:03:46 -0500109 Route route1, route2;
Vince Lehmandbf3f702014-07-15 13:00:43 -0500110
Davide Pesavento484bbe52017-02-15 00:03:46 -0500111 route1 = makeRoute();
112 route2 = route1;
113 BOOST_CHECK_EQUAL(route1, route2);
Vince Lehmandbf3f702014-07-15 13:00:43 -0500114
Davide Pesavento484bbe52017-02-15 00:03:46 -0500115 route2.setFaceId(42);
116 BOOST_CHECK_NE(route1, route2);
Vince Lehmandbf3f702014-07-15 13:00:43 -0500117
Davide Pesavento484bbe52017-02-15 00:03:46 -0500118 route2 = route1;
Davide Pesavento0f830802018-01-16 23:58:58 -0500119 route2.setExpirationPeriod(1_min);
Davide Pesavento484bbe52017-02-15 00:03:46 -0500120 BOOST_CHECK_NE(route1, route2);
Vince Lehmandbf3f702014-07-15 13:00:43 -0500121}
122
123BOOST_AUTO_TEST_CASE(RibEntryEncode)
124{
Davide Pesavento484bbe52017-02-15 00:03:46 -0500125 RibEntry entry1 = makeRibEntry();
126 entry1.addRoute(Route()
127 .setFaceId(2)
Davide Pesaventoe8e48c22017-04-13 00:35:33 -0400128 .setOrigin(ROUTE_ORIGIN_APP)
Davide Pesavento484bbe52017-02-15 00:03:46 -0500129 .setCost(32)
Davide Pesaventoe8e48c22017-04-13 00:35:33 -0400130 .setFlags(ROUTE_FLAG_CHILD_INHERIT)
Davide Pesavento0f830802018-01-16 23:58:58 -0500131 .setExpirationPeriod(5_s));
Davide Pesavento484bbe52017-02-15 00:03:46 -0500132 const Block& wire = entry1.wireEncode();
Vince Lehmandbf3f702014-07-15 13:00:43 -0500133
Davide Pesavento484bbe52017-02-15 00:03:46 -0500134 static const uint8_t expected[] = {
135 0x80, 0x34, 0x07, 0x0e, 0x08, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x08, 0x05, 0x77,
136 0x6f, 0x72, 0x6c, 0x64, 0x81, 0x10, 0x69, 0x01, 0x01, 0x6f, 0x01, 0x80, 0x6a, 0x01,
137 0x64, 0x6c, 0x01, 0x02, 0x6d, 0x02, 0x27, 0x10, 0x81, 0x10, 0x69, 0x01, 0x02, 0x6f,
138 0x01, 0x00, 0x6a, 0x01, 0x20, 0x6c, 0x01, 0x01, 0x6d, 0x02, 0x13, 0x88
139 };
140 BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
Vince Lehmandbf3f702014-07-15 13:00:43 -0500141 wire.begin(), wire.end());
Davide Pesavento484bbe52017-02-15 00:03:46 -0500142
143 RibEntry entry2(wire);
144 BOOST_CHECK_EQUAL(entry1, entry2);
Vince Lehmandbf3f702014-07-15 13:00:43 -0500145}
146
Davide Pesavento484bbe52017-02-15 00:03:46 -0500147BOOST_AUTO_TEST_CASE(RibEntryClearRoutes)
Vince Lehmandbf3f702014-07-15 13:00:43 -0500148{
149 RibEntry entry;
150 entry.setName("/hello/world");
Davide Pesavento484bbe52017-02-15 00:03:46 -0500151 BOOST_CHECK_EQUAL(entry.getRoutes().size(), 0);
Vince Lehmandbf3f702014-07-15 13:00:43 -0500152
153 Route route1;
Davide Pesavento484bbe52017-02-15 00:03:46 -0500154 route1.setFaceId(42);
Vince Lehmandbf3f702014-07-15 13:00:43 -0500155 entry.addRoute(route1);
156 BOOST_REQUIRE_EQUAL(entry.getRoutes().size(), 1);
Davide Pesavento484bbe52017-02-15 00:03:46 -0500157 BOOST_CHECK_EQUAL(entry.getRoutes().front(), route1);
Vince Lehmandbf3f702014-07-15 13:00:43 -0500158
159 entry.clearRoutes();
160 BOOST_CHECK_EQUAL(entry.getRoutes().size(), 0);
Vince Lehmandbf3f702014-07-15 13:00:43 -0500161}
162
Davide Pesavento484bbe52017-02-15 00:03:46 -0500163BOOST_AUTO_TEST_CASE(RibEntryEquality)
Vince Lehmandbf3f702014-07-15 13:00:43 -0500164{
Davide Pesavento484bbe52017-02-15 00:03:46 -0500165 RibEntry entry1, entry2;
166 BOOST_CHECK_EQUAL(entry1, entry2);
167
168 entry1 = entry2 = makeRibEntry();
169 BOOST_CHECK_EQUAL(entry1, entry2);
170 BOOST_CHECK_EQUAL(entry2, entry1);
171
172 entry2.setName("/different/name");
173 BOOST_CHECK_NE(entry1, entry2);
174
175 entry2 = entry1;
176 std::vector<Route> empty;
177 entry2.setRoutes(empty.begin(), empty.end());
178 BOOST_CHECK_NE(entry1, entry2);
179 BOOST_CHECK_NE(entry2, entry1);
180
181 entry2 = entry1;
182 auto r1 = Route()
183 .setFaceId(1)
184 .setCost(1000);
185 entry1.addRoute(r1);
186 BOOST_CHECK_NE(entry1, entry2);
187 BOOST_CHECK_NE(entry2, entry1);
188
189 auto r42 = Route()
190 .setFaceId(42)
191 .setCost(42);
192 entry1.addRoute(r42);
193 entry2.addRoute(r42)
194 .addRoute(r1);
195 BOOST_CHECK_EQUAL(entry1, entry2); // order of Routes is irrelevant
196 BOOST_CHECK_EQUAL(entry2, entry1);
197
198 entry1 = entry2 = makeRibEntry();
199 entry1.addRoute(r1)
200 .addRoute(r42);
201 entry2.addRoute(r42)
202 .addRoute(r42);
203 BOOST_CHECK_NE(entry1, entry2); // match each Route at most once
204 BOOST_CHECK_NE(entry2, entry1);
205}
206
207BOOST_AUTO_TEST_CASE(Print)
208{
209 Route route;
210 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(route),
Davide Pesaventoe8e48c22017-04-13 00:35:33 -0400211 "Route(FaceId: 0, Origin: app, Cost: 0, Flags: 0x1, ExpirationPeriod: infinite)");
Davide Pesavento484bbe52017-02-15 00:03:46 -0500212
Vince Lehmandbf3f702014-07-15 13:00:43 -0500213 RibEntry entry;
Davide Pesavento484bbe52017-02-15 00:03:46 -0500214 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(entry),
215 "RibEntry(Prefix: /,\n"
216 " Routes: []\n"
217 " )");
Vince Lehmandbf3f702014-07-15 13:00:43 -0500218
Davide Pesavento484bbe52017-02-15 00:03:46 -0500219 entry = makeRibEntry();
220 entry.addRoute(Route()
221 .setFaceId(2)
Davide Pesaventoe8e48c22017-04-13 00:35:33 -0400222 .setOrigin(ROUTE_ORIGIN_STATIC)
Davide Pesavento484bbe52017-02-15 00:03:46 -0500223 .setCost(32)
Davide Pesaventoe8e48c22017-04-13 00:35:33 -0400224 .setFlags(ROUTE_FLAG_CHILD_INHERIT));
Davide Pesavento484bbe52017-02-15 00:03:46 -0500225 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(entry),
226 "RibEntry(Prefix: /hello/world,\n"
Davide Pesaventoe8e48c22017-04-13 00:35:33 -0400227 " Routes: [Route(FaceId: 1, Origin: nlsr, Cost: 100, Flags: 0x2, "
Davide Pesavento484bbe52017-02-15 00:03:46 -0500228 "ExpirationPeriod: 10000 milliseconds),\n"
Davide Pesaventoe8e48c22017-04-13 00:35:33 -0400229 " Route(FaceId: 2, Origin: static, Cost: 32, Flags: 0x1, "
Davide Pesavento484bbe52017-02-15 00:03:46 -0500230 "ExpirationPeriod: infinite)]\n"
231 " )");
Vince Lehmandbf3f702014-07-15 13:00:43 -0500232}
233
Junxiao Shi7357ef22016-09-07 02:39:37 +0000234BOOST_AUTO_TEST_SUITE_END() // TestRibEntry
235BOOST_AUTO_TEST_SUITE_END() // Nfd
236BOOST_AUTO_TEST_SUITE_END() // Mgmt
Vince Lehmandbf3f702014-07-15 13:00:43 -0500237
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800238} // namespace tests
Vince Lehmandbf3f702014-07-15 13:00:43 -0500239} // namespace nfd
240} // namespace ndn