blob: 482eac85053b1effb4665c4c0045c24ca35c15e3 [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");
85 i1.setCanBePrefix(false);
Davide Pesavento53533942020-03-04 23:10:06 -050086 i1.setNonce(0x01020304);
Davide Pesavento0e0b3892019-07-30 21:05:05 -040087 BOOST_CHECK_EQUAL(i1.isParametersDigestValid(), true);
88
Junxiao Shi899277a2017-07-07 22:12:12 +000089 Block wire1 = i1.wireEncode();
90 BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
91
92 Interest i2(wire1);
93 BOOST_CHECK_EQUAL(i2.getName(), "/local/ndn/prefix");
Davide Pesavento0e0b3892019-07-30 21:05:05 -040094 BOOST_CHECK_EQUAL(i2.getCanBePrefix(), false);
95 BOOST_CHECK_EQUAL(i2.getMustBeFresh(), false);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -040096 BOOST_CHECK_EQUAL(i2.getForwardingHint().empty(), true);
Davide Pesavento835f0272019-09-21 13:18:24 -040097 BOOST_CHECK_EQUAL(i2.hasNonce(), true);
Davide Pesavento53533942020-03-04 23:10:06 -050098 BOOST_CHECK_EQUAL(i2.getNonce(), 0x01020304);
Junxiao Shi899277a2017-07-07 22:12:12 +000099 BOOST_CHECK_EQUAL(i2.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400100 BOOST_CHECK(i2.getHopLimit() == nullopt);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400101 BOOST_CHECK_EQUAL(i2.hasApplicationParameters(), false);
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400102 BOOST_CHECK_EQUAL(i2.getApplicationParameters().isValid(), false);
Eric Newberry6e262f02020-05-29 23:11:25 -0700103 BOOST_CHECK(i2.getSignatureInfo() == nullopt);
104 BOOST_CHECK_EQUAL(i2.getSignatureValue().isValid(), false);
105 BOOST_CHECK_EQUAL(i2.isSigned(), false);
Junxiao Shi899277a2017-07-07 22:12:12 +0000106}
107
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400108BOOST_AUTO_TEST_CASE(WithParameters)
109{
110 const uint8_t WIRE[] = {
111 0x05, 0x44, // Interest
112 0x07, 0x36, // Name
113 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent
114 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent
115 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent
116 0x02, 0x20, // ParametersSha256DigestComponent
117 0xff, 0x91, 0x00, 0xe0, 0x4e, 0xaa, 0xdc, 0xf3, 0x06, 0x74, 0xd9, 0x80,
118 0x26, 0xa0, 0x51, 0xba, 0x25, 0xf5, 0x6b, 0x69, 0xbf, 0xa0, 0x26, 0xdc,
119 0xcc, 0xd7, 0x2c, 0x6e, 0xa0, 0xf7, 0x31, 0x5a,
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700120 0x0a, 0x04, // Nonce
Davide Pesavento53533942020-03-04 23:10:06 -0500121 0x00, 0x00, 0x00, 0x01,
Davide Pesavento9c19a392019-04-06 15:07:54 -0400122 0x24, 0x04, // ApplicationParameters
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400123 0xc0, 0xc1, 0xc2, 0xc3
124 };
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700125
126 Interest i1;
127 i1.setName("/local/ndn/prefix");
128 i1.setCanBePrefix(false);
Davide Pesavento53533942020-03-04 23:10:06 -0500129 i1.setNonce(0x1);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400130 i1.setApplicationParameters("2404C0C1C2C3"_block);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400131 BOOST_CHECK_EQUAL(i1.isParametersDigestValid(), true);
132
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700133 Block wire1 = i1.wireEncode();
134 BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
135
136 Interest i2(wire1);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400137 BOOST_CHECK_EQUAL(i2.getName(),
138 "/local/ndn/prefix/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a");
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700139 BOOST_CHECK_EQUAL(i2.getCanBePrefix(), false);
140 BOOST_CHECK_EQUAL(i2.getMustBeFresh(), false);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400141 BOOST_CHECK_EQUAL(i2.getForwardingHint().empty(), true);
Davide Pesavento835f0272019-09-21 13:18:24 -0400142 BOOST_CHECK_EQUAL(i2.hasNonce(), true);
Davide Pesavento53533942020-03-04 23:10:06 -0500143 BOOST_CHECK_EQUAL(i2.getNonce(), 0x1);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700144 BOOST_CHECK_EQUAL(i2.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400145 BOOST_CHECK(i2.getHopLimit() == nullopt);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400146 BOOST_CHECK_EQUAL(i2.hasApplicationParameters(), true);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400147 BOOST_CHECK_EQUAL(i2.getApplicationParameters(), "2404C0C1C2C3"_block);
Eric Newberry6e262f02020-05-29 23:11:25 -0700148 BOOST_CHECK(i2.getSignatureInfo() == nullopt);
149 BOOST_CHECK_EQUAL(i2.getSignatureValue().isValid(), false);
150 BOOST_CHECK_EQUAL(i2.isSigned(), false);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700151}
152
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400153BOOST_AUTO_TEST_CASE(Full)
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700154{
155 const uint8_t WIRE[] = {
Junxiao Shie4603e12022-01-05 19:12:25 +0000156 0x05, 0x56, // Interest
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400157 0x07, 0x36, // Name
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700158 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent
159 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent
160 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400161 0x02, 0x20, // ParametersSha256DigestComponent
162 0xff, 0x91, 0x00, 0xe0, 0x4e, 0xaa, 0xdc, 0xf3, 0x06, 0x74, 0xd9, 0x80,
163 0x26, 0xa0, 0x51, 0xba, 0x25, 0xf5, 0x6b, 0x69, 0xbf, 0xa0, 0x26, 0xdc,
164 0xcc, 0xd7, 0x2c, 0x6e, 0xa0, 0xf7, 0x31, 0x5a,
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700165 0x21, 0x00, // CanBePrefix
166 0x12, 0x00, // MustBeFresh
Junxiao Shie4603e12022-01-05 19:12:25 +0000167 0x1e, 0x05, // ForwardingHint
168 0x07, 0x03, 0x08, 0x01, 0x48,
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700169 0x0a, 0x04, // Nonce
Davide Pesavento53533942020-03-04 23:10:06 -0500170 0x4c, 0x1e, 0xcb, 0x4a,
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400171 0x0c, 0x02, // InterestLifetime
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700172 0x76, 0xa1,
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400173 0x22, 0x01, // HopLimit
174 0xdc,
Davide Pesavento9c19a392019-04-06 15:07:54 -0400175 0x24, 0x04, // ApplicationParameters
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400176 0xc0, 0xc1, 0xc2, 0xc3
177 };
178
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700179 Interest i1;
180 i1.setName("/local/ndn/prefix");
181 i1.setMustBeFresh(true);
182 i1.setCanBePrefix(true);
Junxiao Shie4603e12022-01-05 19:12:25 +0000183 i1.setForwardingHint({"/H"});
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700184 i1.setNonce(0x4c1ecb4a);
185 i1.setInterestLifetime(30369_ms);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400186 i1.setHopLimit(220);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400187 i1.setApplicationParameters("2404C0C1C2C3"_block);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400188 BOOST_CHECK_EQUAL(i1.isParametersDigestValid(), true);
189
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700190 Block wire1 = i1.wireEncode();
191 BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
192
193 Interest i2(wire1);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400194 BOOST_CHECK_EQUAL(i2.getName(),
195 "/local/ndn/prefix/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a");
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700196 BOOST_CHECK_EQUAL(i2.getCanBePrefix(), true);
197 BOOST_CHECK_EQUAL(i2.getMustBeFresh(), true);
Junxiao Shie4603e12022-01-05 19:12:25 +0000198 BOOST_TEST(i2.getForwardingHint() == std::vector<Name>({"/H"}), boost::test_tools::per_element());
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400199 BOOST_CHECK_EQUAL(i2.hasNonce(), true);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700200 BOOST_CHECK_EQUAL(i2.getNonce(), 0x4c1ecb4a);
201 BOOST_CHECK_EQUAL(i2.getInterestLifetime(), 30369_ms);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400202 BOOST_CHECK_EQUAL(*i2.getHopLimit(), 220);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400203 BOOST_CHECK_EQUAL(i2.getApplicationParameters(), "2404C0C1C2C3"_block);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700204}
205
Eric Newberry6e262f02020-05-29 23:11:25 -0700206BOOST_AUTO_TEST_CASE(Signed)
207{
208 const uint8_t WIRE[] = {
209 0x05, 0x77, // Interest
210 0x07, 0x36, // Name
211 0x08, 0x05, // GenericNameComponent
212 0x6c, 0x6f, 0x63, 0x61, 0x6c,
213 0x08, 0x03, // GenericNameComponent
214 0x6e, 0x64, 0x6e,
215 0x08, 0x06, // GenericNameComponent
216 0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
217 0x02, 0x20, // ParametersSha256DigestComponent
218 0x6f, 0x29, 0x58, 0x60, 0x53, 0xee, 0x9f, 0xcc,
219 0xd8, 0xa4, 0x22, 0x12, 0x29, 0x25, 0x28, 0x7c,
220 0x0a, 0x18, 0x43, 0x5f, 0x40, 0x74, 0xc4, 0x0a,
221 0xbb, 0x0d, 0x5b, 0x30, 0xe4, 0xaa, 0x62, 0x20,
222 0x12, 0x00, // MustBeFresh
223 0x0a, 0x04, // Nonce
224 0x4c, 0x1e, 0xcb, 0x4a,
225 0x24, 0x04, // ApplicationParameters
226 0xc0, 0xc1, 0xc2, 0xc3,
227 0x2c, 0x0d, // InterestSignatureInfo
228 0x1b, 0x01, // SignatureType
229 0x00,
230 0x26, 0x08, // SignatureNonce
231 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
232 0x2e, 0x20, // InterestSignatureValue
233 0x12, 0x47, 0x1a, 0xe0, 0xf8, 0x72, 0x3a, 0xc1,
234 0x15, 0x6c, 0x37, 0x0a, 0x38, 0x71, 0x1e, 0xbe,
235 0xbf, 0x28, 0x17, 0xde, 0x9b, 0x2d, 0xd9, 0x4e,
236 0x9b, 0x7e, 0x62, 0xf1, 0x17, 0xb8, 0x76, 0xc1,
237 };
238
239 SignatureInfo si(tlv::DigestSha256);
240 std::vector<uint8_t> nonce{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
241 si.setNonce(nonce);
242 Block sv("2E20 12471AE0F8723AC1156C370A38711EBEBF2817DE9B2DD94E9B7E62F117B876C1"_block);
243
244 Interest i1(Block(WIRE, sizeof(WIRE)));
245 BOOST_CHECK_EQUAL(i1.getName(),
246 "/local/ndn/prefix/params-sha256=6f29586053ee9fccd8a422122925287c0a18435f4074c40abb0d5b30e4aa6220");
247 BOOST_CHECK_EQUAL(i1.getCanBePrefix(), false);
248 BOOST_CHECK_EQUAL(i1.getMustBeFresh(), true);
249 BOOST_CHECK_EQUAL(i1.hasNonce(), true);
250 BOOST_CHECK_EQUAL(i1.getNonce(), 0x4c1ecb4a);
251 BOOST_CHECK_EQUAL(i1.getSignatureInfo()->getSignatureType(), tlv::DigestSha256);
252 BOOST_CHECK(i1.getSignatureInfo()->getNonce() == nonce);
253 BOOST_CHECK_EQUAL_COLLECTIONS(i1.getSignatureValue().begin(), i1.getSignatureValue().end(),
254 sv.begin(), sv.end());
255 BOOST_CHECK_EQUAL(i1.getApplicationParameters(), "2404C0C1C2C3"_block);
256 BOOST_CHECK_EQUAL(i1.isParametersDigestValid(), true);
257
258 // Reset wire
259 BOOST_CHECK_EQUAL(i1.hasWire(), true);
260 i1.setCanBePrefix(true);
261 i1.setCanBePrefix(false);
262 BOOST_CHECK_EQUAL(i1.hasWire(), false);
263
264 Block wire1 = i1.wireEncode();
265 BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
266
267 Interest i2("/local/ndn/prefix");
268 i2.setCanBePrefix(false);
269 i2.setMustBeFresh(true);
270 i2.setNonce(0x4c1ecb4a);
271 i2.setApplicationParameters("2404C0C1C2C3"_block);
272 i2.setSignatureInfo(si);
273 i2.setSignatureValue(make_shared<Buffer>(sv.value(), sv.value_size()));
274 BOOST_CHECK_EQUAL(i2.isParametersDigestValid(), true);
275
276 Block wire2 = i2.wireEncode();
277 BOOST_CHECK_EQUAL_COLLECTIONS(wire2.begin(), wire2.end(), WIRE, WIRE + sizeof(WIRE));
278}
279
280BOOST_AUTO_TEST_CASE(SignedApplicationElements)
281{
282 const uint8_t WIRE[] = {
283 0x05, 0x8f, // Interest
284 0x07, 0x36, // Name
285 0x08, 0x05, // GenericNameComponent
286 0x6c, 0x6f, 0x63, 0x61, 0x6c,
287 0x08, 0x03, // GenericNameComponent
288 0x6e, 0x64, 0x6e,
289 0x08, 0x06, // GenericNameComponent
290 0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
291 0x02, 0x20, // ParametersSha256DigestComponent
292 0xbc, 0x36, 0x30, 0xa4, 0xd6, 0x5e, 0x0d, 0xb5,
293 0x48, 0x3d, 0xfa, 0x0d, 0x28, 0xb3, 0x31, 0x2f,
294 0xca, 0xc1, 0xd4, 0x41, 0xec, 0x89, 0x61, 0xd4,
295 0x17, 0x5e, 0x61, 0x75, 0x17, 0x78, 0x10, 0x8e,
296 0x12, 0x00, // MustBeFresh
297 0x0a, 0x04, // Nonce
298 0x4c, 0x1e, 0xcb, 0x4a,
299 0x24, 0x04, // ApplicationParameters
300 0xc0, 0xc1, 0xc2, 0xc3,
301 0xfd, 0x01, 0xfe, 0x08, // Application-specific element (Type 510)
302 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80,
303 0x2c, 0x0d, // InterestSignatureInfo
304 0x1b, 0x01, // SignatureType
305 0x00,
306 0x26, 0x08, // SignatureNonce
307 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
308 0x2e, 0x20, // InterestSignatureValue
309 0x12, 0x47, 0x1a, 0xe0, 0xf8, 0x72, 0x3a, 0xc1,
310 0x15, 0x6c, 0x37, 0x0a, 0x38, 0x71, 0x1e, 0xbe,
311 0xbf, 0x28, 0x17, 0xde, 0x9b, 0x2d, 0xd9, 0x4e,
312 0x9b, 0x7e, 0x62, 0xf1, 0x17, 0xb8, 0x76, 0xc1,
313 0xfd, 0x02, 0x00, 0x08, // Application-specific element (Type 512)
314 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88
315 };
316
317 SignatureInfo si(tlv::DigestSha256);
318 std::vector<uint8_t> nonce{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
319 si.setNonce(nonce);
320 Block sv("2E20 12471AE0F8723AC1156C370A38711EBEBF2817DE9B2DD94E9B7E62F117B876C1"_block);
321
322 Interest i1(Block(WIRE, sizeof(WIRE)));
323 BOOST_CHECK_EQUAL(i1.getName(),
324 "/local/ndn/prefix/params-sha256=bc3630a4d65e0db5483dfa0d28b3312fcac1d441ec8961d4175e61751778108e");
325 BOOST_CHECK_EQUAL(i1.getCanBePrefix(), false);
326 BOOST_CHECK_EQUAL(i1.getMustBeFresh(), true);
327 BOOST_CHECK_EQUAL(i1.hasNonce(), true);
328 BOOST_CHECK_EQUAL(i1.getNonce(), 0x4c1ecb4a);
329 BOOST_CHECK_EQUAL(i1.getSignatureInfo()->getSignatureType(), tlv::DigestSha256);
330 BOOST_CHECK(i1.getSignatureInfo()->getNonce() == nonce);
331 BOOST_CHECK_EQUAL_COLLECTIONS(i1.getSignatureValue().begin(), i1.getSignatureValue().end(),
332 sv.begin(), sv.end());
333 BOOST_CHECK_EQUAL(i1.getApplicationParameters(), "2404C0C1C2C3"_block);
334 BOOST_CHECK_EQUAL(i1.isParametersDigestValid(), true);
335
336 // Reset wire
337 BOOST_CHECK_EQUAL(i1.hasWire(), true);
338 i1.setCanBePrefix(true);
339 i1.setCanBePrefix(false);
340 BOOST_CHECK_EQUAL(i1.hasWire(), false);
341
342 Block wire1 = i1.wireEncode();
343 BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
344}
345
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400346BOOST_AUTO_TEST_CASE(MissingApplicationParameters)
347{
348 Interest i;
349 i.setName(Name("/A").appendParametersSha256DigestPlaceholder());
350 i.setCanBePrefix(false);
351 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
Davide Pesavento905d40f2020-06-09 21:33:25 -0400352 BOOST_CHECK_EXCEPTION(i.wireEncode(), tlv::Error, [] (const auto& e) {
353 return e.what() == "Interest without parameters must not have a ParametersSha256DigestComponent"s;
354 });
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400355}
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400356
357BOOST_AUTO_TEST_CASE(MissingParametersSha256DigestComponent)
358{
359 // there's no way to create an Interest that fails this check via programmatic construction,
360 // so we have to decode an invalid Interest and force reencoding
361
362 DisableAutoCheckParametersDigest disabler;
363 Interest i("050F 0703(080149) 0A04F000F000 2402CAFE"_block);
364 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
365 BOOST_CHECK_NO_THROW(i.wireEncode()); // this succeeds because it uses the cached wire encoding
366
Davide Pesavento905d40f2020-06-09 21:33:25 -0400367 // trigger reencoding
368 i.setNonce(42);
369 // now the check fails while attempting to reencode
370 BOOST_CHECK_EXCEPTION(i.wireEncode(), tlv::Error, [] (const auto& e) {
371 return e.what() == "Interest with parameters must have a ParametersSha256DigestComponent"s;
372 });
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400373}
374
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400375BOOST_AUTO_TEST_SUITE_END() // Encode
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400376
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400377class DecodeFixture
Junxiao Shi899277a2017-07-07 22:12:12 +0000378{
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000379protected:
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400380 DecodeFixture()
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000381 {
382 // initialize all elements to non-empty, to verify wireDecode clears them
383 i.setName("/A");
Junxiao Shie4603e12022-01-05 19:12:25 +0000384 i.setForwardingHint({"/F"});
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000385 i.setNonce(0x03d645a8);
386 i.setInterestLifetime(18554_ms);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400387 i.setHopLimit(64);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400388 i.setApplicationParameters("2404A0A1A2A3"_block);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000389 }
Junxiao Shi899277a2017-07-07 22:12:12 +0000390
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000391protected:
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000392 Interest i;
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000393};
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000394
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400395BOOST_FIXTURE_TEST_SUITE(Decode, DecodeFixture)
396
397BOOST_AUTO_TEST_CASE(NotAnInterest)
398{
Davide Pesavento905d40f2020-06-09 21:33:25 -0400399 BOOST_CHECK_EXCEPTION(i.wireDecode("4202CAFE"_block), tlv::Error, [] (const auto& e) {
400 return e.what() == "Expecting Interest element, but TLV has type 66"s;
401 });
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400402}
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000403
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400404BOOST_AUTO_TEST_CASE(NameOnly)
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000405{
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400406 i.wireDecode("0505 0703(080149)"_block);
Davide Pesavento835f0272019-09-21 13:18:24 -0400407 BOOST_CHECK_EQUAL(i.hasWire(), true);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000408 BOOST_CHECK_EQUAL(i.getName(), "/I");
409 BOOST_CHECK_EQUAL(i.getCanBePrefix(), false);
410 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400411 BOOST_CHECK_EQUAL(i.getForwardingHint().empty(), true);
Davide Pesavento835f0272019-09-21 13:18:24 -0400412 BOOST_CHECK_EQUAL(i.hasNonce(), false);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000413 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400414 BOOST_CHECK(i.getHopLimit() == nullopt);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400415 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), false);
416 BOOST_CHECK_EQUAL(i.getApplicationParameters().isValid(), false);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000417
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400418 // modify then re-encode
Davide Pesavento53533942020-03-04 23:10:06 -0500419 i.setNonce(0x957c6554);
Davide Pesavento835f0272019-09-21 13:18:24 -0400420 BOOST_CHECK_EQUAL(i.hasWire(), false);
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400421 BOOST_CHECK_EQUAL(i.wireEncode(), "050B 0703(080149) 0A04957C6554"_block);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000422}
423
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400424BOOST_AUTO_TEST_CASE(NameCanBePrefix)
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000425{
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400426 i.wireDecode("0507 0703(080149) 2100"_block);
Davide Pesavento835f0272019-09-21 13:18:24 -0400427 BOOST_CHECK_EQUAL(i.hasWire(), true);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400428 BOOST_CHECK_EQUAL(i.getName(), "/I");
429 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
430 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
431 BOOST_CHECK_EQUAL(i.getForwardingHint().empty(), true);
Davide Pesavento835f0272019-09-21 13:18:24 -0400432 BOOST_CHECK_EQUAL(i.hasNonce(), false);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400433 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400434 BOOST_CHECK(i.getHopLimit() == nullopt);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400435 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), false);
436 BOOST_CHECK_EQUAL(i.getApplicationParameters().isValid(), false);
437}
438
439BOOST_AUTO_TEST_CASE(FullWithoutParameters)
440{
441 i.wireDecode("0531 0703(080149) "
442 "FC00 2100 FC00 1200 FC00 1E0B(1F09 1E023E15 0703080148) "
443 "FC00 0A044ACB1E4C FC00 0C0276A1 FC00 2201D6 FC00"_block);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000444 BOOST_CHECK_EQUAL(i.getName(), "/I");
445 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
446 BOOST_CHECK_EQUAL(i.getMustBeFresh(), true);
Junxiao Shie4603e12022-01-05 19:12:25 +0000447 BOOST_TEST(i.getForwardingHint() == std::vector<Name>({"/H"}), boost::test_tools::per_element());
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400448 BOOST_CHECK_EQUAL(i.hasNonce(), true);
Davide Pesavento53533942020-03-04 23:10:06 -0500449 BOOST_CHECK_EQUAL(i.getNonce(), 0x4acb1e4c);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000450 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 30369_ms);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400451 BOOST_CHECK_EQUAL(*i.getHopLimit(), 214);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400452 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), false);
453 BOOST_CHECK_EQUAL(i.getApplicationParameters().isValid(), false);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000454
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000455 // encode without modification: retain original wire encoding
Junxiao Shi8b753a22018-10-24 01:51:40 +0000456 BOOST_CHECK_EQUAL(i.wireEncode().value_size(), 49);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000457
Junxiao Shie4603e12022-01-05 19:12:25 +0000458 // modify then re-encode:
459 // * unrecognized elements are discarded;
460 // * ForwardingHint is re-encoded as a sequence of Names
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000461 i.setName("/J");
Junxiao Shi72c0c642018-04-20 15:41:09 +0000462 BOOST_CHECK_EQUAL(i.wireEncode(),
Junxiao Shie4603e12022-01-05 19:12:25 +0000463 "051D 0703(08014A) "
464 "2100 1200 1E05(0703080148) "
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400465 "0A044ACB1E4C 0C0276A1 2201D6"_block);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400466}
467
468BOOST_AUTO_TEST_CASE(FullWithParameters)
469{
470 i.wireDecode("055B 0725(080149 0220F16DB273F40436A852063F864D5072B01EAD53151F5A688EA1560492BEBEDD05) "
471 "FC00 2100 FC00 1200 FC00 1E0B(1F09 1E023E15 0703080148) "
472 "FC00 0A044ACB1E4C FC00 0C0276A1 FC00 2201D6 FC00 2404C0C1C2C3 FC00"_block);
473 BOOST_CHECK_EQUAL(i.getName(),
474 "/I/params-sha256=f16db273f40436a852063f864d5072b01ead53151f5a688ea1560492bebedd05");
475 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
476 BOOST_CHECK_EQUAL(i.getMustBeFresh(), true);
Junxiao Shie4603e12022-01-05 19:12:25 +0000477 BOOST_TEST(i.getForwardingHint() == std::vector<Name>({"/H"}), boost::test_tools::per_element());
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400478 BOOST_CHECK_EQUAL(i.hasNonce(), true);
Davide Pesavento53533942020-03-04 23:10:06 -0500479 BOOST_CHECK_EQUAL(i.getNonce(), 0x4acb1e4c);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400480 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 30369_ms);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400481 BOOST_CHECK_EQUAL(*i.getHopLimit(), 214);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400482 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true);
483 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2404C0C1C2C3"_block);
484
485 // encode without modification: retain original wire encoding
486 BOOST_CHECK_EQUAL(i.wireEncode().value_size(), 91);
487
Junxiao Shie4603e12022-01-05 19:12:25 +0000488 // modify then re-encode:
489 // * unrecognized elements after ApplicationParameters are preserved, the rest are discarded;
490 // * ForwardingHint is re-encoded as a sequence of Names
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400491 i.setName("/J");
492 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
493 BOOST_CHECK_EQUAL(i.wireEncode(),
Junxiao Shie4603e12022-01-05 19:12:25 +0000494 "0547 0725(08014A 0220F16DB273F40436A852063F864D5072B01EAD53151F5A688EA1560492BEBEDD05) "
495 "2100 1200 1E05(0703080148) "
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400496 "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3 FC00"_block);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400497
498 // modify ApplicationParameters: unrecognized elements are preserved
499 i.setApplicationParameters("2402CAFE"_block);
500 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
501 BOOST_CHECK_EQUAL(i.wireEncode(),
Junxiao Shie4603e12022-01-05 19:12:25 +0000502 "0545 0725(08014A 02205FDA67967EE302FC457E41B7D3D51BA6A9379574D193FD88F64954BF16C2927A) "
503 "2100 1200 1E05(0703080148) "
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400504 "0A044ACB1E4C 0C0276A1 2201D6 2402CAFE FC00"_block);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000505}
506
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000507BOOST_AUTO_TEST_CASE(CriticalElementOutOfOrder)
508{
Davide Pesavento905d40f2020-06-09 21:33:25 -0400509 BOOST_CHECK_EXCEPTION(i.wireDecode(
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000510 "0529 2100 0703080149 1200 1E0B(1F09 1E023E15 0703080148) "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500511 "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3"_block),
Davide Pesavento905d40f2020-06-09 21:33:25 -0400512 tlv::Error,
513 [] (const auto& e) { return e.what() == "Name element is missing or out of order"s; });
514 BOOST_CHECK_EXCEPTION(i.wireDecode(
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000515 "0529 0703080149 1200 2100 1E0B(1F09 1E023E15 0703080148) "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500516 "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3"_block),
Davide Pesavento905d40f2020-06-09 21:33:25 -0400517 tlv::Error,
518 [] (const auto& e) { return e.what() == "CanBePrefix element is out of order"s; });
519 BOOST_CHECK_EXCEPTION(i.wireDecode(
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000520 "0529 0703080149 2100 1E0B(1F09 1E023E15 0703080148) 1200 "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500521 "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3"_block),
Davide Pesavento905d40f2020-06-09 21:33:25 -0400522 tlv::Error,
523 [] (const auto& e) { return e.what() == "MustBeFresh element is out of order"s; });
524 BOOST_CHECK_EXCEPTION(i.wireDecode(
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000525 "0529 0703080149 2100 1200 0A044ACB1E4C "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500526 "1E0B(1F09 1E023E15 0703080148) 0C0276A1 2201D6 2404C0C1C2C3"_block),
Davide Pesavento905d40f2020-06-09 21:33:25 -0400527 tlv::Error,
528 [] (const auto& e) { return e.what() == "ForwardingHint element is out of order"s; });
529 BOOST_CHECK_EXCEPTION(i.wireDecode(
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000530 "0529 0703080149 2100 1200 1E0B(1F09 1E023E15 0703080148) "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500531 "0C0276A1 0A044ACB1E4C 2201D6 2404C0C1C2C3"_block),
Davide Pesavento905d40f2020-06-09 21:33:25 -0400532 tlv::Error,
533 [] (const auto& e) { return e.what() == "Nonce element is out of order"s; });
534 BOOST_CHECK_EXCEPTION(i.wireDecode(
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000535 "0529 0703080149 2100 1200 1E0B(1F09 1E023E15 0703080148) "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500536 "0A044ACB1E4C 2201D6 0C0276A1 2404C0C1C2C3"_block),
Davide Pesavento905d40f2020-06-09 21:33:25 -0400537 tlv::Error,
538 [] (const auto& e) { return e.what() == "InterestLifetime element is out of order"s; });
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000539}
540
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500541BOOST_AUTO_TEST_CASE(NonCriticalElementOutOfOrder)
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000542{
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400543 // duplicate HopLimit
544 i.wireDecode("0536 0725(080149 0220FF9100E04EAADCF30674D98026A051BA25F56B69BFA026DCCCD72C6EA0F7315A)"
545 "2201D6 2200 2404C0C1C2C3 22020101"_block);
546 BOOST_CHECK_EQUAL(i.getName(),
547 "/I/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a");
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400548 BOOST_CHECK_EQUAL(*i.getHopLimit(), 214);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400549 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400550 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2404C0C1C2C3"_block);
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500551
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400552 // duplicate ApplicationParameters
553 i.wireDecode("0541 0725(080149 0220FF9100E04EAADCF30674D98026A051BA25F56B69BFA026DCCCD72C6EA0F7315A)"
554 "2100 1200 0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3 2401EE"_block);
555 BOOST_CHECK_EQUAL(i.getName(),
556 "/I/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a");
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400557 BOOST_CHECK_EQUAL(*i.getHopLimit(), 214);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400558 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true);
559 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2404C0C1C2C3"_block);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000560}
561
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400562BOOST_AUTO_TEST_CASE(MissingName)
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000563{
Davide Pesavento905d40f2020-06-09 21:33:25 -0400564 BOOST_CHECK_EXCEPTION(i.wireDecode("0500"_block), tlv::Error,
565 [] (const auto& e) { return e.what() == "Name element is missing or out of order"s; });
566 BOOST_CHECK_EXCEPTION(i.wireDecode("0502 1200"_block), tlv::Error,
567 [] (const auto& e) { return e.what() == "Name element is missing or out of order"s; });
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000568}
569
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400570BOOST_AUTO_TEST_CASE(BadName)
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000571{
Davide Pesavento905d40f2020-06-09 21:33:25 -0400572 BOOST_CHECK_EXCEPTION(i.wireDecode("0502 0700"_block), tlv::Error,
573 [] (const auto& e) { return e.what() == "Name has zero name components"s; });
574 BOOST_CHECK_EXCEPTION(i.wireDecode("054C 074A(080149"
575 "02200000000000000000000000000000000000000000000000000000000000000000"
576 "080132"
577 "02200000000000000000000000000000000000000000000000000000000000000000)"_block),
578 tlv::Error,
579 [] (const auto& e) { return e.what() == "Name has more than one ParametersSha256DigestComponent"s; });
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000580}
581
582BOOST_AUTO_TEST_CASE(BadCanBePrefix)
583{
Davide Pesavento905d40f2020-06-09 21:33:25 -0400584 BOOST_CHECK_EXCEPTION(i.wireDecode("0508 0703080149 210102"_block), tlv::Error,
585 [] (const auto& e) { return e.what() == "CanBePrefix element has non-zero TLV-LENGTH"s; });
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000586}
587
588BOOST_AUTO_TEST_CASE(BadMustBeFresh)
589{
Davide Pesavento905d40f2020-06-09 21:33:25 -0400590 BOOST_CHECK_EXCEPTION(i.wireDecode("0508 0703080149 120102"_block), tlv::Error,
591 [] (const auto& e) { return e.what() == "MustBeFresh element has non-zero TLV-LENGTH"s; });
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000592}
593
594BOOST_AUTO_TEST_CASE(BadNonce)
595{
Davide Pesavento905d40f2020-06-09 21:33:25 -0400596 BOOST_CHECK_EXCEPTION(i.wireDecode("0507 0703080149 0A00"_block), tlv::Error,
597 [] (const auto& e) { return e.what() == "Nonce element is malformed"s; });
598 BOOST_CHECK_EXCEPTION(i.wireDecode("050A 0703080149 0A0304C263"_block), tlv::Error,
599 [] (const auto& e) { return e.what() == "Nonce element is malformed"s; });
600 BOOST_CHECK_EXCEPTION(i.wireDecode("050C 0703080149 0A05EFA420B262"_block), tlv::Error,
601 [] (const auto& e) { return e.what() == "Nonce element is malformed"s; });
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000602}
603
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500604BOOST_AUTO_TEST_CASE(BadHopLimit)
605{
Davide Pesavento905d40f2020-06-09 21:33:25 -0400606 BOOST_CHECK_EXCEPTION(i.wireDecode("0507 0703080149 2200"_block), tlv::Error,
607 [] (const auto& e) { return e.what() == "HopLimit element is malformed"s; });
608 BOOST_CHECK_EXCEPTION(i.wireDecode("0509 0703080149 22021356"_block), tlv::Error,
609 [] (const auto& e) { return e.what() == "HopLimit element is malformed"s; });
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500610}
611
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400612BOOST_AUTO_TEST_CASE(BadParametersDigest)
613{
614 // ApplicationParameters without ParametersSha256DigestComponent
615 Block b1("0509 0703(080149) 2402CAFE"_block);
616 // ParametersSha256DigestComponent without ApplicationParameters
617 Block b2("0527 0725(080149 0220E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855)"_block);
618 // digest mismatch
619 Block b3("052B 0725(080149 02200000000000000000000000000000000000000000000000000000000000000000) "
620 "2402CAFE"_block);
621
622 BOOST_CHECK_THROW(i.wireDecode(b1), tlv::Error);
623 BOOST_CHECK_THROW(i.wireDecode(b2), tlv::Error);
624 BOOST_CHECK_THROW(i.wireDecode(b3), tlv::Error);
625
626 DisableAutoCheckParametersDigest disabler;
627 BOOST_CHECK_NO_THROW(i.wireDecode(b1));
628 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
629 BOOST_CHECK_NO_THROW(i.wireDecode(b2));
630 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
631 BOOST_CHECK_NO_THROW(i.wireDecode(b3));
632 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
633}
634
Junxiao Shi8b753a22018-10-24 01:51:40 +0000635BOOST_AUTO_TEST_CASE(UnrecognizedNonCriticalElementBeforeName)
636{
Davide Pesavento905d40f2020-06-09 21:33:25 -0400637 BOOST_CHECK_EXCEPTION(i.wireDecode("0507 FC00 0703080149"_block), tlv::Error,
638 [] (const auto& e) { return e.what() == "Name element is missing or out of order"s; });
Junxiao Shi8b753a22018-10-24 01:51:40 +0000639}
640
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000641BOOST_AUTO_TEST_CASE(UnrecognizedCriticalElement)
642{
Davide Pesavento905d40f2020-06-09 21:33:25 -0400643 BOOST_CHECK_EXCEPTION(i.wireDecode("0507 0703080149 FB00"_block), tlv::Error,
644 [] (const auto& e) { return e.what() == "Unrecognized element of critical type 251"s; });
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400645 // v0.2 packet with Selectors
Davide Pesavento905d40f2020-06-09 21:33:25 -0400646 BOOST_CHECK_EXCEPTION(i.wireDecode("0510 0703080149 09030D0101 0A0401000000"_block), tlv::Error,
647 [] (const auto& e) { return e.what() == "Unrecognized element of critical type 9"s; });
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000648}
649
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400650BOOST_AUTO_TEST_SUITE_END() // Decode
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000651
Junxiao Shi899277a2017-07-07 22:12:12 +0000652BOOST_AUTO_TEST_CASE(MatchesData)
653{
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000654 auto interest = makeInterest("/A");
Junxiao Shi899277a2017-07-07 22:12:12 +0000655
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000656 auto data = makeData("/A");
657 BOOST_CHECK_EQUAL(interest->matchesData(*data), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000658
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000659 data->setName("/A/D");
660 BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // violates CanBePrefix
Junxiao Shi899277a2017-07-07 22:12:12 +0000661
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000662 interest->setCanBePrefix(true);
663 BOOST_CHECK_EQUAL(interest->matchesData(*data), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000664
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000665 interest->setMustBeFresh(true);
666 BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // violates MustBeFresh
Junxiao Shi899277a2017-07-07 22:12:12 +0000667
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000668 data->setFreshnessPeriod(1_s);
669 BOOST_CHECK_EQUAL(interest->matchesData(*data), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000670
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000671 data->setName("/H/I");
672 BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // Name does not match
Junxiao Shi899277a2017-07-07 22:12:12 +0000673
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000674 data->wireEncode();
675 interest = makeInterest(data->getFullName());
676 BOOST_CHECK_EQUAL(interest->matchesData(*data), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000677
Davide Pesavento53533942020-03-04 23:10:06 -0500678 setNameComponent(*interest, -1,
679 name::Component::fromEscapedString("sha256digest=00000000000000000000000000"
680 "00000000000000000000000000000000000000"));
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000681 BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // violates implicit digest
Junxiao Shi899277a2017-07-07 22:12:12 +0000682}
683
684BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(MatchesInterest, 1)
685BOOST_AUTO_TEST_CASE(MatchesInterest)
686{
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400687 Interest interest;
688 interest.setName("/A")
689 .setCanBePrefix(true)
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000690 .setMustBeFresh(true)
Junxiao Shie4603e12022-01-05 19:12:25 +0000691 .setForwardingHint({"/H"})
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000692 .setNonce(2228)
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400693 .setInterestLifetime(5_s)
694 .setHopLimit(90);
Junxiao Shi899277a2017-07-07 22:12:12 +0000695
696 Interest other;
697 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false);
698
699 other.setName(interest.getName());
700 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false);
701
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000702 other.setCanBePrefix(interest.getCanBePrefix());
703 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false);
704
705 other.setMustBeFresh(interest.getMustBeFresh());
Junxiao Shi899277a2017-07-07 22:12:12 +0000706 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false); // will match until #3162 implemented
707
Junxiao Shie4603e12022-01-05 19:12:25 +0000708 auto fh = interest.getForwardingHint();
709 other.setForwardingHint(std::vector<Name>(fh.begin(), fh.end()));
Junxiao Shi899277a2017-07-07 22:12:12 +0000710 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
711
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000712 other.setNonce(9336);
Junxiao Shi899277a2017-07-07 22:12:12 +0000713 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
714
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000715 other.setInterestLifetime(3_s);
Junxiao Shi899277a2017-07-07 22:12:12 +0000716 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400717
718 other.setHopLimit(31);
719 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000720}
721
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400722BOOST_AUTO_TEST_CASE(SetName)
723{
724 Interest i;
725 BOOST_CHECK_EQUAL(i.getName(), "/");
726 i.setName("/A/B");
727 BOOST_CHECK_EQUAL(i.getName(), "/A/B");
728 i.setName("/I/params-sha256=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
729 BOOST_CHECK_EQUAL(i.getName(),
730 "/I/params-sha256=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
731 BOOST_CHECK_THROW(i.setName("/I"
732 "/params-sha256=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
733 "/params-sha256=0000000000000000000000000000000000000000000000000000000000000000"),
734 std::invalid_argument);
735}
Junxiao Shi899277a2017-07-07 22:12:12 +0000736
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400737BOOST_AUTO_TEST_CASE(SetCanBePrefix)
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000738{
739 Interest i;
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000740 BOOST_CHECK_EQUAL(i.getCanBePrefix(), false);
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000741 i.setCanBePrefix(true);
742 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
Davide Pesavento478d3382021-03-17 12:46:08 -0400743 i.setCanBePrefix(false);
744 BOOST_CHECK_EQUAL(i.getCanBePrefix(), false);
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000745}
746
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400747BOOST_AUTO_TEST_CASE(SetMustBeFresh)
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000748{
749 Interest i;
750 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
751 i.setMustBeFresh(true);
752 BOOST_CHECK_EQUAL(i.getMustBeFresh(), true);
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000753 i.setMustBeFresh(false);
754 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000755}
756
Junxiao Shi899277a2017-07-07 22:12:12 +0000757BOOST_AUTO_TEST_CASE(GetNonce)
758{
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000759 unique_ptr<Interest> i1, i2;
Davide Pesavento53533942020-03-04 23:10:06 -0500760 Interest::Nonce nonce1(0), nonce2(0);
Junxiao Shi899277a2017-07-07 22:12:12 +0000761
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000762 // getNonce automatically assigns a random Nonce.
763 // 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 -0500764 // identical Nonces in a row.
Junxiao Shi899277a2017-07-07 22:12:12 +0000765 int nIterations = 0;
766 do {
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000767 i1 = make_unique<Interest>();
768 nonce1 = i1->getNonce();
769 i2 = make_unique<Interest>();
770 nonce2 = i2->getNonce();
Junxiao Shi899277a2017-07-07 22:12:12 +0000771 }
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000772 while (nonce1 == nonce2 && ++nIterations < 100);
Davide Pesavento53533942020-03-04 23:10:06 -0500773
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000774 BOOST_CHECK_NE(nonce1, nonce2);
775 BOOST_CHECK(i1->hasNonce());
776 BOOST_CHECK(i2->hasNonce());
Junxiao Shi899277a2017-07-07 22:12:12 +0000777
778 // Once a Nonce is assigned, it should not change.
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000779 BOOST_CHECK_EQUAL(i1->getNonce(), nonce1);
Davide Pesavento53533942020-03-04 23:10:06 -0500780 BOOST_CHECK_EQUAL(i2->getNonce(), nonce2);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000781}
782
783BOOST_AUTO_TEST_CASE(SetNonce)
784{
785 Interest i1("/A");
Junxiao Shib55e5d32018-07-18 13:32:00 -0600786 i1.setCanBePrefix(false);
Davide Pesavento53533942020-03-04 23:10:06 -0500787 BOOST_CHECK(!i1.hasNonce());
788
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000789 i1.setNonce(1);
790 i1.wireEncode();
Davide Pesavento53533942020-03-04 23:10:06 -0500791 BOOST_CHECK(i1.hasNonce());
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000792 BOOST_CHECK_EQUAL(i1.getNonce(), 1);
793
794 Interest i2(i1);
Davide Pesavento53533942020-03-04 23:10:06 -0500795 BOOST_CHECK(i2.hasNonce());
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000796 BOOST_CHECK_EQUAL(i2.getNonce(), 1);
797
798 i2.setNonce(2);
799 BOOST_CHECK_EQUAL(i2.getNonce(), 2);
Davide Pesavento53533942020-03-04 23:10:06 -0500800 BOOST_CHECK_EQUAL(i1.getNonce(), 1); // should not affect i1's Nonce (Bug #4168)
801
802 i2.setNonce(nullopt);
803 BOOST_CHECK(!i2.hasNonce());
Junxiao Shi899277a2017-07-07 22:12:12 +0000804}
805
806BOOST_AUTO_TEST_CASE(RefreshNonce)
807{
808 Interest i;
809 BOOST_CHECK(!i.hasNonce());
810 i.refreshNonce();
811 BOOST_CHECK(!i.hasNonce());
812
813 i.setNonce(1);
814 BOOST_CHECK(i.hasNonce());
815 i.refreshNonce();
816 BOOST_CHECK(i.hasNonce());
817 BOOST_CHECK_NE(i.getNonce(), 1);
818}
819
Davide Pesavento53533942020-03-04 23:10:06 -0500820BOOST_AUTO_TEST_CASE(NonceConversions)
821{
822 Interest i;
823 i.setCanBePrefix(false);
824
825 // 4-arg constructor
826 Interest::Nonce n1(1, 2, 3, 4);
827 i.setNonce(n1);
828 BOOST_CHECK_EQUAL(i.getNonce(), 0x01020304);
829
830 // 4-arg constructor + assignment
831 n1 = {0xf, 0xe, 0xd, 0xc};
832 i.setNonce(n1);
833 BOOST_CHECK_EQUAL(i.getNonce(), 0x0f0e0d0c);
834
835 // 1-arg constructor + assignment (implicit conversion)
836 Interest::Nonce n2;
837 n2 = 42;
838 BOOST_CHECK_NE(n1, n2);
839 i.setNonce(n2);
840 n2 = 21; // should not affect i's Nonce
841 BOOST_CHECK_EQUAL(i.getNonce(), 42);
842 BOOST_CHECK_EQUAL(i.toUri(), "/?Nonce=0000002a"); // stored in big-endian
843}
844
Junxiao Shi899277a2017-07-07 22:12:12 +0000845BOOST_AUTO_TEST_CASE(SetInterestLifetime)
846{
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500847 BOOST_CHECK_THROW(Interest("/A", -1_ms), std::invalid_argument);
Davide Pesavento0f830802018-01-16 23:58:58 -0500848 BOOST_CHECK_NO_THROW(Interest("/A", 0_ms));
Junxiao Shi899277a2017-07-07 22:12:12 +0000849
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400850 Interest i;
Junxiao Shi899277a2017-07-07 22:12:12 +0000851 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500852 BOOST_CHECK_THROW(i.setInterestLifetime(-1_ms), std::invalid_argument);
Junxiao Shi899277a2017-07-07 22:12:12 +0000853 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesavento0f830802018-01-16 23:58:58 -0500854 i.setInterestLifetime(0_ms);
855 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 0_ms);
856 i.setInterestLifetime(1_ms);
857 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 1_ms);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400858
859 i = Interest("/B", 15_s);
860 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 15_s);
861}
862
863BOOST_AUTO_TEST_CASE(SetHopLimit)
864{
865 Interest i;
866 BOOST_CHECK(i.getHopLimit() == nullopt);
867 i.setHopLimit(42);
868 BOOST_CHECK(i.getHopLimit() == 42);
869 i.setHopLimit(nullopt);
870 BOOST_CHECK(i.getHopLimit() == nullopt);
Junxiao Shi899277a2017-07-07 22:12:12 +0000871}
872
Davide Pesavento9c19a392019-04-06 15:07:54 -0400873BOOST_AUTO_TEST_CASE(SetApplicationParameters)
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700874{
875 const uint8_t PARAMETERS1[] = {0xc1};
876 const uint8_t PARAMETERS2[] = {0xc2};
877
878 Interest i;
Davide Pesavento9c19a392019-04-06 15:07:54 -0400879 BOOST_CHECK(!i.hasApplicationParameters());
880 i.setApplicationParameters("2400"_block);
881 BOOST_CHECK(i.hasApplicationParameters());
882 i.unsetApplicationParameters();
883 BOOST_CHECK(!i.hasApplicationParameters());
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700884
Davide Pesavento38912442019-04-06 22:03:39 -0400885 // Block overload
Davide Pesavento38912442019-04-06 22:03:39 -0400886 i.setApplicationParameters("2401C0"_block);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400887 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2401C0"_block);
Davide Pesavento38912442019-04-06 22:03:39 -0400888 i.setApplicationParameters("8001C1"_block);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400889 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "24038001C1"_block);
Davide Pesavento81bd6962020-06-17 16:03:23 -0400890 BOOST_CHECK_THROW(i.setApplicationParameters(Block{}), std::invalid_argument);
Davide Pesavento38912442019-04-06 22:03:39 -0400891
892 // raw buffer+size overload
893 i.setApplicationParameters(PARAMETERS1, sizeof(PARAMETERS1));
894 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2401C1"_block);
895 i.setApplicationParameters(nullptr, 0);
896 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2400"_block);
897 BOOST_CHECK_THROW(i.setApplicationParameters(nullptr, 42), std::invalid_argument);
898
899 // ConstBufferPtr overload
900 i.setApplicationParameters(make_shared<Buffer>(PARAMETERS2, sizeof(PARAMETERS2)));
901 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2401C2"_block);
902 i.setApplicationParameters(make_shared<Buffer>());
903 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2400"_block);
904 BOOST_CHECK_THROW(i.setApplicationParameters(nullptr), std::invalid_argument);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700905}
906
Eric Newberry6e262f02020-05-29 23:11:25 -0700907BOOST_AUTO_TEST_CASE(SetSignature)
908{
909 Interest i;
910 i.setCanBePrefix(false);
911 BOOST_CHECK(i.getSignatureInfo() == nullopt);
912 BOOST_CHECK_EQUAL(i.isSigned(), false);
913
914 // Throws because attempting to set InterestSignatureValue without set InterestSignatureInfo
915 Block sv1("2E04 01020304"_block);
916 auto svBuffer1 = make_shared<Buffer>(sv1.value(), sv1.value_size());
917 BOOST_CHECK_THROW(i.setSignatureValue(svBuffer1), tlv::Error);
918
919 // Simple set/get case for InterestSignatureInfo (no prior set)
920 SignatureInfo si1(tlv::SignatureSha256WithEcdsa);
921 i.setSignatureInfo(si1);
922 BOOST_CHECK(i.getSignatureInfo() == si1);
923 BOOST_CHECK_EQUAL(i.isSigned(), false);
924
925 // Simple set/get case for InterestSignatureValue (no prior set)
926 BOOST_CHECK_EQUAL(i.getSignatureValue().isValid(), false);
927 i.setSignatureValue(svBuffer1);
928 BOOST_CHECK_EQUAL(i.getSignatureValue(), sv1);
929 BOOST_CHECK_EQUAL(i.isSigned(), true);
930
931 // Throws because attempting to set InterestSignatureValue to nullptr
932 BOOST_CHECK_THROW(i.setSignatureValue(nullptr), std::invalid_argument);
933 BOOST_CHECK_EQUAL(i.getSignatureValue(), sv1);
934 BOOST_CHECK_EQUAL(i.isSigned(), true);
935
936 // Ensure that wire is not reset if specified InterestSignatureInfo is same
937 i.wireEncode();
938 BOOST_CHECK_EQUAL(i.hasWire(), true);
939 i.setSignatureInfo(si1);
940 BOOST_CHECK_EQUAL(i.hasWire(), true);
941 BOOST_CHECK(i.getSignatureInfo() == si1);
942 BOOST_CHECK_EQUAL(i.getSignatureValue(), sv1);
943 BOOST_CHECK_EQUAL(i.isSigned(), true);
944
945 // Ensure that wire is reset if specified InterestSignatureInfo is different
946 i.wireEncode();
947 BOOST_CHECK_EQUAL(i.hasWire(), true);
948 SignatureInfo si2(tlv::SignatureSha256WithRsa);
949 i.setSignatureInfo(si2);
950 BOOST_CHECK_EQUAL(i.hasWire(), false);
951 BOOST_CHECK(i.getSignatureInfo() == si2);
952 BOOST_CHECK_EQUAL(i.getSignatureValue(), sv1);
953 BOOST_CHECK_EQUAL(i.isSigned(), true);
954
955 // Ensure that wire is not reset if specified InterestSignatureValue is same
956 i.wireEncode();
957 BOOST_CHECK_EQUAL(i.hasWire(), true);
958 i.setSignatureValue(svBuffer1);
959 BOOST_CHECK_EQUAL(i.hasWire(), true);
960 BOOST_CHECK(i.getSignatureInfo() == si2);
961 BOOST_CHECK_EQUAL(i.getSignatureValue(), sv1);
962 BOOST_CHECK_EQUAL(i.isSigned(), true);
963
964 // Ensure that wire is reset if specified InterestSignatureValue is different
965 i.wireEncode();
966 BOOST_CHECK_EQUAL(i.hasWire(), true);
967 Block sv2("2E04 99887766"_block);
968 auto svBuffer2 = make_shared<Buffer>(sv2.value(), sv2.value_size());
969 i.setSignatureValue(svBuffer2);
970 BOOST_CHECK_EQUAL(i.hasWire(), false);
971 BOOST_CHECK(i.getSignatureInfo() == si2);
972 BOOST_CHECK_EQUAL(i.getSignatureValue(), sv2);
973 BOOST_CHECK_EQUAL(i.isSigned(), true);
974}
975
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400976BOOST_AUTO_TEST_CASE(ParametersSha256DigestComponent)
977{
978 Interest i("/I");
979 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
980
981 i.setApplicationParameters("2404C0C1C2C3"_block); // auto-appends ParametersSha256DigestComponent
982 BOOST_CHECK_EQUAL(i.getName(),
983 "/I/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a");
984 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
985
986 i.setApplicationParameters(nullptr, 0); // updates ParametersSha256DigestComponent
987 BOOST_CHECK_EQUAL(i.getName(),
988 "/I/params-sha256=33b67cb5385ceddad93d0ee960679041613bed34b8b4a5e6362fe7539ba2d3ce");
989 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true);
990 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
991
992 i.unsetApplicationParameters(); // removes ParametersSha256DigestComponent
993 BOOST_CHECK_EQUAL(i.getName(), "/I");
994 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
995
996 i.setName(Name("/P").appendParametersSha256DigestPlaceholder().append("Q"));
997 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), false);
998 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
999
1000 i.unsetApplicationParameters(); // removes ParametersSha256DigestComponent
1001 BOOST_CHECK_EQUAL(i.getName(), "/P/Q");
1002 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
1003
1004 i.setName(Name("/P").appendParametersSha256DigestPlaceholder().append("Q"));
1005 i.setApplicationParameters("2404C0C1C2C3"_block); // updates ParametersSha256DigestComponent
1006 BOOST_CHECK_EQUAL(i.getName(),
1007 "/P/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a/Q");
1008 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
1009
1010 i.setName("/A/B/C"); // auto-appends ParametersSha256DigestComponent
1011 BOOST_CHECK_EQUAL(i.getName(),
1012 "/A/B/C/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a");
1013 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true);
1014 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
Eric Newberry6e262f02020-05-29 23:11:25 -07001015
1016 SignatureInfo si(tlv::SignatureSha256WithEcdsa);
1017 i.setSignatureInfo(si); // updates ParametersSha256DigestComponent
1018 BOOST_CHECK_EQUAL(i.getName(),
1019 "/A/B/C/params-sha256=6400cae1730c15fd7854b26be05794d53685423c94bc61e59c49bd640d646ae8");
1020 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
1021 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2404 C0C1C2C3"_block);
1022 BOOST_CHECK(i.getSignatureInfo() == si);
1023
1024 i.unsetApplicationParameters(); // removes ParametersSha256DigestComponent and InterestSignatureInfo
1025 BOOST_CHECK(i.getSignatureInfo() == nullopt);
1026 BOOST_CHECK_EQUAL(i.getSignatureValue().isValid(), false);
1027 BOOST_CHECK_EQUAL(i.getName(), "/A/B/C");
1028
1029 i.setSignatureInfo(si); // auto-adds an empty ApplicationParameters element
1030 BOOST_CHECK_EQUAL(i.getName(),
1031 "/A/B/C/params-sha256=d2ac0eb1f60f60ab206fb80bf1d0f73cfef353bbec43ba6ea626117f671ca3bb");
1032 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
1033 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2400"_block);
1034 BOOST_CHECK(i.getSignatureInfo() == si);
1035
1036 Block sv("2E04 01020304"_block);
1037 i.setSignatureValue(make_shared<Buffer>(sv.value(), sv.value_size())); // updates ParametersDigestSha256Component
1038 BOOST_CHECK_EQUAL(i.getName(),
1039 "/A/B/C/params-sha256=f649845ef944638390d1c689e2f0618ea02e471eff236110cbeb822d5932d342");
1040 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
1041 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2400"_block);
1042 BOOST_CHECK(i.getSignatureInfo() == si);
1043 BOOST_CHECK_EQUAL(i.getSignatureValue(), sv);
1044
1045 i.setApplicationParameters("2404C0C1C2C3"_block); // updates ParametersSha256DigestComponent
1046 BOOST_CHECK_EQUAL(i.getName(),
1047 "/A/B/C/params-sha256=c5d7e567e6b251ddf36f7a6dbed95235b2d4a0b36215bb0f3cc403ac64ad0284");
1048 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
1049 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2404 C0C1C2C3"_block);
1050 BOOST_CHECK(i.getSignatureInfo() == si);
1051 BOOST_CHECK_EQUAL(i.getSignatureValue(), sv);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -04001052}
Junxiao Shi899277a2017-07-07 22:12:12 +00001053
Eric Newberryb74bbda2020-06-18 19:33:58 -07001054BOOST_AUTO_TEST_CASE(ExtractSignedRanges)
1055{
1056 Interest i1;
1057 i1.setCanBePrefix(false);
1058 BOOST_CHECK_EXCEPTION(i1.extractSignedRanges(), tlv::Error, [] (const auto& e) {
Eric Newberryb74bbda2020-06-18 19:33:58 -07001059 return e.what() == "Name has zero name components"s;
1060 });
1061 i1.setName("/test/prefix");
1062 i1.setNonce(0x01020304);
1063 SignatureInfo sigInfo(tlv::DigestSha256);
1064 i1.setSignatureInfo(sigInfo);
1065
1066 // Test with previously unsigned Interest (no InterestSignatureValue)
1067 auto ranges1 = i1.extractSignedRanges();
1068 BOOST_REQUIRE_EQUAL(ranges1.size(), 2);
1069 const Block& wire1 = i1.wireEncode();
1070 // Ensure Name range captured properly
1071 Block nameWithoutDigest1 = i1.getName().getPrefix(-1).wireEncode();
Davide Pesavento765abc92021-12-27 00:44:04 -05001072 BOOST_CHECK_EQUAL_COLLECTIONS(ranges1.front().begin(), ranges1.front().end(),
Eric Newberryb74bbda2020-06-18 19:33:58 -07001073 nameWithoutDigest1.value_begin(), nameWithoutDigest1.value_end());
1074 // Ensure parameters range captured properly
1075 const auto& appParamsWire1 = wire1.find(tlv::ApplicationParameters);
1076 BOOST_REQUIRE(appParamsWire1 != wire1.elements_end());
Davide Pesavento765abc92021-12-27 00:44:04 -05001077 BOOST_CHECK_EQUAL_COLLECTIONS(ranges1.back().begin(), ranges1.back().end(),
Eric Newberryb74bbda2020-06-18 19:33:58 -07001078 appParamsWire1->begin(), wire1.end());
1079
1080 // Test with Interest with existing InterestSignatureValue
1081 auto sigValue = make_shared<Buffer>();
1082 i1.setSignatureValue(sigValue);
1083 auto ranges2 = i1.extractSignedRanges();
1084 BOOST_REQUIRE_EQUAL(ranges2.size(), 2);
1085 const auto& wire2 = i1.wireEncode();
1086 // Ensure Name range captured properly
1087 Block nameWithoutDigest2 = i1.getName().getPrefix(-1).wireEncode();
Davide Pesavento765abc92021-12-27 00:44:04 -05001088 BOOST_CHECK_EQUAL_COLLECTIONS(ranges2.front().begin(), ranges2.front().end(),
Eric Newberryb74bbda2020-06-18 19:33:58 -07001089 nameWithoutDigest2.value_begin(), nameWithoutDigest2.value_end());
1090 // Ensure parameters range captured properly
1091 const auto& appParamsWire2 = wire2.find(tlv::ApplicationParameters);
1092 BOOST_REQUIRE(appParamsWire2 != wire2.elements_end());
1093 const auto& sigValueWire2 = wire2.find(tlv::InterestSignatureValue);
1094 BOOST_REQUIRE(sigValueWire2 != wire2.elements_end());
Davide Pesavento765abc92021-12-27 00:44:04 -05001095 BOOST_CHECK_EQUAL_COLLECTIONS(ranges2.back().begin(), ranges2.back().end(),
Eric Newberryb74bbda2020-06-18 19:33:58 -07001096 appParamsWire2->begin(), sigValueWire2->begin());
1097
1098 // Test with decoded Interest
1099 const uint8_t WIRE[] = {
1100 0x05, 0x6f, // Interest
1101 0x07, 0x2e, // Name
1102 0x08, 0x04, // GenericNameComponent
1103 0x61, 0x62, 0x63, 0x64,
1104 0x08, 0x04, // GenericNameComponent
1105 0x65, 0x66, 0x67, 0x68,
1106 0x02, 0x20, // ParametersSha256DigestComponent
1107 0x6f, 0x29, 0x58, 0x60, 0x53, 0xee, 0x9f, 0xcc,
1108 0xd8, 0xa4, 0x22, 0x12, 0x29, 0x25, 0x28, 0x7c,
1109 0x0a, 0x18, 0x43, 0x5f, 0x40, 0x74, 0xc4, 0x0a,
1110 0xbb, 0x0d, 0x5b, 0x30, 0xe4, 0xaa, 0x62, 0x20,
1111 0x12, 0x00, // MustBeFresh
1112 0x0a, 0x04, // Nonce
1113 0x4c, 0x1e, 0xcb, 0x4a,
1114 0x24, 0x04, // ApplicationParameters
1115 0xc0, 0xc1, 0xc2, 0xc3,
1116 0x2c, 0x0d, // InterestSignatureInfo
1117 0x1b, 0x01, // SignatureType
1118 0x00,
1119 0x26, 0x08, // SignatureNonce
1120 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
1121 0x2e, 0x20, // InterestSignatureValue
1122 0x12, 0x47, 0x1a, 0xe0, 0xf8, 0x72, 0x3a, 0xc1,
1123 0x15, 0x6c, 0x37, 0x0a, 0x38, 0x71, 0x1e, 0xbe,
1124 0xbf, 0x28, 0x17, 0xde, 0x9b, 0x2d, 0xd9, 0x4e,
1125 0x9b, 0x7e, 0x62, 0xf1, 0x17, 0xb8, 0x76, 0xc1,
1126 };
1127 Block wire3(WIRE, sizeof(WIRE));
1128 Interest i2(wire3);
1129 auto ranges3 = i2.extractSignedRanges();
1130 BOOST_REQUIRE_EQUAL(ranges3.size(), 2);
1131 // Ensure Name range captured properly
Davide Pesavento765abc92021-12-27 00:44:04 -05001132 BOOST_CHECK_EQUAL_COLLECTIONS(ranges3.front().begin(), ranges3.front().end(), &WIRE[4], &WIRE[16]);
Eric Newberryb74bbda2020-06-18 19:33:58 -07001133 // Ensure parameters range captured properly
Davide Pesavento765abc92021-12-27 00:44:04 -05001134 BOOST_CHECK_EQUAL_COLLECTIONS(ranges3.back().begin(), ranges3.back().end(), &WIRE[58], &WIRE[79]);
Eric Newberryb74bbda2020-06-18 19:33:58 -07001135
1136 // Test failure with missing ParametersSha256DigestComponent
1137 Interest i3("/a");
1138 i3.setCanBePrefix(false);
1139 BOOST_CHECK_EXCEPTION(i3.extractSignedRanges(), tlv::Error, [] (const auto& e) {
1140 return e.what() == "Interest Name must end with a ParametersSha256DigestComponent"s;
1141 });
1142
1143 // Test failure with missing InterestSignatureInfo
Davide Pesavento81bd6962020-06-17 16:03:23 -04001144 i3.setApplicationParameters(nullptr, 0);
Eric Newberryb74bbda2020-06-18 19:33:58 -07001145 BOOST_CHECK_EXCEPTION(i3.extractSignedRanges(), tlv::Error, [] (const auto& e) {
1146 return e.what() == "Interest missing InterestSignatureInfo"s;
1147 });
1148}
1149
Davide Pesavento2fdb2742019-07-31 23:03:35 -04001150BOOST_AUTO_TEST_CASE(ToUri)
1151{
1152 Interest i;
1153 i.setCanBePrefix(false);
1154 BOOST_CHECK_EQUAL(i.toUri(), "/");
1155
1156 i.setName("/foo");
1157 BOOST_CHECK_EQUAL(i.toUri(), "/foo");
1158
1159 i.setCanBePrefix(true);
1160 BOOST_CHECK_EQUAL(i.toUri(), "/foo?CanBePrefix");
1161
1162 i.setMustBeFresh(true);
1163 BOOST_CHECK_EQUAL(i.toUri(), "/foo?CanBePrefix&MustBeFresh");
1164
Davide Pesavento53533942020-03-04 23:10:06 -05001165 i.setNonce(0xa1b2c3);
1166 BOOST_CHECK_EQUAL(i.toUri(), "/foo?CanBePrefix&MustBeFresh&Nonce=00a1b2c3");
Davide Pesavento2fdb2742019-07-31 23:03:35 -04001167
1168 i.setInterestLifetime(2_s);
Davide Pesavento53533942020-03-04 23:10:06 -05001169 BOOST_CHECK_EQUAL(i.toUri(), "/foo?CanBePrefix&MustBeFresh&Nonce=00a1b2c3&Lifetime=2000");
Davide Pesavento2fdb2742019-07-31 23:03:35 -04001170
1171 i.setHopLimit(18);
Davide Pesavento53533942020-03-04 23:10:06 -05001172 BOOST_CHECK_EQUAL(i.toUri(), "/foo?CanBePrefix&MustBeFresh&Nonce=00a1b2c3&Lifetime=2000&HopLimit=18");
Davide Pesavento2fdb2742019-07-31 23:03:35 -04001173
1174 i.setCanBePrefix(false);
1175 i.setMustBeFresh(false);
1176 i.setHopLimit(nullopt);
1177 i.setApplicationParameters("2402CAFE"_block);
1178 BOOST_CHECK_EQUAL(i.toUri(),
1179 "/foo/params-sha256=8621f5e8321f04104640c8d02877d7c5142cad6e203c5effda1783b1a0e476d6"
Davide Pesavento53533942020-03-04 23:10:06 -05001180 "?Nonce=00a1b2c3&Lifetime=2000");
Davide Pesavento2fdb2742019-07-31 23:03:35 -04001181}
1182
Davide Pesaventoeee3e822016-11-26 19:19:34 +01001183BOOST_AUTO_TEST_SUITE_END() // TestInterest
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -08001184
Alexander Afanasyev90164962014-03-06 08:29:59 +00001185} // namespace tests
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -08001186} // namespace ndn