Junxiao Shi | 688a641 | 2017-06-22 10:12:07 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | bb8d4a3 | 2017-07-13 02:40:09 +0000 | [diff] [blame] | 2 | /* |
Junxiao Shi | 688a641 | 2017-06-22 10:12:07 +0000 | [diff] [blame] | 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; |
Junxiao Shi | bb8d4a3 | 2017-07-13 02:40:09 +0000 | [diff] [blame] | 173 | BOOST_CHECK_EQUAL(dl.empty(), true); |
Junxiao Shi | 688a641 | 2017-06-22 10:12:07 +0000 | [diff] [blame] | 174 | dl.insert(2, "/A"); |
Junxiao Shi | bb8d4a3 | 2017-07-13 02:40:09 +0000 | [diff] [blame] | 175 | BOOST_CHECK_EQUAL(dl.empty(), false); |
Junxiao Shi | 688a641 | 2017-06-22 10:12:07 +0000 | [diff] [blame] | 176 | dl.insert(1, "/B"); |
| 177 | BOOST_CHECK_EQUAL(dl.size(), 2); |
| 178 | |
| 179 | EncodingBuffer encoder; |
| 180 | dl.wireEncode(encoder); |
| 181 | BOOST_CHECK(encoder.block() == makeDelegationListBlock(tlv::ForwardingHint, {DEL1B, DEL2A})); |
| 182 | } |
| 183 | |
| 184 | BOOST_AUTO_TEST_CASE(InsertReplace) |
| 185 | { |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 186 | DelegationList dl({{2, "/A"}}); |
Junxiao Shi | 688a641 | 2017-06-22 10:12:07 +0000 | [diff] [blame] | 187 | dl.insert(Delegation{1, "/A"}, DelegationList::INS_REPLACE); |
| 188 | BOOST_CHECK_EQUAL(dl.size(), 1); |
| 189 | BOOST_CHECK_EQUAL(dl.at(0).preference, 1); |
| 190 | BOOST_CHECK_EQUAL(dl[0].name, "/A"); |
| 191 | |
| 192 | EncodingBuffer encoder; |
| 193 | dl.wireEncode(encoder); |
| 194 | BOOST_CHECK(encoder.block() == makeDelegationListBlock(tlv::ForwardingHint, {DEL1A})); |
| 195 | } |
| 196 | |
| 197 | BOOST_AUTO_TEST_CASE(InsertAppend) |
| 198 | { |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 199 | DelegationList dl({{2, "/A"}}); |
Junxiao Shi | 688a641 | 2017-06-22 10:12:07 +0000 | [diff] [blame] | 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 | { |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 212 | DelegationList dl({{2, "/A"}}); |
Junxiao Shi | 688a641 | 2017-06-22 10:12:07 +0000 | [diff] [blame] | 213 | dl.insert(Delegation{1, "/A"}, DelegationList::INS_SKIP); |
| 214 | BOOST_CHECK_EQUAL(dl.size(), 1); |
| 215 | BOOST_CHECK_EQUAL(dl.at(0).preference, 2); |
| 216 | |
| 217 | EncodingBuffer encoder; |
| 218 | dl.wireEncode(encoder); |
| 219 | BOOST_CHECK(encoder.block() == makeDelegationListBlock(tlv::ForwardingHint, {DEL2A})); |
| 220 | } |
| 221 | |
| 222 | BOOST_AUTO_TEST_CASE(Unsorted) |
| 223 | { |
| 224 | DelegationList dl(makeDelegationListBlock(tlv::ForwardingHint, {DEL2A}), false); |
| 225 | dl.insert(1, "/B"); |
| 226 | BOOST_CHECK_EQUAL(dl.size(), 2); |
| 227 | BOOST_CHECK_EQUAL(dl.at(0).preference, 2); |
| 228 | BOOST_CHECK_EQUAL(dl.at(0).name, "/A"); |
| 229 | BOOST_CHECK_EQUAL(dl.at(1).preference, 1); |
| 230 | BOOST_CHECK_EQUAL(dl.at(1).name, "/B"); |
| 231 | |
| 232 | EncodingBuffer encoder; |
| 233 | dl.wireEncode(encoder, tlv::Content); |
| 234 | BOOST_CHECK(encoder.block() == makeDelegationListBlock(tlv::Content, {DEL2A, DEL1B})); |
| 235 | } |
| 236 | |
| 237 | BOOST_AUTO_TEST_CASE(EncodeBadType) |
| 238 | { |
| 239 | DelegationList dl(makeDelegationListBlock(tlv::ForwardingHint, {DEL2A})); |
| 240 | EncodingBuffer encoder; |
| 241 | BOOST_CHECK_THROW(dl.wireEncode(encoder, tlv::Selectors), std::invalid_argument); |
| 242 | } |
| 243 | |
| 244 | BOOST_AUTO_TEST_CASE(EncodeEmpty) |
| 245 | { |
| 246 | DelegationList dl; |
| 247 | EncodingBuffer encoder; |
| 248 | BOOST_CHECK_THROW(dl.wireEncode(encoder), DelegationList::Error); |
| 249 | } |
| 250 | |
| 251 | BOOST_AUTO_TEST_SUITE_END() // InsertEncode |
| 252 | |
| 253 | BOOST_AUTO_TEST_SUITE(Erase) |
| 254 | |
| 255 | BOOST_AUTO_TEST_CASE(EraseNoop) |
| 256 | { |
| 257 | DelegationList dl; |
| 258 | dl.insert(1, "/A"); |
| 259 | BOOST_CHECK_EQUAL(dl.erase(2, "/A"), 0); |
| 260 | BOOST_CHECK_EQUAL(dl.erase(Delegation{1, "/B"}), 0); |
| 261 | BOOST_CHECK_EQUAL(dl.size(), 1); |
| 262 | BOOST_CHECK_EQUAL(dl.at(0).preference, 1); |
| 263 | BOOST_CHECK_EQUAL(dl.at(0).name, "/A"); |
| 264 | } |
| 265 | |
| 266 | BOOST_AUTO_TEST_CASE(EraseOne) |
| 267 | { |
| 268 | DelegationList dl; |
| 269 | dl.insert(1, "/A"); |
| 270 | BOOST_CHECK_EQUAL(dl.erase(1, "/A"), 1); |
| 271 | BOOST_CHECK_EQUAL(dl.size(), 0); |
| 272 | } |
| 273 | |
| 274 | BOOST_AUTO_TEST_CASE(EraseByName) |
| 275 | { |
| 276 | DelegationList dl; |
| 277 | dl.insert(1, "/A"); |
| 278 | dl.insert(2, "/A", DelegationList::INS_APPEND); |
| 279 | BOOST_CHECK_EQUAL(dl.size(), 2); |
| 280 | BOOST_CHECK_EQUAL(dl.erase("/A"), 2); |
| 281 | BOOST_CHECK_EQUAL(dl.size(), 0); |
| 282 | } |
| 283 | |
| 284 | BOOST_AUTO_TEST_SUITE_END() // Erase |
| 285 | |
| 286 | BOOST_AUTO_TEST_SUITE(Sort) |
| 287 | |
| 288 | BOOST_AUTO_TEST_CASE(Noop) |
| 289 | { |
| 290 | DelegationList dl(makeDelegationListBlock(tlv::ForwardingHint, {DEL1A})); |
| 291 | BOOST_CHECK_EQUAL(dl.isSorted(), true); |
| 292 | dl.sort(); |
| 293 | BOOST_CHECK_EQUAL(dl.isSorted(), true); |
| 294 | } |
| 295 | |
| 296 | BOOST_AUTO_TEST_CASE(Sort) |
| 297 | { |
| 298 | DelegationList dl(makeDelegationListBlock(tlv::ForwardingHint, {DEL2A, DEL2B, DEL1A}), false); |
| 299 | BOOST_CHECK_EQUAL(dl.isSorted(), false); |
| 300 | dl.sort(); |
| 301 | BOOST_CHECK_EQUAL(dl.isSorted(), true); |
| 302 | BOOST_CHECK_EQUAL(dl.size(), 3); |
| 303 | BOOST_CHECK_EQUAL(dl.at(0).preference, 1); |
| 304 | BOOST_CHECK_EQUAL(dl.at(0).name, "/A"); |
| 305 | BOOST_CHECK_EQUAL(dl.at(1).preference, 2); |
| 306 | BOOST_CHECK_EQUAL(dl.at(1).name, "/A"); |
| 307 | BOOST_CHECK_EQUAL(dl.at(2).preference, 2); |
| 308 | BOOST_CHECK_EQUAL(dl.at(2).name, "/B"); |
| 309 | } |
| 310 | |
| 311 | BOOST_AUTO_TEST_SUITE_END() // Sort |
| 312 | |
| 313 | BOOST_AUTO_TEST_SUITE(Compare) |
| 314 | |
| 315 | BOOST_AUTO_TEST_CASE(Empty) |
| 316 | { |
| 317 | DelegationList dl1, dl2; |
| 318 | BOOST_CHECK_EQUAL(dl1, dl2); |
| 319 | } |
| 320 | |
| 321 | BOOST_AUTO_TEST_CASE(SortedEqual) |
| 322 | { |
| 323 | DelegationList dl1(makeDelegationListBlock(tlv::ForwardingHint, {DEL2A, DEL1B})), |
| 324 | dl2(makeDelegationListBlock(tlv::Content, {DEL1B, DEL2A})); |
| 325 | BOOST_CHECK_EQUAL(dl1, dl2); |
| 326 | } |
| 327 | |
| 328 | BOOST_AUTO_TEST_CASE(SortedUnequal) |
| 329 | { |
| 330 | DelegationList dl1(makeDelegationListBlock(tlv::ForwardingHint, {DEL2A, DEL1B})), |
| 331 | dl2(makeDelegationListBlock(tlv::Content, {DEL1A, DEL2B})); |
| 332 | BOOST_CHECK_NE(dl1, dl2); |
| 333 | } |
| 334 | |
| 335 | BOOST_AUTO_TEST_CASE(UnsortedSameOrder) |
| 336 | { |
| 337 | DelegationList dl1(makeDelegationListBlock(tlv::ForwardingHint, {DEL2A, DEL1B}), false), |
| 338 | dl2(makeDelegationListBlock(tlv::Content, {DEL2A, DEL1B}), false); |
| 339 | BOOST_CHECK_EQUAL(dl1, dl2); |
| 340 | } |
| 341 | |
| 342 | BOOST_AUTO_TEST_CASE(UnsortedDifferentOrder) |
| 343 | { |
| 344 | DelegationList dl1(makeDelegationListBlock(tlv::ForwardingHint, {DEL2A, DEL1B}), false), |
| 345 | dl2(makeDelegationListBlock(tlv::Content, {DEL1B, DEL2A}), false); |
| 346 | BOOST_CHECK_NE(dl1, dl2); |
| 347 | } |
| 348 | |
| 349 | BOOST_AUTO_TEST_SUITE_END() // Compare |
| 350 | |
| 351 | BOOST_AUTO_TEST_SUITE(Print) |
| 352 | |
| 353 | BOOST_AUTO_TEST_CASE(PrintEmpty) |
| 354 | { |
| 355 | DelegationList dl; |
| 356 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(dl), "[]"); |
| 357 | } |
| 358 | |
| 359 | BOOST_AUTO_TEST_CASE(PrintNormal) |
| 360 | { |
| 361 | DelegationList dl(makeDelegationListBlock(tlv::ForwardingHint, {DEL2A, DEL1B})); |
| 362 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(dl), "[/B(1),/A(2)]"); |
| 363 | } |
| 364 | |
| 365 | BOOST_AUTO_TEST_SUITE_END() // Print |
| 366 | |
| 367 | BOOST_AUTO_TEST_SUITE_END() // TestDelegationList |
| 368 | |
| 369 | } // namespace tests |
| 370 | } // namespace ndn |