Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 332783b | 2018-02-04 23:52:46 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | 1d2d337 | 2025-01-14 12:04:12 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2025, Regents of the University of California, |
Yanbiao Li | 6704a4a | 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 | */ |
| 25 | |
| 26 | #include "mgmt/strategy-choice-manager.hpp" |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 27 | #include "table/strategy-choice.hpp" |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [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/fw/dummy-strategy.hpp" |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 31 | |
Junxiao Shi | 25c6ce4 | 2016-09-09 13:49:59 +0000 | [diff] [blame] | 32 | #include <ndn-cxx/mgmt/nfd/strategy-choice.hpp> |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 33 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 34 | namespace nfd::tests { |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 35 | |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 36 | class StrategyChoiceManagerFixture : public ManagerFixtureWithAuthenticator |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 37 | { |
| 38 | public: |
| 39 | StrategyChoiceManagerFixture() |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 40 | : sc(m_forwarder.getStrategyChoice()) |
| 41 | , manager(sc, m_dispatcher, *m_authenticator) |
Eric Newberry | 358414d | 2021-03-21 20:56:42 -0700 | [diff] [blame] | 42 | , strategyNameP(Name("/strategy-choice-manager-P").appendVersion(2)) |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 43 | { |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 44 | VersionedDummyStrategy<2>::registerAs(strategyNameP); |
| 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("strategy-choice"); |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | public: |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 51 | /** \return whether exact-match StrategyChoice entry exists |
| 52 | */ |
| 53 | bool |
| 54 | hasEntry(const Name& name) const |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 55 | { |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 56 | return sc.get(name).first; |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 59 | /** \return strategy instance name from an exact-match StrategyChoice entry |
| 60 | */ |
| 61 | Name |
| 62 | getInstanceName(const Name& name) const |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 63 | { |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 64 | auto [hasEntry, instanceName] = sc.get(name); |
| 65 | return hasEntry ? instanceName : Name("/no-StrategyChoice-entry-at").append(name); |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | protected: |
Davide Pesavento | 1d2d337 | 2025-01-14 12:04:12 -0500 | [diff] [blame] | 69 | static inline const Name SET_SC_REQUEST = Name("/localhost/nfd") |
| 70 | .append(ndn::nfd::StrategyChoiceSetCommand::getName()); |
| 71 | static inline const Name UNSET_SC_REQUEST = Name("/localhost/nfd") |
| 72 | .append(ndn::nfd::StrategyChoiceUnsetCommand::getName()); |
| 73 | |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 74 | StrategyChoice& sc; |
| 75 | StrategyChoiceManager manager; |
| 76 | |
| 77 | const Name strategyNameP; |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 78 | }; |
| 79 | |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 80 | BOOST_AUTO_TEST_SUITE(Mgmt) |
| 81 | BOOST_FIXTURE_TEST_SUITE(TestStrategyChoiceManager, StrategyChoiceManagerFixture) |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 82 | |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 83 | BOOST_AUTO_TEST_CASE(SetSuccess) |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 84 | { |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 85 | ControlParameters reqParams; |
| 86 | reqParams.setName("/A") |
| 87 | .setStrategy(strategyNameP.getPrefix(-1)); // use unversioned strategy name in request |
Davide Pesavento | 1d2d337 | 2025-01-14 12:04:12 -0500 | [diff] [blame] | 88 | auto req = makeControlCommandRequest(SET_SC_REQUEST, reqParams); |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 89 | receiveInterest(req); |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 90 | |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 91 | ControlParameters expectedParams; |
| 92 | expectedParams.setName("/A") |
| 93 | .setStrategy(strategyNameP); // response should have versioned strategy name |
Davide Pesavento | 4296fe3 | 2025-01-14 23:00:41 -0500 | [diff] [blame] | 94 | ControlResponse expectedResp = makeResponse(200, "OK", expectedParams); |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 95 | BOOST_CHECK_EQUAL(checkResponse(0, req.getName(), expectedResp), |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 96 | CheckResponseResult::OK); |
| 97 | |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 98 | BOOST_CHECK_EQUAL(getInstanceName("/A"), strategyNameP); |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 99 | |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 100 | // Strategy versioning and parameters are not tested here because they are covered by |
| 101 | // Table/TestStrategyChoice test suite. |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 104 | BOOST_AUTO_TEST_CASE(SetUnknownStrategy) |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 105 | { |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 106 | ControlParameters reqParams; |
| 107 | reqParams.setName("/A") |
| 108 | .setStrategy("/strategy-choice-manager-unknown"); |
Davide Pesavento | 1d2d337 | 2025-01-14 12:04:12 -0500 | [diff] [blame] | 109 | auto req = makeControlCommandRequest(SET_SC_REQUEST, reqParams); |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 110 | receiveInterest(req); |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 111 | |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 112 | ControlResponse expectedResp; |
| 113 | expectedResp.setCode(404) |
| 114 | .setText("Strategy not registered"); |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 115 | BOOST_CHECK_EQUAL(checkResponse(0, req.getName(), expectedResp), |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 116 | CheckResponseResult::OK); |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 117 | |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 118 | BOOST_CHECK_EQUAL(hasEntry("/A"), false); |
| 119 | } |
| 120 | |
Junxiao Shi | 332783b | 2018-02-04 23:52:46 +0000 | [diff] [blame] | 121 | BOOST_AUTO_TEST_CASE(SetNameTooLong) |
| 122 | { |
| 123 | Name prefix; |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 124 | while (prefix.size() <= NameTree::getMaxDepth()) { |
Junxiao Shi | 332783b | 2018-02-04 23:52:46 +0000 | [diff] [blame] | 125 | prefix.append("A"); |
| 126 | } |
| 127 | ControlParameters reqParams; |
| 128 | reqParams.setName(prefix) |
| 129 | .setStrategy(strategyNameP); |
Davide Pesavento | 1d2d337 | 2025-01-14 12:04:12 -0500 | [diff] [blame] | 130 | auto req = makeControlCommandRequest(SET_SC_REQUEST, reqParams); |
Junxiao Shi | 332783b | 2018-02-04 23:52:46 +0000 | [diff] [blame] | 131 | receiveInterest(req); |
| 132 | |
| 133 | ControlResponse expectedResp; |
| 134 | expectedResp.setCode(414) |
| 135 | .setText("Prefix has too many components (limit is " + |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 136 | std::to_string(NameTree::getMaxDepth()) + ")"); |
Junxiao Shi | 332783b | 2018-02-04 23:52:46 +0000 | [diff] [blame] | 137 | BOOST_CHECK_EQUAL(checkResponse(0, req.getName(), expectedResp), |
| 138 | CheckResponseResult::OK); |
| 139 | |
| 140 | BOOST_CHECK_EQUAL(hasEntry(prefix), false); |
| 141 | } |
| 142 | |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 143 | BOOST_AUTO_TEST_CASE(UnsetSuccess) |
| 144 | { |
| 145 | auto insertRes = sc.insert("/A", strategyNameP); |
Davide Pesavento | 4296fe3 | 2025-01-14 23:00:41 -0500 | [diff] [blame] | 146 | BOOST_TEST_REQUIRE(insertRes); |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 147 | |
| 148 | ControlParameters reqParams; |
| 149 | reqParams.setName("/A"); |
Davide Pesavento | 1d2d337 | 2025-01-14 12:04:12 -0500 | [diff] [blame] | 150 | auto req = makeControlCommandRequest(UNSET_SC_REQUEST, reqParams); |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 151 | receiveInterest(req); |
| 152 | |
| 153 | ControlParameters expectedParams(reqParams); |
Davide Pesavento | 4296fe3 | 2025-01-14 23:00:41 -0500 | [diff] [blame] | 154 | ControlResponse expectedResp = makeResponse(200, "OK", expectedParams); |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 155 | BOOST_CHECK_EQUAL(checkResponse(0, req.getName(), expectedResp), |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 156 | CheckResponseResult::OK); |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 157 | |
| 158 | BOOST_CHECK_EQUAL(hasEntry("/A"), false); |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 161 | BOOST_AUTO_TEST_CASE(UnsetNoop) |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 162 | { |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 163 | ControlParameters reqParams; |
| 164 | reqParams.setName("/A"); |
Davide Pesavento | 1d2d337 | 2025-01-14 12:04:12 -0500 | [diff] [blame] | 165 | auto req = makeControlCommandRequest(UNSET_SC_REQUEST, reqParams); |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 166 | receiveInterest(req); |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 167 | |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 168 | ControlParameters expectedParams(reqParams); |
Davide Pesavento | 4296fe3 | 2025-01-14 23:00:41 -0500 | [diff] [blame] | 169 | ControlResponse expectedResp = makeResponse(200, "OK", expectedParams); |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 170 | BOOST_CHECK_EQUAL(checkResponse(0, req.getName(), expectedResp), |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 171 | CheckResponseResult::OK); |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 172 | |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 173 | BOOST_CHECK_EQUAL(hasEntry("/A"), false); |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 176 | BOOST_AUTO_TEST_CASE(UnsetRootForbidden) |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 177 | { |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 178 | ControlParameters reqParams; |
| 179 | reqParams.setName("/"); |
Davide Pesavento | 1d2d337 | 2025-01-14 12:04:12 -0500 | [diff] [blame] | 180 | auto req = makeControlCommandRequest(UNSET_SC_REQUEST, reqParams); |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 181 | receiveInterest(req); |
| 182 | |
| 183 | ControlResponse expectedResp; |
| 184 | expectedResp.setCode(400) |
| 185 | .setText("failed in validating parameters"); |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 186 | BOOST_CHECK_EQUAL(checkResponse(0, req.getName(), expectedResp), |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 187 | CheckResponseResult::OK); |
| 188 | |
| 189 | BOOST_CHECK_EQUAL(hasEntry("/"), true); |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 192 | BOOST_AUTO_TEST_CASE(StrategyChoiceDataset) |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 193 | { |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 194 | std::map<Name, Name> expected; // namespace => strategy instance name |
Davide Pesavento | 4296fe3 | 2025-01-14 23:00:41 -0500 | [diff] [blame] | 195 | for (const auto& entry : sc) { |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 196 | expected[entry.getPrefix()] = entry.getStrategyInstanceName(); |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 199 | for (size_t i = expected.size(); i < 1024; ++i) { |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 200 | Name name("/SC"); |
| 201 | name.appendNumber(i); |
| 202 | Name strategy = DummyStrategy::getStrategyName(i); |
| 203 | |
| 204 | auto insertRes = sc.insert(name, strategy); |
Davide Pesavento | 4296fe3 | 2025-01-14 23:00:41 -0500 | [diff] [blame] | 205 | BOOST_TEST(insertRes); |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 206 | expected[name] = strategy; |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 207 | } |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 208 | |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 209 | receiveInterest(Interest("/localhost/nfd/strategy-choice/list").setCanBePrefix(true)); |
| 210 | |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 211 | Block dataset = concatenateResponses(); |
| 212 | dataset.parse(); |
| 213 | BOOST_CHECK_EQUAL(dataset.elements_size(), expected.size()); |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 214 | |
Davide Pesavento | 4296fe3 | 2025-01-14 23:00:41 -0500 | [diff] [blame] | 215 | for (const auto& el : dataset.elements()) { |
| 216 | ndn::nfd::StrategyChoice record(el); |
Davide Pesavento | d7083a5 | 2023-10-19 17:51:16 -0400 | [diff] [blame] | 217 | BOOST_TEST_INFO_SCOPE(record); |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 218 | auto found = expected.find(record.getName()); |
| 219 | if (found == expected.end()) { |
| 220 | BOOST_ERROR("record has unexpected namespace " << record.getName()); |
| 221 | } |
| 222 | else { |
Davide Pesavento | d7083a5 | 2023-10-19 17:51:16 -0400 | [diff] [blame] | 223 | BOOST_TEST(record.getStrategy() == found->second); |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 224 | expected.erase(found); |
| 225 | } |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 228 | for (const auto& pair : expected) { |
| 229 | BOOST_ERROR("record for " << pair.first << " is missing"); |
| 230 | } |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | BOOST_AUTO_TEST_SUITE_END() // TestStrategyChoiceManager |
| 234 | BOOST_AUTO_TEST_SUITE_END() // Mgmt |
| 235 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 236 | } // namespace nfd::tests |