Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2015, Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | #include "mgmt/status-dataset-context.hpp" |
| 27 | #include "boost-test.hpp" |
| 28 | #include "unit-tests/make-interest-data.hpp" |
| 29 | |
| 30 | namespace ndn { |
| 31 | namespace mgmt { |
| 32 | namespace tests { |
| 33 | |
| 34 | class StatusDatasetContextFixture |
| 35 | { |
| 36 | public: |
| 37 | StatusDatasetContextFixture() |
| 38 | : interest(util::makeInterest("/test/context/interest")) |
| 39 | , contentBlock(makeStringBlock(tlv::Content, "/test/data/content")) |
| 40 | , context(*interest, bind(&StatusDatasetContextFixture::sendData, this, _1, _2, _3)) |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | void |
| 45 | sendData(const Name& dataName, const Block& content, const MetaInfo& info) |
| 46 | { |
| 47 | sentData.push_back(Data(dataName).setContent(content).setMetaInfo(info)); |
| 48 | } |
| 49 | |
| 50 | Block |
| 51 | concatenate() |
| 52 | { |
| 53 | EncodingBuffer encoder; |
| 54 | size_t valueLength = 0; |
| 55 | for (auto data : sentData) { |
| 56 | valueLength += encoder.appendByteArray(data.getContent().value(), |
| 57 | data.getContent().value_size()); |
| 58 | } |
| 59 | encoder.prependVarNumber(valueLength); |
| 60 | encoder.prependVarNumber(tlv::Content); |
| 61 | return encoder.block(); |
| 62 | } |
| 63 | |
| 64 | public: |
| 65 | std::vector<Data> sentData; |
| 66 | shared_ptr<Interest> interest; |
| 67 | Block contentBlock; |
| 68 | mgmt::StatusDatasetContext context; |
| 69 | }; |
| 70 | |
| 71 | BOOST_FIXTURE_TEST_SUITE(MgmtStatusDatasetContext, StatusDatasetContextFixture) |
| 72 | |
| 73 | BOOST_AUTO_TEST_CASE(GetPrefix) |
| 74 | { |
| 75 | Name dataName = context.getPrefix(); |
| 76 | BOOST_CHECK(dataName[-1].isVersion()); |
| 77 | BOOST_CHECK(dataName.getPrefix(-1) == interest->getName()); |
| 78 | } |
| 79 | |
| 80 | BOOST_AUTO_TEST_SUITE(SetPrefix) |
| 81 | |
| 82 | BOOST_AUTO_TEST_CASE(Valid) |
| 83 | { |
| 84 | Name validPrefix = Name(interest->getName()).append("/valid"); |
| 85 | BOOST_CHECK_NO_THROW(context.setPrefix(validPrefix)); |
| 86 | BOOST_CHECK(context.getPrefix()[-1].isVersion()); |
| 87 | BOOST_CHECK(context.getPrefix().getPrefix(-1) == validPrefix); |
| 88 | } |
| 89 | |
| 90 | BOOST_AUTO_TEST_CASE(Invalid) |
| 91 | { |
| 92 | Name invalidPrefix = Name(interest->getName()).getPrefix(-1).append("/invalid"); |
| 93 | BOOST_CHECK_THROW(context.setPrefix(invalidPrefix), std::invalid_argument); |
| 94 | } |
| 95 | |
| 96 | BOOST_AUTO_TEST_CASE(ValidWithAppendCalled) |
| 97 | { |
| 98 | Name validPrefix = Name(interest->getName()).append("/valid"); |
| 99 | context.append(contentBlock); |
| 100 | BOOST_CHECK_THROW(context.setPrefix(validPrefix), std::domain_error); |
| 101 | } |
| 102 | |
| 103 | BOOST_AUTO_TEST_CASE(ValidWithEndCalled) |
| 104 | { |
| 105 | Name validPrefix = Name(interest->getName()).append("/valid"); |
| 106 | context.end(); |
| 107 | BOOST_CHECK_THROW(context.setPrefix(validPrefix), std::domain_error); |
| 108 | } |
| 109 | |
| 110 | BOOST_AUTO_TEST_CASE(ValidWithRejectCalled) |
| 111 | { |
| 112 | Name validPrefix = Name(interest->getName()).append("/valid"); |
| 113 | context.reject(); |
| 114 | BOOST_CHECK_THROW(context.setPrefix(validPrefix), std::domain_error); |
| 115 | } |
| 116 | |
| 117 | BOOST_AUTO_TEST_SUITE_END() // SetPrefix |
| 118 | |
| 119 | BOOST_AUTO_TEST_CASE(Expiry) |
| 120 | { |
| 121 | // getExpiry & setExpiry |
| 122 | auto period = time::milliseconds(9527); |
| 123 | BOOST_CHECK_EQUAL(context.getExpiry(), time::milliseconds(1000)); |
| 124 | BOOST_CHECK_EQUAL(context.setExpiry(period).getExpiry(), period); |
| 125 | } |
| 126 | |
| 127 | BOOST_AUTO_TEST_CASE(Respond) |
| 128 | { |
| 129 | BOOST_CHECK_NO_THROW(context.append(contentBlock)); |
| 130 | BOOST_CHECK(sentData.empty()); // does not call end yet |
| 131 | |
| 132 | BOOST_CHECK_NO_THROW(context.end()); |
| 133 | BOOST_CHECK_EQUAL(sentData.size(), 1); |
| 134 | BOOST_CHECK_EQUAL(sentData[0].getName()[-1].toSegment(), 0); |
| 135 | BOOST_CHECK_EQUAL(sentData[0].getName().getPrefix(-1), context.getPrefix()); |
| 136 | BOOST_CHECK_EQUAL(sentData[0].getFinalBlockId().toSegment(), 0); |
| 137 | BOOST_CHECK(sentData[0].getContent().blockFromValue() == contentBlock); |
| 138 | } |
| 139 | |
| 140 | BOOST_AUTO_TEST_CASE(RespondLarge) |
| 141 | { |
| 142 | static Block largeBlock = [] () -> Block { |
| 143 | EncodingBuffer encoder; |
| 144 | size_t maxBlockSize = MAX_NDN_PACKET_SIZE >> 1; |
| 145 | for (size_t i = 0; i < maxBlockSize; ++i) { |
| 146 | encoder.prependByte(1); |
| 147 | } |
| 148 | encoder.prependVarNumber(maxBlockSize); |
| 149 | encoder.prependVarNumber(tlv::Content); |
| 150 | return encoder.block(); |
| 151 | }(); |
| 152 | |
| 153 | BOOST_CHECK_NO_THROW(context.append(largeBlock)); |
| 154 | BOOST_CHECK_NO_THROW(context.end()); |
| 155 | BOOST_CHECK_EQUAL(sentData.size(), 2); |
| 156 | BOOST_CHECK_EQUAL(sentData[0].getName()[-1].toSegment(), 0); |
| 157 | BOOST_CHECK_EQUAL(sentData[0].getName().getPrefix(-1), context.getPrefix()); |
| 158 | BOOST_CHECK_EQUAL(sentData[1].getName()[-1].toSegment(), 1); |
| 159 | BOOST_CHECK_EQUAL(sentData[1].getName().getPrefix(-1), context.getPrefix()); |
| 160 | BOOST_CHECK_EQUAL(sentData[1].getFinalBlockId().toSegment(), 1); |
| 161 | |
| 162 | auto contentLargeBlock = concatenate(); |
| 163 | BOOST_CHECK_NO_THROW(contentLargeBlock.parse()); |
| 164 | BOOST_CHECK_EQUAL(contentLargeBlock.elements().size(), 1); |
| 165 | BOOST_CHECK(contentLargeBlock.elements()[0] == largeBlock); |
| 166 | } |
| 167 | |
| 168 | BOOST_AUTO_TEST_CASE(ResponseMultipleSmall) |
| 169 | { |
| 170 | size_t nBlocks = 100; |
| 171 | for (size_t i = 0 ; i < nBlocks ; i ++) { |
| 172 | BOOST_CHECK_NO_THROW(context.append(contentBlock)); |
| 173 | } |
| 174 | BOOST_CHECK_NO_THROW(context.end()); |
| 175 | BOOST_CHECK_EQUAL(sentData.size(), 1); |
| 176 | BOOST_CHECK_EQUAL(sentData[0].getName()[-1].toSegment(), 0); |
| 177 | BOOST_CHECK_EQUAL(sentData[0].getName().getPrefix(-1), context.getPrefix()); |
| 178 | BOOST_CHECK_EQUAL(sentData[0].getFinalBlockId().toSegment(), 0); |
| 179 | |
| 180 | auto contentMultiBlocks = concatenate(); |
| 181 | BOOST_CHECK_NO_THROW(contentMultiBlocks.parse()); |
| 182 | BOOST_CHECK_EQUAL(contentMultiBlocks.elements().size(), nBlocks); |
| 183 | for (auto&& element : contentMultiBlocks.elements()) { |
| 184 | BOOST_CHECK(element == contentBlock); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | BOOST_AUTO_TEST_CASE(Reject) |
| 189 | { |
| 190 | BOOST_CHECK_NO_THROW(context.reject()); |
| 191 | BOOST_CHECK_EQUAL(sentData.size(), 1); |
| 192 | BOOST_CHECK(sentData[0].getContentType() == tlv::ContentType_Nack); |
| 193 | BOOST_CHECK_EQUAL(ControlResponse(sentData[0].getContent().blockFromValue()).getCode(), 400); |
| 194 | } |
| 195 | |
| 196 | BOOST_AUTO_TEST_SUITE(AbnormalState) |
| 197 | |
| 198 | BOOST_AUTO_TEST_CASE(AppendReject) |
| 199 | { |
| 200 | mgmt::StatusDatasetContext context(Interest("/abnormal-state"), bind([]{})); |
| 201 | BOOST_CHECK_NO_THROW(context.append(Block("\x82\x01\x02", 3))); |
| 202 | BOOST_CHECK_THROW(context.reject(), std::domain_error); |
| 203 | } |
| 204 | |
| 205 | BOOST_AUTO_TEST_CASE(AppendEndReject) |
| 206 | { |
| 207 | mgmt::StatusDatasetContext context(Interest("/abnormal-state"), bind([]{})); |
| 208 | BOOST_CHECK_NO_THROW(context.append(Block("\x82\x01\x02", 3))); |
| 209 | BOOST_CHECK_NO_THROW(context.end()); |
| 210 | BOOST_CHECK_THROW(context.reject(), std::domain_error); |
| 211 | } |
| 212 | |
| 213 | BOOST_AUTO_TEST_CASE(EndAppend) |
| 214 | { |
| 215 | mgmt::StatusDatasetContext context (Interest("/abnormal-state"), bind([]{})); |
| 216 | BOOST_CHECK_NO_THROW(context.end()); |
| 217 | // end, append -> error |
| 218 | BOOST_CHECK_THROW(context.append(Block("\x82\x01\x02", 3)), std::domain_error); |
| 219 | } |
| 220 | |
| 221 | BOOST_AUTO_TEST_CASE(EndEnd) |
| 222 | { |
| 223 | mgmt::StatusDatasetContext context(Interest("/abnormal-state"), bind([]{})); |
| 224 | BOOST_CHECK_NO_THROW(context.end()); |
| 225 | BOOST_CHECK_THROW(context.end(), std::domain_error); |
| 226 | } |
| 227 | |
| 228 | BOOST_AUTO_TEST_CASE(EndReject) |
| 229 | { |
| 230 | mgmt::StatusDatasetContext context(Interest("/abnormal-state"), bind([]{})); |
| 231 | BOOST_CHECK_NO_THROW(context.end()); |
| 232 | BOOST_CHECK_THROW(context.reject(), std::domain_error); |
| 233 | } |
| 234 | |
| 235 | BOOST_AUTO_TEST_CASE(RejectAppend) |
| 236 | { |
| 237 | mgmt::StatusDatasetContext context(Interest("/abnormal-state"), bind([]{})); |
| 238 | BOOST_CHECK_NO_THROW(context.reject()); |
| 239 | BOOST_CHECK_THROW(context.append(Block("\x82\x01\x02", 3)), std::domain_error); |
| 240 | } |
| 241 | |
| 242 | BOOST_AUTO_TEST_CASE(RejectEnd) |
| 243 | { |
| 244 | mgmt::StatusDatasetContext context(Interest("/abnormal-state"), bind([]{})); |
| 245 | BOOST_CHECK_NO_THROW(context.reject()); |
| 246 | BOOST_CHECK_THROW(context.end(), std::domain_error); |
| 247 | } |
| 248 | |
| 249 | BOOST_AUTO_TEST_SUITE_END() // AbnormalState |
| 250 | |
| 251 | |
| 252 | BOOST_AUTO_TEST_SUITE_END() |
| 253 | |
| 254 | } // namespace tests |
| 255 | } // namespace mgmt |
| 256 | } // namespace ndn |