blob: a4c78c0afd1ba3d164260e516dd6f4d5fa6ec1b9 [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"));
Junxiao Shidb7464d2017-07-13 03:11:17 +0000203 signature.setValue(encoding::makeEmptyBlock(tlv::SignatureValue));
Junxiao Shi899277a2017-07-07 22:12:12 +0000204 data.setSignature(signature);
205 data.wireEncode();
206 BOOST_CHECK_EQUAL(interest.matchesData(data), true);
207
208 Data data1 = data;
209 data1.setName("ndn:/A"); // violates MinSuffixComponents
210 data1.wireEncode();
211 BOOST_CHECK_EQUAL(interest.matchesData(data1), false);
212
213 Interest interest1 = interest;
214 interest1.setMinSuffixComponents(1);
215 BOOST_CHECK_EQUAL(interest1.matchesData(data1), true);
216
217 Data data2 = data;
218 data2.setName("ndn:/A/E/F"); // violates MaxSuffixComponents
219 data2.wireEncode();
220 BOOST_CHECK_EQUAL(interest.matchesData(data2), false);
221
222 Interest interest2 = interest;
223 interest2.setMaxSuffixComponents(3);
224 BOOST_CHECK_EQUAL(interest2.matchesData(data2), true);
225
226 Data data3 = data;
227 SignatureSha256WithRsa signature3(KeyLocator("ndn:/G")); // violates PublisherPublicKeyLocator
Junxiao Shidb7464d2017-07-13 03:11:17 +0000228 signature3.setValue(encoding::makeEmptyBlock(tlv::SignatureValue));
Junxiao Shi899277a2017-07-07 22:12:12 +0000229 data3.setSignature(signature3);
230 data3.wireEncode();
231 BOOST_CHECK_EQUAL(interest.matchesData(data3), false);
232
233 Interest interest3 = interest;
234 interest3.setPublisherPublicKeyLocator(KeyLocator("ndn:/G"));
235 BOOST_CHECK_EQUAL(interest3.matchesData(data3), true);
236
237 Data data4 = data;
238 DigestSha256 signature4; // violates PublisherPublicKeyLocator
Junxiao Shidb7464d2017-07-13 03:11:17 +0000239 signature4.setValue(encoding::makeEmptyBlock(tlv::SignatureValue));
Junxiao Shi899277a2017-07-07 22:12:12 +0000240 data4.setSignature(signature4);
241 data4.wireEncode();
242 BOOST_CHECK_EQUAL(interest.matchesData(data4), false);
243
244 Interest interest4 = interest;
245 interest4.setPublisherPublicKeyLocator(KeyLocator());
246 BOOST_CHECK_EQUAL(interest4.matchesData(data4), true);
247
248 Data data5 = data;
249 data5.setName("ndn:/A/J"); // violates Exclude
250 data5.wireEncode();
251 BOOST_CHECK_EQUAL(interest.matchesData(data5), false);
252
253 Interest interest5 = interest;
254 interest5.setExclude(Exclude().excludeAfter(name::Component("K")));
255 BOOST_CHECK_EQUAL(interest5.matchesData(data5), true);
256
257 Data data6 = data;
258 data6.setName("ndn:/H/I"); // violates Name
259 data6.wireEncode();
260 BOOST_CHECK_EQUAL(interest.matchesData(data6), false);
261
262 Data data7 = data;
263 data7.setName("ndn:/A/B");
264 data7.wireEncode();
265
Junxiao Shidb7464d2017-07-13 03:11:17 +0000266 Interest interest7("/A/B/sha256digest=54008e240a7eea2714a161dfddf0dd6ced223b3856e9da96792151e180f3b128");
Junxiao Shi899277a2017-07-07 22:12:12 +0000267 BOOST_CHECK_EQUAL(interest7.matchesData(data7), true);
268
269 Interest interest7b("/A/B/sha256digest=0000000000000000000000000000000000000000000000000000000000000000");
270 BOOST_CHECK_EQUAL(interest7b.matchesData(data7), false); // violates implicit digest
271}
272
273BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(MatchesInterest, 1)
274BOOST_AUTO_TEST_CASE(MatchesInterest)
275{
276 Interest interest;
277 interest
278 .setName("/A")
279 .setMinSuffixComponents(2)
280 .setMaxSuffixComponents(2)
281 .setPublisherPublicKeyLocator(KeyLocator("/B"))
282 .setExclude(Exclude().excludeAfter(name::Component("J")))
283 .setNonce(10)
284 .setInterestLifetime(time::seconds(5))
285 .setLink(Block(LINK, sizeof(LINK)));
286
287 Interest other;
288 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false);
289
290 other.setName(interest.getName());
291 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false);
292
293 other.setSelectors(interest.getSelectors());
294 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false); // will match until #3162 implemented
295
296 other.setLink(interest.getLink().wireEncode());
297 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
298
299 other.setNonce(200);
300 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
301
302 other.setInterestLifetime(time::hours(5));
303 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
304
305 other.setSelectedDelegation(0);
306 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
307}
308
309// ---- field accessors ----
310
311BOOST_AUTO_TEST_CASE(GetNonce)
312{
313 unique_ptr<Interest> i;
314
315 // getNonce automatically assigns a random Nonce, which could be zero.
316 // But it's unlikely to get 100 zeros in a row.
317 uint32_t nonce = 0;
318 int nIterations = 0;
319 do {
320 i = make_unique<Interest>();
321 nonce = i->getNonce();
322 }
323 while (nonce == 0 && ++nIterations < 100);
324 BOOST_CHECK_NE(nonce, 0);
325 BOOST_CHECK(i->hasNonce());
326
327 // Once a Nonce is assigned, it should not change.
328 BOOST_CHECK_EQUAL(i->getNonce(), nonce);
329}
330
331BOOST_AUTO_TEST_CASE(RefreshNonce)
332{
333 Interest i;
334 BOOST_CHECK(!i.hasNonce());
335 i.refreshNonce();
336 BOOST_CHECK(!i.hasNonce());
337
338 i.setNonce(1);
339 BOOST_CHECK(i.hasNonce());
340 i.refreshNonce();
341 BOOST_CHECK(i.hasNonce());
342 BOOST_CHECK_NE(i.getNonce(), 1);
343}
344
345BOOST_AUTO_TEST_CASE(SetInterestLifetime)
346{
347 BOOST_CHECK_THROW(Interest("/A", time::milliseconds(-1)), std::invalid_argument);
348 BOOST_CHECK_NO_THROW(Interest("/A", time::milliseconds(0)));
349
350 Interest i("/local/ndn/prefix");
351 i.setNonce(1);
352 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
353 BOOST_CHECK_THROW(i.setInterestLifetime(time::milliseconds(-1)), std::invalid_argument);
354 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
355 i.setInterestLifetime(time::milliseconds(0));
356 BOOST_CHECK_EQUAL(i.getInterestLifetime(), time::milliseconds(0));
357 i.setInterestLifetime(time::milliseconds(1));
358 BOOST_CHECK_EQUAL(i.getInterestLifetime(), time::milliseconds(1));
359}
360
Junxiao Shib2a70332017-07-07 22:15:03 +0000361BOOST_AUTO_TEST_CASE(ModifyForwardingHint)
362{
363 Interest i;
364 i.setForwardingHint({{1, "/A"}});
365 i.wireEncode();
366 BOOST_CHECK(i.hasWire());
367
368 i.modifyForwardingHint([] (DelegationList& fh) { fh.insert(2, "/B"); });
369 BOOST_CHECK(!i.hasWire());
370 BOOST_CHECK_EQUAL(i.getForwardingHint(), DelegationList({{1, "/A"}, {2, "/B"}}));
371}
372
Junxiao Shi899277a2017-07-07 22:12:12 +0000373// ---- operators ----
374
375BOOST_AUTO_TEST_CASE(Equality)
376{
377 Interest a;
378 Interest b;
379
380 // if nonce is not set, it would be set to a random value
381 a.setNonce(1);
382 b.setNonce(1);
383
384 BOOST_CHECK_EQUAL(a == b, true);
385 BOOST_CHECK_EQUAL(a != b, false);
386
387 // compare Name
388 a.setName("/A");
389 BOOST_CHECK_EQUAL(a == b, false);
390 BOOST_CHECK_EQUAL(a != b, true);
391
392 b.setName("/B");
393 BOOST_CHECK_EQUAL(a == b, false);
394 BOOST_CHECK_EQUAL(a != b, true);
395
396 b.setName("/A");
397 BOOST_CHECK_EQUAL(a == b, true);
398 BOOST_CHECK_EQUAL(a != b, false);
399
400 // compare Selectors
401 a.setChildSelector(1);
402 BOOST_CHECK_EQUAL(a == b, false);
403 BOOST_CHECK_EQUAL(a != b, true);
404
405 b.setChildSelector(1);
406 BOOST_CHECK_EQUAL(a == b, true);
407 BOOST_CHECK_EQUAL(a != b, false);
408
409 // compare Nonce
410 a.setNonce(100);
411 BOOST_CHECK_EQUAL(a == b, false);
412 BOOST_CHECK_EQUAL(a != b, true);
413
414 b.setNonce(100);
415 BOOST_CHECK_EQUAL(a == b, true);
416 BOOST_CHECK_EQUAL(a != b, false);
417
418 // compare InterestLifetime
419 a.setInterestLifetime(time::seconds(10));
420 BOOST_CHECK_EQUAL(a == b, false);
421 BOOST_CHECK_EQUAL(a != b, true);
422
423 b.setInterestLifetime(time::seconds(10));
424 BOOST_CHECK_EQUAL(a == b, true);
425 BOOST_CHECK_EQUAL(a != b, false);
426
427 ///\todo #4055 compare ForwardingHint
428
429 // compare Link
430 a.setLink(Block(LINK, sizeof(LINK)));
431 BOOST_CHECK_EQUAL(a == b, false);
432 BOOST_CHECK_EQUAL(a != b, true);
433
434 b.setLink(Block(LINK, sizeof(LINK)));
435 BOOST_CHECK_EQUAL(a == b, true);
436 BOOST_CHECK_EQUAL(a != b, false);
437
438 // compare SelectedDelegation
439 BOOST_CHECK_EQUAL(a.hasSelectedDelegation(), false);
440 BOOST_CHECK_EQUAL(b.hasSelectedDelegation(), false);
441
442 a.setSelectedDelegation(Name("/local"));
443 BOOST_CHECK_EQUAL(a == b, false);
444 BOOST_CHECK_EQUAL(a != b, true);
445
446 b.setSelectedDelegation(Name("/local"));
447 BOOST_CHECK_EQUAL(a == b, true);
448 BOOST_CHECK_EQUAL(a != b, false);
449}
450
451BOOST_FIXTURE_TEST_SUITE(LinkSelectedDelegation, IdentityManagementFixture)
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -0800452
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700453const uint8_t InterestWithLink[] = {
454 0x05, 0xfb, // Interest
455 0x07, 0x14, // Name
456 0x08, 0x5, // NameComponent
457 0x6c, 0x6f, 0x63, 0x61, 0x6c,
458 0x08, 0x3, // NameComponent
459 0x6e, 0x64, 0x6e,
460 0x08, 0x6, // NameComponent
461 0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
462 0x0a, 0x4, // Nonce
463 0x1, 0x0, 0x0, 0x00,
464 0x06, 0xda, // Data
465 0x07, 0x14, // Name
466 0x08, 0x05,
467 0x6c, 0x6f, 0x63, 0x61, 0x6c,
468 0x08, 0x03,
469 0x6e, 0x64, 0x6e,
470 0x08, 0x06,
471 0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
472 0x14, 0x07, // MetaInfo
473 0x18, 0x01, // ContentType
474 0x01,
475 0x19, 0x02, // FreshnessPeriod
476 0x27, 0x10,
477 0x15, 0x1a, // Content
478 0x1f, 0x0c, // LinkDelegation
479 0x1e, 0x01, // LinkPreference
480 0x0a,
481 0x07, 0x07, // Name
482 0x08, 0x05,
483 0x6c, 0x6f, 0x63, 0x61, 0x6c,
484 0x1f, 0x0a, // LinkDelegation
485 0x1e, 0x01, // LinkPreference
486 0x14,
487 0x07, 0x05, // Name
488 0x08, 0x03,
489 0x6e, 0x64, 0x6e,
490 0x16, 0x1b, // SignatureInfo
491 0x1b, 0x01, // SignatureType
492 0x01,
493 0x1c, 0x16, // KeyLocator
494 0x07, 0x14, // Name
495 0x08, 0x04,
496 0x74, 0x65, 0x73, 0x74,
497 0x08, 0x03,
498 0x6b, 0x65, 0x79,
499 0x08, 0x07,
500 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
501 0x17, 0x80, // SignatureValue
502 0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe, 0xb1, 0x6f, 0x3e, 0x31, 0xec,
503 0xe3, 0xb9, 0xea, 0x83, 0x30, 0x40, 0x03, 0xfc, 0xa0, 0x13, 0xd9, 0xb3, 0xc6,
504 0x25, 0x16, 0x2d, 0xa6, 0x58, 0x41, 0x69, 0x62, 0x56, 0xd8, 0xb3, 0x6a, 0x38,
505 0x76, 0x56, 0xea, 0x61, 0xb2, 0x32, 0x70, 0x1c, 0xb6, 0x4d, 0x10, 0x1d, 0xdc,
506 0x92, 0x8e, 0x52, 0xa5, 0x8a, 0x1d, 0xd9, 0x96, 0x5e, 0xc0, 0x62, 0x0b, 0xcf,
507 0x3a, 0x9d, 0x7f, 0xca, 0xbe, 0xa1, 0x41, 0x71, 0x85, 0x7a, 0x8b, 0x5d, 0xa9,
508 0x64, 0xd6, 0x66, 0xb4, 0xe9, 0x8d, 0x0c, 0x28, 0x43, 0xee, 0xa6, 0x64, 0xe8,
509 0x55, 0xf6, 0x1c, 0x19, 0x0b, 0xef, 0x99, 0x25, 0x1e, 0xdc, 0x78, 0xb3, 0xa7,
510 0xaa, 0x0d, 0x14, 0x58, 0x30, 0xe5, 0x37, 0x6a, 0x6d, 0xdb, 0x56, 0xac, 0xa3,
511 0xfc, 0x90, 0x7a, 0xb8, 0x66, 0x9c, 0x0e, 0xf6, 0xb7, 0x64, 0xd1,
512 0x20, 0x01, // SelectedDelegation
513 0x00
514};
515
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700516const uint8_t InterestWithSelectedDelegationButNoLink[] = {
517 0x05, 0x1f, // 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 0x20, 0x01, // SelectedDelegation
528 0x00
529};
530
531const uint8_t InterestWithLinkNotNonIntegerSelectedDelegation[] = {
532 0x05, 0xfb, // Interest
533 0x07, 0x14, // Name
534 0x08, 0x5, // NameComponent
535 0x6c, 0x6f, 0x63, 0x61, 0x6c,
536 0x08, 0x3, // NameComponent
537 0x6e, 0x64, 0x6e,
538 0x08, 0x6, // NameComponent
539 0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
540 0x0a, 0x4, // Nonce
541 0x1, 0x0, 0x0, 0x00,
542 0x06, 0xda, // Data
543 0x07, 0x14, // Name
544 0x08, 0x05,
545 0x6c, 0x6f, 0x63, 0x61, 0x6c,
546 0x08, 0x03,
547 0x6e, 0x64, 0x6e,
548 0x08, 0x06,
549 0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
550 0x14, 0x07, // MetaInfo
551 0x18, 0x01, // ContentType
552 0x01,
553 0x19, 0x02, // FreshnessPeriod
554 0x27, 0x10,
555 0x15, 0x1a, // Content
556 0x1f, 0x0c, // LinkDelegation
557 0x1e, 0x01, // LinkPreference
558 0x0a,
559 0x07, 0x07, // Name
560 0x08, 0x05,
561 0x6c, 0x6f, 0x63, 0x61, 0x6c,
562 0x1f, 0x0a, // LinkDelegation
563 0x1e, 0x01, // LinkPreference
564 0x14,
565 0x07, 0x05, // Name
566 0x08, 0x03,
567 0x6e, 0x64, 0x6e,
568 0x16, 0x1b, // SignatureInfo
569 0x1b, 0x01, // SignatureType
570 0x01,
571 0x1c, 0x16, // KeyLocator
572 0x07, 0x14, // Name
573 0x08, 0x04,
574 0x74, 0x65, 0x73, 0x74,
575 0x08, 0x03,
576 0x6b, 0x65, 0x79,
577 0x08, 0x07,
578 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
579 0x17, 0x78, // SignatureValue
580 0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe, 0xb1, 0x6f, 0x3e, 0x31, 0xec,
581 0xe3, 0xb9, 0xea, 0x83, 0x30, 0x40, 0x03, 0xfc, 0xa0, 0x13, 0xd9, 0xb3, 0xc6,
582 0x25, 0x16, 0x2d, 0xa6, 0x58, 0x41, 0x69, 0x62, 0x56, 0xd8, 0xb3, 0x6a, 0x38,
583 0x76, 0x56, 0xea, 0x61, 0xb2, 0x32, 0x70, 0x1c, 0xb6, 0x4d, 0x10, 0x1d, 0xdc,
584 0x92, 0x8e, 0x52, 0xa5, 0x8a, 0x1d, 0xd9, 0x96, 0x5e, 0xc0, 0x62, 0x0b, 0xcf,
585 0x3a, 0x9d, 0x7f, 0xca, 0xbe, 0xa1, 0x41, 0x71, 0x85, 0x7a, 0x8b, 0x5d, 0xa9,
586 0x64, 0xd6, 0x66, 0xb4, 0xe9, 0x8d, 0x0c, 0x28, 0x43, 0xee, 0xa6, 0x64, 0xe8,
587 0x55, 0xf6, 0x1c, 0x19, 0x0b, 0xef, 0x99, 0x25, 0x1e, 0xdc, 0x78, 0xb3, 0xa7,
588 0xaa, 0x0d, 0x14, 0x58, 0x30, 0xe5, 0x37, 0x6a, 0x6d, 0xdb, 0x56, 0xac, 0xa3,
589 0xfc, 0x90, 0x7a, 0xb8, 0x66, 0x9c, 0x0e, 0xf6, 0xb7,
590 0x20, 0x03, // SelectedDelegation
591 0xAA, 0xAA, 0xAA
592};
593
594const uint8_t InterestWithLinkNonDecreasingOrder[] = {
595 0x05, 0xfb, // Interest
596 0x07, 0x14, // Name
597 0x08, 0x5, // NameComponent
598 0x6c, 0x6f, 0x63, 0x61, 0x6c,
599 0x08, 0x3, // NameComponent
600 0x6e, 0x64, 0x6e,
601 0x08, 0x6, // NameComponent
602 0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
603 0x0a, 0x4, // Nonce
604 0x1, 0x0, 0x0, 0x00,
605 0x06, 0xda, // Data
606 0x07, 0x14, // Name
607 0x08, 0x05,
608 0x6c, 0x6f, 0x63, 0x61, 0x6c,
609 0x08, 0x03,
610 0x6e, 0x64, 0x6e,
611 0x08, 0x06,
612 0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
613 0x14, 0x07, // MetaInfo
614 0x18, 0x01, // ContentType
615 0x01,
616 0x19, 0x02, // FreshnessPeriod
617 0x27, 0x10,
618 0x15, 0x1a, // Content
619 0x1f, 0x0c, // LinkDelegation
620 0x1e, 0x01, // LinkPreference
621 0x14,
622 0x07, 0x07, // Name
623 0x08, 0x05,
624 0x6c, 0x6f, 0x63, 0x61, 0x6c,
625 0x1f, 0x0a, // LinkDelegation
626 0x1e, 0x01, // LinkPreference
627 0x0a,
628 0x07, 0x05, // Name
629 0x08, 0x03,
630 0x6e, 0x64, 0x6e,
631 0x16, 0x1b, // SignatureInfo
632 0x1b, 0x01, // SignatureType
633 0x01,
634 0x1c, 0x16, // KeyLocator
635 0x07, 0x14, // Name
636 0x08, 0x04,
637 0x74, 0x65, 0x73, 0x74,
638 0x08, 0x03,
639 0x6b, 0x65, 0x79,
640 0x08, 0x07,
641 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
642 0x17, 0x80, // SignatureValue
643 0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe, 0xb1, 0x6f, 0x3e, 0x31, 0xec,
644 0xe3, 0xb9, 0xea, 0x83, 0x30, 0x40, 0x03, 0xfc, 0xa0, 0x13, 0xd9, 0xb3, 0xc6,
645 0x25, 0x16, 0x2d, 0xa6, 0x58, 0x41, 0x69, 0x62, 0x56, 0xd8, 0xb3, 0x6a, 0x38,
646 0x76, 0x56, 0xea, 0x61, 0xb2, 0x32, 0x70, 0x1c, 0xb6, 0x4d, 0x10, 0x1d, 0xdc,
647 0x92, 0x8e, 0x52, 0xa5, 0x8a, 0x1d, 0xd9, 0x96, 0x5e, 0xc0, 0x62, 0x0b, 0xcf,
648 0x3a, 0x9d, 0x7f, 0xca, 0xbe, 0xa1, 0x41, 0x71, 0x85, 0x7a, 0x8b, 0x5d, 0xa9,
649 0x64, 0xd6, 0x66, 0xb4, 0xe9, 0x8d, 0x0c, 0x28, 0x43, 0xee, 0xa6, 0x64, 0xe8,
650 0x55, 0xf6, 0x1c, 0x19, 0x0b, 0xef, 0x99, 0x25, 0x1e, 0xdc, 0x78, 0xb3, 0xa7,
651 0xaa, 0x0d, 0x14, 0x58, 0x30, 0xe5, 0x37, 0x6a, 0x6d, 0xdb, 0x56, 0xac, 0xa3,
652 0xfc, 0x90, 0x7a, 0xb8, 0x66, 0x9c, 0x0e, 0xf6, 0xb7, 0x64, 0xd1,
653 0x20, 0x01, // SelectedDelegation
654 0x01
655};
656
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700657BOOST_AUTO_TEST_CASE(LinkObject)
658{
Alexander Afanasyevcac08382015-09-02 14:52:40 -0700659 Link link1("test", {{100, "/test3"}, {20, "/test2"}, {10, "/test1"}});
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700660 m_keyChain.sign(link1);
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700661 Block wire = link1.wireEncode();
662
663 Interest a;
664 BOOST_REQUIRE_NO_THROW(a.setLink(wire));
665
666 BOOST_REQUIRE_NO_THROW(a.getLink());
667
668 Link link2 = a.getLink();
669 Name name = link2.getName();
670 BOOST_CHECK_EQUAL(Name("test"), name);
671 BOOST_CHECK_EQUAL(a.hasLink(), true);
672 Link::DelegationSet delegations;
673 delegations = link2.getDelegations();
674
675 auto i = delegations.begin();
676 BOOST_CHECK_EQUAL(std::get<0>(*i), 10);
677 BOOST_CHECK_EQUAL(std::get<1>(*i), Name("test1"));
678 ++i;
679 BOOST_CHECK_EQUAL(std::get<0>(*i), 20);
680 BOOST_CHECK_EQUAL(std::get<1>(*i), Name("test2"));
681 ++i;
682 BOOST_CHECK_EQUAL(std::get<0>(*i), 100);
683 BOOST_CHECK_EQUAL(std::get<1>(*i), Name("test3"));
684
Alexander Afanasyevcac08382015-09-02 14:52:40 -0700685 a.setLink(Block(LINK, sizeof(LINK)));
686 BOOST_CHECK_EQUAL(a.getLink().getDelegations().size(), 2);
687
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700688 a.unsetLink();
689 BOOST_CHECK_EQUAL(a.hasLink(), false);
690}
691
692BOOST_AUTO_TEST_CASE(SelectedDelegationChecks)
693{
694 Link link("test", {{10, "/test1"}, {20, "/test2"}, {100, "/test3"}});
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700695 m_keyChain.sign(link);
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700696 Block wire = link.wireEncode();
697
698 Interest a;
699 a.setLink(wire);
700 BOOST_CHECK_EQUAL(a.hasSelectedDelegation(), false);
701
702 BOOST_REQUIRE_NO_THROW(a.setSelectedDelegation(Name("test2")));
703 BOOST_CHECK_EQUAL(a.getSelectedDelegation(), Name("test2"));
704
705 BOOST_REQUIRE_NO_THROW(a.setSelectedDelegation(uint32_t(2)));
706 BOOST_CHECK_EQUAL(a.getSelectedDelegation(), Name("test3"));
707
708 a.unsetSelectedDelegation();
709 BOOST_CHECK_EQUAL(a.hasSelectedDelegation(), false);
710}
711
712BOOST_AUTO_TEST_CASE(EncodeDecodeWithLink)
713{
714 Link link1("test", {{10, "/test1"}, {20, "/test2"}, {100, "/test3"}});
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700715 m_keyChain.sign(link1);
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700716 Block wire = link1.wireEncode();
717
718 Interest a;
719 a.setName("/Test/Encode/Decode/With/Link");
720 a.setChildSelector(1);
721 a.setNonce(100);
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700722 a.setInterestLifetime(time::seconds(10));
723 a.setLink(wire);
724
725 Block interestBlock = a.wireEncode();
726 Interest b(interestBlock);
727
728 BOOST_CHECK_EQUAL(a == b, true);
729
730 Link link2 = b.getLink();
731 Link::DelegationSet delegations;
732 delegations = link2.getDelegations();
733
734 auto i = delegations.begin();
735 BOOST_CHECK_EQUAL(std::get<0>(*i), 10);
736 BOOST_CHECK_EQUAL(std::get<1>(*i), Name("test1"));
737 ++i;
738 BOOST_CHECK_EQUAL(std::get<0>(*i), 20);
739 BOOST_CHECK_EQUAL(std::get<1>(*i), Name("test2"));
740 ++i;
741 BOOST_CHECK_EQUAL(std::get<0>(*i), 100);
742 BOOST_CHECK_EQUAL(std::get<1>(*i), Name("test3"));
743
744}
745
746BOOST_AUTO_TEST_CASE(DecodeInterestWithLink)
747{
748 Block interestBlock(InterestWithLink, sizeof(InterestWithLink));
749
750 ndn::Interest i;
751 BOOST_REQUIRE_NO_THROW(i.wireDecode(interestBlock));
752 Link link = i.getLink();
753 BOOST_CHECK_EQUAL(link.getName(), Name("/local/ndn/prefix"));
754 Link::DelegationSet delegations = link.getDelegations();
755
756 auto it = delegations.begin();
757 BOOST_CHECK_EQUAL(std::get<0>(*it), 10);
758 BOOST_CHECK_EQUAL(std::get<1>(*it), Name("local"));
759 ++it;
760 BOOST_CHECK_EQUAL(std::get<0>(*it), 20);
761 BOOST_CHECK_EQUAL(std::get<1>(*it), Name("ndn"));
762
763 BOOST_REQUIRE_NO_THROW(i.getSelectedDelegation());
764 BOOST_CHECK_EQUAL(i.getSelectedDelegation(), Name("local"));
765}
766
767BOOST_AUTO_TEST_CASE(DecodeInterestWithLinkNonDecreasingOrder)
768{
769 Block interestBlock(InterestWithLinkNonDecreasingOrder,
770 sizeof(InterestWithLinkNonDecreasingOrder));
771
772 ndn::Interest i;
773 BOOST_REQUIRE_NO_THROW(i.wireDecode(interestBlock));
774 BOOST_REQUIRE_NO_THROW(i.getSelectedDelegation());
775 BOOST_CHECK_EQUAL(i.getSelectedDelegation(), Name("ndn"));
776}
777
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700778BOOST_AUTO_TEST_CASE(InterestContainingSelectedDelegationButNoLink)
779{
780 Block interestBlock(InterestWithSelectedDelegationButNoLink,
781 sizeof(InterestWithSelectedDelegationButNoLink));
782
783 ndn::Interest i;
Alexander Afanasyevcac08382015-09-02 14:52:40 -0700784 BOOST_CHECK_THROW(i.wireDecode(interestBlock), Interest::Error);
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700785}
786
787BOOST_AUTO_TEST_CASE(SelectedDelegationIsNotNonNegativeInteger)
788{
789 Block interestBlock(InterestWithLinkNotNonIntegerSelectedDelegation,
790 sizeof(InterestWithLinkNotNonIntegerSelectedDelegation));
791
792 ndn::Interest i;
Alexander Afanasyevcac08382015-09-02 14:52:40 -0700793 BOOST_CHECK_THROW(i.wireDecode(interestBlock), tlv::Error);
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700794}
795
796BOOST_AUTO_TEST_CASE(SelectedDelegationEqualToDelegationCount)
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(3), Interest::Error);
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700809}
810
811BOOST_AUTO_TEST_CASE(SelectedDelegationGreaterThanDelegationCount)
812{
813 Link link1("test", {{10, "/test1"}, {20, "/test2"}, {100, "/test3"}});
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700814 m_keyChain.sign(link1);
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700815 Block wire = link1.wireEncode();
816
817 Interest a;
818 a.setName("/Test/Encode/Decode/With/Link");
819 a.setChildSelector(1);
820 a.setNonce(100);
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700821 a.setInterestLifetime(time::seconds(10));
822 a.setLink(wire);
Alexander Afanasyevcac08382015-09-02 14:52:40 -0700823 BOOST_CHECK_THROW(a.setSelectedDelegation(4), Interest::Error);
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700824}
825
Junxiao Shi899277a2017-07-07 22:12:12 +0000826BOOST_AUTO_TEST_SUITE_END() // LinkSelectedDelegation
Alexander Afanasyev90164962014-03-06 08:29:59 +0000827
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100828BOOST_AUTO_TEST_SUITE_END() // TestInterest
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800829
Alexander Afanasyev90164962014-03-06 08:29:59 +0000830} // namespace tests
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800831} // namespace ndn