blob: 9c4b55d8cf7f71e7d11088f8887d2074ec987ce9 [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"
24#include "ndn-cxx/security/digest-sha256.hpp"
25#include "ndn-cxx/security/signature-sha256-with-rsa.hpp"
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070026
Davide Pesavento7e780642018-11-24 15:51:34 -050027#include "tests/boost-test.hpp"
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +000028#include "tests/make-interest-data.hpp"
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080029
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080030namespace ndn {
Alexander Afanasyev90164962014-03-06 08:29:59 +000031namespace tests {
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080032
Junxiao Shi899277a2017-07-07 22:12:12 +000033BOOST_AUTO_TEST_SUITE(TestInterest)
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080034
Junxiao Shi899277a2017-07-07 22:12:12 +000035// ---- constructor, encode, decode ----
36
37BOOST_AUTO_TEST_CASE(DefaultConstructor)
38{
39 Interest i;
Junxiao Shi6efa3b72018-04-14 15:54:08 +000040 BOOST_CHECK(!i.hasWire());
Junxiao Shi899277a2017-07-07 22:12:12 +000041 BOOST_CHECK_EQUAL(i.getName(), "/");
Junxiao Shi6efa3b72018-04-14 15:54:08 +000042 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
43 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
44 BOOST_CHECK(i.getForwardingHint().empty());
45 BOOST_CHECK(!i.hasNonce());
Junxiao Shi899277a2017-07-07 22:12:12 +000046 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Junxiao Shi6efa3b72018-04-14 15:54:08 +000047 BOOST_CHECK(!i.hasSelectors());
Davide Pesavento9c19a392019-04-06 15:07:54 -040048 BOOST_CHECK(!i.hasApplicationParameters());
49 BOOST_CHECK(i.getApplicationParameters().empty());
Davide Pesaventofccb2dc2019-02-09 01:02:35 -050050}
51
52BOOST_AUTO_TEST_CASE(DecodeNotInterest)
53{
54 BOOST_CHECK_THROW(Interest("4202CAFE"_block), tlv::Error);
Junxiao Shi899277a2017-07-07 22:12:12 +000055}
56
Junxiao Shi6efa3b72018-04-14 15:54:08 +000057BOOST_AUTO_TEST_CASE(EncodeDecode02Basic)
Junxiao Shi899277a2017-07-07 22:12:12 +000058{
59 const uint8_t WIRE[] = {
60 0x05, 0x1c, // Interest
61 0x07, 0x14, // Name
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +000062 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent
63 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent
64 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent
Junxiao Shi899277a2017-07-07 22:12:12 +000065 0x0a, 0x04, // Nonce
66 0x01, 0x00, 0x00, 0x00
67 };
68
69 Interest i1("/local/ndn/prefix");
Junxiao Shib55e5d32018-07-18 13:32:00 -060070 i1.setCanBePrefix(true);
Junxiao Shi899277a2017-07-07 22:12:12 +000071 i1.setNonce(1);
72 Block wire1 = i1.wireEncode();
73 BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
74
75 Interest i2(wire1);
76 BOOST_CHECK_EQUAL(i2.getName(), "/local/ndn/prefix");
77 BOOST_CHECK(i2.getSelectors().empty());
78 BOOST_CHECK_EQUAL(i2.getNonce(), 1);
79 BOOST_CHECK_EQUAL(i2.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
80
81 BOOST_CHECK_EQUAL(i1, i2);
82}
83
Junxiao Shi6efa3b72018-04-14 15:54:08 +000084BOOST_AUTO_TEST_CASE(EncodeDecode02Full)
Junxiao Shi899277a2017-07-07 22:12:12 +000085{
86 const uint8_t WIRE[] = {
Junxiao Shi9c154cb2017-07-07 22:14:54 +000087 0x05, 0x31, // Interest
Junxiao Shi899277a2017-07-07 22:12:12 +000088 0x07, 0x14, // Name
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +000089 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent
90 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent
91 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent
Junxiao Shi899277a2017-07-07 22:12:12 +000092 0x09, 0x03, // Selectors
93 0x0d, 0x01, 0x01, // MinSuffixComponents
94 0x0a, 0x04, // Nonce
95 0x01, 0x00, 0x00, 0x00,
96 0x0c, 0x02, // InterestLifetime
Junxiao Shi9c154cb2017-07-07 22:14:54 +000097 0x03, 0xe8,
98 0x1e, 0x0a, // ForwardingHint
99 0x1f, 0x08, // Delegation
100 0x1e, 0x01, 0x01, // Preference=1
101 0x07, 0x03, 0x08, 0x01, 0x41 // Name=/A
Junxiao Shi899277a2017-07-07 22:12:12 +0000102 };
Junxiao Shi899277a2017-07-07 22:12:12 +0000103
104 Interest i1;
105 i1.setName("/local/ndn/prefix");
Junxiao Shib55e5d32018-07-18 13:32:00 -0600106 i1.setCanBePrefix(true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000107 i1.setMinSuffixComponents(1);
108 i1.setNonce(1);
Davide Pesavento0f830802018-01-16 23:58:58 -0500109 i1.setInterestLifetime(1000_ms);
Junxiao Shi9c154cb2017-07-07 22:14:54 +0000110 i1.setForwardingHint({{1, "/A"}});
Junxiao Shi899277a2017-07-07 22:12:12 +0000111 Block wire1 = i1.wireEncode();
112 BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
113
114 Interest i2(wire1);
115 BOOST_CHECK_EQUAL(i2.getName(), "/local/ndn/prefix");
116 BOOST_CHECK_EQUAL(i2.getMinSuffixComponents(), 1);
117 BOOST_CHECK_EQUAL(i2.getNonce(), 1);
Davide Pesavento0f830802018-01-16 23:58:58 -0500118 BOOST_CHECK_EQUAL(i2.getInterestLifetime(), 1000_ms);
Junxiao Shi9c154cb2017-07-07 22:14:54 +0000119 BOOST_CHECK_EQUAL(i2.getForwardingHint(), DelegationList({{1, "/A"}}));
Junxiao Shi899277a2017-07-07 22:12:12 +0000120
121 BOOST_CHECK_EQUAL(i1, i2);
122}
123
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700124BOOST_AUTO_TEST_CASE(EncodeDecode03Basic)
125{
126 const uint8_t WIRE[] = {
127 0x05, 0x22, // Interest
128 0x07, 0x14, // Name
129 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent
130 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent
131 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent
132 0x0a, 0x04, // Nonce
133 0x01, 0x00, 0x00, 0x00,
Davide Pesavento9c19a392019-04-06 15:07:54 -0400134 0x24, 0x04, // ApplicationParameters
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700135 0xc0, 0xc1, 0xc2, 0xc3};
136
137 Interest i1;
138 i1.setName("/local/ndn/prefix");
139 i1.setCanBePrefix(false);
140 i1.setNonce(1);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400141 i1.setApplicationParameters("2404C0C1C2C3"_block);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700142 Block wire1 = i1.wireEncode();
143 BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
144
145 Interest i2(wire1);
146 BOOST_CHECK_EQUAL(i2.getName(), "/local/ndn/prefix");
147 BOOST_CHECK_EQUAL(i2.getCanBePrefix(), false);
148 BOOST_CHECK_EQUAL(i2.getMustBeFresh(), false);
149 BOOST_CHECK(i2.getForwardingHint().empty());
150 BOOST_CHECK_EQUAL(i2.getNonce(), 1);
151 BOOST_CHECK_EQUAL(i2.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400152 BOOST_CHECK(i2.hasApplicationParameters());
153 BOOST_CHECK_EQUAL(i2.getApplicationParameters(), "2404C0C1C2C3"_block);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700154 BOOST_CHECK(i2.getPublisherPublicKeyLocator().empty());
155}
156
157BOOST_AUTO_TEST_CASE(EncodeDecode03Full)
158{
159 const uint8_t WIRE[] = {
160 0x05, 0x37, // Interest
161 0x07, 0x14, // Name
162 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent
163 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent
164 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent
165 0x21, 0x00, // CanBePrefix
166 0x12, 0x00, // MustBeFresh
167 0x1e, 0x0b, // ForwardingHint
168 0x1f, 0x09, // Delegation List
169 0x1e, 0x02,
170 0x3e, 0x15,
171 0x07, 0x03,
172 0x08, 0x01, 0x48,
173 0x0a, 0x04, // Nonce
174 0x4a, 0xcb, 0x1e, 0x4c,
175 0x0c, 0x02, // Interest Lifetime
176 0x76, 0xa1,
Davide Pesavento9c19a392019-04-06 15:07:54 -0400177 0x24, 0x04, // ApplicationParameters
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700178 0xc0, 0xc1, 0xc2, 0xc3};
179 Interest i1;
180 i1.setName("/local/ndn/prefix");
181 i1.setMustBeFresh(true);
182 i1.setCanBePrefix(true);
183 i1.setForwardingHint(DelegationList({{15893, "/H"}}));
184 i1.setNonce(0x4c1ecb4a);
185 i1.setInterestLifetime(30369_ms);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400186 i1.setApplicationParameters("2404C0C1C2C3"_block);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700187 i1.setMinSuffixComponents(1); // v0.2-only elements will not be encoded
188 i1.setExclude(Exclude().excludeAfter(name::Component("J"))); // v0.2-only elements will not be encoded
189 Block wire1 = i1.wireEncode();
190 BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
191
192 Interest i2(wire1);
193 BOOST_CHECK_EQUAL(i2.getName(), "/local/ndn/prefix");
194 BOOST_CHECK_EQUAL(i2.getCanBePrefix(), true);
195 BOOST_CHECK_EQUAL(i2.getMustBeFresh(), true);
196 BOOST_CHECK_EQUAL(i2.getForwardingHint(), DelegationList({{15893, "/H"}}));
197 BOOST_CHECK(i2.hasNonce());
198 BOOST_CHECK_EQUAL(i2.getNonce(), 0x4c1ecb4a);
199 BOOST_CHECK_EQUAL(i2.getInterestLifetime(), 30369_ms);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400200 BOOST_CHECK_EQUAL(i2.getApplicationParameters(), "2404C0C1C2C3"_block);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700201 BOOST_CHECK_EQUAL(i2.getMinSuffixComponents(), -1); // Default because minSuffixComponents was not encoded
202 BOOST_CHECK(i2.getExclude().empty()); // Exclude was not encoded
203}
204
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000205class Decode03Fixture
Junxiao Shi899277a2017-07-07 22:12:12 +0000206{
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000207protected:
208 Decode03Fixture()
209 {
210 // initialize all elements to non-empty, to verify wireDecode clears them
211 i.setName("/A");
212 i.setForwardingHint({{10309, "/F"}});
213 i.setNonce(0x03d645a8);
214 i.setInterestLifetime(18554_ms);
215 i.setPublisherPublicKeyLocator(Name("/K"));
Davide Pesavento9c19a392019-04-06 15:07:54 -0400216 i.setApplicationParameters("2404A0A1A2A3"_block);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000217 }
Junxiao Shi899277a2017-07-07 22:12:12 +0000218
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000219protected:
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000220 Interest i;
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000221};
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000222
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000223BOOST_FIXTURE_TEST_SUITE(Decode03, Decode03Fixture)
224
225BOOST_AUTO_TEST_CASE(Minimal)
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000226{
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000227 i.wireDecode("0505 0703080149"_block);
228 BOOST_CHECK_EQUAL(i.getName(), "/I");
229 BOOST_CHECK_EQUAL(i.getCanBePrefix(), false);
230 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
231 BOOST_CHECK(i.getForwardingHint().empty());
232 BOOST_CHECK(i.hasNonce()); // a random nonce is generated
233 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
234 BOOST_CHECK(i.getPublisherPublicKeyLocator().empty());
Davide Pesavento9c19a392019-04-06 15:07:54 -0400235 BOOST_CHECK(!i.hasApplicationParameters());
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000236
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000237 BOOST_CHECK(!i.hasWire()); // nonce generation resets wire encoding
238
239 // modify then re-encode as v0.2 format
240 i.setNonce(0x54657c95);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000241 BOOST_CHECK_EQUAL(i.wireEncode(), "0510 0703080149 09030E0101 0A04957C6554"_block);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000242}
243
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000244BOOST_AUTO_TEST_CASE(Full)
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000245{
Junxiao Shi8b753a22018-10-24 01:51:40 +0000246 i.wireDecode("0531 0703080149 FC00 2100 FC00 1200 "
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000247 "FC00 1E0B(1F09 1E023E15 0703080148) FC00 0A044ACB1E4C "
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700248 "FC00 0C0276A1 FC00 2201D6 FC00"_block);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000249 BOOST_CHECK_EQUAL(i.getName(), "/I");
250 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
251 BOOST_CHECK_EQUAL(i.getMustBeFresh(), true);
252 BOOST_CHECK_EQUAL(i.getForwardingHint(), DelegationList({{15893, "/H"}}));
253 BOOST_CHECK(i.hasNonce());
254 BOOST_CHECK_EQUAL(i.getNonce(), 0x4c1ecb4a);
255 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 30369_ms);
256 // HopLimit=214 is not stored
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000257
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000258 // encode without modification: retain original wire encoding
Junxiao Shi8b753a22018-10-24 01:51:40 +0000259 BOOST_CHECK_EQUAL(i.wireEncode().value_size(), 49);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000260
261 // modify then re-encode as v0.2 format
262 i.setName("/J");
Junxiao Shi72c0c642018-04-20 15:41:09 +0000263 BOOST_CHECK_EQUAL(i.wireEncode(),
264 "0520 070308014A 09021200 0A044ACB1E4C 0C0276A1 1E0B(1F09 1E023E15 0703080148)"_block);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000265}
266
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000267BOOST_AUTO_TEST_CASE(CriticalElementOutOfOrder)
268{
269 BOOST_CHECK_THROW(i.wireDecode(
270 "0529 2100 0703080149 1200 1E0B(1F09 1E023E15 0703080148) "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500271 "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3"_block),
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000272 tlv::Error);
273 BOOST_CHECK_THROW(i.wireDecode(
274 "0529 0703080149 1200 2100 1E0B(1F09 1E023E15 0703080148) "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500275 "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3"_block),
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000276 tlv::Error);
277 BOOST_CHECK_THROW(i.wireDecode(
278 "0529 0703080149 2100 1E0B(1F09 1E023E15 0703080148) 1200 "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500279 "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3"_block),
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000280 tlv::Error);
281 BOOST_CHECK_THROW(i.wireDecode(
282 "0529 0703080149 2100 1200 0A044ACB1E4C "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500283 "1E0B(1F09 1E023E15 0703080148) 0C0276A1 2201D6 2404C0C1C2C3"_block),
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000284 tlv::Error);
285 BOOST_CHECK_THROW(i.wireDecode(
286 "0529 0703080149 2100 1200 1E0B(1F09 1E023E15 0703080148) "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500287 "0C0276A1 0A044ACB1E4C 2201D6 2404C0C1C2C3"_block),
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000288 tlv::Error);
289 BOOST_CHECK_THROW(i.wireDecode(
290 "0529 0703080149 2100 1200 1E0B(1F09 1E023E15 0703080148) "
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500291 "0A044ACB1E4C 2201D6 0C0276A1 2404C0C1C2C3"_block),
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000292 tlv::Error);
293}
294
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500295BOOST_AUTO_TEST_CASE(NonCriticalElementOutOfOrder)
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000296{
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500297 // HopLimit
298 i.wireDecode("0514 0703080149 2201D6 2200 2404C0C1C2C3 22020101"_block);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000299 BOOST_CHECK_EQUAL(i.getName(), "/I");
300 // HopLimit=214 is not stored
Davide Pesavento9c19a392019-04-06 15:07:54 -0400301 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2404C0C1C2C3"_block);
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500302
Davide Pesavento9c19a392019-04-06 15:07:54 -0400303 // ApplicationParameters
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500304 i.wireDecode("051F 0703080149 2100 1200 0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3 2401EE"_block);
305 BOOST_CHECK_EQUAL(i.getName(), "/I");
Davide Pesavento9c19a392019-04-06 15:07:54 -0400306 BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true);
307 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2404C0C1C2C3"_block);
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000308}
309
310BOOST_AUTO_TEST_CASE(NameMissing)
311{
312 BOOST_CHECK_THROW(i.wireDecode("0500"_block), tlv::Error);
313 BOOST_CHECK_THROW(i.wireDecode("0502 1200"_block), tlv::Error);
314}
315
316BOOST_AUTO_TEST_CASE(NameEmpty)
317{
318 BOOST_CHECK_THROW(i.wireDecode("0502 0700"_block), tlv::Error);
319}
320
321BOOST_AUTO_TEST_CASE(BadCanBePrefix)
322{
323 BOOST_CHECK_THROW(i.wireDecode("0508 0703080149 210102"_block), tlv::Error);
324}
325
326BOOST_AUTO_TEST_CASE(BadMustBeFresh)
327{
328 BOOST_CHECK_THROW(i.wireDecode("0508 0703080149 120102"_block), tlv::Error);
329}
330
331BOOST_AUTO_TEST_CASE(BadNonce)
332{
333 BOOST_CHECK_THROW(i.wireDecode("0507 0703080149 0A00"_block), tlv::Error);
334 BOOST_CHECK_THROW(i.wireDecode("050A 0703080149 0A0304C263"_block), tlv::Error);
335 BOOST_CHECK_THROW(i.wireDecode("050C 0703080149 0A05EFA420B262"_block), tlv::Error);
336}
337
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500338BOOST_AUTO_TEST_CASE(BadHopLimit)
339{
340 BOOST_CHECK_THROW(i.wireDecode("0507 0703080149 2200"_block), tlv::Error);
341 BOOST_CHECK_THROW(i.wireDecode("0509 0703080149 22021356"_block), tlv::Error);
342}
343
Junxiao Shi8b753a22018-10-24 01:51:40 +0000344BOOST_AUTO_TEST_CASE(UnrecognizedNonCriticalElementBeforeName)
345{
346 BOOST_CHECK_THROW(i.wireDecode("0507 FC00 0703080149"_block), tlv::Error);
347}
348
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000349BOOST_AUTO_TEST_CASE(UnrecognizedCriticalElement)
350{
351 BOOST_CHECK_THROW(i.wireDecode("0507 0703080149 FB00"_block), tlv::Error);
352}
353
354BOOST_AUTO_TEST_SUITE_END() // Decode03
355
Junxiao Shi899277a2017-07-07 22:12:12 +0000356// ---- matching ----
357
358BOOST_AUTO_TEST_CASE(MatchesData)
359{
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000360 auto interest = makeInterest("/A");
Junxiao Shi899277a2017-07-07 22:12:12 +0000361
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000362 auto data = makeData("/A");
363 BOOST_CHECK_EQUAL(interest->matchesData(*data), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000364
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000365 data->setName("/A/D");
366 BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // violates CanBePrefix
Junxiao Shi899277a2017-07-07 22:12:12 +0000367
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000368 interest->setCanBePrefix(true);
369 BOOST_CHECK_EQUAL(interest->matchesData(*data), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000370
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000371 interest->setMustBeFresh(true);
372 BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // violates MustBeFresh
Junxiao Shi899277a2017-07-07 22:12:12 +0000373
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000374 data->setFreshnessPeriod(1_s);
375 BOOST_CHECK_EQUAL(interest->matchesData(*data), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000376
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000377 data->setName("/H/I");
378 BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // Name does not match
Junxiao Shi899277a2017-07-07 22:12:12 +0000379
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000380 data->wireEncode();
381 interest = makeInterest(data->getFullName());
382 BOOST_CHECK_EQUAL(interest->matchesData(*data), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000383
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000384 setNameComponent(*interest, -1, Name("/sha256digest=000000000000000000000000"
385 "0000000000000000000000000000000000000000").at(0));
386 BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // violates implicit digest
Junxiao Shi899277a2017-07-07 22:12:12 +0000387}
388
389BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(MatchesInterest, 1)
390BOOST_AUTO_TEST_CASE(MatchesInterest)
391{
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000392 Interest interest("/A");
393 interest.setCanBePrefix(true)
394 .setMustBeFresh(true)
395 .setForwardingHint({{1, "/H"}})
396 .setNonce(2228)
397 .setInterestLifetime(5_s);
Junxiao Shi899277a2017-07-07 22:12:12 +0000398
399 Interest other;
400 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false);
401
402 other.setName(interest.getName());
403 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false);
404
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000405 other.setCanBePrefix(interest.getCanBePrefix());
406 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false);
407
408 other.setMustBeFresh(interest.getMustBeFresh());
Junxiao Shi899277a2017-07-07 22:12:12 +0000409 BOOST_CHECK_EQUAL(interest.matchesInterest(other), false); // will match until #3162 implemented
410
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000411 other.setForwardingHint(interest.getForwardingHint());
Junxiao Shi899277a2017-07-07 22:12:12 +0000412 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
413
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000414 other.setNonce(9336);
Junxiao Shi899277a2017-07-07 22:12:12 +0000415 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
416
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000417 other.setInterestLifetime(3_s);
Junxiao Shi899277a2017-07-07 22:12:12 +0000418 BOOST_CHECK_EQUAL(interest.matchesInterest(other), true);
Junxiao Shi899277a2017-07-07 22:12:12 +0000419}
420
421// ---- field accessors ----
422
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000423BOOST_AUTO_TEST_CASE(CanBePrefix)
424{
425 Interest i;
426 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
427 i.setCanBePrefix(false);
428 BOOST_CHECK_EQUAL(i.getCanBePrefix(), false);
429 BOOST_CHECK_EQUAL(i.getSelectors().getMaxSuffixComponents(), 1);
430 i.setCanBePrefix(true);
431 BOOST_CHECK_EQUAL(i.getCanBePrefix(), true);
432 BOOST_CHECK_EQUAL(i.getSelectors().getMaxSuffixComponents(), -1);
433}
434
435BOOST_AUTO_TEST_CASE(MustBeFresh)
436{
437 Interest i;
438 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
439 i.setMustBeFresh(true);
440 BOOST_CHECK_EQUAL(i.getMustBeFresh(), true);
441 BOOST_CHECK_EQUAL(i.getSelectors().getMustBeFresh(), true);
442 i.setMustBeFresh(false);
443 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
444 BOOST_CHECK_EQUAL(i.getSelectors().getMustBeFresh(), false);
445}
446
447BOOST_AUTO_TEST_CASE(ModifyForwardingHint)
448{
449 Interest i;
Junxiao Shib55e5d32018-07-18 13:32:00 -0600450 i.setCanBePrefix(false);
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000451 i.setForwardingHint({{1, "/A"}});
452 i.wireEncode();
453 BOOST_CHECK(i.hasWire());
454
455 i.modifyForwardingHint([] (DelegationList& fh) { fh.insert(2, "/B"); });
456 BOOST_CHECK(!i.hasWire());
457 BOOST_CHECK_EQUAL(i.getForwardingHint(), DelegationList({{1, "/A"}, {2, "/B"}}));
458}
459
Junxiao Shi899277a2017-07-07 22:12:12 +0000460BOOST_AUTO_TEST_CASE(GetNonce)
461{
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000462 unique_ptr<Interest> i1, i2;
Junxiao Shi899277a2017-07-07 22:12:12 +0000463
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000464 // getNonce automatically assigns a random Nonce.
465 // It's possible to assign the same Nonce to two Interest, but it's unlikely to get 100 pairs of
466 // same Nonces in a row.
Junxiao Shi899277a2017-07-07 22:12:12 +0000467 int nIterations = 0;
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000468 uint32_t nonce1 = 0, nonce2 = 0;
Junxiao Shi899277a2017-07-07 22:12:12 +0000469 do {
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000470 i1 = make_unique<Interest>();
471 nonce1 = i1->getNonce();
472 i2 = make_unique<Interest>();
473 nonce2 = i2->getNonce();
Junxiao Shi899277a2017-07-07 22:12:12 +0000474 }
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000475 while (nonce1 == nonce2 && ++nIterations < 100);
476 BOOST_CHECK_NE(nonce1, nonce2);
477 BOOST_CHECK(i1->hasNonce());
478 BOOST_CHECK(i2->hasNonce());
Junxiao Shi899277a2017-07-07 22:12:12 +0000479
480 // Once a Nonce is assigned, it should not change.
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000481 BOOST_CHECK_EQUAL(i1->getNonce(), nonce1);
482}
483
484BOOST_AUTO_TEST_CASE(SetNonce)
485{
486 Interest i1("/A");
Junxiao Shib55e5d32018-07-18 13:32:00 -0600487 i1.setCanBePrefix(false);
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000488 i1.setNonce(1);
489 i1.wireEncode();
490 BOOST_CHECK_EQUAL(i1.getNonce(), 1);
491
492 Interest i2(i1);
493 BOOST_CHECK_EQUAL(i2.getNonce(), 1);
494
495 i2.setNonce(2);
496 BOOST_CHECK_EQUAL(i2.getNonce(), 2);
497 BOOST_CHECK_EQUAL(i1.getNonce(), 1); // should not affect i1 Nonce (Bug #4168)
Junxiao Shi899277a2017-07-07 22:12:12 +0000498}
499
500BOOST_AUTO_TEST_CASE(RefreshNonce)
501{
502 Interest i;
503 BOOST_CHECK(!i.hasNonce());
504 i.refreshNonce();
505 BOOST_CHECK(!i.hasNonce());
506
507 i.setNonce(1);
508 BOOST_CHECK(i.hasNonce());
509 i.refreshNonce();
510 BOOST_CHECK(i.hasNonce());
511 BOOST_CHECK_NE(i.getNonce(), 1);
512}
513
514BOOST_AUTO_TEST_CASE(SetInterestLifetime)
515{
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500516 BOOST_CHECK_THROW(Interest("/A", -1_ms), std::invalid_argument);
Davide Pesavento0f830802018-01-16 23:58:58 -0500517 BOOST_CHECK_NO_THROW(Interest("/A", 0_ms));
Junxiao Shi899277a2017-07-07 22:12:12 +0000518
519 Interest i("/local/ndn/prefix");
520 i.setNonce(1);
521 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500522 BOOST_CHECK_THROW(i.setInterestLifetime(-1_ms), std::invalid_argument);
Junxiao Shi899277a2017-07-07 22:12:12 +0000523 BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
Davide Pesavento0f830802018-01-16 23:58:58 -0500524 i.setInterestLifetime(0_ms);
525 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 0_ms);
526 i.setInterestLifetime(1_ms);
527 BOOST_CHECK_EQUAL(i.getInterestLifetime(), 1_ms);
Junxiao Shi899277a2017-07-07 22:12:12 +0000528}
529
Davide Pesavento9c19a392019-04-06 15:07:54 -0400530BOOST_AUTO_TEST_CASE(SetApplicationParameters)
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700531{
532 const uint8_t PARAMETERS1[] = {0xc1};
533 const uint8_t PARAMETERS2[] = {0xc2};
534
535 Interest i;
Davide Pesavento9c19a392019-04-06 15:07:54 -0400536 BOOST_CHECK(!i.hasApplicationParameters());
537 i.setApplicationParameters("2400"_block);
538 BOOST_CHECK(i.hasApplicationParameters());
539 i.unsetApplicationParameters();
540 BOOST_CHECK(!i.hasApplicationParameters());
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700541
Davide Pesavento38912442019-04-06 22:03:39 -0400542 // Block overload
543 i.setApplicationParameters(Block{});
544 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2400"_block);
545 i.setApplicationParameters("2401C0"_block);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400546 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2401C0"_block);
Davide Pesavento38912442019-04-06 22:03:39 -0400547 i.setApplicationParameters("8001C1"_block);
Davide Pesavento9c19a392019-04-06 15:07:54 -0400548 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "24038001C1"_block);
Davide Pesavento38912442019-04-06 22:03:39 -0400549
550 // raw buffer+size overload
551 i.setApplicationParameters(PARAMETERS1, sizeof(PARAMETERS1));
552 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2401C1"_block);
553 i.setApplicationParameters(nullptr, 0);
554 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2400"_block);
555 BOOST_CHECK_THROW(i.setApplicationParameters(nullptr, 42), std::invalid_argument);
556
557 // ConstBufferPtr overload
558 i.setApplicationParameters(make_shared<Buffer>(PARAMETERS2, sizeof(PARAMETERS2)));
559 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2401C2"_block);
560 i.setApplicationParameters(make_shared<Buffer>());
561 BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2400"_block);
562 BOOST_CHECK_THROW(i.setApplicationParameters(nullptr), std::invalid_argument);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700563}
564
Junxiao Shi899277a2017-07-07 22:12:12 +0000565// ---- operators ----
566
567BOOST_AUTO_TEST_CASE(Equality)
568{
569 Interest a;
570 Interest b;
571
572 // if nonce is not set, it would be set to a random value
573 a.setNonce(1);
574 b.setNonce(1);
575
576 BOOST_CHECK_EQUAL(a == b, true);
577 BOOST_CHECK_EQUAL(a != b, false);
578
579 // compare Name
580 a.setName("/A");
581 BOOST_CHECK_EQUAL(a == b, false);
582 BOOST_CHECK_EQUAL(a != b, true);
583
584 b.setName("/B");
585 BOOST_CHECK_EQUAL(a == b, false);
586 BOOST_CHECK_EQUAL(a != b, true);
587
588 b.setName("/A");
589 BOOST_CHECK_EQUAL(a == b, true);
590 BOOST_CHECK_EQUAL(a != b, false);
591
592 // compare Selectors
593 a.setChildSelector(1);
594 BOOST_CHECK_EQUAL(a == b, false);
595 BOOST_CHECK_EQUAL(a != b, true);
596
597 b.setChildSelector(1);
598 BOOST_CHECK_EQUAL(a == b, true);
599 BOOST_CHECK_EQUAL(a != b, false);
600
601 // compare Nonce
602 a.setNonce(100);
603 BOOST_CHECK_EQUAL(a == b, false);
604 BOOST_CHECK_EQUAL(a != b, true);
605
606 b.setNonce(100);
607 BOOST_CHECK_EQUAL(a == b, true);
608 BOOST_CHECK_EQUAL(a != b, false);
609
610 // compare InterestLifetime
Davide Pesavento0f830802018-01-16 23:58:58 -0500611 a.setInterestLifetime(10_s);
Junxiao Shi899277a2017-07-07 22:12:12 +0000612 BOOST_CHECK_EQUAL(a == b, false);
613 BOOST_CHECK_EQUAL(a != b, true);
614
Davide Pesavento0f830802018-01-16 23:58:58 -0500615 b.setInterestLifetime(10_s);
Junxiao Shi899277a2017-07-07 22:12:12 +0000616 BOOST_CHECK_EQUAL(a == b, true);
617 BOOST_CHECK_EQUAL(a != b, false);
618
Junxiao Shid701e5b2017-07-26 01:30:41 +0000619 // compare ForwardingHint
620 a.setForwardingHint({{1, "/H"}});
Junxiao Shi899277a2017-07-07 22:12:12 +0000621 BOOST_CHECK_EQUAL(a == b, false);
622 BOOST_CHECK_EQUAL(a != b, true);
623
Junxiao Shid701e5b2017-07-26 01:30:41 +0000624 b.setForwardingHint({{1, "/H"}});
Junxiao Shi899277a2017-07-07 22:12:12 +0000625 BOOST_CHECK_EQUAL(a == b, true);
626 BOOST_CHECK_EQUAL(a != b, false);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700627
Davide Pesavento9c19a392019-04-06 15:07:54 -0400628 // compare ApplicationParameters
629 a.setApplicationParameters("2404C0C1C2C3"_block);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700630 BOOST_CHECK_EQUAL(a == b, false);
631 BOOST_CHECK_EQUAL(a != b, true);
632
Davide Pesavento9c19a392019-04-06 15:07:54 -0400633 b.setApplicationParameters("2404C0C1C2C3"_block);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700634 BOOST_CHECK_EQUAL(a == b, true);
635 BOOST_CHECK_EQUAL(a != b, false);
Junxiao Shi899277a2017-07-07 22:12:12 +0000636}
637
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100638BOOST_AUTO_TEST_SUITE_END() // TestInterest
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800639
Alexander Afanasyev90164962014-03-06 08:29:59 +0000640} // namespace tests
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800641} // namespace ndn