Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 02a0e0f | 2018-02-06 02:03:45 +0000 | [diff] [blame^] | 2 | /* |
| 3 | * Copyright (c) 2014-2018, Regents of the University of California, |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 25 | |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 26 | #include "mgmt/fib-manager.hpp" |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 27 | #include "table/fib-nexthop.hpp" |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 28 | |
Yanbiao Li | df846e5 | 2016-01-30 21:53:47 -0800 | [diff] [blame] | 29 | #include "nfd-manager-common-fixture.hpp" |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 30 | #include "../face/dummy-face.hpp" |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 31 | |
Junxiao Shi | cbc8e94 | 2016-09-06 03:17:45 +0000 | [diff] [blame] | 32 | #include <ndn-cxx/lp/tags.hpp> |
Junxiao Shi | 25c6ce4 | 2016-09-09 13:49:59 +0000 | [diff] [blame] | 33 | #include <ndn-cxx/mgmt/nfd/fib-entry.hpp> |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 34 | |
| 35 | namespace nfd { |
| 36 | namespace tests { |
| 37 | |
Davide Pesavento | 1586aff | 2017-02-19 23:17:51 -0500 | [diff] [blame] | 38 | using ndn::nullopt; |
| 39 | |
Yanbiao Li | df846e5 | 2016-01-30 21:53:47 -0800 | [diff] [blame] | 40 | class FibManagerFixture : public NfdManagerCommonFixture |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 41 | { |
| 42 | public: |
| 43 | FibManagerFixture() |
| 44 | : m_fib(m_forwarder.getFib()) |
| 45 | , m_faceTable(m_forwarder.getFaceTable()) |
Junxiao Shi | 9ddf1b5 | 2016-08-22 03:58:55 +0000 | [diff] [blame] | 46 | , m_manager(m_fib, m_faceTable, m_dispatcher, *m_authenticator) |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 47 | { |
Junxiao Shi | 9ddf1b5 | 2016-08-22 03:58:55 +0000 | [diff] [blame] | 48 | setTopPrefix(); |
Yanbiao Li | df846e5 | 2016-01-30 21:53:47 -0800 | [diff] [blame] | 49 | setPrivilege("fib"); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | public: // for test |
Davide Pesavento | 1586aff | 2017-02-19 23:17:51 -0500 | [diff] [blame] | 53 | static ControlParameters |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 54 | makeParameters(const Name& name, const FaceId& id) |
| 55 | { |
| 56 | return ControlParameters().setName(name).setFaceId(id); |
| 57 | } |
| 58 | |
Davide Pesavento | 1586aff | 2017-02-19 23:17:51 -0500 | [diff] [blame] | 59 | static ControlParameters |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 60 | makeParameters(const Name& name, const FaceId& id, const uint32_t& cost) |
| 61 | { |
| 62 | return ControlParameters().setName(name).setFaceId(id).setCost(cost); |
| 63 | } |
| 64 | |
| 65 | FaceId |
| 66 | addFace() |
| 67 | { |
| 68 | auto face = make_shared<DummyFace>(); |
| 69 | m_faceTable.add(face); |
| 70 | advanceClocks(time::milliseconds(1), 10); |
| 71 | m_responses.clear(); // clear all event notifications, if any |
| 72 | return face->getId(); |
| 73 | } |
| 74 | |
| 75 | public: // for check |
| 76 | enum class CheckNextHopResult |
| 77 | { |
| 78 | OK, |
| 79 | NO_FIB_ENTRY, |
| 80 | WRONG_N_NEXTHOPS, |
| 81 | NO_NEXTHOP, |
| 82 | WRONG_COST |
Davide Pesavento | 1586aff | 2017-02-19 23:17:51 -0500 | [diff] [blame] | 83 | }; |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 84 | |
| 85 | /** |
| 86 | * @brief check whether the nexthop record is added / removed properly |
| 87 | * |
Davide Pesavento | 1586aff | 2017-02-19 23:17:51 -0500 | [diff] [blame] | 88 | * @param expectedNNextHops use nullopt to skip this check |
| 89 | * @param faceId use nullopt to skip NextHopRecord checks |
| 90 | * @param expectedCost use nullopt to skip this check |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 91 | * |
| 92 | * @retval OK FIB entry is found by exact match and has the expected number of nexthops; |
| 93 | * NextHopRe record for faceId is found and has the expected cost |
| 94 | * @retval NO_FIB_ENTRY FIB entry is not found |
| 95 | * @retval WRONG_N_NEXTHOPS FIB entry is found but has wrong number of nexthops |
| 96 | * @retval NO_NEXTHOP NextHopRecord for faceId is not found |
| 97 | * @retval WRONG_COST NextHopRecord for faceId has wrong cost |
| 98 | */ |
| 99 | CheckNextHopResult |
Davide Pesavento | 1586aff | 2017-02-19 23:17:51 -0500 | [diff] [blame] | 100 | checkNextHop(const Name& prefix, |
| 101 | ndn::optional<size_t> expectedNNextHops = nullopt, |
| 102 | ndn::optional<FaceId> faceId = nullopt, |
| 103 | ndn::optional<uint64_t> expectedCost = nullopt) const |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 104 | { |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 105 | const fib::Entry* entry = m_fib.findExactMatch(prefix); |
| 106 | if (entry == nullptr) { |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 107 | return CheckNextHopResult::NO_FIB_ENTRY; |
| 108 | } |
| 109 | |
Davide Pesavento | 1586aff | 2017-02-19 23:17:51 -0500 | [diff] [blame] | 110 | const auto& nextHops = entry->getNextHops(); |
| 111 | if (expectedNNextHops && nextHops.size() != *expectedNNextHops) { |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 112 | return CheckNextHopResult::WRONG_N_NEXTHOPS; |
| 113 | } |
| 114 | |
Davide Pesavento | 1586aff | 2017-02-19 23:17:51 -0500 | [diff] [blame] | 115 | if (faceId) { |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 116 | for (const auto& record : nextHops) { |
Davide Pesavento | 1586aff | 2017-02-19 23:17:51 -0500 | [diff] [blame] | 117 | if (record.getFace().getId() == *faceId) { |
| 118 | if (expectedCost && record.getCost() != *expectedCost) |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 119 | return CheckNextHopResult::WRONG_COST; |
| 120 | else |
| 121 | return CheckNextHopResult::OK; |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 122 | } |
| 123 | } |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 124 | return CheckNextHopResult::NO_NEXTHOP; |
| 125 | } |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 126 | return CheckNextHopResult::OK; |
| 127 | } |
| 128 | |
| 129 | protected: |
| 130 | Fib& m_fib; |
| 131 | FaceTable& m_faceTable; |
| 132 | FibManager m_manager; |
| 133 | }; |
| 134 | |
| 135 | std::ostream& |
Davide Pesavento | 1586aff | 2017-02-19 23:17:51 -0500 | [diff] [blame] | 136 | operator<<(std::ostream& os, FibManagerFixture::CheckNextHopResult result) |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 137 | { |
| 138 | switch (result) { |
| 139 | case FibManagerFixture::CheckNextHopResult::OK: |
Davide Pesavento | 1586aff | 2017-02-19 23:17:51 -0500 | [diff] [blame] | 140 | return os << "OK"; |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 141 | case FibManagerFixture::CheckNextHopResult::NO_FIB_ENTRY: |
Davide Pesavento | 1586aff | 2017-02-19 23:17:51 -0500 | [diff] [blame] | 142 | return os << "NO_FIB_ENTRY"; |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 143 | case FibManagerFixture::CheckNextHopResult::WRONG_N_NEXTHOPS: |
Davide Pesavento | 1586aff | 2017-02-19 23:17:51 -0500 | [diff] [blame] | 144 | return os << "WRONG_N_NEXTHOPS"; |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 145 | case FibManagerFixture::CheckNextHopResult::NO_NEXTHOP: |
Davide Pesavento | 1586aff | 2017-02-19 23:17:51 -0500 | [diff] [blame] | 146 | return os << "NO_NEXTHOP"; |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 147 | case FibManagerFixture::CheckNextHopResult::WRONG_COST: |
Davide Pesavento | 1586aff | 2017-02-19 23:17:51 -0500 | [diff] [blame] | 148 | return os << "WRONG_COST"; |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 149 | }; |
| 150 | |
Davide Pesavento | 1586aff | 2017-02-19 23:17:51 -0500 | [diff] [blame] | 151 | return os << static_cast<int>(result); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 154 | BOOST_AUTO_TEST_SUITE(Mgmt) |
| 155 | BOOST_FIXTURE_TEST_SUITE(TestFibManager, FibManagerFixture) |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 156 | |
| 157 | BOOST_AUTO_TEST_SUITE(AddNextHop) |
| 158 | |
| 159 | BOOST_AUTO_TEST_CASE(UnknownFaceId) |
| 160 | { |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 161 | auto req = makeControlCommandRequest("/localhost/nfd/fib/add-nexthop", |
| 162 | makeParameters("hello", face::FACEID_NULL, 101)); |
| 163 | receiveInterest(req); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 164 | BOOST_REQUIRE_EQUAL(m_responses.size(), 1); |
| 165 | |
| 166 | // check response |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 167 | BOOST_CHECK_EQUAL(checkResponse(0, req.getName(), ControlResponse(410, "Face not found")), |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 168 | CheckResponseResult::OK); |
| 169 | |
| 170 | // double check that the next hop was not added |
Davide Pesavento | 1586aff | 2017-02-19 23:17:51 -0500 | [diff] [blame] | 171 | BOOST_CHECK_EQUAL(checkNextHop("/hello", nullopt, nullopt, 101), CheckNextHopResult::NO_FIB_ENTRY); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Junxiao Shi | 02a0e0f | 2018-02-06 02:03:45 +0000 | [diff] [blame^] | 174 | BOOST_AUTO_TEST_CASE(NameTooLong) |
| 175 | { |
| 176 | Name prefix; |
| 177 | while (prefix.size() <= FIB_MAX_DEPTH) { |
| 178 | prefix.append("A"); |
| 179 | } |
| 180 | |
| 181 | auto req = makeControlCommandRequest("/localhost/nfd/fib/add-nexthop", |
| 182 | makeParameters(prefix, addFace())); |
| 183 | receiveInterest(req); |
| 184 | |
| 185 | ControlResponse expected(414, "FIB entry prefix cannot exceed " + |
| 186 | ndn::to_string(Fib::getMaxDepth()) + " components"); |
| 187 | BOOST_CHECK_EQUAL(checkResponse(0, req.getName(), expected), CheckResponseResult::OK); |
| 188 | |
| 189 | BOOST_CHECK_EQUAL(checkNextHop(prefix), CheckNextHopResult::NO_FIB_ENTRY); |
| 190 | } |
| 191 | |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 192 | BOOST_AUTO_TEST_CASE(ImplicitFaceId) |
| 193 | { |
| 194 | auto face1 = addFace(); |
| 195 | auto face2 = addFace(); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 196 | BOOST_REQUIRE_NE(face1, face::INVALID_FACEID); |
| 197 | BOOST_REQUIRE_NE(face2, face::INVALID_FACEID); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 198 | |
| 199 | Name expectedName; |
| 200 | ControlResponse expectedResponse; |
| 201 | auto testAddNextHop = [&] (ControlParameters parameters, const FaceId& faceId) { |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 202 | auto req = makeControlCommandRequest("/localhost/nfd/fib/add-nexthop", parameters); |
| 203 | req.setTag(make_shared<lp::IncomingFaceIdTag>(faceId)); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 204 | m_responses.clear(); |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 205 | expectedName = req.getName(); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 206 | expectedResponse = makeResponse(200, "Success", parameters.setFaceId(faceId)); |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 207 | receiveInterest(req); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 208 | }; |
| 209 | |
| 210 | testAddNextHop(ControlParameters().setName("/hello").setCost(100).setFaceId(0), face1); |
| 211 | BOOST_REQUIRE_EQUAL(m_responses.size(), 1); |
| 212 | BOOST_CHECK_EQUAL(checkResponse(0, expectedName, expectedResponse), CheckResponseResult::OK); |
| 213 | BOOST_CHECK_EQUAL(checkNextHop("/hello", 1, face1, 100), CheckNextHopResult::OK); |
| 214 | |
| 215 | testAddNextHop(ControlParameters().setName("/hello").setCost(100), face2); |
| 216 | BOOST_REQUIRE_EQUAL(m_responses.size(), 1); |
| 217 | BOOST_CHECK_EQUAL(checkResponse(0, expectedName, expectedResponse), CheckResponseResult::OK); |
| 218 | BOOST_CHECK_EQUAL(checkNextHop("/hello", 2, face2, 100), CheckNextHopResult::OK); |
| 219 | } |
| 220 | |
| 221 | BOOST_AUTO_TEST_CASE(InitialAdd) |
| 222 | { |
| 223 | FaceId addedFaceId = addFace(); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 224 | BOOST_REQUIRE_NE(addedFaceId, face::INVALID_FACEID); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 225 | |
| 226 | auto parameters = makeParameters("hello", addedFaceId, 101); |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 227 | auto req = makeControlCommandRequest("/localhost/nfd/fib/add-nexthop", parameters); |
| 228 | receiveInterest(req); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 229 | |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 230 | BOOST_REQUIRE_EQUAL(m_responses.size(), 1); |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 231 | BOOST_CHECK_EQUAL(checkResponse(0, req.getName(), makeResponse(200, "Success", parameters)), |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 232 | CheckResponseResult::OK); |
| 233 | BOOST_CHECK_EQUAL(checkNextHop("/hello", 1, addedFaceId, 101), CheckNextHopResult::OK); |
| 234 | } |
| 235 | |
| 236 | BOOST_AUTO_TEST_CASE(ImplicitCost) |
| 237 | { |
| 238 | FaceId addedFaceId = addFace(); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 239 | BOOST_REQUIRE_NE(addedFaceId, face::INVALID_FACEID); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 240 | |
| 241 | auto originalParameters = ControlParameters().setName("/hello").setFaceId(addedFaceId); |
| 242 | auto parameters = makeParameters("/hello", addedFaceId, 0); |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 243 | auto req = makeControlCommandRequest("/localhost/nfd/fib/add-nexthop", originalParameters); |
| 244 | receiveInterest(req); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 245 | |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 246 | BOOST_REQUIRE_EQUAL(m_responses.size(), 1); |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 247 | BOOST_CHECK_EQUAL(checkResponse(0, req.getName(), makeResponse(200, "Success", parameters)), |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 248 | CheckResponseResult::OK); |
| 249 | BOOST_CHECK_EQUAL(checkNextHop("/hello", 1, addedFaceId, 0), CheckNextHopResult::OK); |
| 250 | } |
| 251 | |
| 252 | BOOST_AUTO_TEST_CASE(AddToExisting) |
| 253 | { |
| 254 | FaceId face = addFace(); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 255 | BOOST_REQUIRE_NE(face, face::INVALID_FACEID); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 256 | |
| 257 | Name expectedName; |
| 258 | ControlResponse expectedResponse; |
| 259 | auto testAddNextHop = [&] (const ControlParameters& parameters) { |
| 260 | m_responses.clear(); |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 261 | auto req = makeControlCommandRequest("/localhost/nfd/fib/add-nexthop", parameters); |
| 262 | expectedName = req.getName(); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 263 | expectedResponse = makeResponse(200, "Success", parameters); |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 264 | receiveInterest(req); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 265 | }; |
| 266 | |
| 267 | // add initial, succeeds |
| 268 | testAddNextHop(makeParameters("/hello", face, 101)); |
| 269 | BOOST_REQUIRE_EQUAL(m_responses.size(), 1); |
| 270 | BOOST_CHECK_EQUAL(checkResponse(0, expectedName, expectedResponse), CheckResponseResult::OK); |
| 271 | |
| 272 | // add to existing --> update cost, succeeds |
| 273 | testAddNextHop(makeParameters("/hello", face, 102)); |
| 274 | BOOST_REQUIRE_EQUAL(m_responses.size(), 1); |
| 275 | BOOST_CHECK_EQUAL(checkResponse(0, expectedName, expectedResponse), CheckResponseResult::OK); |
| 276 | |
| 277 | BOOST_CHECK_EQUAL(checkNextHop("/hello", 2, face, 102), CheckNextHopResult::WRONG_N_NEXTHOPS); |
| 278 | BOOST_CHECK_EQUAL(checkNextHop("/hello", 1, face, 101), CheckNextHopResult::WRONG_COST); |
| 279 | BOOST_CHECK_EQUAL(checkNextHop("/hello", 1, face, 102), CheckNextHopResult::OK); |
| 280 | } |
| 281 | |
| 282 | BOOST_AUTO_TEST_SUITE_END() // AddNextHop |
| 283 | |
| 284 | BOOST_AUTO_TEST_SUITE(RemoveNextHop) |
| 285 | |
| 286 | BOOST_AUTO_TEST_CASE(Basic) |
| 287 | { |
| 288 | Name expectedName; |
| 289 | ControlResponse expectedResponse; |
| 290 | auto testRemoveNextHop = [&] (const ControlParameters& parameters) { |
| 291 | m_responses.clear(); |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 292 | auto req = makeControlCommandRequest("/localhost/nfd/fib/remove-nexthop", parameters); |
| 293 | expectedName = req.getName(); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 294 | expectedResponse = makeResponse(200, "Success", parameters); |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 295 | receiveInterest(req); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 296 | }; |
| 297 | |
| 298 | FaceId face1 = addFace(); |
| 299 | FaceId face2 = addFace(); |
| 300 | FaceId face3 = addFace(); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 301 | BOOST_REQUIRE_NE(face1, face::INVALID_FACEID); |
| 302 | BOOST_REQUIRE_NE(face2, face::INVALID_FACEID); |
| 303 | BOOST_REQUIRE_NE(face3, face::INVALID_FACEID); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 304 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 305 | fib::Entry* entry = m_fib.insert("/hello").first; |
| 306 | entry->addNextHop(*m_faceTable.get(face1), 101); |
| 307 | entry->addNextHop(*m_faceTable.get(face2), 202); |
| 308 | entry->addNextHop(*m_faceTable.get(face3), 303); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 309 | |
| 310 | testRemoveNextHop(makeParameters("/hello", face1)); |
| 311 | BOOST_REQUIRE_EQUAL(m_responses.size(), 1); |
| 312 | BOOST_CHECK_EQUAL(checkResponse(0, expectedName, expectedResponse), CheckResponseResult::OK); |
| 313 | BOOST_CHECK_EQUAL(checkNextHop("/hello", 2, face1, 101), CheckNextHopResult::NO_NEXTHOP); |
| 314 | |
| 315 | testRemoveNextHop(makeParameters("/hello", face2)); |
| 316 | BOOST_REQUIRE_EQUAL(m_responses.size(), 1); |
| 317 | BOOST_CHECK_EQUAL(checkResponse(0, expectedName, expectedResponse), CheckResponseResult::OK); |
| 318 | BOOST_CHECK_EQUAL(checkNextHop("/hello", 1, face2, 202), CheckNextHopResult::NO_NEXTHOP); |
| 319 | |
| 320 | testRemoveNextHop(makeParameters("/hello", face3)); |
| 321 | BOOST_REQUIRE_EQUAL(m_responses.size(), 1); |
| 322 | BOOST_CHECK_EQUAL(checkResponse(0, expectedName, expectedResponse), CheckResponseResult::OK); |
| 323 | BOOST_CHECK_EQUAL(checkNextHop("/hello", 0, face3, 303), CheckNextHopResult::NO_FIB_ENTRY); |
| 324 | } |
| 325 | |
| 326 | BOOST_AUTO_TEST_CASE(PrefixNotFound) |
| 327 | { |
| 328 | FaceId addedFaceId = addFace(); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 329 | BOOST_REQUIRE_NE(addedFaceId, face::INVALID_FACEID); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 330 | |
| 331 | auto parameters = makeParameters("hello", addedFaceId); |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 332 | auto req = makeControlCommandRequest("/localhost/nfd/fib/remove-nexthop", parameters); |
| 333 | receiveInterest(req); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 334 | BOOST_REQUIRE_EQUAL(m_responses.size(), 1); |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 335 | |
| 336 | auto expectedResponse = makeResponse(200, "Success", parameters); |
| 337 | BOOST_CHECK_EQUAL(checkResponse(0, req.getName(), expectedResponse), CheckResponseResult::OK); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | BOOST_AUTO_TEST_CASE(ImplicitFaceId) |
| 341 | { |
| 342 | auto face1 = addFace(); |
| 343 | auto face2 = addFace(); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 344 | BOOST_REQUIRE_NE(face1, face::INVALID_FACEID); |
| 345 | BOOST_REQUIRE_NE(face2, face::INVALID_FACEID); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 346 | |
| 347 | Name expectedName; |
| 348 | ControlResponse expectedResponse; |
| 349 | auto testWithImplicitFaceId = [&] (ControlParameters parameters, FaceId face) { |
| 350 | m_responses.clear(); |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 351 | auto req = makeControlCommandRequest("/localhost/nfd/fib/remove-nexthop", parameters); |
| 352 | req.setTag(make_shared<lp::IncomingFaceIdTag>(face)); |
| 353 | expectedName = req.getName(); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 354 | expectedResponse = makeResponse(200, "Success", parameters.setFaceId(face)); |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 355 | receiveInterest(req); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 356 | }; |
| 357 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 358 | fib::Entry* entry = m_fib.insert("/hello").first; |
| 359 | entry->addNextHop(*m_faceTable.get(face1), 101); |
| 360 | entry->addNextHop(*m_faceTable.get(face2), 202); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 361 | |
| 362 | testWithImplicitFaceId(ControlParameters().setName("/hello").setFaceId(0), face1); |
| 363 | BOOST_REQUIRE_EQUAL(m_responses.size(), 1); |
| 364 | BOOST_CHECK_EQUAL(checkResponse(0, expectedName, expectedResponse), CheckResponseResult::OK); |
| 365 | BOOST_CHECK_EQUAL(checkNextHop("/hello", 1, face1, 101), CheckNextHopResult::NO_NEXTHOP); |
| 366 | |
| 367 | testWithImplicitFaceId(ControlParameters().setName("/hello"), face2); |
| 368 | BOOST_REQUIRE_EQUAL(m_responses.size(), 1); |
| 369 | BOOST_CHECK_EQUAL(checkResponse(0, expectedName, expectedResponse), CheckResponseResult::OK); |
| 370 | BOOST_CHECK_EQUAL(checkNextHop("/hello", 0, face2, 202), CheckNextHopResult::NO_FIB_ENTRY); |
| 371 | } |
| 372 | |
| 373 | BOOST_AUTO_TEST_CASE(RecordNotExist) |
| 374 | { |
| 375 | auto face1 = addFace(); |
| 376 | auto face2 = addFace(); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 377 | BOOST_REQUIRE_NE(face1, face::INVALID_FACEID); |
| 378 | BOOST_REQUIRE_NE(face2, face::INVALID_FACEID); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 379 | |
| 380 | Name expectedName; |
| 381 | ControlResponse expectedResponse; |
| 382 | auto testRemoveNextHop = [&] (ControlParameters parameters) { |
| 383 | m_responses.clear(); |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 384 | auto req = makeControlCommandRequest("/localhost/nfd/fib/remove-nexthop", parameters); |
| 385 | expectedName = req.getName(); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 386 | expectedResponse = makeResponse(200, "Success", parameters); |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 387 | receiveInterest(req); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 388 | }; |
| 389 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 390 | m_fib.insert("/hello").first->addNextHop(*m_faceTable.get(face1), 101); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 391 | |
| 392 | testRemoveNextHop(makeParameters("/hello", face2 + 100)); |
| 393 | BOOST_REQUIRE_EQUAL(m_responses.size(), 1); // face does not exist |
| 394 | BOOST_CHECK_EQUAL(checkResponse(0, expectedName, expectedResponse), CheckResponseResult::OK); |
Davide Pesavento | 1586aff | 2017-02-19 23:17:51 -0500 | [diff] [blame] | 395 | BOOST_CHECK_EQUAL(checkNextHop("/hello", nullopt, face2 + 100), CheckNextHopResult::NO_NEXTHOP); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 396 | |
| 397 | testRemoveNextHop(makeParameters("/hello", face2)); |
| 398 | BOOST_REQUIRE_EQUAL(m_responses.size(), 1); // record does not exist |
| 399 | BOOST_CHECK_EQUAL(checkResponse(0, expectedName, expectedResponse), CheckResponseResult::OK); |
Davide Pesavento | 1586aff | 2017-02-19 23:17:51 -0500 | [diff] [blame] | 400 | BOOST_CHECK_EQUAL(checkNextHop("/hello", nullopt, face2), CheckNextHopResult::NO_NEXTHOP); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | BOOST_AUTO_TEST_SUITE_END() // RemoveNextHop |
| 404 | |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 405 | BOOST_AUTO_TEST_SUITE(List) |
| 406 | |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 407 | BOOST_AUTO_TEST_CASE(FibDataset) |
| 408 | { |
| 409 | const size_t nEntries = 108; |
| 410 | std::set<Name> actualPrefixes; |
| 411 | for (size_t i = 0 ; i < nEntries ; i ++) { |
| 412 | Name prefix = Name("test").appendSegment(i); |
| 413 | actualPrefixes.insert(prefix); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 414 | fib::Entry* fibEntry = m_fib.insert(prefix).first; |
| 415 | fibEntry->addNextHop(*m_faceTable.get(addFace()), std::numeric_limits<uint8_t>::max() - 1); |
| 416 | fibEntry->addNextHop(*m_faceTable.get(addFace()), std::numeric_limits<uint8_t>::max() - 2); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 419 | receiveInterest(Interest("/localhost/nfd/fib/list")); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 420 | |
Davide Pesavento | 1586aff | 2017-02-19 23:17:51 -0500 | [diff] [blame] | 421 | Block content = concatenateResponses(); |
| 422 | content.parse(); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 423 | BOOST_REQUIRE_EQUAL(content.elements().size(), nEntries); |
| 424 | |
Davide Pesavento | 1586aff | 2017-02-19 23:17:51 -0500 | [diff] [blame] | 425 | std::vector<ndn::nfd::FibEntry> receivedRecords, expectedRecords; |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 426 | for (size_t idx = 0; idx < nEntries; ++idx) { |
Davide Pesavento | 1586aff | 2017-02-19 23:17:51 -0500 | [diff] [blame] | 427 | ndn::nfd::FibEntry decodedEntry(content.elements()[idx]); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 428 | receivedRecords.push_back(decodedEntry); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 429 | actualPrefixes.erase(decodedEntry.getPrefix()); |
| 430 | |
| 431 | auto matchedEntry = m_fib.findExactMatch(decodedEntry.getPrefix()); |
| 432 | BOOST_REQUIRE(matchedEntry != nullptr); |
| 433 | |
Davide Pesavento | 1586aff | 2017-02-19 23:17:51 -0500 | [diff] [blame] | 434 | expectedRecords.emplace_back(); |
| 435 | expectedRecords.back().setPrefix(matchedEntry->getPrefix()); |
| 436 | for (const auto& nh : matchedEntry->getNextHops()) { |
| 437 | expectedRecords.back().addNextHopRecord(ndn::nfd::NextHopRecord() |
| 438 | .setFaceId(nh.getFace().getId()) |
| 439 | .setCost(nh.getCost())); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 440 | } |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | BOOST_CHECK_EQUAL(actualPrefixes.size(), 0); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 444 | BOOST_CHECK_EQUAL_COLLECTIONS(receivedRecords.begin(), receivedRecords.end(), |
| 445 | expectedRecords.begin(), expectedRecords.end()); |
| 446 | } |
| 447 | |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 448 | BOOST_AUTO_TEST_SUITE_END() // List |
| 449 | |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 450 | BOOST_AUTO_TEST_SUITE_END() // TestFibManager |
| 451 | BOOST_AUTO_TEST_SUITE_END() // Mgmt |
| 452 | |
| 453 | } // namespace tests |
| 454 | } // namespace nfd |