blob: 73afe60bea96a03d8ca32c949487ceb14ab6b230 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi899277a2017-07-07 22:12:12 +00002/*
Davide Pesaventof6b45892023-03-13 15:00:51 -04003 * Copyright (c) 2013-2023 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
Davide Pesavento152ef442023-04-22 02:02:29 -040030BOOST_CONCEPT_ASSERT((WireEncodable<Interest>));
31BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<Interest>));
32BOOST_CONCEPT_ASSERT((WireDecodable<Interest>));
33static_assert(std::is_convertible_v<Interest::Error*, tlv::Error*>,
34 "Interest::Error must inherit from tlv::Error");
35
Junxiao Shi899277a2017-07-07 22:12:12 +000036BOOST_AUTO_TEST_SUITE(TestInterest)
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080037
Davide Pesaventoadc9aa22019-06-30 19:00:20 -040038class DisableAutoCheckParametersDigest
39{
40public:
41 DisableAutoCheckParametersDigest()
42 : m_saved(Interest::getAutoCheckParametersDigest())
43 {
44 Interest::setAutoCheckParametersDigest(false);
45 }
46
47 ~DisableAutoCheckParametersDigest()
48 {
49 Interest::setAutoCheckParametersDigest(m_saved);
50 }
51
52private:
53 bool m_saved;
54};
Junxiao Shi899277a2017-07-07 22:12:12 +000055
56BOOST_AUTO_TEST_CASE(DefaultConstructor)
57{
58 Interest i;
Davide Pesaventoadc9aa22019-06-30 19:00:20 -040059 BOOST_CHECK_EQUAL(i.hasWire(), false);
Junxiao Shi899277a2017-07-07 22:12:12 +000060 BOOST_CHECK_EQUAL(i.getName(), "/");
Davide Pesavento478d3382021-03-17 12:46:08 -040061 BOOST_CHECK_EQUAL(i.getCanBePrefix(), false);
Junxiao Shi6efa3b72018-04-14 15:54:08 +000062 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -040063 BOOST_CHECK_EQUAL(i.getForwardingHint().empty(), true);
64 BOOST_CHECK_EQUAL(i.hasNonce(), false);
Junxiao Shi899277a2017-07-07 22:12:12 +000065 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesaventof6b45892023-03-13 15:00:51 -040066 BOOST_CHECK(i.getHopLimit() == std::nullopt);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -040067 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), false);
68 BOOST_CHECK_EQUAL(i.getApplicationParameters().isValid(), false);
69 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
Davide Pesaventof6b45892023-03-13 15:00:51 -040070 BOOST_CHECK(i.getSignatureInfo() == std::nullopt);
Eric Newberry6e262f02020-05-29 23:11:25 -070071 BOOST_CHECK_EQUAL(i.getSignatureValue().isValid(), false);
72 BOOST_CHECK_EQUAL(i.isSigned(), false);
Davide Pesaventofccb2dc2019-02-09 01:02:35 -050073}
74
Davide Pesavento0e0b3892019-07-30 21:05:05 -040075BOOST_AUTO_TEST_SUITE(Encode)
Davide Pesaventoadc9aa22019-06-30 19:00:20 -040076
77BOOST_AUTO_TEST_CASE(Basic)
Junxiao Shi899277a2017-07-07 22:12:12 +000078{
79 const uint8_t WIRE[] = {
80 0x05, 0x1c, // Interest
81 0x07, 0x14, // Name
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +000082 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent
83 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent
84 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent
Junxiao Shi899277a2017-07-07 22:12:12 +000085 0x0a, 0x04, // Nonce
Davide Pesavento53533942020-03-04 23:10:06 -050086 0x01, 0x02, 0x03, 0x04,
Junxiao Shi899277a2017-07-07 22:12:12 +000087 };
88
Davide Pesavento0e0b3892019-07-30 21:05:05 -040089 Interest i1;
90 i1.setName("/local/ndn/prefix");
Davide Pesavento53533942020-03-04 23:10:06 -050091 i1.setNonce(0x01020304);
Davide Pesavento0e0b3892019-07-30 21:05:05 -040092 BOOST_CHECK_EQUAL(i1.isParametersDigestValid(), true);
93
Junxiao Shi899277a2017-07-07 22:12:12 +000094 Block wire1 = i1.wireEncode();
Davide Pesaventodf8fd8a2022-02-21 20:04:21 -050095 BOOST_TEST(wire1 == WIRE, boost::test_tools::per_element());
Junxiao Shi899277a2017-07-07 22:12:12 +000096
97 Interest i2(wire1);
98 BOOST_CHECK_EQUAL(i2.getName(), "/local/ndn/prefix");
Davide Pesavento0e0b3892019-07-30 21:05:05 -040099 BOOST_CHECK_EQUAL(i2.getCanBePrefix(), false);
100 BOOST_CHECK_EQUAL(i2.getMustBeFresh(), false);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400101 BOOST_CHECK_EQUAL(i2.getForwardingHint().empty(), true);
Davide Pesavento835f0272019-09-21 13:18:24 -0400102 BOOST_CHECK_EQUAL(i2.hasNonce(), true);
Davide Pesavento53533942020-03-04 23:10:06 -0500103 BOOST_CHECK_EQUAL(i2.getNonce(), 0x01020304);
Junxiao Shi899277a2017-07-07 22:12:12 +0000104 BOOST_CHECK_EQUAL(i2.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesaventof6b45892023-03-13 15:00:51 -0400105 BOOST_CHECK(i2.getHopLimit() == std::nullopt);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400106 BOOST_CHECK_EQUAL(i2.hasApplicationParameters(), false);
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400107 BOOST_CHECK_EQUAL(i2.getApplicationParameters().isValid(), false);
Davide Pesaventof6b45892023-03-13 15:00:51 -0400108 BOOST_CHECK(i2.getSignatureInfo() == std::nullopt);
Eric Newberry6e262f02020-05-29 23:11:25 -0700109 BOOST_CHECK_EQUAL(i2.getSignatureValue().isValid(), false);
110 BOOST_CHECK_EQUAL(i2.isSigned(), false);
Junxiao Shi899277a2017-07-07 22:12:12 +0000111}
112
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400113BOOST_AUTO_TEST_CASE(WithParameters)
114{
115 const uint8_t WIRE[] = {
116 0x05, 0x44, // Interest
117 0x07, 0x36, // Name
118 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent
119 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent
120 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent
121 0x02, 0x20, // ParametersSha256DigestComponent
122 0xff, 0x91, 0x00, 0xe0, 0x4e, 0xaa, 0xdc, 0xf3, 0x06, 0x74, 0xd9, 0x80,
123 0x26, 0xa0, 0x51, 0xba, 0x25, 0xf5, 0x6b, 0x69, 0xbf, 0xa0, 0x26, 0xdc,
124 0xcc, 0xd7, 0x2c, 0x6e, 0xa0, 0xf7, 0x31, 0x5a,
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700125 0x0a, 0x04, // Nonce
Davide Pesavento53533942020-03-04 23:10:06 -0500126 0x00, 0x00, 0x00, 0x01,
Davide Pesavento9c19a392019-04-06 15:07:54 -0400127 0x24, 0x04, // ApplicationParameters
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400128 0xc0, 0xc1, 0xc2, 0xc3
129 };
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700130
131 Interest i1;
132 i1.setName("/local/ndn/prefix");
Davide Pesavento53533942020-03-04 23:10:06 -0500133 i1.setNonce(0x1);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400134 i1.setApplicationParameters("2404C0C1C2C3"_block);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400135 BOOST_CHECK_EQUAL(i1.isParametersDigestValid(), true);
136
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700137 Block wire1 = i1.wireEncode();
Davide Pesaventodf8fd8a2022-02-21 20:04:21 -0500138 BOOST_TEST(wire1 == WIRE, boost::test_tools::per_element());
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700139
140 Interest i2(wire1);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400141 BOOST_CHECK_EQUAL(i2.getName(),
142 "/local/ndn/prefix/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a");
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700143 BOOST_CHECK_EQUAL(i2.getCanBePrefix(), false);
144 BOOST_CHECK_EQUAL(i2.getMustBeFresh(), false);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400145 BOOST_CHECK_EQUAL(i2.getForwardingHint().empty(), true);
Davide Pesavento835f0272019-09-21 13:18:24 -0400146 BOOST_CHECK_EQUAL(i2.hasNonce(), true);
Davide Pesavento53533942020-03-04 23:10:06 -0500147 BOOST_CHECK_EQUAL(i2.getNonce(), 0x1);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700148 BOOST_CHECK_EQUAL(i2.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesaventof6b45892023-03-13 15:00:51 -0400149 BOOST_CHECK(i2.getHopLimit() == std::nullopt);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400150 BOOST_CHECK_EQUAL(i2.hasApplicationParameters(), true);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400151 BOOST_CHECK_EQUAL(i2.getApplicationParameters(), "2404C0C1C2C3"_block);
Davide Pesaventof6b45892023-03-13 15:00:51 -0400152 BOOST_CHECK(i2.getSignatureInfo() == std::nullopt);
Eric Newberry6e262f02020-05-29 23:11:25 -0700153 BOOST_CHECK_EQUAL(i2.getSignatureValue().isValid(), false);
154 BOOST_CHECK_EQUAL(i2.isSigned(), false);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700155}
156
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400157BOOST_AUTO_TEST_CASE(Full)
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700158{
159 const uint8_t WIRE[] = {
Junxiao Shie4603e12022-01-05 19:12:25 +0000160 0x05, 0x56, // Interest
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400161 0x07, 0x36, // Name
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700162 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent
163 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent
164 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400165 0x02, 0x20, // ParametersSha256DigestComponent
166 0xff, 0x91, 0x00, 0xe0, 0x4e, 0xaa, 0xdc, 0xf3, 0x06, 0x74, 0xd9, 0x80,
167 0x26, 0xa0, 0x51, 0xba, 0x25, 0xf5, 0x6b, 0x69, 0xbf, 0xa0, 0x26, 0xdc,
168 0xcc, 0xd7, 0x2c, 0x6e, 0xa0, 0xf7, 0x31, 0x5a,
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700169 0x21, 0x00, // CanBePrefix
170 0x12, 0x00, // MustBeFresh
Junxiao Shie4603e12022-01-05 19:12:25 +0000171 0x1e, 0x05, // ForwardingHint
172 0x07, 0x03, 0x08, 0x01, 0x48,
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700173 0x0a, 0x04, // Nonce
Davide Pesavento53533942020-03-04 23:10:06 -0500174 0x4c, 0x1e, 0xcb, 0x4a,
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400175 0x0c, 0x02, // InterestLifetime
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700176 0x76, 0xa1,
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400177 0x22, 0x01, // HopLimit
178 0xdc,
Davide Pesavento9c19a392019-04-06 15:07:54 -0400179 0x24, 0x04, // ApplicationParameters
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400180 0xc0, 0xc1, 0xc2, 0xc3
181 };
182
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700183 Interest i1;
184 i1.setName("/local/ndn/prefix");
185 i1.setMustBeFresh(true);
186 i1.setCanBePrefix(true);
Junxiao Shie4603e12022-01-05 19:12:25 +0000187 i1.setForwardingHint({"/H"});
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700188 i1.setNonce(0x4c1ecb4a);
189 i1.setInterestLifetime(30369_ms);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400190 i1.setHopLimit(220);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400191 i1.setApplicationParameters("2404C0C1C2C3"_block);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400192 BOOST_CHECK_EQUAL(i1.isParametersDigestValid(), true);
193
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700194 Block wire1 = i1.wireEncode();
Davide Pesaventodf8fd8a2022-02-21 20:04:21 -0500195 BOOST_TEST(wire1 == WIRE, boost::test_tools::per_element());
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700196
197 Interest i2(wire1);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400198 BOOST_CHECK_EQUAL(i2.getName(),
199 "/local/ndn/prefix/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a");
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700200 BOOST_CHECK_EQUAL(i2.getCanBePrefix(), true);
201 BOOST_CHECK_EQUAL(i2.getMustBeFresh(), true);
Junxiao Shie4603e12022-01-05 19:12:25 +0000202 BOOST_TEST(i2.getForwardingHint() == std::vector<Name>({"/H"}), boost::test_tools::per_element());
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400203 BOOST_CHECK_EQUAL(i2.hasNonce(), true);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700204 BOOST_CHECK_EQUAL(i2.getNonce(), 0x4c1ecb4a);
205 BOOST_CHECK_EQUAL(i2.getInterestLifetime(), 30369_ms);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400206 BOOST_CHECK_EQUAL(*i2.getHopLimit(), 220);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400207 BOOST_CHECK_EQUAL(i2.getApplicationParameters(), "2404C0C1C2C3"_block);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700208}
209
Eric Newberry6e262f02020-05-29 23:11:25 -0700210BOOST_AUTO_TEST_CASE(Signed)
211{
212 const uint8_t WIRE[] = {
213 0x05, 0x77, // Interest
214 0x07, 0x36, // Name
215 0x08, 0x05, // GenericNameComponent
216 0x6c, 0x6f, 0x63, 0x61, 0x6c,
217 0x08, 0x03, // GenericNameComponent
218 0x6e, 0x64, 0x6e,
219 0x08, 0x06, // GenericNameComponent
220 0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
221 0x02, 0x20, // ParametersSha256DigestComponent
222 0x6f, 0x29, 0x58, 0x60, 0x53, 0xee, 0x9f, 0xcc,
223 0xd8, 0xa4, 0x22, 0x12, 0x29, 0x25, 0x28, 0x7c,
224 0x0a, 0x18, 0x43, 0x5f, 0x40, 0x74, 0xc4, 0x0a,
225 0xbb, 0x0d, 0x5b, 0x30, 0xe4, 0xaa, 0x62, 0x20,
226 0x12, 0x00, // MustBeFresh
227 0x0a, 0x04, // Nonce
228 0x4c, 0x1e, 0xcb, 0x4a,
229 0x24, 0x04, // ApplicationParameters
230 0xc0, 0xc1, 0xc2, 0xc3,
231 0x2c, 0x0d, // InterestSignatureInfo
232 0x1b, 0x01, // SignatureType
233 0x00,
234 0x26, 0x08, // SignatureNonce
235 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
236 0x2e, 0x20, // InterestSignatureValue
237 0x12, 0x47, 0x1a, 0xe0, 0xf8, 0x72, 0x3a, 0xc1,
238 0x15, 0x6c, 0x37, 0x0a, 0x38, 0x71, 0x1e, 0xbe,
239 0xbf, 0x28, 0x17, 0xde, 0x9b, 0x2d, 0xd9, 0x4e,
240 0x9b, 0x7e, 0x62, 0xf1, 0x17, 0xb8, 0x76, 0xc1,
241 };
242
243 SignatureInfo si(tlv::DigestSha256);
244 std::vector<uint8_t> nonce{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
245 si.setNonce(nonce);
246 Block sv("2E20 12471AE0F8723AC1156C370A38711EBEBF2817DE9B2DD94E9B7E62F117B876C1"_block);
247
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500248 Interest i1(Block{WIRE});
Eric Newberry6e262f02020-05-29 23:11:25 -0700249 BOOST_CHECK_EQUAL(i1.getName(),
250 "/local/ndn/prefix/params-sha256=6f29586053ee9fccd8a422122925287c0a18435f4074c40abb0d5b30e4aa6220");
251 BOOST_CHECK_EQUAL(i1.getCanBePrefix(), false);
252 BOOST_CHECK_EQUAL(i1.getMustBeFresh(), true);
253 BOOST_CHECK_EQUAL(i1.hasNonce(), true);
254 BOOST_CHECK_EQUAL(i1.getNonce(), 0x4c1ecb4a);
255 BOOST_CHECK_EQUAL(i1.getSignatureInfo()->getSignatureType(), tlv::DigestSha256);
256 BOOST_CHECK(i1.getSignatureInfo()->getNonce() == nonce);
Davide Pesaventodf8fd8a2022-02-21 20:04:21 -0500257 BOOST_TEST(i1.getSignatureValue() == sv, boost::test_tools::per_element());
Eric Newberry6e262f02020-05-29 23:11:25 -0700258 BOOST_CHECK_EQUAL(i1.getApplicationParameters(), "2404C0C1C2C3"_block);
259 BOOST_CHECK_EQUAL(i1.isParametersDigestValid(), true);
260
261 // Reset wire
262 BOOST_CHECK_EQUAL(i1.hasWire(), true);
263 i1.setCanBePrefix(true);
264 i1.setCanBePrefix(false);
265 BOOST_CHECK_EQUAL(i1.hasWire(), false);
266
Davide Pesaventodf8fd8a2022-02-21 20:04:21 -0500267 BOOST_TEST(i1.wireEncode() == WIRE, boost::test_tools::per_element());
Eric Newberry6e262f02020-05-29 23:11:25 -0700268
269 Interest i2("/local/ndn/prefix");
Eric Newberry6e262f02020-05-29 23:11:25 -0700270 i2.setMustBeFresh(true);
271 i2.setNonce(0x4c1ecb4a);
272 i2.setApplicationParameters("2404C0C1C2C3"_block);
273 i2.setSignatureInfo(si);
Davide Pesavento487e3d32022-05-05 18:06:23 -0400274 i2.setSignatureValue(sv.value_bytes());
Eric Newberry6e262f02020-05-29 23:11:25 -0700275 BOOST_CHECK_EQUAL(i2.isParametersDigestValid(), true);
276
Davide Pesaventodf8fd8a2022-02-21 20:04:21 -0500277 BOOST_TEST(i2.wireEncode() == WIRE, boost::test_tools::per_element());
Eric Newberry6e262f02020-05-29 23:11:25 -0700278}
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
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500322 Interest i1(Block{WIRE});
Eric Newberry6e262f02020-05-29 23:11:25 -0700323 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);
Davide Pesaventodf8fd8a2022-02-21 20:04:21 -0500331 BOOST_TEST(i1.getSignatureValue() == sv, boost::test_tools::per_element());
Eric Newberry6e262f02020-05-29 23:11:25 -0700332 BOOST_CHECK_EQUAL(i1.getApplicationParameters(), "2404C0C1C2C3"_block);
333 BOOST_CHECK_EQUAL(i1.isParametersDigestValid(), true);
334
335 // Reset wire
336 BOOST_CHECK_EQUAL(i1.hasWire(), true);
337 i1.setCanBePrefix(true);
338 i1.setCanBePrefix(false);
339 BOOST_CHECK_EQUAL(i1.hasWire(), false);
340
Davide Pesaventodf8fd8a2022-02-21 20:04:21 -0500341 BOOST_TEST(i1.wireEncode() == WIRE, boost::test_tools::per_element());
Eric Newberry6e262f02020-05-29 23:11:25 -0700342}
343
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400344BOOST_AUTO_TEST_CASE(MissingApplicationParameters)
345{
346 Interest i;
347 i.setName(Name("/A").appendParametersSha256DigestPlaceholder());
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400348 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
Davide Pesavento905d40f2020-06-09 21:33:25 -0400349 BOOST_CHECK_EXCEPTION(i.wireEncode(), tlv::Error, [] (const auto& e) {
350 return e.what() == "Interest without parameters must not have a ParametersSha256DigestComponent"s;
351 });
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400352}
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400353
354BOOST_AUTO_TEST_CASE(MissingParametersSha256DigestComponent)
355{
356 // there's no way to create an Interest that fails this check via programmatic construction,
357 // so we have to decode an invalid Interest and force reencoding
358
359 DisableAutoCheckParametersDigest disabler;
360 Interest i("050F 0703(080149) 0A04F000F000 2402CAFE"_block);
361 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
362 BOOST_CHECK_NO_THROW(i.wireEncode()); // this succeeds because it uses the cached wire encoding
363
Davide Pesavento905d40f2020-06-09 21:33:25 -0400364 // trigger reencoding
365 i.setNonce(42);
366 // now the check fails while attempting to reencode
367 BOOST_CHECK_EXCEPTION(i.wireEncode(), tlv::Error, [] (const auto& e) {
368 return e.what() == "Interest with parameters must have a ParametersSha256DigestComponent"s;
369 });
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400370}
371
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400372BOOST_AUTO_TEST_SUITE_END() // Encode
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400373
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400374class DecodeFixture
Junxiao Shi899277a2017-07-07 22:12:12 +0000375{
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000376protected:
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400377 DecodeFixture()
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000378 {
379 // initialize all elements to non-empty, to verify wireDecode clears them
380 i.setName("/A");
Junxiao Shie4603e12022-01-05 19:12:25 +0000381 i.setForwardingHint({"/F"});
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000382 i.setNonce(0x03d645a8);
383 i.setInterestLifetime(18554_ms);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400384 i.setHopLimit(64);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400385 i.setApplicationParameters("2404A0A1A2A3"_block);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000386 }
Junxiao Shi899277a2017-07-07 22:12:12 +0000387
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000388protected:
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000389 Interest i;
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000390};
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000391
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400392BOOST_FIXTURE_TEST_SUITE(Decode, DecodeFixture)
393
394BOOST_AUTO_TEST_CASE(NotAnInterest)
395{
Davide Pesavento905d40f2020-06-09 21:33:25 -0400396 BOOST_CHECK_EXCEPTION(i.wireDecode("4202CAFE"_block), tlv::Error, [] (const auto& e) {
397 return e.what() == "Expecting Interest element, but TLV has type 66"s;
398 });
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400399}
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000400
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400401BOOST_AUTO_TEST_CASE(NameOnly)
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000402{
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400403 i.wireDecode("0505 0703(080149)"_block);
Davide Pesavento835f0272019-09-21 13:18:24 -0400404 BOOST_CHECK_EQUAL(i.hasWire(), true);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000405 BOOST_CHECK_EQUAL(i.getName(), "/I");
406 BOOST_CHECK_EQUAL(i.getCanBePrefix(), false);
407 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400408 BOOST_CHECK_EQUAL(i.getForwardingHint().empty(), true);
Davide Pesavento835f0272019-09-21 13:18:24 -0400409 BOOST_CHECK_EQUAL(i.hasNonce(), false);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000410 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesaventof6b45892023-03-13 15:00:51 -0400411 BOOST_CHECK(i.getHopLimit() == std::nullopt);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400412 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), false);
413 BOOST_CHECK_EQUAL(i.getApplicationParameters().isValid(), false);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000414
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400415 // modify then re-encode
Davide Pesavento53533942020-03-04 23:10:06 -0500416 i.setNonce(0x957c6554);
Davide Pesavento835f0272019-09-21 13:18:24 -0400417 BOOST_CHECK_EQUAL(i.hasWire(), false);
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400418 BOOST_CHECK_EQUAL(i.wireEncode(), "050B 0703(080149) 0A04957C6554"_block);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000419}
420
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400421BOOST_AUTO_TEST_CASE(NameCanBePrefix)
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000422{
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400423 i.wireDecode("0507 0703(080149) 2100"_block);
Davide Pesavento835f0272019-09-21 13:18:24 -0400424 BOOST_CHECK_EQUAL(i.hasWire(), true);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400425 BOOST_CHECK_EQUAL(i.getName(), "/I");
426 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
427 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
428 BOOST_CHECK_EQUAL(i.getForwardingHint().empty(), true);
Davide Pesavento835f0272019-09-21 13:18:24 -0400429 BOOST_CHECK_EQUAL(i.hasNonce(), false);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400430 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesaventof6b45892023-03-13 15:00:51 -0400431 BOOST_CHECK(i.getHopLimit() == std::nullopt);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400432 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), false);
433 BOOST_CHECK_EQUAL(i.getApplicationParameters().isValid(), false);
434}
435
436BOOST_AUTO_TEST_CASE(FullWithoutParameters)
437{
438 i.wireDecode("0531 0703(080149) "
439 "FC00 2100 FC00 1200 FC00 1E0B(1F09 1E023E15 0703080148) "
440 "FC00 0A044ACB1E4C FC00 0C0276A1 FC00 2201D6 FC00"_block);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000441 BOOST_CHECK_EQUAL(i.getName(), "/I");
442 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
443 BOOST_CHECK_EQUAL(i.getMustBeFresh(), true);
Junxiao Shie4603e12022-01-05 19:12:25 +0000444 BOOST_TEST(i.getForwardingHint() == std::vector<Name>({"/H"}), boost::test_tools::per_element());
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400445 BOOST_CHECK_EQUAL(i.hasNonce(), true);
Davide Pesavento53533942020-03-04 23:10:06 -0500446 BOOST_CHECK_EQUAL(i.getNonce(), 0x4acb1e4c);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000447 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 30369_ms);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400448 BOOST_CHECK_EQUAL(*i.getHopLimit(), 214);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400449 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), false);
450 BOOST_CHECK_EQUAL(i.getApplicationParameters().isValid(), false);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000451
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000452 // encode without modification: retain original wire encoding
Junxiao Shi8b753a22018-10-24 01:51:40 +0000453 BOOST_CHECK_EQUAL(i.wireEncode().value_size(), 49);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000454
Junxiao Shie4603e12022-01-05 19:12:25 +0000455 // modify then re-encode:
456 // * unrecognized elements are discarded;
457 // * ForwardingHint is re-encoded as a sequence of Names
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000458 i.setName("/J");
Junxiao Shi72c0c642018-04-20 15:41:09 +0000459 BOOST_CHECK_EQUAL(i.wireEncode(),
Junxiao Shie4603e12022-01-05 19:12:25 +0000460 "051D 0703(08014A) "
461 "2100 1200 1E05(0703080148) "
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400462 "0A044ACB1E4C 0C0276A1 2201D6"_block);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400463}
464
465BOOST_AUTO_TEST_CASE(FullWithParameters)
466{
467 i.wireDecode("055B 0725(080149 0220F16DB273F40436A852063F864D5072B01EAD53151F5A688EA1560492BEBEDD05) "
468 "FC00 2100 FC00 1200 FC00 1E0B(1F09 1E023E15 0703080148) "
469 "FC00 0A044ACB1E4C FC00 0C0276A1 FC00 2201D6 FC00 2404C0C1C2C3 FC00"_block);
470 BOOST_CHECK_EQUAL(i.getName(),
471 "/I/params-sha256=f16db273f40436a852063f864d5072b01ead53151f5a688ea1560492bebedd05");
472 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
473 BOOST_CHECK_EQUAL(i.getMustBeFresh(), true);
Junxiao Shie4603e12022-01-05 19:12:25 +0000474 BOOST_TEST(i.getForwardingHint() == std::vector<Name>({"/H"}), boost::test_tools::per_element());
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400475 BOOST_CHECK_EQUAL(i.hasNonce(), true);
Davide Pesavento53533942020-03-04 23:10:06 -0500476 BOOST_CHECK_EQUAL(i.getNonce(), 0x4acb1e4c);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400477 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 30369_ms);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400478 BOOST_CHECK_EQUAL(*i.getHopLimit(), 214);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400479 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true);
480 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2404C0C1C2C3"_block);
481
482 // encode without modification: retain original wire encoding
483 BOOST_CHECK_EQUAL(i.wireEncode().value_size(), 91);
484
Junxiao Shie4603e12022-01-05 19:12:25 +0000485 // modify then re-encode:
486 // * unrecognized elements after ApplicationParameters are preserved, the rest are discarded;
487 // * ForwardingHint is re-encoded as a sequence of Names
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400488 i.setName("/J");
489 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
490 BOOST_CHECK_EQUAL(i.wireEncode(),
Junxiao Shie4603e12022-01-05 19:12:25 +0000491 "0547 0725(08014A 0220F16DB273F40436A852063F864D5072B01EAD53151F5A688EA1560492BEBEDD05) "
492 "2100 1200 1E05(0703080148) "
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400493 "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3 FC00"_block);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400494
495 // modify ApplicationParameters: unrecognized elements are preserved
496 i.setApplicationParameters("2402CAFE"_block);
497 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
498 BOOST_CHECK_EQUAL(i.wireEncode(),
Junxiao Shie4603e12022-01-05 19:12:25 +0000499 "0545 0725(08014A 02205FDA67967EE302FC457E41B7D3D51BA6A9379574D193FD88F64954BF16C2927A) "
500 "2100 1200 1E05(0703080148) "
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400501 "0A044ACB1E4C 0C0276A1 2201D6 2402CAFE FC00"_block);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000502}
503
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000504BOOST_AUTO_TEST_CASE(CriticalElementOutOfOrder)
505{
Davide Pesavento905d40f2020-06-09 21:33:25 -0400506 BOOST_CHECK_EXCEPTION(i.wireDecode(
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000507 "0529 2100 0703080149 1200 1E0B(1F09 1E023E15 0703080148) "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500508 "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3"_block),
Davide Pesavento905d40f2020-06-09 21:33:25 -0400509 tlv::Error,
510 [] (const auto& e) { return e.what() == "Name element is missing or out of order"s; });
511 BOOST_CHECK_EXCEPTION(i.wireDecode(
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000512 "0529 0703080149 1200 2100 1E0B(1F09 1E023E15 0703080148) "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500513 "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3"_block),
Davide Pesavento905d40f2020-06-09 21:33:25 -0400514 tlv::Error,
515 [] (const auto& e) { return e.what() == "CanBePrefix element is out of order"s; });
516 BOOST_CHECK_EXCEPTION(i.wireDecode(
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000517 "0529 0703080149 2100 1E0B(1F09 1E023E15 0703080148) 1200 "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500518 "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3"_block),
Davide Pesavento905d40f2020-06-09 21:33:25 -0400519 tlv::Error,
520 [] (const auto& e) { return e.what() == "MustBeFresh element is out of order"s; });
521 BOOST_CHECK_EXCEPTION(i.wireDecode(
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000522 "0529 0703080149 2100 1200 0A044ACB1E4C "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500523 "1E0B(1F09 1E023E15 0703080148) 0C0276A1 2201D6 2404C0C1C2C3"_block),
Davide Pesavento905d40f2020-06-09 21:33:25 -0400524 tlv::Error,
525 [] (const auto& e) { return e.what() == "ForwardingHint element is out of order"s; });
526 BOOST_CHECK_EXCEPTION(i.wireDecode(
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000527 "0529 0703080149 2100 1200 1E0B(1F09 1E023E15 0703080148) "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500528 "0C0276A1 0A044ACB1E4C 2201D6 2404C0C1C2C3"_block),
Davide Pesavento905d40f2020-06-09 21:33:25 -0400529 tlv::Error,
530 [] (const auto& e) { return e.what() == "Nonce element is out of order"s; });
531 BOOST_CHECK_EXCEPTION(i.wireDecode(
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000532 "0529 0703080149 2100 1200 1E0B(1F09 1E023E15 0703080148) "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500533 "0A044ACB1E4C 2201D6 0C0276A1 2404C0C1C2C3"_block),
Davide Pesavento905d40f2020-06-09 21:33:25 -0400534 tlv::Error,
535 [] (const auto& e) { return e.what() == "InterestLifetime element is out of order"s; });
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000536}
537
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500538BOOST_AUTO_TEST_CASE(NonCriticalElementOutOfOrder)
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000539{
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400540 // duplicate HopLimit
541 i.wireDecode("0536 0725(080149 0220FF9100E04EAADCF30674D98026A051BA25F56B69BFA026DCCCD72C6EA0F7315A)"
542 "2201D6 2200 2404C0C1C2C3 22020101"_block);
543 BOOST_CHECK_EQUAL(i.getName(),
544 "/I/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a");
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400545 BOOST_CHECK_EQUAL(*i.getHopLimit(), 214);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400546 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400547 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2404C0C1C2C3"_block);
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500548
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400549 // duplicate ApplicationParameters
550 i.wireDecode("0541 0725(080149 0220FF9100E04EAADCF30674D98026A051BA25F56B69BFA026DCCCD72C6EA0F7315A)"
551 "2100 1200 0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3 2401EE"_block);
552 BOOST_CHECK_EQUAL(i.getName(),
553 "/I/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a");
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400554 BOOST_CHECK_EQUAL(*i.getHopLimit(), 214);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400555 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true);
556 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2404C0C1C2C3"_block);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000557}
558
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400559BOOST_AUTO_TEST_CASE(MissingName)
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000560{
Davide Pesavento905d40f2020-06-09 21:33:25 -0400561 BOOST_CHECK_EXCEPTION(i.wireDecode("0500"_block), tlv::Error,
562 [] (const auto& e) { return e.what() == "Name element is missing or out of order"s; });
563 BOOST_CHECK_EXCEPTION(i.wireDecode("0502 1200"_block), tlv::Error,
564 [] (const auto& e) { return e.what() == "Name element is missing or out of order"s; });
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000565}
566
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400567BOOST_AUTO_TEST_CASE(BadName)
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000568{
Davide Pesavento905d40f2020-06-09 21:33:25 -0400569 BOOST_CHECK_EXCEPTION(i.wireDecode("0502 0700"_block), tlv::Error,
570 [] (const auto& e) { return e.what() == "Name has zero name components"s; });
571 BOOST_CHECK_EXCEPTION(i.wireDecode("054C 074A(080149"
572 "02200000000000000000000000000000000000000000000000000000000000000000"
573 "080132"
574 "02200000000000000000000000000000000000000000000000000000000000000000)"_block),
575 tlv::Error,
576 [] (const auto& e) { return e.what() == "Name has more than one ParametersSha256DigestComponent"s; });
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000577}
578
579BOOST_AUTO_TEST_CASE(BadCanBePrefix)
580{
Davide Pesavento905d40f2020-06-09 21:33:25 -0400581 BOOST_CHECK_EXCEPTION(i.wireDecode("0508 0703080149 210102"_block), tlv::Error,
582 [] (const auto& e) { return e.what() == "CanBePrefix element has non-zero TLV-LENGTH"s; });
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000583}
584
585BOOST_AUTO_TEST_CASE(BadMustBeFresh)
586{
Davide Pesavento905d40f2020-06-09 21:33:25 -0400587 BOOST_CHECK_EXCEPTION(i.wireDecode("0508 0703080149 120102"_block), tlv::Error,
588 [] (const auto& e) { return e.what() == "MustBeFresh element has non-zero TLV-LENGTH"s; });
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000589}
590
591BOOST_AUTO_TEST_CASE(BadNonce)
592{
Davide Pesavento905d40f2020-06-09 21:33:25 -0400593 BOOST_CHECK_EXCEPTION(i.wireDecode("0507 0703080149 0A00"_block), tlv::Error,
594 [] (const auto& e) { return e.what() == "Nonce element is malformed"s; });
595 BOOST_CHECK_EXCEPTION(i.wireDecode("050A 0703080149 0A0304C263"_block), tlv::Error,
596 [] (const auto& e) { return e.what() == "Nonce element is malformed"s; });
597 BOOST_CHECK_EXCEPTION(i.wireDecode("050C 0703080149 0A05EFA420B262"_block), tlv::Error,
598 [] (const auto& e) { return e.what() == "Nonce element is malformed"s; });
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000599}
600
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500601BOOST_AUTO_TEST_CASE(BadHopLimit)
602{
Davide Pesavento905d40f2020-06-09 21:33:25 -0400603 BOOST_CHECK_EXCEPTION(i.wireDecode("0507 0703080149 2200"_block), tlv::Error,
604 [] (const auto& e) { return e.what() == "HopLimit element is malformed"s; });
605 BOOST_CHECK_EXCEPTION(i.wireDecode("0509 0703080149 22021356"_block), tlv::Error,
606 [] (const auto& e) { return e.what() == "HopLimit element is malformed"s; });
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500607}
608
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400609BOOST_AUTO_TEST_CASE(BadParametersDigest)
610{
611 // ApplicationParameters without ParametersSha256DigestComponent
612 Block b1("0509 0703(080149) 2402CAFE"_block);
613 // ParametersSha256DigestComponent without ApplicationParameters
614 Block b2("0527 0725(080149 0220E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855)"_block);
615 // digest mismatch
616 Block b3("052B 0725(080149 02200000000000000000000000000000000000000000000000000000000000000000) "
617 "2402CAFE"_block);
618
619 BOOST_CHECK_THROW(i.wireDecode(b1), tlv::Error);
620 BOOST_CHECK_THROW(i.wireDecode(b2), tlv::Error);
621 BOOST_CHECK_THROW(i.wireDecode(b3), tlv::Error);
622
623 DisableAutoCheckParametersDigest disabler;
624 BOOST_CHECK_NO_THROW(i.wireDecode(b1));
625 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
626 BOOST_CHECK_NO_THROW(i.wireDecode(b2));
627 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
628 BOOST_CHECK_NO_THROW(i.wireDecode(b3));
629 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
630}
631
Junxiao Shi8b753a22018-10-24 01:51:40 +0000632BOOST_AUTO_TEST_CASE(UnrecognizedNonCriticalElementBeforeName)
633{
Davide Pesavento905d40f2020-06-09 21:33:25 -0400634 BOOST_CHECK_EXCEPTION(i.wireDecode("0507 FC00 0703080149"_block), tlv::Error,
635 [] (const auto& e) { return e.what() == "Name element is missing or out of order"s; });
Junxiao Shi8b753a22018-10-24 01:51:40 +0000636}
637
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000638BOOST_AUTO_TEST_CASE(UnrecognizedCriticalElement)
639{
Davide Pesavento905d40f2020-06-09 21:33:25 -0400640 BOOST_CHECK_EXCEPTION(i.wireDecode("0507 0703080149 FB00"_block), tlv::Error,
641 [] (const auto& e) { return e.what() == "Unrecognized element of critical type 251"s; });
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400642 // v0.2 packet with Selectors
Davide Pesavento905d40f2020-06-09 21:33:25 -0400643 BOOST_CHECK_EXCEPTION(i.wireDecode("0510 0703080149 09030D0101 0A0401000000"_block), tlv::Error,
644 [] (const auto& e) { return e.what() == "Unrecognized element of critical type 9"s; });
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000645}
646
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400647BOOST_AUTO_TEST_SUITE_END() // Decode
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000648
Junxiao Shi899277a2017-07-07 22:12:12 +0000649BOOST_AUTO_TEST_CASE(MatchesData)
650{
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000651 auto interest = makeInterest("/A");
Junxiao Shi899277a2017-07-07 22:12:12 +0000652
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000653 auto data = makeData("/A");
654 BOOST_CHECK_EQUAL(interest->matchesData(*data), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000655
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000656 data->setName("/A/D");
657 BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // violates CanBePrefix
Junxiao Shi899277a2017-07-07 22:12:12 +0000658
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000659 interest->setCanBePrefix(true);
660 BOOST_CHECK_EQUAL(interest->matchesData(*data), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000661
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000662 interest->setMustBeFresh(true);
663 BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // violates MustBeFresh
Junxiao Shi899277a2017-07-07 22:12:12 +0000664
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000665 data->setFreshnessPeriod(1_s);
666 BOOST_CHECK_EQUAL(interest->matchesData(*data), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000667
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000668 data->setName("/H/I");
669 BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // Name does not match
Junxiao Shi899277a2017-07-07 22:12:12 +0000670
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000671 data->wireEncode();
672 interest = makeInterest(data->getFullName());
673 BOOST_CHECK_EQUAL(interest->matchesData(*data), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000674
Davide Pesavento53533942020-03-04 23:10:06 -0500675 setNameComponent(*interest, -1,
676 name::Component::fromEscapedString("sha256digest=00000000000000000000000000"
677 "00000000000000000000000000000000000000"));
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000678 BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // violates implicit digest
Junxiao Shi899277a2017-07-07 22:12:12 +0000679}
680
681BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(MatchesInterest, 1)
682BOOST_AUTO_TEST_CASE(MatchesInterest)
683{
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400684 Interest interest;
685 interest.setName("/A")
686 .setCanBePrefix(true)
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000687 .setMustBeFresh(true)
Junxiao Shie4603e12022-01-05 19:12:25 +0000688 .setForwardingHint({"/H"})
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000689 .setNonce(2228)
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400690 .setInterestLifetime(5_s)
691 .setHopLimit(90);
Junxiao Shi899277a2017-07-07 22:12:12 +0000692
693 Interest other;
694 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false);
695
696 other.setName(interest.getName());
697 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false);
698
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000699 other.setCanBePrefix(interest.getCanBePrefix());
700 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false);
701
702 other.setMustBeFresh(interest.getMustBeFresh());
Junxiao Shi899277a2017-07-07 22:12:12 +0000703 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false); // will match until #3162 implemented
704
Junxiao Shie4603e12022-01-05 19:12:25 +0000705 auto fh = interest.getForwardingHint();
706 other.setForwardingHint(std::vector<Name>(fh.begin(), fh.end()));
Junxiao Shi899277a2017-07-07 22:12:12 +0000707 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
708
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000709 other.setNonce(9336);
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.setInterestLifetime(3_s);
Junxiao Shi899277a2017-07-07 22:12:12 +0000713 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400714
715 other.setHopLimit(31);
716 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000717}
718
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400719BOOST_AUTO_TEST_CASE(SetName)
720{
721 Interest i;
722 BOOST_CHECK_EQUAL(i.getName(), "/");
723 i.setName("/A/B");
724 BOOST_CHECK_EQUAL(i.getName(), "/A/B");
725 i.setName("/I/params-sha256=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
726 BOOST_CHECK_EQUAL(i.getName(),
727 "/I/params-sha256=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
728 BOOST_CHECK_THROW(i.setName("/I"
729 "/params-sha256=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
730 "/params-sha256=0000000000000000000000000000000000000000000000000000000000000000"),
731 std::invalid_argument);
732}
Junxiao Shi899277a2017-07-07 22:12:12 +0000733
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400734BOOST_AUTO_TEST_CASE(SetCanBePrefix)
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000735{
736 Interest i;
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000737 BOOST_CHECK_EQUAL(i.getCanBePrefix(), false);
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000738 i.setCanBePrefix(true);
739 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
Davide Pesavento478d3382021-03-17 12:46:08 -0400740 i.setCanBePrefix(false);
741 BOOST_CHECK_EQUAL(i.getCanBePrefix(), false);
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000742}
743
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400744BOOST_AUTO_TEST_CASE(SetMustBeFresh)
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000745{
746 Interest i;
747 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
748 i.setMustBeFresh(true);
749 BOOST_CHECK_EQUAL(i.getMustBeFresh(), true);
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000750 i.setMustBeFresh(false);
751 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000752}
753
Junxiao Shi899277a2017-07-07 22:12:12 +0000754BOOST_AUTO_TEST_CASE(GetNonce)
755{
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000756 unique_ptr<Interest> i1, i2;
Davide Pesavento53533942020-03-04 23:10:06 -0500757 Interest::Nonce nonce1(0), nonce2(0);
Junxiao Shi899277a2017-07-07 22:12:12 +0000758
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000759 // getNonce automatically assigns a random Nonce.
760 // 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 -0500761 // identical Nonces in a row.
Junxiao Shi899277a2017-07-07 22:12:12 +0000762 int nIterations = 0;
763 do {
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000764 i1 = make_unique<Interest>();
765 nonce1 = i1->getNonce();
766 i2 = make_unique<Interest>();
767 nonce2 = i2->getNonce();
Junxiao Shi899277a2017-07-07 22:12:12 +0000768 }
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000769 while (nonce1 == nonce2 && ++nIterations < 100);
Davide Pesavento53533942020-03-04 23:10:06 -0500770
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000771 BOOST_CHECK_NE(nonce1, nonce2);
772 BOOST_CHECK(i1->hasNonce());
773 BOOST_CHECK(i2->hasNonce());
Junxiao Shi899277a2017-07-07 22:12:12 +0000774
775 // Once a Nonce is assigned, it should not change.
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000776 BOOST_CHECK_EQUAL(i1->getNonce(), nonce1);
Davide Pesavento53533942020-03-04 23:10:06 -0500777 BOOST_CHECK_EQUAL(i2->getNonce(), nonce2);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000778}
779
780BOOST_AUTO_TEST_CASE(SetNonce)
781{
782 Interest i1("/A");
Davide Pesavento53533942020-03-04 23:10:06 -0500783 BOOST_CHECK(!i1.hasNonce());
784
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000785 i1.setNonce(1);
786 i1.wireEncode();
Davide Pesavento53533942020-03-04 23:10:06 -0500787 BOOST_CHECK(i1.hasNonce());
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000788 BOOST_CHECK_EQUAL(i1.getNonce(), 1);
789
790 Interest i2(i1);
Davide Pesavento53533942020-03-04 23:10:06 -0500791 BOOST_CHECK(i2.hasNonce());
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000792 BOOST_CHECK_EQUAL(i2.getNonce(), 1);
793
794 i2.setNonce(2);
795 BOOST_CHECK_EQUAL(i2.getNonce(), 2);
Davide Pesavento53533942020-03-04 23:10:06 -0500796 BOOST_CHECK_EQUAL(i1.getNonce(), 1); // should not affect i1's Nonce (Bug #4168)
797
Davide Pesaventof6b45892023-03-13 15:00:51 -0400798 i2.setNonce(std::nullopt);
Davide Pesavento53533942020-03-04 23:10:06 -0500799 BOOST_CHECK(!i2.hasNonce());
Junxiao Shi899277a2017-07-07 22:12:12 +0000800}
801
802BOOST_AUTO_TEST_CASE(RefreshNonce)
803{
804 Interest i;
805 BOOST_CHECK(!i.hasNonce());
806 i.refreshNonce();
807 BOOST_CHECK(!i.hasNonce());
808
809 i.setNonce(1);
810 BOOST_CHECK(i.hasNonce());
811 i.refreshNonce();
812 BOOST_CHECK(i.hasNonce());
813 BOOST_CHECK_NE(i.getNonce(), 1);
814}
815
Davide Pesavento53533942020-03-04 23:10:06 -0500816BOOST_AUTO_TEST_CASE(NonceConversions)
817{
818 Interest i;
Davide Pesavento53533942020-03-04 23:10:06 -0500819
820 // 4-arg constructor
821 Interest::Nonce n1(1, 2, 3, 4);
822 i.setNonce(n1);
823 BOOST_CHECK_EQUAL(i.getNonce(), 0x01020304);
824
825 // 4-arg constructor + assignment
826 n1 = {0xf, 0xe, 0xd, 0xc};
827 i.setNonce(n1);
828 BOOST_CHECK_EQUAL(i.getNonce(), 0x0f0e0d0c);
829
830 // 1-arg constructor + assignment (implicit conversion)
831 Interest::Nonce n2;
832 n2 = 42;
833 BOOST_CHECK_NE(n1, n2);
834 i.setNonce(n2);
835 n2 = 21; // should not affect i's Nonce
836 BOOST_CHECK_EQUAL(i.getNonce(), 42);
837 BOOST_CHECK_EQUAL(i.toUri(), "/?Nonce=0000002a"); // stored in big-endian
838}
839
Junxiao Shi899277a2017-07-07 22:12:12 +0000840BOOST_AUTO_TEST_CASE(SetInterestLifetime)
841{
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500842 BOOST_CHECK_THROW(Interest("/A", -1_ms), std::invalid_argument);
Davide Pesavento0f830802018-01-16 23:58:58 -0500843 BOOST_CHECK_NO_THROW(Interest("/A", 0_ms));
Junxiao Shi899277a2017-07-07 22:12:12 +0000844
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400845 Interest i;
Junxiao Shi899277a2017-07-07 22:12:12 +0000846 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500847 BOOST_CHECK_THROW(i.setInterestLifetime(-1_ms), std::invalid_argument);
Junxiao Shi899277a2017-07-07 22:12:12 +0000848 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesavento0f830802018-01-16 23:58:58 -0500849 i.setInterestLifetime(0_ms);
850 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 0_ms);
851 i.setInterestLifetime(1_ms);
852 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 1_ms);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400853
854 i = Interest("/B", 15_s);
855 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 15_s);
856}
857
858BOOST_AUTO_TEST_CASE(SetHopLimit)
859{
860 Interest i;
Davide Pesaventof6b45892023-03-13 15:00:51 -0400861 BOOST_CHECK(i.getHopLimit() == std::nullopt);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400862 i.setHopLimit(42);
863 BOOST_CHECK(i.getHopLimit() == 42);
Davide Pesaventof6b45892023-03-13 15:00:51 -0400864 i.setHopLimit(std::nullopt);
865 BOOST_CHECK(i.getHopLimit() == std::nullopt);
Junxiao Shi899277a2017-07-07 22:12:12 +0000866}
867
Davide Pesavento9c19a392019-04-06 15:07:54 -0400868BOOST_AUTO_TEST_CASE(SetApplicationParameters)
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700869{
870 const uint8_t PARAMETERS1[] = {0xc1};
871 const uint8_t PARAMETERS2[] = {0xc2};
872
873 Interest i;
Davide Pesavento9c19a392019-04-06 15:07:54 -0400874 BOOST_CHECK(!i.hasApplicationParameters());
875 i.setApplicationParameters("2400"_block);
876 BOOST_CHECK(i.hasApplicationParameters());
877 i.unsetApplicationParameters();
878 BOOST_CHECK(!i.hasApplicationParameters());
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700879
Davide Pesavento38912442019-04-06 22:03:39 -0400880 // Block overload
Davide Pesavento38912442019-04-06 22:03:39 -0400881 i.setApplicationParameters("2401C0"_block);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400882 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2401C0"_block);
Davide Pesavento38912442019-04-06 22:03:39 -0400883 i.setApplicationParameters("8001C1"_block);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400884 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "24038001C1"_block);
Davide Pesavento81bd6962020-06-17 16:03:23 -0400885 BOOST_CHECK_THROW(i.setApplicationParameters(Block{}), std::invalid_argument);
Davide Pesavento38912442019-04-06 22:03:39 -0400886
Davide Pesaventoa3d809e2022-02-06 11:55:02 -0500887 // span overload
888 i.setApplicationParameters(PARAMETERS1);
Davide Pesavento38912442019-04-06 22:03:39 -0400889 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2401C1"_block);
Davide Pesaventoa3d809e2022-02-06 11:55:02 -0500890 i.setApplicationParameters(span<uint8_t>{});
Davide Pesavento38912442019-04-06 22:03:39 -0400891 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2400"_block);
Davide Pesaventodf8fd8a2022-02-21 20:04:21 -0500892
Davide Pesavento38912442019-04-06 22:03:39 -0400893 // ConstBufferPtr overload
894 i.setApplicationParameters(make_shared<Buffer>(PARAMETERS2, sizeof(PARAMETERS2)));
895 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2401C2"_block);
896 i.setApplicationParameters(make_shared<Buffer>());
897 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2400"_block);
898 BOOST_CHECK_THROW(i.setApplicationParameters(nullptr), std::invalid_argument);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700899}
900
Eric Newberry6e262f02020-05-29 23:11:25 -0700901BOOST_AUTO_TEST_CASE(SetSignature)
902{
903 Interest i;
Eric Newberry6e262f02020-05-29 23:11:25 -0700904
Eric Newberry6e262f02020-05-29 23:11:25 -0700905 Block sv1("2E04 01020304"_block);
Davide Pesavento487e3d32022-05-05 18:06:23 -0400906 BOOST_CHECK_EXCEPTION(i.setSignatureValue(sv1.value_bytes()), tlv::Error, [] (const auto& e) {
907 return e.what() == "InterestSignatureInfo must be present to set InterestSignatureValue"s;
908 });
909
Davide Pesaventof6b45892023-03-13 15:00:51 -0400910 BOOST_CHECK(i.getSignatureInfo() == std::nullopt);
Davide Pesavento487e3d32022-05-05 18:06:23 -0400911 BOOST_CHECK_EQUAL(i.getSignatureValue().isValid(), false);
912 BOOST_CHECK_EQUAL(i.isSigned(), false);
Eric Newberry6e262f02020-05-29 23:11:25 -0700913
914 // Simple set/get case for InterestSignatureInfo (no prior set)
915 SignatureInfo si1(tlv::SignatureSha256WithEcdsa);
916 i.setSignatureInfo(si1);
917 BOOST_CHECK(i.getSignatureInfo() == si1);
918 BOOST_CHECK_EQUAL(i.isSigned(), false);
919
920 // Simple set/get case for InterestSignatureValue (no prior set)
Davide Pesavento487e3d32022-05-05 18:06:23 -0400921 auto svBuffer1 = make_shared<Buffer>(sv1.value_begin(), sv1.value_end());
Eric Newberry6e262f02020-05-29 23:11:25 -0700922 i.setSignatureValue(svBuffer1);
923 BOOST_CHECK_EQUAL(i.getSignatureValue(), sv1);
924 BOOST_CHECK_EQUAL(i.isSigned(), true);
925
926 // Throws because attempting to set InterestSignatureValue to nullptr
927 BOOST_CHECK_THROW(i.setSignatureValue(nullptr), std::invalid_argument);
928 BOOST_CHECK_EQUAL(i.getSignatureValue(), sv1);
929 BOOST_CHECK_EQUAL(i.isSigned(), true);
930
931 // Ensure that wire is not reset if specified InterestSignatureInfo is same
932 i.wireEncode();
933 BOOST_CHECK_EQUAL(i.hasWire(), true);
934 i.setSignatureInfo(si1);
935 BOOST_CHECK_EQUAL(i.hasWire(), true);
936 BOOST_CHECK(i.getSignatureInfo() == si1);
937 BOOST_CHECK_EQUAL(i.getSignatureValue(), sv1);
938 BOOST_CHECK_EQUAL(i.isSigned(), true);
939
940 // Ensure that wire is reset if specified InterestSignatureInfo is different
941 i.wireEncode();
942 BOOST_CHECK_EQUAL(i.hasWire(), true);
943 SignatureInfo si2(tlv::SignatureSha256WithRsa);
944 i.setSignatureInfo(si2);
945 BOOST_CHECK_EQUAL(i.hasWire(), false);
946 BOOST_CHECK(i.getSignatureInfo() == si2);
947 BOOST_CHECK_EQUAL(i.getSignatureValue(), sv1);
948 BOOST_CHECK_EQUAL(i.isSigned(), true);
949
950 // Ensure that wire is not reset if specified InterestSignatureValue is same
951 i.wireEncode();
952 BOOST_CHECK_EQUAL(i.hasWire(), true);
Davide Pesavento487e3d32022-05-05 18:06:23 -0400953 i.setSignatureValue(sv1.value_bytes());
Eric Newberry6e262f02020-05-29 23:11:25 -0700954 BOOST_CHECK_EQUAL(i.hasWire(), true);
955 BOOST_CHECK(i.getSignatureInfo() == si2);
956 BOOST_CHECK_EQUAL(i.getSignatureValue(), sv1);
957 BOOST_CHECK_EQUAL(i.isSigned(), true);
958
959 // Ensure that wire is reset if specified InterestSignatureValue is different
960 i.wireEncode();
961 BOOST_CHECK_EQUAL(i.hasWire(), true);
Davide Pesavento487e3d32022-05-05 18:06:23 -0400962 const uint8_t sv2[] = {0x99, 0x88, 0x77, 0x66};
963 i.setSignatureValue(sv2);
Eric Newberry6e262f02020-05-29 23:11:25 -0700964 BOOST_CHECK_EQUAL(i.hasWire(), false);
965 BOOST_CHECK(i.getSignatureInfo() == si2);
Davide Pesavento487e3d32022-05-05 18:06:23 -0400966 BOOST_TEST(i.getSignatureValue().value_bytes() == sv2, boost::test_tools::per_element());
Eric Newberry6e262f02020-05-29 23:11:25 -0700967 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
Davide Pesaventof6b45892023-03-13 15:00:51 -04001019 BOOST_CHECK(i.getSignatureInfo() == std::nullopt);
Eric Newberry6e262f02020-05-29 23:11:25 -07001020 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);
Davide Pesavento487e3d32022-05-05 18:06:23 -04001031 i.setSignatureValue(sv.value_bytes()); // updates ParametersDigestSha256Component
Eric Newberry6e262f02020-05-29 23:11:25 -07001032 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
Davide Pesavento487e3d32022-05-05 18:06:23 -04001074 i1.setSignatureValue(std::make_shared<Buffer>());
Eric Newberryb74bbda2020-06-18 19:33:58 -07001075 auto ranges2 = i1.extractSignedRanges();
1076 BOOST_REQUIRE_EQUAL(ranges2.size(), 2);
1077 const auto& wire2 = i1.wireEncode();
1078 // Ensure Name range captured properly
1079 Block nameWithoutDigest2 = i1.getName().getPrefix(-1).wireEncode();
Davide Pesavento765abc92021-12-27 00:44:04 -05001080 BOOST_CHECK_EQUAL_COLLECTIONS(ranges2.front().begin(), ranges2.front().end(),
Eric Newberryb74bbda2020-06-18 19:33:58 -07001081 nameWithoutDigest2.value_begin(), nameWithoutDigest2.value_end());
1082 // Ensure parameters range captured properly
1083 const auto& appParamsWire2 = wire2.find(tlv::ApplicationParameters);
1084 BOOST_REQUIRE(appParamsWire2 != wire2.elements_end());
1085 const auto& sigValueWire2 = wire2.find(tlv::InterestSignatureValue);
1086 BOOST_REQUIRE(sigValueWire2 != wire2.elements_end());
Davide Pesavento765abc92021-12-27 00:44:04 -05001087 BOOST_CHECK_EQUAL_COLLECTIONS(ranges2.back().begin(), ranges2.back().end(),
Eric Newberryb74bbda2020-06-18 19:33:58 -07001088 appParamsWire2->begin(), sigValueWire2->begin());
1089
1090 // Test with decoded Interest
1091 const uint8_t WIRE[] = {
1092 0x05, 0x6f, // Interest
1093 0x07, 0x2e, // Name
1094 0x08, 0x04, // GenericNameComponent
1095 0x61, 0x62, 0x63, 0x64,
1096 0x08, 0x04, // GenericNameComponent
1097 0x65, 0x66, 0x67, 0x68,
1098 0x02, 0x20, // ParametersSha256DigestComponent
1099 0x6f, 0x29, 0x58, 0x60, 0x53, 0xee, 0x9f, 0xcc,
1100 0xd8, 0xa4, 0x22, 0x12, 0x29, 0x25, 0x28, 0x7c,
1101 0x0a, 0x18, 0x43, 0x5f, 0x40, 0x74, 0xc4, 0x0a,
1102 0xbb, 0x0d, 0x5b, 0x30, 0xe4, 0xaa, 0x62, 0x20,
1103 0x12, 0x00, // MustBeFresh
1104 0x0a, 0x04, // Nonce
1105 0x4c, 0x1e, 0xcb, 0x4a,
1106 0x24, 0x04, // ApplicationParameters
1107 0xc0, 0xc1, 0xc2, 0xc3,
1108 0x2c, 0x0d, // InterestSignatureInfo
1109 0x1b, 0x01, // SignatureType
1110 0x00,
1111 0x26, 0x08, // SignatureNonce
1112 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
1113 0x2e, 0x20, // InterestSignatureValue
1114 0x12, 0x47, 0x1a, 0xe0, 0xf8, 0x72, 0x3a, 0xc1,
1115 0x15, 0x6c, 0x37, 0x0a, 0x38, 0x71, 0x1e, 0xbe,
1116 0xbf, 0x28, 0x17, 0xde, 0x9b, 0x2d, 0xd9, 0x4e,
1117 0x9b, 0x7e, 0x62, 0xf1, 0x17, 0xb8, 0x76, 0xc1,
1118 };
Davide Pesaventofbea4fc2022-02-08 07:26:04 -05001119 Block wire3(WIRE);
Eric Newberryb74bbda2020-06-18 19:33:58 -07001120 Interest i2(wire3);
1121 auto ranges3 = i2.extractSignedRanges();
1122 BOOST_REQUIRE_EQUAL(ranges3.size(), 2);
1123 // Ensure Name range captured properly
Davide Pesavento765abc92021-12-27 00:44:04 -05001124 BOOST_CHECK_EQUAL_COLLECTIONS(ranges3.front().begin(), ranges3.front().end(), &WIRE[4], &WIRE[16]);
Eric Newberryb74bbda2020-06-18 19:33:58 -07001125 // Ensure parameters range captured properly
Davide Pesavento765abc92021-12-27 00:44:04 -05001126 BOOST_CHECK_EQUAL_COLLECTIONS(ranges3.back().begin(), ranges3.back().end(), &WIRE[58], &WIRE[79]);
Eric Newberryb74bbda2020-06-18 19:33:58 -07001127
1128 // Test failure with missing ParametersSha256DigestComponent
1129 Interest i3("/a");
Eric Newberryb74bbda2020-06-18 19:33:58 -07001130 BOOST_CHECK_EXCEPTION(i3.extractSignedRanges(), tlv::Error, [] (const auto& e) {
1131 return e.what() == "Interest Name must end with a ParametersSha256DigestComponent"s;
1132 });
1133
1134 // Test failure with missing InterestSignatureInfo
Davide Pesaventoa3d809e2022-02-06 11:55:02 -05001135 i3.setApplicationParameters(span<uint8_t>{});
Eric Newberryb74bbda2020-06-18 19:33:58 -07001136 BOOST_CHECK_EXCEPTION(i3.extractSignedRanges(), tlv::Error, [] (const auto& e) {
1137 return e.what() == "Interest missing InterestSignatureInfo"s;
1138 });
1139}
1140
Davide Pesavento2fdb2742019-07-31 23:03:35 -04001141BOOST_AUTO_TEST_CASE(ToUri)
1142{
1143 Interest i;
Davide Pesavento2fdb2742019-07-31 23:03:35 -04001144 BOOST_CHECK_EQUAL(i.toUri(), "/");
1145
1146 i.setName("/foo");
1147 BOOST_CHECK_EQUAL(i.toUri(), "/foo");
1148
1149 i.setCanBePrefix(true);
1150 BOOST_CHECK_EQUAL(i.toUri(), "/foo?CanBePrefix");
1151
1152 i.setMustBeFresh(true);
1153 BOOST_CHECK_EQUAL(i.toUri(), "/foo?CanBePrefix&MustBeFresh");
1154
Davide Pesavento53533942020-03-04 23:10:06 -05001155 i.setNonce(0xa1b2c3);
1156 BOOST_CHECK_EQUAL(i.toUri(), "/foo?CanBePrefix&MustBeFresh&Nonce=00a1b2c3");
Davide Pesavento2fdb2742019-07-31 23:03:35 -04001157
1158 i.setInterestLifetime(2_s);
Davide Pesavento53533942020-03-04 23:10:06 -05001159 BOOST_CHECK_EQUAL(i.toUri(), "/foo?CanBePrefix&MustBeFresh&Nonce=00a1b2c3&Lifetime=2000");
Davide Pesavento2fdb2742019-07-31 23:03:35 -04001160
1161 i.setHopLimit(18);
Davide Pesavento53533942020-03-04 23:10:06 -05001162 BOOST_CHECK_EQUAL(i.toUri(), "/foo?CanBePrefix&MustBeFresh&Nonce=00a1b2c3&Lifetime=2000&HopLimit=18");
Davide Pesavento2fdb2742019-07-31 23:03:35 -04001163
1164 i.setCanBePrefix(false);
1165 i.setMustBeFresh(false);
Davide Pesaventof6b45892023-03-13 15:00:51 -04001166 i.setHopLimit(std::nullopt);
Davide Pesavento2fdb2742019-07-31 23:03:35 -04001167 i.setApplicationParameters("2402CAFE"_block);
1168 BOOST_CHECK_EQUAL(i.toUri(),
1169 "/foo/params-sha256=8621f5e8321f04104640c8d02877d7c5142cad6e203c5effda1783b1a0e476d6"
Davide Pesavento53533942020-03-04 23:10:06 -05001170 "?Nonce=00a1b2c3&Lifetime=2000");
Davide Pesavento2fdb2742019-07-31 23:03:35 -04001171}
1172
Davide Pesaventoeee3e822016-11-26 19:19:34 +01001173BOOST_AUTO_TEST_SUITE_END() // TestInterest
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -08001174
Alexander Afanasyev90164962014-03-06 08:29:59 +00001175} // namespace tests
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -08001176} // namespace ndn