blob: cc3ed58119a80c07b6708c928b43792890186247 [file] [log] [blame]
Vince Lehmandbf3f702014-07-15 13:00:43 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesavento484bbe52017-02-15 00:03:46 -05003 * Copyright (c) 2013-2017 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)
40 .setOrigin(128)
41 .setCost(100)
42 .setFlags(ndn::nfd::ROUTE_FLAG_CAPTURE);
43}
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()
51 .setExpirationPeriod(time::seconds(10)));
52}
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();
57 route1.setExpirationPeriod(time::seconds(10));
58 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 Pesavento484bbe52017-02-15 00:03:46 -050086BOOST_AUTO_TEST_CASE(RouteEquality)
Vince Lehmandbf3f702014-07-15 13:00:43 -050087{
Davide Pesavento484bbe52017-02-15 00:03:46 -050088 Route route1, route2;
Vince Lehmandbf3f702014-07-15 13:00:43 -050089
Davide Pesavento484bbe52017-02-15 00:03:46 -050090 route1 = makeRoute();
91 route2 = route1;
92 BOOST_CHECK_EQUAL(route1, route2);
Vince Lehmandbf3f702014-07-15 13:00:43 -050093
Davide Pesavento484bbe52017-02-15 00:03:46 -050094 route2.setFaceId(42);
95 BOOST_CHECK_NE(route1, route2);
Vince Lehmandbf3f702014-07-15 13:00:43 -050096
Davide Pesavento484bbe52017-02-15 00:03:46 -050097 route2 = route1;
98 route2.setExpirationPeriod(time::minutes(1));
99 BOOST_CHECK_NE(route1, route2);
Vince Lehmandbf3f702014-07-15 13:00:43 -0500100}
101
102BOOST_AUTO_TEST_CASE(RibEntryEncode)
103{
Davide Pesavento484bbe52017-02-15 00:03:46 -0500104 RibEntry entry1 = makeRibEntry();
105 entry1.addRoute(Route()
106 .setFaceId(2)
107 .setOrigin(0)
108 .setCost(32)
109 .setFlags(ndn::nfd::ROUTE_FLAG_CHILD_INHERIT)
110 .setExpirationPeriod(time::seconds(5)));
111 const Block& wire = entry1.wireEncode();
Vince Lehmandbf3f702014-07-15 13:00:43 -0500112
Davide Pesavento484bbe52017-02-15 00:03:46 -0500113 static const uint8_t expected[] = {
114 0x80, 0x34, 0x07, 0x0e, 0x08, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x08, 0x05, 0x77,
115 0x6f, 0x72, 0x6c, 0x64, 0x81, 0x10, 0x69, 0x01, 0x01, 0x6f, 0x01, 0x80, 0x6a, 0x01,
116 0x64, 0x6c, 0x01, 0x02, 0x6d, 0x02, 0x27, 0x10, 0x81, 0x10, 0x69, 0x01, 0x02, 0x6f,
117 0x01, 0x00, 0x6a, 0x01, 0x20, 0x6c, 0x01, 0x01, 0x6d, 0x02, 0x13, 0x88
118 };
119 BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
Vince Lehmandbf3f702014-07-15 13:00:43 -0500120 wire.begin(), wire.end());
Davide Pesavento484bbe52017-02-15 00:03:46 -0500121
122 RibEntry entry2(wire);
123 BOOST_CHECK_EQUAL(entry1, entry2);
Vince Lehmandbf3f702014-07-15 13:00:43 -0500124}
125
Davide Pesavento484bbe52017-02-15 00:03:46 -0500126BOOST_AUTO_TEST_CASE(RibEntryClearRoutes)
Vince Lehmandbf3f702014-07-15 13:00:43 -0500127{
128 RibEntry entry;
129 entry.setName("/hello/world");
Davide Pesavento484bbe52017-02-15 00:03:46 -0500130 BOOST_CHECK_EQUAL(entry.getRoutes().size(), 0);
Vince Lehmandbf3f702014-07-15 13:00:43 -0500131
132 Route route1;
Davide Pesavento484bbe52017-02-15 00:03:46 -0500133 route1.setFaceId(42);
Vince Lehmandbf3f702014-07-15 13:00:43 -0500134 entry.addRoute(route1);
135 BOOST_REQUIRE_EQUAL(entry.getRoutes().size(), 1);
Davide Pesavento484bbe52017-02-15 00:03:46 -0500136 BOOST_CHECK_EQUAL(entry.getRoutes().front(), route1);
Vince Lehmandbf3f702014-07-15 13:00:43 -0500137
138 entry.clearRoutes();
139 BOOST_CHECK_EQUAL(entry.getRoutes().size(), 0);
Vince Lehmandbf3f702014-07-15 13:00:43 -0500140}
141
Davide Pesavento484bbe52017-02-15 00:03:46 -0500142BOOST_AUTO_TEST_CASE(RibEntryEquality)
Vince Lehmandbf3f702014-07-15 13:00:43 -0500143{
Davide Pesavento484bbe52017-02-15 00:03:46 -0500144 RibEntry entry1, entry2;
145 BOOST_CHECK_EQUAL(entry1, entry2);
146
147 entry1 = entry2 = makeRibEntry();
148 BOOST_CHECK_EQUAL(entry1, entry2);
149 BOOST_CHECK_EQUAL(entry2, entry1);
150
151 entry2.setName("/different/name");
152 BOOST_CHECK_NE(entry1, entry2);
153
154 entry2 = entry1;
155 std::vector<Route> empty;
156 entry2.setRoutes(empty.begin(), empty.end());
157 BOOST_CHECK_NE(entry1, entry2);
158 BOOST_CHECK_NE(entry2, entry1);
159
160 entry2 = entry1;
161 auto r1 = Route()
162 .setFaceId(1)
163 .setCost(1000);
164 entry1.addRoute(r1);
165 BOOST_CHECK_NE(entry1, entry2);
166 BOOST_CHECK_NE(entry2, entry1);
167
168 auto r42 = Route()
169 .setFaceId(42)
170 .setCost(42);
171 entry1.addRoute(r42);
172 entry2.addRoute(r42)
173 .addRoute(r1);
174 BOOST_CHECK_EQUAL(entry1, entry2); // order of Routes is irrelevant
175 BOOST_CHECK_EQUAL(entry2, entry1);
176
177 entry1 = entry2 = makeRibEntry();
178 entry1.addRoute(r1)
179 .addRoute(r42);
180 entry2.addRoute(r42)
181 .addRoute(r42);
182 BOOST_CHECK_NE(entry1, entry2); // match each Route at most once
183 BOOST_CHECK_NE(entry2, entry1);
184}
185
186BOOST_AUTO_TEST_CASE(Print)
187{
188 Route route;
189 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(route),
190 "Route(FaceId: 0, Origin: 0, Cost: 0, Flags: 0x1, ExpirationPeriod: infinite)");
191
Vince Lehmandbf3f702014-07-15 13:00:43 -0500192 RibEntry entry;
Davide Pesavento484bbe52017-02-15 00:03:46 -0500193 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(entry),
194 "RibEntry(Prefix: /,\n"
195 " Routes: []\n"
196 " )");
Vince Lehmandbf3f702014-07-15 13:00:43 -0500197
Davide Pesavento484bbe52017-02-15 00:03:46 -0500198 entry = makeRibEntry();
199 entry.addRoute(Route()
200 .setFaceId(2)
201 .setOrigin(0)
202 .setCost(32)
203 .setFlags(ndn::nfd::ROUTE_FLAG_CHILD_INHERIT));
204 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(entry),
205 "RibEntry(Prefix: /hello/world,\n"
206 " Routes: [Route(FaceId: 1, Origin: 128, Cost: 100, Flags: 0x2, "
207 "ExpirationPeriod: 10000 milliseconds),\n"
208 " Route(FaceId: 2, Origin: 0, Cost: 32, Flags: 0x1, "
209 "ExpirationPeriod: infinite)]\n"
210 " )");
Vince Lehmandbf3f702014-07-15 13:00:43 -0500211}
212
Junxiao Shi7357ef22016-09-07 02:39:37 +0000213BOOST_AUTO_TEST_SUITE_END() // TestRibEntry
214BOOST_AUTO_TEST_SUITE_END() // Nfd
215BOOST_AUTO_TEST_SUITE_END() // Mgmt
Vince Lehmandbf3f702014-07-15 13:00:43 -0500216
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800217} // namespace tests
Vince Lehmandbf3f702014-07-15 13:00:43 -0500218} // namespace nfd
219} // namespace ndn