Junxiao Shi | 3160a3f | 2018-01-09 21:25:15 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Eric Newberry | 4995bf9 | 2020-04-18 16:59:19 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2020, Regents of the University of California, |
Junxiao Shi | 3160a3f | 2018-01-09 21:25:15 +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 | |
| 26 | #include "nfdc/cs-module.hpp" |
| 27 | |
| 28 | #include "status-fixture.hpp" |
Junxiao Shi | cdf7845 | 2018-03-02 23:14:15 +0000 | [diff] [blame] | 29 | #include "execute-command-fixture.hpp" |
Junxiao Shi | 3160a3f | 2018-01-09 21:25:15 +0000 | [diff] [blame] | 30 | |
| 31 | namespace nfd { |
| 32 | namespace tools { |
| 33 | namespace nfdc { |
| 34 | namespace tests { |
| 35 | |
| 36 | BOOST_AUTO_TEST_SUITE(Nfdc) |
Junxiao Shi | cdf7845 | 2018-03-02 23:14:15 +0000 | [diff] [blame] | 37 | BOOST_AUTO_TEST_SUITE(TestCsModule) |
| 38 | |
| 39 | BOOST_FIXTURE_TEST_SUITE(ConfigCommand, ExecuteCommandFixture) |
| 40 | |
| 41 | BOOST_AUTO_TEST_CASE(Normal) |
| 42 | { |
| 43 | this->processInterest = [this] (const Interest& interest) { |
| 44 | ControlParameters req = MOCK_NFD_MGMT_REQUIRE_COMMAND_IS("/localhost/nfd/cs/config"); |
| 45 | BOOST_REQUIRE(req.hasCapacity()); |
| 46 | BOOST_CHECK_EQUAL(req.getCapacity(), 29850); |
| 47 | BOOST_CHECK(req.hasFlagBit(ndn::nfd::BIT_CS_ENABLE_ADMIT)); |
| 48 | BOOST_CHECK_EQUAL(req.getFlagBit(ndn::nfd::BIT_CS_ENABLE_ADMIT), false); |
| 49 | BOOST_CHECK(req.hasFlagBit(ndn::nfd::BIT_CS_ENABLE_SERVE)); |
| 50 | BOOST_CHECK_EQUAL(req.getFlagBit(ndn::nfd::BIT_CS_ENABLE_SERVE), true); |
| 51 | |
| 52 | ControlParameters resp(req); |
| 53 | resp.unsetMask(); |
| 54 | this->succeedCommand(interest, resp); |
| 55 | }; |
| 56 | |
| 57 | this->execute("cs config admit off serve on capacity 29850"); |
| 58 | BOOST_CHECK_EQUAL(exitCode, 0); |
| 59 | BOOST_CHECK(out.is_equal("cs-config-updated capacity=29850 admit=off serve=on\n")); |
| 60 | BOOST_CHECK(err.is_empty()); |
| 61 | } |
| 62 | |
| 63 | BOOST_AUTO_TEST_CASE(NoUpdate) |
| 64 | { |
| 65 | this->processInterest = [this] (const Interest& interest) { |
| 66 | ControlParameters req = MOCK_NFD_MGMT_REQUIRE_COMMAND_IS("/localhost/nfd/cs/config"); |
| 67 | BOOST_CHECK(!req.hasCapacity()); |
| 68 | BOOST_CHECK(!req.hasFlagBit(ndn::nfd::BIT_CS_ENABLE_ADMIT)); |
| 69 | BOOST_CHECK(!req.hasFlagBit(ndn::nfd::BIT_CS_ENABLE_SERVE)); |
| 70 | |
| 71 | ControlParameters resp; |
| 72 | resp.setCapacity(573599); |
| 73 | resp.setFlagBit(ndn::nfd::BIT_CS_ENABLE_ADMIT, true, false); |
| 74 | resp.setFlagBit(ndn::nfd::BIT_CS_ENABLE_SERVE, false, false); |
| 75 | this->succeedCommand(interest, resp); |
| 76 | }; |
| 77 | |
| 78 | this->execute("cs config"); |
| 79 | BOOST_CHECK_EQUAL(exitCode, 0); |
| 80 | BOOST_CHECK(out.is_equal("cs-config-updated capacity=573599 admit=on serve=off\n")); |
| 81 | BOOST_CHECK(err.is_empty()); |
| 82 | } |
| 83 | |
| 84 | BOOST_AUTO_TEST_CASE(ErrorCommand) |
| 85 | { |
| 86 | this->processInterest = nullptr; // no response to command |
| 87 | |
| 88 | this->execute("cs config capacity 19956"); |
| 89 | BOOST_CHECK_EQUAL(exitCode, 1); |
| 90 | BOOST_CHECK(out.is_empty()); |
| 91 | BOOST_CHECK(err.is_equal("Error 10060 when updating CS config: request timed out\n")); |
| 92 | } |
| 93 | |
| 94 | BOOST_AUTO_TEST_SUITE_END() // ConfigCommand |
Junxiao Shi | 3160a3f | 2018-01-09 21:25:15 +0000 | [diff] [blame] | 95 | |
Junxiao Shi | a4d7fe0 | 2018-07-20 06:51:41 -0600 | [diff] [blame] | 96 | BOOST_FIXTURE_TEST_SUITE(EraseCommand, ExecuteCommandFixture) |
| 97 | |
Eric Newberry | 4995bf9 | 2020-04-18 16:59:19 -0700 | [diff] [blame] | 98 | BOOST_AUTO_TEST_CASE(WithoutCount) |
Junxiao Shi | a4d7fe0 | 2018-07-20 06:51:41 -0600 | [diff] [blame] | 99 | { |
Eric Newberry | 4995bf9 | 2020-04-18 16:59:19 -0700 | [diff] [blame] | 100 | size_t countRequests = 0; |
| 101 | this->processInterest = [this, &countRequests] (const Interest& interest) { |
Junxiao Shi | a4d7fe0 | 2018-07-20 06:51:41 -0600 | [diff] [blame] | 102 | ControlParameters req = MOCK_NFD_MGMT_REQUIRE_COMMAND_IS("/localhost/nfd/cs/erase"); |
| 103 | BOOST_REQUIRE(req.hasName()); |
| 104 | BOOST_CHECK_EQUAL(req.getName(), "/S2NrUoNJcQ"); |
| 105 | BOOST_CHECK(!req.hasCount()); |
| 106 | |
| 107 | ControlParameters resp; |
| 108 | resp.setName("/S2NrUoNJcQ"); |
Eric Newberry | 4995bf9 | 2020-04-18 16:59:19 -0700 | [diff] [blame] | 109 | |
| 110 | switch (countRequests) { |
| 111 | case 0: |
| 112 | resp.setCount(1000); |
| 113 | resp.setCapacity(1000); |
| 114 | break; |
| 115 | case 1: |
| 116 | resp.setCount(1000); |
| 117 | resp.setCapacity(1000); |
| 118 | break; |
| 119 | case 2: |
| 120 | resp.setCount(1000); |
| 121 | resp.setCapacity(1000); |
| 122 | break; |
| 123 | case 3: |
| 124 | resp.setCount(500); |
| 125 | break; |
| 126 | default: |
| 127 | BOOST_FAIL("Exceeded allowed number of erase requests"); |
| 128 | break; |
| 129 | } |
| 130 | countRequests++; |
Junxiao Shi | a4d7fe0 | 2018-07-20 06:51:41 -0600 | [diff] [blame] | 131 | this->succeedCommand(interest, resp); |
| 132 | }; |
| 133 | |
| 134 | this->execute("cs erase /S2NrUoNJcQ"); |
| 135 | BOOST_CHECK_EQUAL(exitCode, 0); |
Eric Newberry | 4995bf9 | 2020-04-18 16:59:19 -0700 | [diff] [blame] | 136 | BOOST_CHECK_EQUAL(countRequests, 4); |
| 137 | BOOST_CHECK(out.is_equal("cs-erased prefix=/S2NrUoNJcQ count=3500\n")); |
Junxiao Shi | a4d7fe0 | 2018-07-20 06:51:41 -0600 | [diff] [blame] | 138 | BOOST_CHECK(err.is_empty()); |
| 139 | } |
| 140 | |
| 141 | BOOST_AUTO_TEST_CASE(WithCount) |
| 142 | { |
Eric Newberry | 4995bf9 | 2020-04-18 16:59:19 -0700 | [diff] [blame] | 143 | size_t countRequests = 0; |
| 144 | this->processInterest = [this, &countRequests] (const Interest& interest) { |
Junxiao Shi | a4d7fe0 | 2018-07-20 06:51:41 -0600 | [diff] [blame] | 145 | ControlParameters req = MOCK_NFD_MGMT_REQUIRE_COMMAND_IS("/localhost/nfd/cs/erase"); |
| 146 | BOOST_REQUIRE(req.hasName()); |
| 147 | BOOST_CHECK_EQUAL(req.getName(), "/gr7ADmIq"); |
| 148 | BOOST_REQUIRE(req.hasCount()); |
Junxiao Shi | a4d7fe0 | 2018-07-20 06:51:41 -0600 | [diff] [blame] | 149 | |
| 150 | ControlParameters resp; |
| 151 | resp.setName("/gr7ADmIq"); |
Eric Newberry | 4995bf9 | 2020-04-18 16:59:19 -0700 | [diff] [blame] | 152 | |
| 153 | switch (countRequests) { |
| 154 | case 0: |
| 155 | BOOST_CHECK_EQUAL(req.getCount(), 3568); |
| 156 | resp.setCount(1000); |
| 157 | resp.setCapacity(1000); |
| 158 | break; |
| 159 | case 1: |
| 160 | BOOST_CHECK_EQUAL(req.getCount(), 2568); |
| 161 | resp.setCount(1000); |
| 162 | resp.setCapacity(1000); |
| 163 | break; |
| 164 | case 2: |
| 165 | BOOST_CHECK_EQUAL(req.getCount(), 1568); |
| 166 | resp.setCount(1000); |
| 167 | resp.setCapacity(1000); |
| 168 | break; |
| 169 | case 3: |
| 170 | BOOST_CHECK_EQUAL(req.getCount(), 568); |
| 171 | resp.setCount(568); |
| 172 | break; |
| 173 | default: |
| 174 | BOOST_FAIL("Exceeded allowed number of erase requests"); |
| 175 | break; |
| 176 | } |
| 177 | |
| 178 | countRequests++; |
Junxiao Shi | a4d7fe0 | 2018-07-20 06:51:41 -0600 | [diff] [blame] | 179 | this->succeedCommand(interest, resp); |
| 180 | }; |
| 181 | |
Eric Newberry | 4995bf9 | 2020-04-18 16:59:19 -0700 | [diff] [blame] | 182 | this->execute("cs erase /gr7ADmIq count 3568"); |
Junxiao Shi | a4d7fe0 | 2018-07-20 06:51:41 -0600 | [diff] [blame] | 183 | BOOST_CHECK_EQUAL(exitCode, 0); |
Eric Newberry | 4995bf9 | 2020-04-18 16:59:19 -0700 | [diff] [blame] | 184 | BOOST_CHECK_EQUAL(countRequests, 4); |
| 185 | BOOST_CHECK(out.is_equal("cs-erased prefix=/gr7ADmIq count=3568\n")); |
Junxiao Shi | a4d7fe0 | 2018-07-20 06:51:41 -0600 | [diff] [blame] | 186 | BOOST_CHECK(err.is_empty()); |
| 187 | } |
| 188 | |
Eric Newberry | 4995bf9 | 2020-04-18 16:59:19 -0700 | [diff] [blame] | 189 | BOOST_AUTO_TEST_CASE(WithCountError) |
Junxiao Shi | a4d7fe0 | 2018-07-20 06:51:41 -0600 | [diff] [blame] | 190 | { |
Eric Newberry | 4995bf9 | 2020-04-18 16:59:19 -0700 | [diff] [blame] | 191 | size_t countRequests = 0; |
| 192 | this->processInterest = [this, &countRequests] (const Interest& interest) { |
Junxiao Shi | a4d7fe0 | 2018-07-20 06:51:41 -0600 | [diff] [blame] | 193 | ControlParameters req = MOCK_NFD_MGMT_REQUIRE_COMMAND_IS("/localhost/nfd/cs/erase"); |
| 194 | BOOST_REQUIRE(req.hasName()); |
Eric Newberry | 4995bf9 | 2020-04-18 16:59:19 -0700 | [diff] [blame] | 195 | BOOST_CHECK_EQUAL(req.getName(), "/gr7ADmIq"); |
Junxiao Shi | a4d7fe0 | 2018-07-20 06:51:41 -0600 | [diff] [blame] | 196 | BOOST_REQUIRE(req.hasCount()); |
Junxiao Shi | a4d7fe0 | 2018-07-20 06:51:41 -0600 | [diff] [blame] | 197 | |
| 198 | ControlParameters resp; |
Eric Newberry | 4995bf9 | 2020-04-18 16:59:19 -0700 | [diff] [blame] | 199 | resp.setName("/gr7ADmIq"); |
| 200 | |
| 201 | switch (countRequests) { |
| 202 | case 0: |
| 203 | BOOST_CHECK_EQUAL(req.getCount(), 3568); |
| 204 | resp.setCount(1000); |
| 205 | resp.setCapacity(1000); |
| 206 | this->succeedCommand(interest, resp); |
| 207 | break; |
| 208 | case 1: |
| 209 | BOOST_CHECK_EQUAL(req.getCount(), 2568); |
| 210 | resp.setCount(1000); |
| 211 | resp.setCapacity(1000); |
| 212 | this->failCommand(interest, 500, "internal error"); |
| 213 | break; |
| 214 | default: |
| 215 | BOOST_FAIL("Exceeded allowed number of erase requests"); |
| 216 | break; |
| 217 | } |
| 218 | |
| 219 | countRequests++; |
Junxiao Shi | a4d7fe0 | 2018-07-20 06:51:41 -0600 | [diff] [blame] | 220 | }; |
| 221 | |
Eric Newberry | 4995bf9 | 2020-04-18 16:59:19 -0700 | [diff] [blame] | 222 | this->execute("cs erase /gr7ADmIq count 3568"); |
| 223 | BOOST_CHECK_EQUAL(exitCode, 1); |
| 224 | BOOST_CHECK_EQUAL(countRequests, 2); |
| 225 | BOOST_CHECK(out.is_empty()); |
| 226 | BOOST_CHECK(err.is_equal("Error 500 when erasing cached Data: internal error\n")); |
Junxiao Shi | a4d7fe0 | 2018-07-20 06:51:41 -0600 | [diff] [blame] | 227 | } |
| 228 | |
Eric Newberry | 4995bf9 | 2020-04-18 16:59:19 -0700 | [diff] [blame] | 229 | BOOST_AUTO_TEST_CASE(Timeout) |
Junxiao Shi | a4d7fe0 | 2018-07-20 06:51:41 -0600 | [diff] [blame] | 230 | { |
| 231 | this->processInterest = nullptr; // no response to command |
| 232 | |
| 233 | this->execute("cs erase /8Rq1Merv count 16519"); |
| 234 | BOOST_CHECK_EQUAL(exitCode, 1); |
| 235 | BOOST_CHECK(out.is_empty()); |
| 236 | BOOST_CHECK(err.is_equal("Error 10060 when erasing cached Data: request timed out\n")); |
| 237 | } |
| 238 | |
| 239 | BOOST_AUTO_TEST_SUITE_END() // EraseCommand |
| 240 | |
Junxiao Shi | 3160a3f | 2018-01-09 21:25:15 +0000 | [diff] [blame] | 241 | const std::string STATUS_XML = stripXmlSpaces(R"XML( |
| 242 | <cs> |
Junxiao Shi | 7a36ac7 | 2018-03-21 15:23:22 +0000 | [diff] [blame] | 243 | <capacity>31807</capacity> |
| 244 | <admitEnabled/> |
| 245 | <nEntries>16131</nEntries> |
Junxiao Shi | 3160a3f | 2018-01-09 21:25:15 +0000 | [diff] [blame] | 246 | <nHits>14363</nHits> |
| 247 | <nMisses>27462</nMisses> |
| 248 | </cs> |
| 249 | )XML"); |
| 250 | |
| 251 | const std::string STATUS_TEXT = std::string(R"TEXT( |
| 252 | CS information: |
Junxiao Shi | 7a36ac7 | 2018-03-21 15:23:22 +0000 | [diff] [blame] | 253 | capacity=31807 |
| 254 | admit=on |
| 255 | serve=off |
| 256 | nEntries=16131 |
| 257 | nHits=14363 |
| 258 | nMisses=27462 |
Junxiao Shi | 3160a3f | 2018-01-09 21:25:15 +0000 | [diff] [blame] | 259 | )TEXT").substr(1); |
| 260 | |
Junxiao Shi | cdf7845 | 2018-03-02 23:14:15 +0000 | [diff] [blame] | 261 | BOOST_FIXTURE_TEST_CASE(Status, StatusFixture<CsModule>) |
Junxiao Shi | 3160a3f | 2018-01-09 21:25:15 +0000 | [diff] [blame] | 262 | { |
| 263 | this->fetchStatus(); |
| 264 | CsInfo payload; |
Junxiao Shi | 7a36ac7 | 2018-03-21 15:23:22 +0000 | [diff] [blame] | 265 | payload.setCapacity(31807) |
| 266 | .setEnableAdmit(true) |
| 267 | .setEnableServe(false) |
| 268 | .setNEntries(16131) |
| 269 | .setNHits(14363) |
Junxiao Shi | 3160a3f | 2018-01-09 21:25:15 +0000 | [diff] [blame] | 270 | .setNMisses(27462); |
| 271 | this->sendDataset("/localhost/nfd/cs/info", payload); |
| 272 | this->prepareStatusOutput(); |
| 273 | |
| 274 | BOOST_CHECK(statusXml.is_equal(STATUS_XML)); |
| 275 | BOOST_CHECK(statusText.is_equal(STATUS_TEXT)); |
| 276 | } |
| 277 | |
| 278 | BOOST_AUTO_TEST_SUITE_END() // TestCsModule |
| 279 | BOOST_AUTO_TEST_SUITE_END() // Nfdc |
| 280 | |
| 281 | } // namespace tests |
| 282 | } // namespace nfdc |
| 283 | } // namespace tools |
| 284 | } // namespace nfd |