Junxiao Shi | 3160a3f | 2018-01-09 21:25:15 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2014-2018, Regents of the University of California, |
| 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 | |
| 98 | BOOST_AUTO_TEST_CASE(NoCount) |
| 99 | { |
| 100 | this->processInterest = [this] (const Interest& interest) { |
| 101 | ControlParameters req = MOCK_NFD_MGMT_REQUIRE_COMMAND_IS("/localhost/nfd/cs/erase"); |
| 102 | BOOST_REQUIRE(req.hasName()); |
| 103 | BOOST_CHECK_EQUAL(req.getName(), "/S2NrUoNJcQ"); |
| 104 | BOOST_CHECK(!req.hasCount()); |
| 105 | |
| 106 | ControlParameters resp; |
| 107 | resp.setName("/S2NrUoNJcQ"); |
| 108 | resp.setCount(152); |
| 109 | this->succeedCommand(interest, resp); |
| 110 | }; |
| 111 | |
| 112 | this->execute("cs erase /S2NrUoNJcQ"); |
| 113 | BOOST_CHECK_EQUAL(exitCode, 0); |
| 114 | BOOST_CHECK(out.is_equal("cs-erased prefix=/S2NrUoNJcQ count=152 has-more=no\n")); |
| 115 | BOOST_CHECK(err.is_empty()); |
| 116 | } |
| 117 | |
| 118 | BOOST_AUTO_TEST_CASE(WithCount) |
| 119 | { |
| 120 | this->processInterest = [this] (const Interest& interest) { |
| 121 | ControlParameters req = MOCK_NFD_MGMT_REQUIRE_COMMAND_IS("/localhost/nfd/cs/erase"); |
| 122 | BOOST_REQUIRE(req.hasName()); |
| 123 | BOOST_CHECK_EQUAL(req.getName(), "/gr7ADmIq"); |
| 124 | BOOST_REQUIRE(req.hasCount()); |
| 125 | BOOST_CHECK_EQUAL(req.getCount(), 7568); |
| 126 | |
| 127 | ControlParameters resp; |
| 128 | resp.setName("/gr7ADmIq"); |
| 129 | resp.setCount(141); |
| 130 | this->succeedCommand(interest, resp); |
| 131 | }; |
| 132 | |
| 133 | this->execute("cs erase /gr7ADmIq count 7568"); |
| 134 | BOOST_CHECK_EQUAL(exitCode, 0); |
| 135 | BOOST_CHECK(out.is_equal("cs-erased prefix=/gr7ADmIq count=141 has-more=no\n")); |
| 136 | BOOST_CHECK(err.is_empty()); |
| 137 | } |
| 138 | |
| 139 | BOOST_AUTO_TEST_CASE(HasMore) |
| 140 | { |
| 141 | this->processInterest = [this] (const Interest& interest) { |
| 142 | ControlParameters req = MOCK_NFD_MGMT_REQUIRE_COMMAND_IS("/localhost/nfd/cs/erase"); |
| 143 | BOOST_REQUIRE(req.hasName()); |
| 144 | BOOST_CHECK_EQUAL(req.getName(), "/8Rq1Merv"); |
| 145 | BOOST_REQUIRE(req.hasCount()); |
| 146 | BOOST_CHECK_EQUAL(req.getCount(), 16519); |
| 147 | |
| 148 | ControlParameters resp; |
| 149 | resp.setName("/8Rq1Merv"); |
| 150 | resp.setCount(256); |
| 151 | resp.setCapacity(256); |
| 152 | this->succeedCommand(interest, resp); |
| 153 | }; |
| 154 | |
| 155 | this->execute("cs erase /8Rq1Merv count 16519"); |
| 156 | BOOST_CHECK_EQUAL(exitCode, 0); |
| 157 | BOOST_CHECK(out.is_equal("cs-erased prefix=/8Rq1Merv count=256 has-more=yes\n")); |
| 158 | BOOST_CHECK(err.is_empty()); |
| 159 | } |
| 160 | |
| 161 | BOOST_AUTO_TEST_CASE(ErrorCommand) |
| 162 | { |
| 163 | this->processInterest = nullptr; // no response to command |
| 164 | |
| 165 | this->execute("cs erase /8Rq1Merv count 16519"); |
| 166 | BOOST_CHECK_EQUAL(exitCode, 1); |
| 167 | BOOST_CHECK(out.is_empty()); |
| 168 | BOOST_CHECK(err.is_equal("Error 10060 when erasing cached Data: request timed out\n")); |
| 169 | } |
| 170 | |
| 171 | BOOST_AUTO_TEST_SUITE_END() // EraseCommand |
| 172 | |
Junxiao Shi | 3160a3f | 2018-01-09 21:25:15 +0000 | [diff] [blame] | 173 | const std::string STATUS_XML = stripXmlSpaces(R"XML( |
| 174 | <cs> |
Junxiao Shi | 7a36ac7 | 2018-03-21 15:23:22 +0000 | [diff] [blame] | 175 | <capacity>31807</capacity> |
| 176 | <admitEnabled/> |
| 177 | <nEntries>16131</nEntries> |
Junxiao Shi | 3160a3f | 2018-01-09 21:25:15 +0000 | [diff] [blame] | 178 | <nHits>14363</nHits> |
| 179 | <nMisses>27462</nMisses> |
| 180 | </cs> |
| 181 | )XML"); |
| 182 | |
| 183 | const std::string STATUS_TEXT = std::string(R"TEXT( |
| 184 | CS information: |
Junxiao Shi | 7a36ac7 | 2018-03-21 15:23:22 +0000 | [diff] [blame] | 185 | capacity=31807 |
| 186 | admit=on |
| 187 | serve=off |
| 188 | nEntries=16131 |
| 189 | nHits=14363 |
| 190 | nMisses=27462 |
Junxiao Shi | 3160a3f | 2018-01-09 21:25:15 +0000 | [diff] [blame] | 191 | )TEXT").substr(1); |
| 192 | |
Junxiao Shi | cdf7845 | 2018-03-02 23:14:15 +0000 | [diff] [blame] | 193 | BOOST_FIXTURE_TEST_CASE(Status, StatusFixture<CsModule>) |
Junxiao Shi | 3160a3f | 2018-01-09 21:25:15 +0000 | [diff] [blame] | 194 | { |
| 195 | this->fetchStatus(); |
| 196 | CsInfo payload; |
Junxiao Shi | 7a36ac7 | 2018-03-21 15:23:22 +0000 | [diff] [blame] | 197 | payload.setCapacity(31807) |
| 198 | .setEnableAdmit(true) |
| 199 | .setEnableServe(false) |
| 200 | .setNEntries(16131) |
| 201 | .setNHits(14363) |
Junxiao Shi | 3160a3f | 2018-01-09 21:25:15 +0000 | [diff] [blame] | 202 | .setNMisses(27462); |
| 203 | this->sendDataset("/localhost/nfd/cs/info", payload); |
| 204 | this->prepareStatusOutput(); |
| 205 | |
| 206 | BOOST_CHECK(statusXml.is_equal(STATUS_XML)); |
| 207 | BOOST_CHECK(statusText.is_equal(STATUS_TEXT)); |
| 208 | } |
| 209 | |
| 210 | BOOST_AUTO_TEST_SUITE_END() // TestCsModule |
| 211 | BOOST_AUTO_TEST_SUITE_END() // Nfdc |
| 212 | |
| 213 | } // namespace tests |
| 214 | } // namespace nfdc |
| 215 | } // namespace tools |
| 216 | } // namespace nfd |