blob: 9d358a56a088817e98a4b8fde9ce823f1171aee3 [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 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>
33
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080034namespace ndn {
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040035namespace tests {
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080036
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070037struct PibDirWithDefaultTpm
38{
39 const std::string PATH = "build/keys-with-default-tpm";
40};
41
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080042class FacesFixture : public IdentityManagementFixture
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080043{
Alexander Afanasyev90164962014-03-06 08:29:59 +000044public:
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080045 FacesFixture()
Alexander Afanasyev90164962014-03-06 08:29:59 +000046 : nData(0)
Eric Newberry06690612016-12-27 12:50:15 -070047 , nNacks(0)
Alexander Afanasyev90164962014-03-06 08:29:59 +000048 , nTimeouts(0)
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080049 , regPrefixId(0)
Alexander Afanasyev90164962014-03-06 08:29:59 +000050 , nInInterests(0)
51 , nInInterests2(0)
52 , nRegFailures(0)
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080053 {
54 }
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070055
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080056 void
57 onData()
58 {
Alexander Afanasyev90164962014-03-06 08:29:59 +000059 ++nData;
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080060 }
61
62 void
Eric Newberry06690612016-12-27 12:50:15 -070063 onNack()
64 {
65 ++nNacks;
66 }
67
68 void
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080069 onTimeout()
70 {
Alexander Afanasyev90164962014-03-06 08:29:59 +000071 ++nTimeouts;
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080072 }
73
74 void
Alexander Afanasyev90164962014-03-06 08:29:59 +000075 onInterest(Face& face,
76 const Name&, const Interest&)
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080077 {
Alexander Afanasyev90164962014-03-06 08:29:59 +000078 ++nInInterests;
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080079
80 face.unsetInterestFilter(regPrefixId);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080081 }
82
83 void
Alexander Afanasyev90164962014-03-06 08:29:59 +000084 onInterest2(Face& face,
85 const Name&, const Interest&)
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -080086 {
Alexander Afanasyev90164962014-03-06 08:29:59 +000087 ++nInInterests2;
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -080088
89 face.unsetInterestFilter(regPrefixId2);
90 }
91
92 void
Alexander Afanasyev90164962014-03-06 08:29:59 +000093 onInterestRegex(Face& face,
94 const InterestFilter&, const Interest&)
95 {
96 ++nInInterests;
97 }
98
99 void
100 onInterestRegexError(Face& face,
101 const Name&, const Interest&)
102 {
103 BOOST_FAIL("InterestFilter::Error should have been triggered");
104 }
105
106 void
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800107 onRegFailed()
108 {
Alexander Afanasyev90164962014-03-06 08:29:59 +0000109 ++nRegFailures;
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800110 }
111
112 void
113 expressInterest(Face& face, const Name& name)
114 {
115 Interest i(name);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700116 i.setInterestLifetime(time::milliseconds(50));
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800117 face.expressInterest(i,
118 bind(&FacesFixture::onData, this),
Eric Newberry06690612016-12-27 12:50:15 -0700119 bind(&FacesFixture::onNack, this),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800120 bind(&FacesFixture::onTimeout, this));
121 }
122
123 void
124 terminate(Face& face)
125 {
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800126 face.getIoService().stop();
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800127 }
128
Alexander Afanasyev90164962014-03-06 08:29:59 +0000129 uint32_t nData;
Eric Newberry06690612016-12-27 12:50:15 -0700130 uint32_t nNacks;
Alexander Afanasyev90164962014-03-06 08:29:59 +0000131 uint32_t nTimeouts;
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800132
133 const RegisteredPrefixId* regPrefixId;
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800134 const RegisteredPrefixId* regPrefixId2;
Alexander Afanasyev90164962014-03-06 08:29:59 +0000135 uint32_t nInInterests;
136 uint32_t nInInterests2;
137 uint32_t nRegFailures;
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800138};
139
Alexander Afanasyev90164962014-03-06 08:29:59 +0000140BOOST_FIXTURE_TEST_SUITE(TestFaces, FacesFixture)
141
142BOOST_AUTO_TEST_CASE(Unix)
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800143{
144 Face face;
145
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700146 face.expressInterest(Interest("/", time::milliseconds(1000)),
147 bind(&FacesFixture::onData, this),
Eric Newberry06690612016-12-27 12:50:15 -0700148 bind(&FacesFixture::onNack, this),
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700149 bind(&FacesFixture::onTimeout, this));
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800150
151 BOOST_REQUIRE_NO_THROW(face.processEvents());
152
Alexander Afanasyev90164962014-03-06 08:29:59 +0000153 BOOST_CHECK_EQUAL(nData, 1);
Eric Newberry06690612016-12-27 12:50:15 -0700154 BOOST_CHECK_EQUAL(nNacks, 0);
Alexander Afanasyev90164962014-03-06 08:29:59 +0000155 BOOST_CHECK_EQUAL(nTimeouts, 0);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800156
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700157 face.expressInterest(Interest("/localhost/non-existing/data/should/not/exist/anywhere",
158 time::milliseconds(50)),
159 bind(&FacesFixture::onData, this),
Eric Newberry06690612016-12-27 12:50:15 -0700160 bind(&FacesFixture::onNack, this),
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700161 bind(&FacesFixture::onTimeout, this));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700162
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700163 Name veryLongName;
Eric Newberry06690612016-12-27 12:50:15 -0700164 for (size_t i = 0; i <= MAX_NDN_PACKET_SIZE / 10; i++) {
165 veryLongName.append("0123456789");
166 }
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700167
Eric Newberry06690612016-12-27 12:50:15 -0700168 BOOST_CHECK_THROW(face.expressInterest(veryLongName, nullptr, nullptr, nullptr), Face::Error);
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700169
170 shared_ptr<Data> data = make_shared<Data>(veryLongName);
171 data->setContent(reinterpret_cast<const uint8_t*>("01234567890"), 10);
Yingdi Yu41546342014-11-30 23:37:53 -0800172 m_keyChain.sign(*data);
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700173 BOOST_CHECK_THROW(face.put(*data), Face::Error);
174
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800175 BOOST_REQUIRE_NO_THROW(face.processEvents());
176
Alexander Afanasyev90164962014-03-06 08:29:59 +0000177 BOOST_CHECK_EQUAL(nData, 1);
Eric Newberry06690612016-12-27 12:50:15 -0700178 BOOST_CHECK_EQUAL(nNacks, 0);
Alexander Afanasyev90164962014-03-06 08:29:59 +0000179 BOOST_CHECK_EQUAL(nTimeouts, 1);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800180}
181
Alexander Afanasyev90164962014-03-06 08:29:59 +0000182BOOST_AUTO_TEST_CASE(Tcp)
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800183{
184 Face face("localhost");
185
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700186 face.expressInterest(Interest("/", time::milliseconds(1000)),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800187 bind(&FacesFixture::onData, this),
Eric Newberry06690612016-12-27 12:50:15 -0700188 bind(&FacesFixture::onNack, this),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800189 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);
Eric Newberry06690612016-12-27 12:50:15 -0700194 BOOST_CHECK_EQUAL(nNacks, 0);
Alexander Afanasyev90164962014-03-06 08:29:59 +0000195 BOOST_CHECK_EQUAL(nTimeouts, 0);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800196
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700197 face.expressInterest(Interest("/localhost/non-existing/data/should/not/exist/anywhere",
198 time::milliseconds(50)),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800199 bind(&FacesFixture::onData, this),
Eric Newberry06690612016-12-27 12:50:15 -0700200 bind(&FacesFixture::onNack, this),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800201 bind(&FacesFixture::onTimeout, this));
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800202
203 BOOST_REQUIRE_NO_THROW(face.processEvents());
204
Alexander Afanasyev90164962014-03-06 08:29:59 +0000205 BOOST_CHECK_EQUAL(nData, 1);
Eric Newberry06690612016-12-27 12:50:15 -0700206 BOOST_CHECK_EQUAL(nNacks, 0);
Alexander Afanasyev90164962014-03-06 08:29:59 +0000207 BOOST_CHECK_EQUAL(nTimeouts, 1);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800208}
209
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800210
Alexander Afanasyev90164962014-03-06 08:29:59 +0000211BOOST_AUTO_TEST_CASE(SetFilter)
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800212{
213 Face face;
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700214 Face face2(face.getIoService());
215 Scheduler scheduler(face.getIoService());
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800216 scheduler.scheduleEvent(time::seconds(4),
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700217 bind(&FacesFixture::terminate, this, ref(face)));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700218
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800219 regPrefixId = face.setInterestFilter("/Hello/World",
Alexander Afanasyev90164962014-03-06 08:29:59 +0000220 bind(&FacesFixture::onInterest, this, ref(face), _1, _2),
Eric Newberry06690612016-12-27 12:50:15 -0700221 nullptr,
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800222 bind(&FacesFixture::onRegFailed, this));
223
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700224 scheduler.scheduleEvent(time::milliseconds(200),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800225 bind(&FacesFixture::expressInterest, this,
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700226 ref(face2), Name("/Hello/World/!")));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700227
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800228 BOOST_REQUIRE_NO_THROW(face.processEvents());
229
Alexander Afanasyev90164962014-03-06 08:29:59 +0000230 BOOST_CHECK_EQUAL(nRegFailures, 0);
231 BOOST_CHECK_EQUAL(nInInterests, 1);
232 BOOST_CHECK_EQUAL(nTimeouts, 1);
Eric Newberry06690612016-12-27 12:50:15 -0700233 BOOST_CHECK_EQUAL(nNacks, 0);
Alexander Afanasyev90164962014-03-06 08:29:59 +0000234 BOOST_CHECK_EQUAL(nData, 0);
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800235}
236
Alexander Afanasyev90164962014-03-06 08:29:59 +0000237BOOST_AUTO_TEST_CASE(SetTwoFilters)
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800238{
239 Face face;
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700240 Face face2(face.getIoService());
241 Scheduler scheduler(face.getIoService());
Eric Newberry06690612016-12-27 12:50:15 -0700242 scheduler.scheduleEvent(time::seconds(6),
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700243 bind(&FacesFixture::terminate, this, ref(face)));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700244
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800245 regPrefixId = face.setInterestFilter("/Hello/World",
Alexander Afanasyev90164962014-03-06 08:29:59 +0000246 bind(&FacesFixture::onInterest, this, ref(face), _1, _2),
Eric Newberry06690612016-12-27 12:50:15 -0700247 nullptr,
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800248 bind(&FacesFixture::onRegFailed, this));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700249
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800250 regPrefixId2 = face.setInterestFilter("/Los/Angeles/Lakers",
Alexander Afanasyev90164962014-03-06 08:29:59 +0000251 bind(&FacesFixture::onInterest2, this, ref(face), _1, _2),
Eric Newberry06690612016-12-27 12:50:15 -0700252 nullptr,
Alexander Afanasyev90164962014-03-06 08:29:59 +0000253 bind(&FacesFixture::onRegFailed, this));
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800254
255
Eric Newberry06690612016-12-27 12:50:15 -0700256 scheduler.scheduleEvent(time::seconds(2),
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800257 bind(&FacesFixture::expressInterest, this,
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700258 ref(face2), Name("/Hello/World/!")));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700259
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800260 BOOST_REQUIRE_NO_THROW(face.processEvents());
261
Alexander Afanasyev90164962014-03-06 08:29:59 +0000262 BOOST_CHECK_EQUAL(nRegFailures, 0);
263 BOOST_CHECK_EQUAL(nInInterests, 1);
264 BOOST_CHECK_EQUAL(nInInterests2, 0);
265 BOOST_CHECK_EQUAL(nTimeouts, 1);
Eric Newberry06690612016-12-27 12:50:15 -0700266 BOOST_CHECK_EQUAL(nNacks, 0);
Alexander Afanasyev90164962014-03-06 08:29:59 +0000267 BOOST_CHECK_EQUAL(nData, 0);
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800268}
269
Alexander Afanasyev90164962014-03-06 08:29:59 +0000270BOOST_AUTO_TEST_CASE(SetRegexFilterError)
271{
272 Face face;
273 Face face2(face.getIoService());
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700274 Scheduler scheduler(face.getIoService());
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800275 scheduler.scheduleEvent(time::seconds(4),
Alexander Afanasyev90164962014-03-06 08:29:59 +0000276 bind(&FacesFixture::terminate, this, ref(face)));
277
278 regPrefixId = face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
279 bind(&FacesFixture::onInterestRegexError, this,
280 ref(face), _1, _2),
Eric Newberry06690612016-12-27 12:50:15 -0700281 nullptr,
Alexander Afanasyev90164962014-03-06 08:29:59 +0000282 bind(&FacesFixture::onRegFailed, this));
283
284 scheduler.scheduleEvent(time::milliseconds(300),
285 bind(&FacesFixture::expressInterest, this,
286 ref(face2), Name("/Hello/World/XXX/b/c"))); // should match
287
288 BOOST_REQUIRE_THROW(face.processEvents(), InterestFilter::Error);
289}
290
291BOOST_AUTO_TEST_CASE(SetRegexFilter)
292{
293 Face face;
294 Face face2(face.getIoService());
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700295 Scheduler scheduler(face.getIoService());
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800296 scheduler.scheduleEvent(time::seconds(4),
Alexander Afanasyev90164962014-03-06 08:29:59 +0000297 bind(&FacesFixture::terminate, this, ref(face)));
298
299 regPrefixId = face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
300 bind(&FacesFixture::onInterestRegex, this,
301 ref(face), _1, _2),
Eric Newberry06690612016-12-27 12:50:15 -0700302 nullptr,
Alexander Afanasyev90164962014-03-06 08:29:59 +0000303 bind(&FacesFixture::onRegFailed, this));
304
305 scheduler.scheduleEvent(time::milliseconds(200),
306 bind(&FacesFixture::expressInterest, this,
307 ref(face2), Name("/Hello/World/a"))); // shouldn't match
308
309 scheduler.scheduleEvent(time::milliseconds(300),
310 bind(&FacesFixture::expressInterest, this,
311 ref(face2), Name("/Hello/World/a/b"))); // should match
312
313 scheduler.scheduleEvent(time::milliseconds(400),
314 bind(&FacesFixture::expressInterest, this,
315 ref(face2), Name("/Hello/World/a/b/c"))); // should match
316
317 scheduler.scheduleEvent(time::milliseconds(500),
318 bind(&FacesFixture::expressInterest, this,
319 ref(face2), Name("/Hello/World/a/b/d"))); // should not match
320
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700321 BOOST_REQUIRE_NO_THROW(face.processEvents());
Alexander Afanasyev90164962014-03-06 08:29:59 +0000322
323 BOOST_CHECK_EQUAL(nRegFailures, 0);
324 BOOST_CHECK_EQUAL(nInInterests, 2);
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700325 BOOST_CHECK_EQUAL(nTimeouts, 4);
Eric Newberry06690612016-12-27 12:50:15 -0700326 BOOST_CHECK_EQUAL(nNacks, 0);
Alexander Afanasyev90164962014-03-06 08:29:59 +0000327 BOOST_CHECK_EQUAL(nData, 0);
328}
329
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700330class FacesFixture2 : public FacesFixture
331{
332public:
333 void
Junxiao Shicf9c6bb2016-07-27 02:18:19 +0000334 checkPrefix(bool shouldExist)
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700335 {
Junxiao Shicf9c6bb2016-07-27 02:18:19 +0000336 // Boost.Test fails if a child process exits with non-zero.
337 // http://stackoverflow.com/q/5325202
Eric Newberry06690612016-12-27 12:50:15 -0700338 std::string output = this->executeCommand("nfdc route list | grep /Hello/World || true");
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700339
Junxiao Shicf9c6bb2016-07-27 02:18:19 +0000340 if (shouldExist) {
341 BOOST_CHECK_NE(output.size(), 0);
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700342 }
343 else {
Junxiao Shicf9c6bb2016-07-27 02:18:19 +0000344 BOOST_CHECK_EQUAL(output.size(), 0);
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700345 }
346 }
Junxiao Shicf9c6bb2016-07-27 02:18:19 +0000347
348protected:
349 std::string
350 executeCommand(const std::string& cmd)
351 {
352 std::string output;
353 char buf[256];
354 FILE* pipe = popen(cmd.c_str(), "r");
355 BOOST_REQUIRE_MESSAGE(pipe != nullptr, "cannot execute '" << cmd << "'");
356 while (fgets(buf, sizeof(buf), pipe) != nullptr) {
357 output += buf;
358 }
359 pclose(pipe);
360 return output;
361 }
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700362};
363
364BOOST_FIXTURE_TEST_CASE(RegisterUnregisterPrefix, FacesFixture2)
365{
366 Face face;
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700367 Scheduler scheduler(face.getIoService());
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800368 scheduler.scheduleEvent(time::seconds(4),
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700369 bind(&FacesFixture::terminate, this, ref(face)));
370
371 regPrefixId = face.setInterestFilter(InterestFilter("/Hello/World"),
372 bind(&FacesFixture::onInterest, this,
373 ref(face), _1, _2),
Eric Newberry06690612016-12-27 12:50:15 -0700374 nullptr,
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700375 bind(&FacesFixture::onRegFailed, this));
376
377 scheduler.scheduleEvent(time::milliseconds(500),
378 bind(&FacesFixture2::checkPrefix, this, true));
379
380 scheduler.scheduleEvent(time::seconds(1),
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700381 bind(static_cast<void(Face::*)(const RegisteredPrefixId*)>(&Face::unsetInterestFilter),
382 &face,
383 regPrefixId)); // shouldn't match
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700384
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800385 scheduler.scheduleEvent(time::milliseconds(2000),
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700386 bind(&FacesFixture2::checkPrefix, this, false));
387
388 BOOST_REQUIRE_NO_THROW(face.processEvents());
389}
Alexander Afanasyev90164962014-03-06 08:29:59 +0000390
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700391
392class FacesFixture3 : public FacesFixture2
393{
394public:
395 FacesFixture3()
396 : nRegSuccesses(0)
397 , nUnregSuccesses(0)
398 , nUnregFailures(0)
399 {
400 }
401
402 void
403 onRegSucceeded()
404 {
405 ++nRegSuccesses;
406 }
407
408 void
409 onUnregSucceeded()
410 {
411 ++nUnregSuccesses;
412 }
413
414 void
415 onUnregFailed()
416 {
417 ++nUnregFailures;
418 }
419
420public:
421 uint64_t nRegSuccesses;
422 uint64_t nUnregSuccesses;
423 uint64_t nUnregFailures;
424};
425
426BOOST_FIXTURE_TEST_CASE(RegisterPrefix, FacesFixture3)
427{
428 Face face;
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700429 Face face2(face.getIoService());
430 Scheduler scheduler(face.getIoService());
Eric Newberry06690612016-12-27 12:50:15 -0700431 scheduler.scheduleEvent(time::seconds(6),
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700432 bind(&FacesFixture::terminate, this, ref(face)));
433
Eric Newberry06690612016-12-27 12:50:15 -0700434 scheduler.scheduleEvent(time::seconds(2),
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700435 bind(&FacesFixture2::checkPrefix, this, true));
436
437 regPrefixId = face.registerPrefix("/Hello/World",
438 bind(&FacesFixture3::onRegSucceeded, this),
439 bind(&FacesFixture3::onRegFailed, this));
440
Eric Newberry06690612016-12-27 12:50:15 -0700441 scheduler.scheduleEvent(time::seconds(4),
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700442 bind(&Face::unregisterPrefix, &face,
443 regPrefixId,
444 static_cast<UnregisterPrefixSuccessCallback>(bind(&FacesFixture3::onUnregSucceeded, this)),
445 static_cast<UnregisterPrefixFailureCallback>(bind(&FacesFixture3::onUnregFailed, this))));
446
Eric Newberry06690612016-12-27 12:50:15 -0700447 scheduler.scheduleEvent(time::milliseconds(6500),
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700448 bind(&FacesFixture2::checkPrefix, this, false));
449
450 BOOST_REQUIRE_NO_THROW(face.processEvents());
451
452 BOOST_CHECK_EQUAL(nRegFailures, 0);
453 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
454
455 BOOST_CHECK_EQUAL(nUnregFailures, 0);
456 BOOST_CHECK_EQUAL(nUnregSuccesses, 1);
457}
458
459BOOST_AUTO_TEST_CASE(SetRegexFilterButNoRegister)
460{
461 Face face;
462 Face face2(face.getIoService());
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700463 Scheduler scheduler(face.getIoService());
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700464 scheduler.scheduleEvent(time::seconds(2),
465 bind(&FacesFixture::terminate, this, ref(face)));
466
467 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
468 bind(&FacesFixture::onInterestRegex, this,
469 ref(face), _1, _2));
470
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700471 // prefix is not registered, and also does not match regex
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700472 scheduler.scheduleEvent(time::milliseconds(200),
473 bind(&FacesFixture::expressInterest, this,
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700474 ref(face2), Name("/Hello/World/a")));
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700475
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700476 // matches regex, but prefix is not registered
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700477 scheduler.scheduleEvent(time::milliseconds(300),
478 bind(&FacesFixture::expressInterest, this,
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700479 ref(face2), Name("/Hello/World/a/b")));
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700480
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700481 // matches regex, but prefix is not registered
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700482 scheduler.scheduleEvent(time::milliseconds(400),
483 bind(&FacesFixture::expressInterest, this,
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700484 ref(face2), Name("/Hello/World/a/b/c")));
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700485
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700486 // prefix is not registered, and also does not match regex
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700487 scheduler.scheduleEvent(time::milliseconds(500),
488 bind(&FacesFixture::expressInterest, this,
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700489 ref(face2), Name("/Hello/World/a/b/d")));
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700490
491 BOOST_REQUIRE_NO_THROW(face.processEvents());
492
493 BOOST_CHECK_EQUAL(nRegFailures, 0);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700494 BOOST_CHECK_EQUAL(nInInterests, 0);
Eric Newberry06690612016-12-27 12:50:15 -0700495 BOOST_CHECK_EQUAL(nTimeouts, 0);
496 BOOST_CHECK_EQUAL(nNacks, 4);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700497 BOOST_CHECK_EQUAL(nData, 0);
498}
499
500
501BOOST_FIXTURE_TEST_CASE(SetRegexFilterAndRegister, FacesFixture3)
502{
503 Face face;
504 Face face2(face.getIoService());
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700505 Scheduler scheduler(face.getIoService());
Eric Newberry06690612016-12-27 12:50:15 -0700506 scheduler.scheduleEvent(time::seconds(4),
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700507 bind(&FacesFixture::terminate, this, ref(face)));
508
509 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
510 bind(&FacesFixture::onInterestRegex, this,
511 ref(face), _1, _2));
512
513 face.registerPrefix("/Hello/World",
514 bind(&FacesFixture3::onRegSucceeded, this),
515 bind(&FacesFixture3::onRegFailed, this));
516
517 scheduler.scheduleEvent(time::milliseconds(200),
518 bind(&FacesFixture::expressInterest, this,
519 ref(face2), Name("/Hello/World/a"))); // shouldn't match
520
521 scheduler.scheduleEvent(time::milliseconds(300),
522 bind(&FacesFixture::expressInterest, this,
523 ref(face2), Name("/Hello/World/a/b"))); // should match
524
525 scheduler.scheduleEvent(time::milliseconds(400),
526 bind(&FacesFixture::expressInterest, this,
527 ref(face2), Name("/Hello/World/a/b/c"))); // should match
528
529 scheduler.scheduleEvent(time::milliseconds(500),
530 bind(&FacesFixture::expressInterest, this,
531 ref(face2), Name("/Hello/World/a/b/d"))); // should not match
532
533 BOOST_REQUIRE_NO_THROW(face.processEvents());
534
535 BOOST_CHECK_EQUAL(nRegFailures, 0);
536 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
537
538 BOOST_CHECK_EQUAL(nInInterests, 2);
539 BOOST_CHECK_EQUAL(nTimeouts, 4);
Eric Newberry06690612016-12-27 12:50:15 -0700540 BOOST_CHECK_EQUAL(nNacks, 0);
541 BOOST_CHECK_EQUAL(nData, 0);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700542}
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400543
Alexander Afanasyevfba1ac62015-08-26 15:19:13 -0700544BOOST_AUTO_TEST_CASE(ShutdownWhileSendInProgress) // Bug #3136
545{
546 Face face;
Eric Newberry06690612016-12-27 12:50:15 -0700547 face.expressInterest(Name("/Hello/World/!"), nullptr, nullptr, nullptr);
Alexander Afanasyevfba1ac62015-08-26 15:19:13 -0700548 BOOST_REQUIRE_NO_THROW(face.processEvents(time::seconds(1)));
549
Eric Newberry06690612016-12-27 12:50:15 -0700550 face.expressInterest(Name("/Bye/World/1"), nullptr, nullptr, nullptr);
551 face.expressInterest(Name("/Bye/World/2"), nullptr, nullptr, nullptr);
552 face.expressInterest(Name("/Bye/World/3"), nullptr, nullptr, nullptr);
Alexander Afanasyevfba1ac62015-08-26 15:19:13 -0700553 face.shutdown();
554
555 BOOST_REQUIRE_NO_THROW(face.processEvents(time::seconds(1)));
556 // should not segfault
557}
558
Alexander Afanasyeve508f142015-09-01 15:14:45 -0700559BOOST_AUTO_TEST_CASE(LargeDelayBetweenFaceConstructorAndProcessEvents) // Bug #2742
560{
561 ndn::Face face;
562
563 ::sleep(5); // simulate setup workload
564
565 BOOST_CHECK_NO_THROW(face.processEvents(time::seconds(1)));
566}
567
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800568BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800569
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700570} // namespace tests
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800571} // namespace ndn