blob: e7354e9acff02656a5ca635648149b196264dfdb [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(), "/");
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -040058 BOOST_CHECK_EQUAL(i.hasSelectors(), false);
Junxiao Shi6efa3b72018-04-14 15:54:08 +000059 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
60 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -040061 BOOST_CHECK_EQUAL(i.getForwardingHint().empty(), true);
62 BOOST_CHECK_EQUAL(i.hasNonce(), false);
Junxiao Shi899277a2017-07-07 22:12:12 +000063 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -040064 BOOST_CHECK(i.getHopLimit() == nullopt);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -040065 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), false);
66 BOOST_CHECK_EQUAL(i.getApplicationParameters().isValid(), false);
67 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
Davide Pesaventofccb2dc2019-02-09 01:02:35 -050068}
69
70BOOST_AUTO_TEST_CASE(DecodeNotInterest)
71{
72 BOOST_CHECK_THROW(Interest("4202CAFE"_block), tlv::Error);
Junxiao Shi899277a2017-07-07 22:12:12 +000073}
74
Davide Pesaventoadc9aa22019-06-30 19:00:20 -040075BOOST_AUTO_TEST_SUITE(EncodeDecode02)
76
77BOOST_AUTO_TEST_CASE(Basic)
Junxiao Shi899277a2017-07-07 22:12:12 +000078{
79 const uint8_t WIRE[] = {
80 0x05, 0x1c, // Interest
81 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 0x0a, 0x04, // Nonce
86 0x01, 0x00, 0x00, 0x00
87 };
88
89 Interest i1("/local/ndn/prefix");
Junxiao Shib55e5d32018-07-18 13:32:00 -060090 i1.setCanBePrefix(true);
Junxiao Shi899277a2017-07-07 22:12:12 +000091 i1.setNonce(1);
92 Block wire1 = i1.wireEncode();
93 BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
94
95 Interest i2(wire1);
96 BOOST_CHECK_EQUAL(i2.getName(), "/local/ndn/prefix");
97 BOOST_CHECK(i2.getSelectors().empty());
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -040098 BOOST_CHECK_EQUAL(i2.getForwardingHint().empty(), true);
Junxiao Shi899277a2017-07-07 22:12:12 +000099 BOOST_CHECK_EQUAL(i2.getNonce(), 1);
100 BOOST_CHECK_EQUAL(i2.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400101 BOOST_CHECK(i2.getHopLimit() == nullopt);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400102 BOOST_CHECK_EQUAL(i2.hasApplicationParameters(), false);
103 BOOST_CHECK_EQUAL(i2.isParametersDigestValid(), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000104
105 BOOST_CHECK_EQUAL(i1, i2);
106}
107
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400108BOOST_AUTO_TEST_CASE(Full)
Junxiao Shi899277a2017-07-07 22:12:12 +0000109{
110 const uint8_t WIRE[] = {
Junxiao Shi9c154cb2017-07-07 22:14:54 +0000111 0x05, 0x31, // Interest
Junxiao Shi899277a2017-07-07 22:12:12 +0000112 0x07, 0x14, // Name
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000113 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent
114 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent
115 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent
Junxiao Shi899277a2017-07-07 22:12:12 +0000116 0x09, 0x03, // Selectors
117 0x0d, 0x01, 0x01, // MinSuffixComponents
118 0x0a, 0x04, // Nonce
119 0x01, 0x00, 0x00, 0x00,
120 0x0c, 0x02, // InterestLifetime
Junxiao Shi9c154cb2017-07-07 22:14:54 +0000121 0x03, 0xe8,
122 0x1e, 0x0a, // ForwardingHint
123 0x1f, 0x08, // Delegation
124 0x1e, 0x01, 0x01, // Preference=1
125 0x07, 0x03, 0x08, 0x01, 0x41 // Name=/A
Junxiao Shi899277a2017-07-07 22:12:12 +0000126 };
Junxiao Shi899277a2017-07-07 22:12:12 +0000127
128 Interest i1;
129 i1.setName("/local/ndn/prefix");
Junxiao Shib55e5d32018-07-18 13:32:00 -0600130 i1.setCanBePrefix(true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000131 i1.setMinSuffixComponents(1);
132 i1.setNonce(1);
Davide Pesavento0f830802018-01-16 23:58:58 -0500133 i1.setInterestLifetime(1000_ms);
Junxiao Shi9c154cb2017-07-07 22:14:54 +0000134 i1.setForwardingHint({{1, "/A"}});
Junxiao Shi899277a2017-07-07 22:12:12 +0000135 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.getMinSuffixComponents(), 1);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400141 BOOST_CHECK_EQUAL(i2.getForwardingHint(), DelegationList({{1, "/A"}}));
Junxiao Shi899277a2017-07-07 22:12:12 +0000142 BOOST_CHECK_EQUAL(i2.getNonce(), 1);
Davide Pesavento0f830802018-01-16 23:58:58 -0500143 BOOST_CHECK_EQUAL(i2.getInterestLifetime(), 1000_ms);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400144 BOOST_CHECK(i2.getHopLimit() == nullopt);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400145 BOOST_CHECK_EQUAL(i2.hasApplicationParameters(), false);
146 BOOST_CHECK_EQUAL(i2.isParametersDigestValid(), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000147
148 BOOST_CHECK_EQUAL(i1, i2);
149}
150
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400151BOOST_AUTO_TEST_CASE(ParametersSha256DigestComponent)
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700152{
153 const uint8_t WIRE[] = {
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400154 0x05, 0x60, // Interest
155 0x07, 0x58, // Name
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700156 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent
157 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent
158 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400159 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 0x02, 0x20, // ParametersSha256DigestComponent
164 0xff, 0x91, 0x00, 0xe0, 0x4e, 0xaa, 0xdc, 0xf3, 0x06, 0x74, 0xd9, 0x80,
165 0x26, 0xa0, 0x51, 0xba, 0x25, 0xf5, 0x6b, 0x69, 0xbf, 0xa0, 0x26, 0xdc,
166 0xcc, 0xd7, 0x2c, 0x6e, 0xa0, 0xf7, 0x31, 0x5a,
167 0x0a, 0x04, // Nonce
168 0x01, 0x00, 0x00, 0x00,
169 };
170
171 Interest i1("/I");
172 BOOST_CHECK_THROW(i1.wireDecode(Block(WIRE, sizeof(WIRE))), tlv::Error);
173
174 // i1 is still in a valid state
175 BOOST_CHECK_EQUAL(i1.getName(), "/I");
176 BOOST_CHECK_EQUAL(i1.isParametersDigestValid(), true);
177
178 Interest i2("/I/params-sha256=f16db273f40436a852063f864d5072b01ead53151f5a688ea1560492bebedd05");
179 i2.setCanBePrefix(true);
180 i2.setNonce(2);
181 BOOST_CHECK_EQUAL(i2.isParametersDigestValid(), false);
182 // encoding in v0.2 format does not validate the ParametersSha256DigestComponent
183 Block wire2 = i2.wireEncode();
184 BOOST_CHECK_EQUAL(wire2, "052D 0725(080149 "
185 "0220F16DB273F40436A852063F864D5072B01EAD53151F5A688EA1560492BEBEDD05) "
186 "0A0402000000"_block);
187
188 // decoding from v0.2 format does not validate the ParametersSha256DigestComponent
189 Interest i3(wire2);
190 BOOST_CHECK_EQUAL(i3.getName(), i2.getName());
191 BOOST_CHECK_EQUAL(i3.isParametersDigestValid(), false);
192}
193
194BOOST_AUTO_TEST_SUITE_END() // EncodeDecode02
195
196BOOST_AUTO_TEST_SUITE(Encode03)
197
198// Enable after #4567
199//BOOST_AUTO_TEST_CASE(Basic)
200//{
201// const uint8_t WIRE[] = {
202// 0x05, 0x1c, // Interest
203// 0x07, 0x14, // Name
204// 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent
205// 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent
206// 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent
207// 0x0a, 0x04, // Nonce
208// 0x01, 0x00, 0x00, 0x00,
209// };
210
211// Interest i1;
212// i1.setName("/local/ndn/prefix");
213// i1.setCanBePrefix(false);
214// i1.setNonce(1);
215// BOOST_CHECK_EQUAL(i1.isParametersDigestValid(), true);
216
217// Block wire1 = i1.wireEncode();
218// BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
219
220// Interest i2(wire1);
221// BOOST_CHECK_EQUAL(i2.getName(), "/local/ndn/prefix");
222// BOOST_CHECK_EQUAL(i2.getCanBePrefix(), false);
223// BOOST_CHECK_EQUAL(i2.getMustBeFresh(), false);
224// BOOST_CHECK_EQUAL(i2.getForwardingHint().empty(), true);
225// BOOST_CHECK_EQUAL(i2.getNonce(), 1);
226// BOOST_CHECK_EQUAL(i2.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400227// BOOST_CHECK(i2.getHopLimit() == nullopt);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400228// BOOST_CHECK_EQUAL(i2.hasApplicationParameters(), false);
229// BOOST_CHECK_EQUAL(i2.getApplicationParameters().isValid(), false);
230// BOOST_CHECK_EQUAL(i2.getPublisherPublicKeyLocator().empty(), true);
231//}
232
233BOOST_AUTO_TEST_CASE(WithParameters)
234{
235 const uint8_t WIRE[] = {
236 0x05, 0x44, // Interest
237 0x07, 0x36, // Name
238 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent
239 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent
240 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent
241 0x02, 0x20, // ParametersSha256DigestComponent
242 0xff, 0x91, 0x00, 0xe0, 0x4e, 0xaa, 0xdc, 0xf3, 0x06, 0x74, 0xd9, 0x80,
243 0x26, 0xa0, 0x51, 0xba, 0x25, 0xf5, 0x6b, 0x69, 0xbf, 0xa0, 0x26, 0xdc,
244 0xcc, 0xd7, 0x2c, 0x6e, 0xa0, 0xf7, 0x31, 0x5a,
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700245 0x0a, 0x04, // Nonce
246 0x01, 0x00, 0x00, 0x00,
Davide Pesavento9c19a392019-04-06 15:07:54 -0400247 0x24, 0x04, // ApplicationParameters
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400248 0xc0, 0xc1, 0xc2, 0xc3
249 };
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700250
251 Interest i1;
252 i1.setName("/local/ndn/prefix");
253 i1.setCanBePrefix(false);
254 i1.setNonce(1);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400255 i1.setApplicationParameters("2404C0C1C2C3"_block);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400256 BOOST_CHECK_EQUAL(i1.isParametersDigestValid(), true);
257
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700258 Block wire1 = i1.wireEncode();
259 BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
260
261 Interest i2(wire1);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400262 BOOST_CHECK_EQUAL(i2.getName(),
263 "/local/ndn/prefix/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a");
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700264 BOOST_CHECK_EQUAL(i2.getCanBePrefix(), false);
265 BOOST_CHECK_EQUAL(i2.getMustBeFresh(), false);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400266 BOOST_CHECK_EQUAL(i2.getForwardingHint().empty(), true);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700267 BOOST_CHECK_EQUAL(i2.getNonce(), 1);
268 BOOST_CHECK_EQUAL(i2.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400269 BOOST_CHECK(i2.getHopLimit() == nullopt);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400270 BOOST_CHECK_EQUAL(i2.hasApplicationParameters(), true);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400271 BOOST_CHECK_EQUAL(i2.getApplicationParameters(), "2404C0C1C2C3"_block);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400272 BOOST_CHECK_EQUAL(i2.getPublisherPublicKeyLocator().empty(), true);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700273}
274
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400275BOOST_AUTO_TEST_CASE(Full)
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700276{
277 const uint8_t WIRE[] = {
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400278 0x05, 0x5c, // Interest
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400279 0x07, 0x36, // Name
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700280 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent
281 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent
282 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400283 0x02, 0x20, // ParametersSha256DigestComponent
284 0xff, 0x91, 0x00, 0xe0, 0x4e, 0xaa, 0xdc, 0xf3, 0x06, 0x74, 0xd9, 0x80,
285 0x26, 0xa0, 0x51, 0xba, 0x25, 0xf5, 0x6b, 0x69, 0xbf, 0xa0, 0x26, 0xdc,
286 0xcc, 0xd7, 0x2c, 0x6e, 0xa0, 0xf7, 0x31, 0x5a,
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700287 0x21, 0x00, // CanBePrefix
288 0x12, 0x00, // MustBeFresh
289 0x1e, 0x0b, // ForwardingHint
290 0x1f, 0x09, // Delegation List
291 0x1e, 0x02,
292 0x3e, 0x15,
293 0x07, 0x03,
294 0x08, 0x01, 0x48,
295 0x0a, 0x04, // Nonce
296 0x4a, 0xcb, 0x1e, 0x4c,
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400297 0x0c, 0x02, // InterestLifetime
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700298 0x76, 0xa1,
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400299 0x22, 0x01, // HopLimit
300 0xdc,
Davide Pesavento9c19a392019-04-06 15:07:54 -0400301 0x24, 0x04, // ApplicationParameters
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400302 0xc0, 0xc1, 0xc2, 0xc3
303 };
304
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700305 Interest i1;
306 i1.setName("/local/ndn/prefix");
307 i1.setMustBeFresh(true);
308 i1.setCanBePrefix(true);
309 i1.setForwardingHint(DelegationList({{15893, "/H"}}));
310 i1.setNonce(0x4c1ecb4a);
311 i1.setInterestLifetime(30369_ms);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400312 i1.setHopLimit(220);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400313 i1.setApplicationParameters("2404C0C1C2C3"_block);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700314 i1.setMinSuffixComponents(1); // v0.2-only elements will not be encoded
315 i1.setExclude(Exclude().excludeAfter(name::Component("J"))); // v0.2-only elements will not be encoded
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400316 BOOST_CHECK_EQUAL(i1.isParametersDigestValid(), true);
317
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700318 Block wire1 = i1.wireEncode();
319 BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
320
321 Interest i2(wire1);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400322 BOOST_CHECK_EQUAL(i2.getName(),
323 "/local/ndn/prefix/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a");
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700324 BOOST_CHECK_EQUAL(i2.getCanBePrefix(), true);
325 BOOST_CHECK_EQUAL(i2.getMustBeFresh(), true);
326 BOOST_CHECK_EQUAL(i2.getForwardingHint(), DelegationList({{15893, "/H"}}));
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400327 BOOST_CHECK_EQUAL(i2.hasNonce(), true);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700328 BOOST_CHECK_EQUAL(i2.getNonce(), 0x4c1ecb4a);
329 BOOST_CHECK_EQUAL(i2.getInterestLifetime(), 30369_ms);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400330 BOOST_CHECK_EQUAL(*i2.getHopLimit(), 220);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400331 BOOST_CHECK_EQUAL(i2.getApplicationParameters(), "2404C0C1C2C3"_block);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700332 BOOST_CHECK_EQUAL(i2.getMinSuffixComponents(), -1); // Default because minSuffixComponents was not encoded
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400333 BOOST_CHECK_EQUAL(i2.getExclude().empty(), true); // Exclude was not encoded
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700334}
335
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400336// Enable after #4567
337//BOOST_AUTO_TEST_CASE(MissingApplicationParameters)
338//{
339// Interest i;
340// i.setName(Name("/A").appendParametersSha256DigestPlaceholder());
341// i.setCanBePrefix(false);
342// BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
343// BOOST_CHECK_THROW(i.wireEncode(), tlv::Error);
344//}
345
346BOOST_AUTO_TEST_CASE(MissingParametersSha256DigestComponent)
347{
348 // there's no way to create an Interest that fails this check via programmatic construction,
349 // so we have to decode an invalid Interest and force reencoding
350
351 DisableAutoCheckParametersDigest disabler;
352 Interest i("050F 0703(080149) 0A04F000F000 2402CAFE"_block);
353 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
354 BOOST_CHECK_NO_THROW(i.wireEncode()); // this succeeds because it uses the cached wire encoding
355
356 i.setNonce(42); // trigger reencoding
357 BOOST_CHECK_THROW(i.wireEncode(), tlv::Error); // now the check fails while attempting to reencode
358}
359
360BOOST_AUTO_TEST_SUITE_END() // Encode03
361
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000362class Decode03Fixture
Junxiao Shi899277a2017-07-07 22:12:12 +0000363{
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000364protected:
365 Decode03Fixture()
366 {
367 // initialize all elements to non-empty, to verify wireDecode clears them
368 i.setName("/A");
369 i.setForwardingHint({{10309, "/F"}});
370 i.setNonce(0x03d645a8);
371 i.setInterestLifetime(18554_ms);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400372 i.setHopLimit(64);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000373 i.setPublisherPublicKeyLocator(Name("/K"));
Davide Pesavento9c19a392019-04-06 15:07:54 -0400374 i.setApplicationParameters("2404A0A1A2A3"_block);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000375 }
Junxiao Shi899277a2017-07-07 22:12:12 +0000376
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000377protected:
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000378 Interest i;
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000379};
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000380
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000381BOOST_FIXTURE_TEST_SUITE(Decode03, Decode03Fixture)
382
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400383BOOST_AUTO_TEST_CASE(NameOnly)
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000384{
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400385 i.wireDecode("0505 0703(080149)"_block);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000386 BOOST_CHECK_EQUAL(i.getName(), "/I");
387 BOOST_CHECK_EQUAL(i.getCanBePrefix(), false);
388 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400389 BOOST_CHECK_EQUAL(i.getForwardingHint().empty(), true);
390 BOOST_CHECK_EQUAL(i.hasNonce(), true); // a random nonce is generated
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000391 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400392 BOOST_CHECK(i.getHopLimit() == nullopt);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400393 BOOST_CHECK_EQUAL(i.getPublisherPublicKeyLocator().empty(), true);
394 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), false);
395 BOOST_CHECK_EQUAL(i.getApplicationParameters().isValid(), false);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000396
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000397 BOOST_CHECK(!i.hasWire()); // nonce generation resets wire encoding
398
399 // modify then re-encode as v0.2 format
400 i.setNonce(0x54657c95);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400401 BOOST_CHECK_EQUAL(i.wireEncode(), "0510 0703(080149) 09030E0101 0A04957C6554"_block);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000402}
403
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400404BOOST_AUTO_TEST_CASE(NameCanBePrefix)
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000405{
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400406 i.wireDecode("0507 0703(080149) 2100"_block);
407 BOOST_CHECK_EQUAL(i.getName(), "/I");
408 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
409 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
410 BOOST_CHECK_EQUAL(i.getForwardingHint().empty(), true);
411 BOOST_CHECK_EQUAL(i.hasNonce(), true); // a random nonce is generated
412 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400413 BOOST_CHECK(i.getHopLimit() == nullopt);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400414 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), false);
415 BOOST_CHECK_EQUAL(i.getApplicationParameters().isValid(), false);
416}
417
418BOOST_AUTO_TEST_CASE(FullWithoutParameters)
419{
420 i.wireDecode("0531 0703(080149) "
421 "FC00 2100 FC00 1200 FC00 1E0B(1F09 1E023E15 0703080148) "
422 "FC00 0A044ACB1E4C FC00 0C0276A1 FC00 2201D6 FC00"_block);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000423 BOOST_CHECK_EQUAL(i.getName(), "/I");
424 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
425 BOOST_CHECK_EQUAL(i.getMustBeFresh(), true);
426 BOOST_CHECK_EQUAL(i.getForwardingHint(), DelegationList({{15893, "/H"}}));
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400427 BOOST_CHECK_EQUAL(i.hasNonce(), true);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000428 BOOST_CHECK_EQUAL(i.getNonce(), 0x4c1ecb4a);
429 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 30369_ms);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400430 BOOST_CHECK_EQUAL(*i.getHopLimit(), 214);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400431 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), false);
432 BOOST_CHECK_EQUAL(i.getApplicationParameters().isValid(), false);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000433
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000434 // encode without modification: retain original wire encoding
Junxiao Shi8b753a22018-10-24 01:51:40 +0000435 BOOST_CHECK_EQUAL(i.wireEncode().value_size(), 49);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000436
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400437 // modify then re-encode as v0.3 format: unrecognized elements are discarded
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000438 i.setName("/J");
Junxiao Shi72c0c642018-04-20 15:41:09 +0000439 BOOST_CHECK_EQUAL(i.wireEncode(),
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400440 "0523 0703(08014A) "
441 "2100 1200 1E0B(1F09 1E023E15 0703080148) "
442 "0A044ACB1E4C 0C0276A1 2201D6"_block);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400443}
444
445BOOST_AUTO_TEST_CASE(FullWithParameters)
446{
447 i.wireDecode("055B 0725(080149 0220F16DB273F40436A852063F864D5072B01EAD53151F5A688EA1560492BEBEDD05) "
448 "FC00 2100 FC00 1200 FC00 1E0B(1F09 1E023E15 0703080148) "
449 "FC00 0A044ACB1E4C FC00 0C0276A1 FC00 2201D6 FC00 2404C0C1C2C3 FC00"_block);
450 BOOST_CHECK_EQUAL(i.getName(),
451 "/I/params-sha256=f16db273f40436a852063f864d5072b01ead53151f5a688ea1560492bebedd05");
452 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
453 BOOST_CHECK_EQUAL(i.getMustBeFresh(), true);
454 BOOST_CHECK_EQUAL(i.getForwardingHint(), DelegationList({{15893, "/H"}}));
455 BOOST_CHECK_EQUAL(i.hasNonce(), true);
456 BOOST_CHECK_EQUAL(i.getNonce(), 0x4c1ecb4a);
457 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 30369_ms);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400458 BOOST_CHECK_EQUAL(*i.getHopLimit(), 214);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400459 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true);
460 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2404C0C1C2C3"_block);
461
462 // encode without modification: retain original wire encoding
463 BOOST_CHECK_EQUAL(i.wireEncode().value_size(), 91);
464
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400465 // modify then re-encode as v0.3 format: unrecognized elements
466 // after ApplicationParameters are preserved, the rest are discarded
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400467 i.setName("/J");
468 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
469 BOOST_CHECK_EQUAL(i.wireEncode(),
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400470 "054D 0725(08014A 0220F16DB273F40436A852063F864D5072B01EAD53151F5A688EA1560492BEBEDD05) "
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400471 "2100 1200 1E0B(1F09 1E023E15 0703080148) "
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400472 "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3 FC00"_block);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400473
474 // modify ApplicationParameters: unrecognized elements are preserved
475 i.setApplicationParameters("2402CAFE"_block);
476 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
477 BOOST_CHECK_EQUAL(i.wireEncode(),
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400478 "054B 0725(08014A 02205FDA67967EE302FC457E41B7D3D51BA6A9379574D193FD88F64954BF16C2927A) "
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400479 "2100 1200 1E0B(1F09 1E023E15 0703080148) "
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400480 "0A044ACB1E4C 0C0276A1 2201D6 2402CAFE FC00"_block);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000481}
482
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000483BOOST_AUTO_TEST_CASE(CriticalElementOutOfOrder)
484{
485 BOOST_CHECK_THROW(i.wireDecode(
486 "0529 2100 0703080149 1200 1E0B(1F09 1E023E15 0703080148) "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500487 "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3"_block),
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000488 tlv::Error);
489 BOOST_CHECK_THROW(i.wireDecode(
490 "0529 0703080149 1200 2100 1E0B(1F09 1E023E15 0703080148) "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500491 "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3"_block),
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000492 tlv::Error);
493 BOOST_CHECK_THROW(i.wireDecode(
494 "0529 0703080149 2100 1E0B(1F09 1E023E15 0703080148) 1200 "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500495 "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3"_block),
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000496 tlv::Error);
497 BOOST_CHECK_THROW(i.wireDecode(
498 "0529 0703080149 2100 1200 0A044ACB1E4C "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500499 "1E0B(1F09 1E023E15 0703080148) 0C0276A1 2201D6 2404C0C1C2C3"_block),
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000500 tlv::Error);
501 BOOST_CHECK_THROW(i.wireDecode(
502 "0529 0703080149 2100 1200 1E0B(1F09 1E023E15 0703080148) "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500503 "0C0276A1 0A044ACB1E4C 2201D6 2404C0C1C2C3"_block),
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000504 tlv::Error);
505 BOOST_CHECK_THROW(i.wireDecode(
506 "0529 0703080149 2100 1200 1E0B(1F09 1E023E15 0703080148) "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500507 "0A044ACB1E4C 2201D6 0C0276A1 2404C0C1C2C3"_block),
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000508 tlv::Error);
509}
510
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500511BOOST_AUTO_TEST_CASE(NonCriticalElementOutOfOrder)
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000512{
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400513 // duplicate HopLimit
514 i.wireDecode("0536 0725(080149 0220FF9100E04EAADCF30674D98026A051BA25F56B69BFA026DCCCD72C6EA0F7315A)"
515 "2201D6 2200 2404C0C1C2C3 22020101"_block);
516 BOOST_CHECK_EQUAL(i.getName(),
517 "/I/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a");
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400518 BOOST_CHECK_EQUAL(*i.getHopLimit(), 214);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400519 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400520 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2404C0C1C2C3"_block);
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500521
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400522 // duplicate ApplicationParameters
523 i.wireDecode("0541 0725(080149 0220FF9100E04EAADCF30674D98026A051BA25F56B69BFA026DCCCD72C6EA0F7315A)"
524 "2100 1200 0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3 2401EE"_block);
525 BOOST_CHECK_EQUAL(i.getName(),
526 "/I/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a");
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400527 BOOST_CHECK_EQUAL(*i.getHopLimit(), 214);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400528 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true);
529 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2404C0C1C2C3"_block);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000530}
531
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400532BOOST_AUTO_TEST_CASE(MissingName)
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000533{
534 BOOST_CHECK_THROW(i.wireDecode("0500"_block), tlv::Error);
535 BOOST_CHECK_THROW(i.wireDecode("0502 1200"_block), tlv::Error);
536}
537
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400538BOOST_AUTO_TEST_CASE(BadName)
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000539{
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400540 // empty
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000541 BOOST_CHECK_THROW(i.wireDecode("0502 0700"_block), tlv::Error);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400542
543 // more than one ParametersSha256DigestComponent
544 BOOST_CHECK_THROW(i.wireDecode("054C 074A(080149"
545 "02200000000000000000000000000000000000000000000000000000000000000000"
546 "080132"
547 "02200000000000000000000000000000000000000000000000000000000000000000)"_block),
548 tlv::Error);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000549}
550
551BOOST_AUTO_TEST_CASE(BadCanBePrefix)
552{
553 BOOST_CHECK_THROW(i.wireDecode("0508 0703080149 210102"_block), tlv::Error);
554}
555
556BOOST_AUTO_TEST_CASE(BadMustBeFresh)
557{
558 BOOST_CHECK_THROW(i.wireDecode("0508 0703080149 120102"_block), tlv::Error);
559}
560
561BOOST_AUTO_TEST_CASE(BadNonce)
562{
563 BOOST_CHECK_THROW(i.wireDecode("0507 0703080149 0A00"_block), tlv::Error);
564 BOOST_CHECK_THROW(i.wireDecode("050A 0703080149 0A0304C263"_block), tlv::Error);
565 BOOST_CHECK_THROW(i.wireDecode("050C 0703080149 0A05EFA420B262"_block), tlv::Error);
566}
567
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500568BOOST_AUTO_TEST_CASE(BadHopLimit)
569{
570 BOOST_CHECK_THROW(i.wireDecode("0507 0703080149 2200"_block), tlv::Error);
571 BOOST_CHECK_THROW(i.wireDecode("0509 0703080149 22021356"_block), tlv::Error);
572}
573
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400574BOOST_AUTO_TEST_CASE(BadParametersDigest)
575{
576 // ApplicationParameters without ParametersSha256DigestComponent
577 Block b1("0509 0703(080149) 2402CAFE"_block);
578 // ParametersSha256DigestComponent without ApplicationParameters
579 Block b2("0527 0725(080149 0220E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855)"_block);
580 // digest mismatch
581 Block b3("052B 0725(080149 02200000000000000000000000000000000000000000000000000000000000000000) "
582 "2402CAFE"_block);
583
584 BOOST_CHECK_THROW(i.wireDecode(b1), tlv::Error);
585 BOOST_CHECK_THROW(i.wireDecode(b2), tlv::Error);
586 BOOST_CHECK_THROW(i.wireDecode(b3), tlv::Error);
587
588 DisableAutoCheckParametersDigest disabler;
589 BOOST_CHECK_NO_THROW(i.wireDecode(b1));
590 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
591 BOOST_CHECK_NO_THROW(i.wireDecode(b2));
592 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
593 BOOST_CHECK_NO_THROW(i.wireDecode(b3));
594 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
595}
596
Junxiao Shi8b753a22018-10-24 01:51:40 +0000597BOOST_AUTO_TEST_CASE(UnrecognizedNonCriticalElementBeforeName)
598{
599 BOOST_CHECK_THROW(i.wireDecode("0507 FC00 0703080149"_block), tlv::Error);
600}
601
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000602BOOST_AUTO_TEST_CASE(UnrecognizedCriticalElement)
603{
604 BOOST_CHECK_THROW(i.wireDecode("0507 0703080149 FB00"_block), tlv::Error);
605}
606
607BOOST_AUTO_TEST_SUITE_END() // Decode03
608
Junxiao Shi899277a2017-07-07 22:12:12 +0000609BOOST_AUTO_TEST_CASE(MatchesData)
610{
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000611 auto interest = makeInterest("/A");
Junxiao Shi899277a2017-07-07 22:12:12 +0000612
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000613 auto data = makeData("/A");
614 BOOST_CHECK_EQUAL(interest->matchesData(*data), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000615
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000616 data->setName("/A/D");
617 BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // violates CanBePrefix
Junxiao Shi899277a2017-07-07 22:12:12 +0000618
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000619 interest->setCanBePrefix(true);
620 BOOST_CHECK_EQUAL(interest->matchesData(*data), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000621
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000622 interest->setMustBeFresh(true);
623 BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // violates MustBeFresh
Junxiao Shi899277a2017-07-07 22:12:12 +0000624
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000625 data->setFreshnessPeriod(1_s);
626 BOOST_CHECK_EQUAL(interest->matchesData(*data), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000627
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000628 data->setName("/H/I");
629 BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // Name does not match
Junxiao Shi899277a2017-07-07 22:12:12 +0000630
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000631 data->wireEncode();
632 interest = makeInterest(data->getFullName());
633 BOOST_CHECK_EQUAL(interest->matchesData(*data), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000634
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400635 setNameComponent(*interest, -1, name::Component::fromEscapedString(
636 "sha256digest=0000000000000000000000000000000000000000000000000000000000000000"));
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000637 BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // violates implicit digest
Junxiao Shi899277a2017-07-07 22:12:12 +0000638}
639
640BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(MatchesInterest, 1)
641BOOST_AUTO_TEST_CASE(MatchesInterest)
642{
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400643 Interest interest;
644 interest.setName("/A")
645 .setCanBePrefix(true)
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000646 .setMustBeFresh(true)
647 .setForwardingHint({{1, "/H"}})
648 .setNonce(2228)
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400649 .setInterestLifetime(5_s)
650 .setHopLimit(90);
Junxiao Shi899277a2017-07-07 22:12:12 +0000651
652 Interest other;
653 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false);
654
655 other.setName(interest.getName());
656 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false);
657
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000658 other.setCanBePrefix(interest.getCanBePrefix());
659 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false);
660
661 other.setMustBeFresh(interest.getMustBeFresh());
Junxiao Shi899277a2017-07-07 22:12:12 +0000662 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false); // will match until #3162 implemented
663
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000664 other.setForwardingHint(interest.getForwardingHint());
Junxiao Shi899277a2017-07-07 22:12:12 +0000665 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
666
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000667 other.setNonce(9336);
Junxiao Shi899277a2017-07-07 22:12:12 +0000668 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
669
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000670 other.setInterestLifetime(3_s);
Junxiao Shi899277a2017-07-07 22:12:12 +0000671 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400672
673 other.setHopLimit(31);
674 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000675}
676
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400677BOOST_AUTO_TEST_CASE(SetName)
678{
679 Interest i;
680 BOOST_CHECK_EQUAL(i.getName(), "/");
681 i.setName("/A/B");
682 BOOST_CHECK_EQUAL(i.getName(), "/A/B");
683 i.setName("/I/params-sha256=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
684 BOOST_CHECK_EQUAL(i.getName(),
685 "/I/params-sha256=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
686 BOOST_CHECK_THROW(i.setName("/I"
687 "/params-sha256=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
688 "/params-sha256=0000000000000000000000000000000000000000000000000000000000000000"),
689 std::invalid_argument);
690}
Junxiao Shi899277a2017-07-07 22:12:12 +0000691
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400692BOOST_AUTO_TEST_CASE(SetCanBePrefix)
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000693{
694 Interest i;
695 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
696 i.setCanBePrefix(false);
697 BOOST_CHECK_EQUAL(i.getCanBePrefix(), false);
698 BOOST_CHECK_EQUAL(i.getSelectors().getMaxSuffixComponents(), 1);
699 i.setCanBePrefix(true);
700 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
701 BOOST_CHECK_EQUAL(i.getSelectors().getMaxSuffixComponents(), -1);
702}
703
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400704BOOST_AUTO_TEST_CASE(SetMustBeFresh)
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000705{
706 Interest i;
707 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
708 i.setMustBeFresh(true);
709 BOOST_CHECK_EQUAL(i.getMustBeFresh(), true);
710 BOOST_CHECK_EQUAL(i.getSelectors().getMustBeFresh(), true);
711 i.setMustBeFresh(false);
712 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
713 BOOST_CHECK_EQUAL(i.getSelectors().getMustBeFresh(), false);
714}
715
716BOOST_AUTO_TEST_CASE(ModifyForwardingHint)
717{
718 Interest i;
Junxiao Shib55e5d32018-07-18 13:32:00 -0600719 i.setCanBePrefix(false);
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000720 i.setForwardingHint({{1, "/A"}});
721 i.wireEncode();
722 BOOST_CHECK(i.hasWire());
723
724 i.modifyForwardingHint([] (DelegationList& fh) { fh.insert(2, "/B"); });
725 BOOST_CHECK(!i.hasWire());
726 BOOST_CHECK_EQUAL(i.getForwardingHint(), DelegationList({{1, "/A"}, {2, "/B"}}));
727}
728
Junxiao Shi899277a2017-07-07 22:12:12 +0000729BOOST_AUTO_TEST_CASE(GetNonce)
730{
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000731 unique_ptr<Interest> i1, i2;
Junxiao Shi899277a2017-07-07 22:12:12 +0000732
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000733 // getNonce automatically assigns a random Nonce.
734 // It's possible to assign the same Nonce to two Interest, but it's unlikely to get 100 pairs of
735 // same Nonces in a row.
Junxiao Shi899277a2017-07-07 22:12:12 +0000736 int nIterations = 0;
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000737 uint32_t nonce1 = 0, nonce2 = 0;
Junxiao Shi899277a2017-07-07 22:12:12 +0000738 do {
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000739 i1 = make_unique<Interest>();
740 nonce1 = i1->getNonce();
741 i2 = make_unique<Interest>();
742 nonce2 = i2->getNonce();
Junxiao Shi899277a2017-07-07 22:12:12 +0000743 }
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000744 while (nonce1 == nonce2 && ++nIterations < 100);
745 BOOST_CHECK_NE(nonce1, nonce2);
746 BOOST_CHECK(i1->hasNonce());
747 BOOST_CHECK(i2->hasNonce());
Junxiao Shi899277a2017-07-07 22:12:12 +0000748
749 // Once a Nonce is assigned, it should not change.
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000750 BOOST_CHECK_EQUAL(i1->getNonce(), nonce1);
751}
752
753BOOST_AUTO_TEST_CASE(SetNonce)
754{
755 Interest i1("/A");
Junxiao Shib55e5d32018-07-18 13:32:00 -0600756 i1.setCanBePrefix(false);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000757 i1.setNonce(1);
758 i1.wireEncode();
759 BOOST_CHECK_EQUAL(i1.getNonce(), 1);
760
761 Interest i2(i1);
762 BOOST_CHECK_EQUAL(i2.getNonce(), 1);
763
764 i2.setNonce(2);
765 BOOST_CHECK_EQUAL(i2.getNonce(), 2);
766 BOOST_CHECK_EQUAL(i1.getNonce(), 1); // should not affect i1 Nonce (Bug #4168)
Junxiao Shi899277a2017-07-07 22:12:12 +0000767}
768
769BOOST_AUTO_TEST_CASE(RefreshNonce)
770{
771 Interest i;
772 BOOST_CHECK(!i.hasNonce());
773 i.refreshNonce();
774 BOOST_CHECK(!i.hasNonce());
775
776 i.setNonce(1);
777 BOOST_CHECK(i.hasNonce());
778 i.refreshNonce();
779 BOOST_CHECK(i.hasNonce());
780 BOOST_CHECK_NE(i.getNonce(), 1);
781}
782
783BOOST_AUTO_TEST_CASE(SetInterestLifetime)
784{
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500785 BOOST_CHECK_THROW(Interest("/A", -1_ms), std::invalid_argument);
Davide Pesavento0f830802018-01-16 23:58:58 -0500786 BOOST_CHECK_NO_THROW(Interest("/A", 0_ms));
Junxiao Shi899277a2017-07-07 22:12:12 +0000787
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400788 Interest i;
Junxiao Shi899277a2017-07-07 22:12:12 +0000789 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500790 BOOST_CHECK_THROW(i.setInterestLifetime(-1_ms), std::invalid_argument);
Junxiao Shi899277a2017-07-07 22:12:12 +0000791 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesavento0f830802018-01-16 23:58:58 -0500792 i.setInterestLifetime(0_ms);
793 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 0_ms);
794 i.setInterestLifetime(1_ms);
795 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 1_ms);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400796
797 i = Interest("/B", 15_s);
798 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 15_s);
799}
800
801BOOST_AUTO_TEST_CASE(SetHopLimit)
802{
803 Interest i;
804 BOOST_CHECK(i.getHopLimit() == nullopt);
805 i.setHopLimit(42);
806 BOOST_CHECK(i.getHopLimit() == 42);
807 i.setHopLimit(nullopt);
808 BOOST_CHECK(i.getHopLimit() == nullopt);
Junxiao Shi899277a2017-07-07 22:12:12 +0000809}
810
Davide Pesavento9c19a392019-04-06 15:07:54 -0400811BOOST_AUTO_TEST_CASE(SetApplicationParameters)
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700812{
813 const uint8_t PARAMETERS1[] = {0xc1};
814 const uint8_t PARAMETERS2[] = {0xc2};
815
816 Interest i;
Davide Pesavento9c19a392019-04-06 15:07:54 -0400817 BOOST_CHECK(!i.hasApplicationParameters());
818 i.setApplicationParameters("2400"_block);
819 BOOST_CHECK(i.hasApplicationParameters());
820 i.unsetApplicationParameters();
821 BOOST_CHECK(!i.hasApplicationParameters());
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700822
Davide Pesavento38912442019-04-06 22:03:39 -0400823 // Block overload
824 i.setApplicationParameters(Block{});
825 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2400"_block);
826 i.setApplicationParameters("2401C0"_block);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400827 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2401C0"_block);
Davide Pesavento38912442019-04-06 22:03:39 -0400828 i.setApplicationParameters("8001C1"_block);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400829 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "24038001C1"_block);
Davide Pesavento38912442019-04-06 22:03:39 -0400830
831 // raw buffer+size overload
832 i.setApplicationParameters(PARAMETERS1, sizeof(PARAMETERS1));
833 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2401C1"_block);
834 i.setApplicationParameters(nullptr, 0);
835 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2400"_block);
836 BOOST_CHECK_THROW(i.setApplicationParameters(nullptr, 42), std::invalid_argument);
837
838 // ConstBufferPtr overload
839 i.setApplicationParameters(make_shared<Buffer>(PARAMETERS2, sizeof(PARAMETERS2)));
840 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2401C2"_block);
841 i.setApplicationParameters(make_shared<Buffer>());
842 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2400"_block);
843 BOOST_CHECK_THROW(i.setApplicationParameters(nullptr), std::invalid_argument);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700844}
845
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400846BOOST_AUTO_TEST_CASE(ParametersSha256DigestComponent)
847{
848 Interest i("/I");
849 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
850
851 i.setApplicationParameters("2404C0C1C2C3"_block); // auto-appends ParametersSha256DigestComponent
852 BOOST_CHECK_EQUAL(i.getName(),
853 "/I/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a");
854 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
855
856 i.setApplicationParameters(nullptr, 0); // updates ParametersSha256DigestComponent
857 BOOST_CHECK_EQUAL(i.getName(),
858 "/I/params-sha256=33b67cb5385ceddad93d0ee960679041613bed34b8b4a5e6362fe7539ba2d3ce");
859 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true);
860 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
861
862 i.unsetApplicationParameters(); // removes ParametersSha256DigestComponent
863 BOOST_CHECK_EQUAL(i.getName(), "/I");
864 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
865
866 i.setName(Name("/P").appendParametersSha256DigestPlaceholder().append("Q"));
867 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), false);
868 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
869
870 i.unsetApplicationParameters(); // removes ParametersSha256DigestComponent
871 BOOST_CHECK_EQUAL(i.getName(), "/P/Q");
872 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
873
874 i.setName(Name("/P").appendParametersSha256DigestPlaceholder().append("Q"));
875 i.setApplicationParameters("2404C0C1C2C3"_block); // updates ParametersSha256DigestComponent
876 BOOST_CHECK_EQUAL(i.getName(),
877 "/P/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a/Q");
878 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
879
880 i.setName("/A/B/C"); // auto-appends ParametersSha256DigestComponent
881 BOOST_CHECK_EQUAL(i.getName(),
882 "/A/B/C/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a");
883 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true);
884 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
885}
Junxiao Shi899277a2017-07-07 22:12:12 +0000886
887BOOST_AUTO_TEST_CASE(Equality)
888{
889 Interest a;
890 Interest b;
891
892 // if nonce is not set, it would be set to a random value
893 a.setNonce(1);
894 b.setNonce(1);
895
896 BOOST_CHECK_EQUAL(a == b, true);
897 BOOST_CHECK_EQUAL(a != b, false);
898
899 // compare Name
900 a.setName("/A");
901 BOOST_CHECK_EQUAL(a == b, false);
902 BOOST_CHECK_EQUAL(a != b, true);
903
904 b.setName("/B");
905 BOOST_CHECK_EQUAL(a == b, false);
906 BOOST_CHECK_EQUAL(a != b, true);
907
908 b.setName("/A");
909 BOOST_CHECK_EQUAL(a == b, true);
910 BOOST_CHECK_EQUAL(a != b, false);
911
912 // compare Selectors
913 a.setChildSelector(1);
914 BOOST_CHECK_EQUAL(a == b, false);
915 BOOST_CHECK_EQUAL(a != b, true);
916
917 b.setChildSelector(1);
918 BOOST_CHECK_EQUAL(a == b, true);
919 BOOST_CHECK_EQUAL(a != b, false);
920
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400921 // compare ForwardingHint
922 a.setForwardingHint({{1, "/H"}});
923 BOOST_CHECK_EQUAL(a == b, false);
924 BOOST_CHECK_EQUAL(a != b, true);
925
926 b.setForwardingHint({{1, "/H"}});
927 BOOST_CHECK_EQUAL(a == b, true);
928 BOOST_CHECK_EQUAL(a != b, false);
929
Junxiao Shi899277a2017-07-07 22:12:12 +0000930 // compare Nonce
931 a.setNonce(100);
932 BOOST_CHECK_EQUAL(a == b, false);
933 BOOST_CHECK_EQUAL(a != b, true);
934
935 b.setNonce(100);
936 BOOST_CHECK_EQUAL(a == b, true);
937 BOOST_CHECK_EQUAL(a != b, false);
938
939 // compare InterestLifetime
Davide Pesavento0f830802018-01-16 23:58:58 -0500940 a.setInterestLifetime(10_s);
Junxiao Shi899277a2017-07-07 22:12:12 +0000941 BOOST_CHECK_EQUAL(a == b, false);
942 BOOST_CHECK_EQUAL(a != b, true);
943
Davide Pesavento0f830802018-01-16 23:58:58 -0500944 b.setInterestLifetime(10_s);
Junxiao Shi899277a2017-07-07 22:12:12 +0000945 BOOST_CHECK_EQUAL(a == b, true);
946 BOOST_CHECK_EQUAL(a != b, false);
947
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400948 // compare HopLimit
949 a.setHopLimit(255);
Junxiao Shi899277a2017-07-07 22:12:12 +0000950 BOOST_CHECK_EQUAL(a == b, false);
951 BOOST_CHECK_EQUAL(a != b, true);
952
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400953 b.setHopLimit(255);
Junxiao Shi899277a2017-07-07 22:12:12 +0000954 BOOST_CHECK_EQUAL(a == b, true);
955 BOOST_CHECK_EQUAL(a != b, false);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700956
Davide Pesavento9c19a392019-04-06 15:07:54 -0400957 // compare ApplicationParameters
958 a.setApplicationParameters("2404C0C1C2C3"_block);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700959 BOOST_CHECK_EQUAL(a == b, false);
960 BOOST_CHECK_EQUAL(a != b, true);
961
Davide Pesavento9c19a392019-04-06 15:07:54 -0400962 b.setApplicationParameters("2404C0C1C2C3"_block);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700963 BOOST_CHECK_EQUAL(a == b, true);
964 BOOST_CHECK_EQUAL(a != b, false);
Junxiao Shi899277a2017-07-07 22:12:12 +0000965}
966
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100967BOOST_AUTO_TEST_SUITE_END() // TestInterest
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800968
Alexander Afanasyev90164962014-03-06 08:29:59 +0000969} // namespace tests
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800970} // namespace ndn