blob: b136f95492566350786b7eebb662d7463df0512c [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi899277a2017-07-07 22:12:12 +00002/*
Alexander Afanasyev1013fd02017-01-03 13:19:03 -08003 * Copyright (c) 2013-2017 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 Afanasyev5fa9e9a2013-12-24 19:45:07 -080020 */
21
Alexander Afanasyev09c613f2014-01-29 00:23:58 -080022#include "interest.hpp"
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070023#include "data.hpp"
Yingdi Yubf6a2812014-06-17 15:32:11 -070024#include "security/digest-sha256.hpp"
Junxiao Shi899277a2017-07-07 22:12:12 +000025#include "security/signature-sha256-with-rsa.hpp"
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070026
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070027#include "boost-test.hpp"
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070028#include "identity-management-fixture.hpp"
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080029
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080030namespace ndn {
Alexander Afanasyev90164962014-03-06 08:29:59 +000031namespace tests {
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080032
Junxiao Shi899277a2017-07-07 22:12:12 +000033BOOST_AUTO_TEST_SUITE(TestInterest)
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080034
Junxiao Shi899277a2017-07-07 22:12:12 +000035// ---- constructor, encode, decode ----
36
37BOOST_AUTO_TEST_CASE(DefaultConstructor)
38{
39 Interest i;
40 BOOST_CHECK_EQUAL(i.getName(), "/");
41 BOOST_CHECK(i.getSelectors().empty());
42 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
43 BOOST_CHECK_EQUAL(i.hasLink(), false);
44 BOOST_CHECK(!i.hasSelectedDelegation());
45}
46
47BOOST_AUTO_TEST_CASE(EncodeDecodeBasic)
48{
49 const uint8_t WIRE[] = {
50 0x05, 0x1c, // Interest
51 0x07, 0x14, // Name
52 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // NameComponent
53 0x08, 0x03, 0x6e, 0x64, 0x6e, // NameComponent
54 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // NameComponent
55 0x0a, 0x04, // Nonce
56 0x01, 0x00, 0x00, 0x00
57 };
58
59 Interest i1("/local/ndn/prefix");
60 i1.setNonce(1);
61 Block wire1 = i1.wireEncode();
62 BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
63
64 Interest i2(wire1);
65 BOOST_CHECK_EQUAL(i2.getName(), "/local/ndn/prefix");
66 BOOST_CHECK(i2.getSelectors().empty());
67 BOOST_CHECK_EQUAL(i2.getNonce(), 1);
68 BOOST_CHECK_EQUAL(i2.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
69
70 BOOST_CHECK_EQUAL(i1, i2);
71}
72
73BOOST_AUTO_TEST_CASE(EncodeDecodeFull)
74{
75 const uint8_t WIRE[] = {
76 0x05, 0x25, // Interest
77 0x07, 0x14, // Name
78 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // NameComponent
79 0x08, 0x03, 0x6e, 0x64, 0x6e, // NameComponent
80 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // NameComponent
81 0x09, 0x03, // Selectors
82 0x0d, 0x01, 0x01, // MinSuffixComponents
83 0x0a, 0x04, // Nonce
84 0x01, 0x00, 0x00, 0x00,
85 0x0c, 0x02, // InterestLifetime
86 0x03, 0xe8
87 };
88 ///\todo #4055 ForwardingHint
89
90 Interest i1;
91 i1.setName("/local/ndn/prefix");
92 i1.setMinSuffixComponents(1);
93 i1.setNonce(1);
94 i1.setInterestLifetime(time::milliseconds(1000));
95 Block wire1 = i1.wireEncode();
96 BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
97
98 Interest i2(wire1);
99 BOOST_CHECK_EQUAL(i2.getName(), "/local/ndn/prefix");
100 BOOST_CHECK_EQUAL(i2.getMinSuffixComponents(), 1);
101 BOOST_CHECK_EQUAL(i2.getNonce(), 1);
102 BOOST_CHECK_EQUAL(i2.getInterestLifetime(), time::milliseconds(1000));
103
104 BOOST_CHECK_EQUAL(i1, i2);
105}
106
107const uint8_t LINK[] = {
108 0x06, 0xda, // Data
109 0x07, 0x14, // Name
110 0x08, 0x05,
111 0x6c, 0x6f, 0x63, 0x61, 0x6c,
112 0x08, 0x03,
113 0x6e, 0x64, 0x6e,
114 0x08, 0x06,
115 0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
116 0x14, 0x07, // MetaInfo
117 0x18, 0x01, // ContentType
118 0x01,
119 0x19, 0x02, // FreshnessPeriod
120 0x27, 0x10,
121 0x15, 0x1a, // Content
122 0x1f, 0x0c, // LinkDelegation
123 0x1e, 0x01, // LinkPreference
124 0x0a,
125 0x07, 0x07, // Name
126 0x08, 0x05,
127 0x6c, 0x6f, 0x63, 0x61, 0x6c,
128 0x1f, 0x0a, // LinkDelegation
129 0x1e, 0x01, // LinkPreference
130 0x14,
131 0x07, 0x05, // Name
Junxiao Shib332e782014-03-31 14:23:46 -0700132 0x08, 0x03,
Junxiao Shi899277a2017-07-07 22:12:12 +0000133 0x6e, 0x64, 0x6e,
134 0x16, 0x1b, // SignatureInfo
135 0x1b, 0x01, // SignatureType
136 0x01,
137 0x1c, 0x16, // KeyLocator
138 0x07, 0x14, // Name
139 0x08, 0x04,
140 0x74, 0x65, 0x73, 0x74,
141 0x08, 0x03,
142 0x6b, 0x65, 0x79,
143 0x08, 0x07,
144 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
145 0x17, 0x80, // SignatureValue
146 0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe, 0xb1, 0x6f, 0x3e, 0x31, 0xec,
147 0xe3, 0xb9, 0xea, 0x83, 0x30, 0x40, 0x03, 0xfc, 0xa0, 0x13, 0xd9, 0xb3, 0xc6,
148 0x25, 0x16, 0x2d, 0xa6, 0x58, 0x41, 0x69, 0x62, 0x56, 0xd8, 0xb3, 0x6a, 0x38,
149 0x76, 0x56, 0xea, 0x61, 0xb2, 0x32, 0x70, 0x1c, 0xb6, 0x4d, 0x10, 0x1d, 0xdc,
150 0x92, 0x8e, 0x52, 0xa5, 0x8a, 0x1d, 0xd9, 0x96, 0x5e, 0xc0, 0x62, 0x0b, 0xcf,
151 0x3a, 0x9d, 0x7f, 0xca, 0xbe, 0xa1, 0x41, 0x71, 0x85, 0x7a, 0x8b, 0x5d, 0xa9,
152 0x64, 0xd6, 0x66, 0xb4, 0xe9, 0x8d, 0x0c, 0x28, 0x43, 0xee, 0xa6, 0x64, 0xe8,
153 0x55, 0xf6, 0x1c, 0x19, 0x0b, 0xef, 0x99, 0x25, 0x1e, 0xdc, 0x78, 0xb3, 0xa7,
154 0xaa, 0x0d, 0x14, 0x58, 0x30, 0xe5, 0x37, 0x6a, 0x6d, 0xdb, 0x56, 0xac, 0xa3,
155 0xfc, 0x90, 0x7a, 0xb8, 0x66, 0x9c, 0x0e, 0xf6, 0xb7, 0x64, 0xd1
Alexander Afanasyeve881e932014-06-08 14:47:03 +0300156};
157
Junxiao Shi899277a2017-07-07 22:12:12 +0000158BOOST_AUTO_TEST_CASE(WireDecodeReset) // checks wireDecode resets all fields
159{
160 Interest i1;
161 i1.setName("/test");
162 i1.setMinSuffixComponents(100);
163 i1.setNonce(10);
164 i1.setInterestLifetime(time::seconds(10));
165 i1.setLink(Block(LINK, sizeof(LINK)));
166 i1.setSelectedDelegation(0);
167
168 Interest i2(i1.wireEncode());
169 BOOST_CHECK_EQUAL(i2.getName().toUri(), "/test");
170 BOOST_CHECK_EQUAL(i2.getInterestLifetime(), time::seconds(10));
171 BOOST_CHECK_EQUAL(i2.getMinSuffixComponents(), 100);
172 BOOST_CHECK_EQUAL(i2.getNonce(), 10);
173 BOOST_CHECK_EQUAL(i2.hasLink(), true);
174 BOOST_CHECK_EQUAL(i2.hasSelectedDelegation(), true);
175
176 i2.wireDecode(Interest().wireEncode());
177 BOOST_CHECK_EQUAL(i2.getName().toUri(), "/");
178 BOOST_CHECK_EQUAL(i2.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
179 BOOST_CHECK_EQUAL(i2.getMinSuffixComponents(), -1);
180 BOOST_WARN_NE(i2.getNonce(), 10);
181 BOOST_CHECK_EQUAL(i2.hasLink(), false);
182 BOOST_CHECK_EQUAL(i2.hasSelectedDelegation(), false);
183}
184
185// ---- matching ----
186
187BOOST_AUTO_TEST_CASE(MatchesData)
188{
189 Interest interest;
190 interest.setName("ndn:/A")
191 .setMinSuffixComponents(2)
192 .setMaxSuffixComponents(2)
193 .setPublisherPublicKeyLocator(KeyLocator("ndn:/B"))
194 .setExclude(Exclude().excludeAfter(name::Component("J")));
195
196 Data data("ndn:/A/D");
197 SignatureSha256WithRsa signature(KeyLocator("ndn:/B"));
198 data.setSignature(signature);
199 data.wireEncode();
200 BOOST_CHECK_EQUAL(interest.matchesData(data), true);
201
202 Data data1 = data;
203 data1.setName("ndn:/A"); // violates MinSuffixComponents
204 data1.wireEncode();
205 BOOST_CHECK_EQUAL(interest.matchesData(data1), false);
206
207 Interest interest1 = interest;
208 interest1.setMinSuffixComponents(1);
209 BOOST_CHECK_EQUAL(interest1.matchesData(data1), true);
210
211 Data data2 = data;
212 data2.setName("ndn:/A/E/F"); // violates MaxSuffixComponents
213 data2.wireEncode();
214 BOOST_CHECK_EQUAL(interest.matchesData(data2), false);
215
216 Interest interest2 = interest;
217 interest2.setMaxSuffixComponents(3);
218 BOOST_CHECK_EQUAL(interest2.matchesData(data2), true);
219
220 Data data3 = data;
221 SignatureSha256WithRsa signature3(KeyLocator("ndn:/G")); // violates PublisherPublicKeyLocator
222 data3.setSignature(signature3);
223 data3.wireEncode();
224 BOOST_CHECK_EQUAL(interest.matchesData(data3), false);
225
226 Interest interest3 = interest;
227 interest3.setPublisherPublicKeyLocator(KeyLocator("ndn:/G"));
228 BOOST_CHECK_EQUAL(interest3.matchesData(data3), true);
229
230 Data data4 = data;
231 DigestSha256 signature4; // violates PublisherPublicKeyLocator
232 data4.setSignature(signature4);
233 data4.wireEncode();
234 BOOST_CHECK_EQUAL(interest.matchesData(data4), false);
235
236 Interest interest4 = interest;
237 interest4.setPublisherPublicKeyLocator(KeyLocator());
238 BOOST_CHECK_EQUAL(interest4.matchesData(data4), true);
239
240 Data data5 = data;
241 data5.setName("ndn:/A/J"); // violates Exclude
242 data5.wireEncode();
243 BOOST_CHECK_EQUAL(interest.matchesData(data5), false);
244
245 Interest interest5 = interest;
246 interest5.setExclude(Exclude().excludeAfter(name::Component("K")));
247 BOOST_CHECK_EQUAL(interest5.matchesData(data5), true);
248
249 Data data6 = data;
250 data6.setName("ndn:/H/I"); // violates Name
251 data6.wireEncode();
252 BOOST_CHECK_EQUAL(interest.matchesData(data6), false);
253
254 Data data7 = data;
255 data7.setName("ndn:/A/B");
256 data7.wireEncode();
257
258 Interest interest7("/A/B/sha256digest=D548DECEFC4B880720DC9257A8D815E9DF4465E63742EE55C29133055DAA67C2");
259 BOOST_CHECK_EQUAL(interest7.matchesData(data7), true);
260
261 Interest interest7b("/A/B/sha256digest=0000000000000000000000000000000000000000000000000000000000000000");
262 BOOST_CHECK_EQUAL(interest7b.matchesData(data7), false); // violates implicit digest
263}
264
265BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(MatchesInterest, 1)
266BOOST_AUTO_TEST_CASE(MatchesInterest)
267{
268 Interest interest;
269 interest
270 .setName("/A")
271 .setMinSuffixComponents(2)
272 .setMaxSuffixComponents(2)
273 .setPublisherPublicKeyLocator(KeyLocator("/B"))
274 .setExclude(Exclude().excludeAfter(name::Component("J")))
275 .setNonce(10)
276 .setInterestLifetime(time::seconds(5))
277 .setLink(Block(LINK, sizeof(LINK)));
278
279 Interest other;
280 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false);
281
282 other.setName(interest.getName());
283 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false);
284
285 other.setSelectors(interest.getSelectors());
286 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false); // will match until #3162 implemented
287
288 other.setLink(interest.getLink().wireEncode());
289 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
290
291 other.setNonce(200);
292 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
293
294 other.setInterestLifetime(time::hours(5));
295 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
296
297 other.setSelectedDelegation(0);
298 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
299}
300
301// ---- field accessors ----
302
303BOOST_AUTO_TEST_CASE(GetNonce)
304{
305 unique_ptr<Interest> i;
306
307 // getNonce automatically assigns a random Nonce, which could be zero.
308 // But it's unlikely to get 100 zeros in a row.
309 uint32_t nonce = 0;
310 int nIterations = 0;
311 do {
312 i = make_unique<Interest>();
313 nonce = i->getNonce();
314 }
315 while (nonce == 0 && ++nIterations < 100);
316 BOOST_CHECK_NE(nonce, 0);
317 BOOST_CHECK(i->hasNonce());
318
319 // Once a Nonce is assigned, it should not change.
320 BOOST_CHECK_EQUAL(i->getNonce(), nonce);
321}
322
323BOOST_AUTO_TEST_CASE(RefreshNonce)
324{
325 Interest i;
326 BOOST_CHECK(!i.hasNonce());
327 i.refreshNonce();
328 BOOST_CHECK(!i.hasNonce());
329
330 i.setNonce(1);
331 BOOST_CHECK(i.hasNonce());
332 i.refreshNonce();
333 BOOST_CHECK(i.hasNonce());
334 BOOST_CHECK_NE(i.getNonce(), 1);
335}
336
337BOOST_AUTO_TEST_CASE(SetInterestLifetime)
338{
339 BOOST_CHECK_THROW(Interest("/A", time::milliseconds(-1)), std::invalid_argument);
340 BOOST_CHECK_NO_THROW(Interest("/A", time::milliseconds(0)));
341
342 Interest i("/local/ndn/prefix");
343 i.setNonce(1);
344 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
345 BOOST_CHECK_THROW(i.setInterestLifetime(time::milliseconds(-1)), std::invalid_argument);
346 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
347 i.setInterestLifetime(time::milliseconds(0));
348 BOOST_CHECK_EQUAL(i.getInterestLifetime(), time::milliseconds(0));
349 i.setInterestLifetime(time::milliseconds(1));
350 BOOST_CHECK_EQUAL(i.getInterestLifetime(), time::milliseconds(1));
351}
352
353// ---- operators ----
354
355BOOST_AUTO_TEST_CASE(Equality)
356{
357 Interest a;
358 Interest b;
359
360 // if nonce is not set, it would be set to a random value
361 a.setNonce(1);
362 b.setNonce(1);
363
364 BOOST_CHECK_EQUAL(a == b, true);
365 BOOST_CHECK_EQUAL(a != b, false);
366
367 // compare Name
368 a.setName("/A");
369 BOOST_CHECK_EQUAL(a == b, false);
370 BOOST_CHECK_EQUAL(a != b, true);
371
372 b.setName("/B");
373 BOOST_CHECK_EQUAL(a == b, false);
374 BOOST_CHECK_EQUAL(a != b, true);
375
376 b.setName("/A");
377 BOOST_CHECK_EQUAL(a == b, true);
378 BOOST_CHECK_EQUAL(a != b, false);
379
380 // compare Selectors
381 a.setChildSelector(1);
382 BOOST_CHECK_EQUAL(a == b, false);
383 BOOST_CHECK_EQUAL(a != b, true);
384
385 b.setChildSelector(1);
386 BOOST_CHECK_EQUAL(a == b, true);
387 BOOST_CHECK_EQUAL(a != b, false);
388
389 // compare Nonce
390 a.setNonce(100);
391 BOOST_CHECK_EQUAL(a == b, false);
392 BOOST_CHECK_EQUAL(a != b, true);
393
394 b.setNonce(100);
395 BOOST_CHECK_EQUAL(a == b, true);
396 BOOST_CHECK_EQUAL(a != b, false);
397
398 // compare InterestLifetime
399 a.setInterestLifetime(time::seconds(10));
400 BOOST_CHECK_EQUAL(a == b, false);
401 BOOST_CHECK_EQUAL(a != b, true);
402
403 b.setInterestLifetime(time::seconds(10));
404 BOOST_CHECK_EQUAL(a == b, true);
405 BOOST_CHECK_EQUAL(a != b, false);
406
407 ///\todo #4055 compare ForwardingHint
408
409 // compare Link
410 a.setLink(Block(LINK, sizeof(LINK)));
411 BOOST_CHECK_EQUAL(a == b, false);
412 BOOST_CHECK_EQUAL(a != b, true);
413
414 b.setLink(Block(LINK, sizeof(LINK)));
415 BOOST_CHECK_EQUAL(a == b, true);
416 BOOST_CHECK_EQUAL(a != b, false);
417
418 // compare SelectedDelegation
419 BOOST_CHECK_EQUAL(a.hasSelectedDelegation(), false);
420 BOOST_CHECK_EQUAL(b.hasSelectedDelegation(), false);
421
422 a.setSelectedDelegation(Name("/local"));
423 BOOST_CHECK_EQUAL(a == b, false);
424 BOOST_CHECK_EQUAL(a != b, true);
425
426 b.setSelectedDelegation(Name("/local"));
427 BOOST_CHECK_EQUAL(a == b, true);
428 BOOST_CHECK_EQUAL(a != b, false);
429}
430
431BOOST_FIXTURE_TEST_SUITE(LinkSelectedDelegation, IdentityManagementFixture)
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -0800432
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700433const uint8_t InterestWithLink[] = {
434 0x05, 0xfb, // Interest
435 0x07, 0x14, // Name
436 0x08, 0x5, // NameComponent
437 0x6c, 0x6f, 0x63, 0x61, 0x6c,
438 0x08, 0x3, // NameComponent
439 0x6e, 0x64, 0x6e,
440 0x08, 0x6, // NameComponent
441 0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
442 0x0a, 0x4, // Nonce
443 0x1, 0x0, 0x0, 0x00,
444 0x06, 0xda, // Data
445 0x07, 0x14, // Name
446 0x08, 0x05,
447 0x6c, 0x6f, 0x63, 0x61, 0x6c,
448 0x08, 0x03,
449 0x6e, 0x64, 0x6e,
450 0x08, 0x06,
451 0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
452 0x14, 0x07, // MetaInfo
453 0x18, 0x01, // ContentType
454 0x01,
455 0x19, 0x02, // FreshnessPeriod
456 0x27, 0x10,
457 0x15, 0x1a, // Content
458 0x1f, 0x0c, // LinkDelegation
459 0x1e, 0x01, // LinkPreference
460 0x0a,
461 0x07, 0x07, // Name
462 0x08, 0x05,
463 0x6c, 0x6f, 0x63, 0x61, 0x6c,
464 0x1f, 0x0a, // LinkDelegation
465 0x1e, 0x01, // LinkPreference
466 0x14,
467 0x07, 0x05, // Name
468 0x08, 0x03,
469 0x6e, 0x64, 0x6e,
470 0x16, 0x1b, // SignatureInfo
471 0x1b, 0x01, // SignatureType
472 0x01,
473 0x1c, 0x16, // KeyLocator
474 0x07, 0x14, // Name
475 0x08, 0x04,
476 0x74, 0x65, 0x73, 0x74,
477 0x08, 0x03,
478 0x6b, 0x65, 0x79,
479 0x08, 0x07,
480 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
481 0x17, 0x80, // SignatureValue
482 0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe, 0xb1, 0x6f, 0x3e, 0x31, 0xec,
483 0xe3, 0xb9, 0xea, 0x83, 0x30, 0x40, 0x03, 0xfc, 0xa0, 0x13, 0xd9, 0xb3, 0xc6,
484 0x25, 0x16, 0x2d, 0xa6, 0x58, 0x41, 0x69, 0x62, 0x56, 0xd8, 0xb3, 0x6a, 0x38,
485 0x76, 0x56, 0xea, 0x61, 0xb2, 0x32, 0x70, 0x1c, 0xb6, 0x4d, 0x10, 0x1d, 0xdc,
486 0x92, 0x8e, 0x52, 0xa5, 0x8a, 0x1d, 0xd9, 0x96, 0x5e, 0xc0, 0x62, 0x0b, 0xcf,
487 0x3a, 0x9d, 0x7f, 0xca, 0xbe, 0xa1, 0x41, 0x71, 0x85, 0x7a, 0x8b, 0x5d, 0xa9,
488 0x64, 0xd6, 0x66, 0xb4, 0xe9, 0x8d, 0x0c, 0x28, 0x43, 0xee, 0xa6, 0x64, 0xe8,
489 0x55, 0xf6, 0x1c, 0x19, 0x0b, 0xef, 0x99, 0x25, 0x1e, 0xdc, 0x78, 0xb3, 0xa7,
490 0xaa, 0x0d, 0x14, 0x58, 0x30, 0xe5, 0x37, 0x6a, 0x6d, 0xdb, 0x56, 0xac, 0xa3,
491 0xfc, 0x90, 0x7a, 0xb8, 0x66, 0x9c, 0x0e, 0xf6, 0xb7, 0x64, 0xd1,
492 0x20, 0x01, // SelectedDelegation
493 0x00
494};
495
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700496const uint8_t InterestWithSelectedDelegationButNoLink[] = {
497 0x05, 0x1f, // Interest
498 0x07, 0x14, // Name
499 0x08, 0x5, // NameComponent
500 0x6c, 0x6f, 0x63, 0x61, 0x6c,
501 0x08, 0x3, // NameComponent
502 0x6e, 0x64, 0x6e,
503 0x08, 0x6, // NameComponent
504 0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
505 0x0a, 0x4, // Nonce
506 0x1, 0x0, 0x0, 0x00,
507 0x20, 0x01, // SelectedDelegation
508 0x00
509};
510
511const uint8_t InterestWithLinkNotNonIntegerSelectedDelegation[] = {
512 0x05, 0xfb, // Interest
513 0x07, 0x14, // Name
514 0x08, 0x5, // NameComponent
515 0x6c, 0x6f, 0x63, 0x61, 0x6c,
516 0x08, 0x3, // NameComponent
517 0x6e, 0x64, 0x6e,
518 0x08, 0x6, // NameComponent
519 0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
520 0x0a, 0x4, // Nonce
521 0x1, 0x0, 0x0, 0x00,
522 0x06, 0xda, // Data
523 0x07, 0x14, // Name
524 0x08, 0x05,
525 0x6c, 0x6f, 0x63, 0x61, 0x6c,
526 0x08, 0x03,
527 0x6e, 0x64, 0x6e,
528 0x08, 0x06,
529 0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
530 0x14, 0x07, // MetaInfo
531 0x18, 0x01, // ContentType
532 0x01,
533 0x19, 0x02, // FreshnessPeriod
534 0x27, 0x10,
535 0x15, 0x1a, // Content
536 0x1f, 0x0c, // LinkDelegation
537 0x1e, 0x01, // LinkPreference
538 0x0a,
539 0x07, 0x07, // Name
540 0x08, 0x05,
541 0x6c, 0x6f, 0x63, 0x61, 0x6c,
542 0x1f, 0x0a, // LinkDelegation
543 0x1e, 0x01, // LinkPreference
544 0x14,
545 0x07, 0x05, // Name
546 0x08, 0x03,
547 0x6e, 0x64, 0x6e,
548 0x16, 0x1b, // SignatureInfo
549 0x1b, 0x01, // SignatureType
550 0x01,
551 0x1c, 0x16, // KeyLocator
552 0x07, 0x14, // Name
553 0x08, 0x04,
554 0x74, 0x65, 0x73, 0x74,
555 0x08, 0x03,
556 0x6b, 0x65, 0x79,
557 0x08, 0x07,
558 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
559 0x17, 0x78, // SignatureValue
560 0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe, 0xb1, 0x6f, 0x3e, 0x31, 0xec,
561 0xe3, 0xb9, 0xea, 0x83, 0x30, 0x40, 0x03, 0xfc, 0xa0, 0x13, 0xd9, 0xb3, 0xc6,
562 0x25, 0x16, 0x2d, 0xa6, 0x58, 0x41, 0x69, 0x62, 0x56, 0xd8, 0xb3, 0x6a, 0x38,
563 0x76, 0x56, 0xea, 0x61, 0xb2, 0x32, 0x70, 0x1c, 0xb6, 0x4d, 0x10, 0x1d, 0xdc,
564 0x92, 0x8e, 0x52, 0xa5, 0x8a, 0x1d, 0xd9, 0x96, 0x5e, 0xc0, 0x62, 0x0b, 0xcf,
565 0x3a, 0x9d, 0x7f, 0xca, 0xbe, 0xa1, 0x41, 0x71, 0x85, 0x7a, 0x8b, 0x5d, 0xa9,
566 0x64, 0xd6, 0x66, 0xb4, 0xe9, 0x8d, 0x0c, 0x28, 0x43, 0xee, 0xa6, 0x64, 0xe8,
567 0x55, 0xf6, 0x1c, 0x19, 0x0b, 0xef, 0x99, 0x25, 0x1e, 0xdc, 0x78, 0xb3, 0xa7,
568 0xaa, 0x0d, 0x14, 0x58, 0x30, 0xe5, 0x37, 0x6a, 0x6d, 0xdb, 0x56, 0xac, 0xa3,
569 0xfc, 0x90, 0x7a, 0xb8, 0x66, 0x9c, 0x0e, 0xf6, 0xb7,
570 0x20, 0x03, // SelectedDelegation
571 0xAA, 0xAA, 0xAA
572};
573
574const uint8_t InterestWithLinkNonDecreasingOrder[] = {
575 0x05, 0xfb, // Interest
576 0x07, 0x14, // Name
577 0x08, 0x5, // NameComponent
578 0x6c, 0x6f, 0x63, 0x61, 0x6c,
579 0x08, 0x3, // NameComponent
580 0x6e, 0x64, 0x6e,
581 0x08, 0x6, // NameComponent
582 0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
583 0x0a, 0x4, // Nonce
584 0x1, 0x0, 0x0, 0x00,
585 0x06, 0xda, // Data
586 0x07, 0x14, // Name
587 0x08, 0x05,
588 0x6c, 0x6f, 0x63, 0x61, 0x6c,
589 0x08, 0x03,
590 0x6e, 0x64, 0x6e,
591 0x08, 0x06,
592 0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
593 0x14, 0x07, // MetaInfo
594 0x18, 0x01, // ContentType
595 0x01,
596 0x19, 0x02, // FreshnessPeriod
597 0x27, 0x10,
598 0x15, 0x1a, // Content
599 0x1f, 0x0c, // LinkDelegation
600 0x1e, 0x01, // LinkPreference
601 0x14,
602 0x07, 0x07, // Name
603 0x08, 0x05,
604 0x6c, 0x6f, 0x63, 0x61, 0x6c,
605 0x1f, 0x0a, // LinkDelegation
606 0x1e, 0x01, // LinkPreference
607 0x0a,
608 0x07, 0x05, // Name
609 0x08, 0x03,
610 0x6e, 0x64, 0x6e,
611 0x16, 0x1b, // SignatureInfo
612 0x1b, 0x01, // SignatureType
613 0x01,
614 0x1c, 0x16, // KeyLocator
615 0x07, 0x14, // Name
616 0x08, 0x04,
617 0x74, 0x65, 0x73, 0x74,
618 0x08, 0x03,
619 0x6b, 0x65, 0x79,
620 0x08, 0x07,
621 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
622 0x17, 0x80, // SignatureValue
623 0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe, 0xb1, 0x6f, 0x3e, 0x31, 0xec,
624 0xe3, 0xb9, 0xea, 0x83, 0x30, 0x40, 0x03, 0xfc, 0xa0, 0x13, 0xd9, 0xb3, 0xc6,
625 0x25, 0x16, 0x2d, 0xa6, 0x58, 0x41, 0x69, 0x62, 0x56, 0xd8, 0xb3, 0x6a, 0x38,
626 0x76, 0x56, 0xea, 0x61, 0xb2, 0x32, 0x70, 0x1c, 0xb6, 0x4d, 0x10, 0x1d, 0xdc,
627 0x92, 0x8e, 0x52, 0xa5, 0x8a, 0x1d, 0xd9, 0x96, 0x5e, 0xc0, 0x62, 0x0b, 0xcf,
628 0x3a, 0x9d, 0x7f, 0xca, 0xbe, 0xa1, 0x41, 0x71, 0x85, 0x7a, 0x8b, 0x5d, 0xa9,
629 0x64, 0xd6, 0x66, 0xb4, 0xe9, 0x8d, 0x0c, 0x28, 0x43, 0xee, 0xa6, 0x64, 0xe8,
630 0x55, 0xf6, 0x1c, 0x19, 0x0b, 0xef, 0x99, 0x25, 0x1e, 0xdc, 0x78, 0xb3, 0xa7,
631 0xaa, 0x0d, 0x14, 0x58, 0x30, 0xe5, 0x37, 0x6a, 0x6d, 0xdb, 0x56, 0xac, 0xa3,
632 0xfc, 0x90, 0x7a, 0xb8, 0x66, 0x9c, 0x0e, 0xf6, 0xb7, 0x64, 0xd1,
633 0x20, 0x01, // SelectedDelegation
634 0x01
635};
636
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700637BOOST_AUTO_TEST_CASE(LinkObject)
638{
Alexander Afanasyevcac08382015-09-02 14:52:40 -0700639 Link link1("test", {{100, "/test3"}, {20, "/test2"}, {10, "/test1"}});
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700640 m_keyChain.sign(link1);
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700641 Block wire = link1.wireEncode();
642
643 Interest a;
644 BOOST_REQUIRE_NO_THROW(a.setLink(wire));
645
646 BOOST_REQUIRE_NO_THROW(a.getLink());
647
648 Link link2 = a.getLink();
649 Name name = link2.getName();
650 BOOST_CHECK_EQUAL(Name("test"), name);
651 BOOST_CHECK_EQUAL(a.hasLink(), true);
652 Link::DelegationSet delegations;
653 delegations = link2.getDelegations();
654
655 auto i = delegations.begin();
656 BOOST_CHECK_EQUAL(std::get<0>(*i), 10);
657 BOOST_CHECK_EQUAL(std::get<1>(*i), Name("test1"));
658 ++i;
659 BOOST_CHECK_EQUAL(std::get<0>(*i), 20);
660 BOOST_CHECK_EQUAL(std::get<1>(*i), Name("test2"));
661 ++i;
662 BOOST_CHECK_EQUAL(std::get<0>(*i), 100);
663 BOOST_CHECK_EQUAL(std::get<1>(*i), Name("test3"));
664
Alexander Afanasyevcac08382015-09-02 14:52:40 -0700665 a.setLink(Block(LINK, sizeof(LINK)));
666 BOOST_CHECK_EQUAL(a.getLink().getDelegations().size(), 2);
667
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700668 a.unsetLink();
669 BOOST_CHECK_EQUAL(a.hasLink(), false);
670}
671
672BOOST_AUTO_TEST_CASE(SelectedDelegationChecks)
673{
674 Link link("test", {{10, "/test1"}, {20, "/test2"}, {100, "/test3"}});
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700675 m_keyChain.sign(link);
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700676 Block wire = link.wireEncode();
677
678 Interest a;
679 a.setLink(wire);
680 BOOST_CHECK_EQUAL(a.hasSelectedDelegation(), false);
681
682 BOOST_REQUIRE_NO_THROW(a.setSelectedDelegation(Name("test2")));
683 BOOST_CHECK_EQUAL(a.getSelectedDelegation(), Name("test2"));
684
685 BOOST_REQUIRE_NO_THROW(a.setSelectedDelegation(uint32_t(2)));
686 BOOST_CHECK_EQUAL(a.getSelectedDelegation(), Name("test3"));
687
688 a.unsetSelectedDelegation();
689 BOOST_CHECK_EQUAL(a.hasSelectedDelegation(), false);
690}
691
692BOOST_AUTO_TEST_CASE(EncodeDecodeWithLink)
693{
694 Link link1("test", {{10, "/test1"}, {20, "/test2"}, {100, "/test3"}});
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700695 m_keyChain.sign(link1);
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700696 Block wire = link1.wireEncode();
697
698 Interest a;
699 a.setName("/Test/Encode/Decode/With/Link");
700 a.setChildSelector(1);
701 a.setNonce(100);
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700702 a.setInterestLifetime(time::seconds(10));
703 a.setLink(wire);
704
705 Block interestBlock = a.wireEncode();
706 Interest b(interestBlock);
707
708 BOOST_CHECK_EQUAL(a == b, true);
709
710 Link link2 = b.getLink();
711 Link::DelegationSet delegations;
712 delegations = link2.getDelegations();
713
714 auto i = delegations.begin();
715 BOOST_CHECK_EQUAL(std::get<0>(*i), 10);
716 BOOST_CHECK_EQUAL(std::get<1>(*i), Name("test1"));
717 ++i;
718 BOOST_CHECK_EQUAL(std::get<0>(*i), 20);
719 BOOST_CHECK_EQUAL(std::get<1>(*i), Name("test2"));
720 ++i;
721 BOOST_CHECK_EQUAL(std::get<0>(*i), 100);
722 BOOST_CHECK_EQUAL(std::get<1>(*i), Name("test3"));
723
724}
725
726BOOST_AUTO_TEST_CASE(DecodeInterestWithLink)
727{
728 Block interestBlock(InterestWithLink, sizeof(InterestWithLink));
729
730 ndn::Interest i;
731 BOOST_REQUIRE_NO_THROW(i.wireDecode(interestBlock));
732 Link link = i.getLink();
733 BOOST_CHECK_EQUAL(link.getName(), Name("/local/ndn/prefix"));
734 Link::DelegationSet delegations = link.getDelegations();
735
736 auto it = delegations.begin();
737 BOOST_CHECK_EQUAL(std::get<0>(*it), 10);
738 BOOST_CHECK_EQUAL(std::get<1>(*it), Name("local"));
739 ++it;
740 BOOST_CHECK_EQUAL(std::get<0>(*it), 20);
741 BOOST_CHECK_EQUAL(std::get<1>(*it), Name("ndn"));
742
743 BOOST_REQUIRE_NO_THROW(i.getSelectedDelegation());
744 BOOST_CHECK_EQUAL(i.getSelectedDelegation(), Name("local"));
745}
746
747BOOST_AUTO_TEST_CASE(DecodeInterestWithLinkNonDecreasingOrder)
748{
749 Block interestBlock(InterestWithLinkNonDecreasingOrder,
750 sizeof(InterestWithLinkNonDecreasingOrder));
751
752 ndn::Interest i;
753 BOOST_REQUIRE_NO_THROW(i.wireDecode(interestBlock));
754 BOOST_REQUIRE_NO_THROW(i.getSelectedDelegation());
755 BOOST_CHECK_EQUAL(i.getSelectedDelegation(), Name("ndn"));
756}
757
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700758BOOST_AUTO_TEST_CASE(InterestContainingSelectedDelegationButNoLink)
759{
760 Block interestBlock(InterestWithSelectedDelegationButNoLink,
761 sizeof(InterestWithSelectedDelegationButNoLink));
762
763 ndn::Interest i;
Alexander Afanasyevcac08382015-09-02 14:52:40 -0700764 BOOST_CHECK_THROW(i.wireDecode(interestBlock), Interest::Error);
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700765}
766
767BOOST_AUTO_TEST_CASE(SelectedDelegationIsNotNonNegativeInteger)
768{
769 Block interestBlock(InterestWithLinkNotNonIntegerSelectedDelegation,
770 sizeof(InterestWithLinkNotNonIntegerSelectedDelegation));
771
772 ndn::Interest i;
Alexander Afanasyevcac08382015-09-02 14:52:40 -0700773 BOOST_CHECK_THROW(i.wireDecode(interestBlock), tlv::Error);
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700774}
775
776BOOST_AUTO_TEST_CASE(SelectedDelegationEqualToDelegationCount)
777{
778 Link link1("test", {{10, "/test1"}, {20, "/test2"}, {100, "/test3"}});
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700779 m_keyChain.sign(link1);
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700780 Block wire = link1.wireEncode();
781
782 Interest a;
783 a.setName("/Test/Encode/Decode/With/Link");
784 a.setChildSelector(1);
785 a.setNonce(100);
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700786 a.setInterestLifetime(time::seconds(10));
787 a.setLink(wire);
Alexander Afanasyevcac08382015-09-02 14:52:40 -0700788 BOOST_CHECK_THROW(a.setSelectedDelegation(3), Interest::Error);
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700789}
790
791BOOST_AUTO_TEST_CASE(SelectedDelegationGreaterThanDelegationCount)
792{
793 Link link1("test", {{10, "/test1"}, {20, "/test2"}, {100, "/test3"}});
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700794 m_keyChain.sign(link1);
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700795 Block wire = link1.wireEncode();
796
797 Interest a;
798 a.setName("/Test/Encode/Decode/With/Link");
799 a.setChildSelector(1);
800 a.setNonce(100);
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700801 a.setInterestLifetime(time::seconds(10));
802 a.setLink(wire);
Alexander Afanasyevcac08382015-09-02 14:52:40 -0700803 BOOST_CHECK_THROW(a.setSelectedDelegation(4), Interest::Error);
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700804}
805
Junxiao Shi899277a2017-07-07 22:12:12 +0000806BOOST_AUTO_TEST_SUITE_END() // LinkSelectedDelegation
Alexander Afanasyev90164962014-03-06 08:29:59 +0000807
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100808BOOST_AUTO_TEST_SUITE_END() // TestInterest
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800809
Alexander Afanasyev90164962014-03-06 08:29:59 +0000810} // namespace tests
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800811} // namespace ndn