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