blob: 9b1ba0af542b9b3e5ea9867503af294b17ec7e8a [file] [log] [blame]
Junxiao Shi3160a3f2018-01-09 21:25:15 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Eric Newberry4995bf92020-04-18 16:59:19 -07003 * Copyright (c) 2014-2020, Regents of the University of California,
Junxiao Shi3160a3f2018-01-09 21:25:15 +00004 * 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 Shicdf78452018-03-02 23:14:15 +000029#include "execute-command-fixture.hpp"
Junxiao Shi3160a3f2018-01-09 21:25:15 +000030
31namespace nfd {
32namespace tools {
33namespace nfdc {
34namespace tests {
35
36BOOST_AUTO_TEST_SUITE(Nfdc)
Junxiao Shicdf78452018-03-02 23:14:15 +000037BOOST_AUTO_TEST_SUITE(TestCsModule)
38
39BOOST_FIXTURE_TEST_SUITE(ConfigCommand, ExecuteCommandFixture)
40
41BOOST_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
63BOOST_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
84BOOST_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
94BOOST_AUTO_TEST_SUITE_END() // ConfigCommand
Junxiao Shi3160a3f2018-01-09 21:25:15 +000095
Junxiao Shia4d7fe02018-07-20 06:51:41 -060096BOOST_FIXTURE_TEST_SUITE(EraseCommand, ExecuteCommandFixture)
97
Eric Newberry4995bf92020-04-18 16:59:19 -070098BOOST_AUTO_TEST_CASE(WithoutCount)
Junxiao Shia4d7fe02018-07-20 06:51:41 -060099{
Eric Newberry4995bf92020-04-18 16:59:19 -0700100 size_t countRequests = 0;
101 this->processInterest = [this, &countRequests] (const Interest& interest) {
Junxiao Shia4d7fe02018-07-20 06:51:41 -0600102 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 Newberry4995bf92020-04-18 16:59:19 -0700109
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 Shia4d7fe02018-07-20 06:51:41 -0600131 this->succeedCommand(interest, resp);
132 };
133
134 this->execute("cs erase /S2NrUoNJcQ");
135 BOOST_CHECK_EQUAL(exitCode, 0);
Eric Newberry4995bf92020-04-18 16:59:19 -0700136 BOOST_CHECK_EQUAL(countRequests, 4);
137 BOOST_CHECK(out.is_equal("cs-erased prefix=/S2NrUoNJcQ count=3500\n"));
Junxiao Shia4d7fe02018-07-20 06:51:41 -0600138 BOOST_CHECK(err.is_empty());
139}
140
141BOOST_AUTO_TEST_CASE(WithCount)
142{
Eric Newberry4995bf92020-04-18 16:59:19 -0700143 size_t countRequests = 0;
144 this->processInterest = [this, &countRequests] (const Interest& interest) {
Junxiao Shia4d7fe02018-07-20 06:51:41 -0600145 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 Shia4d7fe02018-07-20 06:51:41 -0600149
150 ControlParameters resp;
151 resp.setName("/gr7ADmIq");
Eric Newberry4995bf92020-04-18 16:59:19 -0700152
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 Shia4d7fe02018-07-20 06:51:41 -0600179 this->succeedCommand(interest, resp);
180 };
181
Eric Newberry4995bf92020-04-18 16:59:19 -0700182 this->execute("cs erase /gr7ADmIq count 3568");
Junxiao Shia4d7fe02018-07-20 06:51:41 -0600183 BOOST_CHECK_EQUAL(exitCode, 0);
Eric Newberry4995bf92020-04-18 16:59:19 -0700184 BOOST_CHECK_EQUAL(countRequests, 4);
185 BOOST_CHECK(out.is_equal("cs-erased prefix=/gr7ADmIq count=3568\n"));
Junxiao Shia4d7fe02018-07-20 06:51:41 -0600186 BOOST_CHECK(err.is_empty());
187}
188
Eric Newberry4995bf92020-04-18 16:59:19 -0700189BOOST_AUTO_TEST_CASE(WithCountError)
Junxiao Shia4d7fe02018-07-20 06:51:41 -0600190{
Eric Newberry4995bf92020-04-18 16:59:19 -0700191 size_t countRequests = 0;
192 this->processInterest = [this, &countRequests] (const Interest& interest) {
Junxiao Shia4d7fe02018-07-20 06:51:41 -0600193 ControlParameters req = MOCK_NFD_MGMT_REQUIRE_COMMAND_IS("/localhost/nfd/cs/erase");
194 BOOST_REQUIRE(req.hasName());
Eric Newberry4995bf92020-04-18 16:59:19 -0700195 BOOST_CHECK_EQUAL(req.getName(), "/gr7ADmIq");
Junxiao Shia4d7fe02018-07-20 06:51:41 -0600196 BOOST_REQUIRE(req.hasCount());
Junxiao Shia4d7fe02018-07-20 06:51:41 -0600197
198 ControlParameters resp;
Eric Newberry4995bf92020-04-18 16:59:19 -0700199 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 Shia4d7fe02018-07-20 06:51:41 -0600220 };
221
Eric Newberry4995bf92020-04-18 16:59:19 -0700222 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 Shia4d7fe02018-07-20 06:51:41 -0600227}
228
Eric Newberry4995bf92020-04-18 16:59:19 -0700229BOOST_AUTO_TEST_CASE(Timeout)
Junxiao Shia4d7fe02018-07-20 06:51:41 -0600230{
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
239BOOST_AUTO_TEST_SUITE_END() // EraseCommand
240
Junxiao Shi3160a3f2018-01-09 21:25:15 +0000241const std::string STATUS_XML = stripXmlSpaces(R"XML(
242 <cs>
Junxiao Shi7a36ac72018-03-21 15:23:22 +0000243 <capacity>31807</capacity>
244 <admitEnabled/>
245 <nEntries>16131</nEntries>
Junxiao Shi3160a3f2018-01-09 21:25:15 +0000246 <nHits>14363</nHits>
247 <nMisses>27462</nMisses>
248 </cs>
249)XML");
250
251const std::string STATUS_TEXT = std::string(R"TEXT(
252CS information:
Junxiao Shi7a36ac72018-03-21 15:23:22 +0000253 capacity=31807
254 admit=on
255 serve=off
256 nEntries=16131
257 nHits=14363
258 nMisses=27462
Junxiao Shi3160a3f2018-01-09 21:25:15 +0000259)TEXT").substr(1);
260
Junxiao Shicdf78452018-03-02 23:14:15 +0000261BOOST_FIXTURE_TEST_CASE(Status, StatusFixture<CsModule>)
Junxiao Shi3160a3f2018-01-09 21:25:15 +0000262{
263 this->fetchStatus();
264 CsInfo payload;
Junxiao Shi7a36ac72018-03-21 15:23:22 +0000265 payload.setCapacity(31807)
266 .setEnableAdmit(true)
267 .setEnableServe(false)
268 .setNEntries(16131)
269 .setNHits(14363)
Junxiao Shi3160a3f2018-01-09 21:25:15 +0000270 .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
278BOOST_AUTO_TEST_SUITE_END() // TestCsModule
279BOOST_AUTO_TEST_SUITE_END() // Nfdc
280
281} // namespace tests
282} // namespace nfdc
283} // namespace tools
284} // namespace nfd