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