blob: 651b16098cfd44f94ed74f171ead387f88d733dc [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/*
3 * Copyright (c) 2013-2017 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
25#include "boost-test.hpp"
26#include <boost/mpl/vector.hpp>
27
28namespace ndn {
29namespace tests {
30
31BOOST_AUTO_TEST_SUITE(TestNameComponent)
32
33static const uint8_t NAME_COMPONENT_WIRE[] = {
34 0x08, 0x03, // NameComponent
35 0x6e, 0x64, 0x6e};
36
37static const uint8_t NAME_COMPONENT2_WIRE[] = {
38 0x08, 0x20, // ImplicitSha256DigestComponent
39 0x28, 0xba, 0xd4, 0xb5, 0x27, 0x5b, 0xd3, 0x92,
40 0xdb, 0xb6, 0x70, 0xc7, 0x5c, 0xf0, 0xb6, 0x6f,
41 0x13, 0xf7, 0x94, 0x2b, 0x21, 0xe8, 0x0f, 0x55,
42 0xc0, 0xe8, 0x6b, 0x37, 0x47, 0x53, 0xa5, 0x49 };
43
44static const uint8_t DIGEST_COMPONENT_WIRE[] = {
45 0x01, 0x20, // ImplicitSha256DigestComponent
46 0x28, 0xba, 0xd4, 0xb5, 0x27, 0x5b, 0xd3, 0x92,
47 0xdb, 0xb6, 0x70, 0xc7, 0x5c, 0xf0, 0xb6, 0x6f,
48 0x13, 0xf7, 0x94, 0x2b, 0x21, 0xe8, 0x0f, 0x55,
49 0xc0, 0xe8, 0x6b, 0x37, 0x47, 0x53, 0xa5, 0x48 };
50
51static const uint8_t DIGEST_COMPONENT2_WIRE[] = {
52 0x01, 0x20, // ImplicitSha256DigestComponent
53 0x28, 0xba, 0xd4, 0xb5, 0x27, 0x5b, 0xd3, 0x92,
54 0xdb, 0xb6, 0x70, 0xc7, 0x5c, 0xf0, 0xb6, 0x6f,
55 0x13, 0xf7, 0x94, 0x2b, 0x21, 0xe8, 0x0f, 0x55,
56 0xc0, 0xe8, 0x6b, 0x37, 0x47, 0x53, 0xa5, 0x49 };
57
58static const uint8_t INVALID_COMPONENT_WIRE[] = {
59 0x07, 0x03, // unknown component type
60 0x6e, 0x64, 0x6e};
61
62BOOST_AUTO_TEST_SUITE(Decode)
63
64BOOST_AUTO_TEST_CASE(Generic)
65{
66 Block block(NAME_COMPONENT_WIRE, sizeof(NAME_COMPONENT_WIRE));
67 name::Component comp;
68 BOOST_REQUIRE_NO_THROW(comp.wireDecode(block));
69 BOOST_CHECK_EQUAL(comp.toUri(), "ndn");
70}
71
72BOOST_AUTO_TEST_CASE(Digest)
73{
74 Block block(DIGEST_COMPONENT_WIRE, sizeof(DIGEST_COMPONENT_WIRE));
75 name::Component comp;
76 BOOST_REQUIRE_NO_THROW(comp.wireDecode(block));
77 BOOST_REQUIRE_EQUAL(comp.toUri(), "sha256digest=28bad4b5275bd392dbb670c75cf0b66f"
78 "13f7942b21e80f55c0e86b374753a548");
79}
80
81BOOST_AUTO_TEST_CASE(Invalid)
82{
83 Block block(INVALID_COMPONENT_WIRE, sizeof(INVALID_COMPONENT_WIRE));
84 name::Component comp;
85 BOOST_REQUIRE_THROW(comp.wireDecode(block), name::Component::Error);
86}
87
88BOOST_AUTO_TEST_SUITE_END() // Decode
89
90BOOST_AUTO_TEST_SUITE(Compare)
91
92BOOST_AUTO_TEST_CASE(Generic)
93{
94 name::Component compD("D");
95 name::Component compD2("D");
96 name::Component compF("F");
97 name::Component compAA("AA");
98
99 BOOST_CHECK_EQUAL(compD == compD2, true);
100 BOOST_CHECK_EQUAL(compD != compD2, false);
101 BOOST_CHECK_EQUAL(compD < compD2, false);
102 BOOST_CHECK_EQUAL(compD <= compD2, true);
103 BOOST_CHECK_EQUAL(compD > compD2, false);
104 BOOST_CHECK_EQUAL(compD >= compD2, true);
105
106 BOOST_CHECK_EQUAL(compD == compF, false);
107 BOOST_CHECK_EQUAL(compD != compF, true);
108 BOOST_CHECK_EQUAL(compD < compF, true);
109 BOOST_CHECK_EQUAL(compD <= compF, true);
110 BOOST_CHECK_EQUAL(compD > compF, false);
111 BOOST_CHECK_EQUAL(compD >= compF, false);
112
113 BOOST_CHECK_EQUAL(compF == compAA, false);
114 BOOST_CHECK_EQUAL(compF != compAA, true);
115 BOOST_CHECK_EQUAL(compF < compAA, true);
116 BOOST_CHECK_EQUAL(compF <= compAA, true);
117 BOOST_CHECK_EQUAL(compF > compAA, false);
118 BOOST_CHECK_EQUAL(compF >= compAA, false);
119}
120
121BOOST_AUTO_TEST_CASE(ZeroLength)
122{
123 name::Component comp0("");
124 BOOST_REQUIRE_EQUAL(comp0.value_size(), 0);
125
126 BOOST_CHECK_EQUAL(comp0, comp0);
127 BOOST_CHECK_EQUAL(comp0, name::Component(""));
128 BOOST_CHECK_LT(comp0, name::Component("A"));
129 BOOST_CHECK_LE(comp0, name::Component("A"));
130 BOOST_CHECK_NE(comp0, name::Component("A"));
131 BOOST_CHECK_GT(name::Component("A"), comp0);
132 BOOST_CHECK_GE(name::Component("A"), comp0);
133}
134
135BOOST_AUTO_TEST_CASE(Digest)
136{
137 name::Component digest1(Block(DIGEST_COMPONENT_WIRE, sizeof(DIGEST_COMPONENT_WIRE)));
138 name::Component digest1b(Block(DIGEST_COMPONENT_WIRE, sizeof(DIGEST_COMPONENT_WIRE)));
139 name::Component digest2(Block(DIGEST_COMPONENT2_WIRE, sizeof(DIGEST_COMPONENT2_WIRE)));
140 name::Component generic0;
141 name::Component generic2(Block(NAME_COMPONENT2_WIRE, sizeof(NAME_COMPONENT2_WIRE)));
142
143 BOOST_CHECK_EQUAL(digest1 == digest1b, true);
144 BOOST_CHECK_EQUAL(digest1 != digest1b, false);
145 BOOST_CHECK_EQUAL(digest1 < digest1b, false);
146 BOOST_CHECK_EQUAL(digest1 <= digest1b, true);
147 BOOST_CHECK_EQUAL(digest1 > digest1b, false);
148 BOOST_CHECK_EQUAL(digest1 >= digest1b, true);
149
150 BOOST_CHECK_EQUAL(digest1 == digest2, false);
151 BOOST_CHECK_EQUAL(digest1 != digest2, true);
152 BOOST_CHECK_EQUAL(digest1 < digest2, true);
153 BOOST_CHECK_EQUAL(digest1 <= digest2, true);
154 BOOST_CHECK_EQUAL(digest1 > digest2, false);
155 BOOST_CHECK_EQUAL(digest1 >= digest2, false);
156
157 BOOST_CHECK_EQUAL(digest1 == generic0, false);
158 BOOST_CHECK_EQUAL(digest1 != generic0, true);
159 BOOST_CHECK_EQUAL(digest1 < generic0, true);
160 BOOST_CHECK_EQUAL(digest1 <= generic0, true);
161 BOOST_CHECK_EQUAL(digest1 > generic0, false);
162 BOOST_CHECK_EQUAL(digest1 >= generic0, false);
163
164 BOOST_CHECK_EQUAL(digest2 == generic2, false);
165 BOOST_CHECK_EQUAL(digest2 != generic2, true);
166 BOOST_CHECK_EQUAL(digest2 < generic2, true);
167 BOOST_CHECK_EQUAL(digest2 <= generic2, true);
168 BOOST_CHECK_EQUAL(digest2 > generic2, false);
169 BOOST_CHECK_EQUAL(digest2 >= generic2, false);
170}
171
172BOOST_AUTO_TEST_SUITE_END() // Compare
173
174BOOST_AUTO_TEST_SUITE(CreateFromIterators) // Bug 2490
175
176typedef boost::mpl::vector<
177 std::vector<uint8_t>,
178 std::list<uint8_t>,
179 std::vector<int8_t>,
180 std::list<int8_t>
181> ContainerTypes;
182
183BOOST_AUTO_TEST_CASE_TEMPLATE(ZeroOctet, T, ContainerTypes)
184{
185 T bytes;
186 name::Component c(bytes.begin(), bytes.end());
187 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};
194 name::Component c(bytes.begin(), bytes.end());
195 BOOST_CHECK_EQUAL(c.value_size(), 1);
196 BOOST_CHECK_EQUAL(c.size(), 3);
197}
198
199BOOST_AUTO_TEST_CASE_TEMPLATE(FourOctets, T, ContainerTypes)
200{
201 T bytes{1, 2, 3, 4};
202 name::Component c(bytes.begin(), bytes.end());
203 BOOST_CHECK_EQUAL(c.value_size(), 4);
204 BOOST_CHECK_EQUAL(c.size(), 6);
205}
206
207BOOST_AUTO_TEST_SUITE_END() // CreateFromIterators
208
Junxiao Shi71ff2312017-07-12 13:32:50 +0000209BOOST_AUTO_TEST_SUITE(NamingConvention)
210
211template<typename ArgType>
212struct ConventionTest
213{
214 function<name::Component(ArgType)> makeComponent;
215 function<ArgType(const name::Component&)> getValue;
216 function<Name&(Name&, ArgType)> append;
217 Name expected;
218 ArgType value;
219 function<bool(const name::Component&)> isComponent;
220};
221
222class NumberWithMarker
223{
224public:
225 ConventionTest<uint64_t>
226 operator()() const
227 {
228 return {bind(&name::Component::fromNumberWithMarker, 0xAA, _1),
229 bind(&name::Component::toNumberWithMarker, _1, 0xAA),
230 bind(&Name::appendNumberWithMarker, _1, 0xAA, _2),
231 Name("/%AA%03%E8"),
232 1000,
233 bind(&name::Component::isNumberWithMarker, _1, 0xAA)};
234 }
235};
236
237class Segment
238{
239public:
240 ConventionTest<uint64_t>
241 operator()() const
242 {
243 return {&name::Component::fromSegment,
244 bind(&name::Component::toSegment, _1),
245 bind(&Name::appendSegment, _1, _2),
246 Name("/%00%27%10"),
247 10000,
248 bind(&name::Component::isSegment, _1)};
249 }
250};
251
252class SegmentOffset
253{
254public:
255 ConventionTest<uint64_t>
256 operator()() const
257 {
258 return {&name::Component::fromSegmentOffset,
259 bind(&name::Component::toSegmentOffset, _1),
260 bind(&Name::appendSegmentOffset, _1, _2),
261 Name("/%FB%00%01%86%A0"),
262 100000,
263 bind(&name::Component::isSegmentOffset, _1)};
264 }
265};
266
267class Version
268{
269public:
270 ConventionTest<uint64_t>
271 operator()() const
272 {
273 return {&name::Component::fromVersion,
274 bind(&name::Component::toVersion, _1),
275 [] (Name& name, uint64_t version) -> Name& { return name.appendVersion(version); },
276 Name("/%FD%00%0FB%40"),
277 1000000,
278 bind(&name::Component::isVersion, _1)};
279 }
280};
281
282class Timestamp
283{
284public:
285 ConventionTest<time::system_clock::TimePoint>
286 operator()() const
287 {
288 return {&name::Component::fromTimestamp,
289 bind(&name::Component::toTimestamp, _1),
290 [] (Name& name, time::system_clock::TimePoint t) -> Name& { return name.appendTimestamp(t); },
291 Name("/%FC%00%04%7BE%E3%1B%00%00"),
292 time::getUnixEpoch() + time::days(14600), // 40 years
293 bind(&name::Component::isTimestamp, _1)};
294 }
295};
296
297class SequenceNumber
298{
299public:
300 ConventionTest<uint64_t>
301 operator()() const
302 {
303 return {&name::Component::fromSequenceNumber,
304 bind(&name::Component::toSequenceNumber, _1),
305 bind(&Name::appendSequenceNumber, _1, _2),
306 Name("/%FE%00%98%96%80"),
307 10000000,
308 bind(&name::Component::isSequenceNumber, _1)};
309 }
310};
311
312using ConventionTests = boost::mpl::vector<
313 NumberWithMarker,
314 Segment,
315 SegmentOffset,
316 Version,
317 Timestamp,
318 SequenceNumber
319>;
320
321BOOST_AUTO_TEST_CASE_TEMPLATE(Convention, T, ConventionTests)
322{
323 name::Component invalidComponent1;
324 name::Component invalidComponent2("1234567890");
325
326 auto test = T()();
327
328 const Name& expected = test.expected;
329 BOOST_TEST_MESSAGE("Check " << expected[0].toUri());
330
331 BOOST_CHECK_EQUAL(expected[0].isGeneric(), true);
332
333 name::Component actualComponent = test.makeComponent(test.value);
334 BOOST_CHECK_EQUAL(actualComponent, expected[0]);
335
336 Name actualName;
337 test.append(actualName, test.value);
338 BOOST_CHECK_EQUAL(actualName, expected);
339
340 BOOST_CHECK_EQUAL(test.isComponent(expected[0]), true);
341 BOOST_CHECK_EQUAL(test.getValue(expected[0]), test.value);
342
343 BOOST_CHECK_EQUAL(test.isComponent(invalidComponent1), false);
344 BOOST_CHECK_EQUAL(test.isComponent(invalidComponent2), false);
345
346 BOOST_CHECK_THROW(test.getValue(invalidComponent1), name::Component::Error);
347 BOOST_CHECK_THROW(test.getValue(invalidComponent2), name::Component::Error);
348}
349
350BOOST_AUTO_TEST_SUITE_END() // NamingConvention
351
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000352BOOST_AUTO_TEST_SUITE_END() // TestNameComponent
353
354} // namespace tests
355} // namespace ndn