blob: 038b499f1cd9cb9c1e2c2b2fa725bce32fec8c5e [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi909ffef2017-07-07 22:12:27 +00002/*
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -08003 * Copyright (c) 2013-2017 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 Afanasyev20d2c582014-01-26 15:32:51 -080028
Yingdi Yu41546342014-11-30 23:37:53 -080029#include "identity-management-fixture.hpp"
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070030#include "boost-test.hpp"
31
Junxiao Shicf9c6bb2016-07-27 02:18:19 +000032#include <stdio.h>
Alexander Afanasyeva54d5a62017-02-11 19:01:34 -080033#include <thread>
34#include <mutex>
35#include <condition_variable>
Junxiao Shicf9c6bb2016-07-27 02:18:19 +000036
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080037namespace ndn {
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040038namespace tests {
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080039
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070040struct PibDirWithDefaultTpm
41{
42 const std::string PATH = "build/keys-with-default-tpm";
43};
44
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080045class FacesFixture : public IdentityManagementFixture
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
Junxiao Shi909ffef2017-07-07 22:12:27 +0000171 BOOST_CHECK_THROW(face.expressInterest(Interest(veryLongName), nullptr, nullptr, nullptr),
172 Face::Error);
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700173
174 shared_ptr<Data> data = make_shared<Data>(veryLongName);
175 data->setContent(reinterpret_cast<const uint8_t*>("01234567890"), 10);
Yingdi Yu41546342014-11-30 23:37:53 -0800176 m_keyChain.sign(*data);
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700177 BOOST_CHECK_THROW(face.put(*data), Face::Error);
178
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800179 BOOST_REQUIRE_NO_THROW(face.processEvents());
180
Alexander Afanasyev90164962014-03-06 08:29:59 +0000181 BOOST_CHECK_EQUAL(nData, 1);
Eric Newberry06690612016-12-27 12:50:15 -0700182 BOOST_CHECK_EQUAL(nNacks, 0);
Alexander Afanasyev90164962014-03-06 08:29:59 +0000183 BOOST_CHECK_EQUAL(nTimeouts, 1);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800184}
185
Alexander Afanasyev90164962014-03-06 08:29:59 +0000186BOOST_AUTO_TEST_CASE(Tcp)
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800187{
188 Face face("localhost");
189
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700190 face.expressInterest(Interest("/", time::milliseconds(1000)),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800191 bind(&FacesFixture::onData, this),
Eric Newberry06690612016-12-27 12:50:15 -0700192 bind(&FacesFixture::onNack, this),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800193 bind(&FacesFixture::onTimeout, this));
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800194
195 BOOST_REQUIRE_NO_THROW(face.processEvents());
196
Alexander Afanasyev90164962014-03-06 08:29:59 +0000197 BOOST_CHECK_EQUAL(nData, 1);
Eric Newberry06690612016-12-27 12:50:15 -0700198 BOOST_CHECK_EQUAL(nNacks, 0);
Alexander Afanasyev90164962014-03-06 08:29:59 +0000199 BOOST_CHECK_EQUAL(nTimeouts, 0);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800200
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700201 face.expressInterest(Interest("/localhost/non-existing/data/should/not/exist/anywhere",
202 time::milliseconds(50)),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800203 bind(&FacesFixture::onData, this),
Eric Newberry06690612016-12-27 12:50:15 -0700204 bind(&FacesFixture::onNack, this),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800205 bind(&FacesFixture::onTimeout, this));
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800206
207 BOOST_REQUIRE_NO_THROW(face.processEvents());
208
Alexander Afanasyev90164962014-03-06 08:29:59 +0000209 BOOST_CHECK_EQUAL(nData, 1);
Eric Newberry06690612016-12-27 12:50:15 -0700210 BOOST_CHECK_EQUAL(nNacks, 0);
Alexander Afanasyev90164962014-03-06 08:29:59 +0000211 BOOST_CHECK_EQUAL(nTimeouts, 1);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800212}
213
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800214
Alexander Afanasyev90164962014-03-06 08:29:59 +0000215BOOST_AUTO_TEST_CASE(SetFilter)
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800216{
217 Face face;
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700218 Face face2(face.getIoService());
219 Scheduler scheduler(face.getIoService());
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800220 scheduler.scheduleEvent(time::seconds(4),
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700221 bind(&FacesFixture::terminate, this, ref(face)));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700222
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800223 regPrefixId = face.setInterestFilter("/Hello/World",
Alexander Afanasyev90164962014-03-06 08:29:59 +0000224 bind(&FacesFixture::onInterest, this, ref(face), _1, _2),
Eric Newberry06690612016-12-27 12:50:15 -0700225 nullptr,
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800226 bind(&FacesFixture::onRegFailed, this));
227
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700228 scheduler.scheduleEvent(time::milliseconds(200),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800229 bind(&FacesFixture::expressInterest, this,
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700230 ref(face2), Name("/Hello/World/!")));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700231
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800232 BOOST_REQUIRE_NO_THROW(face.processEvents());
233
Alexander Afanasyev90164962014-03-06 08:29:59 +0000234 BOOST_CHECK_EQUAL(nRegFailures, 0);
235 BOOST_CHECK_EQUAL(nInInterests, 1);
236 BOOST_CHECK_EQUAL(nTimeouts, 1);
Eric Newberry06690612016-12-27 12:50:15 -0700237 BOOST_CHECK_EQUAL(nNacks, 0);
Alexander Afanasyev90164962014-03-06 08:29:59 +0000238 BOOST_CHECK_EQUAL(nData, 0);
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800239}
240
Alexander Afanasyev90164962014-03-06 08:29:59 +0000241BOOST_AUTO_TEST_CASE(SetTwoFilters)
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800242{
243 Face face;
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700244 Face face2(face.getIoService());
245 Scheduler scheduler(face.getIoService());
Eric Newberry06690612016-12-27 12:50:15 -0700246 scheduler.scheduleEvent(time::seconds(6),
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700247 bind(&FacesFixture::terminate, this, ref(face)));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700248
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800249 regPrefixId = face.setInterestFilter("/Hello/World",
Alexander Afanasyev90164962014-03-06 08:29:59 +0000250 bind(&FacesFixture::onInterest, this, ref(face), _1, _2),
Eric Newberry06690612016-12-27 12:50:15 -0700251 nullptr,
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800252 bind(&FacesFixture::onRegFailed, this));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700253
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800254 regPrefixId2 = face.setInterestFilter("/Los/Angeles/Lakers",
Alexander Afanasyev90164962014-03-06 08:29:59 +0000255 bind(&FacesFixture::onInterest2, this, ref(face), _1, _2),
Eric Newberry06690612016-12-27 12:50:15 -0700256 nullptr,
Alexander Afanasyev90164962014-03-06 08:29:59 +0000257 bind(&FacesFixture::onRegFailed, this));
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800258
259
Eric Newberry06690612016-12-27 12:50:15 -0700260 scheduler.scheduleEvent(time::seconds(2),
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800261 bind(&FacesFixture::expressInterest, this,
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700262 ref(face2), Name("/Hello/World/!")));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700263
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800264 BOOST_REQUIRE_NO_THROW(face.processEvents());
265
Alexander Afanasyev90164962014-03-06 08:29:59 +0000266 BOOST_CHECK_EQUAL(nRegFailures, 0);
267 BOOST_CHECK_EQUAL(nInInterests, 1);
268 BOOST_CHECK_EQUAL(nInInterests2, 0);
269 BOOST_CHECK_EQUAL(nTimeouts, 1);
Eric Newberry06690612016-12-27 12:50:15 -0700270 BOOST_CHECK_EQUAL(nNacks, 0);
Alexander Afanasyev90164962014-03-06 08:29:59 +0000271 BOOST_CHECK_EQUAL(nData, 0);
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800272}
273
Alexander Afanasyev90164962014-03-06 08:29:59 +0000274BOOST_AUTO_TEST_CASE(SetRegexFilterError)
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::onInterestRegexError, this,
284 ref(face), _1, _2),
Eric Newberry06690612016-12-27 12:50:15 -0700285 nullptr,
Alexander Afanasyev90164962014-03-06 08:29:59 +0000286 bind(&FacesFixture::onRegFailed, this));
287
288 scheduler.scheduleEvent(time::milliseconds(300),
289 bind(&FacesFixture::expressInterest, this,
290 ref(face2), Name("/Hello/World/XXX/b/c"))); // should match
291
292 BOOST_REQUIRE_THROW(face.processEvents(), InterestFilter::Error);
293}
294
295BOOST_AUTO_TEST_CASE(SetRegexFilter)
296{
297 Face face;
298 Face face2(face.getIoService());
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700299 Scheduler scheduler(face.getIoService());
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800300 scheduler.scheduleEvent(time::seconds(4),
Alexander Afanasyev90164962014-03-06 08:29:59 +0000301 bind(&FacesFixture::terminate, this, ref(face)));
302
303 regPrefixId = face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
304 bind(&FacesFixture::onInterestRegex, this,
305 ref(face), _1, _2),
Eric Newberry06690612016-12-27 12:50:15 -0700306 nullptr,
Alexander Afanasyev90164962014-03-06 08:29:59 +0000307 bind(&FacesFixture::onRegFailed, this));
308
309 scheduler.scheduleEvent(time::milliseconds(200),
310 bind(&FacesFixture::expressInterest, this,
311 ref(face2), Name("/Hello/World/a"))); // shouldn't match
312
313 scheduler.scheduleEvent(time::milliseconds(300),
314 bind(&FacesFixture::expressInterest, this,
315 ref(face2), Name("/Hello/World/a/b"))); // should match
316
317 scheduler.scheduleEvent(time::milliseconds(400),
318 bind(&FacesFixture::expressInterest, this,
319 ref(face2), Name("/Hello/World/a/b/c"))); // should match
320
321 scheduler.scheduleEvent(time::milliseconds(500),
322 bind(&FacesFixture::expressInterest, this,
323 ref(face2), Name("/Hello/World/a/b/d"))); // should not match
324
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700325 BOOST_REQUIRE_NO_THROW(face.processEvents());
Alexander Afanasyev90164962014-03-06 08:29:59 +0000326
327 BOOST_CHECK_EQUAL(nRegFailures, 0);
328 BOOST_CHECK_EQUAL(nInInterests, 2);
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700329 BOOST_CHECK_EQUAL(nTimeouts, 4);
Eric Newberry06690612016-12-27 12:50:15 -0700330 BOOST_CHECK_EQUAL(nNacks, 0);
Alexander Afanasyev90164962014-03-06 08:29:59 +0000331 BOOST_CHECK_EQUAL(nData, 0);
332}
333
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700334class FacesFixture2 : public FacesFixture
335{
336public:
337 void
Junxiao Shicf9c6bb2016-07-27 02:18:19 +0000338 checkPrefix(bool shouldExist)
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700339 {
Junxiao Shicf9c6bb2016-07-27 02:18:19 +0000340 // Boost.Test fails if a child process exits with non-zero.
341 // http://stackoverflow.com/q/5325202
Eric Newberry06690612016-12-27 12:50:15 -0700342 std::string output = this->executeCommand("nfdc route list | grep /Hello/World || true");
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700343
Junxiao Shicf9c6bb2016-07-27 02:18:19 +0000344 if (shouldExist) {
345 BOOST_CHECK_NE(output.size(), 0);
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700346 }
347 else {
Junxiao Shicf9c6bb2016-07-27 02:18:19 +0000348 BOOST_CHECK_EQUAL(output.size(), 0);
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700349 }
350 }
Junxiao Shicf9c6bb2016-07-27 02:18:19 +0000351
352protected:
353 std::string
354 executeCommand(const std::string& cmd)
355 {
356 std::string output;
357 char buf[256];
358 FILE* pipe = popen(cmd.c_str(), "r");
359 BOOST_REQUIRE_MESSAGE(pipe != nullptr, "cannot execute '" << cmd << "'");
360 while (fgets(buf, sizeof(buf), pipe) != nullptr) {
361 output += buf;
362 }
363 pclose(pipe);
364 return output;
365 }
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700366};
367
368BOOST_FIXTURE_TEST_CASE(RegisterUnregisterPrefix, FacesFixture2)
369{
370 Face face;
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700371 Scheduler scheduler(face.getIoService());
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800372 scheduler.scheduleEvent(time::seconds(4),
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700373 bind(&FacesFixture::terminate, this, ref(face)));
374
375 regPrefixId = face.setInterestFilter(InterestFilter("/Hello/World"),
376 bind(&FacesFixture::onInterest, this,
377 ref(face), _1, _2),
Eric Newberry06690612016-12-27 12:50:15 -0700378 nullptr,
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700379 bind(&FacesFixture::onRegFailed, this));
380
381 scheduler.scheduleEvent(time::milliseconds(500),
382 bind(&FacesFixture2::checkPrefix, this, true));
383
384 scheduler.scheduleEvent(time::seconds(1),
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700385 bind(static_cast<void(Face::*)(const RegisteredPrefixId*)>(&Face::unsetInterestFilter),
386 &face,
387 regPrefixId)); // shouldn't match
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700388
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800389 scheduler.scheduleEvent(time::milliseconds(2000),
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700390 bind(&FacesFixture2::checkPrefix, this, false));
391
392 BOOST_REQUIRE_NO_THROW(face.processEvents());
393}
Alexander Afanasyev90164962014-03-06 08:29:59 +0000394
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700395
396class FacesFixture3 : public FacesFixture2
397{
398public:
399 FacesFixture3()
400 : nRegSuccesses(0)
401 , nUnregSuccesses(0)
402 , nUnregFailures(0)
403 {
404 }
405
406 void
407 onRegSucceeded()
408 {
409 ++nRegSuccesses;
410 }
411
412 void
413 onUnregSucceeded()
414 {
415 ++nUnregSuccesses;
416 }
417
418 void
419 onUnregFailed()
420 {
421 ++nUnregFailures;
422 }
423
424public:
425 uint64_t nRegSuccesses;
426 uint64_t nUnregSuccesses;
427 uint64_t nUnregFailures;
428};
429
430BOOST_FIXTURE_TEST_CASE(RegisterPrefix, FacesFixture3)
431{
432 Face face;
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700433 Face face2(face.getIoService());
434 Scheduler scheduler(face.getIoService());
Eric Newberry06690612016-12-27 12:50:15 -0700435 scheduler.scheduleEvent(time::seconds(6),
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700436 bind(&FacesFixture::terminate, this, ref(face)));
437
Eric Newberry06690612016-12-27 12:50:15 -0700438 scheduler.scheduleEvent(time::seconds(2),
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700439 bind(&FacesFixture2::checkPrefix, this, true));
440
441 regPrefixId = face.registerPrefix("/Hello/World",
442 bind(&FacesFixture3::onRegSucceeded, this),
443 bind(&FacesFixture3::onRegFailed, this));
444
Eric Newberry06690612016-12-27 12:50:15 -0700445 scheduler.scheduleEvent(time::seconds(4),
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700446 bind(&Face::unregisterPrefix, &face,
447 regPrefixId,
448 static_cast<UnregisterPrefixSuccessCallback>(bind(&FacesFixture3::onUnregSucceeded, this)),
449 static_cast<UnregisterPrefixFailureCallback>(bind(&FacesFixture3::onUnregFailed, this))));
450
Eric Newberry06690612016-12-27 12:50:15 -0700451 scheduler.scheduleEvent(time::milliseconds(6500),
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700452 bind(&FacesFixture2::checkPrefix, this, false));
453
454 BOOST_REQUIRE_NO_THROW(face.processEvents());
455
456 BOOST_CHECK_EQUAL(nRegFailures, 0);
457 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
458
459 BOOST_CHECK_EQUAL(nUnregFailures, 0);
460 BOOST_CHECK_EQUAL(nUnregSuccesses, 1);
461}
462
463BOOST_AUTO_TEST_CASE(SetRegexFilterButNoRegister)
464{
465 Face face;
466 Face face2(face.getIoService());
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700467 Scheduler scheduler(face.getIoService());
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700468 scheduler.scheduleEvent(time::seconds(2),
469 bind(&FacesFixture::terminate, this, ref(face)));
470
471 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
472 bind(&FacesFixture::onInterestRegex, this,
473 ref(face), _1, _2));
474
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700475 // prefix is not registered, and also does not match regex
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700476 scheduler.scheduleEvent(time::milliseconds(200),
477 bind(&FacesFixture::expressInterest, this,
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700478 ref(face2), Name("/Hello/World/a")));
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700479
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700480 // matches regex, but prefix is not registered
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700481 scheduler.scheduleEvent(time::milliseconds(300),
482 bind(&FacesFixture::expressInterest, this,
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700483 ref(face2), Name("/Hello/World/a/b")));
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700484
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700485 // matches regex, but prefix is not registered
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700486 scheduler.scheduleEvent(time::milliseconds(400),
487 bind(&FacesFixture::expressInterest, this,
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700488 ref(face2), Name("/Hello/World/a/b/c")));
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700489
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700490 // prefix is not registered, and also does not match regex
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700491 scheduler.scheduleEvent(time::milliseconds(500),
492 bind(&FacesFixture::expressInterest, this,
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700493 ref(face2), Name("/Hello/World/a/b/d")));
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700494
495 BOOST_REQUIRE_NO_THROW(face.processEvents());
496
497 BOOST_CHECK_EQUAL(nRegFailures, 0);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700498 BOOST_CHECK_EQUAL(nInInterests, 0);
Eric Newberry06690612016-12-27 12:50:15 -0700499 BOOST_CHECK_EQUAL(nTimeouts, 0);
500 BOOST_CHECK_EQUAL(nNacks, 4);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700501 BOOST_CHECK_EQUAL(nData, 0);
502}
503
504
505BOOST_FIXTURE_TEST_CASE(SetRegexFilterAndRegister, FacesFixture3)
506{
507 Face face;
508 Face face2(face.getIoService());
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700509 Scheduler scheduler(face.getIoService());
Eric Newberry06690612016-12-27 12:50:15 -0700510 scheduler.scheduleEvent(time::seconds(4),
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700511 bind(&FacesFixture::terminate, this, ref(face)));
512
513 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
514 bind(&FacesFixture::onInterestRegex, this,
515 ref(face), _1, _2));
516
517 face.registerPrefix("/Hello/World",
518 bind(&FacesFixture3::onRegSucceeded, this),
519 bind(&FacesFixture3::onRegFailed, this));
520
521 scheduler.scheduleEvent(time::milliseconds(200),
522 bind(&FacesFixture::expressInterest, this,
523 ref(face2), Name("/Hello/World/a"))); // shouldn't match
524
525 scheduler.scheduleEvent(time::milliseconds(300),
526 bind(&FacesFixture::expressInterest, this,
527 ref(face2), Name("/Hello/World/a/b"))); // should match
528
529 scheduler.scheduleEvent(time::milliseconds(400),
530 bind(&FacesFixture::expressInterest, this,
531 ref(face2), Name("/Hello/World/a/b/c"))); // should match
532
533 scheduler.scheduleEvent(time::milliseconds(500),
534 bind(&FacesFixture::expressInterest, this,
535 ref(face2), Name("/Hello/World/a/b/d"))); // should not match
536
537 BOOST_REQUIRE_NO_THROW(face.processEvents());
538
539 BOOST_CHECK_EQUAL(nRegFailures, 0);
540 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
541
542 BOOST_CHECK_EQUAL(nInInterests, 2);
543 BOOST_CHECK_EQUAL(nTimeouts, 4);
Eric Newberry06690612016-12-27 12:50:15 -0700544 BOOST_CHECK_EQUAL(nNacks, 0);
545 BOOST_CHECK_EQUAL(nData, 0);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700546}
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400547
Alexander Afanasyevfba1ac62015-08-26 15:19:13 -0700548BOOST_AUTO_TEST_CASE(ShutdownWhileSendInProgress) // Bug #3136
549{
550 Face face;
Junxiao Shi909ffef2017-07-07 22:12:27 +0000551 face.expressInterest(Interest("/Hello/World/!"), nullptr, nullptr, nullptr);
Alexander Afanasyevfba1ac62015-08-26 15:19:13 -0700552 BOOST_REQUIRE_NO_THROW(face.processEvents(time::seconds(1)));
553
Junxiao Shi909ffef2017-07-07 22:12:27 +0000554 face.expressInterest(Interest("/Bye/World/1"), nullptr, nullptr, nullptr);
555 face.expressInterest(Interest("/Bye/World/2"), nullptr, nullptr, nullptr);
556 face.expressInterest(Interest("/Bye/World/3"), nullptr, nullptr, nullptr);
Alexander Afanasyevfba1ac62015-08-26 15:19:13 -0700557 face.shutdown();
558
559 BOOST_REQUIRE_NO_THROW(face.processEvents(time::seconds(1)));
560 // should not segfault
561}
562
Alexander Afanasyeve508f142015-09-01 15:14:45 -0700563BOOST_AUTO_TEST_CASE(LargeDelayBetweenFaceConstructorAndProcessEvents) // Bug #2742
564{
565 ndn::Face face;
566
567 ::sleep(5); // simulate setup workload
568
569 BOOST_CHECK_NO_THROW(face.processEvents(time::seconds(1)));
570}
571
Alexander Afanasyeva54d5a62017-02-11 19:01:34 -0800572BOOST_AUTO_TEST_CASE(ProcessEventsBlocksForeverWhenNothingScheduled) // Bug #3957
573{
574 ndn::Face face;
575 std::mutex m;
576 std::condition_variable cv;
577 bool processEventsFinished = false;
578
579 std::thread faceThread([&] {
580 face.processEvents();
581
582 processEventsFinished = true;
583 std::lock_guard<std::mutex> lk(m);
584 cv.notify_one();
585 });
586
587 {
588 std::unique_lock<std::mutex> lk(m);
589 cv.wait_for(lk, std::chrono::seconds(5), [&] { return processEventsFinished; });
590 }
591
592 BOOST_CHECK_EQUAL(processEventsFinished, true);
593 if (!processEventsFinished) {
594 face.shutdown();
595 }
596 faceThread.join();
597}
598
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800599BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800600
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700601} // namespace tests
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800602} // namespace ndn