blob: cf7c872abd9609ac0c28cf88090167b92d152d04 [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));
Junxiao Shib6e276f2017-08-14 20:10:04 +0000153 face.processEvents();
Alexander Afanasyev90164962014-03-06 08:29:59 +0000154 BOOST_CHECK_EQUAL(nData, 1);
Eric Newberry06690612016-12-27 12:50:15 -0700155 BOOST_CHECK_EQUAL(nNacks, 0);
Alexander Afanasyev90164962014-03-06 08:29:59 +0000156 BOOST_CHECK_EQUAL(nTimeouts, 0);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800157
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700158 face.expressInterest(Interest("/localhost/non-existing/data/should/not/exist/anywhere",
159 time::milliseconds(50)),
160 bind(&FacesFixture::onData, this),
Eric Newberry06690612016-12-27 12:50:15 -0700161 bind(&FacesFixture::onNack, this),
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700162 bind(&FacesFixture::onTimeout, this));
Junxiao Shib6e276f2017-08-14 20:10:04 +0000163 face.processEvents();
164 BOOST_CHECK_EQUAL(nData, 1);
165 BOOST_CHECK_EQUAL(nNacks, 0);
166 BOOST_CHECK_EQUAL(nTimeouts, 1);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700167
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700168 Name veryLongName;
Eric Newberry06690612016-12-27 12:50:15 -0700169 for (size_t i = 0; i <= MAX_NDN_PACKET_SIZE / 10; i++) {
170 veryLongName.append("0123456789");
171 }
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700172
Junxiao Shib6e276f2017-08-14 20:10:04 +0000173 BOOST_CHECK_THROW(do {
174 face.expressInterest(Interest(veryLongName), nullptr, nullptr, nullptr);
175 face.processEvents();
176 } while (false), Face::OversizedPacketError);
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700177
178 shared_ptr<Data> data = make_shared<Data>(veryLongName);
179 data->setContent(reinterpret_cast<const uint8_t*>("01234567890"), 10);
Yingdi Yu41546342014-11-30 23:37:53 -0800180 m_keyChain.sign(*data);
Junxiao Shib6e276f2017-08-14 20:10:04 +0000181 BOOST_CHECK_THROW(do {
182 face.put(*data);
183 face.processEvents();
184 } while (false), Face::OversizedPacketError);
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700185
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800186}
187
Alexander Afanasyev90164962014-03-06 08:29:59 +0000188BOOST_AUTO_TEST_CASE(Tcp)
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800189{
190 Face face("localhost");
191
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700192 face.expressInterest(Interest("/", time::milliseconds(1000)),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800193 bind(&FacesFixture::onData, this),
Eric Newberry06690612016-12-27 12:50:15 -0700194 bind(&FacesFixture::onNack, this),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800195 bind(&FacesFixture::onTimeout, this));
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800196
197 BOOST_REQUIRE_NO_THROW(face.processEvents());
198
Alexander Afanasyev90164962014-03-06 08:29:59 +0000199 BOOST_CHECK_EQUAL(nData, 1);
Eric Newberry06690612016-12-27 12:50:15 -0700200 BOOST_CHECK_EQUAL(nNacks, 0);
Alexander Afanasyev90164962014-03-06 08:29:59 +0000201 BOOST_CHECK_EQUAL(nTimeouts, 0);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800202
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700203 face.expressInterest(Interest("/localhost/non-existing/data/should/not/exist/anywhere",
204 time::milliseconds(50)),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800205 bind(&FacesFixture::onData, this),
Eric Newberry06690612016-12-27 12:50:15 -0700206 bind(&FacesFixture::onNack, this),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800207 bind(&FacesFixture::onTimeout, this));
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800208
209 BOOST_REQUIRE_NO_THROW(face.processEvents());
210
Alexander Afanasyev90164962014-03-06 08:29:59 +0000211 BOOST_CHECK_EQUAL(nData, 1);
Eric Newberry06690612016-12-27 12:50:15 -0700212 BOOST_CHECK_EQUAL(nNacks, 0);
Alexander Afanasyev90164962014-03-06 08:29:59 +0000213 BOOST_CHECK_EQUAL(nTimeouts, 1);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800214}
215
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800216
Alexander Afanasyev90164962014-03-06 08:29:59 +0000217BOOST_AUTO_TEST_CASE(SetFilter)
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800218{
219 Face face;
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700220 Face face2(face.getIoService());
221 Scheduler scheduler(face.getIoService());
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800222 scheduler.scheduleEvent(time::seconds(4),
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700223 bind(&FacesFixture::terminate, this, ref(face)));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700224
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800225 regPrefixId = face.setInterestFilter("/Hello/World",
Alexander Afanasyev90164962014-03-06 08:29:59 +0000226 bind(&FacesFixture::onInterest, this, ref(face), _1, _2),
Eric Newberry06690612016-12-27 12:50:15 -0700227 nullptr,
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800228 bind(&FacesFixture::onRegFailed, this));
229
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700230 scheduler.scheduleEvent(time::milliseconds(200),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800231 bind(&FacesFixture::expressInterest, this,
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700232 ref(face2), Name("/Hello/World/!")));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700233
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800234 BOOST_REQUIRE_NO_THROW(face.processEvents());
235
Alexander Afanasyev90164962014-03-06 08:29:59 +0000236 BOOST_CHECK_EQUAL(nRegFailures, 0);
237 BOOST_CHECK_EQUAL(nInInterests, 1);
238 BOOST_CHECK_EQUAL(nTimeouts, 1);
Eric Newberry06690612016-12-27 12:50:15 -0700239 BOOST_CHECK_EQUAL(nNacks, 0);
Alexander Afanasyev90164962014-03-06 08:29:59 +0000240 BOOST_CHECK_EQUAL(nData, 0);
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800241}
242
Alexander Afanasyev90164962014-03-06 08:29:59 +0000243BOOST_AUTO_TEST_CASE(SetTwoFilters)
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800244{
245 Face face;
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700246 Face face2(face.getIoService());
247 Scheduler scheduler(face.getIoService());
Eric Newberry06690612016-12-27 12:50:15 -0700248 scheduler.scheduleEvent(time::seconds(6),
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700249 bind(&FacesFixture::terminate, this, ref(face)));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700250
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800251 regPrefixId = face.setInterestFilter("/Hello/World",
Alexander Afanasyev90164962014-03-06 08:29:59 +0000252 bind(&FacesFixture::onInterest, this, ref(face), _1, _2),
Eric Newberry06690612016-12-27 12:50:15 -0700253 nullptr,
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800254 bind(&FacesFixture::onRegFailed, this));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700255
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800256 regPrefixId2 = face.setInterestFilter("/Los/Angeles/Lakers",
Alexander Afanasyev90164962014-03-06 08:29:59 +0000257 bind(&FacesFixture::onInterest2, this, ref(face), _1, _2),
Eric Newberry06690612016-12-27 12:50:15 -0700258 nullptr,
Alexander Afanasyev90164962014-03-06 08:29:59 +0000259 bind(&FacesFixture::onRegFailed, this));
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800260
261
Eric Newberry06690612016-12-27 12:50:15 -0700262 scheduler.scheduleEvent(time::seconds(2),
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800263 bind(&FacesFixture::expressInterest, this,
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700264 ref(face2), Name("/Hello/World/!")));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700265
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800266 BOOST_REQUIRE_NO_THROW(face.processEvents());
267
Alexander Afanasyev90164962014-03-06 08:29:59 +0000268 BOOST_CHECK_EQUAL(nRegFailures, 0);
269 BOOST_CHECK_EQUAL(nInInterests, 1);
270 BOOST_CHECK_EQUAL(nInInterests2, 0);
271 BOOST_CHECK_EQUAL(nTimeouts, 1);
Eric Newberry06690612016-12-27 12:50:15 -0700272 BOOST_CHECK_EQUAL(nNacks, 0);
Alexander Afanasyev90164962014-03-06 08:29:59 +0000273 BOOST_CHECK_EQUAL(nData, 0);
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800274}
275
Alexander Afanasyev90164962014-03-06 08:29:59 +0000276BOOST_AUTO_TEST_CASE(SetRegexFilterError)
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::onInterestRegexError, this,
286 ref(face), _1, _2),
Eric Newberry06690612016-12-27 12:50:15 -0700287 nullptr,
Alexander Afanasyev90164962014-03-06 08:29:59 +0000288 bind(&FacesFixture::onRegFailed, this));
289
290 scheduler.scheduleEvent(time::milliseconds(300),
291 bind(&FacesFixture::expressInterest, this,
292 ref(face2), Name("/Hello/World/XXX/b/c"))); // should match
293
294 BOOST_REQUIRE_THROW(face.processEvents(), InterestFilter::Error);
295}
296
297BOOST_AUTO_TEST_CASE(SetRegexFilter)
298{
299 Face face;
300 Face face2(face.getIoService());
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700301 Scheduler scheduler(face.getIoService());
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800302 scheduler.scheduleEvent(time::seconds(4),
Alexander Afanasyev90164962014-03-06 08:29:59 +0000303 bind(&FacesFixture::terminate, this, ref(face)));
304
305 regPrefixId = face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
306 bind(&FacesFixture::onInterestRegex, this,
307 ref(face), _1, _2),
Eric Newberry06690612016-12-27 12:50:15 -0700308 nullptr,
Alexander Afanasyev90164962014-03-06 08:29:59 +0000309 bind(&FacesFixture::onRegFailed, this));
310
311 scheduler.scheduleEvent(time::milliseconds(200),
312 bind(&FacesFixture::expressInterest, this,
313 ref(face2), Name("/Hello/World/a"))); // shouldn't match
314
315 scheduler.scheduleEvent(time::milliseconds(300),
316 bind(&FacesFixture::expressInterest, this,
317 ref(face2), Name("/Hello/World/a/b"))); // should match
318
319 scheduler.scheduleEvent(time::milliseconds(400),
320 bind(&FacesFixture::expressInterest, this,
321 ref(face2), Name("/Hello/World/a/b/c"))); // should match
322
323 scheduler.scheduleEvent(time::milliseconds(500),
324 bind(&FacesFixture::expressInterest, this,
325 ref(face2), Name("/Hello/World/a/b/d"))); // should not match
326
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700327 BOOST_REQUIRE_NO_THROW(face.processEvents());
Alexander Afanasyev90164962014-03-06 08:29:59 +0000328
329 BOOST_CHECK_EQUAL(nRegFailures, 0);
330 BOOST_CHECK_EQUAL(nInInterests, 2);
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700331 BOOST_CHECK_EQUAL(nTimeouts, 4);
Eric Newberry06690612016-12-27 12:50:15 -0700332 BOOST_CHECK_EQUAL(nNacks, 0);
Alexander Afanasyev90164962014-03-06 08:29:59 +0000333 BOOST_CHECK_EQUAL(nData, 0);
334}
335
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700336class FacesFixture2 : public FacesFixture
337{
338public:
339 void
Junxiao Shicf9c6bb2016-07-27 02:18:19 +0000340 checkPrefix(bool shouldExist)
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700341 {
Junxiao Shicf9c6bb2016-07-27 02:18:19 +0000342 // Boost.Test fails if a child process exits with non-zero.
343 // http://stackoverflow.com/q/5325202
Eric Newberry06690612016-12-27 12:50:15 -0700344 std::string output = this->executeCommand("nfdc route list | grep /Hello/World || true");
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700345
Junxiao Shicf9c6bb2016-07-27 02:18:19 +0000346 if (shouldExist) {
347 BOOST_CHECK_NE(output.size(), 0);
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700348 }
349 else {
Junxiao Shicf9c6bb2016-07-27 02:18:19 +0000350 BOOST_CHECK_EQUAL(output.size(), 0);
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700351 }
352 }
Junxiao Shicf9c6bb2016-07-27 02:18:19 +0000353
354protected:
355 std::string
356 executeCommand(const std::string& cmd)
357 {
358 std::string output;
359 char buf[256];
360 FILE* pipe = popen(cmd.c_str(), "r");
361 BOOST_REQUIRE_MESSAGE(pipe != nullptr, "cannot execute '" << cmd << "'");
362 while (fgets(buf, sizeof(buf), pipe) != nullptr) {
363 output += buf;
364 }
365 pclose(pipe);
366 return output;
367 }
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700368};
369
370BOOST_FIXTURE_TEST_CASE(RegisterUnregisterPrefix, FacesFixture2)
371{
372 Face face;
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700373 Scheduler scheduler(face.getIoService());
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800374 scheduler.scheduleEvent(time::seconds(4),
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700375 bind(&FacesFixture::terminate, this, ref(face)));
376
377 regPrefixId = face.setInterestFilter(InterestFilter("/Hello/World"),
378 bind(&FacesFixture::onInterest, this,
379 ref(face), _1, _2),
Eric Newberry06690612016-12-27 12:50:15 -0700380 nullptr,
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700381 bind(&FacesFixture::onRegFailed, this));
382
383 scheduler.scheduleEvent(time::milliseconds(500),
384 bind(&FacesFixture2::checkPrefix, this, true));
385
386 scheduler.scheduleEvent(time::seconds(1),
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700387 bind(static_cast<void(Face::*)(const RegisteredPrefixId*)>(&Face::unsetInterestFilter),
388 &face,
389 regPrefixId)); // shouldn't match
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700390
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800391 scheduler.scheduleEvent(time::milliseconds(2000),
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700392 bind(&FacesFixture2::checkPrefix, this, false));
393
394 BOOST_REQUIRE_NO_THROW(face.processEvents());
395}
Alexander Afanasyev90164962014-03-06 08:29:59 +0000396
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700397
398class FacesFixture3 : public FacesFixture2
399{
400public:
401 FacesFixture3()
402 : nRegSuccesses(0)
403 , nUnregSuccesses(0)
404 , nUnregFailures(0)
405 {
406 }
407
408 void
409 onRegSucceeded()
410 {
411 ++nRegSuccesses;
412 }
413
414 void
415 onUnregSucceeded()
416 {
417 ++nUnregSuccesses;
418 }
419
420 void
421 onUnregFailed()
422 {
423 ++nUnregFailures;
424 }
425
426public:
427 uint64_t nRegSuccesses;
428 uint64_t nUnregSuccesses;
429 uint64_t nUnregFailures;
430};
431
432BOOST_FIXTURE_TEST_CASE(RegisterPrefix, FacesFixture3)
433{
434 Face face;
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700435 Face face2(face.getIoService());
436 Scheduler scheduler(face.getIoService());
Eric Newberry06690612016-12-27 12:50:15 -0700437 scheduler.scheduleEvent(time::seconds(6),
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700438 bind(&FacesFixture::terminate, this, ref(face)));
439
Eric Newberry06690612016-12-27 12:50:15 -0700440 scheduler.scheduleEvent(time::seconds(2),
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700441 bind(&FacesFixture2::checkPrefix, this, true));
442
443 regPrefixId = face.registerPrefix("/Hello/World",
444 bind(&FacesFixture3::onRegSucceeded, this),
445 bind(&FacesFixture3::onRegFailed, this));
446
Eric Newberry06690612016-12-27 12:50:15 -0700447 scheduler.scheduleEvent(time::seconds(4),
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700448 bind(&Face::unregisterPrefix, &face,
449 regPrefixId,
450 static_cast<UnregisterPrefixSuccessCallback>(bind(&FacesFixture3::onUnregSucceeded, this)),
451 static_cast<UnregisterPrefixFailureCallback>(bind(&FacesFixture3::onUnregFailed, this))));
452
Eric Newberry06690612016-12-27 12:50:15 -0700453 scheduler.scheduleEvent(time::milliseconds(6500),
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700454 bind(&FacesFixture2::checkPrefix, this, false));
455
456 BOOST_REQUIRE_NO_THROW(face.processEvents());
457
458 BOOST_CHECK_EQUAL(nRegFailures, 0);
459 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
460
461 BOOST_CHECK_EQUAL(nUnregFailures, 0);
462 BOOST_CHECK_EQUAL(nUnregSuccesses, 1);
463}
464
465BOOST_AUTO_TEST_CASE(SetRegexFilterButNoRegister)
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
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700477 // prefix is not registered, and also does not match regex
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700478 scheduler.scheduleEvent(time::milliseconds(200),
479 bind(&FacesFixture::expressInterest, this,
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700480 ref(face2), Name("/Hello/World/a")));
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700481
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700482 // matches regex, but prefix is not registered
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700483 scheduler.scheduleEvent(time::milliseconds(300),
484 bind(&FacesFixture::expressInterest, this,
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700485 ref(face2), Name("/Hello/World/a/b")));
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700486
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700487 // matches regex, but prefix is not registered
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700488 scheduler.scheduleEvent(time::milliseconds(400),
489 bind(&FacesFixture::expressInterest, this,
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700490 ref(face2), Name("/Hello/World/a/b/c")));
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700491
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700492 // prefix is not registered, and also does not match regex
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700493 scheduler.scheduleEvent(time::milliseconds(500),
494 bind(&FacesFixture::expressInterest, this,
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700495 ref(face2), Name("/Hello/World/a/b/d")));
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700496
497 BOOST_REQUIRE_NO_THROW(face.processEvents());
498
499 BOOST_CHECK_EQUAL(nRegFailures, 0);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700500 BOOST_CHECK_EQUAL(nInInterests, 0);
Eric Newberry06690612016-12-27 12:50:15 -0700501 BOOST_CHECK_EQUAL(nTimeouts, 0);
502 BOOST_CHECK_EQUAL(nNacks, 4);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700503 BOOST_CHECK_EQUAL(nData, 0);
504}
505
506
507BOOST_FIXTURE_TEST_CASE(SetRegexFilterAndRegister, FacesFixture3)
508{
509 Face face;
510 Face face2(face.getIoService());
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700511 Scheduler scheduler(face.getIoService());
Eric Newberry06690612016-12-27 12:50:15 -0700512 scheduler.scheduleEvent(time::seconds(4),
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700513 bind(&FacesFixture::terminate, this, ref(face)));
514
515 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
516 bind(&FacesFixture::onInterestRegex, this,
517 ref(face), _1, _2));
518
519 face.registerPrefix("/Hello/World",
520 bind(&FacesFixture3::onRegSucceeded, this),
521 bind(&FacesFixture3::onRegFailed, this));
522
523 scheduler.scheduleEvent(time::milliseconds(200),
524 bind(&FacesFixture::expressInterest, this,
525 ref(face2), Name("/Hello/World/a"))); // shouldn't match
526
527 scheduler.scheduleEvent(time::milliseconds(300),
528 bind(&FacesFixture::expressInterest, this,
529 ref(face2), Name("/Hello/World/a/b"))); // should match
530
531 scheduler.scheduleEvent(time::milliseconds(400),
532 bind(&FacesFixture::expressInterest, this,
533 ref(face2), Name("/Hello/World/a/b/c"))); // should match
534
535 scheduler.scheduleEvent(time::milliseconds(500),
536 bind(&FacesFixture::expressInterest, this,
537 ref(face2), Name("/Hello/World/a/b/d"))); // should not match
538
539 BOOST_REQUIRE_NO_THROW(face.processEvents());
540
541 BOOST_CHECK_EQUAL(nRegFailures, 0);
542 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
543
544 BOOST_CHECK_EQUAL(nInInterests, 2);
545 BOOST_CHECK_EQUAL(nTimeouts, 4);
Eric Newberry06690612016-12-27 12:50:15 -0700546 BOOST_CHECK_EQUAL(nNacks, 0);
547 BOOST_CHECK_EQUAL(nData, 0);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700548}
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400549
Alexander Afanasyevfba1ac62015-08-26 15:19:13 -0700550BOOST_AUTO_TEST_CASE(ShutdownWhileSendInProgress) // Bug #3136
551{
552 Face face;
Junxiao Shi909ffef2017-07-07 22:12:27 +0000553 face.expressInterest(Interest("/Hello/World/!"), nullptr, nullptr, nullptr);
Alexander Afanasyevfba1ac62015-08-26 15:19:13 -0700554 BOOST_REQUIRE_NO_THROW(face.processEvents(time::seconds(1)));
555
Junxiao Shi909ffef2017-07-07 22:12:27 +0000556 face.expressInterest(Interest("/Bye/World/1"), nullptr, nullptr, nullptr);
557 face.expressInterest(Interest("/Bye/World/2"), nullptr, nullptr, nullptr);
558 face.expressInterest(Interest("/Bye/World/3"), nullptr, nullptr, nullptr);
Alexander Afanasyevfba1ac62015-08-26 15:19:13 -0700559 face.shutdown();
560
561 BOOST_REQUIRE_NO_THROW(face.processEvents(time::seconds(1)));
562 // should not segfault
563}
564
Alexander Afanasyeve508f142015-09-01 15:14:45 -0700565BOOST_AUTO_TEST_CASE(LargeDelayBetweenFaceConstructorAndProcessEvents) // Bug #2742
566{
567 ndn::Face face;
568
569 ::sleep(5); // simulate setup workload
570
571 BOOST_CHECK_NO_THROW(face.processEvents(time::seconds(1)));
572}
573
Alexander Afanasyeva54d5a62017-02-11 19:01:34 -0800574BOOST_AUTO_TEST_CASE(ProcessEventsBlocksForeverWhenNothingScheduled) // Bug #3957
575{
576 ndn::Face face;
577 std::mutex m;
578 std::condition_variable cv;
579 bool processEventsFinished = false;
580
581 std::thread faceThread([&] {
582 face.processEvents();
583
584 processEventsFinished = true;
585 std::lock_guard<std::mutex> lk(m);
586 cv.notify_one();
587 });
588
589 {
590 std::unique_lock<std::mutex> lk(m);
591 cv.wait_for(lk, std::chrono::seconds(5), [&] { return processEventsFinished; });
592 }
593
594 BOOST_CHECK_EQUAL(processEventsFinished, true);
595 if (!processEventsFinished) {
596 face.shutdown();
597 }
598 faceThread.join();
599}
600
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800601BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800602
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700603} // namespace tests
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800604} // namespace ndn