blob: c46292c13f2f3c620bef5aa152c229d819b95000 [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 Pesavento4c1ad4c2020-11-16 21:12:02 -05003 * Copyright (c) 2013-2020 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;
39 time::milliseconds imsFresh;
40 bool isFinalBlock;
41 };
42
Davide Pesaventoeee3e822016-11-26 19:19:34 +010043protected:
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070044 StatusDatasetContextFixture()
Junxiao Shi85d90832016-08-04 03:19:46 +000045 : interest(makeInterest("/test/context/interest"))
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070046 , contentBlock(makeStringBlock(tlv::Content, "/test/data/content"))
Yanbiao Li4b4f7542016-03-11 02:04:43 +080047 , context(*interest,
48 [this] (const Name& dataName, const Block& content,
49 time::milliseconds imsFresh, bool isFinalBlock) {
50 SendDataArgs args{dataName, content, imsFresh, isFinalBlock};
51 sendDataHistory.push_back(args);
52 },
53 [this] (const ControlResponse& resp) {
54 sendNackHistory.push_back(resp);
55 })
Davide Pesavento0f830802018-01-16 23:58:58 -050056 , defaultImsFresh(1000_ms)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070057 {
58 }
59
Yanbiao Li4b4f7542016-03-11 02:04:43 +080060 Name
Davide Pesaventoeee3e822016-11-26 19:19:34 +010061 makeSegmentName(size_t segmentNo) const
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070062 {
Yanbiao Li4b4f7542016-03-11 02:04:43 +080063 auto name = context.getPrefix();
64 return name.appendSegment(segmentNo);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070065 }
66
67 Block
Davide Pesaventoeee3e822016-11-26 19:19:34 +010068 concatenateDataContent() const
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070069 {
70 EncodingBuffer encoder;
71 size_t valueLength = 0;
Davide Pesaventoeee3e822016-11-26 19:19:34 +010072 for (const auto& args : sendDataHistory) {
Yanbiao Li4b4f7542016-03-11 02:04:43 +080073 const auto& content = args.content;
74 valueLength += encoder.appendByteArray(content.value(), content.value_size());
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070075 }
76 encoder.prependVarNumber(valueLength);
77 encoder.prependVarNumber(tlv::Content);
78 return encoder.block();
79 }
80
Davide Pesaventoeee3e822016-11-26 19:19:34 +010081protected:
Yanbiao Li4b4f7542016-03-11 02:04:43 +080082 std::vector<SendDataArgs> sendDataHistory;
83 std::vector<ControlResponse> sendNackHistory;
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070084 shared_ptr<Interest> interest;
85 Block contentBlock;
86 mgmt::StatusDatasetContext context;
Yanbiao Li4b4f7542016-03-11 02:04:43 +080087 time::milliseconds defaultImsFresh;
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070088};
89
Yanbiao Li4b4f7542016-03-11 02:04:43 +080090BOOST_AUTO_TEST_SUITE(Mgmt)
91BOOST_FIXTURE_TEST_SUITE(TestStatusDatasetContext, StatusDatasetContextFixture)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070092
Davide Pesaventoeee3e822016-11-26 19:19:34 +010093BOOST_AUTO_TEST_SUITE(Prefix)
94
95BOOST_AUTO_TEST_CASE(Get)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070096{
97 Name dataName = context.getPrefix();
98 BOOST_CHECK(dataName[-1].isVersion());
Junxiao Shi72c0c642018-04-20 15:41:09 +000099 BOOST_CHECK_EQUAL(dataName.getPrefix(-1), interest->getName());
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700100}
101
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100102BOOST_AUTO_TEST_CASE(SetValid)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700103{
104 Name validPrefix = Name(interest->getName()).append("/valid");
105 BOOST_CHECK_NO_THROW(context.setPrefix(validPrefix));
106 BOOST_CHECK(context.getPrefix()[-1].isVersion());
Junxiao Shi72c0c642018-04-20 15:41:09 +0000107 BOOST_CHECK_EQUAL(context.getPrefix().getPrefix(-1), validPrefix);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700108}
109
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100110BOOST_AUTO_TEST_CASE(SetInvalid)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700111{
112 Name invalidPrefix = Name(interest->getName()).getPrefix(-1).append("/invalid");
113 BOOST_CHECK_THROW(context.setPrefix(invalidPrefix), std::invalid_argument);
114}
115
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100116BOOST_AUTO_TEST_CASE(SetValidWithAppendCalled)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700117{
118 Name validPrefix = Name(interest->getName()).append("/valid");
119 context.append(contentBlock);
120 BOOST_CHECK_THROW(context.setPrefix(validPrefix), std::domain_error);
121}
122
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100123BOOST_AUTO_TEST_CASE(SetValidWithEndCalled)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700124{
125 Name validPrefix = Name(interest->getName()).append("/valid");
126 context.end();
127 BOOST_CHECK_THROW(context.setPrefix(validPrefix), std::domain_error);
128}
129
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100130BOOST_AUTO_TEST_CASE(SetValidWithRejectCalled)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700131{
132 Name validPrefix = Name(interest->getName()).append("/valid");
133 context.reject();
134 BOOST_CHECK_THROW(context.setPrefix(validPrefix), std::domain_error);
135}
136
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100137BOOST_AUTO_TEST_SUITE_END() // Prefix
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700138
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100139BOOST_AUTO_TEST_SUITE(Expiry)
140
141BOOST_AUTO_TEST_CASE(GetAndSet)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700142{
Davide Pesavento0f830802018-01-16 23:58:58 -0500143 auto period = 9527_ms;
144 BOOST_CHECK_EQUAL(context.getExpiry(), 1000_ms);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700145 BOOST_CHECK_EQUAL(context.setExpiry(period).getExpiry(), period);
146}
147
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100148BOOST_AUTO_TEST_SUITE_END() // Expiry
149
150BOOST_AUTO_TEST_SUITE(Respond)
151
152BOOST_AUTO_TEST_CASE(Basic)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700153{
154 BOOST_CHECK_NO_THROW(context.append(contentBlock));
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800155 BOOST_CHECK(sendDataHistory.empty()); // does not call end yet
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700156
157 BOOST_CHECK_NO_THROW(context.end());
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800158
159 BOOST_REQUIRE_EQUAL(sendDataHistory.size(), 1);
160 const auto& args = sendDataHistory[0];
161
162 BOOST_CHECK_EQUAL(args.dataName, makeSegmentName(0));
Junxiao Shi72c0c642018-04-20 15:41:09 +0000163 BOOST_CHECK_EQUAL(args.content.blockFromValue(), contentBlock);
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800164 BOOST_CHECK_EQUAL(args.imsFresh, defaultImsFresh);
165 BOOST_CHECK_EQUAL(args.isFinalBlock, true);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700166}
167
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100168BOOST_AUTO_TEST_CASE(Large)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700169{
170 static Block largeBlock = [] () -> Block {
171 EncodingBuffer encoder;
172 size_t maxBlockSize = MAX_NDN_PACKET_SIZE >> 1;
173 for (size_t i = 0; i < maxBlockSize; ++i) {
174 encoder.prependByte(1);
175 }
176 encoder.prependVarNumber(maxBlockSize);
177 encoder.prependVarNumber(tlv::Content);
178 return encoder.block();
179 }();
180
181 BOOST_CHECK_NO_THROW(context.append(largeBlock));
182 BOOST_CHECK_NO_THROW(context.end());
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700183
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800184 // two segments are generated
185 BOOST_REQUIRE_EQUAL(sendDataHistory.size(), 2);
186
187 // check segment0
188 BOOST_CHECK_EQUAL(sendDataHistory[0].dataName, makeSegmentName(0));
189 BOOST_CHECK_EQUAL(sendDataHistory[0].imsFresh, defaultImsFresh);
190 BOOST_CHECK_EQUAL(sendDataHistory[0].isFinalBlock, false);
191
192 // check segment1
193 BOOST_CHECK_EQUAL(sendDataHistory[1].dataName, makeSegmentName(1));
194 BOOST_CHECK_EQUAL(sendDataHistory[1].imsFresh, defaultImsFresh);
195 BOOST_CHECK_EQUAL(sendDataHistory[1].isFinalBlock, true);
196
197 // check data content
198 auto contentLargeBlock = concatenateDataContent();
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700199 BOOST_CHECK_NO_THROW(contentLargeBlock.parse());
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800200 BOOST_REQUIRE_EQUAL(contentLargeBlock.elements().size(), 1);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000201 BOOST_CHECK_EQUAL(contentLargeBlock.elements()[0], largeBlock);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700202}
203
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100204BOOST_AUTO_TEST_CASE(MultipleSmall)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700205{
206 size_t nBlocks = 100;
207 for (size_t i = 0 ; i < nBlocks ; i ++) {
208 BOOST_CHECK_NO_THROW(context.append(contentBlock));
209 }
210 BOOST_CHECK_NO_THROW(context.end());
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700211
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800212 // check data to in-memory storage
213 BOOST_REQUIRE_EQUAL(sendDataHistory.size(), 1);
214 BOOST_CHECK_EQUAL(sendDataHistory[0].dataName, makeSegmentName(0));
215 BOOST_CHECK_EQUAL(sendDataHistory[0].imsFresh, defaultImsFresh);
216 BOOST_CHECK_EQUAL(sendDataHistory[0].isFinalBlock, true);
217
218 auto contentMultiBlocks = concatenateDataContent();
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700219 BOOST_CHECK_NO_THROW(contentMultiBlocks.parse());
220 BOOST_CHECK_EQUAL(contentMultiBlocks.elements().size(), nBlocks);
221 for (auto&& element : contentMultiBlocks.elements()) {
Junxiao Shi72c0c642018-04-20 15:41:09 +0000222 BOOST_CHECK_EQUAL(element, contentBlock);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700223 }
224}
225
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100226BOOST_AUTO_TEST_SUITE_END() // Respond
227
228BOOST_AUTO_TEST_SUITE(Reject)
229
230BOOST_AUTO_TEST_CASE(Basic)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700231{
232 BOOST_CHECK_NO_THROW(context.reject());
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800233 BOOST_REQUIRE_EQUAL(sendNackHistory.size(), 1);
234 BOOST_CHECK_EQUAL(sendNackHistory[0].getCode(), 400);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700235}
236
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100237BOOST_AUTO_TEST_SUITE_END() // Reject
238
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800239class AbnormalStateTestFixture
240{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100241protected:
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800242 AbnormalStateTestFixture()
243 : context(Interest("/abnormal-state"), bind([]{}), bind([]{}))
244 {
245 }
246
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100247protected:
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800248 mgmt::StatusDatasetContext context;
249};
250
251BOOST_FIXTURE_TEST_SUITE(AbnormalState, AbnormalStateTestFixture)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700252
253BOOST_AUTO_TEST_CASE(AppendReject)
254{
Davide Pesaventob10024c2017-09-22 01:36:44 -0400255 const uint8_t buf[] = {0x82, 0x01, 0x02};
256 BOOST_CHECK_NO_THROW(context.append(Block(buf, sizeof(buf))));
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700257 BOOST_CHECK_THROW(context.reject(), std::domain_error);
258}
259
260BOOST_AUTO_TEST_CASE(AppendEndReject)
261{
Davide Pesaventob10024c2017-09-22 01:36:44 -0400262 const uint8_t buf[] = {0x82, 0x01, 0x02};
263 BOOST_CHECK_NO_THROW(context.append(Block(buf, sizeof(buf))));
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700264 BOOST_CHECK_NO_THROW(context.end());
265 BOOST_CHECK_THROW(context.reject(), std::domain_error);
266}
267
268BOOST_AUTO_TEST_CASE(EndAppend)
269{
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700270 BOOST_CHECK_NO_THROW(context.end());
271 // end, append -> error
Davide Pesaventob10024c2017-09-22 01:36:44 -0400272 const uint8_t buf[] = {0x82, 0x01, 0x02};
273 BOOST_CHECK_THROW(context.append(Block(buf, sizeof(buf))), std::domain_error);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700274}
275
276BOOST_AUTO_TEST_CASE(EndEnd)
277{
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700278 BOOST_CHECK_NO_THROW(context.end());
279 BOOST_CHECK_THROW(context.end(), std::domain_error);
280}
281
282BOOST_AUTO_TEST_CASE(EndReject)
283{
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700284 BOOST_CHECK_NO_THROW(context.end());
285 BOOST_CHECK_THROW(context.reject(), std::domain_error);
286}
287
288BOOST_AUTO_TEST_CASE(RejectAppend)
289{
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700290 BOOST_CHECK_NO_THROW(context.reject());
Davide Pesaventob10024c2017-09-22 01:36:44 -0400291 const uint8_t buf[] = {0x82, 0x01, 0x02};
292 BOOST_CHECK_THROW(context.append(Block(buf, sizeof(buf))), std::domain_error);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700293}
294
295BOOST_AUTO_TEST_CASE(RejectEnd)
296{
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700297 BOOST_CHECK_NO_THROW(context.reject());
298 BOOST_CHECK_THROW(context.end(), std::domain_error);
299}
300
301BOOST_AUTO_TEST_SUITE_END() // AbnormalState
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100302
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800303BOOST_AUTO_TEST_SUITE_END() // TestStatusDatasetContext
304BOOST_AUTO_TEST_SUITE_END() // Mgmt
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700305
306} // namespace tests
307} // namespace mgmt
308} // namespace ndn