blob: 69b2694efcd29340fecbcbf97632d0402bca0f16 [file] [log] [blame]
Yanbiao Li8ee37ed2015-05-19 12:44:04 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi2bea5c42017-08-14 20:10: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/status-dataset-context.hpp"
Davide Pesaventoeee3e822016-11-26 19:19:34 +010023
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050024#include "tests/test-common.hpp"
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070025
26namespace ndn {
27namespace mgmt {
28namespace tests {
29
Junxiao Shi85d90832016-08-04 03:19:46 +000030using namespace ndn::tests;
31
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070032class StatusDatasetContextFixture
33{
Davide Pesaventoeee3e822016-11-26 19:19:34 +010034private:
Yanbiao Li4b4f7542016-03-11 02:04:43 +080035 struct SendDataArgs
36 {
37 Name dataName;
38 Block content;
Yanbiao Li4b4f7542016-03-11 02:04:43 +080039 bool isFinalBlock;
40 };
41
Davide Pesaventoeee3e822016-11-26 19:19:34 +010042protected:
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070043 StatusDatasetContextFixture()
Junxiao Shi85d90832016-08-04 03:19:46 +000044 : interest(makeInterest("/test/context/interest"))
Yanbiao Li4b4f7542016-03-11 02:04:43 +080045 , context(*interest,
Davide Pesavento3c34ec12021-03-28 21:50:06 -040046 [this] (auto&&... args) {
47 sendDataHistory.push_back({std::forward<decltype(args)>(args)...});
Yanbiao Li4b4f7542016-03-11 02:04:43 +080048 },
Davide Pesavento3c34ec12021-03-28 21:50:06 -040049 [this] (const auto& resp) {
Yanbiao Li4b4f7542016-03-11 02:04:43 +080050 sendNackHistory.push_back(resp);
51 })
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070052 {
53 }
54
Yanbiao Li4b4f7542016-03-11 02:04:43 +080055 Name
Davide Pesaventoeee3e822016-11-26 19:19:34 +010056 makeSegmentName(size_t segmentNo) const
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070057 {
Yanbiao Li4b4f7542016-03-11 02:04:43 +080058 auto name = context.getPrefix();
59 return name.appendSegment(segmentNo);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070060 }
61
Davide Pesavento258d51a2022-02-27 21:26:28 -050062 ConstBufferPtr
Davide Pesaventoeee3e822016-11-26 19:19:34 +010063 concatenateDataContent() const
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070064 {
Davide Pesavento258d51a2022-02-27 21:26:28 -050065 auto buf = std::make_shared<Buffer>();
Davide Pesaventoeee3e822016-11-26 19:19:34 +010066 for (const auto& args : sendDataHistory) {
Davide Pesavento258d51a2022-02-27 21:26:28 -050067 buf->insert(buf->end(), args.content.value_begin(), args.content.value_end());
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070068 }
Davide Pesavento258d51a2022-02-27 21:26:28 -050069 return buf;
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070070 }
71
Davide Pesaventoeee3e822016-11-26 19:19:34 +010072protected:
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070073 shared_ptr<Interest> interest;
Davide Pesavento3c34ec12021-03-28 21:50:06 -040074 StatusDatasetContext context;
75 std::vector<SendDataArgs> sendDataHistory;
76 std::vector<ControlResponse> sendNackHistory;
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070077};
78
Yanbiao Li4b4f7542016-03-11 02:04:43 +080079BOOST_AUTO_TEST_SUITE(Mgmt)
80BOOST_FIXTURE_TEST_SUITE(TestStatusDatasetContext, StatusDatasetContextFixture)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070081
Davide Pesaventoeee3e822016-11-26 19:19:34 +010082BOOST_AUTO_TEST_SUITE(Prefix)
83
84BOOST_AUTO_TEST_CASE(Get)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070085{
86 Name dataName = context.getPrefix();
87 BOOST_CHECK(dataName[-1].isVersion());
Junxiao Shi72c0c642018-04-20 15:41:09 +000088 BOOST_CHECK_EQUAL(dataName.getPrefix(-1), interest->getName());
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070089}
90
Davide Pesaventoeee3e822016-11-26 19:19:34 +010091BOOST_AUTO_TEST_CASE(SetValid)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070092{
93 Name validPrefix = Name(interest->getName()).append("/valid");
94 BOOST_CHECK_NO_THROW(context.setPrefix(validPrefix));
Junxiao Shi72c0c642018-04-20 15:41:09 +000095 BOOST_CHECK_EQUAL(context.getPrefix().getPrefix(-1), validPrefix);
Davide Pesavento3c34ec12021-03-28 21:50:06 -040096 BOOST_CHECK(context.getPrefix()[-1].isVersion());
97
98 // trailing version component is preserved
99 validPrefix.appendVersion(42);
100 BOOST_CHECK_NO_THROW(context.setPrefix(validPrefix));
101 BOOST_CHECK_EQUAL(context.getPrefix(), validPrefix);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700102}
103
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100104BOOST_AUTO_TEST_CASE(SetInvalid)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700105{
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400106 // Interest name is not a prefix of invalidPrefix
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700107 Name invalidPrefix = Name(interest->getName()).getPrefix(-1).append("/invalid");
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400108 BOOST_CHECK_EXCEPTION(context.setPrefix(invalidPrefix), std::invalid_argument, [] (const auto& e) {
109 return e.what() == "prefix must start with the Interest's name"s;
110 });
111
112 // invalidPrefix contains a segment component
113 invalidPrefix = Name(interest->getName()).appendSegment(1);
114 BOOST_CHECK_EXCEPTION(context.setPrefix(invalidPrefix), std::invalid_argument, [] (const auto& e) {
115 return e.what() == "prefix must not contain a segment component"s;
116 });
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700117}
118
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400119BOOST_AUTO_TEST_CASE(SetValidAfterAppend)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700120{
121 Name validPrefix = Name(interest->getName()).append("/valid");
Davide Pesavento258d51a2022-02-27 21:26:28 -0500122 context.append({0x12, 0x34});
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400123 BOOST_CHECK_EXCEPTION(context.setPrefix(validPrefix), std::logic_error, [] (const auto& e) {
124 return e.what() == "cannot call setPrefix() after append/end/reject"s;
125 });
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700126}
127
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400128BOOST_AUTO_TEST_CASE(SetValidAfterEnd)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700129{
130 Name validPrefix = Name(interest->getName()).append("/valid");
131 context.end();
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400132 BOOST_CHECK_EXCEPTION(context.setPrefix(validPrefix), std::logic_error, [] (const auto& e) {
133 return e.what() == "cannot call setPrefix() after append/end/reject"s;
134 });
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700135}
136
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400137BOOST_AUTO_TEST_CASE(SetValidAfterReject)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700138{
139 Name validPrefix = Name(interest->getName()).append("/valid");
140 context.reject();
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400141 BOOST_CHECK_EXCEPTION(context.setPrefix(validPrefix), std::logic_error, [] (const auto& e) {
142 return e.what() == "cannot call setPrefix() after append/end/reject"s;
143 });
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700144}
145
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100146BOOST_AUTO_TEST_SUITE_END() // Prefix
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700147
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100148BOOST_AUTO_TEST_SUITE(Respond)
149
150BOOST_AUTO_TEST_CASE(Basic)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700151{
Davide Pesavento258d51a2022-02-27 21:26:28 -0500152 const auto testBlock = makeStringBlock(tlv::Content, "TEST");
153 context.append(testBlock);
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400154 BOOST_CHECK(sendDataHistory.empty()); // end() not called yet
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700155
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400156 context.end();
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800157 BOOST_REQUIRE_EQUAL(sendDataHistory.size(), 1);
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800158
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400159 const auto& args = sendDataHistory[0];
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800160 BOOST_CHECK_EQUAL(args.dataName, makeSegmentName(0));
Davide Pesavento258d51a2022-02-27 21:26:28 -0500161 BOOST_CHECK_EQUAL(args.content.blockFromValue(), testBlock);
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800162 BOOST_CHECK_EQUAL(args.isFinalBlock, true);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700163}
164
Davide Pesavento258d51a2022-02-27 21:26:28 -0500165BOOST_AUTO_TEST_CASE(Empty)
166{
167 context.append({});
168 BOOST_TEST(sendDataHistory.empty()); // end() not called yet
169
170 context.end();
171 BOOST_TEST_REQUIRE(sendDataHistory.size() == 1);
172
173 const auto& args = sendDataHistory[0];
174 BOOST_TEST(args.dataName == makeSegmentName(0));
175 BOOST_TEST(args.content.value_size() == 0);
176 BOOST_TEST(args.isFinalBlock);
177}
178
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100179BOOST_AUTO_TEST_CASE(Large)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700180{
Davide Pesavento258d51a2022-02-27 21:26:28 -0500181 const std::vector<uint8_t> big(10000, 'A');
182 context.append(big);
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400183 BOOST_CHECK_EQUAL(sendDataHistory.size(), 1);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700184
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400185 context.end();
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800186 BOOST_REQUIRE_EQUAL(sendDataHistory.size(), 2);
187
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400188 // check segment 0
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800189 BOOST_CHECK_EQUAL(sendDataHistory[0].dataName, makeSegmentName(0));
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800190 BOOST_CHECK_EQUAL(sendDataHistory[0].isFinalBlock, false);
191
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400192 // check segment 1
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800193 BOOST_CHECK_EQUAL(sendDataHistory[1].dataName, makeSegmentName(1));
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800194 BOOST_CHECK_EQUAL(sendDataHistory[1].isFinalBlock, true);
195
196 // check data content
Davide Pesavento258d51a2022-02-27 21:26:28 -0500197 BOOST_TEST(*concatenateDataContent() == big, boost::test_tools::per_element());
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700198}
199
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100200BOOST_AUTO_TEST_CASE(MultipleSmall)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700201{
Davide Pesavento258d51a2022-02-27 21:26:28 -0500202 const size_t nBlocks = 1000;
203 const auto contentBlock = makeStringBlock(0xFFFF, "Test Data Content");
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700204 for (size_t i = 0 ; i < nBlocks ; i ++) {
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400205 context.append(contentBlock);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700206 }
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400207 context.end();
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700208
Davide Pesavento258d51a2022-02-27 21:26:28 -0500209 BOOST_TEST_REQUIRE(sendDataHistory.size() == 3);
210 BOOST_TEST(sendDataHistory[0].dataName == makeSegmentName(0));
211 BOOST_TEST(!sendDataHistory[0].isFinalBlock);
212 BOOST_TEST(sendDataHistory[1].dataName == makeSegmentName(1));
213 BOOST_TEST(!sendDataHistory[1].isFinalBlock);
214 BOOST_TEST(sendDataHistory[2].dataName == makeSegmentName(2));
215 BOOST_TEST(sendDataHistory[2].isFinalBlock);
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800216
Davide Pesavento258d51a2022-02-27 21:26:28 -0500217 Block contentMultiBlocks(tlv::Content, concatenateDataContent());
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400218 contentMultiBlocks.parse();
Davide Pesavento258d51a2022-02-27 21:26:28 -0500219 BOOST_TEST(contentMultiBlocks.elements().size() == nBlocks);
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400220 for (const auto& element : contentMultiBlocks.elements()) {
Davide Pesavento258d51a2022-02-27 21:26:28 -0500221 BOOST_TEST(element == contentBlock, boost::test_tools::per_element());
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700222 }
223}
224
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100225BOOST_AUTO_TEST_SUITE_END() // Respond
226
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400227BOOST_AUTO_TEST_CASE(Reject)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700228{
229 BOOST_CHECK_NO_THROW(context.reject());
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800230 BOOST_REQUIRE_EQUAL(sendNackHistory.size(), 1);
231 BOOST_CHECK_EQUAL(sendNackHistory[0].getCode(), 400);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700232}
233
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800234class AbnormalStateTestFixture
235{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100236protected:
Davide Pesavento2e481fc2021-07-02 18:20:03 -0400237 StatusDatasetContext context{Interest("/abnormal-state"), [] (auto&&...) {}, [] (auto&&...) {}};
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800238};
239
240BOOST_FIXTURE_TEST_SUITE(AbnormalState, AbnormalStateTestFixture)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700241
242BOOST_AUTO_TEST_CASE(AppendReject)
243{
Davide Pesavento258d51a2022-02-27 21:26:28 -0500244 BOOST_CHECK_NO_THROW(context.append({0x82, 0x01, 0x02}));
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400245 BOOST_CHECK_EXCEPTION(context.reject(), std::logic_error, [] (const auto& e) {
246 return e.what() == "cannot call reject() after append/end"s;
247 });
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700248}
249
250BOOST_AUTO_TEST_CASE(AppendEndReject)
251{
Davide Pesavento258d51a2022-02-27 21:26:28 -0500252 BOOST_CHECK_NO_THROW(context.append({0x82, 0x01, 0x02}));
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700253 BOOST_CHECK_NO_THROW(context.end());
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400254 BOOST_CHECK_EXCEPTION(context.reject(), std::logic_error, [] (const auto& e) {
255 return e.what() == "cannot call reject() after append/end"s;
256 });
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700257}
258
259BOOST_AUTO_TEST_CASE(EndAppend)
260{
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700261 BOOST_CHECK_NO_THROW(context.end());
Davide Pesavento258d51a2022-02-27 21:26:28 -0500262 BOOST_CHECK_EXCEPTION(context.append({0x82, 0x01, 0x02}), std::logic_error, [] (const auto& e) {
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400263 return e.what() == "cannot call append() on a finalized context"s;
264 });
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700265}
266
267BOOST_AUTO_TEST_CASE(EndEnd)
268{
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700269 BOOST_CHECK_NO_THROW(context.end());
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400270 BOOST_CHECK_EXCEPTION(context.end(), std::logic_error, [] (const auto& e) {
271 return e.what() == "cannot call end() on a finalized context"s;
272 });
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700273}
274
275BOOST_AUTO_TEST_CASE(EndReject)
276{
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700277 BOOST_CHECK_NO_THROW(context.end());
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400278 BOOST_CHECK_EXCEPTION(context.reject(), std::logic_error, [] (const auto& e) {
279 return e.what() == "cannot call reject() after append/end"s;
280 });
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700281}
282
283BOOST_AUTO_TEST_CASE(RejectAppend)
284{
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700285 BOOST_CHECK_NO_THROW(context.reject());
Davide Pesavento258d51a2022-02-27 21:26:28 -0500286 BOOST_CHECK_EXCEPTION(context.append({0x82, 0x01, 0x02}), std::logic_error, [] (const auto& e) {
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400287 return e.what() == "cannot call append() on a finalized context"s;
288 });
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700289}
290
291BOOST_AUTO_TEST_CASE(RejectEnd)
292{
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700293 BOOST_CHECK_NO_THROW(context.reject());
Davide Pesavento3c34ec12021-03-28 21:50:06 -0400294 BOOST_CHECK_EXCEPTION(context.end(), std::logic_error, [] (const auto& e) {
295 return e.what() == "cannot call end() on a finalized context"s;
296 });
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700297}
298
299BOOST_AUTO_TEST_SUITE_END() // AbnormalState
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100300
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800301BOOST_AUTO_TEST_SUITE_END() // TestStatusDatasetContext
302BOOST_AUTO_TEST_SUITE_END() // Mgmt
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700303
304} // namespace tests
305} // namespace mgmt
306} // namespace ndn