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 | /* |
Davide Pesavento | 152ef44 | 2023-04-22 02:02:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2023 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 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/name-component.hpp" |
| 23 | #include "ndn-cxx/name.hpp" |
| 24 | #include "ndn-cxx/util/string-helper.hpp" |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 25 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 26 | #include "tests/boost-test.hpp" |
| 27 | |
Junxiao Shi | 4053bd5 | 2018-08-16 13:39:25 -0600 | [diff] [blame] | 28 | #include <boost/algorithm/string/case_conv.hpp> |
Davide Pesavento | 009062d | 2019-11-20 00:16:33 -0500 | [diff] [blame] | 29 | #include <boost/algorithm/string/predicate.hpp> |
| 30 | #include <boost/lexical_cast.hpp> |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 31 | #include <boost/mpl/vector.hpp> |
| 32 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 33 | namespace ndn::tests { |
| 34 | |
| 35 | using ndn::name::Component; |
| 36 | using ndn::name::UriFormat; |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 37 | |
Davide Pesavento | 152ef44 | 2023-04-22 02:02:29 -0400 | [diff] [blame] | 38 | BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Component>)); |
| 39 | BOOST_CONCEPT_ASSERT((WireEncodable<Component>)); |
| 40 | BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<Component>)); |
| 41 | BOOST_CONCEPT_ASSERT((WireDecodable<Component>)); |
| 42 | static_assert(std::is_convertible_v<Component::Error*, tlv::Error*>, |
| 43 | "name::Component::Error must inherit from tlv::Error"); |
| 44 | |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 45 | BOOST_AUTO_TEST_SUITE(TestNameComponent) |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 46 | |
| 47 | BOOST_AUTO_TEST_SUITE(Decode) |
| 48 | |
Davide Pesavento | 009062d | 2019-11-20 00:16:33 -0500 | [diff] [blame] | 49 | #define CHECK_COMP_ERR(expr, whatstring) \ |
| 50 | BOOST_CHECK_EXCEPTION(expr, Component::Error, \ |
| 51 | [] (const auto& e) { return boost::contains(e.what(), whatstring); }) |
| 52 | |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 53 | BOOST_AUTO_TEST_CASE(Generic) |
| 54 | { |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 55 | Component comp("0807 6E646E2D637878"_block); |
| 56 | BOOST_CHECK_EQUAL(comp.type(), tlv::GenericNameComponent); |
Davide Pesavento | 009062d | 2019-11-20 00:16:33 -0500 | [diff] [blame] | 57 | BOOST_CHECK_EQUAL(comp.isGeneric(), true); |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 58 | BOOST_CHECK_EQUAL(comp.toUri(), "ndn-cxx"); |
Junxiao Shi | a39c0b5 | 2019-12-31 15:13:15 -0700 | [diff] [blame] | 59 | BOOST_CHECK_EQUAL(comp.toUri(UriFormat::CANONICAL), "8=ndn-cxx"); |
| 60 | BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ALTERNATE), "ndn-cxx"); |
Davide Pesavento | bc17d95 | 2020-02-15 20:10:52 -0500 | [diff] [blame] | 61 | BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_CANONICAL), "8=ndn-cxx"); |
| 62 | BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_ALTERNATE), "ndn-cxx"); |
Davide Pesavento | 009062d | 2019-11-20 00:16:33 -0500 | [diff] [blame] | 63 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(comp), "ndn-cxx"); |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 64 | BOOST_CHECK_EQUAL(Component::fromEscapedString("ndn-cxx"), comp); |
Junxiao Shi | d2e6063 | 2018-08-10 10:48:44 -0600 | [diff] [blame] | 65 | BOOST_CHECK_EQUAL(Component::fromEscapedString("8=ndn-cxx"), comp); |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 66 | |
| 67 | comp.wireDecode("0800"_block); |
| 68 | BOOST_CHECK_EQUAL(comp.toUri(), "..."); |
| 69 | BOOST_CHECK_EQUAL(Component::fromEscapedString("..."), comp); |
Junxiao Shi | d2e6063 | 2018-08-10 10:48:44 -0600 | [diff] [blame] | 70 | BOOST_CHECK_EQUAL(Component::fromEscapedString("8=..."), comp); |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 71 | BOOST_CHECK_EQUAL(Component::fromEscapedString(".%2E."), comp); |
| 72 | |
| 73 | comp.wireDecode("0801 2E"_block); |
| 74 | BOOST_CHECK_EQUAL(comp.toUri(), "...."); |
| 75 | BOOST_CHECK_EQUAL(Component::fromEscapedString("...."), comp); |
| 76 | BOOST_CHECK_EQUAL(Component::fromEscapedString("%2E..%2E"), comp); |
| 77 | |
| 78 | comp.wireDecode("0803 2E412E"_block); |
| 79 | BOOST_CHECK_EQUAL(comp.toUri(), ".A."); |
| 80 | BOOST_CHECK_EQUAL(Component::fromEscapedString(".A."), comp); |
| 81 | |
| 82 | comp.wireDecode("0807 666F6F25626172"_block); |
| 83 | BOOST_CHECK_EQUAL(comp.toUri(), "foo%25bar"); |
| 84 | BOOST_CHECK_EQUAL(Component::fromEscapedString("foo%25bar"), comp); |
Junxiao Shi | d2e6063 | 2018-08-10 10:48:44 -0600 | [diff] [blame] | 85 | BOOST_CHECK_EQUAL(Component::fromEscapedString("8=foo%25bar"), comp); |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 86 | |
| 87 | comp.wireDecode("0804 2D2E5F7E"_block); |
| 88 | BOOST_CHECK_EQUAL(comp.toUri(), "-._~"); |
| 89 | BOOST_CHECK_EQUAL(Component::fromEscapedString("-._~"), comp); |
| 90 | |
Junxiao Shi | d2e6063 | 2018-08-10 10:48:44 -0600 | [diff] [blame] | 91 | comp.wireDecode("0803 393D41"_block); |
| 92 | BOOST_CHECK_EQUAL(comp.toUri(), "9%3DA"); |
| 93 | BOOST_CHECK_EQUAL(Component::fromEscapedString("9%3DA"), comp); |
| 94 | |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 95 | comp = Component(":/?#[]@"); |
| 96 | BOOST_CHECK_EQUAL(comp.toUri(), "%3A%2F%3F%23%5B%5D%40"); |
| 97 | BOOST_CHECK_EQUAL(Component::fromEscapedString("%3A%2F%3F%23%5B%5D%40"), comp); |
| 98 | |
| 99 | BOOST_CHECK_THROW(Component::fromEscapedString(""), Component::Error); |
| 100 | BOOST_CHECK_THROW(Component::fromEscapedString("."), Component::Error); |
| 101 | BOOST_CHECK_THROW(Component::fromEscapedString(".."), Component::Error); |
Davide Pesavento | 21ee899 | 2022-02-25 20:46:04 -0500 | [diff] [blame] | 102 | BOOST_CHECK_THROW(Component::fromEscapedString("8="), Component::Error); |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Junxiao Shi | 4053bd5 | 2018-08-16 13:39:25 -0600 | [diff] [blame] | 105 | static void |
Davide Pesavento | 009062d | 2019-11-20 00:16:33 -0500 | [diff] [blame] | 106 | testSha256Component(uint32_t type, const std::string& uriPrefix) |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 107 | { |
Davide Pesavento | 009062d | 2019-11-20 00:16:33 -0500 | [diff] [blame] | 108 | const std::string hexLower = "28bad4b5275bd392dbb670c75cf0b66f13f7942b21e80f55c0e86b374753a548"; |
| 109 | const std::string hexUpper = boost::to_upper_copy(hexLower); |
Junxiao Shi | 4053bd5 | 2018-08-16 13:39:25 -0600 | [diff] [blame] | 110 | std::string hexPct; |
| 111 | for (size_t i = 0; i < hexUpper.size(); i += 2) { |
| 112 | hexPct += "%" + hexUpper.substr(i, 2); |
| 113 | } |
Junxiao Shi | a39c0b5 | 2019-12-31 15:13:15 -0700 | [diff] [blame] | 114 | const std::string hexPctCanonical = "%28%BA%D4%B5%27%5B%D3%92%DB%B6p%C7%5C%F0%B6o%13%F7%94%2B%21%E8%0FU%C0%E8k7GS%A5H"; |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 115 | |
Davide Pesavento | 21ee899 | 2022-02-25 20:46:04 -0500 | [diff] [blame] | 116 | Component comp(type, fromHex(hexLower)); |
Davide Pesavento | 009062d | 2019-11-20 00:16:33 -0500 | [diff] [blame] | 117 | |
Junxiao Shi | 4053bd5 | 2018-08-16 13:39:25 -0600 | [diff] [blame] | 118 | BOOST_CHECK_EQUAL(comp.type(), type); |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 119 | BOOST_CHECK_EQUAL(comp.toUri(), uriPrefix + hexLower); |
Junxiao Shi | a39c0b5 | 2019-12-31 15:13:15 -0700 | [diff] [blame] | 120 | BOOST_CHECK_EQUAL(comp.toUri(UriFormat::CANONICAL), to_string(type) + "=" + hexPctCanonical); |
| 121 | BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ALTERNATE), uriPrefix + hexLower); |
Davide Pesavento | bc17d95 | 2020-02-15 20:10:52 -0500 | [diff] [blame] | 122 | BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_CANONICAL), to_string(type) + "=" + hexPctCanonical); |
| 123 | BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_ALTERNATE), uriPrefix + hexLower); |
Davide Pesavento | 009062d | 2019-11-20 00:16:33 -0500 | [diff] [blame] | 124 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(comp), uriPrefix + hexLower); |
| 125 | BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(uriPrefix + hexLower)); |
| 126 | BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(uriPrefix + hexUpper)); |
| 127 | BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(to_string(type) + "=" + hexPct)); |
Junxiao Shi | a39c0b5 | 2019-12-31 15:13:15 -0700 | [diff] [blame] | 128 | BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(to_string(type) + "=" + hexPctCanonical)); |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 129 | |
Davide Pesavento | 009062d | 2019-11-20 00:16:33 -0500 | [diff] [blame] | 130 | CHECK_COMP_ERR(comp.wireDecode(Block(type, fromHex("A791806951F25C4D"))), "TLV-LENGTH must be 32"); |
| 131 | CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix), "TLV-LENGTH must be 32"); |
| 132 | CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "a791806951f25c4d"), "TLV-LENGTH must be 32"); |
| 133 | CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "foo"), "invalid hex encoding"); |
| 134 | CHECK_COMP_ERR(Component::fromEscapedString(boost::to_upper_copy(uriPrefix) + hexLower), "Unknown TLV-TYPE"); |
Junxiao Shi | 4053bd5 | 2018-08-16 13:39:25 -0600 | [diff] [blame] | 135 | } |
| 136 | |
Davide Pesavento | 009062d | 2019-11-20 00:16:33 -0500 | [diff] [blame] | 137 | BOOST_AUTO_TEST_CASE(ImplicitDigest) |
Junxiao Shi | 4053bd5 | 2018-08-16 13:39:25 -0600 | [diff] [blame] | 138 | { |
Davide Pesavento | 009062d | 2019-11-20 00:16:33 -0500 | [diff] [blame] | 139 | testSha256Component(tlv::ImplicitSha256DigestComponent, "sha256digest="); |
Junxiao Shi | 4053bd5 | 2018-08-16 13:39:25 -0600 | [diff] [blame] | 140 | } |
| 141 | |
Davide Pesavento | 009062d | 2019-11-20 00:16:33 -0500 | [diff] [blame] | 142 | BOOST_AUTO_TEST_CASE(ParametersDigest) |
Junxiao Shi | 4053bd5 | 2018-08-16 13:39:25 -0600 | [diff] [blame] | 143 | { |
Davide Pesavento | 009062d | 2019-11-20 00:16:33 -0500 | [diff] [blame] | 144 | testSha256Component(tlv::ParametersSha256DigestComponent, "params-sha256="); |
| 145 | } |
| 146 | |
| 147 | static void |
| 148 | testDecimalComponent(uint32_t type, const std::string& uriPrefix) |
| 149 | { |
Davide Pesavento | 47a94d1 | 2021-10-31 16:41:05 -0400 | [diff] [blame] | 150 | const Component comp(makeNonNegativeIntegerBlock(type, 42)); // TLV-VALUE is a NonNegativeInteger |
Davide Pesavento | 009062d | 2019-11-20 00:16:33 -0500 | [diff] [blame] | 151 | BOOST_CHECK_EQUAL(comp.type(), type); |
| 152 | BOOST_CHECK_EQUAL(comp.isNumber(), true); |
| 153 | const auto compUri = uriPrefix + "42"; |
| 154 | BOOST_CHECK_EQUAL(comp.toUri(), compUri); |
Junxiao Shi | a39c0b5 | 2019-12-31 15:13:15 -0700 | [diff] [blame] | 155 | BOOST_CHECK_EQUAL(comp.toUri(UriFormat::CANONICAL), to_string(type) + "=%2A"); |
| 156 | BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ALTERNATE), compUri); |
Davide Pesavento | bc17d95 | 2020-02-15 20:10:52 -0500 | [diff] [blame] | 157 | BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_CANONICAL), to_string(type) + "=%2A"); |
| 158 | BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_ALTERNATE), compUri); |
Davide Pesavento | 009062d | 2019-11-20 00:16:33 -0500 | [diff] [blame] | 159 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(comp), compUri); |
| 160 | BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(compUri)); |
| 161 | BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(to_string(type) + "=%2A")); |
| 162 | BOOST_CHECK_EQUAL(comp, Component::fromNumber(42, type)); |
| 163 | |
Davide Pesavento | 21ee899 | 2022-02-25 20:46:04 -0500 | [diff] [blame] | 164 | const Component comp2(type, fromHex("010203")); // TLV-VALUE is *not* a NonNegativeInteger |
Davide Pesavento | 009062d | 2019-11-20 00:16:33 -0500 | [diff] [blame] | 165 | BOOST_CHECK_EQUAL(comp2.type(), type); |
| 166 | BOOST_CHECK_EQUAL(comp2.isNumber(), false); |
| 167 | const auto comp2Uri = to_string(type) + "=%01%02%03"; |
| 168 | BOOST_CHECK_EQUAL(comp2.toUri(), comp2Uri); |
| 169 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(comp2), comp2Uri); |
| 170 | BOOST_CHECK_EQUAL(comp2, Component::fromEscapedString(comp2Uri)); |
| 171 | |
| 172 | CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix), "invalid format"); |
| 173 | CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "foo"), "invalid format"); |
| 174 | CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "00"), "invalid format"); |
| 175 | CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "-1"), "invalid format"); |
| 176 | CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "9.3"), "invalid format"); |
| 177 | CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + " 84"), "invalid format"); |
| 178 | CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "0xAF"), "invalid format"); |
| 179 | CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "18446744073709551616"), "out of range"); |
| 180 | CHECK_COMP_ERR(Component::fromEscapedString(boost::to_upper_copy(uriPrefix) + "42"), "Unknown TLV-TYPE"); |
| 181 | } |
| 182 | |
| 183 | BOOST_AUTO_TEST_CASE(Segment) |
| 184 | { |
| 185 | testDecimalComponent(tlv::SegmentNameComponent, "seg="); |
| 186 | } |
| 187 | |
| 188 | BOOST_AUTO_TEST_CASE(ByteOffset) |
| 189 | { |
| 190 | testDecimalComponent(tlv::ByteOffsetNameComponent, "off="); |
| 191 | } |
| 192 | |
| 193 | BOOST_AUTO_TEST_CASE(Version) |
| 194 | { |
| 195 | testDecimalComponent(tlv::VersionNameComponent, "v="); |
| 196 | } |
| 197 | |
| 198 | BOOST_AUTO_TEST_CASE(Timestamp) |
| 199 | { |
| 200 | testDecimalComponent(tlv::TimestampNameComponent, "t="); |
| 201 | } |
| 202 | |
| 203 | BOOST_AUTO_TEST_CASE(SequenceNum) |
| 204 | { |
| 205 | testDecimalComponent(tlv::SequenceNumNameComponent, "seq="); |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Davide Pesavento | 21ee899 | 2022-02-25 20:46:04 -0500 | [diff] [blame] | 208 | BOOST_AUTO_TEST_CASE(Keyword) |
| 209 | { |
| 210 | Component comp("2007 6E646E2D637878"_block); |
| 211 | BOOST_CHECK_EQUAL(comp.type(), tlv::KeywordNameComponent); |
| 212 | BOOST_CHECK_EQUAL(comp.isKeyword(), true); |
| 213 | BOOST_CHECK_EQUAL(comp.toUri(), "32=ndn-cxx"); |
| 214 | BOOST_CHECK_EQUAL(comp.toUri(UriFormat::CANONICAL), "32=ndn-cxx"); |
| 215 | BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ALTERNATE), "32=ndn-cxx"); |
| 216 | BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_CANONICAL), "32=ndn-cxx"); |
| 217 | BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_ALTERNATE), "32=ndn-cxx"); |
| 218 | BOOST_CHECK_EQUAL(Component::fromEscapedString("32=ndn-cxx"), comp); |
| 219 | |
| 220 | comp.wireDecode("2000"_block); |
| 221 | BOOST_CHECK_EQUAL(comp.type(), tlv::KeywordNameComponent); |
| 222 | BOOST_CHECK_EQUAL(comp.isKeyword(), true); |
| 223 | BOOST_CHECK_EQUAL(comp.toUri(), "32=..."); |
| 224 | BOOST_CHECK_EQUAL(Component::fromEscapedString("32=..."), comp); |
| 225 | |
| 226 | BOOST_CHECK_THROW(Component::fromEscapedString("32="), Component::Error); |
| 227 | BOOST_CHECK_THROW(Component::fromEscapedString("32=."), Component::Error); |
| 228 | BOOST_CHECK_THROW(Component::fromEscapedString("32=.."), Component::Error); |
| 229 | } |
| 230 | |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 231 | BOOST_AUTO_TEST_CASE(OtherType) |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 232 | { |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 233 | Component comp("0907 6E646E2D637878"_block); |
| 234 | BOOST_CHECK_EQUAL(comp.type(), 0x09); |
| 235 | BOOST_CHECK_EQUAL(comp.toUri(), "9=ndn-cxx"); |
Davide Pesavento | bc17d95 | 2020-02-15 20:10:52 -0500 | [diff] [blame] | 236 | BOOST_CHECK_EQUAL(comp.toUri(UriFormat::CANONICAL), "9=ndn-cxx"); |
| 237 | BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ALTERNATE), "9=ndn-cxx"); |
| 238 | BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_CANONICAL), "9=ndn-cxx"); |
| 239 | BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_ALTERNATE), "9=ndn-cxx"); |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 240 | BOOST_CHECK_EQUAL(Component::fromEscapedString("9=ndn-cxx"), comp); |
| 241 | |
| 242 | comp.wireDecode("FDFFFF00"_block); |
| 243 | BOOST_CHECK_EQUAL(comp.type(), 0xFFFF); |
| 244 | BOOST_CHECK_EQUAL(comp.toUri(), "65535=..."); |
| 245 | BOOST_CHECK_EQUAL(Component::fromEscapedString("65535=..."), comp); |
| 246 | |
| 247 | comp.wireDecode("FD576501 2E"_block); |
| 248 | BOOST_CHECK_EQUAL(comp.type(), 0x5765); |
| 249 | BOOST_CHECK_EQUAL(comp.toUri(), "22373=...."); |
| 250 | BOOST_CHECK_EQUAL(Component::fromEscapedString("22373=...."), comp); |
| 251 | |
| 252 | BOOST_CHECK_THROW(Component::fromEscapedString("3="), Component::Error); |
| 253 | BOOST_CHECK_THROW(Component::fromEscapedString("3=."), Component::Error); |
| 254 | BOOST_CHECK_THROW(Component::fromEscapedString("3=.."), Component::Error); |
| 255 | } |
| 256 | |
| 257 | BOOST_AUTO_TEST_CASE(InvalidType) |
| 258 | { |
| 259 | Component comp; |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 260 | BOOST_CHECK_THROW(comp.wireDecode(Block{}), Component::Error); |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 261 | BOOST_CHECK_THROW(comp.wireDecode("FE0001000001 80"_block), Component::Error); |
| 262 | |
| 263 | BOOST_CHECK_THROW(Component::fromEscapedString("0=A"), Component::Error); |
| 264 | BOOST_CHECK_THROW(Component::fromEscapedString("65536=A"), Component::Error); |
Junxiao Shi | d2e6063 | 2018-08-10 10:48:44 -0600 | [diff] [blame] | 265 | BOOST_CHECK_THROW(Component::fromEscapedString("4294967296=A"), Component::Error); |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 266 | BOOST_CHECK_THROW(Component::fromEscapedString("-1=A"), Component::Error); |
| 267 | BOOST_CHECK_THROW(Component::fromEscapedString("+=A"), Component::Error); |
| 268 | BOOST_CHECK_THROW(Component::fromEscapedString("=A"), Component::Error); |
| 269 | BOOST_CHECK_THROW(Component::fromEscapedString("0x1=A"), Component::Error); |
| 270 | BOOST_CHECK_THROW(Component::fromEscapedString("Z=A"), Component::Error); |
| 271 | BOOST_CHECK_THROW(Component::fromEscapedString("09=A"), Component::Error); |
Junxiao Shi | 4053bd5 | 2018-08-16 13:39:25 -0600 | [diff] [blame] | 272 | BOOST_CHECK_THROW(Component::fromEscapedString("0x3=A"), Component::Error); |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 273 | BOOST_CHECK_THROW(Component::fromEscapedString("+9=A"), Component::Error); |
Junxiao Shi | d2e6063 | 2018-08-10 10:48:44 -0600 | [diff] [blame] | 274 | BOOST_CHECK_THROW(Component::fromEscapedString(" 9=A"), Component::Error); |
| 275 | BOOST_CHECK_THROW(Component::fromEscapedString("9 =A"), Component::Error); |
| 276 | BOOST_CHECK_THROW(Component::fromEscapedString("9.0=A"), Component::Error); |
| 277 | BOOST_CHECK_THROW(Component::fromEscapedString("9E0=A"), Component::Error); |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | BOOST_AUTO_TEST_SUITE_END() // Decode |
| 281 | |
Davide Pesavento | 2f92b6e | 2022-04-10 16:42:01 -0400 | [diff] [blame] | 282 | BOOST_AUTO_TEST_CASE(ConstructFromSpan) |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 283 | { |
Davide Pesavento | 2f92b6e | 2022-04-10 16:42:01 -0400 | [diff] [blame] | 284 | const uint8_t arr[] = {1, 2, 3}; |
| 285 | Component c1(arr); |
| 286 | BOOST_TEST(c1.wireEncode() == "0803010203"_block); |
| 287 | Component c2(128, arr); |
| 288 | BOOST_TEST(c2.wireEncode() == "8003010203"_block); |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 289 | |
Davide Pesavento | 2f92b6e | 2022-04-10 16:42:01 -0400 | [diff] [blame] | 290 | const std::vector<uint8_t> vec = {4, 5, 6}; |
| 291 | Component c3(vec); |
| 292 | BOOST_TEST(c3.wireEncode() == "0803040506"_block); |
| 293 | Component c4(128, vec); |
| 294 | BOOST_TEST(c4.wireEncode() == "8003040506"_block); |
| 295 | |
| 296 | Component c5(128, {7, 8}); |
| 297 | BOOST_TEST(c5.wireEncode() == "80020708"_block); |
| 298 | |
| 299 | const Block b("090109"_block); |
| 300 | Component c6(128, b); |
| 301 | BOOST_TEST(c6.wireEncode() == "8003090109"_block); |
Davide Pesavento | 08378cb | 2018-02-01 16:10:54 -0500 | [diff] [blame] | 302 | } |
| 303 | |
Davide Pesavento | 2f92b6e | 2022-04-10 16:42:01 -0400 | [diff] [blame] | 304 | BOOST_AUTO_TEST_SUITE(ConstructFromIterators) // Bug 2490 |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 305 | |
Davide Pesavento | 009062d | 2019-11-20 00:16:33 -0500 | [diff] [blame] | 306 | using ContainerTypes = boost::mpl::vector<std::vector<uint8_t>, |
| 307 | std::list<uint8_t>, |
| 308 | std::vector<int8_t>, |
| 309 | std::list<int8_t>>; |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 310 | |
Davide Pesavento | 2f92b6e | 2022-04-10 16:42:01 -0400 | [diff] [blame] | 311 | BOOST_AUTO_TEST_CASE_TEMPLATE(ZeroOctets, T, ContainerTypes) |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 312 | { |
| 313 | T bytes; |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 314 | Component c(bytes.begin(), bytes.end()); |
Davide Pesavento | 2f92b6e | 2022-04-10 16:42:01 -0400 | [diff] [blame] | 315 | BOOST_TEST(c.type() == tlv::GenericNameComponent); |
| 316 | BOOST_TEST(c.value_size() == 0); |
| 317 | BOOST_TEST(c.size() == 2); |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | BOOST_AUTO_TEST_CASE_TEMPLATE(OneOctet, T, ContainerTypes) |
| 321 | { |
| 322 | T bytes{1}; |
Davide Pesavento | 2f92b6e | 2022-04-10 16:42:01 -0400 | [diff] [blame] | 323 | Component c(9, bytes.begin(), bytes.end()); |
| 324 | BOOST_TEST(c.type() == 0x09); |
| 325 | BOOST_TEST(c.value_size() == 1); |
| 326 | BOOST_TEST(c.size() == 3); |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | BOOST_AUTO_TEST_CASE_TEMPLATE(FourOctets, T, ContainerTypes) |
| 330 | { |
| 331 | T bytes{1, 2, 3, 4}; |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 332 | Component c(0xFCEC, bytes.begin(), bytes.end()); |
Davide Pesavento | 2f92b6e | 2022-04-10 16:42:01 -0400 | [diff] [blame] | 333 | BOOST_TEST(c.type() == 0xFCEC); |
| 334 | BOOST_TEST(c.value_size() == 4); |
| 335 | BOOST_TEST(c.size() == 8); |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Davide Pesavento | 2f92b6e | 2022-04-10 16:42:01 -0400 | [diff] [blame] | 338 | BOOST_AUTO_TEST_SUITE_END() // ConstructFromIterators |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 339 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 340 | BOOST_AUTO_TEST_SUITE(NamingConvention) |
| 341 | |
| 342 | template<typename ArgType> |
| 343 | struct ConventionTest |
| 344 | { |
Davide Pesavento | 009062d | 2019-11-20 00:16:33 -0500 | [diff] [blame] | 345 | std::function<Component(ArgType)> makeComponent; |
| 346 | std::function<ArgType(const Component&)> getValue; |
| 347 | std::function<Name&(Name&, ArgType)> append; |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 348 | Name expected; |
| 349 | ArgType value; |
Davide Pesavento | 009062d | 2019-11-20 00:16:33 -0500 | [diff] [blame] | 350 | std::function<bool(const Component&)> isComponent; |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 351 | }; |
| 352 | |
Junxiao Shi | e209961 | 2019-02-15 14:46:27 +0000 | [diff] [blame] | 353 | class ConventionMarker |
| 354 | { |
Eric Newberry | c25e463 | 2021-02-11 10:48:11 -0800 | [diff] [blame] | 355 | public: |
| 356 | ConventionMarker() |
| 357 | { |
| 358 | name::setConventionEncoding(name::Convention::MARKER); |
| 359 | } |
| 360 | |
| 361 | ~ConventionMarker() |
| 362 | { |
| 363 | name::setConventionEncoding(name::Convention::TYPED); |
| 364 | } |
Junxiao Shi | e209961 | 2019-02-15 14:46:27 +0000 | [diff] [blame] | 365 | }; |
| 366 | |
| 367 | class ConventionTyped |
| 368 | { |
Junxiao Shi | e209961 | 2019-02-15 14:46:27 +0000 | [diff] [blame] | 369 | }; |
| 370 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 371 | class NumberWithMarker |
| 372 | { |
| 373 | public: |
Junxiao Shi | e209961 | 2019-02-15 14:46:27 +0000 | [diff] [blame] | 374 | using ConventionRev = ConventionMarker; |
| 375 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 376 | ConventionTest<uint64_t> |
| 377 | operator()() const |
| 378 | { |
Davide Pesavento | 1a4a7bf | 2020-12-04 22:30:46 -0500 | [diff] [blame] | 379 | return {[] (auto num) { return Component::fromNumberWithMarker(0xAA, num); }, |
| 380 | [] (const Component& c) { return c.toNumberWithMarker(0xAA); }, |
| 381 | [] (Name& name, auto num) -> Name& { return name.appendNumberWithMarker(0xAA, num); }, |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 382 | Name("/%AA%03%E8"), |
| 383 | 1000, |
Davide Pesavento | 1a4a7bf | 2020-12-04 22:30:46 -0500 | [diff] [blame] | 384 | [] (const Component& c) { return c.isNumberWithMarker(0xAA); }}; |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 385 | } |
| 386 | }; |
| 387 | |
Junxiao Shi | e209961 | 2019-02-15 14:46:27 +0000 | [diff] [blame] | 388 | class SegmentMarker |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 389 | { |
| 390 | public: |
Junxiao Shi | e209961 | 2019-02-15 14:46:27 +0000 | [diff] [blame] | 391 | using ConventionRev = ConventionMarker; |
| 392 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 393 | ConventionTest<uint64_t> |
| 394 | operator()() const |
| 395 | { |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 396 | return {&Component::fromSegment, |
Davide Pesavento | 1a4a7bf | 2020-12-04 22:30:46 -0500 | [diff] [blame] | 397 | &Component::toSegment, |
| 398 | &Name::appendSegment, |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 399 | Name("/%00%27%10"), |
| 400 | 10000, |
Davide Pesavento | 1a4a7bf | 2020-12-04 22:30:46 -0500 | [diff] [blame] | 401 | &Component::isSegment}; |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 402 | } |
| 403 | }; |
| 404 | |
Junxiao Shi | e209961 | 2019-02-15 14:46:27 +0000 | [diff] [blame] | 405 | class SegmentTyped |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 406 | { |
| 407 | public: |
Junxiao Shi | e209961 | 2019-02-15 14:46:27 +0000 | [diff] [blame] | 408 | using ConventionRev = ConventionTyped; |
| 409 | |
| 410 | ConventionTest<uint64_t> |
| 411 | operator()() const |
| 412 | { |
| 413 | return {&Component::fromSegment, |
Davide Pesavento | 1a4a7bf | 2020-12-04 22:30:46 -0500 | [diff] [blame] | 414 | &Component::toSegment, |
| 415 | &Name::appendSegment, |
Davide Pesavento | 47a94d1 | 2021-10-31 16:41:05 -0400 | [diff] [blame] | 416 | Name("/50=%27%10"), |
Junxiao Shi | e209961 | 2019-02-15 14:46:27 +0000 | [diff] [blame] | 417 | 10000, |
Davide Pesavento | 1a4a7bf | 2020-12-04 22:30:46 -0500 | [diff] [blame] | 418 | &Component::isSegment}; |
Junxiao Shi | e209961 | 2019-02-15 14:46:27 +0000 | [diff] [blame] | 419 | } |
| 420 | }; |
| 421 | |
Junxiao Shi | e209961 | 2019-02-15 14:46:27 +0000 | [diff] [blame] | 422 | class ByteOffsetTyped |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 423 | { |
| 424 | public: |
Junxiao Shi | e209961 | 2019-02-15 14:46:27 +0000 | [diff] [blame] | 425 | using ConventionRev = ConventionTyped; |
| 426 | |
| 427 | ConventionTest<uint64_t> |
| 428 | operator()() const |
| 429 | { |
| 430 | return {&Component::fromByteOffset, |
Davide Pesavento | 1a4a7bf | 2020-12-04 22:30:46 -0500 | [diff] [blame] | 431 | &Component::toByteOffset, |
| 432 | &Name::appendByteOffset, |
Davide Pesavento | 47a94d1 | 2021-10-31 16:41:05 -0400 | [diff] [blame] | 433 | Name("/52=%00%01%86%A0"), |
Junxiao Shi | e209961 | 2019-02-15 14:46:27 +0000 | [diff] [blame] | 434 | 100000, |
Davide Pesavento | 1a4a7bf | 2020-12-04 22:30:46 -0500 | [diff] [blame] | 435 | &Component::isByteOffset}; |
Junxiao Shi | e209961 | 2019-02-15 14:46:27 +0000 | [diff] [blame] | 436 | } |
| 437 | }; |
| 438 | |
| 439 | class VersionMarker |
| 440 | { |
| 441 | public: |
| 442 | using ConventionRev = ConventionMarker; |
| 443 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 444 | ConventionTest<uint64_t> |
| 445 | operator()() const |
| 446 | { |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 447 | return {&Component::fromVersion, |
Davide Pesavento | 1a4a7bf | 2020-12-04 22:30:46 -0500 | [diff] [blame] | 448 | &Component::toVersion, |
| 449 | [] (Name& name, auto version) -> Name& { return name.appendVersion(version); }, |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 450 | Name("/%FD%00%0FB%40"), |
| 451 | 1000000, |
Davide Pesavento | 1a4a7bf | 2020-12-04 22:30:46 -0500 | [diff] [blame] | 452 | &Component::isVersion}; |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 453 | } |
| 454 | }; |
| 455 | |
Junxiao Shi | e209961 | 2019-02-15 14:46:27 +0000 | [diff] [blame] | 456 | class VersionTyped |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 457 | { |
| 458 | public: |
Junxiao Shi | e209961 | 2019-02-15 14:46:27 +0000 | [diff] [blame] | 459 | using ConventionRev = ConventionTyped; |
| 460 | |
| 461 | ConventionTest<uint64_t> |
| 462 | operator()() const |
| 463 | { |
| 464 | return {&Component::fromVersion, |
Davide Pesavento | 1a4a7bf | 2020-12-04 22:30:46 -0500 | [diff] [blame] | 465 | &Component::toVersion, |
| 466 | [] (Name& name, auto version) -> Name& { return name.appendVersion(version); }, |
Davide Pesavento | 47a94d1 | 2021-10-31 16:41:05 -0400 | [diff] [blame] | 467 | Name("/54=%00%0FB%40"), |
Junxiao Shi | e209961 | 2019-02-15 14:46:27 +0000 | [diff] [blame] | 468 | 1000000, |
Davide Pesavento | 1a4a7bf | 2020-12-04 22:30:46 -0500 | [diff] [blame] | 469 | &Component::isVersion}; |
Junxiao Shi | e209961 | 2019-02-15 14:46:27 +0000 | [diff] [blame] | 470 | } |
| 471 | }; |
| 472 | |
| 473 | class TimestampMarker |
| 474 | { |
| 475 | public: |
| 476 | using ConventionRev = ConventionMarker; |
| 477 | |
Davide Pesavento | 47a94d1 | 2021-10-31 16:41:05 -0400 | [diff] [blame] | 478 | ConventionTest<time::system_clock::time_point> |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 479 | operator()() const |
| 480 | { |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 481 | return {&Component::fromTimestamp, |
Davide Pesavento | 1a4a7bf | 2020-12-04 22:30:46 -0500 | [diff] [blame] | 482 | &Component::toTimestamp, |
| 483 | [] (Name& name, auto tp) -> Name& { return name.appendTimestamp(tp); }, |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 484 | Name("/%FC%00%04%7BE%E3%1B%00%00"), |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 485 | time::getUnixEpoch() + 14600_days, // 40 years |
Davide Pesavento | 1a4a7bf | 2020-12-04 22:30:46 -0500 | [diff] [blame] | 486 | &Component::isTimestamp}; |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 487 | } |
| 488 | }; |
| 489 | |
Junxiao Shi | e209961 | 2019-02-15 14:46:27 +0000 | [diff] [blame] | 490 | class TimestampTyped |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 491 | { |
| 492 | public: |
Junxiao Shi | e209961 | 2019-02-15 14:46:27 +0000 | [diff] [blame] | 493 | using ConventionRev = ConventionTyped; |
| 494 | |
Davide Pesavento | 47a94d1 | 2021-10-31 16:41:05 -0400 | [diff] [blame] | 495 | ConventionTest<time::system_clock::time_point> |
Junxiao Shi | e209961 | 2019-02-15 14:46:27 +0000 | [diff] [blame] | 496 | operator()() const |
| 497 | { |
| 498 | return {&Component::fromTimestamp, |
Davide Pesavento | 1a4a7bf | 2020-12-04 22:30:46 -0500 | [diff] [blame] | 499 | &Component::toTimestamp, |
| 500 | [] (Name& name, auto tp) -> Name& { return name.appendTimestamp(tp); }, |
Davide Pesavento | 47a94d1 | 2021-10-31 16:41:05 -0400 | [diff] [blame] | 501 | Name("/56=%00%04%7BE%E3%1B%00%00"), |
Junxiao Shi | e209961 | 2019-02-15 14:46:27 +0000 | [diff] [blame] | 502 | time::getUnixEpoch() + 14600_days, // 40 years |
Davide Pesavento | 1a4a7bf | 2020-12-04 22:30:46 -0500 | [diff] [blame] | 503 | &Component::isTimestamp}; |
Junxiao Shi | e209961 | 2019-02-15 14:46:27 +0000 | [diff] [blame] | 504 | } |
| 505 | }; |
| 506 | |
| 507 | class SequenceNumberMarker |
| 508 | { |
| 509 | public: |
| 510 | using ConventionRev = ConventionMarker; |
| 511 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 512 | ConventionTest<uint64_t> |
| 513 | operator()() const |
| 514 | { |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 515 | return {&Component::fromSequenceNumber, |
Davide Pesavento | 1a4a7bf | 2020-12-04 22:30:46 -0500 | [diff] [blame] | 516 | &Component::toSequenceNumber, |
| 517 | &Name::appendSequenceNumber, |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 518 | Name("/%FE%00%98%96%80"), |
| 519 | 10000000, |
Davide Pesavento | 1a4a7bf | 2020-12-04 22:30:46 -0500 | [diff] [blame] | 520 | &Component::isSequenceNumber}; |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 521 | } |
| 522 | }; |
| 523 | |
Junxiao Shi | e209961 | 2019-02-15 14:46:27 +0000 | [diff] [blame] | 524 | class SequenceNumberTyped |
| 525 | { |
| 526 | public: |
| 527 | using ConventionRev = ConventionTyped; |
| 528 | |
| 529 | ConventionTest<uint64_t> |
| 530 | operator()() const |
| 531 | { |
| 532 | return {&Component::fromSequenceNumber, |
Davide Pesavento | 1a4a7bf | 2020-12-04 22:30:46 -0500 | [diff] [blame] | 533 | &Component::toSequenceNumber, |
| 534 | &Name::appendSequenceNumber, |
Davide Pesavento | 47a94d1 | 2021-10-31 16:41:05 -0400 | [diff] [blame] | 535 | Name("/58=%00%98%96%80"), |
Junxiao Shi | e209961 | 2019-02-15 14:46:27 +0000 | [diff] [blame] | 536 | 10000000, |
Davide Pesavento | 1a4a7bf | 2020-12-04 22:30:46 -0500 | [diff] [blame] | 537 | &Component::isSequenceNumber}; |
Junxiao Shi | e209961 | 2019-02-15 14:46:27 +0000 | [diff] [blame] | 538 | } |
| 539 | }; |
| 540 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 541 | using ConventionTests = boost::mpl::vector< |
| 542 | NumberWithMarker, |
Junxiao Shi | e209961 | 2019-02-15 14:46:27 +0000 | [diff] [blame] | 543 | SegmentMarker, |
| 544 | SegmentTyped, |
Junxiao Shi | e209961 | 2019-02-15 14:46:27 +0000 | [diff] [blame] | 545 | ByteOffsetTyped, |
| 546 | VersionMarker, |
| 547 | VersionTyped, |
| 548 | TimestampMarker, |
| 549 | TimestampTyped, |
| 550 | SequenceNumberMarker, |
| 551 | SequenceNumberTyped |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 552 | >; |
| 553 | |
Junxiao Shi | e209961 | 2019-02-15 14:46:27 +0000 | [diff] [blame] | 554 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(Convention, T, ConventionTests, T::ConventionRev) |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 555 | { |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 556 | Component invalidComponent1; |
| 557 | Component invalidComponent2("1234567890"); |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 558 | |
| 559 | auto test = T()(); |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 560 | const Name& expected = test.expected; |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 561 | |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 562 | Component actualComponent = test.makeComponent(test.value); |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 563 | BOOST_CHECK_EQUAL(actualComponent, expected[0]); |
| 564 | |
| 565 | Name actualName; |
| 566 | test.append(actualName, test.value); |
| 567 | BOOST_CHECK_EQUAL(actualName, expected); |
| 568 | |
| 569 | BOOST_CHECK_EQUAL(test.isComponent(expected[0]), true); |
| 570 | BOOST_CHECK_EQUAL(test.getValue(expected[0]), test.value); |
| 571 | |
| 572 | BOOST_CHECK_EQUAL(test.isComponent(invalidComponent1), false); |
| 573 | BOOST_CHECK_EQUAL(test.isComponent(invalidComponent2), false); |
| 574 | |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 575 | BOOST_CHECK_THROW(test.getValue(invalidComponent1), Component::Error); |
| 576 | BOOST_CHECK_THROW(test.getValue(invalidComponent2), Component::Error); |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | BOOST_AUTO_TEST_SUITE_END() // NamingConvention |
| 580 | |
Davide Pesavento | 2f92b6e | 2022-04-10 16:42:01 -0400 | [diff] [blame] | 581 | BOOST_AUTO_TEST_CASE(Compare) |
| 582 | { |
| 583 | const std::vector<Component> comps = { |
| 584 | Component("0120 0000000000000000000000000000000000000000000000000000000000000000"_block), |
| 585 | Component("0120 0000000000000000000000000000000000000000000000000000000000000001"_block), |
| 586 | Component("0120 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"_block), |
| 587 | Component("0220 0000000000000000000000000000000000000000000000000000000000000000"_block), |
| 588 | Component("0220 0000000000000000000000000000000000000000000000000000000000000001"_block), |
| 589 | Component("0220 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"_block), |
| 590 | Component(0x03), |
| 591 | Component("0301 44"_block), |
| 592 | Component("0301 46"_block), |
| 593 | Component("0302 4141"_block), |
| 594 | Component(), |
| 595 | Component("D"), |
| 596 | Component("F"), |
| 597 | Component("AA"), |
| 598 | Component(0x53B2), |
| 599 | Component("FD53B201 44"_block), |
| 600 | Component("FD53B201 46"_block), |
| 601 | Component("FD53B202 4141"_block), |
| 602 | }; |
| 603 | |
| 604 | for (size_t i = 0; i < comps.size(); ++i) { |
| 605 | for (size_t j = 0; j < comps.size(); ++j) { |
| 606 | const auto& lhs = comps[i]; |
| 607 | const auto& rhs = comps[j]; |
| 608 | BOOST_CHECK_EQUAL(lhs == rhs, i == j); |
| 609 | BOOST_CHECK_EQUAL(lhs != rhs, i != j); |
| 610 | BOOST_CHECK_EQUAL(lhs < rhs, i < j); |
| 611 | BOOST_CHECK_EQUAL(lhs <= rhs, i <= j); |
| 612 | BOOST_CHECK_EQUAL(lhs > rhs, i > j); |
| 613 | BOOST_CHECK_EQUAL(lhs >= rhs, i >= j); |
| 614 | } |
| 615 | } |
| 616 | } |
| 617 | |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 618 | BOOST_AUTO_TEST_SUITE_END() // TestNameComponent |
| 619 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 620 | } // namespace ndn::tests |