blob: 30b5853f59747fbbd0d9604fb2cca82ef37de29f [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 Pesaventofbea4fc2022-02-08 07:26:04 -05003 * Copyright (c) 2013-2022 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"
23#include "ndn-cxx/mgmt/nfd/control-parameters.hpp"
24#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
29namespace ndn {
30namespace mgmt {
31namespace tests {
32
33using namespace ndn::tests;
Davide Pesavento2e481fc2021-07-02 18:20:03 -040034using std::bind;
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070035
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050036class DispatcherFixture : public IoKeyChainFixture
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070037{
38public:
39 DispatcherFixture()
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050040 : face(m_io, m_keyChain, {true, true})
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080041 , dispatcher(face, m_keyChain, security::SigningInfo())
Yanbiao Li4b4f7542016-03-11 02:04:43 +080042 , storage(dispatcher.m_storage)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070043 {
44 }
45
46public:
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080047 util::DummyClientFace face;
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070048 mgmt::Dispatcher dispatcher;
Junxiao Shic542f632017-07-18 14:20:32 +000049 InMemoryStorageFifo& storage;
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070050};
51
52class VoidParameters : public mgmt::ControlParameters
53{
54public:
55 explicit
56 VoidParameters(const Block& wire)
57 {
58 wireDecode(wire);
59 }
60
Junxiao Shid97c9532017-04-27 16:17:04 +000061 Block
Davide Pesaventoaa82eb62016-04-22 19:08:40 +020062 wireEncode() const final
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070063 {
64 return Block(128);
65 }
66
Junxiao Shid97c9532017-04-27 16:17:04 +000067 void
Davide Pesaventoaa82eb62016-04-22 19:08:40 +020068 wireDecode(const Block& wire) final
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070069 {
70 if (wire.type() != 128)
Davide Pesavento923ba442019-02-12 22:00:38 -050071 NDN_THROW(tlv::Error("Expecting TLV type 128"));
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070072 }
73};
74
75static Authorization
76makeTestAuthorization()
77{
Davide Pesavento3c34ec12021-03-28 21:50:06 -040078 return [] (const Name&, const Interest& interest, const ControlParameters*,
79 AcceptContinuation accept, RejectContinuation reject) {
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070080 if (interest.getName()[-1] == name::Component("valid")) {
81 accept("");
82 }
83 else {
84 if (interest.getName()[-1] == name::Component("silent")) {
85 reject(RejectReply::SILENT);
86 }
87 else {
88 reject(RejectReply::STATUS403);
89 }
90 }
91 };
92}
93
Junxiao Shid97c9532017-04-27 16:17:04 +000094BOOST_AUTO_TEST_SUITE(Mgmt)
95BOOST_FIXTURE_TEST_SUITE(TestDispatcher, DispatcherFixture)
96
97BOOST_AUTO_TEST_CASE(Basic)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070098{
99 BOOST_CHECK_NO_THROW(dispatcher
100 .addControlCommand<VoidParameters>("test/1", makeAcceptAllAuthorization(),
101 bind([] { return true; }),
102 bind([]{})));
103 BOOST_CHECK_NO_THROW(dispatcher
104 .addControlCommand<VoidParameters>("test/2", makeAcceptAllAuthorization(),
105 bind([] { return true; }),
106 bind([]{})));
107
108 BOOST_CHECK_THROW(dispatcher
109 .addControlCommand<VoidParameters>("test", makeAcceptAllAuthorization(),
110 bind([] { return true; }),
111 bind([]{})),
112 std::out_of_range);
113
114 BOOST_CHECK_NO_THROW(dispatcher.addStatusDataset("status/1",
115 makeAcceptAllAuthorization(), bind([]{})));
116 BOOST_CHECK_NO_THROW(dispatcher.addStatusDataset("status/2",
117 makeAcceptAllAuthorization(), bind([]{})));
118 BOOST_CHECK_THROW(dispatcher.addStatusDataset("status",
119 makeAcceptAllAuthorization(), bind([]{})),
120 std::out_of_range);
121
122 BOOST_CHECK_NO_THROW(dispatcher.addNotificationStream("stream/1"));
123 BOOST_CHECK_NO_THROW(dispatcher.addNotificationStream("stream/2"));
124 BOOST_CHECK_THROW(dispatcher.addNotificationStream("stream"), std::out_of_range);
125
126
127 BOOST_CHECK_NO_THROW(dispatcher.addTopPrefix("/root/1"));
128 BOOST_CHECK_NO_THROW(dispatcher.addTopPrefix("/root/2"));
129 BOOST_CHECK_THROW(dispatcher.addTopPrefix("/root"), std::out_of_range);
130
131 BOOST_CHECK_THROW(dispatcher
132 .addControlCommand<VoidParameters>("test/3", makeAcceptAllAuthorization(),
133 bind([] { return true; }),
134 bind([]{})),
135 std::domain_error);
136
137 BOOST_CHECK_THROW(dispatcher.addStatusDataset("status/3",
138 makeAcceptAllAuthorization(), bind([]{})),
139 std::domain_error);
140
141 BOOST_CHECK_THROW(dispatcher.addNotificationStream("stream/3"), std::domain_error);
142}
143
Junxiao Shid97c9532017-04-27 16:17:04 +0000144BOOST_AUTO_TEST_CASE(AddRemoveTopPrefix)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700145{
146 std::map<std::string, size_t> nCallbackCalled;
147 dispatcher
148 .addControlCommand<VoidParameters>("test/1", makeAcceptAllAuthorization(),
149 bind([] { return true; }),
150 bind([&nCallbackCalled] { ++nCallbackCalled["test/1"]; }));
151
152 dispatcher
153 .addControlCommand<VoidParameters>("test/2", makeAcceptAllAuthorization(),
154 bind([] { return true; }),
155 bind([&nCallbackCalled] { ++nCallbackCalled["test/2"]; }));
156
Junxiao Shi85d90832016-08-04 03:19:46 +0000157 face.receive(*makeInterest("/root/1/test/1/%80%00"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500158 advanceClocks(1_ms);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700159 BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 0);
160 BOOST_CHECK_EQUAL(nCallbackCalled["test/2"], 0);
161
162 dispatcher.addTopPrefix("/root/1");
Davide Pesavento0f830802018-01-16 23:58:58 -0500163 advanceClocks(1_ms);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700164
Junxiao Shi85d90832016-08-04 03:19:46 +0000165 face.receive(*makeInterest("/root/1/test/1/%80%00"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500166 advanceClocks(1_ms);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700167 BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 1);
168 BOOST_CHECK_EQUAL(nCallbackCalled["test/2"], 0);
169
Junxiao Shi85d90832016-08-04 03:19:46 +0000170 face.receive(*makeInterest("/root/1/test/2/%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"], 1);
173 BOOST_CHECK_EQUAL(nCallbackCalled["test/2"], 1);
174
Junxiao Shi85d90832016-08-04 03:19:46 +0000175 face.receive(*makeInterest("/root/2/test/1/%80%00"));
176 face.receive(*makeInterest("/root/2/test/2/%80%00"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500177 advanceClocks(1_ms);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700178 BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 1);
179 BOOST_CHECK_EQUAL(nCallbackCalled["test/2"], 1);
180
181 dispatcher.addTopPrefix("/root/2");
Davide Pesavento0f830802018-01-16 23:58:58 -0500182 advanceClocks(1_ms);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700183
Junxiao Shi85d90832016-08-04 03:19:46 +0000184 face.receive(*makeInterest("/root/1/test/1/%80%00"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500185 advanceClocks(1_ms);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700186 BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 2);
187
Junxiao Shi85d90832016-08-04 03:19:46 +0000188 face.receive(*makeInterest("/root/2/test/1/%80%00"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500189 advanceClocks(1_ms);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700190 BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 3);
191
192 dispatcher.removeTopPrefix("/root/1");
Davide Pesavento0f830802018-01-16 23:58:58 -0500193 advanceClocks(1_ms);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700194
Junxiao Shi85d90832016-08-04 03:19:46 +0000195 face.receive(*makeInterest("/root/1/test/1/%80%00"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500196 advanceClocks(1_ms);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700197 BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 3);
198
Junxiao Shi85d90832016-08-04 03:19:46 +0000199 face.receive(*makeInterest("/root/2/test/1/%80%00"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500200 advanceClocks(1_ms);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700201 BOOST_CHECK_EQUAL(nCallbackCalled["test/1"], 4);
202}
203
Junxiao Shid97c9532017-04-27 16:17:04 +0000204BOOST_AUTO_TEST_CASE(ControlCommand)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700205{
206 size_t nCallbackCalled = 0;
207 dispatcher
208 .addControlCommand<VoidParameters>("test",
209 makeTestAuthorization(),
210 bind([] { return true; }),
211 bind([&nCallbackCalled] { ++nCallbackCalled; }));
212
213 dispatcher.addTopPrefix("/root");
Davide Pesavento0f830802018-01-16 23:58:58 -0500214 advanceClocks(1_ms);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800215 face.sentData.clear();
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700216
Junxiao Shi85d90832016-08-04 03:19:46 +0000217 face.receive(*makeInterest("/root/test/%80%00")); // returns 403
218 face.receive(*makeInterest("/root/test/%80%00/invalid")); // returns 403
219 face.receive(*makeInterest("/root/test/%80%00/silent")); // silently ignored
220 face.receive(*makeInterest("/root/test/.../invalid")); // silently ignored (wrong format)
221 face.receive(*makeInterest("/root/test/.../valid")); // silently ignored (wrong format)
Davide Pesavento0f830802018-01-16 23:58:58 -0500222 advanceClocks(1_ms, 20);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700223 BOOST_CHECK_EQUAL(nCallbackCalled, 0);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800224 BOOST_CHECK_EQUAL(face.sentData.size(), 2);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700225
Junxiao Shi72c0c642018-04-20 15:41:09 +0000226 BOOST_CHECK_EQUAL(face.sentData[0].getContentType(), tlv::ContentType_Blob);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800227 BOOST_CHECK_EQUAL(ControlResponse(face.sentData[0].getContent().blockFromValue()).getCode(), 403);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000228 BOOST_CHECK_EQUAL(face.sentData[1].getContentType(), tlv::ContentType_Blob);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800229 BOOST_CHECK_EQUAL(ControlResponse(face.sentData[1].getContent().blockFromValue()).getCode(), 403);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700230
Junxiao Shi85d90832016-08-04 03:19:46 +0000231 face.receive(*makeInterest("/root/test/%80%00/valid"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500232 advanceClocks(1_ms, 10);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700233 BOOST_CHECK_EQUAL(nCallbackCalled, 1);
234}
235
Junxiao Shid97c9532017-04-27 16:17:04 +0000236class StatefulParameters : public mgmt::ControlParameters
237{
238public:
239 explicit
240 StatefulParameters(const Block& wire)
241 {
242 wireDecode(wire);
243 }
244
245 Block
246 wireEncode() const final
247 {
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500248 return {};
Junxiao Shid97c9532017-04-27 16:17:04 +0000249 }
250
251 void
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400252 wireDecode(const Block&) final
Junxiao Shid97c9532017-04-27 16:17:04 +0000253 {
254 m_state = EXPECTED_STATE;
255 }
256
257 bool
258 check() const
259 {
260 return m_state == EXPECTED_STATE;
261 }
262
263private:
264 static constexpr int EXPECTED_STATE = 12602;
265 int m_state = 0;
266};
267
268BOOST_AUTO_TEST_CASE(ControlCommandAsyncAuthorization) // Bug 4059
269{
270 AcceptContinuation authorizationAccept;
271 auto authorization =
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400272 [&authorizationAccept] (const Name&, const Interest&, const ControlParameters*,
273 AcceptContinuation accept, RejectContinuation) {
274 authorizationAccept = std::move(accept);
Junxiao Shid97c9532017-04-27 16:17:04 +0000275 };
276
277 auto validateParameters =
278 [] (const ControlParameters& params) {
279 return dynamic_cast<const StatefulParameters&>(params).check();
280 };
281
282 size_t nCallbackCalled = 0;
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400283 dispatcher.addControlCommand<StatefulParameters>("test", authorization, validateParameters,
284 bind([&nCallbackCalled] { ++nCallbackCalled; }));
Junxiao Shid97c9532017-04-27 16:17:04 +0000285
286 dispatcher.addTopPrefix("/root");
Davide Pesavento0f830802018-01-16 23:58:58 -0500287 advanceClocks(1_ms);
Junxiao Shid97c9532017-04-27 16:17:04 +0000288
289 face.receive(*makeInterest("/root/test/%80%00"));
290 BOOST_CHECK_EQUAL(nCallbackCalled, 0);
291 BOOST_REQUIRE(authorizationAccept != nullptr);
292
Davide Pesavento0f830802018-01-16 23:58:58 -0500293 advanceClocks(1_ms);
Junxiao Shid97c9532017-04-27 16:17:04 +0000294 authorizationAccept("");
Davide Pesavento0f830802018-01-16 23:58:58 -0500295 advanceClocks(1_ms);
Junxiao Shid97c9532017-04-27 16:17:04 +0000296 BOOST_CHECK_EQUAL(nCallbackCalled, 1);
297}
298
299BOOST_AUTO_TEST_CASE(StatusDataset)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700300{
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500301 const Block smallBlock({0x81, 0x01, 0x01});
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400302 const Block largeBlock = [] {
303 Block b(129, std::make_shared<const Buffer>(3000));
304 b.encode();
305 return b;
306 }();
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700307
308 dispatcher.addStatusDataset("test/small",
309 makeTestAuthorization(),
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400310 [&smallBlock] (const Name&, const Interest&,
Davide Pesaventob10024c2017-09-22 01:36:44 -0400311 StatusDatasetContext& context) {
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700312 context.append(smallBlock);
313 context.append(smallBlock);
314 context.append(smallBlock);
315 context.end();
316 });
317
318 dispatcher.addStatusDataset("test/large",
319 makeTestAuthorization(),
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400320 [&largeBlock] (const Name&, const Interest&,
Davide Pesaventob10024c2017-09-22 01:36:44 -0400321 StatusDatasetContext& context) {
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700322 context.append(largeBlock);
323 context.append(largeBlock);
324 context.append(largeBlock);
325 context.end();
326 });
327
328 dispatcher.addStatusDataset("test/reject",
329 makeTestAuthorization(),
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400330 [] (const Name&, const Interest&, StatusDatasetContext& context) {
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700331 context.reject();
332 });
333
334 dispatcher.addTopPrefix("/root");
Davide Pesavento0f830802018-01-16 23:58:58 -0500335 advanceClocks(1_ms);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800336 face.sentData.clear();
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700337
Junxiao Shi85d90832016-08-04 03:19:46 +0000338 face.receive(*makeInterest("/root/test/small/%80%00")); // returns 403
339 face.receive(*makeInterest("/root/test/small/%80%00/invalid")); // returns 403
340 face.receive(*makeInterest("/root/test/small/%80%00/silent")); // silently ignored
Davide Pesavento0f830802018-01-16 23:58:58 -0500341 advanceClocks(1_ms, 20);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700342
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400343 BOOST_REQUIRE_EQUAL(face.sentData.size(), 2);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000344 BOOST_CHECK_EQUAL(face.sentData[0].getContentType(), tlv::ContentType_Blob);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800345 BOOST_CHECK_EQUAL(ControlResponse(face.sentData[0].getContent().blockFromValue()).getCode(), 403);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000346 BOOST_CHECK_EQUAL(face.sentData[1].getContentType(), tlv::ContentType_Blob);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800347 BOOST_CHECK_EQUAL(ControlResponse(face.sentData[1].getContent().blockFromValue()).getCode(), 403);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700348
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800349 face.sentData.clear();
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800350
Junxiao Shib55e5d32018-07-18 13:32:00 -0600351 auto interestSmall = *makeInterest("/root/test/small/valid", true);
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800352 face.receive(interestSmall);
Davide Pesavento0f830802018-01-16 23:58:58 -0500353 advanceClocks(1_ms, 10);
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800354
355 // one data packet is generated and sent to both places
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400356 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800357 BOOST_CHECK_EQUAL(storage.size(), 1);
358
359 auto fetchedData = storage.find(interestSmall);
360 BOOST_REQUIRE(fetchedData != nullptr);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000361 BOOST_CHECK_EQUAL(face.sentData[0].wireEncode(), fetchedData->wireEncode());
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700362
Junxiao Shi85d90832016-08-04 03:19:46 +0000363 face.receive(*makeInterest(Name("/root/test/small/valid").appendVersion(10))); // should be ignored
364 face.receive(*makeInterest(Name("/root/test/small/valid").appendSegment(20))); // should be ignored
Davide Pesavento0f830802018-01-16 23:58:58 -0500365 advanceClocks(1_ms, 10);
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400366 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800367 BOOST_CHECK_EQUAL(storage.size(), 1);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700368
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800369 Block content = face.sentData[0].getContent();
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700370 BOOST_CHECK_NO_THROW(content.parse());
371
Junxiao Shi72c0c642018-04-20 15:41:09 +0000372 BOOST_REQUIRE_EQUAL(content.elements().size(), 3);
373 BOOST_CHECK_EQUAL(content.elements()[0], smallBlock);
374 BOOST_CHECK_EQUAL(content.elements()[1], smallBlock);
375 BOOST_CHECK_EQUAL(content.elements()[2], smallBlock);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700376
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800377 storage.erase("/", true); // clear the storage
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800378 face.sentData.clear();
Junxiao Shi85d90832016-08-04 03:19:46 +0000379 face.receive(*makeInterest("/root/test/large/valid"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500380 advanceClocks(1_ms, 10);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700381
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800382 // two data packets are generated, the first one will be sent to both places
383 // while the second one will only be inserted into the in-memory storage
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400384 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
385 BOOST_REQUIRE_EQUAL(storage.size(), 2);
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800386
387 // segment0 should be sent through the face
388 const auto& component = face.sentData[0].getName().at(-1);
389 BOOST_CHECK(component.isSegment());
390 BOOST_CHECK_EQUAL(component.toSegment(), 0);
391
392 std::vector<Data> dataInStorage;
393 std::copy(storage.begin(), storage.end(), std::back_inserter(dataInStorage));
394
395 // the Data sent through the face should be the same as the first Data in the storage
396 BOOST_CHECK_EQUAL(face.sentData[0].getName(), dataInStorage[0].getName());
Junxiao Shi72c0c642018-04-20 15:41:09 +0000397 BOOST_CHECK_EQUAL(face.sentData[0].getContent(), dataInStorage[0].getContent());
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800398
399 content = [&dataInStorage] () -> Block {
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700400 EncodingBuffer encoder;
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500401 size_t valueLength = encoder.prependBytes({dataInStorage[1].getContent().value(),
402 dataInStorage[1].getContent().value_size()});
403 valueLength += encoder.prependBytes({dataInStorage[0].getContent().value(),
404 dataInStorage[0].getContent().value_size()});
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700405 encoder.prependVarNumber(valueLength);
406 encoder.prependVarNumber(tlv::Content);
407 return encoder.block();
408 }();
409
410 BOOST_CHECK_NO_THROW(content.parse());
Junxiao Shi72c0c642018-04-20 15:41:09 +0000411 BOOST_REQUIRE_EQUAL(content.elements().size(), 3);
412 BOOST_CHECK_EQUAL(content.elements()[0], largeBlock);
413 BOOST_CHECK_EQUAL(content.elements()[1], largeBlock);
414 BOOST_CHECK_EQUAL(content.elements()[2], largeBlock);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700415
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800416 storage.erase("/", true);// clear the storage
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800417 face.sentData.clear();
Junxiao Shi85d90832016-08-04 03:19:46 +0000418 face.receive(*makeInterest("/root/test/reject/%80%00/valid")); // returns nack
Davide Pesavento0f830802018-01-16 23:58:58 -0500419 advanceClocks(1_ms);
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400420
421 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000422 BOOST_CHECK_EQUAL(face.sentData[0].getContentType(), tlv::ContentType_Nack);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800423 BOOST_CHECK_EQUAL(ControlResponse(face.sentData[0].getContent().blockFromValue()).getCode(), 400);
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800424 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 -0700425}
426
Junxiao Shid97c9532017-04-27 16:17:04 +0000427BOOST_AUTO_TEST_CASE(NotificationStream)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700428{
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500429 const Block block({0x82, 0x01, 0x02});
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700430 auto post = dispatcher.addNotificationStream("test");
431
432 post(block);
Davide Pesavento0f830802018-01-16 23:58:58 -0500433 advanceClocks(1_ms);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800434 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700435
436 dispatcher.addTopPrefix("/root");
Davide Pesavento0f830802018-01-16 23:58:58 -0500437 advanceClocks(1_ms);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800438 face.sentData.clear();
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700439
440 post(block);
Davide Pesavento0f830802018-01-16 23:58:58 -0500441 advanceClocks(1_ms);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800442 BOOST_CHECK_EQUAL(face.sentData.size(), 1);
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800443 BOOST_CHECK_EQUAL(storage.size(), 1);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700444
445 post(block);
446 post(block);
447 post(block);
Davide Pesavento0f830802018-01-16 23:58:58 -0500448 advanceClocks(1_ms, 10);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700449
Junxiao Shi72c0c642018-04-20 15:41:09 +0000450 BOOST_REQUIRE_EQUAL(face.sentData.size(), 4);
Eric Newberryc25e4632021-02-11 10:48:11 -0800451 BOOST_CHECK_EQUAL(face.sentData[0].getName(), "/root/test/seq=0");
452 BOOST_CHECK_EQUAL(face.sentData[1].getName(), "/root/test/seq=1");
453 BOOST_CHECK_EQUAL(face.sentData[2].getName(), "/root/test/seq=2");
454 BOOST_CHECK_EQUAL(face.sentData[3].getName(), "/root/test/seq=3");
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700455
Junxiao Shi72c0c642018-04-20 15:41:09 +0000456 BOOST_CHECK_EQUAL(face.sentData[0].getContent().blockFromValue(), block);
457 BOOST_CHECK_EQUAL(face.sentData[1].getContent().blockFromValue(), block);
458 BOOST_CHECK_EQUAL(face.sentData[2].getContent().blockFromValue(), block);
459 BOOST_CHECK_EQUAL(face.sentData[3].getContent().blockFromValue(), block);
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800460
461 // each version of notification will be sent to both places
462 std::vector<Data> dataInStorage;
463 std::copy(storage.begin(), storage.end(), std::back_inserter(dataInStorage));
Junxiao Shi72c0c642018-04-20 15:41:09 +0000464 BOOST_REQUIRE_EQUAL(dataInStorage.size(), 4);
Eric Newberryc25e4632021-02-11 10:48:11 -0800465 BOOST_CHECK_EQUAL(dataInStorage[0].getName(), "/root/test/seq=0");
466 BOOST_CHECK_EQUAL(dataInStorage[1].getName(), "/root/test/seq=1");
467 BOOST_CHECK_EQUAL(dataInStorage[2].getName(), "/root/test/seq=2");
468 BOOST_CHECK_EQUAL(dataInStorage[3].getName(), "/root/test/seq=3");
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800469
Junxiao Shi72c0c642018-04-20 15:41:09 +0000470 BOOST_CHECK_EQUAL(dataInStorage[0].getContent().blockFromValue(), block);
471 BOOST_CHECK_EQUAL(dataInStorage[1].getContent().blockFromValue(), block);
472 BOOST_CHECK_EQUAL(dataInStorage[2].getContent().blockFromValue(), block);
473 BOOST_CHECK_EQUAL(dataInStorage[3].getContent().blockFromValue(), block);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700474}
475
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800476BOOST_AUTO_TEST_SUITE_END() // TestDispatcher
477BOOST_AUTO_TEST_SUITE_END() // Mgmt
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700478
479} // namespace tests
480} // namespace mgmt
481} // namespace ndn