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 | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame^] | 86 | BOOST_AUTO_TEST_CASE(RouteEquality) |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 87 | { |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame^] | 88 | Route route1, route2; |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 89 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame^] | 90 | route1 = makeRoute(); |
| 91 | route2 = route1; |
| 92 | BOOST_CHECK_EQUAL(route1, route2); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 93 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame^] | 94 | route2.setFaceId(42); |
| 95 | BOOST_CHECK_NE(route1, route2); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 96 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame^] | 97 | route2 = route1; |
| 98 | route2.setExpirationPeriod(time::minutes(1)); |
| 99 | BOOST_CHECK_NE(route1, route2); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | BOOST_AUTO_TEST_CASE(RibEntryEncode) |
| 103 | { |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame^] | 104 | 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 Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 112 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame^] | 113 | 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 Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 120 | wire.begin(), wire.end()); |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame^] | 121 | |
| 122 | RibEntry entry2(wire); |
| 123 | BOOST_CHECK_EQUAL(entry1, entry2); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 124 | } |
| 125 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame^] | 126 | BOOST_AUTO_TEST_CASE(RibEntryClearRoutes) |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 127 | { |
| 128 | RibEntry entry; |
| 129 | entry.setName("/hello/world"); |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame^] | 130 | BOOST_CHECK_EQUAL(entry.getRoutes().size(), 0); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 131 | |
| 132 | Route route1; |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame^] | 133 | route1.setFaceId(42); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 134 | entry.addRoute(route1); |
| 135 | BOOST_REQUIRE_EQUAL(entry.getRoutes().size(), 1); |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame^] | 136 | BOOST_CHECK_EQUAL(entry.getRoutes().front(), route1); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 137 | |
| 138 | entry.clearRoutes(); |
| 139 | BOOST_CHECK_EQUAL(entry.getRoutes().size(), 0); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 140 | } |
| 141 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame^] | 142 | BOOST_AUTO_TEST_CASE(RibEntryEquality) |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 143 | { |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame^] | 144 | 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 | |
| 186 | BOOST_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 Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 192 | RibEntry entry; |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame^] | 193 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(entry), |
| 194 | "RibEntry(Prefix: /,\n" |
| 195 | " Routes: []\n" |
| 196 | " )"); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 197 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame^] | 198 | 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 Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 211 | } |
| 212 | |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 213 | BOOST_AUTO_TEST_SUITE_END() // TestRibEntry |
| 214 | BOOST_AUTO_TEST_SUITE_END() // Nfd |
| 215 | BOOST_AUTO_TEST_SUITE_END() // Mgmt |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 216 | |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 217 | } // namespace tests |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 218 | } // namespace nfd |
| 219 | } // namespace ndn |