blob: fa9b6c582e6cfec809d81f4389c42fe754b6ddcf [file] [log] [blame]
Junxiao Shif2bfb442018-01-05 12:34:57 +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 Shif2bfb442018-01-05 12:34:57 +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 "mgmt/cs-manager.hpp"
Davide Pesavento28181322018-11-08 16:44:50 -050027
Davide Pesavento78ddcab2019-02-28 22:00:03 -050028#include "manager-common-fixture.hpp"
Davide Pesavento28181322018-11-08 16:44:50 -050029
Junxiao Shif2bfb442018-01-05 12:34:57 +000030#include <ndn-cxx/mgmt/nfd/cs-info.hpp>
31
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040032namespace nfd::tests {
Junxiao Shif2bfb442018-01-05 12:34:57 +000033
Davide Pesavento78ddcab2019-02-28 22:00:03 -050034class CsManagerFixture : public ManagerFixtureWithAuthenticator
Junxiao Shif2bfb442018-01-05 12:34:57 +000035{
36public:
37 CsManagerFixture()
38 : m_cs(m_forwarder.getCs())
39 , m_fwCnt(const_cast<ForwarderCounters&>(m_forwarder.getCounters()))
40 , m_manager(m_cs, m_fwCnt, m_dispatcher, *m_authenticator)
41 {
42 setTopPrefix();
Junxiao Shic9b5e012018-02-07 15:04:18 +000043 setPrivilege("cs");
Junxiao Shif2bfb442018-01-05 12:34:57 +000044 }
45
46protected:
47 Cs& m_cs;
48 ForwarderCounters& m_fwCnt;
49 CsManager m_manager;
50};
51
52BOOST_AUTO_TEST_SUITE(Mgmt)
53BOOST_FIXTURE_TEST_SUITE(TestCsManager, CsManagerFixture)
54
Junxiao Shic9b5e012018-02-07 15:04:18 +000055BOOST_AUTO_TEST_CASE(Config)
56{
57 using ndn::nfd::CsFlagBit;
58 const Name cmdPrefix("/localhost/nfd/cs/config");
59
60 // setup initial CS config
61 m_cs.setLimit(22129);
62 m_cs.enableAdmit(false);
63 m_cs.enableServe(true);
64
65 // send empty cs/config command
Davide Pesavento20cafa82022-07-25 01:15:03 -040066 auto req = makeControlCommandRequest(cmdPrefix);
Junxiao Shic9b5e012018-02-07 15:04:18 +000067 receiveInterest(req);
68
69 // response shall reflect current config
70 ControlParameters body;
71 body.setCapacity(22129);
72 body.setFlagBit(CsFlagBit::BIT_CS_ENABLE_ADMIT, false, false);
73 body.setFlagBit(CsFlagBit::BIT_CS_ENABLE_SERVE, true, false);
74 BOOST_CHECK_EQUAL(checkResponse(0, req.getName(),
75 ControlResponse(200, "OK").setBody(body.wireEncode())),
76 CheckResponseResult::OK);
77
78 // send filled cs/config command
79 ControlParameters parameters;
80 parameters.setCapacity(18609);
81 parameters.setFlagBit(CsFlagBit::BIT_CS_ENABLE_ADMIT, true);
82 parameters.setFlagBit(CsFlagBit::BIT_CS_ENABLE_SERVE, false);
83 req = makeControlCommandRequest(cmdPrefix, parameters);
84 receiveInterest(req);
85
86 // response shall reflect updated config
87 body.setCapacity(18609);
88 body.setFlagBit(CsFlagBit::BIT_CS_ENABLE_ADMIT, true, false);
89 body.setFlagBit(CsFlagBit::BIT_CS_ENABLE_SERVE, false, false);
90 BOOST_CHECK_EQUAL(checkResponse(1, req.getName(),
91 ControlResponse(200, "OK").setBody(body.wireEncode())),
92 CheckResponseResult::OK);
93
94 // CS shall have updated config
95 BOOST_CHECK_EQUAL(m_cs.getLimit(), 18609);
96 BOOST_CHECK_EQUAL(m_cs.shouldAdmit(), true);
97 BOOST_CHECK_EQUAL(m_cs.shouldServe(), false);
98}
99
Junxiao Shibd724312018-04-17 02:23:48 +0000100BOOST_AUTO_TEST_CASE(Erase)
101{
102 m_cs.setLimit(CsManager::ERASE_LIMIT * 5);
103 m_cs.insert(*makeData("/B/C/1"));
104 m_cs.insert(*makeData("/B/C/2"));
105 m_cs.insert(*makeData("/B/D/3"));
106 m_cs.insert(*makeData("/B/D/4"));
107 for (size_t i = 0; i < CsManager::ERASE_LIMIT - 1; ++i) {
108 m_cs.insert(*makeData(Name("/E").appendSequenceNumber(i)));
109 }
110 for (size_t i = 0; i < CsManager::ERASE_LIMIT; ++i) {
111 m_cs.insert(*makeData(Name("/F").appendSequenceNumber(i)));
112 }
113 for (size_t i = 0; i < CsManager::ERASE_LIMIT + 1; ++i) {
114 m_cs.insert(*makeData(Name("/G").appendSequenceNumber(i)));
115 }
116 for (size_t i = 0; i < CsManager::ERASE_LIMIT + 1; ++i) {
117 m_cs.insert(*makeData(Name("/H").appendSequenceNumber(i)));
118 }
119 const Name cmdPrefix("/localhost/nfd/cs/erase");
120
121 // requested Name matches no Data
122 auto req = makeControlCommandRequest(cmdPrefix,
123 ControlParameters().setName("/A").setCount(1));
124 receiveInterest(req);
125
126 // response should include zero as actual Count
127 ControlParameters body;
128 body.setName("/A");
129 body.setCount(0);
130 BOOST_CHECK_EQUAL(checkResponse(0, req.getName(),
131 ControlResponse(200, "OK").setBody(body.wireEncode())),
132 CheckResponseResult::OK);
133
134 // requested Count is less than erase limit
135 req = makeControlCommandRequest(cmdPrefix,
136 ControlParameters().setName("/B").setCount(3));
137 receiveInterest(req);
138
139 // response should include actual Count and omit Capacity
140 body.setName("/B");
141 body.setCount(3);
142 BOOST_CHECK_EQUAL(checkResponse(1, req.getName(),
143 ControlResponse(200, "OK").setBody(body.wireEncode())),
144 CheckResponseResult::OK);
145
146 // requested Count equals erase limit
147 req = makeControlCommandRequest(cmdPrefix,
148 ControlParameters().setName("/E").setCount(CsManager::ERASE_LIMIT));
149 receiveInterest(req);
150
151 // response should include actual Count and omit Capacity
152 body.setName("/E");
153 body.setCount(CsManager::ERASE_LIMIT - 1);
154 BOOST_CHECK_EQUAL(checkResponse(2, req.getName(),
155 ControlResponse(200, "OK").setBody(body.wireEncode())),
156 CheckResponseResult::OK);
157
158 // requested Count exceeds erase limit, but there are no more Data
159 req = makeControlCommandRequest(cmdPrefix,
160 ControlParameters().setName("/F").setCount(CsManager::ERASE_LIMIT + 1));
161 receiveInterest(req);
162
163 // response should include actual Count and omit Capacity
164 body.setName("/F");
165 body.setCount(CsManager::ERASE_LIMIT);
166 BOOST_CHECK_EQUAL(checkResponse(3, req.getName(),
167 ControlResponse(200, "OK").setBody(body.wireEncode())),
168 CheckResponseResult::OK);
169
170 // requested Count exceeds erase limit, and there are more Data
171 req = makeControlCommandRequest(cmdPrefix,
172 ControlParameters().setName("/G").setCount(CsManager::ERASE_LIMIT + 1));
173 receiveInterest(req);
174
175 // response should include both actual Count and Capacity
176 body.setName("/G");
177 body.setCount(CsManager::ERASE_LIMIT);
178 body.setCapacity(CsManager::ERASE_LIMIT);
179 BOOST_CHECK_EQUAL(checkResponse(4, req.getName(),
180 ControlResponse(200, "OK").setBody(body.wireEncode())),
181 CheckResponseResult::OK);
182
183 // request omit Count, which implies "no limit" aka exceeds erase limit
184 req = makeControlCommandRequest(cmdPrefix,
185 ControlParameters().setName("/H"));
186 receiveInterest(req);
187
188 // response should include both actual Count and Capacity since there are more Data
189 body.setName("/H");
190 body.setCount(CsManager::ERASE_LIMIT);
191 body.setCapacity(CsManager::ERASE_LIMIT);
192 BOOST_CHECK_EQUAL(checkResponse(5, req.getName(),
193 ControlResponse(200, "OK").setBody(body.wireEncode())),
194 CheckResponseResult::OK);
195
196 // one Data each under /A, /G, /H remain, all other Data are erased
197 BOOST_CHECK_EQUAL(m_cs.size(), 3);
198}
199
Junxiao Shif2bfb442018-01-05 12:34:57 +0000200BOOST_AUTO_TEST_CASE(Info)
201{
Junxiao Shi26667e12018-02-08 20:01:18 +0000202 m_cs.setLimit(2681);
Davide Pesavento28181322018-11-08 16:44:50 -0500203 for (uint64_t i = 0; i < 310; ++i) {
Junxiao Shi26667e12018-02-08 20:01:18 +0000204 m_cs.insert(*makeData(Name("/Q8H4oi4g").appendSequenceNumber(i)));
205 }
206 m_cs.enableAdmit(false);
207 m_cs.enableServe(true);
Junxiao Shif2bfb442018-01-05 12:34:57 +0000208 m_fwCnt.nCsHits.set(362);
209 m_fwCnt.nCsMisses.set(1493);
210
Junxiao Shi9d727852019-05-14 13:44:22 -0600211 receiveInterest(*makeInterest("/localhost/nfd/cs/info", true));
Junxiao Shif2bfb442018-01-05 12:34:57 +0000212 Block dataset = concatenateResponses();
213 dataset.parse();
214 BOOST_REQUIRE_EQUAL(dataset.elements_size(), 1);
215
216 ndn::nfd::CsInfo info(*dataset.elements_begin());
Junxiao Shi26667e12018-02-08 20:01:18 +0000217 BOOST_CHECK_EQUAL(info.getCapacity(), 2681);
218 BOOST_CHECK_EQUAL(info.getEnableAdmit(), false);
219 BOOST_CHECK_EQUAL(info.getEnableServe(), true);
220 BOOST_CHECK_EQUAL(info.getNEntries(), 310);
Junxiao Shif2bfb442018-01-05 12:34:57 +0000221 BOOST_CHECK_EQUAL(info.getNHits(), 362);
222 BOOST_CHECK_EQUAL(info.getNMisses(), 1493);
223}
224
225BOOST_AUTO_TEST_SUITE_END() // TestCsManager
226BOOST_AUTO_TEST_SUITE_END() // Mgmt
227
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400228} // namespace nfd::tests