blob: 71d6d2941d319a324b2e4c49c73dda3a82057da0 [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/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 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
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800368 Block headerBlock = interest.getLocalControlHeader().wireEncode(interest, true, true);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800369
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800370 BOOST_CHECK(interest.hasWire());
371 BOOST_CHECK(headerBlock.hasWire());
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800372
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800373 BOOST_CHECK_NE(headerBlock.wire(), interest.wireEncode().wire());
374 BOOST_CHECK_NE(headerBlock.size(), interest.wireEncode().size());
375 BOOST_CHECK_EQUAL(headerBlock.size(), 5);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800376
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800377 BOOST_CHECK_EQUAL_COLLECTIONS(InterestWithLocalControlHeader,
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700378 InterestWithLocalControlHeader + 5,
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800379 headerBlock.begin(), headerBlock.end());
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800380
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800381 interest.setNonce(1000);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800382
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800383 Block updatedHeaderBlock = interest.getLocalControlHeader().wireEncode(interest, true, true);
384 BOOST_CHECK_EQUAL(updatedHeaderBlock.size(), 5);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800385
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800386 // only length should have changed
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700387 BOOST_CHECK_EQUAL_COLLECTIONS(updatedHeaderBlock.begin() + 2, updatedHeaderBlock.end(),
388 headerBlock.begin() + 2, headerBlock.end());
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800389
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800390 // updating IncomingFaceId that keeps the length
391 interest.setIncomingFaceId(100);
392 updatedHeaderBlock = interest.getLocalControlHeader().wireEncode(interest, true, true);
393 BOOST_CHECK_EQUAL(updatedHeaderBlock.size(), 5);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700394 BOOST_CHECK_NE(*(updatedHeaderBlock.begin() + 4), *(headerBlock.begin() + 4));
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800395
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800396 // updating IncomingFaceId that increases the length by 2
397 interest.setIncomingFaceId(1000);
398 updatedHeaderBlock = interest.getLocalControlHeader().wireEncode(interest, true, true);
399 BOOST_CHECK_EQUAL(updatedHeaderBlock.size(), 6);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800400
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800401 // adding NextHopId
402 interest.setNextHopFaceId(1);
403 updatedHeaderBlock = interest.getLocalControlHeader().wireEncode(interest, true, true);
404 BOOST_CHECK_EQUAL(updatedHeaderBlock.size(), 9);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800405
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800406 // masking IncomingFaceId
407 updatedHeaderBlock = interest.getLocalControlHeader().wireEncode(interest, false, true);
408 BOOST_CHECK_EQUAL(updatedHeaderBlock.size(), 5);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800409
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800410 // masking NextHopId
411 updatedHeaderBlock = interest.getLocalControlHeader().wireEncode(interest, true, false);
412 BOOST_CHECK_EQUAL(updatedHeaderBlock.size(), 6);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800413
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800414 // masking everything
415 BOOST_CHECK_THROW(interest.getLocalControlHeader().wireEncode(interest, false, false),
416 nfd::LocalControlHeader::Error);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800417}
418
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800419
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700420BOOST_AUTO_TEST_CASE(DecodeWithLocalHeader)
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800421{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800422 Block wireBlock(InterestWithLocalControlHeader, sizeof(InterestWithLocalControlHeader));
423 const Block& payload = nfd::LocalControlHeader::getPayload(wireBlock);
424 BOOST_REQUIRE_NE(&payload, &wireBlock);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800425
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600426 BOOST_CHECK_EQUAL(payload.type(), static_cast<uint32_t>(tlv::Interest));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700427 BOOST_CHECK_EQUAL(wireBlock.type(), static_cast<uint32_t>(tlv::nfd::LocalControlHeader));
Junxiao Shib332e782014-03-31 14:23:46 -0700428
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800429 Interest interest(payload);
430 BOOST_CHECK(!interest.getLocalControlHeader().hasIncomingFaceId());
431 BOOST_CHECK(!interest.getLocalControlHeader().hasNextHopFaceId());
432
433 BOOST_REQUIRE_NO_THROW(interest.getLocalControlHeader().wireDecode(wireBlock));
434
435 BOOST_CHECK_EQUAL(interest.getLocalControlHeader().wireEncode(interest, true, true).size(), 5);
Junxiao Shib332e782014-03-31 14:23:46 -0700436
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800437 BOOST_CHECK_EQUAL(interest.getIncomingFaceId(), 10);
438 BOOST_CHECK(!interest.getLocalControlHeader().hasNextHopFaceId());
439
440 BOOST_CHECK_THROW(interest.getLocalControlHeader().wireEncode(interest, false, false),
441 nfd::LocalControlHeader::Error);
442
443 BOOST_CHECK_THROW(interest.getLocalControlHeader().wireEncode(interest, false, true),
444 nfd::LocalControlHeader::Error);
445
446 BOOST_CHECK_NO_THROW(interest.getLocalControlHeader().wireEncode(interest, true, false));
447 BOOST_CHECK_NO_THROW(interest.getLocalControlHeader().wireEncode(interest, true, true));
Junxiao Shib332e782014-03-31 14:23:46 -0700448
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800449 BOOST_CHECK_NE((void*)interest.getLocalControlHeader().wireEncode(interest, true, true).wire(),
450 (void*)wireBlock.wire());
451
452 BOOST_CHECK_EQUAL(interest.getLocalControlHeader().wireEncode(interest, true, true).size(), 5);
453}
454
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700455BOOST_AUTO_TEST_CASE(DecodeWithoutLocalHeader)
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800456{
457 Block wireBlock(InterestWithoutLocalControlHeader, sizeof(InterestWithoutLocalControlHeader));
458 const Block& payload = nfd::LocalControlHeader::getPayload(wireBlock);
459 BOOST_CHECK_EQUAL(&payload, &wireBlock);
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -0800460}
461
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700462BOOST_AUTO_TEST_CASE(MatchesData)
463{
464 Interest interest;
465 interest.setName("ndn:/A")
466 .setMinSuffixComponents(2)
467 .setMaxSuffixComponents(2)
468 .setPublisherPublicKeyLocator(KeyLocator("ndn:/B"))
469 .setExclude(Exclude().excludeBefore(name::Component("C")));
470
471 Data data("ndn:/A/D");
Yingdi Yu4a557052014-07-09 16:40:37 -0700472 SignatureSha256WithRsa signature(KeyLocator("ndn:/B"));
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700473 data.setSignature(signature);
Alexander Afanasyev3b703102014-06-13 17:01:14 -0700474 data.wireEncode();
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700475 BOOST_CHECK_EQUAL(interest.matchesData(data), true);
476
477 Data data1 = data;
Alexander Afanasyev3b703102014-06-13 17:01:14 -0700478 data1.setName("ndn:/A"); // violates MinSuffixComponents
479 data1.wireEncode();
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700480 BOOST_CHECK_EQUAL(interest.matchesData(data1), false);
481
Alexander Afanasyev3b703102014-06-13 17:01:14 -0700482 interest.setMinSuffixComponents(1);
483 BOOST_CHECK_EQUAL(interest.matchesData(data1), true);
484 interest.setMinSuffixComponents(2);
485
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700486 Data data2 = data;
Alexander Afanasyev3b703102014-06-13 17:01:14 -0700487 data2.setName("ndn:/A/E/F"); // violates MaxSuffixComponents
488 data2.wireEncode();
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700489 BOOST_CHECK_EQUAL(interest.matchesData(data2), false);
490
Alexander Afanasyev3b703102014-06-13 17:01:14 -0700491 interest.setMaxSuffixComponents(3);
492 BOOST_CHECK_EQUAL(interest.matchesData(data2), true);
493 interest.setMaxSuffixComponents(2);
494
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700495 Data data3 = data;
Yingdi Yu4a557052014-07-09 16:40:37 -0700496 SignatureSha256WithRsa signature3(KeyLocator("ndn:/G")); // violates PublisherPublicKeyLocator
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700497 data3.setSignature(signature3);
Alexander Afanasyev3b703102014-06-13 17:01:14 -0700498 data3.wireEncode();
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700499 BOOST_CHECK_EQUAL(interest.matchesData(data3), false);
500
Alexander Afanasyev3b703102014-06-13 17:01:14 -0700501 interest.setPublisherPublicKeyLocator(KeyLocator("ndn:/G"));
502 BOOST_CHECK_EQUAL(interest.matchesData(data3), true);
503 interest.setPublisherPublicKeyLocator(KeyLocator("ndn:/B"));
504
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700505 Data data4 = data;
Yingdi Yubf6a2812014-06-17 15:32:11 -0700506 DigestSha256 signature4; // violates PublisherPublicKeyLocator
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700507 data4.setSignature(signature4);
Alexander Afanasyev3b703102014-06-13 17:01:14 -0700508 data4.wireEncode();
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700509 BOOST_CHECK_EQUAL(interest.matchesData(data4), false);
510
Alexander Afanasyev3b703102014-06-13 17:01:14 -0700511 interest.setPublisherPublicKeyLocator(KeyLocator());
512 BOOST_CHECK_EQUAL(interest.matchesData(data4), true);
513 interest.setPublisherPublicKeyLocator(KeyLocator("ndn:/B"));
514
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700515 Data data5 = data;
Alexander Afanasyev3b703102014-06-13 17:01:14 -0700516 data5.setName("ndn:/A/C"); // violates Exclude
517 data5.wireEncode();
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700518 BOOST_CHECK_EQUAL(interest.matchesData(data5), false);
519
Alexander Afanasyev3b703102014-06-13 17:01:14 -0700520 interest.setExclude(Exclude().excludeBefore(name::Component("A")));
521 BOOST_CHECK_EQUAL(interest.matchesData(data5), true);
522 interest.setExclude(Exclude().excludeBefore(name::Component("C")));
523
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700524 Data data6 = data;
Alexander Afanasyev3b703102014-06-13 17:01:14 -0700525 data6.setName("ndn:/H/I"); // violates Name
526 data6.wireEncode();
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700527 BOOST_CHECK_EQUAL(interest.matchesData(data6), false);
Alexander Afanasyev3b703102014-06-13 17:01:14 -0700528
529 Data data7 = data;
530 data7.setName("ndn:/A/B");
531 data7.wireEncode();
532
533 interest = Interest()
Alexander Afanasyev56860f52014-11-07 11:51:17 -0800534 .setName("/A/B/sha256digest=D548DECEFC4B880720DC9257A8D815E9DF4465E63742EE55C29133055DAA67C2");
Alexander Afanasyev3b703102014-06-13 17:01:14 -0700535 BOOST_CHECK_EQUAL(interest.matchesData(data7), true);
536
537 interest = Interest()
538 .setName("/A/B/%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00"
539 "%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00");
540 BOOST_CHECK_EQUAL(interest.matchesData(data7), false); // violates implicit digest
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700541}
542
Alexander Afanasyev90164962014-03-06 08:29:59 +0000543BOOST_AUTO_TEST_CASE(InterestFilterMatching)
544{
545 BOOST_CHECK_EQUAL(InterestFilter("/a").doesMatch("/a/b"), true);
546 BOOST_CHECK_EQUAL(InterestFilter("/a/b").doesMatch("/a/b"), true);
547 BOOST_CHECK_EQUAL(InterestFilter("/a/b/c").doesMatch("/a/b"), false);
548
549 BOOST_CHECK_EQUAL(InterestFilter("/a", "<b>").doesMatch("/a/b"), true);
550 BOOST_CHECK_EQUAL(InterestFilter("/a/b", "<b>").doesMatch("/a/b"), false);
551
552 BOOST_CHECK_EQUAL(InterestFilter("/a/b", "<b>").doesMatch("/a/b/c/b"), false);
553 BOOST_CHECK_EQUAL(InterestFilter("/a/b", "<>*<b>").doesMatch("/a/b/c/b"), true);
554
555 BOOST_CHECK_EQUAL(InterestFilter("/a", "<b>").doesMatch("/a/b/c/d"), false);
556 BOOST_CHECK_EQUAL(InterestFilter("/a", "<b><>*").doesMatch("/a/b/c/d"), true);
557 BOOST_CHECK_EQUAL(InterestFilter("/a", "<b><>*").doesMatch("/a/b"), true);
558 BOOST_CHECK_EQUAL(InterestFilter("/a", "<b><>+").doesMatch("/a/b"), false);
559 BOOST_CHECK_EQUAL(InterestFilter("/a", "<b><>+").doesMatch("/a/b/c"), true);
560}
561
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -0800562BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800563
Alexander Afanasyev90164962014-03-06 08:29:59 +0000564} // namespace tests
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800565} // namespace ndn