blob: a35f9804ddc5fbb275f6174bf6c50900c6c78e85 [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
33namespace ndn {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000034namespace name {
Junxiao Shidf4b24e2016-07-14 21:41:43 +000035namespace tests {
36
Davide Pesavento152ef442023-04-22 02:02:29 -040037BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Component>));
38BOOST_CONCEPT_ASSERT((WireEncodable<Component>));
39BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<Component>));
40BOOST_CONCEPT_ASSERT((WireDecodable<Component>));
41static_assert(std::is_convertible_v<Component::Error*, tlv::Error*>,
42 "name::Component::Error must inherit from tlv::Error");
43
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000044BOOST_AUTO_TEST_SUITE(TestNameComponent)
Junxiao Shidf4b24e2016-07-14 21:41:43 +000045
46BOOST_AUTO_TEST_SUITE(Decode)
47
Davide Pesavento009062d2019-11-20 00:16:33 -050048#define CHECK_COMP_ERR(expr, whatstring) \
49 BOOST_CHECK_EXCEPTION(expr, Component::Error, \
50 [] (const auto& e) { return boost::contains(e.what(), whatstring); })
51
Junxiao Shidf4b24e2016-07-14 21:41:43 +000052BOOST_AUTO_TEST_CASE(Generic)
53{
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000054 Component comp("0807 6E646E2D637878"_block);
55 BOOST_CHECK_EQUAL(comp.type(), tlv::GenericNameComponent);
Davide Pesavento009062d2019-11-20 00:16:33 -050056 BOOST_CHECK_EQUAL(comp.isGeneric(), true);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000057 BOOST_CHECK_EQUAL(comp.toUri(), "ndn-cxx");
Junxiao Shia39c0b52019-12-31 15:13:15 -070058 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::CANONICAL), "8=ndn-cxx");
59 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ALTERNATE), "ndn-cxx");
Davide Pesaventobc17d952020-02-15 20:10:52 -050060 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_CANONICAL), "8=ndn-cxx");
61 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_ALTERNATE), "ndn-cxx");
Davide Pesavento009062d2019-11-20 00:16:33 -050062 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(comp), "ndn-cxx");
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000063 BOOST_CHECK_EQUAL(Component::fromEscapedString("ndn-cxx"), comp);
Junxiao Shid2e60632018-08-10 10:48:44 -060064 BOOST_CHECK_EQUAL(Component::fromEscapedString("8=ndn-cxx"), comp);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000065
66 comp.wireDecode("0800"_block);
67 BOOST_CHECK_EQUAL(comp.toUri(), "...");
68 BOOST_CHECK_EQUAL(Component::fromEscapedString("..."), comp);
Junxiao Shid2e60632018-08-10 10:48:44 -060069 BOOST_CHECK_EQUAL(Component::fromEscapedString("8=..."), comp);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000070 BOOST_CHECK_EQUAL(Component::fromEscapedString(".%2E."), comp);
71
72 comp.wireDecode("0801 2E"_block);
73 BOOST_CHECK_EQUAL(comp.toUri(), "....");
74 BOOST_CHECK_EQUAL(Component::fromEscapedString("...."), comp);
75 BOOST_CHECK_EQUAL(Component::fromEscapedString("%2E..%2E"), comp);
76
77 comp.wireDecode("0803 2E412E"_block);
78 BOOST_CHECK_EQUAL(comp.toUri(), ".A.");
79 BOOST_CHECK_EQUAL(Component::fromEscapedString(".A."), comp);
80
81 comp.wireDecode("0807 666F6F25626172"_block);
82 BOOST_CHECK_EQUAL(comp.toUri(), "foo%25bar");
83 BOOST_CHECK_EQUAL(Component::fromEscapedString("foo%25bar"), comp);
Junxiao Shid2e60632018-08-10 10:48:44 -060084 BOOST_CHECK_EQUAL(Component::fromEscapedString("8=foo%25bar"), comp);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000085
86 comp.wireDecode("0804 2D2E5F7E"_block);
87 BOOST_CHECK_EQUAL(comp.toUri(), "-._~");
88 BOOST_CHECK_EQUAL(Component::fromEscapedString("-._~"), comp);
89
Junxiao Shid2e60632018-08-10 10:48:44 -060090 comp.wireDecode("0803 393D41"_block);
91 BOOST_CHECK_EQUAL(comp.toUri(), "9%3DA");
92 BOOST_CHECK_EQUAL(Component::fromEscapedString("9%3DA"), comp);
93
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000094 comp = Component(":/?#[]@");
95 BOOST_CHECK_EQUAL(comp.toUri(), "%3A%2F%3F%23%5B%5D%40");
96 BOOST_CHECK_EQUAL(Component::fromEscapedString("%3A%2F%3F%23%5B%5D%40"), comp);
97
98 BOOST_CHECK_THROW(Component::fromEscapedString(""), Component::Error);
99 BOOST_CHECK_THROW(Component::fromEscapedString("."), Component::Error);
100 BOOST_CHECK_THROW(Component::fromEscapedString(".."), Component::Error);
Davide Pesavento21ee8992022-02-25 20:46:04 -0500101 BOOST_CHECK_THROW(Component::fromEscapedString("8="), Component::Error);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000102}
103
Junxiao Shi4053bd52018-08-16 13:39:25 -0600104static void
Davide Pesavento009062d2019-11-20 00:16:33 -0500105testSha256Component(uint32_t type, const std::string& uriPrefix)
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000106{
Davide Pesavento009062d2019-11-20 00:16:33 -0500107 const std::string hexLower = "28bad4b5275bd392dbb670c75cf0b66f13f7942b21e80f55c0e86b374753a548";
108 const std::string hexUpper = boost::to_upper_copy(hexLower);
Junxiao Shi4053bd52018-08-16 13:39:25 -0600109 std::string hexPct;
110 for (size_t i = 0; i < hexUpper.size(); i += 2) {
111 hexPct += "%" + hexUpper.substr(i, 2);
112 }
Junxiao Shia39c0b52019-12-31 15:13:15 -0700113 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 +0000114
Davide Pesavento21ee8992022-02-25 20:46:04 -0500115 Component comp(type, fromHex(hexLower));
Davide Pesavento009062d2019-11-20 00:16:33 -0500116
Junxiao Shi4053bd52018-08-16 13:39:25 -0600117 BOOST_CHECK_EQUAL(comp.type(), type);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000118 BOOST_CHECK_EQUAL(comp.toUri(), uriPrefix + hexLower);
Junxiao Shia39c0b52019-12-31 15:13:15 -0700119 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::CANONICAL), to_string(type) + "=" + hexPctCanonical);
120 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ALTERNATE), uriPrefix + hexLower);
Davide Pesaventobc17d952020-02-15 20:10:52 -0500121 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_CANONICAL), to_string(type) + "=" + hexPctCanonical);
122 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_ALTERNATE), uriPrefix + hexLower);
Davide Pesavento009062d2019-11-20 00:16:33 -0500123 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(comp), uriPrefix + hexLower);
124 BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(uriPrefix + hexLower));
125 BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(uriPrefix + hexUpper));
126 BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(to_string(type) + "=" + hexPct));
Junxiao Shia39c0b52019-12-31 15:13:15 -0700127 BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(to_string(type) + "=" + hexPctCanonical));
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000128
Davide Pesavento009062d2019-11-20 00:16:33 -0500129 CHECK_COMP_ERR(comp.wireDecode(Block(type, fromHex("A791806951F25C4D"))), "TLV-LENGTH must be 32");
130 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix), "TLV-LENGTH must be 32");
131 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "a791806951f25c4d"), "TLV-LENGTH must be 32");
132 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "foo"), "invalid hex encoding");
133 CHECK_COMP_ERR(Component::fromEscapedString(boost::to_upper_copy(uriPrefix) + hexLower), "Unknown TLV-TYPE");
Junxiao Shi4053bd52018-08-16 13:39:25 -0600134}
135
Davide Pesavento009062d2019-11-20 00:16:33 -0500136BOOST_AUTO_TEST_CASE(ImplicitDigest)
Junxiao Shi4053bd52018-08-16 13:39:25 -0600137{
Davide Pesavento009062d2019-11-20 00:16:33 -0500138 testSha256Component(tlv::ImplicitSha256DigestComponent, "sha256digest=");
Junxiao Shi4053bd52018-08-16 13:39:25 -0600139}
140
Davide Pesavento009062d2019-11-20 00:16:33 -0500141BOOST_AUTO_TEST_CASE(ParametersDigest)
Junxiao Shi4053bd52018-08-16 13:39:25 -0600142{
Davide Pesavento009062d2019-11-20 00:16:33 -0500143 testSha256Component(tlv::ParametersSha256DigestComponent, "params-sha256=");
144}
145
146static void
147testDecimalComponent(uint32_t type, const std::string& uriPrefix)
148{
Davide Pesavento47a94d12021-10-31 16:41:05 -0400149 const Component comp(makeNonNegativeIntegerBlock(type, 42)); // TLV-VALUE is a NonNegativeInteger
Davide Pesavento009062d2019-11-20 00:16:33 -0500150 BOOST_CHECK_EQUAL(comp.type(), type);
151 BOOST_CHECK_EQUAL(comp.isNumber(), true);
152 const auto compUri = uriPrefix + "42";
153 BOOST_CHECK_EQUAL(comp.toUri(), compUri);
Junxiao Shia39c0b52019-12-31 15:13:15 -0700154 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::CANONICAL), to_string(type) + "=%2A");
155 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ALTERNATE), compUri);
Davide Pesaventobc17d952020-02-15 20:10:52 -0500156 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_CANONICAL), to_string(type) + "=%2A");
157 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_ALTERNATE), compUri);
Davide Pesavento009062d2019-11-20 00:16:33 -0500158 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(comp), compUri);
159 BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(compUri));
160 BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(to_string(type) + "=%2A"));
161 BOOST_CHECK_EQUAL(comp, Component::fromNumber(42, type));
162
Davide Pesavento21ee8992022-02-25 20:46:04 -0500163 const Component comp2(type, fromHex("010203")); // TLV-VALUE is *not* a NonNegativeInteger
Davide Pesavento009062d2019-11-20 00:16:33 -0500164 BOOST_CHECK_EQUAL(comp2.type(), type);
165 BOOST_CHECK_EQUAL(comp2.isNumber(), false);
166 const auto comp2Uri = to_string(type) + "=%01%02%03";
167 BOOST_CHECK_EQUAL(comp2.toUri(), comp2Uri);
168 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(comp2), comp2Uri);
169 BOOST_CHECK_EQUAL(comp2, Component::fromEscapedString(comp2Uri));
170
171 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix), "invalid format");
172 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "foo"), "invalid format");
173 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "00"), "invalid format");
174 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "-1"), "invalid format");
175 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "9.3"), "invalid format");
176 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + " 84"), "invalid format");
177 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "0xAF"), "invalid format");
178 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "18446744073709551616"), "out of range");
179 CHECK_COMP_ERR(Component::fromEscapedString(boost::to_upper_copy(uriPrefix) + "42"), "Unknown TLV-TYPE");
180}
181
182BOOST_AUTO_TEST_CASE(Segment)
183{
184 testDecimalComponent(tlv::SegmentNameComponent, "seg=");
185}
186
187BOOST_AUTO_TEST_CASE(ByteOffset)
188{
189 testDecimalComponent(tlv::ByteOffsetNameComponent, "off=");
190}
191
192BOOST_AUTO_TEST_CASE(Version)
193{
194 testDecimalComponent(tlv::VersionNameComponent, "v=");
195}
196
197BOOST_AUTO_TEST_CASE(Timestamp)
198{
199 testDecimalComponent(tlv::TimestampNameComponent, "t=");
200}
201
202BOOST_AUTO_TEST_CASE(SequenceNum)
203{
204 testDecimalComponent(tlv::SequenceNumNameComponent, "seq=");
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000205}
206
Davide Pesavento21ee8992022-02-25 20:46:04 -0500207BOOST_AUTO_TEST_CASE(Keyword)
208{
209 Component comp("2007 6E646E2D637878"_block);
210 BOOST_CHECK_EQUAL(comp.type(), tlv::KeywordNameComponent);
211 BOOST_CHECK_EQUAL(comp.isKeyword(), true);
212 BOOST_CHECK_EQUAL(comp.toUri(), "32=ndn-cxx");
213 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::CANONICAL), "32=ndn-cxx");
214 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ALTERNATE), "32=ndn-cxx");
215 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_CANONICAL), "32=ndn-cxx");
216 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_ALTERNATE), "32=ndn-cxx");
217 BOOST_CHECK_EQUAL(Component::fromEscapedString("32=ndn-cxx"), comp);
218
219 comp.wireDecode("2000"_block);
220 BOOST_CHECK_EQUAL(comp.type(), tlv::KeywordNameComponent);
221 BOOST_CHECK_EQUAL(comp.isKeyword(), true);
222 BOOST_CHECK_EQUAL(comp.toUri(), "32=...");
223 BOOST_CHECK_EQUAL(Component::fromEscapedString("32=..."), comp);
224
225 BOOST_CHECK_THROW(Component::fromEscapedString("32="), Component::Error);
226 BOOST_CHECK_THROW(Component::fromEscapedString("32=."), Component::Error);
227 BOOST_CHECK_THROW(Component::fromEscapedString("32=.."), Component::Error);
228}
229
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000230BOOST_AUTO_TEST_CASE(OtherType)
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000231{
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000232 Component comp("0907 6E646E2D637878"_block);
233 BOOST_CHECK_EQUAL(comp.type(), 0x09);
234 BOOST_CHECK_EQUAL(comp.toUri(), "9=ndn-cxx");
Davide Pesaventobc17d952020-02-15 20:10:52 -0500235 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::CANONICAL), "9=ndn-cxx");
236 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ALTERNATE), "9=ndn-cxx");
237 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_CANONICAL), "9=ndn-cxx");
238 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_ALTERNATE), "9=ndn-cxx");
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000239 BOOST_CHECK_EQUAL(Component::fromEscapedString("9=ndn-cxx"), comp);
240
241 comp.wireDecode("FDFFFF00"_block);
242 BOOST_CHECK_EQUAL(comp.type(), 0xFFFF);
243 BOOST_CHECK_EQUAL(comp.toUri(), "65535=...");
244 BOOST_CHECK_EQUAL(Component::fromEscapedString("65535=..."), comp);
245
246 comp.wireDecode("FD576501 2E"_block);
247 BOOST_CHECK_EQUAL(comp.type(), 0x5765);
248 BOOST_CHECK_EQUAL(comp.toUri(), "22373=....");
249 BOOST_CHECK_EQUAL(Component::fromEscapedString("22373=...."), comp);
250
251 BOOST_CHECK_THROW(Component::fromEscapedString("3="), Component::Error);
252 BOOST_CHECK_THROW(Component::fromEscapedString("3=."), Component::Error);
253 BOOST_CHECK_THROW(Component::fromEscapedString("3=.."), Component::Error);
254}
255
256BOOST_AUTO_TEST_CASE(InvalidType)
257{
258 Component comp;
Davide Pesavento6b330402019-04-24 00:14:01 -0400259 BOOST_CHECK_THROW(comp.wireDecode(Block{}), Component::Error);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000260 BOOST_CHECK_THROW(comp.wireDecode("FE0001000001 80"_block), Component::Error);
261
262 BOOST_CHECK_THROW(Component::fromEscapedString("0=A"), Component::Error);
263 BOOST_CHECK_THROW(Component::fromEscapedString("65536=A"), Component::Error);
Junxiao Shid2e60632018-08-10 10:48:44 -0600264 BOOST_CHECK_THROW(Component::fromEscapedString("4294967296=A"), Component::Error);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000265 BOOST_CHECK_THROW(Component::fromEscapedString("-1=A"), Component::Error);
266 BOOST_CHECK_THROW(Component::fromEscapedString("+=A"), Component::Error);
267 BOOST_CHECK_THROW(Component::fromEscapedString("=A"), Component::Error);
268 BOOST_CHECK_THROW(Component::fromEscapedString("0x1=A"), Component::Error);
269 BOOST_CHECK_THROW(Component::fromEscapedString("Z=A"), Component::Error);
270 BOOST_CHECK_THROW(Component::fromEscapedString("09=A"), Component::Error);
Junxiao Shi4053bd52018-08-16 13:39:25 -0600271 BOOST_CHECK_THROW(Component::fromEscapedString("0x3=A"), Component::Error);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000272 BOOST_CHECK_THROW(Component::fromEscapedString("+9=A"), Component::Error);
Junxiao Shid2e60632018-08-10 10:48:44 -0600273 BOOST_CHECK_THROW(Component::fromEscapedString(" 9=A"), Component::Error);
274 BOOST_CHECK_THROW(Component::fromEscapedString("9 =A"), Component::Error);
275 BOOST_CHECK_THROW(Component::fromEscapedString("9.0=A"), Component::Error);
276 BOOST_CHECK_THROW(Component::fromEscapedString("9E0=A"), Component::Error);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000277}
278
279BOOST_AUTO_TEST_SUITE_END() // Decode
280
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400281BOOST_AUTO_TEST_CASE(ConstructFromSpan)
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000282{
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400283 const uint8_t arr[] = {1, 2, 3};
284 Component c1(arr);
285 BOOST_TEST(c1.wireEncode() == "0803010203"_block);
286 Component c2(128, arr);
287 BOOST_TEST(c2.wireEncode() == "8003010203"_block);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000288
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400289 const std::vector<uint8_t> vec = {4, 5, 6};
290 Component c3(vec);
291 BOOST_TEST(c3.wireEncode() == "0803040506"_block);
292 Component c4(128, vec);
293 BOOST_TEST(c4.wireEncode() == "8003040506"_block);
294
295 Component c5(128, {7, 8});
296 BOOST_TEST(c5.wireEncode() == "80020708"_block);
297
298 const Block b("090109"_block);
299 Component c6(128, b);
300 BOOST_TEST(c6.wireEncode() == "8003090109"_block);
Davide Pesavento08378cb2018-02-01 16:10:54 -0500301}
302
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400303BOOST_AUTO_TEST_SUITE(ConstructFromIterators) // Bug 2490
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000304
Davide Pesavento009062d2019-11-20 00:16:33 -0500305using ContainerTypes = boost::mpl::vector<std::vector<uint8_t>,
306 std::list<uint8_t>,
307 std::vector<int8_t>,
308 std::list<int8_t>>;
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000309
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400310BOOST_AUTO_TEST_CASE_TEMPLATE(ZeroOctets, T, ContainerTypes)
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000311{
312 T bytes;
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000313 Component c(bytes.begin(), bytes.end());
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400314 BOOST_TEST(c.type() == tlv::GenericNameComponent);
315 BOOST_TEST(c.value_size() == 0);
316 BOOST_TEST(c.size() == 2);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000317}
318
319BOOST_AUTO_TEST_CASE_TEMPLATE(OneOctet, T, ContainerTypes)
320{
321 T bytes{1};
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400322 Component c(9, bytes.begin(), bytes.end());
323 BOOST_TEST(c.type() == 0x09);
324 BOOST_TEST(c.value_size() == 1);
325 BOOST_TEST(c.size() == 3);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000326}
327
328BOOST_AUTO_TEST_CASE_TEMPLATE(FourOctets, T, ContainerTypes)
329{
330 T bytes{1, 2, 3, 4};
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000331 Component c(0xFCEC, bytes.begin(), bytes.end());
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400332 BOOST_TEST(c.type() == 0xFCEC);
333 BOOST_TEST(c.value_size() == 4);
334 BOOST_TEST(c.size() == 8);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000335}
336
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400337BOOST_AUTO_TEST_SUITE_END() // ConstructFromIterators
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000338
Junxiao Shi71ff2312017-07-12 13:32:50 +0000339BOOST_AUTO_TEST_SUITE(NamingConvention)
340
341template<typename ArgType>
342struct ConventionTest
343{
Davide Pesavento009062d2019-11-20 00:16:33 -0500344 std::function<Component(ArgType)> makeComponent;
345 std::function<ArgType(const Component&)> getValue;
346 std::function<Name&(Name&, ArgType)> append;
Junxiao Shi71ff2312017-07-12 13:32:50 +0000347 Name expected;
348 ArgType value;
Davide Pesavento009062d2019-11-20 00:16:33 -0500349 std::function<bool(const Component&)> isComponent;
Junxiao Shi71ff2312017-07-12 13:32:50 +0000350};
351
Junxiao Shie2099612019-02-15 14:46:27 +0000352class ConventionMarker
353{
Eric Newberryc25e4632021-02-11 10:48:11 -0800354public:
355 ConventionMarker()
356 {
357 name::setConventionEncoding(name::Convention::MARKER);
358 }
359
360 ~ConventionMarker()
361 {
362 name::setConventionEncoding(name::Convention::TYPED);
363 }
Junxiao Shie2099612019-02-15 14:46:27 +0000364};
365
366class ConventionTyped
367{
Junxiao Shie2099612019-02-15 14:46:27 +0000368};
369
Junxiao Shi71ff2312017-07-12 13:32:50 +0000370class NumberWithMarker
371{
372public:
Junxiao Shie2099612019-02-15 14:46:27 +0000373 using ConventionRev = ConventionMarker;
374
Junxiao Shi71ff2312017-07-12 13:32:50 +0000375 ConventionTest<uint64_t>
376 operator()() const
377 {
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500378 return {[] (auto num) { return Component::fromNumberWithMarker(0xAA, num); },
379 [] (const Component& c) { return c.toNumberWithMarker(0xAA); },
380 [] (Name& name, auto num) -> Name& { return name.appendNumberWithMarker(0xAA, num); },
Junxiao Shi71ff2312017-07-12 13:32:50 +0000381 Name("/%AA%03%E8"),
382 1000,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500383 [] (const Component& c) { return c.isNumberWithMarker(0xAA); }};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000384 }
385};
386
Junxiao Shie2099612019-02-15 14:46:27 +0000387class SegmentMarker
Junxiao Shi71ff2312017-07-12 13:32:50 +0000388{
389public:
Junxiao Shie2099612019-02-15 14:46:27 +0000390 using ConventionRev = ConventionMarker;
391
Junxiao Shi71ff2312017-07-12 13:32:50 +0000392 ConventionTest<uint64_t>
393 operator()() const
394 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000395 return {&Component::fromSegment,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500396 &Component::toSegment,
397 &Name::appendSegment,
Junxiao Shi71ff2312017-07-12 13:32:50 +0000398 Name("/%00%27%10"),
399 10000,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500400 &Component::isSegment};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000401 }
402};
403
Junxiao Shie2099612019-02-15 14:46:27 +0000404class SegmentTyped
Junxiao Shi71ff2312017-07-12 13:32:50 +0000405{
406public:
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
Junxiao Shie2099612019-02-15 14:46:27 +0000421class ByteOffsetTyped
Junxiao Shi71ff2312017-07-12 13:32:50 +0000422{
423public:
Junxiao Shie2099612019-02-15 14:46:27 +0000424 using ConventionRev = ConventionTyped;
425
426 ConventionTest<uint64_t>
427 operator()() const
428 {
429 return {&Component::fromByteOffset,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500430 &Component::toByteOffset,
431 &Name::appendByteOffset,
Davide Pesavento47a94d12021-10-31 16:41:05 -0400432 Name("/52=%00%01%86%A0"),
Junxiao Shie2099612019-02-15 14:46:27 +0000433 100000,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500434 &Component::isByteOffset};
Junxiao Shie2099612019-02-15 14:46:27 +0000435 }
436};
437
438class VersionMarker
439{
440public:
441 using ConventionRev = ConventionMarker;
442
Junxiao Shi71ff2312017-07-12 13:32:50 +0000443 ConventionTest<uint64_t>
444 operator()() const
445 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000446 return {&Component::fromVersion,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500447 &Component::toVersion,
448 [] (Name& name, auto version) -> Name& { return name.appendVersion(version); },
Junxiao Shi71ff2312017-07-12 13:32:50 +0000449 Name("/%FD%00%0FB%40"),
450 1000000,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500451 &Component::isVersion};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000452 }
453};
454
Junxiao Shie2099612019-02-15 14:46:27 +0000455class VersionTyped
Junxiao Shi71ff2312017-07-12 13:32:50 +0000456{
457public:
Junxiao Shie2099612019-02-15 14:46:27 +0000458 using ConventionRev = ConventionTyped;
459
460 ConventionTest<uint64_t>
461 operator()() const
462 {
463 return {&Component::fromVersion,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500464 &Component::toVersion,
465 [] (Name& name, auto version) -> Name& { return name.appendVersion(version); },
Davide Pesavento47a94d12021-10-31 16:41:05 -0400466 Name("/54=%00%0FB%40"),
Junxiao Shie2099612019-02-15 14:46:27 +0000467 1000000,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500468 &Component::isVersion};
Junxiao Shie2099612019-02-15 14:46:27 +0000469 }
470};
471
472class TimestampMarker
473{
474public:
475 using ConventionRev = ConventionMarker;
476
Davide Pesavento47a94d12021-10-31 16:41:05 -0400477 ConventionTest<time::system_clock::time_point>
Junxiao Shi71ff2312017-07-12 13:32:50 +0000478 operator()() const
479 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000480 return {&Component::fromTimestamp,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500481 &Component::toTimestamp,
482 [] (Name& name, auto tp) -> Name& { return name.appendTimestamp(tp); },
Junxiao Shi71ff2312017-07-12 13:32:50 +0000483 Name("/%FC%00%04%7BE%E3%1B%00%00"),
Davide Pesavento0f830802018-01-16 23:58:58 -0500484 time::getUnixEpoch() + 14600_days, // 40 years
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500485 &Component::isTimestamp};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000486 }
487};
488
Junxiao Shie2099612019-02-15 14:46:27 +0000489class TimestampTyped
Junxiao Shi71ff2312017-07-12 13:32:50 +0000490{
491public:
Junxiao Shie2099612019-02-15 14:46:27 +0000492 using ConventionRev = ConventionTyped;
493
Davide Pesavento47a94d12021-10-31 16:41:05 -0400494 ConventionTest<time::system_clock::time_point>
Junxiao Shie2099612019-02-15 14:46:27 +0000495 operator()() const
496 {
497 return {&Component::fromTimestamp,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500498 &Component::toTimestamp,
499 [] (Name& name, auto tp) -> Name& { return name.appendTimestamp(tp); },
Davide Pesavento47a94d12021-10-31 16:41:05 -0400500 Name("/56=%00%04%7BE%E3%1B%00%00"),
Junxiao Shie2099612019-02-15 14:46:27 +0000501 time::getUnixEpoch() + 14600_days, // 40 years
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500502 &Component::isTimestamp};
Junxiao Shie2099612019-02-15 14:46:27 +0000503 }
504};
505
506class SequenceNumberMarker
507{
508public:
509 using ConventionRev = ConventionMarker;
510
Junxiao Shi71ff2312017-07-12 13:32:50 +0000511 ConventionTest<uint64_t>
512 operator()() const
513 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000514 return {&Component::fromSequenceNumber,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500515 &Component::toSequenceNumber,
516 &Name::appendSequenceNumber,
Junxiao Shi71ff2312017-07-12 13:32:50 +0000517 Name("/%FE%00%98%96%80"),
518 10000000,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500519 &Component::isSequenceNumber};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000520 }
521};
522
Junxiao Shie2099612019-02-15 14:46:27 +0000523class SequenceNumberTyped
524{
525public:
526 using ConventionRev = ConventionTyped;
527
528 ConventionTest<uint64_t>
529 operator()() const
530 {
531 return {&Component::fromSequenceNumber,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500532 &Component::toSequenceNumber,
533 &Name::appendSequenceNumber,
Davide Pesavento47a94d12021-10-31 16:41:05 -0400534 Name("/58=%00%98%96%80"),
Junxiao Shie2099612019-02-15 14:46:27 +0000535 10000000,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500536 &Component::isSequenceNumber};
Junxiao Shie2099612019-02-15 14:46:27 +0000537 }
538};
539
Junxiao Shi71ff2312017-07-12 13:32:50 +0000540using ConventionTests = boost::mpl::vector<
541 NumberWithMarker,
Junxiao Shie2099612019-02-15 14:46:27 +0000542 SegmentMarker,
543 SegmentTyped,
Junxiao Shie2099612019-02-15 14:46:27 +0000544 ByteOffsetTyped,
545 VersionMarker,
546 VersionTyped,
547 TimestampMarker,
548 TimestampTyped,
549 SequenceNumberMarker,
550 SequenceNumberTyped
Junxiao Shi71ff2312017-07-12 13:32:50 +0000551>;
552
Junxiao Shie2099612019-02-15 14:46:27 +0000553BOOST_FIXTURE_TEST_CASE_TEMPLATE(Convention, T, ConventionTests, T::ConventionRev)
Junxiao Shi71ff2312017-07-12 13:32:50 +0000554{
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000555 Component invalidComponent1;
556 Component invalidComponent2("1234567890");
Junxiao Shi71ff2312017-07-12 13:32:50 +0000557
558 auto test = T()();
Junxiao Shi71ff2312017-07-12 13:32:50 +0000559 const Name& expected = test.expected;
Junxiao Shi71ff2312017-07-12 13:32:50 +0000560
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000561 Component actualComponent = test.makeComponent(test.value);
Junxiao Shi71ff2312017-07-12 13:32:50 +0000562 BOOST_CHECK_EQUAL(actualComponent, expected[0]);
563
564 Name actualName;
565 test.append(actualName, test.value);
566 BOOST_CHECK_EQUAL(actualName, expected);
567
568 BOOST_CHECK_EQUAL(test.isComponent(expected[0]), true);
569 BOOST_CHECK_EQUAL(test.getValue(expected[0]), test.value);
570
571 BOOST_CHECK_EQUAL(test.isComponent(invalidComponent1), false);
572 BOOST_CHECK_EQUAL(test.isComponent(invalidComponent2), false);
573
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000574 BOOST_CHECK_THROW(test.getValue(invalidComponent1), Component::Error);
575 BOOST_CHECK_THROW(test.getValue(invalidComponent2), Component::Error);
Junxiao Shi71ff2312017-07-12 13:32:50 +0000576}
577
578BOOST_AUTO_TEST_SUITE_END() // NamingConvention
579
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400580BOOST_AUTO_TEST_CASE(Compare)
581{
582 const std::vector<Component> comps = {
583 Component("0120 0000000000000000000000000000000000000000000000000000000000000000"_block),
584 Component("0120 0000000000000000000000000000000000000000000000000000000000000001"_block),
585 Component("0120 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"_block),
586 Component("0220 0000000000000000000000000000000000000000000000000000000000000000"_block),
587 Component("0220 0000000000000000000000000000000000000000000000000000000000000001"_block),
588 Component("0220 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"_block),
589 Component(0x03),
590 Component("0301 44"_block),
591 Component("0301 46"_block),
592 Component("0302 4141"_block),
593 Component(),
594 Component("D"),
595 Component("F"),
596 Component("AA"),
597 Component(0x53B2),
598 Component("FD53B201 44"_block),
599 Component("FD53B201 46"_block),
600 Component("FD53B202 4141"_block),
601 };
602
603 for (size_t i = 0; i < comps.size(); ++i) {
604 for (size_t j = 0; j < comps.size(); ++j) {
605 const auto& lhs = comps[i];
606 const auto& rhs = comps[j];
607 BOOST_CHECK_EQUAL(lhs == rhs, i == j);
608 BOOST_CHECK_EQUAL(lhs != rhs, i != j);
609 BOOST_CHECK_EQUAL(lhs < rhs, i < 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 }
614 }
615}
616
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000617BOOST_AUTO_TEST_SUITE_END() // TestNameComponent
618
619} // namespace tests
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000620} // namespace name
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000621} // namespace ndn