blob: e5c7d32a285b48c1ae7fc5fd4af783d02eb4d3b2 [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 Pesavento53533942020-03-04 23:10:06 -05003 * Copyright (c) 2013-2020 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(), "/");
Junxiao Shi6efa3b72018-04-14 15:54:08 +000055 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
56 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[] = {
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400156 0x05, 0x5c, // 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
167 0x1e, 0x0b, // ForwardingHint
168 0x1f, 0x09, // Delegation List
169 0x1e, 0x02,
170 0x3e, 0x15,
171 0x07, 0x03,
172 0x08, 0x01, 0x48,
173 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);
187 i1.setForwardingHint(DelegationList({{15893, "/H"}}));
188 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();
195 BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
196
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);
202 BOOST_CHECK_EQUAL(i2.getForwardingHint(), DelegationList({{15893, "/H"}}));
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
248 Interest i1(Block(WIRE, sizeof(WIRE)));
249 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);
257 BOOST_CHECK_EQUAL_COLLECTIONS(i1.getSignatureValue().begin(), i1.getSignatureValue().end(),
258 sv.begin(), sv.end());
259 BOOST_CHECK_EQUAL(i1.getApplicationParameters(), "2404C0C1C2C3"_block);
260 BOOST_CHECK_EQUAL(i1.isParametersDigestValid(), true);
261
262 // Reset wire
263 BOOST_CHECK_EQUAL(i1.hasWire(), true);
264 i1.setCanBePrefix(true);
265 i1.setCanBePrefix(false);
266 BOOST_CHECK_EQUAL(i1.hasWire(), false);
267
268 Block wire1 = i1.wireEncode();
269 BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
270
271 Interest i2("/local/ndn/prefix");
272 i2.setCanBePrefix(false);
273 i2.setMustBeFresh(true);
274 i2.setNonce(0x4c1ecb4a);
275 i2.setApplicationParameters("2404C0C1C2C3"_block);
276 i2.setSignatureInfo(si);
277 i2.setSignatureValue(make_shared<Buffer>(sv.value(), sv.value_size()));
278 BOOST_CHECK_EQUAL(i2.isParametersDigestValid(), true);
279
280 Block wire2 = i2.wireEncode();
281 BOOST_CHECK_EQUAL_COLLECTIONS(wire2.begin(), wire2.end(), WIRE, WIRE + sizeof(WIRE));
282}
283
284BOOST_AUTO_TEST_CASE(SignedApplicationElements)
285{
286 const uint8_t WIRE[] = {
287 0x05, 0x8f, // Interest
288 0x07, 0x36, // Name
289 0x08, 0x05, // GenericNameComponent
290 0x6c, 0x6f, 0x63, 0x61, 0x6c,
291 0x08, 0x03, // GenericNameComponent
292 0x6e, 0x64, 0x6e,
293 0x08, 0x06, // GenericNameComponent
294 0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
295 0x02, 0x20, // ParametersSha256DigestComponent
296 0xbc, 0x36, 0x30, 0xa4, 0xd6, 0x5e, 0x0d, 0xb5,
297 0x48, 0x3d, 0xfa, 0x0d, 0x28, 0xb3, 0x31, 0x2f,
298 0xca, 0xc1, 0xd4, 0x41, 0xec, 0x89, 0x61, 0xd4,
299 0x17, 0x5e, 0x61, 0x75, 0x17, 0x78, 0x10, 0x8e,
300 0x12, 0x00, // MustBeFresh
301 0x0a, 0x04, // Nonce
302 0x4c, 0x1e, 0xcb, 0x4a,
303 0x24, 0x04, // ApplicationParameters
304 0xc0, 0xc1, 0xc2, 0xc3,
305 0xfd, 0x01, 0xfe, 0x08, // Application-specific element (Type 510)
306 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80,
307 0x2c, 0x0d, // InterestSignatureInfo
308 0x1b, 0x01, // SignatureType
309 0x00,
310 0x26, 0x08, // SignatureNonce
311 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
312 0x2e, 0x20, // InterestSignatureValue
313 0x12, 0x47, 0x1a, 0xe0, 0xf8, 0x72, 0x3a, 0xc1,
314 0x15, 0x6c, 0x37, 0x0a, 0x38, 0x71, 0x1e, 0xbe,
315 0xbf, 0x28, 0x17, 0xde, 0x9b, 0x2d, 0xd9, 0x4e,
316 0x9b, 0x7e, 0x62, 0xf1, 0x17, 0xb8, 0x76, 0xc1,
317 0xfd, 0x02, 0x00, 0x08, // Application-specific element (Type 512)
318 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88
319 };
320
321 SignatureInfo si(tlv::DigestSha256);
322 std::vector<uint8_t> nonce{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
323 si.setNonce(nonce);
324 Block sv("2E20 12471AE0F8723AC1156C370A38711EBEBF2817DE9B2DD94E9B7E62F117B876C1"_block);
325
326 Interest i1(Block(WIRE, sizeof(WIRE)));
327 BOOST_CHECK_EQUAL(i1.getName(),
328 "/local/ndn/prefix/params-sha256=bc3630a4d65e0db5483dfa0d28b3312fcac1d441ec8961d4175e61751778108e");
329 BOOST_CHECK_EQUAL(i1.getCanBePrefix(), false);
330 BOOST_CHECK_EQUAL(i1.getMustBeFresh(), true);
331 BOOST_CHECK_EQUAL(i1.hasNonce(), true);
332 BOOST_CHECK_EQUAL(i1.getNonce(), 0x4c1ecb4a);
333 BOOST_CHECK_EQUAL(i1.getSignatureInfo()->getSignatureType(), tlv::DigestSha256);
334 BOOST_CHECK(i1.getSignatureInfo()->getNonce() == nonce);
335 BOOST_CHECK_EQUAL_COLLECTIONS(i1.getSignatureValue().begin(), i1.getSignatureValue().end(),
336 sv.begin(), sv.end());
337 BOOST_CHECK_EQUAL(i1.getApplicationParameters(), "2404C0C1C2C3"_block);
338 BOOST_CHECK_EQUAL(i1.isParametersDigestValid(), true);
339
340 // Reset wire
341 BOOST_CHECK_EQUAL(i1.hasWire(), true);
342 i1.setCanBePrefix(true);
343 i1.setCanBePrefix(false);
344 BOOST_CHECK_EQUAL(i1.hasWire(), false);
345
346 Block wire1 = i1.wireEncode();
347 BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
348}
349
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400350BOOST_AUTO_TEST_CASE(MissingApplicationParameters)
351{
352 Interest i;
353 i.setName(Name("/A").appendParametersSha256DigestPlaceholder());
354 i.setCanBePrefix(false);
355 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
Davide Pesavento905d40f2020-06-09 21:33:25 -0400356 BOOST_CHECK_EXCEPTION(i.wireEncode(), tlv::Error, [] (const auto& e) {
357 return e.what() == "Interest without parameters must not have a ParametersSha256DigestComponent"s;
358 });
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400359}
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400360
361BOOST_AUTO_TEST_CASE(MissingParametersSha256DigestComponent)
362{
363 // there's no way to create an Interest that fails this check via programmatic construction,
364 // so we have to decode an invalid Interest and force reencoding
365
366 DisableAutoCheckParametersDigest disabler;
367 Interest i("050F 0703(080149) 0A04F000F000 2402CAFE"_block);
368 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
369 BOOST_CHECK_NO_THROW(i.wireEncode()); // this succeeds because it uses the cached wire encoding
370
Davide Pesavento905d40f2020-06-09 21:33:25 -0400371 // trigger reencoding
372 i.setNonce(42);
373 // now the check fails while attempting to reencode
374 BOOST_CHECK_EXCEPTION(i.wireEncode(), tlv::Error, [] (const auto& e) {
375 return e.what() == "Interest with parameters must have a ParametersSha256DigestComponent"s;
376 });
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400377}
378
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400379BOOST_AUTO_TEST_SUITE_END() // Encode
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400380
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400381class DecodeFixture
Junxiao Shi899277a2017-07-07 22:12:12 +0000382{
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000383protected:
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400384 DecodeFixture()
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000385 {
386 // initialize all elements to non-empty, to verify wireDecode clears them
387 i.setName("/A");
388 i.setForwardingHint({{10309, "/F"}});
389 i.setNonce(0x03d645a8);
390 i.setInterestLifetime(18554_ms);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400391 i.setHopLimit(64);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400392 i.setApplicationParameters("2404A0A1A2A3"_block);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000393 }
Junxiao Shi899277a2017-07-07 22:12:12 +0000394
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000395protected:
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000396 Interest i;
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000397};
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000398
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400399BOOST_FIXTURE_TEST_SUITE(Decode, DecodeFixture)
400
401BOOST_AUTO_TEST_CASE(NotAnInterest)
402{
Davide Pesavento905d40f2020-06-09 21:33:25 -0400403 BOOST_CHECK_EXCEPTION(i.wireDecode("4202CAFE"_block), tlv::Error, [] (const auto& e) {
404 return e.what() == "Expecting Interest element, but TLV has type 66"s;
405 });
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400406}
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000407
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400408BOOST_AUTO_TEST_CASE(NameOnly)
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000409{
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400410 i.wireDecode("0505 0703(080149)"_block);
Davide Pesavento835f0272019-09-21 13:18:24 -0400411 BOOST_CHECK_EQUAL(i.hasWire(), true);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000412 BOOST_CHECK_EQUAL(i.getName(), "/I");
413 BOOST_CHECK_EQUAL(i.getCanBePrefix(), false);
414 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400415 BOOST_CHECK_EQUAL(i.getForwardingHint().empty(), true);
Davide Pesavento835f0272019-09-21 13:18:24 -0400416 BOOST_CHECK_EQUAL(i.hasNonce(), false);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000417 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400418 BOOST_CHECK(i.getHopLimit() == nullopt);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400419 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), false);
420 BOOST_CHECK_EQUAL(i.getApplicationParameters().isValid(), false);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000421
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400422 // modify then re-encode
Davide Pesavento53533942020-03-04 23:10:06 -0500423 i.setNonce(0x957c6554);
Davide Pesavento835f0272019-09-21 13:18:24 -0400424 BOOST_CHECK_EQUAL(i.hasWire(), false);
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400425 BOOST_CHECK_EQUAL(i.wireEncode(), "050B 0703(080149) 0A04957C6554"_block);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000426}
427
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400428BOOST_AUTO_TEST_CASE(NameCanBePrefix)
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000429{
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400430 i.wireDecode("0507 0703(080149) 2100"_block);
Davide Pesavento835f0272019-09-21 13:18:24 -0400431 BOOST_CHECK_EQUAL(i.hasWire(), true);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400432 BOOST_CHECK_EQUAL(i.getName(), "/I");
433 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
434 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
435 BOOST_CHECK_EQUAL(i.getForwardingHint().empty(), true);
Davide Pesavento835f0272019-09-21 13:18:24 -0400436 BOOST_CHECK_EQUAL(i.hasNonce(), false);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400437 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400438 BOOST_CHECK(i.getHopLimit() == nullopt);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400439 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), false);
440 BOOST_CHECK_EQUAL(i.getApplicationParameters().isValid(), false);
441}
442
443BOOST_AUTO_TEST_CASE(FullWithoutParameters)
444{
445 i.wireDecode("0531 0703(080149) "
446 "FC00 2100 FC00 1200 FC00 1E0B(1F09 1E023E15 0703080148) "
447 "FC00 0A044ACB1E4C FC00 0C0276A1 FC00 2201D6 FC00"_block);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000448 BOOST_CHECK_EQUAL(i.getName(), "/I");
449 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
450 BOOST_CHECK_EQUAL(i.getMustBeFresh(), true);
451 BOOST_CHECK_EQUAL(i.getForwardingHint(), DelegationList({{15893, "/H"}}));
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400452 BOOST_CHECK_EQUAL(i.hasNonce(), true);
Davide Pesavento53533942020-03-04 23:10:06 -0500453 BOOST_CHECK_EQUAL(i.getNonce(), 0x4acb1e4c);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000454 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 30369_ms);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400455 BOOST_CHECK_EQUAL(*i.getHopLimit(), 214);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400456 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), false);
457 BOOST_CHECK_EQUAL(i.getApplicationParameters().isValid(), false);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000458
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000459 // encode without modification: retain original wire encoding
Junxiao Shi8b753a22018-10-24 01:51:40 +0000460 BOOST_CHECK_EQUAL(i.wireEncode().value_size(), 49);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000461
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400462 // modify then re-encode: unrecognized elements are discarded
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000463 i.setName("/J");
Junxiao Shi72c0c642018-04-20 15:41:09 +0000464 BOOST_CHECK_EQUAL(i.wireEncode(),
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400465 "0523 0703(08014A) "
466 "2100 1200 1E0B(1F09 1E023E15 0703080148) "
467 "0A044ACB1E4C 0C0276A1 2201D6"_block);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400468}
469
470BOOST_AUTO_TEST_CASE(FullWithParameters)
471{
472 i.wireDecode("055B 0725(080149 0220F16DB273F40436A852063F864D5072B01EAD53151F5A688EA1560492BEBEDD05) "
473 "FC00 2100 FC00 1200 FC00 1E0B(1F09 1E023E15 0703080148) "
474 "FC00 0A044ACB1E4C FC00 0C0276A1 FC00 2201D6 FC00 2404C0C1C2C3 FC00"_block);
475 BOOST_CHECK_EQUAL(i.getName(),
476 "/I/params-sha256=f16db273f40436a852063f864d5072b01ead53151f5a688ea1560492bebedd05");
477 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
478 BOOST_CHECK_EQUAL(i.getMustBeFresh(), true);
479 BOOST_CHECK_EQUAL(i.getForwardingHint(), DelegationList({{15893, "/H"}}));
480 BOOST_CHECK_EQUAL(i.hasNonce(), true);
Davide Pesavento53533942020-03-04 23:10:06 -0500481 BOOST_CHECK_EQUAL(i.getNonce(), 0x4acb1e4c);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400482 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 30369_ms);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400483 BOOST_CHECK_EQUAL(*i.getHopLimit(), 214);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400484 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true);
485 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2404C0C1C2C3"_block);
486
487 // encode without modification: retain original wire encoding
488 BOOST_CHECK_EQUAL(i.wireEncode().value_size(), 91);
489
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400490 // modify then re-encode: unrecognized elements after ApplicationParameters
491 // are preserved, the rest are discarded
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400492 i.setName("/J");
493 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
494 BOOST_CHECK_EQUAL(i.wireEncode(),
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400495 "054D 0725(08014A 0220F16DB273F40436A852063F864D5072B01EAD53151F5A688EA1560492BEBEDD05) "
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400496 "2100 1200 1E0B(1F09 1E023E15 0703080148) "
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400497 "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3 FC00"_block);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400498
499 // modify ApplicationParameters: unrecognized elements are preserved
500 i.setApplicationParameters("2402CAFE"_block);
501 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
502 BOOST_CHECK_EQUAL(i.wireEncode(),
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400503 "054B 0725(08014A 02205FDA67967EE302FC457E41B7D3D51BA6A9379574D193FD88F64954BF16C2927A) "
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400504 "2100 1200 1E0B(1F09 1E023E15 0703080148) "
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400505 "0A044ACB1E4C 0C0276A1 2201D6 2402CAFE FC00"_block);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000506}
507
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000508BOOST_AUTO_TEST_CASE(CriticalElementOutOfOrder)
509{
Davide Pesavento905d40f2020-06-09 21:33:25 -0400510 BOOST_CHECK_EXCEPTION(i.wireDecode(
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000511 "0529 2100 0703080149 1200 1E0B(1F09 1E023E15 0703080148) "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500512 "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3"_block),
Davide Pesavento905d40f2020-06-09 21:33:25 -0400513 tlv::Error,
514 [] (const auto& e) { return e.what() == "Name element is missing or out of order"s; });
515 BOOST_CHECK_EXCEPTION(i.wireDecode(
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000516 "0529 0703080149 1200 2100 1E0B(1F09 1E023E15 0703080148) "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500517 "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3"_block),
Davide Pesavento905d40f2020-06-09 21:33:25 -0400518 tlv::Error,
519 [] (const auto& e) { return e.what() == "CanBePrefix element is out of order"s; });
520 BOOST_CHECK_EXCEPTION(i.wireDecode(
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000521 "0529 0703080149 2100 1E0B(1F09 1E023E15 0703080148) 1200 "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500522 "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3"_block),
Davide Pesavento905d40f2020-06-09 21:33:25 -0400523 tlv::Error,
524 [] (const auto& e) { return e.what() == "MustBeFresh element is out of order"s; });
525 BOOST_CHECK_EXCEPTION(i.wireDecode(
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000526 "0529 0703080149 2100 1200 0A044ACB1E4C "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500527 "1E0B(1F09 1E023E15 0703080148) 0C0276A1 2201D6 2404C0C1C2C3"_block),
Davide Pesavento905d40f2020-06-09 21:33:25 -0400528 tlv::Error,
529 [] (const auto& e) { return e.what() == "ForwardingHint element is out of order"s; });
530 BOOST_CHECK_EXCEPTION(i.wireDecode(
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000531 "0529 0703080149 2100 1200 1E0B(1F09 1E023E15 0703080148) "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500532 "0C0276A1 0A044ACB1E4C 2201D6 2404C0C1C2C3"_block),
Davide Pesavento905d40f2020-06-09 21:33:25 -0400533 tlv::Error,
534 [] (const auto& e) { return e.what() == "Nonce element is out of order"s; });
535 BOOST_CHECK_EXCEPTION(i.wireDecode(
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000536 "0529 0703080149 2100 1200 1E0B(1F09 1E023E15 0703080148) "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500537 "0A044ACB1E4C 2201D6 0C0276A1 2404C0C1C2C3"_block),
Davide Pesavento905d40f2020-06-09 21:33:25 -0400538 tlv::Error,
539 [] (const auto& e) { return e.what() == "InterestLifetime element is out of order"s; });
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000540}
541
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500542BOOST_AUTO_TEST_CASE(NonCriticalElementOutOfOrder)
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000543{
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400544 // duplicate HopLimit
545 i.wireDecode("0536 0725(080149 0220FF9100E04EAADCF30674D98026A051BA25F56B69BFA026DCCCD72C6EA0F7315A)"
546 "2201D6 2200 2404C0C1C2C3 22020101"_block);
547 BOOST_CHECK_EQUAL(i.getName(),
548 "/I/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a");
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400549 BOOST_CHECK_EQUAL(*i.getHopLimit(), 214);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400550 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400551 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2404C0C1C2C3"_block);
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500552
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400553 // duplicate ApplicationParameters
554 i.wireDecode("0541 0725(080149 0220FF9100E04EAADCF30674D98026A051BA25F56B69BFA026DCCCD72C6EA0F7315A)"
555 "2100 1200 0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3 2401EE"_block);
556 BOOST_CHECK_EQUAL(i.getName(),
557 "/I/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a");
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400558 BOOST_CHECK_EQUAL(*i.getHopLimit(), 214);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400559 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true);
560 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2404C0C1C2C3"_block);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000561}
562
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400563BOOST_AUTO_TEST_CASE(MissingName)
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000564{
Davide Pesavento905d40f2020-06-09 21:33:25 -0400565 BOOST_CHECK_EXCEPTION(i.wireDecode("0500"_block), tlv::Error,
566 [] (const auto& e) { return e.what() == "Name element is missing or out of order"s; });
567 BOOST_CHECK_EXCEPTION(i.wireDecode("0502 1200"_block), tlv::Error,
568 [] (const auto& e) { return e.what() == "Name element is missing or out of order"s; });
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000569}
570
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400571BOOST_AUTO_TEST_CASE(BadName)
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000572{
Davide Pesavento905d40f2020-06-09 21:33:25 -0400573 BOOST_CHECK_EXCEPTION(i.wireDecode("0502 0700"_block), tlv::Error,
574 [] (const auto& e) { return e.what() == "Name has zero name components"s; });
575 BOOST_CHECK_EXCEPTION(i.wireDecode("054C 074A(080149"
576 "02200000000000000000000000000000000000000000000000000000000000000000"
577 "080132"
578 "02200000000000000000000000000000000000000000000000000000000000000000)"_block),
579 tlv::Error,
580 [] (const auto& e) { return e.what() == "Name has more than one ParametersSha256DigestComponent"s; });
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000581}
582
583BOOST_AUTO_TEST_CASE(BadCanBePrefix)
584{
Davide Pesavento905d40f2020-06-09 21:33:25 -0400585 BOOST_CHECK_EXCEPTION(i.wireDecode("0508 0703080149 210102"_block), tlv::Error,
586 [] (const auto& e) { return e.what() == "CanBePrefix element has non-zero TLV-LENGTH"s; });
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000587}
588
589BOOST_AUTO_TEST_CASE(BadMustBeFresh)
590{
Davide Pesavento905d40f2020-06-09 21:33:25 -0400591 BOOST_CHECK_EXCEPTION(i.wireDecode("0508 0703080149 120102"_block), tlv::Error,
592 [] (const auto& e) { return e.what() == "MustBeFresh element has non-zero TLV-LENGTH"s; });
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000593}
594
595BOOST_AUTO_TEST_CASE(BadNonce)
596{
Davide Pesavento905d40f2020-06-09 21:33:25 -0400597 BOOST_CHECK_EXCEPTION(i.wireDecode("0507 0703080149 0A00"_block), tlv::Error,
598 [] (const auto& e) { return e.what() == "Nonce element is malformed"s; });
599 BOOST_CHECK_EXCEPTION(i.wireDecode("050A 0703080149 0A0304C263"_block), tlv::Error,
600 [] (const auto& e) { return e.what() == "Nonce element is malformed"s; });
601 BOOST_CHECK_EXCEPTION(i.wireDecode("050C 0703080149 0A05EFA420B262"_block), tlv::Error,
602 [] (const auto& e) { return e.what() == "Nonce element is malformed"s; });
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000603}
604
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500605BOOST_AUTO_TEST_CASE(BadHopLimit)
606{
Davide Pesavento905d40f2020-06-09 21:33:25 -0400607 BOOST_CHECK_EXCEPTION(i.wireDecode("0507 0703080149 2200"_block), tlv::Error,
608 [] (const auto& e) { return e.what() == "HopLimit element is malformed"s; });
609 BOOST_CHECK_EXCEPTION(i.wireDecode("0509 0703080149 22021356"_block), tlv::Error,
610 [] (const auto& e) { return e.what() == "HopLimit element is malformed"s; });
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500611}
612
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400613BOOST_AUTO_TEST_CASE(BadParametersDigest)
614{
615 // ApplicationParameters without ParametersSha256DigestComponent
616 Block b1("0509 0703(080149) 2402CAFE"_block);
617 // ParametersSha256DigestComponent without ApplicationParameters
618 Block b2("0527 0725(080149 0220E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855)"_block);
619 // digest mismatch
620 Block b3("052B 0725(080149 02200000000000000000000000000000000000000000000000000000000000000000) "
621 "2402CAFE"_block);
622
623 BOOST_CHECK_THROW(i.wireDecode(b1), tlv::Error);
624 BOOST_CHECK_THROW(i.wireDecode(b2), tlv::Error);
625 BOOST_CHECK_THROW(i.wireDecode(b3), tlv::Error);
626
627 DisableAutoCheckParametersDigest disabler;
628 BOOST_CHECK_NO_THROW(i.wireDecode(b1));
629 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
630 BOOST_CHECK_NO_THROW(i.wireDecode(b2));
631 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
632 BOOST_CHECK_NO_THROW(i.wireDecode(b3));
633 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
634}
635
Junxiao Shi8b753a22018-10-24 01:51:40 +0000636BOOST_AUTO_TEST_CASE(UnrecognizedNonCriticalElementBeforeName)
637{
Davide Pesavento905d40f2020-06-09 21:33:25 -0400638 BOOST_CHECK_EXCEPTION(i.wireDecode("0507 FC00 0703080149"_block), tlv::Error,
639 [] (const auto& e) { return e.what() == "Name element is missing or out of order"s; });
Junxiao Shi8b753a22018-10-24 01:51:40 +0000640}
641
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000642BOOST_AUTO_TEST_CASE(UnrecognizedCriticalElement)
643{
Davide Pesavento905d40f2020-06-09 21:33:25 -0400644 BOOST_CHECK_EXCEPTION(i.wireDecode("0507 0703080149 FB00"_block), tlv::Error,
645 [] (const auto& e) { return e.what() == "Unrecognized element of critical type 251"s; });
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400646 // v0.2 packet with Selectors
Davide Pesavento905d40f2020-06-09 21:33:25 -0400647 BOOST_CHECK_EXCEPTION(i.wireDecode("0510 0703080149 09030D0101 0A0401000000"_block), tlv::Error,
648 [] (const auto& e) { return e.what() == "Unrecognized element of critical type 9"s; });
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000649}
650
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400651BOOST_AUTO_TEST_SUITE_END() // Decode
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000652
Junxiao Shi899277a2017-07-07 22:12:12 +0000653BOOST_AUTO_TEST_CASE(MatchesData)
654{
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000655 auto interest = makeInterest("/A");
Junxiao Shi899277a2017-07-07 22:12:12 +0000656
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000657 auto data = makeData("/A");
658 BOOST_CHECK_EQUAL(interest->matchesData(*data), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000659
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000660 data->setName("/A/D");
661 BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // violates CanBePrefix
Junxiao Shi899277a2017-07-07 22:12:12 +0000662
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000663 interest->setCanBePrefix(true);
664 BOOST_CHECK_EQUAL(interest->matchesData(*data), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000665
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000666 interest->setMustBeFresh(true);
667 BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // violates MustBeFresh
Junxiao Shi899277a2017-07-07 22:12:12 +0000668
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000669 data->setFreshnessPeriod(1_s);
670 BOOST_CHECK_EQUAL(interest->matchesData(*data), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000671
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000672 data->setName("/H/I");
673 BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // Name does not match
Junxiao Shi899277a2017-07-07 22:12:12 +0000674
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000675 data->wireEncode();
676 interest = makeInterest(data->getFullName());
677 BOOST_CHECK_EQUAL(interest->matchesData(*data), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000678
Davide Pesavento53533942020-03-04 23:10:06 -0500679 setNameComponent(*interest, -1,
680 name::Component::fromEscapedString("sha256digest=00000000000000000000000000"
681 "00000000000000000000000000000000000000"));
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000682 BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // violates implicit digest
Junxiao Shi899277a2017-07-07 22:12:12 +0000683}
684
685BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(MatchesInterest, 1)
686BOOST_AUTO_TEST_CASE(MatchesInterest)
687{
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400688 Interest interest;
689 interest.setName("/A")
690 .setCanBePrefix(true)
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000691 .setMustBeFresh(true)
692 .setForwardingHint({{1, "/H"}})
693 .setNonce(2228)
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400694 .setInterestLifetime(5_s)
695 .setHopLimit(90);
Junxiao Shi899277a2017-07-07 22:12:12 +0000696
697 Interest other;
698 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false);
699
700 other.setName(interest.getName());
701 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false);
702
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000703 other.setCanBePrefix(interest.getCanBePrefix());
704 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false);
705
706 other.setMustBeFresh(interest.getMustBeFresh());
Junxiao Shi899277a2017-07-07 22:12:12 +0000707 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false); // will match until #3162 implemented
708
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000709 other.setForwardingHint(interest.getForwardingHint());
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;
740 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
741 i.setCanBePrefix(false);
742 BOOST_CHECK_EQUAL(i.getCanBePrefix(), false);
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000743 i.setCanBePrefix(true);
744 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
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
757BOOST_AUTO_TEST_CASE(ModifyForwardingHint)
758{
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400759 Interest i("/I");
Junxiao Shib55e5d32018-07-18 13:32:00 -0600760 i.setCanBePrefix(false);
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000761 i.setForwardingHint({{1, "/A"}});
762 i.wireEncode();
763 BOOST_CHECK(i.hasWire());
764
765 i.modifyForwardingHint([] (DelegationList& fh) { fh.insert(2, "/B"); });
766 BOOST_CHECK(!i.hasWire());
767 BOOST_CHECK_EQUAL(i.getForwardingHint(), DelegationList({{1, "/A"}, {2, "/B"}}));
768}
769
Junxiao Shi899277a2017-07-07 22:12:12 +0000770BOOST_AUTO_TEST_CASE(GetNonce)
771{
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000772 unique_ptr<Interest> i1, i2;
Davide Pesavento53533942020-03-04 23:10:06 -0500773 Interest::Nonce nonce1(0), nonce2(0);
Junxiao Shi899277a2017-07-07 22:12:12 +0000774
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000775 // getNonce automatically assigns a random Nonce.
776 // 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 -0500777 // identical Nonces in a row.
Junxiao Shi899277a2017-07-07 22:12:12 +0000778 int nIterations = 0;
779 do {
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000780 i1 = make_unique<Interest>();
781 nonce1 = i1->getNonce();
782 i2 = make_unique<Interest>();
783 nonce2 = i2->getNonce();
Junxiao Shi899277a2017-07-07 22:12:12 +0000784 }
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000785 while (nonce1 == nonce2 && ++nIterations < 100);
Davide Pesavento53533942020-03-04 23:10:06 -0500786
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000787 BOOST_CHECK_NE(nonce1, nonce2);
788 BOOST_CHECK(i1->hasNonce());
789 BOOST_CHECK(i2->hasNonce());
Junxiao Shi899277a2017-07-07 22:12:12 +0000790
791 // Once a Nonce is assigned, it should not change.
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000792 BOOST_CHECK_EQUAL(i1->getNonce(), nonce1);
Davide Pesavento53533942020-03-04 23:10:06 -0500793 BOOST_CHECK_EQUAL(i2->getNonce(), nonce2);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000794}
795
796BOOST_AUTO_TEST_CASE(SetNonce)
797{
798 Interest i1("/A");
Junxiao Shib55e5d32018-07-18 13:32:00 -0600799 i1.setCanBePrefix(false);
Davide Pesavento53533942020-03-04 23:10:06 -0500800 BOOST_CHECK(!i1.hasNonce());
801
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000802 i1.setNonce(1);
803 i1.wireEncode();
Davide Pesavento53533942020-03-04 23:10:06 -0500804 BOOST_CHECK(i1.hasNonce());
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000805 BOOST_CHECK_EQUAL(i1.getNonce(), 1);
806
807 Interest i2(i1);
Davide Pesavento53533942020-03-04 23:10:06 -0500808 BOOST_CHECK(i2.hasNonce());
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000809 BOOST_CHECK_EQUAL(i2.getNonce(), 1);
810
811 i2.setNonce(2);
812 BOOST_CHECK_EQUAL(i2.getNonce(), 2);
Davide Pesavento53533942020-03-04 23:10:06 -0500813 BOOST_CHECK_EQUAL(i1.getNonce(), 1); // should not affect i1's Nonce (Bug #4168)
814
815 i2.setNonce(nullopt);
816 BOOST_CHECK(!i2.hasNonce());
Junxiao Shi899277a2017-07-07 22:12:12 +0000817}
818
819BOOST_AUTO_TEST_CASE(RefreshNonce)
820{
821 Interest i;
822 BOOST_CHECK(!i.hasNonce());
823 i.refreshNonce();
824 BOOST_CHECK(!i.hasNonce());
825
826 i.setNonce(1);
827 BOOST_CHECK(i.hasNonce());
828 i.refreshNonce();
829 BOOST_CHECK(i.hasNonce());
830 BOOST_CHECK_NE(i.getNonce(), 1);
831}
832
Davide Pesavento53533942020-03-04 23:10:06 -0500833BOOST_AUTO_TEST_CASE(NonceConversions)
834{
835 Interest i;
836 i.setCanBePrefix(false);
837
838 // 4-arg constructor
839 Interest::Nonce n1(1, 2, 3, 4);
840 i.setNonce(n1);
841 BOOST_CHECK_EQUAL(i.getNonce(), 0x01020304);
842
843 // 4-arg constructor + assignment
844 n1 = {0xf, 0xe, 0xd, 0xc};
845 i.setNonce(n1);
846 BOOST_CHECK_EQUAL(i.getNonce(), 0x0f0e0d0c);
847
848 // 1-arg constructor + assignment (implicit conversion)
849 Interest::Nonce n2;
850 n2 = 42;
851 BOOST_CHECK_NE(n1, n2);
852 i.setNonce(n2);
853 n2 = 21; // should not affect i's Nonce
854 BOOST_CHECK_EQUAL(i.getNonce(), 42);
855 BOOST_CHECK_EQUAL(i.toUri(), "/?Nonce=0000002a"); // stored in big-endian
856}
857
Junxiao Shi899277a2017-07-07 22:12:12 +0000858BOOST_AUTO_TEST_CASE(SetInterestLifetime)
859{
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500860 BOOST_CHECK_THROW(Interest("/A", -1_ms), std::invalid_argument);
Davide Pesavento0f830802018-01-16 23:58:58 -0500861 BOOST_CHECK_NO_THROW(Interest("/A", 0_ms));
Junxiao Shi899277a2017-07-07 22:12:12 +0000862
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400863 Interest i;
Junxiao Shi899277a2017-07-07 22:12:12 +0000864 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500865 BOOST_CHECK_THROW(i.setInterestLifetime(-1_ms), std::invalid_argument);
Junxiao Shi899277a2017-07-07 22:12:12 +0000866 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesavento0f830802018-01-16 23:58:58 -0500867 i.setInterestLifetime(0_ms);
868 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 0_ms);
869 i.setInterestLifetime(1_ms);
870 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 1_ms);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400871
872 i = Interest("/B", 15_s);
873 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 15_s);
874}
875
876BOOST_AUTO_TEST_CASE(SetHopLimit)
877{
878 Interest i;
879 BOOST_CHECK(i.getHopLimit() == nullopt);
880 i.setHopLimit(42);
881 BOOST_CHECK(i.getHopLimit() == 42);
882 i.setHopLimit(nullopt);
883 BOOST_CHECK(i.getHopLimit() == nullopt);
Junxiao Shi899277a2017-07-07 22:12:12 +0000884}
885
Davide Pesavento9c19a392019-04-06 15:07:54 -0400886BOOST_AUTO_TEST_CASE(SetApplicationParameters)
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700887{
888 const uint8_t PARAMETERS1[] = {0xc1};
889 const uint8_t PARAMETERS2[] = {0xc2};
890
891 Interest i;
Davide Pesavento9c19a392019-04-06 15:07:54 -0400892 BOOST_CHECK(!i.hasApplicationParameters());
893 i.setApplicationParameters("2400"_block);
894 BOOST_CHECK(i.hasApplicationParameters());
895 i.unsetApplicationParameters();
896 BOOST_CHECK(!i.hasApplicationParameters());
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700897
Davide Pesavento38912442019-04-06 22:03:39 -0400898 // Block overload
Davide Pesavento38912442019-04-06 22:03:39 -0400899 i.setApplicationParameters("2401C0"_block);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400900 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2401C0"_block);
Davide Pesavento38912442019-04-06 22:03:39 -0400901 i.setApplicationParameters("8001C1"_block);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400902 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "24038001C1"_block);
Davide Pesavento81bd6962020-06-17 16:03:23 -0400903 BOOST_CHECK_THROW(i.setApplicationParameters(Block{}), std::invalid_argument);
Davide Pesavento38912442019-04-06 22:03:39 -0400904
905 // raw buffer+size overload
906 i.setApplicationParameters(PARAMETERS1, sizeof(PARAMETERS1));
907 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2401C1"_block);
908 i.setApplicationParameters(nullptr, 0);
909 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2400"_block);
910 BOOST_CHECK_THROW(i.setApplicationParameters(nullptr, 42), std::invalid_argument);
911
912 // ConstBufferPtr overload
913 i.setApplicationParameters(make_shared<Buffer>(PARAMETERS2, sizeof(PARAMETERS2)));
914 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2401C2"_block);
915 i.setApplicationParameters(make_shared<Buffer>());
916 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2400"_block);
917 BOOST_CHECK_THROW(i.setApplicationParameters(nullptr), std::invalid_argument);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700918}
919
Eric Newberry6e262f02020-05-29 23:11:25 -0700920BOOST_AUTO_TEST_CASE(SetSignature)
921{
922 Interest i;
923 i.setCanBePrefix(false);
924 BOOST_CHECK(i.getSignatureInfo() == nullopt);
925 BOOST_CHECK_EQUAL(i.isSigned(), false);
926
927 // Throws because attempting to set InterestSignatureValue without set InterestSignatureInfo
928 Block sv1("2E04 01020304"_block);
929 auto svBuffer1 = make_shared<Buffer>(sv1.value(), sv1.value_size());
930 BOOST_CHECK_THROW(i.setSignatureValue(svBuffer1), tlv::Error);
931
932 // Simple set/get case for InterestSignatureInfo (no prior set)
933 SignatureInfo si1(tlv::SignatureSha256WithEcdsa);
934 i.setSignatureInfo(si1);
935 BOOST_CHECK(i.getSignatureInfo() == si1);
936 BOOST_CHECK_EQUAL(i.isSigned(), false);
937
938 // Simple set/get case for InterestSignatureValue (no prior set)
939 BOOST_CHECK_EQUAL(i.getSignatureValue().isValid(), false);
940 i.setSignatureValue(svBuffer1);
941 BOOST_CHECK_EQUAL(i.getSignatureValue(), sv1);
942 BOOST_CHECK_EQUAL(i.isSigned(), true);
943
944 // Throws because attempting to set InterestSignatureValue to nullptr
945 BOOST_CHECK_THROW(i.setSignatureValue(nullptr), std::invalid_argument);
946 BOOST_CHECK_EQUAL(i.getSignatureValue(), sv1);
947 BOOST_CHECK_EQUAL(i.isSigned(), true);
948
949 // Ensure that wire is not reset if specified InterestSignatureInfo is same
950 i.wireEncode();
951 BOOST_CHECK_EQUAL(i.hasWire(), true);
952 i.setSignatureInfo(si1);
953 BOOST_CHECK_EQUAL(i.hasWire(), true);
954 BOOST_CHECK(i.getSignatureInfo() == si1);
955 BOOST_CHECK_EQUAL(i.getSignatureValue(), sv1);
956 BOOST_CHECK_EQUAL(i.isSigned(), true);
957
958 // Ensure that wire is reset if specified InterestSignatureInfo is different
959 i.wireEncode();
960 BOOST_CHECK_EQUAL(i.hasWire(), true);
961 SignatureInfo si2(tlv::SignatureSha256WithRsa);
962 i.setSignatureInfo(si2);
963 BOOST_CHECK_EQUAL(i.hasWire(), false);
964 BOOST_CHECK(i.getSignatureInfo() == si2);
965 BOOST_CHECK_EQUAL(i.getSignatureValue(), sv1);
966 BOOST_CHECK_EQUAL(i.isSigned(), true);
967
968 // Ensure that wire is not reset if specified InterestSignatureValue is same
969 i.wireEncode();
970 BOOST_CHECK_EQUAL(i.hasWire(), true);
971 i.setSignatureValue(svBuffer1);
972 BOOST_CHECK_EQUAL(i.hasWire(), true);
973 BOOST_CHECK(i.getSignatureInfo() == si2);
974 BOOST_CHECK_EQUAL(i.getSignatureValue(), sv1);
975 BOOST_CHECK_EQUAL(i.isSigned(), true);
976
977 // Ensure that wire is reset if specified InterestSignatureValue is different
978 i.wireEncode();
979 BOOST_CHECK_EQUAL(i.hasWire(), true);
980 Block sv2("2E04 99887766"_block);
981 auto svBuffer2 = make_shared<Buffer>(sv2.value(), sv2.value_size());
982 i.setSignatureValue(svBuffer2);
983 BOOST_CHECK_EQUAL(i.hasWire(), false);
984 BOOST_CHECK(i.getSignatureInfo() == si2);
985 BOOST_CHECK_EQUAL(i.getSignatureValue(), sv2);
986 BOOST_CHECK_EQUAL(i.isSigned(), true);
987}
988
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400989BOOST_AUTO_TEST_CASE(ParametersSha256DigestComponent)
990{
991 Interest i("/I");
992 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
993
994 i.setApplicationParameters("2404C0C1C2C3"_block); // auto-appends ParametersSha256DigestComponent
995 BOOST_CHECK_EQUAL(i.getName(),
996 "/I/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a");
997 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
998
999 i.setApplicationParameters(nullptr, 0); // updates ParametersSha256DigestComponent
1000 BOOST_CHECK_EQUAL(i.getName(),
1001 "/I/params-sha256=33b67cb5385ceddad93d0ee960679041613bed34b8b4a5e6362fe7539ba2d3ce");
1002 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true);
1003 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
1004
1005 i.unsetApplicationParameters(); // removes ParametersSha256DigestComponent
1006 BOOST_CHECK_EQUAL(i.getName(), "/I");
1007 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
1008
1009 i.setName(Name("/P").appendParametersSha256DigestPlaceholder().append("Q"));
1010 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), false);
1011 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
1012
1013 i.unsetApplicationParameters(); // removes ParametersSha256DigestComponent
1014 BOOST_CHECK_EQUAL(i.getName(), "/P/Q");
1015 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
1016
1017 i.setName(Name("/P").appendParametersSha256DigestPlaceholder().append("Q"));
1018 i.setApplicationParameters("2404C0C1C2C3"_block); // updates ParametersSha256DigestComponent
1019 BOOST_CHECK_EQUAL(i.getName(),
1020 "/P/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a/Q");
1021 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
1022
1023 i.setName("/A/B/C"); // auto-appends ParametersSha256DigestComponent
1024 BOOST_CHECK_EQUAL(i.getName(),
1025 "/A/B/C/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a");
1026 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true);
1027 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
Eric Newberry6e262f02020-05-29 23:11:25 -07001028
1029 SignatureInfo si(tlv::SignatureSha256WithEcdsa);
1030 i.setSignatureInfo(si); // updates ParametersSha256DigestComponent
1031 BOOST_CHECK_EQUAL(i.getName(),
1032 "/A/B/C/params-sha256=6400cae1730c15fd7854b26be05794d53685423c94bc61e59c49bd640d646ae8");
1033 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
1034 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2404 C0C1C2C3"_block);
1035 BOOST_CHECK(i.getSignatureInfo() == si);
1036
1037 i.unsetApplicationParameters(); // removes ParametersSha256DigestComponent and InterestSignatureInfo
1038 BOOST_CHECK(i.getSignatureInfo() == nullopt);
1039 BOOST_CHECK_EQUAL(i.getSignatureValue().isValid(), false);
1040 BOOST_CHECK_EQUAL(i.getName(), "/A/B/C");
1041
1042 i.setSignatureInfo(si); // auto-adds an empty ApplicationParameters element
1043 BOOST_CHECK_EQUAL(i.getName(),
1044 "/A/B/C/params-sha256=d2ac0eb1f60f60ab206fb80bf1d0f73cfef353bbec43ba6ea626117f671ca3bb");
1045 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
1046 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2400"_block);
1047 BOOST_CHECK(i.getSignatureInfo() == si);
1048
1049 Block sv("2E04 01020304"_block);
1050 i.setSignatureValue(make_shared<Buffer>(sv.value(), sv.value_size())); // updates ParametersDigestSha256Component
1051 BOOST_CHECK_EQUAL(i.getName(),
1052 "/A/B/C/params-sha256=f649845ef944638390d1c689e2f0618ea02e471eff236110cbeb822d5932d342");
1053 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
1054 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2400"_block);
1055 BOOST_CHECK(i.getSignatureInfo() == si);
1056 BOOST_CHECK_EQUAL(i.getSignatureValue(), sv);
1057
1058 i.setApplicationParameters("2404C0C1C2C3"_block); // updates ParametersSha256DigestComponent
1059 BOOST_CHECK_EQUAL(i.getName(),
1060 "/A/B/C/params-sha256=c5d7e567e6b251ddf36f7a6dbed95235b2d4a0b36215bb0f3cc403ac64ad0284");
1061 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
1062 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2404 C0C1C2C3"_block);
1063 BOOST_CHECK(i.getSignatureInfo() == si);
1064 BOOST_CHECK_EQUAL(i.getSignatureValue(), sv);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -04001065}
Junxiao Shi899277a2017-07-07 22:12:12 +00001066
Eric Newberryb74bbda2020-06-18 19:33:58 -07001067BOOST_AUTO_TEST_CASE(ExtractSignedRanges)
1068{
1069 Interest i1;
1070 i1.setCanBePrefix(false);
1071 BOOST_CHECK_EXCEPTION(i1.extractSignedRanges(), tlv::Error, [] (const auto& e) {
1072 BOOST_TEST_MESSAGE(e.what());
1073 return e.what() == "Name has zero name components"s;
1074 });
1075 i1.setName("/test/prefix");
1076 i1.setNonce(0x01020304);
1077 SignatureInfo sigInfo(tlv::DigestSha256);
1078 i1.setSignatureInfo(sigInfo);
1079
1080 // Test with previously unsigned Interest (no InterestSignatureValue)
1081 auto ranges1 = i1.extractSignedRanges();
1082 BOOST_REQUIRE_EQUAL(ranges1.size(), 2);
1083 const Block& wire1 = i1.wireEncode();
1084 // Ensure Name range captured properly
1085 Block nameWithoutDigest1 = i1.getName().getPrefix(-1).wireEncode();
1086 BOOST_CHECK_EQUAL_COLLECTIONS(ranges1.front().first, ranges1.front().first + ranges1.front().second,
1087 nameWithoutDigest1.value_begin(), nameWithoutDigest1.value_end());
1088 // Ensure parameters range captured properly
1089 const auto& appParamsWire1 = wire1.find(tlv::ApplicationParameters);
1090 BOOST_REQUIRE(appParamsWire1 != wire1.elements_end());
1091 BOOST_CHECK_EQUAL_COLLECTIONS(ranges1.back().first, ranges1.back().first + ranges1.back().second,
1092 appParamsWire1->begin(), wire1.end());
1093
1094 // Test with Interest with existing InterestSignatureValue
1095 auto sigValue = make_shared<Buffer>();
1096 i1.setSignatureValue(sigValue);
1097 auto ranges2 = i1.extractSignedRanges();
1098 BOOST_REQUIRE_EQUAL(ranges2.size(), 2);
1099 const auto& wire2 = i1.wireEncode();
1100 // Ensure Name range captured properly
1101 Block nameWithoutDigest2 = i1.getName().getPrefix(-1).wireEncode();
1102 BOOST_CHECK_EQUAL_COLLECTIONS(ranges2.front().first, ranges2.front().first + ranges2.front().second,
1103 nameWithoutDigest2.value_begin(), nameWithoutDigest2.value_end());
1104 // Ensure parameters range captured properly
1105 const auto& appParamsWire2 = wire2.find(tlv::ApplicationParameters);
1106 BOOST_REQUIRE(appParamsWire2 != wire2.elements_end());
1107 const auto& sigValueWire2 = wire2.find(tlv::InterestSignatureValue);
1108 BOOST_REQUIRE(sigValueWire2 != wire2.elements_end());
1109 BOOST_CHECK_EQUAL_COLLECTIONS(ranges2.back().first, ranges2.back().first + ranges2.back().second,
1110 appParamsWire2->begin(), sigValueWire2->begin());
1111
1112 // Test with decoded Interest
1113 const uint8_t WIRE[] = {
1114 0x05, 0x6f, // Interest
1115 0x07, 0x2e, // Name
1116 0x08, 0x04, // GenericNameComponent
1117 0x61, 0x62, 0x63, 0x64,
1118 0x08, 0x04, // GenericNameComponent
1119 0x65, 0x66, 0x67, 0x68,
1120 0x02, 0x20, // ParametersSha256DigestComponent
1121 0x6f, 0x29, 0x58, 0x60, 0x53, 0xee, 0x9f, 0xcc,
1122 0xd8, 0xa4, 0x22, 0x12, 0x29, 0x25, 0x28, 0x7c,
1123 0x0a, 0x18, 0x43, 0x5f, 0x40, 0x74, 0xc4, 0x0a,
1124 0xbb, 0x0d, 0x5b, 0x30, 0xe4, 0xaa, 0x62, 0x20,
1125 0x12, 0x00, // MustBeFresh
1126 0x0a, 0x04, // Nonce
1127 0x4c, 0x1e, 0xcb, 0x4a,
1128 0x24, 0x04, // ApplicationParameters
1129 0xc0, 0xc1, 0xc2, 0xc3,
1130 0x2c, 0x0d, // InterestSignatureInfo
1131 0x1b, 0x01, // SignatureType
1132 0x00,
1133 0x26, 0x08, // SignatureNonce
1134 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
1135 0x2e, 0x20, // InterestSignatureValue
1136 0x12, 0x47, 0x1a, 0xe0, 0xf8, 0x72, 0x3a, 0xc1,
1137 0x15, 0x6c, 0x37, 0x0a, 0x38, 0x71, 0x1e, 0xbe,
1138 0xbf, 0x28, 0x17, 0xde, 0x9b, 0x2d, 0xd9, 0x4e,
1139 0x9b, 0x7e, 0x62, 0xf1, 0x17, 0xb8, 0x76, 0xc1,
1140 };
1141 Block wire3(WIRE, sizeof(WIRE));
1142 Interest i2(wire3);
1143 auto ranges3 = i2.extractSignedRanges();
1144 BOOST_REQUIRE_EQUAL(ranges3.size(), 2);
1145 // Ensure Name range captured properly
1146 BOOST_CHECK_EQUAL_COLLECTIONS(ranges3.front().first, ranges3.front().first + ranges3.front().second,
1147 &WIRE[4], &WIRE[16]);
1148 // Ensure parameters range captured properly
1149 BOOST_CHECK_EQUAL_COLLECTIONS(ranges3.back().first, ranges3.back().first + ranges3.back().second,
1150 &WIRE[58], &WIRE[79]);
1151
1152 // Test failure with missing ParametersSha256DigestComponent
1153 Interest i3("/a");
1154 i3.setCanBePrefix(false);
1155 BOOST_CHECK_EXCEPTION(i3.extractSignedRanges(), tlv::Error, [] (const auto& e) {
1156 return e.what() == "Interest Name must end with a ParametersSha256DigestComponent"s;
1157 });
1158
1159 // Test failure with missing InterestSignatureInfo
Davide Pesavento81bd6962020-06-17 16:03:23 -04001160 i3.setApplicationParameters(nullptr, 0);
Eric Newberryb74bbda2020-06-18 19:33:58 -07001161 BOOST_CHECK_EXCEPTION(i3.extractSignedRanges(), tlv::Error, [] (const auto& e) {
1162 return e.what() == "Interest missing InterestSignatureInfo"s;
1163 });
1164}
1165
Davide Pesavento2fdb2742019-07-31 23:03:35 -04001166BOOST_AUTO_TEST_CASE(ToUri)
1167{
1168 Interest i;
1169 i.setCanBePrefix(false);
1170 BOOST_CHECK_EQUAL(i.toUri(), "/");
1171
1172 i.setName("/foo");
1173 BOOST_CHECK_EQUAL(i.toUri(), "/foo");
1174
1175 i.setCanBePrefix(true);
1176 BOOST_CHECK_EQUAL(i.toUri(), "/foo?CanBePrefix");
1177
1178 i.setMustBeFresh(true);
1179 BOOST_CHECK_EQUAL(i.toUri(), "/foo?CanBePrefix&MustBeFresh");
1180
Davide Pesavento53533942020-03-04 23:10:06 -05001181 i.setNonce(0xa1b2c3);
1182 BOOST_CHECK_EQUAL(i.toUri(), "/foo?CanBePrefix&MustBeFresh&Nonce=00a1b2c3");
Davide Pesavento2fdb2742019-07-31 23:03:35 -04001183
1184 i.setInterestLifetime(2_s);
Davide Pesavento53533942020-03-04 23:10:06 -05001185 BOOST_CHECK_EQUAL(i.toUri(), "/foo?CanBePrefix&MustBeFresh&Nonce=00a1b2c3&Lifetime=2000");
Davide Pesavento2fdb2742019-07-31 23:03:35 -04001186
1187 i.setHopLimit(18);
Davide Pesavento53533942020-03-04 23:10:06 -05001188 BOOST_CHECK_EQUAL(i.toUri(), "/foo?CanBePrefix&MustBeFresh&Nonce=00a1b2c3&Lifetime=2000&HopLimit=18");
Davide Pesavento2fdb2742019-07-31 23:03:35 -04001189
1190 i.setCanBePrefix(false);
1191 i.setMustBeFresh(false);
1192 i.setHopLimit(nullopt);
1193 i.setApplicationParameters("2402CAFE"_block);
1194 BOOST_CHECK_EQUAL(i.toUri(),
1195 "/foo/params-sha256=8621f5e8321f04104640c8d02877d7c5142cad6e203c5effda1783b1a0e476d6"
Davide Pesavento53533942020-03-04 23:10:06 -05001196 "?Nonce=00a1b2c3&Lifetime=2000");
Davide Pesavento2fdb2742019-07-31 23:03:35 -04001197}
1198
Davide Pesaventoeee3e822016-11-26 19:19:34 +01001199BOOST_AUTO_TEST_SUITE_END() // TestInterest
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -08001200
Alexander Afanasyev90164962014-03-06 08:29:59 +00001201} // namespace tests
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -08001202} // namespace ndn