blob: 3ef7297a3d135395d5235af802e58d30f15d3e74 [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
Junxiao Shicf9c6bb2016-07-27 02:18:19 +000034#include <stdio.h>
35
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080036namespace ndn {
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040037namespace tests {
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080038
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070039struct PibDirWithDefaultTpm
40{
41 const std::string PATH = "build/keys-with-default-tpm";
42};
43
44class FacesFixture : public IdentityManagementFixture,
45 public PibDirFixture<PibDirWithDefaultTpm>
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080046{
Alexander Afanasyev90164962014-03-06 08:29:59 +000047public:
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080048 FacesFixture()
Alexander Afanasyev90164962014-03-06 08:29:59 +000049 : nData(0)
50 , nTimeouts(0)
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080051 , regPrefixId(0)
Alexander Afanasyev90164962014-03-06 08:29:59 +000052 , nInInterests(0)
53 , nInInterests2(0)
54 , nRegFailures(0)
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080055 {
56 }
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070057
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080058 void
59 onData()
60 {
Alexander Afanasyev90164962014-03-06 08:29:59 +000061 ++nData;
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080062 }
63
64 void
65 onTimeout()
66 {
Alexander Afanasyev90164962014-03-06 08:29:59 +000067 ++nTimeouts;
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080068 }
69
70 void
Alexander Afanasyev90164962014-03-06 08:29:59 +000071 onInterest(Face& face,
72 const Name&, const Interest&)
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080073 {
Alexander Afanasyev90164962014-03-06 08:29:59 +000074 ++nInInterests;
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080075
76 face.unsetInterestFilter(regPrefixId);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080077 }
78
79 void
Alexander Afanasyev90164962014-03-06 08:29:59 +000080 onInterest2(Face& face,
81 const Name&, const Interest&)
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -080082 {
Alexander Afanasyev90164962014-03-06 08:29:59 +000083 ++nInInterests2;
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -080084
85 face.unsetInterestFilter(regPrefixId2);
86 }
87
88 void
Alexander Afanasyev90164962014-03-06 08:29:59 +000089 onInterestRegex(Face& face,
90 const InterestFilter&, const Interest&)
91 {
92 ++nInInterests;
93 }
94
95 void
96 onInterestRegexError(Face& face,
97 const Name&, const Interest&)
98 {
99 BOOST_FAIL("InterestFilter::Error should have been triggered");
100 }
101
102 void
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800103 onRegFailed()
104 {
Alexander Afanasyev90164962014-03-06 08:29:59 +0000105 ++nRegFailures;
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800106 }
107
108 void
109 expressInterest(Face& face, const Name& name)
110 {
111 Interest i(name);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700112 i.setInterestLifetime(time::milliseconds(50));
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800113 face.expressInterest(i,
114 bind(&FacesFixture::onData, this),
115 bind(&FacesFixture::onTimeout, this));
116 }
117
118 void
119 terminate(Face& face)
120 {
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800121 face.getIoService().stop();
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800122 }
123
Alexander Afanasyev90164962014-03-06 08:29:59 +0000124 uint32_t nData;
125 uint32_t nTimeouts;
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800126
127 const RegisteredPrefixId* regPrefixId;
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800128 const RegisteredPrefixId* regPrefixId2;
Alexander Afanasyev90164962014-03-06 08:29:59 +0000129 uint32_t nInInterests;
130 uint32_t nInInterests2;
131 uint32_t nRegFailures;
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800132};
133
Alexander Afanasyev90164962014-03-06 08:29:59 +0000134BOOST_FIXTURE_TEST_SUITE(TestFaces, FacesFixture)
135
136BOOST_AUTO_TEST_CASE(Unix)
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800137{
138 Face face;
139
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700140 face.expressInterest(Interest("/", time::milliseconds(1000)),
141 bind(&FacesFixture::onData, this),
142 bind(&FacesFixture::onTimeout, this));
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800143
144 BOOST_REQUIRE_NO_THROW(face.processEvents());
145
Alexander Afanasyev90164962014-03-06 08:29:59 +0000146 BOOST_CHECK_EQUAL(nData, 1);
147 BOOST_CHECK_EQUAL(nTimeouts, 0);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800148
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700149 face.expressInterest(Interest("/localhost/non-existing/data/should/not/exist/anywhere",
150 time::milliseconds(50)),
151 bind(&FacesFixture::onData, this),
152 bind(&FacesFixture::onTimeout, this));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700153
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700154 Name veryLongName;
155 for (size_t i = 0; i <= MAX_NDN_PACKET_SIZE / 10; i++)
156 {
157 veryLongName.append("0123456789");
158 }
159
160 BOOST_CHECK_THROW(face.expressInterest(veryLongName, OnData(), OnTimeout()), Face::Error);
161
162 shared_ptr<Data> data = make_shared<Data>(veryLongName);
163 data->setContent(reinterpret_cast<const uint8_t*>("01234567890"), 10);
Yingdi Yu41546342014-11-30 23:37:53 -0800164 m_keyChain.sign(*data);
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700165 BOOST_CHECK_THROW(face.put(*data), Face::Error);
166
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800167 BOOST_REQUIRE_NO_THROW(face.processEvents());
168
Alexander Afanasyev90164962014-03-06 08:29:59 +0000169 BOOST_CHECK_EQUAL(nData, 1);
170 BOOST_CHECK_EQUAL(nTimeouts, 1);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800171}
172
Alexander Afanasyev90164962014-03-06 08:29:59 +0000173BOOST_AUTO_TEST_CASE(Tcp)
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800174{
175 Face face("localhost");
176
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700177 face.expressInterest(Interest("/", time::milliseconds(1000)),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800178 bind(&FacesFixture::onData, this),
179 bind(&FacesFixture::onTimeout, this));
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800180
181 BOOST_REQUIRE_NO_THROW(face.processEvents());
182
Alexander Afanasyev90164962014-03-06 08:29:59 +0000183 BOOST_CHECK_EQUAL(nData, 1);
184 BOOST_CHECK_EQUAL(nTimeouts, 0);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800185
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700186 face.expressInterest(Interest("/localhost/non-existing/data/should/not/exist/anywhere",
187 time::milliseconds(50)),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800188 bind(&FacesFixture::onData, this),
189 bind(&FacesFixture::onTimeout, this));
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800190
191 BOOST_REQUIRE_NO_THROW(face.processEvents());
192
Alexander Afanasyev90164962014-03-06 08:29:59 +0000193 BOOST_CHECK_EQUAL(nData, 1);
194 BOOST_CHECK_EQUAL(nTimeouts, 1);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800195}
196
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800197
Alexander Afanasyev90164962014-03-06 08:29:59 +0000198BOOST_AUTO_TEST_CASE(SetFilter)
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800199{
200 Face face;
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700201 Face face2(face.getIoService());
202 Scheduler scheduler(face.getIoService());
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800203 scheduler.scheduleEvent(time::seconds(4),
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700204 bind(&FacesFixture::terminate, this, ref(face)));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700205
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800206 regPrefixId = face.setInterestFilter("/Hello/World",
Alexander Afanasyev90164962014-03-06 08:29:59 +0000207 bind(&FacesFixture::onInterest, this, ref(face), _1, _2),
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700208 RegisterPrefixSuccessCallback(),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800209 bind(&FacesFixture::onRegFailed, this));
210
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700211 scheduler.scheduleEvent(time::milliseconds(200),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800212 bind(&FacesFixture::expressInterest, this,
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700213 ref(face2), Name("/Hello/World/!")));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700214
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800215 BOOST_REQUIRE_NO_THROW(face.processEvents());
216
Alexander Afanasyev90164962014-03-06 08:29:59 +0000217 BOOST_CHECK_EQUAL(nRegFailures, 0);
218 BOOST_CHECK_EQUAL(nInInterests, 1);
219 BOOST_CHECK_EQUAL(nTimeouts, 1);
220 BOOST_CHECK_EQUAL(nData, 0);
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800221}
222
Alexander Afanasyev90164962014-03-06 08:29:59 +0000223BOOST_AUTO_TEST_CASE(SetTwoFilters)
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800224{
225 Face face;
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700226 Face face2(face.getIoService());
227 Scheduler scheduler(face.getIoService());
Obaid6e7f5f12014-03-11 14:46:10 -0500228 scheduler.scheduleEvent(time::seconds(1),
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700229 bind(&FacesFixture::terminate, this, ref(face)));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700230
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800231 regPrefixId = face.setInterestFilter("/Hello/World",
Alexander Afanasyev90164962014-03-06 08:29:59 +0000232 bind(&FacesFixture::onInterest, this, ref(face), _1, _2),
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700233 RegisterPrefixSuccessCallback(),
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800234 bind(&FacesFixture::onRegFailed, this));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700235
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800236 regPrefixId2 = face.setInterestFilter("/Los/Angeles/Lakers",
Alexander Afanasyev90164962014-03-06 08:29:59 +0000237 bind(&FacesFixture::onInterest2, this, ref(face), _1, _2),
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700238 RegisterPrefixSuccessCallback(),
Alexander Afanasyev90164962014-03-06 08:29:59 +0000239 bind(&FacesFixture::onRegFailed, this));
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800240
241
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700242 scheduler.scheduleEvent(time::milliseconds(200),
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800243 bind(&FacesFixture::expressInterest, this,
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700244 ref(face2), Name("/Hello/World/!")));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700245
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800246 BOOST_REQUIRE_NO_THROW(face.processEvents());
247
Alexander Afanasyev90164962014-03-06 08:29:59 +0000248 BOOST_CHECK_EQUAL(nRegFailures, 0);
249 BOOST_CHECK_EQUAL(nInInterests, 1);
250 BOOST_CHECK_EQUAL(nInInterests2, 0);
251 BOOST_CHECK_EQUAL(nTimeouts, 1);
252 BOOST_CHECK_EQUAL(nData, 0);
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800253}
254
Alexander Afanasyev90164962014-03-06 08:29:59 +0000255BOOST_AUTO_TEST_CASE(SetRegexFilterError)
256{
257 Face face;
258 Face face2(face.getIoService());
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700259 Scheduler scheduler(face.getIoService());
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800260 scheduler.scheduleEvent(time::seconds(4),
Alexander Afanasyev90164962014-03-06 08:29:59 +0000261 bind(&FacesFixture::terminate, this, ref(face)));
262
263 regPrefixId = face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
264 bind(&FacesFixture::onInterestRegexError, this,
265 ref(face), _1, _2),
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700266 RegisterPrefixSuccessCallback(),
Alexander Afanasyev90164962014-03-06 08:29:59 +0000267 bind(&FacesFixture::onRegFailed, this));
268
269 scheduler.scheduleEvent(time::milliseconds(300),
270 bind(&FacesFixture::expressInterest, this,
271 ref(face2), Name("/Hello/World/XXX/b/c"))); // should match
272
273 BOOST_REQUIRE_THROW(face.processEvents(), InterestFilter::Error);
274}
275
276BOOST_AUTO_TEST_CASE(SetRegexFilter)
277{
278 Face face;
279 Face face2(face.getIoService());
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700280 Scheduler scheduler(face.getIoService());
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800281 scheduler.scheduleEvent(time::seconds(4),
Alexander Afanasyev90164962014-03-06 08:29:59 +0000282 bind(&FacesFixture::terminate, this, ref(face)));
283
284 regPrefixId = face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
285 bind(&FacesFixture::onInterestRegex, this,
286 ref(face), _1, _2),
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700287 RegisterPrefixSuccessCallback(),
Alexander Afanasyev90164962014-03-06 08:29:59 +0000288 bind(&FacesFixture::onRegFailed, this));
289
290 scheduler.scheduleEvent(time::milliseconds(200),
291 bind(&FacesFixture::expressInterest, this,
292 ref(face2), Name("/Hello/World/a"))); // shouldn't match
293
294 scheduler.scheduleEvent(time::milliseconds(300),
295 bind(&FacesFixture::expressInterest, this,
296 ref(face2), Name("/Hello/World/a/b"))); // should match
297
298 scheduler.scheduleEvent(time::milliseconds(400),
299 bind(&FacesFixture::expressInterest, this,
300 ref(face2), Name("/Hello/World/a/b/c"))); // should match
301
302 scheduler.scheduleEvent(time::milliseconds(500),
303 bind(&FacesFixture::expressInterest, this,
304 ref(face2), Name("/Hello/World/a/b/d"))); // should not match
305
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700306 BOOST_REQUIRE_NO_THROW(face.processEvents());
Alexander Afanasyev90164962014-03-06 08:29:59 +0000307
308 BOOST_CHECK_EQUAL(nRegFailures, 0);
309 BOOST_CHECK_EQUAL(nInInterests, 2);
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700310 BOOST_CHECK_EQUAL(nTimeouts, 4);
Alexander Afanasyev90164962014-03-06 08:29:59 +0000311 BOOST_CHECK_EQUAL(nData, 0);
312}
313
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700314class FacesFixture2 : public FacesFixture
315{
316public:
317 void
Junxiao Shicf9c6bb2016-07-27 02:18:19 +0000318 checkPrefix(bool shouldExist)
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700319 {
Junxiao Shicf9c6bb2016-07-27 02:18:19 +0000320 // Boost.Test fails if a child process exits with non-zero.
321 // http://stackoverflow.com/q/5325202
322 std::string output = this->executeCommand("nfd-status -r | grep /Hello/World || true");
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700323
Junxiao Shicf9c6bb2016-07-27 02:18:19 +0000324 if (shouldExist) {
325 BOOST_CHECK_NE(output.size(), 0);
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700326 }
327 else {
Junxiao Shicf9c6bb2016-07-27 02:18:19 +0000328 BOOST_CHECK_EQUAL(output.size(), 0);
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700329 }
330 }
Junxiao Shicf9c6bb2016-07-27 02:18:19 +0000331
332protected:
333 std::string
334 executeCommand(const std::string& cmd)
335 {
336 std::string output;
337 char buf[256];
338 FILE* pipe = popen(cmd.c_str(), "r");
339 BOOST_REQUIRE_MESSAGE(pipe != nullptr, "cannot execute '" << cmd << "'");
340 while (fgets(buf, sizeof(buf), pipe) != nullptr) {
341 output += buf;
342 }
343 pclose(pipe);
344 return output;
345 }
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700346};
347
348BOOST_FIXTURE_TEST_CASE(RegisterUnregisterPrefix, FacesFixture2)
349{
350 Face face;
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700351 Scheduler scheduler(face.getIoService());
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800352 scheduler.scheduleEvent(time::seconds(4),
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700353 bind(&FacesFixture::terminate, this, ref(face)));
354
355 regPrefixId = face.setInterestFilter(InterestFilter("/Hello/World"),
356 bind(&FacesFixture::onInterest, this,
357 ref(face), _1, _2),
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700358 RegisterPrefixSuccessCallback(),
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700359 bind(&FacesFixture::onRegFailed, this));
360
361 scheduler.scheduleEvent(time::milliseconds(500),
362 bind(&FacesFixture2::checkPrefix, this, true));
363
364 scheduler.scheduleEvent(time::seconds(1),
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700365 bind(static_cast<void(Face::*)(const RegisteredPrefixId*)>(&Face::unsetInterestFilter),
366 &face,
367 regPrefixId)); // shouldn't match
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700368
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800369 scheduler.scheduleEvent(time::milliseconds(2000),
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700370 bind(&FacesFixture2::checkPrefix, this, false));
371
372 BOOST_REQUIRE_NO_THROW(face.processEvents());
373}
Alexander Afanasyev90164962014-03-06 08:29:59 +0000374
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700375
376class FacesFixture3 : public FacesFixture2
377{
378public:
379 FacesFixture3()
380 : nRegSuccesses(0)
381 , nUnregSuccesses(0)
382 , nUnregFailures(0)
383 {
384 }
385
386 void
387 onRegSucceeded()
388 {
389 ++nRegSuccesses;
390 }
391
392 void
393 onUnregSucceeded()
394 {
395 ++nUnregSuccesses;
396 }
397
398 void
399 onUnregFailed()
400 {
401 ++nUnregFailures;
402 }
403
404public:
405 uint64_t nRegSuccesses;
406 uint64_t nUnregSuccesses;
407 uint64_t nUnregFailures;
408};
409
410BOOST_FIXTURE_TEST_CASE(RegisterPrefix, FacesFixture3)
411{
412 Face face;
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700413 Face face2(face.getIoService());
414 Scheduler scheduler(face.getIoService());
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700415 scheduler.scheduleEvent(time::seconds(2),
416 bind(&FacesFixture::terminate, this, ref(face)));
417
418 scheduler.scheduleEvent(time::milliseconds(500),
419 bind(&FacesFixture2::checkPrefix, this, true));
420
421 regPrefixId = face.registerPrefix("/Hello/World",
422 bind(&FacesFixture3::onRegSucceeded, this),
423 bind(&FacesFixture3::onRegFailed, this));
424
425 scheduler.scheduleEvent(time::seconds(1),
426 bind(&Face::unregisterPrefix, &face,
427 regPrefixId,
428 static_cast<UnregisterPrefixSuccessCallback>(bind(&FacesFixture3::onUnregSucceeded, this)),
429 static_cast<UnregisterPrefixFailureCallback>(bind(&FacesFixture3::onUnregFailed, this))));
430
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800431 scheduler.scheduleEvent(time::milliseconds(2500),
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700432 bind(&FacesFixture2::checkPrefix, this, false));
433
434 BOOST_REQUIRE_NO_THROW(face.processEvents());
435
436 BOOST_CHECK_EQUAL(nRegFailures, 0);
437 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
438
439 BOOST_CHECK_EQUAL(nUnregFailures, 0);
440 BOOST_CHECK_EQUAL(nUnregSuccesses, 1);
441}
442
443BOOST_AUTO_TEST_CASE(SetRegexFilterButNoRegister)
444{
445 Face face;
446 Face face2(face.getIoService());
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700447 Scheduler scheduler(face.getIoService());
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700448 scheduler.scheduleEvent(time::seconds(2),
449 bind(&FacesFixture::terminate, this, ref(face)));
450
451 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
452 bind(&FacesFixture::onInterestRegex, this,
453 ref(face), _1, _2));
454
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700455 // prefix is not registered, and also does not match regex
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700456 scheduler.scheduleEvent(time::milliseconds(200),
457 bind(&FacesFixture::expressInterest, this,
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700458 ref(face2), Name("/Hello/World/a")));
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700459
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700460 // matches regex, but prefix is not registered
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700461 scheduler.scheduleEvent(time::milliseconds(300),
462 bind(&FacesFixture::expressInterest, this,
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700463 ref(face2), Name("/Hello/World/a/b")));
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700464
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700465 // matches regex, but prefix is not registered
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700466 scheduler.scheduleEvent(time::milliseconds(400),
467 bind(&FacesFixture::expressInterest, this,
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700468 ref(face2), Name("/Hello/World/a/b/c")));
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700469
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700470 // prefix is not registered, and also does not match regex
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700471 scheduler.scheduleEvent(time::milliseconds(500),
472 bind(&FacesFixture::expressInterest, this,
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700473 ref(face2), Name("/Hello/World/a/b/d")));
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700474
475 BOOST_REQUIRE_NO_THROW(face.processEvents());
476
477 BOOST_CHECK_EQUAL(nRegFailures, 0);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700478 BOOST_CHECK_EQUAL(nInInterests, 0);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700479 BOOST_CHECK_EQUAL(nTimeouts, 4);
480 BOOST_CHECK_EQUAL(nData, 0);
481}
482
483
484BOOST_FIXTURE_TEST_CASE(SetRegexFilterAndRegister, FacesFixture3)
485{
486 Face face;
487 Face face2(face.getIoService());
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700488 Scheduler scheduler(face.getIoService());
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700489 scheduler.scheduleEvent(time::seconds(2),
490 bind(&FacesFixture::terminate, this, ref(face)));
491
492 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
493 bind(&FacesFixture::onInterestRegex, this,
494 ref(face), _1, _2));
495
496 face.registerPrefix("/Hello/World",
497 bind(&FacesFixture3::onRegSucceeded, this),
498 bind(&FacesFixture3::onRegFailed, this));
499
500 scheduler.scheduleEvent(time::milliseconds(200),
501 bind(&FacesFixture::expressInterest, this,
502 ref(face2), Name("/Hello/World/a"))); // shouldn't match
503
504 scheduler.scheduleEvent(time::milliseconds(300),
505 bind(&FacesFixture::expressInterest, this,
506 ref(face2), Name("/Hello/World/a/b"))); // should match
507
508 scheduler.scheduleEvent(time::milliseconds(400),
509 bind(&FacesFixture::expressInterest, this,
510 ref(face2), Name("/Hello/World/a/b/c"))); // should match
511
512 scheduler.scheduleEvent(time::milliseconds(500),
513 bind(&FacesFixture::expressInterest, this,
514 ref(face2), Name("/Hello/World/a/b/d"))); // should not match
515
516 BOOST_REQUIRE_NO_THROW(face.processEvents());
517
518 BOOST_CHECK_EQUAL(nRegFailures, 0);
519 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
520
521 BOOST_CHECK_EQUAL(nInInterests, 2);
522 BOOST_CHECK_EQUAL(nTimeouts, 4);
523}
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400524
Alexander Afanasyevfba1ac62015-08-26 15:19:13 -0700525BOOST_AUTO_TEST_CASE(ShutdownWhileSendInProgress) // Bug #3136
526{
527 Face face;
528 face.expressInterest(Name("/Hello/World/!"), bind([]{}), bind([]{}));
529 BOOST_REQUIRE_NO_THROW(face.processEvents(time::seconds(1)));
530
531 face.expressInterest(Name("/Bye/World/1"), bind([]{}), bind([]{}));
532 face.expressInterest(Name("/Bye/World/2"), bind([]{}), bind([]{}));
533 face.expressInterest(Name("/Bye/World/3"), bind([]{}), bind([]{}));
534 face.shutdown();
535
536 BOOST_REQUIRE_NO_THROW(face.processEvents(time::seconds(1)));
537 // should not segfault
538}
539
Alexander Afanasyeve508f142015-09-01 15:14:45 -0700540BOOST_AUTO_TEST_CASE(LargeDelayBetweenFaceConstructorAndProcessEvents) // Bug #2742
541{
542 ndn::Face face;
543
544 ::sleep(5); // simulate setup workload
545
546 BOOST_CHECK_NO_THROW(face.processEvents(time::seconds(1)));
547}
548
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800549BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800550
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700551} // namespace tests
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800552} // namespace ndn