blob: 75a4fb73515801c76cca9de4100cd9d5be3e9b71 [file] [log] [blame]
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -08002/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080011 */
12
Alexander Afanasyev09c613f2014-01-29 00:23:58 -080013#include "interest.hpp"
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070014#include "data.hpp"
15#include "security/signature-sha256-with-rsa.hpp"
16#include "security/signature-sha256.hpp"
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070017#include "encoding/buffer-stream.hpp"
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070018
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070019#include "boost-test.hpp"
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080020
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080021namespace ndn {
Alexander Afanasyev90164962014-03-06 08:29:59 +000022namespace tests {
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080023
24BOOST_AUTO_TEST_SUITE(TestInterest)
25
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070026BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Interest>));
27BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Selectors>));
28
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080029const uint8_t Interest1[] = {
Junxiao Shib332e782014-03-31 14:23:46 -070030 0x05, 0x59, // NDN Interest
Alexander Afanasyev4b456282014-02-13 00:34:34 -080031 0x07, 0x14, // Name
32 0x08, 0x5, // NameComponent
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080033 0x6c, 0x6f, 0x63, 0x61, 0x6c,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080034 0x08, 0x3, // NameComponent
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080035 0x6e, 0x64, 0x6e,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080036 0x08, 0x6, // NameComponent
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080037 0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
Junxiao Shib332e782014-03-31 14:23:46 -070038 0x09, 0x37, // Selectors
Alexander Afanasyev4b456282014-02-13 00:34:34 -080039 0x0d, 0x1, 0x1, // MinSuffix
40 0x0e, 0x1, 0x1, // MaxSuffix
Junxiao Shib332e782014-03-31 14:23:46 -070041 0x1c, 0x16, // KeyLocator
42 0x07, 0x14, // Name
43 0x08, 0x04,
44 0x74, 0x65, 0x73, 0x74,
45 0x08, 0x03,
46 0x6b, 0x65, 0x79,
47 0x08, 0x07,
48 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080049 0x10, 0x14, // Exclude
50 0x08, 0x4, // NameComponent
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080051 0x61, 0x6c, 0x65, 0x78,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080052 0x08, 0x4, // NameComponent
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080053 0x78, 0x78, 0x78, 0x78,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080054 0x13, 0x0, // Any
55 0x08, 0x4, // NameComponent
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080056 0x79, 0x79, 0x79, 0x79,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080057 0x11, 0x1, // ChildSelector
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080058 0x1,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080059 0x0a, 0x1, // Nonce
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080060 0x1,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080061 0x0b, 0x1, // Scope
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080062 0x1,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080063 0x0c, // InterestLifetime
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080064 0x2, 0x3, 0xe8
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080065};
66
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080067const uint8_t InterestWithLocalControlHeader[] = {
68 0x50, 0x22, 0x51, 0x01, 0x0a, 0x05, 0x1d, 0x07, 0x14, 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61,
69 0x6c, 0x08, 0x03, 0x6e, 0x64, 0x6e, 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, 0x09,
70 0x02, 0x12, 0x00, 0x0a, 0x01, 0x01
71};
72
73const uint8_t InterestWithoutLocalControlHeader[] = {
74 0x05, 0x1d, 0x07, 0x14, 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x08, 0x03, 0x6e, 0x64,
75 0x6e, 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, 0x09, 0x02, 0x12, 0x00, 0x0a, 0x01,
76 0x01
77};
78
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070079BOOST_AUTO_TEST_CASE(InterestEqualityChecks)
80{
81 // Interest ::= INTEREST-TYPE TLV-LENGTH
82 // Name
83 // Selectors?
84 // Nonce
85 // Scope?
86 // InterestLifetime?
87
88 Interest a;
89 Interest b;
90
91 // if nonce is not set, it will be set to a random value
92 a.setNonce(1);
93 b.setNonce(1);
94
95 BOOST_CHECK_EQUAL(a == b, true);
96 BOOST_CHECK_EQUAL(a != b, false);
97
98 // check comparison on Name
99 a.setName("ndn:/A");
100 BOOST_CHECK_EQUAL(a == b, false);
101 BOOST_CHECK_EQUAL(a != b, true);
102
103 b.setName("ndn:/B");
104 BOOST_CHECK_EQUAL(a == b, false);
105 BOOST_CHECK_EQUAL(a != b, true);
106
107 b.setName("ndn:/A");
108 BOOST_CHECK_EQUAL(a == b, true);
109 BOOST_CHECK_EQUAL(a != b, false);
110
111 // check comparison on Selectors
112 a.setChildSelector(1);
113 BOOST_CHECK_EQUAL(a == b, false);
114 BOOST_CHECK_EQUAL(a != b, true);
115
116 b.setChildSelector(1);
117 BOOST_CHECK_EQUAL(a == b, true);
118 BOOST_CHECK_EQUAL(a != b, false);
119
120 // check comparison on Nonce
121 a.setNonce(100);
122 BOOST_CHECK_EQUAL(a == b, false);
123 BOOST_CHECK_EQUAL(a != b, true);
124
125 b.setNonce(100);
126 BOOST_CHECK_EQUAL(a == b, true);
127 BOOST_CHECK_EQUAL(a != b, false);
128
129 // check comparison on Nonce
130 a.setScope(1);
131 BOOST_CHECK_EQUAL(a == b, false);
132 BOOST_CHECK_EQUAL(a != b, true);
133
134 b.setScope(1);
135 BOOST_CHECK_EQUAL(a == b, true);
136 BOOST_CHECK_EQUAL(a != b, false);
137
138 // check comparison on InterestLifetime
139 a.setInterestLifetime(time::seconds(10));
140 BOOST_CHECK_EQUAL(a == b, false);
141 BOOST_CHECK_EQUAL(a != b, true);
142
143 b.setInterestLifetime(time::seconds(10));
144 BOOST_CHECK_EQUAL(a == b, true);
145 BOOST_CHECK_EQUAL(a != b, false);
146}
147
148BOOST_AUTO_TEST_CASE(SelectorsEqualityChecks)
149{
150 // Selectors ::= SELECTORS-TYPE TLV-LENGTH
151 // MinSuffixComponents?
152 // MaxSuffixComponents?
153 // PublisherPublicKeyLocator?
154 // Exclude?
155 // ChildSelector?
156 // MustBeFresh?
157
158 Selectors a;
159 Selectors b;
160 BOOST_CHECK_EQUAL(a == b, true);
161 BOOST_CHECK_EQUAL(a != b, false);
162
163 // MinSuffixComponents
164 a.setMinSuffixComponents(1);
165 BOOST_CHECK_EQUAL(a == b, false);
166 BOOST_CHECK_EQUAL(a != b, true);
167
168 b.setMinSuffixComponents(2);
169 BOOST_CHECK_EQUAL(a == b, false);
170 BOOST_CHECK_EQUAL(a != b, true);
171
172 b.setMinSuffixComponents(1);
173 BOOST_CHECK_EQUAL(a == b, true);
174 BOOST_CHECK_EQUAL(a != b, false);
175
176 // MaxSuffixComponents
177 a.setMaxSuffixComponents(10);
178 BOOST_CHECK_EQUAL(a == b, false);
179 BOOST_CHECK_EQUAL(a != b, true);
180
181 b.setMaxSuffixComponents(10);
182 BOOST_CHECK_EQUAL(a == b, true);
183 BOOST_CHECK_EQUAL(a != b, false);
184
185 // PublisherPublicKeyLocator
186 a.setPublisherPublicKeyLocator(KeyLocator("/key/Locator/name"));
187 BOOST_CHECK_EQUAL(a == b, false);
188 BOOST_CHECK_EQUAL(a != b, true);
189
190 b.setPublisherPublicKeyLocator(KeyLocator("/key/Locator/name"));
191 BOOST_CHECK_EQUAL(a == b, true);
192 BOOST_CHECK_EQUAL(a != b, false);
193
194 // Exclude
195 a.setExclude(Exclude().excludeOne(name::Component("exclude")));
196 BOOST_CHECK_EQUAL(a == b, false);
197 BOOST_CHECK_EQUAL(a != b, true);
198
199 b.setExclude(Exclude().excludeOne(name::Component("exclude")));
200 BOOST_CHECK_EQUAL(a == b, true);
201 BOOST_CHECK_EQUAL(a != b, false);
202
203 // ChildSelector
204 a.setChildSelector(1);
205 BOOST_CHECK_EQUAL(a == b, false);
206 BOOST_CHECK_EQUAL(a != b, true);
207
208 b.setChildSelector(1);
209 BOOST_CHECK_EQUAL(a == b, true);
210 BOOST_CHECK_EQUAL(a != b, false);
211
212 // MustBeFresh
213 a.setMustBeFresh(true);
214 BOOST_CHECK_EQUAL(a == b, false);
215 BOOST_CHECK_EQUAL(a != b, true);
216
217 b.setMustBeFresh(true);
218 BOOST_CHECK_EQUAL(a == b, true);
219 BOOST_CHECK_EQUAL(a != b, false);
220}
221
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700222BOOST_AUTO_TEST_CASE(Decode)
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -0800223{
224 Block interestBlock(Interest1, sizeof(Interest1));
Alexander Afanasyev636e9f12014-01-07 12:01:03 -0800225
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -0800226 ndn::Interest i;
Alexander Afanasyev049f8f72013-12-26 19:07:15 -0800227 BOOST_REQUIRE_NO_THROW(i.wireDecode(interestBlock));
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -0800228
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700229 BOOST_CHECK_EQUAL(i.getName().toUri(), "/local/ndn/prefix");
230 BOOST_CHECK_EQUAL(i.getScope(), 1);
231 BOOST_CHECK_EQUAL(i.getInterestLifetime(), time::milliseconds(1000));
232 BOOST_CHECK_EQUAL(i.getMinSuffixComponents(), 1);
233 BOOST_CHECK_EQUAL(i.getMaxSuffixComponents(), 1);
234 BOOST_CHECK_EQUAL(i.getPublisherPublicKeyLocator().getType(),
235 static_cast<uint32_t>(KeyLocator::KeyLocator_Name));
236 BOOST_CHECK_EQUAL(i.getPublisherPublicKeyLocator().getName(), "ndn:/test/key/locator");
237 BOOST_CHECK_EQUAL(i.getChildSelector(), 1);
238 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
239 BOOST_CHECK_EQUAL(i.getExclude().toUri(), "alex,xxxx,*,yyyy");
240 BOOST_CHECK_EQUAL(i.getNonce(), 1U);
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -0800241}
242
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700243BOOST_AUTO_TEST_CASE(DecodeFromStream)
Alexander Afanasyev6835ad82014-02-12 10:07:20 -0800244{
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -0700245 boost::iostreams::stream<boost::iostreams::array_source> is(
246 reinterpret_cast<const char *>(Interest1), sizeof(Interest1));
Junxiao Shib332e782014-03-31 14:23:46 -0700247
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700248 Block interestBlock = Block::fromStream(is);
Alexander Afanasyev6835ad82014-02-12 10:07:20 -0800249
250 ndn::Interest i;
251 BOOST_REQUIRE_NO_THROW(i.wireDecode(interestBlock));
252
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700253 BOOST_CHECK_EQUAL(i.getName().toUri(), "/local/ndn/prefix");
254 BOOST_CHECK_EQUAL(i.getScope(), 1);
255 BOOST_CHECK_EQUAL(i.getInterestLifetime(), time::milliseconds(1000));
256 BOOST_CHECK_EQUAL(i.getMinSuffixComponents(), 1);
257 BOOST_CHECK_EQUAL(i.getMaxSuffixComponents(), 1);
258 BOOST_CHECK_EQUAL(i.getChildSelector(), 1);
259 BOOST_CHECK_EQUAL(i.getMustBeFresh(), false);
260 BOOST_CHECK_EQUAL(i.getExclude().toUri(), "alex,xxxx,*,yyyy");
261 BOOST_CHECK_EQUAL(i.getNonce(), 1U);
Alexander Afanasyev6835ad82014-02-12 10:07:20 -0800262}
263
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700264BOOST_AUTO_TEST_CASE(Encode)
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -0800265{
266 ndn::Interest i(ndn::Name("/local/ndn/prefix"));
267 i.setScope(1);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700268 i.setInterestLifetime(time::milliseconds(1000));
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -0800269 i.setMinSuffixComponents(1);
270 i.setMaxSuffixComponents(1);
Junxiao Shib332e782014-03-31 14:23:46 -0700271 i.setPublisherPublicKeyLocator(KeyLocator("ndn:/test/key/locator"));
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -0800272 i.setChildSelector(1);
273 i.setMustBeFresh(false);
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800274 Exclude exclude;
275 exclude
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800276 .excludeOne(name::Component("alex"))
277 .excludeRange(name::Component("xxxx"), name::Component("yyyy"));
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800278 i.setExclude(exclude);
Alexander Afanasyev840139f2013-12-28 15:02:50 -0800279 i.setNonce(1);
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -0800280
281 const Block &wire = i.wireEncode();
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800282
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700283 BOOST_CHECK_EQUAL_COLLECTIONS(Interest1, Interest1 + sizeof(Interest1),
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800284 wire.begin(), wire.end());
285}
286
287
288BOOST_AUTO_TEST_CASE(EncodeWithLocalHeader)
289{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800290 ndn::Interest interest(ndn::Name("/local/ndn/prefix"));
291 interest.setMustBeFresh(true);
292 interest.setIncomingFaceId(10);
293 interest.setNonce(1);
Junxiao Shib332e782014-03-31 14:23:46 -0700294
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800295 BOOST_CHECK(!interest.hasWire());
Junxiao Shib332e782014-03-31 14:23:46 -0700296
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800297 Block headerBlock = interest.getLocalControlHeader().wireEncode(interest, true, true);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800298
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800299 BOOST_CHECK(interest.hasWire());
300 BOOST_CHECK(headerBlock.hasWire());
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800301
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800302 BOOST_CHECK_NE(headerBlock.wire(), interest.wireEncode().wire());
303 BOOST_CHECK_NE(headerBlock.size(), interest.wireEncode().size());
304 BOOST_CHECK_EQUAL(headerBlock.size(), 5);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800305
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800306 BOOST_CHECK_EQUAL_COLLECTIONS(InterestWithLocalControlHeader,
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700307 InterestWithLocalControlHeader + 5,
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800308 headerBlock.begin(), headerBlock.end());
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800309
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800310 interest.setNonce(1000);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800311
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800312 Block updatedHeaderBlock = interest.getLocalControlHeader().wireEncode(interest, true, true);
313 BOOST_CHECK_EQUAL(updatedHeaderBlock.size(), 5);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800314
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800315 // only length should have changed
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700316 BOOST_CHECK_EQUAL_COLLECTIONS(updatedHeaderBlock.begin() + 2, updatedHeaderBlock.end(),
317 headerBlock.begin() + 2, headerBlock.end());
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800318
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800319 // updating IncomingFaceId that keeps the length
320 interest.setIncomingFaceId(100);
321 updatedHeaderBlock = interest.getLocalControlHeader().wireEncode(interest, true, true);
322 BOOST_CHECK_EQUAL(updatedHeaderBlock.size(), 5);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700323 BOOST_CHECK_NE(*(updatedHeaderBlock.begin() + 4), *(headerBlock.begin() + 4));
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800324
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800325 // updating IncomingFaceId that increases the length by 2
326 interest.setIncomingFaceId(1000);
327 updatedHeaderBlock = interest.getLocalControlHeader().wireEncode(interest, true, true);
328 BOOST_CHECK_EQUAL(updatedHeaderBlock.size(), 6);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800329
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800330 // adding NextHopId
331 interest.setNextHopFaceId(1);
332 updatedHeaderBlock = interest.getLocalControlHeader().wireEncode(interest, true, true);
333 BOOST_CHECK_EQUAL(updatedHeaderBlock.size(), 9);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800334
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800335 // masking IncomingFaceId
336 updatedHeaderBlock = interest.getLocalControlHeader().wireEncode(interest, false, true);
337 BOOST_CHECK_EQUAL(updatedHeaderBlock.size(), 5);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800338
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800339 // masking NextHopId
340 updatedHeaderBlock = interest.getLocalControlHeader().wireEncode(interest, true, false);
341 BOOST_CHECK_EQUAL(updatedHeaderBlock.size(), 6);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800342
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800343 // masking everything
344 BOOST_CHECK_THROW(interest.getLocalControlHeader().wireEncode(interest, false, false),
345 nfd::LocalControlHeader::Error);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800346}
347
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800348
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700349BOOST_AUTO_TEST_CASE(DecodeWithLocalHeader)
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800350{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800351 Block wireBlock(InterestWithLocalControlHeader, sizeof(InterestWithLocalControlHeader));
352 const Block& payload = nfd::LocalControlHeader::getPayload(wireBlock);
353 BOOST_REQUIRE_NE(&payload, &wireBlock);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800354
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700355 BOOST_CHECK_EQUAL(payload.type(), static_cast<uint32_t>(Tlv::Interest));
356 BOOST_CHECK_EQUAL(wireBlock.type(), static_cast<uint32_t>(tlv::nfd::LocalControlHeader));
Junxiao Shib332e782014-03-31 14:23:46 -0700357
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800358 Interest interest(payload);
359 BOOST_CHECK(!interest.getLocalControlHeader().hasIncomingFaceId());
360 BOOST_CHECK(!interest.getLocalControlHeader().hasNextHopFaceId());
361
362 BOOST_REQUIRE_NO_THROW(interest.getLocalControlHeader().wireDecode(wireBlock));
363
364 BOOST_CHECK_EQUAL(interest.getLocalControlHeader().wireEncode(interest, true, true).size(), 5);
Junxiao Shib332e782014-03-31 14:23:46 -0700365
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800366 BOOST_CHECK_EQUAL(interest.getIncomingFaceId(), 10);
367 BOOST_CHECK(!interest.getLocalControlHeader().hasNextHopFaceId());
368
369 BOOST_CHECK_THROW(interest.getLocalControlHeader().wireEncode(interest, false, false),
370 nfd::LocalControlHeader::Error);
371
372 BOOST_CHECK_THROW(interest.getLocalControlHeader().wireEncode(interest, false, true),
373 nfd::LocalControlHeader::Error);
374
375 BOOST_CHECK_NO_THROW(interest.getLocalControlHeader().wireEncode(interest, true, false));
376 BOOST_CHECK_NO_THROW(interest.getLocalControlHeader().wireEncode(interest, true, true));
Junxiao Shib332e782014-03-31 14:23:46 -0700377
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800378 BOOST_CHECK_NE((void*)interest.getLocalControlHeader().wireEncode(interest, true, true).wire(),
379 (void*)wireBlock.wire());
380
381 BOOST_CHECK_EQUAL(interest.getLocalControlHeader().wireEncode(interest, true, true).size(), 5);
382}
383
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700384BOOST_AUTO_TEST_CASE(DecodeWithoutLocalHeader)
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800385{
386 Block wireBlock(InterestWithoutLocalControlHeader, sizeof(InterestWithoutLocalControlHeader));
387 const Block& payload = nfd::LocalControlHeader::getPayload(wireBlock);
388 BOOST_CHECK_EQUAL(&payload, &wireBlock);
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -0800389}
390
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700391BOOST_AUTO_TEST_CASE(MatchesData)
392{
393 Interest interest;
394 interest.setName("ndn:/A")
395 .setMinSuffixComponents(2)
396 .setMaxSuffixComponents(2)
397 .setPublisherPublicKeyLocator(KeyLocator("ndn:/B"))
398 .setExclude(Exclude().excludeBefore(name::Component("C")));
399
400 Data data("ndn:/A/D");
401 SignatureSha256WithRsa signature;
402 signature.setKeyLocator(KeyLocator("ndn:/B"));
403 data.setSignature(signature);
404 BOOST_CHECK_EQUAL(interest.matchesData(data), true);
405
406 Data data1 = data;
407 data1.setName("ndn:/A");// violates MinSuffixComponents
408 BOOST_CHECK_EQUAL(interest.matchesData(data1), false);
409
410 Data data2 = data;
411 data2.setName("ndn:/A/E/F");// violates MaxSuffixComponents
412 BOOST_CHECK_EQUAL(interest.matchesData(data2), false);
413
414 Data data3 = data;
415 SignatureSha256WithRsa signature3;
416 signature3.setKeyLocator(KeyLocator("ndn:/G"));// violates PublisherPublicKeyLocator
417 data3.setSignature(signature3);
418 BOOST_CHECK_EQUAL(interest.matchesData(data3), false);
419
420 Data data4 = data;
421 SignatureSha256 signature4;// violates PublisherPublicKeyLocator
422 data4.setSignature(signature4);
423 BOOST_CHECK_EQUAL(interest.matchesData(data4), false);
424
425 Data data5 = data;
426 data5.setName("ndn:/A/C");// violates Exclude
427 BOOST_CHECK_EQUAL(interest.matchesData(data5), false);
428
429 Data data6 = data;
430 data6.setName("ndn:/H/I");// violates Name
431 BOOST_CHECK_EQUAL(interest.matchesData(data6), false);
432}
433
Alexander Afanasyev90164962014-03-06 08:29:59 +0000434BOOST_AUTO_TEST_CASE(InterestFilterMatching)
435{
436 BOOST_CHECK_EQUAL(InterestFilter("/a").doesMatch("/a/b"), true);
437 BOOST_CHECK_EQUAL(InterestFilter("/a/b").doesMatch("/a/b"), true);
438 BOOST_CHECK_EQUAL(InterestFilter("/a/b/c").doesMatch("/a/b"), false);
439
440 BOOST_CHECK_EQUAL(InterestFilter("/a", "<b>").doesMatch("/a/b"), true);
441 BOOST_CHECK_EQUAL(InterestFilter("/a/b", "<b>").doesMatch("/a/b"), false);
442
443 BOOST_CHECK_EQUAL(InterestFilter("/a/b", "<b>").doesMatch("/a/b/c/b"), false);
444 BOOST_CHECK_EQUAL(InterestFilter("/a/b", "<>*<b>").doesMatch("/a/b/c/b"), true);
445
446 BOOST_CHECK_EQUAL(InterestFilter("/a", "<b>").doesMatch("/a/b/c/d"), false);
447 BOOST_CHECK_EQUAL(InterestFilter("/a", "<b><>*").doesMatch("/a/b/c/d"), true);
448 BOOST_CHECK_EQUAL(InterestFilter("/a", "<b><>*").doesMatch("/a/b"), true);
449 BOOST_CHECK_EQUAL(InterestFilter("/a", "<b><>+").doesMatch("/a/b"), false);
450 BOOST_CHECK_EQUAL(InterestFilter("/a", "<b><>+").doesMatch("/a/b/c"), true);
451}
452
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -0800453BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800454
Alexander Afanasyev90164962014-03-06 08:29:59 +0000455} // namespace tests
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800456} // namespace ndn