Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | ac238f2 | 2017-09-12 15:19:40 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [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 | |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 26 | #include "nfdc/strategy-choice-module.hpp" |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 27 | |
Junxiao Shi | 5d3e481 | 2017-04-05 16:52:59 +0000 | [diff] [blame] | 28 | #include "execute-command-fixture.hpp" |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 29 | #include "status-fixture.hpp" |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 30 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 31 | namespace nfd::tools::nfdc::tests { |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 32 | |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 33 | BOOST_AUTO_TEST_SUITE(Nfdc) |
Junxiao Shi | 5d3e481 | 2017-04-05 16:52:59 +0000 | [diff] [blame] | 34 | BOOST_AUTO_TEST_SUITE(TestStrategyChoiceModule) |
| 35 | |
| 36 | class StrategyListFixture : public ExecuteCommandFixture |
| 37 | { |
| 38 | protected: |
| 39 | bool |
| 40 | respondStrategyChoiceDataset(const Interest& interest) |
| 41 | { |
| 42 | if (!Name("/localhost/nfd/strategy-choice/list").isPrefixOf(interest.getName())) { |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | StrategyChoice entry1; |
| 47 | entry1.setName("/"); |
Eric Newberry | 358414d | 2021-03-21 20:56:42 -0700 | [diff] [blame] | 48 | entry1.setStrategy(Name("/strategyP").appendVersion(1)); |
Junxiao Shi | 5d3e481 | 2017-04-05 16:52:59 +0000 | [diff] [blame] | 49 | |
| 50 | StrategyChoice entry2; |
| 51 | entry2.setName("/52VRvpL9/Yqfut4TNHv"); |
Eric Newberry | 358414d | 2021-03-21 20:56:42 -0700 | [diff] [blame] | 52 | entry2.setStrategy(Name("/strategyQ").appendVersion(2)); |
Junxiao Shi | 5d3e481 | 2017-04-05 16:52:59 +0000 | [diff] [blame] | 53 | |
| 54 | this->sendDataset(interest.getName(), entry1, entry2); |
| 55 | return true; |
| 56 | } |
| 57 | }; |
| 58 | |
| 59 | BOOST_FIXTURE_TEST_SUITE(ListCommand, StrategyListFixture) |
| 60 | |
| 61 | BOOST_AUTO_TEST_CASE(Normal) |
| 62 | { |
| 63 | this->processInterest = [this] (const Interest& interest) { |
| 64 | BOOST_CHECK(this->respondStrategyChoiceDataset(interest)); |
| 65 | }; |
| 66 | |
| 67 | this->execute("strategy list"); |
| 68 | BOOST_CHECK_EQUAL(exitCode, 0); |
Eric Newberry | 7249fb4 | 2021-04-04 21:09:32 -0700 | [diff] [blame] | 69 | BOOST_CHECK(out.is_equal("prefix=/ strategy=/strategyP/v=1\n" |
| 70 | "prefix=/52VRvpL9/Yqfut4TNHv strategy=/strategyQ/v=2\n")); |
Junxiao Shi | 5d3e481 | 2017-04-05 16:52:59 +0000 | [diff] [blame] | 71 | BOOST_CHECK(err.is_empty()); |
| 72 | } |
| 73 | |
| 74 | BOOST_AUTO_TEST_CASE(ErrorDataset) |
| 75 | { |
| 76 | this->processInterest = nullptr; // no response to dataset or command |
| 77 | |
| 78 | this->execute("strategy list"); |
| 79 | BOOST_CHECK_EQUAL(exitCode, 1); |
| 80 | BOOST_CHECK(out.is_empty()); |
Eric Newberry | 359135c | 2018-06-26 21:02:12 -0700 | [diff] [blame] | 81 | BOOST_CHECK(err.is_equal("Error 10060 when fetching strategy choice dataset: Timeout exceeded\n")); |
Junxiao Shi | 5d3e481 | 2017-04-05 16:52:59 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | BOOST_AUTO_TEST_SUITE_END() // ListCommand |
| 85 | |
| 86 | BOOST_FIXTURE_TEST_SUITE(ShowCommand, StrategyListFixture) |
| 87 | |
| 88 | BOOST_AUTO_TEST_CASE(NormalDefaultStrategy) |
| 89 | { |
| 90 | this->processInterest = [this] (const Interest& interest) { |
| 91 | BOOST_CHECK(this->respondStrategyChoiceDataset(interest)); |
| 92 | }; |
| 93 | |
| 94 | this->execute("strategy show /I1Ixgg0X"); |
| 95 | BOOST_CHECK_EQUAL(exitCode, 0); |
| 96 | BOOST_CHECK(out.is_equal(" prefix=/\n" |
Eric Newberry | 7249fb4 | 2021-04-04 21:09:32 -0700 | [diff] [blame] | 97 | "strategy=/strategyP/v=1\n")); |
Junxiao Shi | 5d3e481 | 2017-04-05 16:52:59 +0000 | [diff] [blame] | 98 | BOOST_CHECK(err.is_empty()); |
| 99 | } |
| 100 | |
| 101 | BOOST_AUTO_TEST_CASE(NormalNonDefaultStrategy) |
| 102 | { |
| 103 | this->processInterest = [this] (const Interest& interest) { |
| 104 | BOOST_CHECK(this->respondStrategyChoiceDataset(interest)); |
| 105 | }; |
| 106 | |
| 107 | this->execute("strategy show /52VRvpL9/Yqfut4TNHv/Y5gY7gom"); |
| 108 | BOOST_CHECK_EQUAL(exitCode, 0); |
| 109 | BOOST_CHECK(out.is_equal(" prefix=/52VRvpL9/Yqfut4TNHv\n" |
Eric Newberry | 7249fb4 | 2021-04-04 21:09:32 -0700 | [diff] [blame] | 110 | "strategy=/strategyQ/v=2\n")); |
Junxiao Shi | 5d3e481 | 2017-04-05 16:52:59 +0000 | [diff] [blame] | 111 | BOOST_CHECK(err.is_empty()); |
| 112 | } |
| 113 | |
| 114 | BOOST_AUTO_TEST_CASE(ErrorDataset) |
| 115 | { |
| 116 | this->processInterest = nullptr; // no response to dataset or command |
| 117 | |
| 118 | this->execute("strategy show /xVoIhNsJ"); |
| 119 | BOOST_CHECK_EQUAL(exitCode, 1); |
| 120 | BOOST_CHECK(out.is_empty()); |
Eric Newberry | 359135c | 2018-06-26 21:02:12 -0700 | [diff] [blame] | 121 | BOOST_CHECK(err.is_equal("Error 10060 when fetching strategy choice dataset: Timeout exceeded\n")); |
Junxiao Shi | 5d3e481 | 2017-04-05 16:52:59 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | BOOST_AUTO_TEST_SUITE_END() // ShowCommand |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 125 | |
Junxiao Shi | b283f52 | 2017-04-06 20:46:15 +0000 | [diff] [blame] | 126 | BOOST_FIXTURE_TEST_SUITE(SetCommand, ExecuteCommandFixture) |
| 127 | |
| 128 | BOOST_AUTO_TEST_CASE(Normal) |
| 129 | { |
| 130 | this->processInterest = [this] (const Interest& interest) { |
| 131 | ControlParameters req = MOCK_NFD_MGMT_REQUIRE_COMMAND_IS("/localhost/nfd/strategy-choice/set"); |
| 132 | BOOST_REQUIRE(req.hasName()); |
| 133 | BOOST_CHECK_EQUAL(req.getName(), "/VBXSJg3m/XYs81ARNhx"); |
| 134 | BOOST_REQUIRE(req.hasStrategy()); |
| 135 | BOOST_CHECK_EQUAL(req.getStrategy(), "/strategyP"); |
| 136 | |
| 137 | ControlParameters resp; |
| 138 | resp.setName("/VBXSJg3m/XYs81ARNhx"); |
Eric Newberry | 358414d | 2021-03-21 20:56:42 -0700 | [diff] [blame] | 139 | resp.setStrategy(Name("/strategyP").appendVersion(5)); |
Junxiao Shi | b283f52 | 2017-04-06 20:46:15 +0000 | [diff] [blame] | 140 | this->succeedCommand(interest, resp); |
| 141 | }; |
| 142 | |
| 143 | this->execute("strategy set /VBXSJg3m/XYs81ARNhx /strategyP"); |
| 144 | BOOST_CHECK_EQUAL(exitCode, 0); |
Eric Newberry | 7249fb4 | 2021-04-04 21:09:32 -0700 | [diff] [blame] | 145 | BOOST_CHECK(out.is_equal("strategy-set prefix=/VBXSJg3m/XYs81ARNhx strategy=/strategyP/v=5\n")); |
Junxiao Shi | b283f52 | 2017-04-06 20:46:15 +0000 | [diff] [blame] | 146 | BOOST_CHECK(err.is_empty()); |
| 147 | } |
| 148 | |
| 149 | BOOST_AUTO_TEST_CASE(UnknownStrategy) |
| 150 | { |
| 151 | this->processInterest = [this] (const Interest& interest) { |
| 152 | ControlParameters req = MOCK_NFD_MGMT_REQUIRE_COMMAND_IS("/localhost/nfd/strategy-choice/set"); |
| 153 | |
| 154 | ControlParameters resp; |
| 155 | this->failCommand(interest, 404, "strategy not found"); |
| 156 | }; |
| 157 | |
| 158 | this->execute("strategy set /zezRSP1I /strategyQ"); |
| 159 | BOOST_CHECK_EQUAL(exitCode, 7); |
| 160 | BOOST_CHECK(out.is_empty()); |
| 161 | BOOST_CHECK(err.is_equal("Unknown strategy: /strategyQ\n")); |
| 162 | } |
| 163 | |
| 164 | BOOST_AUTO_TEST_CASE(ErrorCommand) |
| 165 | { |
| 166 | this->processInterest = nullptr; // no response to command |
| 167 | |
| 168 | this->execute("strategy set /LJdNFfQJ8 /strategyP"); |
| 169 | BOOST_CHECK_EQUAL(exitCode, 1); |
| 170 | BOOST_CHECK(out.is_empty()); |
| 171 | BOOST_CHECK(err.is_equal("Error 10060 when setting strategy: request timed out\n")); |
| 172 | } |
| 173 | |
| 174 | BOOST_AUTO_TEST_SUITE_END() // SetCommand |
| 175 | |
| 176 | BOOST_FIXTURE_TEST_SUITE(UnsetCommand, ExecuteCommandFixture) |
| 177 | |
| 178 | BOOST_AUTO_TEST_CASE(Normal) |
| 179 | { |
| 180 | this->processInterest = [this] (const Interest& interest) { |
| 181 | ControlParameters req = MOCK_NFD_MGMT_REQUIRE_COMMAND_IS("/localhost/nfd/strategy-choice/unset"); |
| 182 | BOOST_REQUIRE(req.hasName()); |
| 183 | BOOST_CHECK_EQUAL(req.getName(), "/CFVecryP"); |
| 184 | |
| 185 | ControlParameters resp; |
| 186 | resp.setName("/CFVecryP"); |
| 187 | this->succeedCommand(interest, resp); |
| 188 | }; |
| 189 | |
| 190 | this->execute("strategy unset /CFVecryP"); |
| 191 | BOOST_CHECK_EQUAL(exitCode, 0); |
| 192 | BOOST_CHECK(out.is_equal("strategy-unset prefix=/CFVecryP\n")); |
| 193 | BOOST_CHECK(err.is_empty()); |
| 194 | } |
| 195 | |
| 196 | BOOST_AUTO_TEST_CASE(CannotUnsetDefault) |
| 197 | { |
Davide Pesavento | ac238f2 | 2017-09-12 15:19:40 -0400 | [diff] [blame] | 198 | this->processInterest = [] (const Interest&) { |
Junxiao Shi | b283f52 | 2017-04-06 20:46:15 +0000 | [diff] [blame] | 199 | BOOST_ERROR("unexpected command"); |
| 200 | }; |
| 201 | |
| 202 | this->execute("strategy unset /"); |
| 203 | BOOST_CHECK_EQUAL(exitCode, 2); |
| 204 | BOOST_CHECK(out.is_empty()); |
| 205 | BOOST_CHECK(err.is_equal("Unsetting default strategy is prohibited\n")); |
| 206 | } |
| 207 | |
| 208 | BOOST_AUTO_TEST_CASE(ErrorCommand) |
| 209 | { |
| 210 | this->processInterest = nullptr; // no response to command |
| 211 | |
| 212 | this->execute("strategy unset /GQcEsG96"); |
| 213 | BOOST_CHECK_EQUAL(exitCode, 1); |
| 214 | BOOST_CHECK(out.is_empty()); |
| 215 | BOOST_CHECK(err.is_equal("Error 10060 when unsetting strategy: request timed out\n")); |
| 216 | } |
| 217 | |
| 218 | BOOST_AUTO_TEST_SUITE_END() // UnsetCommand |
| 219 | |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 220 | const std::string STATUS_XML = stripXmlSpaces(R"XML( |
| 221 | <strategyChoices> |
| 222 | <strategyChoice> |
| 223 | <namespace>/</namespace> |
| 224 | <strategy> |
Eric Newberry | 7249fb4 | 2021-04-04 21:09:32 -0700 | [diff] [blame] | 225 | <name>/localhost/nfd/strategy/best-route/v=4</name> |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 226 | </strategy> |
| 227 | </strategyChoice> |
| 228 | <strategyChoice> |
| 229 | <namespace>/localhost</namespace> |
| 230 | <strategy> |
Eric Newberry | 7249fb4 | 2021-04-04 21:09:32 -0700 | [diff] [blame] | 231 | <name>/localhost/nfd/strategy/multicast/v=4</name> |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 232 | </strategy> |
| 233 | </strategyChoice> |
| 234 | </strategyChoices> |
| 235 | )XML"); |
| 236 | |
| 237 | const std::string STATUS_TEXT = std::string(R"TEXT( |
| 238 | Strategy choices: |
Eric Newberry | 7249fb4 | 2021-04-04 21:09:32 -0700 | [diff] [blame] | 239 | prefix=/ strategy=/localhost/nfd/strategy/best-route/v=4 |
| 240 | prefix=/localhost strategy=/localhost/nfd/strategy/multicast/v=4 |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 241 | )TEXT").substr(1); |
| 242 | |
Junxiao Shi | 5d3e481 | 2017-04-05 16:52:59 +0000 | [diff] [blame] | 243 | BOOST_FIXTURE_TEST_CASE(Status, StatusFixture<StrategyChoiceModule>) |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 244 | { |
| 245 | this->fetchStatus(); |
| 246 | StrategyChoice payload1; |
| 247 | payload1.setName("/") |
Eric Newberry | 358414d | 2021-03-21 20:56:42 -0700 | [diff] [blame] | 248 | .setStrategy(Name("/localhost/nfd/strategy/best-route").appendVersion(4)); |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 249 | StrategyChoice payload2; |
| 250 | payload2.setName("/localhost") |
Eric Newberry | 358414d | 2021-03-21 20:56:42 -0700 | [diff] [blame] | 251 | .setStrategy(Name("/localhost/nfd/strategy/multicast").appendVersion(4)); |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 252 | this->sendDataset("/localhost/nfd/strategy-choice/list", payload1, payload2); |
| 253 | this->prepareStatusOutput(); |
| 254 | |
| 255 | BOOST_CHECK(statusXml.is_equal(STATUS_XML)); |
| 256 | BOOST_CHECK(statusText.is_equal(STATUS_TEXT)); |
| 257 | } |
| 258 | |
| 259 | BOOST_AUTO_TEST_SUITE_END() // TestStrategyChoiceModule |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 260 | BOOST_AUTO_TEST_SUITE_END() // Nfdc |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 261 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 262 | } // namespace nfd::tools::nfdc::tests |