blob: ae924bfd06c3e878bee67b6829fe40e7429855c4 [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/*
Davide Pesavento152ef442023-04-22 02:02:29 -04003 * Copyright (c) 2013-2023 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
Davide Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/mgmt/nfd/rib-entry.hpp"
Davide Pesavento152ef442023-04-22 02:02:29 -040023#include "ndn-cxx/util/concepts.hpp"
Vince Lehmandbf3f702014-07-15 13:00:43 -050024
Davide Pesavento7e780642018-11-24 15:51:34 -050025#include "tests/boost-test.hpp"
Davide Pesavento152ef442023-04-22 02:02:29 -040026
Davide Pesavento484bbe52017-02-15 00:03:46 -050027#include <boost/lexical_cast.hpp>
Vince Lehmandbf3f702014-07-15 13:00:43 -050028
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040029namespace ndn::tests {
30
31using namespace ndn::nfd;
Vince Lehmandbf3f702014-07-15 13:00:43 -050032
Davide Pesavento152ef442023-04-22 02:02:29 -040033BOOST_CONCEPT_ASSERT((StatusDatasetItem<Route>));
34BOOST_CONCEPT_ASSERT((StatusDatasetItem<RibEntry>));
35
Junxiao Shi7357ef22016-09-07 02:39:37 +000036BOOST_AUTO_TEST_SUITE(Mgmt)
37BOOST_AUTO_TEST_SUITE(Nfd)
38BOOST_AUTO_TEST_SUITE(TestRibEntry)
Vince Lehmandbf3f702014-07-15 13:00:43 -050039
Davide Pesavento484bbe52017-02-15 00:03:46 -050040static Route
41makeRoute()
Vince Lehmandbf3f702014-07-15 13:00:43 -050042{
Davide Pesavento484bbe52017-02-15 00:03:46 -050043 return Route()
44 .setFaceId(1)
Davide Pesaventoe8e48c22017-04-13 00:35:33 -040045 .setOrigin(ROUTE_ORIGIN_NLSR)
Davide Pesavento484bbe52017-02-15 00:03:46 -050046 .setCost(100)
Davide Pesaventoe8e48c22017-04-13 00:35:33 -040047 .setFlags(ROUTE_FLAG_CAPTURE);
Davide Pesavento484bbe52017-02-15 00:03:46 -050048}
Vince Lehmandbf3f702014-07-15 13:00:43 -050049
Davide Pesavento484bbe52017-02-15 00:03:46 -050050static RibEntry
51makeRibEntry()
Vince Lehmandbf3f702014-07-15 13:00:43 -050052{
Davide Pesavento484bbe52017-02-15 00:03:46 -050053 return RibEntry()
54 .setName("/hello/world")
55 .addRoute(makeRoute()
Davide Pesavento0f830802018-01-16 23:58:58 -050056 .setExpirationPeriod(10_s));
Davide Pesavento484bbe52017-02-15 00:03:46 -050057}
Vince Lehmandbf3f702014-07-15 13:00:43 -050058
59BOOST_AUTO_TEST_CASE(RouteEncode)
60{
Davide Pesavento484bbe52017-02-15 00:03:46 -050061 Route route1 = makeRoute();
Davide Pesavento0f830802018-01-16 23:58:58 -050062 route1.setExpirationPeriod(10_s);
Davide Pesavento484bbe52017-02-15 00:03:46 -050063 const Block& wire = route1.wireEncode();
Vince Lehmandbf3f702014-07-15 13:00:43 -050064
Davide Pesavento484bbe52017-02-15 00:03:46 -050065 static const uint8_t expected[] = {
66 0x81, 0x10, 0x69, 0x01, 0x01, 0x6f, 0x01, 0x80, 0x6a, 0x01, 0x64, 0x6c, 0x01, 0x02,
67 0x6d, 0x02, 0x27, 0x10
68 };
69 BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
70 wire.begin(), wire.end());
Vince Lehmandbf3f702014-07-15 13:00:43 -050071
Davide Pesavento484bbe52017-02-15 00:03:46 -050072 Route route2(wire);
73 BOOST_CHECK_EQUAL(route1, route2);
Vince Lehmandbf3f702014-07-15 13:00:43 -050074}
75
Davide Pesavento484bbe52017-02-15 00:03:46 -050076BOOST_AUTO_TEST_CASE(RouteNoExpirationPeriodEncode)
Vince Lehmandbf3f702014-07-15 13:00:43 -050077{
Davide Pesavento484bbe52017-02-15 00:03:46 -050078 Route route1 = makeRoute();
79 const Block& wire = route1.wireEncode();
Vince Lehmandbf3f702014-07-15 13:00:43 -050080
Davide Pesavento484bbe52017-02-15 00:03:46 -050081 static const uint8_t expected[] = {
82 0x81, 0x0C, 0x69, 0x01, 0x01, 0x6f, 0x01, 0x80, 0x6a, 0x01, 0x64, 0x6c, 0x01, 0x02
83 };
84 BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
85 wire.begin(), wire.end());
Vince Lehmandbf3f702014-07-15 13:00:43 -050086
Davide Pesavento484bbe52017-02-15 00:03:46 -050087 Route route2(wire);
88 BOOST_CHECK_EQUAL(route1, route2);
Vince Lehmandbf3f702014-07-15 13:00:43 -050089}
90
Davide Pesavento156c1ea2017-03-19 16:09:33 -040091BOOST_AUTO_TEST_CASE(RouteExpirationPeriod)
92{
93 Route route;
94 BOOST_CHECK_EQUAL(route.hasExpirationPeriod(), false);
95 BOOST_CHECK_EQUAL(route.getExpirationPeriod(), time::milliseconds::max());
96
Davide Pesavento0f830802018-01-16 23:58:58 -050097 route.setExpirationPeriod(1_min);
Davide Pesavento156c1ea2017-03-19 16:09:33 -040098 BOOST_CHECK_EQUAL(route.hasExpirationPeriod(), true);
Davide Pesavento0f830802018-01-16 23:58:58 -050099 BOOST_CHECK_EQUAL(route.getExpirationPeriod(), 1_min);
Davide Pesavento156c1ea2017-03-19 16:09:33 -0400100
101 route.setExpirationPeriod(time::milliseconds::max());
102 BOOST_CHECK_EQUAL(route.hasExpirationPeriod(), false);
103 BOOST_CHECK_EQUAL(route.getExpirationPeriod(), time::milliseconds::max());
104
Davide Pesavento0f830802018-01-16 23:58:58 -0500105 route.setExpirationPeriod(1_min);
Davide Pesavento156c1ea2017-03-19 16:09:33 -0400106 BOOST_CHECK_EQUAL(route.hasExpirationPeriod(), true);
107
108 route.unsetExpirationPeriod();
109 BOOST_CHECK_EQUAL(route.hasExpirationPeriod(), false);
110}
111
Davide Pesavento484bbe52017-02-15 00:03:46 -0500112BOOST_AUTO_TEST_CASE(RouteEquality)
Vince Lehmandbf3f702014-07-15 13:00:43 -0500113{
Davide Pesavento484bbe52017-02-15 00:03:46 -0500114 Route route1, route2;
Vince Lehmandbf3f702014-07-15 13:00:43 -0500115
Davide Pesavento484bbe52017-02-15 00:03:46 -0500116 route1 = makeRoute();
117 route2 = route1;
118 BOOST_CHECK_EQUAL(route1, route2);
Vince Lehmandbf3f702014-07-15 13:00:43 -0500119
Davide Pesavento484bbe52017-02-15 00:03:46 -0500120 route2.setFaceId(42);
121 BOOST_CHECK_NE(route1, route2);
Vince Lehmandbf3f702014-07-15 13:00:43 -0500122
Davide Pesavento484bbe52017-02-15 00:03:46 -0500123 route2 = route1;
Davide Pesavento0f830802018-01-16 23:58:58 -0500124 route2.setExpirationPeriod(1_min);
Davide Pesavento484bbe52017-02-15 00:03:46 -0500125 BOOST_CHECK_NE(route1, route2);
Vince Lehmandbf3f702014-07-15 13:00:43 -0500126}
127
128BOOST_AUTO_TEST_CASE(RibEntryEncode)
129{
Davide Pesavento484bbe52017-02-15 00:03:46 -0500130 RibEntry entry1 = makeRibEntry();
131 entry1.addRoute(Route()
132 .setFaceId(2)
Davide Pesaventoe8e48c22017-04-13 00:35:33 -0400133 .setOrigin(ROUTE_ORIGIN_APP)
Davide Pesavento484bbe52017-02-15 00:03:46 -0500134 .setCost(32)
Davide Pesaventoe8e48c22017-04-13 00:35:33 -0400135 .setFlags(ROUTE_FLAG_CHILD_INHERIT)
Davide Pesavento0f830802018-01-16 23:58:58 -0500136 .setExpirationPeriod(5_s));
Davide Pesavento484bbe52017-02-15 00:03:46 -0500137 const Block& wire = entry1.wireEncode();
Vince Lehmandbf3f702014-07-15 13:00:43 -0500138
Davide Pesavento484bbe52017-02-15 00:03:46 -0500139 static const uint8_t expected[] = {
140 0x80, 0x34, 0x07, 0x0e, 0x08, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x08, 0x05, 0x77,
141 0x6f, 0x72, 0x6c, 0x64, 0x81, 0x10, 0x69, 0x01, 0x01, 0x6f, 0x01, 0x80, 0x6a, 0x01,
142 0x64, 0x6c, 0x01, 0x02, 0x6d, 0x02, 0x27, 0x10, 0x81, 0x10, 0x69, 0x01, 0x02, 0x6f,
143 0x01, 0x00, 0x6a, 0x01, 0x20, 0x6c, 0x01, 0x01, 0x6d, 0x02, 0x13, 0x88
144 };
145 BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
Vince Lehmandbf3f702014-07-15 13:00:43 -0500146 wire.begin(), wire.end());
Davide Pesavento484bbe52017-02-15 00:03:46 -0500147
148 RibEntry entry2(wire);
149 BOOST_CHECK_EQUAL(entry1, entry2);
Vince Lehmandbf3f702014-07-15 13:00:43 -0500150}
151
Davide Pesavento484bbe52017-02-15 00:03:46 -0500152BOOST_AUTO_TEST_CASE(RibEntryClearRoutes)
Vince Lehmandbf3f702014-07-15 13:00:43 -0500153{
154 RibEntry entry;
155 entry.setName("/hello/world");
Davide Pesavento484bbe52017-02-15 00:03:46 -0500156 BOOST_CHECK_EQUAL(entry.getRoutes().size(), 0);
Vince Lehmandbf3f702014-07-15 13:00:43 -0500157
158 Route route1;
Davide Pesavento484bbe52017-02-15 00:03:46 -0500159 route1.setFaceId(42);
Vince Lehmandbf3f702014-07-15 13:00:43 -0500160 entry.addRoute(route1);
161 BOOST_REQUIRE_EQUAL(entry.getRoutes().size(), 1);
Davide Pesavento484bbe52017-02-15 00:03:46 -0500162 BOOST_CHECK_EQUAL(entry.getRoutes().front(), route1);
Vince Lehmandbf3f702014-07-15 13:00:43 -0500163
164 entry.clearRoutes();
165 BOOST_CHECK_EQUAL(entry.getRoutes().size(), 0);
Vince Lehmandbf3f702014-07-15 13:00:43 -0500166}
167
Davide Pesavento484bbe52017-02-15 00:03:46 -0500168BOOST_AUTO_TEST_CASE(RibEntryEquality)
Vince Lehmandbf3f702014-07-15 13:00:43 -0500169{
Davide Pesavento484bbe52017-02-15 00:03:46 -0500170 RibEntry entry1, entry2;
171 BOOST_CHECK_EQUAL(entry1, entry2);
172
173 entry1 = entry2 = makeRibEntry();
174 BOOST_CHECK_EQUAL(entry1, entry2);
175 BOOST_CHECK_EQUAL(entry2, entry1);
176
177 entry2.setName("/different/name");
178 BOOST_CHECK_NE(entry1, entry2);
179
180 entry2 = entry1;
181 std::vector<Route> empty;
182 entry2.setRoutes(empty.begin(), empty.end());
183 BOOST_CHECK_NE(entry1, entry2);
184 BOOST_CHECK_NE(entry2, entry1);
185
186 entry2 = entry1;
187 auto r1 = Route()
188 .setFaceId(1)
189 .setCost(1000);
190 entry1.addRoute(r1);
191 BOOST_CHECK_NE(entry1, entry2);
192 BOOST_CHECK_NE(entry2, entry1);
193
194 auto r42 = Route()
195 .setFaceId(42)
196 .setCost(42);
197 entry1.addRoute(r42);
198 entry2.addRoute(r42)
199 .addRoute(r1);
200 BOOST_CHECK_EQUAL(entry1, entry2); // order of Routes is irrelevant
201 BOOST_CHECK_EQUAL(entry2, entry1);
202
203 entry1 = entry2 = makeRibEntry();
204 entry1.addRoute(r1)
205 .addRoute(r42);
206 entry2.addRoute(r42)
207 .addRoute(r42);
208 BOOST_CHECK_NE(entry1, entry2); // match each Route at most once
209 BOOST_CHECK_NE(entry2, entry1);
210}
211
212BOOST_AUTO_TEST_CASE(Print)
213{
214 Route route;
215 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(route),
Davide Pesaventoe8e48c22017-04-13 00:35:33 -0400216 "Route(FaceId: 0, Origin: app, Cost: 0, Flags: 0x1, ExpirationPeriod: infinite)");
Davide Pesavento484bbe52017-02-15 00:03:46 -0500217
Vince Lehmandbf3f702014-07-15 13:00:43 -0500218 RibEntry entry;
Davide Pesavento484bbe52017-02-15 00:03:46 -0500219 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(entry),
220 "RibEntry(Prefix: /,\n"
221 " Routes: []\n"
222 " )");
Vince Lehmandbf3f702014-07-15 13:00:43 -0500223
Davide Pesavento484bbe52017-02-15 00:03:46 -0500224 entry = makeRibEntry();
225 entry.addRoute(Route()
226 .setFaceId(2)
Davide Pesaventoe8e48c22017-04-13 00:35:33 -0400227 .setOrigin(ROUTE_ORIGIN_STATIC)
Davide Pesavento484bbe52017-02-15 00:03:46 -0500228 .setCost(32)
Davide Pesaventoe8e48c22017-04-13 00:35:33 -0400229 .setFlags(ROUTE_FLAG_CHILD_INHERIT));
Davide Pesavento484bbe52017-02-15 00:03:46 -0500230 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(entry),
231 "RibEntry(Prefix: /hello/world,\n"
Davide Pesaventoe8e48c22017-04-13 00:35:33 -0400232 " Routes: [Route(FaceId: 1, Origin: nlsr, Cost: 100, Flags: 0x2, "
Davide Pesavento484bbe52017-02-15 00:03:46 -0500233 "ExpirationPeriod: 10000 milliseconds),\n"
Davide Pesaventoe8e48c22017-04-13 00:35:33 -0400234 " Route(FaceId: 2, Origin: static, Cost: 32, Flags: 0x1, "
Davide Pesavento484bbe52017-02-15 00:03:46 -0500235 "ExpirationPeriod: infinite)]\n"
236 " )");
Vince Lehmandbf3f702014-07-15 13:00:43 -0500237}
238
Junxiao Shi7357ef22016-09-07 02:39:37 +0000239BOOST_AUTO_TEST_SUITE_END() // TestRibEntry
240BOOST_AUTO_TEST_SUITE_END() // Nfd
241BOOST_AUTO_TEST_SUITE_END() // Mgmt
Vince Lehmandbf3f702014-07-15 13:00:43 -0500242
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400243} // namespace ndn::tests