blob: e536755f5f3597e30dfd8a22cf3684fdd6ab0d5e [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"
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +000028#include "tests/make-interest-data.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
Davide Pesaventoadc9aa22019-06-30 19:00:20 -040035class DisableAutoCheckParametersDigest
36{
37public:
38 DisableAutoCheckParametersDigest()
39 : m_saved(Interest::getAutoCheckParametersDigest())
40 {
41 Interest::setAutoCheckParametersDigest(false);
42 }
43
44 ~DisableAutoCheckParametersDigest()
45 {
46 Interest::setAutoCheckParametersDigest(m_saved);
47 }
48
49private:
50 bool m_saved;
51};
Junxiao Shi899277a2017-07-07 22:12:12 +000052
53BOOST_AUTO_TEST_CASE(DefaultConstructor)
54{
55 Interest i;
Davide Pesaventoadc9aa22019-06-30 19:00:20 -040056 BOOST_CHECK_EQUAL(i.hasWire(), false);
Junxiao Shi899277a2017-07-07 22:12:12 +000057 BOOST_CHECK_EQUAL(i.getName(), "/");
Junxiao Shi6efa3b72018-04-14 15:54:08 +000058 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
59 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -040060 BOOST_CHECK_EQUAL(i.getForwardingHint().empty(), true);
61 BOOST_CHECK_EQUAL(i.hasNonce(), false);
Junxiao Shi899277a2017-07-07 22:12:12 +000062 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -040063 BOOST_CHECK_EQUAL(i.hasSelectors(), false);
64 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), false);
65 BOOST_CHECK_EQUAL(i.getApplicationParameters().isValid(), false);
66 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
Davide Pesaventofccb2dc2019-02-09 01:02:35 -050067}
68
69BOOST_AUTO_TEST_CASE(DecodeNotInterest)
70{
71 BOOST_CHECK_THROW(Interest("4202CAFE"_block), tlv::Error);
Junxiao Shi899277a2017-07-07 22:12:12 +000072}
73
Davide Pesaventoadc9aa22019-06-30 19:00:20 -040074BOOST_AUTO_TEST_SUITE(EncodeDecode02)
75
76BOOST_AUTO_TEST_CASE(Basic)
Junxiao Shi899277a2017-07-07 22:12:12 +000077{
78 const uint8_t WIRE[] = {
79 0x05, 0x1c, // Interest
80 0x07, 0x14, // Name
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +000081 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent
82 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent
83 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent
Junxiao Shi899277a2017-07-07 22:12:12 +000084 0x0a, 0x04, // Nonce
85 0x01, 0x00, 0x00, 0x00
86 };
87
88 Interest i1("/local/ndn/prefix");
Junxiao Shib55e5d32018-07-18 13:32:00 -060089 i1.setCanBePrefix(true);
Junxiao Shi899277a2017-07-07 22:12:12 +000090 i1.setNonce(1);
91 Block wire1 = i1.wireEncode();
92 BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
93
94 Interest i2(wire1);
95 BOOST_CHECK_EQUAL(i2.getName(), "/local/ndn/prefix");
96 BOOST_CHECK(i2.getSelectors().empty());
97 BOOST_CHECK_EQUAL(i2.getNonce(), 1);
98 BOOST_CHECK_EQUAL(i2.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -040099 BOOST_CHECK_EQUAL(i2.hasApplicationParameters(), false);
100 BOOST_CHECK_EQUAL(i2.isParametersDigestValid(), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000101
102 BOOST_CHECK_EQUAL(i1, i2);
103}
104
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400105BOOST_AUTO_TEST_CASE(Full)
Junxiao Shi899277a2017-07-07 22:12:12 +0000106{
107 const uint8_t WIRE[] = {
Junxiao Shi9c154cb2017-07-07 22:14:54 +0000108 0x05, 0x31, // Interest
Junxiao Shi899277a2017-07-07 22:12:12 +0000109 0x07, 0x14, // Name
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000110 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent
111 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent
112 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent
Junxiao Shi899277a2017-07-07 22:12:12 +0000113 0x09, 0x03, // Selectors
114 0x0d, 0x01, 0x01, // MinSuffixComponents
115 0x0a, 0x04, // Nonce
116 0x01, 0x00, 0x00, 0x00,
117 0x0c, 0x02, // InterestLifetime
Junxiao Shi9c154cb2017-07-07 22:14:54 +0000118 0x03, 0xe8,
119 0x1e, 0x0a, // ForwardingHint
120 0x1f, 0x08, // Delegation
121 0x1e, 0x01, 0x01, // Preference=1
122 0x07, 0x03, 0x08, 0x01, 0x41 // Name=/A
Junxiao Shi899277a2017-07-07 22:12:12 +0000123 };
Junxiao Shi899277a2017-07-07 22:12:12 +0000124
125 Interest i1;
126 i1.setName("/local/ndn/prefix");
Junxiao Shib55e5d32018-07-18 13:32:00 -0600127 i1.setCanBePrefix(true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000128 i1.setMinSuffixComponents(1);
129 i1.setNonce(1);
Davide Pesavento0f830802018-01-16 23:58:58 -0500130 i1.setInterestLifetime(1000_ms);
Junxiao Shi9c154cb2017-07-07 22:14:54 +0000131 i1.setForwardingHint({{1, "/A"}});
Junxiao Shi899277a2017-07-07 22:12:12 +0000132 Block wire1 = i1.wireEncode();
133 BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
134
135 Interest i2(wire1);
136 BOOST_CHECK_EQUAL(i2.getName(), "/local/ndn/prefix");
137 BOOST_CHECK_EQUAL(i2.getMinSuffixComponents(), 1);
138 BOOST_CHECK_EQUAL(i2.getNonce(), 1);
Davide Pesavento0f830802018-01-16 23:58:58 -0500139 BOOST_CHECK_EQUAL(i2.getInterestLifetime(), 1000_ms);
Junxiao Shi9c154cb2017-07-07 22:14:54 +0000140 BOOST_CHECK_EQUAL(i2.getForwardingHint(), DelegationList({{1, "/A"}}));
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400141 BOOST_CHECK_EQUAL(i2.hasApplicationParameters(), false);
142 BOOST_CHECK_EQUAL(i2.isParametersDigestValid(), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000143
144 BOOST_CHECK_EQUAL(i1, i2);
145}
146
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400147BOOST_AUTO_TEST_CASE(ParametersSha256DigestComponent)
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700148{
149 const uint8_t WIRE[] = {
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400150 0x05, 0x60, // Interest
151 0x07, 0x58, // Name
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700152 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent
153 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent
154 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400155 0x02, 0x20, // ParametersSha256DigestComponent
156 0xff, 0x91, 0x00, 0xe0, 0x4e, 0xaa, 0xdc, 0xf3, 0x06, 0x74, 0xd9, 0x80,
157 0x26, 0xa0, 0x51, 0xba, 0x25, 0xf5, 0x6b, 0x69, 0xbf, 0xa0, 0x26, 0xdc,
158 0xcc, 0xd7, 0x2c, 0x6e, 0xa0, 0xf7, 0x31, 0x5a,
159 0x02, 0x20, // ParametersSha256DigestComponent
160 0xff, 0x91, 0x00, 0xe0, 0x4e, 0xaa, 0xdc, 0xf3, 0x06, 0x74, 0xd9, 0x80,
161 0x26, 0xa0, 0x51, 0xba, 0x25, 0xf5, 0x6b, 0x69, 0xbf, 0xa0, 0x26, 0xdc,
162 0xcc, 0xd7, 0x2c, 0x6e, 0xa0, 0xf7, 0x31, 0x5a,
163 0x0a, 0x04, // Nonce
164 0x01, 0x00, 0x00, 0x00,
165 };
166
167 Interest i1("/I");
168 BOOST_CHECK_THROW(i1.wireDecode(Block(WIRE, sizeof(WIRE))), tlv::Error);
169
170 // i1 is still in a valid state
171 BOOST_CHECK_EQUAL(i1.getName(), "/I");
172 BOOST_CHECK_EQUAL(i1.isParametersDigestValid(), true);
173
174 Interest i2("/I/params-sha256=f16db273f40436a852063f864d5072b01ead53151f5a688ea1560492bebedd05");
175 i2.setCanBePrefix(true);
176 i2.setNonce(2);
177 BOOST_CHECK_EQUAL(i2.isParametersDigestValid(), false);
178 // encoding in v0.2 format does not validate the ParametersSha256DigestComponent
179 Block wire2 = i2.wireEncode();
180 BOOST_CHECK_EQUAL(wire2, "052D 0725(080149 "
181 "0220F16DB273F40436A852063F864D5072B01EAD53151F5A688EA1560492BEBEDD05) "
182 "0A0402000000"_block);
183
184 // decoding from v0.2 format does not validate the ParametersSha256DigestComponent
185 Interest i3(wire2);
186 BOOST_CHECK_EQUAL(i3.getName(), i2.getName());
187 BOOST_CHECK_EQUAL(i3.isParametersDigestValid(), false);
188}
189
190BOOST_AUTO_TEST_SUITE_END() // EncodeDecode02
191
192BOOST_AUTO_TEST_SUITE(Encode03)
193
194// Enable after #4567
195//BOOST_AUTO_TEST_CASE(Basic)
196//{
197// const uint8_t WIRE[] = {
198// 0x05, 0x1c, // Interest
199// 0x07, 0x14, // Name
200// 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent
201// 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent
202// 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent
203// 0x0a, 0x04, // Nonce
204// 0x01, 0x00, 0x00, 0x00,
205// };
206
207// Interest i1;
208// i1.setName("/local/ndn/prefix");
209// i1.setCanBePrefix(false);
210// i1.setNonce(1);
211// BOOST_CHECK_EQUAL(i1.isParametersDigestValid(), true);
212
213// Block wire1 = i1.wireEncode();
214// BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
215
216// Interest i2(wire1);
217// BOOST_CHECK_EQUAL(i2.getName(), "/local/ndn/prefix");
218// BOOST_CHECK_EQUAL(i2.getCanBePrefix(), false);
219// BOOST_CHECK_EQUAL(i2.getMustBeFresh(), false);
220// BOOST_CHECK_EQUAL(i2.getForwardingHint().empty(), true);
221// BOOST_CHECK_EQUAL(i2.getNonce(), 1);
222// BOOST_CHECK_EQUAL(i2.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
223// BOOST_CHECK_EQUAL(i2.hasApplicationParameters(), false);
224// BOOST_CHECK_EQUAL(i2.getApplicationParameters().isValid(), false);
225// BOOST_CHECK_EQUAL(i2.getPublisherPublicKeyLocator().empty(), true);
226//}
227
228BOOST_AUTO_TEST_CASE(WithParameters)
229{
230 const uint8_t WIRE[] = {
231 0x05, 0x44, // Interest
232 0x07, 0x36, // Name
233 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent
234 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent
235 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent
236 0x02, 0x20, // ParametersSha256DigestComponent
237 0xff, 0x91, 0x00, 0xe0, 0x4e, 0xaa, 0xdc, 0xf3, 0x06, 0x74, 0xd9, 0x80,
238 0x26, 0xa0, 0x51, 0xba, 0x25, 0xf5, 0x6b, 0x69, 0xbf, 0xa0, 0x26, 0xdc,
239 0xcc, 0xd7, 0x2c, 0x6e, 0xa0, 0xf7, 0x31, 0x5a,
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700240 0x0a, 0x04, // Nonce
241 0x01, 0x00, 0x00, 0x00,
Davide Pesavento9c19a392019-04-06 15:07:54 -0400242 0x24, 0x04, // ApplicationParameters
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400243 0xc0, 0xc1, 0xc2, 0xc3
244 };
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700245
246 Interest i1;
247 i1.setName("/local/ndn/prefix");
248 i1.setCanBePrefix(false);
249 i1.setNonce(1);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400250 i1.setApplicationParameters("2404C0C1C2C3"_block);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400251 BOOST_CHECK_EQUAL(i1.isParametersDigestValid(), true);
252
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700253 Block wire1 = i1.wireEncode();
254 BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
255
256 Interest i2(wire1);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400257 BOOST_CHECK_EQUAL(i2.getName(),
258 "/local/ndn/prefix/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a");
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700259 BOOST_CHECK_EQUAL(i2.getCanBePrefix(), false);
260 BOOST_CHECK_EQUAL(i2.getMustBeFresh(), false);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400261 BOOST_CHECK_EQUAL(i2.getForwardingHint().empty(), true);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700262 BOOST_CHECK_EQUAL(i2.getNonce(), 1);
263 BOOST_CHECK_EQUAL(i2.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400264 BOOST_CHECK_EQUAL(i2.hasApplicationParameters(), true);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400265 BOOST_CHECK_EQUAL(i2.getApplicationParameters(), "2404C0C1C2C3"_block);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400266 BOOST_CHECK_EQUAL(i2.getPublisherPublicKeyLocator().empty(), true);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700267}
268
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400269BOOST_AUTO_TEST_CASE(Full)
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700270{
271 const uint8_t WIRE[] = {
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400272 0x05, 0x59, // Interest
273 0x07, 0x36, // Name
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700274 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent
275 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent
276 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400277 0x02, 0x20, // ParametersSha256DigestComponent
278 0xff, 0x91, 0x00, 0xe0, 0x4e, 0xaa, 0xdc, 0xf3, 0x06, 0x74, 0xd9, 0x80,
279 0x26, 0xa0, 0x51, 0xba, 0x25, 0xf5, 0x6b, 0x69, 0xbf, 0xa0, 0x26, 0xdc,
280 0xcc, 0xd7, 0x2c, 0x6e, 0xa0, 0xf7, 0x31, 0x5a,
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700281 0x21, 0x00, // CanBePrefix
282 0x12, 0x00, // MustBeFresh
283 0x1e, 0x0b, // ForwardingHint
284 0x1f, 0x09, // Delegation List
285 0x1e, 0x02,
286 0x3e, 0x15,
287 0x07, 0x03,
288 0x08, 0x01, 0x48,
289 0x0a, 0x04, // Nonce
290 0x4a, 0xcb, 0x1e, 0x4c,
291 0x0c, 0x02, // Interest Lifetime
292 0x76, 0xa1,
Davide Pesavento9c19a392019-04-06 15:07:54 -0400293 0x24, 0x04, // ApplicationParameters
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400294 0xc0, 0xc1, 0xc2, 0xc3
295 };
296
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700297 Interest i1;
298 i1.setName("/local/ndn/prefix");
299 i1.setMustBeFresh(true);
300 i1.setCanBePrefix(true);
301 i1.setForwardingHint(DelegationList({{15893, "/H"}}));
302 i1.setNonce(0x4c1ecb4a);
303 i1.setInterestLifetime(30369_ms);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400304 i1.setApplicationParameters("2404C0C1C2C3"_block);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700305 i1.setMinSuffixComponents(1); // v0.2-only elements will not be encoded
306 i1.setExclude(Exclude().excludeAfter(name::Component("J"))); // v0.2-only elements will not be encoded
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400307 BOOST_CHECK_EQUAL(i1.isParametersDigestValid(), true);
308
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700309 Block wire1 = i1.wireEncode();
310 BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
311
312 Interest i2(wire1);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400313 BOOST_CHECK_EQUAL(i2.getName(),
314 "/local/ndn/prefix/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a");
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700315 BOOST_CHECK_EQUAL(i2.getCanBePrefix(), true);
316 BOOST_CHECK_EQUAL(i2.getMustBeFresh(), true);
317 BOOST_CHECK_EQUAL(i2.getForwardingHint(), DelegationList({{15893, "/H"}}));
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400318 BOOST_CHECK_EQUAL(i2.hasNonce(), true);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700319 BOOST_CHECK_EQUAL(i2.getNonce(), 0x4c1ecb4a);
320 BOOST_CHECK_EQUAL(i2.getInterestLifetime(), 30369_ms);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400321 BOOST_CHECK_EQUAL(i2.getApplicationParameters(), "2404C0C1C2C3"_block);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700322 BOOST_CHECK_EQUAL(i2.getMinSuffixComponents(), -1); // Default because minSuffixComponents was not encoded
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400323 BOOST_CHECK_EQUAL(i2.getExclude().empty(), true); // Exclude was not encoded
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700324}
325
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400326// Enable after #4567
327//BOOST_AUTO_TEST_CASE(MissingApplicationParameters)
328//{
329// Interest i;
330// i.setName(Name("/A").appendParametersSha256DigestPlaceholder());
331// i.setCanBePrefix(false);
332// BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
333// BOOST_CHECK_THROW(i.wireEncode(), tlv::Error);
334//}
335
336BOOST_AUTO_TEST_CASE(MissingParametersSha256DigestComponent)
337{
338 // there's no way to create an Interest that fails this check via programmatic construction,
339 // so we have to decode an invalid Interest and force reencoding
340
341 DisableAutoCheckParametersDigest disabler;
342 Interest i("050F 0703(080149) 0A04F000F000 2402CAFE"_block);
343 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
344 BOOST_CHECK_NO_THROW(i.wireEncode()); // this succeeds because it uses the cached wire encoding
345
346 i.setNonce(42); // trigger reencoding
347 BOOST_CHECK_THROW(i.wireEncode(), tlv::Error); // now the check fails while attempting to reencode
348}
349
350BOOST_AUTO_TEST_SUITE_END() // Encode03
351
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000352class Decode03Fixture
Junxiao Shi899277a2017-07-07 22:12:12 +0000353{
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000354protected:
355 Decode03Fixture()
356 {
357 // initialize all elements to non-empty, to verify wireDecode clears them
358 i.setName("/A");
359 i.setForwardingHint({{10309, "/F"}});
360 i.setNonce(0x03d645a8);
361 i.setInterestLifetime(18554_ms);
362 i.setPublisherPublicKeyLocator(Name("/K"));
Davide Pesavento9c19a392019-04-06 15:07:54 -0400363 i.setApplicationParameters("2404A0A1A2A3"_block);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000364 }
Junxiao Shi899277a2017-07-07 22:12:12 +0000365
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000366protected:
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000367 Interest i;
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000368};
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000369
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000370BOOST_FIXTURE_TEST_SUITE(Decode03, Decode03Fixture)
371
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400372BOOST_AUTO_TEST_CASE(NameOnly)
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000373{
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400374 i.wireDecode("0505 0703(080149)"_block);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000375 BOOST_CHECK_EQUAL(i.getName(), "/I");
376 BOOST_CHECK_EQUAL(i.getCanBePrefix(), false);
377 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400378 BOOST_CHECK_EQUAL(i.getForwardingHint().empty(), true);
379 BOOST_CHECK_EQUAL(i.hasNonce(), true); // a random nonce is generated
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000380 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400381 BOOST_CHECK_EQUAL(i.getPublisherPublicKeyLocator().empty(), true);
382 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), false);
383 BOOST_CHECK_EQUAL(i.getApplicationParameters().isValid(), false);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000384
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000385 BOOST_CHECK(!i.hasWire()); // nonce generation resets wire encoding
386
387 // modify then re-encode as v0.2 format
388 i.setNonce(0x54657c95);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400389 BOOST_CHECK_EQUAL(i.wireEncode(), "0510 0703(080149) 09030E0101 0A04957C6554"_block);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000390}
391
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400392BOOST_AUTO_TEST_CASE(NameCanBePrefix)
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000393{
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400394 i.wireDecode("0507 0703(080149) 2100"_block);
395 BOOST_CHECK_EQUAL(i.getName(), "/I");
396 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
397 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
398 BOOST_CHECK_EQUAL(i.getForwardingHint().empty(), true);
399 BOOST_CHECK_EQUAL(i.hasNonce(), true); // a random nonce is generated
400 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
401 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), false);
402 BOOST_CHECK_EQUAL(i.getApplicationParameters().isValid(), false);
403}
404
405BOOST_AUTO_TEST_CASE(FullWithoutParameters)
406{
407 i.wireDecode("0531 0703(080149) "
408 "FC00 2100 FC00 1200 FC00 1E0B(1F09 1E023E15 0703080148) "
409 "FC00 0A044ACB1E4C FC00 0C0276A1 FC00 2201D6 FC00"_block);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000410 BOOST_CHECK_EQUAL(i.getName(), "/I");
411 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
412 BOOST_CHECK_EQUAL(i.getMustBeFresh(), true);
413 BOOST_CHECK_EQUAL(i.getForwardingHint(), DelegationList({{15893, "/H"}}));
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400414 BOOST_CHECK_EQUAL(i.hasNonce(), true);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000415 BOOST_CHECK_EQUAL(i.getNonce(), 0x4c1ecb4a);
416 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 30369_ms);
417 // HopLimit=214 is not stored
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400418 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), false);
419 BOOST_CHECK_EQUAL(i.getApplicationParameters().isValid(), false);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000420
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000421 // encode without modification: retain original wire encoding
Junxiao Shi8b753a22018-10-24 01:51:40 +0000422 BOOST_CHECK_EQUAL(i.wireEncode().value_size(), 49);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000423
424 // modify then re-encode as v0.2 format
425 i.setName("/J");
Junxiao Shi72c0c642018-04-20 15:41:09 +0000426 BOOST_CHECK_EQUAL(i.wireEncode(),
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400427 "0520 0703(08014A) 09021200 0A044ACB1E4C 0C0276A1 1E0B(1F09 1E023E15 0703080148)"_block);
428}
429
430BOOST_AUTO_TEST_CASE(FullWithParameters)
431{
432 i.wireDecode("055B 0725(080149 0220F16DB273F40436A852063F864D5072B01EAD53151F5A688EA1560492BEBEDD05) "
433 "FC00 2100 FC00 1200 FC00 1E0B(1F09 1E023E15 0703080148) "
434 "FC00 0A044ACB1E4C FC00 0C0276A1 FC00 2201D6 FC00 2404C0C1C2C3 FC00"_block);
435 BOOST_CHECK_EQUAL(i.getName(),
436 "/I/params-sha256=f16db273f40436a852063f864d5072b01ead53151f5a688ea1560492bebedd05");
437 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
438 BOOST_CHECK_EQUAL(i.getMustBeFresh(), true);
439 BOOST_CHECK_EQUAL(i.getForwardingHint(), DelegationList({{15893, "/H"}}));
440 BOOST_CHECK_EQUAL(i.hasNonce(), true);
441 BOOST_CHECK_EQUAL(i.getNonce(), 0x4c1ecb4a);
442 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 30369_ms);
443 // HopLimit=214 is not stored
444 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true);
445 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2404C0C1C2C3"_block);
446
447 // encode without modification: retain original wire encoding
448 BOOST_CHECK_EQUAL(i.wireEncode().value_size(), 91);
449
450 // modify then re-encode as v0.3 format:
451 // - unrecognized elements after ApplicationParameters are preserved, the rest are discarded
452 // - HopLimit is dropped (encoding not implemented)
453 i.setName("/J");
454 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
455 BOOST_CHECK_EQUAL(i.wireEncode(),
456 "054A 0725(08014A 0220F16DB273F40436A852063F864D5072B01EAD53151F5A688EA1560492BEBEDD05) "
457 "2100 1200 1E0B(1F09 1E023E15 0703080148) "
458 "0A044ACB1E4C 0C0276A1 2404C0C1C2C3 FC00"_block);
459
460 // modify ApplicationParameters: unrecognized elements are preserved
461 i.setApplicationParameters("2402CAFE"_block);
462 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
463 BOOST_CHECK_EQUAL(i.wireEncode(),
464 "0548 0725(08014A 02205FDA67967EE302FC457E41B7D3D51BA6A9379574D193FD88F64954BF16C2927A) "
465 "2100 1200 1E0B(1F09 1E023E15 0703080148) "
466 "0A044ACB1E4C 0C0276A1 2402CAFE FC00"_block);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000467}
468
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000469BOOST_AUTO_TEST_CASE(CriticalElementOutOfOrder)
470{
471 BOOST_CHECK_THROW(i.wireDecode(
472 "0529 2100 0703080149 1200 1E0B(1F09 1E023E15 0703080148) "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500473 "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3"_block),
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000474 tlv::Error);
475 BOOST_CHECK_THROW(i.wireDecode(
476 "0529 0703080149 1200 2100 1E0B(1F09 1E023E15 0703080148) "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500477 "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3"_block),
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000478 tlv::Error);
479 BOOST_CHECK_THROW(i.wireDecode(
480 "0529 0703080149 2100 1E0B(1F09 1E023E15 0703080148) 1200 "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500481 "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3"_block),
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000482 tlv::Error);
483 BOOST_CHECK_THROW(i.wireDecode(
484 "0529 0703080149 2100 1200 0A044ACB1E4C "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500485 "1E0B(1F09 1E023E15 0703080148) 0C0276A1 2201D6 2404C0C1C2C3"_block),
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000486 tlv::Error);
487 BOOST_CHECK_THROW(i.wireDecode(
488 "0529 0703080149 2100 1200 1E0B(1F09 1E023E15 0703080148) "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500489 "0C0276A1 0A044ACB1E4C 2201D6 2404C0C1C2C3"_block),
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000490 tlv::Error);
491 BOOST_CHECK_THROW(i.wireDecode(
492 "0529 0703080149 2100 1200 1E0B(1F09 1E023E15 0703080148) "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500493 "0A044ACB1E4C 2201D6 0C0276A1 2404C0C1C2C3"_block),
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000494 tlv::Error);
495}
496
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500497BOOST_AUTO_TEST_CASE(NonCriticalElementOutOfOrder)
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000498{
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400499 // duplicate HopLimit
500 i.wireDecode("0536 0725(080149 0220FF9100E04EAADCF30674D98026A051BA25F56B69BFA026DCCCD72C6EA0F7315A)"
501 "2201D6 2200 2404C0C1C2C3 22020101"_block);
502 BOOST_CHECK_EQUAL(i.getName(),
503 "/I/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a");
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000504 // HopLimit=214 is not stored
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400505 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400506 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2404C0C1C2C3"_block);
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500507
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400508 // duplicate ApplicationParameters
509 i.wireDecode("0541 0725(080149 0220FF9100E04EAADCF30674D98026A051BA25F56B69BFA026DCCCD72C6EA0F7315A)"
510 "2100 1200 0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3 2401EE"_block);
511 BOOST_CHECK_EQUAL(i.getName(),
512 "/I/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a");
Davide Pesavento9c19a392019-04-06 15:07:54 -0400513 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true);
514 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2404C0C1C2C3"_block);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000515}
516
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400517BOOST_AUTO_TEST_CASE(MissingName)
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000518{
519 BOOST_CHECK_THROW(i.wireDecode("0500"_block), tlv::Error);
520 BOOST_CHECK_THROW(i.wireDecode("0502 1200"_block), tlv::Error);
521}
522
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400523BOOST_AUTO_TEST_CASE(BadName)
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000524{
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400525 // empty
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000526 BOOST_CHECK_THROW(i.wireDecode("0502 0700"_block), tlv::Error);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400527
528 // more than one ParametersSha256DigestComponent
529 BOOST_CHECK_THROW(i.wireDecode("054C 074A(080149"
530 "02200000000000000000000000000000000000000000000000000000000000000000"
531 "080132"
532 "02200000000000000000000000000000000000000000000000000000000000000000)"_block),
533 tlv::Error);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000534}
535
536BOOST_AUTO_TEST_CASE(BadCanBePrefix)
537{
538 BOOST_CHECK_THROW(i.wireDecode("0508 0703080149 210102"_block), tlv::Error);
539}
540
541BOOST_AUTO_TEST_CASE(BadMustBeFresh)
542{
543 BOOST_CHECK_THROW(i.wireDecode("0508 0703080149 120102"_block), tlv::Error);
544}
545
546BOOST_AUTO_TEST_CASE(BadNonce)
547{
548 BOOST_CHECK_THROW(i.wireDecode("0507 0703080149 0A00"_block), tlv::Error);
549 BOOST_CHECK_THROW(i.wireDecode("050A 0703080149 0A0304C263"_block), tlv::Error);
550 BOOST_CHECK_THROW(i.wireDecode("050C 0703080149 0A05EFA420B262"_block), tlv::Error);
551}
552
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500553BOOST_AUTO_TEST_CASE(BadHopLimit)
554{
555 BOOST_CHECK_THROW(i.wireDecode("0507 0703080149 2200"_block), tlv::Error);
556 BOOST_CHECK_THROW(i.wireDecode("0509 0703080149 22021356"_block), tlv::Error);
557}
558
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400559BOOST_AUTO_TEST_CASE(BadParametersDigest)
560{
561 // ApplicationParameters without ParametersSha256DigestComponent
562 Block b1("0509 0703(080149) 2402CAFE"_block);
563 // ParametersSha256DigestComponent without ApplicationParameters
564 Block b2("0527 0725(080149 0220E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855)"_block);
565 // digest mismatch
566 Block b3("052B 0725(080149 02200000000000000000000000000000000000000000000000000000000000000000) "
567 "2402CAFE"_block);
568
569 BOOST_CHECK_THROW(i.wireDecode(b1), tlv::Error);
570 BOOST_CHECK_THROW(i.wireDecode(b2), tlv::Error);
571 BOOST_CHECK_THROW(i.wireDecode(b3), tlv::Error);
572
573 DisableAutoCheckParametersDigest disabler;
574 BOOST_CHECK_NO_THROW(i.wireDecode(b1));
575 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
576 BOOST_CHECK_NO_THROW(i.wireDecode(b2));
577 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
578 BOOST_CHECK_NO_THROW(i.wireDecode(b3));
579 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
580}
581
Junxiao Shi8b753a22018-10-24 01:51:40 +0000582BOOST_AUTO_TEST_CASE(UnrecognizedNonCriticalElementBeforeName)
583{
584 BOOST_CHECK_THROW(i.wireDecode("0507 FC00 0703080149"_block), tlv::Error);
585}
586
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000587BOOST_AUTO_TEST_CASE(UnrecognizedCriticalElement)
588{
589 BOOST_CHECK_THROW(i.wireDecode("0507 0703080149 FB00"_block), tlv::Error);
590}
591
592BOOST_AUTO_TEST_SUITE_END() // Decode03
593
Junxiao Shi899277a2017-07-07 22:12:12 +0000594BOOST_AUTO_TEST_CASE(MatchesData)
595{
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000596 auto interest = makeInterest("/A");
Junxiao Shi899277a2017-07-07 22:12:12 +0000597
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000598 auto data = makeData("/A");
599 BOOST_CHECK_EQUAL(interest->matchesData(*data), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000600
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000601 data->setName("/A/D");
602 BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // violates CanBePrefix
Junxiao Shi899277a2017-07-07 22:12:12 +0000603
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000604 interest->setCanBePrefix(true);
605 BOOST_CHECK_EQUAL(interest->matchesData(*data), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000606
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000607 interest->setMustBeFresh(true);
608 BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // violates MustBeFresh
Junxiao Shi899277a2017-07-07 22:12:12 +0000609
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000610 data->setFreshnessPeriod(1_s);
611 BOOST_CHECK_EQUAL(interest->matchesData(*data), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000612
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000613 data->setName("/H/I");
614 BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // Name does not match
Junxiao Shi899277a2017-07-07 22:12:12 +0000615
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000616 data->wireEncode();
617 interest = makeInterest(data->getFullName());
618 BOOST_CHECK_EQUAL(interest->matchesData(*data), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000619
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400620 setNameComponent(*interest, -1, name::Component::fromEscapedString(
621 "sha256digest=0000000000000000000000000000000000000000000000000000000000000000"));
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000622 BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // violates implicit digest
Junxiao Shi899277a2017-07-07 22:12:12 +0000623}
624
625BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(MatchesInterest, 1)
626BOOST_AUTO_TEST_CASE(MatchesInterest)
627{
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000628 Interest interest("/A");
629 interest.setCanBePrefix(true)
630 .setMustBeFresh(true)
631 .setForwardingHint({{1, "/H"}})
632 .setNonce(2228)
633 .setInterestLifetime(5_s);
Junxiao Shi899277a2017-07-07 22:12:12 +0000634
635 Interest other;
636 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false);
637
638 other.setName(interest.getName());
639 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false);
640
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000641 other.setCanBePrefix(interest.getCanBePrefix());
642 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false);
643
644 other.setMustBeFresh(interest.getMustBeFresh());
Junxiao Shi899277a2017-07-07 22:12:12 +0000645 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false); // will match until #3162 implemented
646
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000647 other.setForwardingHint(interest.getForwardingHint());
Junxiao Shi899277a2017-07-07 22:12:12 +0000648 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
649
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000650 other.setNonce(9336);
Junxiao Shi899277a2017-07-07 22:12:12 +0000651 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
652
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000653 other.setInterestLifetime(3_s);
Junxiao Shi899277a2017-07-07 22:12:12 +0000654 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000655}
656
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400657BOOST_AUTO_TEST_CASE(SetName)
658{
659 Interest i;
660 BOOST_CHECK_EQUAL(i.getName(), "/");
661 i.setName("/A/B");
662 BOOST_CHECK_EQUAL(i.getName(), "/A/B");
663 i.setName("/I/params-sha256=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
664 BOOST_CHECK_EQUAL(i.getName(),
665 "/I/params-sha256=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
666 BOOST_CHECK_THROW(i.setName("/I"
667 "/params-sha256=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
668 "/params-sha256=0000000000000000000000000000000000000000000000000000000000000000"),
669 std::invalid_argument);
670}
Junxiao Shi899277a2017-07-07 22:12:12 +0000671
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400672BOOST_AUTO_TEST_CASE(SetCanBePrefix)
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000673{
674 Interest i;
675 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
676 i.setCanBePrefix(false);
677 BOOST_CHECK_EQUAL(i.getCanBePrefix(), false);
678 BOOST_CHECK_EQUAL(i.getSelectors().getMaxSuffixComponents(), 1);
679 i.setCanBePrefix(true);
680 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
681 BOOST_CHECK_EQUAL(i.getSelectors().getMaxSuffixComponents(), -1);
682}
683
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400684BOOST_AUTO_TEST_CASE(SetMustBeFresh)
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000685{
686 Interest i;
687 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
688 i.setMustBeFresh(true);
689 BOOST_CHECK_EQUAL(i.getMustBeFresh(), true);
690 BOOST_CHECK_EQUAL(i.getSelectors().getMustBeFresh(), true);
691 i.setMustBeFresh(false);
692 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
693 BOOST_CHECK_EQUAL(i.getSelectors().getMustBeFresh(), false);
694}
695
696BOOST_AUTO_TEST_CASE(ModifyForwardingHint)
697{
698 Interest i;
Junxiao Shib55e5d32018-07-18 13:32:00 -0600699 i.setCanBePrefix(false);
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000700 i.setForwardingHint({{1, "/A"}});
701 i.wireEncode();
702 BOOST_CHECK(i.hasWire());
703
704 i.modifyForwardingHint([] (DelegationList& fh) { fh.insert(2, "/B"); });
705 BOOST_CHECK(!i.hasWire());
706 BOOST_CHECK_EQUAL(i.getForwardingHint(), DelegationList({{1, "/A"}, {2, "/B"}}));
707}
708
Junxiao Shi899277a2017-07-07 22:12:12 +0000709BOOST_AUTO_TEST_CASE(GetNonce)
710{
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000711 unique_ptr<Interest> i1, i2;
Junxiao Shi899277a2017-07-07 22:12:12 +0000712
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000713 // getNonce automatically assigns a random Nonce.
714 // It's possible to assign the same Nonce to two Interest, but it's unlikely to get 100 pairs of
715 // same Nonces in a row.
Junxiao Shi899277a2017-07-07 22:12:12 +0000716 int nIterations = 0;
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000717 uint32_t nonce1 = 0, nonce2 = 0;
Junxiao Shi899277a2017-07-07 22:12:12 +0000718 do {
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000719 i1 = make_unique<Interest>();
720 nonce1 = i1->getNonce();
721 i2 = make_unique<Interest>();
722 nonce2 = i2->getNonce();
Junxiao Shi899277a2017-07-07 22:12:12 +0000723 }
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000724 while (nonce1 == nonce2 && ++nIterations < 100);
725 BOOST_CHECK_NE(nonce1, nonce2);
726 BOOST_CHECK(i1->hasNonce());
727 BOOST_CHECK(i2->hasNonce());
Junxiao Shi899277a2017-07-07 22:12:12 +0000728
729 // Once a Nonce is assigned, it should not change.
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000730 BOOST_CHECK_EQUAL(i1->getNonce(), nonce1);
731}
732
733BOOST_AUTO_TEST_CASE(SetNonce)
734{
735 Interest i1("/A");
Junxiao Shib55e5d32018-07-18 13:32:00 -0600736 i1.setCanBePrefix(false);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000737 i1.setNonce(1);
738 i1.wireEncode();
739 BOOST_CHECK_EQUAL(i1.getNonce(), 1);
740
741 Interest i2(i1);
742 BOOST_CHECK_EQUAL(i2.getNonce(), 1);
743
744 i2.setNonce(2);
745 BOOST_CHECK_EQUAL(i2.getNonce(), 2);
746 BOOST_CHECK_EQUAL(i1.getNonce(), 1); // should not affect i1 Nonce (Bug #4168)
Junxiao Shi899277a2017-07-07 22:12:12 +0000747}
748
749BOOST_AUTO_TEST_CASE(RefreshNonce)
750{
751 Interest i;
752 BOOST_CHECK(!i.hasNonce());
753 i.refreshNonce();
754 BOOST_CHECK(!i.hasNonce());
755
756 i.setNonce(1);
757 BOOST_CHECK(i.hasNonce());
758 i.refreshNonce();
759 BOOST_CHECK(i.hasNonce());
760 BOOST_CHECK_NE(i.getNonce(), 1);
761}
762
763BOOST_AUTO_TEST_CASE(SetInterestLifetime)
764{
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500765 BOOST_CHECK_THROW(Interest("/A", -1_ms), std::invalid_argument);
Davide Pesavento0f830802018-01-16 23:58:58 -0500766 BOOST_CHECK_NO_THROW(Interest("/A", 0_ms));
Junxiao Shi899277a2017-07-07 22:12:12 +0000767
768 Interest i("/local/ndn/prefix");
769 i.setNonce(1);
770 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500771 BOOST_CHECK_THROW(i.setInterestLifetime(-1_ms), std::invalid_argument);
Junxiao Shi899277a2017-07-07 22:12:12 +0000772 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesavento0f830802018-01-16 23:58:58 -0500773 i.setInterestLifetime(0_ms);
774 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 0_ms);
775 i.setInterestLifetime(1_ms);
776 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 1_ms);
Junxiao Shi899277a2017-07-07 22:12:12 +0000777}
778
Davide Pesavento9c19a392019-04-06 15:07:54 -0400779BOOST_AUTO_TEST_CASE(SetApplicationParameters)
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700780{
781 const uint8_t PARAMETERS1[] = {0xc1};
782 const uint8_t PARAMETERS2[] = {0xc2};
783
784 Interest i;
Davide Pesavento9c19a392019-04-06 15:07:54 -0400785 BOOST_CHECK(!i.hasApplicationParameters());
786 i.setApplicationParameters("2400"_block);
787 BOOST_CHECK(i.hasApplicationParameters());
788 i.unsetApplicationParameters();
789 BOOST_CHECK(!i.hasApplicationParameters());
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700790
Davide Pesavento38912442019-04-06 22:03:39 -0400791 // Block overload
792 i.setApplicationParameters(Block{});
793 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2400"_block);
794 i.setApplicationParameters("2401C0"_block);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400795 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2401C0"_block);
Davide Pesavento38912442019-04-06 22:03:39 -0400796 i.setApplicationParameters("8001C1"_block);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400797 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "24038001C1"_block);
Davide Pesavento38912442019-04-06 22:03:39 -0400798
799 // raw buffer+size overload
800 i.setApplicationParameters(PARAMETERS1, sizeof(PARAMETERS1));
801 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2401C1"_block);
802 i.setApplicationParameters(nullptr, 0);
803 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2400"_block);
804 BOOST_CHECK_THROW(i.setApplicationParameters(nullptr, 42), std::invalid_argument);
805
806 // ConstBufferPtr overload
807 i.setApplicationParameters(make_shared<Buffer>(PARAMETERS2, sizeof(PARAMETERS2)));
808 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2401C2"_block);
809 i.setApplicationParameters(make_shared<Buffer>());
810 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2400"_block);
811 BOOST_CHECK_THROW(i.setApplicationParameters(nullptr), std::invalid_argument);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700812}
813
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400814BOOST_AUTO_TEST_CASE(ParametersSha256DigestComponent)
815{
816 Interest i("/I");
817 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
818
819 i.setApplicationParameters("2404C0C1C2C3"_block); // auto-appends ParametersSha256DigestComponent
820 BOOST_CHECK_EQUAL(i.getName(),
821 "/I/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a");
822 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
823
824 i.setApplicationParameters(nullptr, 0); // updates ParametersSha256DigestComponent
825 BOOST_CHECK_EQUAL(i.getName(),
826 "/I/params-sha256=33b67cb5385ceddad93d0ee960679041613bed34b8b4a5e6362fe7539ba2d3ce");
827 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true);
828 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
829
830 i.unsetApplicationParameters(); // removes ParametersSha256DigestComponent
831 BOOST_CHECK_EQUAL(i.getName(), "/I");
832 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
833
834 i.setName(Name("/P").appendParametersSha256DigestPlaceholder().append("Q"));
835 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), false);
836 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
837
838 i.unsetApplicationParameters(); // removes ParametersSha256DigestComponent
839 BOOST_CHECK_EQUAL(i.getName(), "/P/Q");
840 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
841
842 i.setName(Name("/P").appendParametersSha256DigestPlaceholder().append("Q"));
843 i.setApplicationParameters("2404C0C1C2C3"_block); // updates ParametersSha256DigestComponent
844 BOOST_CHECK_EQUAL(i.getName(),
845 "/P/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a/Q");
846 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
847
848 i.setName("/A/B/C"); // auto-appends ParametersSha256DigestComponent
849 BOOST_CHECK_EQUAL(i.getName(),
850 "/A/B/C/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a");
851 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true);
852 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
853}
Junxiao Shi899277a2017-07-07 22:12:12 +0000854
855BOOST_AUTO_TEST_CASE(Equality)
856{
857 Interest a;
858 Interest b;
859
860 // if nonce is not set, it would be set to a random value
861 a.setNonce(1);
862 b.setNonce(1);
863
864 BOOST_CHECK_EQUAL(a == b, true);
865 BOOST_CHECK_EQUAL(a != b, false);
866
867 // compare Name
868 a.setName("/A");
869 BOOST_CHECK_EQUAL(a == b, false);
870 BOOST_CHECK_EQUAL(a != b, true);
871
872 b.setName("/B");
873 BOOST_CHECK_EQUAL(a == b, false);
874 BOOST_CHECK_EQUAL(a != b, true);
875
876 b.setName("/A");
877 BOOST_CHECK_EQUAL(a == b, true);
878 BOOST_CHECK_EQUAL(a != b, false);
879
880 // compare Selectors
881 a.setChildSelector(1);
882 BOOST_CHECK_EQUAL(a == b, false);
883 BOOST_CHECK_EQUAL(a != b, true);
884
885 b.setChildSelector(1);
886 BOOST_CHECK_EQUAL(a == b, true);
887 BOOST_CHECK_EQUAL(a != b, false);
888
889 // compare Nonce
890 a.setNonce(100);
891 BOOST_CHECK_EQUAL(a == b, false);
892 BOOST_CHECK_EQUAL(a != b, true);
893
894 b.setNonce(100);
895 BOOST_CHECK_EQUAL(a == b, true);
896 BOOST_CHECK_EQUAL(a != b, false);
897
898 // compare InterestLifetime
Davide Pesavento0f830802018-01-16 23:58:58 -0500899 a.setInterestLifetime(10_s);
Junxiao Shi899277a2017-07-07 22:12:12 +0000900 BOOST_CHECK_EQUAL(a == b, false);
901 BOOST_CHECK_EQUAL(a != b, true);
902
Davide Pesavento0f830802018-01-16 23:58:58 -0500903 b.setInterestLifetime(10_s);
Junxiao Shi899277a2017-07-07 22:12:12 +0000904 BOOST_CHECK_EQUAL(a == b, true);
905 BOOST_CHECK_EQUAL(a != b, false);
906
Junxiao Shid701e5b2017-07-26 01:30:41 +0000907 // compare ForwardingHint
908 a.setForwardingHint({{1, "/H"}});
Junxiao Shi899277a2017-07-07 22:12:12 +0000909 BOOST_CHECK_EQUAL(a == b, false);
910 BOOST_CHECK_EQUAL(a != b, true);
911
Junxiao Shid701e5b2017-07-26 01:30:41 +0000912 b.setForwardingHint({{1, "/H"}});
Junxiao Shi899277a2017-07-07 22:12:12 +0000913 BOOST_CHECK_EQUAL(a == b, true);
914 BOOST_CHECK_EQUAL(a != b, false);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700915
Davide Pesavento9c19a392019-04-06 15:07:54 -0400916 // compare ApplicationParameters
917 a.setApplicationParameters("2404C0C1C2C3"_block);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700918 BOOST_CHECK_EQUAL(a == b, false);
919 BOOST_CHECK_EQUAL(a != b, true);
920
Davide Pesavento9c19a392019-04-06 15:07:54 -0400921 b.setApplicationParameters("2404C0C1C2C3"_block);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700922 BOOST_CHECK_EQUAL(a == b, true);
923 BOOST_CHECK_EQUAL(a != b, false);
Junxiao Shi899277a2017-07-07 22:12:12 +0000924}
925
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100926BOOST_AUTO_TEST_SUITE_END() // TestInterest
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800927
Alexander Afanasyev90164962014-03-06 08:29:59 +0000928} // namespace tests
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800929} // namespace ndn