blob: 75aba88ad62fecfc21d364838eb3f7117db666dd [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
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400106BOOST_AUTO_TEST_CASE(Full)
Junxiao Shi899277a2017-07-07 22:12:12 +0000107{
108 const uint8_t WIRE[] = {
Junxiao Shi9c154cb2017-07-07 22:14:54 +0000109 0x05, 0x31, // Interest
Junxiao Shi899277a2017-07-07 22:12:12 +0000110 0x07, 0x14, // Name
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000111 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent
112 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent
113 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent
Junxiao Shi899277a2017-07-07 22:12:12 +0000114 0x09, 0x03, // Selectors
115 0x0d, 0x01, 0x01, // MinSuffixComponents
116 0x0a, 0x04, // Nonce
117 0x01, 0x00, 0x00, 0x00,
118 0x0c, 0x02, // InterestLifetime
Junxiao Shi9c154cb2017-07-07 22:14:54 +0000119 0x03, 0xe8,
120 0x1e, 0x0a, // ForwardingHint
121 0x1f, 0x08, // Delegation
122 0x1e, 0x01, 0x01, // Preference=1
123 0x07, 0x03, 0x08, 0x01, 0x41 // Name=/A
Junxiao Shi899277a2017-07-07 22:12:12 +0000124 };
Junxiao Shi899277a2017-07-07 22:12:12 +0000125
126 Interest i1;
127 i1.setName("/local/ndn/prefix");
Junxiao Shib55e5d32018-07-18 13:32:00 -0600128 i1.setCanBePrefix(true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000129 i1.setMinSuffixComponents(1);
130 i1.setNonce(1);
Davide Pesavento0f830802018-01-16 23:58:58 -0500131 i1.setInterestLifetime(1000_ms);
Junxiao Shi9c154cb2017-07-07 22:14:54 +0000132 i1.setForwardingHint({{1, "/A"}});
Junxiao Shi899277a2017-07-07 22:12:12 +0000133 Block wire1 = i1.wireEncode();
134 BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
135
136 Interest i2(wire1);
137 BOOST_CHECK_EQUAL(i2.getName(), "/local/ndn/prefix");
138 BOOST_CHECK_EQUAL(i2.getMinSuffixComponents(), 1);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400139 BOOST_CHECK_EQUAL(i2.getForwardingHint(), DelegationList({{1, "/A"}}));
Junxiao Shi899277a2017-07-07 22:12:12 +0000140 BOOST_CHECK_EQUAL(i2.getNonce(), 1);
Davide Pesavento0f830802018-01-16 23:58:58 -0500141 BOOST_CHECK_EQUAL(i2.getInterestLifetime(), 1000_ms);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400142 BOOST_CHECK(i2.getHopLimit() == nullopt);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400143 BOOST_CHECK_EQUAL(i2.hasApplicationParameters(), false);
144 BOOST_CHECK_EQUAL(i2.isParametersDigestValid(), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000145}
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);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400223// BOOST_CHECK(i2.getHopLimit() == nullopt);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400224// BOOST_CHECK_EQUAL(i2.hasApplicationParameters(), false);
225// BOOST_CHECK_EQUAL(i2.getApplicationParameters().isValid(), false);
226// BOOST_CHECK_EQUAL(i2.getPublisherPublicKeyLocator().empty(), true);
227//}
228
229BOOST_AUTO_TEST_CASE(WithParameters)
230{
231 const uint8_t WIRE[] = {
232 0x05, 0x44, // Interest
233 0x07, 0x36, // Name
234 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent
235 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent
236 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent
237 0x02, 0x20, // ParametersSha256DigestComponent
238 0xff, 0x91, 0x00, 0xe0, 0x4e, 0xaa, 0xdc, 0xf3, 0x06, 0x74, 0xd9, 0x80,
239 0x26, 0xa0, 0x51, 0xba, 0x25, 0xf5, 0x6b, 0x69, 0xbf, 0xa0, 0x26, 0xdc,
240 0xcc, 0xd7, 0x2c, 0x6e, 0xa0, 0xf7, 0x31, 0x5a,
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700241 0x0a, 0x04, // Nonce
242 0x01, 0x00, 0x00, 0x00,
Davide Pesavento9c19a392019-04-06 15:07:54 -0400243 0x24, 0x04, // ApplicationParameters
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400244 0xc0, 0xc1, 0xc2, 0xc3
245 };
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700246
247 Interest i1;
248 i1.setName("/local/ndn/prefix");
249 i1.setCanBePrefix(false);
250 i1.setNonce(1);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400251 i1.setApplicationParameters("2404C0C1C2C3"_block);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400252 BOOST_CHECK_EQUAL(i1.isParametersDigestValid(), true);
253
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700254 Block wire1 = i1.wireEncode();
255 BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
256
257 Interest i2(wire1);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400258 BOOST_CHECK_EQUAL(i2.getName(),
259 "/local/ndn/prefix/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a");
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700260 BOOST_CHECK_EQUAL(i2.getCanBePrefix(), false);
261 BOOST_CHECK_EQUAL(i2.getMustBeFresh(), false);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400262 BOOST_CHECK_EQUAL(i2.getForwardingHint().empty(), true);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700263 BOOST_CHECK_EQUAL(i2.getNonce(), 1);
264 BOOST_CHECK_EQUAL(i2.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400265 BOOST_CHECK(i2.getHopLimit() == nullopt);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400266 BOOST_CHECK_EQUAL(i2.hasApplicationParameters(), true);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400267 BOOST_CHECK_EQUAL(i2.getApplicationParameters(), "2404C0C1C2C3"_block);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400268 BOOST_CHECK_EQUAL(i2.getPublisherPublicKeyLocator().empty(), true);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700269}
270
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400271BOOST_AUTO_TEST_CASE(Full)
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700272{
273 const uint8_t WIRE[] = {
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400274 0x05, 0x5c, // Interest
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400275 0x07, 0x36, // Name
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700276 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent
277 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent
278 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400279 0x02, 0x20, // ParametersSha256DigestComponent
280 0xff, 0x91, 0x00, 0xe0, 0x4e, 0xaa, 0xdc, 0xf3, 0x06, 0x74, 0xd9, 0x80,
281 0x26, 0xa0, 0x51, 0xba, 0x25, 0xf5, 0x6b, 0x69, 0xbf, 0xa0, 0x26, 0xdc,
282 0xcc, 0xd7, 0x2c, 0x6e, 0xa0, 0xf7, 0x31, 0x5a,
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700283 0x21, 0x00, // CanBePrefix
284 0x12, 0x00, // MustBeFresh
285 0x1e, 0x0b, // ForwardingHint
286 0x1f, 0x09, // Delegation List
287 0x1e, 0x02,
288 0x3e, 0x15,
289 0x07, 0x03,
290 0x08, 0x01, 0x48,
291 0x0a, 0x04, // Nonce
292 0x4a, 0xcb, 0x1e, 0x4c,
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400293 0x0c, 0x02, // InterestLifetime
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700294 0x76, 0xa1,
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400295 0x22, 0x01, // HopLimit
296 0xdc,
Davide Pesavento9c19a392019-04-06 15:07:54 -0400297 0x24, 0x04, // ApplicationParameters
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400298 0xc0, 0xc1, 0xc2, 0xc3
299 };
300
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700301 Interest i1;
302 i1.setName("/local/ndn/prefix");
303 i1.setMustBeFresh(true);
304 i1.setCanBePrefix(true);
305 i1.setForwardingHint(DelegationList({{15893, "/H"}}));
306 i1.setNonce(0x4c1ecb4a);
307 i1.setInterestLifetime(30369_ms);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400308 i1.setHopLimit(220);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400309 i1.setApplicationParameters("2404C0C1C2C3"_block);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700310 i1.setMinSuffixComponents(1); // v0.2-only elements will not be encoded
311 i1.setExclude(Exclude().excludeAfter(name::Component("J"))); // v0.2-only elements will not be encoded
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400312 BOOST_CHECK_EQUAL(i1.isParametersDigestValid(), true);
313
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700314 Block wire1 = i1.wireEncode();
315 BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
316
317 Interest i2(wire1);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400318 BOOST_CHECK_EQUAL(i2.getName(),
319 "/local/ndn/prefix/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a");
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700320 BOOST_CHECK_EQUAL(i2.getCanBePrefix(), true);
321 BOOST_CHECK_EQUAL(i2.getMustBeFresh(), true);
322 BOOST_CHECK_EQUAL(i2.getForwardingHint(), DelegationList({{15893, "/H"}}));
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400323 BOOST_CHECK_EQUAL(i2.hasNonce(), true);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700324 BOOST_CHECK_EQUAL(i2.getNonce(), 0x4c1ecb4a);
325 BOOST_CHECK_EQUAL(i2.getInterestLifetime(), 30369_ms);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400326 BOOST_CHECK_EQUAL(*i2.getHopLimit(), 220);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400327 BOOST_CHECK_EQUAL(i2.getApplicationParameters(), "2404C0C1C2C3"_block);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700328 BOOST_CHECK_EQUAL(i2.getMinSuffixComponents(), -1); // Default because minSuffixComponents was not encoded
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400329 BOOST_CHECK_EQUAL(i2.getExclude().empty(), true); // Exclude was not encoded
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700330}
331
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400332// Enable after #4567
333//BOOST_AUTO_TEST_CASE(MissingApplicationParameters)
334//{
335// Interest i;
336// i.setName(Name("/A").appendParametersSha256DigestPlaceholder());
337// i.setCanBePrefix(false);
338// BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
339// BOOST_CHECK_THROW(i.wireEncode(), tlv::Error);
340//}
341
342BOOST_AUTO_TEST_CASE(MissingParametersSha256DigestComponent)
343{
344 // there's no way to create an Interest that fails this check via programmatic construction,
345 // so we have to decode an invalid Interest and force reencoding
346
347 DisableAutoCheckParametersDigest disabler;
348 Interest i("050F 0703(080149) 0A04F000F000 2402CAFE"_block);
349 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
350 BOOST_CHECK_NO_THROW(i.wireEncode()); // this succeeds because it uses the cached wire encoding
351
352 i.setNonce(42); // trigger reencoding
353 BOOST_CHECK_THROW(i.wireEncode(), tlv::Error); // now the check fails while attempting to reencode
354}
355
356BOOST_AUTO_TEST_SUITE_END() // Encode03
357
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000358class Decode03Fixture
Junxiao Shi899277a2017-07-07 22:12:12 +0000359{
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000360protected:
361 Decode03Fixture()
362 {
363 // initialize all elements to non-empty, to verify wireDecode clears them
364 i.setName("/A");
365 i.setForwardingHint({{10309, "/F"}});
366 i.setNonce(0x03d645a8);
367 i.setInterestLifetime(18554_ms);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400368 i.setHopLimit(64);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000369 i.setPublisherPublicKeyLocator(Name("/K"));
Davide Pesavento9c19a392019-04-06 15:07:54 -0400370 i.setApplicationParameters("2404A0A1A2A3"_block);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000371 }
Junxiao Shi899277a2017-07-07 22:12:12 +0000372
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000373protected:
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000374 Interest i;
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000375};
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000376
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000377BOOST_FIXTURE_TEST_SUITE(Decode03, Decode03Fixture)
378
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400379BOOST_AUTO_TEST_CASE(NameOnly)
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000380{
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400381 i.wireDecode("0505 0703(080149)"_block);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000382 BOOST_CHECK_EQUAL(i.getName(), "/I");
383 BOOST_CHECK_EQUAL(i.getCanBePrefix(), false);
384 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400385 BOOST_CHECK_EQUAL(i.getForwardingHint().empty(), true);
386 BOOST_CHECK_EQUAL(i.hasNonce(), true); // a random nonce is generated
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000387 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400388 BOOST_CHECK(i.getHopLimit() == nullopt);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400389 BOOST_CHECK_EQUAL(i.getPublisherPublicKeyLocator().empty(), true);
390 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), false);
391 BOOST_CHECK_EQUAL(i.getApplicationParameters().isValid(), false);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000392
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000393 BOOST_CHECK(!i.hasWire()); // nonce generation resets wire encoding
394
395 // modify then re-encode as v0.2 format
396 i.setNonce(0x54657c95);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400397 BOOST_CHECK_EQUAL(i.wireEncode(), "0510 0703(080149) 09030E0101 0A04957C6554"_block);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000398}
399
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400400BOOST_AUTO_TEST_CASE(NameCanBePrefix)
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000401{
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400402 i.wireDecode("0507 0703(080149) 2100"_block);
403 BOOST_CHECK_EQUAL(i.getName(), "/I");
404 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
405 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
406 BOOST_CHECK_EQUAL(i.getForwardingHint().empty(), true);
407 BOOST_CHECK_EQUAL(i.hasNonce(), true); // a random nonce is generated
408 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400409 BOOST_CHECK(i.getHopLimit() == nullopt);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400410 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), false);
411 BOOST_CHECK_EQUAL(i.getApplicationParameters().isValid(), false);
412}
413
414BOOST_AUTO_TEST_CASE(FullWithoutParameters)
415{
416 i.wireDecode("0531 0703(080149) "
417 "FC00 2100 FC00 1200 FC00 1E0B(1F09 1E023E15 0703080148) "
418 "FC00 0A044ACB1E4C FC00 0C0276A1 FC00 2201D6 FC00"_block);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000419 BOOST_CHECK_EQUAL(i.getName(), "/I");
420 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
421 BOOST_CHECK_EQUAL(i.getMustBeFresh(), true);
422 BOOST_CHECK_EQUAL(i.getForwardingHint(), DelegationList({{15893, "/H"}}));
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400423 BOOST_CHECK_EQUAL(i.hasNonce(), true);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000424 BOOST_CHECK_EQUAL(i.getNonce(), 0x4c1ecb4a);
425 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 30369_ms);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400426 BOOST_CHECK_EQUAL(*i.getHopLimit(), 214);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400427 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), false);
428 BOOST_CHECK_EQUAL(i.getApplicationParameters().isValid(), false);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000429
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000430 // encode without modification: retain original wire encoding
Junxiao Shi8b753a22018-10-24 01:51:40 +0000431 BOOST_CHECK_EQUAL(i.wireEncode().value_size(), 49);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000432
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400433 // modify then re-encode as v0.3 format: unrecognized elements are discarded
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000434 i.setName("/J");
Junxiao Shi72c0c642018-04-20 15:41:09 +0000435 BOOST_CHECK_EQUAL(i.wireEncode(),
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400436 "0523 0703(08014A) "
437 "2100 1200 1E0B(1F09 1E023E15 0703080148) "
438 "0A044ACB1E4C 0C0276A1 2201D6"_block);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400439}
440
441BOOST_AUTO_TEST_CASE(FullWithParameters)
442{
443 i.wireDecode("055B 0725(080149 0220F16DB273F40436A852063F864D5072B01EAD53151F5A688EA1560492BEBEDD05) "
444 "FC00 2100 FC00 1200 FC00 1E0B(1F09 1E023E15 0703080148) "
445 "FC00 0A044ACB1E4C FC00 0C0276A1 FC00 2201D6 FC00 2404C0C1C2C3 FC00"_block);
446 BOOST_CHECK_EQUAL(i.getName(),
447 "/I/params-sha256=f16db273f40436a852063f864d5072b01ead53151f5a688ea1560492bebedd05");
448 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
449 BOOST_CHECK_EQUAL(i.getMustBeFresh(), true);
450 BOOST_CHECK_EQUAL(i.getForwardingHint(), DelegationList({{15893, "/H"}}));
451 BOOST_CHECK_EQUAL(i.hasNonce(), true);
452 BOOST_CHECK_EQUAL(i.getNonce(), 0x4c1ecb4a);
453 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 30369_ms);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400454 BOOST_CHECK_EQUAL(*i.getHopLimit(), 214);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400455 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true);
456 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2404C0C1C2C3"_block);
457
458 // encode without modification: retain original wire encoding
459 BOOST_CHECK_EQUAL(i.wireEncode().value_size(), 91);
460
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400461 // modify then re-encode as v0.3 format: unrecognized elements
462 // after ApplicationParameters are preserved, the rest are discarded
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400463 i.setName("/J");
464 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
465 BOOST_CHECK_EQUAL(i.wireEncode(),
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400466 "054D 0725(08014A 0220F16DB273F40436A852063F864D5072B01EAD53151F5A688EA1560492BEBEDD05) "
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400467 "2100 1200 1E0B(1F09 1E023E15 0703080148) "
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400468 "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3 FC00"_block);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400469
470 // modify ApplicationParameters: unrecognized elements are preserved
471 i.setApplicationParameters("2402CAFE"_block);
472 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
473 BOOST_CHECK_EQUAL(i.wireEncode(),
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400474 "054B 0725(08014A 02205FDA67967EE302FC457E41B7D3D51BA6A9379574D193FD88F64954BF16C2927A) "
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400475 "2100 1200 1E0B(1F09 1E023E15 0703080148) "
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400476 "0A044ACB1E4C 0C0276A1 2201D6 2402CAFE FC00"_block);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000477}
478
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000479BOOST_AUTO_TEST_CASE(CriticalElementOutOfOrder)
480{
481 BOOST_CHECK_THROW(i.wireDecode(
482 "0529 2100 0703080149 1200 1E0B(1F09 1E023E15 0703080148) "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500483 "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3"_block),
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000484 tlv::Error);
485 BOOST_CHECK_THROW(i.wireDecode(
486 "0529 0703080149 1200 2100 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 2100 1E0B(1F09 1E023E15 0703080148) 1200 "
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 1200 0A044ACB1E4C "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500495 "1E0B(1F09 1E023E15 0703080148) 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 1E0B(1F09 1E023E15 0703080148) "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500499 "0C0276A1 0A044ACB1E4C 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 "0A044ACB1E4C 2201D6 0C0276A1 2404C0C1C2C3"_block),
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000504 tlv::Error);
505}
506
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500507BOOST_AUTO_TEST_CASE(NonCriticalElementOutOfOrder)
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000508{
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400509 // duplicate HopLimit
510 i.wireDecode("0536 0725(080149 0220FF9100E04EAADCF30674D98026A051BA25F56B69BFA026DCCCD72C6EA0F7315A)"
511 "2201D6 2200 2404C0C1C2C3 22020101"_block);
512 BOOST_CHECK_EQUAL(i.getName(),
513 "/I/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a");
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400514 BOOST_CHECK_EQUAL(*i.getHopLimit(), 214);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400515 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400516 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2404C0C1C2C3"_block);
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500517
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400518 // duplicate ApplicationParameters
519 i.wireDecode("0541 0725(080149 0220FF9100E04EAADCF30674D98026A051BA25F56B69BFA026DCCCD72C6EA0F7315A)"
520 "2100 1200 0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3 2401EE"_block);
521 BOOST_CHECK_EQUAL(i.getName(),
522 "/I/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a");
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400523 BOOST_CHECK_EQUAL(*i.getHopLimit(), 214);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400524 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true);
525 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2404C0C1C2C3"_block);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000526}
527
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400528BOOST_AUTO_TEST_CASE(MissingName)
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000529{
530 BOOST_CHECK_THROW(i.wireDecode("0500"_block), tlv::Error);
531 BOOST_CHECK_THROW(i.wireDecode("0502 1200"_block), tlv::Error);
532}
533
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400534BOOST_AUTO_TEST_CASE(BadName)
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000535{
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400536 // empty
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000537 BOOST_CHECK_THROW(i.wireDecode("0502 0700"_block), tlv::Error);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400538
539 // more than one ParametersSha256DigestComponent
540 BOOST_CHECK_THROW(i.wireDecode("054C 074A(080149"
541 "02200000000000000000000000000000000000000000000000000000000000000000"
542 "080132"
543 "02200000000000000000000000000000000000000000000000000000000000000000)"_block),
544 tlv::Error);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000545}
546
547BOOST_AUTO_TEST_CASE(BadCanBePrefix)
548{
549 BOOST_CHECK_THROW(i.wireDecode("0508 0703080149 210102"_block), tlv::Error);
550}
551
552BOOST_AUTO_TEST_CASE(BadMustBeFresh)
553{
554 BOOST_CHECK_THROW(i.wireDecode("0508 0703080149 120102"_block), tlv::Error);
555}
556
557BOOST_AUTO_TEST_CASE(BadNonce)
558{
559 BOOST_CHECK_THROW(i.wireDecode("0507 0703080149 0A00"_block), tlv::Error);
560 BOOST_CHECK_THROW(i.wireDecode("050A 0703080149 0A0304C263"_block), tlv::Error);
561 BOOST_CHECK_THROW(i.wireDecode("050C 0703080149 0A05EFA420B262"_block), tlv::Error);
562}
563
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500564BOOST_AUTO_TEST_CASE(BadHopLimit)
565{
566 BOOST_CHECK_THROW(i.wireDecode("0507 0703080149 2200"_block), tlv::Error);
567 BOOST_CHECK_THROW(i.wireDecode("0509 0703080149 22021356"_block), tlv::Error);
568}
569
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400570BOOST_AUTO_TEST_CASE(BadParametersDigest)
571{
572 // ApplicationParameters without ParametersSha256DigestComponent
573 Block b1("0509 0703(080149) 2402CAFE"_block);
574 // ParametersSha256DigestComponent without ApplicationParameters
575 Block b2("0527 0725(080149 0220E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855)"_block);
576 // digest mismatch
577 Block b3("052B 0725(080149 02200000000000000000000000000000000000000000000000000000000000000000) "
578 "2402CAFE"_block);
579
580 BOOST_CHECK_THROW(i.wireDecode(b1), tlv::Error);
581 BOOST_CHECK_THROW(i.wireDecode(b2), tlv::Error);
582 BOOST_CHECK_THROW(i.wireDecode(b3), tlv::Error);
583
584 DisableAutoCheckParametersDigest disabler;
585 BOOST_CHECK_NO_THROW(i.wireDecode(b1));
586 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
587 BOOST_CHECK_NO_THROW(i.wireDecode(b2));
588 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
589 BOOST_CHECK_NO_THROW(i.wireDecode(b3));
590 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
591}
592
Junxiao Shi8b753a22018-10-24 01:51:40 +0000593BOOST_AUTO_TEST_CASE(UnrecognizedNonCriticalElementBeforeName)
594{
595 BOOST_CHECK_THROW(i.wireDecode("0507 FC00 0703080149"_block), tlv::Error);
596}
597
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000598BOOST_AUTO_TEST_CASE(UnrecognizedCriticalElement)
599{
600 BOOST_CHECK_THROW(i.wireDecode("0507 0703080149 FB00"_block), tlv::Error);
601}
602
603BOOST_AUTO_TEST_SUITE_END() // Decode03
604
Junxiao Shi899277a2017-07-07 22:12:12 +0000605BOOST_AUTO_TEST_CASE(MatchesData)
606{
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000607 auto interest = makeInterest("/A");
Junxiao Shi899277a2017-07-07 22:12:12 +0000608
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000609 auto data = makeData("/A");
610 BOOST_CHECK_EQUAL(interest->matchesData(*data), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000611
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000612 data->setName("/A/D");
613 BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // violates CanBePrefix
Junxiao Shi899277a2017-07-07 22:12:12 +0000614
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000615 interest->setCanBePrefix(true);
616 BOOST_CHECK_EQUAL(interest->matchesData(*data), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000617
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000618 interest->setMustBeFresh(true);
619 BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // violates MustBeFresh
Junxiao Shi899277a2017-07-07 22:12:12 +0000620
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000621 data->setFreshnessPeriod(1_s);
622 BOOST_CHECK_EQUAL(interest->matchesData(*data), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000623
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000624 data->setName("/H/I");
625 BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // Name does not match
Junxiao Shi899277a2017-07-07 22:12:12 +0000626
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000627 data->wireEncode();
628 interest = makeInterest(data->getFullName());
629 BOOST_CHECK_EQUAL(interest->matchesData(*data), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000630
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400631 setNameComponent(*interest, -1, name::Component::fromEscapedString(
632 "sha256digest=0000000000000000000000000000000000000000000000000000000000000000"));
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000633 BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // violates implicit digest
Junxiao Shi899277a2017-07-07 22:12:12 +0000634}
635
636BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(MatchesInterest, 1)
637BOOST_AUTO_TEST_CASE(MatchesInterest)
638{
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400639 Interest interest;
640 interest.setName("/A")
641 .setCanBePrefix(true)
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000642 .setMustBeFresh(true)
643 .setForwardingHint({{1, "/H"}})
644 .setNonce(2228)
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400645 .setInterestLifetime(5_s)
646 .setHopLimit(90);
Junxiao Shi899277a2017-07-07 22:12:12 +0000647
648 Interest other;
649 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false);
650
651 other.setName(interest.getName());
652 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false);
653
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000654 other.setCanBePrefix(interest.getCanBePrefix());
655 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false);
656
657 other.setMustBeFresh(interest.getMustBeFresh());
Junxiao Shi899277a2017-07-07 22:12:12 +0000658 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false); // will match until #3162 implemented
659
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000660 other.setForwardingHint(interest.getForwardingHint());
Junxiao Shi899277a2017-07-07 22:12:12 +0000661 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
662
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000663 other.setNonce(9336);
Junxiao Shi899277a2017-07-07 22:12:12 +0000664 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
665
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000666 other.setInterestLifetime(3_s);
Junxiao Shi899277a2017-07-07 22:12:12 +0000667 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400668
669 other.setHopLimit(31);
670 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000671}
672
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400673BOOST_AUTO_TEST_CASE(SetName)
674{
675 Interest i;
676 BOOST_CHECK_EQUAL(i.getName(), "/");
677 i.setName("/A/B");
678 BOOST_CHECK_EQUAL(i.getName(), "/A/B");
679 i.setName("/I/params-sha256=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
680 BOOST_CHECK_EQUAL(i.getName(),
681 "/I/params-sha256=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
682 BOOST_CHECK_THROW(i.setName("/I"
683 "/params-sha256=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
684 "/params-sha256=0000000000000000000000000000000000000000000000000000000000000000"),
685 std::invalid_argument);
686}
Junxiao Shi899277a2017-07-07 22:12:12 +0000687
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400688BOOST_AUTO_TEST_CASE(SetCanBePrefix)
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000689{
690 Interest i;
691 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
692 i.setCanBePrefix(false);
693 BOOST_CHECK_EQUAL(i.getCanBePrefix(), false);
694 BOOST_CHECK_EQUAL(i.getSelectors().getMaxSuffixComponents(), 1);
695 i.setCanBePrefix(true);
696 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
697 BOOST_CHECK_EQUAL(i.getSelectors().getMaxSuffixComponents(), -1);
698}
699
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400700BOOST_AUTO_TEST_CASE(SetMustBeFresh)
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000701{
702 Interest i;
703 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
704 i.setMustBeFresh(true);
705 BOOST_CHECK_EQUAL(i.getMustBeFresh(), true);
706 BOOST_CHECK_EQUAL(i.getSelectors().getMustBeFresh(), true);
707 i.setMustBeFresh(false);
708 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
709 BOOST_CHECK_EQUAL(i.getSelectors().getMustBeFresh(), false);
710}
711
712BOOST_AUTO_TEST_CASE(ModifyForwardingHint)
713{
714 Interest i;
Junxiao Shib55e5d32018-07-18 13:32:00 -0600715 i.setCanBePrefix(false);
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000716 i.setForwardingHint({{1, "/A"}});
717 i.wireEncode();
718 BOOST_CHECK(i.hasWire());
719
720 i.modifyForwardingHint([] (DelegationList& fh) { fh.insert(2, "/B"); });
721 BOOST_CHECK(!i.hasWire());
722 BOOST_CHECK_EQUAL(i.getForwardingHint(), DelegationList({{1, "/A"}, {2, "/B"}}));
723}
724
Junxiao Shi899277a2017-07-07 22:12:12 +0000725BOOST_AUTO_TEST_CASE(GetNonce)
726{
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000727 unique_ptr<Interest> i1, i2;
Junxiao Shi899277a2017-07-07 22:12:12 +0000728
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000729 // getNonce automatically assigns a random Nonce.
730 // It's possible to assign the same Nonce to two Interest, but it's unlikely to get 100 pairs of
731 // same Nonces in a row.
Junxiao Shi899277a2017-07-07 22:12:12 +0000732 int nIterations = 0;
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000733 uint32_t nonce1 = 0, nonce2 = 0;
Junxiao Shi899277a2017-07-07 22:12:12 +0000734 do {
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000735 i1 = make_unique<Interest>();
736 nonce1 = i1->getNonce();
737 i2 = make_unique<Interest>();
738 nonce2 = i2->getNonce();
Junxiao Shi899277a2017-07-07 22:12:12 +0000739 }
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000740 while (nonce1 == nonce2 && ++nIterations < 100);
741 BOOST_CHECK_NE(nonce1, nonce2);
742 BOOST_CHECK(i1->hasNonce());
743 BOOST_CHECK(i2->hasNonce());
Junxiao Shi899277a2017-07-07 22:12:12 +0000744
745 // Once a Nonce is assigned, it should not change.
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000746 BOOST_CHECK_EQUAL(i1->getNonce(), nonce1);
747}
748
749BOOST_AUTO_TEST_CASE(SetNonce)
750{
751 Interest i1("/A");
Junxiao Shib55e5d32018-07-18 13:32:00 -0600752 i1.setCanBePrefix(false);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000753 i1.setNonce(1);
754 i1.wireEncode();
755 BOOST_CHECK_EQUAL(i1.getNonce(), 1);
756
757 Interest i2(i1);
758 BOOST_CHECK_EQUAL(i2.getNonce(), 1);
759
760 i2.setNonce(2);
761 BOOST_CHECK_EQUAL(i2.getNonce(), 2);
762 BOOST_CHECK_EQUAL(i1.getNonce(), 1); // should not affect i1 Nonce (Bug #4168)
Junxiao Shi899277a2017-07-07 22:12:12 +0000763}
764
765BOOST_AUTO_TEST_CASE(RefreshNonce)
766{
767 Interest i;
768 BOOST_CHECK(!i.hasNonce());
769 i.refreshNonce();
770 BOOST_CHECK(!i.hasNonce());
771
772 i.setNonce(1);
773 BOOST_CHECK(i.hasNonce());
774 i.refreshNonce();
775 BOOST_CHECK(i.hasNonce());
776 BOOST_CHECK_NE(i.getNonce(), 1);
777}
778
779BOOST_AUTO_TEST_CASE(SetInterestLifetime)
780{
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500781 BOOST_CHECK_THROW(Interest("/A", -1_ms), std::invalid_argument);
Davide Pesavento0f830802018-01-16 23:58:58 -0500782 BOOST_CHECK_NO_THROW(Interest("/A", 0_ms));
Junxiao Shi899277a2017-07-07 22:12:12 +0000783
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400784 Interest i;
Junxiao Shi899277a2017-07-07 22:12:12 +0000785 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500786 BOOST_CHECK_THROW(i.setInterestLifetime(-1_ms), std::invalid_argument);
Junxiao Shi899277a2017-07-07 22:12:12 +0000787 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesavento0f830802018-01-16 23:58:58 -0500788 i.setInterestLifetime(0_ms);
789 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 0_ms);
790 i.setInterestLifetime(1_ms);
791 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 1_ms);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400792
793 i = Interest("/B", 15_s);
794 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 15_s);
795}
796
797BOOST_AUTO_TEST_CASE(SetHopLimit)
798{
799 Interest i;
800 BOOST_CHECK(i.getHopLimit() == nullopt);
801 i.setHopLimit(42);
802 BOOST_CHECK(i.getHopLimit() == 42);
803 i.setHopLimit(nullopt);
804 BOOST_CHECK(i.getHopLimit() == nullopt);
Junxiao Shi899277a2017-07-07 22:12:12 +0000805}
806
Davide Pesavento9c19a392019-04-06 15:07:54 -0400807BOOST_AUTO_TEST_CASE(SetApplicationParameters)
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700808{
809 const uint8_t PARAMETERS1[] = {0xc1};
810 const uint8_t PARAMETERS2[] = {0xc2};
811
812 Interest i;
Davide Pesavento9c19a392019-04-06 15:07:54 -0400813 BOOST_CHECK(!i.hasApplicationParameters());
814 i.setApplicationParameters("2400"_block);
815 BOOST_CHECK(i.hasApplicationParameters());
816 i.unsetApplicationParameters();
817 BOOST_CHECK(!i.hasApplicationParameters());
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700818
Davide Pesavento38912442019-04-06 22:03:39 -0400819 // Block overload
820 i.setApplicationParameters(Block{});
821 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2400"_block);
822 i.setApplicationParameters("2401C0"_block);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400823 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2401C0"_block);
Davide Pesavento38912442019-04-06 22:03:39 -0400824 i.setApplicationParameters("8001C1"_block);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400825 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "24038001C1"_block);
Davide Pesavento38912442019-04-06 22:03:39 -0400826
827 // raw buffer+size overload
828 i.setApplicationParameters(PARAMETERS1, sizeof(PARAMETERS1));
829 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2401C1"_block);
830 i.setApplicationParameters(nullptr, 0);
831 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2400"_block);
832 BOOST_CHECK_THROW(i.setApplicationParameters(nullptr, 42), std::invalid_argument);
833
834 // ConstBufferPtr overload
835 i.setApplicationParameters(make_shared<Buffer>(PARAMETERS2, sizeof(PARAMETERS2)));
836 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2401C2"_block);
837 i.setApplicationParameters(make_shared<Buffer>());
838 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2400"_block);
839 BOOST_CHECK_THROW(i.setApplicationParameters(nullptr), std::invalid_argument);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700840}
841
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400842BOOST_AUTO_TEST_CASE(ParametersSha256DigestComponent)
843{
844 Interest i("/I");
845 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
846
847 i.setApplicationParameters("2404C0C1C2C3"_block); // auto-appends ParametersSha256DigestComponent
848 BOOST_CHECK_EQUAL(i.getName(),
849 "/I/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a");
850 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
851
852 i.setApplicationParameters(nullptr, 0); // updates ParametersSha256DigestComponent
853 BOOST_CHECK_EQUAL(i.getName(),
854 "/I/params-sha256=33b67cb5385ceddad93d0ee960679041613bed34b8b4a5e6362fe7539ba2d3ce");
855 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true);
856 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
857
858 i.unsetApplicationParameters(); // removes ParametersSha256DigestComponent
859 BOOST_CHECK_EQUAL(i.getName(), "/I");
860 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
861
862 i.setName(Name("/P").appendParametersSha256DigestPlaceholder().append("Q"));
863 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), false);
864 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
865
866 i.unsetApplicationParameters(); // removes ParametersSha256DigestComponent
867 BOOST_CHECK_EQUAL(i.getName(), "/P/Q");
868 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
869
870 i.setName(Name("/P").appendParametersSha256DigestPlaceholder().append("Q"));
871 i.setApplicationParameters("2404C0C1C2C3"_block); // updates ParametersSha256DigestComponent
872 BOOST_CHECK_EQUAL(i.getName(),
873 "/P/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a/Q");
874 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
875
876 i.setName("/A/B/C"); // auto-appends ParametersSha256DigestComponent
877 BOOST_CHECK_EQUAL(i.getName(),
878 "/A/B/C/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a");
879 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true);
880 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
881}
Junxiao Shi899277a2017-07-07 22:12:12 +0000882
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100883BOOST_AUTO_TEST_SUITE_END() // TestInterest
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800884
Alexander Afanasyev90164962014-03-06 08:29:59 +0000885} // namespace tests
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800886} // namespace ndn