blob: c8c43c5d221d86492c252354e0cd27f910615f55 [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 Pesaventofccb2dc2019-02-09 01:02:35 -05003 * Copyright (c) 2013-2019 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
Davide Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/interest.hpp"
23#include "ndn-cxx/data.hpp"
24#include "ndn-cxx/security/digest-sha256.hpp"
25#include "ndn-cxx/security/signature-sha256-with-rsa.hpp"
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070026
Davide Pesavento7e780642018-11-24 15:51:34 -050027#include "tests/boost-test.hpp"
28#include "tests/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());
Davide Pesaventofccb2dc2019-02-09 01:02:35 -050048 BOOST_CHECK(!i.hasParameters());
49 BOOST_CHECK(i.getParameters().empty());
50}
51
52BOOST_AUTO_TEST_CASE(DecodeNotInterest)
53{
54 BOOST_CHECK_THROW(Interest("4202CAFE"_block), tlv::Error);
Junxiao Shi899277a2017-07-07 22:12:12 +000055}
56
Junxiao Shi6efa3b72018-04-14 15:54:08 +000057BOOST_AUTO_TEST_CASE(EncodeDecode02Basic)
Junxiao Shi899277a2017-07-07 22:12:12 +000058{
59 const uint8_t WIRE[] = {
60 0x05, 0x1c, // Interest
61 0x07, 0x14, // Name
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +000062 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent
63 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent
64 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent
Junxiao Shi899277a2017-07-07 22:12:12 +000065 0x0a, 0x04, // Nonce
66 0x01, 0x00, 0x00, 0x00
67 };
68
69 Interest i1("/local/ndn/prefix");
Junxiao Shib55e5d32018-07-18 13:32:00 -060070 i1.setCanBePrefix(true);
Junxiao Shi899277a2017-07-07 22:12:12 +000071 i1.setNonce(1);
72 Block wire1 = i1.wireEncode();
73 BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
74
75 Interest i2(wire1);
76 BOOST_CHECK_EQUAL(i2.getName(), "/local/ndn/prefix");
77 BOOST_CHECK(i2.getSelectors().empty());
78 BOOST_CHECK_EQUAL(i2.getNonce(), 1);
79 BOOST_CHECK_EQUAL(i2.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
80
81 BOOST_CHECK_EQUAL(i1, i2);
82}
83
Junxiao Shi6efa3b72018-04-14 15:54:08 +000084BOOST_AUTO_TEST_CASE(EncodeDecode02Full)
Junxiao Shi899277a2017-07-07 22:12:12 +000085{
86 const uint8_t WIRE[] = {
Junxiao Shi9c154cb2017-07-07 22:14:54 +000087 0x05, 0x31, // Interest
Junxiao Shi899277a2017-07-07 22:12:12 +000088 0x07, 0x14, // Name
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +000089 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent
90 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent
91 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent
Junxiao Shi899277a2017-07-07 22:12:12 +000092 0x09, 0x03, // Selectors
93 0x0d, 0x01, 0x01, // MinSuffixComponents
94 0x0a, 0x04, // Nonce
95 0x01, 0x00, 0x00, 0x00,
96 0x0c, 0x02, // InterestLifetime
Junxiao Shi9c154cb2017-07-07 22:14:54 +000097 0x03, 0xe8,
98 0x1e, 0x0a, // ForwardingHint
99 0x1f, 0x08, // Delegation
100 0x1e, 0x01, 0x01, // Preference=1
101 0x07, 0x03, 0x08, 0x01, 0x41 // Name=/A
Junxiao Shi899277a2017-07-07 22:12:12 +0000102 };
Junxiao Shi899277a2017-07-07 22:12:12 +0000103
104 Interest i1;
105 i1.setName("/local/ndn/prefix");
Junxiao Shib55e5d32018-07-18 13:32:00 -0600106 i1.setCanBePrefix(true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000107 i1.setMinSuffixComponents(1);
108 i1.setNonce(1);
Davide Pesavento0f830802018-01-16 23:58:58 -0500109 i1.setInterestLifetime(1000_ms);
Junxiao Shi9c154cb2017-07-07 22:14:54 +0000110 i1.setForwardingHint({{1, "/A"}});
Junxiao Shi899277a2017-07-07 22:12:12 +0000111 Block wire1 = i1.wireEncode();
112 BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
113
114 Interest i2(wire1);
115 BOOST_CHECK_EQUAL(i2.getName(), "/local/ndn/prefix");
116 BOOST_CHECK_EQUAL(i2.getMinSuffixComponents(), 1);
117 BOOST_CHECK_EQUAL(i2.getNonce(), 1);
Davide Pesavento0f830802018-01-16 23:58:58 -0500118 BOOST_CHECK_EQUAL(i2.getInterestLifetime(), 1000_ms);
Junxiao Shi9c154cb2017-07-07 22:14:54 +0000119 BOOST_CHECK_EQUAL(i2.getForwardingHint(), DelegationList({{1, "/A"}}));
Junxiao Shi899277a2017-07-07 22:12:12 +0000120
121 BOOST_CHECK_EQUAL(i1, i2);
122}
123
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700124BOOST_AUTO_TEST_CASE(EncodeDecode03Basic)
125{
126 const uint8_t WIRE[] = {
127 0x05, 0x22, // Interest
128 0x07, 0x14, // Name
129 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent
130 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent
131 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent
132 0x0a, 0x04, // Nonce
133 0x01, 0x00, 0x00, 0x00,
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500134 0x24, 0x04, // Parameters
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700135 0xc0, 0xc1, 0xc2, 0xc3};
136
137 Interest i1;
138 i1.setName("/local/ndn/prefix");
139 i1.setCanBePrefix(false);
140 i1.setNonce(1);
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500141 i1.setParameters("2404C0C1C2C3"_block);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700142 Block wire1 = i1.wireEncode();
143 BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
144
145 Interest i2(wire1);
146 BOOST_CHECK_EQUAL(i2.getName(), "/local/ndn/prefix");
147 BOOST_CHECK_EQUAL(i2.getCanBePrefix(), false);
148 BOOST_CHECK_EQUAL(i2.getMustBeFresh(), false);
149 BOOST_CHECK(i2.getForwardingHint().empty());
150 BOOST_CHECK_EQUAL(i2.getNonce(), 1);
151 BOOST_CHECK_EQUAL(i2.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
152 BOOST_CHECK(i2.hasParameters());
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500153 BOOST_CHECK_EQUAL(i2.getParameters(), "2404C0C1C2C3"_block);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700154 BOOST_CHECK(i2.getPublisherPublicKeyLocator().empty());
155}
156
157BOOST_AUTO_TEST_CASE(EncodeDecode03Full)
158{
159 const uint8_t WIRE[] = {
160 0x05, 0x37, // Interest
161 0x07, 0x14, // Name
162 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent
163 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent
164 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent
165 0x21, 0x00, // CanBePrefix
166 0x12, 0x00, // MustBeFresh
167 0x1e, 0x0b, // ForwardingHint
168 0x1f, 0x09, // Delegation List
169 0x1e, 0x02,
170 0x3e, 0x15,
171 0x07, 0x03,
172 0x08, 0x01, 0x48,
173 0x0a, 0x04, // Nonce
174 0x4a, 0xcb, 0x1e, 0x4c,
175 0x0c, 0x02, // Interest Lifetime
176 0x76, 0xa1,
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500177 0x24, 0x04, // Parameters
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700178 0xc0, 0xc1, 0xc2, 0xc3};
179 Interest i1;
180 i1.setName("/local/ndn/prefix");
181 i1.setMustBeFresh(true);
182 i1.setCanBePrefix(true);
183 i1.setForwardingHint(DelegationList({{15893, "/H"}}));
184 i1.setNonce(0x4c1ecb4a);
185 i1.setInterestLifetime(30369_ms);
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500186 i1.setParameters("2404C0C1C2C3"_block);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700187 i1.setMinSuffixComponents(1); // v0.2-only elements will not be encoded
188 i1.setExclude(Exclude().excludeAfter(name::Component("J"))); // v0.2-only elements will not be encoded
189 Block wire1 = i1.wireEncode();
190 BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
191
192 Interest i2(wire1);
193 BOOST_CHECK_EQUAL(i2.getName(), "/local/ndn/prefix");
194 BOOST_CHECK_EQUAL(i2.getCanBePrefix(), true);
195 BOOST_CHECK_EQUAL(i2.getMustBeFresh(), true);
196 BOOST_CHECK_EQUAL(i2.getForwardingHint(), DelegationList({{15893, "/H"}}));
197 BOOST_CHECK(i2.hasNonce());
198 BOOST_CHECK_EQUAL(i2.getNonce(), 0x4c1ecb4a);
199 BOOST_CHECK_EQUAL(i2.getInterestLifetime(), 30369_ms);
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500200 BOOST_CHECK_EQUAL(i2.getParameters(), "2404C0C1C2C3"_block);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700201 BOOST_CHECK_EQUAL(i2.getMinSuffixComponents(), -1); // Default because minSuffixComponents was not encoded
202 BOOST_CHECK(i2.getExclude().empty()); // Exclude was not encoded
203}
204
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000205class Decode03Fixture
Junxiao Shi899277a2017-07-07 22:12:12 +0000206{
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000207protected:
208 Decode03Fixture()
209 {
210 // initialize all elements to non-empty, to verify wireDecode clears them
211 i.setName("/A");
212 i.setForwardingHint({{10309, "/F"}});
213 i.setNonce(0x03d645a8);
214 i.setInterestLifetime(18554_ms);
215 i.setPublisherPublicKeyLocator(Name("/K"));
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500216 i.setParameters("2404A0A1A2A3"_block);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000217 }
Junxiao Shi899277a2017-07-07 22:12:12 +0000218
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000219protected:
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000220 Interest i;
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000221};
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000222
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000223BOOST_FIXTURE_TEST_SUITE(Decode03, Decode03Fixture)
224
225BOOST_AUTO_TEST_CASE(Minimal)
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000226{
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000227 i.wireDecode("0505 0703080149"_block);
228 BOOST_CHECK_EQUAL(i.getName(), "/I");
229 BOOST_CHECK_EQUAL(i.getCanBePrefix(), false);
230 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
231 BOOST_CHECK(i.getForwardingHint().empty());
232 BOOST_CHECK(i.hasNonce()); // a random nonce is generated
233 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
234 BOOST_CHECK(i.getPublisherPublicKeyLocator().empty());
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700235 BOOST_CHECK(!i.hasParameters());
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000236
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000237 BOOST_CHECK(!i.hasWire()); // nonce generation resets wire encoding
238
239 // modify then re-encode as v0.2 format
240 i.setNonce(0x54657c95);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000241 BOOST_CHECK_EQUAL(i.wireEncode(), "0510 0703080149 09030E0101 0A04957C6554"_block);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000242}
243
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000244BOOST_AUTO_TEST_CASE(Full)
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000245{
Junxiao Shi8b753a22018-10-24 01:51:40 +0000246 i.wireDecode("0531 0703080149 FC00 2100 FC00 1200 "
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000247 "FC00 1E0B(1F09 1E023E15 0703080148) FC00 0A044ACB1E4C "
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700248 "FC00 0C0276A1 FC00 2201D6 FC00"_block);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000249 BOOST_CHECK_EQUAL(i.getName(), "/I");
250 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
251 BOOST_CHECK_EQUAL(i.getMustBeFresh(), true);
252 BOOST_CHECK_EQUAL(i.getForwardingHint(), DelegationList({{15893, "/H"}}));
253 BOOST_CHECK(i.hasNonce());
254 BOOST_CHECK_EQUAL(i.getNonce(), 0x4c1ecb4a);
255 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 30369_ms);
256 // HopLimit=214 is not stored
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000257
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000258 // encode without modification: retain original wire encoding
Junxiao Shi8b753a22018-10-24 01:51:40 +0000259 BOOST_CHECK_EQUAL(i.wireEncode().value_size(), 49);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000260
261 // modify then re-encode as v0.2 format
262 i.setName("/J");
Junxiao Shi72c0c642018-04-20 15:41:09 +0000263 BOOST_CHECK_EQUAL(i.wireEncode(),
264 "0520 070308014A 09021200 0A044ACB1E4C 0C0276A1 1E0B(1F09 1E023E15 0703080148)"_block);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000265}
266
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000267BOOST_AUTO_TEST_CASE(CriticalElementOutOfOrder)
268{
269 BOOST_CHECK_THROW(i.wireDecode(
270 "0529 2100 0703080149 1200 1E0B(1F09 1E023E15 0703080148) "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500271 "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3"_block),
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000272 tlv::Error);
273 BOOST_CHECK_THROW(i.wireDecode(
274 "0529 0703080149 1200 2100 1E0B(1F09 1E023E15 0703080148) "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500275 "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3"_block),
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000276 tlv::Error);
277 BOOST_CHECK_THROW(i.wireDecode(
278 "0529 0703080149 2100 1E0B(1F09 1E023E15 0703080148) 1200 "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500279 "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3"_block),
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000280 tlv::Error);
281 BOOST_CHECK_THROW(i.wireDecode(
282 "0529 0703080149 2100 1200 0A044ACB1E4C "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500283 "1E0B(1F09 1E023E15 0703080148) 0C0276A1 2201D6 2404C0C1C2C3"_block),
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000284 tlv::Error);
285 BOOST_CHECK_THROW(i.wireDecode(
286 "0529 0703080149 2100 1200 1E0B(1F09 1E023E15 0703080148) "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500287 "0C0276A1 0A044ACB1E4C 2201D6 2404C0C1C2C3"_block),
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000288 tlv::Error);
289 BOOST_CHECK_THROW(i.wireDecode(
290 "0529 0703080149 2100 1200 1E0B(1F09 1E023E15 0703080148) "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500291 "0A044ACB1E4C 2201D6 0C0276A1 2404C0C1C2C3"_block),
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000292 tlv::Error);
293}
294
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500295BOOST_AUTO_TEST_CASE(NonCriticalElementOutOfOrder)
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000296{
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500297 // HopLimit
298 i.wireDecode("0514 0703080149 2201D6 2200 2404C0C1C2C3 22020101"_block);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000299 BOOST_CHECK_EQUAL(i.getName(), "/I");
300 // HopLimit=214 is not stored
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500301 BOOST_CHECK_EQUAL(i.getParameters(), "2404C0C1C2C3"_block);
302
303 // Parameters
304 i.wireDecode("051F 0703080149 2100 1200 0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3 2401EE"_block);
305 BOOST_CHECK_EQUAL(i.getName(), "/I");
306 BOOST_CHECK_EQUAL(i.hasParameters(), true);
307 BOOST_CHECK_EQUAL(i.getParameters(), "2404C0C1C2C3"_block);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000308}
309
310BOOST_AUTO_TEST_CASE(NameMissing)
311{
312 BOOST_CHECK_THROW(i.wireDecode("0500"_block), tlv::Error);
313 BOOST_CHECK_THROW(i.wireDecode("0502 1200"_block), tlv::Error);
314}
315
316BOOST_AUTO_TEST_CASE(NameEmpty)
317{
318 BOOST_CHECK_THROW(i.wireDecode("0502 0700"_block), tlv::Error);
319}
320
321BOOST_AUTO_TEST_CASE(BadCanBePrefix)
322{
323 BOOST_CHECK_THROW(i.wireDecode("0508 0703080149 210102"_block), tlv::Error);
324}
325
326BOOST_AUTO_TEST_CASE(BadMustBeFresh)
327{
328 BOOST_CHECK_THROW(i.wireDecode("0508 0703080149 120102"_block), tlv::Error);
329}
330
331BOOST_AUTO_TEST_CASE(BadNonce)
332{
333 BOOST_CHECK_THROW(i.wireDecode("0507 0703080149 0A00"_block), tlv::Error);
334 BOOST_CHECK_THROW(i.wireDecode("050A 0703080149 0A0304C263"_block), tlv::Error);
335 BOOST_CHECK_THROW(i.wireDecode("050C 0703080149 0A05EFA420B262"_block), tlv::Error);
336}
337
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500338BOOST_AUTO_TEST_CASE(BadHopLimit)
339{
340 BOOST_CHECK_THROW(i.wireDecode("0507 0703080149 2200"_block), tlv::Error);
341 BOOST_CHECK_THROW(i.wireDecode("0509 0703080149 22021356"_block), tlv::Error);
342}
343
Junxiao Shi8b753a22018-10-24 01:51:40 +0000344BOOST_AUTO_TEST_CASE(UnrecognizedNonCriticalElementBeforeName)
345{
346 BOOST_CHECK_THROW(i.wireDecode("0507 FC00 0703080149"_block), tlv::Error);
347}
348
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000349BOOST_AUTO_TEST_CASE(UnrecognizedCriticalElement)
350{
351 BOOST_CHECK_THROW(i.wireDecode("0507 0703080149 FB00"_block), tlv::Error);
352}
353
354BOOST_AUTO_TEST_SUITE_END() // Decode03
355
Junxiao Shi899277a2017-07-07 22:12:12 +0000356// ---- matching ----
357
358BOOST_AUTO_TEST_CASE(MatchesData)
359{
360 Interest interest;
361 interest.setName("ndn:/A")
362 .setMinSuffixComponents(2)
363 .setMaxSuffixComponents(2)
364 .setPublisherPublicKeyLocator(KeyLocator("ndn:/B"))
365 .setExclude(Exclude().excludeAfter(name::Component("J")));
366
367 Data data("ndn:/A/D");
368 SignatureSha256WithRsa signature(KeyLocator("ndn:/B"));
Junxiao Shidb7464d2017-07-13 03:11:17 +0000369 signature.setValue(encoding::makeEmptyBlock(tlv::SignatureValue));
Junxiao Shi899277a2017-07-07 22:12:12 +0000370 data.setSignature(signature);
371 data.wireEncode();
372 BOOST_CHECK_EQUAL(interest.matchesData(data), true);
373
374 Data data1 = data;
375 data1.setName("ndn:/A"); // violates MinSuffixComponents
376 data1.wireEncode();
377 BOOST_CHECK_EQUAL(interest.matchesData(data1), false);
378
379 Interest interest1 = interest;
380 interest1.setMinSuffixComponents(1);
381 BOOST_CHECK_EQUAL(interest1.matchesData(data1), true);
382
383 Data data2 = data;
384 data2.setName("ndn:/A/E/F"); // violates MaxSuffixComponents
385 data2.wireEncode();
386 BOOST_CHECK_EQUAL(interest.matchesData(data2), false);
387
388 Interest interest2 = interest;
389 interest2.setMaxSuffixComponents(3);
390 BOOST_CHECK_EQUAL(interest2.matchesData(data2), true);
391
392 Data data3 = data;
393 SignatureSha256WithRsa signature3(KeyLocator("ndn:/G")); // violates PublisherPublicKeyLocator
Junxiao Shidb7464d2017-07-13 03:11:17 +0000394 signature3.setValue(encoding::makeEmptyBlock(tlv::SignatureValue));
Junxiao Shi899277a2017-07-07 22:12:12 +0000395 data3.setSignature(signature3);
396 data3.wireEncode();
397 BOOST_CHECK_EQUAL(interest.matchesData(data3), false);
398
399 Interest interest3 = interest;
400 interest3.setPublisherPublicKeyLocator(KeyLocator("ndn:/G"));
401 BOOST_CHECK_EQUAL(interest3.matchesData(data3), true);
402
403 Data data4 = data;
404 DigestSha256 signature4; // violates PublisherPublicKeyLocator
Junxiao Shidb7464d2017-07-13 03:11:17 +0000405 signature4.setValue(encoding::makeEmptyBlock(tlv::SignatureValue));
Junxiao Shi899277a2017-07-07 22:12:12 +0000406 data4.setSignature(signature4);
407 data4.wireEncode();
408 BOOST_CHECK_EQUAL(interest.matchesData(data4), false);
409
410 Interest interest4 = interest;
411 interest4.setPublisherPublicKeyLocator(KeyLocator());
412 BOOST_CHECK_EQUAL(interest4.matchesData(data4), true);
413
414 Data data5 = data;
415 data5.setName("ndn:/A/J"); // violates Exclude
416 data5.wireEncode();
417 BOOST_CHECK_EQUAL(interest.matchesData(data5), false);
418
419 Interest interest5 = interest;
420 interest5.setExclude(Exclude().excludeAfter(name::Component("K")));
421 BOOST_CHECK_EQUAL(interest5.matchesData(data5), true);
422
423 Data data6 = data;
424 data6.setName("ndn:/H/I"); // violates Name
425 data6.wireEncode();
426 BOOST_CHECK_EQUAL(interest.matchesData(data6), false);
427
428 Data data7 = data;
429 data7.setName("ndn:/A/B");
430 data7.wireEncode();
431
Junxiao Shidb7464d2017-07-13 03:11:17 +0000432 Interest interest7("/A/B/sha256digest=54008e240a7eea2714a161dfddf0dd6ced223b3856e9da96792151e180f3b128");
Junxiao Shi899277a2017-07-07 22:12:12 +0000433 BOOST_CHECK_EQUAL(interest7.matchesData(data7), true);
434
435 Interest interest7b("/A/B/sha256digest=0000000000000000000000000000000000000000000000000000000000000000");
436 BOOST_CHECK_EQUAL(interest7b.matchesData(data7), false); // violates implicit digest
437}
438
439BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(MatchesInterest, 1)
440BOOST_AUTO_TEST_CASE(MatchesInterest)
441{
442 Interest interest;
443 interest
444 .setName("/A")
445 .setMinSuffixComponents(2)
446 .setMaxSuffixComponents(2)
447 .setPublisherPublicKeyLocator(KeyLocator("/B"))
448 .setExclude(Exclude().excludeAfter(name::Component("J")))
449 .setNonce(10)
Davide Pesavento0f830802018-01-16 23:58:58 -0500450 .setInterestLifetime(5_s)
Junxiao Shid701e5b2017-07-26 01:30:41 +0000451 .setForwardingHint({{1, "/H"}});
Junxiao Shi899277a2017-07-07 22:12:12 +0000452
453 Interest other;
454 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false);
455
456 other.setName(interest.getName());
457 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false);
458
459 other.setSelectors(interest.getSelectors());
460 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false); // will match until #3162 implemented
461
Junxiao Shid701e5b2017-07-26 01:30:41 +0000462 other.setForwardingHint({{1, "/H"}});
Junxiao Shi899277a2017-07-07 22:12:12 +0000463 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
464
465 other.setNonce(200);
466 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
467
Davide Pesavento0f830802018-01-16 23:58:58 -0500468 other.setInterestLifetime(5_h);
Junxiao Shi899277a2017-07-07 22:12:12 +0000469 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000470}
471
472// ---- field accessors ----
473
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000474BOOST_AUTO_TEST_CASE(CanBePrefix)
475{
476 Interest i;
477 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
478 i.setCanBePrefix(false);
479 BOOST_CHECK_EQUAL(i.getCanBePrefix(), false);
480 BOOST_CHECK_EQUAL(i.getSelectors().getMaxSuffixComponents(), 1);
481 i.setCanBePrefix(true);
482 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
483 BOOST_CHECK_EQUAL(i.getSelectors().getMaxSuffixComponents(), -1);
484}
485
486BOOST_AUTO_TEST_CASE(MustBeFresh)
487{
488 Interest i;
489 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
490 i.setMustBeFresh(true);
491 BOOST_CHECK_EQUAL(i.getMustBeFresh(), true);
492 BOOST_CHECK_EQUAL(i.getSelectors().getMustBeFresh(), true);
493 i.setMustBeFresh(false);
494 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
495 BOOST_CHECK_EQUAL(i.getSelectors().getMustBeFresh(), false);
496}
497
498BOOST_AUTO_TEST_CASE(ModifyForwardingHint)
499{
500 Interest i;
Junxiao Shib55e5d32018-07-18 13:32:00 -0600501 i.setCanBePrefix(false);
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000502 i.setForwardingHint({{1, "/A"}});
503 i.wireEncode();
504 BOOST_CHECK(i.hasWire());
505
506 i.modifyForwardingHint([] (DelegationList& fh) { fh.insert(2, "/B"); });
507 BOOST_CHECK(!i.hasWire());
508 BOOST_CHECK_EQUAL(i.getForwardingHint(), DelegationList({{1, "/A"}, {2, "/B"}}));
509}
510
Junxiao Shi899277a2017-07-07 22:12:12 +0000511BOOST_AUTO_TEST_CASE(GetNonce)
512{
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000513 unique_ptr<Interest> i1, i2;
Junxiao Shi899277a2017-07-07 22:12:12 +0000514
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000515 // getNonce automatically assigns a random Nonce.
516 // It's possible to assign the same Nonce to two Interest, but it's unlikely to get 100 pairs of
517 // same Nonces in a row.
Junxiao Shi899277a2017-07-07 22:12:12 +0000518 int nIterations = 0;
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000519 uint32_t nonce1 = 0, nonce2 = 0;
Junxiao Shi899277a2017-07-07 22:12:12 +0000520 do {
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000521 i1 = make_unique<Interest>();
522 nonce1 = i1->getNonce();
523 i2 = make_unique<Interest>();
524 nonce2 = i2->getNonce();
Junxiao Shi899277a2017-07-07 22:12:12 +0000525 }
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000526 while (nonce1 == nonce2 && ++nIterations < 100);
527 BOOST_CHECK_NE(nonce1, nonce2);
528 BOOST_CHECK(i1->hasNonce());
529 BOOST_CHECK(i2->hasNonce());
Junxiao Shi899277a2017-07-07 22:12:12 +0000530
531 // Once a Nonce is assigned, it should not change.
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000532 BOOST_CHECK_EQUAL(i1->getNonce(), nonce1);
533}
534
535BOOST_AUTO_TEST_CASE(SetNonce)
536{
537 Interest i1("/A");
Junxiao Shib55e5d32018-07-18 13:32:00 -0600538 i1.setCanBePrefix(false);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000539 i1.setNonce(1);
540 i1.wireEncode();
541 BOOST_CHECK_EQUAL(i1.getNonce(), 1);
542
543 Interest i2(i1);
544 BOOST_CHECK_EQUAL(i2.getNonce(), 1);
545
546 i2.setNonce(2);
547 BOOST_CHECK_EQUAL(i2.getNonce(), 2);
548 BOOST_CHECK_EQUAL(i1.getNonce(), 1); // should not affect i1 Nonce (Bug #4168)
Junxiao Shi899277a2017-07-07 22:12:12 +0000549}
550
551BOOST_AUTO_TEST_CASE(RefreshNonce)
552{
553 Interest i;
554 BOOST_CHECK(!i.hasNonce());
555 i.refreshNonce();
556 BOOST_CHECK(!i.hasNonce());
557
558 i.setNonce(1);
559 BOOST_CHECK(i.hasNonce());
560 i.refreshNonce();
561 BOOST_CHECK(i.hasNonce());
562 BOOST_CHECK_NE(i.getNonce(), 1);
563}
564
565BOOST_AUTO_TEST_CASE(SetInterestLifetime)
566{
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500567 BOOST_CHECK_THROW(Interest("/A", -1_ms), std::invalid_argument);
Davide Pesavento0f830802018-01-16 23:58:58 -0500568 BOOST_CHECK_NO_THROW(Interest("/A", 0_ms));
Junxiao Shi899277a2017-07-07 22:12:12 +0000569
570 Interest i("/local/ndn/prefix");
571 i.setNonce(1);
572 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500573 BOOST_CHECK_THROW(i.setInterestLifetime(-1_ms), std::invalid_argument);
Junxiao Shi899277a2017-07-07 22:12:12 +0000574 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesavento0f830802018-01-16 23:58:58 -0500575 i.setInterestLifetime(0_ms);
576 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 0_ms);
577 i.setInterestLifetime(1_ms);
578 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 1_ms);
Junxiao Shi899277a2017-07-07 22:12:12 +0000579}
580
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700581BOOST_AUTO_TEST_CASE(SetParameters)
582{
583 const uint8_t PARAMETERS1[] = {0xc1};
584 const uint8_t PARAMETERS2[] = {0xc2};
585
586 Interest i;
587 BOOST_CHECK(!i.hasParameters());
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500588 i.setParameters("2400"_block);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700589 BOOST_CHECK(i.hasParameters());
590 i.unsetParameters();
591 BOOST_CHECK(!i.hasParameters());
592
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500593 i.setParameters("2401C0"_block); // Block overload
594 BOOST_CHECK_EQUAL(i.getParameters(), "2401C0"_block);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700595 i.setParameters(PARAMETERS1, sizeof(PARAMETERS1)); // raw buffer overload
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500596 BOOST_CHECK_EQUAL(i.getParameters(), "2401C1"_block);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700597 i.setParameters(make_shared<Buffer>(PARAMETERS2, sizeof(PARAMETERS2))); // ConstBufferPtr overload
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500598 BOOST_CHECK_EQUAL(i.getParameters(), "2401C2"_block);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700599 i.setParameters("8001C1"_block); // Block of non-Parameters type
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500600 BOOST_CHECK_EQUAL(i.getParameters(), "24038001C1"_block);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700601}
602
Junxiao Shi899277a2017-07-07 22:12:12 +0000603// ---- operators ----
604
605BOOST_AUTO_TEST_CASE(Equality)
606{
607 Interest a;
608 Interest b;
609
610 // if nonce is not set, it would be set to a random value
611 a.setNonce(1);
612 b.setNonce(1);
613
614 BOOST_CHECK_EQUAL(a == b, true);
615 BOOST_CHECK_EQUAL(a != b, false);
616
617 // compare Name
618 a.setName("/A");
619 BOOST_CHECK_EQUAL(a == b, false);
620 BOOST_CHECK_EQUAL(a != b, true);
621
622 b.setName("/B");
623 BOOST_CHECK_EQUAL(a == b, false);
624 BOOST_CHECK_EQUAL(a != b, true);
625
626 b.setName("/A");
627 BOOST_CHECK_EQUAL(a == b, true);
628 BOOST_CHECK_EQUAL(a != b, false);
629
630 // compare Selectors
631 a.setChildSelector(1);
632 BOOST_CHECK_EQUAL(a == b, false);
633 BOOST_CHECK_EQUAL(a != b, true);
634
635 b.setChildSelector(1);
636 BOOST_CHECK_EQUAL(a == b, true);
637 BOOST_CHECK_EQUAL(a != b, false);
638
639 // compare Nonce
640 a.setNonce(100);
641 BOOST_CHECK_EQUAL(a == b, false);
642 BOOST_CHECK_EQUAL(a != b, true);
643
644 b.setNonce(100);
645 BOOST_CHECK_EQUAL(a == b, true);
646 BOOST_CHECK_EQUAL(a != b, false);
647
648 // compare InterestLifetime
Davide Pesavento0f830802018-01-16 23:58:58 -0500649 a.setInterestLifetime(10_s);
Junxiao Shi899277a2017-07-07 22:12:12 +0000650 BOOST_CHECK_EQUAL(a == b, false);
651 BOOST_CHECK_EQUAL(a != b, true);
652
Davide Pesavento0f830802018-01-16 23:58:58 -0500653 b.setInterestLifetime(10_s);
Junxiao Shi899277a2017-07-07 22:12:12 +0000654 BOOST_CHECK_EQUAL(a == b, true);
655 BOOST_CHECK_EQUAL(a != b, false);
656
Junxiao Shid701e5b2017-07-26 01:30:41 +0000657 // compare ForwardingHint
658 a.setForwardingHint({{1, "/H"}});
Junxiao Shi899277a2017-07-07 22:12:12 +0000659 BOOST_CHECK_EQUAL(a == b, false);
660 BOOST_CHECK_EQUAL(a != b, true);
661
Junxiao Shid701e5b2017-07-26 01:30:41 +0000662 b.setForwardingHint({{1, "/H"}});
Junxiao Shi899277a2017-07-07 22:12:12 +0000663 BOOST_CHECK_EQUAL(a == b, true);
664 BOOST_CHECK_EQUAL(a != b, false);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700665
666 // compare Parameters
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500667 a.setParameters("2404C0C1C2C3"_block);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700668 BOOST_CHECK_EQUAL(a == b, false);
669 BOOST_CHECK_EQUAL(a != b, true);
670
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500671 b.setParameters("2404C0C1C2C3"_block);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700672 BOOST_CHECK_EQUAL(a == b, true);
673 BOOST_CHECK_EQUAL(a != b, false);
Junxiao Shi899277a2017-07-07 22:12:12 +0000674}
675
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100676BOOST_AUTO_TEST_SUITE_END() // TestInterest
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800677
Alexander Afanasyev90164962014-03-06 08:29:59 +0000678} // namespace tests
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800679} // namespace ndn