blob: cc38cf26c7d0bc06f26ff7f4a24b48e0bc32da4c [file] [log] [blame]
Junxiao Shidf4b24e2016-07-14 21:41:43 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi71ff2312017-07-12 13:32:50 +00002/*
Davide Pesavento0c526032024-01-31 21:14:01 -05003 * Copyright (c) 2013-2024 Regents of the University of California.
Junxiao Shidf4b24e2016-07-14 21:41:43 +00004 *
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 Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/name-component.hpp"
23#include "ndn-cxx/name.hpp"
24#include "ndn-cxx/util/string-helper.hpp"
Junxiao Shidf4b24e2016-07-14 21:41:43 +000025
Davide Pesavento7e780642018-11-24 15:51:34 -050026#include "tests/boost-test.hpp"
27
Junxiao Shi4053bd52018-08-16 13:39:25 -060028#include <boost/algorithm/string/case_conv.hpp>
Davide Pesavento009062d2019-11-20 00:16:33 -050029#include <boost/algorithm/string/predicate.hpp>
30#include <boost/lexical_cast.hpp>
Davide Pesavento49e1e872023-11-11 00:45:23 -050031#include <boost/mp11/list.hpp>
Junxiao Shidf4b24e2016-07-14 21:41:43 +000032
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040033namespace ndn::tests {
34
35using ndn::name::Component;
36using ndn::name::UriFormat;
Junxiao Shidf4b24e2016-07-14 21:41:43 +000037
Davide Pesaventofffdd622023-08-28 22:50:43 -040038static_assert(sizeof(Component) == sizeof(Block));
Davide Pesavento152ef442023-04-22 02:02:29 -040039BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Component>));
Davide Pesaventofffdd622023-08-28 22:50:43 -040040BOOST_CONCEPT_ASSERT((boost::Comparable<Component>));
Davide Pesavento152ef442023-04-22 02:02:29 -040041BOOST_CONCEPT_ASSERT((WireEncodable<Component>));
42BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<Component>));
43BOOST_CONCEPT_ASSERT((WireDecodable<Component>));
44static_assert(std::is_convertible_v<Component::Error*, tlv::Error*>,
45 "name::Component::Error must inherit from tlv::Error");
46
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000047BOOST_AUTO_TEST_SUITE(TestNameComponent)
Junxiao Shidf4b24e2016-07-14 21:41:43 +000048
49BOOST_AUTO_TEST_SUITE(Decode)
50
Davide Pesavento009062d2019-11-20 00:16:33 -050051#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 Shidf4b24e2016-07-14 21:41:43 +000055BOOST_AUTO_TEST_CASE(Generic)
56{
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000057 Component comp("0807 6E646E2D637878"_block);
58 BOOST_CHECK_EQUAL(comp.type(), tlv::GenericNameComponent);
Davide Pesavento009062d2019-11-20 00:16:33 -050059 BOOST_CHECK_EQUAL(comp.isGeneric(), true);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000060 BOOST_CHECK_EQUAL(comp.toUri(), "ndn-cxx");
Junxiao Shia39c0b52019-12-31 15:13:15 -070061 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::CANONICAL), "8=ndn-cxx");
62 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ALTERNATE), "ndn-cxx");
Davide Pesaventobc17d952020-02-15 20:10:52 -050063 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 Pesavento009062d2019-11-20 00:16:33 -050065 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(comp), "ndn-cxx");
Davide Pesavento8e047e12024-02-12 16:50:23 -050066 BOOST_CHECK_EQUAL(Component::fromUri("ndn-cxx"), comp);
67 BOOST_CHECK_EQUAL(Component::fromUri("8=ndn-cxx"sv), comp);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000068
69 comp.wireDecode("0800"_block);
70 BOOST_CHECK_EQUAL(comp.toUri(), "...");
Davide Pesavento8e047e12024-02-12 16:50:23 -050071 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 Shicf4ac5b2018-03-28 22:46:06 +000074
75 comp.wireDecode("0801 2E"_block);
76 BOOST_CHECK_EQUAL(comp.toUri(), "....");
Davide Pesavento8e047e12024-02-12 16:50:23 -050077 BOOST_CHECK_EQUAL(Component::fromUri("...."), comp);
78 BOOST_CHECK_EQUAL(Component::fromUri("%2E..%2E"sv), comp);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000079
80 comp.wireDecode("0803 2E412E"_block);
81 BOOST_CHECK_EQUAL(comp.toUri(), ".A.");
Davide Pesavento8e047e12024-02-12 16:50:23 -050082 BOOST_CHECK_EQUAL(Component::fromUri(".A."sv), comp);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000083
84 comp.wireDecode("0807 666F6F25626172"_block);
85 BOOST_CHECK_EQUAL(comp.toUri(), "foo%25bar");
Davide Pesavento8e047e12024-02-12 16:50:23 -050086 BOOST_CHECK_EQUAL(Component::fromUri("foo%25bar"), comp);
87 BOOST_CHECK_EQUAL(Component::fromUri("8=foo%25bar"sv), comp);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000088
89 comp.wireDecode("0804 2D2E5F7E"_block);
90 BOOST_CHECK_EQUAL(comp.toUri(), "-._~");
Davide Pesavento8e047e12024-02-12 16:50:23 -050091 BOOST_CHECK_EQUAL(Component::fromUri("-._~"sv), comp);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000092
Junxiao Shid2e60632018-08-10 10:48:44 -060093 comp.wireDecode("0803 393D41"_block);
94 BOOST_CHECK_EQUAL(comp.toUri(), "9%3DA");
Davide Pesavento8e047e12024-02-12 16:50:23 -050095 BOOST_CHECK_EQUAL(Component::fromUri("9%3DA"sv), comp);
Junxiao Shid2e60632018-08-10 10:48:44 -060096
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000097 comp = Component(":/?#[]@");
98 BOOST_CHECK_EQUAL(comp.toUri(), "%3A%2F%3F%23%5B%5D%40");
Davide Pesavento8e047e12024-02-12 16:50:23 -050099 BOOST_CHECK_EQUAL(Component::fromUri("%3A%2F%3F%23%5B%5D%40"sv), comp);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000100
Davide Pesavento8e047e12024-02-12 16:50:23 -0500101 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 Shidf4b24e2016-07-14 21:41:43 +0000106}
107
Junxiao Shi4053bd52018-08-16 13:39:25 -0600108static void
Davide Pesavento009062d2019-11-20 00:16:33 -0500109testSha256Component(uint32_t type, const std::string& uriPrefix)
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000110{
Davide Pesavento009062d2019-11-20 00:16:33 -0500111 const std::string hexLower = "28bad4b5275bd392dbb670c75cf0b66f13f7942b21e80f55c0e86b374753a548";
112 const std::string hexUpper = boost::to_upper_copy(hexLower);
Junxiao Shi4053bd52018-08-16 13:39:25 -0600113 std::string hexPct;
114 for (size_t i = 0; i < hexUpper.size(); i += 2) {
115 hexPct += "%" + hexUpper.substr(i, 2);
116 }
Junxiao Shia39c0b52019-12-31 15:13:15 -0700117 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 Shicf4ac5b2018-03-28 22:46:06 +0000118
Davide Pesavento21ee8992022-02-25 20:46:04 -0500119 Component comp(type, fromHex(hexLower));
Davide Pesavento009062d2019-11-20 00:16:33 -0500120
Junxiao Shi4053bd52018-08-16 13:39:25 -0600121 BOOST_CHECK_EQUAL(comp.type(), type);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000122 BOOST_CHECK_EQUAL(comp.toUri(), uriPrefix + hexLower);
Davide Pesavento8e047e12024-02-12 16:50:23 -0500123 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::CANONICAL), std::to_string(type) + "=" + hexPctCanonical);
Junxiao Shia39c0b52019-12-31 15:13:15 -0700124 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ALTERNATE), uriPrefix + hexLower);
Davide Pesavento8e047e12024-02-12 16:50:23 -0500125 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_CANONICAL), std::to_string(type) + "=" + hexPctCanonical);
Davide Pesaventobc17d952020-02-15 20:10:52 -0500126 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_ALTERNATE), uriPrefix + hexLower);
Davide Pesavento009062d2019-11-20 00:16:33 -0500127 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(comp), uriPrefix + hexLower);
Davide Pesavento8e047e12024-02-12 16:50:23 -0500128 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 Shicf4ac5b2018-03-28 22:46:06 +0000132
Davide Pesavento009062d2019-11-20 00:16:33 -0500133 CHECK_COMP_ERR(comp.wireDecode(Block(type, fromHex("A791806951F25C4D"))), "TLV-LENGTH must be 32");
Davide Pesavento8e047e12024-02-12 16:50:23 -0500134 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 Shi4053bd52018-08-16 13:39:25 -0600138}
139
Davide Pesavento009062d2019-11-20 00:16:33 -0500140BOOST_AUTO_TEST_CASE(ImplicitDigest)
Junxiao Shi4053bd52018-08-16 13:39:25 -0600141{
Davide Pesavento009062d2019-11-20 00:16:33 -0500142 testSha256Component(tlv::ImplicitSha256DigestComponent, "sha256digest=");
Junxiao Shi4053bd52018-08-16 13:39:25 -0600143}
144
Davide Pesavento009062d2019-11-20 00:16:33 -0500145BOOST_AUTO_TEST_CASE(ParametersDigest)
Junxiao Shi4053bd52018-08-16 13:39:25 -0600146{
Davide Pesavento009062d2019-11-20 00:16:33 -0500147 testSha256Component(tlv::ParametersSha256DigestComponent, "params-sha256=");
148}
149
150static void
151testDecimalComponent(uint32_t type, const std::string& uriPrefix)
152{
Davide Pesavento47a94d12021-10-31 16:41:05 -0400153 const Component comp(makeNonNegativeIntegerBlock(type, 42)); // TLV-VALUE is a NonNegativeInteger
Davide Pesavento009062d2019-11-20 00:16:33 -0500154 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 Pesavento8e047e12024-02-12 16:50:23 -0500158 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::CANONICAL), std::to_string(type) + "=%2A");
Junxiao Shia39c0b52019-12-31 15:13:15 -0700159 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ALTERNATE), compUri);
Davide Pesavento8e047e12024-02-12 16:50:23 -0500160 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_CANONICAL), std::to_string(type) + "=%2A");
Davide Pesaventobc17d952020-02-15 20:10:52 -0500161 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_ALTERNATE), compUri);
Davide Pesavento009062d2019-11-20 00:16:33 -0500162 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(comp), compUri);
Davide Pesavento8e047e12024-02-12 16:50:23 -0500163 BOOST_CHECK_EQUAL(comp, Component::fromUri(compUri));
164 BOOST_CHECK_EQUAL(comp, Component::fromUri(std::to_string(type) + "=%2A"));
Davide Pesavento009062d2019-11-20 00:16:33 -0500165 BOOST_CHECK_EQUAL(comp, Component::fromNumber(42, type));
166
Davide Pesavento21ee8992022-02-25 20:46:04 -0500167 const Component comp2(type, fromHex("010203")); // TLV-VALUE is *not* a NonNegativeInteger
Davide Pesavento009062d2019-11-20 00:16:33 -0500168 BOOST_CHECK_EQUAL(comp2.type(), type);
169 BOOST_CHECK_EQUAL(comp2.isNumber(), false);
Davide Pesavento8e047e12024-02-12 16:50:23 -0500170 const auto comp2Uri = std::to_string(type) + "=%01%02%03";
Davide Pesavento009062d2019-11-20 00:16:33 -0500171 BOOST_CHECK_EQUAL(comp2.toUri(), comp2Uri);
172 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(comp2), comp2Uri);
Davide Pesavento8e047e12024-02-12 16:50:23 -0500173 BOOST_CHECK_EQUAL(comp2, Component::fromUri(comp2Uri));
Davide Pesavento009062d2019-11-20 00:16:33 -0500174
Davide Pesavento8e047e12024-02-12 16:50:23 -0500175 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 Pesavento009062d2019-11-20 00:16:33 -0500184}
185
186BOOST_AUTO_TEST_CASE(Segment)
187{
188 testDecimalComponent(tlv::SegmentNameComponent, "seg=");
189}
190
191BOOST_AUTO_TEST_CASE(ByteOffset)
192{
193 testDecimalComponent(tlv::ByteOffsetNameComponent, "off=");
194}
195
196BOOST_AUTO_TEST_CASE(Version)
197{
198 testDecimalComponent(tlv::VersionNameComponent, "v=");
199}
200
201BOOST_AUTO_TEST_CASE(Timestamp)
202{
203 testDecimalComponent(tlv::TimestampNameComponent, "t=");
204}
205
206BOOST_AUTO_TEST_CASE(SequenceNum)
207{
208 testDecimalComponent(tlv::SequenceNumNameComponent, "seq=");
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000209}
210
Davide Pesavento21ee8992022-02-25 20:46:04 -0500211BOOST_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 Pesavento8e047e12024-02-12 16:50:23 -0500221 BOOST_CHECK_EQUAL(Component::fromUri("32=ndn-cxx"sv), comp);
Davide Pesavento21ee8992022-02-25 20:46:04 -0500222
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 Pesavento8e047e12024-02-12 16:50:23 -0500227 BOOST_CHECK_EQUAL(Component::fromUri("32=..."sv), comp);
Davide Pesavento21ee8992022-02-25 20:46:04 -0500228
Davide Pesavento8e047e12024-02-12 16:50:23 -0500229 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 Pesavento21ee8992022-02-25 20:46:04 -0500232}
233
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000234BOOST_AUTO_TEST_CASE(OtherType)
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000235{
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000236 Component comp("0907 6E646E2D637878"_block);
237 BOOST_CHECK_EQUAL(comp.type(), 0x09);
238 BOOST_CHECK_EQUAL(comp.toUri(), "9=ndn-cxx");
Davide Pesaventobc17d952020-02-15 20:10:52 -0500239 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 Pesavento8e047e12024-02-12 16:50:23 -0500243 BOOST_CHECK_EQUAL(Component::fromUri("9=ndn-cxx"sv), comp);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000244
245 comp.wireDecode("FDFFFF00"_block);
246 BOOST_CHECK_EQUAL(comp.type(), 0xFFFF);
247 BOOST_CHECK_EQUAL(comp.toUri(), "65535=...");
Davide Pesavento8e047e12024-02-12 16:50:23 -0500248 BOOST_CHECK_EQUAL(Component::fromUri("65535=..."sv), comp);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000249
250 comp.wireDecode("FD576501 2E"_block);
251 BOOST_CHECK_EQUAL(comp.type(), 0x5765);
252 BOOST_CHECK_EQUAL(comp.toUri(), "22373=....");
Davide Pesavento8e047e12024-02-12 16:50:23 -0500253 BOOST_CHECK_EQUAL(Component::fromUri("22373=...."sv), comp);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000254
Davide Pesavento8e047e12024-02-12 16:50:23 -0500255 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 Shicf4ac5b2018-03-28 22:46:06 +0000258}
259
260BOOST_AUTO_TEST_CASE(InvalidType)
261{
262 Component comp;
Davide Pesavento6b330402019-04-24 00:14:01 -0400263 BOOST_CHECK_THROW(comp.wireDecode(Block{}), Component::Error);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000264 BOOST_CHECK_THROW(comp.wireDecode("FE0001000001 80"_block), Component::Error);
265
Davide Pesavento8e047e12024-02-12 16:50:23 -0500266 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 Shidf4b24e2016-07-14 21:41:43 +0000281}
282
283BOOST_AUTO_TEST_SUITE_END() // Decode
284
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400285BOOST_AUTO_TEST_CASE(ConstructFromSpan)
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000286{
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400287 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 Shidf4b24e2016-07-14 21:41:43 +0000292
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400293 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 Pesavento08378cb2018-02-01 16:10:54 -0500305}
306
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400307BOOST_AUTO_TEST_SUITE(ConstructFromIterators) // Bug 2490
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000308
Davide Pesavento49e1e872023-11-11 00:45:23 -0500309using 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 Shidf4b24e2016-07-14 21:41:43 +0000315
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400316BOOST_AUTO_TEST_CASE_TEMPLATE(ZeroOctets, T, ContainerTypes)
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000317{
318 T bytes;
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000319 Component c(bytes.begin(), bytes.end());
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400320 BOOST_TEST(c.type() == tlv::GenericNameComponent);
321 BOOST_TEST(c.value_size() == 0);
322 BOOST_TEST(c.size() == 2);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000323}
324
325BOOST_AUTO_TEST_CASE_TEMPLATE(OneOctet, T, ContainerTypes)
326{
327 T bytes{1};
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400328 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 Shidf4b24e2016-07-14 21:41:43 +0000332}
333
334BOOST_AUTO_TEST_CASE_TEMPLATE(FourOctets, T, ContainerTypes)
335{
336 T bytes{1, 2, 3, 4};
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000337 Component c(0xFCEC, bytes.begin(), bytes.end());
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400338 BOOST_TEST(c.type() == 0xFCEC);
339 BOOST_TEST(c.value_size() == 4);
340 BOOST_TEST(c.size() == 8);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000341}
342
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400343BOOST_AUTO_TEST_SUITE_END() // ConstructFromIterators
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000344
Junxiao Shi71ff2312017-07-12 13:32:50 +0000345template<typename ArgType>
346struct ConventionTest
347{
Davide Pesavento009062d2019-11-20 00:16:33 -0500348 std::function<Component(ArgType)> makeComponent;
349 std::function<ArgType(const Component&)> getValue;
350 std::function<Name&(Name&, ArgType)> append;
Junxiao Shi71ff2312017-07-12 13:32:50 +0000351 Name expected;
352 ArgType value;
Davide Pesavento009062d2019-11-20 00:16:33 -0500353 std::function<bool(const Component&)> isComponent;
Junxiao Shi71ff2312017-07-12 13:32:50 +0000354};
355
Davide Pesavento0c526032024-01-31 21:14:01 -0500356struct ConventionMarker
Junxiao Shie2099612019-02-15 14:46:27 +0000357{
Eric Newberryc25e4632021-02-11 10:48:11 -0800358 ConventionMarker()
359 {
360 name::setConventionEncoding(name::Convention::MARKER);
361 }
362
363 ~ConventionMarker()
364 {
365 name::setConventionEncoding(name::Convention::TYPED);
366 }
Junxiao Shie2099612019-02-15 14:46:27 +0000367};
368
Davide Pesavento0c526032024-01-31 21:14:01 -0500369struct ConventionTyped
Junxiao Shie2099612019-02-15 14:46:27 +0000370{
Junxiao Shie2099612019-02-15 14:46:27 +0000371};
372
Davide Pesavento0c526032024-01-31 21:14:01 -0500373struct NumberWithMarker
Junxiao Shi71ff2312017-07-12 13:32:50 +0000374{
Junxiao Shie2099612019-02-15 14:46:27 +0000375 using ConventionRev = ConventionMarker;
376
Junxiao Shi71ff2312017-07-12 13:32:50 +0000377 ConventionTest<uint64_t>
378 operator()() const
379 {
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500380 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 Shi71ff2312017-07-12 13:32:50 +0000383 Name("/%AA%03%E8"),
384 1000,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500385 [] (const Component& c) { return c.isNumberWithMarker(0xAA); }};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000386 }
387};
388
Davide Pesavento0c526032024-01-31 21:14:01 -0500389struct SegmentMarker
Junxiao Shi71ff2312017-07-12 13:32:50 +0000390{
Junxiao Shie2099612019-02-15 14:46:27 +0000391 using ConventionRev = ConventionMarker;
392
Junxiao Shi71ff2312017-07-12 13:32:50 +0000393 ConventionTest<uint64_t>
394 operator()() const
395 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000396 return {&Component::fromSegment,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500397 &Component::toSegment,
398 &Name::appendSegment,
Junxiao Shi71ff2312017-07-12 13:32:50 +0000399 Name("/%00%27%10"),
400 10000,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500401 &Component::isSegment};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000402 }
403};
404
Davide Pesavento0c526032024-01-31 21:14:01 -0500405struct SegmentTyped
Junxiao Shi71ff2312017-07-12 13:32:50 +0000406{
Junxiao Shie2099612019-02-15 14:46:27 +0000407 using ConventionRev = ConventionTyped;
408
409 ConventionTest<uint64_t>
410 operator()() const
411 {
412 return {&Component::fromSegment,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500413 &Component::toSegment,
414 &Name::appendSegment,
Davide Pesavento47a94d12021-10-31 16:41:05 -0400415 Name("/50=%27%10"),
Junxiao Shie2099612019-02-15 14:46:27 +0000416 10000,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500417 &Component::isSegment};
Junxiao Shie2099612019-02-15 14:46:27 +0000418 }
419};
420
Davide Pesavento0c526032024-01-31 21:14:01 -0500421struct ByteOffsetTyped
Junxiao Shi71ff2312017-07-12 13:32:50 +0000422{
Junxiao Shie2099612019-02-15 14:46:27 +0000423 using ConventionRev = ConventionTyped;
424
425 ConventionTest<uint64_t>
426 operator()() const
427 {
428 return {&Component::fromByteOffset,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500429 &Component::toByteOffset,
430 &Name::appendByteOffset,
Davide Pesavento47a94d12021-10-31 16:41:05 -0400431 Name("/52=%00%01%86%A0"),
Junxiao Shie2099612019-02-15 14:46:27 +0000432 100000,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500433 &Component::isByteOffset};
Junxiao Shie2099612019-02-15 14:46:27 +0000434 }
435};
436
Davide Pesavento0c526032024-01-31 21:14:01 -0500437struct VersionMarker
Junxiao Shie2099612019-02-15 14:46:27 +0000438{
Junxiao Shie2099612019-02-15 14:46:27 +0000439 using ConventionRev = ConventionMarker;
440
Junxiao Shi71ff2312017-07-12 13:32:50 +0000441 ConventionTest<uint64_t>
442 operator()() const
443 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000444 return {&Component::fromVersion,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500445 &Component::toVersion,
446 [] (Name& name, auto version) -> Name& { return name.appendVersion(version); },
Junxiao Shi71ff2312017-07-12 13:32:50 +0000447 Name("/%FD%00%0FB%40"),
448 1000000,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500449 &Component::isVersion};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000450 }
451};
452
Davide Pesavento0c526032024-01-31 21:14:01 -0500453struct VersionTyped
Junxiao Shi71ff2312017-07-12 13:32:50 +0000454{
Junxiao Shie2099612019-02-15 14:46:27 +0000455 using ConventionRev = ConventionTyped;
456
457 ConventionTest<uint64_t>
458 operator()() const
459 {
460 return {&Component::fromVersion,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500461 &Component::toVersion,
462 [] (Name& name, auto version) -> Name& { return name.appendVersion(version); },
Davide Pesavento47a94d12021-10-31 16:41:05 -0400463 Name("/54=%00%0FB%40"),
Junxiao Shie2099612019-02-15 14:46:27 +0000464 1000000,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500465 &Component::isVersion};
Junxiao Shie2099612019-02-15 14:46:27 +0000466 }
467};
468
Davide Pesavento0c526032024-01-31 21:14:01 -0500469struct TimestampMarker
Junxiao Shie2099612019-02-15 14:46:27 +0000470{
Junxiao Shie2099612019-02-15 14:46:27 +0000471 using ConventionRev = ConventionMarker;
472
Davide Pesavento47a94d12021-10-31 16:41:05 -0400473 ConventionTest<time::system_clock::time_point>
Junxiao Shi71ff2312017-07-12 13:32:50 +0000474 operator()() const
475 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000476 return {&Component::fromTimestamp,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500477 &Component::toTimestamp,
478 [] (Name& name, auto tp) -> Name& { return name.appendTimestamp(tp); },
Junxiao Shi71ff2312017-07-12 13:32:50 +0000479 Name("/%FC%00%04%7BE%E3%1B%00%00"),
Davide Pesavento0f830802018-01-16 23:58:58 -0500480 time::getUnixEpoch() + 14600_days, // 40 years
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500481 &Component::isTimestamp};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000482 }
483};
484
Davide Pesavento0c526032024-01-31 21:14:01 -0500485struct TimestampTyped
Junxiao Shi71ff2312017-07-12 13:32:50 +0000486{
Junxiao Shie2099612019-02-15 14:46:27 +0000487 using ConventionRev = ConventionTyped;
488
Davide Pesavento47a94d12021-10-31 16:41:05 -0400489 ConventionTest<time::system_clock::time_point>
Junxiao Shie2099612019-02-15 14:46:27 +0000490 operator()() const
491 {
492 return {&Component::fromTimestamp,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500493 &Component::toTimestamp,
494 [] (Name& name, auto tp) -> Name& { return name.appendTimestamp(tp); },
Davide Pesavento47a94d12021-10-31 16:41:05 -0400495 Name("/56=%00%04%7BE%E3%1B%00%00"),
Junxiao Shie2099612019-02-15 14:46:27 +0000496 time::getUnixEpoch() + 14600_days, // 40 years
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500497 &Component::isTimestamp};
Junxiao Shie2099612019-02-15 14:46:27 +0000498 }
499};
500
Davide Pesavento0c526032024-01-31 21:14:01 -0500501struct SequenceNumberMarker
Junxiao Shie2099612019-02-15 14:46:27 +0000502{
Junxiao Shie2099612019-02-15 14:46:27 +0000503 using ConventionRev = ConventionMarker;
504
Junxiao Shi71ff2312017-07-12 13:32:50 +0000505 ConventionTest<uint64_t>
506 operator()() const
507 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000508 return {&Component::fromSequenceNumber,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500509 &Component::toSequenceNumber,
510 &Name::appendSequenceNumber,
Junxiao Shi71ff2312017-07-12 13:32:50 +0000511 Name("/%FE%00%98%96%80"),
512 10000000,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500513 &Component::isSequenceNumber};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000514 }
515};
516
Davide Pesavento0c526032024-01-31 21:14:01 -0500517struct SequenceNumberTyped
Junxiao Shie2099612019-02-15 14:46:27 +0000518{
Junxiao Shie2099612019-02-15 14:46:27 +0000519 using ConventionRev = ConventionTyped;
520
521 ConventionTest<uint64_t>
522 operator()() const
523 {
524 return {&Component::fromSequenceNumber,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500525 &Component::toSequenceNumber,
526 &Name::appendSequenceNumber,
Davide Pesavento47a94d12021-10-31 16:41:05 -0400527 Name("/58=%00%98%96%80"),
Junxiao Shie2099612019-02-15 14:46:27 +0000528 10000000,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500529 &Component::isSequenceNumber};
Junxiao Shie2099612019-02-15 14:46:27 +0000530 }
531};
532
Davide Pesavento0c526032024-01-31 21:14:01 -0500533using NamingConventionTests = boost::mp11::mp_list<
Junxiao Shi71ff2312017-07-12 13:32:50 +0000534 NumberWithMarker,
Junxiao Shie2099612019-02-15 14:46:27 +0000535 SegmentMarker,
536 SegmentTyped,
Junxiao Shie2099612019-02-15 14:46:27 +0000537 ByteOffsetTyped,
538 VersionMarker,
539 VersionTyped,
540 TimestampMarker,
541 TimestampTyped,
542 SequenceNumberMarker,
543 SequenceNumberTyped
Junxiao Shi71ff2312017-07-12 13:32:50 +0000544>;
545
Davide Pesavento0c526032024-01-31 21:14:01 -0500546BOOST_FIXTURE_TEST_CASE_TEMPLATE(NamingConvention, T, NamingConventionTests, T::ConventionRev)
Junxiao Shi71ff2312017-07-12 13:32:50 +0000547{
Junxiao Shi71ff2312017-07-12 13:32:50 +0000548 auto test = T()();
Junxiao Shi71ff2312017-07-12 13:32:50 +0000549
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000550 Component actualComponent = test.makeComponent(test.value);
Davide Pesavento0c526032024-01-31 21:14:01 -0500551 BOOST_CHECK_EQUAL(actualComponent, test.expected[0]);
Junxiao Shi71ff2312017-07-12 13:32:50 +0000552
553 Name actualName;
554 test.append(actualName, test.value);
Davide Pesavento0c526032024-01-31 21:14:01 -0500555 BOOST_CHECK_EQUAL(actualName, test.expected);
Junxiao Shi71ff2312017-07-12 13:32:50 +0000556
Davide Pesavento0c526032024-01-31 21:14:01 -0500557 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 Shi71ff2312017-07-12 13:32:50 +0000562
563 BOOST_CHECK_EQUAL(test.isComponent(invalidComponent1), false);
564 BOOST_CHECK_EQUAL(test.isComponent(invalidComponent2), false);
565
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000566 BOOST_CHECK_THROW(test.getValue(invalidComponent1), Component::Error);
567 BOOST_CHECK_THROW(test.getValue(invalidComponent2), Component::Error);
Junxiao Shi71ff2312017-07-12 13:32:50 +0000568}
569
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400570BOOST_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 Pesavento0c526032024-01-31 21:14:01 -0500597 BOOST_TEST_INFO_SCOPE("lhs = " << lhs);
598 BOOST_TEST_INFO_SCOPE("rhs = " << rhs);
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400599 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 Shidf4b24e2016-07-14 21:41:43 +0000609BOOST_AUTO_TEST_SUITE_END() // TestNameComponent
610
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400611} // namespace ndn::tests