blob: d7c07164ebd7fa19325241e8f9f0e02e0b7b11b7 [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
30class StatusDatasetContextFixture
31{
32public:
Yanbiao Li4b4f7542016-03-11 02:04:43 +080033 struct SendDataArgs
34 {
35 Name dataName;
36 Block content;
37 time::milliseconds imsFresh;
38 bool isFinalBlock;
39 };
40
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070041 StatusDatasetContextFixture()
42 : interest(util::makeInterest("/test/context/interest"))
43 , contentBlock(makeStringBlock(tlv::Content, "/test/data/content"))
Yanbiao Li4b4f7542016-03-11 02:04:43 +080044 , context(*interest,
45 [this] (const Name& dataName, const Block& content,
46 time::milliseconds imsFresh, bool isFinalBlock) {
47 SendDataArgs args{dataName, content, imsFresh, isFinalBlock};
48 sendDataHistory.push_back(args);
49 },
50 [this] (const ControlResponse& resp) {
51 sendNackHistory.push_back(resp);
52 })
53 , defaultImsFresh(time::milliseconds(1000))
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070054 {
55 }
56
Yanbiao Li4b4f7542016-03-11 02:04:43 +080057 Name
58 makeSegmentName(size_t segmentNo)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070059 {
Yanbiao Li4b4f7542016-03-11 02:04:43 +080060 auto name = context.getPrefix();
61 return name.appendSegment(segmentNo);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070062 }
63
64 Block
Yanbiao Li4b4f7542016-03-11 02:04:43 +080065 concatenateDataContent()
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070066 {
67 EncodingBuffer encoder;
68 size_t valueLength = 0;
Yanbiao Li4b4f7542016-03-11 02:04:43 +080069 for (auto args : sendDataHistory) {
70 const auto& content = args.content;
71 valueLength += encoder.appendByteArray(content.value(), content.value_size());
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070072 }
73 encoder.prependVarNumber(valueLength);
74 encoder.prependVarNumber(tlv::Content);
75 return encoder.block();
76 }
77
78public:
Yanbiao Li4b4f7542016-03-11 02:04:43 +080079 std::vector<SendDataArgs> sendDataHistory;
80 std::vector<ControlResponse> sendNackHistory;
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070081 shared_ptr<Interest> interest;
82 Block contentBlock;
83 mgmt::StatusDatasetContext context;
Yanbiao Li4b4f7542016-03-11 02:04:43 +080084 time::milliseconds defaultImsFresh;
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070085};
86
Yanbiao Li4b4f7542016-03-11 02:04:43 +080087BOOST_AUTO_TEST_SUITE(Mgmt)
88BOOST_FIXTURE_TEST_SUITE(TestStatusDatasetContext, StatusDatasetContextFixture)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070089
90BOOST_AUTO_TEST_CASE(GetPrefix)
91{
92 Name dataName = context.getPrefix();
93 BOOST_CHECK(dataName[-1].isVersion());
94 BOOST_CHECK(dataName.getPrefix(-1) == interest->getName());
95}
96
97BOOST_AUTO_TEST_SUITE(SetPrefix)
98
99BOOST_AUTO_TEST_CASE(Valid)
100{
101 Name validPrefix = Name(interest->getName()).append("/valid");
102 BOOST_CHECK_NO_THROW(context.setPrefix(validPrefix));
103 BOOST_CHECK(context.getPrefix()[-1].isVersion());
104 BOOST_CHECK(context.getPrefix().getPrefix(-1) == validPrefix);
105}
106
107BOOST_AUTO_TEST_CASE(Invalid)
108{
109 Name invalidPrefix = Name(interest->getName()).getPrefix(-1).append("/invalid");
110 BOOST_CHECK_THROW(context.setPrefix(invalidPrefix), std::invalid_argument);
111}
112
113BOOST_AUTO_TEST_CASE(ValidWithAppendCalled)
114{
115 Name validPrefix = Name(interest->getName()).append("/valid");
116 context.append(contentBlock);
117 BOOST_CHECK_THROW(context.setPrefix(validPrefix), std::domain_error);
118}
119
120BOOST_AUTO_TEST_CASE(ValidWithEndCalled)
121{
122 Name validPrefix = Name(interest->getName()).append("/valid");
123 context.end();
124 BOOST_CHECK_THROW(context.setPrefix(validPrefix), std::domain_error);
125}
126
127BOOST_AUTO_TEST_CASE(ValidWithRejectCalled)
128{
129 Name validPrefix = Name(interest->getName()).append("/valid");
130 context.reject();
131 BOOST_CHECK_THROW(context.setPrefix(validPrefix), std::domain_error);
132}
133
134BOOST_AUTO_TEST_SUITE_END() // SetPrefix
135
136BOOST_AUTO_TEST_CASE(Expiry)
137{
138 // getExpiry & setExpiry
139 auto period = time::milliseconds(9527);
140 BOOST_CHECK_EQUAL(context.getExpiry(), time::milliseconds(1000));
141 BOOST_CHECK_EQUAL(context.setExpiry(period).getExpiry(), period);
142}
143
144BOOST_AUTO_TEST_CASE(Respond)
145{
146 BOOST_CHECK_NO_THROW(context.append(contentBlock));
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800147 BOOST_CHECK(sendDataHistory.empty()); // does not call end yet
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700148
149 BOOST_CHECK_NO_THROW(context.end());
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800150
151 BOOST_REQUIRE_EQUAL(sendDataHistory.size(), 1);
152 const auto& args = sendDataHistory[0];
153
154 BOOST_CHECK_EQUAL(args.dataName, makeSegmentName(0));
155 BOOST_CHECK(args.content.blockFromValue() == contentBlock);
156 BOOST_CHECK_EQUAL(args.imsFresh, defaultImsFresh);
157 BOOST_CHECK_EQUAL(args.isFinalBlock, true);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700158}
159
160BOOST_AUTO_TEST_CASE(RespondLarge)
161{
162 static Block largeBlock = [] () -> Block {
163 EncodingBuffer encoder;
164 size_t maxBlockSize = MAX_NDN_PACKET_SIZE >> 1;
165 for (size_t i = 0; i < maxBlockSize; ++i) {
166 encoder.prependByte(1);
167 }
168 encoder.prependVarNumber(maxBlockSize);
169 encoder.prependVarNumber(tlv::Content);
170 return encoder.block();
171 }();
172
173 BOOST_CHECK_NO_THROW(context.append(largeBlock));
174 BOOST_CHECK_NO_THROW(context.end());
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700175
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800176 // two segments are generated
177 BOOST_REQUIRE_EQUAL(sendDataHistory.size(), 2);
178
179 // check segment0
180 BOOST_CHECK_EQUAL(sendDataHistory[0].dataName, makeSegmentName(0));
181 BOOST_CHECK_EQUAL(sendDataHistory[0].imsFresh, defaultImsFresh);
182 BOOST_CHECK_EQUAL(sendDataHistory[0].isFinalBlock, false);
183
184 // check segment1
185 BOOST_CHECK_EQUAL(sendDataHistory[1].dataName, makeSegmentName(1));
186 BOOST_CHECK_EQUAL(sendDataHistory[1].imsFresh, defaultImsFresh);
187 BOOST_CHECK_EQUAL(sendDataHistory[1].isFinalBlock, true);
188
189 // check data content
190 auto contentLargeBlock = concatenateDataContent();
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700191 BOOST_CHECK_NO_THROW(contentLargeBlock.parse());
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800192 BOOST_REQUIRE_EQUAL(contentLargeBlock.elements().size(), 1);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700193 BOOST_CHECK(contentLargeBlock.elements()[0] == largeBlock);
194}
195
196BOOST_AUTO_TEST_CASE(ResponseMultipleSmall)
197{
198 size_t nBlocks = 100;
199 for (size_t i = 0 ; i < nBlocks ; i ++) {
200 BOOST_CHECK_NO_THROW(context.append(contentBlock));
201 }
202 BOOST_CHECK_NO_THROW(context.end());
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700203
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800204 // check data to in-memory storage
205 BOOST_REQUIRE_EQUAL(sendDataHistory.size(), 1);
206 BOOST_CHECK_EQUAL(sendDataHistory[0].dataName, makeSegmentName(0));
207 BOOST_CHECK_EQUAL(sendDataHistory[0].imsFresh, defaultImsFresh);
208 BOOST_CHECK_EQUAL(sendDataHistory[0].isFinalBlock, true);
209
210 auto contentMultiBlocks = concatenateDataContent();
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700211 BOOST_CHECK_NO_THROW(contentMultiBlocks.parse());
212 BOOST_CHECK_EQUAL(contentMultiBlocks.elements().size(), nBlocks);
213 for (auto&& element : contentMultiBlocks.elements()) {
214 BOOST_CHECK(element == contentBlock);
215 }
216}
217
218BOOST_AUTO_TEST_CASE(Reject)
219{
220 BOOST_CHECK_NO_THROW(context.reject());
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800221 BOOST_REQUIRE_EQUAL(sendNackHistory.size(), 1);
222 BOOST_CHECK_EQUAL(sendNackHistory[0].getCode(), 400);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700223}
224
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800225class AbnormalStateTestFixture
226{
227public:
228 AbnormalStateTestFixture()
229 : context(Interest("/abnormal-state"), bind([]{}), bind([]{}))
230 {
231 }
232
233public:
234 mgmt::StatusDatasetContext context;
235};
236
237BOOST_FIXTURE_TEST_SUITE(AbnormalState, AbnormalStateTestFixture)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700238
239BOOST_AUTO_TEST_CASE(AppendReject)
240{
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700241 BOOST_CHECK_NO_THROW(context.append(Block("\x82\x01\x02", 3)));
242 BOOST_CHECK_THROW(context.reject(), std::domain_error);
243}
244
245BOOST_AUTO_TEST_CASE(AppendEndReject)
246{
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700247 BOOST_CHECK_NO_THROW(context.append(Block("\x82\x01\x02", 3)));
248 BOOST_CHECK_NO_THROW(context.end());
249 BOOST_CHECK_THROW(context.reject(), std::domain_error);
250}
251
252BOOST_AUTO_TEST_CASE(EndAppend)
253{
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700254 BOOST_CHECK_NO_THROW(context.end());
255 // end, append -> error
256 BOOST_CHECK_THROW(context.append(Block("\x82\x01\x02", 3)), std::domain_error);
257}
258
259BOOST_AUTO_TEST_CASE(EndEnd)
260{
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700261 BOOST_CHECK_NO_THROW(context.end());
262 BOOST_CHECK_THROW(context.end(), std::domain_error);
263}
264
265BOOST_AUTO_TEST_CASE(EndReject)
266{
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700267 BOOST_CHECK_NO_THROW(context.end());
268 BOOST_CHECK_THROW(context.reject(), std::domain_error);
269}
270
271BOOST_AUTO_TEST_CASE(RejectAppend)
272{
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700273 BOOST_CHECK_NO_THROW(context.reject());
274 BOOST_CHECK_THROW(context.append(Block("\x82\x01\x02", 3)), std::domain_error);
275}
276
277BOOST_AUTO_TEST_CASE(RejectEnd)
278{
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700279 BOOST_CHECK_NO_THROW(context.reject());
280 BOOST_CHECK_THROW(context.end(), std::domain_error);
281}
282
283BOOST_AUTO_TEST_SUITE_END() // AbnormalState
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800284BOOST_AUTO_TEST_SUITE_END() // TestStatusDatasetContext
285BOOST_AUTO_TEST_SUITE_END() // Mgmt
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700286
287} // namespace tests
288} // namespace mgmt
289} // namespace ndn