Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame^] | 3 | * Copyright (c) 2013-2016 Regents of the University of California. |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 4 | * |
Alexander Afanasyev | 80b68e1 | 2015-09-17 17:01:04 -0700 | [diff] [blame] | 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | 80b68e1 | 2015-09-17 17:01:04 -0700 | [diff] [blame] | 7 | * 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 Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 10 | * |
Alexander Afanasyev | 80b68e1 | 2015-09-17 17:01:04 -0700 | [diff] [blame] | 11 | * 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 Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 14 | * |
Alexander Afanasyev | 80b68e1 | 2015-09-17 17:01:04 -0700 | [diff] [blame] | 15 | * 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 Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include "mgmt/status-dataset-context.hpp" |
| 23 | #include "boost-test.hpp" |
| 24 | #include "unit-tests/make-interest-data.hpp" |
| 25 | |
| 26 | namespace ndn { |
| 27 | namespace mgmt { |
| 28 | namespace tests { |
| 29 | |
| 30 | class StatusDatasetContextFixture |
| 31 | { |
| 32 | public: |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame^] | 33 | struct SendDataArgs |
| 34 | { |
| 35 | Name dataName; |
| 36 | Block content; |
| 37 | time::milliseconds imsFresh; |
| 38 | bool isFinalBlock; |
| 39 | }; |
| 40 | |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 41 | StatusDatasetContextFixture() |
| 42 | : interest(util::makeInterest("/test/context/interest")) |
| 43 | , contentBlock(makeStringBlock(tlv::Content, "/test/data/content")) |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame^] | 44 | , 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 Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 54 | { |
| 55 | } |
| 56 | |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame^] | 57 | Name |
| 58 | makeSegmentName(size_t segmentNo) |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 59 | { |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame^] | 60 | auto name = context.getPrefix(); |
| 61 | return name.appendSegment(segmentNo); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | Block |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame^] | 65 | concatenateDataContent() |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 66 | { |
| 67 | EncodingBuffer encoder; |
| 68 | size_t valueLength = 0; |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame^] | 69 | for (auto args : sendDataHistory) { |
| 70 | const auto& content = args.content; |
| 71 | valueLength += encoder.appendByteArray(content.value(), content.value_size()); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 72 | } |
| 73 | encoder.prependVarNumber(valueLength); |
| 74 | encoder.prependVarNumber(tlv::Content); |
| 75 | return encoder.block(); |
| 76 | } |
| 77 | |
| 78 | public: |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame^] | 79 | std::vector<SendDataArgs> sendDataHistory; |
| 80 | std::vector<ControlResponse> sendNackHistory; |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 81 | shared_ptr<Interest> interest; |
| 82 | Block contentBlock; |
| 83 | mgmt::StatusDatasetContext context; |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame^] | 84 | time::milliseconds defaultImsFresh; |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame^] | 87 | BOOST_AUTO_TEST_SUITE(Mgmt) |
| 88 | BOOST_FIXTURE_TEST_SUITE(TestStatusDatasetContext, StatusDatasetContextFixture) |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 89 | |
| 90 | BOOST_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 | |
| 97 | BOOST_AUTO_TEST_SUITE(SetPrefix) |
| 98 | |
| 99 | BOOST_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 | |
| 107 | BOOST_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 | |
| 113 | BOOST_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 | |
| 120 | BOOST_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 | |
| 127 | BOOST_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 | |
| 134 | BOOST_AUTO_TEST_SUITE_END() // SetPrefix |
| 135 | |
| 136 | BOOST_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 | |
| 144 | BOOST_AUTO_TEST_CASE(Respond) |
| 145 | { |
| 146 | BOOST_CHECK_NO_THROW(context.append(contentBlock)); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame^] | 147 | BOOST_CHECK(sendDataHistory.empty()); // does not call end yet |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 148 | |
| 149 | BOOST_CHECK_NO_THROW(context.end()); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame^] | 150 | |
| 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 Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | BOOST_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 Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 175 | |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame^] | 176 | // 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 Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 191 | BOOST_CHECK_NO_THROW(contentLargeBlock.parse()); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame^] | 192 | BOOST_REQUIRE_EQUAL(contentLargeBlock.elements().size(), 1); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 193 | BOOST_CHECK(contentLargeBlock.elements()[0] == largeBlock); |
| 194 | } |
| 195 | |
| 196 | BOOST_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 Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 203 | |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame^] | 204 | // 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 Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 211 | 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 | |
| 218 | BOOST_AUTO_TEST_CASE(Reject) |
| 219 | { |
| 220 | BOOST_CHECK_NO_THROW(context.reject()); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame^] | 221 | BOOST_REQUIRE_EQUAL(sendNackHistory.size(), 1); |
| 222 | BOOST_CHECK_EQUAL(sendNackHistory[0].getCode(), 400); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame^] | 225 | class AbnormalStateTestFixture |
| 226 | { |
| 227 | public: |
| 228 | AbnormalStateTestFixture() |
| 229 | : context(Interest("/abnormal-state"), bind([]{}), bind([]{})) |
| 230 | { |
| 231 | } |
| 232 | |
| 233 | public: |
| 234 | mgmt::StatusDatasetContext context; |
| 235 | }; |
| 236 | |
| 237 | BOOST_FIXTURE_TEST_SUITE(AbnormalState, AbnormalStateTestFixture) |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 238 | |
| 239 | BOOST_AUTO_TEST_CASE(AppendReject) |
| 240 | { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 241 | BOOST_CHECK_NO_THROW(context.append(Block("\x82\x01\x02", 3))); |
| 242 | BOOST_CHECK_THROW(context.reject(), std::domain_error); |
| 243 | } |
| 244 | |
| 245 | BOOST_AUTO_TEST_CASE(AppendEndReject) |
| 246 | { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 247 | 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 | |
| 252 | BOOST_AUTO_TEST_CASE(EndAppend) |
| 253 | { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 254 | 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 | |
| 259 | BOOST_AUTO_TEST_CASE(EndEnd) |
| 260 | { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 261 | BOOST_CHECK_NO_THROW(context.end()); |
| 262 | BOOST_CHECK_THROW(context.end(), std::domain_error); |
| 263 | } |
| 264 | |
| 265 | BOOST_AUTO_TEST_CASE(EndReject) |
| 266 | { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 267 | BOOST_CHECK_NO_THROW(context.end()); |
| 268 | BOOST_CHECK_THROW(context.reject(), std::domain_error); |
| 269 | } |
| 270 | |
| 271 | BOOST_AUTO_TEST_CASE(RejectAppend) |
| 272 | { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 273 | BOOST_CHECK_NO_THROW(context.reject()); |
| 274 | BOOST_CHECK_THROW(context.append(Block("\x82\x01\x02", 3)), std::domain_error); |
| 275 | } |
| 276 | |
| 277 | BOOST_AUTO_TEST_CASE(RejectEnd) |
| 278 | { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 279 | BOOST_CHECK_NO_THROW(context.reject()); |
| 280 | BOOST_CHECK_THROW(context.end(), std::domain_error); |
| 281 | } |
| 282 | |
| 283 | BOOST_AUTO_TEST_SUITE_END() // AbnormalState |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame^] | 284 | BOOST_AUTO_TEST_SUITE_END() // TestStatusDatasetContext |
| 285 | BOOST_AUTO_TEST_SUITE_END() // Mgmt |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 286 | |
| 287 | } // namespace tests |
| 288 | } // namespace mgmt |
| 289 | } // namespace ndn |