blob: fe62ee077e7bc0a78b217585c6299a3e74a5cdce [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/*
Junxiao Shie2099612019-02-15 14:46:27 +00003 * Copyright (c) 2013-2019 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 Pesavento009062d2019-11-20 00:16:33 -050053 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(comp), "ndn-cxx");
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000054 BOOST_CHECK_EQUAL(Component::fromEscapedString("ndn-cxx"), comp);
Junxiao Shid2e60632018-08-10 10:48:44 -060055 BOOST_CHECK_EQUAL(Component::fromEscapedString("8=ndn-cxx"), comp);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000056
57 comp.wireDecode("0800"_block);
58 BOOST_CHECK_EQUAL(comp.toUri(), "...");
59 BOOST_CHECK_EQUAL(Component::fromEscapedString("..."), comp);
Junxiao Shid2e60632018-08-10 10:48:44 -060060 BOOST_CHECK_EQUAL(Component::fromEscapedString("8=..."), comp);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000061 BOOST_CHECK_EQUAL(Component::fromEscapedString(".%2E."), comp);
62
63 comp.wireDecode("0801 2E"_block);
64 BOOST_CHECK_EQUAL(comp.toUri(), "....");
65 BOOST_CHECK_EQUAL(Component::fromEscapedString("...."), comp);
66 BOOST_CHECK_EQUAL(Component::fromEscapedString("%2E..%2E"), comp);
67
68 comp.wireDecode("0803 2E412E"_block);
69 BOOST_CHECK_EQUAL(comp.toUri(), ".A.");
70 BOOST_CHECK_EQUAL(Component::fromEscapedString(".A."), comp);
71
72 comp.wireDecode("0807 666F6F25626172"_block);
73 BOOST_CHECK_EQUAL(comp.toUri(), "foo%25bar");
74 BOOST_CHECK_EQUAL(Component::fromEscapedString("foo%25bar"), comp);
Junxiao Shid2e60632018-08-10 10:48:44 -060075 BOOST_CHECK_EQUAL(Component::fromEscapedString("8=foo%25bar"), comp);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000076
77 comp.wireDecode("0804 2D2E5F7E"_block);
78 BOOST_CHECK_EQUAL(comp.toUri(), "-._~");
79 BOOST_CHECK_EQUAL(Component::fromEscapedString("-._~"), comp);
80
Junxiao Shid2e60632018-08-10 10:48:44 -060081 comp.wireDecode("0803 393D41"_block);
82 BOOST_CHECK_EQUAL(comp.toUri(), "9%3DA");
83 BOOST_CHECK_EQUAL(Component::fromEscapedString("9%3DA"), comp);
84
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000085 comp = Component(":/?#[]@");
86 BOOST_CHECK_EQUAL(comp.toUri(), "%3A%2F%3F%23%5B%5D%40");
87 BOOST_CHECK_EQUAL(Component::fromEscapedString("%3A%2F%3F%23%5B%5D%40"), comp);
88
89 BOOST_CHECK_THROW(Component::fromEscapedString(""), Component::Error);
90 BOOST_CHECK_THROW(Component::fromEscapedString("."), Component::Error);
91 BOOST_CHECK_THROW(Component::fromEscapedString(".."), Component::Error);
Junxiao Shidf4b24e2016-07-14 21:41:43 +000092}
93
Junxiao Shi4053bd52018-08-16 13:39:25 -060094static void
Davide Pesavento009062d2019-11-20 00:16:33 -050095testSha256Component(uint32_t type, const std::string& uriPrefix)
Junxiao Shidf4b24e2016-07-14 21:41:43 +000096{
Davide Pesavento009062d2019-11-20 00:16:33 -050097 const std::string hexLower = "28bad4b5275bd392dbb670c75cf0b66f13f7942b21e80f55c0e86b374753a548";
98 const std::string hexUpper = boost::to_upper_copy(hexLower);
Junxiao Shi4053bd52018-08-16 13:39:25 -060099 std::string hexPct;
100 for (size_t i = 0; i < hexUpper.size(); i += 2) {
101 hexPct += "%" + hexUpper.substr(i, 2);
102 }
Junxiao Shia39c0b52019-12-31 15:13:15 -0700103 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 +0000104
Junxiao Shi4053bd52018-08-16 13:39:25 -0600105 Component comp(Block(type, fromHex(hexLower)));
Davide Pesavento009062d2019-11-20 00:16:33 -0500106
Junxiao Shi4053bd52018-08-16 13:39:25 -0600107 BOOST_CHECK_EQUAL(comp.type(), type);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000108 BOOST_CHECK_EQUAL(comp.toUri(), uriPrefix + hexLower);
Junxiao Shia39c0b52019-12-31 15:13:15 -0700109 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::CANONICAL), to_string(type) + "=" + hexPctCanonical);
110 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ALTERNATE), uriPrefix + hexLower);
Davide Pesavento009062d2019-11-20 00:16:33 -0500111 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(comp), uriPrefix + hexLower);
112 BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(uriPrefix + hexLower));
113 BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(uriPrefix + hexUpper));
114 BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(to_string(type) + "=" + hexPct));
Junxiao Shia39c0b52019-12-31 15:13:15 -0700115 BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(to_string(type) + "=" + hexPctCanonical));
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000116
Davide Pesavento009062d2019-11-20 00:16:33 -0500117 CHECK_COMP_ERR(comp.wireDecode(Block(type, fromHex("A791806951F25C4D"))), "TLV-LENGTH must be 32");
118 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix), "TLV-LENGTH must be 32");
119 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "a791806951f25c4d"), "TLV-LENGTH must be 32");
120 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "foo"), "invalid hex encoding");
121 CHECK_COMP_ERR(Component::fromEscapedString(boost::to_upper_copy(uriPrefix) + hexLower), "Unknown TLV-TYPE");
Junxiao Shi4053bd52018-08-16 13:39:25 -0600122}
123
Davide Pesavento009062d2019-11-20 00:16:33 -0500124BOOST_AUTO_TEST_CASE(ImplicitDigest)
Junxiao Shi4053bd52018-08-16 13:39:25 -0600125{
Davide Pesavento009062d2019-11-20 00:16:33 -0500126 testSha256Component(tlv::ImplicitSha256DigestComponent, "sha256digest=");
Junxiao Shi4053bd52018-08-16 13:39:25 -0600127}
128
Davide Pesavento009062d2019-11-20 00:16:33 -0500129BOOST_AUTO_TEST_CASE(ParametersDigest)
Junxiao Shi4053bd52018-08-16 13:39:25 -0600130{
Davide Pesavento009062d2019-11-20 00:16:33 -0500131 testSha256Component(tlv::ParametersSha256DigestComponent, "params-sha256=");
132}
133
134static void
135testDecimalComponent(uint32_t type, const std::string& uriPrefix)
136{
137 const Component comp(makeNonNegativeIntegerBlock(type, 42)); // TLV-VALUE is a nonNegativeInteger
138 BOOST_CHECK_EQUAL(comp.type(), type);
139 BOOST_CHECK_EQUAL(comp.isNumber(), true);
140 const auto compUri = uriPrefix + "42";
141 BOOST_CHECK_EQUAL(comp.toUri(), compUri);
Junxiao Shia39c0b52019-12-31 15:13:15 -0700142 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::CANONICAL), to_string(type) + "=%2A");
143 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ALTERNATE), compUri);
Davide Pesavento009062d2019-11-20 00:16:33 -0500144 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(comp), compUri);
145 BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(compUri));
146 BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(to_string(type) + "=%2A"));
147 BOOST_CHECK_EQUAL(comp, Component::fromNumber(42, type));
148
149 const Component comp2(Block(type, fromHex("010203"))); // TLV-VALUE is *not* a nonNegativeInteger
150 BOOST_CHECK_EQUAL(comp2.type(), type);
151 BOOST_CHECK_EQUAL(comp2.isNumber(), false);
152 const auto comp2Uri = to_string(type) + "=%01%02%03";
153 BOOST_CHECK_EQUAL(comp2.toUri(), comp2Uri);
154 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(comp2), comp2Uri);
155 BOOST_CHECK_EQUAL(comp2, Component::fromEscapedString(comp2Uri));
156
157 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix), "invalid format");
158 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "foo"), "invalid format");
159 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "00"), "invalid format");
160 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "-1"), "invalid format");
161 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "9.3"), "invalid format");
162 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + " 84"), "invalid format");
163 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "0xAF"), "invalid format");
164 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "18446744073709551616"), "out of range");
165 CHECK_COMP_ERR(Component::fromEscapedString(boost::to_upper_copy(uriPrefix) + "42"), "Unknown TLV-TYPE");
166}
167
168BOOST_AUTO_TEST_CASE(Segment)
169{
170 testDecimalComponent(tlv::SegmentNameComponent, "seg=");
171}
172
173BOOST_AUTO_TEST_CASE(ByteOffset)
174{
175 testDecimalComponent(tlv::ByteOffsetNameComponent, "off=");
176}
177
178BOOST_AUTO_TEST_CASE(Version)
179{
180 testDecimalComponent(tlv::VersionNameComponent, "v=");
181}
182
183BOOST_AUTO_TEST_CASE(Timestamp)
184{
185 testDecimalComponent(tlv::TimestampNameComponent, "t=");
186}
187
188BOOST_AUTO_TEST_CASE(SequenceNum)
189{
190 testDecimalComponent(tlv::SequenceNumNameComponent, "seq=");
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000191}
192
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000193BOOST_AUTO_TEST_CASE(OtherType)
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000194{
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000195 Component comp("0907 6E646E2D637878"_block);
196 BOOST_CHECK_EQUAL(comp.type(), 0x09);
197 BOOST_CHECK_EQUAL(comp.toUri(), "9=ndn-cxx");
198 BOOST_CHECK_EQUAL(Component::fromEscapedString("9=ndn-cxx"), comp);
199
200 comp.wireDecode("FDFFFF00"_block);
201 BOOST_CHECK_EQUAL(comp.type(), 0xFFFF);
202 BOOST_CHECK_EQUAL(comp.toUri(), "65535=...");
203 BOOST_CHECK_EQUAL(Component::fromEscapedString("65535=..."), comp);
204
205 comp.wireDecode("FD576501 2E"_block);
206 BOOST_CHECK_EQUAL(comp.type(), 0x5765);
207 BOOST_CHECK_EQUAL(comp.toUri(), "22373=....");
208 BOOST_CHECK_EQUAL(Component::fromEscapedString("22373=...."), comp);
209
210 BOOST_CHECK_THROW(Component::fromEscapedString("3="), Component::Error);
211 BOOST_CHECK_THROW(Component::fromEscapedString("3=."), Component::Error);
212 BOOST_CHECK_THROW(Component::fromEscapedString("3=.."), Component::Error);
213}
214
215BOOST_AUTO_TEST_CASE(InvalidType)
216{
217 Component comp;
Davide Pesavento6b330402019-04-24 00:14:01 -0400218 BOOST_CHECK_THROW(comp.wireDecode(Block{}), Component::Error);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000219 BOOST_CHECK_THROW(comp.wireDecode("FE0001000001 80"_block), Component::Error);
220
221 BOOST_CHECK_THROW(Component::fromEscapedString("0=A"), Component::Error);
222 BOOST_CHECK_THROW(Component::fromEscapedString("65536=A"), Component::Error);
Junxiao Shid2e60632018-08-10 10:48:44 -0600223 BOOST_CHECK_THROW(Component::fromEscapedString("4294967296=A"), Component::Error);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000224 BOOST_CHECK_THROW(Component::fromEscapedString("-1=A"), Component::Error);
225 BOOST_CHECK_THROW(Component::fromEscapedString("+=A"), Component::Error);
226 BOOST_CHECK_THROW(Component::fromEscapedString("=A"), Component::Error);
227 BOOST_CHECK_THROW(Component::fromEscapedString("0x1=A"), Component::Error);
228 BOOST_CHECK_THROW(Component::fromEscapedString("Z=A"), Component::Error);
229 BOOST_CHECK_THROW(Component::fromEscapedString("09=A"), Component::Error);
Junxiao Shi4053bd52018-08-16 13:39:25 -0600230 BOOST_CHECK_THROW(Component::fromEscapedString("0x3=A"), Component::Error);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000231 BOOST_CHECK_THROW(Component::fromEscapedString("+9=A"), Component::Error);
Junxiao Shid2e60632018-08-10 10:48:44 -0600232 BOOST_CHECK_THROW(Component::fromEscapedString(" 9=A"), Component::Error);
233 BOOST_CHECK_THROW(Component::fromEscapedString("9 =A"), Component::Error);
234 BOOST_CHECK_THROW(Component::fromEscapedString("9.0=A"), Component::Error);
235 BOOST_CHECK_THROW(Component::fromEscapedString("9E0=A"), Component::Error);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000236}
237
238BOOST_AUTO_TEST_SUITE_END() // Decode
239
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000240BOOST_AUTO_TEST_CASE(Compare)
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000241{
Davide Pesavento009062d2019-11-20 00:16:33 -0500242 const std::vector<Component> comps = {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000243 Component("0120 0000000000000000000000000000000000000000000000000000000000000000"_block),
244 Component("0120 0000000000000000000000000000000000000000000000000000000000000001"_block),
245 Component("0120 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"_block),
Junxiao Shi4053bd52018-08-16 13:39:25 -0600246 Component("0220 0000000000000000000000000000000000000000000000000000000000000000"_block),
247 Component("0220 0000000000000000000000000000000000000000000000000000000000000001"_block),
248 Component("0220 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"_block),
249 Component(0x03),
250 Component("0301 44"_block),
251 Component("0301 46"_block),
252 Component("0302 4141"_block),
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000253 Component(),
254 Component("D"),
255 Component("F"),
256 Component("AA"),
257 Component(0x53B2),
258 Component("FD53B201 44"_block),
259 Component("FD53B201 46"_block),
260 Component("FD53B202 4141"_block),
261 };
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000262
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000263 for (size_t i = 0; i < comps.size(); ++i) {
264 for (size_t j = 0; j < comps.size(); ++j) {
265 Component lhs = comps[i];
266 Component rhs = comps[j];
267 BOOST_CHECK_EQUAL(lhs == rhs, i == j);
268 BOOST_CHECK_EQUAL(lhs != rhs, i != j);
269 BOOST_CHECK_EQUAL(lhs < rhs, i < j);
270 BOOST_CHECK_EQUAL(lhs <= rhs, i <= j);
271 BOOST_CHECK_EQUAL(lhs > rhs, i > j);
272 BOOST_CHECK_EQUAL(lhs >= rhs, i >= j);
273 }
274 }
Davide Pesavento08378cb2018-02-01 16:10:54 -0500275}
276
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000277BOOST_AUTO_TEST_SUITE(CreateFromIterators) // Bug 2490
278
Davide Pesavento009062d2019-11-20 00:16:33 -0500279using ContainerTypes = boost::mpl::vector<std::vector<uint8_t>,
280 std::list<uint8_t>,
281 std::vector<int8_t>,
282 std::list<int8_t>>;
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000283
284BOOST_AUTO_TEST_CASE_TEMPLATE(ZeroOctet, T, ContainerTypes)
285{
286 T bytes;
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000287 Component c(bytes.begin(), bytes.end());
288 BOOST_CHECK_EQUAL(c.type(), tlv::GenericNameComponent);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000289 BOOST_CHECK_EQUAL(c.value_size(), 0);
290 BOOST_CHECK_EQUAL(c.size(), 2);
291}
292
293BOOST_AUTO_TEST_CASE_TEMPLATE(OneOctet, T, ContainerTypes)
294{
295 T bytes{1};
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000296 Component c(0x09, bytes.begin(), bytes.end());
297 BOOST_CHECK_EQUAL(c.type(), 0x09);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000298 BOOST_CHECK_EQUAL(c.value_size(), 1);
299 BOOST_CHECK_EQUAL(c.size(), 3);
300}
301
302BOOST_AUTO_TEST_CASE_TEMPLATE(FourOctets, T, ContainerTypes)
303{
304 T bytes{1, 2, 3, 4};
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000305 Component c(0xFCEC, bytes.begin(), bytes.end());
306 BOOST_CHECK_EQUAL(c.type(), 0xFCEC);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000307 BOOST_CHECK_EQUAL(c.value_size(), 4);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000308 BOOST_CHECK_EQUAL(c.size(), 8);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000309}
310
311BOOST_AUTO_TEST_SUITE_END() // CreateFromIterators
312
Junxiao Shi71ff2312017-07-12 13:32:50 +0000313BOOST_AUTO_TEST_SUITE(NamingConvention)
314
315template<typename ArgType>
316struct ConventionTest
317{
Davide Pesavento009062d2019-11-20 00:16:33 -0500318 std::function<Component(ArgType)> makeComponent;
319 std::function<ArgType(const Component&)> getValue;
320 std::function<Name&(Name&, ArgType)> append;
Junxiao Shi71ff2312017-07-12 13:32:50 +0000321 Name expected;
322 ArgType value;
Davide Pesavento009062d2019-11-20 00:16:33 -0500323 std::function<bool(const Component&)> isComponent;
Junxiao Shi71ff2312017-07-12 13:32:50 +0000324};
325
Junxiao Shie2099612019-02-15 14:46:27 +0000326class ConventionMarker
327{
328};
329
330class ConventionTyped
331{
332public:
333 ConventionTyped()
334 {
335 name::setConventionEncoding(name::Convention::TYPED);
336 }
337
338 ~ConventionTyped()
339 {
340 name::setConventionEncoding(name::Convention::MARKER);
341 }
342};
343
Junxiao Shi71ff2312017-07-12 13:32:50 +0000344class NumberWithMarker
345{
346public:
Junxiao Shie2099612019-02-15 14:46:27 +0000347 using ConventionRev = ConventionMarker;
348
Junxiao Shi71ff2312017-07-12 13:32:50 +0000349 ConventionTest<uint64_t>
350 operator()() const
351 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000352 return {bind(&Component::fromNumberWithMarker, 0xAA, _1),
353 bind(&Component::toNumberWithMarker, _1, 0xAA),
Junxiao Shi71ff2312017-07-12 13:32:50 +0000354 bind(&Name::appendNumberWithMarker, _1, 0xAA, _2),
355 Name("/%AA%03%E8"),
356 1000,
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000357 bind(&Component::isNumberWithMarker, _1, 0xAA)};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000358 }
359};
360
Junxiao Shie2099612019-02-15 14:46:27 +0000361class SegmentMarker
Junxiao Shi71ff2312017-07-12 13:32:50 +0000362{
363public:
Junxiao Shie2099612019-02-15 14:46:27 +0000364 using ConventionRev = ConventionMarker;
365
Junxiao Shi71ff2312017-07-12 13:32:50 +0000366 ConventionTest<uint64_t>
367 operator()() const
368 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000369 return {&Component::fromSegment,
370 bind(&Component::toSegment, _1),
Junxiao Shi71ff2312017-07-12 13:32:50 +0000371 bind(&Name::appendSegment, _1, _2),
372 Name("/%00%27%10"),
373 10000,
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000374 bind(&Component::isSegment, _1)};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000375 }
376};
377
Junxiao Shie2099612019-02-15 14:46:27 +0000378class SegmentTyped
Junxiao Shi71ff2312017-07-12 13:32:50 +0000379{
380public:
Junxiao Shie2099612019-02-15 14:46:27 +0000381 using ConventionRev = ConventionTyped;
382
383 ConventionTest<uint64_t>
384 operator()() const
385 {
386 return {&Component::fromSegment,
387 bind(&Component::toSegment, _1),
388 bind(&Name::appendSegment, _1, _2),
389 Name("/33=%27%10"),
390 10000,
391 bind(&Component::isSegment, _1)};
392 }
393};
394
395class SegmentOffsetMarker
396{
397public:
398 using ConventionRev = ConventionMarker;
399
Junxiao Shi71ff2312017-07-12 13:32:50 +0000400 ConventionTest<uint64_t>
401 operator()() const
402 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000403 return {&Component::fromSegmentOffset,
404 bind(&Component::toSegmentOffset, _1),
Junxiao Shi71ff2312017-07-12 13:32:50 +0000405 bind(&Name::appendSegmentOffset, _1, _2),
406 Name("/%FB%00%01%86%A0"),
407 100000,
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000408 bind(&Component::isSegmentOffset, _1)};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000409 }
410};
411
Junxiao Shie2099612019-02-15 14:46:27 +0000412class ByteOffsetTyped
Junxiao Shi71ff2312017-07-12 13:32:50 +0000413{
414public:
Junxiao Shie2099612019-02-15 14:46:27 +0000415 using ConventionRev = ConventionTyped;
416
417 ConventionTest<uint64_t>
418 operator()() const
419 {
420 return {&Component::fromByteOffset,
421 bind(&Component::toByteOffset, _1),
422 bind(&Name::appendByteOffset, _1, _2),
423 Name("/34=%00%01%86%A0"),
424 100000,
425 bind(&Component::isByteOffset, _1)};
426 }
427};
428
429class VersionMarker
430{
431public:
432 using ConventionRev = ConventionMarker;
433
Junxiao Shi71ff2312017-07-12 13:32:50 +0000434 ConventionTest<uint64_t>
435 operator()() const
436 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000437 return {&Component::fromVersion,
438 bind(&Component::toVersion, _1),
Junxiao Shi71ff2312017-07-12 13:32:50 +0000439 [] (Name& name, uint64_t version) -> Name& { return name.appendVersion(version); },
440 Name("/%FD%00%0FB%40"),
441 1000000,
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000442 bind(&Component::isVersion, _1)};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000443 }
444};
445
Junxiao Shie2099612019-02-15 14:46:27 +0000446class VersionTyped
Junxiao Shi71ff2312017-07-12 13:32:50 +0000447{
448public:
Junxiao Shie2099612019-02-15 14:46:27 +0000449 using ConventionRev = ConventionTyped;
450
451 ConventionTest<uint64_t>
452 operator()() const
453 {
454 return {&Component::fromVersion,
455 bind(&Component::toVersion, _1),
456 [] (Name& name, uint64_t version) -> Name& { return name.appendVersion(version); },
457 Name("/35=%00%0FB%40"),
458 1000000,
459 bind(&Component::isVersion, _1)};
460 }
461};
462
463class TimestampMarker
464{
465public:
466 using ConventionRev = ConventionMarker;
467
Junxiao Shi71ff2312017-07-12 13:32:50 +0000468 ConventionTest<time::system_clock::TimePoint>
469 operator()() const
470 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000471 return {&Component::fromTimestamp,
472 bind(&Component::toTimestamp, _1),
Junxiao Shi71ff2312017-07-12 13:32:50 +0000473 [] (Name& name, time::system_clock::TimePoint t) -> Name& { return name.appendTimestamp(t); },
474 Name("/%FC%00%04%7BE%E3%1B%00%00"),
Davide Pesavento0f830802018-01-16 23:58:58 -0500475 time::getUnixEpoch() + 14600_days, // 40 years
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000476 bind(&Component::isTimestamp, _1)};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000477 }
478};
479
Junxiao Shie2099612019-02-15 14:46:27 +0000480class TimestampTyped
Junxiao Shi71ff2312017-07-12 13:32:50 +0000481{
482public:
Junxiao Shie2099612019-02-15 14:46:27 +0000483 using ConventionRev = ConventionTyped;
484
485 ConventionTest<time::system_clock::TimePoint>
486 operator()() const
487 {
488 return {&Component::fromTimestamp,
489 bind(&Component::toTimestamp, _1),
490 [] (Name& name, time::system_clock::TimePoint t) -> Name& { return name.appendTimestamp(t); },
491 Name("/36=%00%04%7BE%E3%1B%00%00"),
492 time::getUnixEpoch() + 14600_days, // 40 years
493 bind(&Component::isTimestamp, _1)};
494 }
495};
496
497class SequenceNumberMarker
498{
499public:
500 using ConventionRev = ConventionMarker;
501
Junxiao Shi71ff2312017-07-12 13:32:50 +0000502 ConventionTest<uint64_t>
503 operator()() const
504 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000505 return {&Component::fromSequenceNumber,
506 bind(&Component::toSequenceNumber, _1),
Junxiao Shi71ff2312017-07-12 13:32:50 +0000507 bind(&Name::appendSequenceNumber, _1, _2),
508 Name("/%FE%00%98%96%80"),
509 10000000,
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000510 bind(&Component::isSequenceNumber, _1)};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000511 }
512};
513
Junxiao Shie2099612019-02-15 14:46:27 +0000514class SequenceNumberTyped
515{
516public:
517 using ConventionRev = ConventionTyped;
518
519 ConventionTest<uint64_t>
520 operator()() const
521 {
522 return {&Component::fromSequenceNumber,
523 bind(&Component::toSequenceNumber, _1),
524 bind(&Name::appendSequenceNumber, _1, _2),
525 Name("/37=%00%98%96%80"),
526 10000000,
527 bind(&Component::isSequenceNumber, _1)};
528 }
529};
530
Junxiao Shi71ff2312017-07-12 13:32:50 +0000531using ConventionTests = boost::mpl::vector<
532 NumberWithMarker,
Junxiao Shie2099612019-02-15 14:46:27 +0000533 SegmentMarker,
534 SegmentTyped,
535 SegmentOffsetMarker,
536 ByteOffsetTyped,
537 VersionMarker,
538 VersionTyped,
539 TimestampMarker,
540 TimestampTyped,
541 SequenceNumberMarker,
542 SequenceNumberTyped
Junxiao Shi71ff2312017-07-12 13:32:50 +0000543>;
544
Junxiao Shie2099612019-02-15 14:46:27 +0000545BOOST_FIXTURE_TEST_CASE_TEMPLATE(Convention, T, ConventionTests, T::ConventionRev)
Junxiao Shi71ff2312017-07-12 13:32:50 +0000546{
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000547 Component invalidComponent1;
548 Component invalidComponent2("1234567890");
Junxiao Shi71ff2312017-07-12 13:32:50 +0000549
550 auto test = T()();
551
552 const Name& expected = test.expected;
Davide Pesavento009062d2019-11-20 00:16:33 -0500553 BOOST_TEST_MESSAGE("Check " << expected[0]);
Junxiao Shi71ff2312017-07-12 13:32:50 +0000554
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000555 Component actualComponent = test.makeComponent(test.value);
Junxiao Shi71ff2312017-07-12 13:32:50 +0000556 BOOST_CHECK_EQUAL(actualComponent, expected[0]);
557
558 Name actualName;
559 test.append(actualName, test.value);
560 BOOST_CHECK_EQUAL(actualName, expected);
561
562 BOOST_CHECK_EQUAL(test.isComponent(expected[0]), true);
563 BOOST_CHECK_EQUAL(test.getValue(expected[0]), test.value);
564
565 BOOST_CHECK_EQUAL(test.isComponent(invalidComponent1), false);
566 BOOST_CHECK_EQUAL(test.isComponent(invalidComponent2), false);
567
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000568 BOOST_CHECK_THROW(test.getValue(invalidComponent1), Component::Error);
569 BOOST_CHECK_THROW(test.getValue(invalidComponent2), Component::Error);
Junxiao Shi71ff2312017-07-12 13:32:50 +0000570}
571
572BOOST_AUTO_TEST_SUITE_END() // NamingConvention
573
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000574BOOST_AUTO_TEST_SUITE_END() // TestNameComponent
575
576} // namespace tests
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000577} // namespace name
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000578} // namespace ndn