blob: bd0a553661fb9e14dbca5cbf082b6def0d00f1a1 [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 Pesavento21ee8992022-02-25 20:46:04 -05003 * Copyright (c) 2013-2022 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
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000037BOOST_AUTO_TEST_SUITE(TestNameComponent)
Junxiao Shidf4b24e2016-07-14 21:41:43 +000038
39BOOST_AUTO_TEST_SUITE(Decode)
40
Davide Pesavento009062d2019-11-20 00:16:33 -050041#define CHECK_COMP_ERR(expr, whatstring) \
42 BOOST_CHECK_EXCEPTION(expr, Component::Error, \
43 [] (const auto& e) { return boost::contains(e.what(), whatstring); })
44
Junxiao Shidf4b24e2016-07-14 21:41:43 +000045BOOST_AUTO_TEST_CASE(Generic)
46{
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000047 Component comp("0807 6E646E2D637878"_block);
48 BOOST_CHECK_EQUAL(comp.type(), tlv::GenericNameComponent);
Davide Pesavento009062d2019-11-20 00:16:33 -050049 BOOST_CHECK_EQUAL(comp.isGeneric(), true);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000050 BOOST_CHECK_EQUAL(comp.toUri(), "ndn-cxx");
Junxiao Shia39c0b52019-12-31 15:13:15 -070051 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::CANONICAL), "8=ndn-cxx");
52 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ALTERNATE), "ndn-cxx");
Davide Pesaventobc17d952020-02-15 20:10:52 -050053 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_CANONICAL), "8=ndn-cxx");
54 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_ALTERNATE), "ndn-cxx");
Davide Pesavento009062d2019-11-20 00:16:33 -050055 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(comp), "ndn-cxx");
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000056 BOOST_CHECK_EQUAL(Component::fromEscapedString("ndn-cxx"), comp);
Junxiao Shid2e60632018-08-10 10:48:44 -060057 BOOST_CHECK_EQUAL(Component::fromEscapedString("8=ndn-cxx"), comp);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000058
59 comp.wireDecode("0800"_block);
60 BOOST_CHECK_EQUAL(comp.toUri(), "...");
61 BOOST_CHECK_EQUAL(Component::fromEscapedString("..."), comp);
Junxiao Shid2e60632018-08-10 10:48:44 -060062 BOOST_CHECK_EQUAL(Component::fromEscapedString("8=..."), comp);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000063 BOOST_CHECK_EQUAL(Component::fromEscapedString(".%2E."), comp);
64
65 comp.wireDecode("0801 2E"_block);
66 BOOST_CHECK_EQUAL(comp.toUri(), "....");
67 BOOST_CHECK_EQUAL(Component::fromEscapedString("...."), comp);
68 BOOST_CHECK_EQUAL(Component::fromEscapedString("%2E..%2E"), comp);
69
70 comp.wireDecode("0803 2E412E"_block);
71 BOOST_CHECK_EQUAL(comp.toUri(), ".A.");
72 BOOST_CHECK_EQUAL(Component::fromEscapedString(".A."), comp);
73
74 comp.wireDecode("0807 666F6F25626172"_block);
75 BOOST_CHECK_EQUAL(comp.toUri(), "foo%25bar");
76 BOOST_CHECK_EQUAL(Component::fromEscapedString("foo%25bar"), comp);
Junxiao Shid2e60632018-08-10 10:48:44 -060077 BOOST_CHECK_EQUAL(Component::fromEscapedString("8=foo%25bar"), comp);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000078
79 comp.wireDecode("0804 2D2E5F7E"_block);
80 BOOST_CHECK_EQUAL(comp.toUri(), "-._~");
81 BOOST_CHECK_EQUAL(Component::fromEscapedString("-._~"), comp);
82
Junxiao Shid2e60632018-08-10 10:48:44 -060083 comp.wireDecode("0803 393D41"_block);
84 BOOST_CHECK_EQUAL(comp.toUri(), "9%3DA");
85 BOOST_CHECK_EQUAL(Component::fromEscapedString("9%3DA"), comp);
86
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000087 comp = Component(":/?#[]@");
88 BOOST_CHECK_EQUAL(comp.toUri(), "%3A%2F%3F%23%5B%5D%40");
89 BOOST_CHECK_EQUAL(Component::fromEscapedString("%3A%2F%3F%23%5B%5D%40"), comp);
90
91 BOOST_CHECK_THROW(Component::fromEscapedString(""), Component::Error);
92 BOOST_CHECK_THROW(Component::fromEscapedString("."), Component::Error);
93 BOOST_CHECK_THROW(Component::fromEscapedString(".."), Component::Error);
Davide Pesavento21ee8992022-02-25 20:46:04 -050094 BOOST_CHECK_THROW(Component::fromEscapedString("8="), Component::Error);
Junxiao Shidf4b24e2016-07-14 21:41:43 +000095}
96
Junxiao Shi4053bd52018-08-16 13:39:25 -060097static void
Davide Pesavento009062d2019-11-20 00:16:33 -050098testSha256Component(uint32_t type, const std::string& uriPrefix)
Junxiao Shidf4b24e2016-07-14 21:41:43 +000099{
Davide Pesavento009062d2019-11-20 00:16:33 -0500100 const std::string hexLower = "28bad4b5275bd392dbb670c75cf0b66f13f7942b21e80f55c0e86b374753a548";
101 const std::string hexUpper = boost::to_upper_copy(hexLower);
Junxiao Shi4053bd52018-08-16 13:39:25 -0600102 std::string hexPct;
103 for (size_t i = 0; i < hexUpper.size(); i += 2) {
104 hexPct += "%" + hexUpper.substr(i, 2);
105 }
Junxiao Shia39c0b52019-12-31 15:13:15 -0700106 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 +0000107
Davide Pesavento21ee8992022-02-25 20:46:04 -0500108 Component comp(type, fromHex(hexLower));
Davide Pesavento009062d2019-11-20 00:16:33 -0500109
Junxiao Shi4053bd52018-08-16 13:39:25 -0600110 BOOST_CHECK_EQUAL(comp.type(), type);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000111 BOOST_CHECK_EQUAL(comp.toUri(), uriPrefix + hexLower);
Junxiao Shia39c0b52019-12-31 15:13:15 -0700112 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::CANONICAL), to_string(type) + "=" + hexPctCanonical);
113 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ALTERNATE), uriPrefix + hexLower);
Davide Pesaventobc17d952020-02-15 20:10:52 -0500114 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_CANONICAL), to_string(type) + "=" + hexPctCanonical);
115 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_ALTERNATE), uriPrefix + hexLower);
Davide Pesavento009062d2019-11-20 00:16:33 -0500116 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(comp), uriPrefix + hexLower);
117 BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(uriPrefix + hexLower));
118 BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(uriPrefix + hexUpper));
119 BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(to_string(type) + "=" + hexPct));
Junxiao Shia39c0b52019-12-31 15:13:15 -0700120 BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(to_string(type) + "=" + hexPctCanonical));
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000121
Davide Pesavento009062d2019-11-20 00:16:33 -0500122 CHECK_COMP_ERR(comp.wireDecode(Block(type, fromHex("A791806951F25C4D"))), "TLV-LENGTH must be 32");
123 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix), "TLV-LENGTH must be 32");
124 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "a791806951f25c4d"), "TLV-LENGTH must be 32");
125 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "foo"), "invalid hex encoding");
126 CHECK_COMP_ERR(Component::fromEscapedString(boost::to_upper_copy(uriPrefix) + hexLower), "Unknown TLV-TYPE");
Junxiao Shi4053bd52018-08-16 13:39:25 -0600127}
128
Davide Pesavento009062d2019-11-20 00:16:33 -0500129BOOST_AUTO_TEST_CASE(ImplicitDigest)
Junxiao Shi4053bd52018-08-16 13:39:25 -0600130{
Davide Pesavento009062d2019-11-20 00:16:33 -0500131 testSha256Component(tlv::ImplicitSha256DigestComponent, "sha256digest=");
Junxiao Shi4053bd52018-08-16 13:39:25 -0600132}
133
Davide Pesavento009062d2019-11-20 00:16:33 -0500134BOOST_AUTO_TEST_CASE(ParametersDigest)
Junxiao Shi4053bd52018-08-16 13:39:25 -0600135{
Davide Pesavento009062d2019-11-20 00:16:33 -0500136 testSha256Component(tlv::ParametersSha256DigestComponent, "params-sha256=");
137}
138
139static void
140testDecimalComponent(uint32_t type, const std::string& uriPrefix)
141{
Davide Pesavento47a94d12021-10-31 16:41:05 -0400142 const Component comp(makeNonNegativeIntegerBlock(type, 42)); // TLV-VALUE is a NonNegativeInteger
Davide Pesavento009062d2019-11-20 00:16:33 -0500143 BOOST_CHECK_EQUAL(comp.type(), type);
144 BOOST_CHECK_EQUAL(comp.isNumber(), true);
145 const auto compUri = uriPrefix + "42";
146 BOOST_CHECK_EQUAL(comp.toUri(), compUri);
Junxiao Shia39c0b52019-12-31 15:13:15 -0700147 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::CANONICAL), to_string(type) + "=%2A");
148 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ALTERNATE), compUri);
Davide Pesaventobc17d952020-02-15 20:10:52 -0500149 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_CANONICAL), to_string(type) + "=%2A");
150 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_ALTERNATE), compUri);
Davide Pesavento009062d2019-11-20 00:16:33 -0500151 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(comp), compUri);
152 BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(compUri));
153 BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(to_string(type) + "=%2A"));
154 BOOST_CHECK_EQUAL(comp, Component::fromNumber(42, type));
155
Davide Pesavento21ee8992022-02-25 20:46:04 -0500156 const Component comp2(type, fromHex("010203")); // TLV-VALUE is *not* a NonNegativeInteger
Davide Pesavento009062d2019-11-20 00:16:33 -0500157 BOOST_CHECK_EQUAL(comp2.type(), type);
158 BOOST_CHECK_EQUAL(comp2.isNumber(), false);
159 const auto comp2Uri = to_string(type) + "=%01%02%03";
160 BOOST_CHECK_EQUAL(comp2.toUri(), comp2Uri);
161 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(comp2), comp2Uri);
162 BOOST_CHECK_EQUAL(comp2, Component::fromEscapedString(comp2Uri));
163
164 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix), "invalid format");
165 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "foo"), "invalid format");
166 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "00"), "invalid format");
167 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "-1"), "invalid format");
168 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "9.3"), "invalid format");
169 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + " 84"), "invalid format");
170 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "0xAF"), "invalid format");
171 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "18446744073709551616"), "out of range");
172 CHECK_COMP_ERR(Component::fromEscapedString(boost::to_upper_copy(uriPrefix) + "42"), "Unknown TLV-TYPE");
173}
174
175BOOST_AUTO_TEST_CASE(Segment)
176{
177 testDecimalComponent(tlv::SegmentNameComponent, "seg=");
178}
179
180BOOST_AUTO_TEST_CASE(ByteOffset)
181{
182 testDecimalComponent(tlv::ByteOffsetNameComponent, "off=");
183}
184
185BOOST_AUTO_TEST_CASE(Version)
186{
187 testDecimalComponent(tlv::VersionNameComponent, "v=");
188}
189
190BOOST_AUTO_TEST_CASE(Timestamp)
191{
192 testDecimalComponent(tlv::TimestampNameComponent, "t=");
193}
194
195BOOST_AUTO_TEST_CASE(SequenceNum)
196{
197 testDecimalComponent(tlv::SequenceNumNameComponent, "seq=");
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000198}
199
Davide Pesavento21ee8992022-02-25 20:46:04 -0500200BOOST_AUTO_TEST_CASE(Keyword)
201{
202 Component comp("2007 6E646E2D637878"_block);
203 BOOST_CHECK_EQUAL(comp.type(), tlv::KeywordNameComponent);
204 BOOST_CHECK_EQUAL(comp.isKeyword(), true);
205 BOOST_CHECK_EQUAL(comp.toUri(), "32=ndn-cxx");
206 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::CANONICAL), "32=ndn-cxx");
207 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ALTERNATE), "32=ndn-cxx");
208 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_CANONICAL), "32=ndn-cxx");
209 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_ALTERNATE), "32=ndn-cxx");
210 BOOST_CHECK_EQUAL(Component::fromEscapedString("32=ndn-cxx"), comp);
211
212 comp.wireDecode("2000"_block);
213 BOOST_CHECK_EQUAL(comp.type(), tlv::KeywordNameComponent);
214 BOOST_CHECK_EQUAL(comp.isKeyword(), true);
215 BOOST_CHECK_EQUAL(comp.toUri(), "32=...");
216 BOOST_CHECK_EQUAL(Component::fromEscapedString("32=..."), comp);
217
218 BOOST_CHECK_THROW(Component::fromEscapedString("32="), Component::Error);
219 BOOST_CHECK_THROW(Component::fromEscapedString("32=."), Component::Error);
220 BOOST_CHECK_THROW(Component::fromEscapedString("32=.."), Component::Error);
221}
222
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000223BOOST_AUTO_TEST_CASE(OtherType)
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000224{
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000225 Component comp("0907 6E646E2D637878"_block);
226 BOOST_CHECK_EQUAL(comp.type(), 0x09);
227 BOOST_CHECK_EQUAL(comp.toUri(), "9=ndn-cxx");
Davide Pesaventobc17d952020-02-15 20:10:52 -0500228 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::CANONICAL), "9=ndn-cxx");
229 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ALTERNATE), "9=ndn-cxx");
230 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_CANONICAL), "9=ndn-cxx");
231 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_ALTERNATE), "9=ndn-cxx");
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000232 BOOST_CHECK_EQUAL(Component::fromEscapedString("9=ndn-cxx"), comp);
233
234 comp.wireDecode("FDFFFF00"_block);
235 BOOST_CHECK_EQUAL(comp.type(), 0xFFFF);
236 BOOST_CHECK_EQUAL(comp.toUri(), "65535=...");
237 BOOST_CHECK_EQUAL(Component::fromEscapedString("65535=..."), comp);
238
239 comp.wireDecode("FD576501 2E"_block);
240 BOOST_CHECK_EQUAL(comp.type(), 0x5765);
241 BOOST_CHECK_EQUAL(comp.toUri(), "22373=....");
242 BOOST_CHECK_EQUAL(Component::fromEscapedString("22373=...."), comp);
243
244 BOOST_CHECK_THROW(Component::fromEscapedString("3="), Component::Error);
245 BOOST_CHECK_THROW(Component::fromEscapedString("3=."), Component::Error);
246 BOOST_CHECK_THROW(Component::fromEscapedString("3=.."), Component::Error);
247}
248
249BOOST_AUTO_TEST_CASE(InvalidType)
250{
251 Component comp;
Davide Pesavento6b330402019-04-24 00:14:01 -0400252 BOOST_CHECK_THROW(comp.wireDecode(Block{}), Component::Error);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000253 BOOST_CHECK_THROW(comp.wireDecode("FE0001000001 80"_block), Component::Error);
254
255 BOOST_CHECK_THROW(Component::fromEscapedString("0=A"), Component::Error);
256 BOOST_CHECK_THROW(Component::fromEscapedString("65536=A"), Component::Error);
Junxiao Shid2e60632018-08-10 10:48:44 -0600257 BOOST_CHECK_THROW(Component::fromEscapedString("4294967296=A"), Component::Error);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000258 BOOST_CHECK_THROW(Component::fromEscapedString("-1=A"), Component::Error);
259 BOOST_CHECK_THROW(Component::fromEscapedString("+=A"), Component::Error);
260 BOOST_CHECK_THROW(Component::fromEscapedString("=A"), Component::Error);
261 BOOST_CHECK_THROW(Component::fromEscapedString("0x1=A"), Component::Error);
262 BOOST_CHECK_THROW(Component::fromEscapedString("Z=A"), Component::Error);
263 BOOST_CHECK_THROW(Component::fromEscapedString("09=A"), Component::Error);
Junxiao Shi4053bd52018-08-16 13:39:25 -0600264 BOOST_CHECK_THROW(Component::fromEscapedString("0x3=A"), Component::Error);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000265 BOOST_CHECK_THROW(Component::fromEscapedString("+9=A"), Component::Error);
Junxiao Shid2e60632018-08-10 10:48:44 -0600266 BOOST_CHECK_THROW(Component::fromEscapedString(" 9=A"), Component::Error);
267 BOOST_CHECK_THROW(Component::fromEscapedString("9 =A"), Component::Error);
268 BOOST_CHECK_THROW(Component::fromEscapedString("9.0=A"), Component::Error);
269 BOOST_CHECK_THROW(Component::fromEscapedString("9E0=A"), Component::Error);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000270}
271
272BOOST_AUTO_TEST_SUITE_END() // Decode
273
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400274BOOST_AUTO_TEST_CASE(ConstructFromSpan)
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000275{
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400276 const uint8_t arr[] = {1, 2, 3};
277 Component c1(arr);
278 BOOST_TEST(c1.wireEncode() == "0803010203"_block);
279 Component c2(128, arr);
280 BOOST_TEST(c2.wireEncode() == "8003010203"_block);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000281
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400282 const std::vector<uint8_t> vec = {4, 5, 6};
283 Component c3(vec);
284 BOOST_TEST(c3.wireEncode() == "0803040506"_block);
285 Component c4(128, vec);
286 BOOST_TEST(c4.wireEncode() == "8003040506"_block);
287
288 Component c5(128, {7, 8});
289 BOOST_TEST(c5.wireEncode() == "80020708"_block);
290
291 const Block b("090109"_block);
292 Component c6(128, b);
293 BOOST_TEST(c6.wireEncode() == "8003090109"_block);
Davide Pesavento08378cb2018-02-01 16:10:54 -0500294}
295
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400296BOOST_AUTO_TEST_SUITE(ConstructFromIterators) // Bug 2490
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000297
Davide Pesavento009062d2019-11-20 00:16:33 -0500298using ContainerTypes = boost::mpl::vector<std::vector<uint8_t>,
299 std::list<uint8_t>,
300 std::vector<int8_t>,
301 std::list<int8_t>>;
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000302
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400303BOOST_AUTO_TEST_CASE_TEMPLATE(ZeroOctets, T, ContainerTypes)
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000304{
305 T bytes;
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000306 Component c(bytes.begin(), bytes.end());
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400307 BOOST_TEST(c.type() == tlv::GenericNameComponent);
308 BOOST_TEST(c.value_size() == 0);
309 BOOST_TEST(c.size() == 2);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000310}
311
312BOOST_AUTO_TEST_CASE_TEMPLATE(OneOctet, T, ContainerTypes)
313{
314 T bytes{1};
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400315 Component c(9, bytes.begin(), bytes.end());
316 BOOST_TEST(c.type() == 0x09);
317 BOOST_TEST(c.value_size() == 1);
318 BOOST_TEST(c.size() == 3);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000319}
320
321BOOST_AUTO_TEST_CASE_TEMPLATE(FourOctets, T, ContainerTypes)
322{
323 T bytes{1, 2, 3, 4};
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000324 Component c(0xFCEC, bytes.begin(), bytes.end());
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400325 BOOST_TEST(c.type() == 0xFCEC);
326 BOOST_TEST(c.value_size() == 4);
327 BOOST_TEST(c.size() == 8);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000328}
329
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400330BOOST_AUTO_TEST_SUITE_END() // ConstructFromIterators
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000331
Junxiao Shi71ff2312017-07-12 13:32:50 +0000332BOOST_AUTO_TEST_SUITE(NamingConvention)
333
334template<typename ArgType>
335struct ConventionTest
336{
Davide Pesavento009062d2019-11-20 00:16:33 -0500337 std::function<Component(ArgType)> makeComponent;
338 std::function<ArgType(const Component&)> getValue;
339 std::function<Name&(Name&, ArgType)> append;
Junxiao Shi71ff2312017-07-12 13:32:50 +0000340 Name expected;
341 ArgType value;
Davide Pesavento009062d2019-11-20 00:16:33 -0500342 std::function<bool(const Component&)> isComponent;
Junxiao Shi71ff2312017-07-12 13:32:50 +0000343};
344
Junxiao Shie2099612019-02-15 14:46:27 +0000345class ConventionMarker
346{
Eric Newberryc25e4632021-02-11 10:48:11 -0800347public:
348 ConventionMarker()
349 {
350 name::setConventionEncoding(name::Convention::MARKER);
351 }
352
353 ~ConventionMarker()
354 {
355 name::setConventionEncoding(name::Convention::TYPED);
356 }
Junxiao Shie2099612019-02-15 14:46:27 +0000357};
358
359class ConventionTyped
360{
Junxiao Shie2099612019-02-15 14:46:27 +0000361};
362
Junxiao Shi71ff2312017-07-12 13:32:50 +0000363class NumberWithMarker
364{
365public:
Junxiao Shie2099612019-02-15 14:46:27 +0000366 using ConventionRev = ConventionMarker;
367
Junxiao Shi71ff2312017-07-12 13:32:50 +0000368 ConventionTest<uint64_t>
369 operator()() const
370 {
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500371 return {[] (auto num) { return Component::fromNumberWithMarker(0xAA, num); },
372 [] (const Component& c) { return c.toNumberWithMarker(0xAA); },
373 [] (Name& name, auto num) -> Name& { return name.appendNumberWithMarker(0xAA, num); },
Junxiao Shi71ff2312017-07-12 13:32:50 +0000374 Name("/%AA%03%E8"),
375 1000,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500376 [] (const Component& c) { return c.isNumberWithMarker(0xAA); }};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000377 }
378};
379
Junxiao Shie2099612019-02-15 14:46:27 +0000380class SegmentMarker
Junxiao Shi71ff2312017-07-12 13:32:50 +0000381{
382public:
Junxiao Shie2099612019-02-15 14:46:27 +0000383 using ConventionRev = ConventionMarker;
384
Junxiao Shi71ff2312017-07-12 13:32:50 +0000385 ConventionTest<uint64_t>
386 operator()() const
387 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000388 return {&Component::fromSegment,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500389 &Component::toSegment,
390 &Name::appendSegment,
Junxiao Shi71ff2312017-07-12 13:32:50 +0000391 Name("/%00%27%10"),
392 10000,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500393 &Component::isSegment};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000394 }
395};
396
Junxiao Shie2099612019-02-15 14:46:27 +0000397class SegmentTyped
Junxiao Shi71ff2312017-07-12 13:32:50 +0000398{
399public:
Junxiao Shie2099612019-02-15 14:46:27 +0000400 using ConventionRev = ConventionTyped;
401
402 ConventionTest<uint64_t>
403 operator()() const
404 {
405 return {&Component::fromSegment,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500406 &Component::toSegment,
407 &Name::appendSegment,
Davide Pesavento47a94d12021-10-31 16:41:05 -0400408 Name("/50=%27%10"),
Junxiao Shie2099612019-02-15 14:46:27 +0000409 10000,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500410 &Component::isSegment};
Junxiao Shie2099612019-02-15 14:46:27 +0000411 }
412};
413
Junxiao Shie2099612019-02-15 14:46:27 +0000414class ByteOffsetTyped
Junxiao Shi71ff2312017-07-12 13:32:50 +0000415{
416public:
Junxiao Shie2099612019-02-15 14:46:27 +0000417 using ConventionRev = ConventionTyped;
418
419 ConventionTest<uint64_t>
420 operator()() const
421 {
422 return {&Component::fromByteOffset,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500423 &Component::toByteOffset,
424 &Name::appendByteOffset,
Davide Pesavento47a94d12021-10-31 16:41:05 -0400425 Name("/52=%00%01%86%A0"),
Junxiao Shie2099612019-02-15 14:46:27 +0000426 100000,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500427 &Component::isByteOffset};
Junxiao Shie2099612019-02-15 14:46:27 +0000428 }
429};
430
431class VersionMarker
432{
433public:
434 using ConventionRev = ConventionMarker;
435
Junxiao Shi71ff2312017-07-12 13:32:50 +0000436 ConventionTest<uint64_t>
437 operator()() const
438 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000439 return {&Component::fromVersion,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500440 &Component::toVersion,
441 [] (Name& name, auto version) -> Name& { return name.appendVersion(version); },
Junxiao Shi71ff2312017-07-12 13:32:50 +0000442 Name("/%FD%00%0FB%40"),
443 1000000,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500444 &Component::isVersion};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000445 }
446};
447
Junxiao Shie2099612019-02-15 14:46:27 +0000448class VersionTyped
Junxiao Shi71ff2312017-07-12 13:32:50 +0000449{
450public:
Junxiao Shie2099612019-02-15 14:46:27 +0000451 using ConventionRev = ConventionTyped;
452
453 ConventionTest<uint64_t>
454 operator()() const
455 {
456 return {&Component::fromVersion,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500457 &Component::toVersion,
458 [] (Name& name, auto version) -> Name& { return name.appendVersion(version); },
Davide Pesavento47a94d12021-10-31 16:41:05 -0400459 Name("/54=%00%0FB%40"),
Junxiao Shie2099612019-02-15 14:46:27 +0000460 1000000,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500461 &Component::isVersion};
Junxiao Shie2099612019-02-15 14:46:27 +0000462 }
463};
464
465class TimestampMarker
466{
467public:
468 using ConventionRev = ConventionMarker;
469
Davide Pesavento47a94d12021-10-31 16:41:05 -0400470 ConventionTest<time::system_clock::time_point>
Junxiao Shi71ff2312017-07-12 13:32:50 +0000471 operator()() const
472 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000473 return {&Component::fromTimestamp,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500474 &Component::toTimestamp,
475 [] (Name& name, auto tp) -> Name& { return name.appendTimestamp(tp); },
Junxiao Shi71ff2312017-07-12 13:32:50 +0000476 Name("/%FC%00%04%7BE%E3%1B%00%00"),
Davide Pesavento0f830802018-01-16 23:58:58 -0500477 time::getUnixEpoch() + 14600_days, // 40 years
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500478 &Component::isTimestamp};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000479 }
480};
481
Junxiao Shie2099612019-02-15 14:46:27 +0000482class TimestampTyped
Junxiao Shi71ff2312017-07-12 13:32:50 +0000483{
484public:
Junxiao Shie2099612019-02-15 14:46:27 +0000485 using ConventionRev = ConventionTyped;
486
Davide Pesavento47a94d12021-10-31 16:41:05 -0400487 ConventionTest<time::system_clock::time_point>
Junxiao Shie2099612019-02-15 14:46:27 +0000488 operator()() const
489 {
490 return {&Component::fromTimestamp,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500491 &Component::toTimestamp,
492 [] (Name& name, auto tp) -> Name& { return name.appendTimestamp(tp); },
Davide Pesavento47a94d12021-10-31 16:41:05 -0400493 Name("/56=%00%04%7BE%E3%1B%00%00"),
Junxiao Shie2099612019-02-15 14:46:27 +0000494 time::getUnixEpoch() + 14600_days, // 40 years
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500495 &Component::isTimestamp};
Junxiao Shie2099612019-02-15 14:46:27 +0000496 }
497};
498
499class SequenceNumberMarker
500{
501public:
502 using ConventionRev = ConventionMarker;
503
Junxiao Shi71ff2312017-07-12 13:32:50 +0000504 ConventionTest<uint64_t>
505 operator()() const
506 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000507 return {&Component::fromSequenceNumber,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500508 &Component::toSequenceNumber,
509 &Name::appendSequenceNumber,
Junxiao Shi71ff2312017-07-12 13:32:50 +0000510 Name("/%FE%00%98%96%80"),
511 10000000,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500512 &Component::isSequenceNumber};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000513 }
514};
515
Junxiao Shie2099612019-02-15 14:46:27 +0000516class SequenceNumberTyped
517{
518public:
519 using ConventionRev = ConventionTyped;
520
521 ConventionTest<uint64_t>
522 operator()() const
523 {
524 return {&Component::fromSequenceNumber,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500525 &Component::toSequenceNumber,
526 &Name::appendSequenceNumber,
Davide Pesavento47a94d12021-10-31 16:41:05 -0400527 Name("/58=%00%98%96%80"),
Junxiao Shie2099612019-02-15 14:46:27 +0000528 10000000,
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500529 &Component::isSequenceNumber};
Junxiao Shie2099612019-02-15 14:46:27 +0000530 }
531};
532
Junxiao Shi71ff2312017-07-12 13:32:50 +0000533using ConventionTests = boost::mpl::vector<
534 NumberWithMarker,
Junxiao Shie2099612019-02-15 14:46:27 +0000535 SegmentMarker,
536 SegmentTyped,
Junxiao Shie2099612019-02-15 14:46:27 +0000537 ByteOffsetTyped,
538 VersionMarker,
539 VersionTyped,
540 TimestampMarker,
541 TimestampTyped,
542 SequenceNumberMarker,
543 SequenceNumberTyped
Junxiao Shi71ff2312017-07-12 13:32:50 +0000544>;
545
Junxiao Shie2099612019-02-15 14:46:27 +0000546BOOST_FIXTURE_TEST_CASE_TEMPLATE(Convention, T, ConventionTests, T::ConventionRev)
Junxiao Shi71ff2312017-07-12 13:32:50 +0000547{
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000548 Component invalidComponent1;
549 Component invalidComponent2("1234567890");
Junxiao Shi71ff2312017-07-12 13:32:50 +0000550
551 auto test = T()();
Junxiao Shi71ff2312017-07-12 13:32:50 +0000552 const Name& expected = test.expected;
Junxiao Shi71ff2312017-07-12 13:32:50 +0000553
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000554 Component actualComponent = test.makeComponent(test.value);
Junxiao Shi71ff2312017-07-12 13:32:50 +0000555 BOOST_CHECK_EQUAL(actualComponent, expected[0]);
556
557 Name actualName;
558 test.append(actualName, test.value);
559 BOOST_CHECK_EQUAL(actualName, expected);
560
561 BOOST_CHECK_EQUAL(test.isComponent(expected[0]), true);
562 BOOST_CHECK_EQUAL(test.getValue(expected[0]), test.value);
563
564 BOOST_CHECK_EQUAL(test.isComponent(invalidComponent1), false);
565 BOOST_CHECK_EQUAL(test.isComponent(invalidComponent2), false);
566
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000567 BOOST_CHECK_THROW(test.getValue(invalidComponent1), Component::Error);
568 BOOST_CHECK_THROW(test.getValue(invalidComponent2), Component::Error);
Junxiao Shi71ff2312017-07-12 13:32:50 +0000569}
570
571BOOST_AUTO_TEST_SUITE_END() // NamingConvention
572
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400573BOOST_AUTO_TEST_CASE(Compare)
574{
575 const std::vector<Component> comps = {
576 Component("0120 0000000000000000000000000000000000000000000000000000000000000000"_block),
577 Component("0120 0000000000000000000000000000000000000000000000000000000000000001"_block),
578 Component("0120 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"_block),
579 Component("0220 0000000000000000000000000000000000000000000000000000000000000000"_block),
580 Component("0220 0000000000000000000000000000000000000000000000000000000000000001"_block),
581 Component("0220 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"_block),
582 Component(0x03),
583 Component("0301 44"_block),
584 Component("0301 46"_block),
585 Component("0302 4141"_block),
586 Component(),
587 Component("D"),
588 Component("F"),
589 Component("AA"),
590 Component(0x53B2),
591 Component("FD53B201 44"_block),
592 Component("FD53B201 46"_block),
593 Component("FD53B202 4141"_block),
594 };
595
596 for (size_t i = 0; i < comps.size(); ++i) {
597 for (size_t j = 0; j < comps.size(); ++j) {
598 const auto& lhs = comps[i];
599 const auto& rhs = comps[j];
600 BOOST_CHECK_EQUAL(lhs == rhs, i == j);
601 BOOST_CHECK_EQUAL(lhs != rhs, i != j);
602 BOOST_CHECK_EQUAL(lhs < rhs, i < j);
603 BOOST_CHECK_EQUAL(lhs <= rhs, i <= j);
604 BOOST_CHECK_EQUAL(lhs > rhs, i > j);
605 BOOST_CHECK_EQUAL(lhs >= rhs, i >= j);
606 }
607 }
608}
609
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000610BOOST_AUTO_TEST_SUITE_END() // TestNameComponent
611
612} // namespace tests
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000613} // namespace name
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000614} // namespace ndn