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 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 30 | using namespace ndn::tests; |
| 31 | |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 32 | class StatusDatasetContextFixture |
| 33 | { |
| 34 | public: |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 35 | struct SendDataArgs |
| 36 | { |
| 37 | Name dataName; |
| 38 | Block content; |
| 39 | time::milliseconds imsFresh; |
| 40 | bool isFinalBlock; |
| 41 | }; |
| 42 | |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 43 | StatusDatasetContextFixture() |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 44 | : interest(makeInterest("/test/context/interest")) |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 45 | , contentBlock(makeStringBlock(tlv::Content, "/test/data/content")) |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 46 | , 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 Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 56 | { |
| 57 | } |
| 58 | |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 59 | Name |
| 60 | makeSegmentName(size_t segmentNo) |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 61 | { |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 62 | auto name = context.getPrefix(); |
| 63 | return name.appendSegment(segmentNo); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | Block |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 67 | concatenateDataContent() |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 68 | { |
| 69 | EncodingBuffer encoder; |
| 70 | size_t valueLength = 0; |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 71 | for (auto args : sendDataHistory) { |
| 72 | const auto& content = args.content; |
| 73 | valueLength += encoder.appendByteArray(content.value(), content.value_size()); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 74 | } |
| 75 | encoder.prependVarNumber(valueLength); |
| 76 | encoder.prependVarNumber(tlv::Content); |
| 77 | return encoder.block(); |
| 78 | } |
| 79 | |
| 80 | public: |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 81 | std::vector<SendDataArgs> sendDataHistory; |
| 82 | std::vector<ControlResponse> sendNackHistory; |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 83 | shared_ptr<Interest> interest; |
| 84 | Block contentBlock; |
| 85 | mgmt::StatusDatasetContext context; |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 86 | time::milliseconds defaultImsFresh; |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 87 | }; |
| 88 | |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 89 | BOOST_AUTO_TEST_SUITE(Mgmt) |
| 90 | BOOST_FIXTURE_TEST_SUITE(TestStatusDatasetContext, StatusDatasetContextFixture) |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 91 | |
| 92 | BOOST_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 | |
| 99 | BOOST_AUTO_TEST_SUITE(SetPrefix) |
| 100 | |
| 101 | BOOST_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 | |
| 109 | BOOST_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 | |
| 115 | BOOST_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 | |
| 122 | BOOST_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 | |
| 129 | BOOST_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 | |
| 136 | BOOST_AUTO_TEST_SUITE_END() // SetPrefix |
| 137 | |
| 138 | BOOST_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 | |
| 146 | BOOST_AUTO_TEST_CASE(Respond) |
| 147 | { |
| 148 | BOOST_CHECK_NO_THROW(context.append(contentBlock)); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 149 | BOOST_CHECK(sendDataHistory.empty()); // does not call end yet |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 150 | |
| 151 | BOOST_CHECK_NO_THROW(context.end()); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 152 | |
| 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 Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | BOOST_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 Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 177 | |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 178 | // 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 Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 193 | BOOST_CHECK_NO_THROW(contentLargeBlock.parse()); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 194 | BOOST_REQUIRE_EQUAL(contentLargeBlock.elements().size(), 1); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 195 | BOOST_CHECK(contentLargeBlock.elements()[0] == largeBlock); |
| 196 | } |
| 197 | |
| 198 | BOOST_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 Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 205 | |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 206 | // 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 Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 213 | 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 | |
| 220 | BOOST_AUTO_TEST_CASE(Reject) |
| 221 | { |
| 222 | BOOST_CHECK_NO_THROW(context.reject()); |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 223 | BOOST_REQUIRE_EQUAL(sendNackHistory.size(), 1); |
| 224 | BOOST_CHECK_EQUAL(sendNackHistory[0].getCode(), 400); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 227 | class AbnormalStateTestFixture |
| 228 | { |
| 229 | public: |
| 230 | AbnormalStateTestFixture() |
| 231 | : context(Interest("/abnormal-state"), bind([]{}), bind([]{})) |
| 232 | { |
| 233 | } |
| 234 | |
| 235 | public: |
| 236 | mgmt::StatusDatasetContext context; |
| 237 | }; |
| 238 | |
| 239 | BOOST_FIXTURE_TEST_SUITE(AbnormalState, AbnormalStateTestFixture) |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 240 | |
| 241 | BOOST_AUTO_TEST_CASE(AppendReject) |
| 242 | { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 243 | BOOST_CHECK_NO_THROW(context.append(Block("\x82\x01\x02", 3))); |
| 244 | BOOST_CHECK_THROW(context.reject(), std::domain_error); |
| 245 | } |
| 246 | |
| 247 | BOOST_AUTO_TEST_CASE(AppendEndReject) |
| 248 | { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 249 | 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 | |
| 254 | BOOST_AUTO_TEST_CASE(EndAppend) |
| 255 | { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 256 | 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 | |
| 261 | BOOST_AUTO_TEST_CASE(EndEnd) |
| 262 | { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 263 | BOOST_CHECK_NO_THROW(context.end()); |
| 264 | BOOST_CHECK_THROW(context.end(), std::domain_error); |
| 265 | } |
| 266 | |
| 267 | BOOST_AUTO_TEST_CASE(EndReject) |
| 268 | { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 269 | BOOST_CHECK_NO_THROW(context.end()); |
| 270 | BOOST_CHECK_THROW(context.reject(), std::domain_error); |
| 271 | } |
| 272 | |
| 273 | BOOST_AUTO_TEST_CASE(RejectAppend) |
| 274 | { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 275 | BOOST_CHECK_NO_THROW(context.reject()); |
| 276 | BOOST_CHECK_THROW(context.append(Block("\x82\x01\x02", 3)), std::domain_error); |
| 277 | } |
| 278 | |
| 279 | BOOST_AUTO_TEST_CASE(RejectEnd) |
| 280 | { |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 281 | BOOST_CHECK_NO_THROW(context.reject()); |
| 282 | BOOST_CHECK_THROW(context.end(), std::domain_error); |
| 283 | } |
| 284 | |
| 285 | BOOST_AUTO_TEST_SUITE_END() // AbnormalState |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 286 | BOOST_AUTO_TEST_SUITE_END() // TestStatusDatasetContext |
| 287 | BOOST_AUTO_TEST_SUITE_END() // Mgmt |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 288 | |
| 289 | } // namespace tests |
| 290 | } // namespace mgmt |
| 291 | } // namespace ndn |