blob: 0bc7978bc04e544629247067850c96f6b015e7b9 [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);
45
46 comp.wireDecode("0800"_block);
47 BOOST_CHECK_EQUAL(comp.toUri(), "...");
48 BOOST_CHECK_EQUAL(Component::fromEscapedString("..."), comp);
49 BOOST_CHECK_EQUAL(Component::fromEscapedString(".%2E."), comp);
50
51 comp.wireDecode("0801 2E"_block);
52 BOOST_CHECK_EQUAL(comp.toUri(), "....");
53 BOOST_CHECK_EQUAL(Component::fromEscapedString("...."), comp);
54 BOOST_CHECK_EQUAL(Component::fromEscapedString("%2E..%2E"), comp);
55
56 comp.wireDecode("0803 2E412E"_block);
57 BOOST_CHECK_EQUAL(comp.toUri(), ".A.");
58 BOOST_CHECK_EQUAL(Component::fromEscapedString(".A."), comp);
59
60 comp.wireDecode("0807 666F6F25626172"_block);
61 BOOST_CHECK_EQUAL(comp.toUri(), "foo%25bar");
62 BOOST_CHECK_EQUAL(Component::fromEscapedString("foo%25bar"), comp);
63
64 comp.wireDecode("0804 2D2E5F7E"_block);
65 BOOST_CHECK_EQUAL(comp.toUri(), "-._~");
66 BOOST_CHECK_EQUAL(Component::fromEscapedString("-._~"), comp);
67
68 comp = Component(":/?#[]@");
69 BOOST_CHECK_EQUAL(comp.toUri(), "%3A%2F%3F%23%5B%5D%40");
70 BOOST_CHECK_EQUAL(Component::fromEscapedString("%3A%2F%3F%23%5B%5D%40"), comp);
71
72 BOOST_CHECK_THROW(Component::fromEscapedString(""), Component::Error);
73 BOOST_CHECK_THROW(Component::fromEscapedString("."), Component::Error);
74 BOOST_CHECK_THROW(Component::fromEscapedString(".."), Component::Error);
75 BOOST_CHECK_THROW(Component::fromEscapedString("8=A"), Component::Error);
Junxiao Shidf4b24e2016-07-14 21:41:43 +000076}
77
78BOOST_AUTO_TEST_CASE(Digest)
79{
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000080 std::string uriPrefix = "sha256digest=";
81 std::string hexLower = "28bad4b5275bd392dbb670c75cf0b66f13f7942b21e80f55c0e86b374753a548";
82 std::string hexUpper = "28BAD4B5275BD392DBB670C75CF0B66F13F7942B21E80F55C0E86B374753A548";
83
84 Component comp("0120 28BAD4B5275BD392DBB670C75CF0B66F13F7942B21E80F55C0E86B374753A548"_block);
85 BOOST_CHECK_EQUAL(comp.toUri(), uriPrefix + hexLower);
86 BOOST_CHECK_EQUAL(Component::fromEscapedString(uriPrefix + hexLower), comp);
87 BOOST_CHECK_EQUAL(Component::fromEscapedString(uriPrefix + hexUpper), comp);
88
89 BOOST_CHECK_THROW(comp.wireDecode("0108 A791806951F25C4D"_block), Component::Error);
90 BOOST_CHECK_THROW(Component::fromEscapedString(uriPrefix), Component::Error);
91 BOOST_CHECK_THROW(Component::fromEscapedString(uriPrefix + "=a791806951f25c4d"),
92 Component::Error);
93 BOOST_CHECK_THROW(Component::fromEscapedString("1=" + hexLower), Component::Error);
94 BOOST_CHECK_THROW(Component::fromEscapedString("SHA256DIGEST=" + hexLower), Component::Error);
Junxiao Shidf4b24e2016-07-14 21:41:43 +000095}
96
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000097BOOST_AUTO_TEST_CASE(OtherType)
Junxiao Shidf4b24e2016-07-14 21:41:43 +000098{
Junxiao Shicf4ac5b2018-03-28 22:46:06 +000099 Component comp("0907 6E646E2D637878"_block);
100 BOOST_CHECK_EQUAL(comp.type(), 0x09);
101 BOOST_CHECK_EQUAL(comp.toUri(), "9=ndn-cxx");
102 BOOST_CHECK_EQUAL(Component::fromEscapedString("9=ndn-cxx"), comp);
103
104 comp.wireDecode("FDFFFF00"_block);
105 BOOST_CHECK_EQUAL(comp.type(), 0xFFFF);
106 BOOST_CHECK_EQUAL(comp.toUri(), "65535=...");
107 BOOST_CHECK_EQUAL(Component::fromEscapedString("65535=..."), comp);
108
109 comp.wireDecode("FD576501 2E"_block);
110 BOOST_CHECK_EQUAL(comp.type(), 0x5765);
111 BOOST_CHECK_EQUAL(comp.toUri(), "22373=....");
112 BOOST_CHECK_EQUAL(Component::fromEscapedString("22373=...."), comp);
113
114 BOOST_CHECK_THROW(Component::fromEscapedString("3="), Component::Error);
115 BOOST_CHECK_THROW(Component::fromEscapedString("3=."), Component::Error);
116 BOOST_CHECK_THROW(Component::fromEscapedString("3=.."), Component::Error);
117}
118
119BOOST_AUTO_TEST_CASE(InvalidType)
120{
121 Component comp;
122 BOOST_CHECK_THROW(comp.wireDecode("0001 80"_block), Component::Error);
123 BOOST_CHECK_THROW(comp.wireDecode("FE0001000001 80"_block), Component::Error);
124
125 BOOST_CHECK_THROW(Component::fromEscapedString("0=A"), Component::Error);
126 BOOST_CHECK_THROW(Component::fromEscapedString("65536=A"), Component::Error);
127 BOOST_CHECK_THROW(Component::fromEscapedString("-1=A"), Component::Error);
128 BOOST_CHECK_THROW(Component::fromEscapedString("+=A"), Component::Error);
129 BOOST_CHECK_THROW(Component::fromEscapedString("=A"), Component::Error);
130 BOOST_CHECK_THROW(Component::fromEscapedString("0x1=A"), Component::Error);
131 BOOST_CHECK_THROW(Component::fromEscapedString("Z=A"), Component::Error);
132 BOOST_CHECK_THROW(Component::fromEscapedString("09=A"), Component::Error);
133 BOOST_CHECK_THROW(Component::fromEscapedString("0x2=A"), Component::Error);
134 BOOST_CHECK_THROW(Component::fromEscapedString("+9=A"), Component::Error);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000135}
136
137BOOST_AUTO_TEST_SUITE_END() // Decode
138
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000139BOOST_AUTO_TEST_CASE(Compare)
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000140{
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000141 std::vector<Component> comps = {
142 Component("0120 0000000000000000000000000000000000000000000000000000000000000000"_block),
143 Component("0120 0000000000000000000000000000000000000000000000000000000000000001"_block),
144 Component("0120 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"_block),
145 Component(0x02),
146 Component("0201 44"_block),
147 Component("0201 46"_block),
148 Component("0202 4141"_block),
149 Component(),
150 Component("D"),
151 Component("F"),
152 Component("AA"),
153 Component(0x53B2),
154 Component("FD53B201 44"_block),
155 Component("FD53B201 46"_block),
156 Component("FD53B202 4141"_block),
157 };
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000158
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000159 for (size_t i = 0; i < comps.size(); ++i) {
160 for (size_t j = 0; j < comps.size(); ++j) {
161 Component lhs = comps[i];
162 Component rhs = comps[j];
163 BOOST_CHECK_EQUAL(lhs == rhs, i == j);
164 BOOST_CHECK_EQUAL(lhs != rhs, i != j);
165 BOOST_CHECK_EQUAL(lhs < rhs, i < j);
166 BOOST_CHECK_EQUAL(lhs <= rhs, i <= j);
167 BOOST_CHECK_EQUAL(lhs > rhs, i > j);
168 BOOST_CHECK_EQUAL(lhs >= rhs, i >= j);
169 }
170 }
Davide Pesavento08378cb2018-02-01 16:10:54 -0500171}
172
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000173BOOST_AUTO_TEST_SUITE(CreateFromIterators) // Bug 2490
174
175typedef boost::mpl::vector<
176 std::vector<uint8_t>,
177 std::list<uint8_t>,
178 std::vector<int8_t>,
179 std::list<int8_t>
180> ContainerTypes;
181
182BOOST_AUTO_TEST_CASE_TEMPLATE(ZeroOctet, T, ContainerTypes)
183{
184 T bytes;
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000185 Component c(bytes.begin(), bytes.end());
186 BOOST_CHECK_EQUAL(c.type(), tlv::GenericNameComponent);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000187 BOOST_CHECK_EQUAL(c.value_size(), 0);
188 BOOST_CHECK_EQUAL(c.size(), 2);
189}
190
191BOOST_AUTO_TEST_CASE_TEMPLATE(OneOctet, T, ContainerTypes)
192{
193 T bytes{1};
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000194 Component c(0x09, bytes.begin(), bytes.end());
195 BOOST_CHECK_EQUAL(c.type(), 0x09);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000196 BOOST_CHECK_EQUAL(c.value_size(), 1);
197 BOOST_CHECK_EQUAL(c.size(), 3);
198}
199
200BOOST_AUTO_TEST_CASE_TEMPLATE(FourOctets, T, ContainerTypes)
201{
202 T bytes{1, 2, 3, 4};
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000203 Component c(0xFCEC, bytes.begin(), bytes.end());
204 BOOST_CHECK_EQUAL(c.type(), 0xFCEC);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000205 BOOST_CHECK_EQUAL(c.value_size(), 4);
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000206 BOOST_CHECK_EQUAL(c.size(), 8);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000207}
208
209BOOST_AUTO_TEST_SUITE_END() // CreateFromIterators
210
Junxiao Shi71ff2312017-07-12 13:32:50 +0000211BOOST_AUTO_TEST_SUITE(NamingConvention)
212
213template<typename ArgType>
214struct ConventionTest
215{
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000216 function<Component(ArgType)> makeComponent;
217 function<ArgType(const Component&)> getValue;
Junxiao Shi71ff2312017-07-12 13:32:50 +0000218 function<Name&(Name&, ArgType)> append;
219 Name expected;
220 ArgType value;
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000221 function<bool(const Component&)> isComponent;
Junxiao Shi71ff2312017-07-12 13:32:50 +0000222};
223
224class NumberWithMarker
225{
226public:
227 ConventionTest<uint64_t>
228 operator()() const
229 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000230 return {bind(&Component::fromNumberWithMarker, 0xAA, _1),
231 bind(&Component::toNumberWithMarker, _1, 0xAA),
Junxiao Shi71ff2312017-07-12 13:32:50 +0000232 bind(&Name::appendNumberWithMarker, _1, 0xAA, _2),
233 Name("/%AA%03%E8"),
234 1000,
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000235 bind(&Component::isNumberWithMarker, _1, 0xAA)};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000236 }
237};
238
239class Segment
240{
241public:
242 ConventionTest<uint64_t>
243 operator()() const
244 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000245 return {&Component::fromSegment,
246 bind(&Component::toSegment, _1),
Junxiao Shi71ff2312017-07-12 13:32:50 +0000247 bind(&Name::appendSegment, _1, _2),
248 Name("/%00%27%10"),
249 10000,
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000250 bind(&Component::isSegment, _1)};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000251 }
252};
253
254class SegmentOffset
255{
256public:
257 ConventionTest<uint64_t>
258 operator()() const
259 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000260 return {&Component::fromSegmentOffset,
261 bind(&Component::toSegmentOffset, _1),
Junxiao Shi71ff2312017-07-12 13:32:50 +0000262 bind(&Name::appendSegmentOffset, _1, _2),
263 Name("/%FB%00%01%86%A0"),
264 100000,
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000265 bind(&Component::isSegmentOffset, _1)};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000266 }
267};
268
269class Version
270{
271public:
272 ConventionTest<uint64_t>
273 operator()() const
274 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000275 return {&Component::fromVersion,
276 bind(&Component::toVersion, _1),
Junxiao Shi71ff2312017-07-12 13:32:50 +0000277 [] (Name& name, uint64_t version) -> Name& { return name.appendVersion(version); },
278 Name("/%FD%00%0FB%40"),
279 1000000,
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000280 bind(&Component::isVersion, _1)};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000281 }
282};
283
284class Timestamp
285{
286public:
287 ConventionTest<time::system_clock::TimePoint>
288 operator()() const
289 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000290 return {&Component::fromTimestamp,
291 bind(&Component::toTimestamp, _1),
Junxiao Shi71ff2312017-07-12 13:32:50 +0000292 [] (Name& name, time::system_clock::TimePoint t) -> Name& { return name.appendTimestamp(t); },
293 Name("/%FC%00%04%7BE%E3%1B%00%00"),
Davide Pesavento0f830802018-01-16 23:58:58 -0500294 time::getUnixEpoch() + 14600_days, // 40 years
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000295 bind(&Component::isTimestamp, _1)};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000296 }
297};
298
299class SequenceNumber
300{
301public:
302 ConventionTest<uint64_t>
303 operator()() const
304 {
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000305 return {&Component::fromSequenceNumber,
306 bind(&Component::toSequenceNumber, _1),
Junxiao Shi71ff2312017-07-12 13:32:50 +0000307 bind(&Name::appendSequenceNumber, _1, _2),
308 Name("/%FE%00%98%96%80"),
309 10000000,
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000310 bind(&Component::isSequenceNumber, _1)};
Junxiao Shi71ff2312017-07-12 13:32:50 +0000311 }
312};
313
314using ConventionTests = boost::mpl::vector<
315 NumberWithMarker,
316 Segment,
317 SegmentOffset,
318 Version,
319 Timestamp,
320 SequenceNumber
321>;
322
323BOOST_AUTO_TEST_CASE_TEMPLATE(Convention, T, ConventionTests)
324{
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000325 Component invalidComponent1;
326 Component invalidComponent2("1234567890");
Junxiao Shi71ff2312017-07-12 13:32:50 +0000327
328 auto test = T()();
329
330 const Name& expected = test.expected;
331 BOOST_TEST_MESSAGE("Check " << expected[0].toUri());
332
333 BOOST_CHECK_EQUAL(expected[0].isGeneric(), true);
334
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000335 Component actualComponent = test.makeComponent(test.value);
Junxiao Shi71ff2312017-07-12 13:32:50 +0000336 BOOST_CHECK_EQUAL(actualComponent, expected[0]);
337
338 Name actualName;
339 test.append(actualName, test.value);
340 BOOST_CHECK_EQUAL(actualName, expected);
341
342 BOOST_CHECK_EQUAL(test.isComponent(expected[0]), true);
343 BOOST_CHECK_EQUAL(test.getValue(expected[0]), test.value);
344
345 BOOST_CHECK_EQUAL(test.isComponent(invalidComponent1), false);
346 BOOST_CHECK_EQUAL(test.isComponent(invalidComponent2), false);
347
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000348 BOOST_CHECK_THROW(test.getValue(invalidComponent1), Component::Error);
349 BOOST_CHECK_THROW(test.getValue(invalidComponent2), Component::Error);
Junxiao Shi71ff2312017-07-12 13:32:50 +0000350}
351
352BOOST_AUTO_TEST_SUITE_END() // NamingConvention
353
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000354BOOST_AUTO_TEST_SUITE_END() // TestNameComponent
355
356} // namespace tests
Junxiao Shicf4ac5b2018-03-28 22:46:06 +0000357} // namespace name
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000358} // namespace ndn