blob: 05a2d6a93bfcf431e746e3f491a84ac7f14f0bd9 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev20d2c582014-01-26 15:32:51 -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 Afanasyev20d2c582014-01-26 15:32:51 -080020 */
21
Alexander Afanasyev09c613f2014-01-29 00:23:58 -080022#include "face.hpp"
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080023#include "util/scheduler.hpp"
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -070024#include "security/key-chain.hpp"
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080025
Yingdi Yu41546342014-11-30 23:37:53 -080026#include "identity-management-fixture.hpp"
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070027#include "boost-test.hpp"
28
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080029namespace ndn {
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040030namespace tests {
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080031
Yingdi Yu41546342014-11-30 23:37:53 -080032class FacesFixture : public security::IdentityManagementFixture
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080033{
Alexander Afanasyev90164962014-03-06 08:29:59 +000034public:
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080035 FacesFixture()
Alexander Afanasyev90164962014-03-06 08:29:59 +000036 : nData(0)
37 , nTimeouts(0)
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080038 , regPrefixId(0)
Alexander Afanasyev90164962014-03-06 08:29:59 +000039 , nInInterests(0)
40 , nInInterests2(0)
41 , nRegFailures(0)
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080042 {
43 }
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070044
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080045 void
46 onData()
47 {
Alexander Afanasyev90164962014-03-06 08:29:59 +000048 ++nData;
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080049 }
50
51 void
52 onTimeout()
53 {
Alexander Afanasyev90164962014-03-06 08:29:59 +000054 ++nTimeouts;
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080055 }
56
57 void
Alexander Afanasyev90164962014-03-06 08:29:59 +000058 onInterest(Face& face,
59 const Name&, const Interest&)
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080060 {
Alexander Afanasyev90164962014-03-06 08:29:59 +000061 ++nInInterests;
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080062
63 face.unsetInterestFilter(regPrefixId);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080064 }
65
66 void
Alexander Afanasyev90164962014-03-06 08:29:59 +000067 onInterest2(Face& face,
68 const Name&, const Interest&)
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -080069 {
Alexander Afanasyev90164962014-03-06 08:29:59 +000070 ++nInInterests2;
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -080071
72 face.unsetInterestFilter(regPrefixId2);
73 }
74
75 void
Alexander Afanasyev90164962014-03-06 08:29:59 +000076 onInterestRegex(Face& face,
77 const InterestFilter&, const Interest&)
78 {
79 ++nInInterests;
80 }
81
82 void
83 onInterestRegexError(Face& face,
84 const Name&, const Interest&)
85 {
86 BOOST_FAIL("InterestFilter::Error should have been triggered");
87 }
88
89 void
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080090 onRegFailed()
91 {
Alexander Afanasyev90164962014-03-06 08:29:59 +000092 ++nRegFailures;
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080093 }
94
95 void
96 expressInterest(Face& face, const Name& name)
97 {
98 Interest i(name);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070099 i.setInterestLifetime(time::milliseconds(50));
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800100 face.expressInterest(i,
101 bind(&FacesFixture::onData, this),
102 bind(&FacesFixture::onTimeout, this));
103 }
104
105 void
106 terminate(Face& face)
107 {
108 face.shutdown();
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800109 }
110
Alexander Afanasyev90164962014-03-06 08:29:59 +0000111 uint32_t nData;
112 uint32_t nTimeouts;
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800113
114 const RegisteredPrefixId* regPrefixId;
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800115 const RegisteredPrefixId* regPrefixId2;
Alexander Afanasyev90164962014-03-06 08:29:59 +0000116 uint32_t nInInterests;
117 uint32_t nInInterests2;
118 uint32_t nRegFailures;
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800119};
120
Alexander Afanasyev90164962014-03-06 08:29:59 +0000121BOOST_FIXTURE_TEST_SUITE(TestFaces, FacesFixture)
122
123BOOST_AUTO_TEST_CASE(Unix)
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800124{
125 Face face;
126
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700127 face.expressInterest(Interest("/", time::milliseconds(1000)),
128 bind(&FacesFixture::onData, this),
129 bind(&FacesFixture::onTimeout, this));
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800130
131 BOOST_REQUIRE_NO_THROW(face.processEvents());
132
Alexander Afanasyev90164962014-03-06 08:29:59 +0000133 BOOST_CHECK_EQUAL(nData, 1);
134 BOOST_CHECK_EQUAL(nTimeouts, 0);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800135
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700136 face.expressInterest(Interest("/localhost/non-existing/data/should/not/exist/anywhere",
137 time::milliseconds(50)),
138 bind(&FacesFixture::onData, this),
139 bind(&FacesFixture::onTimeout, this));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700140
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700141 Name veryLongName;
142 for (size_t i = 0; i <= MAX_NDN_PACKET_SIZE / 10; i++)
143 {
144 veryLongName.append("0123456789");
145 }
146
147 BOOST_CHECK_THROW(face.expressInterest(veryLongName, OnData(), OnTimeout()), Face::Error);
148
149 shared_ptr<Data> data = make_shared<Data>(veryLongName);
150 data->setContent(reinterpret_cast<const uint8_t*>("01234567890"), 10);
Yingdi Yu41546342014-11-30 23:37:53 -0800151 m_keyChain.sign(*data);
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700152 BOOST_CHECK_THROW(face.put(*data), Face::Error);
153
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800154 BOOST_REQUIRE_NO_THROW(face.processEvents());
155
Alexander Afanasyev90164962014-03-06 08:29:59 +0000156 BOOST_CHECK_EQUAL(nData, 1);
157 BOOST_CHECK_EQUAL(nTimeouts, 1);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800158}
159
Alexander Afanasyev90164962014-03-06 08:29:59 +0000160BOOST_AUTO_TEST_CASE(Tcp)
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800161{
162 Face face("localhost");
163
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700164 face.expressInterest(Interest("/", time::milliseconds(1000)),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800165 bind(&FacesFixture::onData, this),
166 bind(&FacesFixture::onTimeout, this));
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800167
168 BOOST_REQUIRE_NO_THROW(face.processEvents());
169
Alexander Afanasyev90164962014-03-06 08:29:59 +0000170 BOOST_CHECK_EQUAL(nData, 1);
171 BOOST_CHECK_EQUAL(nTimeouts, 0);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800172
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700173 face.expressInterest(Interest("/localhost/non-existing/data/should/not/exist/anywhere",
174 time::milliseconds(50)),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800175 bind(&FacesFixture::onData, this),
176 bind(&FacesFixture::onTimeout, this));
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800177
178 BOOST_REQUIRE_NO_THROW(face.processEvents());
179
Alexander Afanasyev90164962014-03-06 08:29:59 +0000180 BOOST_CHECK_EQUAL(nData, 1);
181 BOOST_CHECK_EQUAL(nTimeouts, 1);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800182}
183
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800184
Alexander Afanasyev90164962014-03-06 08:29:59 +0000185BOOST_AUTO_TEST_CASE(SetFilter)
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800186{
187 Face face;
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700188 Face face2(face.getIoService());
189 Scheduler scheduler(face.getIoService());
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700190 scheduler.scheduleEvent(time::milliseconds(300),
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700191 bind(&FacesFixture::terminate, this, ref(face)));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700192
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800193 regPrefixId = face.setInterestFilter("/Hello/World",
Alexander Afanasyev90164962014-03-06 08:29:59 +0000194 bind(&FacesFixture::onInterest, this, ref(face), _1, _2),
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700195 RegisterPrefixSuccessCallback(),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800196 bind(&FacesFixture::onRegFailed, this));
197
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700198 scheduler.scheduleEvent(time::milliseconds(200),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800199 bind(&FacesFixture::expressInterest, this,
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700200 ref(face2), Name("/Hello/World/!")));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700201
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800202 BOOST_REQUIRE_NO_THROW(face.processEvents());
203
Alexander Afanasyev90164962014-03-06 08:29:59 +0000204 BOOST_CHECK_EQUAL(nRegFailures, 0);
205 BOOST_CHECK_EQUAL(nInInterests, 1);
206 BOOST_CHECK_EQUAL(nTimeouts, 1);
207 BOOST_CHECK_EQUAL(nData, 0);
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800208}
209
Alexander Afanasyev90164962014-03-06 08:29:59 +0000210BOOST_AUTO_TEST_CASE(SetTwoFilters)
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800211{
212 Face face;
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700213 Face face2(face.getIoService());
214 Scheduler scheduler(face.getIoService());
Obaid6e7f5f12014-03-11 14:46:10 -0500215 scheduler.scheduleEvent(time::seconds(1),
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700216 bind(&FacesFixture::terminate, this, ref(face)));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700217
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800218 regPrefixId = face.setInterestFilter("/Hello/World",
Alexander Afanasyev90164962014-03-06 08:29:59 +0000219 bind(&FacesFixture::onInterest, this, ref(face), _1, _2),
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700220 RegisterPrefixSuccessCallback(),
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800221 bind(&FacesFixture::onRegFailed, this));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700222
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800223 regPrefixId2 = face.setInterestFilter("/Los/Angeles/Lakers",
Alexander Afanasyev90164962014-03-06 08:29:59 +0000224 bind(&FacesFixture::onInterest2, this, ref(face), _1, _2),
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700225 RegisterPrefixSuccessCallback(),
Alexander Afanasyev90164962014-03-06 08:29:59 +0000226 bind(&FacesFixture::onRegFailed, this));
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800227
228
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700229 scheduler.scheduleEvent(time::milliseconds(200),
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800230 bind(&FacesFixture::expressInterest, this,
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700231 ref(face2), Name("/Hello/World/!")));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700232
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800233 BOOST_REQUIRE_NO_THROW(face.processEvents());
234
Alexander Afanasyev90164962014-03-06 08:29:59 +0000235 BOOST_CHECK_EQUAL(nRegFailures, 0);
236 BOOST_CHECK_EQUAL(nInInterests, 1);
237 BOOST_CHECK_EQUAL(nInInterests2, 0);
238 BOOST_CHECK_EQUAL(nTimeouts, 1);
239 BOOST_CHECK_EQUAL(nData, 0);
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800240}
241
Alexander Afanasyev90164962014-03-06 08:29:59 +0000242BOOST_AUTO_TEST_CASE(SetRegexFilterError)
243{
244 Face face;
245 Face face2(face.getIoService());
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700246 Scheduler scheduler(face.getIoService());
Alexander Afanasyev90164962014-03-06 08:29:59 +0000247 scheduler.scheduleEvent(time::seconds(1),
248 bind(&FacesFixture::terminate, this, ref(face)));
249
250 regPrefixId = face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
251 bind(&FacesFixture::onInterestRegexError, this,
252 ref(face), _1, _2),
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700253 RegisterPrefixSuccessCallback(),
Alexander Afanasyev90164962014-03-06 08:29:59 +0000254 bind(&FacesFixture::onRegFailed, this));
255
256 scheduler.scheduleEvent(time::milliseconds(300),
257 bind(&FacesFixture::expressInterest, this,
258 ref(face2), Name("/Hello/World/XXX/b/c"))); // should match
259
260 BOOST_REQUIRE_THROW(face.processEvents(), InterestFilter::Error);
261}
262
263BOOST_AUTO_TEST_CASE(SetRegexFilter)
264{
265 Face face;
266 Face face2(face.getIoService());
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700267 Scheduler scheduler(face.getIoService());
Alexander Afanasyev90164962014-03-06 08:29:59 +0000268 scheduler.scheduleEvent(time::seconds(2),
269 bind(&FacesFixture::terminate, this, ref(face)));
270
271 regPrefixId = face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
272 bind(&FacesFixture::onInterestRegex, this,
273 ref(face), _1, _2),
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700274 RegisterPrefixSuccessCallback(),
Alexander Afanasyev90164962014-03-06 08:29:59 +0000275 bind(&FacesFixture::onRegFailed, this));
276
277 scheduler.scheduleEvent(time::milliseconds(200),
278 bind(&FacesFixture::expressInterest, this,
279 ref(face2), Name("/Hello/World/a"))); // shouldn't match
280
281 scheduler.scheduleEvent(time::milliseconds(300),
282 bind(&FacesFixture::expressInterest, this,
283 ref(face2), Name("/Hello/World/a/b"))); // should match
284
285 scheduler.scheduleEvent(time::milliseconds(400),
286 bind(&FacesFixture::expressInterest, this,
287 ref(face2), Name("/Hello/World/a/b/c"))); // should match
288
289 scheduler.scheduleEvent(time::milliseconds(500),
290 bind(&FacesFixture::expressInterest, this,
291 ref(face2), Name("/Hello/World/a/b/d"))); // should not match
292
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700293 BOOST_REQUIRE_NO_THROW(face.processEvents());
Alexander Afanasyev90164962014-03-06 08:29:59 +0000294
295 BOOST_CHECK_EQUAL(nRegFailures, 0);
296 BOOST_CHECK_EQUAL(nInInterests, 2);
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700297 BOOST_CHECK_EQUAL(nTimeouts, 4);
Alexander Afanasyev90164962014-03-06 08:29:59 +0000298 BOOST_CHECK_EQUAL(nData, 0);
299}
300
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700301class FacesFixture2 : public FacesFixture
302{
303public:
304 void
305 checkPrefix(bool doesExist)
306 {
307 int result = std::system("nfd-status | grep /Hello/World >/dev/null");
308
309 if (doesExist) {
310 BOOST_CHECK_EQUAL(result, 0);
311 }
312 else {
313 BOOST_CHECK_NE(result, 0);
314 }
315 }
316};
317
318BOOST_FIXTURE_TEST_CASE(RegisterUnregisterPrefix, FacesFixture2)
319{
320 Face face;
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700321 Scheduler scheduler(face.getIoService());
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700322 scheduler.scheduleEvent(time::seconds(2),
323 bind(&FacesFixture::terminate, this, ref(face)));
324
325 regPrefixId = face.setInterestFilter(InterestFilter("/Hello/World"),
326 bind(&FacesFixture::onInterest, this,
327 ref(face), _1, _2),
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700328 RegisterPrefixSuccessCallback(),
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700329 bind(&FacesFixture::onRegFailed, this));
330
331 scheduler.scheduleEvent(time::milliseconds(500),
332 bind(&FacesFixture2::checkPrefix, this, true));
333
334 scheduler.scheduleEvent(time::seconds(1),
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700335 bind(static_cast<void(Face::*)(const RegisteredPrefixId*)>(&Face::unsetInterestFilter),
336 &face,
337 regPrefixId)); // shouldn't match
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700338
339 scheduler.scheduleEvent(time::milliseconds(1500),
340 bind(&FacesFixture2::checkPrefix, this, false));
341
342 BOOST_REQUIRE_NO_THROW(face.processEvents());
343}
Alexander Afanasyev90164962014-03-06 08:29:59 +0000344
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700345
346class FacesFixture3 : public FacesFixture2
347{
348public:
349 FacesFixture3()
350 : nRegSuccesses(0)
351 , nUnregSuccesses(0)
352 , nUnregFailures(0)
353 {
354 }
355
356 void
357 onRegSucceeded()
358 {
359 ++nRegSuccesses;
360 }
361
362 void
363 onUnregSucceeded()
364 {
365 ++nUnregSuccesses;
366 }
367
368 void
369 onUnregFailed()
370 {
371 ++nUnregFailures;
372 }
373
374public:
375 uint64_t nRegSuccesses;
376 uint64_t nUnregSuccesses;
377 uint64_t nUnregFailures;
378};
379
380BOOST_FIXTURE_TEST_CASE(RegisterPrefix, FacesFixture3)
381{
382 Face face;
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700383 Face face2(face.getIoService());
384 Scheduler scheduler(face.getIoService());
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700385 scheduler.scheduleEvent(time::seconds(2),
386 bind(&FacesFixture::terminate, this, ref(face)));
387
388 scheduler.scheduleEvent(time::milliseconds(500),
389 bind(&FacesFixture2::checkPrefix, this, true));
390
391 regPrefixId = face.registerPrefix("/Hello/World",
392 bind(&FacesFixture3::onRegSucceeded, this),
393 bind(&FacesFixture3::onRegFailed, this));
394
395 scheduler.scheduleEvent(time::seconds(1),
396 bind(&Face::unregisterPrefix, &face,
397 regPrefixId,
398 static_cast<UnregisterPrefixSuccessCallback>(bind(&FacesFixture3::onUnregSucceeded, this)),
399 static_cast<UnregisterPrefixFailureCallback>(bind(&FacesFixture3::onUnregFailed, this))));
400
401 scheduler.scheduleEvent(time::milliseconds(1500),
402 bind(&FacesFixture2::checkPrefix, this, false));
403
404 BOOST_REQUIRE_NO_THROW(face.processEvents());
405
406 BOOST_CHECK_EQUAL(nRegFailures, 0);
407 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
408
409 BOOST_CHECK_EQUAL(nUnregFailures, 0);
410 BOOST_CHECK_EQUAL(nUnregSuccesses, 1);
411}
412
413BOOST_AUTO_TEST_CASE(SetRegexFilterButNoRegister)
414{
415 Face face;
416 Face face2(face.getIoService());
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700417 Scheduler scheduler(face.getIoService());
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700418 scheduler.scheduleEvent(time::seconds(2),
419 bind(&FacesFixture::terminate, this, ref(face)));
420
421 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
422 bind(&FacesFixture::onInterestRegex, this,
423 ref(face), _1, _2));
424
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700425 // prefix is not registered, and also does not match regex
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700426 scheduler.scheduleEvent(time::milliseconds(200),
427 bind(&FacesFixture::expressInterest, this,
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700428 ref(face2), Name("/Hello/World/a")));
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700429
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700430 // matches regex, but prefix is not registered
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700431 scheduler.scheduleEvent(time::milliseconds(300),
432 bind(&FacesFixture::expressInterest, this,
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700433 ref(face2), Name("/Hello/World/a/b")));
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700434
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700435 // matches regex, but prefix is not registered
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700436 scheduler.scheduleEvent(time::milliseconds(400),
437 bind(&FacesFixture::expressInterest, this,
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700438 ref(face2), Name("/Hello/World/a/b/c")));
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700439
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700440 // prefix is not registered, and also does not match regex
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700441 scheduler.scheduleEvent(time::milliseconds(500),
442 bind(&FacesFixture::expressInterest, this,
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700443 ref(face2), Name("/Hello/World/a/b/d")));
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700444
445 BOOST_REQUIRE_NO_THROW(face.processEvents());
446
447 BOOST_CHECK_EQUAL(nRegFailures, 0);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700448 BOOST_CHECK_EQUAL(nInInterests, 0);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700449 BOOST_CHECK_EQUAL(nTimeouts, 4);
450 BOOST_CHECK_EQUAL(nData, 0);
451}
452
453
454BOOST_FIXTURE_TEST_CASE(SetRegexFilterAndRegister, FacesFixture3)
455{
456 Face face;
457 Face face2(face.getIoService());
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700458 Scheduler scheduler(face.getIoService());
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700459 scheduler.scheduleEvent(time::seconds(2),
460 bind(&FacesFixture::terminate, this, ref(face)));
461
462 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
463 bind(&FacesFixture::onInterestRegex, this,
464 ref(face), _1, _2));
465
466 face.registerPrefix("/Hello/World",
467 bind(&FacesFixture3::onRegSucceeded, this),
468 bind(&FacesFixture3::onRegFailed, this));
469
470 scheduler.scheduleEvent(time::milliseconds(200),
471 bind(&FacesFixture::expressInterest, this,
472 ref(face2), Name("/Hello/World/a"))); // shouldn't match
473
474 scheduler.scheduleEvent(time::milliseconds(300),
475 bind(&FacesFixture::expressInterest, this,
476 ref(face2), Name("/Hello/World/a/b"))); // should match
477
478 scheduler.scheduleEvent(time::milliseconds(400),
479 bind(&FacesFixture::expressInterest, this,
480 ref(face2), Name("/Hello/World/a/b/c"))); // should match
481
482 scheduler.scheduleEvent(time::milliseconds(500),
483 bind(&FacesFixture::expressInterest, this,
484 ref(face2), Name("/Hello/World/a/b/d"))); // should not match
485
486 BOOST_REQUIRE_NO_THROW(face.processEvents());
487
488 BOOST_CHECK_EQUAL(nRegFailures, 0);
489 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
490
491 BOOST_CHECK_EQUAL(nInInterests, 2);
492 BOOST_CHECK_EQUAL(nTimeouts, 4);
493}
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400494
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800495BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800496
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400497} // tests
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800498} // namespace ndn