blob: d0345a4150869712b526bfb8ec7d851358878c07 [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
Junxiao Shib2a70332017-07-07 22:15:03 +0000358BOOST_AUTO_TEST_CASE(ModifyForwardingHint)
359{
360 Interest i;
361 i.setForwardingHint({{1, "/A"}});
362 i.wireEncode();
363 BOOST_CHECK(i.hasWire());
364
365 i.modifyForwardingHint([] (DelegationList& fh) { fh.insert(2, "/B"); });
366 BOOST_CHECK(!i.hasWire());
367 BOOST_CHECK_EQUAL(i.getForwardingHint(), DelegationList({{1, "/A"}, {2, "/B"}}));
368}
369
Junxiao Shi899277a2017-07-07 22:12:12 +0000370// ---- operators ----
371
372BOOST_AUTO_TEST_CASE(Equality)
373{
374 Interest a;
375 Interest b;
376
377 // if nonce is not set, it would be set to a random value
378 a.setNonce(1);
379 b.setNonce(1);
380
381 BOOST_CHECK_EQUAL(a == b, true);
382 BOOST_CHECK_EQUAL(a != b, false);
383
384 // compare Name
385 a.setName("/A");
386 BOOST_CHECK_EQUAL(a == b, false);
387 BOOST_CHECK_EQUAL(a != b, true);
388
389 b.setName("/B");
390 BOOST_CHECK_EQUAL(a == b, false);
391 BOOST_CHECK_EQUAL(a != b, true);
392
393 b.setName("/A");
394 BOOST_CHECK_EQUAL(a == b, true);
395 BOOST_CHECK_EQUAL(a != b, false);
396
397 // compare Selectors
398 a.setChildSelector(1);
399 BOOST_CHECK_EQUAL(a == b, false);
400 BOOST_CHECK_EQUAL(a != b, true);
401
402 b.setChildSelector(1);
403 BOOST_CHECK_EQUAL(a == b, true);
404 BOOST_CHECK_EQUAL(a != b, false);
405
406 // compare Nonce
407 a.setNonce(100);
408 BOOST_CHECK_EQUAL(a == b, false);
409 BOOST_CHECK_EQUAL(a != b, true);
410
411 b.setNonce(100);
412 BOOST_CHECK_EQUAL(a == b, true);
413 BOOST_CHECK_EQUAL(a != b, false);
414
415 // compare InterestLifetime
416 a.setInterestLifetime(time::seconds(10));
417 BOOST_CHECK_EQUAL(a == b, false);
418 BOOST_CHECK_EQUAL(a != b, true);
419
420 b.setInterestLifetime(time::seconds(10));
421 BOOST_CHECK_EQUAL(a == b, true);
422 BOOST_CHECK_EQUAL(a != b, false);
423
424 ///\todo #4055 compare ForwardingHint
425
426 // compare Link
427 a.setLink(Block(LINK, sizeof(LINK)));
428 BOOST_CHECK_EQUAL(a == b, false);
429 BOOST_CHECK_EQUAL(a != b, true);
430
431 b.setLink(Block(LINK, sizeof(LINK)));
432 BOOST_CHECK_EQUAL(a == b, true);
433 BOOST_CHECK_EQUAL(a != b, false);
434
435 // compare SelectedDelegation
436 BOOST_CHECK_EQUAL(a.hasSelectedDelegation(), false);
437 BOOST_CHECK_EQUAL(b.hasSelectedDelegation(), false);
438
439 a.setSelectedDelegation(Name("/local"));
440 BOOST_CHECK_EQUAL(a == b, false);
441 BOOST_CHECK_EQUAL(a != b, true);
442
443 b.setSelectedDelegation(Name("/local"));
444 BOOST_CHECK_EQUAL(a == b, true);
445 BOOST_CHECK_EQUAL(a != b, false);
446}
447
448BOOST_FIXTURE_TEST_SUITE(LinkSelectedDelegation, IdentityManagementFixture)
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -0800449
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700450const uint8_t InterestWithLink[] = {
451 0x05, 0xfb, // Interest
452 0x07, 0x14, // Name
453 0x08, 0x5, // NameComponent
454 0x6c, 0x6f, 0x63, 0x61, 0x6c,
455 0x08, 0x3, // NameComponent
456 0x6e, 0x64, 0x6e,
457 0x08, 0x6, // NameComponent
458 0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
459 0x0a, 0x4, // Nonce
460 0x1, 0x0, 0x0, 0x00,
461 0x06, 0xda, // Data
462 0x07, 0x14, // Name
463 0x08, 0x05,
464 0x6c, 0x6f, 0x63, 0x61, 0x6c,
465 0x08, 0x03,
466 0x6e, 0x64, 0x6e,
467 0x08, 0x06,
468 0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
469 0x14, 0x07, // MetaInfo
470 0x18, 0x01, // ContentType
471 0x01,
472 0x19, 0x02, // FreshnessPeriod
473 0x27, 0x10,
474 0x15, 0x1a, // Content
475 0x1f, 0x0c, // LinkDelegation
476 0x1e, 0x01, // LinkPreference
477 0x0a,
478 0x07, 0x07, // Name
479 0x08, 0x05,
480 0x6c, 0x6f, 0x63, 0x61, 0x6c,
481 0x1f, 0x0a, // LinkDelegation
482 0x1e, 0x01, // LinkPreference
483 0x14,
484 0x07, 0x05, // Name
485 0x08, 0x03,
486 0x6e, 0x64, 0x6e,
487 0x16, 0x1b, // SignatureInfo
488 0x1b, 0x01, // SignatureType
489 0x01,
490 0x1c, 0x16, // KeyLocator
491 0x07, 0x14, // Name
492 0x08, 0x04,
493 0x74, 0x65, 0x73, 0x74,
494 0x08, 0x03,
495 0x6b, 0x65, 0x79,
496 0x08, 0x07,
497 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
498 0x17, 0x80, // SignatureValue
499 0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe, 0xb1, 0x6f, 0x3e, 0x31, 0xec,
500 0xe3, 0xb9, 0xea, 0x83, 0x30, 0x40, 0x03, 0xfc, 0xa0, 0x13, 0xd9, 0xb3, 0xc6,
501 0x25, 0x16, 0x2d, 0xa6, 0x58, 0x41, 0x69, 0x62, 0x56, 0xd8, 0xb3, 0x6a, 0x38,
502 0x76, 0x56, 0xea, 0x61, 0xb2, 0x32, 0x70, 0x1c, 0xb6, 0x4d, 0x10, 0x1d, 0xdc,
503 0x92, 0x8e, 0x52, 0xa5, 0x8a, 0x1d, 0xd9, 0x96, 0x5e, 0xc0, 0x62, 0x0b, 0xcf,
504 0x3a, 0x9d, 0x7f, 0xca, 0xbe, 0xa1, 0x41, 0x71, 0x85, 0x7a, 0x8b, 0x5d, 0xa9,
505 0x64, 0xd6, 0x66, 0xb4, 0xe9, 0x8d, 0x0c, 0x28, 0x43, 0xee, 0xa6, 0x64, 0xe8,
506 0x55, 0xf6, 0x1c, 0x19, 0x0b, 0xef, 0x99, 0x25, 0x1e, 0xdc, 0x78, 0xb3, 0xa7,
507 0xaa, 0x0d, 0x14, 0x58, 0x30, 0xe5, 0x37, 0x6a, 0x6d, 0xdb, 0x56, 0xac, 0xa3,
508 0xfc, 0x90, 0x7a, 0xb8, 0x66, 0x9c, 0x0e, 0xf6, 0xb7, 0x64, 0xd1,
509 0x20, 0x01, // SelectedDelegation
510 0x00
511};
512
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700513const uint8_t InterestWithSelectedDelegationButNoLink[] = {
514 0x05, 0x1f, // Interest
515 0x07, 0x14, // Name
516 0x08, 0x5, // NameComponent
517 0x6c, 0x6f, 0x63, 0x61, 0x6c,
518 0x08, 0x3, // NameComponent
519 0x6e, 0x64, 0x6e,
520 0x08, 0x6, // NameComponent
521 0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
522 0x0a, 0x4, // Nonce
523 0x1, 0x0, 0x0, 0x00,
524 0x20, 0x01, // SelectedDelegation
525 0x00
526};
527
528const uint8_t InterestWithLinkNotNonIntegerSelectedDelegation[] = {
529 0x05, 0xfb, // Interest
530 0x07, 0x14, // Name
531 0x08, 0x5, // NameComponent
532 0x6c, 0x6f, 0x63, 0x61, 0x6c,
533 0x08, 0x3, // NameComponent
534 0x6e, 0x64, 0x6e,
535 0x08, 0x6, // NameComponent
536 0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
537 0x0a, 0x4, // Nonce
538 0x1, 0x0, 0x0, 0x00,
539 0x06, 0xda, // Data
540 0x07, 0x14, // Name
541 0x08, 0x05,
542 0x6c, 0x6f, 0x63, 0x61, 0x6c,
543 0x08, 0x03,
544 0x6e, 0x64, 0x6e,
545 0x08, 0x06,
546 0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
547 0x14, 0x07, // MetaInfo
548 0x18, 0x01, // ContentType
549 0x01,
550 0x19, 0x02, // FreshnessPeriod
551 0x27, 0x10,
552 0x15, 0x1a, // Content
553 0x1f, 0x0c, // LinkDelegation
554 0x1e, 0x01, // LinkPreference
555 0x0a,
556 0x07, 0x07, // Name
557 0x08, 0x05,
558 0x6c, 0x6f, 0x63, 0x61, 0x6c,
559 0x1f, 0x0a, // LinkDelegation
560 0x1e, 0x01, // LinkPreference
561 0x14,
562 0x07, 0x05, // Name
563 0x08, 0x03,
564 0x6e, 0x64, 0x6e,
565 0x16, 0x1b, // SignatureInfo
566 0x1b, 0x01, // SignatureType
567 0x01,
568 0x1c, 0x16, // KeyLocator
569 0x07, 0x14, // Name
570 0x08, 0x04,
571 0x74, 0x65, 0x73, 0x74,
572 0x08, 0x03,
573 0x6b, 0x65, 0x79,
574 0x08, 0x07,
575 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
576 0x17, 0x78, // SignatureValue
577 0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe, 0xb1, 0x6f, 0x3e, 0x31, 0xec,
578 0xe3, 0xb9, 0xea, 0x83, 0x30, 0x40, 0x03, 0xfc, 0xa0, 0x13, 0xd9, 0xb3, 0xc6,
579 0x25, 0x16, 0x2d, 0xa6, 0x58, 0x41, 0x69, 0x62, 0x56, 0xd8, 0xb3, 0x6a, 0x38,
580 0x76, 0x56, 0xea, 0x61, 0xb2, 0x32, 0x70, 0x1c, 0xb6, 0x4d, 0x10, 0x1d, 0xdc,
581 0x92, 0x8e, 0x52, 0xa5, 0x8a, 0x1d, 0xd9, 0x96, 0x5e, 0xc0, 0x62, 0x0b, 0xcf,
582 0x3a, 0x9d, 0x7f, 0xca, 0xbe, 0xa1, 0x41, 0x71, 0x85, 0x7a, 0x8b, 0x5d, 0xa9,
583 0x64, 0xd6, 0x66, 0xb4, 0xe9, 0x8d, 0x0c, 0x28, 0x43, 0xee, 0xa6, 0x64, 0xe8,
584 0x55, 0xf6, 0x1c, 0x19, 0x0b, 0xef, 0x99, 0x25, 0x1e, 0xdc, 0x78, 0xb3, 0xa7,
585 0xaa, 0x0d, 0x14, 0x58, 0x30, 0xe5, 0x37, 0x6a, 0x6d, 0xdb, 0x56, 0xac, 0xa3,
586 0xfc, 0x90, 0x7a, 0xb8, 0x66, 0x9c, 0x0e, 0xf6, 0xb7,
587 0x20, 0x03, // SelectedDelegation
588 0xAA, 0xAA, 0xAA
589};
590
591const uint8_t InterestWithLinkNonDecreasingOrder[] = {
592 0x05, 0xfb, // Interest
593 0x07, 0x14, // Name
594 0x08, 0x5, // NameComponent
595 0x6c, 0x6f, 0x63, 0x61, 0x6c,
596 0x08, 0x3, // NameComponent
597 0x6e, 0x64, 0x6e,
598 0x08, 0x6, // NameComponent
599 0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
600 0x0a, 0x4, // Nonce
601 0x1, 0x0, 0x0, 0x00,
602 0x06, 0xda, // Data
603 0x07, 0x14, // Name
604 0x08, 0x05,
605 0x6c, 0x6f, 0x63, 0x61, 0x6c,
606 0x08, 0x03,
607 0x6e, 0x64, 0x6e,
608 0x08, 0x06,
609 0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
610 0x14, 0x07, // MetaInfo
611 0x18, 0x01, // ContentType
612 0x01,
613 0x19, 0x02, // FreshnessPeriod
614 0x27, 0x10,
615 0x15, 0x1a, // Content
616 0x1f, 0x0c, // LinkDelegation
617 0x1e, 0x01, // LinkPreference
618 0x14,
619 0x07, 0x07, // Name
620 0x08, 0x05,
621 0x6c, 0x6f, 0x63, 0x61, 0x6c,
622 0x1f, 0x0a, // LinkDelegation
623 0x1e, 0x01, // LinkPreference
624 0x0a,
625 0x07, 0x05, // Name
626 0x08, 0x03,
627 0x6e, 0x64, 0x6e,
628 0x16, 0x1b, // SignatureInfo
629 0x1b, 0x01, // SignatureType
630 0x01,
631 0x1c, 0x16, // KeyLocator
632 0x07, 0x14, // Name
633 0x08, 0x04,
634 0x74, 0x65, 0x73, 0x74,
635 0x08, 0x03,
636 0x6b, 0x65, 0x79,
637 0x08, 0x07,
638 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
639 0x17, 0x80, // SignatureValue
640 0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe, 0xb1, 0x6f, 0x3e, 0x31, 0xec,
641 0xe3, 0xb9, 0xea, 0x83, 0x30, 0x40, 0x03, 0xfc, 0xa0, 0x13, 0xd9, 0xb3, 0xc6,
642 0x25, 0x16, 0x2d, 0xa6, 0x58, 0x41, 0x69, 0x62, 0x56, 0xd8, 0xb3, 0x6a, 0x38,
643 0x76, 0x56, 0xea, 0x61, 0xb2, 0x32, 0x70, 0x1c, 0xb6, 0x4d, 0x10, 0x1d, 0xdc,
644 0x92, 0x8e, 0x52, 0xa5, 0x8a, 0x1d, 0xd9, 0x96, 0x5e, 0xc0, 0x62, 0x0b, 0xcf,
645 0x3a, 0x9d, 0x7f, 0xca, 0xbe, 0xa1, 0x41, 0x71, 0x85, 0x7a, 0x8b, 0x5d, 0xa9,
646 0x64, 0xd6, 0x66, 0xb4, 0xe9, 0x8d, 0x0c, 0x28, 0x43, 0xee, 0xa6, 0x64, 0xe8,
647 0x55, 0xf6, 0x1c, 0x19, 0x0b, 0xef, 0x99, 0x25, 0x1e, 0xdc, 0x78, 0xb3, 0xa7,
648 0xaa, 0x0d, 0x14, 0x58, 0x30, 0xe5, 0x37, 0x6a, 0x6d, 0xdb, 0x56, 0xac, 0xa3,
649 0xfc, 0x90, 0x7a, 0xb8, 0x66, 0x9c, 0x0e, 0xf6, 0xb7, 0x64, 0xd1,
650 0x20, 0x01, // SelectedDelegation
651 0x01
652};
653
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700654BOOST_AUTO_TEST_CASE(LinkObject)
655{
Alexander Afanasyevcac08382015-09-02 14:52:40 -0700656 Link link1("test", {{100, "/test3"}, {20, "/test2"}, {10, "/test1"}});
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700657 m_keyChain.sign(link1);
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700658 Block wire = link1.wireEncode();
659
660 Interest a;
661 BOOST_REQUIRE_NO_THROW(a.setLink(wire));
662
663 BOOST_REQUIRE_NO_THROW(a.getLink());
664
665 Link link2 = a.getLink();
666 Name name = link2.getName();
667 BOOST_CHECK_EQUAL(Name("test"), name);
668 BOOST_CHECK_EQUAL(a.hasLink(), true);
669 Link::DelegationSet delegations;
670 delegations = link2.getDelegations();
671
672 auto i = delegations.begin();
673 BOOST_CHECK_EQUAL(std::get<0>(*i), 10);
674 BOOST_CHECK_EQUAL(std::get<1>(*i), Name("test1"));
675 ++i;
676 BOOST_CHECK_EQUAL(std::get<0>(*i), 20);
677 BOOST_CHECK_EQUAL(std::get<1>(*i), Name("test2"));
678 ++i;
679 BOOST_CHECK_EQUAL(std::get<0>(*i), 100);
680 BOOST_CHECK_EQUAL(std::get<1>(*i), Name("test3"));
681
Alexander Afanasyevcac08382015-09-02 14:52:40 -0700682 a.setLink(Block(LINK, sizeof(LINK)));
683 BOOST_CHECK_EQUAL(a.getLink().getDelegations().size(), 2);
684
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700685 a.unsetLink();
686 BOOST_CHECK_EQUAL(a.hasLink(), false);
687}
688
689BOOST_AUTO_TEST_CASE(SelectedDelegationChecks)
690{
691 Link link("test", {{10, "/test1"}, {20, "/test2"}, {100, "/test3"}});
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700692 m_keyChain.sign(link);
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700693 Block wire = link.wireEncode();
694
695 Interest a;
696 a.setLink(wire);
697 BOOST_CHECK_EQUAL(a.hasSelectedDelegation(), false);
698
699 BOOST_REQUIRE_NO_THROW(a.setSelectedDelegation(Name("test2")));
700 BOOST_CHECK_EQUAL(a.getSelectedDelegation(), Name("test2"));
701
702 BOOST_REQUIRE_NO_THROW(a.setSelectedDelegation(uint32_t(2)));
703 BOOST_CHECK_EQUAL(a.getSelectedDelegation(), Name("test3"));
704
705 a.unsetSelectedDelegation();
706 BOOST_CHECK_EQUAL(a.hasSelectedDelegation(), false);
707}
708
709BOOST_AUTO_TEST_CASE(EncodeDecodeWithLink)
710{
711 Link link1("test", {{10, "/test1"}, {20, "/test2"}, {100, "/test3"}});
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700712 m_keyChain.sign(link1);
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700713 Block wire = link1.wireEncode();
714
715 Interest a;
716 a.setName("/Test/Encode/Decode/With/Link");
717 a.setChildSelector(1);
718 a.setNonce(100);
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700719 a.setInterestLifetime(time::seconds(10));
720 a.setLink(wire);
721
722 Block interestBlock = a.wireEncode();
723 Interest b(interestBlock);
724
725 BOOST_CHECK_EQUAL(a == b, true);
726
727 Link link2 = b.getLink();
728 Link::DelegationSet delegations;
729 delegations = link2.getDelegations();
730
731 auto i = delegations.begin();
732 BOOST_CHECK_EQUAL(std::get<0>(*i), 10);
733 BOOST_CHECK_EQUAL(std::get<1>(*i), Name("test1"));
734 ++i;
735 BOOST_CHECK_EQUAL(std::get<0>(*i), 20);
736 BOOST_CHECK_EQUAL(std::get<1>(*i), Name("test2"));
737 ++i;
738 BOOST_CHECK_EQUAL(std::get<0>(*i), 100);
739 BOOST_CHECK_EQUAL(std::get<1>(*i), Name("test3"));
740
741}
742
743BOOST_AUTO_TEST_CASE(DecodeInterestWithLink)
744{
745 Block interestBlock(InterestWithLink, sizeof(InterestWithLink));
746
747 ndn::Interest i;
748 BOOST_REQUIRE_NO_THROW(i.wireDecode(interestBlock));
749 Link link = i.getLink();
750 BOOST_CHECK_EQUAL(link.getName(), Name("/local/ndn/prefix"));
751 Link::DelegationSet delegations = link.getDelegations();
752
753 auto it = delegations.begin();
754 BOOST_CHECK_EQUAL(std::get<0>(*it), 10);
755 BOOST_CHECK_EQUAL(std::get<1>(*it), Name("local"));
756 ++it;
757 BOOST_CHECK_EQUAL(std::get<0>(*it), 20);
758 BOOST_CHECK_EQUAL(std::get<1>(*it), Name("ndn"));
759
760 BOOST_REQUIRE_NO_THROW(i.getSelectedDelegation());
761 BOOST_CHECK_EQUAL(i.getSelectedDelegation(), Name("local"));
762}
763
764BOOST_AUTO_TEST_CASE(DecodeInterestWithLinkNonDecreasingOrder)
765{
766 Block interestBlock(InterestWithLinkNonDecreasingOrder,
767 sizeof(InterestWithLinkNonDecreasingOrder));
768
769 ndn::Interest i;
770 BOOST_REQUIRE_NO_THROW(i.wireDecode(interestBlock));
771 BOOST_REQUIRE_NO_THROW(i.getSelectedDelegation());
772 BOOST_CHECK_EQUAL(i.getSelectedDelegation(), Name("ndn"));
773}
774
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700775BOOST_AUTO_TEST_CASE(InterestContainingSelectedDelegationButNoLink)
776{
777 Block interestBlock(InterestWithSelectedDelegationButNoLink,
778 sizeof(InterestWithSelectedDelegationButNoLink));
779
780 ndn::Interest i;
Alexander Afanasyevcac08382015-09-02 14:52:40 -0700781 BOOST_CHECK_THROW(i.wireDecode(interestBlock), Interest::Error);
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700782}
783
784BOOST_AUTO_TEST_CASE(SelectedDelegationIsNotNonNegativeInteger)
785{
786 Block interestBlock(InterestWithLinkNotNonIntegerSelectedDelegation,
787 sizeof(InterestWithLinkNotNonIntegerSelectedDelegation));
788
789 ndn::Interest i;
Alexander Afanasyevcac08382015-09-02 14:52:40 -0700790 BOOST_CHECK_THROW(i.wireDecode(interestBlock), tlv::Error);
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700791}
792
793BOOST_AUTO_TEST_CASE(SelectedDelegationEqualToDelegationCount)
794{
795 Link link1("test", {{10, "/test1"}, {20, "/test2"}, {100, "/test3"}});
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700796 m_keyChain.sign(link1);
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700797 Block wire = link1.wireEncode();
798
799 Interest a;
800 a.setName("/Test/Encode/Decode/With/Link");
801 a.setChildSelector(1);
802 a.setNonce(100);
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700803 a.setInterestLifetime(time::seconds(10));
804 a.setLink(wire);
Alexander Afanasyevcac08382015-09-02 14:52:40 -0700805 BOOST_CHECK_THROW(a.setSelectedDelegation(3), Interest::Error);
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700806}
807
808BOOST_AUTO_TEST_CASE(SelectedDelegationGreaterThanDelegationCount)
809{
810 Link link1("test", {{10, "/test1"}, {20, "/test2"}, {100, "/test3"}});
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700811 m_keyChain.sign(link1);
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700812 Block wire = link1.wireEncode();
813
814 Interest a;
815 a.setName("/Test/Encode/Decode/With/Link");
816 a.setChildSelector(1);
817 a.setNonce(100);
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700818 a.setInterestLifetime(time::seconds(10));
819 a.setLink(wire);
Alexander Afanasyevcac08382015-09-02 14:52:40 -0700820 BOOST_CHECK_THROW(a.setSelectedDelegation(4), Interest::Error);
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700821}
822
Junxiao Shi899277a2017-07-07 22:12:12 +0000823BOOST_AUTO_TEST_SUITE_END() // LinkSelectedDelegation
Alexander Afanasyev90164962014-03-06 08:29:59 +0000824
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100825BOOST_AUTO_TEST_SUITE_END() // TestInterest
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800826
Alexander Afanasyev90164962014-03-06 08:29:59 +0000827} // namespace tests
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800828} // namespace ndn