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