blob: 7abec3652d7890fc4d9fa810692c398ab4d57ab8 [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 Pesaventobc17d952020-02-15 20:10:52 -05003 * Copyright (c) 2013-2020 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);
Junxiao Shidf4b24e2016-07-14 21:41:43 +000094}
95
Junxiao Shi4053bd52018-08-16 13:39:25 -060096static void
Davide Pesavento009062d2019-11-20 00:16:33 -050097testSha256Component(uint32_t type, const std::string& uriPrefix)
Junxiao Shidf4b24e2016-07-14 21:41:43 +000098{
Davide Pesavento009062d2019-11-20 00:16:33 -050099 const std::string hexLower = "28bad4b5275bd392dbb670c75cf0b66f13f7942b21e80f55c0e86b374753a548";
100 const std::string hexUpper = boost::to_upper_copy(hexLower);
Junxiao Shi4053bd52018-08-16 13:39:25 -0600101 std::string hexPct;
102 for (size_t i = 0; i < hexUpper.size(); i += 2) {
103 hexPct += "%" + hexUpper.substr(i, 2);
104 }
Junxiao Shia39c0b52019-12-31 15:13:15 -0700105 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 +0000106
Junxiao Shi4053bd52018-08-16 13:39:25 -0600107 Component comp(Block(type, fromHex(hexLower)));
Davide Pesavento009062d2019-11-20 00:16:33 -0500108
Junxiao Shi4053bd52018-08-16 13:39:25 -0600109 BOOST_CHECK_EQUAL(comp.type(), type);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000110 BOOST_CHECK_EQUAL(comp.toUri(), uriPrefix + hexLower);
Junxiao Shia39c0b52019-12-31 15:13:15 -0700111 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::CANONICAL), to_string(type) + "=" + hexPctCanonical);
112 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ALTERNATE), uriPrefix + hexLower);
Davide Pesaventobc17d952020-02-15 20:10:52 -0500113 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_CANONICAL), to_string(type) + "=" + hexPctCanonical);
114 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_ALTERNATE), uriPrefix + hexLower);
Davide Pesavento009062d2019-11-20 00:16:33 -0500115 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(comp), uriPrefix + hexLower);
116 BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(uriPrefix + hexLower));
117 BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(uriPrefix + hexUpper));
118 BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(to_string(type) + "=" + hexPct));
Junxiao Shia39c0b52019-12-31 15:13:15 -0700119 BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(to_string(type) + "=" + hexPctCanonical));
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000120
Davide Pesavento009062d2019-11-20 00:16:33 -0500121 CHECK_COMP_ERR(comp.wireDecode(Block(type, fromHex("A791806951F25C4D"))), "TLV-LENGTH must be 32");
122 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix), "TLV-LENGTH must be 32");
123 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "a791806951f25c4d"), "TLV-LENGTH must be 32");
124 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "foo"), "invalid hex encoding");
125 CHECK_COMP_ERR(Component::fromEscapedString(boost::to_upper_copy(uriPrefix) + hexLower), "Unknown TLV-TYPE");
Junxiao Shi4053bd52018-08-16 13:39:25 -0600126}
127
Davide Pesavento009062d2019-11-20 00:16:33 -0500128BOOST_AUTO_TEST_CASE(ImplicitDigest)
Junxiao Shi4053bd52018-08-16 13:39:25 -0600129{
Davide Pesavento009062d2019-11-20 00:16:33 -0500130 testSha256Component(tlv::ImplicitSha256DigestComponent, "sha256digest=");
Junxiao Shi4053bd52018-08-16 13:39:25 -0600131}
132
Davide Pesavento009062d2019-11-20 00:16:33 -0500133BOOST_AUTO_TEST_CASE(ParametersDigest)
Junxiao Shi4053bd52018-08-16 13:39:25 -0600134{
Davide Pesavento009062d2019-11-20 00:16:33 -0500135 testSha256Component(tlv::ParametersSha256DigestComponent, "params-sha256=");
136}
137
138static void
139testDecimalComponent(uint32_t type, const std::string& uriPrefix)
140{
141 const Component comp(makeNonNegativeIntegerBlock(type, 42)); // TLV-VALUE is a nonNegativeInteger
142 BOOST_CHECK_EQUAL(comp.type(), type);
143 BOOST_CHECK_EQUAL(comp.isNumber(), true);
144 const auto compUri = uriPrefix + "42";
145 BOOST_CHECK_EQUAL(comp.toUri(), compUri);
Junxiao Shia39c0b52019-12-31 15:13:15 -0700146 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::CANONICAL), to_string(type) + "=%2A");
147 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ALTERNATE), compUri);
Davide Pesaventobc17d952020-02-15 20:10:52 -0500148 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_CANONICAL), to_string(type) + "=%2A");
149 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_ALTERNATE), compUri);
Davide Pesavento009062d2019-11-20 00:16:33 -0500150 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(comp), compUri);
151 BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(compUri));
152 BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(to_string(type) + "=%2A"));
153 BOOST_CHECK_EQUAL(comp, Component::fromNumber(42, type));
154
155 const Component comp2(Block(type, fromHex("010203"))); // TLV-VALUE is *not* a nonNegativeInteger
156 BOOST_CHECK_EQUAL(comp2.type(), type);
157 BOOST_CHECK_EQUAL(comp2.isNumber(), false);
158 const auto comp2Uri = to_string(type) + "=%01%02%03";
159 BOOST_CHECK_EQUAL(comp2.toUri(), comp2Uri);
160 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(comp2), comp2Uri);
161 BOOST_CHECK_EQUAL(comp2, Component::fromEscapedString(comp2Uri));
162
163 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix), "invalid format");
164 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "foo"), "invalid format");
165 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "00"), "invalid format");
166 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "-1"), "invalid format");
167 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "9.3"), "invalid format");
168 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + " 84"), "invalid format");
169 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "0xAF"), "invalid format");
170 CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix + "18446744073709551616"), "out of range");
171 CHECK_COMP_ERR(Component::fromEscapedString(boost::to_upper_copy(uriPrefix) + "42"), "Unknown TLV-TYPE");
172}
173
174BOOST_AUTO_TEST_CASE(Segment)
175{
176 testDecimalComponent(tlv::SegmentNameComponent, "seg=");
177}
178
179BOOST_AUTO_TEST_CASE(ByteOffset)
180{
181 testDecimalComponent(tlv::ByteOffsetNameComponent, "off=");
182}
183
184BOOST_AUTO_TEST_CASE(Version)
185{
186 testDecimalComponent(tlv::VersionNameComponent, "v=");
187}
188
189BOOST_AUTO_TEST_CASE(Timestamp)
190{
191 testDecimalComponent(tlv::TimestampNameComponent, "t=");
192}
193
194BOOST_AUTO_TEST_CASE(SequenceNum)
195{
196 testDecimalComponent(tlv::SequenceNumNameComponent, "seq=");
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000197}
198
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000199BOOST_AUTO_TEST_CASE(OtherType)
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000200{
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000201 Component comp("0907 6E646E2D637878"_block);
202 BOOST_CHECK_EQUAL(comp.type(), 0x09);
203 BOOST_CHECK_EQUAL(comp.toUri(), "9=ndn-cxx");
Davide Pesaventobc17d952020-02-15 20:10:52 -0500204 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::CANONICAL), "9=ndn-cxx");
205 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ALTERNATE), "9=ndn-cxx");
206 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_CANONICAL), "9=ndn-cxx");
207 BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ENV_OR_ALTERNATE), "9=ndn-cxx");
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000208 BOOST_CHECK_EQUAL(Component::fromEscapedString("9=ndn-cxx"), comp);
209
210 comp.wireDecode("FDFFFF00"_block);
211 BOOST_CHECK_EQUAL(comp.type(), 0xFFFF);
212 BOOST_CHECK_EQUAL(comp.toUri(), "65535=...");
213 BOOST_CHECK_EQUAL(Component::fromEscapedString("65535=..."), comp);
214
215 comp.wireDecode("FD576501 2E"_block);
216 BOOST_CHECK_EQUAL(comp.type(), 0x5765);
217 BOOST_CHECK_EQUAL(comp.toUri(), "22373=....");
218 BOOST_CHECK_EQUAL(Component::fromEscapedString("22373=...."), comp);
219
220 BOOST_CHECK_THROW(Component::fromEscapedString("3="), Component::Error);
221 BOOST_CHECK_THROW(Component::fromEscapedString("3=."), Component::Error);
222 BOOST_CHECK_THROW(Component::fromEscapedString("3=.."), Component::Error);
223}
224
225BOOST_AUTO_TEST_CASE(InvalidType)
226{
227 Component comp;
Davide Pesavento6b330402019-04-24 00:14:01 -0400228 BOOST_CHECK_THROW(comp.wireDecode(Block{}), Component::Error);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000229 BOOST_CHECK_THROW(comp.wireDecode("FE0001000001 80"_block), Component::Error);
230
231 BOOST_CHECK_THROW(Component::fromEscapedString("0=A"), Component::Error);
232 BOOST_CHECK_THROW(Component::fromEscapedString("65536=A"), Component::Error);
Junxiao Shid2e60632018-08-10 10:48:44 -0600233 BOOST_CHECK_THROW(Component::fromEscapedString("4294967296=A"), Component::Error);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000234 BOOST_CHECK_THROW(Component::fromEscapedString("-1=A"), Component::Error);
235 BOOST_CHECK_THROW(Component::fromEscapedString("+=A"), Component::Error);
236 BOOST_CHECK_THROW(Component::fromEscapedString("=A"), Component::Error);
237 BOOST_CHECK_THROW(Component::fromEscapedString("0x1=A"), Component::Error);
238 BOOST_CHECK_THROW(Component::fromEscapedString("Z=A"), Component::Error);
239 BOOST_CHECK_THROW(Component::fromEscapedString("09=A"), Component::Error);
Junxiao Shi4053bd52018-08-16 13:39:25 -0600240 BOOST_CHECK_THROW(Component::fromEscapedString("0x3=A"), Component::Error);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000241 BOOST_CHECK_THROW(Component::fromEscapedString("+9=A"), Component::Error);
Junxiao Shid2e60632018-08-10 10:48:44 -0600242 BOOST_CHECK_THROW(Component::fromEscapedString(" 9=A"), Component::Error);
243 BOOST_CHECK_THROW(Component::fromEscapedString("9 =A"), Component::Error);
244 BOOST_CHECK_THROW(Component::fromEscapedString("9.0=A"), Component::Error);
245 BOOST_CHECK_THROW(Component::fromEscapedString("9E0=A"), Component::Error);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000246}
247
248BOOST_AUTO_TEST_SUITE_END() // Decode
249
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000250BOOST_AUTO_TEST_CASE(Compare)
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000251{
Davide Pesavento009062d2019-11-20 00:16:33 -0500252 const std::vector<Component> comps = {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000253 Component("0120 0000000000000000000000000000000000000000000000000000000000000000"_block),
254 Component("0120 0000000000000000000000000000000000000000000000000000000000000001"_block),
255 Component("0120 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"_block),
Junxiao Shi4053bd52018-08-16 13:39:25 -0600256 Component("0220 0000000000000000000000000000000000000000000000000000000000000000"_block),
257 Component("0220 0000000000000000000000000000000000000000000000000000000000000001"_block),
258 Component("0220 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"_block),
259 Component(0x03),
260 Component("0301 44"_block),
261 Component("0301 46"_block),
262 Component("0302 4141"_block),
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000263 Component(),
264 Component("D"),
265 Component("F"),
266 Component("AA"),
267 Component(0x53B2),
268 Component("FD53B201 44"_block),
269 Component("FD53B201 46"_block),
270 Component("FD53B202 4141"_block),
271 };
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000272
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000273 for (size_t i = 0; i < comps.size(); ++i) {
274 for (size_t j = 0; j < comps.size(); ++j) {
275 Component lhs = comps[i];
276 Component rhs = comps[j];
277 BOOST_CHECK_EQUAL(lhs == rhs, i == j);
278 BOOST_CHECK_EQUAL(lhs != rhs, i != j);
279 BOOST_CHECK_EQUAL(lhs < rhs, i < j);
280 BOOST_CHECK_EQUAL(lhs <= rhs, i <= j);
281 BOOST_CHECK_EQUAL(lhs > rhs, i > j);
282 BOOST_CHECK_EQUAL(lhs >= rhs, i >= j);
283 }
284 }
Davide Pesavento08378cb2018-02-01 16:10:54 -0500285}
286
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000287BOOST_AUTO_TEST_SUITE(CreateFromIterators) // Bug 2490
288
Davide Pesavento009062d2019-11-20 00:16:33 -0500289using ContainerTypes = boost::mpl::vector<std::vector<uint8_t>,
290 std::list<uint8_t>,
291 std::vector<int8_t>,
292 std::list<int8_t>>;
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000293
294BOOST_AUTO_TEST_CASE_TEMPLATE(ZeroOctet, T, ContainerTypes)
295{
296 T bytes;
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000297 Component c(bytes.begin(), bytes.end());
298 BOOST_CHECK_EQUAL(c.type(), tlv::GenericNameComponent);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000299 BOOST_CHECK_EQUAL(c.value_size(), 0);
300 BOOST_CHECK_EQUAL(c.size(), 2);
301}
302
303BOOST_AUTO_TEST_CASE_TEMPLATE(OneOctet, T, ContainerTypes)
304{
305 T bytes{1};
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000306 Component c(0x09, bytes.begin(), bytes.end());
307 BOOST_CHECK_EQUAL(c.type(), 0x09);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000308 BOOST_CHECK_EQUAL(c.value_size(), 1);
309 BOOST_CHECK_EQUAL(c.size(), 3);
310}
311
312BOOST_AUTO_TEST_CASE_TEMPLATE(FourOctets, T, ContainerTypes)
313{
314 T bytes{1, 2, 3, 4};
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000315 Component c(0xFCEC, bytes.begin(), bytes.end());
316 BOOST_CHECK_EQUAL(c.type(), 0xFCEC);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000317 BOOST_CHECK_EQUAL(c.value_size(), 4);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000318 BOOST_CHECK_EQUAL(c.size(), 8);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000319}
320
321BOOST_AUTO_TEST_SUITE_END() // CreateFromIterators
322
Junxiao Shi71ff2312017-07-12 13:32:50 +0000323BOOST_AUTO_TEST_SUITE(NamingConvention)
324
325template<typename ArgType>
326struct ConventionTest
327{
Davide Pesavento009062d2019-11-20 00:16:33 -0500328 std::function<Component(ArgType)> makeComponent;
329 std::function<ArgType(const Component&)> getValue;
330 std::function<Name&(Name&, ArgType)> append;
Junxiao Shi71ff2312017-07-12 13:32:50 +0000331 Name expected;
332 ArgType value;
Davide Pesavento009062d2019-11-20 00:16:33 -0500333 std::function<bool(const Component&)> isComponent;
Junxiao Shi71ff2312017-07-12 13:32:50 +0000334};
335
Junxiao Shie2099612019-02-15 14:46:27 +0000336class ConventionMarker
337{
338};
339
340class ConventionTyped
341{
342public:
343 ConventionTyped()
344 {
345 name::setConventionEncoding(name::Convention::TYPED);
346 }
347
348 ~ConventionTyped()
349 {
350 name::setConventionEncoding(name::Convention::MARKER);
351 }
352};
353
Junxiao Shi71ff2312017-07-12 13:32:50 +0000354class NumberWithMarker
355{
356public:
Junxiao Shie2099612019-02-15 14:46:27 +0000357 using ConventionRev = ConventionMarker;
358
Junxiao Shi71ff2312017-07-12 13:32:50 +0000359 ConventionTest<uint64_t>
360 operator()() const
361 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000362 return {bind(&Component::fromNumberWithMarker, 0xAA, _1),
363 bind(&Component::toNumberWithMarker, _1, 0xAA),
Junxiao Shi71ff2312017-07-12 13:32:50 +0000364 bind(&Name::appendNumberWithMarker, _1, 0xAA, _2),
365 Name("/%AA%03%E8"),
366 1000,
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000367 bind(&Component::isNumberWithMarker, _1, 0xAA)};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000368 }
369};
370
Junxiao Shie2099612019-02-15 14:46:27 +0000371class SegmentMarker
Junxiao Shi71ff2312017-07-12 13:32:50 +0000372{
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 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000379 return {&Component::fromSegment,
380 bind(&Component::toSegment, _1),
Junxiao Shi71ff2312017-07-12 13:32:50 +0000381 bind(&Name::appendSegment, _1, _2),
382 Name("/%00%27%10"),
383 10000,
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000384 bind(&Component::isSegment, _1)};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000385 }
386};
387
Junxiao Shie2099612019-02-15 14:46:27 +0000388class SegmentTyped
Junxiao Shi71ff2312017-07-12 13:32:50 +0000389{
390public:
Junxiao Shie2099612019-02-15 14:46:27 +0000391 using ConventionRev = ConventionTyped;
392
393 ConventionTest<uint64_t>
394 operator()() const
395 {
396 return {&Component::fromSegment,
397 bind(&Component::toSegment, _1),
398 bind(&Name::appendSegment, _1, _2),
399 Name("/33=%27%10"),
400 10000,
401 bind(&Component::isSegment, _1)};
402 }
403};
404
405class SegmentOffsetMarker
406{
407public:
408 using ConventionRev = ConventionMarker;
409
Junxiao Shi71ff2312017-07-12 13:32:50 +0000410 ConventionTest<uint64_t>
411 operator()() const
412 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000413 return {&Component::fromSegmentOffset,
414 bind(&Component::toSegmentOffset, _1),
Junxiao Shi71ff2312017-07-12 13:32:50 +0000415 bind(&Name::appendSegmentOffset, _1, _2),
416 Name("/%FB%00%01%86%A0"),
417 100000,
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000418 bind(&Component::isSegmentOffset, _1)};
Junxiao Shi71ff2312017-07-12 13:32:50 +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,
431 bind(&Component::toByteOffset, _1),
432 bind(&Name::appendByteOffset, _1, _2),
433 Name("/34=%00%01%86%A0"),
434 100000,
435 bind(&Component::isByteOffset, _1)};
436 }
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,
448 bind(&Component::toVersion, _1),
Junxiao Shi71ff2312017-07-12 13:32:50 +0000449 [] (Name& name, uint64_t version) -> Name& { return name.appendVersion(version); },
450 Name("/%FD%00%0FB%40"),
451 1000000,
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000452 bind(&Component::isVersion, _1)};
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,
465 bind(&Component::toVersion, _1),
466 [] (Name& name, uint64_t version) -> Name& { return name.appendVersion(version); },
467 Name("/35=%00%0FB%40"),
468 1000000,
469 bind(&Component::isVersion, _1)};
470 }
471};
472
473class TimestampMarker
474{
475public:
476 using ConventionRev = ConventionMarker;
477
Junxiao Shi71ff2312017-07-12 13:32:50 +0000478 ConventionTest<time::system_clock::TimePoint>
479 operator()() const
480 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000481 return {&Component::fromTimestamp,
482 bind(&Component::toTimestamp, _1),
Junxiao Shi71ff2312017-07-12 13:32:50 +0000483 [] (Name& name, time::system_clock::TimePoint t) -> Name& { return name.appendTimestamp(t); },
484 Name("/%FC%00%04%7BE%E3%1B%00%00"),
Davide Pesavento0f830802018-01-16 23:58:58 -0500485 time::getUnixEpoch() + 14600_days, // 40 years
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000486 bind(&Component::isTimestamp, _1)};
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
495 ConventionTest<time::system_clock::TimePoint>
496 operator()() const
497 {
498 return {&Component::fromTimestamp,
499 bind(&Component::toTimestamp, _1),
500 [] (Name& name, time::system_clock::TimePoint t) -> Name& { return name.appendTimestamp(t); },
501 Name("/36=%00%04%7BE%E3%1B%00%00"),
502 time::getUnixEpoch() + 14600_days, // 40 years
503 bind(&Component::isTimestamp, _1)};
504 }
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,
516 bind(&Component::toSequenceNumber, _1),
Junxiao Shi71ff2312017-07-12 13:32:50 +0000517 bind(&Name::appendSequenceNumber, _1, _2),
518 Name("/%FE%00%98%96%80"),
519 10000000,
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000520 bind(&Component::isSequenceNumber, _1)};
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,
533 bind(&Component::toSequenceNumber, _1),
534 bind(&Name::appendSequenceNumber, _1, _2),
535 Name("/37=%00%98%96%80"),
536 10000000,
537 bind(&Component::isSequenceNumber, _1)};
538 }
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,
545 SegmentOffsetMarker,
546 ByteOffsetTyped,
547 VersionMarker,
548 VersionTyped,
549 TimestampMarker,
550 TimestampTyped,
551 SequenceNumberMarker,
552 SequenceNumberTyped
Junxiao Shi71ff2312017-07-12 13:32:50 +0000553>;
554
Junxiao Shie2099612019-02-15 14:46:27 +0000555BOOST_FIXTURE_TEST_CASE_TEMPLATE(Convention, T, ConventionTests, T::ConventionRev)
Junxiao Shi71ff2312017-07-12 13:32:50 +0000556{
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000557 Component invalidComponent1;
558 Component invalidComponent2("1234567890");
Junxiao Shi71ff2312017-07-12 13:32:50 +0000559
560 auto test = T()();
561
562 const Name& expected = test.expected;
Davide Pesavento009062d2019-11-20 00:16:33 -0500563 BOOST_TEST_MESSAGE("Check " << expected[0]);
Junxiao Shi71ff2312017-07-12 13:32:50 +0000564
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000565 Component actualComponent = test.makeComponent(test.value);
Junxiao Shi71ff2312017-07-12 13:32:50 +0000566 BOOST_CHECK_EQUAL(actualComponent, expected[0]);
567
568 Name actualName;
569 test.append(actualName, test.value);
570 BOOST_CHECK_EQUAL(actualName, expected);
571
572 BOOST_CHECK_EQUAL(test.isComponent(expected[0]), true);
573 BOOST_CHECK_EQUAL(test.getValue(expected[0]), test.value);
574
575 BOOST_CHECK_EQUAL(test.isComponent(invalidComponent1), false);
576 BOOST_CHECK_EQUAL(test.isComponent(invalidComponent2), false);
577
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000578 BOOST_CHECK_THROW(test.getValue(invalidComponent1), Component::Error);
579 BOOST_CHECK_THROW(test.getValue(invalidComponent2), Component::Error);
Junxiao Shi71ff2312017-07-12 13:32:50 +0000580}
581
582BOOST_AUTO_TEST_SUITE_END() // NamingConvention
583
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000584BOOST_AUTO_TEST_SUITE_END() // TestNameComponent
585
586} // namespace tests
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000587} // namespace name
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000588} // namespace ndn