blob: 16e2ba26d1ea27e6b3e2a4a18f94f0ba2a87e67a [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
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;
Junxiao Shi6efa3b72018-04-14 15:54:08 +000040 BOOST_CHECK(!i.hasWire());
Junxiao Shi899277a2017-07-07 22:12:12 +000041 BOOST_CHECK_EQUAL(i.getName(), "/");
Junxiao Shi6efa3b72018-04-14 15:54:08 +000042 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
43 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
44 BOOST_CHECK(i.getForwardingHint().empty());
45 BOOST_CHECK(!i.hasNonce());
Junxiao Shi899277a2017-07-07 22:12:12 +000046 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Junxiao Shi6efa3b72018-04-14 15:54:08 +000047 BOOST_CHECK(!i.hasSelectors());
Junxiao Shi899277a2017-07-07 22:12:12 +000048}
49
Junxiao Shi6efa3b72018-04-14 15:54:08 +000050BOOST_AUTO_TEST_CASE(EncodeDecode02Basic)
Junxiao Shi899277a2017-07-07 22:12:12 +000051{
52 const uint8_t WIRE[] = {
53 0x05, 0x1c, // Interest
54 0x07, 0x14, // Name
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +000055 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent
56 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent
57 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent
Junxiao Shi899277a2017-07-07 22:12:12 +000058 0x0a, 0x04, // Nonce
59 0x01, 0x00, 0x00, 0x00
60 };
61
62 Interest i1("/local/ndn/prefix");
Junxiao Shib55e5d32018-07-18 13:32:00 -060063 i1.setCanBePrefix(true);
Junxiao Shi899277a2017-07-07 22:12:12 +000064 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");
Junxiao Shib55e5d32018-07-18 13:32:00 -060099 i1.setCanBePrefix(true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000100 i1.setMinSuffixComponents(1);
101 i1.setNonce(1);
Davide Pesavento0f830802018-01-16 23:58:58 -0500102 i1.setInterestLifetime(1000_ms);
Junxiao Shi9c154cb2017-07-07 22:14:54 +0000103 i1.setForwardingHint({{1, "/A"}});
Junxiao Shi899277a2017-07-07 22:12:12 +0000104 Block wire1 = i1.wireEncode();
105 BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
106
107 Interest i2(wire1);
108 BOOST_CHECK_EQUAL(i2.getName(), "/local/ndn/prefix");
109 BOOST_CHECK_EQUAL(i2.getMinSuffixComponents(), 1);
110 BOOST_CHECK_EQUAL(i2.getNonce(), 1);
Davide Pesavento0f830802018-01-16 23:58:58 -0500111 BOOST_CHECK_EQUAL(i2.getInterestLifetime(), 1000_ms);
Junxiao Shi9c154cb2017-07-07 22:14:54 +0000112 BOOST_CHECK_EQUAL(i2.getForwardingHint(), DelegationList({{1, "/A"}}));
Junxiao Shi899277a2017-07-07 22:12:12 +0000113
114 BOOST_CHECK_EQUAL(i1, i2);
115}
116
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700117BOOST_AUTO_TEST_CASE(EncodeDecode03Basic)
118{
119 const uint8_t WIRE[] = {
120 0x05, 0x22, // Interest
121 0x07, 0x14, // Name
122 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent
123 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent
124 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent
125 0x0a, 0x04, // Nonce
126 0x01, 0x00, 0x00, 0x00,
127 0x23, 0x04, // Parameters
128 0xc0, 0xc1, 0xc2, 0xc3};
129
130 Interest i1;
131 i1.setName("/local/ndn/prefix");
132 i1.setCanBePrefix(false);
133 i1.setNonce(1);
134 i1.setParameters("2304C0C1C2C3"_block);
135 Block wire1 = i1.wireEncode();
136 BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
137
138 Interest i2(wire1);
139 BOOST_CHECK_EQUAL(i2.getName(), "/local/ndn/prefix");
140 BOOST_CHECK_EQUAL(i2.getCanBePrefix(), false);
141 BOOST_CHECK_EQUAL(i2.getMustBeFresh(), false);
142 BOOST_CHECK(i2.getForwardingHint().empty());
143 BOOST_CHECK_EQUAL(i2.getNonce(), 1);
144 BOOST_CHECK_EQUAL(i2.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
145 BOOST_CHECK(i2.hasParameters());
146 BOOST_CHECK_EQUAL(i2.getParameters(), "2304C0C1C2C3"_block);
147 BOOST_CHECK(i2.getPublisherPublicKeyLocator().empty());
148}
149
150BOOST_AUTO_TEST_CASE(EncodeDecode03Full)
151{
152 const uint8_t WIRE[] = {
153 0x05, 0x37, // Interest
154 0x07, 0x14, // Name
155 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent
156 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent
157 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent
158 0x21, 0x00, // CanBePrefix
159 0x12, 0x00, // MustBeFresh
160 0x1e, 0x0b, // ForwardingHint
161 0x1f, 0x09, // Delegation List
162 0x1e, 0x02,
163 0x3e, 0x15,
164 0x07, 0x03,
165 0x08, 0x01, 0x48,
166 0x0a, 0x04, // Nonce
167 0x4a, 0xcb, 0x1e, 0x4c,
168 0x0c, 0x02, // Interest Lifetime
169 0x76, 0xa1,
170 0x23, 0x04, // Parameters
171 0xc0, 0xc1, 0xc2, 0xc3};
172 Interest i1;
173 i1.setName("/local/ndn/prefix");
174 i1.setMustBeFresh(true);
175 i1.setCanBePrefix(true);
176 i1.setForwardingHint(DelegationList({{15893, "/H"}}));
177 i1.setNonce(0x4c1ecb4a);
178 i1.setInterestLifetime(30369_ms);
179 i1.setParameters("2304C0C1C2C3"_block);
180 i1.setMinSuffixComponents(1); // v0.2-only elements will not be encoded
181 i1.setExclude(Exclude().excludeAfter(name::Component("J"))); // v0.2-only elements will not be encoded
182 Block wire1 = i1.wireEncode();
183 BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
184
185 Interest i2(wire1);
186 BOOST_CHECK_EQUAL(i2.getName(), "/local/ndn/prefix");
187 BOOST_CHECK_EQUAL(i2.getCanBePrefix(), true);
188 BOOST_CHECK_EQUAL(i2.getMustBeFresh(), true);
189 BOOST_CHECK_EQUAL(i2.getForwardingHint(), DelegationList({{15893, "/H"}}));
190 BOOST_CHECK(i2.hasNonce());
191 BOOST_CHECK_EQUAL(i2.getNonce(), 0x4c1ecb4a);
192 BOOST_CHECK_EQUAL(i2.getInterestLifetime(), 30369_ms);
193 BOOST_CHECK_EQUAL(i2.getParameters(), "2304C0C1C2C3"_block);
194 BOOST_CHECK_EQUAL(i2.getMinSuffixComponents(), -1); // Default because minSuffixComponents was not encoded
195 BOOST_CHECK(i2.getExclude().empty()); // Exclude was not encoded
196}
197
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000198class Decode03Fixture
Junxiao Shi899277a2017-07-07 22:12:12 +0000199{
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000200protected:
201 Decode03Fixture()
202 {
203 // initialize all elements to non-empty, to verify wireDecode clears them
204 i.setName("/A");
205 i.setForwardingHint({{10309, "/F"}});
206 i.setNonce(0x03d645a8);
207 i.setInterestLifetime(18554_ms);
208 i.setPublisherPublicKeyLocator(Name("/K"));
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700209 i.setParameters("2304A0A1A2A3"_block);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000210 }
Junxiao Shi899277a2017-07-07 22:12:12 +0000211
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000212protected:
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000213 Interest i;
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000214};
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000215
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000216BOOST_FIXTURE_TEST_SUITE(Decode03, Decode03Fixture)
217
218BOOST_AUTO_TEST_CASE(Minimal)
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000219{
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000220 i.wireDecode("0505 0703080149"_block);
221 BOOST_CHECK_EQUAL(i.getName(), "/I");
222 BOOST_CHECK_EQUAL(i.getCanBePrefix(), false);
223 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
224 BOOST_CHECK(i.getForwardingHint().empty());
225 BOOST_CHECK(i.hasNonce()); // a random nonce is generated
226 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
227 BOOST_CHECK(i.getPublisherPublicKeyLocator().empty());
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700228 BOOST_CHECK(!i.hasParameters());
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000229
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000230 BOOST_CHECK(!i.hasWire()); // nonce generation resets wire encoding
231
232 // modify then re-encode as v0.2 format
233 i.setNonce(0x54657c95);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000234 BOOST_CHECK_EQUAL(i.wireEncode(), "0510 0703080149 09030E0101 0A04957C6554"_block);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000235}
236
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000237BOOST_AUTO_TEST_CASE(Full)
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000238{
Junxiao Shi8b753a22018-10-24 01:51:40 +0000239 i.wireDecode("0531 0703080149 FC00 2100 FC00 1200 "
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000240 "FC00 1E0B(1F09 1E023E15 0703080148) FC00 0A044ACB1E4C "
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700241 "FC00 0C0276A1 FC00 2201D6 FC00"_block);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000242 BOOST_CHECK_EQUAL(i.getName(), "/I");
243 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
244 BOOST_CHECK_EQUAL(i.getMustBeFresh(), true);
245 BOOST_CHECK_EQUAL(i.getForwardingHint(), DelegationList({{15893, "/H"}}));
246 BOOST_CHECK(i.hasNonce());
247 BOOST_CHECK_EQUAL(i.getNonce(), 0x4c1ecb4a);
248 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 30369_ms);
249 // HopLimit=214 is not stored
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000250
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000251 // encode without modification: retain original wire encoding
Junxiao Shi8b753a22018-10-24 01:51:40 +0000252 BOOST_CHECK_EQUAL(i.wireEncode().value_size(), 49);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000253
254 // modify then re-encode as v0.2 format
255 i.setName("/J");
Junxiao Shi72c0c642018-04-20 15:41:09 +0000256 BOOST_CHECK_EQUAL(i.wireEncode(),
257 "0520 070308014A 09021200 0A044ACB1E4C 0C0276A1 1E0B(1F09 1E023E15 0703080148)"_block);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000258}
259
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000260BOOST_AUTO_TEST_CASE(CriticalElementOutOfOrder)
261{
262 BOOST_CHECK_THROW(i.wireDecode(
263 "0529 2100 0703080149 1200 1E0B(1F09 1E023E15 0703080148) "
264 "0A044ACB1E4C 0C0276A1 2201D6 2304C0C1C2C3"_block),
265 tlv::Error);
266 BOOST_CHECK_THROW(i.wireDecode(
267 "0529 0703080149 1200 2100 1E0B(1F09 1E023E15 0703080148) "
268 "0A044ACB1E4C 0C0276A1 2201D6 2304C0C1C2C3"_block),
269 tlv::Error);
270 BOOST_CHECK_THROW(i.wireDecode(
271 "0529 0703080149 2100 1E0B(1F09 1E023E15 0703080148) 1200 "
272 "0A044ACB1E4C 0C0276A1 2201D6 2304C0C1C2C3"_block),
273 tlv::Error);
274 BOOST_CHECK_THROW(i.wireDecode(
275 "0529 0703080149 2100 1200 0A044ACB1E4C "
276 "1E0B(1F09 1E023E15 0703080148) 0C0276A1 2201D6 2304C0C1C2C3"_block),
277 tlv::Error);
278 BOOST_CHECK_THROW(i.wireDecode(
279 "0529 0703080149 2100 1200 1E0B(1F09 1E023E15 0703080148) "
280 "0C0276A1 0A044ACB1E4C 2201D6 2304C0C1C2C3"_block),
281 tlv::Error);
282 BOOST_CHECK_THROW(i.wireDecode(
283 "0529 0703080149 2100 1200 1E0B(1F09 1E023E15 0703080148) "
284 "0A044ACB1E4C 2201D6 0C0276A1 2304C0C1C2C3"_block),
285 tlv::Error);
286 BOOST_CHECK_THROW(i.wireDecode(
287 "052F 0703080149 2100 1200 1E0B(1F09 1E023E15 0703080148) "
288 "0A044ACB1E4C 0C0276A1 2201D6 2304C0C1C2C3 2304C0C1C2C3"_block),
289 tlv::Error);
290}
291
292BOOST_AUTO_TEST_CASE(HopLimitOutOfOrder)
293{
294 // HopLimit is non-critical, its out-of-order appearances are ignored
295 i.wireDecode("0514 0703080149 2201D6 2200 2304C0C1C2C3 22020101"_block);
296 BOOST_CHECK_EQUAL(i.getName(), "/I");
297 // HopLimit=214 is not stored
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700298 BOOST_CHECK_EQUAL(i.getParameters(), "2304C0C1C2C3"_block);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000299}
300
301BOOST_AUTO_TEST_CASE(NameMissing)
302{
303 BOOST_CHECK_THROW(i.wireDecode("0500"_block), tlv::Error);
304 BOOST_CHECK_THROW(i.wireDecode("0502 1200"_block), tlv::Error);
305}
306
307BOOST_AUTO_TEST_CASE(NameEmpty)
308{
309 BOOST_CHECK_THROW(i.wireDecode("0502 0700"_block), tlv::Error);
310}
311
312BOOST_AUTO_TEST_CASE(BadCanBePrefix)
313{
314 BOOST_CHECK_THROW(i.wireDecode("0508 0703080149 210102"_block), tlv::Error);
315}
316
317BOOST_AUTO_TEST_CASE(BadMustBeFresh)
318{
319 BOOST_CHECK_THROW(i.wireDecode("0508 0703080149 120102"_block), tlv::Error);
320}
321
322BOOST_AUTO_TEST_CASE(BadNonce)
323{
324 BOOST_CHECK_THROW(i.wireDecode("0507 0703080149 0A00"_block), tlv::Error);
325 BOOST_CHECK_THROW(i.wireDecode("050A 0703080149 0A0304C263"_block), tlv::Error);
326 BOOST_CHECK_THROW(i.wireDecode("050C 0703080149 0A05EFA420B262"_block), tlv::Error);
327}
328
Junxiao Shi8b753a22018-10-24 01:51:40 +0000329BOOST_AUTO_TEST_CASE(UnrecognizedNonCriticalElementBeforeName)
330{
331 BOOST_CHECK_THROW(i.wireDecode("0507 FC00 0703080149"_block), tlv::Error);
332}
333
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000334BOOST_AUTO_TEST_CASE(UnrecognizedCriticalElement)
335{
336 BOOST_CHECK_THROW(i.wireDecode("0507 0703080149 FB00"_block), tlv::Error);
337}
338
339BOOST_AUTO_TEST_SUITE_END() // Decode03
340
Junxiao Shi899277a2017-07-07 22:12:12 +0000341// ---- matching ----
342
343BOOST_AUTO_TEST_CASE(MatchesData)
344{
345 Interest interest;
346 interest.setName("ndn:/A")
347 .setMinSuffixComponents(2)
348 .setMaxSuffixComponents(2)
349 .setPublisherPublicKeyLocator(KeyLocator("ndn:/B"))
350 .setExclude(Exclude().excludeAfter(name::Component("J")));
351
352 Data data("ndn:/A/D");
353 SignatureSha256WithRsa signature(KeyLocator("ndn:/B"));
Junxiao Shidb7464d2017-07-13 03:11:17 +0000354 signature.setValue(encoding::makeEmptyBlock(tlv::SignatureValue));
Junxiao Shi899277a2017-07-07 22:12:12 +0000355 data.setSignature(signature);
356 data.wireEncode();
357 BOOST_CHECK_EQUAL(interest.matchesData(data), true);
358
359 Data data1 = data;
360 data1.setName("ndn:/A"); // violates MinSuffixComponents
361 data1.wireEncode();
362 BOOST_CHECK_EQUAL(interest.matchesData(data1), false);
363
364 Interest interest1 = interest;
365 interest1.setMinSuffixComponents(1);
366 BOOST_CHECK_EQUAL(interest1.matchesData(data1), true);
367
368 Data data2 = data;
369 data2.setName("ndn:/A/E/F"); // violates MaxSuffixComponents
370 data2.wireEncode();
371 BOOST_CHECK_EQUAL(interest.matchesData(data2), false);
372
373 Interest interest2 = interest;
374 interest2.setMaxSuffixComponents(3);
375 BOOST_CHECK_EQUAL(interest2.matchesData(data2), true);
376
377 Data data3 = data;
378 SignatureSha256WithRsa signature3(KeyLocator("ndn:/G")); // violates PublisherPublicKeyLocator
Junxiao Shidb7464d2017-07-13 03:11:17 +0000379 signature3.setValue(encoding::makeEmptyBlock(tlv::SignatureValue));
Junxiao Shi899277a2017-07-07 22:12:12 +0000380 data3.setSignature(signature3);
381 data3.wireEncode();
382 BOOST_CHECK_EQUAL(interest.matchesData(data3), false);
383
384 Interest interest3 = interest;
385 interest3.setPublisherPublicKeyLocator(KeyLocator("ndn:/G"));
386 BOOST_CHECK_EQUAL(interest3.matchesData(data3), true);
387
388 Data data4 = data;
389 DigestSha256 signature4; // violates PublisherPublicKeyLocator
Junxiao Shidb7464d2017-07-13 03:11:17 +0000390 signature4.setValue(encoding::makeEmptyBlock(tlv::SignatureValue));
Junxiao Shi899277a2017-07-07 22:12:12 +0000391 data4.setSignature(signature4);
392 data4.wireEncode();
393 BOOST_CHECK_EQUAL(interest.matchesData(data4), false);
394
395 Interest interest4 = interest;
396 interest4.setPublisherPublicKeyLocator(KeyLocator());
397 BOOST_CHECK_EQUAL(interest4.matchesData(data4), true);
398
399 Data data5 = data;
400 data5.setName("ndn:/A/J"); // violates Exclude
401 data5.wireEncode();
402 BOOST_CHECK_EQUAL(interest.matchesData(data5), false);
403
404 Interest interest5 = interest;
405 interest5.setExclude(Exclude().excludeAfter(name::Component("K")));
406 BOOST_CHECK_EQUAL(interest5.matchesData(data5), true);
407
408 Data data6 = data;
409 data6.setName("ndn:/H/I"); // violates Name
410 data6.wireEncode();
411 BOOST_CHECK_EQUAL(interest.matchesData(data6), false);
412
413 Data data7 = data;
414 data7.setName("ndn:/A/B");
415 data7.wireEncode();
416
Junxiao Shidb7464d2017-07-13 03:11:17 +0000417 Interest interest7("/A/B/sha256digest=54008e240a7eea2714a161dfddf0dd6ced223b3856e9da96792151e180f3b128");
Junxiao Shi899277a2017-07-07 22:12:12 +0000418 BOOST_CHECK_EQUAL(interest7.matchesData(data7), true);
419
420 Interest interest7b("/A/B/sha256digest=0000000000000000000000000000000000000000000000000000000000000000");
421 BOOST_CHECK_EQUAL(interest7b.matchesData(data7), false); // violates implicit digest
422}
423
424BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(MatchesInterest, 1)
425BOOST_AUTO_TEST_CASE(MatchesInterest)
426{
427 Interest interest;
428 interest
429 .setName("/A")
430 .setMinSuffixComponents(2)
431 .setMaxSuffixComponents(2)
432 .setPublisherPublicKeyLocator(KeyLocator("/B"))
433 .setExclude(Exclude().excludeAfter(name::Component("J")))
434 .setNonce(10)
Davide Pesavento0f830802018-01-16 23:58:58 -0500435 .setInterestLifetime(5_s)
Junxiao Shid701e5b2017-07-26 01:30:41 +0000436 .setForwardingHint({{1, "/H"}});
Junxiao Shi899277a2017-07-07 22:12:12 +0000437
438 Interest other;
439 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false);
440
441 other.setName(interest.getName());
442 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false);
443
444 other.setSelectors(interest.getSelectors());
445 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false); // will match until #3162 implemented
446
Junxiao Shid701e5b2017-07-26 01:30:41 +0000447 other.setForwardingHint({{1, "/H"}});
Junxiao Shi899277a2017-07-07 22:12:12 +0000448 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
449
450 other.setNonce(200);
451 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
452
Davide Pesavento0f830802018-01-16 23:58:58 -0500453 other.setInterestLifetime(5_h);
Junxiao Shi899277a2017-07-07 22:12:12 +0000454 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000455}
456
457// ---- field accessors ----
458
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000459BOOST_AUTO_TEST_CASE(CanBePrefix)
460{
461 Interest i;
462 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
463 i.setCanBePrefix(false);
464 BOOST_CHECK_EQUAL(i.getCanBePrefix(), false);
465 BOOST_CHECK_EQUAL(i.getSelectors().getMaxSuffixComponents(), 1);
466 i.setCanBePrefix(true);
467 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
468 BOOST_CHECK_EQUAL(i.getSelectors().getMaxSuffixComponents(), -1);
469}
470
471BOOST_AUTO_TEST_CASE(MustBeFresh)
472{
473 Interest i;
474 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
475 i.setMustBeFresh(true);
476 BOOST_CHECK_EQUAL(i.getMustBeFresh(), true);
477 BOOST_CHECK_EQUAL(i.getSelectors().getMustBeFresh(), true);
478 i.setMustBeFresh(false);
479 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
480 BOOST_CHECK_EQUAL(i.getSelectors().getMustBeFresh(), false);
481}
482
483BOOST_AUTO_TEST_CASE(ModifyForwardingHint)
484{
485 Interest i;
Junxiao Shib55e5d32018-07-18 13:32:00 -0600486 i.setCanBePrefix(false);
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000487 i.setForwardingHint({{1, "/A"}});
488 i.wireEncode();
489 BOOST_CHECK(i.hasWire());
490
491 i.modifyForwardingHint([] (DelegationList& fh) { fh.insert(2, "/B"); });
492 BOOST_CHECK(!i.hasWire());
493 BOOST_CHECK_EQUAL(i.getForwardingHint(), DelegationList({{1, "/A"}, {2, "/B"}}));
494}
495
Junxiao Shi899277a2017-07-07 22:12:12 +0000496BOOST_AUTO_TEST_CASE(GetNonce)
497{
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000498 unique_ptr<Interest> i1, i2;
Junxiao Shi899277a2017-07-07 22:12:12 +0000499
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000500 // getNonce automatically assigns a random Nonce.
501 // It's possible to assign the same Nonce to two Interest, but it's unlikely to get 100 pairs of
502 // same Nonces in a row.
Junxiao Shi899277a2017-07-07 22:12:12 +0000503 int nIterations = 0;
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000504 uint32_t nonce1 = 0, nonce2 = 0;
Junxiao Shi899277a2017-07-07 22:12:12 +0000505 do {
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000506 i1 = make_unique<Interest>();
507 nonce1 = i1->getNonce();
508 i2 = make_unique<Interest>();
509 nonce2 = i2->getNonce();
Junxiao Shi899277a2017-07-07 22:12:12 +0000510 }
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000511 while (nonce1 == nonce2 && ++nIterations < 100);
512 BOOST_CHECK_NE(nonce1, nonce2);
513 BOOST_CHECK(i1->hasNonce());
514 BOOST_CHECK(i2->hasNonce());
Junxiao Shi899277a2017-07-07 22:12:12 +0000515
516 // Once a Nonce is assigned, it should not change.
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000517 BOOST_CHECK_EQUAL(i1->getNonce(), nonce1);
518}
519
520BOOST_AUTO_TEST_CASE(SetNonce)
521{
522 Interest i1("/A");
Junxiao Shib55e5d32018-07-18 13:32:00 -0600523 i1.setCanBePrefix(false);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000524 i1.setNonce(1);
525 i1.wireEncode();
526 BOOST_CHECK_EQUAL(i1.getNonce(), 1);
527
528 Interest i2(i1);
529 BOOST_CHECK_EQUAL(i2.getNonce(), 1);
530
531 i2.setNonce(2);
532 BOOST_CHECK_EQUAL(i2.getNonce(), 2);
533 BOOST_CHECK_EQUAL(i1.getNonce(), 1); // should not affect i1 Nonce (Bug #4168)
Junxiao Shi899277a2017-07-07 22:12:12 +0000534}
535
536BOOST_AUTO_TEST_CASE(RefreshNonce)
537{
538 Interest i;
539 BOOST_CHECK(!i.hasNonce());
540 i.refreshNonce();
541 BOOST_CHECK(!i.hasNonce());
542
543 i.setNonce(1);
544 BOOST_CHECK(i.hasNonce());
545 i.refreshNonce();
546 BOOST_CHECK(i.hasNonce());
547 BOOST_CHECK_NE(i.getNonce(), 1);
548}
549
550BOOST_AUTO_TEST_CASE(SetInterestLifetime)
551{
552 BOOST_CHECK_THROW(Interest("/A", time::milliseconds(-1)), std::invalid_argument);
Davide Pesavento0f830802018-01-16 23:58:58 -0500553 BOOST_CHECK_NO_THROW(Interest("/A", 0_ms));
Junxiao Shi899277a2017-07-07 22:12:12 +0000554
555 Interest i("/local/ndn/prefix");
556 i.setNonce(1);
557 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
558 BOOST_CHECK_THROW(i.setInterestLifetime(time::milliseconds(-1)), std::invalid_argument);
559 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesavento0f830802018-01-16 23:58:58 -0500560 i.setInterestLifetime(0_ms);
561 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 0_ms);
562 i.setInterestLifetime(1_ms);
563 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 1_ms);
Junxiao Shi899277a2017-07-07 22:12:12 +0000564}
565
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700566BOOST_AUTO_TEST_CASE(SetParameters)
567{
568 const uint8_t PARAMETERS1[] = {0xc1};
569 const uint8_t PARAMETERS2[] = {0xc2};
570
571 Interest i;
572 BOOST_CHECK(!i.hasParameters());
573 i.setParameters("2300"_block);
574 BOOST_CHECK(i.hasParameters());
575 i.unsetParameters();
576 BOOST_CHECK(!i.hasParameters());
577
578 i.setParameters("2301C0"_block); // Block overload
579 BOOST_CHECK_EQUAL(i.getParameters(), "2301C0"_block);
580 i.setParameters(PARAMETERS1, sizeof(PARAMETERS1)); // raw buffer overload
581 BOOST_CHECK_EQUAL(i.getParameters(), "2301C1"_block);
582 i.setParameters(make_shared<Buffer>(PARAMETERS2, sizeof(PARAMETERS2))); // ConstBufferPtr overload
583 BOOST_CHECK_EQUAL(i.getParameters(), "2301C2"_block);
584 i.setParameters("8001C1"_block); // Block of non-Parameters type
585 BOOST_CHECK_EQUAL(i.getParameters(), "23038001C1"_block);
586}
587
Junxiao Shi899277a2017-07-07 22:12:12 +0000588// ---- operators ----
589
590BOOST_AUTO_TEST_CASE(Equality)
591{
592 Interest a;
593 Interest b;
594
595 // if nonce is not set, it would be set to a random value
596 a.setNonce(1);
597 b.setNonce(1);
598
599 BOOST_CHECK_EQUAL(a == b, true);
600 BOOST_CHECK_EQUAL(a != b, false);
601
602 // compare Name
603 a.setName("/A");
604 BOOST_CHECK_EQUAL(a == b, false);
605 BOOST_CHECK_EQUAL(a != b, true);
606
607 b.setName("/B");
608 BOOST_CHECK_EQUAL(a == b, false);
609 BOOST_CHECK_EQUAL(a != b, true);
610
611 b.setName("/A");
612 BOOST_CHECK_EQUAL(a == b, true);
613 BOOST_CHECK_EQUAL(a != b, false);
614
615 // compare Selectors
616 a.setChildSelector(1);
617 BOOST_CHECK_EQUAL(a == b, false);
618 BOOST_CHECK_EQUAL(a != b, true);
619
620 b.setChildSelector(1);
621 BOOST_CHECK_EQUAL(a == b, true);
622 BOOST_CHECK_EQUAL(a != b, false);
623
624 // compare Nonce
625 a.setNonce(100);
626 BOOST_CHECK_EQUAL(a == b, false);
627 BOOST_CHECK_EQUAL(a != b, true);
628
629 b.setNonce(100);
630 BOOST_CHECK_EQUAL(a == b, true);
631 BOOST_CHECK_EQUAL(a != b, false);
632
633 // compare InterestLifetime
Davide Pesavento0f830802018-01-16 23:58:58 -0500634 a.setInterestLifetime(10_s);
Junxiao Shi899277a2017-07-07 22:12:12 +0000635 BOOST_CHECK_EQUAL(a == b, false);
636 BOOST_CHECK_EQUAL(a != b, true);
637
Davide Pesavento0f830802018-01-16 23:58:58 -0500638 b.setInterestLifetime(10_s);
Junxiao Shi899277a2017-07-07 22:12:12 +0000639 BOOST_CHECK_EQUAL(a == b, true);
640 BOOST_CHECK_EQUAL(a != b, false);
641
Junxiao Shid701e5b2017-07-26 01:30:41 +0000642 // compare ForwardingHint
643 a.setForwardingHint({{1, "/H"}});
Junxiao Shi899277a2017-07-07 22:12:12 +0000644 BOOST_CHECK_EQUAL(a == b, false);
645 BOOST_CHECK_EQUAL(a != b, true);
646
Junxiao Shid701e5b2017-07-26 01:30:41 +0000647 b.setForwardingHint({{1, "/H"}});
Junxiao Shi899277a2017-07-07 22:12:12 +0000648 BOOST_CHECK_EQUAL(a == b, true);
649 BOOST_CHECK_EQUAL(a != b, false);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700650
651 // compare Parameters
652 a.setParameters("2304C0C1C2C3"_block);
653 BOOST_CHECK_EQUAL(a == b, false);
654 BOOST_CHECK_EQUAL(a != b, true);
655
656 b.setParameters("2304C0C1C2C3"_block);
657 BOOST_CHECK_EQUAL(a == b, true);
658 BOOST_CHECK_EQUAL(a != b, false);
Junxiao Shi899277a2017-07-07 22:12:12 +0000659}
660
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100661BOOST_AUTO_TEST_SUITE_END() // TestInterest
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800662
Alexander Afanasyev90164962014-03-06 08:29:59 +0000663} // namespace tests
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800664} // namespace ndn