blob: ac046bddcf038c24bd71d9abd5d6775a5da10290 [file] [log] [blame]
Junxiao Shi3160a3f2018-01-09 21:25:15 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesaventoe422f9e2022-06-03 01:30:23 -04003 * Copyright (c) 2014-2022, 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
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040031namespace nfd::tools::nfdc::tests {
Junxiao Shi3160a3f2018-01-09 21:25:15 +000032
33BOOST_AUTO_TEST_SUITE(Nfdc)
Junxiao Shicdf78452018-03-02 23:14:15 +000034BOOST_AUTO_TEST_SUITE(TestCsModule)
35
36BOOST_FIXTURE_TEST_SUITE(ConfigCommand, ExecuteCommandFixture)
37
38BOOST_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
60BOOST_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
81BOOST_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
91BOOST_AUTO_TEST_SUITE_END() // ConfigCommand
Junxiao Shi3160a3f2018-01-09 21:25:15 +000092
Junxiao Shia4d7fe02018-07-20 06:51:41 -060093BOOST_FIXTURE_TEST_SUITE(EraseCommand, ExecuteCommandFixture)
94
Eric Newberry4995bf92020-04-18 16:59:19 -070095BOOST_AUTO_TEST_CASE(WithoutCount)
Junxiao Shia4d7fe02018-07-20 06:51:41 -060096{
Eric Newberry4995bf92020-04-18 16:59:19 -070097 size_t countRequests = 0;
98 this->processInterest = [this, &countRequests] (const Interest& interest) {
Junxiao Shia4d7fe02018-07-20 06:51:41 -060099 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 Newberry4995bf92020-04-18 16:59:19 -0700106
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 Shia4d7fe02018-07-20 06:51:41 -0600128 this->succeedCommand(interest, resp);
129 };
130
131 this->execute("cs erase /S2NrUoNJcQ");
132 BOOST_CHECK_EQUAL(exitCode, 0);
Eric Newberry4995bf92020-04-18 16:59:19 -0700133 BOOST_CHECK_EQUAL(countRequests, 4);
134 BOOST_CHECK(out.is_equal("cs-erased prefix=/S2NrUoNJcQ count=3500\n"));
Junxiao Shia4d7fe02018-07-20 06:51:41 -0600135 BOOST_CHECK(err.is_empty());
136}
137
138BOOST_AUTO_TEST_CASE(WithCount)
139{
Eric Newberry4995bf92020-04-18 16:59:19 -0700140 size_t countRequests = 0;
141 this->processInterest = [this, &countRequests] (const Interest& interest) {
Junxiao Shia4d7fe02018-07-20 06:51:41 -0600142 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 Shia4d7fe02018-07-20 06:51:41 -0600146
147 ControlParameters resp;
148 resp.setName("/gr7ADmIq");
Eric Newberry4995bf92020-04-18 16:59:19 -0700149
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 Shia4d7fe02018-07-20 06:51:41 -0600176 this->succeedCommand(interest, resp);
177 };
178
Eric Newberry4995bf92020-04-18 16:59:19 -0700179 this->execute("cs erase /gr7ADmIq count 3568");
Junxiao Shia4d7fe02018-07-20 06:51:41 -0600180 BOOST_CHECK_EQUAL(exitCode, 0);
Eric Newberry4995bf92020-04-18 16:59:19 -0700181 BOOST_CHECK_EQUAL(countRequests, 4);
182 BOOST_CHECK(out.is_equal("cs-erased prefix=/gr7ADmIq count=3568\n"));
Junxiao Shia4d7fe02018-07-20 06:51:41 -0600183 BOOST_CHECK(err.is_empty());
184}
185
Eric Newberry4995bf92020-04-18 16:59:19 -0700186BOOST_AUTO_TEST_CASE(WithCountError)
Junxiao Shia4d7fe02018-07-20 06:51:41 -0600187{
Eric Newberry4995bf92020-04-18 16:59:19 -0700188 size_t countRequests = 0;
189 this->processInterest = [this, &countRequests] (const Interest& interest) {
Junxiao Shia4d7fe02018-07-20 06:51:41 -0600190 ControlParameters req = MOCK_NFD_MGMT_REQUIRE_COMMAND_IS("/localhost/nfd/cs/erase");
191 BOOST_REQUIRE(req.hasName());
Eric Newberry4995bf92020-04-18 16:59:19 -0700192 BOOST_CHECK_EQUAL(req.getName(), "/gr7ADmIq");
Junxiao Shia4d7fe02018-07-20 06:51:41 -0600193 BOOST_REQUIRE(req.hasCount());
Junxiao Shia4d7fe02018-07-20 06:51:41 -0600194
195 ControlParameters resp;
Eric Newberry4995bf92020-04-18 16:59:19 -0700196 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 Shia4d7fe02018-07-20 06:51:41 -0600217 };
218
Eric Newberry4995bf92020-04-18 16:59:19 -0700219 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 Shia4d7fe02018-07-20 06:51:41 -0600224}
225
Eric Newberry4995bf92020-04-18 16:59:19 -0700226BOOST_AUTO_TEST_CASE(Timeout)
Junxiao Shia4d7fe02018-07-20 06:51:41 -0600227{
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
236BOOST_AUTO_TEST_SUITE_END() // EraseCommand
237
Junxiao Shi3160a3f2018-01-09 21:25:15 +0000238const std::string STATUS_XML = stripXmlSpaces(R"XML(
239 <cs>
Junxiao Shi7a36ac72018-03-21 15:23:22 +0000240 <capacity>31807</capacity>
241 <admitEnabled/>
242 <nEntries>16131</nEntries>
Junxiao Shi3160a3f2018-01-09 21:25:15 +0000243 <nHits>14363</nHits>
244 <nMisses>27462</nMisses>
245 </cs>
246)XML");
247
248const std::string STATUS_TEXT = std::string(R"TEXT(
249CS information:
Junxiao Shi7a36ac72018-03-21 15:23:22 +0000250 capacity=31807
251 admit=on
252 serve=off
253 nEntries=16131
254 nHits=14363
255 nMisses=27462
Junxiao Shi3160a3f2018-01-09 21:25:15 +0000256)TEXT").substr(1);
257
Junxiao Shicdf78452018-03-02 23:14:15 +0000258BOOST_FIXTURE_TEST_CASE(Status, StatusFixture<CsModule>)
Junxiao Shi3160a3f2018-01-09 21:25:15 +0000259{
260 this->fetchStatus();
261 CsInfo payload;
Junxiao Shi7a36ac72018-03-21 15:23:22 +0000262 payload.setCapacity(31807)
263 .setEnableAdmit(true)
264 .setEnableServe(false)
265 .setNEntries(16131)
266 .setNHits(14363)
Junxiao Shi3160a3f2018-01-09 21:25:15 +0000267 .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
275BOOST_AUTO_TEST_SUITE_END() // TestCsModule
276BOOST_AUTO_TEST_SUITE_END() // Nfdc
277
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400278} // namespace nfd::tools::nfdc::tests