blob: 96732fd3526bc47c60639e1689774849bf350d6a [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -08002/**
Junxiao Shia6452ac2015-01-23 11:21:06 -07003 * Copyright (c) 2013-2015 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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.
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080020 */
21
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080022#include "name.hpp"
23
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070024#include "boost-test.hpp"
Alexander Afanasyev15f67312014-07-22 15:11:09 -070025#include <boost/tuple/tuple.hpp>
26#include <boost/mpl/vector.hpp>
Yingdi Yu90e23582014-11-06 14:21:04 -080027#include <unordered_map>
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070028
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080029namespace ndn {
Junxiao Shi6bf0adf2015-02-15 13:54:56 -070030namespace tests {
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080031
32BOOST_AUTO_TEST_SUITE(TestName)
33
34static const uint8_t TestName[] = {
Alexander Afanasyev4b456282014-02-13 00:34:34 -080035 0x7, 0x14, // Name
36 0x8, 0x5, // NameComponent
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080037 0x6c, 0x6f, 0x63, 0x61, 0x6c,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080038 0x8, 0x3, // NameComponent
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080039 0x6e, 0x64, 0x6e,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080040 0x8, 0x6, // NameComponent
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080041 0x70, 0x72, 0x65, 0x66, 0x69, 0x78
42};
43
Alexander Afanasyev4b456282014-02-13 00:34:34 -080044const uint8_t Name1[] = {0x7, 0x7, // Name
45 0x8, 0x5, // NameComponent
46 0x6c, 0x6f, 0x63, 0x61, 0x6c};
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070047
Alexander Afanasyev4b456282014-02-13 00:34:34 -080048const uint8_t Name2[] = {0x7, 0xc, // Name
49 0x8, 0x5, // NameComponent
50 0x6c, 0x6f, 0x63, 0x61, 0x6c,
51 0x8, 0x3, // NameComponent
52 0x6e, 0x64, 0x6e};
53
Qiuhan Ding2c3cbe42014-11-25 18:10:23 -080054static const uint8_t TestNameComponent[] = {
55 0x8, 0x3, // NameComponent
56 0x6e, 0x64, 0x6e};
57
58static const uint8_t TestDigestComponent[] = {
59 0x1, 0x20, // ImplicitSha256DigestComponent
60 0x28, 0xba, 0xd4, 0xb5, 0x27, 0x5b, 0xd3, 0x92,
61 0xdb, 0xb6, 0x70, 0xc7, 0x5c, 0xf0, 0xb6, 0x6f,
62 0x13, 0xf7, 0x94, 0x2b, 0x21, 0xe8, 0x0f, 0x55,
63 0xc0, 0xe8, 0x6b, 0x37, 0x47, 0x53, 0xa5, 0x48 };
64
65const uint8_t Component1[] = {0x7, 0x3, // Error in Type
66 0x6e, 0x64, 0x6e};
67
Alexander Afanasyev4b456282014-02-13 00:34:34 -080068
Alexander Afanasyevc2344292014-03-02 00:08:00 +000069BOOST_AUTO_TEST_CASE(Basic)
70{
71 Name name("/hello/world");
72
73 BOOST_CHECK_NO_THROW(name.at(0));
74 BOOST_CHECK_NO_THROW(name.at(1));
75 BOOST_CHECK_NO_THROW(name.at(-1));
76 BOOST_CHECK_NO_THROW(name.at(-2));
77
78 BOOST_CHECK_THROW(name.at(2), Name::Error);
79 BOOST_CHECK_THROW(name.at(-3), Name::Error);
80}
81
82BOOST_AUTO_TEST_CASE(Encode)
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080083{
84 Name name("/local/ndn/prefix");
85
86 const Block &wire = name.wireEncode();
87
88 // for (Buffer::const_iterator i = wire.begin();
89 // i != wire.end();
90 // ++i)
91 // {
92 // std::ios::fmtflags saveFlags = std::cout.flags(std::ios::hex);
93
94 // if (i != wire.begin())
95 // std::cout << ", ";
96 // std::cout << "0x" << static_cast<uint32_t>(*i);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070097
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080098 // std::cout.flags(saveFlags);
99 // }
100 // std::cout << std::endl;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700101
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800102 BOOST_CHECK_EQUAL_COLLECTIONS(TestName, TestName+sizeof(TestName),
103 wire.begin(), wire.end());
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800104}
105
106
Alexander Afanasyevc2344292014-03-02 00:08:00 +0000107BOOST_AUTO_TEST_CASE(Decode)
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800108{
109 Block block(TestName, sizeof(TestName));
110
111 Name name(block);
112
113 BOOST_CHECK_EQUAL(name.toUri(), "/local/ndn/prefix");
114}
115
Qiuhan Ding2c3cbe42014-11-25 18:10:23 -0800116BOOST_AUTO_TEST_CASE(DecodeComponent)
117{
118 Block componentBlock(TestNameComponent, sizeof(TestNameComponent));
119 name::Component nameComponent;
120 BOOST_REQUIRE_NO_THROW(nameComponent.wireDecode(componentBlock));
121 BOOST_CHECK_EQUAL(nameComponent.toUri(), "ndn");
122
123 Block digestComponentBlock(TestDigestComponent, sizeof(TestDigestComponent));
124 name::Component digestComponent;
125 BOOST_REQUIRE_NO_THROW(digestComponent.wireDecode(digestComponentBlock));
126
127 Block errorBlock(Component1, sizeof(Component1));
128 name::Component errorComponent;
129 BOOST_REQUIRE_THROW(errorComponent.wireDecode(errorBlock), name::Component::Error);
130}
131
Alexander Afanasyevc2344292014-03-02 00:08:00 +0000132BOOST_AUTO_TEST_CASE(AppendsAndMultiEncode)
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800133{
134 Name name("/local");
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700135
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800136 BOOST_CHECK_EQUAL_COLLECTIONS(name.wireEncode().begin(), name.wireEncode().end(),
Alexander Afanasyev4b456282014-02-13 00:34:34 -0800137 Name1, Name1 + sizeof(Name1));
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800138
139 name.append("ndn");
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700140
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800141 BOOST_CHECK_EQUAL_COLLECTIONS(name.wireEncode().begin(), name.wireEncode().end(),
Alexander Afanasyev4b456282014-02-13 00:34:34 -0800142 Name2, Name2 + sizeof(Name2));
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800143
144 name.append("prefix");
145 BOOST_CHECK_EQUAL_COLLECTIONS(name.wireEncode().begin(), name.wireEncode().end(),
146 TestName, TestName+sizeof(TestName));
147}
148
Junxiao Shi2586be42015-01-30 20:13:01 -0700149BOOST_AUTO_TEST_CASE(ZeroLengthComponent)
150{
151 static const uint8_t compOctets[] {0x08, 0x00};
152 Block compBlock(compOctets, sizeof(compOctets));
153 name::Component comp;
154 BOOST_REQUIRE_NO_THROW(comp.wireDecode(compBlock));
155 BOOST_CHECK_EQUAL(comp.value_size(), 0);
156
157 static const uint8_t nameOctets[] {0x07, 0x08, 0x08, 0x01, 0x41, 0x08, 0x00, 0x08, 0x01, 0x42};
158 Block nameBlock(nameOctets, sizeof(nameOctets));
159 static const std::string nameUri("/A/.../B");
160 Name name;
161 BOOST_REQUIRE_NO_THROW(name.wireDecode(nameBlock));
162 BOOST_CHECK_EQUAL(name.toUri(), nameUri);
163 Block nameEncoded = name.wireEncode();
164 BOOST_CHECK(nameEncoded == nameBlock);
165
166 Name name2;
Alexander Afanasyevc89efb42015-02-10 18:26:42 -0800167 BOOST_REQUIRE_NO_THROW(name2 = std::move(Name(nameUri)));
Alexander Afanasyevd7eacc72015-04-03 13:06:26 -0700168 BOOST_CHECK_EQUAL(name2.toUri(), nameUri);
Junxiao Shi2586be42015-01-30 20:13:01 -0700169 Block name2Encoded = name2.wireEncode();
170 BOOST_CHECK(name2Encoded == nameBlock);
171}
172
Steve DiBenedettoc145d492014-03-11 16:35:45 -0600173BOOST_AUTO_TEST_CASE(AppendNumber)
174{
175 Name name;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700176 for (uint32_t i = 0; i < 10; i++)
Steve DiBenedettoc145d492014-03-11 16:35:45 -0600177 {
178 name.appendNumber(i);
179 }
180
181 BOOST_CHECK_EQUAL(name.size(), 10);
182
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700183 for (uint32_t i = 0; i < 10; i++)
Steve DiBenedettoc145d492014-03-11 16:35:45 -0600184 {
185 BOOST_CHECK_EQUAL(name[i].toNumber(), i);
186 }
187}
188
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700189class Numeric
190{
191public:
192 typedef std::list<boost::tuple<function<name::Component(uint64_t)>,
193 function<uint64_t(const name::Component&)>,
194 function<Name&(Name&, uint64_t)>,
195 Name/*expected*/,
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700196 uint64_t/*value*/,
197 function<bool(const name::Component&)> > > Dataset;
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700198
199 Numeric()
200 {
201 dataset.push_back(boost::make_tuple(bind(&name::Component::fromNumberWithMarker,
202 0xAA, _1),
203 bind(&name::Component::toNumberWithMarker, _1, 0xAA),
204 bind(&Name::appendNumberWithMarker, _1, 0xAA, _2),
205 Name("/%AA%03%E8"),
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700206 1000,
207 bind(&name::Component::isNumberWithMarker, _1, 0xAA)));
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700208 dataset.push_back(boost::make_tuple(&name::Component::fromSegment,
209 bind(&name::Component::toSegment, _1),
210 bind(&Name::appendSegment, _1, _2),
211 Name("/%00%27%10"),
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700212 10000,
213 bind(&name::Component::isSegment, _1)));
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700214 dataset.push_back(boost::make_tuple(&name::Component::fromSegmentOffset,
215 bind(&name::Component::toSegmentOffset, _1),
216 bind(&Name::appendSegmentOffset, _1, _2),
217 Name("/%FB%00%01%86%A0"),
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700218 100000,
219 bind(&name::Component::isSegmentOffset, _1)));
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700220 dataset.push_back(boost::make_tuple(&name::Component::fromVersion,
221 bind(&name::Component::toVersion, _1),
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +0200222 bind(static_cast<Name&(Name::*)(uint64_t)>(
223 &Name::appendVersion), _1, _2),
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700224 Name("/%FD%00%0FB%40"),
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700225 1000000,
226 bind(&name::Component::isVersion, _1)));
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700227 dataset.push_back(boost::make_tuple(&name::Component::fromSequenceNumber,
228 bind(&name::Component::toSequenceNumber, _1),
229 bind(&Name::appendSequenceNumber, _1, _2),
230 Name("/%FE%00%98%96%80"),
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700231 10000000,
232 bind(&name::Component::isSequenceNumber, _1)));
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700233 }
234
235 Dataset dataset;
236};
237
238class Timestamp
239{
240public:
241 typedef std::list<boost::tuple<function<name::Component(const time::system_clock::TimePoint&)>,
242 function<time::system_clock::TimePoint(const name::Component&)>,
243 function<Name&(Name&, const time::system_clock::TimePoint&)>,
244 Name/*expected*/,
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700245 time::system_clock::TimePoint/*value*/,
246 function<bool(const name::Component&)> > > Dataset;
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700247 Timestamp()
248 {
249 dataset.push_back(boost::make_tuple(&name::Component::fromTimestamp,
Alexander Afanasyev6f9ec932014-10-31 10:34:00 -0700250 ndn::bind(&name::Component::toTimestamp, _1),
251 ndn::bind(&Name::appendTimestamp, _1, _2),
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700252 Name("/%FC%00%04%7BE%E3%1B%00%00"),
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +0200253 time::getUnixEpoch() + time::days(14600/*40 years*/),
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700254 bind(&name::Component::isTimestamp, _1)));
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700255 }
256
257 Dataset dataset;
258};
259
260typedef boost::mpl::vector<Numeric, Timestamp> ConventionsDatasets;
261
262BOOST_FIXTURE_TEST_CASE_TEMPLATE(NamingConventions, T, ConventionsDatasets, T)
263{
264 // // These octets are obtained by the snippet below.
265 // // This check is intended to detect unexpected encoding change in the future.
266 // for (typename T::Dataset::const_iterator it = this->dataset.begin();
267 // it != this->dataset.end(); ++it) {
268 // Name name;
269 // name.append(it->template get<0>()(it->template get<4>()));
270 // std::cout << name << std::endl;
271 // }
272
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700273 name::Component invalidComponent1;
274 name::Component invalidComponent2("1234567890");
275
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700276 for (typename T::Dataset::const_iterator it = this->dataset.begin();
277 it != this->dataset.end(); ++it) {
278 const Name& expected = it->template get<3>();
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700279 BOOST_TEST_MESSAGE("Check " << expected[0].toUri());
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700280
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700281 BOOST_CHECK_EQUAL(expected[0].isGeneric(), true);
282
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700283 name::Component actualComponent = it->template get<0>()(it->template get<4>());
284 BOOST_CHECK_EQUAL(actualComponent, expected[0]);
285
286 Name actualName;
287 it->template get<2>()(actualName, it->template get<4>());
288 BOOST_CHECK_EQUAL(actualName, expected);
289
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700290 BOOST_CHECK_EQUAL(it->template get<5>()(expected[0]), true);
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700291 BOOST_REQUIRE_NO_THROW(it->template get<1>()(expected[0]));
292 BOOST_CHECK_EQUAL(it->template get<1>()(expected[0]), it->template get<4>());
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700293
294 BOOST_CHECK_EQUAL(it->template get<5>()(invalidComponent1), false);
295 BOOST_CHECK_EQUAL(it->template get<5>()(invalidComponent2), false);
296
297 BOOST_REQUIRE_THROW(it->template get<1>()(invalidComponent1), name::Component::Error);
298 BOOST_REQUIRE_THROW(it->template get<1>()(invalidComponent2), name::Component::Error);
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700299 }
300}
301
Shuo Chen5aa8c742014-06-22 15:00:02 +0800302BOOST_AUTO_TEST_CASE(GetSuccessor)
303{
304 BOOST_CHECK_EQUAL(Name("ndn:/%00%01/%01%02").getSuccessor(), Name("ndn:/%00%01/%01%03"));
305 BOOST_CHECK_EQUAL(Name("ndn:/%00%01/%01%FF").getSuccessor(), Name("ndn:/%00%01/%02%00"));
306 BOOST_CHECK_EQUAL(Name("ndn:/%00%01/%FF%FF").getSuccessor(), Name("ndn:/%00%01/%00%00%00"));
307 BOOST_CHECK_EQUAL(Name().getSuccessor(), Name("ndn:/%00"));
308}
309
Junxiao Shi937e4612014-10-22 15:39:07 -0700310BOOST_AUTO_TEST_CASE(Markers)
311{
312 Name name;
313 uint64_t number;
314
315 BOOST_REQUIRE_NO_THROW(number = name.appendSegment(30923).at(-1).toSegment());
316 BOOST_CHECK_EQUAL(number, 30923);
317
318 BOOST_REQUIRE_NO_THROW(number = name.appendSegmentOffset(589).at(-1).toSegmentOffset());
319 BOOST_CHECK_EQUAL(number, 589);
320
321 BOOST_REQUIRE_NO_THROW(number = name.appendVersion().at(-1).toVersion());
322
323 BOOST_REQUIRE_NO_THROW(number = name.appendVersion(25912).at(-1).toVersion());
324 BOOST_CHECK_EQUAL(number, 25912);
325
326 const time::system_clock::TimePoint tp = time::system_clock::now();
327 time::system_clock::TimePoint tp2;
328 BOOST_REQUIRE_NO_THROW(tp2 = name.appendTimestamp(tp).at(-1).toTimestamp());
329 BOOST_CHECK_LE(std::abs(time::duration_cast<time::microseconds>(tp2 - tp).count()), 1);
330
331 BOOST_REQUIRE_NO_THROW(number = name.appendSequenceNumber(11676).at(-1).toSequenceNumber());
332 BOOST_CHECK_EQUAL(number, 11676);
333}
334
Yingdi Yu90e23582014-11-06 14:21:04 -0800335BOOST_AUTO_TEST_CASE(UnorderedMap)
336{
337 std::unordered_map<Name, int> map;
338 Name name1("/1");
339 Name name2("/2");
340 Name name3("/3");
341 map[name1] = 1;
342 map[name2] = 2;
343 map[name3] = 3;
344
345 BOOST_CHECK_EQUAL(map[name1], 1);
346 BOOST_CHECK_EQUAL(map[name2], 2);
347 BOOST_CHECK_EQUAL(map[name3], 3);
348}
349
Junxiao Shi6bf0adf2015-02-15 13:54:56 -0700350BOOST_AUTO_TEST_CASE(ImplicitSha256Digest)
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700351{
352 Name n;
353
Junxiao Shi6bf0adf2015-02-15 13:54:56 -0700354 static const uint8_t DIGEST[] = {0x28, 0xba, 0xd4, 0xb5, 0x27, 0x5b, 0xd3, 0x92,
355 0xdb, 0xb6, 0x70, 0xc7, 0x5c, 0xf0, 0xb6, 0x6f,
356 0x13, 0xf7, 0x94, 0x2b, 0x21, 0xe8, 0x0f, 0x55,
357 0xc0, 0xe8, 0x6b, 0x37, 0x47, 0x53, 0xa5, 0x48,
358 0x00, 0x00};
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700359
360 BOOST_REQUIRE_NO_THROW(n.appendImplicitSha256Digest(DIGEST, 32));
361 BOOST_REQUIRE_NO_THROW(n.appendImplicitSha256Digest(make_shared<Buffer>(DIGEST, 32)));
362 BOOST_CHECK_EQUAL(n.get(0), n.get(1));
363
364 BOOST_REQUIRE_THROW(n.appendImplicitSha256Digest(DIGEST, 34), name::Component::Error);
365 BOOST_REQUIRE_THROW(n.appendImplicitSha256Digest(DIGEST, 30), name::Component::Error);
366
367 n.append(DIGEST, 32);
368 BOOST_CHECK_LT(n.get(0), n.get(2));
369 BOOST_CHECK_EQUAL_COLLECTIONS(n.get(0).value_begin(), n.get(0).value_end(),
370 n.get(2).value_begin(), n.get(2).value_end());
371
372 n.append(DIGEST + 1, 32);
373 BOOST_CHECK_LT(n.get(0), n.get(3));
374
375 n.append(DIGEST + 2, 32);
376 BOOST_CHECK_LT(n.get(0), n.get(4));
377
378 BOOST_CHECK_EQUAL(n.get(0).toUri(), "sha256digest="
379 "28bad4b5275bd392dbb670c75cf0b66f13f7942b21e80f55c0e86b374753a548");
380
381 BOOST_CHECK_EQUAL(n.get(0).isImplicitSha256Digest(), true);
382 BOOST_CHECK_EQUAL(n.get(2).isImplicitSha256Digest(), false);
383
384 BOOST_CHECK_THROW(Name("/hello/sha256digest=hmm"), name::Component::Error);
385
386 Name n2;
387 // check canonical URI encoding (lower case)
388 BOOST_CHECK_NO_THROW(n2 = Name("/hello/sha256digest="
389 "28bad4b5275bd392dbb670c75cf0b66f13f7942b21e80f55c0e86b374753a548"));
390 BOOST_CHECK_EQUAL(n.get(0), n2.get(1));
391
392 // will accept hex value in upper case too
393 BOOST_CHECK_NO_THROW(n2 = Name("/hello/sha256digest="
394 "28BAD4B5275BD392DBB670C75CF0B66F13F7942B21E80F55C0E86B374753A548"));
395 BOOST_CHECK_EQUAL(n.get(0), n2.get(1));
396
397 // this is not valid sha256digest component, will be treated as generic component
398 BOOST_CHECK_NO_THROW(n2 = Name("/hello/SHA256DIGEST="
399 "28BAD4B5275BD392DBB670C75CF0B66F13F7942B21E80F55C0E86B374753A548"));
400 BOOST_CHECK_NE(n.get(0), n2.get(1));
Junxiao Shi6bf0adf2015-02-15 13:54:56 -0700401 BOOST_CHECK(n2.get(1).isGeneric());
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700402}
403
Junxiao Shia6452ac2015-01-23 11:21:06 -0700404BOOST_AUTO_TEST_CASE(Compare)
405{
Marcin Juszkiewicz1ddc13d2015-11-16 10:20:44 +0100406 BOOST_CHECK_EQUAL(Name("/A") .compare(Name("/A")), 0);
407 BOOST_CHECK_EQUAL(Name("/A") .compare(Name("/A")), 0);
408 BOOST_CHECK_LT (Name("/A") .compare(Name("/B")), 0);
409 BOOST_CHECK_GT (Name("/B") .compare(Name("/A")), 0);
410 BOOST_CHECK_LT (Name("/A") .compare(Name("/AA")), 0);
411 BOOST_CHECK_GT (Name("/AA") .compare(Name("/A")), 0);
412 BOOST_CHECK_LT (Name("/A") .compare(Name("/A/C")), 0);
413 BOOST_CHECK_GT (Name("/A/C").compare(Name("/A")), 0);
Junxiao Shia6452ac2015-01-23 11:21:06 -0700414
Marcin Juszkiewicz1ddc13d2015-11-16 10:20:44 +0100415 BOOST_CHECK_EQUAL(Name("/Z/A/Y") .compare(1, 1, Name("/A")), 0);
416 BOOST_CHECK_EQUAL(Name("/Z/A/Y") .compare(1, 1, Name("/A")), 0);
417 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/B")), 0);
418 BOOST_CHECK_GT (Name("/Z/B/Y") .compare(1, 1, Name("/A")), 0);
419 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/AA")), 0);
420 BOOST_CHECK_GT (Name("/Z/AA/Y") .compare(1, 1, Name("/A")), 0);
421 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/A/C")), 0);
422 BOOST_CHECK_GT (Name("/Z/A/C/Y").compare(1, 2, Name("/A")), 0);
Junxiao Shia6452ac2015-01-23 11:21:06 -0700423
Marcin Juszkiewicz1ddc13d2015-11-16 10:20:44 +0100424 BOOST_CHECK_EQUAL(Name("/Z/A") .compare(1, Name::npos, Name("/A")), 0);
425 BOOST_CHECK_EQUAL(Name("/Z/A") .compare(1, Name::npos, Name("/A")), 0);
426 BOOST_CHECK_LT (Name("/Z/A") .compare(1, Name::npos, Name("/B")), 0);
427 BOOST_CHECK_GT (Name("/Z/B") .compare(1, Name::npos, Name("/A")), 0);
428 BOOST_CHECK_LT (Name("/Z/A") .compare(1, Name::npos, Name("/AA")), 0);
429 BOOST_CHECK_GT (Name("/Z/AA") .compare(1, Name::npos, Name("/A")), 0);
430 BOOST_CHECK_LT (Name("/Z/A") .compare(1, Name::npos, Name("/A/C")), 0);
431 BOOST_CHECK_GT (Name("/Z/A/C").compare(1, Name::npos, Name("/A")), 0);
Junxiao Shia6452ac2015-01-23 11:21:06 -0700432
Marcin Juszkiewicz1ddc13d2015-11-16 10:20:44 +0100433 BOOST_CHECK_EQUAL(Name("/Z/A/Y") .compare(1, 1, Name("/X/A/W"), 1, 1), 0);
434 BOOST_CHECK_EQUAL(Name("/Z/A/Y") .compare(1, 1, Name("/X/A/W"), 1, 1), 0);
435 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/B/W"), 1, 1), 0);
436 BOOST_CHECK_GT (Name("/Z/B/Y") .compare(1, 1, Name("/X/A/W"), 1, 1), 0);
437 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/AA/W"), 1, 1), 0);
438 BOOST_CHECK_GT (Name("/Z/AA/Y") .compare(1, 1, Name("/X/A/W"), 1, 1), 0);
439 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/A/C/W"), 1, 2), 0);
440 BOOST_CHECK_GT (Name("/Z/A/C/Y").compare(1, 2, Name("/X/A/W"), 1, 1), 0);
Junxiao Shia6452ac2015-01-23 11:21:06 -0700441
Marcin Juszkiewicz1ddc13d2015-11-16 10:20:44 +0100442 BOOST_CHECK_EQUAL(Name("/Z/A/Y") .compare(1, 1, Name("/X/A"), 1), 0);
443 BOOST_CHECK_EQUAL(Name("/Z/A/Y") .compare(1, 1, Name("/X/A"), 1), 0);
444 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/B"), 1), 0);
445 BOOST_CHECK_GT (Name("/Z/B/Y") .compare(1, 1, Name("/X/A"), 1), 0);
446 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/AA"), 1), 0);
447 BOOST_CHECK_GT (Name("/Z/AA/Y") .compare(1, 1, Name("/X/A"), 1), 0);
448 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/A/C"), 1), 0);
449 BOOST_CHECK_GT (Name("/Z/A/C/Y").compare(1, 2, Name("/X/A"), 1), 0);
Junxiao Shia6452ac2015-01-23 11:21:06 -0700450}
451
Junxiao Shi2586be42015-01-30 20:13:01 -0700452BOOST_AUTO_TEST_CASE(ZeroLengthComponentCompare)
453{
454 name::Component comp0("");
455 BOOST_REQUIRE_EQUAL(comp0.value_size(), 0);
456
457 BOOST_CHECK_EQUAL(comp0, comp0);
458 BOOST_CHECK_EQUAL(comp0, name::Component(""));
459 BOOST_CHECK_LT(comp0, name::Component("A"));
460 BOOST_CHECK_LE(comp0, name::Component("A"));
461 BOOST_CHECK_NE(comp0, name::Component("A"));
462 BOOST_CHECK_GT(name::Component("A"), comp0);
463 BOOST_CHECK_GE(name::Component("A"), comp0);
464}
465
Alexander Afanasyev74633892015-02-08 18:08:46 -0800466BOOST_AUTO_TEST_CASE(CreateComponentWithIterators) // Bug #2490
467{
468 {
469 std::vector<uint8_t> bytes = {1};
470 name::Component c(bytes.begin(), bytes.end());
471 BOOST_CHECK_EQUAL(c.value_size(), 1);
472 BOOST_CHECK_EQUAL(c.size(), 3);
473 }
474
475 {
476 std::list<uint8_t> bytes = {1, 2, 3, 4};
477 name::Component c(bytes.begin(), bytes.end());
478 BOOST_CHECK_EQUAL(c.value_size(), 4);
479 BOOST_CHECK_EQUAL(c.size(), 6);
480 }
481
482 {
483 std::vector<int8_t> bytes = {1};
484 name::Component c(bytes.begin(), bytes.end());
485 BOOST_CHECK_EQUAL(c.value_size(), 1);
486 BOOST_CHECK_EQUAL(c.size(), 3);
487 }
488
489 {
490 std::list<int8_t> bytes = {1, 2, 3, 4};
491 name::Component c(bytes.begin(), bytes.end());
492 BOOST_CHECK_EQUAL(c.value_size(), 4);
493 BOOST_CHECK_EQUAL(c.size(), 6);
494 }
495}
496
Alexander Afanasyevd7eacc72015-04-03 13:06:26 -0700497BOOST_AUTO_TEST_CASE(NameWithSpaces)
498{
499 Name name("/ hello\t/\tworld \r\n");
500
501 BOOST_CHECK_EQUAL("/hello/world", name);
502 BOOST_CHECK_THROW(Name("/hello//world"), name::Component::Error);
503}
504
Joao Pereira6f7cfd02015-06-15 11:36:26 -0400505BOOST_AUTO_TEST_CASE(Append)
506{
507 PartialName toAppend("/and");
508 PartialName toAppend1("/beyond");
509 {
510 Name name("/hello/world");
511 BOOST_CHECK_EQUAL("/hello/world/hello/world", name.append(name));
512 BOOST_CHECK_EQUAL("/hello/world/hello/world", name);
513 }
514 {
515 Name name("/hello/world");
516 BOOST_CHECK_EQUAL("/hello/world/and", name.append(toAppend));
517 }
518 {
519 Name name("/hello/world");
520 BOOST_CHECK_EQUAL("/hello/world/and/beyond", name.append(toAppend).append(toAppend1));
521 }
522}
523
524BOOST_AUTO_TEST_CASE(SubName)
525{
526 Name name("/hello/world");
527
528 BOOST_CHECK_EQUAL("/hello/world", name.getSubName(0));
529 BOOST_CHECK_EQUAL("/world", name.getSubName(1));
530 BOOST_CHECK_EQUAL("/hello/", name.getSubName(0, 1));
531}
532
533BOOST_AUTO_TEST_CASE(SubNameNegativeIndex)
534{
535 Name name("/first/second/third/last");
536
537 BOOST_CHECK_EQUAL("/last", name.getSubName(-1));
538 BOOST_CHECK_EQUAL("/third/last", name.getSubName(-2));
539 BOOST_CHECK_EQUAL("/second", name.getSubName(-3, 1));
540}
541
542BOOST_AUTO_TEST_CASE(SubNameOutOfRangeIndexes)
543{
544 Name name("/first/second/last");
545 // No length
546 BOOST_CHECK_EQUAL("/first/second/last", name.getSubName(-10));
547 BOOST_CHECK_EQUAL("/", name.getSubName(10));
548
549 // Starting after the max position
550 BOOST_CHECK_EQUAL("/", name.getSubName(10, 1));
551 BOOST_CHECK_EQUAL("/", name.getSubName(10, 10));
552
553 // Not enough components
554 BOOST_CHECK_EQUAL("/second/last", name.getSubName(1, 10));
555 BOOST_CHECK_EQUAL("/last", name.getSubName(-1, 10));
556
557 // Start before first
558 BOOST_CHECK_EQUAL("/first/second", name.getSubName(-10, 2));
559 BOOST_CHECK_EQUAL("/first/second/last", name.getSubName(-10, 10));
560}
Alexander Afanasyevd7eacc72015-04-03 13:06:26 -0700561
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800562BOOST_AUTO_TEST_SUITE_END()
563
Junxiao Shi6bf0adf2015-02-15 13:54:56 -0700564} // namespace tests
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800565} // namespace ndn