blob: 73a423bb3033f3ae99bb92ea0f2b9baa1edc525a [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>
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");
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 Pesavento49e1e872023-11-11 00:45:23 -0500308using ContainerTypes = boost::mp11::mp_list<
309 std::vector<uint8_t>,
310 std::list<uint8_t>,
311 std::vector<int8_t>,
312 std::list<int8_t>
313>;
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000314
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400315BOOST_AUTO_TEST_CASE_TEMPLATE(ZeroOctets, T, ContainerTypes)
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000316{
317 T bytes;
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000318 Component c(bytes.begin(), bytes.end());
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400319 BOOST_TEST(c.type() == tlv::GenericNameComponent);
320 BOOST_TEST(c.value_size() == 0);
321 BOOST_TEST(c.size() == 2);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000322}
323
324BOOST_AUTO_TEST_CASE_TEMPLATE(OneOctet, T, ContainerTypes)
325{
326 T bytes{1};
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400327 Component c(9, bytes.begin(), bytes.end());
328 BOOST_TEST(c.type() == 0x09);
329 BOOST_TEST(c.value_size() == 1);
330 BOOST_TEST(c.size() == 3);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000331}
332
333BOOST_AUTO_TEST_CASE_TEMPLATE(FourOctets, T, ContainerTypes)
334{
335 T bytes{1, 2, 3, 4};
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000336 Component c(0xFCEC, bytes.begin(), bytes.end());
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400337 BOOST_TEST(c.type() == 0xFCEC);
338 BOOST_TEST(c.value_size() == 4);
339 BOOST_TEST(c.size() == 8);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000340}
341
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400342BOOST_AUTO_TEST_SUITE_END() // ConstructFromIterators
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000343
Junxiao Shi71ff2312017-07-12 13:32:50 +0000344BOOST_AUTO_TEST_SUITE(NamingConvention)
345
346template<typename ArgType>
347struct ConventionTest
348{
Davide Pesavento009062d2019-11-20 00:16:33 -0500349 std::function<Component(ArgType)> makeComponent;
350 std::function<ArgType(const Component&)> getValue;
351 std::function<Name&(Name&, ArgType)> append;
Junxiao Shi71ff2312017-07-12 13:32:50 +0000352 Name expected;
353 ArgType value;
Davide Pesavento009062d2019-11-20 00:16:33 -0500354 std::function<bool(const Component&)> isComponent;
Junxiao Shi71ff2312017-07-12 13:32:50 +0000355};
356
Junxiao Shie2099612019-02-15 14:46:27 +0000357class ConventionMarker
358{
Eric Newberryc25e4632021-02-11 10:48:11 -0800359public:
360 ConventionMarker()
361 {
362 name::setConventionEncoding(name::Convention::MARKER);
363 }
364
365 ~ConventionMarker()
366 {
367 name::setConventionEncoding(name::Convention::TYPED);
368 }
Junxiao Shie2099612019-02-15 14:46:27 +0000369};
370
371class ConventionTyped
372{
Junxiao Shie2099612019-02-15 14:46:27 +0000373};
374
Junxiao Shi71ff2312017-07-12 13:32:50 +0000375class NumberWithMarker
376{
377public:
Junxiao Shie2099612019-02-15 14:46:27 +0000378 using ConventionRev = ConventionMarker;
379
Junxiao Shi71ff2312017-07-12 13:32:50 +0000380 ConventionTest<uint64_t>
381 operator()() const
382 {
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500383 return {[] (auto num) { return Component::fromNumberWithMarker(0xAA, num); },
384 [] (const Component& c) { return c.toNumberWithMarker(0xAA); },
385 [] (Name& name, auto num) -> Name& { return name.appendNumberWithMarker(0xAA, num); },
Junxiao Shi71ff2312017-07-12 13:32:50 +0000386 Name("/%AA%03%E8"),
387 1000,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500388 [] (const Component& c) { return c.isNumberWithMarker(0xAA); }};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000389 }
390};
391
Junxiao Shie2099612019-02-15 14:46:27 +0000392class SegmentMarker
Junxiao Shi71ff2312017-07-12 13:32:50 +0000393{
394public:
Junxiao Shie2099612019-02-15 14:46:27 +0000395 using ConventionRev = ConventionMarker;
396
Junxiao Shi71ff2312017-07-12 13:32:50 +0000397 ConventionTest<uint64_t>
398 operator()() const
399 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000400 return {&Component::fromSegment,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500401 &Component::toSegment,
402 &Name::appendSegment,
Junxiao Shi71ff2312017-07-12 13:32:50 +0000403 Name("/%00%27%10"),
404 10000,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500405 &Component::isSegment};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000406 }
407};
408
Junxiao Shie2099612019-02-15 14:46:27 +0000409class SegmentTyped
Junxiao Shi71ff2312017-07-12 13:32:50 +0000410{
411public:
Junxiao Shie2099612019-02-15 14:46:27 +0000412 using ConventionRev = ConventionTyped;
413
414 ConventionTest<uint64_t>
415 operator()() const
416 {
417 return {&Component::fromSegment,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500418 &Component::toSegment,
419 &Name::appendSegment,
Davide Pesavento47a94d12021-10-31 16:41:05 -0400420 Name("/50=%27%10"),
Junxiao Shie2099612019-02-15 14:46:27 +0000421 10000,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500422 &Component::isSegment};
Junxiao Shie2099612019-02-15 14:46:27 +0000423 }
424};
425
Junxiao Shie2099612019-02-15 14:46:27 +0000426class ByteOffsetTyped
Junxiao Shi71ff2312017-07-12 13:32:50 +0000427{
428public:
Junxiao Shie2099612019-02-15 14:46:27 +0000429 using ConventionRev = ConventionTyped;
430
431 ConventionTest<uint64_t>
432 operator()() const
433 {
434 return {&Component::fromByteOffset,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500435 &Component::toByteOffset,
436 &Name::appendByteOffset,
Davide Pesavento47a94d12021-10-31 16:41:05 -0400437 Name("/52=%00%01%86%A0"),
Junxiao Shie2099612019-02-15 14:46:27 +0000438 100000,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500439 &Component::isByteOffset};
Junxiao Shie2099612019-02-15 14:46:27 +0000440 }
441};
442
443class VersionMarker
444{
445public:
446 using ConventionRev = ConventionMarker;
447
Junxiao Shi71ff2312017-07-12 13:32:50 +0000448 ConventionTest<uint64_t>
449 operator()() const
450 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000451 return {&Component::fromVersion,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500452 &Component::toVersion,
453 [] (Name& name, auto version) -> Name& { return name.appendVersion(version); },
Junxiao Shi71ff2312017-07-12 13:32:50 +0000454 Name("/%FD%00%0FB%40"),
455 1000000,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500456 &Component::isVersion};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000457 }
458};
459
Junxiao Shie2099612019-02-15 14:46:27 +0000460class VersionTyped
Junxiao Shi71ff2312017-07-12 13:32:50 +0000461{
462public:
Junxiao Shie2099612019-02-15 14:46:27 +0000463 using ConventionRev = ConventionTyped;
464
465 ConventionTest<uint64_t>
466 operator()() const
467 {
468 return {&Component::fromVersion,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500469 &Component::toVersion,
470 [] (Name& name, auto version) -> Name& { return name.appendVersion(version); },
Davide Pesavento47a94d12021-10-31 16:41:05 -0400471 Name("/54=%00%0FB%40"),
Junxiao Shie2099612019-02-15 14:46:27 +0000472 1000000,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500473 &Component::isVersion};
Junxiao Shie2099612019-02-15 14:46:27 +0000474 }
475};
476
477class TimestampMarker
478{
479public:
480 using ConventionRev = ConventionMarker;
481
Davide Pesavento47a94d12021-10-31 16:41:05 -0400482 ConventionTest<time::system_clock::time_point>
Junxiao Shi71ff2312017-07-12 13:32:50 +0000483 operator()() const
484 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000485 return {&Component::fromTimestamp,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500486 &Component::toTimestamp,
487 [] (Name& name, auto tp) -> Name& { return name.appendTimestamp(tp); },
Junxiao Shi71ff2312017-07-12 13:32:50 +0000488 Name("/%FC%00%04%7BE%E3%1B%00%00"),
Davide Pesavento0f830802018-01-16 23:58:58 -0500489 time::getUnixEpoch() + 14600_days, // 40 years
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500490 &Component::isTimestamp};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000491 }
492};
493
Junxiao Shie2099612019-02-15 14:46:27 +0000494class TimestampTyped
Junxiao Shi71ff2312017-07-12 13:32:50 +0000495{
496public:
Junxiao Shie2099612019-02-15 14:46:27 +0000497 using ConventionRev = ConventionTyped;
498
Davide Pesavento47a94d12021-10-31 16:41:05 -0400499 ConventionTest<time::system_clock::time_point>
Junxiao Shie2099612019-02-15 14:46:27 +0000500 operator()() const
501 {
502 return {&Component::fromTimestamp,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500503 &Component::toTimestamp,
504 [] (Name& name, auto tp) -> Name& { return name.appendTimestamp(tp); },
Davide Pesavento47a94d12021-10-31 16:41:05 -0400505 Name("/56=%00%04%7BE%E3%1B%00%00"),
Junxiao Shie2099612019-02-15 14:46:27 +0000506 time::getUnixEpoch() + 14600_days, // 40 years
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500507 &Component::isTimestamp};
Junxiao Shie2099612019-02-15 14:46:27 +0000508 }
509};
510
511class SequenceNumberMarker
512{
513public:
514 using ConventionRev = ConventionMarker;
515
Junxiao Shi71ff2312017-07-12 13:32:50 +0000516 ConventionTest<uint64_t>
517 operator()() const
518 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000519 return {&Component::fromSequenceNumber,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500520 &Component::toSequenceNumber,
521 &Name::appendSequenceNumber,
Junxiao Shi71ff2312017-07-12 13:32:50 +0000522 Name("/%FE%00%98%96%80"),
523 10000000,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500524 &Component::isSequenceNumber};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000525 }
526};
527
Junxiao Shie2099612019-02-15 14:46:27 +0000528class SequenceNumberTyped
529{
530public:
531 using ConventionRev = ConventionTyped;
532
533 ConventionTest<uint64_t>
534 operator()() const
535 {
536 return {&Component::fromSequenceNumber,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500537 &Component::toSequenceNumber,
538 &Name::appendSequenceNumber,
Davide Pesavento47a94d12021-10-31 16:41:05 -0400539 Name("/58=%00%98%96%80"),
Junxiao Shie2099612019-02-15 14:46:27 +0000540 10000000,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500541 &Component::isSequenceNumber};
Junxiao Shie2099612019-02-15 14:46:27 +0000542 }
543};
544
Davide Pesavento49e1e872023-11-11 00:45:23 -0500545using ConventionTests = boost::mp11::mp_list<
Junxiao Shi71ff2312017-07-12 13:32:50 +0000546 NumberWithMarker,
Junxiao Shie2099612019-02-15 14:46:27 +0000547 SegmentMarker,
548 SegmentTyped,
Junxiao Shie2099612019-02-15 14:46:27 +0000549 ByteOffsetTyped,
550 VersionMarker,
551 VersionTyped,
552 TimestampMarker,
553 TimestampTyped,
554 SequenceNumberMarker,
555 SequenceNumberTyped
Junxiao Shi71ff2312017-07-12 13:32:50 +0000556>;
557
Junxiao Shie2099612019-02-15 14:46:27 +0000558BOOST_FIXTURE_TEST_CASE_TEMPLATE(Convention, T, ConventionTests, T::ConventionRev)
Junxiao Shi71ff2312017-07-12 13:32:50 +0000559{
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000560 Component invalidComponent1;
561 Component invalidComponent2("1234567890");
Junxiao Shi71ff2312017-07-12 13:32:50 +0000562
563 auto test = T()();
Junxiao Shi71ff2312017-07-12 13:32:50 +0000564 const Name& expected = test.expected;
Junxiao Shi71ff2312017-07-12 13:32:50 +0000565
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000566 Component actualComponent = test.makeComponent(test.value);
Junxiao Shi71ff2312017-07-12 13:32:50 +0000567 BOOST_CHECK_EQUAL(actualComponent, expected[0]);
568
569 Name actualName;
570 test.append(actualName, test.value);
571 BOOST_CHECK_EQUAL(actualName, expected);
572
573 BOOST_CHECK_EQUAL(test.isComponent(expected[0]), true);
574 BOOST_CHECK_EQUAL(test.getValue(expected[0]), test.value);
575
576 BOOST_CHECK_EQUAL(test.isComponent(invalidComponent1), false);
577 BOOST_CHECK_EQUAL(test.isComponent(invalidComponent2), false);
578
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000579 BOOST_CHECK_THROW(test.getValue(invalidComponent1), Component::Error);
580 BOOST_CHECK_THROW(test.getValue(invalidComponent2), Component::Error);
Junxiao Shi71ff2312017-07-12 13:32:50 +0000581}
582
583BOOST_AUTO_TEST_SUITE_END() // NamingConvention
584
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400585BOOST_AUTO_TEST_CASE(Compare)
586{
587 const std::vector<Component> comps = {
588 Component("0120 0000000000000000000000000000000000000000000000000000000000000000"_block),
589 Component("0120 0000000000000000000000000000000000000000000000000000000000000001"_block),
590 Component("0120 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"_block),
591 Component("0220 0000000000000000000000000000000000000000000000000000000000000000"_block),
592 Component("0220 0000000000000000000000000000000000000000000000000000000000000001"_block),
593 Component("0220 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"_block),
594 Component(0x03),
595 Component("0301 44"_block),
596 Component("0301 46"_block),
597 Component("0302 4141"_block),
598 Component(),
599 Component("D"),
600 Component("F"),
601 Component("AA"),
602 Component(0x53B2),
603 Component("FD53B201 44"_block),
604 Component("FD53B201 46"_block),
605 Component("FD53B202 4141"_block),
606 };
607
608 for (size_t i = 0; i < comps.size(); ++i) {
609 for (size_t j = 0; j < comps.size(); ++j) {
610 const auto& lhs = comps[i];
611 const auto& rhs = comps[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 BOOST_CHECK_EQUAL(lhs > rhs, i > j);
617 BOOST_CHECK_EQUAL(lhs >= rhs, i >= j);
618 }
619 }
620}
621
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000622BOOST_AUTO_TEST_SUITE_END() // TestNameComponent
623
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400624} // namespace ndn::tests