blob: 8caedc2bfbb2a4129699ed95a1ac6c336d0ca10f [file] [log] [blame]
Yanbiao Li8ee37ed2015-05-19 12:44:04 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shic542f632017-07-18 14:20:32 +00002/*
Davide Pesaventocb385e32024-12-27 15:22:02 -05003 * Copyright (c) 2013-2025 Regents of the University of California.
Yanbiao Li8ee37ed2015-05-19 12:44:04 -07004 *
Alexander Afanasyev80b68e12015-09-17 17:01:04 -07005 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Yanbiao Li8ee37ed2015-05-19 12:44:04 -07006 *
Alexander Afanasyev80b68e12015-09-17 17:01:04 -07007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070010 *
Alexander Afanasyev80b68e12015-09-17 17:01:04 -070011 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070014 *
Alexander Afanasyev80b68e12015-09-17 17:01:04 -070015 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070020 */
21
Davide Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/mgmt/dispatcher.hpp"
Davide Pesaventocb385e32024-12-27 15:22:02 -050023#include "ndn-cxx/mgmt/nfd/control-command.hpp"
Davide Pesavento7e780642018-11-24 15:51:34 -050024#include "ndn-cxx/util/dummy-client-face.hpp"
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070025
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050026#include "tests/test-common.hpp"
27#include "tests/unit/io-key-chain-fixture.hpp"
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070028
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040029namespace ndn::tests {
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070030
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040031using namespace ndn::mgmt;
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070032
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050033class DispatcherFixture : public IoKeyChainFixture
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070034{
Davide Pesavento54dfc4a2024-12-26 17:30:41 -050035protected:
36 DummyClientFace face{m_io, m_keyChain, {true, true}};
37 Dispatcher dispatcher{face, m_keyChain};
38 InMemoryStorageFifo& storage{dispatcher.m_storage};
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070039};
40
Davide Pesavento0e13ed32024-12-28 15:01:25 -050041class VoidParameters : public ControlParametersBase
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070042{
43public:
44 explicit
45 VoidParameters(const Block& wire)
46 {
47 wireDecode(wire);
48 }
49
Junxiao Shid97c9532017-04-27 16:17:04 +000050 Block
Davide Pesaventoaa82eb62016-04-22 19:08:40 +020051 wireEncode() const final
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070052 {
53 return Block(128);
54 }
55
Junxiao Shid97c9532017-04-27 16:17:04 +000056 void
Davide Pesaventoaa82eb62016-04-22 19:08:40 +020057 wireDecode(const Block& wire) final
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070058 {
59 if (wire.type() != 128)
Davide Pesavento923ba442019-02-12 22:00:38 -050060 NDN_THROW(tlv::Error("Expecting TLV type 128"));
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070061 }
62};
63
64static Authorization
65makeTestAuthorization()
66{
Davide Pesavento0e13ed32024-12-28 15:01:25 -050067 return [] (const Name&, const Interest& interest, const ControlParametersBase*,
Davide Pesavento3c34ec12021-03-28 21:50:06 -040068 AcceptContinuation accept, RejectContinuation reject) {
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070069 if (interest.getName()[-1] == name::Component("valid")) {
70 accept("");
71 }
72 else {
73 if (interest.getName()[-1] == name::Component("silent")) {
74 reject(RejectReply::SILENT);
75 }
76 else {
77 reject(RejectReply::STATUS403);
78 }
79 }
80 };
81}
82
Junxiao Shid97c9532017-04-27 16:17:04 +000083BOOST_AUTO_TEST_SUITE(Mgmt)
84BOOST_FIXTURE_TEST_SUITE(TestDispatcher, DispatcherFixture)
85
86BOOST_AUTO_TEST_CASE(Basic)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070087{
Davide Pesaventocb385e32024-12-27 15:22:02 -050088 auto nop = [] (auto&&...) {};
89 auto tautology = [] (auto&&...) { return true; };
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070090
Davide Pesaventocb385e32024-12-27 15:22:02 -050091 BOOST_CHECK_NO_THROW(dispatcher
92 .addControlCommand<VoidParameters>("test/1", makeAcceptAllAuthorization(),
93 tautology, nop));
94 BOOST_CHECK_NO_THROW(dispatcher
95 .addControlCommand<VoidParameters>("test/2", makeAcceptAllAuthorization(),
96 tautology, nop));
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070097 BOOST_CHECK_THROW(dispatcher
Davide Pesaventocb385e32024-12-27 15:22:02 -050098 .addControlCommand<VoidParameters>("test", makeAcceptAllAuthorization(),
99 tautology, nop),
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700100 std::out_of_range);
101
Davide Pesaventocb385e32024-12-27 15:22:02 -0500102 BOOST_CHECK_NO_THROW(dispatcher.addStatusDataset("status/1", makeAcceptAllAuthorization(), nop));
103 BOOST_CHECK_NO_THROW(dispatcher.addStatusDataset("status/2", makeAcceptAllAuthorization(), nop));
104 BOOST_CHECK_THROW(dispatcher.addStatusDataset("status", makeAcceptAllAuthorization(), nop),
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700105 std::out_of_range);
106
107 BOOST_CHECK_NO_THROW(dispatcher.addNotificationStream("stream/1"));
108 BOOST_CHECK_NO_THROW(dispatcher.addNotificationStream("stream/2"));
109 BOOST_CHECK_THROW(dispatcher.addNotificationStream("stream"), std::out_of_range);
110
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700111 BOOST_CHECK_NO_THROW(dispatcher.addTopPrefix("/root/1"));
112 BOOST_CHECK_NO_THROW(dispatcher.addTopPrefix("/root/2"));
113 BOOST_CHECK_THROW(dispatcher.addTopPrefix("/root"), std::out_of_range);
114
115 BOOST_CHECK_THROW(dispatcher
Davide Pesaventocb385e32024-12-27 15:22:02 -0500116 .addControlCommand<VoidParameters>("test/3", makeAcceptAllAuthorization(),
117 tautology, nop),
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700118 std::domain_error);
119
Davide Pesaventocb385e32024-12-27 15:22:02 -0500120 BOOST_CHECK_THROW(dispatcher.addStatusDataset("status/3", makeAcceptAllAuthorization(), nop),
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700121 std::domain_error);
122
123 BOOST_CHECK_THROW(dispatcher.addNotificationStream("stream/3"), std::domain_error);
124}
125
Junxiao Shid97c9532017-04-27 16:17:04 +0000126BOOST_AUTO_TEST_CASE(AddRemoveTopPrefix)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700127{
128 std::map<std::string, size_t> nCallbackCalled;
129 dispatcher
130 .addControlCommand<VoidParameters>("test/1", makeAcceptAllAuthorization(),
Davide Pesaventocb385e32024-12-27 15:22:02 -0500131 [] (auto&&...) { return true; },
132 [&nCallbackCalled] (auto&&...) { ++nCallbackCalled["test/1"]; });
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700133
134 dispatcher
135 .addControlCommand<VoidParameters>("test/2", makeAcceptAllAuthorization(),
Davide Pesaventocb385e32024-12-27 15:22:02 -0500136 [] (auto&&...) { return true; },
137 [&nCallbackCalled] (auto&&...) { ++nCallbackCalled["test/2"]; });
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700138
Junxiao Shi85d90832016-08-04 03:19:46 +0000139 face.receive(*makeInterest("/root/1/test/1/%80%00"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500140 advanceClocks(1_ms);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700141 BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 0);
142 BOOST_CHECK_EQUAL(nCallbackCalled["test/2"], 0);
143
144 dispatcher.addTopPrefix("/root/1");
Davide Pesavento0f830802018-01-16 23:58:58 -0500145 advanceClocks(1_ms);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700146
Junxiao Shi85d90832016-08-04 03:19:46 +0000147 face.receive(*makeInterest("/root/1/test/1/%80%00"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500148 advanceClocks(1_ms);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700149 BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 1);
150 BOOST_CHECK_EQUAL(nCallbackCalled["test/2"], 0);
151
Junxiao Shi85d90832016-08-04 03:19:46 +0000152 face.receive(*makeInterest("/root/1/test/2/%80%00"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500153 advanceClocks(1_ms);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700154 BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 1);
155 BOOST_CHECK_EQUAL(nCallbackCalled["test/2"], 1);
156
Junxiao Shi85d90832016-08-04 03:19:46 +0000157 face.receive(*makeInterest("/root/2/test/1/%80%00"));
158 face.receive(*makeInterest("/root/2/test/2/%80%00"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500159 advanceClocks(1_ms);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700160 BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 1);
161 BOOST_CHECK_EQUAL(nCallbackCalled["test/2"], 1);
162
163 dispatcher.addTopPrefix("/root/2");
Davide Pesavento0f830802018-01-16 23:58:58 -0500164 advanceClocks(1_ms);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700165
Junxiao Shi85d90832016-08-04 03:19:46 +0000166 face.receive(*makeInterest("/root/1/test/1/%80%00"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500167 advanceClocks(1_ms);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700168 BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 2);
169
Junxiao Shi85d90832016-08-04 03:19:46 +0000170 face.receive(*makeInterest("/root/2/test/1/%80%00"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500171 advanceClocks(1_ms);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700172 BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 3);
173
174 dispatcher.removeTopPrefix("/root/1");
Davide Pesavento0f830802018-01-16 23:58:58 -0500175 advanceClocks(1_ms);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700176
Junxiao Shi85d90832016-08-04 03:19:46 +0000177 face.receive(*makeInterest("/root/1/test/1/%80%00"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500178 advanceClocks(1_ms);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700179 BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 3);
180
Junxiao Shi85d90832016-08-04 03:19:46 +0000181 face.receive(*makeInterest("/root/2/test/1/%80%00"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500182 advanceClocks(1_ms);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700183 BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 4);
184}
185
Davide Pesaventocb385e32024-12-27 15:22:02 -0500186BOOST_AUTO_TEST_CASE(ControlCommandOld,
187 * ut::description("test old-style ControlCommand registration"))
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700188{
189 size_t nCallbackCalled = 0;
Davide Pesaventocb385e32024-12-27 15:22:02 -0500190 auto handler = [&nCallbackCalled] (auto&&...) { ++nCallbackCalled; };
191 dispatcher.addControlCommand<VoidParameters>("test", makeTestAuthorization(),
192 [] (auto&&...) { return true; }, std::move(handler));
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700193
194 dispatcher.addTopPrefix("/root");
Davide Pesavento0f830802018-01-16 23:58:58 -0500195 advanceClocks(1_ms);
Davide Pesaventocb385e32024-12-27 15:22:02 -0500196 BOOST_REQUIRE_EQUAL(face.sentData.size(), 0);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700197
Junxiao Shi85d90832016-08-04 03:19:46 +0000198 face.receive(*makeInterest("/root/test/%80%00")); // returns 403
199 face.receive(*makeInterest("/root/test/%80%00/invalid")); // returns 403
200 face.receive(*makeInterest("/root/test/%80%00/silent")); // silently ignored
201 face.receive(*makeInterest("/root/test/.../invalid")); // silently ignored (wrong format)
202 face.receive(*makeInterest("/root/test/.../valid")); // silently ignored (wrong format)
Davide Pesavento0f830802018-01-16 23:58:58 -0500203 advanceClocks(1_ms, 20);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700204 BOOST_CHECK_EQUAL(nCallbackCalled, 0);
Davide Pesaventocb385e32024-12-27 15:22:02 -0500205 BOOST_REQUIRE_EQUAL(face.sentData.size(), 2);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700206
Junxiao Shi72c0c642018-04-20 15:41:09 +0000207 BOOST_CHECK_EQUAL(face.sentData[0].getContentType(), tlv::ContentType_Blob);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800208 BOOST_CHECK_EQUAL(ControlResponse(face.sentData[0].getContent().blockFromValue()).getCode(), 403);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000209 BOOST_CHECK_EQUAL(face.sentData[1].getContentType(), tlv::ContentType_Blob);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800210 BOOST_CHECK_EQUAL(ControlResponse(face.sentData[1].getContent().blockFromValue()).getCode(), 403);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700211
Junxiao Shi85d90832016-08-04 03:19:46 +0000212 face.receive(*makeInterest("/root/test/%80%00/valid"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500213 advanceClocks(1_ms, 10);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700214 BOOST_CHECK_EQUAL(nCallbackCalled, 1);
215}
216
Davide Pesaventocb385e32024-12-27 15:22:02 -0500217BOOST_AUTO_TEST_CASE(ControlCommandNew,
218 * ut::description("test new-style ControlCommand registration"))
219{
220 size_t nHandlerCalled = 0;
221 auto handler = [&nHandlerCalled] (auto&&...) { ++nHandlerCalled; };
222
223 // test addControlCommand()
224 dispatcher.addControlCommand<nfd::FaceCreateCommand>(makeAcceptAllAuthorization(), handler);
225 dispatcher.addControlCommand<nfd::FaceUpdateCommand>(makeAcceptAllAuthorization(), handler);
226 dispatcher.addControlCommand<nfd::FaceDestroyCommand>(makeAcceptAllAuthorization(), handler);
227 dispatcher.addControlCommand<nfd::FibAddNextHopCommand>(makeAcceptAllAuthorization(), handler);
228 dispatcher.addControlCommand<nfd::FibRemoveNextHopCommand>(makeAcceptAllAuthorization(), handler);
229 dispatcher.addControlCommand<nfd::CsConfigCommand>(makeAcceptAllAuthorization(), handler);
230 dispatcher.addControlCommand<nfd::CsEraseCommand>(makeAcceptAllAuthorization(), handler);
231 dispatcher.addControlCommand<nfd::StrategyChoiceSetCommand>(makeAcceptAllAuthorization(), handler);
232 dispatcher.addControlCommand<nfd::StrategyChoiceUnsetCommand>(makeAcceptAllAuthorization(), handler);
233 dispatcher.addControlCommand<nfd::RibRegisterCommand>(makeAcceptAllAuthorization(), handler);
234 dispatcher.addControlCommand<nfd::RibUnregisterCommand>(makeAcceptAllAuthorization(), handler);
235
236 BOOST_CHECK_THROW(dispatcher.addControlCommand<nfd::CsConfigCommand>(makeAcceptAllAuthorization(),
237 [] (auto&&...) {}),
238 std::out_of_range);
239
240 dispatcher.addTopPrefix("/root");
241 advanceClocks(1_ms);
242 BOOST_REQUIRE_EQUAL(face.sentData.size(), 0);
243
244 BOOST_CHECK_THROW(dispatcher.addControlCommand<nfd::CsConfigCommand>(makeAcceptAllAuthorization(),
245 [] (auto&&...) {}),
246 std::domain_error);
247
248 // we pick FaceDestroyCommand as an example for the following tests
249
250 // malformed request (missing ControlParameters) => silently ignored
251 auto baseName = Name("/root").append(nfd::FaceDestroyCommand::getName());
252 auto interest = makeInterest(baseName);
253 face.receive(*interest);
254 advanceClocks(1_ms);
255 BOOST_CHECK_EQUAL(nHandlerCalled, 0);
256 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
257
258 // ControlParameters present but invalid (missing required field) => reply with error 400
259 nfd::ControlParameters params;
260 interest->setName(Name(baseName).append(params.wireEncode()));
261 face.receive(*interest);
262 advanceClocks(1_ms);
263 BOOST_CHECK_EQUAL(nHandlerCalled, 0);
264 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
265 BOOST_CHECK_EQUAL(face.sentData[0].getContentType(), tlv::ContentType_Blob);
266 BOOST_CHECK_EQUAL(ControlResponse(face.sentData[0].getContent().blockFromValue()).getCode(), 400);
267
268 // valid request
269 params.setFaceId(42);
270 interest->setName(Name(baseName).append(params.wireEncode()));
271 face.receive(*interest);
272 advanceClocks(1_ms);
273 BOOST_CHECK_EQUAL(nHandlerCalled, 1);
274 BOOST_CHECK_EQUAL(face.sentData.size(), 1);
275}
276
277BOOST_AUTO_TEST_CASE(ControlCommandResponse)
278{
279 auto handler = [] (const Name& prefix, const Interest& interest,
Davide Pesavento0e13ed32024-12-28 15:01:25 -0500280 const ControlParametersBase&, const CommandContinuation& done) {
Davide Pesaventocb385e32024-12-27 15:22:02 -0500281 BOOST_CHECK_EQUAL(prefix, "/root");
282 BOOST_CHECK_EQUAL(interest.getName().getPrefix(3),
283 Name("/root").append(nfd::CsConfigCommand::getName()));
284 done(ControlResponse(42, "the answer"));
285 };
286
287 // use CsConfigCommand as an example
288 dispatcher.addControlCommand<nfd::CsConfigCommand>(makeAcceptAllAuthorization(), handler);
289 dispatcher.addTopPrefix("/root");
290 advanceClocks(1_ms);
291 BOOST_REQUIRE_EQUAL(face.sentData.size(), 0);
292
293 nfd::ControlParameters params;
294 auto interest = makeInterest(Name("/root")
295 .append(nfd::CsConfigCommand::getName())
296 .append(params.wireEncode()));
297 face.receive(*interest);
298 advanceClocks(1_ms, 10);
299
300 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
301 BOOST_CHECK_EQUAL(face.sentData[0].getContentType(), tlv::ContentType_Blob);
302 ControlResponse resp(face.sentData[0].getContent().blockFromValue());
303 BOOST_CHECK_EQUAL(resp.getCode(), 42);
304 BOOST_CHECK_EQUAL(resp.getText(), "the answer");
305}
306
Davide Pesavento0e13ed32024-12-28 15:01:25 -0500307class StatefulParameters : public ControlParametersBase
Junxiao Shid97c9532017-04-27 16:17:04 +0000308{
309public:
310 explicit
311 StatefulParameters(const Block& wire)
312 {
313 wireDecode(wire);
314 }
315
316 Block
317 wireEncode() const final
318 {
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500319 return {};
Junxiao Shid97c9532017-04-27 16:17:04 +0000320 }
321
322 void
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400323 wireDecode(const Block&) final
Junxiao Shid97c9532017-04-27 16:17:04 +0000324 {
325 m_state = EXPECTED_STATE;
326 }
327
328 bool
329 check() const
330 {
331 return m_state == EXPECTED_STATE;
332 }
333
334private:
335 static constexpr int EXPECTED_STATE = 12602;
336 int m_state = 0;
337};
338
Davide Pesavento0c526032024-01-31 21:14:01 -0500339BOOST_AUTO_TEST_CASE(ControlCommandAsyncAuthorization,
340 * ut::description("test for bug #4059"))
Junxiao Shid97c9532017-04-27 16:17:04 +0000341{
342 AcceptContinuation authorizationAccept;
Davide Pesavento0e13ed32024-12-28 15:01:25 -0500343 auto authorization = [&authorizationAccept] (const Name&, const Interest&, const ControlParametersBase*,
Davide Pesavento54dfc4a2024-12-26 17:30:41 -0500344 AcceptContinuation accept, RejectContinuation) {
345 authorizationAccept = std::move(accept);
346 };
Junxiao Shid97c9532017-04-27 16:17:04 +0000347
Davide Pesavento0e13ed32024-12-28 15:01:25 -0500348 auto validateParameters = [] (const ControlParametersBase& params) {
Davide Pesavento54dfc4a2024-12-26 17:30:41 -0500349 return dynamic_cast<const StatefulParameters&>(params).check();
350 };
Junxiao Shid97c9532017-04-27 16:17:04 +0000351
352 size_t nCallbackCalled = 0;
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400353 dispatcher.addControlCommand<StatefulParameters>("test", authorization, validateParameters,
Davide Pesaventocb385e32024-12-27 15:22:02 -0500354 [&nCallbackCalled] (auto&&...) { ++nCallbackCalled; });
Junxiao Shid97c9532017-04-27 16:17:04 +0000355
356 dispatcher.addTopPrefix("/root");
Davide Pesavento0f830802018-01-16 23:58:58 -0500357 advanceClocks(1_ms);
Junxiao Shid97c9532017-04-27 16:17:04 +0000358
359 face.receive(*makeInterest("/root/test/%80%00"));
360 BOOST_CHECK_EQUAL(nCallbackCalled, 0);
361 BOOST_REQUIRE(authorizationAccept != nullptr);
362
Davide Pesavento0f830802018-01-16 23:58:58 -0500363 advanceClocks(1_ms);
Junxiao Shid97c9532017-04-27 16:17:04 +0000364 authorizationAccept("");
Davide Pesavento0f830802018-01-16 23:58:58 -0500365 advanceClocks(1_ms);
Junxiao Shid97c9532017-04-27 16:17:04 +0000366 BOOST_CHECK_EQUAL(nCallbackCalled, 1);
367}
368
369BOOST_AUTO_TEST_CASE(StatusDataset)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700370{
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500371 const Block smallBlock({0x81, 0x01, 0x01});
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400372 const Block largeBlock = [] {
373 Block b(129, std::make_shared<const Buffer>(3000));
374 b.encode();
375 return b;
376 }();
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700377
378 dispatcher.addStatusDataset("test/small",
379 makeTestAuthorization(),
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400380 [&smallBlock] (const Name&, const Interest&,
Davide Pesaventob10024c2017-09-22 01:36:44 -0400381 StatusDatasetContext& context) {
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700382 context.append(smallBlock);
383 context.append(smallBlock);
384 context.append(smallBlock);
385 context.end();
386 });
387
388 dispatcher.addStatusDataset("test/large",
389 makeTestAuthorization(),
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400390 [&largeBlock] (const Name&, const Interest&,
Davide Pesaventob10024c2017-09-22 01:36:44 -0400391 StatusDatasetContext& context) {
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700392 context.append(largeBlock);
393 context.append(largeBlock);
394 context.append(largeBlock);
395 context.end();
396 });
397
398 dispatcher.addStatusDataset("test/reject",
399 makeTestAuthorization(),
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400400 [] (const Name&, const Interest&, StatusDatasetContext& context) {
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700401 context.reject();
402 });
403
404 dispatcher.addTopPrefix("/root");
Davide Pesavento0f830802018-01-16 23:58:58 -0500405 advanceClocks(1_ms);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800406 face.sentData.clear();
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700407
Junxiao Shi85d90832016-08-04 03:19:46 +0000408 face.receive(*makeInterest("/root/test/small/%80%00")); // returns 403
409 face.receive(*makeInterest("/root/test/small/%80%00/invalid")); // returns 403
410 face.receive(*makeInterest("/root/test/small/%80%00/silent")); // silently ignored
Davide Pesavento0f830802018-01-16 23:58:58 -0500411 advanceClocks(1_ms, 20);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700412
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400413 BOOST_REQUIRE_EQUAL(face.sentData.size(), 2);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000414 BOOST_CHECK_EQUAL(face.sentData[0].getContentType(), tlv::ContentType_Blob);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800415 BOOST_CHECK_EQUAL(ControlResponse(face.sentData[0].getContent().blockFromValue()).getCode(), 403);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000416 BOOST_CHECK_EQUAL(face.sentData[1].getContentType(), tlv::ContentType_Blob);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800417 BOOST_CHECK_EQUAL(ControlResponse(face.sentData[1].getContent().blockFromValue()).getCode(), 403);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700418
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800419 face.sentData.clear();
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800420
Junxiao Shib55e5d32018-07-18 13:32:00 -0600421 auto interestSmall = *makeInterest("/root/test/small/valid", true);
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800422 face.receive(interestSmall);
Davide Pesavento0f830802018-01-16 23:58:58 -0500423 advanceClocks(1_ms, 10);
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800424
425 // one data packet is generated and sent to both places
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400426 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800427 BOOST_CHECK_EQUAL(storage.size(), 1);
428
429 auto fetchedData = storage.find(interestSmall);
430 BOOST_REQUIRE(fetchedData != nullptr);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000431 BOOST_CHECK_EQUAL(face.sentData[0].wireEncode(), fetchedData->wireEncode());
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700432
Junxiao Shi85d90832016-08-04 03:19:46 +0000433 face.receive(*makeInterest(Name("/root/test/small/valid").appendVersion(10))); // should be ignored
434 face.receive(*makeInterest(Name("/root/test/small/valid").appendSegment(20))); // should be ignored
Davide Pesavento0f830802018-01-16 23:58:58 -0500435 advanceClocks(1_ms, 10);
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400436 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800437 BOOST_CHECK_EQUAL(storage.size(), 1);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700438
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800439 Block content = face.sentData[0].getContent();
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700440 BOOST_CHECK_NO_THROW(content.parse());
441
Junxiao Shi72c0c642018-04-20 15:41:09 +0000442 BOOST_REQUIRE_EQUAL(content.elements().size(), 3);
443 BOOST_CHECK_EQUAL(content.elements()[0], smallBlock);
444 BOOST_CHECK_EQUAL(content.elements()[1], smallBlock);
445 BOOST_CHECK_EQUAL(content.elements()[2], smallBlock);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700446
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800447 storage.erase("/", true); // clear the storage
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800448 face.sentData.clear();
Junxiao Shi85d90832016-08-04 03:19:46 +0000449 face.receive(*makeInterest("/root/test/large/valid"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500450 advanceClocks(1_ms, 10);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700451
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800452 // two data packets are generated, the first one will be sent to both places
453 // while the second one will only be inserted into the in-memory storage
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400454 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
455 BOOST_REQUIRE_EQUAL(storage.size(), 2);
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800456
457 // segment0 should be sent through the face
458 const auto& component = face.sentData[0].getName().at(-1);
459 BOOST_CHECK(component.isSegment());
460 BOOST_CHECK_EQUAL(component.toSegment(), 0);
461
462 std::vector<Data> dataInStorage;
463 std::copy(storage.begin(), storage.end(), std::back_inserter(dataInStorage));
464
465 // the Data sent through the face should be the same as the first Data in the storage
466 BOOST_CHECK_EQUAL(face.sentData[0].getName(), dataInStorage[0].getName());
Junxiao Shi72c0c642018-04-20 15:41:09 +0000467 BOOST_CHECK_EQUAL(face.sentData[0].getContent(), dataInStorage[0].getContent());
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800468
469 content = [&dataInStorage] () -> Block {
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700470 EncodingBuffer encoder;
Davide Pesavento258d51a2022-02-27 21:26:28 -0500471 size_t valueLength = encoder.prependBytes(dataInStorage[1].getContent().value_bytes());
472 valueLength += encoder.prependBytes(dataInStorage[0].getContent().value_bytes());
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700473 encoder.prependVarNumber(valueLength);
474 encoder.prependVarNumber(tlv::Content);
475 return encoder.block();
476 }();
477
478 BOOST_CHECK_NO_THROW(content.parse());
Junxiao Shi72c0c642018-04-20 15:41:09 +0000479 BOOST_REQUIRE_EQUAL(content.elements().size(), 3);
480 BOOST_CHECK_EQUAL(content.elements()[0], largeBlock);
481 BOOST_CHECK_EQUAL(content.elements()[1], largeBlock);
482 BOOST_CHECK_EQUAL(content.elements()[2], largeBlock);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700483
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800484 storage.erase("/", true);// clear the storage
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800485 face.sentData.clear();
Junxiao Shi85d90832016-08-04 03:19:46 +0000486 face.receive(*makeInterest("/root/test/reject/%80%00/valid")); // returns nack
Davide Pesavento0f830802018-01-16 23:58:58 -0500487 advanceClocks(1_ms);
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400488
489 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000490 BOOST_CHECK_EQUAL(face.sentData[0].getContentType(), tlv::ContentType_Nack);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800491 BOOST_CHECK_EQUAL(ControlResponse(face.sentData[0].getContent().blockFromValue()).getCode(), 400);
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800492 BOOST_CHECK_EQUAL(storage.size(), 0); // the nack packet will not be inserted into the in-memory storage
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700493}
494
Junxiao Shid97c9532017-04-27 16:17:04 +0000495BOOST_AUTO_TEST_CASE(NotificationStream)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700496{
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500497 const Block block({0x82, 0x01, 0x02});
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700498 auto post = dispatcher.addNotificationStream("test");
499
500 post(block);
Davide Pesavento0f830802018-01-16 23:58:58 -0500501 advanceClocks(1_ms);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800502 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700503
504 dispatcher.addTopPrefix("/root");
Davide Pesavento0f830802018-01-16 23:58:58 -0500505 advanceClocks(1_ms);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800506 face.sentData.clear();
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700507
508 post(block);
Davide Pesavento0f830802018-01-16 23:58:58 -0500509 advanceClocks(1_ms);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800510 BOOST_CHECK_EQUAL(face.sentData.size(), 1);
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800511 BOOST_CHECK_EQUAL(storage.size(), 1);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700512
513 post(block);
514 post(block);
515 post(block);
Davide Pesavento0f830802018-01-16 23:58:58 -0500516 advanceClocks(1_ms, 10);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700517
Junxiao Shi72c0c642018-04-20 15:41:09 +0000518 BOOST_REQUIRE_EQUAL(face.sentData.size(), 4);
Eric Newberryc25e4632021-02-11 10:48:11 -0800519 BOOST_CHECK_EQUAL(face.sentData[0].getName(), "/root/test/seq=0");
520 BOOST_CHECK_EQUAL(face.sentData[1].getName(), "/root/test/seq=1");
521 BOOST_CHECK_EQUAL(face.sentData[2].getName(), "/root/test/seq=2");
522 BOOST_CHECK_EQUAL(face.sentData[3].getName(), "/root/test/seq=3");
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700523
Junxiao Shi72c0c642018-04-20 15:41:09 +0000524 BOOST_CHECK_EQUAL(face.sentData[0].getContent().blockFromValue(), block);
525 BOOST_CHECK_EQUAL(face.sentData[1].getContent().blockFromValue(), block);
526 BOOST_CHECK_EQUAL(face.sentData[2].getContent().blockFromValue(), block);
527 BOOST_CHECK_EQUAL(face.sentData[3].getContent().blockFromValue(), block);
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800528
529 // each version of notification will be sent to both places
530 std::vector<Data> dataInStorage;
531 std::copy(storage.begin(), storage.end(), std::back_inserter(dataInStorage));
Junxiao Shi72c0c642018-04-20 15:41:09 +0000532 BOOST_REQUIRE_EQUAL(dataInStorage.size(), 4);
Eric Newberryc25e4632021-02-11 10:48:11 -0800533 BOOST_CHECK_EQUAL(dataInStorage[0].getName(), "/root/test/seq=0");
534 BOOST_CHECK_EQUAL(dataInStorage[1].getName(), "/root/test/seq=1");
535 BOOST_CHECK_EQUAL(dataInStorage[2].getName(), "/root/test/seq=2");
536 BOOST_CHECK_EQUAL(dataInStorage[3].getName(), "/root/test/seq=3");
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800537
Junxiao Shi72c0c642018-04-20 15:41:09 +0000538 BOOST_CHECK_EQUAL(dataInStorage[0].getContent().blockFromValue(), block);
539 BOOST_CHECK_EQUAL(dataInStorage[1].getContent().blockFromValue(), block);
540 BOOST_CHECK_EQUAL(dataInStorage[2].getContent().blockFromValue(), block);
541 BOOST_CHECK_EQUAL(dataInStorage[3].getContent().blockFromValue(), block);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700542}
543
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800544BOOST_AUTO_TEST_SUITE_END() // TestDispatcher
545BOOST_AUTO_TEST_SUITE_END() // Mgmt
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700546
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400547} // namespace ndn::tests