blob: b1876db85d974231590766d76b54224586e5958a [file] [log] [blame]
Yanbiao Li8ee37ed2015-05-19 12:44:04 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yanbiao Li4b4f7542016-03-11 02:04:43 +08003 * Copyright (c) 2013-2016 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
22#include "mgmt/status-dataset-context.hpp"
23#include "boost-test.hpp"
24#include "unit-tests/make-interest-data.hpp"
25
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{
34public:
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
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070043 StatusDatasetContextFixture()
Junxiao Shi85d90832016-08-04 03:19:46 +000044 : interest(makeInterest("/test/context/interest"))
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070045 , contentBlock(makeStringBlock(tlv::Content, "/test/data/content"))
Yanbiao Li4b4f7542016-03-11 02:04:43 +080046 , context(*interest,
47 [this] (const Name& dataName, const Block& content,
48 time::milliseconds imsFresh, bool isFinalBlock) {
49 SendDataArgs args{dataName, content, imsFresh, isFinalBlock};
50 sendDataHistory.push_back(args);
51 },
52 [this] (const ControlResponse& resp) {
53 sendNackHistory.push_back(resp);
54 })
55 , defaultImsFresh(time::milliseconds(1000))
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070056 {
57 }
58
Yanbiao Li4b4f7542016-03-11 02:04:43 +080059 Name
60 makeSegmentName(size_t segmentNo)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070061 {
Yanbiao Li4b4f7542016-03-11 02:04:43 +080062 auto name = context.getPrefix();
63 return name.appendSegment(segmentNo);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070064 }
65
66 Block
Yanbiao Li4b4f7542016-03-11 02:04:43 +080067 concatenateDataContent()
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070068 {
69 EncodingBuffer encoder;
70 size_t valueLength = 0;
Yanbiao Li4b4f7542016-03-11 02:04:43 +080071 for (auto args : sendDataHistory) {
72 const auto& content = args.content;
73 valueLength += encoder.appendByteArray(content.value(), content.value_size());
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070074 }
75 encoder.prependVarNumber(valueLength);
76 encoder.prependVarNumber(tlv::Content);
77 return encoder.block();
78 }
79
80public:
Yanbiao Li4b4f7542016-03-11 02:04:43 +080081 std::vector<SendDataArgs> sendDataHistory;
82 std::vector<ControlResponse> sendNackHistory;
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070083 shared_ptr<Interest> interest;
84 Block contentBlock;
85 mgmt::StatusDatasetContext context;
Yanbiao Li4b4f7542016-03-11 02:04:43 +080086 time::milliseconds defaultImsFresh;
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070087};
88
Yanbiao Li4b4f7542016-03-11 02:04:43 +080089BOOST_AUTO_TEST_SUITE(Mgmt)
90BOOST_FIXTURE_TEST_SUITE(TestStatusDatasetContext, StatusDatasetContextFixture)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070091
92BOOST_AUTO_TEST_CASE(GetPrefix)
93{
94 Name dataName = context.getPrefix();
95 BOOST_CHECK(dataName[-1].isVersion());
96 BOOST_CHECK(dataName.getPrefix(-1) == interest->getName());
97}
98
99BOOST_AUTO_TEST_SUITE(SetPrefix)
100
101BOOST_AUTO_TEST_CASE(Valid)
102{
103 Name validPrefix = Name(interest->getName()).append("/valid");
104 BOOST_CHECK_NO_THROW(context.setPrefix(validPrefix));
105 BOOST_CHECK(context.getPrefix()[-1].isVersion());
106 BOOST_CHECK(context.getPrefix().getPrefix(-1) == validPrefix);
107}
108
109BOOST_AUTO_TEST_CASE(Invalid)
110{
111 Name invalidPrefix = Name(interest->getName()).getPrefix(-1).append("/invalid");
112 BOOST_CHECK_THROW(context.setPrefix(invalidPrefix), std::invalid_argument);
113}
114
115BOOST_AUTO_TEST_CASE(ValidWithAppendCalled)
116{
117 Name validPrefix = Name(interest->getName()).append("/valid");
118 context.append(contentBlock);
119 BOOST_CHECK_THROW(context.setPrefix(validPrefix), std::domain_error);
120}
121
122BOOST_AUTO_TEST_CASE(ValidWithEndCalled)
123{
124 Name validPrefix = Name(interest->getName()).append("/valid");
125 context.end();
126 BOOST_CHECK_THROW(context.setPrefix(validPrefix), std::domain_error);
127}
128
129BOOST_AUTO_TEST_CASE(ValidWithRejectCalled)
130{
131 Name validPrefix = Name(interest->getName()).append("/valid");
132 context.reject();
133 BOOST_CHECK_THROW(context.setPrefix(validPrefix), std::domain_error);
134}
135
136BOOST_AUTO_TEST_SUITE_END() // SetPrefix
137
138BOOST_AUTO_TEST_CASE(Expiry)
139{
140 // getExpiry & setExpiry
141 auto period = time::milliseconds(9527);
142 BOOST_CHECK_EQUAL(context.getExpiry(), time::milliseconds(1000));
143 BOOST_CHECK_EQUAL(context.setExpiry(period).getExpiry(), period);
144}
145
146BOOST_AUTO_TEST_CASE(Respond)
147{
148 BOOST_CHECK_NO_THROW(context.append(contentBlock));
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800149 BOOST_CHECK(sendDataHistory.empty()); // does not call end yet
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700150
151 BOOST_CHECK_NO_THROW(context.end());
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800152
153 BOOST_REQUIRE_EQUAL(sendDataHistory.size(), 1);
154 const auto& args = sendDataHistory[0];
155
156 BOOST_CHECK_EQUAL(args.dataName, makeSegmentName(0));
157 BOOST_CHECK(args.content.blockFromValue() == contentBlock);
158 BOOST_CHECK_EQUAL(args.imsFresh, defaultImsFresh);
159 BOOST_CHECK_EQUAL(args.isFinalBlock, true);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700160}
161
162BOOST_AUTO_TEST_CASE(RespondLarge)
163{
164 static Block largeBlock = [] () -> Block {
165 EncodingBuffer encoder;
166 size_t maxBlockSize = MAX_NDN_PACKET_SIZE >> 1;
167 for (size_t i = 0; i < maxBlockSize; ++i) {
168 encoder.prependByte(1);
169 }
170 encoder.prependVarNumber(maxBlockSize);
171 encoder.prependVarNumber(tlv::Content);
172 return encoder.block();
173 }();
174
175 BOOST_CHECK_NO_THROW(context.append(largeBlock));
176 BOOST_CHECK_NO_THROW(context.end());
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700177
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800178 // two segments are generated
179 BOOST_REQUIRE_EQUAL(sendDataHistory.size(), 2);
180
181 // check segment0
182 BOOST_CHECK_EQUAL(sendDataHistory[0].dataName, makeSegmentName(0));
183 BOOST_CHECK_EQUAL(sendDataHistory[0].imsFresh, defaultImsFresh);
184 BOOST_CHECK_EQUAL(sendDataHistory[0].isFinalBlock, false);
185
186 // check segment1
187 BOOST_CHECK_EQUAL(sendDataHistory[1].dataName, makeSegmentName(1));
188 BOOST_CHECK_EQUAL(sendDataHistory[1].imsFresh, defaultImsFresh);
189 BOOST_CHECK_EQUAL(sendDataHistory[1].isFinalBlock, true);
190
191 // check data content
192 auto contentLargeBlock = concatenateDataContent();
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700193 BOOST_CHECK_NO_THROW(contentLargeBlock.parse());
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800194 BOOST_REQUIRE_EQUAL(contentLargeBlock.elements().size(), 1);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700195 BOOST_CHECK(contentLargeBlock.elements()[0] == largeBlock);
196}
197
198BOOST_AUTO_TEST_CASE(ResponseMultipleSmall)
199{
200 size_t nBlocks = 100;
201 for (size_t i = 0 ; i < nBlocks ; i ++) {
202 BOOST_CHECK_NO_THROW(context.append(contentBlock));
203 }
204 BOOST_CHECK_NO_THROW(context.end());
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700205
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800206 // check data to in-memory storage
207 BOOST_REQUIRE_EQUAL(sendDataHistory.size(), 1);
208 BOOST_CHECK_EQUAL(sendDataHistory[0].dataName, makeSegmentName(0));
209 BOOST_CHECK_EQUAL(sendDataHistory[0].imsFresh, defaultImsFresh);
210 BOOST_CHECK_EQUAL(sendDataHistory[0].isFinalBlock, true);
211
212 auto contentMultiBlocks = concatenateDataContent();
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700213 BOOST_CHECK_NO_THROW(contentMultiBlocks.parse());
214 BOOST_CHECK_EQUAL(contentMultiBlocks.elements().size(), nBlocks);
215 for (auto&& element : contentMultiBlocks.elements()) {
216 BOOST_CHECK(element == contentBlock);
217 }
218}
219
220BOOST_AUTO_TEST_CASE(Reject)
221{
222 BOOST_CHECK_NO_THROW(context.reject());
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800223 BOOST_REQUIRE_EQUAL(sendNackHistory.size(), 1);
224 BOOST_CHECK_EQUAL(sendNackHistory[0].getCode(), 400);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700225}
226
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800227class AbnormalStateTestFixture
228{
229public:
230 AbnormalStateTestFixture()
231 : context(Interest("/abnormal-state"), bind([]{}), bind([]{}))
232 {
233 }
234
235public:
236 mgmt::StatusDatasetContext context;
237};
238
239BOOST_FIXTURE_TEST_SUITE(AbnormalState, AbnormalStateTestFixture)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700240
241BOOST_AUTO_TEST_CASE(AppendReject)
242{
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700243 BOOST_CHECK_NO_THROW(context.append(Block("\x82\x01\x02", 3)));
244 BOOST_CHECK_THROW(context.reject(), std::domain_error);
245}
246
247BOOST_AUTO_TEST_CASE(AppendEndReject)
248{
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700249 BOOST_CHECK_NO_THROW(context.append(Block("\x82\x01\x02", 3)));
250 BOOST_CHECK_NO_THROW(context.end());
251 BOOST_CHECK_THROW(context.reject(), std::domain_error);
252}
253
254BOOST_AUTO_TEST_CASE(EndAppend)
255{
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700256 BOOST_CHECK_NO_THROW(context.end());
257 // end, append -> error
258 BOOST_CHECK_THROW(context.append(Block("\x82\x01\x02", 3)), std::domain_error);
259}
260
261BOOST_AUTO_TEST_CASE(EndEnd)
262{
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700263 BOOST_CHECK_NO_THROW(context.end());
264 BOOST_CHECK_THROW(context.end(), std::domain_error);
265}
266
267BOOST_AUTO_TEST_CASE(EndReject)
268{
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700269 BOOST_CHECK_NO_THROW(context.end());
270 BOOST_CHECK_THROW(context.reject(), std::domain_error);
271}
272
273BOOST_AUTO_TEST_CASE(RejectAppend)
274{
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700275 BOOST_CHECK_NO_THROW(context.reject());
276 BOOST_CHECK_THROW(context.append(Block("\x82\x01\x02", 3)), std::domain_error);
277}
278
279BOOST_AUTO_TEST_CASE(RejectEnd)
280{
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700281 BOOST_CHECK_NO_THROW(context.reject());
282 BOOST_CHECK_THROW(context.end(), std::domain_error);
283}
284
285BOOST_AUTO_TEST_SUITE_END() // AbnormalState
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800286BOOST_AUTO_TEST_SUITE_END() // TestStatusDatasetContext
287BOOST_AUTO_TEST_SUITE_END() // Mgmt
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700288
289} // namespace tests
290} // namespace mgmt
291} // namespace ndn