blob: 0831dca33e298f78a2924da8781f866a9e95c25c [file] [log] [blame]
Junxiao Shi688a6412017-06-22 10:12:07 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shibb8d4a32017-07-13 02:40:09 +00002/*
Junxiao Shi72c0c642018-04-20 15:41:09 +00003 * Copyright (c) 2013-2018 Regents of the University of California.
Junxiao Shi688a6412017-06-22 10:12:07 +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
Davide Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/delegation-list.hpp"
Junxiao Shi688a6412017-06-22 10:12:07 +000023
Davide Pesavento7e780642018-11-24 15:51:34 -050024#include "tests/boost-test.hpp"
Junxiao Shi688a6412017-06-22 10:12:07 +000025#include <boost/lexical_cast.hpp>
26
27namespace ndn {
28namespace tests {
29
30BOOST_AUTO_TEST_SUITE(TestDelegationList)
31
32const uint8_t DEL1A[] = {
33 0x1f, 0x08, // Delegation
34 0x1e, 0x01, 0x01, // Preference=1
35 0x07, 0x03, 0x08, 0x01, 0x41 // Name=/A
36};
37const uint8_t DEL1B[] = {
38 0x1f, 0x08, // Delegation
39 0x1e, 0x01, 0x01, // Preference=1
40 0x07, 0x03, 0x08, 0x01, 0x42 // Name=/B
41};
42const uint8_t DEL2A[] = {
43 0x1f, 0x08, // Delegation
44 0x1e, 0x01, 0x02, // Preference=2
45 0x07, 0x03, 0x08, 0x01, 0x41 // Name=/A
46};
47const uint8_t DEL2B[] = {
48 0x1f, 0x08, // Delegation
49 0x1e, 0x01, 0x02, // Preference=2
50 0x07, 0x03, 0x08, 0x01, 0x42 // Name=/B
51};
52
53Block
54makeDelegationListBlock(uint32_t type, std::initializer_list<const uint8_t*> dels)
55{
56 Block block(type);
57 for (const uint8_t* del : dels) {
58 block.push_back(Block(del, 2 + del[1]));
59 }
60 block.encode();
61 return block;
62}
63
64BOOST_AUTO_TEST_SUITE(Decode)
65
66BOOST_AUTO_TEST_CASE(DecodeUnsorted)
67{
68 DelegationList dl(makeDelegationListBlock(tlv::ForwardingHint, {DEL2A, DEL2B, DEL1A}), false);
69 BOOST_CHECK_EQUAL(dl.size(), 3);
70 BOOST_CHECK_EQUAL(dl.at(0).preference, 2);
71 BOOST_CHECK_EQUAL(dl.at(0).name, "/A");
72 BOOST_CHECK_EQUAL(dl.at(1).preference, 2);
73 BOOST_CHECK_EQUAL(dl.at(1).name, "/B");
74 BOOST_CHECK_EQUAL(dl.at(2).preference, 1);
75 BOOST_CHECK_EQUAL(dl.at(2).name, "/A");
76}
77
78BOOST_AUTO_TEST_CASE(DecodeSorted)
79{
80 DelegationList dl(makeDelegationListBlock(tlv::Content, {DEL2A, DEL2B, DEL1A}));
81 BOOST_CHECK_EQUAL(dl.size(), 3);
82 BOOST_CHECK_EQUAL(dl.at(0).preference, 1);
83 BOOST_CHECK_EQUAL(dl.at(0).name, "/A");
84 BOOST_CHECK_EQUAL(dl.at(1).preference, 2);
85 BOOST_CHECK_EQUAL(dl.at(1).name, "/A");
86 BOOST_CHECK_EQUAL(dl.at(2).preference, 2);
87 BOOST_CHECK_EQUAL(dl.at(2).name, "/B");
88}
89
90BOOST_AUTO_TEST_CASE(DecodeEmpty)
91{
92 DelegationList dl;
93 Block block = makeDelegationListBlock(tlv::ForwardingHint, {});
94 BOOST_CHECK_THROW(dl.wireDecode(block), DelegationList::Error);
95}
96
97BOOST_AUTO_TEST_CASE(DecodeBadType)
98{
99 DelegationList dl;
100 Block block = makeDelegationListBlock(tlv::Selectors, {DEL1A, DEL2B});
101 BOOST_CHECK_THROW(dl.wireDecode(block), DelegationList::Error);
102}
103
104BOOST_AUTO_TEST_CASE(DecodeNotDelegation)
105{
106 const uint8_t BAD_DEL[] = {
107 0x09, 0x00 // Selectors
108 };
109
110 DelegationList dl;
111 Block block = makeDelegationListBlock(tlv::ForwardingHint, {DEL1A, BAD_DEL});
112 BOOST_CHECK_THROW(dl.wireDecode(block), DelegationList::Error);
113}
114
115BOOST_AUTO_TEST_CASE(DecodeMissingPreference)
116{
117 const uint8_t BAD_DEL[] = {
118 0x1f, 0x05, // Delegation
119 0x07, 0x03, 0x08, 0x01, 0x42 // Name=/B
120 };
121
122 DelegationList dl;
123 Block block = makeDelegationListBlock(tlv::ForwardingHint, {DEL1A, BAD_DEL});
124 BOOST_CHECK_THROW(dl.wireDecode(block), DelegationList::Error);
125}
126
127BOOST_AUTO_TEST_CASE(DecodeMissingName)
128{
129 const uint8_t BAD_DEL[] = {
130 0x1f, 0x03, // Delegation
131 0x1e, 0x01, 0x02, // Preference=2
132 };
133
134 DelegationList dl;
135 Block block = makeDelegationListBlock(tlv::ForwardingHint, {DEL1A, BAD_DEL});
136 BOOST_CHECK_THROW(dl.wireDecode(block), DelegationList::Error);
137}
138
139BOOST_AUTO_TEST_CASE(DecodeUnknownField)
140{
141 const uint8_t BAD_DEL[] = {
142 0x1f, 0x0a, // Delegation
143 0x1e, 0x01, 0x02, // Preference=2
144 0x09, 0x00, // Selectors
145 0x07, 0x03, 0x08, 0x01, 0x42 // Name=/B
146 };
147
148 DelegationList dl;
149 Block block = makeDelegationListBlock(tlv::ForwardingHint, {DEL1A, BAD_DEL});
150 BOOST_CHECK_THROW(dl.wireDecode(block), DelegationList::Error);
151}
152
153BOOST_AUTO_TEST_CASE(DecodeWrongOrder)
154{
155 const uint8_t BAD_DEL[] = {
156 0x1f, 0x08, // Delegation
157 0x07, 0x03, 0x08, 0x01, 0x42, // Name=/B
158 0x1e, 0x01, 0x02 // Preference=2
159 };
160
161 DelegationList dl;
162 Block block = makeDelegationListBlock(tlv::ForwardingHint, {DEL1A, BAD_DEL});
163 BOOST_CHECK_THROW(dl.wireDecode(block), DelegationList::Error);
164}
165
166BOOST_AUTO_TEST_SUITE_END() // Decode
167
168BOOST_AUTO_TEST_SUITE(InsertEncode)
169
170BOOST_AUTO_TEST_CASE(InsertSimple)
171{
172 DelegationList dl;
Junxiao Shibb8d4a32017-07-13 02:40:09 +0000173 BOOST_CHECK_EQUAL(dl.empty(), true);
Junxiao Shi688a6412017-06-22 10:12:07 +0000174 dl.insert(2, "/A");
Junxiao Shibb8d4a32017-07-13 02:40:09 +0000175 BOOST_CHECK_EQUAL(dl.empty(), false);
Junxiao Shi688a6412017-06-22 10:12:07 +0000176 dl.insert(1, "/B");
177 BOOST_CHECK_EQUAL(dl.size(), 2);
178
179 EncodingBuffer encoder;
180 dl.wireEncode(encoder);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000181 BOOST_CHECK_EQUAL(encoder.block(), makeDelegationListBlock(tlv::ForwardingHint, {DEL1B, DEL2A}));
Junxiao Shi688a6412017-06-22 10:12:07 +0000182}
183
184BOOST_AUTO_TEST_CASE(InsertReplace)
185{
Junxiao Shid21abd32017-06-30 02:56:40 +0000186 DelegationList dl({{2, "/A"}});
Junxiao Shi688a6412017-06-22 10:12:07 +0000187 dl.insert(Delegation{1, "/A"}, DelegationList::INS_REPLACE);
188 BOOST_CHECK_EQUAL(dl.size(), 1);
189 BOOST_CHECK_EQUAL(dl.at(0).preference, 1);
190 BOOST_CHECK_EQUAL(dl[0].name, "/A");
191
192 EncodingBuffer encoder;
193 dl.wireEncode(encoder);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000194 BOOST_CHECK_EQUAL(encoder.block(), makeDelegationListBlock(tlv::ForwardingHint, {DEL1A}));
Junxiao Shi688a6412017-06-22 10:12:07 +0000195}
196
197BOOST_AUTO_TEST_CASE(InsertAppend)
198{
Junxiao Shid21abd32017-06-30 02:56:40 +0000199 DelegationList dl({{2, "/A"}});
Junxiao Shi688a6412017-06-22 10:12:07 +0000200 dl.insert(Delegation{1, "/A"}, DelegationList::INS_APPEND);
201 BOOST_CHECK_EQUAL(dl.size(), 2);
202 BOOST_CHECK_EQUAL(dl.at(0).preference, 1);
203 BOOST_CHECK_EQUAL(dl.at(1).preference, 2);
204
205 EncodingBuffer encoder;
206 dl.wireEncode(encoder);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000207 BOOST_CHECK_EQUAL(encoder.block(), makeDelegationListBlock(tlv::ForwardingHint, {DEL1A, DEL2A}));
Junxiao Shi688a6412017-06-22 10:12:07 +0000208}
209
210BOOST_AUTO_TEST_CASE(InsertSkip)
211{
Junxiao Shid21abd32017-06-30 02:56:40 +0000212 DelegationList dl({{2, "/A"}});
Junxiao Shi688a6412017-06-22 10:12:07 +0000213 dl.insert(Delegation{1, "/A"}, DelegationList::INS_SKIP);
214 BOOST_CHECK_EQUAL(dl.size(), 1);
215 BOOST_CHECK_EQUAL(dl.at(0).preference, 2);
216
217 EncodingBuffer encoder;
218 dl.wireEncode(encoder);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000219 BOOST_CHECK_EQUAL(encoder.block(), makeDelegationListBlock(tlv::ForwardingHint, {DEL2A}));
Junxiao Shi688a6412017-06-22 10:12:07 +0000220}
221
222BOOST_AUTO_TEST_CASE(Unsorted)
223{
224 DelegationList dl(makeDelegationListBlock(tlv::ForwardingHint, {DEL2A}), false);
225 dl.insert(1, "/B");
226 BOOST_CHECK_EQUAL(dl.size(), 2);
227 BOOST_CHECK_EQUAL(dl.at(0).preference, 2);
228 BOOST_CHECK_EQUAL(dl.at(0).name, "/A");
229 BOOST_CHECK_EQUAL(dl.at(1).preference, 1);
230 BOOST_CHECK_EQUAL(dl.at(1).name, "/B");
231
232 EncodingBuffer encoder;
233 dl.wireEncode(encoder, tlv::Content);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000234 BOOST_CHECK_EQUAL(encoder.block(), makeDelegationListBlock(tlv::Content, {DEL2A, DEL1B}));
Junxiao Shi688a6412017-06-22 10:12:07 +0000235}
236
237BOOST_AUTO_TEST_CASE(EncodeBadType)
238{
239 DelegationList dl(makeDelegationListBlock(tlv::ForwardingHint, {DEL2A}));
240 EncodingBuffer encoder;
241 BOOST_CHECK_THROW(dl.wireEncode(encoder, tlv::Selectors), std::invalid_argument);
242}
243
244BOOST_AUTO_TEST_CASE(EncodeEmpty)
245{
246 DelegationList dl;
247 EncodingBuffer encoder;
248 BOOST_CHECK_THROW(dl.wireEncode(encoder), DelegationList::Error);
249}
250
251BOOST_AUTO_TEST_SUITE_END() // InsertEncode
252
253BOOST_AUTO_TEST_SUITE(Erase)
254
255BOOST_AUTO_TEST_CASE(EraseNoop)
256{
257 DelegationList dl;
258 dl.insert(1, "/A");
259 BOOST_CHECK_EQUAL(dl.erase(2, "/A"), 0);
260 BOOST_CHECK_EQUAL(dl.erase(Delegation{1, "/B"}), 0);
261 BOOST_CHECK_EQUAL(dl.size(), 1);
262 BOOST_CHECK_EQUAL(dl.at(0).preference, 1);
263 BOOST_CHECK_EQUAL(dl.at(0).name, "/A");
264}
265
266BOOST_AUTO_TEST_CASE(EraseOne)
267{
268 DelegationList dl;
269 dl.insert(1, "/A");
270 BOOST_CHECK_EQUAL(dl.erase(1, "/A"), 1);
271 BOOST_CHECK_EQUAL(dl.size(), 0);
272}
273
274BOOST_AUTO_TEST_CASE(EraseByName)
275{
276 DelegationList dl;
277 dl.insert(1, "/A");
278 dl.insert(2, "/A", DelegationList::INS_APPEND);
279 BOOST_CHECK_EQUAL(dl.size(), 2);
280 BOOST_CHECK_EQUAL(dl.erase("/A"), 2);
281 BOOST_CHECK_EQUAL(dl.size(), 0);
282}
283
284BOOST_AUTO_TEST_SUITE_END() // Erase
285
286BOOST_AUTO_TEST_SUITE(Sort)
287
288BOOST_AUTO_TEST_CASE(Noop)
289{
290 DelegationList dl(makeDelegationListBlock(tlv::ForwardingHint, {DEL1A}));
291 BOOST_CHECK_EQUAL(dl.isSorted(), true);
292 dl.sort();
293 BOOST_CHECK_EQUAL(dl.isSorted(), true);
294}
295
296BOOST_AUTO_TEST_CASE(Sort)
297{
298 DelegationList dl(makeDelegationListBlock(tlv::ForwardingHint, {DEL2A, DEL2B, DEL1A}), false);
299 BOOST_CHECK_EQUAL(dl.isSorted(), false);
300 dl.sort();
301 BOOST_CHECK_EQUAL(dl.isSorted(), true);
302 BOOST_CHECK_EQUAL(dl.size(), 3);
303 BOOST_CHECK_EQUAL(dl.at(0).preference, 1);
304 BOOST_CHECK_EQUAL(dl.at(0).name, "/A");
305 BOOST_CHECK_EQUAL(dl.at(1).preference, 2);
306 BOOST_CHECK_EQUAL(dl.at(1).name, "/A");
307 BOOST_CHECK_EQUAL(dl.at(2).preference, 2);
308 BOOST_CHECK_EQUAL(dl.at(2).name, "/B");
309}
310
311BOOST_AUTO_TEST_SUITE_END() // Sort
312
313BOOST_AUTO_TEST_SUITE(Compare)
314
315BOOST_AUTO_TEST_CASE(Empty)
316{
317 DelegationList dl1, dl2;
318 BOOST_CHECK_EQUAL(dl1, dl2);
319}
320
321BOOST_AUTO_TEST_CASE(SortedEqual)
322{
323 DelegationList dl1(makeDelegationListBlock(tlv::ForwardingHint, {DEL2A, DEL1B})),
324 dl2(makeDelegationListBlock(tlv::Content, {DEL1B, DEL2A}));
325 BOOST_CHECK_EQUAL(dl1, dl2);
326}
327
328BOOST_AUTO_TEST_CASE(SortedUnequal)
329{
330 DelegationList dl1(makeDelegationListBlock(tlv::ForwardingHint, {DEL2A, DEL1B})),
331 dl2(makeDelegationListBlock(tlv::Content, {DEL1A, DEL2B}));
332 BOOST_CHECK_NE(dl1, dl2);
333}
334
335BOOST_AUTO_TEST_CASE(UnsortedSameOrder)
336{
337 DelegationList dl1(makeDelegationListBlock(tlv::ForwardingHint, {DEL2A, DEL1B}), false),
338 dl2(makeDelegationListBlock(tlv::Content, {DEL2A, DEL1B}), false);
339 BOOST_CHECK_EQUAL(dl1, dl2);
340}
341
342BOOST_AUTO_TEST_CASE(UnsortedDifferentOrder)
343{
344 DelegationList dl1(makeDelegationListBlock(tlv::ForwardingHint, {DEL2A, DEL1B}), false),
345 dl2(makeDelegationListBlock(tlv::Content, {DEL1B, DEL2A}), false);
346 BOOST_CHECK_NE(dl1, dl2);
347}
348
349BOOST_AUTO_TEST_SUITE_END() // Compare
350
351BOOST_AUTO_TEST_SUITE(Print)
352
353BOOST_AUTO_TEST_CASE(PrintEmpty)
354{
355 DelegationList dl;
356 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(dl), "[]");
357}
358
359BOOST_AUTO_TEST_CASE(PrintNormal)
360{
361 DelegationList dl(makeDelegationListBlock(tlv::ForwardingHint, {DEL2A, DEL1B}));
362 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(dl), "[/B(1),/A(2)]");
363}
364
365BOOST_AUTO_TEST_SUITE_END() // Print
366
367BOOST_AUTO_TEST_SUITE_END() // TestDelegationList
368
369} // namespace tests
370} // namespace ndn