blob: eb01f8f53e4c7e5512959f2eacfd7287d19bd21d [file] [log] [blame]
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +00002/*
Junxiao Shiec475a72019-01-13 21:53:55 +00003 * Copyright (c) 2013-2019 Regents of the University of California.
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -04004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * 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.
20 */
21
Davide Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/face.hpp"
23#include "ndn-cxx/lp/tags.hpp"
24#include "ndn-cxx/transport/tcp-transport.hpp"
25#include "ndn-cxx/transport/unix-transport.hpp"
26#include "ndn-cxx/util/dummy-client-face.hpp"
27#include "ndn-cxx/util/scheduler.hpp"
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040028
Davide Pesavento7e780642018-11-24 15:51:34 -050029#include "tests/boost-test.hpp"
30#include "tests/make-interest-data.hpp"
31#include "tests/unit/identity-management-time-fixture.hpp"
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040032
Junxiao Shiec475a72019-01-13 21:53:55 +000033#include <boost/logic/tribool.hpp>
34
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040035namespace ndn {
36namespace tests {
37
Junxiao Shia60d9362014-11-12 09:38:21 -070038using ndn::util::DummyClientFace;
Junxiao Shia60d9362014-11-12 09:38:21 -070039
Junxiao Shiec475a72019-01-13 21:53:55 +000040struct WantPrefixRegReply;
41struct NoPrefixRegReply;
42
43template<typename PrefixRegReply = WantPrefixRegReply>
Alexander Afanasyev80782e02017-01-04 13:16:54 -080044class FaceFixture : public IdentityManagementTimeFixture
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040045{
46public:
Junxiao Shiec475a72019-01-13 21:53:55 +000047 FaceFixture()
48 : face(io, m_keyChain, {true, !std::is_same<PrefixRegReply, NoPrefixRegReply>::value})
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040049 {
Junxiao Shiec475a72019-01-13 21:53:55 +000050 static_assert(std::is_same<PrefixRegReply, WantPrefixRegReply>::value ||
51 std::is_same<PrefixRegReply, NoPrefixRegReply>::value, "");
52 }
53
54 /** \brief Execute a prefix registration, and optionally check the name in callback.
55 * \return whether the prefix registration succeeded.
56 */
57 bool
58 runPrefixReg(function<void(const RegisterPrefixSuccessCallback& success,
59 const RegisterPrefixFailureCallback& failure)> f)
60 {
61 boost::logic::tribool result = boost::logic::indeterminate;
62 f([&] (const Name&) { result = true; },
63 [&] (const Name&, const std::string&) { result = false; });
64
65 advanceClocks(1_ms);
66 BOOST_REQUIRE(!boost::logic::indeterminate(result));
67 return static_cast<bool>(result);
68 }
69
70 /** \brief Execute a prefix unregistration, and optionally check the name in callback.
71 * \return whether the prefix unregistration succeeded.
72 */
73 bool
74 runPrefixUnreg(function<void(const UnregisterPrefixSuccessCallback& success,
75 const UnregisterPrefixFailureCallback& failure)> f)
76 {
77 boost::logic::tribool result = boost::logic::indeterminate;
78 f([&] { result = true; }, [&] (const std::string&) { result = false; });
79
80 advanceClocks(1_ms);
81 BOOST_REQUIRE(!boost::logic::indeterminate(result));
82 return static_cast<bool>(result);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040083 }
84
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080085public:
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080086 DummyClientFace face;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040087};
88
Junxiao Shiec475a72019-01-13 21:53:55 +000089BOOST_FIXTURE_TEST_SUITE(TestFace, FaceFixture<>)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040090
Junxiao Shi103d8ed2016-08-07 20:34:10 +000091BOOST_AUTO_TEST_SUITE(Consumer)
92
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040093BOOST_AUTO_TEST_CASE(ExpressInterestData)
94{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080095 size_t nData = 0;
Junxiao Shib55e5d32018-07-18 13:32:00 -060096 face.expressInterest(*makeInterest("/Hello/World", true, 50_ms),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080097 [&] (const Interest& i, const Data& d) {
98 BOOST_CHECK(i.getName().isPrefixOf(d.getName()));
99 BOOST_CHECK_EQUAL(i.getName(), "/Hello/World");
100 ++nData;
101 },
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000102 bind([] { BOOST_FAIL("Unexpected Nack"); }),
103 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Eric Newberry83872fd2015-08-06 17:01:24 -0700104
Davide Pesavento0f830802018-01-16 23:58:58 -0500105 advanceClocks(40_ms);
Eric Newberry83872fd2015-08-06 17:01:24 -0700106
Junxiao Shi85d90832016-08-04 03:19:46 +0000107 face.receive(*makeData("/Bye/World/a"));
108 face.receive(*makeData("/Hello/World/a"));
Eric Newberry83872fd2015-08-06 17:01:24 -0700109
Davide Pesavento0f830802018-01-16 23:58:58 -0500110 advanceClocks(50_ms, 2);
Eric Newberry83872fd2015-08-06 17:01:24 -0700111
112 BOOST_CHECK_EQUAL(nData, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800113 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
114 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Eric Newberry83872fd2015-08-06 17:01:24 -0700115
116 size_t nTimeouts = 0;
Junxiao Shib55e5d32018-07-18 13:32:00 -0600117 face.expressInterest(*makeInterest("/Hello/World/a/2", false, 50_ms),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800118 bind([]{}),
119 bind([]{}),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000120 bind([&nTimeouts] { ++nTimeouts; }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500121 advanceClocks(200_ms, 5);
Eric Newberry83872fd2015-08-06 17:01:24 -0700122 BOOST_CHECK_EQUAL(nTimeouts, 1);
123}
124
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800125BOOST_AUTO_TEST_CASE(ExpressMultipleInterestData)
126{
127 size_t nData = 0;
128
Junxiao Shib55e5d32018-07-18 13:32:00 -0600129 face.expressInterest(*makeInterest("/Hello/World", true, 50_ms),
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800130 [&] (const Interest& i, const Data& d) {
131 ++nData;
132 },
133 bind([] { BOOST_FAIL("Unexpected Nack"); }),
134 bind([] { BOOST_FAIL("Unexpected timeout"); }));
135
Junxiao Shib55e5d32018-07-18 13:32:00 -0600136 face.expressInterest(*makeInterest("/Hello/World/a", true, 50_ms),
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800137 [&] (const Interest& i, const Data& d) {
138 ++nData;
139 },
140 bind([] { BOOST_FAIL("Unexpected Nack"); }),
141 bind([] { BOOST_FAIL("Unexpected timeout"); }));
142
Davide Pesavento0f830802018-01-16 23:58:58 -0500143 advanceClocks(40_ms);
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800144
145 face.receive(*makeData("/Hello/World/a/b"));
146
Davide Pesavento0f830802018-01-16 23:58:58 -0500147 advanceClocks(50_ms, 2);
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800148
149 BOOST_CHECK_EQUAL(nData, 2);
150 BOOST_CHECK_EQUAL(face.sentInterests.size(), 2);
151 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
152}
153
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000154BOOST_AUTO_TEST_CASE(ExpressInterestEmptyDataCallback)
155{
Junxiao Shib55e5d32018-07-18 13:32:00 -0600156 face.expressInterest(*makeInterest("/Hello/World", true),
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000157 nullptr,
158 bind([] { BOOST_FAIL("Unexpected Nack"); }),
159 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500160 advanceClocks(1_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000161
162 BOOST_CHECK_NO_THROW(do {
163 face.receive(*makeData("/Hello/World/a"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500164 advanceClocks(1_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000165 } while (false));
166}
167
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400168BOOST_AUTO_TEST_CASE(ExpressInterestTimeout)
169{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800170 size_t nTimeouts = 0;
Junxiao Shib55e5d32018-07-18 13:32:00 -0600171 face.expressInterest(*makeInterest("/Hello/World", false, 50_ms),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000172 bind([] { BOOST_FAIL("Unexpected Data"); }),
173 bind([] { BOOST_FAIL("Unexpected Nack"); }),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800174 [&nTimeouts] (const Interest& i) {
175 BOOST_CHECK_EQUAL(i.getName(), "/Hello/World");
176 ++nTimeouts;
177 });
Eric Newberry83872fd2015-08-06 17:01:24 -0700178
Davide Pesavento0f830802018-01-16 23:58:58 -0500179 advanceClocks(200_ms, 5);
Eric Newberry83872fd2015-08-06 17:01:24 -0700180
181 BOOST_CHECK_EQUAL(nTimeouts, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800182 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
183 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
184 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0);
Eric Newberry83872fd2015-08-06 17:01:24 -0700185}
186
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000187BOOST_AUTO_TEST_CASE(ExpressInterestEmptyTimeoutCallback)
188{
Junxiao Shib55e5d32018-07-18 13:32:00 -0600189 face.expressInterest(*makeInterest("/Hello/World", false, 50_ms),
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000190 bind([] { BOOST_FAIL("Unexpected Data"); }),
191 bind([] { BOOST_FAIL("Unexpected Nack"); }),
192 nullptr);
Davide Pesavento0f830802018-01-16 23:58:58 -0500193 advanceClocks(40_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000194
195 BOOST_CHECK_NO_THROW(do {
Davide Pesavento0f830802018-01-16 23:58:58 -0500196 advanceClocks(6_ms, 2);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000197 } while (false));
198}
199
Eric Newberry83872fd2015-08-06 17:01:24 -0700200BOOST_AUTO_TEST_CASE(ExpressInterestNack)
201{
202 size_t nNacks = 0;
203
Junxiao Shib55e5d32018-07-18 13:32:00 -0600204 auto interest = makeInterest("/Hello/World", false, 50_ms);
Eric Newberry83872fd2015-08-06 17:01:24 -0700205
Junxiao Shib55e5d32018-07-18 13:32:00 -0600206 face.expressInterest(*interest,
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000207 bind([] { BOOST_FAIL("Unexpected Data"); }),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800208 [&] (const Interest& i, const lp::Nack& n) {
209 BOOST_CHECK(i.getName().isPrefixOf(n.getInterest().getName()));
210 BOOST_CHECK_EQUAL(i.getName(), "/Hello/World");
211 BOOST_CHECK_EQUAL(n.getReason(), lp::NackReason::DUPLICATE);
212 ++nNacks;
213 },
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000214 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Eric Newberry83872fd2015-08-06 17:01:24 -0700215
Davide Pesavento0f830802018-01-16 23:58:58 -0500216 advanceClocks(40_ms);
Eric Newberry83872fd2015-08-06 17:01:24 -0700217
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000218 face.receive(makeNack(face.sentInterests.at(0), lp::NackReason::DUPLICATE));
Eric Newberry83872fd2015-08-06 17:01:24 -0700219
Davide Pesavento0f830802018-01-16 23:58:58 -0500220 advanceClocks(50_ms, 2);
Eric Newberry83872fd2015-08-06 17:01:24 -0700221
222 BOOST_CHECK_EQUAL(nNacks, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800223 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
Eric Newberry83872fd2015-08-06 17:01:24 -0700224}
225
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800226BOOST_AUTO_TEST_CASE(ExpressMultipleInterestNack)
227{
228 size_t nNacks = 0;
229
Junxiao Shib55e5d32018-07-18 13:32:00 -0600230 auto interest = makeInterest("/Hello/World", false, 50_ms, 1);
231 face.expressInterest(*interest,
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800232 bind([] { BOOST_FAIL("Unexpected Data"); }),
233 [&] (const Interest& i, const lp::Nack& n) {
234 ++nNacks;
235 },
236 bind([] { BOOST_FAIL("Unexpected timeout"); }));
237
Junxiao Shib55e5d32018-07-18 13:32:00 -0600238 interest->setNonce(2);
239 face.expressInterest(*interest,
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800240 bind([] { BOOST_FAIL("Unexpected Data"); }),
241 [&] (const Interest& i, const lp::Nack& n) {
242 ++nNacks;
243 },
244 bind([] { BOOST_FAIL("Unexpected timeout"); }));
245
Davide Pesavento0f830802018-01-16 23:58:58 -0500246 advanceClocks(40_ms);
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800247
248 face.receive(makeNack(face.sentInterests.at(1), lp::NackReason::DUPLICATE));
249
Davide Pesavento0f830802018-01-16 23:58:58 -0500250 advanceClocks(50_ms, 2);
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800251
252 BOOST_CHECK_EQUAL(nNacks, 2);
253 BOOST_CHECK_EQUAL(face.sentInterests.size(), 2);
254}
255
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000256BOOST_AUTO_TEST_CASE(ExpressInterestEmptyNackCallback)
257{
Junxiao Shib55e5d32018-07-18 13:32:00 -0600258 face.expressInterest(*makeInterest("/Hello/World"),
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000259 bind([] { BOOST_FAIL("Unexpected Data"); }),
260 nullptr,
261 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500262 advanceClocks(1_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000263
264 BOOST_CHECK_NO_THROW(do {
265 face.receive(makeNack(face.sentInterests.at(0), lp::NackReason::DUPLICATE));
Davide Pesavento0f830802018-01-16 23:58:58 -0500266 advanceClocks(1_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000267 } while (false));
268}
269
Junxiao Shi80609d42019-01-29 18:15:22 +0000270BOOST_AUTO_TEST_CASE(CancelPendingInterestHandle)
271{
272 auto hdl = face.expressInterest(*makeInterest("/Hello/World", true, 50_ms),
273 bind([] { BOOST_FAIL("Unexpected data"); }),
274 bind([] { BOOST_FAIL("Unexpected nack"); }),
275 bind([] { BOOST_FAIL("Unexpected timeout"); }));
276 advanceClocks(10_ms);
277
278 hdl.cancel();
279 advanceClocks(10_ms);
280
281 face.receive(*makeData("/Hello/World/%21"));
282 advanceClocks(200_ms, 5);
283
284 // avoid "test case [...] did not check any assertions" message from Boost.Test
285 BOOST_CHECK(true);
286}
287
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000288BOOST_AUTO_TEST_CASE(RemoveAllPendingInterests)
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500289{
Junxiao Shib55e5d32018-07-18 13:32:00 -0600290 face.expressInterest(*makeInterest("/Hello/World/0", false, 50_ms),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800291 bind([] { BOOST_FAIL("Unexpected data"); }),
292 bind([] { BOOST_FAIL("Unexpected nack"); }),
293 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500294
Junxiao Shib55e5d32018-07-18 13:32:00 -0600295 face.expressInterest(*makeInterest("/Hello/World/1", false, 50_ms),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800296 bind([] { BOOST_FAIL("Unexpected data"); }),
297 bind([] { BOOST_FAIL("Unexpected nack"); }),
298 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500299
Davide Pesavento0f830802018-01-16 23:58:58 -0500300 advanceClocks(10_ms);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500301
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800302 face.removeAllPendingInterests();
Davide Pesavento0f830802018-01-16 23:58:58 -0500303 advanceClocks(10_ms);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500304
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800305 BOOST_CHECK_EQUAL(face.getNPendingInterests(), 0);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500306
Junxiao Shi85d90832016-08-04 03:19:46 +0000307 face.receive(*makeData("/Hello/World/0"));
308 face.receive(*makeData("/Hello/World/1"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500309 advanceClocks(200_ms, 5);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500310}
311
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000312BOOST_AUTO_TEST_CASE(DestructionWithoutCancellingPendingInterests) // Bug #2518
313{
314 {
315 DummyClientFace face2(io, m_keyChain);
Junxiao Shib55e5d32018-07-18 13:32:00 -0600316 face2.expressInterest(*makeInterest("/Hello/World", false, 50_ms),
Alexander Afanasyeve6835fe2017-01-19 20:05:01 -0800317 nullptr, nullptr, nullptr);
Davide Pesavento0f830802018-01-16 23:58:58 -0500318 advanceClocks(50_ms, 2);
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000319 }
320
Davide Pesavento0f830802018-01-16 23:58:58 -0500321 advanceClocks(50_ms, 2); // should not crash
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100322
323 // avoid "test case [...] did not check any assertions" message from Boost.Test
324 BOOST_CHECK(true);
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000325}
326
Ashlesh Gawandee2404222018-05-21 18:30:24 -0500327BOOST_AUTO_TEST_CASE(DataCallbackPutData) // Bug 4596
328{
Junxiao Shib55e5d32018-07-18 13:32:00 -0600329 face.expressInterest(*makeInterest("/localhost/notification/1"),
Ashlesh Gawandee2404222018-05-21 18:30:24 -0500330 [&] (const Interest& i, const Data& d) {
331 face.put(*makeData("/chronosync/sampleDigest/1"));
332 }, nullptr, nullptr);
333 advanceClocks(10_ms);
334 BOOST_CHECK_EQUAL(face.sentInterests.back().getName(), "/localhost/notification/1");
335
Junxiao Shib55e5d32018-07-18 13:32:00 -0600336 face.receive(*makeInterest("/chronosync/sampleDigest", true));
Ashlesh Gawandee2404222018-05-21 18:30:24 -0500337 advanceClocks(10_ms);
338
339 face.put(*makeData("/localhost/notification/1"));
340 advanceClocks(10_ms);
341 BOOST_CHECK_EQUAL(face.sentData.back().getName(), "/chronosync/sampleDigest/1");
342}
343
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000344BOOST_AUTO_TEST_SUITE_END() // Consumer
345
346BOOST_AUTO_TEST_SUITE(Producer)
347
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000348BOOST_AUTO_TEST_CASE(PutData)
349{
350 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
351
352 Data data("/4g7xxcuEow/KFvK5Kf2m");
353 signData(data);
354 face.put(data);
355
356 lp::CachePolicy cachePolicy;
357 cachePolicy.setPolicy(lp::CachePolicyType::NO_CACHE);
358 data.setTag(make_shared<lp::CachePolicyTag>(cachePolicy));
Eric Newberry4d261b62016-11-10 13:40:09 -0700359 data.setTag(make_shared<lp::CongestionMarkTag>(1));
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000360 face.put(data);
361
Davide Pesavento0f830802018-01-16 23:58:58 -0500362 advanceClocks(10_ms);
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000363 BOOST_REQUIRE_EQUAL(face.sentData.size(), 2);
364 BOOST_CHECK(face.sentData[0].getTag<lp::CachePolicyTag>() == nullptr);
Eric Newberry4d261b62016-11-10 13:40:09 -0700365 BOOST_CHECK(face.sentData[0].getTag<lp::CongestionMarkTag>() == nullptr);
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000366 BOOST_CHECK(face.sentData[1].getTag<lp::CachePolicyTag>() != nullptr);
Eric Newberry4d261b62016-11-10 13:40:09 -0700367 BOOST_CHECK(face.sentData[1].getTag<lp::CongestionMarkTag>() != nullptr);
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000368}
369
Junxiao Shib6828912017-11-20 14:06:32 +0000370BOOST_AUTO_TEST_CASE(PutDataLoopback)
371{
372 bool hasInterest1 = false, hasData = false;
373
374 // first InterestFilter allows loopback and should receive Interest
Davide Pesavento720e25c2019-07-14 01:33:52 -0400375 face.setInterestFilter("/", [&] (const InterestFilter&, const Interest&) {
Junxiao Shib6828912017-11-20 14:06:32 +0000376 hasInterest1 = true;
377 // do not respond with Data right away, so Face must send Interest to forwarder
378 });
379 // second InterestFilter disallows loopback and should not receive Interest
380 face.setInterestFilter(InterestFilter("/").allowLoopback(false),
381 bind([] { BOOST_ERROR("Unexpected Interest on second InterestFilter"); }));
382
Junxiao Shib55e5d32018-07-18 13:32:00 -0600383 face.expressInterest(*makeInterest("/A", true),
Junxiao Shib6828912017-11-20 14:06:32 +0000384 bind([&] { hasData = true; }),
385 bind([] { BOOST_FAIL("Unexpected nack"); }),
386 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500387 advanceClocks(1_ms);
Junxiao Shib6828912017-11-20 14:06:32 +0000388 BOOST_CHECK_EQUAL(hasInterest1, true); // Interest looped back
389 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1); // Interest sent to forwarder
390 BOOST_CHECK_EQUAL(hasData, false); // waiting for Data
391
392 face.put(*makeData("/A/B")); // first InterestFilter responds with Data
Davide Pesavento0f830802018-01-16 23:58:58 -0500393 advanceClocks(1_ms);
Junxiao Shib6828912017-11-20 14:06:32 +0000394 BOOST_CHECK_EQUAL(hasData, true);
395 BOOST_CHECK_EQUAL(face.sentData.size(), 0); // do not spill Data to forwarder
396}
397
Junxiao Shi859888f2017-09-12 14:29:16 +0000398BOOST_AUTO_TEST_CASE(PutMultipleData)
399{
400 bool hasInterest1 = false;
401 // register two Interest destinations
402 face.setInterestFilter("/", bind([&] {
403 hasInterest1 = true;
404 // sending Data right away from the first destination, don't care whether Interest goes to second destination
405 face.put(*makeData("/A/B"));
406 }));
407 face.setInterestFilter("/", bind([]{}));
Davide Pesavento0f830802018-01-16 23:58:58 -0500408 advanceClocks(10_ms);
Junxiao Shi859888f2017-09-12 14:29:16 +0000409
Junxiao Shib55e5d32018-07-18 13:32:00 -0600410 face.receive(*makeInterest("/A", true));
Davide Pesavento0f830802018-01-16 23:58:58 -0500411 advanceClocks(10_ms);
Junxiao Shi859888f2017-09-12 14:29:16 +0000412 BOOST_CHECK(hasInterest1);
413 BOOST_CHECK_EQUAL(face.sentData.size(), 1);
414 BOOST_CHECK_EQUAL(face.sentData.at(0).getName(), "/A/B");
415
416 face.put(*makeData("/A/C"));
417 BOOST_CHECK_EQUAL(face.sentData.size(), 1); // additional Data are ignored
418}
419
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000420BOOST_AUTO_TEST_CASE(PutNack)
421{
Junxiao Shi79a7a162017-09-09 08:33:57 +0000422 face.setInterestFilter("/", bind([]{})); // register one Interest destination so that face can accept Nacks
Davide Pesavento0f830802018-01-16 23:58:58 -0500423 advanceClocks(10_ms);
Junxiao Shi79a7a162017-09-09 08:33:57 +0000424
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000425 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0);
426
Junxiao Shib55e5d32018-07-18 13:32:00 -0600427 face.put(makeNack(*makeInterest("/unsolicited", false, DEFAULT_INTEREST_LIFETIME, 18645250),
428 lp::NackReason::NO_ROUTE));
Davide Pesavento0f830802018-01-16 23:58:58 -0500429 advanceClocks(10_ms);
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000430 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0); // unsolicited Nack would not be sent
Eric Newberry4d261b62016-11-10 13:40:09 -0700431
Junxiao Shib55e5d32018-07-18 13:32:00 -0600432 auto interest1 = makeInterest("/Hello/World", false, DEFAULT_INTEREST_LIFETIME, 14247162);
433 face.receive(*interest1);
434 auto interest2 = makeInterest("/another/prefix", false, DEFAULT_INTEREST_LIFETIME, 92203002);
435 face.receive(*interest2);
Davide Pesavento0f830802018-01-16 23:58:58 -0500436 advanceClocks(10_ms);
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000437
Junxiao Shib55e5d32018-07-18 13:32:00 -0600438 face.put(makeNack(*interest1, lp::NackReason::DUPLICATE));
Davide Pesavento0f830802018-01-16 23:58:58 -0500439 advanceClocks(10_ms);
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000440 BOOST_REQUIRE_EQUAL(face.sentNacks.size(), 1);
441 BOOST_CHECK_EQUAL(face.sentNacks[0].getReason(), lp::NackReason::DUPLICATE);
442 BOOST_CHECK(face.sentNacks[0].getTag<lp::CongestionMarkTag>() == nullptr);
443
Junxiao Shib55e5d32018-07-18 13:32:00 -0600444 auto nack = makeNack(*interest2, lp::NackReason::NO_ROUTE);
Eric Newberry4d261b62016-11-10 13:40:09 -0700445 nack.setTag(make_shared<lp::CongestionMarkTag>(1));
446 face.put(nack);
Davide Pesavento0f830802018-01-16 23:58:58 -0500447 advanceClocks(10_ms);
Eric Newberry4d261b62016-11-10 13:40:09 -0700448 BOOST_REQUIRE_EQUAL(face.sentNacks.size(), 2);
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000449 BOOST_CHECK_EQUAL(face.sentNacks[1].getReason(), lp::NackReason::NO_ROUTE);
Eric Newberry4d261b62016-11-10 13:40:09 -0700450 BOOST_CHECK(face.sentNacks[1].getTag<lp::CongestionMarkTag>() != nullptr);
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000451}
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500452
Junxiao Shi79a7a162017-09-09 08:33:57 +0000453BOOST_AUTO_TEST_CASE(PutMultipleNack)
454{
Junxiao Shi859888f2017-09-12 14:29:16 +0000455 bool hasInterest1 = false, hasInterest2 = false;
456 // register two Interest destinations
457 face.setInterestFilter("/", [&] (const InterestFilter&, const Interest& interest) {
458 hasInterest1 = true;
459 // sending Nack right away from the first destination, Interest should still go to second destination
460 face.put(makeNack(interest, lp::NackReason::CONGESTION));
461 });
462 face.setInterestFilter("/", bind([&] { hasInterest2 = true; }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500463 advanceClocks(10_ms);
Junxiao Shi79a7a162017-09-09 08:33:57 +0000464
Junxiao Shib55e5d32018-07-18 13:32:00 -0600465 auto interest = makeInterest("/A", false, DEFAULT_INTEREST_LIFETIME, 14333271);
466 face.receive(*interest);
Davide Pesavento0f830802018-01-16 23:58:58 -0500467 advanceClocks(10_ms);
Junxiao Shi859888f2017-09-12 14:29:16 +0000468 BOOST_CHECK(hasInterest1);
469 BOOST_CHECK(hasInterest2);
Junxiao Shi79a7a162017-09-09 08:33:57 +0000470
Junxiao Shi859888f2017-09-12 14:29:16 +0000471 // Nack from first destination is received, should wait for a response from the other destination
472 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0);
Junxiao Shi79a7a162017-09-09 08:33:57 +0000473
Junxiao Shib55e5d32018-07-18 13:32:00 -0600474 face.put(makeNack(*interest, lp::NackReason::NO_ROUTE)); // Nack from second destination
Davide Pesavento0f830802018-01-16 23:58:58 -0500475 advanceClocks(10_ms);
Junxiao Shi859888f2017-09-12 14:29:16 +0000476 BOOST_CHECK_EQUAL(face.sentNacks.size(), 1); // sending Nack after both destinations Nacked
Junxiao Shi79a7a162017-09-09 08:33:57 +0000477 BOOST_CHECK_EQUAL(face.sentNacks.at(0).getReason(), lp::NackReason::CONGESTION); // least severe reason
478
Junxiao Shib55e5d32018-07-18 13:32:00 -0600479 face.put(makeNack(*interest, lp::NackReason::DUPLICATE));
Junxiao Shi79a7a162017-09-09 08:33:57 +0000480 BOOST_CHECK_EQUAL(face.sentNacks.size(), 1); // additional Nacks are ignored
481}
482
Junxiao Shib6828912017-11-20 14:06:32 +0000483BOOST_AUTO_TEST_CASE(PutMultipleNackLoopback)
484{
485 bool hasInterest1 = false, hasNack = false;
486
487 // first InterestFilter allows loopback and should receive Interest
488 face.setInterestFilter("/", [&] (const InterestFilter&, const Interest& interest) {
489 hasInterest1 = true;
490 face.put(makeNack(interest, lp::NackReason::CONGESTION));
491 });
492 // second InterestFilter disallows loopback and should not receive Interest
493 face.setInterestFilter(InterestFilter("/").allowLoopback(false),
494 bind([] { BOOST_ERROR("Unexpected Interest on second InterestFilter"); }));
495
Junxiao Shib55e5d32018-07-18 13:32:00 -0600496 auto interest = makeInterest("/A", false, DEFAULT_INTEREST_LIFETIME, 28395852);
497 face.expressInterest(*interest,
Junxiao Shib6828912017-11-20 14:06:32 +0000498 bind([] { BOOST_FAIL("Unexpected data"); }),
499 [&] (const Interest&, const lp::Nack& nack) {
500 hasNack = true;
501 BOOST_CHECK_EQUAL(nack.getReason(), lp::NackReason::CONGESTION);
502 },
503 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500504 advanceClocks(1_ms);
Junxiao Shib6828912017-11-20 14:06:32 +0000505 BOOST_CHECK_EQUAL(hasInterest1, true); // Interest looped back
506 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1); // Interest sent to forwarder
507 BOOST_CHECK_EQUAL(hasNack, false); // waiting for Nack from forwarder
508
Junxiao Shib55e5d32018-07-18 13:32:00 -0600509 face.receive(makeNack(*interest, lp::NackReason::NO_ROUTE));
Davide Pesavento0f830802018-01-16 23:58:58 -0500510 advanceClocks(1_ms);
Junxiao Shib6828912017-11-20 14:06:32 +0000511 BOOST_CHECK_EQUAL(hasNack, true);
512}
513
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400514BOOST_AUTO_TEST_CASE(SetUnsetInterestFilter)
515{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800516 size_t nInterests = 0;
517 size_t nRegs = 0;
Davide Pesavento720e25c2019-07-14 01:33:52 -0400518 auto hdl = face.setInterestFilter("/Hello/World",
519 bind([&nInterests] { ++nInterests; }),
520 bind([&nRegs] { ++nRegs; }),
521 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500522 advanceClocks(25_ms, 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800523 BOOST_CHECK_EQUAL(nRegs, 1);
524 BOOST_CHECK_EQUAL(nInterests, 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400525
Junxiao Shib55e5d32018-07-18 13:32:00 -0600526 face.receive(*makeInterest("/Hello/World/%21"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500527 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400528
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800529 BOOST_CHECK_EQUAL(nRegs, 1);
530 BOOST_CHECK_EQUAL(nInterests, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400531
Junxiao Shib55e5d32018-07-18 13:32:00 -0600532 face.receive(*makeInterest("/Bye/World/%21"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500533 advanceClocks(10000_ms, 10);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800534 BOOST_CHECK_EQUAL(nInterests, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400535
Junxiao Shib55e5d32018-07-18 13:32:00 -0600536 face.receive(*makeInterest("/Hello/World/%21/2"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500537 advanceClocks(25_ms, 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800538 BOOST_CHECK_EQUAL(nInterests, 2);
539
540 // removing filter
Davide Pesavento720e25c2019-07-14 01:33:52 -0400541 hdl.cancel();
Davide Pesavento0f830802018-01-16 23:58:58 -0500542 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400543
Junxiao Shib55e5d32018-07-18 13:32:00 -0600544 face.receive(*makeInterest("/Hello/World/%21/3"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800545 BOOST_CHECK_EQUAL(nInterests, 2);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400546
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000547 face.unsetInterestFilter(static_cast<const RegisteredPrefixId*>(nullptr));
Davide Pesavento0f830802018-01-16 23:58:58 -0500548 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400549
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000550 face.unsetInterestFilter(static_cast<const InterestFilterId*>(nullptr));
Davide Pesavento0f830802018-01-16 23:58:58 -0500551 advanceClocks(25_ms, 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800552}
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400553
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000554BOOST_AUTO_TEST_CASE(SetInterestFilterEmptyInterestCallback)
555{
556 face.setInterestFilter("/A", nullptr);
Davide Pesavento0f830802018-01-16 23:58:58 -0500557 advanceClocks(1_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000558
559 BOOST_CHECK_NO_THROW(do {
560 face.receive(*makeInterest("/A/1"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500561 advanceClocks(1_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000562 } while (false));
563}
564
Joao Pereira0b3cac52015-07-02 14:49:49 -0400565BOOST_AUTO_TEST_CASE(SetUnsetInterestFilterWithoutSucessCallback)
566{
567 size_t nInterests = 0;
Davide Pesavento720e25c2019-07-14 01:33:52 -0400568 auto hdl = face.setInterestFilter("/Hello/World",
569 bind([&nInterests] { ++nInterests; }),
570 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500571 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400572 BOOST_CHECK_EQUAL(nInterests, 0);
573
Junxiao Shib55e5d32018-07-18 13:32:00 -0600574 face.receive(*makeInterest("/Hello/World/%21"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500575 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400576
577 BOOST_CHECK_EQUAL(nInterests, 1);
578
Junxiao Shib55e5d32018-07-18 13:32:00 -0600579 face.receive(*makeInterest("/Bye/World/%21"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500580 advanceClocks(10000_ms, 10);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400581 BOOST_CHECK_EQUAL(nInterests, 1);
582
Junxiao Shib55e5d32018-07-18 13:32:00 -0600583 face.receive(*makeInterest("/Hello/World/%21/2"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500584 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400585 BOOST_CHECK_EQUAL(nInterests, 2);
586
587 // removing filter
Davide Pesavento720e25c2019-07-14 01:33:52 -0400588 hdl.cancel();
Davide Pesavento0f830802018-01-16 23:58:58 -0500589 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400590
Junxiao Shib55e5d32018-07-18 13:32:00 -0600591 face.receive(*makeInterest("/Hello/World/%21/3"));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400592 BOOST_CHECK_EQUAL(nInterests, 2);
593
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000594 face.unsetInterestFilter(static_cast<const RegisteredPrefixId*>(nullptr));
Davide Pesavento0f830802018-01-16 23:58:58 -0500595 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400596
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000597 face.unsetInterestFilter(static_cast<const InterestFilterId*>(nullptr));
Davide Pesavento0f830802018-01-16 23:58:58 -0500598 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400599}
600
Junxiao Shiec475a72019-01-13 21:53:55 +0000601BOOST_FIXTURE_TEST_CASE(SetInterestFilterFail, FaceFixture<NoPrefixRegReply>)
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800602{
603 // don't enable registration reply
604 size_t nRegFailed = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800605 face.setInterestFilter("/Hello/World",
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000606 bind([] { BOOST_FAIL("Unexpected Interest"); }),
607 bind([] { BOOST_FAIL("Unexpected success of setInterestFilter"); }),
608 bind([&nRegFailed] { ++nRegFailed; }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800609
Davide Pesavento0f830802018-01-16 23:58:58 -0500610 advanceClocks(25_ms, 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800611 BOOST_CHECK_EQUAL(nRegFailed, 0);
612
Davide Pesavento0f830802018-01-16 23:58:58 -0500613 advanceClocks(2000_ms, 5);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800614 BOOST_CHECK_EQUAL(nRegFailed, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400615}
616
Junxiao Shiec475a72019-01-13 21:53:55 +0000617BOOST_FIXTURE_TEST_CASE(SetInterestFilterFailWithoutSuccessCallback, FaceFixture<NoPrefixRegReply>)
Joao Pereira0b3cac52015-07-02 14:49:49 -0400618{
619 // don't enable registration reply
620 size_t nRegFailed = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800621 face.setInterestFilter("/Hello/World",
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000622 bind([] { BOOST_FAIL("Unexpected Interest"); }),
623 bind([&nRegFailed] { ++nRegFailed; }));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400624
Davide Pesavento0f830802018-01-16 23:58:58 -0500625 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400626 BOOST_CHECK_EQUAL(nRegFailed, 0);
627
Davide Pesavento0f830802018-01-16 23:58:58 -0500628 advanceClocks(2000_ms, 5);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400629 BOOST_CHECK_EQUAL(nRegFailed, 1);
630}
631
Junxiao Shiec475a72019-01-13 21:53:55 +0000632BOOST_FIXTURE_TEST_CASE(RegisterUnregisterPrefixFail, FaceFixture<NoPrefixRegReply>)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400633{
Junxiao Shiec475a72019-01-13 21:53:55 +0000634 BOOST_CHECK(!runPrefixReg([&] (const auto& success, const auto& failure) {
635 face.registerPrefix("/Hello/World", success, failure);
636 this->advanceClocks(5_s, 20); // wait for command timeout
637 }));
638}
Junxiao Shi60aaef02019-01-14 04:59:30 +0000639
Junxiao Shiec475a72019-01-13 21:53:55 +0000640BOOST_AUTO_TEST_CASE(RegisterUnregisterPrefixHandle)
641{
642 RegisteredPrefixHandle hdl;
643 BOOST_CHECK(!runPrefixUnreg([&] (const auto& success, const auto& failure) {
644 // despite the "undefined behavior" warning, we try not to crash, but no API guarantee for this
645 hdl.unregister(success, failure);
646 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400647
Junxiao Shiec475a72019-01-13 21:53:55 +0000648 BOOST_CHECK(runPrefixReg([&] (const auto& success, const auto& failure) {
649 hdl = face.registerPrefix("/Hello/World", success, failure);
650 }));
651
652 BOOST_CHECK(runPrefixUnreg([&] (const auto& success, const auto& failure) {
653 hdl.unregister(success, failure);
654 }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800655}
656
657BOOST_AUTO_TEST_CASE(SimilarFilters)
658{
659 size_t nInInterests1 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800660 face.setInterestFilter("/Hello/World",
661 bind([&nInInterests1] { ++nInInterests1; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000662 nullptr,
663 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800664
665 size_t nInInterests2 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800666 face.setInterestFilter("/Hello",
667 bind([&nInInterests2] { ++nInInterests2; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000668 nullptr,
669 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400670
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800671 size_t nInInterests3 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800672 face.setInterestFilter("/Los/Angeles/Lakers",
673 bind([&nInInterests3] { ++nInInterests3; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000674 nullptr,
675 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400676
Davide Pesavento0f830802018-01-16 23:58:58 -0500677 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400678
Junxiao Shib55e5d32018-07-18 13:32:00 -0600679 face.receive(*makeInterest("/Hello/World/%21"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500680 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400681
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800682 BOOST_CHECK_EQUAL(nInInterests1, 1);
683 BOOST_CHECK_EQUAL(nInInterests2, 1);
684 BOOST_CHECK_EQUAL(nInInterests3, 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400685}
686
687BOOST_AUTO_TEST_CASE(SetRegexFilterError)
688{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800689 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
690 [] (const Name&, const Interest&) {
691 BOOST_FAIL("InterestFilter::Error should have been triggered");
692 },
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000693 nullptr,
694 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400695
Davide Pesavento0f830802018-01-16 23:58:58 -0500696 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400697
Junxiao Shib55e5d32018-07-18 13:32:00 -0600698 BOOST_REQUIRE_THROW(face.receive(*makeInterest("/Hello/World/XXX/b/c")), InterestFilter::Error);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400699}
700
701BOOST_AUTO_TEST_CASE(SetRegexFilter)
702{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800703 size_t nInInterests = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800704 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
705 bind([&nInInterests] { ++nInInterests; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000706 nullptr,
707 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400708
Davide Pesavento0f830802018-01-16 23:58:58 -0500709 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400710
Junxiao Shib55e5d32018-07-18 13:32:00 -0600711 face.receive(*makeInterest("/Hello/World/a")); // shouldn't match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400712 BOOST_CHECK_EQUAL(nInInterests, 0);
713
Junxiao Shib55e5d32018-07-18 13:32:00 -0600714 face.receive(*makeInterest("/Hello/World/a/b")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400715 BOOST_CHECK_EQUAL(nInInterests, 1);
716
Junxiao Shib55e5d32018-07-18 13:32:00 -0600717 face.receive(*makeInterest("/Hello/World/a/b/c")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400718 BOOST_CHECK_EQUAL(nInInterests, 2);
719
Junxiao Shib55e5d32018-07-18 13:32:00 -0600720 face.receive(*makeInterest("/Hello/World/a/b/d")); // should not match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400721 BOOST_CHECK_EQUAL(nInInterests, 2);
722}
723
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400724BOOST_AUTO_TEST_CASE(SetRegexFilterAndRegister)
725{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800726 size_t nInInterests = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800727 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
728 bind([&nInInterests] { ++nInInterests; }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400729
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800730 size_t nRegSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800731 face.registerPrefix("/Hello/World",
732 bind([&nRegSuccesses] { ++nRegSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000733 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400734
Davide Pesavento0f830802018-01-16 23:58:58 -0500735 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400736 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
737
Junxiao Shib55e5d32018-07-18 13:32:00 -0600738 face.receive(*makeInterest("/Hello/World/a")); // shouldn't match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400739 BOOST_CHECK_EQUAL(nInInterests, 0);
740
Junxiao Shib55e5d32018-07-18 13:32:00 -0600741 face.receive(*makeInterest("/Hello/World/a/b")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400742 BOOST_CHECK_EQUAL(nInInterests, 1);
743
Junxiao Shib55e5d32018-07-18 13:32:00 -0600744 face.receive(*makeInterest("/Hello/World/a/b/c")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400745 BOOST_CHECK_EQUAL(nInInterests, 2);
746
Junxiao Shib55e5d32018-07-18 13:32:00 -0600747 face.receive(*makeInterest("/Hello/World/a/b/d")); // should not match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400748 BOOST_CHECK_EQUAL(nInInterests, 2);
749}
750
Junxiao Shiec475a72019-01-13 21:53:55 +0000751BOOST_FIXTURE_TEST_CASE(SetInterestFilterNoReg, FaceFixture<NoPrefixRegReply>) // Bug 2318
Junxiao Shia1ea5062014-12-27 22:33:39 -0700752{
753 // This behavior is specific to DummyClientFace.
754 // Regular Face won't accept incoming packets until something is sent.
755
756 int hit = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800757 face.setInterestFilter(Name("/"), bind([&hit] { ++hit; }));
758 face.processEvents(time::milliseconds(-1));
Junxiao Shia1ea5062014-12-27 22:33:39 -0700759
Junxiao Shib55e5d32018-07-18 13:32:00 -0600760 face.receive(*makeInterest("/A"));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800761 face.processEvents(time::milliseconds(-1));
Junxiao Shia1ea5062014-12-27 22:33:39 -0700762
763 BOOST_CHECK_EQUAL(hit, 1);
764}
765
Junxiao Shi60aaef02019-01-14 04:59:30 +0000766BOOST_AUTO_TEST_CASE(SetInterestFilterHandle)
767{
768 int hit = 0;
769 auto hdl = face.setInterestFilter(Name("/"), bind([&hit] { ++hit; }));
770 face.processEvents(-1_ms);
771
772 face.receive(*makeInterest("/A"));
773 face.processEvents(-1_ms);
774 BOOST_CHECK_EQUAL(hit, 1);
775
776 hdl.cancel();
777 face.processEvents(-1_ms);
778
779 face.receive(*makeInterest("/B"));
780 face.processEvents(-1_ms);
781 BOOST_CHECK_EQUAL(hit, 1);
782}
783
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000784BOOST_AUTO_TEST_SUITE_END() // Producer
785
Junxiao Shiae0b4182016-08-08 22:53:17 +0000786BOOST_AUTO_TEST_SUITE(IoRoutines)
787
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500788BOOST_AUTO_TEST_CASE(ProcessEvents)
789{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800790 face.processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500791
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800792 size_t nRegSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800793 face.registerPrefix("/Hello/World",
794 bind([&nRegSuccesses] { ++nRegSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000795 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500796
797 // io_service::poll() without reset
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800798 face.getIoService().poll();
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500799 BOOST_CHECK_EQUAL(nRegSuccesses, 0);
800
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800801 face.processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500802 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
803}
804
Junxiao Shiae0b4182016-08-08 22:53:17 +0000805BOOST_AUTO_TEST_CASE(DestroyWithoutProcessEvents) // Bug 3248
806{
807 auto face2 = make_unique<Face>(io);
808 face2.reset();
809
810 io.poll(); // should not crash
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100811
812 // avoid "test case [...] did not check any assertions" message from Boost.Test
813 BOOST_CHECK(true);
Junxiao Shiae0b4182016-08-08 22:53:17 +0000814}
815
816BOOST_AUTO_TEST_SUITE_END() // IoRoutines
817
818BOOST_AUTO_TEST_SUITE(Transport)
819
820using ndn::Transport;
821
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700822struct PibDirWithDefaultTpm
823{
824 const std::string PATH = "build/keys-with-default-tpm";
825};
826
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800827BOOST_FIXTURE_TEST_CASE(FaceTransport, IdentityManagementTimeFixture)
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800828{
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800829 BOOST_CHECK(Face().getTransport() != nullptr);
830
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800831 BOOST_CHECK(Face(shared_ptr<Transport>()).getTransport() != nullptr);
832 BOOST_CHECK(Face(shared_ptr<Transport>(), io).getTransport() != nullptr);
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800833 BOOST_CHECK(Face(shared_ptr<Transport>(), io, m_keyChain).getTransport() != nullptr);
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800834
835 auto transport = make_shared<TcpTransport>("localhost", "6363"); // no real io operations will be scheduled
836 BOOST_CHECK(Face(transport).getTransport() == transport);
837 BOOST_CHECK(Face(transport, io).getTransport() == transport);
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800838 BOOST_CHECK(Face(transport, io, m_keyChain).getTransport() == transport);
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800839}
840
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700841class WithEnv : private IdentityManagementTimeFixture
842{
843public:
844 WithEnv()
845 {
846 if (getenv("NDN_CLIENT_TRANSPORT") != nullptr) {
847 m_oldTransport = getenv("NDN_CLIENT_TRANSPORT");
848 unsetenv("NDN_CLIENT_TRANSPORT");
849 }
850 }
851
852 void
853 configure(const std::string& faceUri)
854 {
855 setenv("NDN_CLIENT_TRANSPORT", faceUri.c_str(), true);
856 }
857
858 ~WithEnv()
859 {
860 if (!m_oldTransport.empty()) {
861 setenv("NDN_CLIENT_TRANSPORT", m_oldTransport.c_str(), true);
862 }
863 else {
864 unsetenv("NDN_CLIENT_TRANSPORT");
865 }
866 }
867
868private:
869 std::string m_oldTransport;
870};
871
872class WithConfig : private TestHomeFixture<DefaultPibDir>
873{
874public:
875 void
876 configure(const std::string& faceUri)
877 {
878 createClientConf({"transport=" + faceUri});
879 }
880};
881
882class WithEnvAndConfig : public WithEnv, public WithConfig
883{
884};
885
886typedef boost::mpl::vector<WithEnv, WithConfig> ConfigOptions;
887
888BOOST_FIXTURE_TEST_CASE(NoConfig, WithEnvAndConfig) // fixture configures test HOME and PIB/TPM path
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700889{
890 shared_ptr<Face> face;
891 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
892 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700893}
894
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700895BOOST_FIXTURE_TEST_CASE_TEMPLATE(Unix, T, ConfigOptions, T)
896{
897 this->configure("unix://some/path");
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700898
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700899 shared_ptr<Face> face;
900 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
901 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
902}
903
904BOOST_FIXTURE_TEST_CASE_TEMPLATE(Tcp, T, ConfigOptions, T)
905{
906 this->configure("tcp://127.0.0.1:6000");
907
908 shared_ptr<Face> face;
909 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
910 BOOST_CHECK(dynamic_pointer_cast<TcpTransport>(face->getTransport()) != nullptr);
911}
912
913BOOST_FIXTURE_TEST_CASE_TEMPLATE(WrongTransport, T, ConfigOptions, T)
914{
915 this->configure("wrong-transport:");
916
917 BOOST_CHECK_THROW(make_shared<Face>(), ConfigFile::Error);
918}
919
920BOOST_FIXTURE_TEST_CASE_TEMPLATE(WrongUri, T, ConfigOptions, T)
921{
922 this->configure("wrong-uri");
923
924 BOOST_CHECK_THROW(make_shared<Face>(), ConfigFile::Error);
925}
926
927BOOST_FIXTURE_TEST_CASE(EnvOverride, WithEnvAndConfig)
928{
929 this->WithEnv::configure("tcp://127.0.0.1:6000");
930 this->WithConfig::configure("unix://some/path");
931
932 shared_ptr<Face> face;
933 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
934 BOOST_CHECK(dynamic_pointer_cast<TcpTransport>(face->getTransport()) != nullptr);
935}
936
937BOOST_FIXTURE_TEST_CASE(ExplicitTransport, WithEnvAndConfig)
938{
939 this->WithEnv::configure("wrong-uri");
940 this->WithConfig::configure("wrong-transport:");
941
942 auto transport = make_shared<UnixTransport>("unix://some/path");
943 shared_ptr<Face> face;
944 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>(transport));
945 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
946}
947
Junxiao Shiae0b4182016-08-08 22:53:17 +0000948BOOST_AUTO_TEST_SUITE_END() // Transport
949
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700950BOOST_AUTO_TEST_SUITE_END() // TestFace
951
952} // namespace tests
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400953} // namespace ndn