Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 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 | |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 22 | #include "mgmt/nfd/rib-entry.hpp" |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 23 | |
| 24 | #include "boost-test.hpp" |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 25 | #include <boost/lexical_cast.hpp> |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 26 | |
| 27 | namespace ndn { |
| 28 | namespace nfd { |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 29 | namespace tests { |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 30 | |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 31 | BOOST_AUTO_TEST_SUITE(Mgmt) |
| 32 | BOOST_AUTO_TEST_SUITE(Nfd) |
| 33 | BOOST_AUTO_TEST_SUITE(TestRibEntry) |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 34 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 35 | static Route |
| 36 | makeRoute() |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 37 | { |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 38 | return Route() |
| 39 | .setFaceId(1) |
| 40 | .setOrigin(128) |
| 41 | .setCost(100) |
| 42 | .setFlags(ndn::nfd::ROUTE_FLAG_CAPTURE); |
| 43 | } |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 44 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 45 | static RibEntry |
| 46 | makeRibEntry() |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 47 | { |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 48 | return RibEntry() |
| 49 | .setName("/hello/world") |
| 50 | .addRoute(makeRoute() |
| 51 | .setExpirationPeriod(time::seconds(10))); |
| 52 | } |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 53 | |
| 54 | BOOST_AUTO_TEST_CASE(RouteEncode) |
| 55 | { |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 56 | Route route1 = makeRoute(); |
| 57 | route1.setExpirationPeriod(time::seconds(10)); |
| 58 | const Block& wire = route1.wireEncode(); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 59 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 60 | 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 Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 66 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 67 | Route route2(wire); |
| 68 | BOOST_CHECK_EQUAL(route1, route2); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 69 | } |
| 70 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 71 | BOOST_AUTO_TEST_CASE(RouteNoExpirationPeriodEncode) |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 72 | { |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 73 | Route route1 = makeRoute(); |
| 74 | const Block& wire = route1.wireEncode(); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 75 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 76 | 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 Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 81 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 82 | Route route2(wire); |
| 83 | BOOST_CHECK_EQUAL(route1, route2); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 84 | } |
| 85 | |
Davide Pesavento | 156c1ea | 2017-03-19 16:09:33 -0400 | [diff] [blame^] | 86 | BOOST_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 | |
| 92 | route.setExpirationPeriod(time::minutes(1)); |
| 93 | BOOST_CHECK_EQUAL(route.hasExpirationPeriod(), true); |
| 94 | BOOST_CHECK_EQUAL(route.getExpirationPeriod(), time::minutes(1)); |
| 95 | |
| 96 | route.setExpirationPeriod(time::milliseconds::max()); |
| 97 | BOOST_CHECK_EQUAL(route.hasExpirationPeriod(), false); |
| 98 | BOOST_CHECK_EQUAL(route.getExpirationPeriod(), time::milliseconds::max()); |
| 99 | |
| 100 | route.setExpirationPeriod(time::minutes(1)); |
| 101 | BOOST_CHECK_EQUAL(route.hasExpirationPeriod(), true); |
| 102 | |
| 103 | route.unsetExpirationPeriod(); |
| 104 | BOOST_CHECK_EQUAL(route.hasExpirationPeriod(), false); |
| 105 | } |
| 106 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 107 | BOOST_AUTO_TEST_CASE(RouteEquality) |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 108 | { |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 109 | Route route1, route2; |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 110 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 111 | route1 = makeRoute(); |
| 112 | route2 = route1; |
| 113 | BOOST_CHECK_EQUAL(route1, route2); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 114 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 115 | route2.setFaceId(42); |
| 116 | BOOST_CHECK_NE(route1, route2); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 117 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 118 | route2 = route1; |
| 119 | route2.setExpirationPeriod(time::minutes(1)); |
| 120 | BOOST_CHECK_NE(route1, route2); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | BOOST_AUTO_TEST_CASE(RibEntryEncode) |
| 124 | { |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 125 | RibEntry entry1 = makeRibEntry(); |
| 126 | entry1.addRoute(Route() |
| 127 | .setFaceId(2) |
| 128 | .setOrigin(0) |
| 129 | .setCost(32) |
| 130 | .setFlags(ndn::nfd::ROUTE_FLAG_CHILD_INHERIT) |
| 131 | .setExpirationPeriod(time::seconds(5))); |
| 132 | const Block& wire = entry1.wireEncode(); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 133 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 134 | 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 Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 141 | wire.begin(), wire.end()); |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 142 | |
| 143 | RibEntry entry2(wire); |
| 144 | BOOST_CHECK_EQUAL(entry1, entry2); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 145 | } |
| 146 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 147 | BOOST_AUTO_TEST_CASE(RibEntryClearRoutes) |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 148 | { |
| 149 | RibEntry entry; |
| 150 | entry.setName("/hello/world"); |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 151 | BOOST_CHECK_EQUAL(entry.getRoutes().size(), 0); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 152 | |
| 153 | Route route1; |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 154 | route1.setFaceId(42); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 155 | entry.addRoute(route1); |
| 156 | BOOST_REQUIRE_EQUAL(entry.getRoutes().size(), 1); |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 157 | BOOST_CHECK_EQUAL(entry.getRoutes().front(), route1); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 158 | |
| 159 | entry.clearRoutes(); |
| 160 | BOOST_CHECK_EQUAL(entry.getRoutes().size(), 0); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 161 | } |
| 162 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 163 | BOOST_AUTO_TEST_CASE(RibEntryEquality) |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 164 | { |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 165 | 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 | |
| 207 | BOOST_AUTO_TEST_CASE(Print) |
| 208 | { |
| 209 | Route route; |
| 210 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(route), |
| 211 | "Route(FaceId: 0, Origin: 0, Cost: 0, Flags: 0x1, ExpirationPeriod: infinite)"); |
| 212 | |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 213 | RibEntry entry; |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 214 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(entry), |
| 215 | "RibEntry(Prefix: /,\n" |
| 216 | " Routes: []\n" |
| 217 | " )"); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 218 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 219 | entry = makeRibEntry(); |
| 220 | entry.addRoute(Route() |
| 221 | .setFaceId(2) |
| 222 | .setOrigin(0) |
| 223 | .setCost(32) |
| 224 | .setFlags(ndn::nfd::ROUTE_FLAG_CHILD_INHERIT)); |
| 225 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(entry), |
| 226 | "RibEntry(Prefix: /hello/world,\n" |
| 227 | " Routes: [Route(FaceId: 1, Origin: 128, Cost: 100, Flags: 0x2, " |
| 228 | "ExpirationPeriod: 10000 milliseconds),\n" |
| 229 | " Route(FaceId: 2, Origin: 0, Cost: 32, Flags: 0x1, " |
| 230 | "ExpirationPeriod: infinite)]\n" |
| 231 | " )"); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 232 | } |
| 233 | |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 234 | BOOST_AUTO_TEST_SUITE_END() // TestRibEntry |
| 235 | BOOST_AUTO_TEST_SUITE_END() // Nfd |
| 236 | BOOST_AUTO_TEST_SUITE_END() // Mgmt |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 237 | |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 238 | } // namespace tests |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 239 | } // namespace nfd |
| 240 | } // namespace ndn |