blob: f1e79dcff85c31f387b75d9ee387673881e45808 [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/*
Davide Pesavento0f830802018-01-16 23:58:58 -05003 * Copyright (c) 2013-2018 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
Junxiao Shi6efa3b72018-04-14 15:54:08 +000027#include "block-literal.hpp"
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070028#include "boost-test.hpp"
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070029#include "identity-management-fixture.hpp"
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080030
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080031namespace ndn {
Alexander Afanasyev90164962014-03-06 08:29:59 +000032namespace tests {
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080033
Junxiao Shi899277a2017-07-07 22:12:12 +000034BOOST_AUTO_TEST_SUITE(TestInterest)
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080035
Junxiao Shi899277a2017-07-07 22:12:12 +000036// ---- constructor, encode, decode ----
37
38BOOST_AUTO_TEST_CASE(DefaultConstructor)
39{
40 Interest i;
Junxiao Shi6efa3b72018-04-14 15:54:08 +000041 BOOST_CHECK(!i.hasWire());
Junxiao Shi899277a2017-07-07 22:12:12 +000042 BOOST_CHECK_EQUAL(i.getName(), "/");
Junxiao Shi6efa3b72018-04-14 15:54:08 +000043 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
44 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
45 BOOST_CHECK(i.getForwardingHint().empty());
46 BOOST_CHECK(!i.hasNonce());
Junxiao Shi899277a2017-07-07 22:12:12 +000047 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Junxiao Shi6efa3b72018-04-14 15:54:08 +000048 BOOST_CHECK(!i.hasSelectors());
Junxiao Shi899277a2017-07-07 22:12:12 +000049}
50
Junxiao Shi6efa3b72018-04-14 15:54:08 +000051BOOST_AUTO_TEST_CASE(EncodeDecode02Basic)
Junxiao Shi899277a2017-07-07 22:12:12 +000052{
53 const uint8_t WIRE[] = {
54 0x05, 0x1c, // Interest
55 0x07, 0x14, // Name
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +000056 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent
57 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent
58 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent
Junxiao Shi899277a2017-07-07 22:12:12 +000059 0x0a, 0x04, // Nonce
60 0x01, 0x00, 0x00, 0x00
61 };
62
63 Interest i1("/local/ndn/prefix");
64 i1.setNonce(1);
65 Block wire1 = i1.wireEncode();
66 BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
67
68 Interest i2(wire1);
69 BOOST_CHECK_EQUAL(i2.getName(), "/local/ndn/prefix");
70 BOOST_CHECK(i2.getSelectors().empty());
71 BOOST_CHECK_EQUAL(i2.getNonce(), 1);
72 BOOST_CHECK_EQUAL(i2.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
73
74 BOOST_CHECK_EQUAL(i1, i2);
75}
76
Junxiao Shi6efa3b72018-04-14 15:54:08 +000077BOOST_AUTO_TEST_CASE(EncodeDecode02Full)
Junxiao Shi899277a2017-07-07 22:12:12 +000078{
79 const uint8_t WIRE[] = {
Junxiao Shi9c154cb2017-07-07 22:14:54 +000080 0x05, 0x31, // Interest
Junxiao Shi899277a2017-07-07 22:12:12 +000081 0x07, 0x14, // Name
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +000082 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent
83 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent
84 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent
Junxiao Shi899277a2017-07-07 22:12:12 +000085 0x09, 0x03, // Selectors
86 0x0d, 0x01, 0x01, // MinSuffixComponents
87 0x0a, 0x04, // Nonce
88 0x01, 0x00, 0x00, 0x00,
89 0x0c, 0x02, // InterestLifetime
Junxiao Shi9c154cb2017-07-07 22:14:54 +000090 0x03, 0xe8,
91 0x1e, 0x0a, // ForwardingHint
92 0x1f, 0x08, // Delegation
93 0x1e, 0x01, 0x01, // Preference=1
94 0x07, 0x03, 0x08, 0x01, 0x41 // Name=/A
Junxiao Shi899277a2017-07-07 22:12:12 +000095 };
Junxiao Shi899277a2017-07-07 22:12:12 +000096
97 Interest i1;
98 i1.setName("/local/ndn/prefix");
99 i1.setMinSuffixComponents(1);
100 i1.setNonce(1);
Davide Pesavento0f830802018-01-16 23:58:58 -0500101 i1.setInterestLifetime(1000_ms);
Junxiao Shi9c154cb2017-07-07 22:14:54 +0000102 i1.setForwardingHint({{1, "/A"}});
Junxiao Shi899277a2017-07-07 22:12:12 +0000103 Block wire1 = i1.wireEncode();
104 BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
105
106 Interest i2(wire1);
107 BOOST_CHECK_EQUAL(i2.getName(), "/local/ndn/prefix");
108 BOOST_CHECK_EQUAL(i2.getMinSuffixComponents(), 1);
109 BOOST_CHECK_EQUAL(i2.getNonce(), 1);
Davide Pesavento0f830802018-01-16 23:58:58 -0500110 BOOST_CHECK_EQUAL(i2.getInterestLifetime(), 1000_ms);
Junxiao Shi9c154cb2017-07-07 22:14:54 +0000111 BOOST_CHECK_EQUAL(i2.getForwardingHint(), DelegationList({{1, "/A"}}));
Junxiao Shi899277a2017-07-07 22:12:12 +0000112
113 BOOST_CHECK_EQUAL(i1, i2);
114}
115
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000116class Decode03Fixture
Junxiao Shi899277a2017-07-07 22:12:12 +0000117{
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000118protected:
119 Decode03Fixture()
120 {
121 // initialize all elements to non-empty, to verify wireDecode clears them
122 i.setName("/A");
123 i.setForwardingHint({{10309, "/F"}});
124 i.setNonce(0x03d645a8);
125 i.setInterestLifetime(18554_ms);
126 i.setPublisherPublicKeyLocator(Name("/K"));
127 }
Junxiao Shi899277a2017-07-07 22:12:12 +0000128
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000129protected:
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000130 Interest i;
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000131};
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000132
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000133BOOST_FIXTURE_TEST_SUITE(Decode03, Decode03Fixture)
134
135BOOST_AUTO_TEST_CASE(Minimal)
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000136{
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000137 i.wireDecode("0505 0703080149"_block);
138 BOOST_CHECK_EQUAL(i.getName(), "/I");
139 BOOST_CHECK_EQUAL(i.getCanBePrefix(), false);
140 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
141 BOOST_CHECK(i.getForwardingHint().empty());
142 BOOST_CHECK(i.hasNonce()); // a random nonce is generated
143 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
144 BOOST_CHECK(i.getPublisherPublicKeyLocator().empty());
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000145
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000146 BOOST_CHECK(!i.hasWire()); // nonce generation resets wire encoding
147
148 // modify then re-encode as v0.2 format
149 i.setNonce(0x54657c95);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000150 BOOST_CHECK_EQUAL(i.wireEncode(), "0510 0703080149 09030E0101 0A04957C6554"_block);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000151}
152
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000153BOOST_AUTO_TEST_CASE(Full)
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000154{
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000155 i.wireDecode("053B FC00 0703080149 FC00 2100 FC00 1200 "
156 "FC00 1E0B(1F09 1E023E15 0703080148) FC00 0A044ACB1E4C "
157 "FC00 0C0276A1 FC00 2201D6 FC00 2304C0C1C2C3 FC00"_block);
158 BOOST_CHECK_EQUAL(i.getName(), "/I");
159 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
160 BOOST_CHECK_EQUAL(i.getMustBeFresh(), true);
161 BOOST_CHECK_EQUAL(i.getForwardingHint(), DelegationList({{15893, "/H"}}));
162 BOOST_CHECK(i.hasNonce());
163 BOOST_CHECK_EQUAL(i.getNonce(), 0x4c1ecb4a);
164 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 30369_ms);
165 // HopLimit=214 is not stored
166 // Parameters="C0C1C2C3" is not stored
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000167
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000168 // encode without modification: retain original wire encoding
169 BOOST_CHECK_EQUAL(i.wireEncode().value_size(), 59);
170
171 // modify then re-encode as v0.2 format
172 i.setName("/J");
Junxiao Shi72c0c642018-04-20 15:41:09 +0000173 BOOST_CHECK_EQUAL(i.wireEncode(),
174 "0520 070308014A 09021200 0A044ACB1E4C 0C0276A1 1E0B(1F09 1E023E15 0703080148)"_block);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000175}
176
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000177BOOST_AUTO_TEST_CASE(CriticalElementOutOfOrder)
178{
179 BOOST_CHECK_THROW(i.wireDecode(
180 "0529 2100 0703080149 1200 1E0B(1F09 1E023E15 0703080148) "
181 "0A044ACB1E4C 0C0276A1 2201D6 2304C0C1C2C3"_block),
182 tlv::Error);
183 BOOST_CHECK_THROW(i.wireDecode(
184 "0529 0703080149 1200 2100 1E0B(1F09 1E023E15 0703080148) "
185 "0A044ACB1E4C 0C0276A1 2201D6 2304C0C1C2C3"_block),
186 tlv::Error);
187 BOOST_CHECK_THROW(i.wireDecode(
188 "0529 0703080149 2100 1E0B(1F09 1E023E15 0703080148) 1200 "
189 "0A044ACB1E4C 0C0276A1 2201D6 2304C0C1C2C3"_block),
190 tlv::Error);
191 BOOST_CHECK_THROW(i.wireDecode(
192 "0529 0703080149 2100 1200 0A044ACB1E4C "
193 "1E0B(1F09 1E023E15 0703080148) 0C0276A1 2201D6 2304C0C1C2C3"_block),
194 tlv::Error);
195 BOOST_CHECK_THROW(i.wireDecode(
196 "0529 0703080149 2100 1200 1E0B(1F09 1E023E15 0703080148) "
197 "0C0276A1 0A044ACB1E4C 2201D6 2304C0C1C2C3"_block),
198 tlv::Error);
199 BOOST_CHECK_THROW(i.wireDecode(
200 "0529 0703080149 2100 1200 1E0B(1F09 1E023E15 0703080148) "
201 "0A044ACB1E4C 2201D6 0C0276A1 2304C0C1C2C3"_block),
202 tlv::Error);
203 BOOST_CHECK_THROW(i.wireDecode(
204 "052F 0703080149 2100 1200 1E0B(1F09 1E023E15 0703080148) "
205 "0A044ACB1E4C 0C0276A1 2201D6 2304C0C1C2C3 2304C0C1C2C3"_block),
206 tlv::Error);
207}
208
209BOOST_AUTO_TEST_CASE(HopLimitOutOfOrder)
210{
211 // HopLimit is non-critical, its out-of-order appearances are ignored
212 i.wireDecode("0514 0703080149 2201D6 2200 2304C0C1C2C3 22020101"_block);
213 BOOST_CHECK_EQUAL(i.getName(), "/I");
214 // HopLimit=214 is not stored
215 // Parameters="C0C1C2C3" is not stored
216}
217
218BOOST_AUTO_TEST_CASE(NameMissing)
219{
220 BOOST_CHECK_THROW(i.wireDecode("0500"_block), tlv::Error);
221 BOOST_CHECK_THROW(i.wireDecode("0502 1200"_block), tlv::Error);
222}
223
224BOOST_AUTO_TEST_CASE(NameEmpty)
225{
226 BOOST_CHECK_THROW(i.wireDecode("0502 0700"_block), tlv::Error);
227}
228
229BOOST_AUTO_TEST_CASE(BadCanBePrefix)
230{
231 BOOST_CHECK_THROW(i.wireDecode("0508 0703080149 210102"_block), tlv::Error);
232}
233
234BOOST_AUTO_TEST_CASE(BadMustBeFresh)
235{
236 BOOST_CHECK_THROW(i.wireDecode("0508 0703080149 120102"_block), tlv::Error);
237}
238
239BOOST_AUTO_TEST_CASE(BadNonce)
240{
241 BOOST_CHECK_THROW(i.wireDecode("0507 0703080149 0A00"_block), tlv::Error);
242 BOOST_CHECK_THROW(i.wireDecode("050A 0703080149 0A0304C263"_block), tlv::Error);
243 BOOST_CHECK_THROW(i.wireDecode("050C 0703080149 0A05EFA420B262"_block), tlv::Error);
244}
245
246BOOST_AUTO_TEST_CASE(UnrecognizedCriticalElement)
247{
248 BOOST_CHECK_THROW(i.wireDecode("0507 0703080149 FB00"_block), tlv::Error);
249}
250
251BOOST_AUTO_TEST_SUITE_END() // Decode03
252
Junxiao Shi899277a2017-07-07 22:12:12 +0000253// ---- matching ----
254
255BOOST_AUTO_TEST_CASE(MatchesData)
256{
257 Interest interest;
258 interest.setName("ndn:/A")
259 .setMinSuffixComponents(2)
260 .setMaxSuffixComponents(2)
261 .setPublisherPublicKeyLocator(KeyLocator("ndn:/B"))
262 .setExclude(Exclude().excludeAfter(name::Component("J")));
263
264 Data data("ndn:/A/D");
265 SignatureSha256WithRsa signature(KeyLocator("ndn:/B"));
Junxiao Shidb7464d2017-07-13 03:11:17 +0000266 signature.setValue(encoding::makeEmptyBlock(tlv::SignatureValue));
Junxiao Shi899277a2017-07-07 22:12:12 +0000267 data.setSignature(signature);
268 data.wireEncode();
269 BOOST_CHECK_EQUAL(interest.matchesData(data), true);
270
271 Data data1 = data;
272 data1.setName("ndn:/A"); // violates MinSuffixComponents
273 data1.wireEncode();
274 BOOST_CHECK_EQUAL(interest.matchesData(data1), false);
275
276 Interest interest1 = interest;
277 interest1.setMinSuffixComponents(1);
278 BOOST_CHECK_EQUAL(interest1.matchesData(data1), true);
279
280 Data data2 = data;
281 data2.setName("ndn:/A/E/F"); // violates MaxSuffixComponents
282 data2.wireEncode();
283 BOOST_CHECK_EQUAL(interest.matchesData(data2), false);
284
285 Interest interest2 = interest;
286 interest2.setMaxSuffixComponents(3);
287 BOOST_CHECK_EQUAL(interest2.matchesData(data2), true);
288
289 Data data3 = data;
290 SignatureSha256WithRsa signature3(KeyLocator("ndn:/G")); // violates PublisherPublicKeyLocator
Junxiao Shidb7464d2017-07-13 03:11:17 +0000291 signature3.setValue(encoding::makeEmptyBlock(tlv::SignatureValue));
Junxiao Shi899277a2017-07-07 22:12:12 +0000292 data3.setSignature(signature3);
293 data3.wireEncode();
294 BOOST_CHECK_EQUAL(interest.matchesData(data3), false);
295
296 Interest interest3 = interest;
297 interest3.setPublisherPublicKeyLocator(KeyLocator("ndn:/G"));
298 BOOST_CHECK_EQUAL(interest3.matchesData(data3), true);
299
300 Data data4 = data;
301 DigestSha256 signature4; // violates PublisherPublicKeyLocator
Junxiao Shidb7464d2017-07-13 03:11:17 +0000302 signature4.setValue(encoding::makeEmptyBlock(tlv::SignatureValue));
Junxiao Shi899277a2017-07-07 22:12:12 +0000303 data4.setSignature(signature4);
304 data4.wireEncode();
305 BOOST_CHECK_EQUAL(interest.matchesData(data4), false);
306
307 Interest interest4 = interest;
308 interest4.setPublisherPublicKeyLocator(KeyLocator());
309 BOOST_CHECK_EQUAL(interest4.matchesData(data4), true);
310
311 Data data5 = data;
312 data5.setName("ndn:/A/J"); // violates Exclude
313 data5.wireEncode();
314 BOOST_CHECK_EQUAL(interest.matchesData(data5), false);
315
316 Interest interest5 = interest;
317 interest5.setExclude(Exclude().excludeAfter(name::Component("K")));
318 BOOST_CHECK_EQUAL(interest5.matchesData(data5), true);
319
320 Data data6 = data;
321 data6.setName("ndn:/H/I"); // violates Name
322 data6.wireEncode();
323 BOOST_CHECK_EQUAL(interest.matchesData(data6), false);
324
325 Data data7 = data;
326 data7.setName("ndn:/A/B");
327 data7.wireEncode();
328
Junxiao Shidb7464d2017-07-13 03:11:17 +0000329 Interest interest7("/A/B/sha256digest=54008e240a7eea2714a161dfddf0dd6ced223b3856e9da96792151e180f3b128");
Junxiao Shi899277a2017-07-07 22:12:12 +0000330 BOOST_CHECK_EQUAL(interest7.matchesData(data7), true);
331
332 Interest interest7b("/A/B/sha256digest=0000000000000000000000000000000000000000000000000000000000000000");
333 BOOST_CHECK_EQUAL(interest7b.matchesData(data7), false); // violates implicit digest
334}
335
336BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(MatchesInterest, 1)
337BOOST_AUTO_TEST_CASE(MatchesInterest)
338{
339 Interest interest;
340 interest
341 .setName("/A")
342 .setMinSuffixComponents(2)
343 .setMaxSuffixComponents(2)
344 .setPublisherPublicKeyLocator(KeyLocator("/B"))
345 .setExclude(Exclude().excludeAfter(name::Component("J")))
346 .setNonce(10)
Davide Pesavento0f830802018-01-16 23:58:58 -0500347 .setInterestLifetime(5_s)
Junxiao Shid701e5b2017-07-26 01:30:41 +0000348 .setForwardingHint({{1, "/H"}});
Junxiao Shi899277a2017-07-07 22:12:12 +0000349
350 Interest other;
351 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false);
352
353 other.setName(interest.getName());
354 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false);
355
356 other.setSelectors(interest.getSelectors());
357 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false); // will match until #3162 implemented
358
Junxiao Shid701e5b2017-07-26 01:30:41 +0000359 other.setForwardingHint({{1, "/H"}});
Junxiao Shi899277a2017-07-07 22:12:12 +0000360 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
361
362 other.setNonce(200);
363 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
364
Davide Pesavento0f830802018-01-16 23:58:58 -0500365 other.setInterestLifetime(5_h);
Junxiao Shi899277a2017-07-07 22:12:12 +0000366 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000367}
368
369// ---- field accessors ----
370
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000371BOOST_AUTO_TEST_CASE(CanBePrefix)
372{
373 Interest i;
374 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
375 i.setCanBePrefix(false);
376 BOOST_CHECK_EQUAL(i.getCanBePrefix(), false);
377 BOOST_CHECK_EQUAL(i.getSelectors().getMaxSuffixComponents(), 1);
378 i.setCanBePrefix(true);
379 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
380 BOOST_CHECK_EQUAL(i.getSelectors().getMaxSuffixComponents(), -1);
381}
382
383BOOST_AUTO_TEST_CASE(MustBeFresh)
384{
385 Interest i;
386 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
387 i.setMustBeFresh(true);
388 BOOST_CHECK_EQUAL(i.getMustBeFresh(), true);
389 BOOST_CHECK_EQUAL(i.getSelectors().getMustBeFresh(), true);
390 i.setMustBeFresh(false);
391 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
392 BOOST_CHECK_EQUAL(i.getSelectors().getMustBeFresh(), false);
393}
394
395BOOST_AUTO_TEST_CASE(ModifyForwardingHint)
396{
397 Interest i;
398 i.setForwardingHint({{1, "/A"}});
399 i.wireEncode();
400 BOOST_CHECK(i.hasWire());
401
402 i.modifyForwardingHint([] (DelegationList& fh) { fh.insert(2, "/B"); });
403 BOOST_CHECK(!i.hasWire());
404 BOOST_CHECK_EQUAL(i.getForwardingHint(), DelegationList({{1, "/A"}, {2, "/B"}}));
405}
406
Junxiao Shi899277a2017-07-07 22:12:12 +0000407BOOST_AUTO_TEST_CASE(GetNonce)
408{
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000409 unique_ptr<Interest> i1, i2;
Junxiao Shi899277a2017-07-07 22:12:12 +0000410
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000411 // getNonce automatically assigns a random Nonce.
412 // It's possible to assign the same Nonce to two Interest, but it's unlikely to get 100 pairs of
413 // same Nonces in a row.
Junxiao Shi899277a2017-07-07 22:12:12 +0000414 int nIterations = 0;
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000415 uint32_t nonce1 = 0, nonce2 = 0;
Junxiao Shi899277a2017-07-07 22:12:12 +0000416 do {
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000417 i1 = make_unique<Interest>();
418 nonce1 = i1->getNonce();
419 i2 = make_unique<Interest>();
420 nonce2 = i2->getNonce();
Junxiao Shi899277a2017-07-07 22:12:12 +0000421 }
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000422 while (nonce1 == nonce2 && ++nIterations < 100);
423 BOOST_CHECK_NE(nonce1, nonce2);
424 BOOST_CHECK(i1->hasNonce());
425 BOOST_CHECK(i2->hasNonce());
Junxiao Shi899277a2017-07-07 22:12:12 +0000426
427 // Once a Nonce is assigned, it should not change.
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000428 BOOST_CHECK_EQUAL(i1->getNonce(), nonce1);
429}
430
431BOOST_AUTO_TEST_CASE(SetNonce)
432{
433 Interest i1("/A");
434 i1.setNonce(1);
435 i1.wireEncode();
436 BOOST_CHECK_EQUAL(i1.getNonce(), 1);
437
438 Interest i2(i1);
439 BOOST_CHECK_EQUAL(i2.getNonce(), 1);
440
441 i2.setNonce(2);
442 BOOST_CHECK_EQUAL(i2.getNonce(), 2);
443 BOOST_CHECK_EQUAL(i1.getNonce(), 1); // should not affect i1 Nonce (Bug #4168)
Junxiao Shi899277a2017-07-07 22:12:12 +0000444}
445
446BOOST_AUTO_TEST_CASE(RefreshNonce)
447{
448 Interest i;
449 BOOST_CHECK(!i.hasNonce());
450 i.refreshNonce();
451 BOOST_CHECK(!i.hasNonce());
452
453 i.setNonce(1);
454 BOOST_CHECK(i.hasNonce());
455 i.refreshNonce();
456 BOOST_CHECK(i.hasNonce());
457 BOOST_CHECK_NE(i.getNonce(), 1);
458}
459
460BOOST_AUTO_TEST_CASE(SetInterestLifetime)
461{
462 BOOST_CHECK_THROW(Interest("/A", time::milliseconds(-1)), std::invalid_argument);
Davide Pesavento0f830802018-01-16 23:58:58 -0500463 BOOST_CHECK_NO_THROW(Interest("/A", 0_ms));
Junxiao Shi899277a2017-07-07 22:12:12 +0000464
465 Interest i("/local/ndn/prefix");
466 i.setNonce(1);
467 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
468 BOOST_CHECK_THROW(i.setInterestLifetime(time::milliseconds(-1)), std::invalid_argument);
469 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesavento0f830802018-01-16 23:58:58 -0500470 i.setInterestLifetime(0_ms);
471 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 0_ms);
472 i.setInterestLifetime(1_ms);
473 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 1_ms);
Junxiao Shi899277a2017-07-07 22:12:12 +0000474}
475
476// ---- operators ----
477
478BOOST_AUTO_TEST_CASE(Equality)
479{
480 Interest a;
481 Interest b;
482
483 // if nonce is not set, it would be set to a random value
484 a.setNonce(1);
485 b.setNonce(1);
486
487 BOOST_CHECK_EQUAL(a == b, true);
488 BOOST_CHECK_EQUAL(a != b, false);
489
490 // compare Name
491 a.setName("/A");
492 BOOST_CHECK_EQUAL(a == b, false);
493 BOOST_CHECK_EQUAL(a != b, true);
494
495 b.setName("/B");
496 BOOST_CHECK_EQUAL(a == b, false);
497 BOOST_CHECK_EQUAL(a != b, true);
498
499 b.setName("/A");
500 BOOST_CHECK_EQUAL(a == b, true);
501 BOOST_CHECK_EQUAL(a != b, false);
502
503 // compare Selectors
504 a.setChildSelector(1);
505 BOOST_CHECK_EQUAL(a == b, false);
506 BOOST_CHECK_EQUAL(a != b, true);
507
508 b.setChildSelector(1);
509 BOOST_CHECK_EQUAL(a == b, true);
510 BOOST_CHECK_EQUAL(a != b, false);
511
512 // compare Nonce
513 a.setNonce(100);
514 BOOST_CHECK_EQUAL(a == b, false);
515 BOOST_CHECK_EQUAL(a != b, true);
516
517 b.setNonce(100);
518 BOOST_CHECK_EQUAL(a == b, true);
519 BOOST_CHECK_EQUAL(a != b, false);
520
521 // compare InterestLifetime
Davide Pesavento0f830802018-01-16 23:58:58 -0500522 a.setInterestLifetime(10_s);
Junxiao Shi899277a2017-07-07 22:12:12 +0000523 BOOST_CHECK_EQUAL(a == b, false);
524 BOOST_CHECK_EQUAL(a != b, true);
525
Davide Pesavento0f830802018-01-16 23:58:58 -0500526 b.setInterestLifetime(10_s);
Junxiao Shi899277a2017-07-07 22:12:12 +0000527 BOOST_CHECK_EQUAL(a == b, true);
528 BOOST_CHECK_EQUAL(a != b, false);
529
Junxiao Shid701e5b2017-07-26 01:30:41 +0000530 // compare ForwardingHint
531 a.setForwardingHint({{1, "/H"}});
Junxiao Shi899277a2017-07-07 22:12:12 +0000532 BOOST_CHECK_EQUAL(a == b, false);
533 BOOST_CHECK_EQUAL(a != b, true);
534
Junxiao Shid701e5b2017-07-26 01:30:41 +0000535 b.setForwardingHint({{1, "/H"}});
Junxiao Shi899277a2017-07-07 22:12:12 +0000536 BOOST_CHECK_EQUAL(a == b, true);
537 BOOST_CHECK_EQUAL(a != b, false);
538}
539
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100540BOOST_AUTO_TEST_SUITE_END() // TestInterest
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800541
Alexander Afanasyev90164962014-03-06 08:29:59 +0000542} // namespace tests
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800543} // namespace ndn