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