blob: 6ea594e2edc76d34af8e2afcc9733b2cd691b6d5 [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 Pesavento0f830802018-01-16 23:58:58 -05003 * Copyright (c) 2013-2018 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 Pesavento7e780642018-11-24 15:51:34 -050024#include "tests/boost-test.hpp"
25#include "tests/make-interest-data.hpp"
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070026
27namespace ndn {
28namespace mgmt {
29namespace tests {
30
Junxiao Shi85d90832016-08-04 03:19:46 +000031using namespace ndn::tests;
32
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070033class StatusDatasetContextFixture
34{
Davide Pesaventoeee3e822016-11-26 19:19:34 +010035private:
Yanbiao Li4b4f7542016-03-11 02:04:43 +080036 struct SendDataArgs
37 {
38 Name dataName;
39 Block content;
40 time::milliseconds imsFresh;
41 bool isFinalBlock;
42 };
43
Davide Pesaventoeee3e822016-11-26 19:19:34 +010044protected:
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070045 StatusDatasetContextFixture()
Junxiao Shi85d90832016-08-04 03:19:46 +000046 : interest(makeInterest("/test/context/interest"))
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070047 , contentBlock(makeStringBlock(tlv::Content, "/test/data/content"))
Yanbiao Li4b4f7542016-03-11 02:04:43 +080048 , context(*interest,
49 [this] (const Name& dataName, const Block& content,
50 time::milliseconds imsFresh, bool isFinalBlock) {
51 SendDataArgs args{dataName, content, imsFresh, isFinalBlock};
52 sendDataHistory.push_back(args);
53 },
54 [this] (const ControlResponse& resp) {
55 sendNackHistory.push_back(resp);
56 })
Davide Pesavento0f830802018-01-16 23:58:58 -050057 , defaultImsFresh(1000_ms)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070058 {
59 }
60
Yanbiao Li4b4f7542016-03-11 02:04:43 +080061 Name
Davide Pesaventoeee3e822016-11-26 19:19:34 +010062 makeSegmentName(size_t segmentNo) const
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070063 {
Yanbiao Li4b4f7542016-03-11 02:04:43 +080064 auto name = context.getPrefix();
65 return name.appendSegment(segmentNo);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070066 }
67
68 Block
Davide Pesaventoeee3e822016-11-26 19:19:34 +010069 concatenateDataContent() const
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070070 {
71 EncodingBuffer encoder;
72 size_t valueLength = 0;
Davide Pesaventoeee3e822016-11-26 19:19:34 +010073 for (const auto& args : sendDataHistory) {
Yanbiao Li4b4f7542016-03-11 02:04:43 +080074 const auto& content = args.content;
75 valueLength += encoder.appendByteArray(content.value(), content.value_size());
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070076 }
77 encoder.prependVarNumber(valueLength);
78 encoder.prependVarNumber(tlv::Content);
79 return encoder.block();
80 }
81
Davide Pesaventoeee3e822016-11-26 19:19:34 +010082protected:
Yanbiao Li4b4f7542016-03-11 02:04:43 +080083 std::vector<SendDataArgs> sendDataHistory;
84 std::vector<ControlResponse> sendNackHistory;
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070085 shared_ptr<Interest> interest;
86 Block contentBlock;
87 mgmt::StatusDatasetContext context;
Yanbiao Li4b4f7542016-03-11 02:04:43 +080088 time::milliseconds defaultImsFresh;
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070089};
90
Yanbiao Li4b4f7542016-03-11 02:04:43 +080091BOOST_AUTO_TEST_SUITE(Mgmt)
92BOOST_FIXTURE_TEST_SUITE(TestStatusDatasetContext, StatusDatasetContextFixture)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070093
Davide Pesaventoeee3e822016-11-26 19:19:34 +010094BOOST_AUTO_TEST_SUITE(Prefix)
95
96BOOST_AUTO_TEST_CASE(Get)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070097{
98 Name dataName = context.getPrefix();
99 BOOST_CHECK(dataName[-1].isVersion());
Junxiao Shi72c0c642018-04-20 15:41:09 +0000100 BOOST_CHECK_EQUAL(dataName.getPrefix(-1), interest->getName());
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700101}
102
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100103BOOST_AUTO_TEST_CASE(SetValid)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700104{
105 Name validPrefix = Name(interest->getName()).append("/valid");
106 BOOST_CHECK_NO_THROW(context.setPrefix(validPrefix));
107 BOOST_CHECK(context.getPrefix()[-1].isVersion());
Junxiao Shi72c0c642018-04-20 15:41:09 +0000108 BOOST_CHECK_EQUAL(context.getPrefix().getPrefix(-1), validPrefix);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700109}
110
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100111BOOST_AUTO_TEST_CASE(SetInvalid)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700112{
113 Name invalidPrefix = Name(interest->getName()).getPrefix(-1).append("/invalid");
114 BOOST_CHECK_THROW(context.setPrefix(invalidPrefix), std::invalid_argument);
115}
116
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100117BOOST_AUTO_TEST_CASE(SetValidWithAppendCalled)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700118{
119 Name validPrefix = Name(interest->getName()).append("/valid");
120 context.append(contentBlock);
121 BOOST_CHECK_THROW(context.setPrefix(validPrefix), std::domain_error);
122}
123
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100124BOOST_AUTO_TEST_CASE(SetValidWithEndCalled)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700125{
126 Name validPrefix = Name(interest->getName()).append("/valid");
127 context.end();
128 BOOST_CHECK_THROW(context.setPrefix(validPrefix), std::domain_error);
129}
130
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100131BOOST_AUTO_TEST_CASE(SetValidWithRejectCalled)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700132{
133 Name validPrefix = Name(interest->getName()).append("/valid");
134 context.reject();
135 BOOST_CHECK_THROW(context.setPrefix(validPrefix), std::domain_error);
136}
137
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100138BOOST_AUTO_TEST_SUITE_END() // Prefix
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700139
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100140BOOST_AUTO_TEST_SUITE(Expiry)
141
142BOOST_AUTO_TEST_CASE(GetAndSet)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700143{
Davide Pesavento0f830802018-01-16 23:58:58 -0500144 auto period = 9527_ms;
145 BOOST_CHECK_EQUAL(context.getExpiry(), 1000_ms);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700146 BOOST_CHECK_EQUAL(context.setExpiry(period).getExpiry(), period);
147}
148
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100149BOOST_AUTO_TEST_SUITE_END() // Expiry
150
151BOOST_AUTO_TEST_SUITE(Respond)
152
153BOOST_AUTO_TEST_CASE(Basic)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700154{
155 BOOST_CHECK_NO_THROW(context.append(contentBlock));
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800156 BOOST_CHECK(sendDataHistory.empty()); // does not call end yet
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700157
158 BOOST_CHECK_NO_THROW(context.end());
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800159
160 BOOST_REQUIRE_EQUAL(sendDataHistory.size(), 1);
161 const auto& args = sendDataHistory[0];
162
163 BOOST_CHECK_EQUAL(args.dataName, makeSegmentName(0));
Junxiao Shi72c0c642018-04-20 15:41:09 +0000164 BOOST_CHECK_EQUAL(args.content.blockFromValue(), contentBlock);
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800165 BOOST_CHECK_EQUAL(args.imsFresh, defaultImsFresh);
166 BOOST_CHECK_EQUAL(args.isFinalBlock, true);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700167}
168
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100169BOOST_AUTO_TEST_CASE(Large)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700170{
171 static Block largeBlock = [] () -> Block {
172 EncodingBuffer encoder;
173 size_t maxBlockSize = MAX_NDN_PACKET_SIZE >> 1;
174 for (size_t i = 0; i < maxBlockSize; ++i) {
175 encoder.prependByte(1);
176 }
177 encoder.prependVarNumber(maxBlockSize);
178 encoder.prependVarNumber(tlv::Content);
179 return encoder.block();
180 }();
181
182 BOOST_CHECK_NO_THROW(context.append(largeBlock));
183 BOOST_CHECK_NO_THROW(context.end());
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700184
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800185 // two segments are generated
186 BOOST_REQUIRE_EQUAL(sendDataHistory.size(), 2);
187
188 // check segment0
189 BOOST_CHECK_EQUAL(sendDataHistory[0].dataName, makeSegmentName(0));
190 BOOST_CHECK_EQUAL(sendDataHistory[0].imsFresh, defaultImsFresh);
191 BOOST_CHECK_EQUAL(sendDataHistory[0].isFinalBlock, false);
192
193 // check segment1
194 BOOST_CHECK_EQUAL(sendDataHistory[1].dataName, makeSegmentName(1));
195 BOOST_CHECK_EQUAL(sendDataHistory[1].imsFresh, defaultImsFresh);
196 BOOST_CHECK_EQUAL(sendDataHistory[1].isFinalBlock, true);
197
198 // check data content
199 auto contentLargeBlock = concatenateDataContent();
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700200 BOOST_CHECK_NO_THROW(contentLargeBlock.parse());
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800201 BOOST_REQUIRE_EQUAL(contentLargeBlock.elements().size(), 1);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000202 BOOST_CHECK_EQUAL(contentLargeBlock.elements()[0], largeBlock);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700203}
204
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100205BOOST_AUTO_TEST_CASE(MultipleSmall)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700206{
207 size_t nBlocks = 100;
208 for (size_t i = 0 ; i < nBlocks ; i ++) {
209 BOOST_CHECK_NO_THROW(context.append(contentBlock));
210 }
211 BOOST_CHECK_NO_THROW(context.end());
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700212
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800213 // check data to in-memory storage
214 BOOST_REQUIRE_EQUAL(sendDataHistory.size(), 1);
215 BOOST_CHECK_EQUAL(sendDataHistory[0].dataName, makeSegmentName(0));
216 BOOST_CHECK_EQUAL(sendDataHistory[0].imsFresh, defaultImsFresh);
217 BOOST_CHECK_EQUAL(sendDataHistory[0].isFinalBlock, true);
218
219 auto contentMultiBlocks = concatenateDataContent();
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700220 BOOST_CHECK_NO_THROW(contentMultiBlocks.parse());
221 BOOST_CHECK_EQUAL(contentMultiBlocks.elements().size(), nBlocks);
222 for (auto&& element : contentMultiBlocks.elements()) {
Junxiao Shi72c0c642018-04-20 15:41:09 +0000223 BOOST_CHECK_EQUAL(element, contentBlock);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700224 }
225}
226
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100227BOOST_AUTO_TEST_SUITE_END() // Respond
228
229BOOST_AUTO_TEST_SUITE(Reject)
230
231BOOST_AUTO_TEST_CASE(Basic)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700232{
233 BOOST_CHECK_NO_THROW(context.reject());
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800234 BOOST_REQUIRE_EQUAL(sendNackHistory.size(), 1);
235 BOOST_CHECK_EQUAL(sendNackHistory[0].getCode(), 400);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700236}
237
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100238BOOST_AUTO_TEST_SUITE_END() // Reject
239
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800240class AbnormalStateTestFixture
241{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100242protected:
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800243 AbnormalStateTestFixture()
244 : context(Interest("/abnormal-state"), bind([]{}), bind([]{}))
245 {
246 }
247
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100248protected:
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800249 mgmt::StatusDatasetContext context;
250};
251
252BOOST_FIXTURE_TEST_SUITE(AbnormalState, AbnormalStateTestFixture)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700253
254BOOST_AUTO_TEST_CASE(AppendReject)
255{
Davide Pesaventob10024c2017-09-22 01:36:44 -0400256 const uint8_t buf[] = {0x82, 0x01, 0x02};
257 BOOST_CHECK_NO_THROW(context.append(Block(buf, sizeof(buf))));
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700258 BOOST_CHECK_THROW(context.reject(), std::domain_error);
259}
260
261BOOST_AUTO_TEST_CASE(AppendEndReject)
262{
Davide Pesaventob10024c2017-09-22 01:36:44 -0400263 const uint8_t buf[] = {0x82, 0x01, 0x02};
264 BOOST_CHECK_NO_THROW(context.append(Block(buf, sizeof(buf))));
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700265 BOOST_CHECK_NO_THROW(context.end());
266 BOOST_CHECK_THROW(context.reject(), std::domain_error);
267}
268
269BOOST_AUTO_TEST_CASE(EndAppend)
270{
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700271 BOOST_CHECK_NO_THROW(context.end());
272 // end, append -> error
Davide Pesaventob10024c2017-09-22 01:36:44 -0400273 const uint8_t buf[] = {0x82, 0x01, 0x02};
274 BOOST_CHECK_THROW(context.append(Block(buf, sizeof(buf))), std::domain_error);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700275}
276
277BOOST_AUTO_TEST_CASE(EndEnd)
278{
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700279 BOOST_CHECK_NO_THROW(context.end());
280 BOOST_CHECK_THROW(context.end(), std::domain_error);
281}
282
283BOOST_AUTO_TEST_CASE(EndReject)
284{
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700285 BOOST_CHECK_NO_THROW(context.end());
286 BOOST_CHECK_THROW(context.reject(), std::domain_error);
287}
288
289BOOST_AUTO_TEST_CASE(RejectAppend)
290{
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700291 BOOST_CHECK_NO_THROW(context.reject());
Davide Pesaventob10024c2017-09-22 01:36:44 -0400292 const uint8_t buf[] = {0x82, 0x01, 0x02};
293 BOOST_CHECK_THROW(context.append(Block(buf, sizeof(buf))), std::domain_error);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700294}
295
296BOOST_AUTO_TEST_CASE(RejectEnd)
297{
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700298 BOOST_CHECK_NO_THROW(context.reject());
299 BOOST_CHECK_THROW(context.end(), std::domain_error);
300}
301
302BOOST_AUTO_TEST_SUITE_END() // AbnormalState
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100303
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800304BOOST_AUTO_TEST_SUITE_END() // TestStatusDatasetContext
305BOOST_AUTO_TEST_SUITE_END() // Mgmt
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700306
307} // namespace tests
308} // namespace mgmt
309} // namespace ndn