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