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