blob: abb4280d21d736b8887dee8eb90cab6b56d9a44a [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 Pesavento7e780642018-11-24 15:51:34 -050025#include "tests/boost-test.hpp"
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +000026#include "tests/make-interest-data.hpp"
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080027
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080028namespace ndn {
Alexander Afanasyev90164962014-03-06 08:29:59 +000029namespace tests {
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080030
Junxiao Shi899277a2017-07-07 22:12:12 +000031BOOST_AUTO_TEST_SUITE(TestInterest)
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080032
Davide Pesaventoadc9aa22019-06-30 19:00:20 -040033class DisableAutoCheckParametersDigest
34{
35public:
36 DisableAutoCheckParametersDigest()
37 : m_saved(Interest::getAutoCheckParametersDigest())
38 {
39 Interest::setAutoCheckParametersDigest(false);
40 }
41
42 ~DisableAutoCheckParametersDigest()
43 {
44 Interest::setAutoCheckParametersDigest(m_saved);
45 }
46
47private:
48 bool m_saved;
49};
Junxiao Shi899277a2017-07-07 22:12:12 +000050
51BOOST_AUTO_TEST_CASE(DefaultConstructor)
52{
53 Interest i;
Davide Pesaventoadc9aa22019-06-30 19:00:20 -040054 BOOST_CHECK_EQUAL(i.hasWire(), false);
Junxiao Shi899277a2017-07-07 22:12:12 +000055 BOOST_CHECK_EQUAL(i.getName(), "/");
Junxiao Shi6efa3b72018-04-14 15:54:08 +000056 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
57 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -040058 BOOST_CHECK_EQUAL(i.getForwardingHint().empty(), true);
59 BOOST_CHECK_EQUAL(i.hasNonce(), false);
Junxiao Shi899277a2017-07-07 22:12:12 +000060 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -040061 BOOST_CHECK(i.getHopLimit() == nullopt);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -040062 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), false);
63 BOOST_CHECK_EQUAL(i.getApplicationParameters().isValid(), false);
64 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
Eric Newberry6e262f02020-05-29 23:11:25 -070065 BOOST_CHECK(i.getSignatureInfo() == nullopt);
66 BOOST_CHECK_EQUAL(i.getSignatureValue().isValid(), false);
67 BOOST_CHECK_EQUAL(i.isSigned(), false);
Davide Pesaventofccb2dc2019-02-09 01:02:35 -050068}
69
Davide Pesavento0e0b3892019-07-30 21:05:05 -040070BOOST_AUTO_TEST_SUITE(Encode)
Davide Pesaventoadc9aa22019-06-30 19:00:20 -040071
72BOOST_AUTO_TEST_CASE(Basic)
Junxiao Shi899277a2017-07-07 22:12:12 +000073{
74 const uint8_t WIRE[] = {
75 0x05, 0x1c, // Interest
76 0x07, 0x14, // Name
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +000077 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent
78 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent
79 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent
Junxiao Shi899277a2017-07-07 22:12:12 +000080 0x0a, 0x04, // Nonce
Davide Pesavento53533942020-03-04 23:10:06 -050081 0x01, 0x02, 0x03, 0x04,
Junxiao Shi899277a2017-07-07 22:12:12 +000082 };
83
Davide Pesavento0e0b3892019-07-30 21:05:05 -040084 Interest i1;
85 i1.setName("/local/ndn/prefix");
86 i1.setCanBePrefix(false);
Davide Pesavento53533942020-03-04 23:10:06 -050087 i1.setNonce(0x01020304);
Davide Pesavento0e0b3892019-07-30 21:05:05 -040088 BOOST_CHECK_EQUAL(i1.isParametersDigestValid(), true);
89
Junxiao Shi899277a2017-07-07 22:12:12 +000090 Block wire1 = i1.wireEncode();
91 BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
92
93 Interest i2(wire1);
94 BOOST_CHECK_EQUAL(i2.getName(), "/local/ndn/prefix");
Davide Pesavento0e0b3892019-07-30 21:05:05 -040095 BOOST_CHECK_EQUAL(i2.getCanBePrefix(), false);
96 BOOST_CHECK_EQUAL(i2.getMustBeFresh(), false);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -040097 BOOST_CHECK_EQUAL(i2.getForwardingHint().empty(), true);
Davide Pesavento835f0272019-09-21 13:18:24 -040098 BOOST_CHECK_EQUAL(i2.hasNonce(), true);
Davide Pesavento53533942020-03-04 23:10:06 -050099 BOOST_CHECK_EQUAL(i2.getNonce(), 0x01020304);
Junxiao Shi899277a2017-07-07 22:12:12 +0000100 BOOST_CHECK_EQUAL(i2.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400101 BOOST_CHECK(i2.getHopLimit() == nullopt);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400102 BOOST_CHECK_EQUAL(i2.hasApplicationParameters(), false);
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400103 BOOST_CHECK_EQUAL(i2.getApplicationParameters().isValid(), false);
Eric Newberry6e262f02020-05-29 23:11:25 -0700104 BOOST_CHECK(i2.getSignatureInfo() == nullopt);
105 BOOST_CHECK_EQUAL(i2.getSignatureValue().isValid(), false);
106 BOOST_CHECK_EQUAL(i2.isSigned(), false);
Junxiao Shi899277a2017-07-07 22:12:12 +0000107}
108
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400109BOOST_AUTO_TEST_CASE(WithParameters)
110{
111 const uint8_t WIRE[] = {
112 0x05, 0x44, // Interest
113 0x07, 0x36, // Name
114 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent
115 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent
116 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent
117 0x02, 0x20, // ParametersSha256DigestComponent
118 0xff, 0x91, 0x00, 0xe0, 0x4e, 0xaa, 0xdc, 0xf3, 0x06, 0x74, 0xd9, 0x80,
119 0x26, 0xa0, 0x51, 0xba, 0x25, 0xf5, 0x6b, 0x69, 0xbf, 0xa0, 0x26, 0xdc,
120 0xcc, 0xd7, 0x2c, 0x6e, 0xa0, 0xf7, 0x31, 0x5a,
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700121 0x0a, 0x04, // Nonce
Davide Pesavento53533942020-03-04 23:10:06 -0500122 0x00, 0x00, 0x00, 0x01,
Davide Pesavento9c19a392019-04-06 15:07:54 -0400123 0x24, 0x04, // ApplicationParameters
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400124 0xc0, 0xc1, 0xc2, 0xc3
125 };
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700126
127 Interest i1;
128 i1.setName("/local/ndn/prefix");
129 i1.setCanBePrefix(false);
Davide Pesavento53533942020-03-04 23:10:06 -0500130 i1.setNonce(0x1);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400131 i1.setApplicationParameters("2404C0C1C2C3"_block);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400132 BOOST_CHECK_EQUAL(i1.isParametersDigestValid(), true);
133
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700134 Block wire1 = i1.wireEncode();
135 BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
136
137 Interest i2(wire1);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400138 BOOST_CHECK_EQUAL(i2.getName(),
139 "/local/ndn/prefix/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a");
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700140 BOOST_CHECK_EQUAL(i2.getCanBePrefix(), false);
141 BOOST_CHECK_EQUAL(i2.getMustBeFresh(), false);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400142 BOOST_CHECK_EQUAL(i2.getForwardingHint().empty(), true);
Davide Pesavento835f0272019-09-21 13:18:24 -0400143 BOOST_CHECK_EQUAL(i2.hasNonce(), true);
Davide Pesavento53533942020-03-04 23:10:06 -0500144 BOOST_CHECK_EQUAL(i2.getNonce(), 0x1);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700145 BOOST_CHECK_EQUAL(i2.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400146 BOOST_CHECK(i2.getHopLimit() == nullopt);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400147 BOOST_CHECK_EQUAL(i2.hasApplicationParameters(), true);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400148 BOOST_CHECK_EQUAL(i2.getApplicationParameters(), "2404C0C1C2C3"_block);
Eric Newberry6e262f02020-05-29 23:11:25 -0700149 BOOST_CHECK(i2.getSignatureInfo() == nullopt);
150 BOOST_CHECK_EQUAL(i2.getSignatureValue().isValid(), false);
151 BOOST_CHECK_EQUAL(i2.isSigned(), false);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700152}
153
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400154BOOST_AUTO_TEST_CASE(Full)
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700155{
156 const uint8_t WIRE[] = {
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400157 0x05, 0x5c, // Interest
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400158 0x07, 0x36, // Name
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700159 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent
160 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent
161 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400162 0x02, 0x20, // ParametersSha256DigestComponent
163 0xff, 0x91, 0x00, 0xe0, 0x4e, 0xaa, 0xdc, 0xf3, 0x06, 0x74, 0xd9, 0x80,
164 0x26, 0xa0, 0x51, 0xba, 0x25, 0xf5, 0x6b, 0x69, 0xbf, 0xa0, 0x26, 0xdc,
165 0xcc, 0xd7, 0x2c, 0x6e, 0xa0, 0xf7, 0x31, 0x5a,
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700166 0x21, 0x00, // CanBePrefix
167 0x12, 0x00, // MustBeFresh
168 0x1e, 0x0b, // ForwardingHint
169 0x1f, 0x09, // Delegation List
170 0x1e, 0x02,
171 0x3e, 0x15,
172 0x07, 0x03,
173 0x08, 0x01, 0x48,
174 0x0a, 0x04, // Nonce
Davide Pesavento53533942020-03-04 23:10:06 -0500175 0x4c, 0x1e, 0xcb, 0x4a,
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400176 0x0c, 0x02, // InterestLifetime
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700177 0x76, 0xa1,
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400178 0x22, 0x01, // HopLimit
179 0xdc,
Davide Pesavento9c19a392019-04-06 15:07:54 -0400180 0x24, 0x04, // ApplicationParameters
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400181 0xc0, 0xc1, 0xc2, 0xc3
182 };
183
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700184 Interest i1;
185 i1.setName("/local/ndn/prefix");
186 i1.setMustBeFresh(true);
187 i1.setCanBePrefix(true);
188 i1.setForwardingHint(DelegationList({{15893, "/H"}}));
189 i1.setNonce(0x4c1ecb4a);
190 i1.setInterestLifetime(30369_ms);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400191 i1.setHopLimit(220);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400192 i1.setApplicationParameters("2404C0C1C2C3"_block);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400193 BOOST_CHECK_EQUAL(i1.isParametersDigestValid(), true);
194
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700195 Block wire1 = i1.wireEncode();
196 BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
197
198 Interest i2(wire1);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400199 BOOST_CHECK_EQUAL(i2.getName(),
200 "/local/ndn/prefix/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a");
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700201 BOOST_CHECK_EQUAL(i2.getCanBePrefix(), true);
202 BOOST_CHECK_EQUAL(i2.getMustBeFresh(), true);
203 BOOST_CHECK_EQUAL(i2.getForwardingHint(), DelegationList({{15893, "/H"}}));
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400204 BOOST_CHECK_EQUAL(i2.hasNonce(), true);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700205 BOOST_CHECK_EQUAL(i2.getNonce(), 0x4c1ecb4a);
206 BOOST_CHECK_EQUAL(i2.getInterestLifetime(), 30369_ms);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400207 BOOST_CHECK_EQUAL(*i2.getHopLimit(), 220);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400208 BOOST_CHECK_EQUAL(i2.getApplicationParameters(), "2404C0C1C2C3"_block);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700209}
210
Eric Newberry6e262f02020-05-29 23:11:25 -0700211BOOST_AUTO_TEST_CASE(Signed)
212{
213 const uint8_t WIRE[] = {
214 0x05, 0x77, // Interest
215 0x07, 0x36, // Name
216 0x08, 0x05, // GenericNameComponent
217 0x6c, 0x6f, 0x63, 0x61, 0x6c,
218 0x08, 0x03, // GenericNameComponent
219 0x6e, 0x64, 0x6e,
220 0x08, 0x06, // GenericNameComponent
221 0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
222 0x02, 0x20, // ParametersSha256DigestComponent
223 0x6f, 0x29, 0x58, 0x60, 0x53, 0xee, 0x9f, 0xcc,
224 0xd8, 0xa4, 0x22, 0x12, 0x29, 0x25, 0x28, 0x7c,
225 0x0a, 0x18, 0x43, 0x5f, 0x40, 0x74, 0xc4, 0x0a,
226 0xbb, 0x0d, 0x5b, 0x30, 0xe4, 0xaa, 0x62, 0x20,
227 0x12, 0x00, // MustBeFresh
228 0x0a, 0x04, // Nonce
229 0x4c, 0x1e, 0xcb, 0x4a,
230 0x24, 0x04, // ApplicationParameters
231 0xc0, 0xc1, 0xc2, 0xc3,
232 0x2c, 0x0d, // InterestSignatureInfo
233 0x1b, 0x01, // SignatureType
234 0x00,
235 0x26, 0x08, // SignatureNonce
236 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
237 0x2e, 0x20, // InterestSignatureValue
238 0x12, 0x47, 0x1a, 0xe0, 0xf8, 0x72, 0x3a, 0xc1,
239 0x15, 0x6c, 0x37, 0x0a, 0x38, 0x71, 0x1e, 0xbe,
240 0xbf, 0x28, 0x17, 0xde, 0x9b, 0x2d, 0xd9, 0x4e,
241 0x9b, 0x7e, 0x62, 0xf1, 0x17, 0xb8, 0x76, 0xc1,
242 };
243
244 SignatureInfo si(tlv::DigestSha256);
245 std::vector<uint8_t> nonce{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
246 si.setNonce(nonce);
247 Block sv("2E20 12471AE0F8723AC1156C370A38711EBEBF2817DE9B2DD94E9B7E62F117B876C1"_block);
248
249 Interest i1(Block(WIRE, sizeof(WIRE)));
250 BOOST_CHECK_EQUAL(i1.getName(),
251 "/local/ndn/prefix/params-sha256=6f29586053ee9fccd8a422122925287c0a18435f4074c40abb0d5b30e4aa6220");
252 BOOST_CHECK_EQUAL(i1.getCanBePrefix(), false);
253 BOOST_CHECK_EQUAL(i1.getMustBeFresh(), true);
254 BOOST_CHECK_EQUAL(i1.hasNonce(), true);
255 BOOST_CHECK_EQUAL(i1.getNonce(), 0x4c1ecb4a);
256 BOOST_CHECK_EQUAL(i1.getSignatureInfo()->getSignatureType(), tlv::DigestSha256);
257 BOOST_CHECK(i1.getSignatureInfo()->getNonce() == nonce);
258 BOOST_CHECK_EQUAL_COLLECTIONS(i1.getSignatureValue().begin(), i1.getSignatureValue().end(),
259 sv.begin(), sv.end());
260 BOOST_CHECK_EQUAL(i1.getApplicationParameters(), "2404C0C1C2C3"_block);
261 BOOST_CHECK_EQUAL(i1.isParametersDigestValid(), true);
262
263 // Reset wire
264 BOOST_CHECK_EQUAL(i1.hasWire(), true);
265 i1.setCanBePrefix(true);
266 i1.setCanBePrefix(false);
267 BOOST_CHECK_EQUAL(i1.hasWire(), false);
268
269 Block wire1 = i1.wireEncode();
270 BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
271
272 Interest i2("/local/ndn/prefix");
273 i2.setCanBePrefix(false);
274 i2.setMustBeFresh(true);
275 i2.setNonce(0x4c1ecb4a);
276 i2.setApplicationParameters("2404C0C1C2C3"_block);
277 i2.setSignatureInfo(si);
278 i2.setSignatureValue(make_shared<Buffer>(sv.value(), sv.value_size()));
279 BOOST_CHECK_EQUAL(i2.isParametersDigestValid(), true);
280
281 Block wire2 = i2.wireEncode();
282 BOOST_CHECK_EQUAL_COLLECTIONS(wire2.begin(), wire2.end(), WIRE, WIRE + sizeof(WIRE));
283}
284
285BOOST_AUTO_TEST_CASE(SignedApplicationElements)
286{
287 const uint8_t WIRE[] = {
288 0x05, 0x8f, // Interest
289 0x07, 0x36, // Name
290 0x08, 0x05, // GenericNameComponent
291 0x6c, 0x6f, 0x63, 0x61, 0x6c,
292 0x08, 0x03, // GenericNameComponent
293 0x6e, 0x64, 0x6e,
294 0x08, 0x06, // GenericNameComponent
295 0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
296 0x02, 0x20, // ParametersSha256DigestComponent
297 0xbc, 0x36, 0x30, 0xa4, 0xd6, 0x5e, 0x0d, 0xb5,
298 0x48, 0x3d, 0xfa, 0x0d, 0x28, 0xb3, 0x31, 0x2f,
299 0xca, 0xc1, 0xd4, 0x41, 0xec, 0x89, 0x61, 0xd4,
300 0x17, 0x5e, 0x61, 0x75, 0x17, 0x78, 0x10, 0x8e,
301 0x12, 0x00, // MustBeFresh
302 0x0a, 0x04, // Nonce
303 0x4c, 0x1e, 0xcb, 0x4a,
304 0x24, 0x04, // ApplicationParameters
305 0xc0, 0xc1, 0xc2, 0xc3,
306 0xfd, 0x01, 0xfe, 0x08, // Application-specific element (Type 510)
307 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80,
308 0x2c, 0x0d, // InterestSignatureInfo
309 0x1b, 0x01, // SignatureType
310 0x00,
311 0x26, 0x08, // SignatureNonce
312 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
313 0x2e, 0x20, // InterestSignatureValue
314 0x12, 0x47, 0x1a, 0xe0, 0xf8, 0x72, 0x3a, 0xc1,
315 0x15, 0x6c, 0x37, 0x0a, 0x38, 0x71, 0x1e, 0xbe,
316 0xbf, 0x28, 0x17, 0xde, 0x9b, 0x2d, 0xd9, 0x4e,
317 0x9b, 0x7e, 0x62, 0xf1, 0x17, 0xb8, 0x76, 0xc1,
318 0xfd, 0x02, 0x00, 0x08, // Application-specific element (Type 512)
319 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88
320 };
321
322 SignatureInfo si(tlv::DigestSha256);
323 std::vector<uint8_t> nonce{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
324 si.setNonce(nonce);
325 Block sv("2E20 12471AE0F8723AC1156C370A38711EBEBF2817DE9B2DD94E9B7E62F117B876C1"_block);
326
327 Interest i1(Block(WIRE, sizeof(WIRE)));
328 BOOST_CHECK_EQUAL(i1.getName(),
329 "/local/ndn/prefix/params-sha256=bc3630a4d65e0db5483dfa0d28b3312fcac1d441ec8961d4175e61751778108e");
330 BOOST_CHECK_EQUAL(i1.getCanBePrefix(), false);
331 BOOST_CHECK_EQUAL(i1.getMustBeFresh(), true);
332 BOOST_CHECK_EQUAL(i1.hasNonce(), true);
333 BOOST_CHECK_EQUAL(i1.getNonce(), 0x4c1ecb4a);
334 BOOST_CHECK_EQUAL(i1.getSignatureInfo()->getSignatureType(), tlv::DigestSha256);
335 BOOST_CHECK(i1.getSignatureInfo()->getNonce() == nonce);
336 BOOST_CHECK_EQUAL_COLLECTIONS(i1.getSignatureValue().begin(), i1.getSignatureValue().end(),
337 sv.begin(), sv.end());
338 BOOST_CHECK_EQUAL(i1.getApplicationParameters(), "2404C0C1C2C3"_block);
339 BOOST_CHECK_EQUAL(i1.isParametersDigestValid(), true);
340
341 // Reset wire
342 BOOST_CHECK_EQUAL(i1.hasWire(), true);
343 i1.setCanBePrefix(true);
344 i1.setCanBePrefix(false);
345 BOOST_CHECK_EQUAL(i1.hasWire(), false);
346
347 Block wire1 = i1.wireEncode();
348 BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
349}
350
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400351BOOST_AUTO_TEST_CASE(MissingApplicationParameters)
352{
353 Interest i;
354 i.setName(Name("/A").appendParametersSha256DigestPlaceholder());
355 i.setCanBePrefix(false);
356 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
357 BOOST_CHECK_THROW(i.wireEncode(), tlv::Error);
358}
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400359
360BOOST_AUTO_TEST_CASE(MissingParametersSha256DigestComponent)
361{
362 // there's no way to create an Interest that fails this check via programmatic construction,
363 // so we have to decode an invalid Interest and force reencoding
364
365 DisableAutoCheckParametersDigest disabler;
366 Interest i("050F 0703(080149) 0A04F000F000 2402CAFE"_block);
367 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
368 BOOST_CHECK_NO_THROW(i.wireEncode()); // this succeeds because it uses the cached wire encoding
369
370 i.setNonce(42); // trigger reencoding
371 BOOST_CHECK_THROW(i.wireEncode(), tlv::Error); // now the check fails while attempting to reencode
372}
373
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400374BOOST_AUTO_TEST_SUITE_END() // Encode
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400375
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400376class DecodeFixture
Junxiao Shi899277a2017-07-07 22:12:12 +0000377{
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000378protected:
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400379 DecodeFixture()
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000380 {
381 // initialize all elements to non-empty, to verify wireDecode clears them
382 i.setName("/A");
383 i.setForwardingHint({{10309, "/F"}});
384 i.setNonce(0x03d645a8);
385 i.setInterestLifetime(18554_ms);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400386 i.setHopLimit(64);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400387 i.setApplicationParameters("2404A0A1A2A3"_block);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000388 }
Junxiao Shi899277a2017-07-07 22:12:12 +0000389
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000390protected:
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000391 Interest i;
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000392};
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000393
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400394BOOST_FIXTURE_TEST_SUITE(Decode, DecodeFixture)
395
396BOOST_AUTO_TEST_CASE(NotAnInterest)
397{
398 BOOST_CHECK_THROW(i.wireDecode("4202CAFE"_block), tlv::Error);
399}
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 Pesavento2b0cc7b2019-07-14 16:50:04 -0400411 BOOST_CHECK(i.getHopLimit() == 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 Pesavento2b0cc7b2019-07-14 16:50:04 -0400431 BOOST_CHECK(i.getHopLimit() == 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);
444 BOOST_CHECK_EQUAL(i.getForwardingHint(), DelegationList({{15893, "/H"}}));
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
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400455 // modify then re-encode: unrecognized elements are discarded
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000456 i.setName("/J");
Junxiao Shi72c0c642018-04-20 15:41:09 +0000457 BOOST_CHECK_EQUAL(i.wireEncode(),
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400458 "0523 0703(08014A) "
459 "2100 1200 1E0B(1F09 1E023E15 0703080148) "
460 "0A044ACB1E4C 0C0276A1 2201D6"_block);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400461}
462
463BOOST_AUTO_TEST_CASE(FullWithParameters)
464{
465 i.wireDecode("055B 0725(080149 0220F16DB273F40436A852063F864D5072B01EAD53151F5A688EA1560492BEBEDD05) "
466 "FC00 2100 FC00 1200 FC00 1E0B(1F09 1E023E15 0703080148) "
467 "FC00 0A044ACB1E4C FC00 0C0276A1 FC00 2201D6 FC00 2404C0C1C2C3 FC00"_block);
468 BOOST_CHECK_EQUAL(i.getName(),
469 "/I/params-sha256=f16db273f40436a852063f864d5072b01ead53151f5a688ea1560492bebedd05");
470 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
471 BOOST_CHECK_EQUAL(i.getMustBeFresh(), true);
472 BOOST_CHECK_EQUAL(i.getForwardingHint(), DelegationList({{15893, "/H"}}));
473 BOOST_CHECK_EQUAL(i.hasNonce(), true);
Davide Pesavento53533942020-03-04 23:10:06 -0500474 BOOST_CHECK_EQUAL(i.getNonce(), 0x4acb1e4c);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400475 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 30369_ms);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400476 BOOST_CHECK_EQUAL(*i.getHopLimit(), 214);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400477 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true);
478 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2404C0C1C2C3"_block);
479
480 // encode without modification: retain original wire encoding
481 BOOST_CHECK_EQUAL(i.wireEncode().value_size(), 91);
482
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400483 // modify then re-encode: unrecognized elements after ApplicationParameters
484 // are preserved, the rest are discarded
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400485 i.setName("/J");
486 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
487 BOOST_CHECK_EQUAL(i.wireEncode(),
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400488 "054D 0725(08014A 0220F16DB273F40436A852063F864D5072B01EAD53151F5A688EA1560492BEBEDD05) "
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400489 "2100 1200 1E0B(1F09 1E023E15 0703080148) "
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400490 "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3 FC00"_block);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400491
492 // modify ApplicationParameters: unrecognized elements are preserved
493 i.setApplicationParameters("2402CAFE"_block);
494 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
495 BOOST_CHECK_EQUAL(i.wireEncode(),
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400496 "054B 0725(08014A 02205FDA67967EE302FC457E41B7D3D51BA6A9379574D193FD88F64954BF16C2927A) "
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400497 "2100 1200 1E0B(1F09 1E023E15 0703080148) "
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400498 "0A044ACB1E4C 0C0276A1 2201D6 2402CAFE FC00"_block);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000499}
500
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000501BOOST_AUTO_TEST_CASE(CriticalElementOutOfOrder)
502{
503 BOOST_CHECK_THROW(i.wireDecode(
504 "0529 2100 0703080149 1200 1E0B(1F09 1E023E15 0703080148) "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500505 "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3"_block),
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000506 tlv::Error);
507 BOOST_CHECK_THROW(i.wireDecode(
508 "0529 0703080149 1200 2100 1E0B(1F09 1E023E15 0703080148) "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500509 "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3"_block),
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000510 tlv::Error);
511 BOOST_CHECK_THROW(i.wireDecode(
512 "0529 0703080149 2100 1E0B(1F09 1E023E15 0703080148) 1200 "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500513 "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3"_block),
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000514 tlv::Error);
515 BOOST_CHECK_THROW(i.wireDecode(
516 "0529 0703080149 2100 1200 0A044ACB1E4C "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500517 "1E0B(1F09 1E023E15 0703080148) 0C0276A1 2201D6 2404C0C1C2C3"_block),
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000518 tlv::Error);
519 BOOST_CHECK_THROW(i.wireDecode(
520 "0529 0703080149 2100 1200 1E0B(1F09 1E023E15 0703080148) "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500521 "0C0276A1 0A044ACB1E4C 2201D6 2404C0C1C2C3"_block),
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000522 tlv::Error);
523 BOOST_CHECK_THROW(i.wireDecode(
524 "0529 0703080149 2100 1200 1E0B(1F09 1E023E15 0703080148) "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500525 "0A044ACB1E4C 2201D6 0C0276A1 2404C0C1C2C3"_block),
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000526 tlv::Error);
527}
528
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500529BOOST_AUTO_TEST_CASE(NonCriticalElementOutOfOrder)
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000530{
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400531 // duplicate HopLimit
532 i.wireDecode("0536 0725(080149 0220FF9100E04EAADCF30674D98026A051BA25F56B69BFA026DCCCD72C6EA0F7315A)"
533 "2201D6 2200 2404C0C1C2C3 22020101"_block);
534 BOOST_CHECK_EQUAL(i.getName(),
535 "/I/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a");
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400536 BOOST_CHECK_EQUAL(*i.getHopLimit(), 214);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400537 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400538 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2404C0C1C2C3"_block);
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500539
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400540 // duplicate ApplicationParameters
541 i.wireDecode("0541 0725(080149 0220FF9100E04EAADCF30674D98026A051BA25F56B69BFA026DCCCD72C6EA0F7315A)"
542 "2100 1200 0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3 2401EE"_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 Pesavento9c19a392019-04-06 15:07:54 -0400546 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true);
547 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2404C0C1C2C3"_block);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000548}
549
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400550BOOST_AUTO_TEST_CASE(MissingName)
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000551{
552 BOOST_CHECK_THROW(i.wireDecode("0500"_block), tlv::Error);
553 BOOST_CHECK_THROW(i.wireDecode("0502 1200"_block), tlv::Error);
554}
555
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400556BOOST_AUTO_TEST_CASE(BadName)
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000557{
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400558 // empty
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000559 BOOST_CHECK_THROW(i.wireDecode("0502 0700"_block), tlv::Error);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400560
561 // more than one ParametersSha256DigestComponent
562 BOOST_CHECK_THROW(i.wireDecode("054C 074A(080149"
563 "02200000000000000000000000000000000000000000000000000000000000000000"
564 "080132"
565 "02200000000000000000000000000000000000000000000000000000000000000000)"_block),
566 tlv::Error);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000567}
568
569BOOST_AUTO_TEST_CASE(BadCanBePrefix)
570{
571 BOOST_CHECK_THROW(i.wireDecode("0508 0703080149 210102"_block), tlv::Error);
572}
573
574BOOST_AUTO_TEST_CASE(BadMustBeFresh)
575{
576 BOOST_CHECK_THROW(i.wireDecode("0508 0703080149 120102"_block), tlv::Error);
577}
578
579BOOST_AUTO_TEST_CASE(BadNonce)
580{
581 BOOST_CHECK_THROW(i.wireDecode("0507 0703080149 0A00"_block), tlv::Error);
582 BOOST_CHECK_THROW(i.wireDecode("050A 0703080149 0A0304C263"_block), tlv::Error);
583 BOOST_CHECK_THROW(i.wireDecode("050C 0703080149 0A05EFA420B262"_block), tlv::Error);
584}
585
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500586BOOST_AUTO_TEST_CASE(BadHopLimit)
587{
588 BOOST_CHECK_THROW(i.wireDecode("0507 0703080149 2200"_block), tlv::Error);
589 BOOST_CHECK_THROW(i.wireDecode("0509 0703080149 22021356"_block), tlv::Error);
590}
591
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400592BOOST_AUTO_TEST_CASE(BadParametersDigest)
593{
594 // ApplicationParameters without ParametersSha256DigestComponent
595 Block b1("0509 0703(080149) 2402CAFE"_block);
596 // ParametersSha256DigestComponent without ApplicationParameters
597 Block b2("0527 0725(080149 0220E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855)"_block);
598 // digest mismatch
599 Block b3("052B 0725(080149 02200000000000000000000000000000000000000000000000000000000000000000) "
600 "2402CAFE"_block);
601
602 BOOST_CHECK_THROW(i.wireDecode(b1), tlv::Error);
603 BOOST_CHECK_THROW(i.wireDecode(b2), tlv::Error);
604 BOOST_CHECK_THROW(i.wireDecode(b3), tlv::Error);
605
606 DisableAutoCheckParametersDigest disabler;
607 BOOST_CHECK_NO_THROW(i.wireDecode(b1));
608 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
609 BOOST_CHECK_NO_THROW(i.wireDecode(b2));
610 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
611 BOOST_CHECK_NO_THROW(i.wireDecode(b3));
612 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
613}
614
Junxiao Shi8b753a22018-10-24 01:51:40 +0000615BOOST_AUTO_TEST_CASE(UnrecognizedNonCriticalElementBeforeName)
616{
617 BOOST_CHECK_THROW(i.wireDecode("0507 FC00 0703080149"_block), tlv::Error);
618}
619
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000620BOOST_AUTO_TEST_CASE(UnrecognizedCriticalElement)
621{
622 BOOST_CHECK_THROW(i.wireDecode("0507 0703080149 FB00"_block), tlv::Error);
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400623 // v0.2 packet with Selectors
624 BOOST_CHECK_THROW(i.wireDecode("0507 0703080149 09030D0101 0A0401000000"_block), tlv::Error);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000625}
626
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400627BOOST_AUTO_TEST_SUITE_END() // Decode
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000628
Junxiao Shi899277a2017-07-07 22:12:12 +0000629BOOST_AUTO_TEST_CASE(MatchesData)
630{
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000631 auto interest = makeInterest("/A");
Junxiao Shi899277a2017-07-07 22:12:12 +0000632
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000633 auto data = makeData("/A");
634 BOOST_CHECK_EQUAL(interest->matchesData(*data), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000635
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000636 data->setName("/A/D");
637 BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // violates CanBePrefix
Junxiao Shi899277a2017-07-07 22:12:12 +0000638
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000639 interest->setCanBePrefix(true);
640 BOOST_CHECK_EQUAL(interest->matchesData(*data), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000641
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000642 interest->setMustBeFresh(true);
643 BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // violates MustBeFresh
Junxiao Shi899277a2017-07-07 22:12:12 +0000644
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000645 data->setFreshnessPeriod(1_s);
646 BOOST_CHECK_EQUAL(interest->matchesData(*data), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000647
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000648 data->setName("/H/I");
649 BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // Name does not match
Junxiao Shi899277a2017-07-07 22:12:12 +0000650
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000651 data->wireEncode();
652 interest = makeInterest(data->getFullName());
653 BOOST_CHECK_EQUAL(interest->matchesData(*data), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000654
Davide Pesavento53533942020-03-04 23:10:06 -0500655 setNameComponent(*interest, -1,
656 name::Component::fromEscapedString("sha256digest=00000000000000000000000000"
657 "00000000000000000000000000000000000000"));
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000658 BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // violates implicit digest
Junxiao Shi899277a2017-07-07 22:12:12 +0000659}
660
661BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(MatchesInterest, 1)
662BOOST_AUTO_TEST_CASE(MatchesInterest)
663{
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400664 Interest interest;
665 interest.setName("/A")
666 .setCanBePrefix(true)
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000667 .setMustBeFresh(true)
668 .setForwardingHint({{1, "/H"}})
669 .setNonce(2228)
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400670 .setInterestLifetime(5_s)
671 .setHopLimit(90);
Junxiao Shi899277a2017-07-07 22:12:12 +0000672
673 Interest other;
674 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false);
675
676 other.setName(interest.getName());
677 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false);
678
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000679 other.setCanBePrefix(interest.getCanBePrefix());
680 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false);
681
682 other.setMustBeFresh(interest.getMustBeFresh());
Junxiao Shi899277a2017-07-07 22:12:12 +0000683 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false); // will match until #3162 implemented
684
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000685 other.setForwardingHint(interest.getForwardingHint());
Junxiao Shi899277a2017-07-07 22:12:12 +0000686 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
687
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000688 other.setNonce(9336);
Junxiao Shi899277a2017-07-07 22:12:12 +0000689 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
690
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000691 other.setInterestLifetime(3_s);
Junxiao Shi899277a2017-07-07 22:12:12 +0000692 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400693
694 other.setHopLimit(31);
695 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000696}
697
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400698BOOST_AUTO_TEST_CASE(SetName)
699{
700 Interest i;
701 BOOST_CHECK_EQUAL(i.getName(), "/");
702 i.setName("/A/B");
703 BOOST_CHECK_EQUAL(i.getName(), "/A/B");
704 i.setName("/I/params-sha256=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
705 BOOST_CHECK_EQUAL(i.getName(),
706 "/I/params-sha256=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
707 BOOST_CHECK_THROW(i.setName("/I"
708 "/params-sha256=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
709 "/params-sha256=0000000000000000000000000000000000000000000000000000000000000000"),
710 std::invalid_argument);
711}
Junxiao Shi899277a2017-07-07 22:12:12 +0000712
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400713BOOST_AUTO_TEST_CASE(SetCanBePrefix)
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000714{
715 Interest i;
716 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
717 i.setCanBePrefix(false);
718 BOOST_CHECK_EQUAL(i.getCanBePrefix(), false);
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000719 i.setCanBePrefix(true);
720 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000721}
722
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400723BOOST_AUTO_TEST_CASE(SetMustBeFresh)
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000724{
725 Interest i;
726 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
727 i.setMustBeFresh(true);
728 BOOST_CHECK_EQUAL(i.getMustBeFresh(), true);
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000729 i.setMustBeFresh(false);
730 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000731}
732
733BOOST_AUTO_TEST_CASE(ModifyForwardingHint)
734{
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400735 Interest i("/I");
Junxiao Shib55e5d32018-07-18 13:32:00 -0600736 i.setCanBePrefix(false);
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000737 i.setForwardingHint({{1, "/A"}});
738 i.wireEncode();
739 BOOST_CHECK(i.hasWire());
740
741 i.modifyForwardingHint([] (DelegationList& fh) { fh.insert(2, "/B"); });
742 BOOST_CHECK(!i.hasWire());
743 BOOST_CHECK_EQUAL(i.getForwardingHint(), DelegationList({{1, "/A"}, {2, "/B"}}));
744}
745
Junxiao Shi899277a2017-07-07 22:12:12 +0000746BOOST_AUTO_TEST_CASE(GetNonce)
747{
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000748 unique_ptr<Interest> i1, i2;
Davide Pesavento53533942020-03-04 23:10:06 -0500749 Interest::Nonce nonce1(0), nonce2(0);
Junxiao Shi899277a2017-07-07 22:12:12 +0000750
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000751 // getNonce automatically assigns a random Nonce.
752 // 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 -0500753 // identical Nonces in a row.
Junxiao Shi899277a2017-07-07 22:12:12 +0000754 int nIterations = 0;
755 do {
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000756 i1 = make_unique<Interest>();
757 nonce1 = i1->getNonce();
758 i2 = make_unique<Interest>();
759 nonce2 = i2->getNonce();
Junxiao Shi899277a2017-07-07 22:12:12 +0000760 }
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000761 while (nonce1 == nonce2 && ++nIterations < 100);
Davide Pesavento53533942020-03-04 23:10:06 -0500762
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000763 BOOST_CHECK_NE(nonce1, nonce2);
764 BOOST_CHECK(i1->hasNonce());
765 BOOST_CHECK(i2->hasNonce());
Junxiao Shi899277a2017-07-07 22:12:12 +0000766
767 // Once a Nonce is assigned, it should not change.
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000768 BOOST_CHECK_EQUAL(i1->getNonce(), nonce1);
Davide Pesavento53533942020-03-04 23:10:06 -0500769 BOOST_CHECK_EQUAL(i2->getNonce(), nonce2);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000770}
771
772BOOST_AUTO_TEST_CASE(SetNonce)
773{
774 Interest i1("/A");
Junxiao Shib55e5d32018-07-18 13:32:00 -0600775 i1.setCanBePrefix(false);
Davide Pesavento53533942020-03-04 23:10:06 -0500776 BOOST_CHECK(!i1.hasNonce());
777
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000778 i1.setNonce(1);
779 i1.wireEncode();
Davide Pesavento53533942020-03-04 23:10:06 -0500780 BOOST_CHECK(i1.hasNonce());
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000781 BOOST_CHECK_EQUAL(i1.getNonce(), 1);
782
783 Interest i2(i1);
Davide Pesavento53533942020-03-04 23:10:06 -0500784 BOOST_CHECK(i2.hasNonce());
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000785 BOOST_CHECK_EQUAL(i2.getNonce(), 1);
786
787 i2.setNonce(2);
788 BOOST_CHECK_EQUAL(i2.getNonce(), 2);
Davide Pesavento53533942020-03-04 23:10:06 -0500789 BOOST_CHECK_EQUAL(i1.getNonce(), 1); // should not affect i1's Nonce (Bug #4168)
790
791 i2.setNonce(nullopt);
792 BOOST_CHECK(!i2.hasNonce());
Junxiao Shi899277a2017-07-07 22:12:12 +0000793}
794
795BOOST_AUTO_TEST_CASE(RefreshNonce)
796{
797 Interest i;
798 BOOST_CHECK(!i.hasNonce());
799 i.refreshNonce();
800 BOOST_CHECK(!i.hasNonce());
801
802 i.setNonce(1);
803 BOOST_CHECK(i.hasNonce());
804 i.refreshNonce();
805 BOOST_CHECK(i.hasNonce());
806 BOOST_CHECK_NE(i.getNonce(), 1);
807}
808
Davide Pesavento53533942020-03-04 23:10:06 -0500809BOOST_AUTO_TEST_CASE(NonceConversions)
810{
811 Interest i;
812 i.setCanBePrefix(false);
813
814 // 4-arg constructor
815 Interest::Nonce n1(1, 2, 3, 4);
816 i.setNonce(n1);
817 BOOST_CHECK_EQUAL(i.getNonce(), 0x01020304);
818
819 // 4-arg constructor + assignment
820 n1 = {0xf, 0xe, 0xd, 0xc};
821 i.setNonce(n1);
822 BOOST_CHECK_EQUAL(i.getNonce(), 0x0f0e0d0c);
823
824 // 1-arg constructor + assignment (implicit conversion)
825 Interest::Nonce n2;
826 n2 = 42;
827 BOOST_CHECK_NE(n1, n2);
828 i.setNonce(n2);
829 n2 = 21; // should not affect i's Nonce
830 BOOST_CHECK_EQUAL(i.getNonce(), 42);
831 BOOST_CHECK_EQUAL(i.toUri(), "/?Nonce=0000002a"); // stored in big-endian
832}
833
Junxiao Shi899277a2017-07-07 22:12:12 +0000834BOOST_AUTO_TEST_CASE(SetInterestLifetime)
835{
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500836 BOOST_CHECK_THROW(Interest("/A", -1_ms), std::invalid_argument);
Davide Pesavento0f830802018-01-16 23:58:58 -0500837 BOOST_CHECK_NO_THROW(Interest("/A", 0_ms));
Junxiao Shi899277a2017-07-07 22:12:12 +0000838
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400839 Interest i;
Junxiao Shi899277a2017-07-07 22:12:12 +0000840 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500841 BOOST_CHECK_THROW(i.setInterestLifetime(-1_ms), std::invalid_argument);
Junxiao Shi899277a2017-07-07 22:12:12 +0000842 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesavento0f830802018-01-16 23:58:58 -0500843 i.setInterestLifetime(0_ms);
844 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 0_ms);
845 i.setInterestLifetime(1_ms);
846 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 1_ms);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400847
848 i = Interest("/B", 15_s);
849 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 15_s);
850}
851
852BOOST_AUTO_TEST_CASE(SetHopLimit)
853{
854 Interest i;
855 BOOST_CHECK(i.getHopLimit() == nullopt);
856 i.setHopLimit(42);
857 BOOST_CHECK(i.getHopLimit() == 42);
858 i.setHopLimit(nullopt);
859 BOOST_CHECK(i.getHopLimit() == nullopt);
Junxiao Shi899277a2017-07-07 22:12:12 +0000860}
861
Davide Pesavento9c19a392019-04-06 15:07:54 -0400862BOOST_AUTO_TEST_CASE(SetApplicationParameters)
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700863{
864 const uint8_t PARAMETERS1[] = {0xc1};
865 const uint8_t PARAMETERS2[] = {0xc2};
866
867 Interest i;
Davide Pesavento9c19a392019-04-06 15:07:54 -0400868 BOOST_CHECK(!i.hasApplicationParameters());
869 i.setApplicationParameters("2400"_block);
870 BOOST_CHECK(i.hasApplicationParameters());
871 i.unsetApplicationParameters();
872 BOOST_CHECK(!i.hasApplicationParameters());
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700873
Davide Pesavento38912442019-04-06 22:03:39 -0400874 // Block overload
875 i.setApplicationParameters(Block{});
876 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2400"_block);
877 i.setApplicationParameters("2401C0"_block);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400878 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2401C0"_block);
Davide Pesavento38912442019-04-06 22:03:39 -0400879 i.setApplicationParameters("8001C1"_block);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400880 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "24038001C1"_block);
Davide Pesavento38912442019-04-06 22:03:39 -0400881
882 // raw buffer+size overload
883 i.setApplicationParameters(PARAMETERS1, sizeof(PARAMETERS1));
884 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2401C1"_block);
885 i.setApplicationParameters(nullptr, 0);
886 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2400"_block);
887 BOOST_CHECK_THROW(i.setApplicationParameters(nullptr, 42), std::invalid_argument);
888
889 // ConstBufferPtr overload
890 i.setApplicationParameters(make_shared<Buffer>(PARAMETERS2, sizeof(PARAMETERS2)));
891 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2401C2"_block);
892 i.setApplicationParameters(make_shared<Buffer>());
893 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2400"_block);
894 BOOST_CHECK_THROW(i.setApplicationParameters(nullptr), std::invalid_argument);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700895}
896
Eric Newberry6e262f02020-05-29 23:11:25 -0700897BOOST_AUTO_TEST_CASE(SetSignature)
898{
899 Interest i;
900 i.setCanBePrefix(false);
901 BOOST_CHECK(i.getSignatureInfo() == nullopt);
902 BOOST_CHECK_EQUAL(i.isSigned(), false);
903
904 // Throws because attempting to set InterestSignatureValue without set InterestSignatureInfo
905 Block sv1("2E04 01020304"_block);
906 auto svBuffer1 = make_shared<Buffer>(sv1.value(), sv1.value_size());
907 BOOST_CHECK_THROW(i.setSignatureValue(svBuffer1), tlv::Error);
908
909 // Simple set/get case for InterestSignatureInfo (no prior set)
910 SignatureInfo si1(tlv::SignatureSha256WithEcdsa);
911 i.setSignatureInfo(si1);
912 BOOST_CHECK(i.getSignatureInfo() == si1);
913 BOOST_CHECK_EQUAL(i.isSigned(), false);
914
915 // Simple set/get case for InterestSignatureValue (no prior set)
916 BOOST_CHECK_EQUAL(i.getSignatureValue().isValid(), false);
917 i.setSignatureValue(svBuffer1);
918 BOOST_CHECK_EQUAL(i.getSignatureValue(), sv1);
919 BOOST_CHECK_EQUAL(i.isSigned(), true);
920
921 // Throws because attempting to set InterestSignatureValue to nullptr
922 BOOST_CHECK_THROW(i.setSignatureValue(nullptr), std::invalid_argument);
923 BOOST_CHECK_EQUAL(i.getSignatureValue(), sv1);
924 BOOST_CHECK_EQUAL(i.isSigned(), true);
925
926 // Ensure that wire is not reset if specified InterestSignatureInfo is same
927 i.wireEncode();
928 BOOST_CHECK_EQUAL(i.hasWire(), true);
929 i.setSignatureInfo(si1);
930 BOOST_CHECK_EQUAL(i.hasWire(), true);
931 BOOST_CHECK(i.getSignatureInfo() == si1);
932 BOOST_CHECK_EQUAL(i.getSignatureValue(), sv1);
933 BOOST_CHECK_EQUAL(i.isSigned(), true);
934
935 // Ensure that wire is reset if specified InterestSignatureInfo is different
936 i.wireEncode();
937 BOOST_CHECK_EQUAL(i.hasWire(), true);
938 SignatureInfo si2(tlv::SignatureSha256WithRsa);
939 i.setSignatureInfo(si2);
940 BOOST_CHECK_EQUAL(i.hasWire(), false);
941 BOOST_CHECK(i.getSignatureInfo() == si2);
942 BOOST_CHECK_EQUAL(i.getSignatureValue(), sv1);
943 BOOST_CHECK_EQUAL(i.isSigned(), true);
944
945 // Ensure that wire is not reset if specified InterestSignatureValue is same
946 i.wireEncode();
947 BOOST_CHECK_EQUAL(i.hasWire(), true);
948 i.setSignatureValue(svBuffer1);
949 BOOST_CHECK_EQUAL(i.hasWire(), true);
950 BOOST_CHECK(i.getSignatureInfo() == si2);
951 BOOST_CHECK_EQUAL(i.getSignatureValue(), sv1);
952 BOOST_CHECK_EQUAL(i.isSigned(), true);
953
954 // Ensure that wire is reset if specified InterestSignatureValue is different
955 i.wireEncode();
956 BOOST_CHECK_EQUAL(i.hasWire(), true);
957 Block sv2("2E04 99887766"_block);
958 auto svBuffer2 = make_shared<Buffer>(sv2.value(), sv2.value_size());
959 i.setSignatureValue(svBuffer2);
960 BOOST_CHECK_EQUAL(i.hasWire(), false);
961 BOOST_CHECK(i.getSignatureInfo() == si2);
962 BOOST_CHECK_EQUAL(i.getSignatureValue(), sv2);
963 BOOST_CHECK_EQUAL(i.isSigned(), true);
964}
965
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400966BOOST_AUTO_TEST_CASE(ParametersSha256DigestComponent)
967{
968 Interest i("/I");
969 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
970
971 i.setApplicationParameters("2404C0C1C2C3"_block); // auto-appends ParametersSha256DigestComponent
972 BOOST_CHECK_EQUAL(i.getName(),
973 "/I/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a");
974 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
975
976 i.setApplicationParameters(nullptr, 0); // updates ParametersSha256DigestComponent
977 BOOST_CHECK_EQUAL(i.getName(),
978 "/I/params-sha256=33b67cb5385ceddad93d0ee960679041613bed34b8b4a5e6362fe7539ba2d3ce");
979 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true);
980 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
981
982 i.unsetApplicationParameters(); // removes ParametersSha256DigestComponent
983 BOOST_CHECK_EQUAL(i.getName(), "/I");
984 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
985
986 i.setName(Name("/P").appendParametersSha256DigestPlaceholder().append("Q"));
987 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), false);
988 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
989
990 i.unsetApplicationParameters(); // removes ParametersSha256DigestComponent
991 BOOST_CHECK_EQUAL(i.getName(), "/P/Q");
992 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
993
994 i.setName(Name("/P").appendParametersSha256DigestPlaceholder().append("Q"));
995 i.setApplicationParameters("2404C0C1C2C3"_block); // updates ParametersSha256DigestComponent
996 BOOST_CHECK_EQUAL(i.getName(),
997 "/P/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a/Q");
998 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
999
1000 i.setName("/A/B/C"); // auto-appends ParametersSha256DigestComponent
1001 BOOST_CHECK_EQUAL(i.getName(),
1002 "/A/B/C/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a");
1003 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true);
1004 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
Eric Newberry6e262f02020-05-29 23:11:25 -07001005
1006 SignatureInfo si(tlv::SignatureSha256WithEcdsa);
1007 i.setSignatureInfo(si); // updates ParametersSha256DigestComponent
1008 BOOST_CHECK_EQUAL(i.getName(),
1009 "/A/B/C/params-sha256=6400cae1730c15fd7854b26be05794d53685423c94bc61e59c49bd640d646ae8");
1010 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
1011 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2404 C0C1C2C3"_block);
1012 BOOST_CHECK(i.getSignatureInfo() == si);
1013
1014 i.unsetApplicationParameters(); // removes ParametersSha256DigestComponent and InterestSignatureInfo
1015 BOOST_CHECK(i.getSignatureInfo() == nullopt);
1016 BOOST_CHECK_EQUAL(i.getSignatureValue().isValid(), false);
1017 BOOST_CHECK_EQUAL(i.getName(), "/A/B/C");
1018
1019 i.setSignatureInfo(si); // auto-adds an empty ApplicationParameters element
1020 BOOST_CHECK_EQUAL(i.getName(),
1021 "/A/B/C/params-sha256=d2ac0eb1f60f60ab206fb80bf1d0f73cfef353bbec43ba6ea626117f671ca3bb");
1022 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
1023 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2400"_block);
1024 BOOST_CHECK(i.getSignatureInfo() == si);
1025
1026 Block sv("2E04 01020304"_block);
1027 i.setSignatureValue(make_shared<Buffer>(sv.value(), sv.value_size())); // updates ParametersDigestSha256Component
1028 BOOST_CHECK_EQUAL(i.getName(),
1029 "/A/B/C/params-sha256=f649845ef944638390d1c689e2f0618ea02e471eff236110cbeb822d5932d342");
1030 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
1031 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2400"_block);
1032 BOOST_CHECK(i.getSignatureInfo() == si);
1033 BOOST_CHECK_EQUAL(i.getSignatureValue(), sv);
1034
1035 i.setApplicationParameters("2404C0C1C2C3"_block); // updates ParametersSha256DigestComponent
1036 BOOST_CHECK_EQUAL(i.getName(),
1037 "/A/B/C/params-sha256=c5d7e567e6b251ddf36f7a6dbed95235b2d4a0b36215bb0f3cc403ac64ad0284");
1038 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
1039 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2404 C0C1C2C3"_block);
1040 BOOST_CHECK(i.getSignatureInfo() == si);
1041 BOOST_CHECK_EQUAL(i.getSignatureValue(), sv);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -04001042}
Junxiao Shi899277a2017-07-07 22:12:12 +00001043
Davide Pesavento2fdb2742019-07-31 23:03:35 -04001044BOOST_AUTO_TEST_CASE(ToUri)
1045{
1046 Interest i;
1047 i.setCanBePrefix(false);
1048 BOOST_CHECK_EQUAL(i.toUri(), "/");
1049
1050 i.setName("/foo");
1051 BOOST_CHECK_EQUAL(i.toUri(), "/foo");
1052
1053 i.setCanBePrefix(true);
1054 BOOST_CHECK_EQUAL(i.toUri(), "/foo?CanBePrefix");
1055
1056 i.setMustBeFresh(true);
1057 BOOST_CHECK_EQUAL(i.toUri(), "/foo?CanBePrefix&MustBeFresh");
1058
Davide Pesavento53533942020-03-04 23:10:06 -05001059 i.setNonce(0xa1b2c3);
1060 BOOST_CHECK_EQUAL(i.toUri(), "/foo?CanBePrefix&MustBeFresh&Nonce=00a1b2c3");
Davide Pesavento2fdb2742019-07-31 23:03:35 -04001061
1062 i.setInterestLifetime(2_s);
Davide Pesavento53533942020-03-04 23:10:06 -05001063 BOOST_CHECK_EQUAL(i.toUri(), "/foo?CanBePrefix&MustBeFresh&Nonce=00a1b2c3&Lifetime=2000");
Davide Pesavento2fdb2742019-07-31 23:03:35 -04001064
1065 i.setHopLimit(18);
Davide Pesavento53533942020-03-04 23:10:06 -05001066 BOOST_CHECK_EQUAL(i.toUri(), "/foo?CanBePrefix&MustBeFresh&Nonce=00a1b2c3&Lifetime=2000&HopLimit=18");
Davide Pesavento2fdb2742019-07-31 23:03:35 -04001067
1068 i.setCanBePrefix(false);
1069 i.setMustBeFresh(false);
1070 i.setHopLimit(nullopt);
1071 i.setApplicationParameters("2402CAFE"_block);
1072 BOOST_CHECK_EQUAL(i.toUri(),
1073 "/foo/params-sha256=8621f5e8321f04104640c8d02877d7c5142cad6e203c5effda1783b1a0e476d6"
Davide Pesavento53533942020-03-04 23:10:06 -05001074 "?Nonce=00a1b2c3&Lifetime=2000");
Davide Pesavento2fdb2742019-07-31 23:03:35 -04001075}
1076
Davide Pesaventoeee3e822016-11-26 19:19:34 +01001077BOOST_AUTO_TEST_SUITE_END() // TestInterest
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -08001078
Alexander Afanasyev90164962014-03-06 08:29:59 +00001079} // namespace tests
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -08001080} // namespace ndn