blob: 68cc91edb33b9f8ac06b11aba0023f3da263455f [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 Afanasyeve4f8c3b2016-06-23 16:03:48 -07003 * Copyright (c) 2013-2016 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 Afanasyeve6c65e22015-01-28 19:56:03 -080022#define BOOST_TEST_MAIN 1
23#define BOOST_TEST_DYN_LINK 1
24#define BOOST_TEST_MODULE ndn-cxx Integrated Tests (Face)
25
Alexander Afanasyev09c613f2014-01-29 00:23:58 -080026#include "face.hpp"
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080027#include "util/scheduler.hpp"
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -070028#include "security/key-chain.hpp"
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080029
Yingdi Yu41546342014-11-30 23:37:53 -080030#include "identity-management-fixture.hpp"
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070031#include "boost-test.hpp"
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070032#include "key-chain-fixture.hpp"
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070033
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080034namespace ndn {
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040035namespace tests {
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080036
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070037struct PibDirWithDefaultTpm
38{
39 const std::string PATH = "build/keys-with-default-tpm";
40};
41
42class FacesFixture : public IdentityManagementFixture,
43 public PibDirFixture<PibDirWithDefaultTpm>
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080044{
Alexander Afanasyev90164962014-03-06 08:29:59 +000045public:
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080046 FacesFixture()
Alexander Afanasyev90164962014-03-06 08:29:59 +000047 : nData(0)
48 , nTimeouts(0)
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080049 , regPrefixId(0)
Alexander Afanasyev90164962014-03-06 08:29:59 +000050 , nInInterests(0)
51 , nInInterests2(0)
52 , nRegFailures(0)
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080053 {
54 }
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070055
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080056 void
57 onData()
58 {
Alexander Afanasyev90164962014-03-06 08:29:59 +000059 ++nData;
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080060 }
61
62 void
63 onTimeout()
64 {
Alexander Afanasyev90164962014-03-06 08:29:59 +000065 ++nTimeouts;
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080066 }
67
68 void
Alexander Afanasyev90164962014-03-06 08:29:59 +000069 onInterest(Face& face,
70 const Name&, const Interest&)
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080071 {
Alexander Afanasyev90164962014-03-06 08:29:59 +000072 ++nInInterests;
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080073
74 face.unsetInterestFilter(regPrefixId);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080075 }
76
77 void
Alexander Afanasyev90164962014-03-06 08:29:59 +000078 onInterest2(Face& face,
79 const Name&, const Interest&)
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -080080 {
Alexander Afanasyev90164962014-03-06 08:29:59 +000081 ++nInInterests2;
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -080082
83 face.unsetInterestFilter(regPrefixId2);
84 }
85
86 void
Alexander Afanasyev90164962014-03-06 08:29:59 +000087 onInterestRegex(Face& face,
88 const InterestFilter&, const Interest&)
89 {
90 ++nInInterests;
91 }
92
93 void
94 onInterestRegexError(Face& face,
95 const Name&, const Interest&)
96 {
97 BOOST_FAIL("InterestFilter::Error should have been triggered");
98 }
99
100 void
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800101 onRegFailed()
102 {
Alexander Afanasyev90164962014-03-06 08:29:59 +0000103 ++nRegFailures;
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800104 }
105
106 void
107 expressInterest(Face& face, const Name& name)
108 {
109 Interest i(name);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700110 i.setInterestLifetime(time::milliseconds(50));
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800111 face.expressInterest(i,
112 bind(&FacesFixture::onData, this),
113 bind(&FacesFixture::onTimeout, this));
114 }
115
116 void
117 terminate(Face& face)
118 {
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800119 face.getIoService().stop();
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800120 }
121
Alexander Afanasyev90164962014-03-06 08:29:59 +0000122 uint32_t nData;
123 uint32_t nTimeouts;
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800124
125 const RegisteredPrefixId* regPrefixId;
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800126 const RegisteredPrefixId* regPrefixId2;
Alexander Afanasyev90164962014-03-06 08:29:59 +0000127 uint32_t nInInterests;
128 uint32_t nInInterests2;
129 uint32_t nRegFailures;
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800130};
131
Alexander Afanasyev90164962014-03-06 08:29:59 +0000132BOOST_FIXTURE_TEST_SUITE(TestFaces, FacesFixture)
133
134BOOST_AUTO_TEST_CASE(Unix)
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800135{
136 Face face;
137
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700138 face.expressInterest(Interest("/", time::milliseconds(1000)),
139 bind(&FacesFixture::onData, this),
140 bind(&FacesFixture::onTimeout, this));
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800141
142 BOOST_REQUIRE_NO_THROW(face.processEvents());
143
Alexander Afanasyev90164962014-03-06 08:29:59 +0000144 BOOST_CHECK_EQUAL(nData, 1);
145 BOOST_CHECK_EQUAL(nTimeouts, 0);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800146
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700147 face.expressInterest(Interest("/localhost/non-existing/data/should/not/exist/anywhere",
148 time::milliseconds(50)),
149 bind(&FacesFixture::onData, this),
150 bind(&FacesFixture::onTimeout, this));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700151
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700152 Name veryLongName;
153 for (size_t i = 0; i <= MAX_NDN_PACKET_SIZE / 10; i++)
154 {
155 veryLongName.append("0123456789");
156 }
157
158 BOOST_CHECK_THROW(face.expressInterest(veryLongName, OnData(), OnTimeout()), Face::Error);
159
160 shared_ptr<Data> data = make_shared<Data>(veryLongName);
161 data->setContent(reinterpret_cast<const uint8_t*>("01234567890"), 10);
Yingdi Yu41546342014-11-30 23:37:53 -0800162 m_keyChain.sign(*data);
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700163 BOOST_CHECK_THROW(face.put(*data), Face::Error);
164
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800165 BOOST_REQUIRE_NO_THROW(face.processEvents());
166
Alexander Afanasyev90164962014-03-06 08:29:59 +0000167 BOOST_CHECK_EQUAL(nData, 1);
168 BOOST_CHECK_EQUAL(nTimeouts, 1);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800169}
170
Alexander Afanasyev90164962014-03-06 08:29:59 +0000171BOOST_AUTO_TEST_CASE(Tcp)
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800172{
173 Face face("localhost");
174
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700175 face.expressInterest(Interest("/", time::milliseconds(1000)),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800176 bind(&FacesFixture::onData, this),
177 bind(&FacesFixture::onTimeout, this));
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800178
179 BOOST_REQUIRE_NO_THROW(face.processEvents());
180
Alexander Afanasyev90164962014-03-06 08:29:59 +0000181 BOOST_CHECK_EQUAL(nData, 1);
182 BOOST_CHECK_EQUAL(nTimeouts, 0);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800183
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700184 face.expressInterest(Interest("/localhost/non-existing/data/should/not/exist/anywhere",
185 time::milliseconds(50)),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800186 bind(&FacesFixture::onData, this),
187 bind(&FacesFixture::onTimeout, this));
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800188
189 BOOST_REQUIRE_NO_THROW(face.processEvents());
190
Alexander Afanasyev90164962014-03-06 08:29:59 +0000191 BOOST_CHECK_EQUAL(nData, 1);
192 BOOST_CHECK_EQUAL(nTimeouts, 1);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800193}
194
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800195
Alexander Afanasyev90164962014-03-06 08:29:59 +0000196BOOST_AUTO_TEST_CASE(SetFilter)
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800197{
198 Face face;
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700199 Face face2(face.getIoService());
200 Scheduler scheduler(face.getIoService());
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800201 scheduler.scheduleEvent(time::seconds(4),
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700202 bind(&FacesFixture::terminate, this, ref(face)));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700203
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800204 regPrefixId = face.setInterestFilter("/Hello/World",
Alexander Afanasyev90164962014-03-06 08:29:59 +0000205 bind(&FacesFixture::onInterest, this, ref(face), _1, _2),
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700206 RegisterPrefixSuccessCallback(),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800207 bind(&FacesFixture::onRegFailed, this));
208
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700209 scheduler.scheduleEvent(time::milliseconds(200),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800210 bind(&FacesFixture::expressInterest, this,
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700211 ref(face2), Name("/Hello/World/!")));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700212
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800213 BOOST_REQUIRE_NO_THROW(face.processEvents());
214
Alexander Afanasyev90164962014-03-06 08:29:59 +0000215 BOOST_CHECK_EQUAL(nRegFailures, 0);
216 BOOST_CHECK_EQUAL(nInInterests, 1);
217 BOOST_CHECK_EQUAL(nTimeouts, 1);
218 BOOST_CHECK_EQUAL(nData, 0);
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800219}
220
Alexander Afanasyev90164962014-03-06 08:29:59 +0000221BOOST_AUTO_TEST_CASE(SetTwoFilters)
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800222{
223 Face face;
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700224 Face face2(face.getIoService());
225 Scheduler scheduler(face.getIoService());
Obaid6e7f5f12014-03-11 14:46:10 -0500226 scheduler.scheduleEvent(time::seconds(1),
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700227 bind(&FacesFixture::terminate, this, ref(face)));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700228
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800229 regPrefixId = face.setInterestFilter("/Hello/World",
Alexander Afanasyev90164962014-03-06 08:29:59 +0000230 bind(&FacesFixture::onInterest, this, ref(face), _1, _2),
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700231 RegisterPrefixSuccessCallback(),
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800232 bind(&FacesFixture::onRegFailed, this));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700233
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800234 regPrefixId2 = face.setInterestFilter("/Los/Angeles/Lakers",
Alexander Afanasyev90164962014-03-06 08:29:59 +0000235 bind(&FacesFixture::onInterest2, this, ref(face), _1, _2),
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700236 RegisterPrefixSuccessCallback(),
Alexander Afanasyev90164962014-03-06 08:29:59 +0000237 bind(&FacesFixture::onRegFailed, this));
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800238
239
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700240 scheduler.scheduleEvent(time::milliseconds(200),
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800241 bind(&FacesFixture::expressInterest, this,
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700242 ref(face2), Name("/Hello/World/!")));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700243
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800244 BOOST_REQUIRE_NO_THROW(face.processEvents());
245
Alexander Afanasyev90164962014-03-06 08:29:59 +0000246 BOOST_CHECK_EQUAL(nRegFailures, 0);
247 BOOST_CHECK_EQUAL(nInInterests, 1);
248 BOOST_CHECK_EQUAL(nInInterests2, 0);
249 BOOST_CHECK_EQUAL(nTimeouts, 1);
250 BOOST_CHECK_EQUAL(nData, 0);
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800251}
252
Alexander Afanasyev90164962014-03-06 08:29:59 +0000253BOOST_AUTO_TEST_CASE(SetRegexFilterError)
254{
255 Face face;
256 Face face2(face.getIoService());
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700257 Scheduler scheduler(face.getIoService());
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800258 scheduler.scheduleEvent(time::seconds(4),
Alexander Afanasyev90164962014-03-06 08:29:59 +0000259 bind(&FacesFixture::terminate, this, ref(face)));
260
261 regPrefixId = face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
262 bind(&FacesFixture::onInterestRegexError, this,
263 ref(face), _1, _2),
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700264 RegisterPrefixSuccessCallback(),
Alexander Afanasyev90164962014-03-06 08:29:59 +0000265 bind(&FacesFixture::onRegFailed, this));
266
267 scheduler.scheduleEvent(time::milliseconds(300),
268 bind(&FacesFixture::expressInterest, this,
269 ref(face2), Name("/Hello/World/XXX/b/c"))); // should match
270
271 BOOST_REQUIRE_THROW(face.processEvents(), InterestFilter::Error);
272}
273
274BOOST_AUTO_TEST_CASE(SetRegexFilter)
275{
276 Face face;
277 Face face2(face.getIoService());
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700278 Scheduler scheduler(face.getIoService());
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800279 scheduler.scheduleEvent(time::seconds(4),
Alexander Afanasyev90164962014-03-06 08:29:59 +0000280 bind(&FacesFixture::terminate, this, ref(face)));
281
282 regPrefixId = face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
283 bind(&FacesFixture::onInterestRegex, this,
284 ref(face), _1, _2),
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700285 RegisterPrefixSuccessCallback(),
Alexander Afanasyev90164962014-03-06 08:29:59 +0000286 bind(&FacesFixture::onRegFailed, this));
287
288 scheduler.scheduleEvent(time::milliseconds(200),
289 bind(&FacesFixture::expressInterest, this,
290 ref(face2), Name("/Hello/World/a"))); // shouldn't match
291
292 scheduler.scheduleEvent(time::milliseconds(300),
293 bind(&FacesFixture::expressInterest, this,
294 ref(face2), Name("/Hello/World/a/b"))); // should match
295
296 scheduler.scheduleEvent(time::milliseconds(400),
297 bind(&FacesFixture::expressInterest, this,
298 ref(face2), Name("/Hello/World/a/b/c"))); // should match
299
300 scheduler.scheduleEvent(time::milliseconds(500),
301 bind(&FacesFixture::expressInterest, this,
302 ref(face2), Name("/Hello/World/a/b/d"))); // should not match
303
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700304 BOOST_REQUIRE_NO_THROW(face.processEvents());
Alexander Afanasyev90164962014-03-06 08:29:59 +0000305
306 BOOST_CHECK_EQUAL(nRegFailures, 0);
307 BOOST_CHECK_EQUAL(nInInterests, 2);
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700308 BOOST_CHECK_EQUAL(nTimeouts, 4);
Alexander Afanasyev90164962014-03-06 08:29:59 +0000309 BOOST_CHECK_EQUAL(nData, 0);
310}
311
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700312class FacesFixture2 : public FacesFixture
313{
314public:
315 void
316 checkPrefix(bool doesExist)
317 {
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800318 int result = std::system("nfd-status -r | grep /Hello/World >/dev/null");
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700319
320 if (doesExist) {
321 BOOST_CHECK_EQUAL(result, 0);
322 }
323 else {
324 BOOST_CHECK_NE(result, 0);
325 }
326 }
327};
328
329BOOST_FIXTURE_TEST_CASE(RegisterUnregisterPrefix, FacesFixture2)
330{
331 Face face;
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700332 Scheduler scheduler(face.getIoService());
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800333 scheduler.scheduleEvent(time::seconds(4),
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700334 bind(&FacesFixture::terminate, this, ref(face)));
335
336 regPrefixId = face.setInterestFilter(InterestFilter("/Hello/World"),
337 bind(&FacesFixture::onInterest, this,
338 ref(face), _1, _2),
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700339 RegisterPrefixSuccessCallback(),
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700340 bind(&FacesFixture::onRegFailed, this));
341
342 scheduler.scheduleEvent(time::milliseconds(500),
343 bind(&FacesFixture2::checkPrefix, this, true));
344
345 scheduler.scheduleEvent(time::seconds(1),
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700346 bind(static_cast<void(Face::*)(const RegisteredPrefixId*)>(&Face::unsetInterestFilter),
347 &face,
348 regPrefixId)); // shouldn't match
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700349
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800350 scheduler.scheduleEvent(time::milliseconds(2000),
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700351 bind(&FacesFixture2::checkPrefix, this, false));
352
353 BOOST_REQUIRE_NO_THROW(face.processEvents());
354}
Alexander Afanasyev90164962014-03-06 08:29:59 +0000355
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700356
357class FacesFixture3 : public FacesFixture2
358{
359public:
360 FacesFixture3()
361 : nRegSuccesses(0)
362 , nUnregSuccesses(0)
363 , nUnregFailures(0)
364 {
365 }
366
367 void
368 onRegSucceeded()
369 {
370 ++nRegSuccesses;
371 }
372
373 void
374 onUnregSucceeded()
375 {
376 ++nUnregSuccesses;
377 }
378
379 void
380 onUnregFailed()
381 {
382 ++nUnregFailures;
383 }
384
385public:
386 uint64_t nRegSuccesses;
387 uint64_t nUnregSuccesses;
388 uint64_t nUnregFailures;
389};
390
391BOOST_FIXTURE_TEST_CASE(RegisterPrefix, FacesFixture3)
392{
393 Face face;
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700394 Face face2(face.getIoService());
395 Scheduler scheduler(face.getIoService());
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700396 scheduler.scheduleEvent(time::seconds(2),
397 bind(&FacesFixture::terminate, this, ref(face)));
398
399 scheduler.scheduleEvent(time::milliseconds(500),
400 bind(&FacesFixture2::checkPrefix, this, true));
401
402 regPrefixId = face.registerPrefix("/Hello/World",
403 bind(&FacesFixture3::onRegSucceeded, this),
404 bind(&FacesFixture3::onRegFailed, this));
405
406 scheduler.scheduleEvent(time::seconds(1),
407 bind(&Face::unregisterPrefix, &face,
408 regPrefixId,
409 static_cast<UnregisterPrefixSuccessCallback>(bind(&FacesFixture3::onUnregSucceeded, this)),
410 static_cast<UnregisterPrefixFailureCallback>(bind(&FacesFixture3::onUnregFailed, this))));
411
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800412 scheduler.scheduleEvent(time::milliseconds(2500),
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700413 bind(&FacesFixture2::checkPrefix, this, false));
414
415 BOOST_REQUIRE_NO_THROW(face.processEvents());
416
417 BOOST_CHECK_EQUAL(nRegFailures, 0);
418 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
419
420 BOOST_CHECK_EQUAL(nUnregFailures, 0);
421 BOOST_CHECK_EQUAL(nUnregSuccesses, 1);
422}
423
424BOOST_AUTO_TEST_CASE(SetRegexFilterButNoRegister)
425{
426 Face face;
427 Face face2(face.getIoService());
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700428 Scheduler scheduler(face.getIoService());
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700429 scheduler.scheduleEvent(time::seconds(2),
430 bind(&FacesFixture::terminate, this, ref(face)));
431
432 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
433 bind(&FacesFixture::onInterestRegex, this,
434 ref(face), _1, _2));
435
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700436 // prefix is not registered, and also does not match regex
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700437 scheduler.scheduleEvent(time::milliseconds(200),
438 bind(&FacesFixture::expressInterest, this,
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700439 ref(face2), Name("/Hello/World/a")));
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700440
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700441 // matches regex, but prefix is not registered
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700442 scheduler.scheduleEvent(time::milliseconds(300),
443 bind(&FacesFixture::expressInterest, this,
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700444 ref(face2), Name("/Hello/World/a/b")));
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700445
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700446 // matches regex, but prefix is not registered
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700447 scheduler.scheduleEvent(time::milliseconds(400),
448 bind(&FacesFixture::expressInterest, this,
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700449 ref(face2), Name("/Hello/World/a/b/c")));
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700450
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700451 // prefix is not registered, and also does not match regex
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700452 scheduler.scheduleEvent(time::milliseconds(500),
453 bind(&FacesFixture::expressInterest, this,
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700454 ref(face2), Name("/Hello/World/a/b/d")));
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700455
456 BOOST_REQUIRE_NO_THROW(face.processEvents());
457
458 BOOST_CHECK_EQUAL(nRegFailures, 0);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700459 BOOST_CHECK_EQUAL(nInInterests, 0);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700460 BOOST_CHECK_EQUAL(nTimeouts, 4);
461 BOOST_CHECK_EQUAL(nData, 0);
462}
463
464
465BOOST_FIXTURE_TEST_CASE(SetRegexFilterAndRegister, FacesFixture3)
466{
467 Face face;
468 Face face2(face.getIoService());
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700469 Scheduler scheduler(face.getIoService());
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700470 scheduler.scheduleEvent(time::seconds(2),
471 bind(&FacesFixture::terminate, this, ref(face)));
472
473 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
474 bind(&FacesFixture::onInterestRegex, this,
475 ref(face), _1, _2));
476
477 face.registerPrefix("/Hello/World",
478 bind(&FacesFixture3::onRegSucceeded, this),
479 bind(&FacesFixture3::onRegFailed, this));
480
481 scheduler.scheduleEvent(time::milliseconds(200),
482 bind(&FacesFixture::expressInterest, this,
483 ref(face2), Name("/Hello/World/a"))); // shouldn't match
484
485 scheduler.scheduleEvent(time::milliseconds(300),
486 bind(&FacesFixture::expressInterest, this,
487 ref(face2), Name("/Hello/World/a/b"))); // should match
488
489 scheduler.scheduleEvent(time::milliseconds(400),
490 bind(&FacesFixture::expressInterest, this,
491 ref(face2), Name("/Hello/World/a/b/c"))); // should match
492
493 scheduler.scheduleEvent(time::milliseconds(500),
494 bind(&FacesFixture::expressInterest, this,
495 ref(face2), Name("/Hello/World/a/b/d"))); // should not match
496
497 BOOST_REQUIRE_NO_THROW(face.processEvents());
498
499 BOOST_CHECK_EQUAL(nRegFailures, 0);
500 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
501
502 BOOST_CHECK_EQUAL(nInInterests, 2);
503 BOOST_CHECK_EQUAL(nTimeouts, 4);
504}
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400505
Alexander Afanasyevfba1ac62015-08-26 15:19:13 -0700506BOOST_AUTO_TEST_CASE(ShutdownWhileSendInProgress) // Bug #3136
507{
508 Face face;
509 face.expressInterest(Name("/Hello/World/!"), bind([]{}), bind([]{}));
510 BOOST_REQUIRE_NO_THROW(face.processEvents(time::seconds(1)));
511
512 face.expressInterest(Name("/Bye/World/1"), bind([]{}), bind([]{}));
513 face.expressInterest(Name("/Bye/World/2"), bind([]{}), bind([]{}));
514 face.expressInterest(Name("/Bye/World/3"), bind([]{}), bind([]{}));
515 face.shutdown();
516
517 BOOST_REQUIRE_NO_THROW(face.processEvents(time::seconds(1)));
518 // should not segfault
519}
520
Alexander Afanasyeve508f142015-09-01 15:14:45 -0700521BOOST_AUTO_TEST_CASE(LargeDelayBetweenFaceConstructorAndProcessEvents) // Bug #2742
522{
523 ndn::Face face;
524
525 ::sleep(5); // simulate setup workload
526
527 BOOST_CHECK_NO_THROW(face.processEvents(time::seconds(1)));
528}
529
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800530BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800531
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700532} // namespace tests
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800533} // namespace ndn