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 | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, 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 | |
| 34 | namespace nfd { |
| 35 | namespace tests { |
| 36 | |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 37 | class StrategyChoiceManagerFixture : public ManagerFixtureWithAuthenticator |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 38 | { |
| 39 | public: |
| 40 | StrategyChoiceManagerFixture() |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 41 | : sc(m_forwarder.getStrategyChoice()) |
| 42 | , manager(sc, m_dispatcher, *m_authenticator) |
| 43 | , strategyNameP("/strategy-choice-manager-P/%FD%02") |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 44 | { |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 45 | VersionedDummyStrategy<2>::registerAs(strategyNameP); |
| 46 | |
Junxiao Shi | 9ddf1b5 | 2016-08-22 03:58:55 +0000 | [diff] [blame] | 47 | setTopPrefix(); |
Yanbiao Li | df846e5 | 2016-01-30 21:53:47 -0800 | [diff] [blame] | 48 | setPrivilege("strategy-choice"); |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | public: |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 52 | /** \return whether exact-match StrategyChoice entry exists |
| 53 | */ |
| 54 | bool |
| 55 | hasEntry(const Name& name) const |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 56 | { |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 57 | return sc.get(name).first; |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 60 | /** \return strategy instance name from an exact-match StrategyChoice entry |
| 61 | */ |
| 62 | Name |
| 63 | getInstanceName(const Name& name) const |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 64 | { |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 65 | bool hasEntry = false; |
| 66 | Name instanceName; |
| 67 | std::tie(hasEntry, instanceName) = sc.get(name); |
| 68 | return hasEntry ? |
| 69 | instanceName : |
| 70 | Name("/no-StrategyChoice-entry-at").append(name); |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | protected: |
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 |
| 88 | auto req = makeControlCommandRequest("/localhost/nfd/strategy-choice/set", reqParams); |
| 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 |
| 94 | ControlResponse expectedResp; |
| 95 | expectedResp.setCode(200) |
| 96 | .setText("OK") |
| 97 | .setBody(expectedParams.wireEncode()); |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 98 | BOOST_CHECK_EQUAL(checkResponse(0, req.getName(), expectedResp), |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 99 | CheckResponseResult::OK); |
| 100 | |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 101 | BOOST_CHECK_EQUAL(getInstanceName("/A"), strategyNameP); |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 102 | |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 103 | // Strategy versioning and parameters are not tested here because they are covered by |
| 104 | // Table/TestStrategyChoice test suite. |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 107 | BOOST_AUTO_TEST_CASE(SetUnknownStrategy) |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 108 | { |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 109 | ControlParameters reqParams; |
| 110 | reqParams.setName("/A") |
| 111 | .setStrategy("/strategy-choice-manager-unknown"); |
| 112 | auto req = makeControlCommandRequest("/localhost/nfd/strategy-choice/set", reqParams); |
| 113 | receiveInterest(req); |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 114 | |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 115 | ControlResponse expectedResp; |
| 116 | expectedResp.setCode(404) |
| 117 | .setText("Strategy not registered"); |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 118 | BOOST_CHECK_EQUAL(checkResponse(0, req.getName(), expectedResp), |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 119 | CheckResponseResult::OK); |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 120 | |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 121 | BOOST_CHECK_EQUAL(hasEntry("/A"), false); |
| 122 | } |
| 123 | |
Junxiao Shi | 332783b | 2018-02-04 23:52:46 +0000 | [diff] [blame] | 124 | BOOST_AUTO_TEST_CASE(SetNameTooLong) |
| 125 | { |
| 126 | Name prefix; |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 127 | while (prefix.size() <= NameTree::getMaxDepth()) { |
Junxiao Shi | 332783b | 2018-02-04 23:52:46 +0000 | [diff] [blame] | 128 | prefix.append("A"); |
| 129 | } |
| 130 | ControlParameters reqParams; |
| 131 | reqParams.setName(prefix) |
| 132 | .setStrategy(strategyNameP); |
| 133 | auto req = makeControlCommandRequest("/localhost/nfd/strategy-choice/set", reqParams); |
| 134 | receiveInterest(req); |
| 135 | |
| 136 | ControlResponse expectedResp; |
| 137 | expectedResp.setCode(414) |
| 138 | .setText("Prefix has too many components (limit is " + |
| 139 | to_string(NameTree::getMaxDepth()) + ")"); |
| 140 | BOOST_CHECK_EQUAL(checkResponse(0, req.getName(), expectedResp), |
| 141 | CheckResponseResult::OK); |
| 142 | |
| 143 | BOOST_CHECK_EQUAL(hasEntry(prefix), false); |
| 144 | } |
| 145 | |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 146 | BOOST_AUTO_TEST_CASE(UnsetSuccess) |
| 147 | { |
| 148 | auto insertRes = sc.insert("/A", strategyNameP); |
| 149 | BOOST_REQUIRE(insertRes); |
| 150 | |
| 151 | ControlParameters reqParams; |
| 152 | reqParams.setName("/A"); |
| 153 | auto req = makeControlCommandRequest("/localhost/nfd/strategy-choice/unset", reqParams); |
| 154 | receiveInterest(req); |
| 155 | |
| 156 | ControlParameters expectedParams(reqParams); |
| 157 | ControlResponse expectedResp; |
| 158 | expectedResp.setCode(200) |
| 159 | .setText("OK") |
| 160 | .setBody(expectedParams.wireEncode()); |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 161 | BOOST_CHECK_EQUAL(checkResponse(0, req.getName(), expectedResp), |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 162 | CheckResponseResult::OK); |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 163 | |
| 164 | BOOST_CHECK_EQUAL(hasEntry("/A"), false); |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 167 | BOOST_AUTO_TEST_CASE(UnsetNoop) |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 168 | { |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 169 | ControlParameters reqParams; |
| 170 | reqParams.setName("/A"); |
| 171 | auto req = makeControlCommandRequest("/localhost/nfd/strategy-choice/unset", reqParams); |
| 172 | receiveInterest(req); |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 173 | |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 174 | ControlParameters expectedParams(reqParams); |
| 175 | ControlResponse expectedResp; |
| 176 | expectedResp.setCode(200) |
| 177 | .setText("OK") |
| 178 | .setBody(expectedParams.wireEncode()); |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 179 | BOOST_CHECK_EQUAL(checkResponse(0, req.getName(), expectedResp), |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 180 | CheckResponseResult::OK); |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 181 | |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 182 | BOOST_CHECK_EQUAL(hasEntry("/A"), false); |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 185 | BOOST_AUTO_TEST_CASE(UnsetRootForbidden) |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 186 | { |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 187 | ControlParameters reqParams; |
| 188 | reqParams.setName("/"); |
| 189 | auto req = makeControlCommandRequest("/localhost/nfd/strategy-choice/unset", reqParams); |
| 190 | receiveInterest(req); |
| 191 | |
| 192 | ControlResponse expectedResp; |
| 193 | expectedResp.setCode(400) |
| 194 | .setText("failed in validating parameters"); |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 195 | BOOST_CHECK_EQUAL(checkResponse(0, req.getName(), expectedResp), |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 196 | CheckResponseResult::OK); |
| 197 | |
| 198 | BOOST_CHECK_EQUAL(hasEntry("/"), true); |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 201 | BOOST_AUTO_TEST_CASE(StrategyChoiceDataset) |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 202 | { |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 203 | std::map<Name, Name> expected; // namespace => strategy instance name |
| 204 | for (const strategy_choice::Entry& entry : sc) { |
| 205 | expected[entry.getPrefix()] = entry.getStrategyInstanceName(); |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 208 | for (size_t i = expected.size(); i < 1024; ++i) { |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 209 | Name name("/SC"); |
| 210 | name.appendNumber(i); |
| 211 | Name strategy = DummyStrategy::getStrategyName(i); |
| 212 | |
| 213 | auto insertRes = sc.insert(name, strategy); |
| 214 | BOOST_CHECK(insertRes); |
| 215 | expected[name] = strategy; |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 216 | } |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 217 | |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 218 | receiveInterest(Interest("/localhost/nfd/strategy-choice/list").setCanBePrefix(true)); |
| 219 | |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 220 | Block dataset = concatenateResponses(); |
| 221 | dataset.parse(); |
| 222 | BOOST_CHECK_EQUAL(dataset.elements_size(), expected.size()); |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 223 | |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 224 | for (auto i = dataset.elements_begin(); i != dataset.elements_end(); ++i) { |
| 225 | ndn::nfd::StrategyChoice record(*i); |
| 226 | auto found = expected.find(record.getName()); |
| 227 | if (found == expected.end()) { |
| 228 | BOOST_ERROR("record has unexpected namespace " << record.getName()); |
| 229 | } |
| 230 | else { |
| 231 | BOOST_CHECK_MESSAGE(record.getStrategy() == found->second, |
| 232 | "record for " << record.getName() << " has wrong strategy " << record.getStrategy() << |
| 233 | ", should be " << found->second); |
| 234 | expected.erase(found); |
| 235 | } |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 238 | for (const auto& pair : expected) { |
| 239 | BOOST_ERROR("record for " << pair.first << " is missing"); |
| 240 | } |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | BOOST_AUTO_TEST_SUITE_END() // TestStrategyChoiceManager |
| 244 | BOOST_AUTO_TEST_SUITE_END() // Mgmt |
| 245 | |
| 246 | } // namespace tests |
| 247 | } // namespace nfd |