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