blob: 7b8f8438d2db2eaf4e8e0c0796d4d0340b3bcf68 [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 Pesaventofccb2dc2019-02-09 01:02:35 -05003 * Copyright (c) 2013-2019 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);
Davide Pesaventofccb2dc2019-02-09 01:02:35 -050065}
66
Davide Pesavento0e0b3892019-07-30 21:05:05 -040067BOOST_AUTO_TEST_SUITE(Encode)
Davide Pesaventoadc9aa22019-06-30 19:00:20 -040068
69BOOST_AUTO_TEST_CASE(Basic)
Junxiao Shi899277a2017-07-07 22:12:12 +000070{
71 const uint8_t WIRE[] = {
72 0x05, 0x1c, // Interest
73 0x07, 0x14, // Name
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +000074 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent
75 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent
76 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent
Junxiao Shi899277a2017-07-07 22:12:12 +000077 0x0a, 0x04, // Nonce
Davide Pesavento0e0b3892019-07-30 21:05:05 -040078 0x01, 0x00, 0x00, 0x00,
Junxiao Shi899277a2017-07-07 22:12:12 +000079 };
80
Davide Pesavento0e0b3892019-07-30 21:05:05 -040081 Interest i1;
82 i1.setName("/local/ndn/prefix");
83 i1.setCanBePrefix(false);
Junxiao Shi899277a2017-07-07 22:12:12 +000084 i1.setNonce(1);
Davide Pesavento0e0b3892019-07-30 21:05:05 -040085 BOOST_CHECK_EQUAL(i1.isParametersDigestValid(), true);
86
Junxiao Shi899277a2017-07-07 22:12:12 +000087 Block wire1 = i1.wireEncode();
88 BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
89
90 Interest i2(wire1);
91 BOOST_CHECK_EQUAL(i2.getName(), "/local/ndn/prefix");
Davide Pesavento0e0b3892019-07-30 21:05:05 -040092 BOOST_CHECK_EQUAL(i2.getCanBePrefix(), false);
93 BOOST_CHECK_EQUAL(i2.getMustBeFresh(), false);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -040094 BOOST_CHECK_EQUAL(i2.getForwardingHint().empty(), true);
Davide Pesavento835f0272019-09-21 13:18:24 -040095 BOOST_CHECK_EQUAL(i2.hasNonce(), true);
Junxiao Shi899277a2017-07-07 22:12:12 +000096 BOOST_CHECK_EQUAL(i2.getNonce(), 1);
97 BOOST_CHECK_EQUAL(i2.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -040098 BOOST_CHECK(i2.getHopLimit() == nullopt);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -040099 BOOST_CHECK_EQUAL(i2.hasApplicationParameters(), false);
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400100 BOOST_CHECK_EQUAL(i2.getApplicationParameters().isValid(), false);
Junxiao Shi899277a2017-07-07 22:12:12 +0000101}
102
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400103BOOST_AUTO_TEST_CASE(WithParameters)
104{
105 const uint8_t WIRE[] = {
106 0x05, 0x44, // Interest
107 0x07, 0x36, // Name
108 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent
109 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent
110 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent
111 0x02, 0x20, // ParametersSha256DigestComponent
112 0xff, 0x91, 0x00, 0xe0, 0x4e, 0xaa, 0xdc, 0xf3, 0x06, 0x74, 0xd9, 0x80,
113 0x26, 0xa0, 0x51, 0xba, 0x25, 0xf5, 0x6b, 0x69, 0xbf, 0xa0, 0x26, 0xdc,
114 0xcc, 0xd7, 0x2c, 0x6e, 0xa0, 0xf7, 0x31, 0x5a,
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700115 0x0a, 0x04, // Nonce
116 0x01, 0x00, 0x00, 0x00,
Davide Pesavento9c19a392019-04-06 15:07:54 -0400117 0x24, 0x04, // ApplicationParameters
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400118 0xc0, 0xc1, 0xc2, 0xc3
119 };
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700120
121 Interest i1;
122 i1.setName("/local/ndn/prefix");
123 i1.setCanBePrefix(false);
124 i1.setNonce(1);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400125 i1.setApplicationParameters("2404C0C1C2C3"_block);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400126 BOOST_CHECK_EQUAL(i1.isParametersDigestValid(), true);
127
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700128 Block wire1 = i1.wireEncode();
129 BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
130
131 Interest i2(wire1);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400132 BOOST_CHECK_EQUAL(i2.getName(),
133 "/local/ndn/prefix/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a");
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700134 BOOST_CHECK_EQUAL(i2.getCanBePrefix(), false);
135 BOOST_CHECK_EQUAL(i2.getMustBeFresh(), false);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400136 BOOST_CHECK_EQUAL(i2.getForwardingHint().empty(), true);
Davide Pesavento835f0272019-09-21 13:18:24 -0400137 BOOST_CHECK_EQUAL(i2.hasNonce(), true);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700138 BOOST_CHECK_EQUAL(i2.getNonce(), 1);
139 BOOST_CHECK_EQUAL(i2.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400140 BOOST_CHECK(i2.getHopLimit() == nullopt);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400141 BOOST_CHECK_EQUAL(i2.hasApplicationParameters(), true);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400142 BOOST_CHECK_EQUAL(i2.getApplicationParameters(), "2404C0C1C2C3"_block);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700143}
144
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400145BOOST_AUTO_TEST_CASE(Full)
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700146{
147 const uint8_t WIRE[] = {
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400148 0x05, 0x5c, // Interest
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400149 0x07, 0x36, // Name
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700150 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent
151 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent
152 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400153 0x02, 0x20, // ParametersSha256DigestComponent
154 0xff, 0x91, 0x00, 0xe0, 0x4e, 0xaa, 0xdc, 0xf3, 0x06, 0x74, 0xd9, 0x80,
155 0x26, 0xa0, 0x51, 0xba, 0x25, 0xf5, 0x6b, 0x69, 0xbf, 0xa0, 0x26, 0xdc,
156 0xcc, 0xd7, 0x2c, 0x6e, 0xa0, 0xf7, 0x31, 0x5a,
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700157 0x21, 0x00, // CanBePrefix
158 0x12, 0x00, // MustBeFresh
159 0x1e, 0x0b, // ForwardingHint
160 0x1f, 0x09, // Delegation List
161 0x1e, 0x02,
162 0x3e, 0x15,
163 0x07, 0x03,
164 0x08, 0x01, 0x48,
165 0x0a, 0x04, // Nonce
166 0x4a, 0xcb, 0x1e, 0x4c,
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400167 0x0c, 0x02, // InterestLifetime
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700168 0x76, 0xa1,
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400169 0x22, 0x01, // HopLimit
170 0xdc,
Davide Pesavento9c19a392019-04-06 15:07:54 -0400171 0x24, 0x04, // ApplicationParameters
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400172 0xc0, 0xc1, 0xc2, 0xc3
173 };
174
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700175 Interest i1;
176 i1.setName("/local/ndn/prefix");
177 i1.setMustBeFresh(true);
178 i1.setCanBePrefix(true);
179 i1.setForwardingHint(DelegationList({{15893, "/H"}}));
180 i1.setNonce(0x4c1ecb4a);
181 i1.setInterestLifetime(30369_ms);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400182 i1.setHopLimit(220);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400183 i1.setApplicationParameters("2404C0C1C2C3"_block);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400184 BOOST_CHECK_EQUAL(i1.isParametersDigestValid(), true);
185
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700186 Block wire1 = i1.wireEncode();
187 BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
188
189 Interest i2(wire1);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400190 BOOST_CHECK_EQUAL(i2.getName(),
191 "/local/ndn/prefix/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a");
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700192 BOOST_CHECK_EQUAL(i2.getCanBePrefix(), true);
193 BOOST_CHECK_EQUAL(i2.getMustBeFresh(), true);
194 BOOST_CHECK_EQUAL(i2.getForwardingHint(), DelegationList({{15893, "/H"}}));
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400195 BOOST_CHECK_EQUAL(i2.hasNonce(), true);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700196 BOOST_CHECK_EQUAL(i2.getNonce(), 0x4c1ecb4a);
197 BOOST_CHECK_EQUAL(i2.getInterestLifetime(), 30369_ms);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400198 BOOST_CHECK_EQUAL(*i2.getHopLimit(), 220);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400199 BOOST_CHECK_EQUAL(i2.getApplicationParameters(), "2404C0C1C2C3"_block);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700200}
201
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400202BOOST_AUTO_TEST_CASE(MissingApplicationParameters)
203{
204 Interest i;
205 i.setName(Name("/A").appendParametersSha256DigestPlaceholder());
206 i.setCanBePrefix(false);
207 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
208 BOOST_CHECK_THROW(i.wireEncode(), tlv::Error);
209}
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400210
211BOOST_AUTO_TEST_CASE(MissingParametersSha256DigestComponent)
212{
213 // there's no way to create an Interest that fails this check via programmatic construction,
214 // so we have to decode an invalid Interest and force reencoding
215
216 DisableAutoCheckParametersDigest disabler;
217 Interest i("050F 0703(080149) 0A04F000F000 2402CAFE"_block);
218 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
219 BOOST_CHECK_NO_THROW(i.wireEncode()); // this succeeds because it uses the cached wire encoding
220
221 i.setNonce(42); // trigger reencoding
222 BOOST_CHECK_THROW(i.wireEncode(), tlv::Error); // now the check fails while attempting to reencode
223}
224
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400225BOOST_AUTO_TEST_SUITE_END() // Encode
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400226
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400227class DecodeFixture
Junxiao Shi899277a2017-07-07 22:12:12 +0000228{
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000229protected:
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400230 DecodeFixture()
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000231 {
232 // initialize all elements to non-empty, to verify wireDecode clears them
233 i.setName("/A");
234 i.setForwardingHint({{10309, "/F"}});
235 i.setNonce(0x03d645a8);
236 i.setInterestLifetime(18554_ms);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400237 i.setHopLimit(64);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400238 i.setApplicationParameters("2404A0A1A2A3"_block);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000239 }
Junxiao Shi899277a2017-07-07 22:12:12 +0000240
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000241protected:
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000242 Interest i;
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000243};
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000244
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400245BOOST_FIXTURE_TEST_SUITE(Decode, DecodeFixture)
246
247BOOST_AUTO_TEST_CASE(NotAnInterest)
248{
249 BOOST_CHECK_THROW(i.wireDecode("4202CAFE"_block), tlv::Error);
250}
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000251
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400252BOOST_AUTO_TEST_CASE(NameOnly)
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000253{
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400254 i.wireDecode("0505 0703(080149)"_block);
Davide Pesavento835f0272019-09-21 13:18:24 -0400255 BOOST_CHECK_EQUAL(i.hasWire(), true);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000256 BOOST_CHECK_EQUAL(i.getName(), "/I");
257 BOOST_CHECK_EQUAL(i.getCanBePrefix(), false);
258 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400259 BOOST_CHECK_EQUAL(i.getForwardingHint().empty(), true);
Davide Pesavento835f0272019-09-21 13:18:24 -0400260 BOOST_CHECK_EQUAL(i.hasNonce(), false);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000261 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400262 BOOST_CHECK(i.getHopLimit() == nullopt);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400263 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), false);
264 BOOST_CHECK_EQUAL(i.getApplicationParameters().isValid(), false);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000265
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400266 // modify then re-encode
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000267 i.setNonce(0x54657c95);
Davide Pesavento835f0272019-09-21 13:18:24 -0400268 BOOST_CHECK_EQUAL(i.hasWire(), false);
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400269 BOOST_CHECK_EQUAL(i.wireEncode(), "050B 0703(080149) 0A04957C6554"_block);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000270}
271
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400272BOOST_AUTO_TEST_CASE(NameCanBePrefix)
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000273{
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400274 i.wireDecode("0507 0703(080149) 2100"_block);
Davide Pesavento835f0272019-09-21 13:18:24 -0400275 BOOST_CHECK_EQUAL(i.hasWire(), true);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400276 BOOST_CHECK_EQUAL(i.getName(), "/I");
277 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
278 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
279 BOOST_CHECK_EQUAL(i.getForwardingHint().empty(), true);
Davide Pesavento835f0272019-09-21 13:18:24 -0400280 BOOST_CHECK_EQUAL(i.hasNonce(), false);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400281 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400282 BOOST_CHECK(i.getHopLimit() == nullopt);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400283 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), false);
284 BOOST_CHECK_EQUAL(i.getApplicationParameters().isValid(), false);
285}
286
287BOOST_AUTO_TEST_CASE(FullWithoutParameters)
288{
289 i.wireDecode("0531 0703(080149) "
290 "FC00 2100 FC00 1200 FC00 1E0B(1F09 1E023E15 0703080148) "
291 "FC00 0A044ACB1E4C FC00 0C0276A1 FC00 2201D6 FC00"_block);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000292 BOOST_CHECK_EQUAL(i.getName(), "/I");
293 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
294 BOOST_CHECK_EQUAL(i.getMustBeFresh(), true);
295 BOOST_CHECK_EQUAL(i.getForwardingHint(), DelegationList({{15893, "/H"}}));
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400296 BOOST_CHECK_EQUAL(i.hasNonce(), true);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000297 BOOST_CHECK_EQUAL(i.getNonce(), 0x4c1ecb4a);
298 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 30369_ms);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400299 BOOST_CHECK_EQUAL(*i.getHopLimit(), 214);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400300 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), false);
301 BOOST_CHECK_EQUAL(i.getApplicationParameters().isValid(), false);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000302
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000303 // encode without modification: retain original wire encoding
Junxiao Shi8b753a22018-10-24 01:51:40 +0000304 BOOST_CHECK_EQUAL(i.wireEncode().value_size(), 49);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000305
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400306 // modify then re-encode: unrecognized elements are discarded
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000307 i.setName("/J");
Junxiao Shi72c0c642018-04-20 15:41:09 +0000308 BOOST_CHECK_EQUAL(i.wireEncode(),
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400309 "0523 0703(08014A) "
310 "2100 1200 1E0B(1F09 1E023E15 0703080148) "
311 "0A044ACB1E4C 0C0276A1 2201D6"_block);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400312}
313
314BOOST_AUTO_TEST_CASE(FullWithParameters)
315{
316 i.wireDecode("055B 0725(080149 0220F16DB273F40436A852063F864D5072B01EAD53151F5A688EA1560492BEBEDD05) "
317 "FC00 2100 FC00 1200 FC00 1E0B(1F09 1E023E15 0703080148) "
318 "FC00 0A044ACB1E4C FC00 0C0276A1 FC00 2201D6 FC00 2404C0C1C2C3 FC00"_block);
319 BOOST_CHECK_EQUAL(i.getName(),
320 "/I/params-sha256=f16db273f40436a852063f864d5072b01ead53151f5a688ea1560492bebedd05");
321 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
322 BOOST_CHECK_EQUAL(i.getMustBeFresh(), true);
323 BOOST_CHECK_EQUAL(i.getForwardingHint(), DelegationList({{15893, "/H"}}));
324 BOOST_CHECK_EQUAL(i.hasNonce(), true);
325 BOOST_CHECK_EQUAL(i.getNonce(), 0x4c1ecb4a);
326 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 30369_ms);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400327 BOOST_CHECK_EQUAL(*i.getHopLimit(), 214);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400328 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true);
329 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2404C0C1C2C3"_block);
330
331 // encode without modification: retain original wire encoding
332 BOOST_CHECK_EQUAL(i.wireEncode().value_size(), 91);
333
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400334 // modify then re-encode: unrecognized elements after ApplicationParameters
335 // are preserved, the rest are discarded
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400336 i.setName("/J");
337 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
338 BOOST_CHECK_EQUAL(i.wireEncode(),
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400339 "054D 0725(08014A 0220F16DB273F40436A852063F864D5072B01EAD53151F5A688EA1560492BEBEDD05) "
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400340 "2100 1200 1E0B(1F09 1E023E15 0703080148) "
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400341 "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3 FC00"_block);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400342
343 // modify ApplicationParameters: unrecognized elements are preserved
344 i.setApplicationParameters("2402CAFE"_block);
345 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
346 BOOST_CHECK_EQUAL(i.wireEncode(),
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400347 "054B 0725(08014A 02205FDA67967EE302FC457E41B7D3D51BA6A9379574D193FD88F64954BF16C2927A) "
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400348 "2100 1200 1E0B(1F09 1E023E15 0703080148) "
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400349 "0A044ACB1E4C 0C0276A1 2201D6 2402CAFE FC00"_block);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000350}
351
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000352BOOST_AUTO_TEST_CASE(CriticalElementOutOfOrder)
353{
354 BOOST_CHECK_THROW(i.wireDecode(
355 "0529 2100 0703080149 1200 1E0B(1F09 1E023E15 0703080148) "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500356 "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3"_block),
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000357 tlv::Error);
358 BOOST_CHECK_THROW(i.wireDecode(
359 "0529 0703080149 1200 2100 1E0B(1F09 1E023E15 0703080148) "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500360 "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3"_block),
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000361 tlv::Error);
362 BOOST_CHECK_THROW(i.wireDecode(
363 "0529 0703080149 2100 1E0B(1F09 1E023E15 0703080148) 1200 "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500364 "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3"_block),
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000365 tlv::Error);
366 BOOST_CHECK_THROW(i.wireDecode(
367 "0529 0703080149 2100 1200 0A044ACB1E4C "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500368 "1E0B(1F09 1E023E15 0703080148) 0C0276A1 2201D6 2404C0C1C2C3"_block),
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000369 tlv::Error);
370 BOOST_CHECK_THROW(i.wireDecode(
371 "0529 0703080149 2100 1200 1E0B(1F09 1E023E15 0703080148) "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500372 "0C0276A1 0A044ACB1E4C 2201D6 2404C0C1C2C3"_block),
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000373 tlv::Error);
374 BOOST_CHECK_THROW(i.wireDecode(
375 "0529 0703080149 2100 1200 1E0B(1F09 1E023E15 0703080148) "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500376 "0A044ACB1E4C 2201D6 0C0276A1 2404C0C1C2C3"_block),
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000377 tlv::Error);
378}
379
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500380BOOST_AUTO_TEST_CASE(NonCriticalElementOutOfOrder)
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000381{
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400382 // duplicate HopLimit
383 i.wireDecode("0536 0725(080149 0220FF9100E04EAADCF30674D98026A051BA25F56B69BFA026DCCCD72C6EA0F7315A)"
384 "2201D6 2200 2404C0C1C2C3 22020101"_block);
385 BOOST_CHECK_EQUAL(i.getName(),
386 "/I/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a");
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400387 BOOST_CHECK_EQUAL(*i.getHopLimit(), 214);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400388 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400389 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2404C0C1C2C3"_block);
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500390
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400391 // duplicate ApplicationParameters
392 i.wireDecode("0541 0725(080149 0220FF9100E04EAADCF30674D98026A051BA25F56B69BFA026DCCCD72C6EA0F7315A)"
393 "2100 1200 0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3 2401EE"_block);
394 BOOST_CHECK_EQUAL(i.getName(),
395 "/I/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a");
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400396 BOOST_CHECK_EQUAL(*i.getHopLimit(), 214);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400397 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true);
398 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2404C0C1C2C3"_block);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000399}
400
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400401BOOST_AUTO_TEST_CASE(MissingName)
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000402{
403 BOOST_CHECK_THROW(i.wireDecode("0500"_block), tlv::Error);
404 BOOST_CHECK_THROW(i.wireDecode("0502 1200"_block), tlv::Error);
405}
406
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400407BOOST_AUTO_TEST_CASE(BadName)
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000408{
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400409 // empty
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000410 BOOST_CHECK_THROW(i.wireDecode("0502 0700"_block), tlv::Error);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400411
412 // more than one ParametersSha256DigestComponent
413 BOOST_CHECK_THROW(i.wireDecode("054C 074A(080149"
414 "02200000000000000000000000000000000000000000000000000000000000000000"
415 "080132"
416 "02200000000000000000000000000000000000000000000000000000000000000000)"_block),
417 tlv::Error);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000418}
419
420BOOST_AUTO_TEST_CASE(BadCanBePrefix)
421{
422 BOOST_CHECK_THROW(i.wireDecode("0508 0703080149 210102"_block), tlv::Error);
423}
424
425BOOST_AUTO_TEST_CASE(BadMustBeFresh)
426{
427 BOOST_CHECK_THROW(i.wireDecode("0508 0703080149 120102"_block), tlv::Error);
428}
429
430BOOST_AUTO_TEST_CASE(BadNonce)
431{
432 BOOST_CHECK_THROW(i.wireDecode("0507 0703080149 0A00"_block), tlv::Error);
433 BOOST_CHECK_THROW(i.wireDecode("050A 0703080149 0A0304C263"_block), tlv::Error);
434 BOOST_CHECK_THROW(i.wireDecode("050C 0703080149 0A05EFA420B262"_block), tlv::Error);
435}
436
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500437BOOST_AUTO_TEST_CASE(BadHopLimit)
438{
439 BOOST_CHECK_THROW(i.wireDecode("0507 0703080149 2200"_block), tlv::Error);
440 BOOST_CHECK_THROW(i.wireDecode("0509 0703080149 22021356"_block), tlv::Error);
441}
442
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400443BOOST_AUTO_TEST_CASE(BadParametersDigest)
444{
445 // ApplicationParameters without ParametersSha256DigestComponent
446 Block b1("0509 0703(080149) 2402CAFE"_block);
447 // ParametersSha256DigestComponent without ApplicationParameters
448 Block b2("0527 0725(080149 0220E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855)"_block);
449 // digest mismatch
450 Block b3("052B 0725(080149 02200000000000000000000000000000000000000000000000000000000000000000) "
451 "2402CAFE"_block);
452
453 BOOST_CHECK_THROW(i.wireDecode(b1), tlv::Error);
454 BOOST_CHECK_THROW(i.wireDecode(b2), tlv::Error);
455 BOOST_CHECK_THROW(i.wireDecode(b3), tlv::Error);
456
457 DisableAutoCheckParametersDigest disabler;
458 BOOST_CHECK_NO_THROW(i.wireDecode(b1));
459 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
460 BOOST_CHECK_NO_THROW(i.wireDecode(b2));
461 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
462 BOOST_CHECK_NO_THROW(i.wireDecode(b3));
463 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
464}
465
Junxiao Shi8b753a22018-10-24 01:51:40 +0000466BOOST_AUTO_TEST_CASE(UnrecognizedNonCriticalElementBeforeName)
467{
468 BOOST_CHECK_THROW(i.wireDecode("0507 FC00 0703080149"_block), tlv::Error);
469}
470
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000471BOOST_AUTO_TEST_CASE(UnrecognizedCriticalElement)
472{
473 BOOST_CHECK_THROW(i.wireDecode("0507 0703080149 FB00"_block), tlv::Error);
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400474 // v0.2 packet with Selectors
475 BOOST_CHECK_THROW(i.wireDecode("0507 0703080149 09030D0101 0A0401000000"_block), tlv::Error);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000476}
477
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400478BOOST_AUTO_TEST_SUITE_END() // Decode
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000479
Junxiao Shi899277a2017-07-07 22:12:12 +0000480BOOST_AUTO_TEST_CASE(MatchesData)
481{
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000482 auto interest = makeInterest("/A");
Junxiao Shi899277a2017-07-07 22:12:12 +0000483
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000484 auto data = makeData("/A");
485 BOOST_CHECK_EQUAL(interest->matchesData(*data), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000486
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000487 data->setName("/A/D");
488 BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // violates CanBePrefix
Junxiao Shi899277a2017-07-07 22:12:12 +0000489
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000490 interest->setCanBePrefix(true);
491 BOOST_CHECK_EQUAL(interest->matchesData(*data), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000492
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000493 interest->setMustBeFresh(true);
494 BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // violates MustBeFresh
Junxiao Shi899277a2017-07-07 22:12:12 +0000495
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000496 data->setFreshnessPeriod(1_s);
497 BOOST_CHECK_EQUAL(interest->matchesData(*data), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000498
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000499 data->setName("/H/I");
500 BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // Name does not match
Junxiao Shi899277a2017-07-07 22:12:12 +0000501
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000502 data->wireEncode();
503 interest = makeInterest(data->getFullName());
504 BOOST_CHECK_EQUAL(interest->matchesData(*data), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000505
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400506 setNameComponent(*interest, -1, name::Component::fromEscapedString(
507 "sha256digest=0000000000000000000000000000000000000000000000000000000000000000"));
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000508 BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // violates implicit digest
Junxiao Shi899277a2017-07-07 22:12:12 +0000509}
510
511BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(MatchesInterest, 1)
512BOOST_AUTO_TEST_CASE(MatchesInterest)
513{
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400514 Interest interest;
515 interest.setName("/A")
516 .setCanBePrefix(true)
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000517 .setMustBeFresh(true)
518 .setForwardingHint({{1, "/H"}})
519 .setNonce(2228)
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400520 .setInterestLifetime(5_s)
521 .setHopLimit(90);
Junxiao Shi899277a2017-07-07 22:12:12 +0000522
523 Interest other;
524 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false);
525
526 other.setName(interest.getName());
527 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false);
528
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000529 other.setCanBePrefix(interest.getCanBePrefix());
530 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false);
531
532 other.setMustBeFresh(interest.getMustBeFresh());
Junxiao Shi899277a2017-07-07 22:12:12 +0000533 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false); // will match until #3162 implemented
534
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000535 other.setForwardingHint(interest.getForwardingHint());
Junxiao Shi899277a2017-07-07 22:12:12 +0000536 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
537
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000538 other.setNonce(9336);
Junxiao Shi899277a2017-07-07 22:12:12 +0000539 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
540
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000541 other.setInterestLifetime(3_s);
Junxiao Shi899277a2017-07-07 22:12:12 +0000542 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400543
544 other.setHopLimit(31);
545 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000546}
547
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400548BOOST_AUTO_TEST_CASE(SetName)
549{
550 Interest i;
551 BOOST_CHECK_EQUAL(i.getName(), "/");
552 i.setName("/A/B");
553 BOOST_CHECK_EQUAL(i.getName(), "/A/B");
554 i.setName("/I/params-sha256=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
555 BOOST_CHECK_EQUAL(i.getName(),
556 "/I/params-sha256=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
557 BOOST_CHECK_THROW(i.setName("/I"
558 "/params-sha256=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
559 "/params-sha256=0000000000000000000000000000000000000000000000000000000000000000"),
560 std::invalid_argument);
561}
Junxiao Shi899277a2017-07-07 22:12:12 +0000562
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400563BOOST_AUTO_TEST_CASE(SetCanBePrefix)
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000564{
565 Interest i;
566 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
567 i.setCanBePrefix(false);
568 BOOST_CHECK_EQUAL(i.getCanBePrefix(), false);
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000569 i.setCanBePrefix(true);
570 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000571}
572
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400573BOOST_AUTO_TEST_CASE(SetMustBeFresh)
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000574{
575 Interest i;
576 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
577 i.setMustBeFresh(true);
578 BOOST_CHECK_EQUAL(i.getMustBeFresh(), true);
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000579 i.setMustBeFresh(false);
580 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000581}
582
583BOOST_AUTO_TEST_CASE(ModifyForwardingHint)
584{
Davide Pesavento0e0b3892019-07-30 21:05:05 -0400585 Interest i("/I");
Junxiao Shib55e5d32018-07-18 13:32:00 -0600586 i.setCanBePrefix(false);
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000587 i.setForwardingHint({{1, "/A"}});
588 i.wireEncode();
589 BOOST_CHECK(i.hasWire());
590
591 i.modifyForwardingHint([] (DelegationList& fh) { fh.insert(2, "/B"); });
592 BOOST_CHECK(!i.hasWire());
593 BOOST_CHECK_EQUAL(i.getForwardingHint(), DelegationList({{1, "/A"}, {2, "/B"}}));
594}
595
Junxiao Shi899277a2017-07-07 22:12:12 +0000596BOOST_AUTO_TEST_CASE(GetNonce)
597{
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000598 unique_ptr<Interest> i1, i2;
Junxiao Shi899277a2017-07-07 22:12:12 +0000599
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000600 // getNonce automatically assigns a random Nonce.
601 // It's possible to assign the same Nonce to two Interest, but it's unlikely to get 100 pairs of
602 // same Nonces in a row.
Junxiao Shi899277a2017-07-07 22:12:12 +0000603 int nIterations = 0;
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000604 uint32_t nonce1 = 0, nonce2 = 0;
Junxiao Shi899277a2017-07-07 22:12:12 +0000605 do {
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000606 i1 = make_unique<Interest>();
607 nonce1 = i1->getNonce();
608 i2 = make_unique<Interest>();
609 nonce2 = i2->getNonce();
Junxiao Shi899277a2017-07-07 22:12:12 +0000610 }
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000611 while (nonce1 == nonce2 && ++nIterations < 100);
612 BOOST_CHECK_NE(nonce1, nonce2);
613 BOOST_CHECK(i1->hasNonce());
614 BOOST_CHECK(i2->hasNonce());
Junxiao Shi899277a2017-07-07 22:12:12 +0000615
616 // Once a Nonce is assigned, it should not change.
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000617 BOOST_CHECK_EQUAL(i1->getNonce(), nonce1);
618}
619
620BOOST_AUTO_TEST_CASE(SetNonce)
621{
622 Interest i1("/A");
Junxiao Shib55e5d32018-07-18 13:32:00 -0600623 i1.setCanBePrefix(false);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000624 i1.setNonce(1);
625 i1.wireEncode();
626 BOOST_CHECK_EQUAL(i1.getNonce(), 1);
627
628 Interest i2(i1);
629 BOOST_CHECK_EQUAL(i2.getNonce(), 1);
630
631 i2.setNonce(2);
632 BOOST_CHECK_EQUAL(i2.getNonce(), 2);
633 BOOST_CHECK_EQUAL(i1.getNonce(), 1); // should not affect i1 Nonce (Bug #4168)
Junxiao Shi899277a2017-07-07 22:12:12 +0000634}
635
636BOOST_AUTO_TEST_CASE(RefreshNonce)
637{
638 Interest i;
639 BOOST_CHECK(!i.hasNonce());
640 i.refreshNonce();
641 BOOST_CHECK(!i.hasNonce());
642
643 i.setNonce(1);
644 BOOST_CHECK(i.hasNonce());
645 i.refreshNonce();
646 BOOST_CHECK(i.hasNonce());
647 BOOST_CHECK_NE(i.getNonce(), 1);
648}
649
650BOOST_AUTO_TEST_CASE(SetInterestLifetime)
651{
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500652 BOOST_CHECK_THROW(Interest("/A", -1_ms), std::invalid_argument);
Davide Pesavento0f830802018-01-16 23:58:58 -0500653 BOOST_CHECK_NO_THROW(Interest("/A", 0_ms));
Junxiao Shi899277a2017-07-07 22:12:12 +0000654
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400655 Interest i;
Junxiao Shi899277a2017-07-07 22:12:12 +0000656 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500657 BOOST_CHECK_THROW(i.setInterestLifetime(-1_ms), std::invalid_argument);
Junxiao Shi899277a2017-07-07 22:12:12 +0000658 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesavento0f830802018-01-16 23:58:58 -0500659 i.setInterestLifetime(0_ms);
660 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 0_ms);
661 i.setInterestLifetime(1_ms);
662 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 1_ms);
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400663
664 i = Interest("/B", 15_s);
665 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 15_s);
666}
667
668BOOST_AUTO_TEST_CASE(SetHopLimit)
669{
670 Interest i;
671 BOOST_CHECK(i.getHopLimit() == nullopt);
672 i.setHopLimit(42);
673 BOOST_CHECK(i.getHopLimit() == 42);
674 i.setHopLimit(nullopt);
675 BOOST_CHECK(i.getHopLimit() == nullopt);
Junxiao Shi899277a2017-07-07 22:12:12 +0000676}
677
Davide Pesavento9c19a392019-04-06 15:07:54 -0400678BOOST_AUTO_TEST_CASE(SetApplicationParameters)
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700679{
680 const uint8_t PARAMETERS1[] = {0xc1};
681 const uint8_t PARAMETERS2[] = {0xc2};
682
683 Interest i;
Davide Pesavento9c19a392019-04-06 15:07:54 -0400684 BOOST_CHECK(!i.hasApplicationParameters());
685 i.setApplicationParameters("2400"_block);
686 BOOST_CHECK(i.hasApplicationParameters());
687 i.unsetApplicationParameters();
688 BOOST_CHECK(!i.hasApplicationParameters());
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700689
Davide Pesavento38912442019-04-06 22:03:39 -0400690 // Block overload
691 i.setApplicationParameters(Block{});
692 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2400"_block);
693 i.setApplicationParameters("2401C0"_block);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400694 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2401C0"_block);
Davide Pesavento38912442019-04-06 22:03:39 -0400695 i.setApplicationParameters("8001C1"_block);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400696 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "24038001C1"_block);
Davide Pesavento38912442019-04-06 22:03:39 -0400697
698 // raw buffer+size overload
699 i.setApplicationParameters(PARAMETERS1, sizeof(PARAMETERS1));
700 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2401C1"_block);
701 i.setApplicationParameters(nullptr, 0);
702 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2400"_block);
703 BOOST_CHECK_THROW(i.setApplicationParameters(nullptr, 42), std::invalid_argument);
704
705 // ConstBufferPtr overload
706 i.setApplicationParameters(make_shared<Buffer>(PARAMETERS2, sizeof(PARAMETERS2)));
707 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2401C2"_block);
708 i.setApplicationParameters(make_shared<Buffer>());
709 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2400"_block);
710 BOOST_CHECK_THROW(i.setApplicationParameters(nullptr), std::invalid_argument);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700711}
712
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400713BOOST_AUTO_TEST_CASE(ParametersSha256DigestComponent)
714{
715 Interest i("/I");
716 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
717
718 i.setApplicationParameters("2404C0C1C2C3"_block); // auto-appends ParametersSha256DigestComponent
719 BOOST_CHECK_EQUAL(i.getName(),
720 "/I/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a");
721 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
722
723 i.setApplicationParameters(nullptr, 0); // updates ParametersSha256DigestComponent
724 BOOST_CHECK_EQUAL(i.getName(),
725 "/I/params-sha256=33b67cb5385ceddad93d0ee960679041613bed34b8b4a5e6362fe7539ba2d3ce");
726 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true);
727 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
728
729 i.unsetApplicationParameters(); // removes ParametersSha256DigestComponent
730 BOOST_CHECK_EQUAL(i.getName(), "/I");
731 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
732
733 i.setName(Name("/P").appendParametersSha256DigestPlaceholder().append("Q"));
734 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), false);
735 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false);
736
737 i.unsetApplicationParameters(); // removes ParametersSha256DigestComponent
738 BOOST_CHECK_EQUAL(i.getName(), "/P/Q");
739 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
740
741 i.setName(Name("/P").appendParametersSha256DigestPlaceholder().append("Q"));
742 i.setApplicationParameters("2404C0C1C2C3"_block); // updates ParametersSha256DigestComponent
743 BOOST_CHECK_EQUAL(i.getName(),
744 "/P/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a/Q");
745 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
746
747 i.setName("/A/B/C"); // auto-appends ParametersSha256DigestComponent
748 BOOST_CHECK_EQUAL(i.getName(),
749 "/A/B/C/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a");
750 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true);
751 BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true);
752}
Junxiao Shi899277a2017-07-07 22:12:12 +0000753
Davide Pesavento2fdb2742019-07-31 23:03:35 -0400754BOOST_AUTO_TEST_CASE(ToUri)
755{
756 Interest i;
757 i.setCanBePrefix(false);
758 BOOST_CHECK_EQUAL(i.toUri(), "/");
759
760 i.setName("/foo");
761 BOOST_CHECK_EQUAL(i.toUri(), "/foo");
762
763 i.setCanBePrefix(true);
764 BOOST_CHECK_EQUAL(i.toUri(), "/foo?CanBePrefix");
765
766 i.setMustBeFresh(true);
767 BOOST_CHECK_EQUAL(i.toUri(), "/foo?CanBePrefix&MustBeFresh");
768
769 i.setNonce(1234);
770 BOOST_CHECK_EQUAL(i.toUri(), "/foo?CanBePrefix&MustBeFresh&Nonce=1234");
771
772 i.setInterestLifetime(2_s);
773 BOOST_CHECK_EQUAL(i.toUri(), "/foo?CanBePrefix&MustBeFresh&Nonce=1234&Lifetime=2000");
774
775 i.setHopLimit(18);
776 BOOST_CHECK_EQUAL(i.toUri(), "/foo?CanBePrefix&MustBeFresh&Nonce=1234&Lifetime=2000&HopLimit=18");
777
778 i.setCanBePrefix(false);
779 i.setMustBeFresh(false);
780 i.setHopLimit(nullopt);
781 i.setApplicationParameters("2402CAFE"_block);
782 BOOST_CHECK_EQUAL(i.toUri(),
783 "/foo/params-sha256=8621f5e8321f04104640c8d02877d7c5142cad6e203c5effda1783b1a0e476d6"
784 "?Nonce=1234&Lifetime=2000");
785}
786
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100787BOOST_AUTO_TEST_SUITE_END() // TestInterest
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800788
Alexander Afanasyev90164962014-03-06 08:29:59 +0000789} // namespace tests
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800790} // namespace ndn