blob: b98c67aa97e9bb1f5e7234e18aa24e8f5bc93ce3 [file] [log] [blame]
Junxiao Shi688a6412017-06-22 10:12:07 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2017 Regents of the University of California.
4 *
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 "delegation-list.hpp"
23
24#include "boost-test.hpp"
25#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;
173 dl.insert(2, "/A");
174 dl.insert(1, "/B");
175 BOOST_CHECK_EQUAL(dl.size(), 2);
176
177 EncodingBuffer encoder;
178 dl.wireEncode(encoder);
179 BOOST_CHECK(encoder.block() == makeDelegationListBlock(tlv::ForwardingHint, {DEL1B, DEL2A}));
180}
181
182BOOST_AUTO_TEST_CASE(InsertReplace)
183{
Junxiao Shid21abd32017-06-30 02:56:40 +0000184 DelegationList dl({{2, "/A"}});
Junxiao Shi688a6412017-06-22 10:12:07 +0000185 dl.insert(Delegation{1, "/A"}, DelegationList::INS_REPLACE);
186 BOOST_CHECK_EQUAL(dl.size(), 1);
187 BOOST_CHECK_EQUAL(dl.at(0).preference, 1);
188 BOOST_CHECK_EQUAL(dl[0].name, "/A");
189
190 EncodingBuffer encoder;
191 dl.wireEncode(encoder);
192 BOOST_CHECK(encoder.block() == makeDelegationListBlock(tlv::ForwardingHint, {DEL1A}));
193}
194
195BOOST_AUTO_TEST_CASE(InsertAppend)
196{
Junxiao Shid21abd32017-06-30 02:56:40 +0000197 DelegationList dl({{2, "/A"}});
Junxiao Shi688a6412017-06-22 10:12:07 +0000198 dl.insert(Delegation{1, "/A"}, DelegationList::INS_APPEND);
199 BOOST_CHECK_EQUAL(dl.size(), 2);
200 BOOST_CHECK_EQUAL(dl.at(0).preference, 1);
201 BOOST_CHECK_EQUAL(dl.at(1).preference, 2);
202
203 EncodingBuffer encoder;
204 dl.wireEncode(encoder);
205 BOOST_CHECK(encoder.block() == makeDelegationListBlock(tlv::ForwardingHint, {DEL1A, DEL2A}));
206}
207
208BOOST_AUTO_TEST_CASE(InsertSkip)
209{
Junxiao Shid21abd32017-06-30 02:56:40 +0000210 DelegationList dl({{2, "/A"}});
Junxiao Shi688a6412017-06-22 10:12:07 +0000211 dl.insert(Delegation{1, "/A"}, DelegationList::INS_SKIP);
212 BOOST_CHECK_EQUAL(dl.size(), 1);
213 BOOST_CHECK_EQUAL(dl.at(0).preference, 2);
214
215 EncodingBuffer encoder;
216 dl.wireEncode(encoder);
217 BOOST_CHECK(encoder.block() == makeDelegationListBlock(tlv::ForwardingHint, {DEL2A}));
218}
219
220BOOST_AUTO_TEST_CASE(Unsorted)
221{
222 DelegationList dl(makeDelegationListBlock(tlv::ForwardingHint, {DEL2A}), false);
223 dl.insert(1, "/B");
224 BOOST_CHECK_EQUAL(dl.size(), 2);
225 BOOST_CHECK_EQUAL(dl.at(0).preference, 2);
226 BOOST_CHECK_EQUAL(dl.at(0).name, "/A");
227 BOOST_CHECK_EQUAL(dl.at(1).preference, 1);
228 BOOST_CHECK_EQUAL(dl.at(1).name, "/B");
229
230 EncodingBuffer encoder;
231 dl.wireEncode(encoder, tlv::Content);
232 BOOST_CHECK(encoder.block() == makeDelegationListBlock(tlv::Content, {DEL2A, DEL1B}));
233}
234
235BOOST_AUTO_TEST_CASE(EncodeBadType)
236{
237 DelegationList dl(makeDelegationListBlock(tlv::ForwardingHint, {DEL2A}));
238 EncodingBuffer encoder;
239 BOOST_CHECK_THROW(dl.wireEncode(encoder, tlv::Selectors), std::invalid_argument);
240}
241
242BOOST_AUTO_TEST_CASE(EncodeEmpty)
243{
244 DelegationList dl;
245 EncodingBuffer encoder;
246 BOOST_CHECK_THROW(dl.wireEncode(encoder), DelegationList::Error);
247}
248
249BOOST_AUTO_TEST_SUITE_END() // InsertEncode
250
251BOOST_AUTO_TEST_SUITE(Erase)
252
253BOOST_AUTO_TEST_CASE(EraseNoop)
254{
255 DelegationList dl;
256 dl.insert(1, "/A");
257 BOOST_CHECK_EQUAL(dl.erase(2, "/A"), 0);
258 BOOST_CHECK_EQUAL(dl.erase(Delegation{1, "/B"}), 0);
259 BOOST_CHECK_EQUAL(dl.size(), 1);
260 BOOST_CHECK_EQUAL(dl.at(0).preference, 1);
261 BOOST_CHECK_EQUAL(dl.at(0).name, "/A");
262}
263
264BOOST_AUTO_TEST_CASE(EraseOne)
265{
266 DelegationList dl;
267 dl.insert(1, "/A");
268 BOOST_CHECK_EQUAL(dl.erase(1, "/A"), 1);
269 BOOST_CHECK_EQUAL(dl.size(), 0);
270}
271
272BOOST_AUTO_TEST_CASE(EraseByName)
273{
274 DelegationList dl;
275 dl.insert(1, "/A");
276 dl.insert(2, "/A", DelegationList::INS_APPEND);
277 BOOST_CHECK_EQUAL(dl.size(), 2);
278 BOOST_CHECK_EQUAL(dl.erase("/A"), 2);
279 BOOST_CHECK_EQUAL(dl.size(), 0);
280}
281
282BOOST_AUTO_TEST_SUITE_END() // Erase
283
284BOOST_AUTO_TEST_SUITE(Sort)
285
286BOOST_AUTO_TEST_CASE(Noop)
287{
288 DelegationList dl(makeDelegationListBlock(tlv::ForwardingHint, {DEL1A}));
289 BOOST_CHECK_EQUAL(dl.isSorted(), true);
290 dl.sort();
291 BOOST_CHECK_EQUAL(dl.isSorted(), true);
292}
293
294BOOST_AUTO_TEST_CASE(Sort)
295{
296 DelegationList dl(makeDelegationListBlock(tlv::ForwardingHint, {DEL2A, DEL2B, DEL1A}), false);
297 BOOST_CHECK_EQUAL(dl.isSorted(), false);
298 dl.sort();
299 BOOST_CHECK_EQUAL(dl.isSorted(), true);
300 BOOST_CHECK_EQUAL(dl.size(), 3);
301 BOOST_CHECK_EQUAL(dl.at(0).preference, 1);
302 BOOST_CHECK_EQUAL(dl.at(0).name, "/A");
303 BOOST_CHECK_EQUAL(dl.at(1).preference, 2);
304 BOOST_CHECK_EQUAL(dl.at(1).name, "/A");
305 BOOST_CHECK_EQUAL(dl.at(2).preference, 2);
306 BOOST_CHECK_EQUAL(dl.at(2).name, "/B");
307}
308
309BOOST_AUTO_TEST_SUITE_END() // Sort
310
311BOOST_AUTO_TEST_SUITE(Compare)
312
313BOOST_AUTO_TEST_CASE(Empty)
314{
315 DelegationList dl1, dl2;
316 BOOST_CHECK_EQUAL(dl1, dl2);
317}
318
319BOOST_AUTO_TEST_CASE(SortedEqual)
320{
321 DelegationList dl1(makeDelegationListBlock(tlv::ForwardingHint, {DEL2A, DEL1B})),
322 dl2(makeDelegationListBlock(tlv::Content, {DEL1B, DEL2A}));
323 BOOST_CHECK_EQUAL(dl1, dl2);
324}
325
326BOOST_AUTO_TEST_CASE(SortedUnequal)
327{
328 DelegationList dl1(makeDelegationListBlock(tlv::ForwardingHint, {DEL2A, DEL1B})),
329 dl2(makeDelegationListBlock(tlv::Content, {DEL1A, DEL2B}));
330 BOOST_CHECK_NE(dl1, dl2);
331}
332
333BOOST_AUTO_TEST_CASE(UnsortedSameOrder)
334{
335 DelegationList dl1(makeDelegationListBlock(tlv::ForwardingHint, {DEL2A, DEL1B}), false),
336 dl2(makeDelegationListBlock(tlv::Content, {DEL2A, DEL1B}), false);
337 BOOST_CHECK_EQUAL(dl1, dl2);
338}
339
340BOOST_AUTO_TEST_CASE(UnsortedDifferentOrder)
341{
342 DelegationList dl1(makeDelegationListBlock(tlv::ForwardingHint, {DEL2A, DEL1B}), false),
343 dl2(makeDelegationListBlock(tlv::Content, {DEL1B, DEL2A}), false);
344 BOOST_CHECK_NE(dl1, dl2);
345}
346
347BOOST_AUTO_TEST_SUITE_END() // Compare
348
349BOOST_AUTO_TEST_SUITE(Print)
350
351BOOST_AUTO_TEST_CASE(PrintEmpty)
352{
353 DelegationList dl;
354 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(dl), "[]");
355}
356
357BOOST_AUTO_TEST_CASE(PrintNormal)
358{
359 DelegationList dl(makeDelegationListBlock(tlv::ForwardingHint, {DEL2A, DEL1B}));
360 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(dl), "[/B(1),/A(2)]");
361}
362
363BOOST_AUTO_TEST_SUITE_END() // Print
364
365BOOST_AUTO_TEST_SUITE_END() // TestDelegationList
366
367} // namespace tests
368} // namespace ndn