Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Yanbiao Li | df846e5 | 2016-01-30 21:53:47 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, 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 | |
Davide Pesavento | 5f47aa6 | 2016-10-07 22:09:09 +0200 | [diff] [blame] | 28 | #include "core/random.hpp" |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 29 | #include "face/face.hpp" |
| 30 | #include "face/internal-face.hpp" |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 31 | #include "fw/strategy.hpp" |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 32 | #include "table/name-tree.hpp" |
| 33 | #include "table/strategy-choice.hpp" |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame] | 34 | |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 35 | #include "nfd-manager-common-fixture.hpp" |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 36 | #include "../fw/dummy-strategy.hpp" |
| 37 | #include "../fw/choose-strategy.hpp" |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 38 | |
Junxiao Shi | 25c6ce4 | 2016-09-09 13:49:59 +0000 | [diff] [blame] | 39 | #include <ndn-cxx/mgmt/nfd/strategy-choice.hpp> |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 40 | |
| 41 | namespace nfd { |
| 42 | namespace tests { |
| 43 | |
Yanbiao Li | df846e5 | 2016-01-30 21:53:47 -0800 | [diff] [blame] | 44 | class StrategyChoiceManagerFixture : public NfdManagerCommonFixture |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 45 | { |
| 46 | public: |
| 47 | StrategyChoiceManagerFixture() |
| 48 | : m_strategyChoice(m_forwarder.getStrategyChoice()) |
Junxiao Shi | 9ddf1b5 | 2016-08-22 03:58:55 +0000 | [diff] [blame] | 49 | , m_manager(m_strategyChoice, m_dispatcher, *m_authenticator) |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 50 | { |
Junxiao Shi | 9ddf1b5 | 2016-08-22 03:58:55 +0000 | [diff] [blame] | 51 | setTopPrefix(); |
Yanbiao Li | df846e5 | 2016-01-30 21:53:47 -0800 | [diff] [blame] | 52 | setPrivilege("strategy-choice"); |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | public: |
| 56 | void |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame] | 57 | installStrategy(const Name& strategyName) |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 58 | { |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 59 | // install<DummyStrategy>(m_forwarder, strategyName); |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | const Name& |
| 63 | findStrategy(const Name& name) |
| 64 | { |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 65 | return m_strategyChoice.findEffectiveStrategy(name).getInstanceName(); |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | ControlParameters |
| 69 | makeParameters(const Name& name, const Name& strategy) |
| 70 | { |
| 71 | return ControlParameters().setName(name).setStrategy(strategy); |
| 72 | } |
| 73 | |
| 74 | protected: |
| 75 | StrategyChoice& m_strategyChoice; |
| 76 | StrategyChoiceManager m_manager; |
| 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 | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 82 | ///\todo #3868 rewrite test case after changing strategy versioning scheme |
Junxiao Shi | 65800d2 | 2016-12-25 21:05:45 +0000 | [diff] [blame] | 83 | BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(SetStrategy, 7) |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 84 | BOOST_AUTO_TEST_CASE(SetStrategy) |
| 85 | { |
| 86 | auto testSetStrategy = [this] (const ControlParameters& parameters) -> Name { |
| 87 | m_responses.clear(); |
| 88 | auto command = makeControlCommandRequest("/localhost/nfd/strategy-choice/set", parameters); |
| 89 | receiveInterest(command); |
| 90 | return command->getName(); |
| 91 | }; |
| 92 | |
| 93 | installStrategy("/localhost/nfd/strategy/test-strategy-a"); |
| 94 | installStrategy("/localhost/nfd/strategy/test-strategy-c/%FD%01"); // version 1 |
| 95 | installStrategy("/localhost/nfd/strategy/test-strategy-c/%FD%02"); // version 2 |
| 96 | |
| 97 | auto parametersA = makeParameters("test", "/localhost/nfd/strategy/test-strategy-a"); |
| 98 | auto parametersB = makeParameters("test", "/localhost/nfd/strategy/test-strategy-b"); |
| 99 | auto parametersC1 = makeParameters("test", "/localhost/nfd/strategy/test-strategy-c/%FD%01"); |
| 100 | auto parametersC = makeParameters("test", "/localhost/nfd/strategy/test-strategy-c"); |
| 101 | |
| 102 | auto commandNameA = testSetStrategy(parametersA); // succeed |
| 103 | BOOST_REQUIRE_EQUAL(m_responses.size(), 1); |
| 104 | BOOST_CHECK_EQUAL(checkResponse(0, commandNameA, makeResponse(200, "OK", parametersA)), |
| 105 | CheckResponseResult::OK); |
| 106 | BOOST_CHECK_EQUAL(findStrategy("/test"), "/localhost/nfd/strategy/test-strategy-a"); |
| 107 | |
| 108 | auto commandNameB = testSetStrategy(parametersB); // not installed |
| 109 | BOOST_REQUIRE_EQUAL(m_responses.size(), 1); |
| 110 | BOOST_CHECK_EQUAL(checkResponse(0, commandNameB, ControlResponse(504, "Unsupported strategy")), |
| 111 | CheckResponseResult::OK); |
| 112 | |
| 113 | auto commandNameC1 = testSetStrategy(parametersC1); // specified version |
| 114 | BOOST_REQUIRE_EQUAL(m_responses.size(), 1); |
| 115 | BOOST_CHECK_EQUAL(checkResponse(0, commandNameC1, makeResponse(200, "OK", parametersC1)), |
| 116 | CheckResponseResult::OK); |
| 117 | BOOST_CHECK_EQUAL(findStrategy("/test"), "/localhost/nfd/strategy/test-strategy-c/%FD%01"); |
| 118 | |
| 119 | auto commandNameC = testSetStrategy(parametersC); // latest version |
| 120 | parametersC.setStrategy("/localhost/nfd/strategy/test-strategy-c/%FD%02"); // change to latest |
| 121 | BOOST_REQUIRE_EQUAL(m_responses.size(), 1); |
| 122 | BOOST_CHECK_EQUAL(checkResponse(0, commandNameC, makeResponse(200, "OK", parametersC)), |
| 123 | CheckResponseResult::OK); |
| 124 | BOOST_CHECK_EQUAL(findStrategy("/test"), "/localhost/nfd/strategy/test-strategy-c/%FD%02"); |
| 125 | } |
| 126 | |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 127 | ///\todo #3868 rewrite test case after changing strategy versioning scheme |
| 128 | BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(UnsetStrategy, 9) |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 129 | BOOST_AUTO_TEST_CASE(UnsetStrategy) |
| 130 | { |
| 131 | auto testUnsetStrategy = [this] (const ControlParameters& parameters) -> Name { |
| 132 | m_responses.clear(); |
| 133 | auto command = makeControlCommandRequest("/localhost/nfd/strategy-choice/unset", parameters); |
| 134 | receiveInterest(command); |
| 135 | return command->getName(); |
| 136 | }; |
| 137 | |
| 138 | installStrategy("/localhost/nfd/strategy/test-strategy-a"); |
| 139 | installStrategy("/localhost/nfd/strategy/test-strategy-b"); |
| 140 | installStrategy("/localhost/nfd/strategy/test-strategy-c"); |
| 141 | |
| 142 | BOOST_CHECK(m_strategyChoice.insert("ndn:/", "/localhost/nfd/strategy/test-strategy-a")); // root |
| 143 | BOOST_CHECK(m_strategyChoice.insert("/test", "/localhost/nfd/strategy/test-strategy-b")); // test |
| 144 | BOOST_CHECK_EQUAL(findStrategy("/"), "/localhost/nfd/strategy/test-strategy-a"); |
| 145 | |
| 146 | auto parametersRoot = ControlParameters().setName("ndn:/"); // root prefix |
| 147 | auto parametersNone = ControlParameters().setName("/none"); // no such entry |
| 148 | auto parametersTest = ControlParameters().setName("/test"); // has such entry |
| 149 | |
| 150 | BOOST_CHECK_EQUAL(findStrategy("/"), "/localhost/nfd/strategy/test-strategy-a"); // root |
| 151 | auto commandRootName = testUnsetStrategy(parametersRoot); |
| 152 | auto expectedResponse = ControlResponse(400, "failed in validating parameters"); |
| 153 | BOOST_REQUIRE_EQUAL(m_responses.size(), 1); |
| 154 | BOOST_CHECK_EQUAL(checkResponse(0, commandRootName, expectedResponse), CheckResponseResult::OK); |
| 155 | BOOST_CHECK_EQUAL(findStrategy("/"), "/localhost/nfd/strategy/test-strategy-a"); // keep as root |
| 156 | |
| 157 | BOOST_CHECK_EQUAL(findStrategy("/none"), "/localhost/nfd/strategy/test-strategy-a"); // root |
| 158 | auto commandNoneName = testUnsetStrategy(parametersNone); |
| 159 | BOOST_REQUIRE_EQUAL(m_responses.size(), 1); |
| 160 | BOOST_CHECK_EQUAL(checkResponse(0, commandNoneName, makeResponse(200, "OK", parametersNone)), |
| 161 | CheckResponseResult::OK); |
| 162 | BOOST_CHECK_EQUAL(findStrategy("/none"), "/localhost/nfd/strategy/test-strategy-a"); // root |
| 163 | |
| 164 | BOOST_CHECK_EQUAL(findStrategy("/test"), "/localhost/nfd/strategy/test-strategy-b"); // self |
| 165 | auto commandTestName = testUnsetStrategy(parametersTest); |
| 166 | BOOST_REQUIRE_EQUAL(m_responses.size(), 1); |
| 167 | BOOST_CHECK_EQUAL(checkResponse(0, commandTestName, makeResponse(200, "OK", parametersTest)), |
| 168 | CheckResponseResult::OK); |
| 169 | BOOST_CHECK_EQUAL(findStrategy("/test"), "/localhost/nfd/strategy/test-strategy-a"); // parent |
| 170 | } |
| 171 | |
| 172 | // @todo Remove when ndn::nfd::StrategyChoice implements operator!= and operator<< |
| 173 | class StrategyChoice : public ndn::nfd::StrategyChoice |
| 174 | { |
| 175 | public: |
| 176 | StrategyChoice() = default; |
| 177 | |
| 178 | StrategyChoice(const ndn::nfd::StrategyChoice& entry) |
| 179 | : ndn::nfd::StrategyChoice(entry) |
| 180 | { |
| 181 | } |
| 182 | }; |
| 183 | |
| 184 | bool |
| 185 | operator!=(const StrategyChoice& left, const StrategyChoice& right) |
| 186 | { |
| 187 | return left.getName() != right.getName() || left.getStrategy() != right.getStrategy(); |
| 188 | } |
| 189 | |
| 190 | std::ostream& |
| 191 | operator<<(std::ostream &os, const StrategyChoice& entry) |
| 192 | { |
| 193 | os << "[ " << entry.getName() << ", " << entry.getStrategy() << " ]"; |
| 194 | return os; |
| 195 | } |
| 196 | |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 197 | ///\todo #3868 rewrite test case after changing strategy versioning scheme |
| 198 | BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(StrategyChoiceDataset, 4) |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 199 | BOOST_AUTO_TEST_CASE(StrategyChoiceDataset) |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 200 | { |
| 201 | size_t nPreInsertedStrategies = m_strategyChoice.size(); // the best-route strategy |
| 202 | std::set<Name> actualNames, actualStrategies; |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 203 | for (const auto& entry : m_strategyChoice) { |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 204 | actualNames.insert(entry.getPrefix()); |
Junxiao Shi | 4cb7431 | 2016-12-25 20:48:47 +0000 | [diff] [blame] | 205 | actualStrategies.insert(entry.getStrategyInstanceName()); |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Davide Pesavento | 5f47aa6 | 2016-10-07 22:09:09 +0200 | [diff] [blame] | 208 | std::uniform_int_distribution<uint64_t> dist; |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 209 | size_t nEntries = 1024; |
| 210 | for (size_t i = 0 ; i < nEntries ; i++) { |
| 211 | auto name = Name("test-name").appendSegment(i); |
Davide Pesavento | 5f47aa6 | 2016-10-07 22:09:09 +0200 | [diff] [blame] | 212 | auto strategy = Name("test-strategy").appendSegment(dist(getGlobalRng())); |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 213 | auto entry = ndn::nfd::StrategyChoice().setName(name).setStrategy(strategy); |
| 214 | actualNames.insert(name); |
| 215 | actualStrategies.insert(strategy); |
| 216 | installStrategy(strategy); |
| 217 | m_strategyChoice.insert(name, strategy); |
| 218 | } |
| 219 | nEntries += nPreInsertedStrategies; |
| 220 | |
| 221 | receiveInterest(makeInterest("/localhost/nfd/strategy-choice/list")); |
| 222 | |
| 223 | Block content; |
| 224 | BOOST_CHECK_NO_THROW(content = concatenateResponses()); |
| 225 | BOOST_CHECK_NO_THROW(content.parse()); |
| 226 | BOOST_CHECK_EQUAL(content.elements().size(), nEntries); |
| 227 | |
| 228 | std::vector<StrategyChoice> receivedRecords, expectedRecords; |
| 229 | for (size_t idx = 0; idx < nEntries; ++idx) { |
| 230 | BOOST_TEST_MESSAGE("processing element: " << idx); |
| 231 | |
| 232 | StrategyChoice decodedEntry; |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 233 | //BOOST_REQUIRE_NO_THROW(decodedEntry.wireDecode(content.elements()[idx])); |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 234 | receivedRecords.push_back(decodedEntry); |
| 235 | |
| 236 | actualNames.erase(decodedEntry.getName()); |
| 237 | actualStrategies.erase(decodedEntry.getStrategy()); |
| 238 | |
| 239 | auto result = m_strategyChoice.get(decodedEntry.getName()); |
| 240 | BOOST_REQUIRE(result.first); |
| 241 | |
| 242 | auto record = StrategyChoice().setName(decodedEntry.getName()).setStrategy(result.second); |
| 243 | expectedRecords.push_back(record); |
| 244 | } |
| 245 | |
| 246 | BOOST_CHECK_EQUAL(actualNames.size(), 0); |
| 247 | BOOST_CHECK_EQUAL(actualStrategies.size(), 0); |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 248 | BOOST_CHECK_EQUAL_COLLECTIONS(receivedRecords.begin(), receivedRecords.end(), |
| 249 | expectedRecords.begin(), expectedRecords.end()); |
| 250 | } |
| 251 | |
| 252 | BOOST_AUTO_TEST_SUITE_END() // TestStrategyChoiceManager |
| 253 | BOOST_AUTO_TEST_SUITE_END() // Mgmt |
| 254 | |
| 255 | } // namespace tests |
| 256 | } // namespace nfd |