blob: 0c9b4479b1b7095fb7fc1a38d69e98597092a191 [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 Pesavento0f830802018-01-16 23:58:58 -05003 * Copyright (c) 2013-2018 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
22#include "name-component.hpp"
Junxiao Shi71ff2312017-07-12 13:32:50 +000023#include "name.hpp"
Junxiao Shidf4b24e2016-07-14 21:41:43 +000024
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000025#include "block-literal.hpp"
Junxiao Shidf4b24e2016-07-14 21:41:43 +000026#include "boost-test.hpp"
27#include <boost/mpl/vector.hpp>
28
29namespace ndn {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000030namespace name {
Junxiao Shidf4b24e2016-07-14 21:41:43 +000031namespace tests {
32
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000033using namespace ndn::tests;
Junxiao Shidf4b24e2016-07-14 21:41:43 +000034
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000035BOOST_AUTO_TEST_SUITE(TestNameComponent)
Junxiao Shidf4b24e2016-07-14 21:41:43 +000036
37BOOST_AUTO_TEST_SUITE(Decode)
38
39BOOST_AUTO_TEST_CASE(Generic)
40{
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000041 Component comp("0807 6E646E2D637878"_block);
42 BOOST_CHECK_EQUAL(comp.type(), tlv::GenericNameComponent);
43 BOOST_CHECK_EQUAL(comp.toUri(), "ndn-cxx");
44 BOOST_CHECK_EQUAL(Component::fromEscapedString("ndn-cxx"), comp);
Junxiao Shid2e60632018-08-10 10:48:44 -060045 BOOST_CHECK_EQUAL(Component::fromEscapedString("8=ndn-cxx"), comp);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000046
47 comp.wireDecode("0800"_block);
48 BOOST_CHECK_EQUAL(comp.toUri(), "...");
49 BOOST_CHECK_EQUAL(Component::fromEscapedString("..."), comp);
Junxiao Shid2e60632018-08-10 10:48:44 -060050 BOOST_CHECK_EQUAL(Component::fromEscapedString("8=..."), comp);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000051 BOOST_CHECK_EQUAL(Component::fromEscapedString(".%2E."), comp);
52
53 comp.wireDecode("0801 2E"_block);
54 BOOST_CHECK_EQUAL(comp.toUri(), "....");
55 BOOST_CHECK_EQUAL(Component::fromEscapedString("...."), comp);
56 BOOST_CHECK_EQUAL(Component::fromEscapedString("%2E..%2E"), comp);
57
58 comp.wireDecode("0803 2E412E"_block);
59 BOOST_CHECK_EQUAL(comp.toUri(), ".A.");
60 BOOST_CHECK_EQUAL(Component::fromEscapedString(".A."), comp);
61
62 comp.wireDecode("0807 666F6F25626172"_block);
63 BOOST_CHECK_EQUAL(comp.toUri(), "foo%25bar");
64 BOOST_CHECK_EQUAL(Component::fromEscapedString("foo%25bar"), comp);
Junxiao Shid2e60632018-08-10 10:48:44 -060065 BOOST_CHECK_EQUAL(Component::fromEscapedString("8=foo%25bar"), comp);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000066
67 comp.wireDecode("0804 2D2E5F7E"_block);
68 BOOST_CHECK_EQUAL(comp.toUri(), "-._~");
69 BOOST_CHECK_EQUAL(Component::fromEscapedString("-._~"), comp);
70
Junxiao Shid2e60632018-08-10 10:48:44 -060071 comp.wireDecode("0803 393D41"_block);
72 BOOST_CHECK_EQUAL(comp.toUri(), "9%3DA");
73 BOOST_CHECK_EQUAL(Component::fromEscapedString("9%3DA"), comp);
74
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000075 comp = Component(":/?#[]@");
76 BOOST_CHECK_EQUAL(comp.toUri(), "%3A%2F%3F%23%5B%5D%40");
77 BOOST_CHECK_EQUAL(Component::fromEscapedString("%3A%2F%3F%23%5B%5D%40"), comp);
78
79 BOOST_CHECK_THROW(Component::fromEscapedString(""), Component::Error);
80 BOOST_CHECK_THROW(Component::fromEscapedString("."), Component::Error);
81 BOOST_CHECK_THROW(Component::fromEscapedString(".."), Component::Error);
Junxiao Shidf4b24e2016-07-14 21:41:43 +000082}
83
84BOOST_AUTO_TEST_CASE(Digest)
85{
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000086 std::string uriPrefix = "sha256digest=";
87 std::string hexLower = "28bad4b5275bd392dbb670c75cf0b66f13f7942b21e80f55c0e86b374753a548";
88 std::string hexUpper = "28BAD4B5275BD392DBB670C75CF0B66F13F7942B21E80F55C0E86B374753A548";
Junxiao Shid2e60632018-08-10 10:48:44 -060089 std::string hexPct = "%28%BA%D4%B5%27%5B%D3%92%DB%B6%70%C7%5C%F0%B6%6F"
90 "%13%F7%94%2B%21%E8%0F%55%C0%E8%6B%37%47%53%A5%48";
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000091
92 Component comp("0120 28BAD4B5275BD392DBB670C75CF0B66F13F7942B21E80F55C0E86B374753A548"_block);
93 BOOST_CHECK_EQUAL(comp.toUri(), uriPrefix + hexLower);
94 BOOST_CHECK_EQUAL(Component::fromEscapedString(uriPrefix + hexLower), comp);
95 BOOST_CHECK_EQUAL(Component::fromEscapedString(uriPrefix + hexUpper), comp);
Junxiao Shid2e60632018-08-10 10:48:44 -060096 BOOST_CHECK_EQUAL(Component::fromEscapedString("1=" + hexPct), comp);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000097
98 BOOST_CHECK_THROW(comp.wireDecode("0108 A791806951F25C4D"_block), Component::Error);
99 BOOST_CHECK_THROW(Component::fromEscapedString(uriPrefix), Component::Error);
100 BOOST_CHECK_THROW(Component::fromEscapedString(uriPrefix + "=a791806951f25c4d"),
101 Component::Error);
102 BOOST_CHECK_THROW(Component::fromEscapedString("1=" + hexLower), Component::Error);
103 BOOST_CHECK_THROW(Component::fromEscapedString("SHA256DIGEST=" + hexLower), Component::Error);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000104}
105
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000106BOOST_AUTO_TEST_CASE(OtherType)
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000107{
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000108 Component comp("0907 6E646E2D637878"_block);
109 BOOST_CHECK_EQUAL(comp.type(), 0x09);
110 BOOST_CHECK_EQUAL(comp.toUri(), "9=ndn-cxx");
111 BOOST_CHECK_EQUAL(Component::fromEscapedString("9=ndn-cxx"), comp);
112
113 comp.wireDecode("FDFFFF00"_block);
114 BOOST_CHECK_EQUAL(comp.type(), 0xFFFF);
115 BOOST_CHECK_EQUAL(comp.toUri(), "65535=...");
116 BOOST_CHECK_EQUAL(Component::fromEscapedString("65535=..."), comp);
117
118 comp.wireDecode("FD576501 2E"_block);
119 BOOST_CHECK_EQUAL(comp.type(), 0x5765);
120 BOOST_CHECK_EQUAL(comp.toUri(), "22373=....");
121 BOOST_CHECK_EQUAL(Component::fromEscapedString("22373=...."), comp);
122
123 BOOST_CHECK_THROW(Component::fromEscapedString("3="), Component::Error);
124 BOOST_CHECK_THROW(Component::fromEscapedString("3=."), Component::Error);
125 BOOST_CHECK_THROW(Component::fromEscapedString("3=.."), Component::Error);
126}
127
128BOOST_AUTO_TEST_CASE(InvalidType)
129{
130 Component comp;
131 BOOST_CHECK_THROW(comp.wireDecode("0001 80"_block), Component::Error);
132 BOOST_CHECK_THROW(comp.wireDecode("FE0001000001 80"_block), Component::Error);
133
134 BOOST_CHECK_THROW(Component::fromEscapedString("0=A"), Component::Error);
135 BOOST_CHECK_THROW(Component::fromEscapedString("65536=A"), Component::Error);
Junxiao Shid2e60632018-08-10 10:48:44 -0600136 BOOST_CHECK_THROW(Component::fromEscapedString("4294967296=A"), Component::Error);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000137 BOOST_CHECK_THROW(Component::fromEscapedString("-1=A"), Component::Error);
138 BOOST_CHECK_THROW(Component::fromEscapedString("+=A"), Component::Error);
139 BOOST_CHECK_THROW(Component::fromEscapedString("=A"), Component::Error);
140 BOOST_CHECK_THROW(Component::fromEscapedString("0x1=A"), Component::Error);
141 BOOST_CHECK_THROW(Component::fromEscapedString("Z=A"), Component::Error);
142 BOOST_CHECK_THROW(Component::fromEscapedString("09=A"), Component::Error);
143 BOOST_CHECK_THROW(Component::fromEscapedString("0x2=A"), Component::Error);
144 BOOST_CHECK_THROW(Component::fromEscapedString("+9=A"), Component::Error);
Junxiao Shid2e60632018-08-10 10:48:44 -0600145 BOOST_CHECK_THROW(Component::fromEscapedString(" 9=A"), Component::Error);
146 BOOST_CHECK_THROW(Component::fromEscapedString("9 =A"), Component::Error);
147 BOOST_CHECK_THROW(Component::fromEscapedString("9.0=A"), Component::Error);
148 BOOST_CHECK_THROW(Component::fromEscapedString("9E0=A"), Component::Error);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000149}
150
151BOOST_AUTO_TEST_SUITE_END() // Decode
152
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000153BOOST_AUTO_TEST_CASE(Compare)
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000154{
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000155 std::vector<Component> comps = {
156 Component("0120 0000000000000000000000000000000000000000000000000000000000000000"_block),
157 Component("0120 0000000000000000000000000000000000000000000000000000000000000001"_block),
158 Component("0120 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"_block),
159 Component(0x02),
160 Component("0201 44"_block),
161 Component("0201 46"_block),
162 Component("0202 4141"_block),
163 Component(),
164 Component("D"),
165 Component("F"),
166 Component("AA"),
167 Component(0x53B2),
168 Component("FD53B201 44"_block),
169 Component("FD53B201 46"_block),
170 Component("FD53B202 4141"_block),
171 };
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000172
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000173 for (size_t i = 0; i < comps.size(); ++i) {
174 for (size_t j = 0; j < comps.size(); ++j) {
175 Component lhs = comps[i];
176 Component rhs = comps[j];
177 BOOST_CHECK_EQUAL(lhs == rhs, i == j);
178 BOOST_CHECK_EQUAL(lhs != rhs, i != j);
179 BOOST_CHECK_EQUAL(lhs < rhs, i < j);
180 BOOST_CHECK_EQUAL(lhs <= rhs, i <= j);
181 BOOST_CHECK_EQUAL(lhs > rhs, i > j);
182 BOOST_CHECK_EQUAL(lhs >= rhs, i >= j);
183 }
184 }
Davide Pesavento08378cb2018-02-01 16:10:54 -0500185}
186
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000187BOOST_AUTO_TEST_SUITE(CreateFromIterators) // Bug 2490
188
189typedef boost::mpl::vector<
190 std::vector<uint8_t>,
191 std::list<uint8_t>,
192 std::vector<int8_t>,
193 std::list<int8_t>
194> ContainerTypes;
195
196BOOST_AUTO_TEST_CASE_TEMPLATE(ZeroOctet, T, ContainerTypes)
197{
198 T bytes;
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000199 Component c(bytes.begin(), bytes.end());
200 BOOST_CHECK_EQUAL(c.type(), tlv::GenericNameComponent);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000201 BOOST_CHECK_EQUAL(c.value_size(), 0);
202 BOOST_CHECK_EQUAL(c.size(), 2);
203}
204
205BOOST_AUTO_TEST_CASE_TEMPLATE(OneOctet, T, ContainerTypes)
206{
207 T bytes{1};
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000208 Component c(0x09, bytes.begin(), bytes.end());
209 BOOST_CHECK_EQUAL(c.type(), 0x09);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000210 BOOST_CHECK_EQUAL(c.value_size(), 1);
211 BOOST_CHECK_EQUAL(c.size(), 3);
212}
213
214BOOST_AUTO_TEST_CASE_TEMPLATE(FourOctets, T, ContainerTypes)
215{
216 T bytes{1, 2, 3, 4};
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000217 Component c(0xFCEC, bytes.begin(), bytes.end());
218 BOOST_CHECK_EQUAL(c.type(), 0xFCEC);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000219 BOOST_CHECK_EQUAL(c.value_size(), 4);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000220 BOOST_CHECK_EQUAL(c.size(), 8);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000221}
222
223BOOST_AUTO_TEST_SUITE_END() // CreateFromIterators
224
Junxiao Shi71ff2312017-07-12 13:32:50 +0000225BOOST_AUTO_TEST_SUITE(NamingConvention)
226
227template<typename ArgType>
228struct ConventionTest
229{
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000230 function<Component(ArgType)> makeComponent;
231 function<ArgType(const Component&)> getValue;
Junxiao Shi71ff2312017-07-12 13:32:50 +0000232 function<Name&(Name&, ArgType)> append;
233 Name expected;
234 ArgType value;
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000235 function<bool(const Component&)> isComponent;
Junxiao Shi71ff2312017-07-12 13:32:50 +0000236};
237
238class NumberWithMarker
239{
240public:
241 ConventionTest<uint64_t>
242 operator()() const
243 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000244 return {bind(&Component::fromNumberWithMarker, 0xAA, _1),
245 bind(&Component::toNumberWithMarker, _1, 0xAA),
Junxiao Shi71ff2312017-07-12 13:32:50 +0000246 bind(&Name::appendNumberWithMarker, _1, 0xAA, _2),
247 Name("/%AA%03%E8"),
248 1000,
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000249 bind(&Component::isNumberWithMarker, _1, 0xAA)};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000250 }
251};
252
253class Segment
254{
255public:
256 ConventionTest<uint64_t>
257 operator()() const
258 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000259 return {&Component::fromSegment,
260 bind(&Component::toSegment, _1),
Junxiao Shi71ff2312017-07-12 13:32:50 +0000261 bind(&Name::appendSegment, _1, _2),
262 Name("/%00%27%10"),
263 10000,
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000264 bind(&Component::isSegment, _1)};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000265 }
266};
267
268class SegmentOffset
269{
270public:
271 ConventionTest<uint64_t>
272 operator()() const
273 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000274 return {&Component::fromSegmentOffset,
275 bind(&Component::toSegmentOffset, _1),
Junxiao Shi71ff2312017-07-12 13:32:50 +0000276 bind(&Name::appendSegmentOffset, _1, _2),
277 Name("/%FB%00%01%86%A0"),
278 100000,
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000279 bind(&Component::isSegmentOffset, _1)};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000280 }
281};
282
283class Version
284{
285public:
286 ConventionTest<uint64_t>
287 operator()() const
288 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000289 return {&Component::fromVersion,
290 bind(&Component::toVersion, _1),
Junxiao Shi71ff2312017-07-12 13:32:50 +0000291 [] (Name& name, uint64_t version) -> Name& { return name.appendVersion(version); },
292 Name("/%FD%00%0FB%40"),
293 1000000,
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000294 bind(&Component::isVersion, _1)};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000295 }
296};
297
298class Timestamp
299{
300public:
301 ConventionTest<time::system_clock::TimePoint>
302 operator()() const
303 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000304 return {&Component::fromTimestamp,
305 bind(&Component::toTimestamp, _1),
Junxiao Shi71ff2312017-07-12 13:32:50 +0000306 [] (Name& name, time::system_clock::TimePoint t) -> Name& { return name.appendTimestamp(t); },
307 Name("/%FC%00%04%7BE%E3%1B%00%00"),
Davide Pesavento0f830802018-01-16 23:58:58 -0500308 time::getUnixEpoch() + 14600_days, // 40 years
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000309 bind(&Component::isTimestamp, _1)};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000310 }
311};
312
313class SequenceNumber
314{
315public:
316 ConventionTest<uint64_t>
317 operator()() const
318 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000319 return {&Component::fromSequenceNumber,
320 bind(&Component::toSequenceNumber, _1),
Junxiao Shi71ff2312017-07-12 13:32:50 +0000321 bind(&Name::appendSequenceNumber, _1, _2),
322 Name("/%FE%00%98%96%80"),
323 10000000,
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000324 bind(&Component::isSequenceNumber, _1)};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000325 }
326};
327
328using ConventionTests = boost::mpl::vector<
329 NumberWithMarker,
330 Segment,
331 SegmentOffset,
332 Version,
333 Timestamp,
334 SequenceNumber
335>;
336
337BOOST_AUTO_TEST_CASE_TEMPLATE(Convention, T, ConventionTests)
338{
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000339 Component invalidComponent1;
340 Component invalidComponent2("1234567890");
Junxiao Shi71ff2312017-07-12 13:32:50 +0000341
342 auto test = T()();
343
344 const Name& expected = test.expected;
345 BOOST_TEST_MESSAGE("Check " << expected[0].toUri());
346
347 BOOST_CHECK_EQUAL(expected[0].isGeneric(), true);
348
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000349 Component actualComponent = test.makeComponent(test.value);
Junxiao Shi71ff2312017-07-12 13:32:50 +0000350 BOOST_CHECK_EQUAL(actualComponent, expected[0]);
351
352 Name actualName;
353 test.append(actualName, test.value);
354 BOOST_CHECK_EQUAL(actualName, expected);
355
356 BOOST_CHECK_EQUAL(test.isComponent(expected[0]), true);
357 BOOST_CHECK_EQUAL(test.getValue(expected[0]), test.value);
358
359 BOOST_CHECK_EQUAL(test.isComponent(invalidComponent1), false);
360 BOOST_CHECK_EQUAL(test.isComponent(invalidComponent2), false);
361
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000362 BOOST_CHECK_THROW(test.getValue(invalidComponent1), Component::Error);
363 BOOST_CHECK_THROW(test.getValue(invalidComponent2), Component::Error);
Junxiao Shi71ff2312017-07-12 13:32:50 +0000364}
365
366BOOST_AUTO_TEST_SUITE_END() // NamingConvention
367
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000368BOOST_AUTO_TEST_SUITE_END() // TestNameComponent
369
370} // namespace tests
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000371} // namespace name
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000372} // namespace ndn