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