blob: d72de0f58428ecfebf0db4338079f1010daab0b6 [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 Pesavento152ef442023-04-22 02:02:29 -040038BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Component>));
39BOOST_CONCEPT_ASSERT((WireEncodable<Component>));
40BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<Component>));
41BOOST_CONCEPT_ASSERT((WireDecodable<Component>));
42static_assert(std::is_convertible_v<Component::Error*, tlv::Error*>,
43 "name::Component::Error must inherit from tlv::Error");
44
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000045BOOST_AUTO_TEST_SUITE(TestNameComponent)
Junxiao Shidf4b24e2016-07-14 21:41:43 +000046
47BOOST_AUTO_TEST_SUITE(Decode)
48
Davide Pesavento009062d2019-11-20 00:16:33 -050049#define CHECK_COMP_ERR(expr, whatstring) \
50 BOOST_CHECK_EXCEPTION(expr, Component::Error, \
51 [] (const auto& e) { return boost::contains(e.what(), whatstring); })
52
Junxiao Shidf4b24e2016-07-14 21:41:43 +000053BOOST_AUTO_TEST_CASE(Generic)
54{
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000055 Component comp("0807 6E646E2D637878"_block);
56 BOOST_CHECK_EQUAL(comp.type(), tlv::GenericNameComponent);
Davide Pesavento009062d2019-11-20 00:16:33 -050057 BOOST_CHECK_EQUAL(comp.isGeneric(), true);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000058 BOOST_CHECK_EQUAL(comp.toUri(), "ndn-cxx");
Junxiao Shia39c0b52019-12-31 15:13:15 -070059 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::CANONICAL), "8=ndn-cxx");
60 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ALTERNATE), "ndn-cxx");
Davide Pesaventobc17d952020-02-15 20:10:52 -050061 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_CANONICAL), "8=ndn-cxx");
62 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_ALTERNATE), "ndn-cxx");
Davide Pesavento009062d2019-11-20 00:16:33 -050063 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(comp), "ndn-cxx");
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000064 BOOST_CHECK_EQUAL(Component::fromEscapedString("ndn-cxx"), comp);
Junxiao Shid2e60632018-08-10 10:48:44 -060065 BOOST_CHECK_EQUAL(Component::fromEscapedString("8=ndn-cxx"), comp);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000066
67 comp.wireDecode("0800"_block);
68 BOOST_CHECK_EQUAL(comp.toUri(), "...");
69 BOOST_CHECK_EQUAL(Component::fromEscapedString("..."), comp);
Junxiao Shid2e60632018-08-10 10:48:44 -060070 BOOST_CHECK_EQUAL(Component::fromEscapedString("8=..."), comp);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000071 BOOST_CHECK_EQUAL(Component::fromEscapedString(".%2E."), comp);
72
73 comp.wireDecode("0801 2E"_block);
74 BOOST_CHECK_EQUAL(comp.toUri(), "....");
75 BOOST_CHECK_EQUAL(Component::fromEscapedString("...."), comp);
76 BOOST_CHECK_EQUAL(Component::fromEscapedString("%2E..%2E"), comp);
77
78 comp.wireDecode("0803 2E412E"_block);
79 BOOST_CHECK_EQUAL(comp.toUri(), ".A.");
80 BOOST_CHECK_EQUAL(Component::fromEscapedString(".A."), comp);
81
82 comp.wireDecode("0807 666F6F25626172"_block);
83 BOOST_CHECK_EQUAL(comp.toUri(), "foo%25bar");
84 BOOST_CHECK_EQUAL(Component::fromEscapedString("foo%25bar"), comp);
Junxiao Shid2e60632018-08-10 10:48:44 -060085 BOOST_CHECK_EQUAL(Component::fromEscapedString("8=foo%25bar"), comp);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000086
87 comp.wireDecode("0804 2D2E5F7E"_block);
88 BOOST_CHECK_EQUAL(comp.toUri(), "-._~");
89 BOOST_CHECK_EQUAL(Component::fromEscapedString("-._~"), comp);
90
Junxiao Shid2e60632018-08-10 10:48:44 -060091 comp.wireDecode("0803 393D41"_block);
92 BOOST_CHECK_EQUAL(comp.toUri(), "9%3DA");
93 BOOST_CHECK_EQUAL(Component::fromEscapedString("9%3DA"), comp);
94
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000095 comp = Component(":/?#[]@");
96 BOOST_CHECK_EQUAL(comp.toUri(), "%3A%2F%3F%23%5B%5D%40");
97 BOOST_CHECK_EQUAL(Component::fromEscapedString("%3A%2F%3F%23%5B%5D%40"), comp);
98
99 BOOST_CHECK_THROW(Component::fromEscapedString(""), Component::Error);
100 BOOST_CHECK_THROW(Component::fromEscapedString("."), Component::Error);
101 BOOST_CHECK_THROW(Component::fromEscapedString(".."), Component::Error);
Davide Pesavento21ee8992022-02-25 20:46:04 -0500102 BOOST_CHECK_THROW(Component::fromEscapedString("8="), Component::Error);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000103}
104
Junxiao Shi4053bd52018-08-16 13:39:25 -0600105static void
Davide Pesavento009062d2019-11-20 00:16:33 -0500106testSha256Component(uint32_t type, const std::string& uriPrefix)
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000107{
Davide Pesavento009062d2019-11-20 00:16:33 -0500108 const std::string hexLower = "28bad4b5275bd392dbb670c75cf0b66f13f7942b21e80f55c0e86b374753a548";
109 const std::string hexUpper = boost::to_upper_copy(hexLower);
Junxiao Shi4053bd52018-08-16 13:39:25 -0600110 std::string hexPct;
111 for (size_t i = 0; i < hexUpper.size(); i += 2) {
112 hexPct += "%" + hexUpper.substr(i, 2);
113 }
Junxiao Shia39c0b52019-12-31 15:13:15 -0700114 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 +0000115
Davide Pesavento21ee8992022-02-25 20:46:04 -0500116 Component comp(type, fromHex(hexLower));
Davide Pesavento009062d2019-11-20 00:16:33 -0500117
Junxiao Shi4053bd52018-08-16 13:39:25 -0600118 BOOST_CHECK_EQUAL(comp.type(), type);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000119 BOOST_CHECK_EQUAL(comp.toUri(), uriPrefix + hexLower);
Junxiao Shia39c0b52019-12-31 15:13:15 -0700120 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::CANONICAL), to_string(type) + "=" + hexPctCanonical);
121 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ALTERNATE), uriPrefix + hexLower);
Davide Pesaventobc17d952020-02-15 20:10:52 -0500122 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_CANONICAL), to_string(type) + "=" + hexPctCanonical);
123 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_ALTERNATE), uriPrefix + hexLower);
Davide Pesavento009062d2019-11-20 00:16:33 -0500124 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(comp), uriPrefix + hexLower);
125 BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(uriPrefix + hexLower));
126 BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(uriPrefix + hexUpper));
127 BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(to_string(type) + "=" + hexPct));
Junxiao Shia39c0b52019-12-31 15:13:15 -0700128 BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(to_string(type) + "=" + hexPctCanonical));
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000129
Davide Pesavento009062d2019-11-20 00:16:33 -0500130 CHECK_COMP_ERR(comp.wireDecode(Block(type, fromHex("A791806951F25C4D"))), "TLV-LENGTH must be 32");
131 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix), "TLV-LENGTH must be 32");
132 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "a791806951f25c4d"), "TLV-LENGTH must be 32");
133 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "foo"), "invalid hex encoding");
134 CHECK_COMP_ERR(Component::fromEscapedString(boost::to_upper_copy(uriPrefix) + hexLower), "Unknown TLV-TYPE");
Junxiao Shi4053bd52018-08-16 13:39:25 -0600135}
136
Davide Pesavento009062d2019-11-20 00:16:33 -0500137BOOST_AUTO_TEST_CASE(ImplicitDigest)
Junxiao Shi4053bd52018-08-16 13:39:25 -0600138{
Davide Pesavento009062d2019-11-20 00:16:33 -0500139 testSha256Component(tlv::ImplicitSha256DigestComponent, "sha256digest=");
Junxiao Shi4053bd52018-08-16 13:39:25 -0600140}
141
Davide Pesavento009062d2019-11-20 00:16:33 -0500142BOOST_AUTO_TEST_CASE(ParametersDigest)
Junxiao Shi4053bd52018-08-16 13:39:25 -0600143{
Davide Pesavento009062d2019-11-20 00:16:33 -0500144 testSha256Component(tlv::ParametersSha256DigestComponent, "params-sha256=");
145}
146
147static void
148testDecimalComponent(uint32_t type, const std::string& uriPrefix)
149{
Davide Pesavento47a94d12021-10-31 16:41:05 -0400150 const Component comp(makeNonNegativeIntegerBlock(type, 42)); // TLV-VALUE is a NonNegativeInteger
Davide Pesavento009062d2019-11-20 00:16:33 -0500151 BOOST_CHECK_EQUAL(comp.type(), type);
152 BOOST_CHECK_EQUAL(comp.isNumber(), true);
153 const auto compUri = uriPrefix + "42";
154 BOOST_CHECK_EQUAL(comp.toUri(), compUri);
Junxiao Shia39c0b52019-12-31 15:13:15 -0700155 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::CANONICAL), to_string(type) + "=%2A");
156 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ALTERNATE), compUri);
Davide Pesaventobc17d952020-02-15 20:10:52 -0500157 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_CANONICAL), to_string(type) + "=%2A");
158 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_ALTERNATE), compUri);
Davide Pesavento009062d2019-11-20 00:16:33 -0500159 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(comp), compUri);
160 BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(compUri));
161 BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(to_string(type) + "=%2A"));
162 BOOST_CHECK_EQUAL(comp, Component::fromNumber(42, type));
163
Davide Pesavento21ee8992022-02-25 20:46:04 -0500164 const Component comp2(type, fromHex("010203")); // TLV-VALUE is *not* a NonNegativeInteger
Davide Pesavento009062d2019-11-20 00:16:33 -0500165 BOOST_CHECK_EQUAL(comp2.type(), type);
166 BOOST_CHECK_EQUAL(comp2.isNumber(), false);
167 const auto comp2Uri = to_string(type) + "=%01%02%03";
168 BOOST_CHECK_EQUAL(comp2.toUri(), comp2Uri);
169 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(comp2), comp2Uri);
170 BOOST_CHECK_EQUAL(comp2, Component::fromEscapedString(comp2Uri));
171
172 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix), "invalid format");
173 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "foo"), "invalid format");
174 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "00"), "invalid format");
175 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "-1"), "invalid format");
176 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "9.3"), "invalid format");
177 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + " 84"), "invalid format");
178 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "0xAF"), "invalid format");
179 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "18446744073709551616"), "out of range");
180 CHECK_COMP_ERR(Component::fromEscapedString(boost::to_upper_copy(uriPrefix) + "42"), "Unknown TLV-TYPE");
181}
182
183BOOST_AUTO_TEST_CASE(Segment)
184{
185 testDecimalComponent(tlv::SegmentNameComponent, "seg=");
186}
187
188BOOST_AUTO_TEST_CASE(ByteOffset)
189{
190 testDecimalComponent(tlv::ByteOffsetNameComponent, "off=");
191}
192
193BOOST_AUTO_TEST_CASE(Version)
194{
195 testDecimalComponent(tlv::VersionNameComponent, "v=");
196}
197
198BOOST_AUTO_TEST_CASE(Timestamp)
199{
200 testDecimalComponent(tlv::TimestampNameComponent, "t=");
201}
202
203BOOST_AUTO_TEST_CASE(SequenceNum)
204{
205 testDecimalComponent(tlv::SequenceNumNameComponent, "seq=");
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000206}
207
Davide Pesavento21ee8992022-02-25 20:46:04 -0500208BOOST_AUTO_TEST_CASE(Keyword)
209{
210 Component comp("2007 6E646E2D637878"_block);
211 BOOST_CHECK_EQUAL(comp.type(), tlv::KeywordNameComponent);
212 BOOST_CHECK_EQUAL(comp.isKeyword(), true);
213 BOOST_CHECK_EQUAL(comp.toUri(), "32=ndn-cxx");
214 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::CANONICAL), "32=ndn-cxx");
215 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ALTERNATE), "32=ndn-cxx");
216 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_CANONICAL), "32=ndn-cxx");
217 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_ALTERNATE), "32=ndn-cxx");
218 BOOST_CHECK_EQUAL(Component::fromEscapedString("32=ndn-cxx"), comp);
219
220 comp.wireDecode("2000"_block);
221 BOOST_CHECK_EQUAL(comp.type(), tlv::KeywordNameComponent);
222 BOOST_CHECK_EQUAL(comp.isKeyword(), true);
223 BOOST_CHECK_EQUAL(comp.toUri(), "32=...");
224 BOOST_CHECK_EQUAL(Component::fromEscapedString("32=..."), comp);
225
226 BOOST_CHECK_THROW(Component::fromEscapedString("32="), Component::Error);
227 BOOST_CHECK_THROW(Component::fromEscapedString("32=."), Component::Error);
228 BOOST_CHECK_THROW(Component::fromEscapedString("32=.."), Component::Error);
229}
230
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000231BOOST_AUTO_TEST_CASE(OtherType)
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000232{
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000233 Component comp("0907 6E646E2D637878"_block);
234 BOOST_CHECK_EQUAL(comp.type(), 0x09);
235 BOOST_CHECK_EQUAL(comp.toUri(), "9=ndn-cxx");
Davide Pesaventobc17d952020-02-15 20:10:52 -0500236 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::CANONICAL), "9=ndn-cxx");
237 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ALTERNATE), "9=ndn-cxx");
238 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_CANONICAL), "9=ndn-cxx");
239 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_ALTERNATE), "9=ndn-cxx");
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000240 BOOST_CHECK_EQUAL(Component::fromEscapedString("9=ndn-cxx"), comp);
241
242 comp.wireDecode("FDFFFF00"_block);
243 BOOST_CHECK_EQUAL(comp.type(), 0xFFFF);
244 BOOST_CHECK_EQUAL(comp.toUri(), "65535=...");
245 BOOST_CHECK_EQUAL(Component::fromEscapedString("65535=..."), comp);
246
247 comp.wireDecode("FD576501 2E"_block);
248 BOOST_CHECK_EQUAL(comp.type(), 0x5765);
249 BOOST_CHECK_EQUAL(comp.toUri(), "22373=....");
250 BOOST_CHECK_EQUAL(Component::fromEscapedString("22373=...."), comp);
251
252 BOOST_CHECK_THROW(Component::fromEscapedString("3="), Component::Error);
253 BOOST_CHECK_THROW(Component::fromEscapedString("3=."), Component::Error);
254 BOOST_CHECK_THROW(Component::fromEscapedString("3=.."), Component::Error);
255}
256
257BOOST_AUTO_TEST_CASE(InvalidType)
258{
259 Component comp;
Davide Pesavento6b330402019-04-24 00:14:01 -0400260 BOOST_CHECK_THROW(comp.wireDecode(Block{}), Component::Error);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000261 BOOST_CHECK_THROW(comp.wireDecode("FE0001000001 80"_block), Component::Error);
262
263 BOOST_CHECK_THROW(Component::fromEscapedString("0=A"), Component::Error);
264 BOOST_CHECK_THROW(Component::fromEscapedString("65536=A"), Component::Error);
Junxiao Shid2e60632018-08-10 10:48:44 -0600265 BOOST_CHECK_THROW(Component::fromEscapedString("4294967296=A"), Component::Error);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000266 BOOST_CHECK_THROW(Component::fromEscapedString("-1=A"), Component::Error);
267 BOOST_CHECK_THROW(Component::fromEscapedString("+=A"), Component::Error);
268 BOOST_CHECK_THROW(Component::fromEscapedString("=A"), Component::Error);
269 BOOST_CHECK_THROW(Component::fromEscapedString("0x1=A"), Component::Error);
270 BOOST_CHECK_THROW(Component::fromEscapedString("Z=A"), Component::Error);
271 BOOST_CHECK_THROW(Component::fromEscapedString("09=A"), Component::Error);
Junxiao Shi4053bd52018-08-16 13:39:25 -0600272 BOOST_CHECK_THROW(Component::fromEscapedString("0x3=A"), Component::Error);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000273 BOOST_CHECK_THROW(Component::fromEscapedString("+9=A"), Component::Error);
Junxiao Shid2e60632018-08-10 10:48:44 -0600274 BOOST_CHECK_THROW(Component::fromEscapedString(" 9=A"), Component::Error);
275 BOOST_CHECK_THROW(Component::fromEscapedString("9 =A"), Component::Error);
276 BOOST_CHECK_THROW(Component::fromEscapedString("9.0=A"), Component::Error);
277 BOOST_CHECK_THROW(Component::fromEscapedString("9E0=A"), Component::Error);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000278}
279
280BOOST_AUTO_TEST_SUITE_END() // Decode
281
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400282BOOST_AUTO_TEST_CASE(ConstructFromSpan)
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000283{
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400284 const uint8_t arr[] = {1, 2, 3};
285 Component c1(arr);
286 BOOST_TEST(c1.wireEncode() == "0803010203"_block);
287 Component c2(128, arr);
288 BOOST_TEST(c2.wireEncode() == "8003010203"_block);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000289
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400290 const std::vector<uint8_t> vec = {4, 5, 6};
291 Component c3(vec);
292 BOOST_TEST(c3.wireEncode() == "0803040506"_block);
293 Component c4(128, vec);
294 BOOST_TEST(c4.wireEncode() == "8003040506"_block);
295
296 Component c5(128, {7, 8});
297 BOOST_TEST(c5.wireEncode() == "80020708"_block);
298
299 const Block b("090109"_block);
300 Component c6(128, b);
301 BOOST_TEST(c6.wireEncode() == "8003090109"_block);
Davide Pesavento08378cb2018-02-01 16:10:54 -0500302}
303
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400304BOOST_AUTO_TEST_SUITE(ConstructFromIterators) // Bug 2490
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000305
Davide Pesavento009062d2019-11-20 00:16:33 -0500306using ContainerTypes = boost::mpl::vector<std::vector<uint8_t>,
307 std::list<uint8_t>,
308 std::vector<int8_t>,
309 std::list<int8_t>>;
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000310
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400311BOOST_AUTO_TEST_CASE_TEMPLATE(ZeroOctets, T, ContainerTypes)
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000312{
313 T bytes;
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000314 Component c(bytes.begin(), bytes.end());
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400315 BOOST_TEST(c.type() == tlv::GenericNameComponent);
316 BOOST_TEST(c.value_size() == 0);
317 BOOST_TEST(c.size() == 2);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000318}
319
320BOOST_AUTO_TEST_CASE_TEMPLATE(OneOctet, T, ContainerTypes)
321{
322 T bytes{1};
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400323 Component c(9, bytes.begin(), bytes.end());
324 BOOST_TEST(c.type() == 0x09);
325 BOOST_TEST(c.value_size() == 1);
326 BOOST_TEST(c.size() == 3);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000327}
328
329BOOST_AUTO_TEST_CASE_TEMPLATE(FourOctets, T, ContainerTypes)
330{
331 T bytes{1, 2, 3, 4};
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000332 Component c(0xFCEC, bytes.begin(), bytes.end());
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400333 BOOST_TEST(c.type() == 0xFCEC);
334 BOOST_TEST(c.value_size() == 4);
335 BOOST_TEST(c.size() == 8);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000336}
337
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400338BOOST_AUTO_TEST_SUITE_END() // ConstructFromIterators
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000339
Junxiao Shi71ff2312017-07-12 13:32:50 +0000340BOOST_AUTO_TEST_SUITE(NamingConvention)
341
342template<typename ArgType>
343struct ConventionTest
344{
Davide Pesavento009062d2019-11-20 00:16:33 -0500345 std::function<Component(ArgType)> makeComponent;
346 std::function<ArgType(const Component&)> getValue;
347 std::function<Name&(Name&, ArgType)> append;
Junxiao Shi71ff2312017-07-12 13:32:50 +0000348 Name expected;
349 ArgType value;
Davide Pesavento009062d2019-11-20 00:16:33 -0500350 std::function<bool(const Component&)> isComponent;
Junxiao Shi71ff2312017-07-12 13:32:50 +0000351};
352
Junxiao Shie2099612019-02-15 14:46:27 +0000353class ConventionMarker
354{
Eric Newberryc25e4632021-02-11 10:48:11 -0800355public:
356 ConventionMarker()
357 {
358 name::setConventionEncoding(name::Convention::MARKER);
359 }
360
361 ~ConventionMarker()
362 {
363 name::setConventionEncoding(name::Convention::TYPED);
364 }
Junxiao Shie2099612019-02-15 14:46:27 +0000365};
366
367class ConventionTyped
368{
Junxiao Shie2099612019-02-15 14:46:27 +0000369};
370
Junxiao Shi71ff2312017-07-12 13:32:50 +0000371class NumberWithMarker
372{
373public:
Junxiao Shie2099612019-02-15 14:46:27 +0000374 using ConventionRev = ConventionMarker;
375
Junxiao Shi71ff2312017-07-12 13:32:50 +0000376 ConventionTest<uint64_t>
377 operator()() const
378 {
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500379 return {[] (auto num) { return Component::fromNumberWithMarker(0xAA, num); },
380 [] (const Component& c) { return c.toNumberWithMarker(0xAA); },
381 [] (Name& name, auto num) -> Name& { return name.appendNumberWithMarker(0xAA, num); },
Junxiao Shi71ff2312017-07-12 13:32:50 +0000382 Name("/%AA%03%E8"),
383 1000,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500384 [] (const Component& c) { return c.isNumberWithMarker(0xAA); }};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000385 }
386};
387
Junxiao Shie2099612019-02-15 14:46:27 +0000388class SegmentMarker
Junxiao Shi71ff2312017-07-12 13:32:50 +0000389{
390public:
Junxiao Shie2099612019-02-15 14:46:27 +0000391 using ConventionRev = ConventionMarker;
392
Junxiao Shi71ff2312017-07-12 13:32:50 +0000393 ConventionTest<uint64_t>
394 operator()() const
395 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000396 return {&Component::fromSegment,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500397 &Component::toSegment,
398 &Name::appendSegment,
Junxiao Shi71ff2312017-07-12 13:32:50 +0000399 Name("/%00%27%10"),
400 10000,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500401 &Component::isSegment};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000402 }
403};
404
Junxiao Shie2099612019-02-15 14:46:27 +0000405class SegmentTyped
Junxiao Shi71ff2312017-07-12 13:32:50 +0000406{
407public:
Junxiao Shie2099612019-02-15 14:46:27 +0000408 using ConventionRev = ConventionTyped;
409
410 ConventionTest<uint64_t>
411 operator()() const
412 {
413 return {&Component::fromSegment,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500414 &Component::toSegment,
415 &Name::appendSegment,
Davide Pesavento47a94d12021-10-31 16:41:05 -0400416 Name("/50=%27%10"),
Junxiao Shie2099612019-02-15 14:46:27 +0000417 10000,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500418 &Component::isSegment};
Junxiao Shie2099612019-02-15 14:46:27 +0000419 }
420};
421
Junxiao Shie2099612019-02-15 14:46:27 +0000422class ByteOffsetTyped
Junxiao Shi71ff2312017-07-12 13:32:50 +0000423{
424public:
Junxiao Shie2099612019-02-15 14:46:27 +0000425 using ConventionRev = ConventionTyped;
426
427 ConventionTest<uint64_t>
428 operator()() const
429 {
430 return {&Component::fromByteOffset,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500431 &Component::toByteOffset,
432 &Name::appendByteOffset,
Davide Pesavento47a94d12021-10-31 16:41:05 -0400433 Name("/52=%00%01%86%A0"),
Junxiao Shie2099612019-02-15 14:46:27 +0000434 100000,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500435 &Component::isByteOffset};
Junxiao Shie2099612019-02-15 14:46:27 +0000436 }
437};
438
439class VersionMarker
440{
441public:
442 using ConventionRev = ConventionMarker;
443
Junxiao Shi71ff2312017-07-12 13:32:50 +0000444 ConventionTest<uint64_t>
445 operator()() const
446 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000447 return {&Component::fromVersion,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500448 &Component::toVersion,
449 [] (Name& name, auto version) -> Name& { return name.appendVersion(version); },
Junxiao Shi71ff2312017-07-12 13:32:50 +0000450 Name("/%FD%00%0FB%40"),
451 1000000,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500452 &Component::isVersion};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000453 }
454};
455
Junxiao Shie2099612019-02-15 14:46:27 +0000456class VersionTyped
Junxiao Shi71ff2312017-07-12 13:32:50 +0000457{
458public:
Junxiao Shie2099612019-02-15 14:46:27 +0000459 using ConventionRev = ConventionTyped;
460
461 ConventionTest<uint64_t>
462 operator()() const
463 {
464 return {&Component::fromVersion,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500465 &Component::toVersion,
466 [] (Name& name, auto version) -> Name& { return name.appendVersion(version); },
Davide Pesavento47a94d12021-10-31 16:41:05 -0400467 Name("/54=%00%0FB%40"),
Junxiao Shie2099612019-02-15 14:46:27 +0000468 1000000,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500469 &Component::isVersion};
Junxiao Shie2099612019-02-15 14:46:27 +0000470 }
471};
472
473class TimestampMarker
474{
475public:
476 using ConventionRev = ConventionMarker;
477
Davide Pesavento47a94d12021-10-31 16:41:05 -0400478 ConventionTest<time::system_clock::time_point>
Junxiao Shi71ff2312017-07-12 13:32:50 +0000479 operator()() const
480 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000481 return {&Component::fromTimestamp,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500482 &Component::toTimestamp,
483 [] (Name& name, auto tp) -> Name& { return name.appendTimestamp(tp); },
Junxiao Shi71ff2312017-07-12 13:32:50 +0000484 Name("/%FC%00%04%7BE%E3%1B%00%00"),
Davide Pesavento0f830802018-01-16 23:58:58 -0500485 time::getUnixEpoch() + 14600_days, // 40 years
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500486 &Component::isTimestamp};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000487 }
488};
489
Junxiao Shie2099612019-02-15 14:46:27 +0000490class TimestampTyped
Junxiao Shi71ff2312017-07-12 13:32:50 +0000491{
492public:
Junxiao Shie2099612019-02-15 14:46:27 +0000493 using ConventionRev = ConventionTyped;
494
Davide Pesavento47a94d12021-10-31 16:41:05 -0400495 ConventionTest<time::system_clock::time_point>
Junxiao Shie2099612019-02-15 14:46:27 +0000496 operator()() const
497 {
498 return {&Component::fromTimestamp,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500499 &Component::toTimestamp,
500 [] (Name& name, auto tp) -> Name& { return name.appendTimestamp(tp); },
Davide Pesavento47a94d12021-10-31 16:41:05 -0400501 Name("/56=%00%04%7BE%E3%1B%00%00"),
Junxiao Shie2099612019-02-15 14:46:27 +0000502 time::getUnixEpoch() + 14600_days, // 40 years
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500503 &Component::isTimestamp};
Junxiao Shie2099612019-02-15 14:46:27 +0000504 }
505};
506
507class SequenceNumberMarker
508{
509public:
510 using ConventionRev = ConventionMarker;
511
Junxiao Shi71ff2312017-07-12 13:32:50 +0000512 ConventionTest<uint64_t>
513 operator()() const
514 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000515 return {&Component::fromSequenceNumber,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500516 &Component::toSequenceNumber,
517 &Name::appendSequenceNumber,
Junxiao Shi71ff2312017-07-12 13:32:50 +0000518 Name("/%FE%00%98%96%80"),
519 10000000,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500520 &Component::isSequenceNumber};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000521 }
522};
523
Junxiao Shie2099612019-02-15 14:46:27 +0000524class SequenceNumberTyped
525{
526public:
527 using ConventionRev = ConventionTyped;
528
529 ConventionTest<uint64_t>
530 operator()() const
531 {
532 return {&Component::fromSequenceNumber,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500533 &Component::toSequenceNumber,
534 &Name::appendSequenceNumber,
Davide Pesavento47a94d12021-10-31 16:41:05 -0400535 Name("/58=%00%98%96%80"),
Junxiao Shie2099612019-02-15 14:46:27 +0000536 10000000,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500537 &Component::isSequenceNumber};
Junxiao Shie2099612019-02-15 14:46:27 +0000538 }
539};
540
Junxiao Shi71ff2312017-07-12 13:32:50 +0000541using ConventionTests = boost::mpl::vector<
542 NumberWithMarker,
Junxiao Shie2099612019-02-15 14:46:27 +0000543 SegmentMarker,
544 SegmentTyped,
Junxiao Shie2099612019-02-15 14:46:27 +0000545 ByteOffsetTyped,
546 VersionMarker,
547 VersionTyped,
548 TimestampMarker,
549 TimestampTyped,
550 SequenceNumberMarker,
551 SequenceNumberTyped
Junxiao Shi71ff2312017-07-12 13:32:50 +0000552>;
553
Junxiao Shie2099612019-02-15 14:46:27 +0000554BOOST_FIXTURE_TEST_CASE_TEMPLATE(Convention, T, ConventionTests, T::ConventionRev)
Junxiao Shi71ff2312017-07-12 13:32:50 +0000555{
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000556 Component invalidComponent1;
557 Component invalidComponent2("1234567890");
Junxiao Shi71ff2312017-07-12 13:32:50 +0000558
559 auto test = T()();
Junxiao Shi71ff2312017-07-12 13:32:50 +0000560 const Name& expected = test.expected;
Junxiao Shi71ff2312017-07-12 13:32:50 +0000561
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000562 Component actualComponent = test.makeComponent(test.value);
Junxiao Shi71ff2312017-07-12 13:32:50 +0000563 BOOST_CHECK_EQUAL(actualComponent, expected[0]);
564
565 Name actualName;
566 test.append(actualName, test.value);
567 BOOST_CHECK_EQUAL(actualName, expected);
568
569 BOOST_CHECK_EQUAL(test.isComponent(expected[0]), true);
570 BOOST_CHECK_EQUAL(test.getValue(expected[0]), test.value);
571
572 BOOST_CHECK_EQUAL(test.isComponent(invalidComponent1), false);
573 BOOST_CHECK_EQUAL(test.isComponent(invalidComponent2), false);
574
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000575 BOOST_CHECK_THROW(test.getValue(invalidComponent1), Component::Error);
576 BOOST_CHECK_THROW(test.getValue(invalidComponent2), Component::Error);
Junxiao Shi71ff2312017-07-12 13:32:50 +0000577}
578
579BOOST_AUTO_TEST_SUITE_END() // NamingConvention
580
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400581BOOST_AUTO_TEST_CASE(Compare)
582{
583 const std::vector<Component> comps = {
584 Component("0120 0000000000000000000000000000000000000000000000000000000000000000"_block),
585 Component("0120 0000000000000000000000000000000000000000000000000000000000000001"_block),
586 Component("0120 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"_block),
587 Component("0220 0000000000000000000000000000000000000000000000000000000000000000"_block),
588 Component("0220 0000000000000000000000000000000000000000000000000000000000000001"_block),
589 Component("0220 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"_block),
590 Component(0x03),
591 Component("0301 44"_block),
592 Component("0301 46"_block),
593 Component("0302 4141"_block),
594 Component(),
595 Component("D"),
596 Component("F"),
597 Component("AA"),
598 Component(0x53B2),
599 Component("FD53B201 44"_block),
600 Component("FD53B201 46"_block),
601 Component("FD53B202 4141"_block),
602 };
603
604 for (size_t i = 0; i < comps.size(); ++i) {
605 for (size_t j = 0; j < comps.size(); ++j) {
606 const auto& lhs = comps[i];
607 const auto& rhs = comps[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 BOOST_CHECK_EQUAL(lhs >= rhs, i >= j);
614 }
615 }
616}
617
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000618BOOST_AUTO_TEST_SUITE_END() // TestNameComponent
619
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400620} // namespace ndn::tests