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