Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, 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 | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 29 | #include "nfd-manager-common-fixture.hpp" |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 30 | #include "../fw/dummy-strategy.hpp" |
Junxiao Shi | 25c6ce4 | 2016-09-09 13:49:59 +0000 | [diff] [blame] | 31 | #include <ndn-cxx/mgmt/nfd/strategy-choice.hpp> |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 32 | |
| 33 | namespace nfd { |
| 34 | namespace tests { |
| 35 | |
Yanbiao Li | df846e5 | 2016-01-30 21:53:47 -0800 | [diff] [blame] | 36 | class StrategyChoiceManagerFixture : public NfdManagerCommonFixture |
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) |
| 42 | , strategyNameP("/strategy-choice-manager-P/%FD%02") |
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 | { |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 64 | bool hasEntry = false; |
| 65 | Name instanceName; |
| 66 | std::tie(hasEntry, instanceName) = sc.get(name); |
| 67 | return hasEntry ? |
| 68 | instanceName : |
| 69 | Name("/no-StrategyChoice-entry-at").append(name); |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | protected: |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 73 | StrategyChoice& sc; |
| 74 | StrategyChoiceManager manager; |
| 75 | |
| 76 | const Name strategyNameP; |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 77 | }; |
| 78 | |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 79 | BOOST_AUTO_TEST_SUITE(Mgmt) |
| 80 | BOOST_FIXTURE_TEST_SUITE(TestStrategyChoiceManager, StrategyChoiceManagerFixture) |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 81 | |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 82 | BOOST_AUTO_TEST_CASE(SetSuccess) |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 83 | { |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 84 | ControlParameters reqParams; |
| 85 | reqParams.setName("/A") |
| 86 | .setStrategy(strategyNameP.getPrefix(-1)); // use unversioned strategy name in request |
| 87 | auto req = makeControlCommandRequest("/localhost/nfd/strategy-choice/set", reqParams); |
| 88 | receiveInterest(req); |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 89 | |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 90 | ControlParameters expectedParams; |
| 91 | expectedParams.setName("/A") |
| 92 | .setStrategy(strategyNameP); // response should have versioned strategy name |
| 93 | ControlResponse expectedResp; |
| 94 | expectedResp.setCode(200) |
| 95 | .setText("OK") |
| 96 | .setBody(expectedParams.wireEncode()); |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 97 | BOOST_CHECK_EQUAL(checkResponse(0, req.getName(), expectedResp), |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 98 | CheckResponseResult::OK); |
| 99 | |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 100 | BOOST_CHECK_EQUAL(getInstanceName("/A"), strategyNameP); |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 101 | |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 102 | // Strategy versioning and parameters are not tested here because they are covered by |
| 103 | // Table/TestStrategyChoice test suite. |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 106 | BOOST_AUTO_TEST_CASE(SetUnknownStrategy) |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 107 | { |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 108 | ControlParameters reqParams; |
| 109 | reqParams.setName("/A") |
| 110 | .setStrategy("/strategy-choice-manager-unknown"); |
| 111 | auto req = makeControlCommandRequest("/localhost/nfd/strategy-choice/set", reqParams); |
| 112 | receiveInterest(req); |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 113 | |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 114 | ControlResponse expectedResp; |
| 115 | expectedResp.setCode(404) |
| 116 | .setText("Strategy not registered"); |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 117 | BOOST_CHECK_EQUAL(checkResponse(0, req.getName(), expectedResp), |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 118 | CheckResponseResult::OK); |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 119 | |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 120 | BOOST_CHECK_EQUAL(hasEntry("/A"), false); |
| 121 | } |
| 122 | |
| 123 | BOOST_AUTO_TEST_CASE(UnsetSuccess) |
| 124 | { |
| 125 | auto insertRes = sc.insert("/A", strategyNameP); |
| 126 | BOOST_REQUIRE(insertRes); |
| 127 | |
| 128 | ControlParameters reqParams; |
| 129 | reqParams.setName("/A"); |
| 130 | auto req = makeControlCommandRequest("/localhost/nfd/strategy-choice/unset", reqParams); |
| 131 | receiveInterest(req); |
| 132 | |
| 133 | ControlParameters expectedParams(reqParams); |
| 134 | ControlResponse expectedResp; |
| 135 | expectedResp.setCode(200) |
| 136 | .setText("OK") |
| 137 | .setBody(expectedParams.wireEncode()); |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 138 | BOOST_CHECK_EQUAL(checkResponse(0, req.getName(), expectedResp), |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 139 | CheckResponseResult::OK); |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 140 | |
| 141 | BOOST_CHECK_EQUAL(hasEntry("/A"), false); |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 144 | BOOST_AUTO_TEST_CASE(UnsetNoop) |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 145 | { |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 146 | ControlParameters reqParams; |
| 147 | reqParams.setName("/A"); |
| 148 | auto req = makeControlCommandRequest("/localhost/nfd/strategy-choice/unset", reqParams); |
| 149 | receiveInterest(req); |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 150 | |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 151 | ControlParameters expectedParams(reqParams); |
| 152 | ControlResponse expectedResp; |
| 153 | expectedResp.setCode(200) |
| 154 | .setText("OK") |
| 155 | .setBody(expectedParams.wireEncode()); |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 156 | BOOST_CHECK_EQUAL(checkResponse(0, req.getName(), expectedResp), |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 157 | CheckResponseResult::OK); |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 158 | |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 159 | BOOST_CHECK_EQUAL(hasEntry("/A"), false); |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 162 | BOOST_AUTO_TEST_CASE(UnsetRootForbidden) |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 163 | { |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 164 | ControlParameters reqParams; |
| 165 | reqParams.setName("/"); |
| 166 | auto req = makeControlCommandRequest("/localhost/nfd/strategy-choice/unset", reqParams); |
| 167 | receiveInterest(req); |
| 168 | |
| 169 | ControlResponse expectedResp; |
| 170 | expectedResp.setCode(400) |
| 171 | .setText("failed in validating parameters"); |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 172 | BOOST_CHECK_EQUAL(checkResponse(0, req.getName(), expectedResp), |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 173 | CheckResponseResult::OK); |
| 174 | |
| 175 | BOOST_CHECK_EQUAL(hasEntry("/"), true); |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 178 | BOOST_AUTO_TEST_CASE(StrategyChoiceDataset) |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 179 | { |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 180 | std::map<Name, Name> expected; // namespace => strategy instance name |
| 181 | for (const strategy_choice::Entry& entry : sc) { |
| 182 | expected[entry.getPrefix()] = entry.getStrategyInstanceName(); |
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 | for (int i = expected.size(); i < 1024; ++i) { |
| 186 | Name name("/SC"); |
| 187 | name.appendNumber(i); |
| 188 | Name strategy = DummyStrategy::getStrategyName(i); |
| 189 | |
| 190 | auto insertRes = sc.insert(name, strategy); |
| 191 | BOOST_CHECK(insertRes); |
| 192 | expected[name] = strategy; |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 193 | } |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 194 | |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 195 | receiveInterest(Interest("/localhost/nfd/strategy-choice/list")); |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 196 | Block dataset = concatenateResponses(); |
| 197 | dataset.parse(); |
| 198 | BOOST_CHECK_EQUAL(dataset.elements_size(), expected.size()); |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 199 | |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 200 | for (auto i = dataset.elements_begin(); i != dataset.elements_end(); ++i) { |
| 201 | ndn::nfd::StrategyChoice record(*i); |
| 202 | auto found = expected.find(record.getName()); |
| 203 | if (found == expected.end()) { |
| 204 | BOOST_ERROR("record has unexpected namespace " << record.getName()); |
| 205 | } |
| 206 | else { |
| 207 | BOOST_CHECK_MESSAGE(record.getStrategy() == found->second, |
| 208 | "record for " << record.getName() << " has wrong strategy " << record.getStrategy() << |
| 209 | ", should be " << found->second); |
| 210 | expected.erase(found); |
| 211 | } |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 214 | for (const auto& pair : expected) { |
| 215 | BOOST_ERROR("record for " << pair.first << " is missing"); |
| 216 | } |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | BOOST_AUTO_TEST_SUITE_END() // TestStrategyChoiceManager |
| 220 | BOOST_AUTO_TEST_SUITE_END() // Mgmt |
| 221 | |
| 222 | } // namespace tests |
| 223 | } // namespace nfd |