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