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