blob: d4b674d8709f3a8b93e00a7c4c5f696446eca765 [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 Pesavento152ef442023-04-22 02:02:29 -04003 * Copyright (c) 2013-2023 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>
Junxiao Shidf4b24e2016-07-14 21:41:43 +000031#include <boost/mpl/vector.hpp>
32
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");
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000066 BOOST_CHECK_EQUAL(Component::fromEscapedString("ndn-cxx"), comp);
Junxiao Shid2e60632018-08-10 10:48:44 -060067 BOOST_CHECK_EQUAL(Component::fromEscapedString("8=ndn-cxx"), comp);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000068
69 comp.wireDecode("0800"_block);
70 BOOST_CHECK_EQUAL(comp.toUri(), "...");
71 BOOST_CHECK_EQUAL(Component::fromEscapedString("..."), comp);
Junxiao Shid2e60632018-08-10 10:48:44 -060072 BOOST_CHECK_EQUAL(Component::fromEscapedString("8=..."), comp);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000073 BOOST_CHECK_EQUAL(Component::fromEscapedString(".%2E."), comp);
74
75 comp.wireDecode("0801 2E"_block);
76 BOOST_CHECK_EQUAL(comp.toUri(), "....");
77 BOOST_CHECK_EQUAL(Component::fromEscapedString("...."), comp);
78 BOOST_CHECK_EQUAL(Component::fromEscapedString("%2E..%2E"), comp);
79
80 comp.wireDecode("0803 2E412E"_block);
81 BOOST_CHECK_EQUAL(comp.toUri(), ".A.");
82 BOOST_CHECK_EQUAL(Component::fromEscapedString(".A."), comp);
83
84 comp.wireDecode("0807 666F6F25626172"_block);
85 BOOST_CHECK_EQUAL(comp.toUri(), "foo%25bar");
86 BOOST_CHECK_EQUAL(Component::fromEscapedString("foo%25bar"), comp);
Junxiao Shid2e60632018-08-10 10:48:44 -060087 BOOST_CHECK_EQUAL(Component::fromEscapedString("8=foo%25bar"), comp);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000088
89 comp.wireDecode("0804 2D2E5F7E"_block);
90 BOOST_CHECK_EQUAL(comp.toUri(), "-._~");
91 BOOST_CHECK_EQUAL(Component::fromEscapedString("-._~"), comp);
92
Junxiao Shid2e60632018-08-10 10:48:44 -060093 comp.wireDecode("0803 393D41"_block);
94 BOOST_CHECK_EQUAL(comp.toUri(), "9%3DA");
95 BOOST_CHECK_EQUAL(Component::fromEscapedString("9%3DA"), comp);
96
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000097 comp = Component(":/?#[]@");
98 BOOST_CHECK_EQUAL(comp.toUri(), "%3A%2F%3F%23%5B%5D%40");
99 BOOST_CHECK_EQUAL(Component::fromEscapedString("%3A%2F%3F%23%5B%5D%40"), comp);
100
101 BOOST_CHECK_THROW(Component::fromEscapedString(""), Component::Error);
102 BOOST_CHECK_THROW(Component::fromEscapedString("."), Component::Error);
103 BOOST_CHECK_THROW(Component::fromEscapedString(".."), Component::Error);
Davide Pesavento21ee8992022-02-25 20:46:04 -0500104 BOOST_CHECK_THROW(Component::fromEscapedString("8="), Component::Error);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000105}
106
Junxiao Shi4053bd52018-08-16 13:39:25 -0600107static void
Davide Pesavento009062d2019-11-20 00:16:33 -0500108testSha256Component(uint32_t type, const std::string& uriPrefix)
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000109{
Davide Pesavento009062d2019-11-20 00:16:33 -0500110 const std::string hexLower = "28bad4b5275bd392dbb670c75cf0b66f13f7942b21e80f55c0e86b374753a548";
111 const std::string hexUpper = boost::to_upper_copy(hexLower);
Junxiao Shi4053bd52018-08-16 13:39:25 -0600112 std::string hexPct;
113 for (size_t i = 0; i < hexUpper.size(); i += 2) {
114 hexPct += "%" + hexUpper.substr(i, 2);
115 }
Junxiao Shia39c0b52019-12-31 15:13:15 -0700116 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 +0000117
Davide Pesavento21ee8992022-02-25 20:46:04 -0500118 Component comp(type, fromHex(hexLower));
Davide Pesavento009062d2019-11-20 00:16:33 -0500119
Junxiao Shi4053bd52018-08-16 13:39:25 -0600120 BOOST_CHECK_EQUAL(comp.type(), type);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000121 BOOST_CHECK_EQUAL(comp.toUri(), uriPrefix + hexLower);
Junxiao Shia39c0b52019-12-31 15:13:15 -0700122 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::CANONICAL), to_string(type) + "=" + hexPctCanonical);
123 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ALTERNATE), uriPrefix + hexLower);
Davide Pesaventobc17d952020-02-15 20:10:52 -0500124 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_CANONICAL), to_string(type) + "=" + hexPctCanonical);
125 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_ALTERNATE), uriPrefix + hexLower);
Davide Pesavento009062d2019-11-20 00:16:33 -0500126 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(comp), uriPrefix + hexLower);
127 BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(uriPrefix + hexLower));
128 BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(uriPrefix + hexUpper));
129 BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(to_string(type) + "=" + hexPct));
Junxiao Shia39c0b52019-12-31 15:13:15 -0700130 BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(to_string(type) + "=" + hexPctCanonical));
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000131
Davide Pesavento009062d2019-11-20 00:16:33 -0500132 CHECK_COMP_ERR(comp.wireDecode(Block(type, fromHex("A791806951F25C4D"))), "TLV-LENGTH must be 32");
133 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix), "TLV-LENGTH must be 32");
134 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "a791806951f25c4d"), "TLV-LENGTH must be 32");
135 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "foo"), "invalid hex encoding");
136 CHECK_COMP_ERR(Component::fromEscapedString(boost::to_upper_copy(uriPrefix) + hexLower), "Unknown TLV-TYPE");
Junxiao Shi4053bd52018-08-16 13:39:25 -0600137}
138
Davide Pesavento009062d2019-11-20 00:16:33 -0500139BOOST_AUTO_TEST_CASE(ImplicitDigest)
Junxiao Shi4053bd52018-08-16 13:39:25 -0600140{
Davide Pesavento009062d2019-11-20 00:16:33 -0500141 testSha256Component(tlv::ImplicitSha256DigestComponent, "sha256digest=");
Junxiao Shi4053bd52018-08-16 13:39:25 -0600142}
143
Davide Pesavento009062d2019-11-20 00:16:33 -0500144BOOST_AUTO_TEST_CASE(ParametersDigest)
Junxiao Shi4053bd52018-08-16 13:39:25 -0600145{
Davide Pesavento009062d2019-11-20 00:16:33 -0500146 testSha256Component(tlv::ParametersSha256DigestComponent, "params-sha256=");
147}
148
149static void
150testDecimalComponent(uint32_t type, const std::string& uriPrefix)
151{
Davide Pesavento47a94d12021-10-31 16:41:05 -0400152 const Component comp(makeNonNegativeIntegerBlock(type, 42)); // TLV-VALUE is a NonNegativeInteger
Davide Pesavento009062d2019-11-20 00:16:33 -0500153 BOOST_CHECK_EQUAL(comp.type(), type);
154 BOOST_CHECK_EQUAL(comp.isNumber(), true);
155 const auto compUri = uriPrefix + "42";
156 BOOST_CHECK_EQUAL(comp.toUri(), compUri);
Junxiao Shia39c0b52019-12-31 15:13:15 -0700157 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::CANONICAL), to_string(type) + "=%2A");
158 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ALTERNATE), compUri);
Davide Pesaventobc17d952020-02-15 20:10:52 -0500159 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_CANONICAL), to_string(type) + "=%2A");
160 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_ALTERNATE), compUri);
Davide Pesavento009062d2019-11-20 00:16:33 -0500161 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(comp), compUri);
162 BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(compUri));
163 BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(to_string(type) + "=%2A"));
164 BOOST_CHECK_EQUAL(comp, Component::fromNumber(42, type));
165
Davide Pesavento21ee8992022-02-25 20:46:04 -0500166 const Component comp2(type, fromHex("010203")); // TLV-VALUE is *not* a NonNegativeInteger
Davide Pesavento009062d2019-11-20 00:16:33 -0500167 BOOST_CHECK_EQUAL(comp2.type(), type);
168 BOOST_CHECK_EQUAL(comp2.isNumber(), false);
169 const auto comp2Uri = to_string(type) + "=%01%02%03";
170 BOOST_CHECK_EQUAL(comp2.toUri(), comp2Uri);
171 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(comp2), comp2Uri);
172 BOOST_CHECK_EQUAL(comp2, Component::fromEscapedString(comp2Uri));
173
174 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix), "invalid format");
175 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "foo"), "invalid format");
176 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "00"), "invalid format");
177 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "-1"), "invalid format");
178 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "9.3"), "invalid format");
179 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + " 84"), "invalid format");
180 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "0xAF"), "invalid format");
181 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "18446744073709551616"), "out of range");
182 CHECK_COMP_ERR(Component::fromEscapedString(boost::to_upper_copy(uriPrefix) + "42"), "Unknown TLV-TYPE");
183}
184
185BOOST_AUTO_TEST_CASE(Segment)
186{
187 testDecimalComponent(tlv::SegmentNameComponent, "seg=");
188}
189
190BOOST_AUTO_TEST_CASE(ByteOffset)
191{
192 testDecimalComponent(tlv::ByteOffsetNameComponent, "off=");
193}
194
195BOOST_AUTO_TEST_CASE(Version)
196{
197 testDecimalComponent(tlv::VersionNameComponent, "v=");
198}
199
200BOOST_AUTO_TEST_CASE(Timestamp)
201{
202 testDecimalComponent(tlv::TimestampNameComponent, "t=");
203}
204
205BOOST_AUTO_TEST_CASE(SequenceNum)
206{
207 testDecimalComponent(tlv::SequenceNumNameComponent, "seq=");
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000208}
209
Davide Pesavento21ee8992022-02-25 20:46:04 -0500210BOOST_AUTO_TEST_CASE(Keyword)
211{
212 Component comp("2007 6E646E2D637878"_block);
213 BOOST_CHECK_EQUAL(comp.type(), tlv::KeywordNameComponent);
214 BOOST_CHECK_EQUAL(comp.isKeyword(), true);
215 BOOST_CHECK_EQUAL(comp.toUri(), "32=ndn-cxx");
216 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::CANONICAL), "32=ndn-cxx");
217 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ALTERNATE), "32=ndn-cxx");
218 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_CANONICAL), "32=ndn-cxx");
219 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_ALTERNATE), "32=ndn-cxx");
220 BOOST_CHECK_EQUAL(Component::fromEscapedString("32=ndn-cxx"), comp);
221
222 comp.wireDecode("2000"_block);
223 BOOST_CHECK_EQUAL(comp.type(), tlv::KeywordNameComponent);
224 BOOST_CHECK_EQUAL(comp.isKeyword(), true);
225 BOOST_CHECK_EQUAL(comp.toUri(), "32=...");
226 BOOST_CHECK_EQUAL(Component::fromEscapedString("32=..."), comp);
227
228 BOOST_CHECK_THROW(Component::fromEscapedString("32="), Component::Error);
229 BOOST_CHECK_THROW(Component::fromEscapedString("32=."), Component::Error);
230 BOOST_CHECK_THROW(Component::fromEscapedString("32=.."), Component::Error);
231}
232
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000233BOOST_AUTO_TEST_CASE(OtherType)
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000234{
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000235 Component comp("0907 6E646E2D637878"_block);
236 BOOST_CHECK_EQUAL(comp.type(), 0x09);
237 BOOST_CHECK_EQUAL(comp.toUri(), "9=ndn-cxx");
Davide Pesaventobc17d952020-02-15 20:10:52 -0500238 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::CANONICAL), "9=ndn-cxx");
239 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ALTERNATE), "9=ndn-cxx");
240 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_CANONICAL), "9=ndn-cxx");
241 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_ALTERNATE), "9=ndn-cxx");
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000242 BOOST_CHECK_EQUAL(Component::fromEscapedString("9=ndn-cxx"), comp);
243
244 comp.wireDecode("FDFFFF00"_block);
245 BOOST_CHECK_EQUAL(comp.type(), 0xFFFF);
246 BOOST_CHECK_EQUAL(comp.toUri(), "65535=...");
247 BOOST_CHECK_EQUAL(Component::fromEscapedString("65535=..."), comp);
248
249 comp.wireDecode("FD576501 2E"_block);
250 BOOST_CHECK_EQUAL(comp.type(), 0x5765);
251 BOOST_CHECK_EQUAL(comp.toUri(), "22373=....");
252 BOOST_CHECK_EQUAL(Component::fromEscapedString("22373=...."), comp);
253
254 BOOST_CHECK_THROW(Component::fromEscapedString("3="), Component::Error);
255 BOOST_CHECK_THROW(Component::fromEscapedString("3=."), Component::Error);
256 BOOST_CHECK_THROW(Component::fromEscapedString("3=.."), Component::Error);
257}
258
259BOOST_AUTO_TEST_CASE(InvalidType)
260{
261 Component comp;
Davide Pesavento6b330402019-04-24 00:14:01 -0400262 BOOST_CHECK_THROW(comp.wireDecode(Block{}), Component::Error);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000263 BOOST_CHECK_THROW(comp.wireDecode("FE0001000001 80"_block), Component::Error);
264
265 BOOST_CHECK_THROW(Component::fromEscapedString("0=A"), Component::Error);
266 BOOST_CHECK_THROW(Component::fromEscapedString("65536=A"), Component::Error);
Junxiao Shid2e60632018-08-10 10:48:44 -0600267 BOOST_CHECK_THROW(Component::fromEscapedString("4294967296=A"), Component::Error);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000268 BOOST_CHECK_THROW(Component::fromEscapedString("-1=A"), Component::Error);
269 BOOST_CHECK_THROW(Component::fromEscapedString("+=A"), Component::Error);
270 BOOST_CHECK_THROW(Component::fromEscapedString("=A"), Component::Error);
271 BOOST_CHECK_THROW(Component::fromEscapedString("0x1=A"), Component::Error);
272 BOOST_CHECK_THROW(Component::fromEscapedString("Z=A"), Component::Error);
273 BOOST_CHECK_THROW(Component::fromEscapedString("09=A"), Component::Error);
Junxiao Shi4053bd52018-08-16 13:39:25 -0600274 BOOST_CHECK_THROW(Component::fromEscapedString("0x3=A"), Component::Error);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000275 BOOST_CHECK_THROW(Component::fromEscapedString("+9=A"), Component::Error);
Junxiao Shid2e60632018-08-10 10:48:44 -0600276 BOOST_CHECK_THROW(Component::fromEscapedString(" 9=A"), Component::Error);
277 BOOST_CHECK_THROW(Component::fromEscapedString("9 =A"), Component::Error);
278 BOOST_CHECK_THROW(Component::fromEscapedString("9.0=A"), Component::Error);
279 BOOST_CHECK_THROW(Component::fromEscapedString("9E0=A"), Component::Error);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000280}
281
282BOOST_AUTO_TEST_SUITE_END() // Decode
283
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400284BOOST_AUTO_TEST_CASE(ConstructFromSpan)
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000285{
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400286 const uint8_t arr[] = {1, 2, 3};
287 Component c1(arr);
288 BOOST_TEST(c1.wireEncode() == "0803010203"_block);
289 Component c2(128, arr);
290 BOOST_TEST(c2.wireEncode() == "8003010203"_block);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000291
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400292 const std::vector<uint8_t> vec = {4, 5, 6};
293 Component c3(vec);
294 BOOST_TEST(c3.wireEncode() == "0803040506"_block);
295 Component c4(128, vec);
296 BOOST_TEST(c4.wireEncode() == "8003040506"_block);
297
298 Component c5(128, {7, 8});
299 BOOST_TEST(c5.wireEncode() == "80020708"_block);
300
301 const Block b("090109"_block);
302 Component c6(128, b);
303 BOOST_TEST(c6.wireEncode() == "8003090109"_block);
Davide Pesavento08378cb2018-02-01 16:10:54 -0500304}
305
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400306BOOST_AUTO_TEST_SUITE(ConstructFromIterators) // Bug 2490
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000307
Davide Pesavento009062d2019-11-20 00:16:33 -0500308using ContainerTypes = boost::mpl::vector<std::vector<uint8_t>,
309 std::list<uint8_t>,
310 std::vector<int8_t>,
311 std::list<int8_t>>;
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000312
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400313BOOST_AUTO_TEST_CASE_TEMPLATE(ZeroOctets, T, ContainerTypes)
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000314{
315 T bytes;
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000316 Component c(bytes.begin(), bytes.end());
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400317 BOOST_TEST(c.type() == tlv::GenericNameComponent);
318 BOOST_TEST(c.value_size() == 0);
319 BOOST_TEST(c.size() == 2);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000320}
321
322BOOST_AUTO_TEST_CASE_TEMPLATE(OneOctet, T, ContainerTypes)
323{
324 T bytes{1};
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400325 Component c(9, bytes.begin(), bytes.end());
326 BOOST_TEST(c.type() == 0x09);
327 BOOST_TEST(c.value_size() == 1);
328 BOOST_TEST(c.size() == 3);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000329}
330
331BOOST_AUTO_TEST_CASE_TEMPLATE(FourOctets, T, ContainerTypes)
332{
333 T bytes{1, 2, 3, 4};
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000334 Component c(0xFCEC, bytes.begin(), bytes.end());
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400335 BOOST_TEST(c.type() == 0xFCEC);
336 BOOST_TEST(c.value_size() == 4);
337 BOOST_TEST(c.size() == 8);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000338}
339
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400340BOOST_AUTO_TEST_SUITE_END() // ConstructFromIterators
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000341
Junxiao Shi71ff2312017-07-12 13:32:50 +0000342BOOST_AUTO_TEST_SUITE(NamingConvention)
343
344template<typename ArgType>
345struct ConventionTest
346{
Davide Pesavento009062d2019-11-20 00:16:33 -0500347 std::function<Component(ArgType)> makeComponent;
348 std::function<ArgType(const Component&)> getValue;
349 std::function<Name&(Name&, ArgType)> append;
Junxiao Shi71ff2312017-07-12 13:32:50 +0000350 Name expected;
351 ArgType value;
Davide Pesavento009062d2019-11-20 00:16:33 -0500352 std::function<bool(const Component&)> isComponent;
Junxiao Shi71ff2312017-07-12 13:32:50 +0000353};
354
Junxiao Shie2099612019-02-15 14:46:27 +0000355class ConventionMarker
356{
Eric Newberryc25e4632021-02-11 10:48:11 -0800357public:
358 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
369class ConventionTyped
370{
Junxiao Shie2099612019-02-15 14:46:27 +0000371};
372
Junxiao Shi71ff2312017-07-12 13:32:50 +0000373class NumberWithMarker
374{
375public:
Junxiao Shie2099612019-02-15 14:46:27 +0000376 using ConventionRev = ConventionMarker;
377
Junxiao Shi71ff2312017-07-12 13:32:50 +0000378 ConventionTest<uint64_t>
379 operator()() const
380 {
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500381 return {[] (auto num) { return Component::fromNumberWithMarker(0xAA, num); },
382 [] (const Component& c) { return c.toNumberWithMarker(0xAA); },
383 [] (Name& name, auto num) -> Name& { return name.appendNumberWithMarker(0xAA, num); },
Junxiao Shi71ff2312017-07-12 13:32:50 +0000384 Name("/%AA%03%E8"),
385 1000,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500386 [] (const Component& c) { return c.isNumberWithMarker(0xAA); }};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000387 }
388};
389
Junxiao Shie2099612019-02-15 14:46:27 +0000390class SegmentMarker
Junxiao Shi71ff2312017-07-12 13:32:50 +0000391{
392public:
Junxiao Shie2099612019-02-15 14:46:27 +0000393 using ConventionRev = ConventionMarker;
394
Junxiao Shi71ff2312017-07-12 13:32:50 +0000395 ConventionTest<uint64_t>
396 operator()() const
397 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000398 return {&Component::fromSegment,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500399 &Component::toSegment,
400 &Name::appendSegment,
Junxiao Shi71ff2312017-07-12 13:32:50 +0000401 Name("/%00%27%10"),
402 10000,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500403 &Component::isSegment};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000404 }
405};
406
Junxiao Shie2099612019-02-15 14:46:27 +0000407class SegmentTyped
Junxiao Shi71ff2312017-07-12 13:32:50 +0000408{
409public:
Junxiao Shie2099612019-02-15 14:46:27 +0000410 using ConventionRev = ConventionTyped;
411
412 ConventionTest<uint64_t>
413 operator()() const
414 {
415 return {&Component::fromSegment,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500416 &Component::toSegment,
417 &Name::appendSegment,
Davide Pesavento47a94d12021-10-31 16:41:05 -0400418 Name("/50=%27%10"),
Junxiao Shie2099612019-02-15 14:46:27 +0000419 10000,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500420 &Component::isSegment};
Junxiao Shie2099612019-02-15 14:46:27 +0000421 }
422};
423
Junxiao Shie2099612019-02-15 14:46:27 +0000424class ByteOffsetTyped
Junxiao Shi71ff2312017-07-12 13:32:50 +0000425{
426public:
Junxiao Shie2099612019-02-15 14:46:27 +0000427 using ConventionRev = ConventionTyped;
428
429 ConventionTest<uint64_t>
430 operator()() const
431 {
432 return {&Component::fromByteOffset,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500433 &Component::toByteOffset,
434 &Name::appendByteOffset,
Davide Pesavento47a94d12021-10-31 16:41:05 -0400435 Name("/52=%00%01%86%A0"),
Junxiao Shie2099612019-02-15 14:46:27 +0000436 100000,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500437 &Component::isByteOffset};
Junxiao Shie2099612019-02-15 14:46:27 +0000438 }
439};
440
441class VersionMarker
442{
443public:
444 using ConventionRev = ConventionMarker;
445
Junxiao Shi71ff2312017-07-12 13:32:50 +0000446 ConventionTest<uint64_t>
447 operator()() const
448 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000449 return {&Component::fromVersion,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500450 &Component::toVersion,
451 [] (Name& name, auto version) -> Name& { return name.appendVersion(version); },
Junxiao Shi71ff2312017-07-12 13:32:50 +0000452 Name("/%FD%00%0FB%40"),
453 1000000,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500454 &Component::isVersion};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000455 }
456};
457
Junxiao Shie2099612019-02-15 14:46:27 +0000458class VersionTyped
Junxiao Shi71ff2312017-07-12 13:32:50 +0000459{
460public:
Junxiao Shie2099612019-02-15 14:46:27 +0000461 using ConventionRev = ConventionTyped;
462
463 ConventionTest<uint64_t>
464 operator()() const
465 {
466 return {&Component::fromVersion,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500467 &Component::toVersion,
468 [] (Name& name, auto version) -> Name& { return name.appendVersion(version); },
Davide Pesavento47a94d12021-10-31 16:41:05 -0400469 Name("/54=%00%0FB%40"),
Junxiao Shie2099612019-02-15 14:46:27 +0000470 1000000,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500471 &Component::isVersion};
Junxiao Shie2099612019-02-15 14:46:27 +0000472 }
473};
474
475class TimestampMarker
476{
477public:
478 using ConventionRev = ConventionMarker;
479
Davide Pesavento47a94d12021-10-31 16:41:05 -0400480 ConventionTest<time::system_clock::time_point>
Junxiao Shi71ff2312017-07-12 13:32:50 +0000481 operator()() const
482 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000483 return {&Component::fromTimestamp,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500484 &Component::toTimestamp,
485 [] (Name& name, auto tp) -> Name& { return name.appendTimestamp(tp); },
Junxiao Shi71ff2312017-07-12 13:32:50 +0000486 Name("/%FC%00%04%7BE%E3%1B%00%00"),
Davide Pesavento0f830802018-01-16 23:58:58 -0500487 time::getUnixEpoch() + 14600_days, // 40 years
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500488 &Component::isTimestamp};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000489 }
490};
491
Junxiao Shie2099612019-02-15 14:46:27 +0000492class TimestampTyped
Junxiao Shi71ff2312017-07-12 13:32:50 +0000493{
494public:
Junxiao Shie2099612019-02-15 14:46:27 +0000495 using ConventionRev = ConventionTyped;
496
Davide Pesavento47a94d12021-10-31 16:41:05 -0400497 ConventionTest<time::system_clock::time_point>
Junxiao Shie2099612019-02-15 14:46:27 +0000498 operator()() const
499 {
500 return {&Component::fromTimestamp,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500501 &Component::toTimestamp,
502 [] (Name& name, auto tp) -> Name& { return name.appendTimestamp(tp); },
Davide Pesavento47a94d12021-10-31 16:41:05 -0400503 Name("/56=%00%04%7BE%E3%1B%00%00"),
Junxiao Shie2099612019-02-15 14:46:27 +0000504 time::getUnixEpoch() + 14600_days, // 40 years
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500505 &Component::isTimestamp};
Junxiao Shie2099612019-02-15 14:46:27 +0000506 }
507};
508
509class SequenceNumberMarker
510{
511public:
512 using ConventionRev = ConventionMarker;
513
Junxiao Shi71ff2312017-07-12 13:32:50 +0000514 ConventionTest<uint64_t>
515 operator()() const
516 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000517 return {&Component::fromSequenceNumber,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500518 &Component::toSequenceNumber,
519 &Name::appendSequenceNumber,
Junxiao Shi71ff2312017-07-12 13:32:50 +0000520 Name("/%FE%00%98%96%80"),
521 10000000,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500522 &Component::isSequenceNumber};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000523 }
524};
525
Junxiao Shie2099612019-02-15 14:46:27 +0000526class SequenceNumberTyped
527{
528public:
529 using ConventionRev = ConventionTyped;
530
531 ConventionTest<uint64_t>
532 operator()() const
533 {
534 return {&Component::fromSequenceNumber,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500535 &Component::toSequenceNumber,
536 &Name::appendSequenceNumber,
Davide Pesavento47a94d12021-10-31 16:41:05 -0400537 Name("/58=%00%98%96%80"),
Junxiao Shie2099612019-02-15 14:46:27 +0000538 10000000,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500539 &Component::isSequenceNumber};
Junxiao Shie2099612019-02-15 14:46:27 +0000540 }
541};
542
Junxiao Shi71ff2312017-07-12 13:32:50 +0000543using ConventionTests = boost::mpl::vector<
544 NumberWithMarker,
Junxiao Shie2099612019-02-15 14:46:27 +0000545 SegmentMarker,
546 SegmentTyped,
Junxiao Shie2099612019-02-15 14:46:27 +0000547 ByteOffsetTyped,
548 VersionMarker,
549 VersionTyped,
550 TimestampMarker,
551 TimestampTyped,
552 SequenceNumberMarker,
553 SequenceNumberTyped
Junxiao Shi71ff2312017-07-12 13:32:50 +0000554>;
555
Junxiao Shie2099612019-02-15 14:46:27 +0000556BOOST_FIXTURE_TEST_CASE_TEMPLATE(Convention, T, ConventionTests, T::ConventionRev)
Junxiao Shi71ff2312017-07-12 13:32:50 +0000557{
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000558 Component invalidComponent1;
559 Component invalidComponent2("1234567890");
Junxiao Shi71ff2312017-07-12 13:32:50 +0000560
561 auto test = T()();
Junxiao Shi71ff2312017-07-12 13:32:50 +0000562 const Name& expected = test.expected;
Junxiao Shi71ff2312017-07-12 13:32:50 +0000563
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000564 Component actualComponent = test.makeComponent(test.value);
Junxiao Shi71ff2312017-07-12 13:32:50 +0000565 BOOST_CHECK_EQUAL(actualComponent, expected[0]);
566
567 Name actualName;
568 test.append(actualName, test.value);
569 BOOST_CHECK_EQUAL(actualName, expected);
570
571 BOOST_CHECK_EQUAL(test.isComponent(expected[0]), true);
572 BOOST_CHECK_EQUAL(test.getValue(expected[0]), test.value);
573
574 BOOST_CHECK_EQUAL(test.isComponent(invalidComponent1), false);
575 BOOST_CHECK_EQUAL(test.isComponent(invalidComponent2), false);
576
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000577 BOOST_CHECK_THROW(test.getValue(invalidComponent1), Component::Error);
578 BOOST_CHECK_THROW(test.getValue(invalidComponent2), Component::Error);
Junxiao Shi71ff2312017-07-12 13:32:50 +0000579}
580
581BOOST_AUTO_TEST_SUITE_END() // NamingConvention
582
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400583BOOST_AUTO_TEST_CASE(Compare)
584{
585 const std::vector<Component> comps = {
586 Component("0120 0000000000000000000000000000000000000000000000000000000000000000"_block),
587 Component("0120 0000000000000000000000000000000000000000000000000000000000000001"_block),
588 Component("0120 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"_block),
589 Component("0220 0000000000000000000000000000000000000000000000000000000000000000"_block),
590 Component("0220 0000000000000000000000000000000000000000000000000000000000000001"_block),
591 Component("0220 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"_block),
592 Component(0x03),
593 Component("0301 44"_block),
594 Component("0301 46"_block),
595 Component("0302 4141"_block),
596 Component(),
597 Component("D"),
598 Component("F"),
599 Component("AA"),
600 Component(0x53B2),
601 Component("FD53B201 44"_block),
602 Component("FD53B201 46"_block),
603 Component("FD53B202 4141"_block),
604 };
605
606 for (size_t i = 0; i < comps.size(); ++i) {
607 for (size_t j = 0; j < comps.size(); ++j) {
608 const auto& lhs = comps[i];
609 const auto& rhs = comps[j];
610 BOOST_CHECK_EQUAL(lhs == rhs, i == j);
611 BOOST_CHECK_EQUAL(lhs != rhs, i != j);
612 BOOST_CHECK_EQUAL(lhs < rhs, i < j);
613 BOOST_CHECK_EQUAL(lhs <= rhs, i <= j);
614 BOOST_CHECK_EQUAL(lhs > rhs, i > j);
615 BOOST_CHECK_EQUAL(lhs >= rhs, i >= j);
616 }
617 }
618}
619
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000620BOOST_AUTO_TEST_SUITE_END() // TestNameComponent
621
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400622} // namespace ndn::tests