blob: be466b097c285b29a786245c9c1a05a0c2398321 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -08002/**
Spyridon Mastorakis429634f2015-02-19 17:35:33 -08003 * Copyright (c) 2013-2015 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
Alexander Afanasyev09c613f2014-01-29 00:23:58 -080022#include "interest.hpp"
Junxiao Shi2af905b2014-11-27 13:10:54 -070023#include "interest-filter.hpp"
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070024#include "data.hpp"
25#include "security/signature-sha256-with-rsa.hpp"
Yingdi Yubf6a2812014-06-17 15:32:11 -070026#include "security/digest-sha256.hpp"
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070027#include "encoding/buffer-stream.hpp"
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070028
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070029#include "boost-test.hpp"
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080030
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080031namespace ndn {
Alexander Afanasyev90164962014-03-06 08:29:59 +000032namespace tests {
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080033
34BOOST_AUTO_TEST_SUITE(TestInterest)
35
36const uint8_t Interest1[] = {
Alexander Afanasyeve881e932014-06-08 14:47:03 +030037 0x05, 0x5c, // NDN Interest
Alexander Afanasyev4b456282014-02-13 00:34:34 -080038 0x07, 0x14, // Name
39 0x08, 0x5, // NameComponent
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080040 0x6c, 0x6f, 0x63, 0x61, 0x6c,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080041 0x08, 0x3, // NameComponent
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080042 0x6e, 0x64, 0x6e,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080043 0x08, 0x6, // NameComponent
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080044 0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
Junxiao Shib332e782014-03-31 14:23:46 -070045 0x09, 0x37, // Selectors
Alexander Afanasyev4b456282014-02-13 00:34:34 -080046 0x0d, 0x1, 0x1, // MinSuffix
47 0x0e, 0x1, 0x1, // MaxSuffix
Junxiao Shib332e782014-03-31 14:23:46 -070048 0x1c, 0x16, // KeyLocator
49 0x07, 0x14, // Name
50 0x08, 0x04,
51 0x74, 0x65, 0x73, 0x74,
52 0x08, 0x03,
53 0x6b, 0x65, 0x79,
54 0x08, 0x07,
55 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080056 0x10, 0x14, // Exclude
57 0x08, 0x4, // NameComponent
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080058 0x61, 0x6c, 0x65, 0x78,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080059 0x08, 0x4, // NameComponent
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080060 0x78, 0x78, 0x78, 0x78,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080061 0x13, 0x0, // Any
62 0x08, 0x4, // NameComponent
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080063 0x79, 0x79, 0x79, 0x79,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080064 0x11, 0x1, // ChildSelector
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080065 0x1,
Alexander Afanasyeve881e932014-06-08 14:47:03 +030066 0x0a, 0x4, // Nonce
67 0x1, 0x0, 0x0, 0x00,
68 0x0b, 0x1, // Scope
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080069 0x1,
Alexander Afanasyeve881e932014-06-08 14:47:03 +030070 0x0c, // InterestLifetime
71 0x2, 0x3, 0xe8
72};
73
74const uint8_t Interest2[] = {
75 0x05, 0x5c, // NDN Interest
76 0x07, 0x14, // Name
77 0x08, 0x5, // NameComponent
78 0x6c, 0x6f, 0x63, 0x61, 0x6c,
79 0x08, 0x3, // NameComponent
80 0x6e, 0x64, 0x6e,
81 0x08, 0x6, // NameComponent
82 0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
83 0x09, 0x37, // Selectors
84 0x0d, 0x1, 0x1, // MinSuffix
85 0x0e, 0x1, 0x1, // MaxSuffix
86 0x1c, 0x16, // KeyLocator
87 0x07, 0x14, // Name
88 0x08, 0x04,
89 0x74, 0x65, 0x73, 0x74,
90 0x08, 0x03,
91 0x6b, 0x65, 0x79,
92 0x08, 0x07,
93 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
94 0x10, 0x14, // Exclude
95 0x08, 0x4, // NameComponent
96 0x61, 0x6c, 0x65, 0x78,
97 0x08, 0x4, // NameComponent
98 0x78, 0x78, 0x78, 0x78,
99 0x13, 0x0, // Any
100 0x08, 0x4, // NameComponent
101 0x79, 0x79, 0x79, 0x79,
102 0x11, 0x1, // ChildSelector
103 0x1,
104 0x0a, 0x4, // Nonce
105 0x2, 0x0, 0x0, 0x00,
Alexander Afanasyev4b456282014-02-13 00:34:34 -0800106 0x0b, 0x1, // Scope
Alexander Afanasyev636e9f12014-01-07 12:01:03 -0800107 0x1,
Alexander Afanasyev4b456282014-02-13 00:34:34 -0800108 0x0c, // InterestLifetime
Alexander Afanasyev636e9f12014-01-07 12:01:03 -0800109 0x2, 0x3, 0xe8
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -0800110};
111
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800112const uint8_t InterestWithLocalControlHeader[] = {
Alexander Afanasyeve881e932014-06-08 14:47:03 +0300113 0x50, 0x25, 0x51, 0x01, 0x0a,
114 0x05, 0x20, 0x07, 0x14, 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x08, 0x03, 0x6e, 0x64,
115 0x6e, 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, 0x09, 0x02, 0x12, 0x00, 0x0a, 0x04,
116 0x01, 0x00, 0x00, 0x00
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800117};
118
119const uint8_t InterestWithoutLocalControlHeader[] = {
Alexander Afanasyeve881e932014-06-08 14:47:03 +0300120 0x05, 0x20, 0x07, 0x14, 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x08, 0x03, 0x6e, 0x64,
121 0x6e, 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, 0x09, 0x02, 0x12, 0x00, 0x0a, 0x04,
122 0x01, 0x00, 0x00, 0x00
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800123};
124
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700125BOOST_AUTO_TEST_CASE(InterestEqualityChecks)
126{
127 // Interest ::= INTEREST-TYPE TLV-LENGTH
128 // Name
129 // Selectors?
130 // Nonce
131 // Scope?
132 // InterestLifetime?
133
134 Interest a;
135 Interest b;
136
137 // if nonce is not set, it will be set to a random value
138 a.setNonce(1);
139 b.setNonce(1);
140
141 BOOST_CHECK_EQUAL(a == b, true);
142 BOOST_CHECK_EQUAL(a != b, false);
143
144 // check comparison on Name
145 a.setName("ndn:/A");
146 BOOST_CHECK_EQUAL(a == b, false);
147 BOOST_CHECK_EQUAL(a != b, true);
148
149 b.setName("ndn:/B");
150 BOOST_CHECK_EQUAL(a == b, false);
151 BOOST_CHECK_EQUAL(a != b, true);
152
153 b.setName("ndn:/A");
154 BOOST_CHECK_EQUAL(a == b, true);
155 BOOST_CHECK_EQUAL(a != b, false);
156
157 // check comparison on Selectors
158 a.setChildSelector(1);
159 BOOST_CHECK_EQUAL(a == b, false);
160 BOOST_CHECK_EQUAL(a != b, true);
161
162 b.setChildSelector(1);
163 BOOST_CHECK_EQUAL(a == b, true);
164 BOOST_CHECK_EQUAL(a != b, false);
165
166 // check comparison on Nonce
167 a.setNonce(100);
168 BOOST_CHECK_EQUAL(a == b, false);
169 BOOST_CHECK_EQUAL(a != b, true);
170
171 b.setNonce(100);
172 BOOST_CHECK_EQUAL(a == b, true);
173 BOOST_CHECK_EQUAL(a != b, false);
174
175 // check comparison on Nonce
176 a.setScope(1);
177 BOOST_CHECK_EQUAL(a == b, false);
178 BOOST_CHECK_EQUAL(a != b, true);
179
180 b.setScope(1);
181 BOOST_CHECK_EQUAL(a == b, true);
182 BOOST_CHECK_EQUAL(a != b, false);
183
184 // check comparison on InterestLifetime
185 a.setInterestLifetime(time::seconds(10));
186 BOOST_CHECK_EQUAL(a == b, false);
187 BOOST_CHECK_EQUAL(a != b, true);
188
189 b.setInterestLifetime(time::seconds(10));
190 BOOST_CHECK_EQUAL(a == b, true);
191 BOOST_CHECK_EQUAL(a != b, false);
192}
193
194BOOST_AUTO_TEST_CASE(SelectorsEqualityChecks)
195{
196 // Selectors ::= SELECTORS-TYPE TLV-LENGTH
197 // MinSuffixComponents?
198 // MaxSuffixComponents?
199 // PublisherPublicKeyLocator?
200 // Exclude?
201 // ChildSelector?
202 // MustBeFresh?
203
204 Selectors a;
205 Selectors b;
206 BOOST_CHECK_EQUAL(a == b, true);
207 BOOST_CHECK_EQUAL(a != b, false);
208
209 // MinSuffixComponents
210 a.setMinSuffixComponents(1);
211 BOOST_CHECK_EQUAL(a == b, false);
212 BOOST_CHECK_EQUAL(a != b, true);
213
214 b.setMinSuffixComponents(2);
215 BOOST_CHECK_EQUAL(a == b, false);
216 BOOST_CHECK_EQUAL(a != b, true);
217
218 b.setMinSuffixComponents(1);
219 BOOST_CHECK_EQUAL(a == b, true);
220 BOOST_CHECK_EQUAL(a != b, false);
221
222 // MaxSuffixComponents
223 a.setMaxSuffixComponents(10);
224 BOOST_CHECK_EQUAL(a == b, false);
225 BOOST_CHECK_EQUAL(a != b, true);
226
227 b.setMaxSuffixComponents(10);
228 BOOST_CHECK_EQUAL(a == b, true);
229 BOOST_CHECK_EQUAL(a != b, false);
230
231 // PublisherPublicKeyLocator
232 a.setPublisherPublicKeyLocator(KeyLocator("/key/Locator/name"));
233 BOOST_CHECK_EQUAL(a == b, false);
234 BOOST_CHECK_EQUAL(a != b, true);
235
236 b.setPublisherPublicKeyLocator(KeyLocator("/key/Locator/name"));
237 BOOST_CHECK_EQUAL(a == b, true);
238 BOOST_CHECK_EQUAL(a != b, false);
239
240 // Exclude
241 a.setExclude(Exclude().excludeOne(name::Component("exclude")));
242 BOOST_CHECK_EQUAL(a == b, false);
243 BOOST_CHECK_EQUAL(a != b, true);
244
245 b.setExclude(Exclude().excludeOne(name::Component("exclude")));
246 BOOST_CHECK_EQUAL(a == b, true);
247 BOOST_CHECK_EQUAL(a != b, false);
248
249 // ChildSelector
250 a.setChildSelector(1);
251 BOOST_CHECK_EQUAL(a == b, false);
252 BOOST_CHECK_EQUAL(a != b, true);
253
254 b.setChildSelector(1);
255 BOOST_CHECK_EQUAL(a == b, true);
256 BOOST_CHECK_EQUAL(a != b, false);
257
258 // MustBeFresh
259 a.setMustBeFresh(true);
260 BOOST_CHECK_EQUAL(a == b, false);
261 BOOST_CHECK_EQUAL(a != b, true);
262
263 b.setMustBeFresh(true);
264 BOOST_CHECK_EQUAL(a == b, true);
265 BOOST_CHECK_EQUAL(a != b, false);
266}
267
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700268BOOST_AUTO_TEST_CASE(Decode)
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -0800269{
270 Block interestBlock(Interest1, sizeof(Interest1));
Alexander Afanasyev636e9f12014-01-07 12:01:03 -0800271
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -0800272 ndn::Interest i;
Alexander Afanasyev049f8f72013-12-26 19:07:15 -0800273 BOOST_REQUIRE_NO_THROW(i.wireDecode(interestBlock));
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -0800274
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700275 BOOST_CHECK_EQUAL(i.getName().toUri(), "/local/ndn/prefix");
276 BOOST_CHECK_EQUAL(i.getScope(), 1);
277 BOOST_CHECK_EQUAL(i.getInterestLifetime(), time::milliseconds(1000));
278 BOOST_CHECK_EQUAL(i.getMinSuffixComponents(), 1);
279 BOOST_CHECK_EQUAL(i.getMaxSuffixComponents(), 1);
280 BOOST_CHECK_EQUAL(i.getPublisherPublicKeyLocator().getType(),
281 static_cast<uint32_t>(KeyLocator::KeyLocator_Name));
282 BOOST_CHECK_EQUAL(i.getPublisherPublicKeyLocator().getName(), "ndn:/test/key/locator");
283 BOOST_CHECK_EQUAL(i.getChildSelector(), 1);
284 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
285 BOOST_CHECK_EQUAL(i.getExclude().toUri(), "alex,xxxx,*,yyyy");
286 BOOST_CHECK_EQUAL(i.getNonce(), 1U);
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -0800287}
288
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700289BOOST_AUTO_TEST_CASE(DecodeFromStream)
Alexander Afanasyev6835ad82014-02-12 10:07:20 -0800290{
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -0700291 boost::iostreams::stream<boost::iostreams::array_source> is(
292 reinterpret_cast<const char *>(Interest1), sizeof(Interest1));
Junxiao Shib332e782014-03-31 14:23:46 -0700293
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700294 Block interestBlock = Block::fromStream(is);
Alexander Afanasyev6835ad82014-02-12 10:07:20 -0800295
296 ndn::Interest i;
297 BOOST_REQUIRE_NO_THROW(i.wireDecode(interestBlock));
298
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700299 BOOST_CHECK_EQUAL(i.getName().toUri(), "/local/ndn/prefix");
300 BOOST_CHECK_EQUAL(i.getScope(), 1);
301 BOOST_CHECK_EQUAL(i.getInterestLifetime(), time::milliseconds(1000));
302 BOOST_CHECK_EQUAL(i.getMinSuffixComponents(), 1);
303 BOOST_CHECK_EQUAL(i.getMaxSuffixComponents(), 1);
304 BOOST_CHECK_EQUAL(i.getChildSelector(), 1);
305 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
306 BOOST_CHECK_EQUAL(i.getExclude().toUri(), "alex,xxxx,*,yyyy");
307 BOOST_CHECK_EQUAL(i.getNonce(), 1U);
Alexander Afanasyev6835ad82014-02-12 10:07:20 -0800308}
309
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700310BOOST_AUTO_TEST_CASE(Encode)
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -0800311{
312 ndn::Interest i(ndn::Name("/local/ndn/prefix"));
313 i.setScope(1);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700314 i.setInterestLifetime(time::milliseconds(1000));
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -0800315 i.setMinSuffixComponents(1);
316 i.setMaxSuffixComponents(1);
Junxiao Shib332e782014-03-31 14:23:46 -0700317 i.setPublisherPublicKeyLocator(KeyLocator("ndn:/test/key/locator"));
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -0800318 i.setChildSelector(1);
319 i.setMustBeFresh(false);
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800320 Exclude exclude;
321 exclude
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800322 .excludeOne(name::Component("alex"))
323 .excludeRange(name::Component("xxxx"), name::Component("yyyy"));
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800324 i.setExclude(exclude);
Alexander Afanasyev840139f2013-12-28 15:02:50 -0800325 i.setNonce(1);
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -0800326
Alexander Afanasyeve881e932014-06-08 14:47:03 +0300327 BOOST_CHECK_EQUAL(i.hasWire(), false);
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -0800328 const Block &wire = i.wireEncode();
Alexander Afanasyeve881e932014-06-08 14:47:03 +0300329 BOOST_CHECK_EQUAL(i.hasWire(), true);
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800330
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700331 BOOST_CHECK_EQUAL_COLLECTIONS(Interest1, Interest1 + sizeof(Interest1),
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800332 wire.begin(), wire.end());
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800333
Alexander Afanasyeve881e932014-06-08 14:47:03 +0300334 const uint8_t* originalWire = wire.wire();
335 i.setNonce(2);
336 BOOST_CHECK_EQUAL(i.hasWire(), true);
337 BOOST_CHECK_EQUAL(originalWire, i.wireEncode().wire());
338 BOOST_CHECK_EQUAL(i.hasWire(), true);
339
340 BOOST_CHECK_EQUAL_COLLECTIONS(Interest2, Interest2 + sizeof(Interest2),
341 wire.begin(), wire.end());
Alexander Afanasyeva0c5f832014-06-19 13:27:56 -0700342
343 std::ostringstream strStream;
344 BOOST_CHECK_NO_THROW(strStream << i);
345
346 BOOST_CHECK_EQUAL(strStream.str(),
347 "/local/ndn/prefix?"
348 "ndn.MinSuffixComponents=1&ndn.MaxSuffixComponents=1&"
349 "ndn.ChildSelector=1&ndn.Scope=1&"
350 "ndn.InterestLifetime=1000&"
351 "ndn.Nonce=2&ndn.Exclude=alex,xxxx,*,yyyy");
Alexander Afanasyevc3932172014-07-10 18:53:56 -0700352
353 i.refreshNonce();
354 BOOST_CHECK_EQUAL(i.hasWire(), true);
355 BOOST_CHECK_EQUAL(originalWire, i.wireEncode().wire());
356 BOOST_CHECK_NE(i.getNonce(), 2);
Alexander Afanasyeve881e932014-06-08 14:47:03 +0300357}
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800358
359BOOST_AUTO_TEST_CASE(EncodeWithLocalHeader)
360{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800361 ndn::Interest interest(ndn::Name("/local/ndn/prefix"));
362 interest.setMustBeFresh(true);
363 interest.setIncomingFaceId(10);
364 interest.setNonce(1);
Junxiao Shib332e782014-03-31 14:23:46 -0700365
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800366 BOOST_CHECK(!interest.hasWire());
Junxiao Shib332e782014-03-31 14:23:46 -0700367
Jiewen Tanc759a202015-01-29 23:31:09 -0800368 Block headerBlock =
369 interest.getLocalControlHeader()
370 .wireEncode(interest, nfd::LocalControlHeader::ENCODE_INCOMING_FACE_ID |
371 nfd::LocalControlHeader::ENCODE_NEXT_HOP);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800372
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800373 BOOST_CHECK(interest.hasWire());
374 BOOST_CHECK(headerBlock.hasWire());
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800375
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800376 BOOST_CHECK_NE(headerBlock.wire(), interest.wireEncode().wire());
377 BOOST_CHECK_NE(headerBlock.size(), interest.wireEncode().size());
378 BOOST_CHECK_EQUAL(headerBlock.size(), 5);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800379
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800380 BOOST_CHECK_EQUAL_COLLECTIONS(InterestWithLocalControlHeader,
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700381 InterestWithLocalControlHeader + 5,
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800382 headerBlock.begin(), headerBlock.end());
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800383
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800384 interest.setNonce(1000);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800385
Jiewen Tanc759a202015-01-29 23:31:09 -0800386 Block updatedHeaderBlock =
387 interest.getLocalControlHeader()
388 .wireEncode(interest, nfd::LocalControlHeader::ENCODE_INCOMING_FACE_ID |
389 nfd::LocalControlHeader::ENCODE_NEXT_HOP);
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800390 BOOST_CHECK_EQUAL(updatedHeaderBlock.size(), 5);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800391
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800392 // only length should have changed
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700393 BOOST_CHECK_EQUAL_COLLECTIONS(updatedHeaderBlock.begin() + 2, updatedHeaderBlock.end(),
394 headerBlock.begin() + 2, headerBlock.end());
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800395
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800396 // updating IncomingFaceId that keeps the length
397 interest.setIncomingFaceId(100);
Jiewen Tanc759a202015-01-29 23:31:09 -0800398 updatedHeaderBlock =
399 interest.getLocalControlHeader()
400 .wireEncode(interest, nfd::LocalControlHeader::ENCODE_INCOMING_FACE_ID |
401 nfd::LocalControlHeader::ENCODE_NEXT_HOP);
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800402 BOOST_CHECK_EQUAL(updatedHeaderBlock.size(), 5);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700403 BOOST_CHECK_NE(*(updatedHeaderBlock.begin() + 4), *(headerBlock.begin() + 4));
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800404
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800405 // updating IncomingFaceId that increases the length by 2
406 interest.setIncomingFaceId(1000);
Jiewen Tanc759a202015-01-29 23:31:09 -0800407 updatedHeaderBlock =
408 interest.getLocalControlHeader()
409 .wireEncode(interest, nfd::LocalControlHeader::ENCODE_INCOMING_FACE_ID |
410 nfd::LocalControlHeader::ENCODE_NEXT_HOP);
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800411 BOOST_CHECK_EQUAL(updatedHeaderBlock.size(), 6);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800412
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800413 // adding NextHopId
414 interest.setNextHopFaceId(1);
Jiewen Tanc759a202015-01-29 23:31:09 -0800415 updatedHeaderBlock =
416 interest.getLocalControlHeader()
417 .wireEncode(interest, nfd::LocalControlHeader::ENCODE_INCOMING_FACE_ID |
418 nfd::LocalControlHeader::ENCODE_NEXT_HOP);
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800419 BOOST_CHECK_EQUAL(updatedHeaderBlock.size(), 9);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800420
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800421 // masking IncomingFaceId
Jiewen Tanc759a202015-01-29 23:31:09 -0800422 updatedHeaderBlock = interest.getLocalControlHeader()
423 .wireEncode(interest, nfd::LocalControlHeader::ENCODE_NEXT_HOP);
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800424 BOOST_CHECK_EQUAL(updatedHeaderBlock.size(), 5);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800425
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800426 // masking NextHopId
Jiewen Tanc759a202015-01-29 23:31:09 -0800427 updatedHeaderBlock =
428 interest.getLocalControlHeader()
429 .wireEncode(interest, nfd::LocalControlHeader::ENCODE_INCOMING_FACE_ID);
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800430 BOOST_CHECK_EQUAL(updatedHeaderBlock.size(), 6);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800431
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800432 // masking everything
Jiewen Tanc759a202015-01-29 23:31:09 -0800433 BOOST_CHECK_THROW(interest.getLocalControlHeader()
434 .wireEncode(interest, nfd::LocalControlHeader::ENCODE_NONE),
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800435 nfd::LocalControlHeader::Error);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800436}
437
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800438
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700439BOOST_AUTO_TEST_CASE(DecodeWithLocalHeader)
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800440{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800441 Block wireBlock(InterestWithLocalControlHeader, sizeof(InterestWithLocalControlHeader));
442 const Block& payload = nfd::LocalControlHeader::getPayload(wireBlock);
443 BOOST_REQUIRE_NE(&payload, &wireBlock);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800444
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600445 BOOST_CHECK_EQUAL(payload.type(), static_cast<uint32_t>(tlv::Interest));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700446 BOOST_CHECK_EQUAL(wireBlock.type(), static_cast<uint32_t>(tlv::nfd::LocalControlHeader));
Junxiao Shib332e782014-03-31 14:23:46 -0700447
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800448 Interest interest(payload);
449 BOOST_CHECK(!interest.getLocalControlHeader().hasIncomingFaceId());
450 BOOST_CHECK(!interest.getLocalControlHeader().hasNextHopFaceId());
451
452 BOOST_REQUIRE_NO_THROW(interest.getLocalControlHeader().wireDecode(wireBlock));
453
Jiewen Tanc759a202015-01-29 23:31:09 -0800454 BOOST_CHECK_EQUAL(
455 interest.getLocalControlHeader()
456 .wireEncode(interest, nfd::LocalControlHeader::ENCODE_INCOMING_FACE_ID |
457 nfd::LocalControlHeader::ENCODE_NEXT_HOP).size(),
458 5);
Junxiao Shib332e782014-03-31 14:23:46 -0700459
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800460 BOOST_CHECK_EQUAL(interest.getIncomingFaceId(), 10);
461 BOOST_CHECK(!interest.getLocalControlHeader().hasNextHopFaceId());
462
Jiewen Tanc759a202015-01-29 23:31:09 -0800463 BOOST_CHECK_THROW(interest.getLocalControlHeader()
464 .wireEncode(interest, nfd::LocalControlHeader::ENCODE_NONE),
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800465 nfd::LocalControlHeader::Error);
466
Jiewen Tanc759a202015-01-29 23:31:09 -0800467 BOOST_CHECK_THROW(interest.getLocalControlHeader()
468 .wireEncode(interest, nfd::LocalControlHeader::ENCODE_NEXT_HOP),
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800469 nfd::LocalControlHeader::Error);
470
Jiewen Tanc759a202015-01-29 23:31:09 -0800471 BOOST_CHECK_NO_THROW(
472 interest.getLocalControlHeader()
473 .wireEncode(interest, nfd::LocalControlHeader::ENCODE_INCOMING_FACE_ID));
474 BOOST_CHECK_NO_THROW(
475 interest.getLocalControlHeader()
476 .wireEncode(interest, nfd::LocalControlHeader::ENCODE_INCOMING_FACE_ID |
477 nfd::LocalControlHeader::ENCODE_NEXT_HOP));
Junxiao Shib332e782014-03-31 14:23:46 -0700478
Jiewen Tanc759a202015-01-29 23:31:09 -0800479 BOOST_CHECK_NE(
480 (void*)interest.getLocalControlHeader()
481 .wireEncode(interest, nfd::LocalControlHeader::ENCODE_INCOMING_FACE_ID |
482 nfd::LocalControlHeader::ENCODE_NEXT_HOP)
483 .wire(),
484 (void*)wireBlock.wire());
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800485
Jiewen Tanc759a202015-01-29 23:31:09 -0800486 BOOST_CHECK_EQUAL(interest.getLocalControlHeader()
487 .wireEncode(interest, nfd::LocalControlHeader::ENCODE_INCOMING_FACE_ID |
488 nfd::LocalControlHeader::ENCODE_NEXT_HOP).size(),
489 5);
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800490}
491
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700492BOOST_AUTO_TEST_CASE(DecodeWithoutLocalHeader)
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800493{
494 Block wireBlock(InterestWithoutLocalControlHeader, sizeof(InterestWithoutLocalControlHeader));
495 const Block& payload = nfd::LocalControlHeader::getPayload(wireBlock);
496 BOOST_CHECK_EQUAL(&payload, &wireBlock);
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -0800497}
498
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700499BOOST_AUTO_TEST_CASE(MatchesData)
500{
501 Interest interest;
502 interest.setName("ndn:/A")
503 .setMinSuffixComponents(2)
504 .setMaxSuffixComponents(2)
505 .setPublisherPublicKeyLocator(KeyLocator("ndn:/B"))
506 .setExclude(Exclude().excludeBefore(name::Component("C")));
507
508 Data data("ndn:/A/D");
Yingdi Yu4a557052014-07-09 16:40:37 -0700509 SignatureSha256WithRsa signature(KeyLocator("ndn:/B"));
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700510 data.setSignature(signature);
Alexander Afanasyev3b703102014-06-13 17:01:14 -0700511 data.wireEncode();
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700512 BOOST_CHECK_EQUAL(interest.matchesData(data), true);
513
514 Data data1 = data;
Alexander Afanasyev3b703102014-06-13 17:01:14 -0700515 data1.setName("ndn:/A"); // violates MinSuffixComponents
516 data1.wireEncode();
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700517 BOOST_CHECK_EQUAL(interest.matchesData(data1), false);
518
Alexander Afanasyev3b703102014-06-13 17:01:14 -0700519 interest.setMinSuffixComponents(1);
520 BOOST_CHECK_EQUAL(interest.matchesData(data1), true);
521 interest.setMinSuffixComponents(2);
522
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700523 Data data2 = data;
Alexander Afanasyev3b703102014-06-13 17:01:14 -0700524 data2.setName("ndn:/A/E/F"); // violates MaxSuffixComponents
525 data2.wireEncode();
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700526 BOOST_CHECK_EQUAL(interest.matchesData(data2), false);
527
Alexander Afanasyev3b703102014-06-13 17:01:14 -0700528 interest.setMaxSuffixComponents(3);
529 BOOST_CHECK_EQUAL(interest.matchesData(data2), true);
530 interest.setMaxSuffixComponents(2);
531
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700532 Data data3 = data;
Yingdi Yu4a557052014-07-09 16:40:37 -0700533 SignatureSha256WithRsa signature3(KeyLocator("ndn:/G")); // violates PublisherPublicKeyLocator
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700534 data3.setSignature(signature3);
Alexander Afanasyev3b703102014-06-13 17:01:14 -0700535 data3.wireEncode();
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700536 BOOST_CHECK_EQUAL(interest.matchesData(data3), false);
537
Alexander Afanasyev3b703102014-06-13 17:01:14 -0700538 interest.setPublisherPublicKeyLocator(KeyLocator("ndn:/G"));
539 BOOST_CHECK_EQUAL(interest.matchesData(data3), true);
540 interest.setPublisherPublicKeyLocator(KeyLocator("ndn:/B"));
541
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700542 Data data4 = data;
Yingdi Yubf6a2812014-06-17 15:32:11 -0700543 DigestSha256 signature4; // violates PublisherPublicKeyLocator
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700544 data4.setSignature(signature4);
Alexander Afanasyev3b703102014-06-13 17:01:14 -0700545 data4.wireEncode();
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700546 BOOST_CHECK_EQUAL(interest.matchesData(data4), false);
547
Alexander Afanasyev3b703102014-06-13 17:01:14 -0700548 interest.setPublisherPublicKeyLocator(KeyLocator());
549 BOOST_CHECK_EQUAL(interest.matchesData(data4), true);
550 interest.setPublisherPublicKeyLocator(KeyLocator("ndn:/B"));
551
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700552 Data data5 = data;
Alexander Afanasyev3b703102014-06-13 17:01:14 -0700553 data5.setName("ndn:/A/C"); // violates Exclude
554 data5.wireEncode();
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700555 BOOST_CHECK_EQUAL(interest.matchesData(data5), false);
556
Alexander Afanasyev3b703102014-06-13 17:01:14 -0700557 interest.setExclude(Exclude().excludeBefore(name::Component("A")));
558 BOOST_CHECK_EQUAL(interest.matchesData(data5), true);
559 interest.setExclude(Exclude().excludeBefore(name::Component("C")));
560
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700561 Data data6 = data;
Alexander Afanasyev3b703102014-06-13 17:01:14 -0700562 data6.setName("ndn:/H/I"); // violates Name
563 data6.wireEncode();
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700564 BOOST_CHECK_EQUAL(interest.matchesData(data6), false);
Alexander Afanasyev3b703102014-06-13 17:01:14 -0700565
566 Data data7 = data;
567 data7.setName("ndn:/A/B");
568 data7.wireEncode();
569
570 interest = Interest()
Alexander Afanasyev56860f52014-11-07 11:51:17 -0800571 .setName("/A/B/sha256digest=D548DECEFC4B880720DC9257A8D815E9DF4465E63742EE55C29133055DAA67C2");
Alexander Afanasyev3b703102014-06-13 17:01:14 -0700572 BOOST_CHECK_EQUAL(interest.matchesData(data7), true);
573
574 interest = Interest()
575 .setName("/A/B/%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00"
576 "%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00");
577 BOOST_CHECK_EQUAL(interest.matchesData(data7), false); // violates implicit digest
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700578}
579
Alexander Afanasyev90164962014-03-06 08:29:59 +0000580BOOST_AUTO_TEST_CASE(InterestFilterMatching)
581{
582 BOOST_CHECK_EQUAL(InterestFilter("/a").doesMatch("/a/b"), true);
583 BOOST_CHECK_EQUAL(InterestFilter("/a/b").doesMatch("/a/b"), true);
584 BOOST_CHECK_EQUAL(InterestFilter("/a/b/c").doesMatch("/a/b"), false);
585
586 BOOST_CHECK_EQUAL(InterestFilter("/a", "<b>").doesMatch("/a/b"), true);
587 BOOST_CHECK_EQUAL(InterestFilter("/a/b", "<b>").doesMatch("/a/b"), false);
588
589 BOOST_CHECK_EQUAL(InterestFilter("/a/b", "<b>").doesMatch("/a/b/c/b"), false);
590 BOOST_CHECK_EQUAL(InterestFilter("/a/b", "<>*<b>").doesMatch("/a/b/c/b"), true);
591
592 BOOST_CHECK_EQUAL(InterestFilter("/a", "<b>").doesMatch("/a/b/c/d"), false);
593 BOOST_CHECK_EQUAL(InterestFilter("/a", "<b><>*").doesMatch("/a/b/c/d"), true);
594 BOOST_CHECK_EQUAL(InterestFilter("/a", "<b><>*").doesMatch("/a/b"), true);
595 BOOST_CHECK_EQUAL(InterestFilter("/a", "<b><>+").doesMatch("/a/b"), false);
596 BOOST_CHECK_EQUAL(InterestFilter("/a", "<b><>+").doesMatch("/a/b/c"), true);
597}
598
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -0800599BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800600
Alexander Afanasyev90164962014-03-06 08:29:59 +0000601} // namespace tests
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800602} // namespace ndn