Junxiao Shi | 688a641 | 2017-06-22 10:12:07 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 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. |
| 10 | * |
| 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. |
| 14 | * |
| 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. |
| 20 | */ |
| 21 | |
| 22 | #include "delegation-list.hpp" |
| 23 | |
| 24 | #include "boost-test.hpp" |
| 25 | #include <boost/lexical_cast.hpp> |
| 26 | |
| 27 | namespace ndn { |
| 28 | namespace tests { |
| 29 | |
| 30 | BOOST_AUTO_TEST_SUITE(TestDelegationList) |
| 31 | |
| 32 | const uint8_t DEL1A[] = { |
| 33 | 0x1f, 0x08, // Delegation |
| 34 | 0x1e, 0x01, 0x01, // Preference=1 |
| 35 | 0x07, 0x03, 0x08, 0x01, 0x41 // Name=/A |
| 36 | }; |
| 37 | const uint8_t DEL1B[] = { |
| 38 | 0x1f, 0x08, // Delegation |
| 39 | 0x1e, 0x01, 0x01, // Preference=1 |
| 40 | 0x07, 0x03, 0x08, 0x01, 0x42 // Name=/B |
| 41 | }; |
| 42 | const uint8_t DEL2A[] = { |
| 43 | 0x1f, 0x08, // Delegation |
| 44 | 0x1e, 0x01, 0x02, // Preference=2 |
| 45 | 0x07, 0x03, 0x08, 0x01, 0x41 // Name=/A |
| 46 | }; |
| 47 | const uint8_t DEL2B[] = { |
| 48 | 0x1f, 0x08, // Delegation |
| 49 | 0x1e, 0x01, 0x02, // Preference=2 |
| 50 | 0x07, 0x03, 0x08, 0x01, 0x42 // Name=/B |
| 51 | }; |
| 52 | |
| 53 | Block |
| 54 | makeDelegationListBlock(uint32_t type, std::initializer_list<const uint8_t*> dels) |
| 55 | { |
| 56 | Block block(type); |
| 57 | for (const uint8_t* del : dels) { |
| 58 | block.push_back(Block(del, 2 + del[1])); |
| 59 | } |
| 60 | block.encode(); |
| 61 | return block; |
| 62 | } |
| 63 | |
| 64 | BOOST_AUTO_TEST_SUITE(Decode) |
| 65 | |
| 66 | BOOST_AUTO_TEST_CASE(DecodeUnsorted) |
| 67 | { |
| 68 | DelegationList dl(makeDelegationListBlock(tlv::ForwardingHint, {DEL2A, DEL2B, DEL1A}), false); |
| 69 | BOOST_CHECK_EQUAL(dl.size(), 3); |
| 70 | BOOST_CHECK_EQUAL(dl.at(0).preference, 2); |
| 71 | BOOST_CHECK_EQUAL(dl.at(0).name, "/A"); |
| 72 | BOOST_CHECK_EQUAL(dl.at(1).preference, 2); |
| 73 | BOOST_CHECK_EQUAL(dl.at(1).name, "/B"); |
| 74 | BOOST_CHECK_EQUAL(dl.at(2).preference, 1); |
| 75 | BOOST_CHECK_EQUAL(dl.at(2).name, "/A"); |
| 76 | } |
| 77 | |
| 78 | BOOST_AUTO_TEST_CASE(DecodeSorted) |
| 79 | { |
| 80 | DelegationList dl(makeDelegationListBlock(tlv::Content, {DEL2A, DEL2B, DEL1A})); |
| 81 | BOOST_CHECK_EQUAL(dl.size(), 3); |
| 82 | BOOST_CHECK_EQUAL(dl.at(0).preference, 1); |
| 83 | BOOST_CHECK_EQUAL(dl.at(0).name, "/A"); |
| 84 | BOOST_CHECK_EQUAL(dl.at(1).preference, 2); |
| 85 | BOOST_CHECK_EQUAL(dl.at(1).name, "/A"); |
| 86 | BOOST_CHECK_EQUAL(dl.at(2).preference, 2); |
| 87 | BOOST_CHECK_EQUAL(dl.at(2).name, "/B"); |
| 88 | } |
| 89 | |
| 90 | BOOST_AUTO_TEST_CASE(DecodeEmpty) |
| 91 | { |
| 92 | DelegationList dl; |
| 93 | Block block = makeDelegationListBlock(tlv::ForwardingHint, {}); |
| 94 | BOOST_CHECK_THROW(dl.wireDecode(block), DelegationList::Error); |
| 95 | } |
| 96 | |
| 97 | BOOST_AUTO_TEST_CASE(DecodeBadType) |
| 98 | { |
| 99 | DelegationList dl; |
| 100 | Block block = makeDelegationListBlock(tlv::Selectors, {DEL1A, DEL2B}); |
| 101 | BOOST_CHECK_THROW(dl.wireDecode(block), DelegationList::Error); |
| 102 | } |
| 103 | |
| 104 | BOOST_AUTO_TEST_CASE(DecodeNotDelegation) |
| 105 | { |
| 106 | const uint8_t BAD_DEL[] = { |
| 107 | 0x09, 0x00 // Selectors |
| 108 | }; |
| 109 | |
| 110 | DelegationList dl; |
| 111 | Block block = makeDelegationListBlock(tlv::ForwardingHint, {DEL1A, BAD_DEL}); |
| 112 | BOOST_CHECK_THROW(dl.wireDecode(block), DelegationList::Error); |
| 113 | } |
| 114 | |
| 115 | BOOST_AUTO_TEST_CASE(DecodeMissingPreference) |
| 116 | { |
| 117 | const uint8_t BAD_DEL[] = { |
| 118 | 0x1f, 0x05, // Delegation |
| 119 | 0x07, 0x03, 0x08, 0x01, 0x42 // Name=/B |
| 120 | }; |
| 121 | |
| 122 | DelegationList dl; |
| 123 | Block block = makeDelegationListBlock(tlv::ForwardingHint, {DEL1A, BAD_DEL}); |
| 124 | BOOST_CHECK_THROW(dl.wireDecode(block), DelegationList::Error); |
| 125 | } |
| 126 | |
| 127 | BOOST_AUTO_TEST_CASE(DecodeMissingName) |
| 128 | { |
| 129 | const uint8_t BAD_DEL[] = { |
| 130 | 0x1f, 0x03, // Delegation |
| 131 | 0x1e, 0x01, 0x02, // Preference=2 |
| 132 | }; |
| 133 | |
| 134 | DelegationList dl; |
| 135 | Block block = makeDelegationListBlock(tlv::ForwardingHint, {DEL1A, BAD_DEL}); |
| 136 | BOOST_CHECK_THROW(dl.wireDecode(block), DelegationList::Error); |
| 137 | } |
| 138 | |
| 139 | BOOST_AUTO_TEST_CASE(DecodeUnknownField) |
| 140 | { |
| 141 | const uint8_t BAD_DEL[] = { |
| 142 | 0x1f, 0x0a, // Delegation |
| 143 | 0x1e, 0x01, 0x02, // Preference=2 |
| 144 | 0x09, 0x00, // Selectors |
| 145 | 0x07, 0x03, 0x08, 0x01, 0x42 // Name=/B |
| 146 | }; |
| 147 | |
| 148 | DelegationList dl; |
| 149 | Block block = makeDelegationListBlock(tlv::ForwardingHint, {DEL1A, BAD_DEL}); |
| 150 | BOOST_CHECK_THROW(dl.wireDecode(block), DelegationList::Error); |
| 151 | } |
| 152 | |
| 153 | BOOST_AUTO_TEST_CASE(DecodeWrongOrder) |
| 154 | { |
| 155 | const uint8_t BAD_DEL[] = { |
| 156 | 0x1f, 0x08, // Delegation |
| 157 | 0x07, 0x03, 0x08, 0x01, 0x42, // Name=/B |
| 158 | 0x1e, 0x01, 0x02 // Preference=2 |
| 159 | }; |
| 160 | |
| 161 | DelegationList dl; |
| 162 | Block block = makeDelegationListBlock(tlv::ForwardingHint, {DEL1A, BAD_DEL}); |
| 163 | BOOST_CHECK_THROW(dl.wireDecode(block), DelegationList::Error); |
| 164 | } |
| 165 | |
| 166 | BOOST_AUTO_TEST_SUITE_END() // Decode |
| 167 | |
| 168 | BOOST_AUTO_TEST_SUITE(InsertEncode) |
| 169 | |
| 170 | BOOST_AUTO_TEST_CASE(InsertSimple) |
| 171 | { |
| 172 | DelegationList dl; |
| 173 | dl.insert(2, "/A"); |
| 174 | dl.insert(1, "/B"); |
| 175 | BOOST_CHECK_EQUAL(dl.size(), 2); |
| 176 | |
| 177 | EncodingBuffer encoder; |
| 178 | dl.wireEncode(encoder); |
| 179 | BOOST_CHECK(encoder.block() == makeDelegationListBlock(tlv::ForwardingHint, {DEL1B, DEL2A})); |
| 180 | } |
| 181 | |
| 182 | BOOST_AUTO_TEST_CASE(InsertReplace) |
| 183 | { |
| 184 | DelegationList dl; |
| 185 | dl.insert(2, "/A"); |
| 186 | dl.insert(Delegation{1, "/A"}, DelegationList::INS_REPLACE); |
| 187 | BOOST_CHECK_EQUAL(dl.size(), 1); |
| 188 | BOOST_CHECK_EQUAL(dl.at(0).preference, 1); |
| 189 | BOOST_CHECK_EQUAL(dl[0].name, "/A"); |
| 190 | |
| 191 | EncodingBuffer encoder; |
| 192 | dl.wireEncode(encoder); |
| 193 | BOOST_CHECK(encoder.block() == makeDelegationListBlock(tlv::ForwardingHint, {DEL1A})); |
| 194 | } |
| 195 | |
| 196 | BOOST_AUTO_TEST_CASE(InsertAppend) |
| 197 | { |
| 198 | DelegationList dl; |
| 199 | dl.insert(2, "/A"); |
| 200 | dl.insert(Delegation{1, "/A"}, DelegationList::INS_APPEND); |
| 201 | BOOST_CHECK_EQUAL(dl.size(), 2); |
| 202 | BOOST_CHECK_EQUAL(dl.at(0).preference, 1); |
| 203 | BOOST_CHECK_EQUAL(dl.at(1).preference, 2); |
| 204 | |
| 205 | EncodingBuffer encoder; |
| 206 | dl.wireEncode(encoder); |
| 207 | BOOST_CHECK(encoder.block() == makeDelegationListBlock(tlv::ForwardingHint, {DEL1A, DEL2A})); |
| 208 | } |
| 209 | |
| 210 | BOOST_AUTO_TEST_CASE(InsertSkip) |
| 211 | { |
| 212 | DelegationList dl; |
| 213 | dl.insert(2, "/A"); |
| 214 | dl.insert(Delegation{1, "/A"}, DelegationList::INS_SKIP); |
| 215 | BOOST_CHECK_EQUAL(dl.size(), 1); |
| 216 | BOOST_CHECK_EQUAL(dl.at(0).preference, 2); |
| 217 | |
| 218 | EncodingBuffer encoder; |
| 219 | dl.wireEncode(encoder); |
| 220 | BOOST_CHECK(encoder.block() == makeDelegationListBlock(tlv::ForwardingHint, {DEL2A})); |
| 221 | } |
| 222 | |
| 223 | BOOST_AUTO_TEST_CASE(Unsorted) |
| 224 | { |
| 225 | DelegationList dl(makeDelegationListBlock(tlv::ForwardingHint, {DEL2A}), false); |
| 226 | dl.insert(1, "/B"); |
| 227 | BOOST_CHECK_EQUAL(dl.size(), 2); |
| 228 | BOOST_CHECK_EQUAL(dl.at(0).preference, 2); |
| 229 | BOOST_CHECK_EQUAL(dl.at(0).name, "/A"); |
| 230 | BOOST_CHECK_EQUAL(dl.at(1).preference, 1); |
| 231 | BOOST_CHECK_EQUAL(dl.at(1).name, "/B"); |
| 232 | |
| 233 | EncodingBuffer encoder; |
| 234 | dl.wireEncode(encoder, tlv::Content); |
| 235 | BOOST_CHECK(encoder.block() == makeDelegationListBlock(tlv::Content, {DEL2A, DEL1B})); |
| 236 | } |
| 237 | |
| 238 | BOOST_AUTO_TEST_CASE(EncodeBadType) |
| 239 | { |
| 240 | DelegationList dl(makeDelegationListBlock(tlv::ForwardingHint, {DEL2A})); |
| 241 | EncodingBuffer encoder; |
| 242 | BOOST_CHECK_THROW(dl.wireEncode(encoder, tlv::Selectors), std::invalid_argument); |
| 243 | } |
| 244 | |
| 245 | BOOST_AUTO_TEST_CASE(EncodeEmpty) |
| 246 | { |
| 247 | DelegationList dl; |
| 248 | EncodingBuffer encoder; |
| 249 | BOOST_CHECK_THROW(dl.wireEncode(encoder), DelegationList::Error); |
| 250 | } |
| 251 | |
| 252 | BOOST_AUTO_TEST_SUITE_END() // InsertEncode |
| 253 | |
| 254 | BOOST_AUTO_TEST_SUITE(Erase) |
| 255 | |
| 256 | BOOST_AUTO_TEST_CASE(EraseNoop) |
| 257 | { |
| 258 | DelegationList dl; |
| 259 | dl.insert(1, "/A"); |
| 260 | BOOST_CHECK_EQUAL(dl.erase(2, "/A"), 0); |
| 261 | BOOST_CHECK_EQUAL(dl.erase(Delegation{1, "/B"}), 0); |
| 262 | BOOST_CHECK_EQUAL(dl.size(), 1); |
| 263 | BOOST_CHECK_EQUAL(dl.at(0).preference, 1); |
| 264 | BOOST_CHECK_EQUAL(dl.at(0).name, "/A"); |
| 265 | } |
| 266 | |
| 267 | BOOST_AUTO_TEST_CASE(EraseOne) |
| 268 | { |
| 269 | DelegationList dl; |
| 270 | dl.insert(1, "/A"); |
| 271 | BOOST_CHECK_EQUAL(dl.erase(1, "/A"), 1); |
| 272 | BOOST_CHECK_EQUAL(dl.size(), 0); |
| 273 | } |
| 274 | |
| 275 | BOOST_AUTO_TEST_CASE(EraseByName) |
| 276 | { |
| 277 | DelegationList dl; |
| 278 | dl.insert(1, "/A"); |
| 279 | dl.insert(2, "/A", DelegationList::INS_APPEND); |
| 280 | BOOST_CHECK_EQUAL(dl.size(), 2); |
| 281 | BOOST_CHECK_EQUAL(dl.erase("/A"), 2); |
| 282 | BOOST_CHECK_EQUAL(dl.size(), 0); |
| 283 | } |
| 284 | |
| 285 | BOOST_AUTO_TEST_SUITE_END() // Erase |
| 286 | |
| 287 | BOOST_AUTO_TEST_SUITE(Sort) |
| 288 | |
| 289 | BOOST_AUTO_TEST_CASE(Noop) |
| 290 | { |
| 291 | DelegationList dl(makeDelegationListBlock(tlv::ForwardingHint, {DEL1A})); |
| 292 | BOOST_CHECK_EQUAL(dl.isSorted(), true); |
| 293 | dl.sort(); |
| 294 | BOOST_CHECK_EQUAL(dl.isSorted(), true); |
| 295 | } |
| 296 | |
| 297 | BOOST_AUTO_TEST_CASE(Sort) |
| 298 | { |
| 299 | DelegationList dl(makeDelegationListBlock(tlv::ForwardingHint, {DEL2A, DEL2B, DEL1A}), false); |
| 300 | BOOST_CHECK_EQUAL(dl.isSorted(), false); |
| 301 | dl.sort(); |
| 302 | BOOST_CHECK_EQUAL(dl.isSorted(), true); |
| 303 | BOOST_CHECK_EQUAL(dl.size(), 3); |
| 304 | BOOST_CHECK_EQUAL(dl.at(0).preference, 1); |
| 305 | BOOST_CHECK_EQUAL(dl.at(0).name, "/A"); |
| 306 | BOOST_CHECK_EQUAL(dl.at(1).preference, 2); |
| 307 | BOOST_CHECK_EQUAL(dl.at(1).name, "/A"); |
| 308 | BOOST_CHECK_EQUAL(dl.at(2).preference, 2); |
| 309 | BOOST_CHECK_EQUAL(dl.at(2).name, "/B"); |
| 310 | } |
| 311 | |
| 312 | BOOST_AUTO_TEST_SUITE_END() // Sort |
| 313 | |
| 314 | BOOST_AUTO_TEST_SUITE(Compare) |
| 315 | |
| 316 | BOOST_AUTO_TEST_CASE(Empty) |
| 317 | { |
| 318 | DelegationList dl1, dl2; |
| 319 | BOOST_CHECK_EQUAL(dl1, dl2); |
| 320 | } |
| 321 | |
| 322 | BOOST_AUTO_TEST_CASE(SortedEqual) |
| 323 | { |
| 324 | DelegationList dl1(makeDelegationListBlock(tlv::ForwardingHint, {DEL2A, DEL1B})), |
| 325 | dl2(makeDelegationListBlock(tlv::Content, {DEL1B, DEL2A})); |
| 326 | BOOST_CHECK_EQUAL(dl1, dl2); |
| 327 | } |
| 328 | |
| 329 | BOOST_AUTO_TEST_CASE(SortedUnequal) |
| 330 | { |
| 331 | DelegationList dl1(makeDelegationListBlock(tlv::ForwardingHint, {DEL2A, DEL1B})), |
| 332 | dl2(makeDelegationListBlock(tlv::Content, {DEL1A, DEL2B})); |
| 333 | BOOST_CHECK_NE(dl1, dl2); |
| 334 | } |
| 335 | |
| 336 | BOOST_AUTO_TEST_CASE(UnsortedSameOrder) |
| 337 | { |
| 338 | DelegationList dl1(makeDelegationListBlock(tlv::ForwardingHint, {DEL2A, DEL1B}), false), |
| 339 | dl2(makeDelegationListBlock(tlv::Content, {DEL2A, DEL1B}), false); |
| 340 | BOOST_CHECK_EQUAL(dl1, dl2); |
| 341 | } |
| 342 | |
| 343 | BOOST_AUTO_TEST_CASE(UnsortedDifferentOrder) |
| 344 | { |
| 345 | DelegationList dl1(makeDelegationListBlock(tlv::ForwardingHint, {DEL2A, DEL1B}), false), |
| 346 | dl2(makeDelegationListBlock(tlv::Content, {DEL1B, DEL2A}), false); |
| 347 | BOOST_CHECK_NE(dl1, dl2); |
| 348 | } |
| 349 | |
| 350 | BOOST_AUTO_TEST_SUITE_END() // Compare |
| 351 | |
| 352 | BOOST_AUTO_TEST_SUITE(Print) |
| 353 | |
| 354 | BOOST_AUTO_TEST_CASE(PrintEmpty) |
| 355 | { |
| 356 | DelegationList dl; |
| 357 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(dl), "[]"); |
| 358 | } |
| 359 | |
| 360 | BOOST_AUTO_TEST_CASE(PrintNormal) |
| 361 | { |
| 362 | DelegationList dl(makeDelegationListBlock(tlv::ForwardingHint, {DEL2A, DEL1B})); |
| 363 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(dl), "[/B(1),/A(2)]"); |
| 364 | } |
| 365 | |
| 366 | BOOST_AUTO_TEST_SUITE_END() // Print |
| 367 | |
| 368 | BOOST_AUTO_TEST_SUITE_END() // TestDelegationList |
| 369 | |
| 370 | } // namespace tests |
| 371 | } // namespace ndn |