blob: 27b0255a6d3777cca5e9a6a4ff401484d107fccc [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi71ff2312017-07-12 13:32:50 +00002/*
Junxiao Shicf4ac5b2018-03-28 22:46:06 +00003 * Copyright (c) 2013-2018 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
Junxiao Shia36f15d2018-04-04 02:22:11 +000024#include "block-literal.hpp"
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070025#include "boost-test.hpp"
Yingdi Yu90e23582014-11-06 14:21:04 -080026#include <unordered_map>
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070027
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080028namespace ndn {
Junxiao Shi6bf0adf2015-02-15 13:54:56 -070029namespace tests {
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080030
Junxiao Shia36f15d2018-04-04 02:22:11 +000031using Component = name::Component;
32
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080033BOOST_AUTO_TEST_SUITE(TestName)
34
Junxiao Shia36f15d2018-04-04 02:22:11 +000035// ---- encoding, decoding, and URI ----
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080036
Junxiao Shia36f15d2018-04-04 02:22:11 +000037BOOST_AUTO_TEST_CASE(EncodeDecode)
Alexander Afanasyevc2344292014-03-02 00:08:00 +000038{
Junxiao Shia36f15d2018-04-04 02:22:11 +000039 std::string uri = "/Emid/25042=P3/.../..../%1C%9F/"
40 "sha256digest=0415e3624a151850ac686c84f155f29808c0dd73819aa4a4c20be73a4d8a874c";
41 Name name(uri);
42 BOOST_CHECK_EQUAL(name.size(), 6);
43 BOOST_CHECK_EQUAL(name[0], Component("Emid"));
44 BOOST_CHECK_EQUAL(name[1], Component("FD61D2025033"_block));
45 BOOST_CHECK_EQUAL(name[2], Component(""));
46 BOOST_CHECK_EQUAL(name[3], Component("."));
47 BOOST_CHECK_EQUAL(name[4], Component("\x1C\x9F"));
48 BOOST_CHECK(name[5].isImplicitSha256Digest());
Alexander Afanasyevc2344292014-03-02 00:08:00 +000049
Junxiao Shia36f15d2018-04-04 02:22:11 +000050 Block wire = name.wireEncode();
51 BOOST_CHECK(wire == "0737 0804456D6964 FD61D2025033 0800 08012E 08021C9F "
52 "01200415E3624A151850AC686C84F155F29808C0DD73819AA4A4C20BE73A4D8A874C"_block);
Alexander Afanasyevc2344292014-03-02 00:08:00 +000053
Junxiao Shia36f15d2018-04-04 02:22:11 +000054 Name decoded(wire);
55 BOOST_CHECK_EQUAL(decoded, name);
Alexander Afanasyev6486d522014-10-23 14:14:11 -070056}
57
Alexander Afanasyevd7eacc72015-04-03 13:06:26 -070058BOOST_AUTO_TEST_CASE(NameWithSpaces)
59{
60 Name name("/ hello\t/\tworld \r\n");
61
62 BOOST_CHECK_EQUAL("/hello/world", name);
63 BOOST_CHECK_THROW(Name("/hello//world"), name::Component::Error);
64}
65
Junxiao Shi71ff2312017-07-12 13:32:50 +000066BOOST_AUTO_TEST_CASE(DeepCopy)
67{
68 Name n1("/hello/world");
69 Name n2 = n1.deepCopy();
70
71 BOOST_CHECK_EQUAL(n1, n2);
72 BOOST_CHECK_NE(&n1.wireEncode(), &n2.wireEncode());
73
74 EncodingBuffer buffer(1024, 0);
75 n1.wireEncode(buffer);
76 Name n3(buffer.block());
77
78 BOOST_CHECK_EQUAL(n1, n3);
79 BOOST_CHECK_EQUAL(n3.wireEncode().getBuffer()->size(), 1024);
80 n3 = n3.deepCopy();
81
82 BOOST_CHECK_LT(n3.wireEncode().size(), 1024);
83 BOOST_CHECK_EQUAL(n3.wireEncode().getBuffer()->size(), n3.wireEncode().size());
84}
85
Junxiao Shia36f15d2018-04-04 02:22:11 +000086// ---- access ----
87
88BOOST_AUTO_TEST_CASE(At)
89{
90 Name name("/hello/5=NDN");
91
92 BOOST_CHECK_EQUAL(name.at(0), name::Component("080568656C6C6F"_block));
93 BOOST_CHECK_EQUAL(name.at(1), name::Component("05034E444E"_block));
94 BOOST_CHECK_EQUAL(name.at(-1), name::Component("05034E444E"_block));
95 BOOST_CHECK_EQUAL(name.at(-2), name::Component("080568656C6C6F"_block));
96
97 BOOST_CHECK_THROW(name.at(2), Name::Error);
98 BOOST_CHECK_THROW(name.at(-3), Name::Error);
99}
100
101BOOST_AUTO_TEST_CASE(SubName)
102{
103 Name name("/hello/world");
104
105 BOOST_CHECK_EQUAL("/hello/world", name.getSubName(0));
106 BOOST_CHECK_EQUAL("/world", name.getSubName(1));
107 BOOST_CHECK_EQUAL("/hello/", name.getSubName(0, 1));
108}
109
110BOOST_AUTO_TEST_CASE(SubNameNegativeIndex)
111{
112 Name name("/first/second/third/last");
113
114 BOOST_CHECK_EQUAL("/last", name.getSubName(-1));
115 BOOST_CHECK_EQUAL("/third/last", name.getSubName(-2));
116 BOOST_CHECK_EQUAL("/second", name.getSubName(-3, 1));
117}
118
119BOOST_AUTO_TEST_CASE(SubNameOutOfRangeIndexes)
120{
121 Name name("/first/second/last");
122 // No length
123 BOOST_CHECK_EQUAL("/first/second/last", name.getSubName(-10));
124 BOOST_CHECK_EQUAL("/", name.getSubName(10));
125
126 // Starting after the max position
127 BOOST_CHECK_EQUAL("/", name.getSubName(10, 1));
128 BOOST_CHECK_EQUAL("/", name.getSubName(10, 10));
129
130 // Not enough components
131 BOOST_CHECK_EQUAL("/second/last", name.getSubName(1, 10));
132 BOOST_CHECK_EQUAL("/last", name.getSubName(-1, 10));
133
134 // Start before first
135 BOOST_CHECK_EQUAL("/first/second", name.getSubName(-10, 2));
136 BOOST_CHECK_EQUAL("/first/second/last", name.getSubName(-10, 10));
137}
138
Junxiao Shi71ff2312017-07-12 13:32:50 +0000139// ---- iterators ----
140
141BOOST_AUTO_TEST_CASE(ForwardIterator)
142{
143 name::Component comps[] {
144 name::Component("A"),
145 name::Component("B"),
146 name::Component("C"),
147 name::Component("D")
148 };
149
150 Name n0;
151 BOOST_CHECK_EQUAL_COLLECTIONS(n0.begin(), n0.end(), comps, comps + 0);
152
153 Name n4("/A/B/C/D");
154 BOOST_CHECK_EQUAL_COLLECTIONS(n4.begin(), n4.end(), comps, comps + 4);
155}
156
157BOOST_AUTO_TEST_CASE(ReverseIterator)
158{
159 name::Component comps[] {
160 name::Component("D"),
161 name::Component("C"),
162 name::Component("B"),
163 name::Component("A")
164 };
165
166 Name n0;
167 BOOST_CHECK_EQUAL_COLLECTIONS(n0.rbegin(), n0.rend(), comps, comps + 0);
168
169 Name n4("/A/B/C/D");
170 BOOST_CHECK_EQUAL_COLLECTIONS(n4.rbegin(), n4.rend(), comps, comps + 4);
171}
172
173// ---- modifiers ----
174
Junxiao Shia36f15d2018-04-04 02:22:11 +0000175BOOST_AUTO_TEST_CASE(AppendComponent)
Joao Pereira6f7cfd02015-06-15 11:36:26 -0400176{
Junxiao Shia36f15d2018-04-04 02:22:11 +0000177 Name name;
178 BOOST_CHECK(name.wireEncode() == "0700"_block);
Ashlesh Gawande7dffab62017-12-21 16:40:53 -0600179
Junxiao Shia36f15d2018-04-04 02:22:11 +0000180 name.append(Component("Emid"));
181 BOOST_CHECK(name.wireEncode() == "0706 0804456D6964"_block);
182
183 name.append(25042, reinterpret_cast<const uint8_t*>("P3"), 2);
184 BOOST_CHECK(name.wireEncode() == "070C 0804456D6964 FD61D2025033"_block);
185
186 name.append(reinterpret_cast<const uint8_t*>("."), 1);
187 BOOST_CHECK(name.wireEncode() == "070F 0804456D6964 FD61D2025033 08012E"_block);
188
189 std::vector<uint8_t> v1{0x28, 0xF0, 0xA3, 0x6B};
190 name.append(16, v1.begin(), v1.end());
191 BOOST_CHECK(name.wireEncode() == "0715 0804456D6964 FD61D2025033 08012E 100428F0A36B"_block);
192
193 BOOST_CHECK(!name.empty());
194 name.clear();
195 BOOST_CHECK(name.empty());
196 BOOST_CHECK(name.wireEncode() == "0700"_block);
197
198 name.append(v1.begin(), v1.end());
199 BOOST_CHECK(name.wireEncode() == "0706 080428F0A36B"_block);
200
201 name.append("xKh");
202 BOOST_CHECK(name.wireEncode() == "070B 080428F0A36B 0803784B68"_block);
203
204 name.append("0100"_block);
205 BOOST_CHECK(name.wireEncode() == "070F 080428F0A36B 0803784B68 08020100"_block);
206
207 name.append("080109"_block);
208 BOOST_CHECK(name.wireEncode() == "0712 080428F0A36B 0803784B68 08020100 080109"_block);
Joao Pereira6f7cfd02015-06-15 11:36:26 -0400209}
210
Junxiao Shia36f15d2018-04-04 02:22:11 +0000211BOOST_AUTO_TEST_CASE(AppendPartialName)
Junxiao Shi71ff2312017-07-12 13:32:50 +0000212{
Junxiao Shia36f15d2018-04-04 02:22:11 +0000213 Name name("/A/B");
214 name.append(PartialName("/6=C/D"))
215 .append(PartialName("/E"));
216 BOOST_CHECK(name.wireEncode() == "070F 080141 080142 060143 080144 080145"_block);
Junxiao Shi71ff2312017-07-12 13:32:50 +0000217}
218
219BOOST_AUTO_TEST_CASE(AppendNumber)
220{
221 Name name;
222 for (uint32_t i = 0; i < 10; i++) {
223 name.appendNumber(i);
224 }
Junxiao Shi71ff2312017-07-12 13:32:50 +0000225 BOOST_CHECK_EQUAL(name.size(), 10);
226
227 for (uint32_t i = 0; i < 10; i++) {
228 BOOST_CHECK_EQUAL(name[i].toNumber(), i);
229 }
230}
231
232BOOST_AUTO_TEST_CASE(Markers)
233{
234 // TestNameComponent/NamingConvention provides additional coverage for these methods,
235 // including verifications of the wire format.
236
237 Name name;
238 uint64_t number;
239
240 BOOST_REQUIRE_NO_THROW(number = name.appendSegment(30923).at(-1).toSegment());
241 BOOST_CHECK_EQUAL(number, 30923);
242
243 BOOST_REQUIRE_NO_THROW(number = name.appendSegmentOffset(589).at(-1).toSegmentOffset());
244 BOOST_CHECK_EQUAL(number, 589);
245
246 BOOST_REQUIRE_NO_THROW(number = name.appendVersion().at(-1).toVersion());
247
248 BOOST_REQUIRE_NO_THROW(number = name.appendVersion(25912).at(-1).toVersion());
249 BOOST_CHECK_EQUAL(number, 25912);
250
251 const time::system_clock::TimePoint tp = time::system_clock::now();
252 time::system_clock::TimePoint tp2;
253 BOOST_REQUIRE_NO_THROW(tp2 = name.appendTimestamp(tp).at(-1).toTimestamp());
254 BOOST_CHECK_LE(std::abs(time::duration_cast<time::microseconds>(tp2 - tp).count()), 1);
255
256 BOOST_REQUIRE_NO_THROW(number = name.appendSequenceNumber(11676).at(-1).toSequenceNumber());
257 BOOST_CHECK_EQUAL(number, 11676);
258}
259
Junxiao Shi71ff2312017-07-12 13:32:50 +0000260// ---- algorithms ----
261
262BOOST_AUTO_TEST_CASE(GetSuccessor)
263{
Junxiao Shia36f15d2018-04-04 02:22:11 +0000264 BOOST_CHECK_EQUAL(Name("/%00%01/%01%02").getSuccessor(), Name("/%00%01/%01%03"));
265 BOOST_CHECK_EQUAL(Name("/%00%01/%01%FF").getSuccessor(), Name("/%00%01/%02%00"));
266 BOOST_CHECK_EQUAL(Name("/%00%01/%FF%FF").getSuccessor(), Name("/%00%01/%00%00%00"));
267 BOOST_CHECK_EQUAL(Name().getSuccessor(), Name("/%00"));
Junxiao Shi71ff2312017-07-12 13:32:50 +0000268}
269
Junxiao Shia36f15d2018-04-04 02:22:11 +0000270BOOST_AUTO_TEST_CASE(IsPrefixOf)
271{
272 BOOST_CHECK(Name("/").isPrefixOf("/"));
273 BOOST_CHECK(Name("/").isPrefixOf("/sha256digest=0000000000000000000000000000000000000000000000000000000000000000"));
274 BOOST_CHECK(Name("/").isPrefixOf("/2=D"));
275 BOOST_CHECK(Name("/").isPrefixOf("/F"));
276 BOOST_CHECK(Name("/").isPrefixOf("/21426=AA"));
277
278 BOOST_CHECK(Name("/B").isPrefixOf("/B"));
279 BOOST_CHECK(Name("/B").isPrefixOf("/B/sha256digest=0000000000000000000000000000000000000000000000000000000000000000"));
280 BOOST_CHECK(Name("/B").isPrefixOf("/B/2=D"));
281 BOOST_CHECK(Name("/B").isPrefixOf("/B/F"));
282 BOOST_CHECK(Name("/B").isPrefixOf("/B/21426=AA"));
283
284 BOOST_CHECK(!Name("/C").isPrefixOf("/"));
285 BOOST_CHECK(!Name("/C").isPrefixOf("/sha256digest=0000000000000000000000000000000000000000000000000000000000000000"));
286 BOOST_CHECK(!Name("/C").isPrefixOf("/2=D"));
287 BOOST_CHECK(!Name("/C").isPrefixOf("/F"));
288 BOOST_CHECK(!Name("/C").isPrefixOf("/21426=AA"));
289}
290
291BOOST_AUTO_TEST_CASE(CompareOp)
292{
293 std::vector<Name> names = {
294 Name("/"),
295 Name("/sha256digest=0000000000000000000000000000000000000000000000000000000000000000"),
296 Name("/sha256digest=0000000000000000000000000000000000000000000000000000000000000001"),
297 Name("/sha256digest=FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"),
298 Name("/2=..."),
299 Name("/2=D"),
300 Name("/2=F"),
301 Name("/2=AA"),
302 Name("/..."),
303 Name("/D"),
304 Name("/D/sha256digest=0000000000000000000000000000000000000000000000000000000000000000"),
305 Name("/D/sha256digest=0000000000000000000000000000000000000000000000000000000000000001"),
306 Name("/D/sha256digest=FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"),
307 Name("/D/2=..."),
308 Name("/D/2=D"),
309 Name("/D/2=F"),
310 Name("/D/2=AA"),
311 Name("/D/..."),
312 Name("/D/D"),
313 Name("/D/F"),
314 Name("/D/AA"),
315 Name("/D/21426=..."),
316 Name("/D/21426=D"),
317 Name("/D/21426=F"),
318 Name("/D/21426=AA"),
319 Name("/F"),
320 Name("/AA"),
321 Name("/21426=..."),
322 Name("/21426=D"),
323 Name("/21426=F"),
324 Name("/21426=AA"),
325 };
326
327 for (size_t i = 0; i < names.size(); ++i) {
328 for (size_t j = 0; j < names.size(); ++j) {
329 Name lhs = names[i];
330 Name rhs = names[j];
331 BOOST_CHECK_EQUAL(lhs == rhs, i == j);
332 BOOST_CHECK_EQUAL(lhs != rhs, i != j);
333 BOOST_CHECK_EQUAL(lhs < rhs, i < j);
334 BOOST_CHECK_EQUAL(lhs <= rhs, i <= j);
335 BOOST_CHECK_EQUAL(lhs > rhs, i > j);
336 BOOST_CHECK_EQUAL(lhs >= rhs, i >= j);
337 }
338 }
339}
340
341BOOST_AUTO_TEST_CASE(CompareFunc)
Junxiao Shi71ff2312017-07-12 13:32:50 +0000342{
343 BOOST_CHECK_EQUAL(Name("/A") .compare(Name("/A")), 0);
344 BOOST_CHECK_LT (Name("/A") .compare(Name("/B")), 0);
345 BOOST_CHECK_GT (Name("/B") .compare(Name("/A")), 0);
346 BOOST_CHECK_LT (Name("/A") .compare(Name("/AA")), 0);
347 BOOST_CHECK_GT (Name("/AA") .compare(Name("/A")), 0);
348 BOOST_CHECK_LT (Name("/A") .compare(Name("/A/C")), 0);
349 BOOST_CHECK_GT (Name("/A/C").compare(Name("/A")), 0);
350
351 BOOST_CHECK_EQUAL(Name("/Z/A/Y") .compare(1, 1, Name("/A")), 0);
352 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/B")), 0);
353 BOOST_CHECK_GT (Name("/Z/B/Y") .compare(1, 1, Name("/A")), 0);
354 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/AA")), 0);
355 BOOST_CHECK_GT (Name("/Z/AA/Y") .compare(1, 1, Name("/A")), 0);
356 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/A/C")), 0);
357 BOOST_CHECK_GT (Name("/Z/A/C/Y").compare(1, 2, Name("/A")), 0);
358
359 BOOST_CHECK_EQUAL(Name("/Z/A") .compare(1, Name::npos, Name("/A")), 0);
360 BOOST_CHECK_LT (Name("/Z/A") .compare(1, Name::npos, Name("/B")), 0);
361 BOOST_CHECK_GT (Name("/Z/B") .compare(1, Name::npos, Name("/A")), 0);
362 BOOST_CHECK_LT (Name("/Z/A") .compare(1, Name::npos, Name("/AA")), 0);
363 BOOST_CHECK_GT (Name("/Z/AA") .compare(1, Name::npos, Name("/A")), 0);
364 BOOST_CHECK_LT (Name("/Z/A") .compare(1, Name::npos, Name("/A/C")), 0);
365 BOOST_CHECK_GT (Name("/Z/A/C").compare(1, Name::npos, Name("/A")), 0);
366
367 BOOST_CHECK_EQUAL(Name("/Z/A/Y") .compare(1, 1, Name("/X/A/W"), 1, 1), 0);
368 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/B/W"), 1, 1), 0);
369 BOOST_CHECK_GT (Name("/Z/B/Y") .compare(1, 1, Name("/X/A/W"), 1, 1), 0);
370 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/AA/W"), 1, 1), 0);
371 BOOST_CHECK_GT (Name("/Z/AA/Y") .compare(1, 1, Name("/X/A/W"), 1, 1), 0);
372 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/A/C/W"), 1, 2), 0);
373 BOOST_CHECK_GT (Name("/Z/A/C/Y").compare(1, 2, Name("/X/A/W"), 1, 1), 0);
374
375 BOOST_CHECK_EQUAL(Name("/Z/A/Y") .compare(1, 1, Name("/X/A"), 1), 0);
376 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/B"), 1), 0);
377 BOOST_CHECK_GT (Name("/Z/B/Y") .compare(1, 1, Name("/X/A"), 1), 0);
378 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/AA"), 1), 0);
379 BOOST_CHECK_GT (Name("/Z/AA/Y") .compare(1, 1, Name("/X/A"), 1), 0);
380 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/A/C"), 1), 0);
381 BOOST_CHECK_GT (Name("/Z/A/C/Y").compare(1, 2, Name("/X/A"), 1), 0);
382}
383
Junxiao Shi71ff2312017-07-12 13:32:50 +0000384BOOST_AUTO_TEST_CASE(UnorderedMap)
Alexander Afanasyev4f512fb2016-05-18 10:47:53 -0700385{
Junxiao Shi71ff2312017-07-12 13:32:50 +0000386 std::unordered_map<Name, int> map;
387 Name name1("/1");
388 Name name2("/2");
389 Name name3("/3");
390 map[name1] = 1;
391 map[name2] = 2;
392 map[name3] = 3;
Alexander Afanasyev4f512fb2016-05-18 10:47:53 -0700393
Junxiao Shi71ff2312017-07-12 13:32:50 +0000394 BOOST_CHECK_EQUAL(map[name1], 1);
395 BOOST_CHECK_EQUAL(map[name2], 2);
396 BOOST_CHECK_EQUAL(map[name3], 3);
Alexander Afanasyev4f512fb2016-05-18 10:47:53 -0700397}
398
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100399BOOST_AUTO_TEST_SUITE_END() // TestName
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800400
Junxiao Shi6bf0adf2015-02-15 13:54:56 -0700401} // namespace tests
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800402} // namespace ndn