blob: c009175b55ddadbaf221a6faaca2e6cfacc5fe96 [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");
Davide Pesavento009062d2019-11-20 00:16:33 -050051 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(comp), "ndn-cxx");
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000052 BOOST_CHECK_EQUAL(Component::fromEscapedString("ndn-cxx"), comp);
Junxiao Shid2e60632018-08-10 10:48:44 -060053 BOOST_CHECK_EQUAL(Component::fromEscapedString("8=ndn-cxx"), comp);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000054
55 comp.wireDecode("0800"_block);
56 BOOST_CHECK_EQUAL(comp.toUri(), "...");
57 BOOST_CHECK_EQUAL(Component::fromEscapedString("..."), comp);
Junxiao Shid2e60632018-08-10 10:48:44 -060058 BOOST_CHECK_EQUAL(Component::fromEscapedString("8=..."), comp);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000059 BOOST_CHECK_EQUAL(Component::fromEscapedString(".%2E."), comp);
60
61 comp.wireDecode("0801 2E"_block);
62 BOOST_CHECK_EQUAL(comp.toUri(), "....");
63 BOOST_CHECK_EQUAL(Component::fromEscapedString("...."), comp);
64 BOOST_CHECK_EQUAL(Component::fromEscapedString("%2E..%2E"), comp);
65
66 comp.wireDecode("0803 2E412E"_block);
67 BOOST_CHECK_EQUAL(comp.toUri(), ".A.");
68 BOOST_CHECK_EQUAL(Component::fromEscapedString(".A."), comp);
69
70 comp.wireDecode("0807 666F6F25626172"_block);
71 BOOST_CHECK_EQUAL(comp.toUri(), "foo%25bar");
72 BOOST_CHECK_EQUAL(Component::fromEscapedString("foo%25bar"), comp);
Junxiao Shid2e60632018-08-10 10:48:44 -060073 BOOST_CHECK_EQUAL(Component::fromEscapedString("8=foo%25bar"), comp);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000074
75 comp.wireDecode("0804 2D2E5F7E"_block);
76 BOOST_CHECK_EQUAL(comp.toUri(), "-._~");
77 BOOST_CHECK_EQUAL(Component::fromEscapedString("-._~"), comp);
78
Junxiao Shid2e60632018-08-10 10:48:44 -060079 comp.wireDecode("0803 393D41"_block);
80 BOOST_CHECK_EQUAL(comp.toUri(), "9%3DA");
81 BOOST_CHECK_EQUAL(Component::fromEscapedString("9%3DA"), comp);
82
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000083 comp = Component(":/?#[]@");
84 BOOST_CHECK_EQUAL(comp.toUri(), "%3A%2F%3F%23%5B%5D%40");
85 BOOST_CHECK_EQUAL(Component::fromEscapedString("%3A%2F%3F%23%5B%5D%40"), comp);
86
87 BOOST_CHECK_THROW(Component::fromEscapedString(""), Component::Error);
88 BOOST_CHECK_THROW(Component::fromEscapedString("."), Component::Error);
89 BOOST_CHECK_THROW(Component::fromEscapedString(".."), Component::Error);
Junxiao Shidf4b24e2016-07-14 21:41:43 +000090}
91
Junxiao Shi4053bd52018-08-16 13:39:25 -060092static void
Davide Pesavento009062d2019-11-20 00:16:33 -050093testSha256Component(uint32_t type, const std::string& uriPrefix)
Junxiao Shidf4b24e2016-07-14 21:41:43 +000094{
Davide Pesavento009062d2019-11-20 00:16:33 -050095 const std::string hexLower = "28bad4b5275bd392dbb670c75cf0b66f13f7942b21e80f55c0e86b374753a548";
96 const std::string hexUpper = boost::to_upper_copy(hexLower);
Junxiao Shi4053bd52018-08-16 13:39:25 -060097 std::string hexPct;
98 for (size_t i = 0; i < hexUpper.size(); i += 2) {
99 hexPct += "%" + hexUpper.substr(i, 2);
100 }
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000101
Junxiao Shi4053bd52018-08-16 13:39:25 -0600102 Component comp(Block(type, fromHex(hexLower)));
Davide Pesavento009062d2019-11-20 00:16:33 -0500103
Junxiao Shi4053bd52018-08-16 13:39:25 -0600104 BOOST_CHECK_EQUAL(comp.type(), type);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000105 BOOST_CHECK_EQUAL(comp.toUri(), uriPrefix + hexLower);
Davide Pesavento009062d2019-11-20 00:16:33 -0500106 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(comp), uriPrefix + hexLower);
107 BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(uriPrefix + hexLower));
108 BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(uriPrefix + hexUpper));
109 BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(to_string(type) + "=" + hexPct));
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000110
Davide Pesavento009062d2019-11-20 00:16:33 -0500111 CHECK_COMP_ERR(comp.wireDecode(Block(type, fromHex("A791806951F25C4D"))), "TLV-LENGTH must be 32");
112 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix), "TLV-LENGTH must be 32");
113 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "a791806951f25c4d"), "TLV-LENGTH must be 32");
114 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "foo"), "invalid hex encoding");
115 CHECK_COMP_ERR(Component::fromEscapedString(boost::to_upper_copy(uriPrefix) + hexLower), "Unknown TLV-TYPE");
Junxiao Shi4053bd52018-08-16 13:39:25 -0600116}
117
Davide Pesavento009062d2019-11-20 00:16:33 -0500118BOOST_AUTO_TEST_CASE(ImplicitDigest)
Junxiao Shi4053bd52018-08-16 13:39:25 -0600119{
Davide Pesavento009062d2019-11-20 00:16:33 -0500120 testSha256Component(tlv::ImplicitSha256DigestComponent, "sha256digest=");
Junxiao Shi4053bd52018-08-16 13:39:25 -0600121}
122
Davide Pesavento009062d2019-11-20 00:16:33 -0500123BOOST_AUTO_TEST_CASE(ParametersDigest)
Junxiao Shi4053bd52018-08-16 13:39:25 -0600124{
Davide Pesavento009062d2019-11-20 00:16:33 -0500125 testSha256Component(tlv::ParametersSha256DigestComponent, "params-sha256=");
126}
127
128static void
129testDecimalComponent(uint32_t type, const std::string& uriPrefix)
130{
131 const Component comp(makeNonNegativeIntegerBlock(type, 42)); // TLV-VALUE is a nonNegativeInteger
132 BOOST_CHECK_EQUAL(comp.type(), type);
133 BOOST_CHECK_EQUAL(comp.isNumber(), true);
134 const auto compUri = uriPrefix + "42";
135 BOOST_CHECK_EQUAL(comp.toUri(), compUri);
136 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(comp), compUri);
137 BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(compUri));
138 BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(to_string(type) + "=%2A"));
139 BOOST_CHECK_EQUAL(comp, Component::fromNumber(42, type));
140
141 const Component comp2(Block(type, fromHex("010203"))); // TLV-VALUE is *not* a nonNegativeInteger
142 BOOST_CHECK_EQUAL(comp2.type(), type);
143 BOOST_CHECK_EQUAL(comp2.isNumber(), false);
144 const auto comp2Uri = to_string(type) + "=%01%02%03";
145 BOOST_CHECK_EQUAL(comp2.toUri(), comp2Uri);
146 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(comp2), comp2Uri);
147 BOOST_CHECK_EQUAL(comp2, Component::fromEscapedString(comp2Uri));
148
149 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix), "invalid format");
150 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "foo"), "invalid format");
151 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "00"), "invalid format");
152 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "-1"), "invalid format");
153 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "9.3"), "invalid format");
154 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + " 84"), "invalid format");
155 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "0xAF"), "invalid format");
156 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "18446744073709551616"), "out of range");
157 CHECK_COMP_ERR(Component::fromEscapedString(boost::to_upper_copy(uriPrefix) + "42"), "Unknown TLV-TYPE");
158}
159
160BOOST_AUTO_TEST_CASE(Segment)
161{
162 testDecimalComponent(tlv::SegmentNameComponent, "seg=");
163}
164
165BOOST_AUTO_TEST_CASE(ByteOffset)
166{
167 testDecimalComponent(tlv::ByteOffsetNameComponent, "off=");
168}
169
170BOOST_AUTO_TEST_CASE(Version)
171{
172 testDecimalComponent(tlv::VersionNameComponent, "v=");
173}
174
175BOOST_AUTO_TEST_CASE(Timestamp)
176{
177 testDecimalComponent(tlv::TimestampNameComponent, "t=");
178}
179
180BOOST_AUTO_TEST_CASE(SequenceNum)
181{
182 testDecimalComponent(tlv::SequenceNumNameComponent, "seq=");
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000183}
184
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000185BOOST_AUTO_TEST_CASE(OtherType)
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000186{
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000187 Component comp("0907 6E646E2D637878"_block);
188 BOOST_CHECK_EQUAL(comp.type(), 0x09);
189 BOOST_CHECK_EQUAL(comp.toUri(), "9=ndn-cxx");
190 BOOST_CHECK_EQUAL(Component::fromEscapedString("9=ndn-cxx"), comp);
191
192 comp.wireDecode("FDFFFF00"_block);
193 BOOST_CHECK_EQUAL(comp.type(), 0xFFFF);
194 BOOST_CHECK_EQUAL(comp.toUri(), "65535=...");
195 BOOST_CHECK_EQUAL(Component::fromEscapedString("65535=..."), comp);
196
197 comp.wireDecode("FD576501 2E"_block);
198 BOOST_CHECK_EQUAL(comp.type(), 0x5765);
199 BOOST_CHECK_EQUAL(comp.toUri(), "22373=....");
200 BOOST_CHECK_EQUAL(Component::fromEscapedString("22373=...."), comp);
201
202 BOOST_CHECK_THROW(Component::fromEscapedString("3="), Component::Error);
203 BOOST_CHECK_THROW(Component::fromEscapedString("3=."), Component::Error);
204 BOOST_CHECK_THROW(Component::fromEscapedString("3=.."), Component::Error);
205}
206
207BOOST_AUTO_TEST_CASE(InvalidType)
208{
209 Component comp;
Davide Pesavento6b330402019-04-24 00:14:01 -0400210 BOOST_CHECK_THROW(comp.wireDecode(Block{}), Component::Error);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000211 BOOST_CHECK_THROW(comp.wireDecode("FE0001000001 80"_block), Component::Error);
212
213 BOOST_CHECK_THROW(Component::fromEscapedString("0=A"), Component::Error);
214 BOOST_CHECK_THROW(Component::fromEscapedString("65536=A"), Component::Error);
Junxiao Shid2e60632018-08-10 10:48:44 -0600215 BOOST_CHECK_THROW(Component::fromEscapedString("4294967296=A"), Component::Error);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000216 BOOST_CHECK_THROW(Component::fromEscapedString("-1=A"), Component::Error);
217 BOOST_CHECK_THROW(Component::fromEscapedString("+=A"), Component::Error);
218 BOOST_CHECK_THROW(Component::fromEscapedString("=A"), Component::Error);
219 BOOST_CHECK_THROW(Component::fromEscapedString("0x1=A"), Component::Error);
220 BOOST_CHECK_THROW(Component::fromEscapedString("Z=A"), Component::Error);
221 BOOST_CHECK_THROW(Component::fromEscapedString("09=A"), Component::Error);
Junxiao Shi4053bd52018-08-16 13:39:25 -0600222 BOOST_CHECK_THROW(Component::fromEscapedString("0x3=A"), Component::Error);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000223 BOOST_CHECK_THROW(Component::fromEscapedString("+9=A"), Component::Error);
Junxiao Shid2e60632018-08-10 10:48:44 -0600224 BOOST_CHECK_THROW(Component::fromEscapedString(" 9=A"), Component::Error);
225 BOOST_CHECK_THROW(Component::fromEscapedString("9 =A"), Component::Error);
226 BOOST_CHECK_THROW(Component::fromEscapedString("9.0=A"), Component::Error);
227 BOOST_CHECK_THROW(Component::fromEscapedString("9E0=A"), Component::Error);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000228}
229
230BOOST_AUTO_TEST_SUITE_END() // Decode
231
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000232BOOST_AUTO_TEST_CASE(Compare)
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000233{
Davide Pesavento009062d2019-11-20 00:16:33 -0500234 const std::vector<Component> comps = {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000235 Component("0120 0000000000000000000000000000000000000000000000000000000000000000"_block),
236 Component("0120 0000000000000000000000000000000000000000000000000000000000000001"_block),
237 Component("0120 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"_block),
Junxiao Shi4053bd52018-08-16 13:39:25 -0600238 Component("0220 0000000000000000000000000000000000000000000000000000000000000000"_block),
239 Component("0220 0000000000000000000000000000000000000000000000000000000000000001"_block),
240 Component("0220 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"_block),
241 Component(0x03),
242 Component("0301 44"_block),
243 Component("0301 46"_block),
244 Component("0302 4141"_block),
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000245 Component(),
246 Component("D"),
247 Component("F"),
248 Component("AA"),
249 Component(0x53B2),
250 Component("FD53B201 44"_block),
251 Component("FD53B201 46"_block),
252 Component("FD53B202 4141"_block),
253 };
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000254
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000255 for (size_t i = 0; i < comps.size(); ++i) {
256 for (size_t j = 0; j < comps.size(); ++j) {
257 Component lhs = comps[i];
258 Component rhs = comps[j];
259 BOOST_CHECK_EQUAL(lhs == rhs, i == j);
260 BOOST_CHECK_EQUAL(lhs != rhs, i != j);
261 BOOST_CHECK_EQUAL(lhs < rhs, i < j);
262 BOOST_CHECK_EQUAL(lhs <= rhs, i <= j);
263 BOOST_CHECK_EQUAL(lhs > rhs, i > j);
264 BOOST_CHECK_EQUAL(lhs >= rhs, i >= j);
265 }
266 }
Davide Pesavento08378cb2018-02-01 16:10:54 -0500267}
268
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000269BOOST_AUTO_TEST_SUITE(CreateFromIterators) // Bug 2490
270
Davide Pesavento009062d2019-11-20 00:16:33 -0500271using ContainerTypes = boost::mpl::vector<std::vector<uint8_t>,
272 std::list<uint8_t>,
273 std::vector<int8_t>,
274 std::list<int8_t>>;
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000275
276BOOST_AUTO_TEST_CASE_TEMPLATE(ZeroOctet, T, ContainerTypes)
277{
278 T bytes;
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000279 Component c(bytes.begin(), bytes.end());
280 BOOST_CHECK_EQUAL(c.type(), tlv::GenericNameComponent);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000281 BOOST_CHECK_EQUAL(c.value_size(), 0);
282 BOOST_CHECK_EQUAL(c.size(), 2);
283}
284
285BOOST_AUTO_TEST_CASE_TEMPLATE(OneOctet, T, ContainerTypes)
286{
287 T bytes{1};
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000288 Component c(0x09, bytes.begin(), bytes.end());
289 BOOST_CHECK_EQUAL(c.type(), 0x09);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000290 BOOST_CHECK_EQUAL(c.value_size(), 1);
291 BOOST_CHECK_EQUAL(c.size(), 3);
292}
293
294BOOST_AUTO_TEST_CASE_TEMPLATE(FourOctets, T, ContainerTypes)
295{
296 T bytes{1, 2, 3, 4};
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000297 Component c(0xFCEC, bytes.begin(), bytes.end());
298 BOOST_CHECK_EQUAL(c.type(), 0xFCEC);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000299 BOOST_CHECK_EQUAL(c.value_size(), 4);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000300 BOOST_CHECK_EQUAL(c.size(), 8);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000301}
302
303BOOST_AUTO_TEST_SUITE_END() // CreateFromIterators
304
Junxiao Shi71ff2312017-07-12 13:32:50 +0000305BOOST_AUTO_TEST_SUITE(NamingConvention)
306
307template<typename ArgType>
308struct ConventionTest
309{
Davide Pesavento009062d2019-11-20 00:16:33 -0500310 std::function<Component(ArgType)> makeComponent;
311 std::function<ArgType(const Component&)> getValue;
312 std::function<Name&(Name&, ArgType)> append;
Junxiao Shi71ff2312017-07-12 13:32:50 +0000313 Name expected;
314 ArgType value;
Davide Pesavento009062d2019-11-20 00:16:33 -0500315 std::function<bool(const Component&)> isComponent;
Junxiao Shi71ff2312017-07-12 13:32:50 +0000316};
317
Junxiao Shie2099612019-02-15 14:46:27 +0000318class ConventionMarker
319{
320};
321
322class ConventionTyped
323{
324public:
325 ConventionTyped()
326 {
327 name::setConventionEncoding(name::Convention::TYPED);
328 }
329
330 ~ConventionTyped()
331 {
332 name::setConventionEncoding(name::Convention::MARKER);
333 }
334};
335
Junxiao Shi71ff2312017-07-12 13:32:50 +0000336class NumberWithMarker
337{
338public:
Junxiao Shie2099612019-02-15 14:46:27 +0000339 using ConventionRev = ConventionMarker;
340
Junxiao Shi71ff2312017-07-12 13:32:50 +0000341 ConventionTest<uint64_t>
342 operator()() const
343 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000344 return {bind(&Component::fromNumberWithMarker, 0xAA, _1),
345 bind(&Component::toNumberWithMarker, _1, 0xAA),
Junxiao Shi71ff2312017-07-12 13:32:50 +0000346 bind(&Name::appendNumberWithMarker, _1, 0xAA, _2),
347 Name("/%AA%03%E8"),
348 1000,
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000349 bind(&Component::isNumberWithMarker, _1, 0xAA)};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000350 }
351};
352
Junxiao Shie2099612019-02-15 14:46:27 +0000353class SegmentMarker
Junxiao Shi71ff2312017-07-12 13:32:50 +0000354{
355public:
Junxiao Shie2099612019-02-15 14:46:27 +0000356 using ConventionRev = ConventionMarker;
357
Junxiao Shi71ff2312017-07-12 13:32:50 +0000358 ConventionTest<uint64_t>
359 operator()() const
360 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000361 return {&Component::fromSegment,
362 bind(&Component::toSegment, _1),
Junxiao Shi71ff2312017-07-12 13:32:50 +0000363 bind(&Name::appendSegment, _1, _2),
364 Name("/%00%27%10"),
365 10000,
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000366 bind(&Component::isSegment, _1)};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000367 }
368};
369
Junxiao Shie2099612019-02-15 14:46:27 +0000370class SegmentTyped
Junxiao Shi71ff2312017-07-12 13:32:50 +0000371{
372public:
Junxiao Shie2099612019-02-15 14:46:27 +0000373 using ConventionRev = ConventionTyped;
374
375 ConventionTest<uint64_t>
376 operator()() const
377 {
378 return {&Component::fromSegment,
379 bind(&Component::toSegment, _1),
380 bind(&Name::appendSegment, _1, _2),
381 Name("/33=%27%10"),
382 10000,
383 bind(&Component::isSegment, _1)};
384 }
385};
386
387class SegmentOffsetMarker
388{
389public:
390 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::fromSegmentOffset,
396 bind(&Component::toSegmentOffset, _1),
Junxiao Shi71ff2312017-07-12 13:32:50 +0000397 bind(&Name::appendSegmentOffset, _1, _2),
398 Name("/%FB%00%01%86%A0"),
399 100000,
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000400 bind(&Component::isSegmentOffset, _1)};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000401 }
402};
403
Junxiao Shie2099612019-02-15 14:46:27 +0000404class ByteOffsetTyped
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::fromByteOffset,
413 bind(&Component::toByteOffset, _1),
414 bind(&Name::appendByteOffset, _1, _2),
415 Name("/34=%00%01%86%A0"),
416 100000,
417 bind(&Component::isByteOffset, _1)};
418 }
419};
420
421class VersionMarker
422{
423public:
424 using ConventionRev = ConventionMarker;
425
Junxiao Shi71ff2312017-07-12 13:32:50 +0000426 ConventionTest<uint64_t>
427 operator()() const
428 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000429 return {&Component::fromVersion,
430 bind(&Component::toVersion, _1),
Junxiao Shi71ff2312017-07-12 13:32:50 +0000431 [] (Name& name, uint64_t version) -> Name& { return name.appendVersion(version); },
432 Name("/%FD%00%0FB%40"),
433 1000000,
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000434 bind(&Component::isVersion, _1)};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000435 }
436};
437
Junxiao Shie2099612019-02-15 14:46:27 +0000438class VersionTyped
Junxiao Shi71ff2312017-07-12 13:32:50 +0000439{
440public:
Junxiao Shie2099612019-02-15 14:46:27 +0000441 using ConventionRev = ConventionTyped;
442
443 ConventionTest<uint64_t>
444 operator()() const
445 {
446 return {&Component::fromVersion,
447 bind(&Component::toVersion, _1),
448 [] (Name& name, uint64_t version) -> Name& { return name.appendVersion(version); },
449 Name("/35=%00%0FB%40"),
450 1000000,
451 bind(&Component::isVersion, _1)};
452 }
453};
454
455class TimestampMarker
456{
457public:
458 using ConventionRev = ConventionMarker;
459
Junxiao Shi71ff2312017-07-12 13:32:50 +0000460 ConventionTest<time::system_clock::TimePoint>
461 operator()() const
462 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000463 return {&Component::fromTimestamp,
464 bind(&Component::toTimestamp, _1),
Junxiao Shi71ff2312017-07-12 13:32:50 +0000465 [] (Name& name, time::system_clock::TimePoint t) -> Name& { return name.appendTimestamp(t); },
466 Name("/%FC%00%04%7BE%E3%1B%00%00"),
Davide Pesavento0f830802018-01-16 23:58:58 -0500467 time::getUnixEpoch() + 14600_days, // 40 years
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000468 bind(&Component::isTimestamp, _1)};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000469 }
470};
471
Junxiao Shie2099612019-02-15 14:46:27 +0000472class TimestampTyped
Junxiao Shi71ff2312017-07-12 13:32:50 +0000473{
474public:
Junxiao Shie2099612019-02-15 14:46:27 +0000475 using ConventionRev = ConventionTyped;
476
477 ConventionTest<time::system_clock::TimePoint>
478 operator()() const
479 {
480 return {&Component::fromTimestamp,
481 bind(&Component::toTimestamp, _1),
482 [] (Name& name, time::system_clock::TimePoint t) -> Name& { return name.appendTimestamp(t); },
483 Name("/36=%00%04%7BE%E3%1B%00%00"),
484 time::getUnixEpoch() + 14600_days, // 40 years
485 bind(&Component::isTimestamp, _1)};
486 }
487};
488
489class SequenceNumberMarker
490{
491public:
492 using ConventionRev = ConventionMarker;
493
Junxiao Shi71ff2312017-07-12 13:32:50 +0000494 ConventionTest<uint64_t>
495 operator()() const
496 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000497 return {&Component::fromSequenceNumber,
498 bind(&Component::toSequenceNumber, _1),
Junxiao Shi71ff2312017-07-12 13:32:50 +0000499 bind(&Name::appendSequenceNumber, _1, _2),
500 Name("/%FE%00%98%96%80"),
501 10000000,
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000502 bind(&Component::isSequenceNumber, _1)};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000503 }
504};
505
Junxiao Shie2099612019-02-15 14:46:27 +0000506class SequenceNumberTyped
507{
508public:
509 using ConventionRev = ConventionTyped;
510
511 ConventionTest<uint64_t>
512 operator()() const
513 {
514 return {&Component::fromSequenceNumber,
515 bind(&Component::toSequenceNumber, _1),
516 bind(&Name::appendSequenceNumber, _1, _2),
517 Name("/37=%00%98%96%80"),
518 10000000,
519 bind(&Component::isSequenceNumber, _1)};
520 }
521};
522
Junxiao Shi71ff2312017-07-12 13:32:50 +0000523using ConventionTests = boost::mpl::vector<
524 NumberWithMarker,
Junxiao Shie2099612019-02-15 14:46:27 +0000525 SegmentMarker,
526 SegmentTyped,
527 SegmentOffsetMarker,
528 ByteOffsetTyped,
529 VersionMarker,
530 VersionTyped,
531 TimestampMarker,
532 TimestampTyped,
533 SequenceNumberMarker,
534 SequenceNumberTyped
Junxiao Shi71ff2312017-07-12 13:32:50 +0000535>;
536
Junxiao Shie2099612019-02-15 14:46:27 +0000537BOOST_FIXTURE_TEST_CASE_TEMPLATE(Convention, T, ConventionTests, T::ConventionRev)
Junxiao Shi71ff2312017-07-12 13:32:50 +0000538{
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000539 Component invalidComponent1;
540 Component invalidComponent2("1234567890");
Junxiao Shi71ff2312017-07-12 13:32:50 +0000541
542 auto test = T()();
543
544 const Name& expected = test.expected;
Davide Pesavento009062d2019-11-20 00:16:33 -0500545 BOOST_TEST_MESSAGE("Check " << expected[0]);
Junxiao Shi71ff2312017-07-12 13:32:50 +0000546
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000547 Component actualComponent = test.makeComponent(test.value);
Junxiao Shi71ff2312017-07-12 13:32:50 +0000548 BOOST_CHECK_EQUAL(actualComponent, expected[0]);
549
550 Name actualName;
551 test.append(actualName, test.value);
552 BOOST_CHECK_EQUAL(actualName, expected);
553
554 BOOST_CHECK_EQUAL(test.isComponent(expected[0]), true);
555 BOOST_CHECK_EQUAL(test.getValue(expected[0]), test.value);
556
557 BOOST_CHECK_EQUAL(test.isComponent(invalidComponent1), false);
558 BOOST_CHECK_EQUAL(test.isComponent(invalidComponent2), false);
559
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000560 BOOST_CHECK_THROW(test.getValue(invalidComponent1), Component::Error);
561 BOOST_CHECK_THROW(test.getValue(invalidComponent2), Component::Error);
Junxiao Shi71ff2312017-07-12 13:32:50 +0000562}
563
564BOOST_AUTO_TEST_SUITE_END() // NamingConvention
565
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000566BOOST_AUTO_TEST_SUITE_END() // TestNameComponent
567
568} // namespace tests
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000569} // namespace name
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000570} // namespace ndn