Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 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 "name-component.hpp" |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 23 | #include "name.hpp" |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 24 | |
| 25 | #include "boost-test.hpp" |
| 26 | #include <boost/mpl/vector.hpp> |
| 27 | |
| 28 | namespace ndn { |
| 29 | namespace tests { |
| 30 | |
| 31 | BOOST_AUTO_TEST_SUITE(TestNameComponent) |
| 32 | |
| 33 | static const uint8_t NAME_COMPONENT_WIRE[] = { |
| 34 | 0x08, 0x03, // NameComponent |
| 35 | 0x6e, 0x64, 0x6e}; |
| 36 | |
| 37 | static const uint8_t NAME_COMPONENT2_WIRE[] = { |
| 38 | 0x08, 0x20, // ImplicitSha256DigestComponent |
| 39 | 0x28, 0xba, 0xd4, 0xb5, 0x27, 0x5b, 0xd3, 0x92, |
| 40 | 0xdb, 0xb6, 0x70, 0xc7, 0x5c, 0xf0, 0xb6, 0x6f, |
| 41 | 0x13, 0xf7, 0x94, 0x2b, 0x21, 0xe8, 0x0f, 0x55, |
| 42 | 0xc0, 0xe8, 0x6b, 0x37, 0x47, 0x53, 0xa5, 0x49 }; |
| 43 | |
| 44 | static const uint8_t DIGEST_COMPONENT_WIRE[] = { |
| 45 | 0x01, 0x20, // ImplicitSha256DigestComponent |
| 46 | 0x28, 0xba, 0xd4, 0xb5, 0x27, 0x5b, 0xd3, 0x92, |
| 47 | 0xdb, 0xb6, 0x70, 0xc7, 0x5c, 0xf0, 0xb6, 0x6f, |
| 48 | 0x13, 0xf7, 0x94, 0x2b, 0x21, 0xe8, 0x0f, 0x55, |
| 49 | 0xc0, 0xe8, 0x6b, 0x37, 0x47, 0x53, 0xa5, 0x48 }; |
| 50 | |
| 51 | static const uint8_t DIGEST_COMPONENT2_WIRE[] = { |
| 52 | 0x01, 0x20, // ImplicitSha256DigestComponent |
| 53 | 0x28, 0xba, 0xd4, 0xb5, 0x27, 0x5b, 0xd3, 0x92, |
| 54 | 0xdb, 0xb6, 0x70, 0xc7, 0x5c, 0xf0, 0xb6, 0x6f, |
| 55 | 0x13, 0xf7, 0x94, 0x2b, 0x21, 0xe8, 0x0f, 0x55, |
| 56 | 0xc0, 0xe8, 0x6b, 0x37, 0x47, 0x53, 0xa5, 0x49 }; |
| 57 | |
| 58 | static const uint8_t INVALID_COMPONENT_WIRE[] = { |
| 59 | 0x07, 0x03, // unknown component type |
| 60 | 0x6e, 0x64, 0x6e}; |
| 61 | |
| 62 | BOOST_AUTO_TEST_SUITE(Decode) |
| 63 | |
| 64 | BOOST_AUTO_TEST_CASE(Generic) |
| 65 | { |
| 66 | Block block(NAME_COMPONENT_WIRE, sizeof(NAME_COMPONENT_WIRE)); |
| 67 | name::Component comp; |
| 68 | BOOST_REQUIRE_NO_THROW(comp.wireDecode(block)); |
| 69 | BOOST_CHECK_EQUAL(comp.toUri(), "ndn"); |
| 70 | } |
| 71 | |
| 72 | BOOST_AUTO_TEST_CASE(Digest) |
| 73 | { |
| 74 | Block block(DIGEST_COMPONENT_WIRE, sizeof(DIGEST_COMPONENT_WIRE)); |
| 75 | name::Component comp; |
| 76 | BOOST_REQUIRE_NO_THROW(comp.wireDecode(block)); |
| 77 | BOOST_REQUIRE_EQUAL(comp.toUri(), "sha256digest=28bad4b5275bd392dbb670c75cf0b66f" |
| 78 | "13f7942b21e80f55c0e86b374753a548"); |
| 79 | } |
| 80 | |
| 81 | BOOST_AUTO_TEST_CASE(Invalid) |
| 82 | { |
| 83 | Block block(INVALID_COMPONENT_WIRE, sizeof(INVALID_COMPONENT_WIRE)); |
| 84 | name::Component comp; |
| 85 | BOOST_REQUIRE_THROW(comp.wireDecode(block), name::Component::Error); |
| 86 | } |
| 87 | |
| 88 | BOOST_AUTO_TEST_SUITE_END() // Decode |
| 89 | |
| 90 | BOOST_AUTO_TEST_SUITE(Compare) |
| 91 | |
| 92 | BOOST_AUTO_TEST_CASE(Generic) |
| 93 | { |
Davide Pesavento | e245b05 | 2017-10-31 13:00:44 -0400 | [diff] [blame^] | 94 | name::Component empty; |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 95 | name::Component compD("D"); |
| 96 | name::Component compD2("D"); |
| 97 | name::Component compF("F"); |
| 98 | name::Component compAA("AA"); |
| 99 | |
Davide Pesavento | e245b05 | 2017-10-31 13:00:44 -0400 | [diff] [blame^] | 100 | BOOST_CHECK_EQUAL(empty == empty, true); |
| 101 | BOOST_CHECK_EQUAL(empty != empty, false); |
| 102 | BOOST_CHECK_EQUAL(empty < empty, false); |
| 103 | BOOST_CHECK_EQUAL(empty <= empty, true); |
| 104 | BOOST_CHECK_EQUAL(empty == compD, false); |
| 105 | BOOST_CHECK_EQUAL(empty < compD, true); |
| 106 | |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 107 | BOOST_CHECK_EQUAL(compD == compD2, true); |
| 108 | BOOST_CHECK_EQUAL(compD != compD2, false); |
| 109 | BOOST_CHECK_EQUAL(compD < compD2, false); |
| 110 | BOOST_CHECK_EQUAL(compD <= compD2, true); |
| 111 | BOOST_CHECK_EQUAL(compD > compD2, false); |
| 112 | BOOST_CHECK_EQUAL(compD >= compD2, true); |
| 113 | |
| 114 | BOOST_CHECK_EQUAL(compD == compF, false); |
| 115 | BOOST_CHECK_EQUAL(compD != compF, true); |
| 116 | BOOST_CHECK_EQUAL(compD < compF, true); |
| 117 | BOOST_CHECK_EQUAL(compD <= compF, true); |
| 118 | BOOST_CHECK_EQUAL(compD > compF, false); |
| 119 | BOOST_CHECK_EQUAL(compD >= compF, false); |
| 120 | |
| 121 | BOOST_CHECK_EQUAL(compF == compAA, false); |
| 122 | BOOST_CHECK_EQUAL(compF != compAA, true); |
| 123 | BOOST_CHECK_EQUAL(compF < compAA, true); |
| 124 | BOOST_CHECK_EQUAL(compF <= compAA, true); |
| 125 | BOOST_CHECK_EQUAL(compF > compAA, false); |
| 126 | BOOST_CHECK_EQUAL(compF >= compAA, false); |
| 127 | } |
| 128 | |
| 129 | BOOST_AUTO_TEST_CASE(ZeroLength) |
| 130 | { |
| 131 | name::Component comp0(""); |
| 132 | BOOST_REQUIRE_EQUAL(comp0.value_size(), 0); |
| 133 | |
| 134 | BOOST_CHECK_EQUAL(comp0, comp0); |
| 135 | BOOST_CHECK_EQUAL(comp0, name::Component("")); |
| 136 | BOOST_CHECK_LT(comp0, name::Component("A")); |
| 137 | BOOST_CHECK_LE(comp0, name::Component("A")); |
| 138 | BOOST_CHECK_NE(comp0, name::Component("A")); |
| 139 | BOOST_CHECK_GT(name::Component("A"), comp0); |
| 140 | BOOST_CHECK_GE(name::Component("A"), comp0); |
| 141 | } |
| 142 | |
| 143 | BOOST_AUTO_TEST_CASE(Digest) |
| 144 | { |
| 145 | name::Component digest1(Block(DIGEST_COMPONENT_WIRE, sizeof(DIGEST_COMPONENT_WIRE))); |
| 146 | name::Component digest1b(Block(DIGEST_COMPONENT_WIRE, sizeof(DIGEST_COMPONENT_WIRE))); |
| 147 | name::Component digest2(Block(DIGEST_COMPONENT2_WIRE, sizeof(DIGEST_COMPONENT2_WIRE))); |
| 148 | name::Component generic0; |
| 149 | name::Component generic2(Block(NAME_COMPONENT2_WIRE, sizeof(NAME_COMPONENT2_WIRE))); |
| 150 | |
| 151 | BOOST_CHECK_EQUAL(digest1 == digest1b, true); |
| 152 | BOOST_CHECK_EQUAL(digest1 != digest1b, false); |
| 153 | BOOST_CHECK_EQUAL(digest1 < digest1b, false); |
| 154 | BOOST_CHECK_EQUAL(digest1 <= digest1b, true); |
| 155 | BOOST_CHECK_EQUAL(digest1 > digest1b, false); |
| 156 | BOOST_CHECK_EQUAL(digest1 >= digest1b, true); |
| 157 | |
| 158 | BOOST_CHECK_EQUAL(digest1 == digest2, false); |
| 159 | BOOST_CHECK_EQUAL(digest1 != digest2, true); |
| 160 | BOOST_CHECK_EQUAL(digest1 < digest2, true); |
| 161 | BOOST_CHECK_EQUAL(digest1 <= digest2, true); |
| 162 | BOOST_CHECK_EQUAL(digest1 > digest2, false); |
| 163 | BOOST_CHECK_EQUAL(digest1 >= digest2, false); |
| 164 | |
| 165 | BOOST_CHECK_EQUAL(digest1 == generic0, false); |
| 166 | BOOST_CHECK_EQUAL(digest1 != generic0, true); |
| 167 | BOOST_CHECK_EQUAL(digest1 < generic0, true); |
| 168 | BOOST_CHECK_EQUAL(digest1 <= generic0, true); |
| 169 | BOOST_CHECK_EQUAL(digest1 > generic0, false); |
| 170 | BOOST_CHECK_EQUAL(digest1 >= generic0, false); |
| 171 | |
| 172 | BOOST_CHECK_EQUAL(digest2 == generic2, false); |
| 173 | BOOST_CHECK_EQUAL(digest2 != generic2, true); |
| 174 | BOOST_CHECK_EQUAL(digest2 < generic2, true); |
| 175 | BOOST_CHECK_EQUAL(digest2 <= generic2, true); |
| 176 | BOOST_CHECK_EQUAL(digest2 > generic2, false); |
| 177 | BOOST_CHECK_EQUAL(digest2 >= generic2, false); |
| 178 | } |
| 179 | |
| 180 | BOOST_AUTO_TEST_SUITE_END() // Compare |
| 181 | |
| 182 | BOOST_AUTO_TEST_SUITE(CreateFromIterators) // Bug 2490 |
| 183 | |
| 184 | typedef boost::mpl::vector< |
| 185 | std::vector<uint8_t>, |
| 186 | std::list<uint8_t>, |
| 187 | std::vector<int8_t>, |
| 188 | std::list<int8_t> |
| 189 | > ContainerTypes; |
| 190 | |
| 191 | BOOST_AUTO_TEST_CASE_TEMPLATE(ZeroOctet, T, ContainerTypes) |
| 192 | { |
| 193 | T bytes; |
| 194 | name::Component c(bytes.begin(), bytes.end()); |
| 195 | BOOST_CHECK_EQUAL(c.value_size(), 0); |
| 196 | BOOST_CHECK_EQUAL(c.size(), 2); |
| 197 | } |
| 198 | |
| 199 | BOOST_AUTO_TEST_CASE_TEMPLATE(OneOctet, T, ContainerTypes) |
| 200 | { |
| 201 | T bytes{1}; |
| 202 | name::Component c(bytes.begin(), bytes.end()); |
| 203 | BOOST_CHECK_EQUAL(c.value_size(), 1); |
| 204 | BOOST_CHECK_EQUAL(c.size(), 3); |
| 205 | } |
| 206 | |
| 207 | BOOST_AUTO_TEST_CASE_TEMPLATE(FourOctets, T, ContainerTypes) |
| 208 | { |
| 209 | T bytes{1, 2, 3, 4}; |
| 210 | name::Component c(bytes.begin(), bytes.end()); |
| 211 | BOOST_CHECK_EQUAL(c.value_size(), 4); |
| 212 | BOOST_CHECK_EQUAL(c.size(), 6); |
| 213 | } |
| 214 | |
| 215 | BOOST_AUTO_TEST_SUITE_END() // CreateFromIterators |
| 216 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 217 | BOOST_AUTO_TEST_SUITE(NamingConvention) |
| 218 | |
| 219 | template<typename ArgType> |
| 220 | struct ConventionTest |
| 221 | { |
| 222 | function<name::Component(ArgType)> makeComponent; |
| 223 | function<ArgType(const name::Component&)> getValue; |
| 224 | function<Name&(Name&, ArgType)> append; |
| 225 | Name expected; |
| 226 | ArgType value; |
| 227 | function<bool(const name::Component&)> isComponent; |
| 228 | }; |
| 229 | |
| 230 | class NumberWithMarker |
| 231 | { |
| 232 | public: |
| 233 | ConventionTest<uint64_t> |
| 234 | operator()() const |
| 235 | { |
| 236 | return {bind(&name::Component::fromNumberWithMarker, 0xAA, _1), |
| 237 | bind(&name::Component::toNumberWithMarker, _1, 0xAA), |
| 238 | bind(&Name::appendNumberWithMarker, _1, 0xAA, _2), |
| 239 | Name("/%AA%03%E8"), |
| 240 | 1000, |
| 241 | bind(&name::Component::isNumberWithMarker, _1, 0xAA)}; |
| 242 | } |
| 243 | }; |
| 244 | |
| 245 | class Segment |
| 246 | { |
| 247 | public: |
| 248 | ConventionTest<uint64_t> |
| 249 | operator()() const |
| 250 | { |
| 251 | return {&name::Component::fromSegment, |
| 252 | bind(&name::Component::toSegment, _1), |
| 253 | bind(&Name::appendSegment, _1, _2), |
| 254 | Name("/%00%27%10"), |
| 255 | 10000, |
| 256 | bind(&name::Component::isSegment, _1)}; |
| 257 | } |
| 258 | }; |
| 259 | |
| 260 | class SegmentOffset |
| 261 | { |
| 262 | public: |
| 263 | ConventionTest<uint64_t> |
| 264 | operator()() const |
| 265 | { |
| 266 | return {&name::Component::fromSegmentOffset, |
| 267 | bind(&name::Component::toSegmentOffset, _1), |
| 268 | bind(&Name::appendSegmentOffset, _1, _2), |
| 269 | Name("/%FB%00%01%86%A0"), |
| 270 | 100000, |
| 271 | bind(&name::Component::isSegmentOffset, _1)}; |
| 272 | } |
| 273 | }; |
| 274 | |
| 275 | class Version |
| 276 | { |
| 277 | public: |
| 278 | ConventionTest<uint64_t> |
| 279 | operator()() const |
| 280 | { |
| 281 | return {&name::Component::fromVersion, |
| 282 | bind(&name::Component::toVersion, _1), |
| 283 | [] (Name& name, uint64_t version) -> Name& { return name.appendVersion(version); }, |
| 284 | Name("/%FD%00%0FB%40"), |
| 285 | 1000000, |
| 286 | bind(&name::Component::isVersion, _1)}; |
| 287 | } |
| 288 | }; |
| 289 | |
| 290 | class Timestamp |
| 291 | { |
| 292 | public: |
| 293 | ConventionTest<time::system_clock::TimePoint> |
| 294 | operator()() const |
| 295 | { |
| 296 | return {&name::Component::fromTimestamp, |
| 297 | bind(&name::Component::toTimestamp, _1), |
| 298 | [] (Name& name, time::system_clock::TimePoint t) -> Name& { return name.appendTimestamp(t); }, |
| 299 | Name("/%FC%00%04%7BE%E3%1B%00%00"), |
| 300 | time::getUnixEpoch() + time::days(14600), // 40 years |
| 301 | bind(&name::Component::isTimestamp, _1)}; |
| 302 | } |
| 303 | }; |
| 304 | |
| 305 | class SequenceNumber |
| 306 | { |
| 307 | public: |
| 308 | ConventionTest<uint64_t> |
| 309 | operator()() const |
| 310 | { |
| 311 | return {&name::Component::fromSequenceNumber, |
| 312 | bind(&name::Component::toSequenceNumber, _1), |
| 313 | bind(&Name::appendSequenceNumber, _1, _2), |
| 314 | Name("/%FE%00%98%96%80"), |
| 315 | 10000000, |
| 316 | bind(&name::Component::isSequenceNumber, _1)}; |
| 317 | } |
| 318 | }; |
| 319 | |
| 320 | using ConventionTests = boost::mpl::vector< |
| 321 | NumberWithMarker, |
| 322 | Segment, |
| 323 | SegmentOffset, |
| 324 | Version, |
| 325 | Timestamp, |
| 326 | SequenceNumber |
| 327 | >; |
| 328 | |
| 329 | BOOST_AUTO_TEST_CASE_TEMPLATE(Convention, T, ConventionTests) |
| 330 | { |
| 331 | name::Component invalidComponent1; |
| 332 | name::Component invalidComponent2("1234567890"); |
| 333 | |
| 334 | auto test = T()(); |
| 335 | |
| 336 | const Name& expected = test.expected; |
| 337 | BOOST_TEST_MESSAGE("Check " << expected[0].toUri()); |
| 338 | |
| 339 | BOOST_CHECK_EQUAL(expected[0].isGeneric(), true); |
| 340 | |
| 341 | name::Component actualComponent = test.makeComponent(test.value); |
| 342 | BOOST_CHECK_EQUAL(actualComponent, expected[0]); |
| 343 | |
| 344 | Name actualName; |
| 345 | test.append(actualName, test.value); |
| 346 | BOOST_CHECK_EQUAL(actualName, expected); |
| 347 | |
| 348 | BOOST_CHECK_EQUAL(test.isComponent(expected[0]), true); |
| 349 | BOOST_CHECK_EQUAL(test.getValue(expected[0]), test.value); |
| 350 | |
| 351 | BOOST_CHECK_EQUAL(test.isComponent(invalidComponent1), false); |
| 352 | BOOST_CHECK_EQUAL(test.isComponent(invalidComponent2), false); |
| 353 | |
| 354 | BOOST_CHECK_THROW(test.getValue(invalidComponent1), name::Component::Error); |
| 355 | BOOST_CHECK_THROW(test.getValue(invalidComponent2), name::Component::Error); |
| 356 | } |
| 357 | |
| 358 | BOOST_AUTO_TEST_SUITE_END() // NamingConvention |
| 359 | |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 360 | BOOST_AUTO_TEST_SUITE_END() // TestNameComponent |
| 361 | |
| 362 | } // namespace tests |
| 363 | } // namespace ndn |