blob: 114544f939595f4673203b7618fef406c29227cc [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)
Eric Newberry06690612016-12-27 12:50:15 -070050 , nNacks(0)
Alexander Afanasyev90164962014-03-06 08:29:59 +000051 , nTimeouts(0)
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080052 , regPrefixId(0)
Alexander Afanasyev90164962014-03-06 08:29:59 +000053 , nInInterests(0)
54 , nInInterests2(0)
55 , nRegFailures(0)
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080056 {
57 }
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070058
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080059 void
60 onData()
61 {
Alexander Afanasyev90164962014-03-06 08:29:59 +000062 ++nData;
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080063 }
64
65 void
Eric Newberry06690612016-12-27 12:50:15 -070066 onNack()
67 {
68 ++nNacks;
69 }
70
71 void
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080072 onTimeout()
73 {
Alexander Afanasyev90164962014-03-06 08:29:59 +000074 ++nTimeouts;
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080075 }
76
77 void
Alexander Afanasyev90164962014-03-06 08:29:59 +000078 onInterest(Face& face,
79 const Name&, const Interest&)
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080080 {
Alexander Afanasyev90164962014-03-06 08:29:59 +000081 ++nInInterests;
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080082
83 face.unsetInterestFilter(regPrefixId);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080084 }
85
86 void
Alexander Afanasyev90164962014-03-06 08:29:59 +000087 onInterest2(Face& face,
88 const Name&, const Interest&)
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -080089 {
Alexander Afanasyev90164962014-03-06 08:29:59 +000090 ++nInInterests2;
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -080091
92 face.unsetInterestFilter(regPrefixId2);
93 }
94
95 void
Alexander Afanasyev90164962014-03-06 08:29:59 +000096 onInterestRegex(Face& face,
97 const InterestFilter&, const Interest&)
98 {
99 ++nInInterests;
100 }
101
102 void
103 onInterestRegexError(Face& face,
104 const Name&, const Interest&)
105 {
106 BOOST_FAIL("InterestFilter::Error should have been triggered");
107 }
108
109 void
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800110 onRegFailed()
111 {
Alexander Afanasyev90164962014-03-06 08:29:59 +0000112 ++nRegFailures;
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800113 }
114
115 void
116 expressInterest(Face& face, const Name& name)
117 {
118 Interest i(name);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700119 i.setInterestLifetime(time::milliseconds(50));
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800120 face.expressInterest(i,
121 bind(&FacesFixture::onData, this),
Eric Newberry06690612016-12-27 12:50:15 -0700122 bind(&FacesFixture::onNack, this),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800123 bind(&FacesFixture::onTimeout, this));
124 }
125
126 void
127 terminate(Face& face)
128 {
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800129 face.getIoService().stop();
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800130 }
131
Alexander Afanasyev90164962014-03-06 08:29:59 +0000132 uint32_t nData;
Eric Newberry06690612016-12-27 12:50:15 -0700133 uint32_t nNacks;
Alexander Afanasyev90164962014-03-06 08:29:59 +0000134 uint32_t nTimeouts;
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800135
136 const RegisteredPrefixId* regPrefixId;
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800137 const RegisteredPrefixId* regPrefixId2;
Alexander Afanasyev90164962014-03-06 08:29:59 +0000138 uint32_t nInInterests;
139 uint32_t nInInterests2;
140 uint32_t nRegFailures;
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800141};
142
Alexander Afanasyev90164962014-03-06 08:29:59 +0000143BOOST_FIXTURE_TEST_SUITE(TestFaces, FacesFixture)
144
145BOOST_AUTO_TEST_CASE(Unix)
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800146{
147 Face face;
148
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700149 face.expressInterest(Interest("/", time::milliseconds(1000)),
150 bind(&FacesFixture::onData, this),
Eric Newberry06690612016-12-27 12:50:15 -0700151 bind(&FacesFixture::onNack, this),
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700152 bind(&FacesFixture::onTimeout, this));
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800153
154 BOOST_REQUIRE_NO_THROW(face.processEvents());
155
Alexander Afanasyev90164962014-03-06 08:29:59 +0000156 BOOST_CHECK_EQUAL(nData, 1);
Eric Newberry06690612016-12-27 12:50:15 -0700157 BOOST_CHECK_EQUAL(nNacks, 0);
Alexander Afanasyev90164962014-03-06 08:29:59 +0000158 BOOST_CHECK_EQUAL(nTimeouts, 0);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800159
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700160 face.expressInterest(Interest("/localhost/non-existing/data/should/not/exist/anywhere",
161 time::milliseconds(50)),
162 bind(&FacesFixture::onData, this),
Eric Newberry06690612016-12-27 12:50:15 -0700163 bind(&FacesFixture::onNack, this),
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700164 bind(&FacesFixture::onTimeout, this));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700165
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700166 Name veryLongName;
Eric Newberry06690612016-12-27 12:50:15 -0700167 for (size_t i = 0; i <= MAX_NDN_PACKET_SIZE / 10; i++) {
168 veryLongName.append("0123456789");
169 }
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700170
Eric Newberry06690612016-12-27 12:50:15 -0700171 BOOST_CHECK_THROW(face.expressInterest(veryLongName, nullptr, nullptr, nullptr), Face::Error);
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700172
173 shared_ptr<Data> data = make_shared<Data>(veryLongName);
174 data->setContent(reinterpret_cast<const uint8_t*>("01234567890"), 10);
Yingdi Yu41546342014-11-30 23:37:53 -0800175 m_keyChain.sign(*data);
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700176 BOOST_CHECK_THROW(face.put(*data), Face::Error);
177
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800178 BOOST_REQUIRE_NO_THROW(face.processEvents());
179
Alexander Afanasyev90164962014-03-06 08:29:59 +0000180 BOOST_CHECK_EQUAL(nData, 1);
Eric Newberry06690612016-12-27 12:50:15 -0700181 BOOST_CHECK_EQUAL(nNacks, 0);
Alexander Afanasyev90164962014-03-06 08:29:59 +0000182 BOOST_CHECK_EQUAL(nTimeouts, 1);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800183}
184
Alexander Afanasyev90164962014-03-06 08:29:59 +0000185BOOST_AUTO_TEST_CASE(Tcp)
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800186{
187 Face face("localhost");
188
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700189 face.expressInterest(Interest("/", time::milliseconds(1000)),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800190 bind(&FacesFixture::onData, this),
Eric Newberry06690612016-12-27 12:50:15 -0700191 bind(&FacesFixture::onNack, this),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800192 bind(&FacesFixture::onTimeout, this));
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800193
194 BOOST_REQUIRE_NO_THROW(face.processEvents());
195
Alexander Afanasyev90164962014-03-06 08:29:59 +0000196 BOOST_CHECK_EQUAL(nData, 1);
Eric Newberry06690612016-12-27 12:50:15 -0700197 BOOST_CHECK_EQUAL(nNacks, 0);
Alexander Afanasyev90164962014-03-06 08:29:59 +0000198 BOOST_CHECK_EQUAL(nTimeouts, 0);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800199
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700200 face.expressInterest(Interest("/localhost/non-existing/data/should/not/exist/anywhere",
201 time::milliseconds(50)),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800202 bind(&FacesFixture::onData, this),
Eric Newberry06690612016-12-27 12:50:15 -0700203 bind(&FacesFixture::onNack, this),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800204 bind(&FacesFixture::onTimeout, this));
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800205
206 BOOST_REQUIRE_NO_THROW(face.processEvents());
207
Alexander Afanasyev90164962014-03-06 08:29:59 +0000208 BOOST_CHECK_EQUAL(nData, 1);
Eric Newberry06690612016-12-27 12:50:15 -0700209 BOOST_CHECK_EQUAL(nNacks, 0);
Alexander Afanasyev90164962014-03-06 08:29:59 +0000210 BOOST_CHECK_EQUAL(nTimeouts, 1);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800211}
212
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800213
Alexander Afanasyev90164962014-03-06 08:29:59 +0000214BOOST_AUTO_TEST_CASE(SetFilter)
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800215{
216 Face face;
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700217 Face face2(face.getIoService());
218 Scheduler scheduler(face.getIoService());
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800219 scheduler.scheduleEvent(time::seconds(4),
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700220 bind(&FacesFixture::terminate, this, ref(face)));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700221
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800222 regPrefixId = face.setInterestFilter("/Hello/World",
Alexander Afanasyev90164962014-03-06 08:29:59 +0000223 bind(&FacesFixture::onInterest, this, ref(face), _1, _2),
Eric Newberry06690612016-12-27 12:50:15 -0700224 nullptr,
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800225 bind(&FacesFixture::onRegFailed, this));
226
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700227 scheduler.scheduleEvent(time::milliseconds(200),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800228 bind(&FacesFixture::expressInterest, this,
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700229 ref(face2), Name("/Hello/World/!")));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700230
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800231 BOOST_REQUIRE_NO_THROW(face.processEvents());
232
Alexander Afanasyev90164962014-03-06 08:29:59 +0000233 BOOST_CHECK_EQUAL(nRegFailures, 0);
234 BOOST_CHECK_EQUAL(nInInterests, 1);
235 BOOST_CHECK_EQUAL(nTimeouts, 1);
Eric Newberry06690612016-12-27 12:50:15 -0700236 BOOST_CHECK_EQUAL(nNacks, 0);
Alexander Afanasyev90164962014-03-06 08:29:59 +0000237 BOOST_CHECK_EQUAL(nData, 0);
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800238}
239
Alexander Afanasyev90164962014-03-06 08:29:59 +0000240BOOST_AUTO_TEST_CASE(SetTwoFilters)
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800241{
242 Face face;
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700243 Face face2(face.getIoService());
244 Scheduler scheduler(face.getIoService());
Eric Newberry06690612016-12-27 12:50:15 -0700245 scheduler.scheduleEvent(time::seconds(6),
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700246 bind(&FacesFixture::terminate, this, ref(face)));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700247
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800248 regPrefixId = face.setInterestFilter("/Hello/World",
Alexander Afanasyev90164962014-03-06 08:29:59 +0000249 bind(&FacesFixture::onInterest, this, ref(face), _1, _2),
Eric Newberry06690612016-12-27 12:50:15 -0700250 nullptr,
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800251 bind(&FacesFixture::onRegFailed, this));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700252
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800253 regPrefixId2 = face.setInterestFilter("/Los/Angeles/Lakers",
Alexander Afanasyev90164962014-03-06 08:29:59 +0000254 bind(&FacesFixture::onInterest2, this, ref(face), _1, _2),
Eric Newberry06690612016-12-27 12:50:15 -0700255 nullptr,
Alexander Afanasyev90164962014-03-06 08:29:59 +0000256 bind(&FacesFixture::onRegFailed, this));
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800257
258
Eric Newberry06690612016-12-27 12:50:15 -0700259 scheduler.scheduleEvent(time::seconds(2),
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800260 bind(&FacesFixture::expressInterest, this,
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700261 ref(face2), Name("/Hello/World/!")));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700262
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800263 BOOST_REQUIRE_NO_THROW(face.processEvents());
264
Alexander Afanasyev90164962014-03-06 08:29:59 +0000265 BOOST_CHECK_EQUAL(nRegFailures, 0);
266 BOOST_CHECK_EQUAL(nInInterests, 1);
267 BOOST_CHECK_EQUAL(nInInterests2, 0);
268 BOOST_CHECK_EQUAL(nTimeouts, 1);
Eric Newberry06690612016-12-27 12:50:15 -0700269 BOOST_CHECK_EQUAL(nNacks, 0);
Alexander Afanasyev90164962014-03-06 08:29:59 +0000270 BOOST_CHECK_EQUAL(nData, 0);
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800271}
272
Alexander Afanasyev90164962014-03-06 08:29:59 +0000273BOOST_AUTO_TEST_CASE(SetRegexFilterError)
274{
275 Face face;
276 Face face2(face.getIoService());
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700277 Scheduler scheduler(face.getIoService());
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800278 scheduler.scheduleEvent(time::seconds(4),
Alexander Afanasyev90164962014-03-06 08:29:59 +0000279 bind(&FacesFixture::terminate, this, ref(face)));
280
281 regPrefixId = face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
282 bind(&FacesFixture::onInterestRegexError, this,
283 ref(face), _1, _2),
Eric Newberry06690612016-12-27 12:50:15 -0700284 nullptr,
Alexander Afanasyev90164962014-03-06 08:29:59 +0000285 bind(&FacesFixture::onRegFailed, this));
286
287 scheduler.scheduleEvent(time::milliseconds(300),
288 bind(&FacesFixture::expressInterest, this,
289 ref(face2), Name("/Hello/World/XXX/b/c"))); // should match
290
291 BOOST_REQUIRE_THROW(face.processEvents(), InterestFilter::Error);
292}
293
294BOOST_AUTO_TEST_CASE(SetRegexFilter)
295{
296 Face face;
297 Face face2(face.getIoService());
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700298 Scheduler scheduler(face.getIoService());
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800299 scheduler.scheduleEvent(time::seconds(4),
Alexander Afanasyev90164962014-03-06 08:29:59 +0000300 bind(&FacesFixture::terminate, this, ref(face)));
301
302 regPrefixId = face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
303 bind(&FacesFixture::onInterestRegex, this,
304 ref(face), _1, _2),
Eric Newberry06690612016-12-27 12:50:15 -0700305 nullptr,
Alexander Afanasyev90164962014-03-06 08:29:59 +0000306 bind(&FacesFixture::onRegFailed, this));
307
308 scheduler.scheduleEvent(time::milliseconds(200),
309 bind(&FacesFixture::expressInterest, this,
310 ref(face2), Name("/Hello/World/a"))); // shouldn't match
311
312 scheduler.scheduleEvent(time::milliseconds(300),
313 bind(&FacesFixture::expressInterest, this,
314 ref(face2), Name("/Hello/World/a/b"))); // should match
315
316 scheduler.scheduleEvent(time::milliseconds(400),
317 bind(&FacesFixture::expressInterest, this,
318 ref(face2), Name("/Hello/World/a/b/c"))); // should match
319
320 scheduler.scheduleEvent(time::milliseconds(500),
321 bind(&FacesFixture::expressInterest, this,
322 ref(face2), Name("/Hello/World/a/b/d"))); // should not match
323
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700324 BOOST_REQUIRE_NO_THROW(face.processEvents());
Alexander Afanasyev90164962014-03-06 08:29:59 +0000325
326 BOOST_CHECK_EQUAL(nRegFailures, 0);
327 BOOST_CHECK_EQUAL(nInInterests, 2);
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700328 BOOST_CHECK_EQUAL(nTimeouts, 4);
Eric Newberry06690612016-12-27 12:50:15 -0700329 BOOST_CHECK_EQUAL(nNacks, 0);
Alexander Afanasyev90164962014-03-06 08:29:59 +0000330 BOOST_CHECK_EQUAL(nData, 0);
331}
332
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700333class FacesFixture2 : public FacesFixture
334{
335public:
336 void
Junxiao Shicf9c6bb2016-07-27 02:18:19 +0000337 checkPrefix(bool shouldExist)
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700338 {
Junxiao Shicf9c6bb2016-07-27 02:18:19 +0000339 // Boost.Test fails if a child process exits with non-zero.
340 // http://stackoverflow.com/q/5325202
Eric Newberry06690612016-12-27 12:50:15 -0700341 std::string output = this->executeCommand("nfdc route list | grep /Hello/World || true");
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700342
Junxiao Shicf9c6bb2016-07-27 02:18:19 +0000343 if (shouldExist) {
344 BOOST_CHECK_NE(output.size(), 0);
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700345 }
346 else {
Junxiao Shicf9c6bb2016-07-27 02:18:19 +0000347 BOOST_CHECK_EQUAL(output.size(), 0);
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700348 }
349 }
Junxiao Shicf9c6bb2016-07-27 02:18:19 +0000350
351protected:
352 std::string
353 executeCommand(const std::string& cmd)
354 {
355 std::string output;
356 char buf[256];
357 FILE* pipe = popen(cmd.c_str(), "r");
358 BOOST_REQUIRE_MESSAGE(pipe != nullptr, "cannot execute '" << cmd << "'");
359 while (fgets(buf, sizeof(buf), pipe) != nullptr) {
360 output += buf;
361 }
362 pclose(pipe);
363 return output;
364 }
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700365};
366
367BOOST_FIXTURE_TEST_CASE(RegisterUnregisterPrefix, FacesFixture2)
368{
369 Face face;
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700370 Scheduler scheduler(face.getIoService());
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800371 scheduler.scheduleEvent(time::seconds(4),
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700372 bind(&FacesFixture::terminate, this, ref(face)));
373
374 regPrefixId = face.setInterestFilter(InterestFilter("/Hello/World"),
375 bind(&FacesFixture::onInterest, this,
376 ref(face), _1, _2),
Eric Newberry06690612016-12-27 12:50:15 -0700377 nullptr,
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700378 bind(&FacesFixture::onRegFailed, this));
379
380 scheduler.scheduleEvent(time::milliseconds(500),
381 bind(&FacesFixture2::checkPrefix, this, true));
382
383 scheduler.scheduleEvent(time::seconds(1),
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700384 bind(static_cast<void(Face::*)(const RegisteredPrefixId*)>(&Face::unsetInterestFilter),
385 &face,
386 regPrefixId)); // shouldn't match
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700387
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800388 scheduler.scheduleEvent(time::milliseconds(2000),
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700389 bind(&FacesFixture2::checkPrefix, this, false));
390
391 BOOST_REQUIRE_NO_THROW(face.processEvents());
392}
Alexander Afanasyev90164962014-03-06 08:29:59 +0000393
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700394
395class FacesFixture3 : public FacesFixture2
396{
397public:
398 FacesFixture3()
399 : nRegSuccesses(0)
400 , nUnregSuccesses(0)
401 , nUnregFailures(0)
402 {
403 }
404
405 void
406 onRegSucceeded()
407 {
408 ++nRegSuccesses;
409 }
410
411 void
412 onUnregSucceeded()
413 {
414 ++nUnregSuccesses;
415 }
416
417 void
418 onUnregFailed()
419 {
420 ++nUnregFailures;
421 }
422
423public:
424 uint64_t nRegSuccesses;
425 uint64_t nUnregSuccesses;
426 uint64_t nUnregFailures;
427};
428
429BOOST_FIXTURE_TEST_CASE(RegisterPrefix, FacesFixture3)
430{
431 Face face;
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700432 Face face2(face.getIoService());
433 Scheduler scheduler(face.getIoService());
Eric Newberry06690612016-12-27 12:50:15 -0700434 scheduler.scheduleEvent(time::seconds(6),
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700435 bind(&FacesFixture::terminate, this, ref(face)));
436
Eric Newberry06690612016-12-27 12:50:15 -0700437 scheduler.scheduleEvent(time::seconds(2),
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700438 bind(&FacesFixture2::checkPrefix, this, true));
439
440 regPrefixId = face.registerPrefix("/Hello/World",
441 bind(&FacesFixture3::onRegSucceeded, this),
442 bind(&FacesFixture3::onRegFailed, this));
443
Eric Newberry06690612016-12-27 12:50:15 -0700444 scheduler.scheduleEvent(time::seconds(4),
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700445 bind(&Face::unregisterPrefix, &face,
446 regPrefixId,
447 static_cast<UnregisterPrefixSuccessCallback>(bind(&FacesFixture3::onUnregSucceeded, this)),
448 static_cast<UnregisterPrefixFailureCallback>(bind(&FacesFixture3::onUnregFailed, this))));
449
Eric Newberry06690612016-12-27 12:50:15 -0700450 scheduler.scheduleEvent(time::milliseconds(6500),
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700451 bind(&FacesFixture2::checkPrefix, this, false));
452
453 BOOST_REQUIRE_NO_THROW(face.processEvents());
454
455 BOOST_CHECK_EQUAL(nRegFailures, 0);
456 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
457
458 BOOST_CHECK_EQUAL(nUnregFailures, 0);
459 BOOST_CHECK_EQUAL(nUnregSuccesses, 1);
460}
461
462BOOST_AUTO_TEST_CASE(SetRegexFilterButNoRegister)
463{
464 Face face;
465 Face face2(face.getIoService());
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700466 Scheduler scheduler(face.getIoService());
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700467 scheduler.scheduleEvent(time::seconds(2),
468 bind(&FacesFixture::terminate, this, ref(face)));
469
470 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
471 bind(&FacesFixture::onInterestRegex, this,
472 ref(face), _1, _2));
473
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700474 // prefix is not registered, and also does not match regex
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700475 scheduler.scheduleEvent(time::milliseconds(200),
476 bind(&FacesFixture::expressInterest, this,
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700477 ref(face2), Name("/Hello/World/a")));
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700478
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700479 // matches regex, but prefix is not registered
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700480 scheduler.scheduleEvent(time::milliseconds(300),
481 bind(&FacesFixture::expressInterest, this,
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700482 ref(face2), Name("/Hello/World/a/b")));
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700483
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700484 // matches regex, but prefix is not registered
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700485 scheduler.scheduleEvent(time::milliseconds(400),
486 bind(&FacesFixture::expressInterest, this,
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700487 ref(face2), Name("/Hello/World/a/b/c")));
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700488
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700489 // prefix is not registered, and also does not match regex
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700490 scheduler.scheduleEvent(time::milliseconds(500),
491 bind(&FacesFixture::expressInterest, this,
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700492 ref(face2), Name("/Hello/World/a/b/d")));
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700493
494 BOOST_REQUIRE_NO_THROW(face.processEvents());
495
496 BOOST_CHECK_EQUAL(nRegFailures, 0);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700497 BOOST_CHECK_EQUAL(nInInterests, 0);
Eric Newberry06690612016-12-27 12:50:15 -0700498 BOOST_CHECK_EQUAL(nTimeouts, 0);
499 BOOST_CHECK_EQUAL(nNacks, 4);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700500 BOOST_CHECK_EQUAL(nData, 0);
501}
502
503
504BOOST_FIXTURE_TEST_CASE(SetRegexFilterAndRegister, FacesFixture3)
505{
506 Face face;
507 Face face2(face.getIoService());
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700508 Scheduler scheduler(face.getIoService());
Eric Newberry06690612016-12-27 12:50:15 -0700509 scheduler.scheduleEvent(time::seconds(4),
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700510 bind(&FacesFixture::terminate, this, ref(face)));
511
512 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
513 bind(&FacesFixture::onInterestRegex, this,
514 ref(face), _1, _2));
515
516 face.registerPrefix("/Hello/World",
517 bind(&FacesFixture3::onRegSucceeded, this),
518 bind(&FacesFixture3::onRegFailed, this));
519
520 scheduler.scheduleEvent(time::milliseconds(200),
521 bind(&FacesFixture::expressInterest, this,
522 ref(face2), Name("/Hello/World/a"))); // shouldn't match
523
524 scheduler.scheduleEvent(time::milliseconds(300),
525 bind(&FacesFixture::expressInterest, this,
526 ref(face2), Name("/Hello/World/a/b"))); // should match
527
528 scheduler.scheduleEvent(time::milliseconds(400),
529 bind(&FacesFixture::expressInterest, this,
530 ref(face2), Name("/Hello/World/a/b/c"))); // should match
531
532 scheduler.scheduleEvent(time::milliseconds(500),
533 bind(&FacesFixture::expressInterest, this,
534 ref(face2), Name("/Hello/World/a/b/d"))); // should not match
535
536 BOOST_REQUIRE_NO_THROW(face.processEvents());
537
538 BOOST_CHECK_EQUAL(nRegFailures, 0);
539 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
540
541 BOOST_CHECK_EQUAL(nInInterests, 2);
542 BOOST_CHECK_EQUAL(nTimeouts, 4);
Eric Newberry06690612016-12-27 12:50:15 -0700543 BOOST_CHECK_EQUAL(nNacks, 0);
544 BOOST_CHECK_EQUAL(nData, 0);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700545}
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400546
Alexander Afanasyevfba1ac62015-08-26 15:19:13 -0700547BOOST_AUTO_TEST_CASE(ShutdownWhileSendInProgress) // Bug #3136
548{
549 Face face;
Eric Newberry06690612016-12-27 12:50:15 -0700550 face.expressInterest(Name("/Hello/World/!"), nullptr, nullptr, nullptr);
Alexander Afanasyevfba1ac62015-08-26 15:19:13 -0700551 BOOST_REQUIRE_NO_THROW(face.processEvents(time::seconds(1)));
552
Eric Newberry06690612016-12-27 12:50:15 -0700553 face.expressInterest(Name("/Bye/World/1"), nullptr, nullptr, nullptr);
554 face.expressInterest(Name("/Bye/World/2"), nullptr, nullptr, nullptr);
555 face.expressInterest(Name("/Bye/World/3"), nullptr, nullptr, nullptr);
Alexander Afanasyevfba1ac62015-08-26 15:19:13 -0700556 face.shutdown();
557
558 BOOST_REQUIRE_NO_THROW(face.processEvents(time::seconds(1)));
559 // should not segfault
560}
561
Alexander Afanasyeve508f142015-09-01 15:14:45 -0700562BOOST_AUTO_TEST_CASE(LargeDelayBetweenFaceConstructorAndProcessEvents) // Bug #2742
563{
564 ndn::Face face;
565
566 ::sleep(5); // simulate setup workload
567
568 BOOST_CHECK_NO_THROW(face.processEvents(time::seconds(1)));
569}
570
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800571BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800572
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700573} // namespace tests
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800574} // namespace ndn