blob: 903d9bbcf78a4845eb45950e7e65a55a9bd721c4 [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/*
Junxiao Shie4603e12022-01-05 19:12:25 +00003 * Copyright (c) 2013-2022 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"
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070024
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050025#include "tests/test-common.hpp"
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080026
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080027namespace ndn {
Alexander Afanasyev90164962014-03-06 08:29:59 +000028namespace tests {
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080029
Junxiao Shi899277a2017-07-07 22:12:12 +000030BOOST_AUTO_TEST_SUITE(TestInterest)
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080031
Davide Pesaventoadc9aa22019-06-30 19:00:20 -040032class DisableAutoCheckParametersDigest
33{
34public:
35 DisableAutoCheckParametersDigest()
36 : m_saved(Interest::getAutoCheckParametersDigest())
37 {
38 Interest::setAutoCheckParametersDigest(false);
39 }
40
41 ~DisableAutoCheckParametersDigest()
42 {
43 Interest::setAutoCheckParametersDigest(m_saved);
44 }
45
46private:
47 bool m_saved;
48};
Junxiao Shi899277a2017-07-07 22:12:12 +000049
50BOOST_AUTO_TEST_CASE(DefaultConstructor)
51{
52 Interest i;
Davide Pesaventoadc9aa22019-06-30 19:00:20 -040053 BOOST_CHECK_EQUAL(i.hasWire(), false);
Junxiao Shi899277a2017-07-07 22:12:12 +000054 BOOST_CHECK_EQUAL(i.getName(), "/");
Davide Pesavento478d3382021-03-17 12:46:08 -040055 BOOST_CHECK_EQUAL(i.getCanBePrefix(), false);
Junxiao Shi6efa3b72018-04-14 15:54:08 +000056 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -040057 BOOST_CHECK_EQUAL(i.getForwardingHint().empty(), true);
58 BOOST_CHECK_EQUAL(i.hasNonce(), false);
Junxiao Shi899277a2017-07-07 22:12:12 +000059 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -040060 BOOST_CHECK(i.getHopLimit() == nullopt);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -040061 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), false);
62 BOOST_CHECK_EQUAL(i.getApplicationParameters().isValid(), false);
63 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
Eric Newberry6e262f02020-05-29 23:11:25 -070064 BOOST_CHECK(i.getSignatureInfo() == nullopt);
65 BOOST_CHECK_EQUAL(i.getSignatureValue().isValid(), false);
66 BOOST_CHECK_EQUAL(i.isSigned(), false);
Davide Pesaventofccb2dc2019-02-09 01:02:35 -050067}
68
Davide Pesavento0e0b3892019-07-30 21:05:05 -040069BOOST_AUTO_TEST_SUITE(Encode)
Davide Pesaventoadc9aa22019-06-30 19:00:20 -040070
71BOOST_AUTO_TEST_CASE(Basic)
Junxiao Shi899277a2017-07-07 22:12:12 +000072{
73 const uint8_t WIRE[] = {
74 0x05, 0x1c, // Interest
75 0x07, 0x14, // Name
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +000076 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent
77 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent
78 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent
Junxiao Shi899277a2017-07-07 22:12:12 +000079 0x0a, 0x04, // Nonce
Davide Pesavento53533942020-03-04 23:10:06 -050080 0x01, 0x02, 0x03, 0x04,
Junxiao Shi899277a2017-07-07 22:12:12 +000081 };
82
Davide Pesavento0e0b3892019-07-30 21:05:05 -040083 Interest i1;
84 i1.setName("/local/ndn/prefix");
Davide Pesavento53533942020-03-04 23:10:06 -050085 i1.setNonce(0x01020304);
Davide Pesavento0e0b3892019-07-30 21:05:05 -040086 BOOST_CHECK_EQUAL(i1.isParametersDigestValid(), true);
87
Junxiao Shi899277a2017-07-07 22:12:12 +000088 Block wire1 = i1.wireEncode();
89 BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
90
91 Interest i2(wire1);
92 BOOST_CHECK_EQUAL(i2.getName(), "/local/ndn/prefix");
Davide Pesavento0e0b3892019-07-30 21:05:05 -040093 BOOST_CHECK_EQUAL(i2.getCanBePrefix(), false);
94 BOOST_CHECK_EQUAL(i2.getMustBeFresh(), false);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -040095 BOOST_CHECK_EQUAL(i2.getForwardingHint().empty(), true);
Davide Pesavento835f0272019-09-21 13:18:24 -040096 BOOST_CHECK_EQUAL(i2.hasNonce(), true);
Davide Pesavento53533942020-03-04 23:10:06 -050097 BOOST_CHECK_EQUAL(i2.getNonce(), 0x01020304);
Junxiao Shi899277a2017-07-07 22:12:12 +000098 BOOST_CHECK_EQUAL(i2.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -040099 BOOST_CHECK(i2.getHopLimit() == nullopt);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400100 BOOST_CHECK_EQUAL(i2.hasApplicationParameters(), false);
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400101 BOOST_CHECK_EQUAL(i2.getApplicationParameters().isValid(), false);
Eric Newberry6e262f02020-05-29 23:11:25 -0700102 BOOST_CHECK(i2.getSignatureInfo() == nullopt);
103 BOOST_CHECK_EQUAL(i2.getSignatureValue().isValid(), false);
104 BOOST_CHECK_EQUAL(i2.isSigned(), false);
Junxiao Shi899277a2017-07-07 22:12:12 +0000105}
106
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400107BOOST_AUTO_TEST_CASE(WithParameters)
108{
109 const uint8_t WIRE[] = {
110 0x05, 0x44, // Interest
111 0x07, 0x36, // Name
112 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent
113 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent
114 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent
115 0x02, 0x20, // ParametersSha256DigestComponent
116 0xff, 0x91, 0x00, 0xe0, 0x4e, 0xaa, 0xdc, 0xf3, 0x06, 0x74, 0xd9, 0x80,
117 0x26, 0xa0, 0x51, 0xba, 0x25, 0xf5, 0x6b, 0x69, 0xbf, 0xa0, 0x26, 0xdc,
118 0xcc, 0xd7, 0x2c, 0x6e, 0xa0, 0xf7, 0x31, 0x5a,
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700119 0x0a, 0x04, // Nonce
Davide Pesavento53533942020-03-04 23:10:06 -0500120 0x00, 0x00, 0x00, 0x01,
Davide Pesavento9c19a392019-04-06 15:07:54 -0400121 0x24, 0x04, // ApplicationParameters
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400122 0xc0, 0xc1, 0xc2, 0xc3
123 };
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700124
125 Interest i1;
126 i1.setName("/local/ndn/prefix");
Davide Pesavento53533942020-03-04 23:10:06 -0500127 i1.setNonce(0x1);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400128 i1.setApplicationParameters("2404C0C1C2C3"_block);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400129 BOOST_CHECK_EQUAL(i1.isParametersDigestValid(), true);
130
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700131 Block wire1 = i1.wireEncode();
132 BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
133
134 Interest i2(wire1);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400135 BOOST_CHECK_EQUAL(i2.getName(),
136 "/local/ndn/prefix/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a");
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700137 BOOST_CHECK_EQUAL(i2.getCanBePrefix(), false);
138 BOOST_CHECK_EQUAL(i2.getMustBeFresh(), false);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400139 BOOST_CHECK_EQUAL(i2.getForwardingHint().empty(), true);
Davide Pesavento835f0272019-09-21 13:18:24 -0400140 BOOST_CHECK_EQUAL(i2.hasNonce(), true);
Davide Pesavento53533942020-03-04 23:10:06 -0500141 BOOST_CHECK_EQUAL(i2.getNonce(), 0x1);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700142 BOOST_CHECK_EQUAL(i2.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400143 BOOST_CHECK(i2.getHopLimit() == nullopt);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400144 BOOST_CHECK_EQUAL(i2.hasApplicationParameters(), true);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400145 BOOST_CHECK_EQUAL(i2.getApplicationParameters(), "2404C0C1C2C3"_block);
Eric Newberry6e262f02020-05-29 23:11:25 -0700146 BOOST_CHECK(i2.getSignatureInfo() == nullopt);
147 BOOST_CHECK_EQUAL(i2.getSignatureValue().isValid(), false);
148 BOOST_CHECK_EQUAL(i2.isSigned(), false);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700149}
150
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400151BOOST_AUTO_TEST_CASE(Full)
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700152{
153 const uint8_t WIRE[] = {
Junxiao Shie4603e12022-01-05 19:12:25 +0000154 0x05, 0x56, // Interest
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400155 0x07, 0x36, // 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,
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700163 0x21, 0x00, // CanBePrefix
164 0x12, 0x00, // MustBeFresh
Junxiao Shie4603e12022-01-05 19:12:25 +0000165 0x1e, 0x05, // ForwardingHint
166 0x07, 0x03, 0x08, 0x01, 0x48,
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700167 0x0a, 0x04, // Nonce
Davide Pesavento53533942020-03-04 23:10:06 -0500168 0x4c, 0x1e, 0xcb, 0x4a,
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400169 0x0c, 0x02, // InterestLifetime
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700170 0x76, 0xa1,
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400171 0x22, 0x01, // HopLimit
172 0xdc,
Davide Pesavento9c19a392019-04-06 15:07:54 -0400173 0x24, 0x04, // ApplicationParameters
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400174 0xc0, 0xc1, 0xc2, 0xc3
175 };
176
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700177 Interest i1;
178 i1.setName("/local/ndn/prefix");
179 i1.setMustBeFresh(true);
180 i1.setCanBePrefix(true);
Junxiao Shie4603e12022-01-05 19:12:25 +0000181 i1.setForwardingHint({"/H"});
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700182 i1.setNonce(0x4c1ecb4a);
183 i1.setInterestLifetime(30369_ms);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400184 i1.setHopLimit(220);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400185 i1.setApplicationParameters("2404C0C1C2C3"_block);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400186 BOOST_CHECK_EQUAL(i1.isParametersDigestValid(), true);
187
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700188 Block wire1 = i1.wireEncode();
189 BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
190
191 Interest i2(wire1);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400192 BOOST_CHECK_EQUAL(i2.getName(),
193 "/local/ndn/prefix/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a");
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700194 BOOST_CHECK_EQUAL(i2.getCanBePrefix(), true);
195 BOOST_CHECK_EQUAL(i2.getMustBeFresh(), true);
Junxiao Shie4603e12022-01-05 19:12:25 +0000196 BOOST_TEST(i2.getForwardingHint() == std::vector<Name>({"/H"}), boost::test_tools::per_element());
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400197 BOOST_CHECK_EQUAL(i2.hasNonce(), true);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700198 BOOST_CHECK_EQUAL(i2.getNonce(), 0x4c1ecb4a);
199 BOOST_CHECK_EQUAL(i2.getInterestLifetime(), 30369_ms);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400200 BOOST_CHECK_EQUAL(*i2.getHopLimit(), 220);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400201 BOOST_CHECK_EQUAL(i2.getApplicationParameters(), "2404C0C1C2C3"_block);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700202}
203
Eric Newberry6e262f02020-05-29 23:11:25 -0700204BOOST_AUTO_TEST_CASE(Signed)
205{
206 const uint8_t WIRE[] = {
207 0x05, 0x77, // Interest
208 0x07, 0x36, // Name
209 0x08, 0x05, // GenericNameComponent
210 0x6c, 0x6f, 0x63, 0x61, 0x6c,
211 0x08, 0x03, // GenericNameComponent
212 0x6e, 0x64, 0x6e,
213 0x08, 0x06, // GenericNameComponent
214 0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
215 0x02, 0x20, // ParametersSha256DigestComponent
216 0x6f, 0x29, 0x58, 0x60, 0x53, 0xee, 0x9f, 0xcc,
217 0xd8, 0xa4, 0x22, 0x12, 0x29, 0x25, 0x28, 0x7c,
218 0x0a, 0x18, 0x43, 0x5f, 0x40, 0x74, 0xc4, 0x0a,
219 0xbb, 0x0d, 0x5b, 0x30, 0xe4, 0xaa, 0x62, 0x20,
220 0x12, 0x00, // MustBeFresh
221 0x0a, 0x04, // Nonce
222 0x4c, 0x1e, 0xcb, 0x4a,
223 0x24, 0x04, // ApplicationParameters
224 0xc0, 0xc1, 0xc2, 0xc3,
225 0x2c, 0x0d, // InterestSignatureInfo
226 0x1b, 0x01, // SignatureType
227 0x00,
228 0x26, 0x08, // SignatureNonce
229 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
230 0x2e, 0x20, // InterestSignatureValue
231 0x12, 0x47, 0x1a, 0xe0, 0xf8, 0x72, 0x3a, 0xc1,
232 0x15, 0x6c, 0x37, 0x0a, 0x38, 0x71, 0x1e, 0xbe,
233 0xbf, 0x28, 0x17, 0xde, 0x9b, 0x2d, 0xd9, 0x4e,
234 0x9b, 0x7e, 0x62, 0xf1, 0x17, 0xb8, 0x76, 0xc1,
235 };
236
237 SignatureInfo si(tlv::DigestSha256);
238 std::vector<uint8_t> nonce{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
239 si.setNonce(nonce);
240 Block sv("2E20 12471AE0F8723AC1156C370A38711EBEBF2817DE9B2DD94E9B7E62F117B876C1"_block);
241
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500242 Interest i1(Block{WIRE});
Eric Newberry6e262f02020-05-29 23:11:25 -0700243 BOOST_CHECK_EQUAL(i1.getName(),
244 "/local/ndn/prefix/params-sha256=6f29586053ee9fccd8a422122925287c0a18435f4074c40abb0d5b30e4aa6220");
245 BOOST_CHECK_EQUAL(i1.getCanBePrefix(), false);
246 BOOST_CHECK_EQUAL(i1.getMustBeFresh(), true);
247 BOOST_CHECK_EQUAL(i1.hasNonce(), true);
248 BOOST_CHECK_EQUAL(i1.getNonce(), 0x4c1ecb4a);
249 BOOST_CHECK_EQUAL(i1.getSignatureInfo()->getSignatureType(), tlv::DigestSha256);
250 BOOST_CHECK(i1.getSignatureInfo()->getNonce() == nonce);
251 BOOST_CHECK_EQUAL_COLLECTIONS(i1.getSignatureValue().begin(), i1.getSignatureValue().end(),
252 sv.begin(), sv.end());
253 BOOST_CHECK_EQUAL(i1.getApplicationParameters(), "2404C0C1C2C3"_block);
254 BOOST_CHECK_EQUAL(i1.isParametersDigestValid(), true);
255
256 // Reset wire
257 BOOST_CHECK_EQUAL(i1.hasWire(), true);
258 i1.setCanBePrefix(true);
259 i1.setCanBePrefix(false);
260 BOOST_CHECK_EQUAL(i1.hasWire(), false);
261
262 Block wire1 = i1.wireEncode();
263 BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
264
265 Interest i2("/local/ndn/prefix");
Eric Newberry6e262f02020-05-29 23:11:25 -0700266 i2.setMustBeFresh(true);
267 i2.setNonce(0x4c1ecb4a);
268 i2.setApplicationParameters("2404C0C1C2C3"_block);
269 i2.setSignatureInfo(si);
270 i2.setSignatureValue(make_shared<Buffer>(sv.value(), sv.value_size()));
271 BOOST_CHECK_EQUAL(i2.isParametersDigestValid(), true);
272
273 Block wire2 = i2.wireEncode();
274 BOOST_CHECK_EQUAL_COLLECTIONS(wire2.begin(), wire2.end(), WIRE, WIRE + sizeof(WIRE));
275}
276
277BOOST_AUTO_TEST_CASE(SignedApplicationElements)
278{
279 const uint8_t WIRE[] = {
280 0x05, 0x8f, // Interest
281 0x07, 0x36, // Name
282 0x08, 0x05, // GenericNameComponent
283 0x6c, 0x6f, 0x63, 0x61, 0x6c,
284 0x08, 0x03, // GenericNameComponent
285 0x6e, 0x64, 0x6e,
286 0x08, 0x06, // GenericNameComponent
287 0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
288 0x02, 0x20, // ParametersSha256DigestComponent
289 0xbc, 0x36, 0x30, 0xa4, 0xd6, 0x5e, 0x0d, 0xb5,
290 0x48, 0x3d, 0xfa, 0x0d, 0x28, 0xb3, 0x31, 0x2f,
291 0xca, 0xc1, 0xd4, 0x41, 0xec, 0x89, 0x61, 0xd4,
292 0x17, 0x5e, 0x61, 0x75, 0x17, 0x78, 0x10, 0x8e,
293 0x12, 0x00, // MustBeFresh
294 0x0a, 0x04, // Nonce
295 0x4c, 0x1e, 0xcb, 0x4a,
296 0x24, 0x04, // ApplicationParameters
297 0xc0, 0xc1, 0xc2, 0xc3,
298 0xfd, 0x01, 0xfe, 0x08, // Application-specific element (Type 510)
299 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80,
300 0x2c, 0x0d, // InterestSignatureInfo
301 0x1b, 0x01, // SignatureType
302 0x00,
303 0x26, 0x08, // SignatureNonce
304 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
305 0x2e, 0x20, // InterestSignatureValue
306 0x12, 0x47, 0x1a, 0xe0, 0xf8, 0x72, 0x3a, 0xc1,
307 0x15, 0x6c, 0x37, 0x0a, 0x38, 0x71, 0x1e, 0xbe,
308 0xbf, 0x28, 0x17, 0xde, 0x9b, 0x2d, 0xd9, 0x4e,
309 0x9b, 0x7e, 0x62, 0xf1, 0x17, 0xb8, 0x76, 0xc1,
310 0xfd, 0x02, 0x00, 0x08, // Application-specific element (Type 512)
311 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88
312 };
313
314 SignatureInfo si(tlv::DigestSha256);
315 std::vector<uint8_t> nonce{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
316 si.setNonce(nonce);
317 Block sv("2E20 12471AE0F8723AC1156C370A38711EBEBF2817DE9B2DD94E9B7E62F117B876C1"_block);
318
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500319 Interest i1(Block{WIRE});
Eric Newberry6e262f02020-05-29 23:11:25 -0700320 BOOST_CHECK_EQUAL(i1.getName(),
321 "/local/ndn/prefix/params-sha256=bc3630a4d65e0db5483dfa0d28b3312fcac1d441ec8961d4175e61751778108e");
322 BOOST_CHECK_EQUAL(i1.getCanBePrefix(), false);
323 BOOST_CHECK_EQUAL(i1.getMustBeFresh(), true);
324 BOOST_CHECK_EQUAL(i1.hasNonce(), true);
325 BOOST_CHECK_EQUAL(i1.getNonce(), 0x4c1ecb4a);
326 BOOST_CHECK_EQUAL(i1.getSignatureInfo()->getSignatureType(), tlv::DigestSha256);
327 BOOST_CHECK(i1.getSignatureInfo()->getNonce() == nonce);
328 BOOST_CHECK_EQUAL_COLLECTIONS(i1.getSignatureValue().begin(), i1.getSignatureValue().end(),
329 sv.begin(), sv.end());
330 BOOST_CHECK_EQUAL(i1.getApplicationParameters(), "2404C0C1C2C3"_block);
331 BOOST_CHECK_EQUAL(i1.isParametersDigestValid(), true);
332
333 // Reset wire
334 BOOST_CHECK_EQUAL(i1.hasWire(), true);
335 i1.setCanBePrefix(true);
336 i1.setCanBePrefix(false);
337 BOOST_CHECK_EQUAL(i1.hasWire(), false);
338
339 Block wire1 = i1.wireEncode();
340 BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
341}
342
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400343BOOST_AUTO_TEST_CASE(MissingApplicationParameters)
344{
345 Interest i;
346 i.setName(Name("/A").appendParametersSha256DigestPlaceholder());
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400347 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
Davide Pesavento905d40f2020-06-09 21:33:25 -0400348 BOOST_CHECK_EXCEPTION(i.wireEncode(), tlv::Error, [] (const auto& e) {
349 return e.what() == "Interest without parameters must not have a ParametersSha256DigestComponent"s;
350 });
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400351}
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400352
353BOOST_AUTO_TEST_CASE(MissingParametersSha256DigestComponent)
354{
355 // there's no way to create an Interest that fails this check via programmatic construction,
356 // so we have to decode an invalid Interest and force reencoding
357
358 DisableAutoCheckParametersDigest disabler;
359 Interest i("050F 0703(080149) 0A04F000F000 2402CAFE"_block);
360 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
361 BOOST_CHECK_NO_THROW(i.wireEncode()); // this succeeds because it uses the cached wire encoding
362
Davide Pesavento905d40f2020-06-09 21:33:25 -0400363 // trigger reencoding
364 i.setNonce(42);
365 // now the check fails while attempting to reencode
366 BOOST_CHECK_EXCEPTION(i.wireEncode(), tlv::Error, [] (const auto& e) {
367 return e.what() == "Interest with parameters must have a ParametersSha256DigestComponent"s;
368 });
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400369}
370
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400371BOOST_AUTO_TEST_SUITE_END() // Encode
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400372
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400373class DecodeFixture
Junxiao Shi899277a2017-07-07 22:12:12 +0000374{
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000375protected:
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400376 DecodeFixture()
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000377 {
378 // initialize all elements to non-empty, to verify wireDecode clears them
379 i.setName("/A");
Junxiao Shie4603e12022-01-05 19:12:25 +0000380 i.setForwardingHint({"/F"});
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000381 i.setNonce(0x03d645a8);
382 i.setInterestLifetime(18554_ms);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400383 i.setHopLimit(64);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400384 i.setApplicationParameters("2404A0A1A2A3"_block);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000385 }
Junxiao Shi899277a2017-07-07 22:12:12 +0000386
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000387protected:
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000388 Interest i;
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000389};
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000390
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400391BOOST_FIXTURE_TEST_SUITE(Decode, DecodeFixture)
392
393BOOST_AUTO_TEST_CASE(NotAnInterest)
394{
Davide Pesavento905d40f2020-06-09 21:33:25 -0400395 BOOST_CHECK_EXCEPTION(i.wireDecode("4202CAFE"_block), tlv::Error, [] (const auto& e) {
396 return e.what() == "Expecting Interest element, but TLV has type 66"s;
397 });
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400398}
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000399
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400400BOOST_AUTO_TEST_CASE(NameOnly)
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000401{
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400402 i.wireDecode("0505 0703(080149)"_block);
Davide Pesavento835f0272019-09-21 13:18:24 -0400403 BOOST_CHECK_EQUAL(i.hasWire(), true);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000404 BOOST_CHECK_EQUAL(i.getName(), "/I");
405 BOOST_CHECK_EQUAL(i.getCanBePrefix(), false);
406 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400407 BOOST_CHECK_EQUAL(i.getForwardingHint().empty(), true);
Davide Pesavento835f0272019-09-21 13:18:24 -0400408 BOOST_CHECK_EQUAL(i.hasNonce(), false);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000409 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400410 BOOST_CHECK(i.getHopLimit() == nullopt);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400411 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), false);
412 BOOST_CHECK_EQUAL(i.getApplicationParameters().isValid(), false);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000413
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400414 // modify then re-encode
Davide Pesavento53533942020-03-04 23:10:06 -0500415 i.setNonce(0x957c6554);
Davide Pesavento835f0272019-09-21 13:18:24 -0400416 BOOST_CHECK_EQUAL(i.hasWire(), false);
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400417 BOOST_CHECK_EQUAL(i.wireEncode(), "050B 0703(080149) 0A04957C6554"_block);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000418}
419
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400420BOOST_AUTO_TEST_CASE(NameCanBePrefix)
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000421{
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400422 i.wireDecode("0507 0703(080149) 2100"_block);
Davide Pesavento835f0272019-09-21 13:18:24 -0400423 BOOST_CHECK_EQUAL(i.hasWire(), true);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400424 BOOST_CHECK_EQUAL(i.getName(), "/I");
425 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
426 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
427 BOOST_CHECK_EQUAL(i.getForwardingHint().empty(), true);
Davide Pesavento835f0272019-09-21 13:18:24 -0400428 BOOST_CHECK_EQUAL(i.hasNonce(), false);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400429 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400430 BOOST_CHECK(i.getHopLimit() == nullopt);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400431 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), false);
432 BOOST_CHECK_EQUAL(i.getApplicationParameters().isValid(), false);
433}
434
435BOOST_AUTO_TEST_CASE(FullWithoutParameters)
436{
437 i.wireDecode("0531 0703(080149) "
438 "FC00 2100 FC00 1200 FC00 1E0B(1F09 1E023E15 0703080148) "
439 "FC00 0A044ACB1E4C FC00 0C0276A1 FC00 2201D6 FC00"_block);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000440 BOOST_CHECK_EQUAL(i.getName(), "/I");
441 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
442 BOOST_CHECK_EQUAL(i.getMustBeFresh(), true);
Junxiao Shie4603e12022-01-05 19:12:25 +0000443 BOOST_TEST(i.getForwardingHint() == std::vector<Name>({"/H"}), boost::test_tools::per_element());
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400444 BOOST_CHECK_EQUAL(i.hasNonce(), true);
Davide Pesavento53533942020-03-04 23:10:06 -0500445 BOOST_CHECK_EQUAL(i.getNonce(), 0x4acb1e4c);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000446 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 30369_ms);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400447 BOOST_CHECK_EQUAL(*i.getHopLimit(), 214);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400448 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), false);
449 BOOST_CHECK_EQUAL(i.getApplicationParameters().isValid(), false);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000450
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000451 // encode without modification: retain original wire encoding
Junxiao Shi8b753a22018-10-24 01:51:40 +0000452 BOOST_CHECK_EQUAL(i.wireEncode().value_size(), 49);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000453
Junxiao Shie4603e12022-01-05 19:12:25 +0000454 // modify then re-encode:
455 // * unrecognized elements are discarded;
456 // * ForwardingHint is re-encoded as a sequence of Names
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000457 i.setName("/J");
Junxiao Shi72c0c642018-04-20 15:41:09 +0000458 BOOST_CHECK_EQUAL(i.wireEncode(),
Junxiao Shie4603e12022-01-05 19:12:25 +0000459 "051D 0703(08014A) "
460 "2100 1200 1E05(0703080148) "
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400461 "0A044ACB1E4C 0C0276A1 2201D6"_block);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400462}
463
464BOOST_AUTO_TEST_CASE(FullWithParameters)
465{
466 i.wireDecode("055B 0725(080149 0220F16DB273F40436A852063F864D5072B01EAD53151F5A688EA1560492BEBEDD05) "
467 "FC00 2100 FC00 1200 FC00 1E0B(1F09 1E023E15 0703080148) "
468 "FC00 0A044ACB1E4C FC00 0C0276A1 FC00 2201D6 FC00 2404C0C1C2C3 FC00"_block);
469 BOOST_CHECK_EQUAL(i.getName(),
470 "/I/params-sha256=f16db273f40436a852063f864d5072b01ead53151f5a688ea1560492bebedd05");
471 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
472 BOOST_CHECK_EQUAL(i.getMustBeFresh(), true);
Junxiao Shie4603e12022-01-05 19:12:25 +0000473 BOOST_TEST(i.getForwardingHint() == std::vector<Name>({"/H"}), boost::test_tools::per_element());
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400474 BOOST_CHECK_EQUAL(i.hasNonce(), true);
Davide Pesavento53533942020-03-04 23:10:06 -0500475 BOOST_CHECK_EQUAL(i.getNonce(), 0x4acb1e4c);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400476 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 30369_ms);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400477 BOOST_CHECK_EQUAL(*i.getHopLimit(), 214);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400478 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true);
479 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2404C0C1C2C3"_block);
480
481 // encode without modification: retain original wire encoding
482 BOOST_CHECK_EQUAL(i.wireEncode().value_size(), 91);
483
Junxiao Shie4603e12022-01-05 19:12:25 +0000484 // modify then re-encode:
485 // * unrecognized elements after ApplicationParameters are preserved, the rest are discarded;
486 // * ForwardingHint is re-encoded as a sequence of Names
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400487 i.setName("/J");
488 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
489 BOOST_CHECK_EQUAL(i.wireEncode(),
Junxiao Shie4603e12022-01-05 19:12:25 +0000490 "0547 0725(08014A 0220F16DB273F40436A852063F864D5072B01EAD53151F5A688EA1560492BEBEDD05) "
491 "2100 1200 1E05(0703080148) "
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400492 "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3 FC00"_block);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400493
494 // modify ApplicationParameters: unrecognized elements are preserved
495 i.setApplicationParameters("2402CAFE"_block);
496 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
497 BOOST_CHECK_EQUAL(i.wireEncode(),
Junxiao Shie4603e12022-01-05 19:12:25 +0000498 "0545 0725(08014A 02205FDA67967EE302FC457E41B7D3D51BA6A9379574D193FD88F64954BF16C2927A) "
499 "2100 1200 1E05(0703080148) "
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400500 "0A044ACB1E4C 0C0276A1 2201D6 2402CAFE FC00"_block);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000501}
502
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000503BOOST_AUTO_TEST_CASE(CriticalElementOutOfOrder)
504{
Davide Pesavento905d40f2020-06-09 21:33:25 -0400505 BOOST_CHECK_EXCEPTION(i.wireDecode(
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000506 "0529 2100 0703080149 1200 1E0B(1F09 1E023E15 0703080148) "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500507 "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3"_block),
Davide Pesavento905d40f2020-06-09 21:33:25 -0400508 tlv::Error,
509 [] (const auto& e) { return e.what() == "Name element is missing or out of order"s; });
510 BOOST_CHECK_EXCEPTION(i.wireDecode(
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000511 "0529 0703080149 1200 2100 1E0B(1F09 1E023E15 0703080148) "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500512 "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3"_block),
Davide Pesavento905d40f2020-06-09 21:33:25 -0400513 tlv::Error,
514 [] (const auto& e) { return e.what() == "CanBePrefix element is out of order"s; });
515 BOOST_CHECK_EXCEPTION(i.wireDecode(
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000516 "0529 0703080149 2100 1E0B(1F09 1E023E15 0703080148) 1200 "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500517 "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3"_block),
Davide Pesavento905d40f2020-06-09 21:33:25 -0400518 tlv::Error,
519 [] (const auto& e) { return e.what() == "MustBeFresh element is out of order"s; });
520 BOOST_CHECK_EXCEPTION(i.wireDecode(
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000521 "0529 0703080149 2100 1200 0A044ACB1E4C "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500522 "1E0B(1F09 1E023E15 0703080148) 0C0276A1 2201D6 2404C0C1C2C3"_block),
Davide Pesavento905d40f2020-06-09 21:33:25 -0400523 tlv::Error,
524 [] (const auto& e) { return e.what() == "ForwardingHint element is out of order"s; });
525 BOOST_CHECK_EXCEPTION(i.wireDecode(
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000526 "0529 0703080149 2100 1200 1E0B(1F09 1E023E15 0703080148) "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500527 "0C0276A1 0A044ACB1E4C 2201D6 2404C0C1C2C3"_block),
Davide Pesavento905d40f2020-06-09 21:33:25 -0400528 tlv::Error,
529 [] (const auto& e) { return e.what() == "Nonce element is out of order"s; });
530 BOOST_CHECK_EXCEPTION(i.wireDecode(
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000531 "0529 0703080149 2100 1200 1E0B(1F09 1E023E15 0703080148) "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500532 "0A044ACB1E4C 2201D6 0C0276A1 2404C0C1C2C3"_block),
Davide Pesavento905d40f2020-06-09 21:33:25 -0400533 tlv::Error,
534 [] (const auto& e) { return e.what() == "InterestLifetime element is out of order"s; });
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000535}
536
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500537BOOST_AUTO_TEST_CASE(NonCriticalElementOutOfOrder)
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000538{
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400539 // duplicate HopLimit
540 i.wireDecode("0536 0725(080149 0220FF9100E04EAADCF30674D98026A051BA25F56B69BFA026DCCCD72C6EA0F7315A)"
541 "2201D6 2200 2404C0C1C2C3 22020101"_block);
542 BOOST_CHECK_EQUAL(i.getName(),
543 "/I/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a");
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400544 BOOST_CHECK_EQUAL(*i.getHopLimit(), 214);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400545 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400546 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2404C0C1C2C3"_block);
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500547
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400548 // duplicate ApplicationParameters
549 i.wireDecode("0541 0725(080149 0220FF9100E04EAADCF30674D98026A051BA25F56B69BFA026DCCCD72C6EA0F7315A)"
550 "2100 1200 0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3 2401EE"_block);
551 BOOST_CHECK_EQUAL(i.getName(),
552 "/I/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a");
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400553 BOOST_CHECK_EQUAL(*i.getHopLimit(), 214);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400554 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true);
555 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2404C0C1C2C3"_block);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000556}
557
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400558BOOST_AUTO_TEST_CASE(MissingName)
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000559{
Davide Pesavento905d40f2020-06-09 21:33:25 -0400560 BOOST_CHECK_EXCEPTION(i.wireDecode("0500"_block), tlv::Error,
561 [] (const auto& e) { return e.what() == "Name element is missing or out of order"s; });
562 BOOST_CHECK_EXCEPTION(i.wireDecode("0502 1200"_block), tlv::Error,
563 [] (const auto& e) { return e.what() == "Name element is missing or out of order"s; });
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000564}
565
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400566BOOST_AUTO_TEST_CASE(BadName)
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000567{
Davide Pesavento905d40f2020-06-09 21:33:25 -0400568 BOOST_CHECK_EXCEPTION(i.wireDecode("0502 0700"_block), tlv::Error,
569 [] (const auto& e) { return e.what() == "Name has zero name components"s; });
570 BOOST_CHECK_EXCEPTION(i.wireDecode("054C 074A(080149"
571 "02200000000000000000000000000000000000000000000000000000000000000000"
572 "080132"
573 "02200000000000000000000000000000000000000000000000000000000000000000)"_block),
574 tlv::Error,
575 [] (const auto& e) { return e.what() == "Name has more than one ParametersSha256DigestComponent"s; });
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000576}
577
578BOOST_AUTO_TEST_CASE(BadCanBePrefix)
579{
Davide Pesavento905d40f2020-06-09 21:33:25 -0400580 BOOST_CHECK_EXCEPTION(i.wireDecode("0508 0703080149 210102"_block), tlv::Error,
581 [] (const auto& e) { return e.what() == "CanBePrefix element has non-zero TLV-LENGTH"s; });
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000582}
583
584BOOST_AUTO_TEST_CASE(BadMustBeFresh)
585{
Davide Pesavento905d40f2020-06-09 21:33:25 -0400586 BOOST_CHECK_EXCEPTION(i.wireDecode("0508 0703080149 120102"_block), tlv::Error,
587 [] (const auto& e) { return e.what() == "MustBeFresh element has non-zero TLV-LENGTH"s; });
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000588}
589
590BOOST_AUTO_TEST_CASE(BadNonce)
591{
Davide Pesavento905d40f2020-06-09 21:33:25 -0400592 BOOST_CHECK_EXCEPTION(i.wireDecode("0507 0703080149 0A00"_block), tlv::Error,
593 [] (const auto& e) { return e.what() == "Nonce element is malformed"s; });
594 BOOST_CHECK_EXCEPTION(i.wireDecode("050A 0703080149 0A0304C263"_block), tlv::Error,
595 [] (const auto& e) { return e.what() == "Nonce element is malformed"s; });
596 BOOST_CHECK_EXCEPTION(i.wireDecode("050C 0703080149 0A05EFA420B262"_block), tlv::Error,
597 [] (const auto& e) { return e.what() == "Nonce element is malformed"s; });
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000598}
599
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500600BOOST_AUTO_TEST_CASE(BadHopLimit)
601{
Davide Pesavento905d40f2020-06-09 21:33:25 -0400602 BOOST_CHECK_EXCEPTION(i.wireDecode("0507 0703080149 2200"_block), tlv::Error,
603 [] (const auto& e) { return e.what() == "HopLimit element is malformed"s; });
604 BOOST_CHECK_EXCEPTION(i.wireDecode("0509 0703080149 22021356"_block), tlv::Error,
605 [] (const auto& e) { return e.what() == "HopLimit element is malformed"s; });
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500606}
607
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400608BOOST_AUTO_TEST_CASE(BadParametersDigest)
609{
610 // ApplicationParameters without ParametersSha256DigestComponent
611 Block b1("0509 0703(080149) 2402CAFE"_block);
612 // ParametersSha256DigestComponent without ApplicationParameters
613 Block b2("0527 0725(080149 0220E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855)"_block);
614 // digest mismatch
615 Block b3("052B 0725(080149 02200000000000000000000000000000000000000000000000000000000000000000) "
616 "2402CAFE"_block);
617
618 BOOST_CHECK_THROW(i.wireDecode(b1), tlv::Error);
619 BOOST_CHECK_THROW(i.wireDecode(b2), tlv::Error);
620 BOOST_CHECK_THROW(i.wireDecode(b3), tlv::Error);
621
622 DisableAutoCheckParametersDigest disabler;
623 BOOST_CHECK_NO_THROW(i.wireDecode(b1));
624 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
625 BOOST_CHECK_NO_THROW(i.wireDecode(b2));
626 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
627 BOOST_CHECK_NO_THROW(i.wireDecode(b3));
628 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
629}
630
Junxiao Shi8b753a22018-10-24 01:51:40 +0000631BOOST_AUTO_TEST_CASE(UnrecognizedNonCriticalElementBeforeName)
632{
Davide Pesavento905d40f2020-06-09 21:33:25 -0400633 BOOST_CHECK_EXCEPTION(i.wireDecode("0507 FC00 0703080149"_block), tlv::Error,
634 [] (const auto& e) { return e.what() == "Name element is missing or out of order"s; });
Junxiao Shi8b753a22018-10-24 01:51:40 +0000635}
636
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000637BOOST_AUTO_TEST_CASE(UnrecognizedCriticalElement)
638{
Davide Pesavento905d40f2020-06-09 21:33:25 -0400639 BOOST_CHECK_EXCEPTION(i.wireDecode("0507 0703080149 FB00"_block), tlv::Error,
640 [] (const auto& e) { return e.what() == "Unrecognized element of critical type 251"s; });
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400641 // v0.2 packet with Selectors
Davide Pesavento905d40f2020-06-09 21:33:25 -0400642 BOOST_CHECK_EXCEPTION(i.wireDecode("0510 0703080149 09030D0101 0A0401000000"_block), tlv::Error,
643 [] (const auto& e) { return e.what() == "Unrecognized element of critical type 9"s; });
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000644}
645
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400646BOOST_AUTO_TEST_SUITE_END() // Decode
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000647
Junxiao Shi899277a2017-07-07 22:12:12 +0000648BOOST_AUTO_TEST_CASE(MatchesData)
649{
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000650 auto interest = makeInterest("/A");
Junxiao Shi899277a2017-07-07 22:12:12 +0000651
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000652 auto data = makeData("/A");
653 BOOST_CHECK_EQUAL(interest->matchesData(*data), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000654
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000655 data->setName("/A/D");
656 BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // violates CanBePrefix
Junxiao Shi899277a2017-07-07 22:12:12 +0000657
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000658 interest->setCanBePrefix(true);
659 BOOST_CHECK_EQUAL(interest->matchesData(*data), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000660
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000661 interest->setMustBeFresh(true);
662 BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // violates MustBeFresh
Junxiao Shi899277a2017-07-07 22:12:12 +0000663
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000664 data->setFreshnessPeriod(1_s);
665 BOOST_CHECK_EQUAL(interest->matchesData(*data), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000666
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000667 data->setName("/H/I");
668 BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // Name does not match
Junxiao Shi899277a2017-07-07 22:12:12 +0000669
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000670 data->wireEncode();
671 interest = makeInterest(data->getFullName());
672 BOOST_CHECK_EQUAL(interest->matchesData(*data), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000673
Davide Pesavento53533942020-03-04 23:10:06 -0500674 setNameComponent(*interest, -1,
675 name::Component::fromEscapedString("sha256digest=00000000000000000000000000"
676 "00000000000000000000000000000000000000"));
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000677 BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // violates implicit digest
Junxiao Shi899277a2017-07-07 22:12:12 +0000678}
679
680BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(MatchesInterest, 1)
681BOOST_AUTO_TEST_CASE(MatchesInterest)
682{
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400683 Interest interest;
684 interest.setName("/A")
685 .setCanBePrefix(true)
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000686 .setMustBeFresh(true)
Junxiao Shie4603e12022-01-05 19:12:25 +0000687 .setForwardingHint({"/H"})
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000688 .setNonce(2228)
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400689 .setInterestLifetime(5_s)
690 .setHopLimit(90);
Junxiao Shi899277a2017-07-07 22:12:12 +0000691
692 Interest other;
693 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false);
694
695 other.setName(interest.getName());
696 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false);
697
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000698 other.setCanBePrefix(interest.getCanBePrefix());
699 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false);
700
701 other.setMustBeFresh(interest.getMustBeFresh());
Junxiao Shi899277a2017-07-07 22:12:12 +0000702 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false); // will match until #3162 implemented
703
Junxiao Shie4603e12022-01-05 19:12:25 +0000704 auto fh = interest.getForwardingHint();
705 other.setForwardingHint(std::vector<Name>(fh.begin(), fh.end()));
Junxiao Shi899277a2017-07-07 22:12:12 +0000706 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
707
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000708 other.setNonce(9336);
Junxiao Shi899277a2017-07-07 22:12:12 +0000709 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
710
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000711 other.setInterestLifetime(3_s);
Junxiao Shi899277a2017-07-07 22:12:12 +0000712 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400713
714 other.setHopLimit(31);
715 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000716}
717
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400718BOOST_AUTO_TEST_CASE(SetName)
719{
720 Interest i;
721 BOOST_CHECK_EQUAL(i.getName(), "/");
722 i.setName("/A/B");
723 BOOST_CHECK_EQUAL(i.getName(), "/A/B");
724 i.setName("/I/params-sha256=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
725 BOOST_CHECK_EQUAL(i.getName(),
726 "/I/params-sha256=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
727 BOOST_CHECK_THROW(i.setName("/I"
728 "/params-sha256=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
729 "/params-sha256=0000000000000000000000000000000000000000000000000000000000000000"),
730 std::invalid_argument);
731}
Junxiao Shi899277a2017-07-07 22:12:12 +0000732
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400733BOOST_AUTO_TEST_CASE(SetCanBePrefix)
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000734{
735 Interest i;
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000736 BOOST_CHECK_EQUAL(i.getCanBePrefix(), false);
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000737 i.setCanBePrefix(true);
738 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
Davide Pesavento478d3382021-03-17 12:46:08 -0400739 i.setCanBePrefix(false);
740 BOOST_CHECK_EQUAL(i.getCanBePrefix(), false);
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000741}
742
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400743BOOST_AUTO_TEST_CASE(SetMustBeFresh)
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000744{
745 Interest i;
746 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
747 i.setMustBeFresh(true);
748 BOOST_CHECK_EQUAL(i.getMustBeFresh(), true);
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000749 i.setMustBeFresh(false);
750 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000751}
752
Junxiao Shi899277a2017-07-07 22:12:12 +0000753BOOST_AUTO_TEST_CASE(GetNonce)
754{
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000755 unique_ptr<Interest> i1, i2;
Davide Pesavento53533942020-03-04 23:10:06 -0500756 Interest::Nonce nonce1(0), nonce2(0);
Junxiao Shi899277a2017-07-07 22:12:12 +0000757
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000758 // getNonce automatically assigns a random Nonce.
759 // It's possible to assign the same Nonce to two Interest, but it's unlikely to get 100 pairs of
Davide Pesavento53533942020-03-04 23:10:06 -0500760 // identical Nonces in a row.
Junxiao Shi899277a2017-07-07 22:12:12 +0000761 int nIterations = 0;
762 do {
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000763 i1 = make_unique<Interest>();
764 nonce1 = i1->getNonce();
765 i2 = make_unique<Interest>();
766 nonce2 = i2->getNonce();
Junxiao Shi899277a2017-07-07 22:12:12 +0000767 }
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000768 while (nonce1 == nonce2 && ++nIterations < 100);
Davide Pesavento53533942020-03-04 23:10:06 -0500769
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000770 BOOST_CHECK_NE(nonce1, nonce2);
771 BOOST_CHECK(i1->hasNonce());
772 BOOST_CHECK(i2->hasNonce());
Junxiao Shi899277a2017-07-07 22:12:12 +0000773
774 // Once a Nonce is assigned, it should not change.
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000775 BOOST_CHECK_EQUAL(i1->getNonce(), nonce1);
Davide Pesavento53533942020-03-04 23:10:06 -0500776 BOOST_CHECK_EQUAL(i2->getNonce(), nonce2);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000777}
778
779BOOST_AUTO_TEST_CASE(SetNonce)
780{
781 Interest i1("/A");
Davide Pesavento53533942020-03-04 23:10:06 -0500782 BOOST_CHECK(!i1.hasNonce());
783
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000784 i1.setNonce(1);
785 i1.wireEncode();
Davide Pesavento53533942020-03-04 23:10:06 -0500786 BOOST_CHECK(i1.hasNonce());
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000787 BOOST_CHECK_EQUAL(i1.getNonce(), 1);
788
789 Interest i2(i1);
Davide Pesavento53533942020-03-04 23:10:06 -0500790 BOOST_CHECK(i2.hasNonce());
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000791 BOOST_CHECK_EQUAL(i2.getNonce(), 1);
792
793 i2.setNonce(2);
794 BOOST_CHECK_EQUAL(i2.getNonce(), 2);
Davide Pesavento53533942020-03-04 23:10:06 -0500795 BOOST_CHECK_EQUAL(i1.getNonce(), 1); // should not affect i1's Nonce (Bug #4168)
796
797 i2.setNonce(nullopt);
798 BOOST_CHECK(!i2.hasNonce());
Junxiao Shi899277a2017-07-07 22:12:12 +0000799}
800
801BOOST_AUTO_TEST_CASE(RefreshNonce)
802{
803 Interest i;
804 BOOST_CHECK(!i.hasNonce());
805 i.refreshNonce();
806 BOOST_CHECK(!i.hasNonce());
807
808 i.setNonce(1);
809 BOOST_CHECK(i.hasNonce());
810 i.refreshNonce();
811 BOOST_CHECK(i.hasNonce());
812 BOOST_CHECK_NE(i.getNonce(), 1);
813}
814
Davide Pesavento53533942020-03-04 23:10:06 -0500815BOOST_AUTO_TEST_CASE(NonceConversions)
816{
817 Interest i;
Davide Pesavento53533942020-03-04 23:10:06 -0500818
819 // 4-arg constructor
820 Interest::Nonce n1(1, 2, 3, 4);
821 i.setNonce(n1);
822 BOOST_CHECK_EQUAL(i.getNonce(), 0x01020304);
823
824 // 4-arg constructor + assignment
825 n1 = {0xf, 0xe, 0xd, 0xc};
826 i.setNonce(n1);
827 BOOST_CHECK_EQUAL(i.getNonce(), 0x0f0e0d0c);
828
829 // 1-arg constructor + assignment (implicit conversion)
830 Interest::Nonce n2;
831 n2 = 42;
832 BOOST_CHECK_NE(n1, n2);
833 i.setNonce(n2);
834 n2 = 21; // should not affect i's Nonce
835 BOOST_CHECK_EQUAL(i.getNonce(), 42);
836 BOOST_CHECK_EQUAL(i.toUri(), "/?Nonce=0000002a"); // stored in big-endian
837}
838
Junxiao Shi899277a2017-07-07 22:12:12 +0000839BOOST_AUTO_TEST_CASE(SetInterestLifetime)
840{
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500841 BOOST_CHECK_THROW(Interest("/A", -1_ms), std::invalid_argument);
Davide Pesavento0f830802018-01-16 23:58:58 -0500842 BOOST_CHECK_NO_THROW(Interest("/A", 0_ms));
Junxiao Shi899277a2017-07-07 22:12:12 +0000843
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400844 Interest i;
Junxiao Shi899277a2017-07-07 22:12:12 +0000845 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500846 BOOST_CHECK_THROW(i.setInterestLifetime(-1_ms), std::invalid_argument);
Junxiao Shi899277a2017-07-07 22:12:12 +0000847 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesavento0f830802018-01-16 23:58:58 -0500848 i.setInterestLifetime(0_ms);
849 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 0_ms);
850 i.setInterestLifetime(1_ms);
851 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 1_ms);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400852
853 i = Interest("/B", 15_s);
854 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 15_s);
855}
856
857BOOST_AUTO_TEST_CASE(SetHopLimit)
858{
859 Interest i;
860 BOOST_CHECK(i.getHopLimit() == nullopt);
861 i.setHopLimit(42);
862 BOOST_CHECK(i.getHopLimit() == 42);
863 i.setHopLimit(nullopt);
864 BOOST_CHECK(i.getHopLimit() == nullopt);
Junxiao Shi899277a2017-07-07 22:12:12 +0000865}
866
Davide Pesavento9c19a392019-04-06 15:07:54 -0400867BOOST_AUTO_TEST_CASE(SetApplicationParameters)
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700868{
869 const uint8_t PARAMETERS1[] = {0xc1};
870 const uint8_t PARAMETERS2[] = {0xc2};
871
872 Interest i;
Davide Pesavento9c19a392019-04-06 15:07:54 -0400873 BOOST_CHECK(!i.hasApplicationParameters());
874 i.setApplicationParameters("2400"_block);
875 BOOST_CHECK(i.hasApplicationParameters());
876 i.unsetApplicationParameters();
877 BOOST_CHECK(!i.hasApplicationParameters());
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700878
Davide Pesavento38912442019-04-06 22:03:39 -0400879 // Block overload
Davide Pesavento38912442019-04-06 22:03:39 -0400880 i.setApplicationParameters("2401C0"_block);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400881 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2401C0"_block);
Davide Pesavento38912442019-04-06 22:03:39 -0400882 i.setApplicationParameters("8001C1"_block);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400883 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "24038001C1"_block);
Davide Pesavento81bd6962020-06-17 16:03:23 -0400884 BOOST_CHECK_THROW(i.setApplicationParameters(Block{}), std::invalid_argument);
Davide Pesavento38912442019-04-06 22:03:39 -0400885
Davide Pesaventoa3d809e2022-02-06 11:55:02 -0500886 // span overload
887 i.setApplicationParameters(PARAMETERS1);
Davide Pesavento38912442019-04-06 22:03:39 -0400888 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2401C1"_block);
Davide Pesaventoa3d809e2022-02-06 11:55:02 -0500889 i.setApplicationParameters(span<uint8_t>{});
Davide Pesavento38912442019-04-06 22:03:39 -0400890 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2400"_block);
Davide Pesaventoa3d809e2022-02-06 11:55:02 -0500891 // raw buffer+size overload (deprecated)
Davide Pesavento38912442019-04-06 22:03:39 -0400892 BOOST_CHECK_THROW(i.setApplicationParameters(nullptr, 42), std::invalid_argument);
893
894 // ConstBufferPtr overload
895 i.setApplicationParameters(make_shared<Buffer>(PARAMETERS2, sizeof(PARAMETERS2)));
896 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2401C2"_block);
897 i.setApplicationParameters(make_shared<Buffer>());
898 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2400"_block);
899 BOOST_CHECK_THROW(i.setApplicationParameters(nullptr), std::invalid_argument);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700900}
901
Eric Newberry6e262f02020-05-29 23:11:25 -0700902BOOST_AUTO_TEST_CASE(SetSignature)
903{
904 Interest i;
Eric Newberry6e262f02020-05-29 23:11:25 -0700905 BOOST_CHECK(i.getSignatureInfo() == nullopt);
906 BOOST_CHECK_EQUAL(i.isSigned(), false);
907
908 // Throws because attempting to set InterestSignatureValue without set InterestSignatureInfo
909 Block sv1("2E04 01020304"_block);
910 auto svBuffer1 = make_shared<Buffer>(sv1.value(), sv1.value_size());
911 BOOST_CHECK_THROW(i.setSignatureValue(svBuffer1), tlv::Error);
912
913 // Simple set/get case for InterestSignatureInfo (no prior set)
914 SignatureInfo si1(tlv::SignatureSha256WithEcdsa);
915 i.setSignatureInfo(si1);
916 BOOST_CHECK(i.getSignatureInfo() == si1);
917 BOOST_CHECK_EQUAL(i.isSigned(), false);
918
919 // Simple set/get case for InterestSignatureValue (no prior set)
920 BOOST_CHECK_EQUAL(i.getSignatureValue().isValid(), false);
921 i.setSignatureValue(svBuffer1);
922 BOOST_CHECK_EQUAL(i.getSignatureValue(), sv1);
923 BOOST_CHECK_EQUAL(i.isSigned(), true);
924
925 // Throws because attempting to set InterestSignatureValue to nullptr
926 BOOST_CHECK_THROW(i.setSignatureValue(nullptr), std::invalid_argument);
927 BOOST_CHECK_EQUAL(i.getSignatureValue(), sv1);
928 BOOST_CHECK_EQUAL(i.isSigned(), true);
929
930 // Ensure that wire is not reset if specified InterestSignatureInfo is same
931 i.wireEncode();
932 BOOST_CHECK_EQUAL(i.hasWire(), true);
933 i.setSignatureInfo(si1);
934 BOOST_CHECK_EQUAL(i.hasWire(), true);
935 BOOST_CHECK(i.getSignatureInfo() == si1);
936 BOOST_CHECK_EQUAL(i.getSignatureValue(), sv1);
937 BOOST_CHECK_EQUAL(i.isSigned(), true);
938
939 // Ensure that wire is reset if specified InterestSignatureInfo is different
940 i.wireEncode();
941 BOOST_CHECK_EQUAL(i.hasWire(), true);
942 SignatureInfo si2(tlv::SignatureSha256WithRsa);
943 i.setSignatureInfo(si2);
944 BOOST_CHECK_EQUAL(i.hasWire(), false);
945 BOOST_CHECK(i.getSignatureInfo() == si2);
946 BOOST_CHECK_EQUAL(i.getSignatureValue(), sv1);
947 BOOST_CHECK_EQUAL(i.isSigned(), true);
948
949 // Ensure that wire is not reset if specified InterestSignatureValue is same
950 i.wireEncode();
951 BOOST_CHECK_EQUAL(i.hasWire(), true);
952 i.setSignatureValue(svBuffer1);
953 BOOST_CHECK_EQUAL(i.hasWire(), true);
954 BOOST_CHECK(i.getSignatureInfo() == si2);
955 BOOST_CHECK_EQUAL(i.getSignatureValue(), sv1);
956 BOOST_CHECK_EQUAL(i.isSigned(), true);
957
958 // Ensure that wire is reset if specified InterestSignatureValue is different
959 i.wireEncode();
960 BOOST_CHECK_EQUAL(i.hasWire(), true);
961 Block sv2("2E04 99887766"_block);
962 auto svBuffer2 = make_shared<Buffer>(sv2.value(), sv2.value_size());
963 i.setSignatureValue(svBuffer2);
964 BOOST_CHECK_EQUAL(i.hasWire(), false);
965 BOOST_CHECK(i.getSignatureInfo() == si2);
966 BOOST_CHECK_EQUAL(i.getSignatureValue(), sv2);
967 BOOST_CHECK_EQUAL(i.isSigned(), true);
968}
969
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400970BOOST_AUTO_TEST_CASE(ParametersSha256DigestComponent)
971{
972 Interest i("/I");
973 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
974
975 i.setApplicationParameters("2404C0C1C2C3"_block); // auto-appends ParametersSha256DigestComponent
976 BOOST_CHECK_EQUAL(i.getName(),
977 "/I/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a");
978 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
979
Davide Pesaventoa3d809e2022-02-06 11:55:02 -0500980 i.setApplicationParameters(span<uint8_t>{}); // updates ParametersSha256DigestComponent
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400981 BOOST_CHECK_EQUAL(i.getName(),
982 "/I/params-sha256=33b67cb5385ceddad93d0ee960679041613bed34b8b4a5e6362fe7539ba2d3ce");
983 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true);
984 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
985
986 i.unsetApplicationParameters(); // removes ParametersSha256DigestComponent
987 BOOST_CHECK_EQUAL(i.getName(), "/I");
988 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
989
990 i.setName(Name("/P").appendParametersSha256DigestPlaceholder().append("Q"));
991 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), false);
992 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
993
994 i.unsetApplicationParameters(); // removes ParametersSha256DigestComponent
995 BOOST_CHECK_EQUAL(i.getName(), "/P/Q");
996 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
997
998 i.setName(Name("/P").appendParametersSha256DigestPlaceholder().append("Q"));
999 i.setApplicationParameters("2404C0C1C2C3"_block); // updates ParametersSha256DigestComponent
1000 BOOST_CHECK_EQUAL(i.getName(),
1001 "/P/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a/Q");
1002 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
1003
1004 i.setName("/A/B/C"); // auto-appends ParametersSha256DigestComponent
1005 BOOST_CHECK_EQUAL(i.getName(),
1006 "/A/B/C/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a");
1007 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true);
1008 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
Eric Newberry6e262f02020-05-29 23:11:25 -07001009
1010 SignatureInfo si(tlv::SignatureSha256WithEcdsa);
1011 i.setSignatureInfo(si); // updates ParametersSha256DigestComponent
1012 BOOST_CHECK_EQUAL(i.getName(),
1013 "/A/B/C/params-sha256=6400cae1730c15fd7854b26be05794d53685423c94bc61e59c49bd640d646ae8");
1014 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
1015 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2404 C0C1C2C3"_block);
1016 BOOST_CHECK(i.getSignatureInfo() == si);
1017
1018 i.unsetApplicationParameters(); // removes ParametersSha256DigestComponent and InterestSignatureInfo
1019 BOOST_CHECK(i.getSignatureInfo() == nullopt);
1020 BOOST_CHECK_EQUAL(i.getSignatureValue().isValid(), false);
1021 BOOST_CHECK_EQUAL(i.getName(), "/A/B/C");
1022
1023 i.setSignatureInfo(si); // auto-adds an empty ApplicationParameters element
1024 BOOST_CHECK_EQUAL(i.getName(),
1025 "/A/B/C/params-sha256=d2ac0eb1f60f60ab206fb80bf1d0f73cfef353bbec43ba6ea626117f671ca3bb");
1026 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
1027 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2400"_block);
1028 BOOST_CHECK(i.getSignatureInfo() == si);
1029
1030 Block sv("2E04 01020304"_block);
1031 i.setSignatureValue(make_shared<Buffer>(sv.value(), sv.value_size())); // updates ParametersDigestSha256Component
1032 BOOST_CHECK_EQUAL(i.getName(),
1033 "/A/B/C/params-sha256=f649845ef944638390d1c689e2f0618ea02e471eff236110cbeb822d5932d342");
1034 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
1035 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2400"_block);
1036 BOOST_CHECK(i.getSignatureInfo() == si);
1037 BOOST_CHECK_EQUAL(i.getSignatureValue(), sv);
1038
1039 i.setApplicationParameters("2404C0C1C2C3"_block); // updates ParametersSha256DigestComponent
1040 BOOST_CHECK_EQUAL(i.getName(),
1041 "/A/B/C/params-sha256=c5d7e567e6b251ddf36f7a6dbed95235b2d4a0b36215bb0f3cc403ac64ad0284");
1042 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
1043 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2404 C0C1C2C3"_block);
1044 BOOST_CHECK(i.getSignatureInfo() == si);
1045 BOOST_CHECK_EQUAL(i.getSignatureValue(), sv);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -04001046}
Junxiao Shi899277a2017-07-07 22:12:12 +00001047
Eric Newberryb74bbda2020-06-18 19:33:58 -07001048BOOST_AUTO_TEST_CASE(ExtractSignedRanges)
1049{
1050 Interest i1;
Eric Newberryb74bbda2020-06-18 19:33:58 -07001051 BOOST_CHECK_EXCEPTION(i1.extractSignedRanges(), tlv::Error, [] (const auto& e) {
Eric Newberryb74bbda2020-06-18 19:33:58 -07001052 return e.what() == "Name has zero name components"s;
1053 });
1054 i1.setName("/test/prefix");
1055 i1.setNonce(0x01020304);
1056 SignatureInfo sigInfo(tlv::DigestSha256);
1057 i1.setSignatureInfo(sigInfo);
1058
1059 // Test with previously unsigned Interest (no InterestSignatureValue)
1060 auto ranges1 = i1.extractSignedRanges();
1061 BOOST_REQUIRE_EQUAL(ranges1.size(), 2);
1062 const Block& wire1 = i1.wireEncode();
1063 // Ensure Name range captured properly
1064 Block nameWithoutDigest1 = i1.getName().getPrefix(-1).wireEncode();
Davide Pesavento765abc92021-12-27 00:44:04 -05001065 BOOST_CHECK_EQUAL_COLLECTIONS(ranges1.front().begin(), ranges1.front().end(),
Eric Newberryb74bbda2020-06-18 19:33:58 -07001066 nameWithoutDigest1.value_begin(), nameWithoutDigest1.value_end());
1067 // Ensure parameters range captured properly
1068 const auto& appParamsWire1 = wire1.find(tlv::ApplicationParameters);
1069 BOOST_REQUIRE(appParamsWire1 != wire1.elements_end());
Davide Pesavento765abc92021-12-27 00:44:04 -05001070 BOOST_CHECK_EQUAL_COLLECTIONS(ranges1.back().begin(), ranges1.back().end(),
Eric Newberryb74bbda2020-06-18 19:33:58 -07001071 appParamsWire1->begin(), wire1.end());
1072
1073 // Test with Interest with existing InterestSignatureValue
1074 auto sigValue = make_shared<Buffer>();
1075 i1.setSignatureValue(sigValue);
1076 auto ranges2 = i1.extractSignedRanges();
1077 BOOST_REQUIRE_EQUAL(ranges2.size(), 2);
1078 const auto& wire2 = i1.wireEncode();
1079 // Ensure Name range captured properly
1080 Block nameWithoutDigest2 = i1.getName().getPrefix(-1).wireEncode();
Davide Pesavento765abc92021-12-27 00:44:04 -05001081 BOOST_CHECK_EQUAL_COLLECTIONS(ranges2.front().begin(), ranges2.front().end(),
Eric Newberryb74bbda2020-06-18 19:33:58 -07001082 nameWithoutDigest2.value_begin(), nameWithoutDigest2.value_end());
1083 // Ensure parameters range captured properly
1084 const auto& appParamsWire2 = wire2.find(tlv::ApplicationParameters);
1085 BOOST_REQUIRE(appParamsWire2 != wire2.elements_end());
1086 const auto& sigValueWire2 = wire2.find(tlv::InterestSignatureValue);
1087 BOOST_REQUIRE(sigValueWire2 != wire2.elements_end());
Davide Pesavento765abc92021-12-27 00:44:04 -05001088 BOOST_CHECK_EQUAL_COLLECTIONS(ranges2.back().begin(), ranges2.back().end(),
Eric Newberryb74bbda2020-06-18 19:33:58 -07001089 appParamsWire2->begin(), sigValueWire2->begin());
1090
1091 // Test with decoded Interest
1092 const uint8_t WIRE[] = {
1093 0x05, 0x6f, // Interest
1094 0x07, 0x2e, // Name
1095 0x08, 0x04, // GenericNameComponent
1096 0x61, 0x62, 0x63, 0x64,
1097 0x08, 0x04, // GenericNameComponent
1098 0x65, 0x66, 0x67, 0x68,
1099 0x02, 0x20, // ParametersSha256DigestComponent
1100 0x6f, 0x29, 0x58, 0x60, 0x53, 0xee, 0x9f, 0xcc,
1101 0xd8, 0xa4, 0x22, 0x12, 0x29, 0x25, 0x28, 0x7c,
1102 0x0a, 0x18, 0x43, 0x5f, 0x40, 0x74, 0xc4, 0x0a,
1103 0xbb, 0x0d, 0x5b, 0x30, 0xe4, 0xaa, 0x62, 0x20,
1104 0x12, 0x00, // MustBeFresh
1105 0x0a, 0x04, // Nonce
1106 0x4c, 0x1e, 0xcb, 0x4a,
1107 0x24, 0x04, // ApplicationParameters
1108 0xc0, 0xc1, 0xc2, 0xc3,
1109 0x2c, 0x0d, // InterestSignatureInfo
1110 0x1b, 0x01, // SignatureType
1111 0x00,
1112 0x26, 0x08, // SignatureNonce
1113 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
1114 0x2e, 0x20, // InterestSignatureValue
1115 0x12, 0x47, 0x1a, 0xe0, 0xf8, 0x72, 0x3a, 0xc1,
1116 0x15, 0x6c, 0x37, 0x0a, 0x38, 0x71, 0x1e, 0xbe,
1117 0xbf, 0x28, 0x17, 0xde, 0x9b, 0x2d, 0xd9, 0x4e,
1118 0x9b, 0x7e, 0x62, 0xf1, 0x17, 0xb8, 0x76, 0xc1,
1119 };
Davide Pesaventofbea4fc2022-02-08 07:26:04 -05001120 Block wire3(WIRE);
Eric Newberryb74bbda2020-06-18 19:33:58 -07001121 Interest i2(wire3);
1122 auto ranges3 = i2.extractSignedRanges();
1123 BOOST_REQUIRE_EQUAL(ranges3.size(), 2);
1124 // Ensure Name range captured properly
Davide Pesavento765abc92021-12-27 00:44:04 -05001125 BOOST_CHECK_EQUAL_COLLECTIONS(ranges3.front().begin(), ranges3.front().end(), &WIRE[4], &WIRE[16]);
Eric Newberryb74bbda2020-06-18 19:33:58 -07001126 // Ensure parameters range captured properly
Davide Pesavento765abc92021-12-27 00:44:04 -05001127 BOOST_CHECK_EQUAL_COLLECTIONS(ranges3.back().begin(), ranges3.back().end(), &WIRE[58], &WIRE[79]);
Eric Newberryb74bbda2020-06-18 19:33:58 -07001128
1129 // Test failure with missing ParametersSha256DigestComponent
1130 Interest i3("/a");
Eric Newberryb74bbda2020-06-18 19:33:58 -07001131 BOOST_CHECK_EXCEPTION(i3.extractSignedRanges(), tlv::Error, [] (const auto& e) {
1132 return e.what() == "Interest Name must end with a ParametersSha256DigestComponent"s;
1133 });
1134
1135 // Test failure with missing InterestSignatureInfo
Davide Pesaventoa3d809e2022-02-06 11:55:02 -05001136 i3.setApplicationParameters(span<uint8_t>{});
Eric Newberryb74bbda2020-06-18 19:33:58 -07001137 BOOST_CHECK_EXCEPTION(i3.extractSignedRanges(), tlv::Error, [] (const auto& e) {
1138 return e.what() == "Interest missing InterestSignatureInfo"s;
1139 });
1140}
1141
Davide Pesavento2fdb2742019-07-31 23:03:35 -04001142BOOST_AUTO_TEST_CASE(ToUri)
1143{
1144 Interest i;
Davide Pesavento2fdb2742019-07-31 23:03:35 -04001145 BOOST_CHECK_EQUAL(i.toUri(), "/");
1146
1147 i.setName("/foo");
1148 BOOST_CHECK_EQUAL(i.toUri(), "/foo");
1149
1150 i.setCanBePrefix(true);
1151 BOOST_CHECK_EQUAL(i.toUri(), "/foo?CanBePrefix");
1152
1153 i.setMustBeFresh(true);
1154 BOOST_CHECK_EQUAL(i.toUri(), "/foo?CanBePrefix&MustBeFresh");
1155
Davide Pesavento53533942020-03-04 23:10:06 -05001156 i.setNonce(0xa1b2c3);
1157 BOOST_CHECK_EQUAL(i.toUri(), "/foo?CanBePrefix&MustBeFresh&Nonce=00a1b2c3");
Davide Pesavento2fdb2742019-07-31 23:03:35 -04001158
1159 i.setInterestLifetime(2_s);
Davide Pesavento53533942020-03-04 23:10:06 -05001160 BOOST_CHECK_EQUAL(i.toUri(), "/foo?CanBePrefix&MustBeFresh&Nonce=00a1b2c3&Lifetime=2000");
Davide Pesavento2fdb2742019-07-31 23:03:35 -04001161
1162 i.setHopLimit(18);
Davide Pesavento53533942020-03-04 23:10:06 -05001163 BOOST_CHECK_EQUAL(i.toUri(), "/foo?CanBePrefix&MustBeFresh&Nonce=00a1b2c3&Lifetime=2000&HopLimit=18");
Davide Pesavento2fdb2742019-07-31 23:03:35 -04001164
1165 i.setCanBePrefix(false);
1166 i.setMustBeFresh(false);
1167 i.setHopLimit(nullopt);
1168 i.setApplicationParameters("2402CAFE"_block);
1169 BOOST_CHECK_EQUAL(i.toUri(),
1170 "/foo/params-sha256=8621f5e8321f04104640c8d02877d7c5142cad6e203c5effda1783b1a0e476d6"
Davide Pesavento53533942020-03-04 23:10:06 -05001171 "?Nonce=00a1b2c3&Lifetime=2000");
Davide Pesavento2fdb2742019-07-31 23:03:35 -04001172}
1173
Davide Pesaventoeee3e822016-11-26 19:19:34 +01001174BOOST_AUTO_TEST_SUITE_END() // TestInterest
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -08001175
Alexander Afanasyev90164962014-03-06 08:29:59 +00001176} // namespace tests
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -08001177} // namespace ndn