Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | 152ef44 | 2023-04-22 02:02:29 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2013-2023 Regents of the University of California. |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 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 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/mgmt/nfd/rib-entry.hpp" |
Davide Pesavento | 152ef44 | 2023-04-22 02:02:29 -0400 | [diff] [blame^] | 23 | #include "ndn-cxx/util/concepts.hpp" |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 24 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 25 | #include "tests/boost-test.hpp" |
Davide Pesavento | 152ef44 | 2023-04-22 02:02:29 -0400 | [diff] [blame^] | 26 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 27 | #include <boost/lexical_cast.hpp> |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 28 | |
| 29 | namespace ndn { |
| 30 | namespace nfd { |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 31 | namespace tests { |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 32 | |
Davide Pesavento | 152ef44 | 2023-04-22 02:02:29 -0400 | [diff] [blame^] | 33 | BOOST_CONCEPT_ASSERT((StatusDatasetItem<Route>)); |
| 34 | BOOST_CONCEPT_ASSERT((StatusDatasetItem<RibEntry>)); |
| 35 | |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 36 | BOOST_AUTO_TEST_SUITE(Mgmt) |
| 37 | BOOST_AUTO_TEST_SUITE(Nfd) |
| 38 | BOOST_AUTO_TEST_SUITE(TestRibEntry) |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 39 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 40 | static Route |
| 41 | makeRoute() |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 42 | { |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 43 | return Route() |
| 44 | .setFaceId(1) |
Davide Pesavento | e8e48c2 | 2017-04-13 00:35:33 -0400 | [diff] [blame] | 45 | .setOrigin(ROUTE_ORIGIN_NLSR) |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 46 | .setCost(100) |
Davide Pesavento | e8e48c2 | 2017-04-13 00:35:33 -0400 | [diff] [blame] | 47 | .setFlags(ROUTE_FLAG_CAPTURE); |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 48 | } |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 49 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 50 | static RibEntry |
| 51 | makeRibEntry() |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 52 | { |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 53 | return RibEntry() |
| 54 | .setName("/hello/world") |
| 55 | .addRoute(makeRoute() |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 56 | .setExpirationPeriod(10_s)); |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 57 | } |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 58 | |
| 59 | BOOST_AUTO_TEST_CASE(RouteEncode) |
| 60 | { |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 61 | Route route1 = makeRoute(); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 62 | route1.setExpirationPeriod(10_s); |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 63 | const Block& wire = route1.wireEncode(); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 64 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 65 | 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 Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 71 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 72 | Route route2(wire); |
| 73 | BOOST_CHECK_EQUAL(route1, route2); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 74 | } |
| 75 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 76 | BOOST_AUTO_TEST_CASE(RouteNoExpirationPeriodEncode) |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 77 | { |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 78 | Route route1 = makeRoute(); |
| 79 | const Block& wire = route1.wireEncode(); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 80 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 81 | 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 Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 86 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 87 | Route route2(wire); |
| 88 | BOOST_CHECK_EQUAL(route1, route2); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 89 | } |
| 90 | |
Davide Pesavento | 156c1ea | 2017-03-19 16:09:33 -0400 | [diff] [blame] | 91 | BOOST_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 Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 97 | route.setExpirationPeriod(1_min); |
Davide Pesavento | 156c1ea | 2017-03-19 16:09:33 -0400 | [diff] [blame] | 98 | BOOST_CHECK_EQUAL(route.hasExpirationPeriod(), true); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 99 | BOOST_CHECK_EQUAL(route.getExpirationPeriod(), 1_min); |
Davide Pesavento | 156c1ea | 2017-03-19 16:09:33 -0400 | [diff] [blame] | 100 | |
| 101 | route.setExpirationPeriod(time::milliseconds::max()); |
| 102 | BOOST_CHECK_EQUAL(route.hasExpirationPeriod(), false); |
| 103 | BOOST_CHECK_EQUAL(route.getExpirationPeriod(), time::milliseconds::max()); |
| 104 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 105 | route.setExpirationPeriod(1_min); |
Davide Pesavento | 156c1ea | 2017-03-19 16:09:33 -0400 | [diff] [blame] | 106 | BOOST_CHECK_EQUAL(route.hasExpirationPeriod(), true); |
| 107 | |
| 108 | route.unsetExpirationPeriod(); |
| 109 | BOOST_CHECK_EQUAL(route.hasExpirationPeriod(), false); |
| 110 | } |
| 111 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 112 | BOOST_AUTO_TEST_CASE(RouteEquality) |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 113 | { |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 114 | Route route1, route2; |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 115 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 116 | route1 = makeRoute(); |
| 117 | route2 = route1; |
| 118 | BOOST_CHECK_EQUAL(route1, route2); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 119 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 120 | route2.setFaceId(42); |
| 121 | BOOST_CHECK_NE(route1, route2); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 122 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 123 | route2 = route1; |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 124 | route2.setExpirationPeriod(1_min); |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 125 | BOOST_CHECK_NE(route1, route2); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | BOOST_AUTO_TEST_CASE(RibEntryEncode) |
| 129 | { |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 130 | RibEntry entry1 = makeRibEntry(); |
| 131 | entry1.addRoute(Route() |
| 132 | .setFaceId(2) |
Davide Pesavento | e8e48c2 | 2017-04-13 00:35:33 -0400 | [diff] [blame] | 133 | .setOrigin(ROUTE_ORIGIN_APP) |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 134 | .setCost(32) |
Davide Pesavento | e8e48c2 | 2017-04-13 00:35:33 -0400 | [diff] [blame] | 135 | .setFlags(ROUTE_FLAG_CHILD_INHERIT) |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 136 | .setExpirationPeriod(5_s)); |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 137 | const Block& wire = entry1.wireEncode(); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 138 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 139 | 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 Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 146 | wire.begin(), wire.end()); |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 147 | |
| 148 | RibEntry entry2(wire); |
| 149 | BOOST_CHECK_EQUAL(entry1, entry2); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 150 | } |
| 151 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 152 | BOOST_AUTO_TEST_CASE(RibEntryClearRoutes) |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 153 | { |
| 154 | RibEntry entry; |
| 155 | entry.setName("/hello/world"); |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 156 | BOOST_CHECK_EQUAL(entry.getRoutes().size(), 0); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 157 | |
| 158 | Route route1; |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 159 | route1.setFaceId(42); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 160 | entry.addRoute(route1); |
| 161 | BOOST_REQUIRE_EQUAL(entry.getRoutes().size(), 1); |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 162 | BOOST_CHECK_EQUAL(entry.getRoutes().front(), route1); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 163 | |
| 164 | entry.clearRoutes(); |
| 165 | BOOST_CHECK_EQUAL(entry.getRoutes().size(), 0); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 166 | } |
| 167 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 168 | BOOST_AUTO_TEST_CASE(RibEntryEquality) |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 169 | { |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 170 | 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 | |
| 212 | BOOST_AUTO_TEST_CASE(Print) |
| 213 | { |
| 214 | Route route; |
| 215 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(route), |
Davide Pesavento | e8e48c2 | 2017-04-13 00:35:33 -0400 | [diff] [blame] | 216 | "Route(FaceId: 0, Origin: app, Cost: 0, Flags: 0x1, ExpirationPeriod: infinite)"); |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 217 | |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 218 | RibEntry entry; |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 219 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(entry), |
| 220 | "RibEntry(Prefix: /,\n" |
| 221 | " Routes: []\n" |
| 222 | " )"); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 223 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 224 | entry = makeRibEntry(); |
| 225 | entry.addRoute(Route() |
| 226 | .setFaceId(2) |
Davide Pesavento | e8e48c2 | 2017-04-13 00:35:33 -0400 | [diff] [blame] | 227 | .setOrigin(ROUTE_ORIGIN_STATIC) |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 228 | .setCost(32) |
Davide Pesavento | e8e48c2 | 2017-04-13 00:35:33 -0400 | [diff] [blame] | 229 | .setFlags(ROUTE_FLAG_CHILD_INHERIT)); |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 230 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(entry), |
| 231 | "RibEntry(Prefix: /hello/world,\n" |
Davide Pesavento | e8e48c2 | 2017-04-13 00:35:33 -0400 | [diff] [blame] | 232 | " Routes: [Route(FaceId: 1, Origin: nlsr, Cost: 100, Flags: 0x2, " |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 233 | "ExpirationPeriod: 10000 milliseconds),\n" |
Davide Pesavento | e8e48c2 | 2017-04-13 00:35:33 -0400 | [diff] [blame] | 234 | " Route(FaceId: 2, Origin: static, Cost: 32, Flags: 0x1, " |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 235 | "ExpirationPeriod: infinite)]\n" |
| 236 | " )"); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 237 | } |
| 238 | |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 239 | BOOST_AUTO_TEST_SUITE_END() // TestRibEntry |
| 240 | BOOST_AUTO_TEST_SUITE_END() // Nfd |
| 241 | BOOST_AUTO_TEST_SUITE_END() // Mgmt |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 242 | |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 243 | } // namespace tests |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 244 | } // namespace nfd |
| 245 | } // namespace ndn |