blob: 43b4db5a052c2f590323ceb371ace2d4c724efde [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(RemovePendingInterestId)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400271{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800272 const PendingInterestId* interestId =
Junxiao Shib55e5d32018-07-18 13:32:00 -0600273 face.expressInterest(*makeInterest("/Hello/World", true, 50_ms),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000274 bind([] { BOOST_FAIL("Unexpected data"); }),
275 bind([] { BOOST_FAIL("Unexpected nack"); }),
276 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500277 advanceClocks(10_ms);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400278
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800279 face.removePendingInterest(interestId);
Davide Pesavento0f830802018-01-16 23:58:58 -0500280 advanceClocks(10_ms);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400281
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000282 face.receive(*makeData("/Hello/World/%21"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500283 advanceClocks(200_ms, 5);
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100284
285 // avoid "test case [...] did not check any assertions" message from Boost.Test
286 BOOST_CHECK(true);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400287}
288
Junxiao Shi80609d42019-01-29 18:15:22 +0000289BOOST_AUTO_TEST_CASE(CancelPendingInterestHandle)
290{
291 auto hdl = face.expressInterest(*makeInterest("/Hello/World", true, 50_ms),
292 bind([] { BOOST_FAIL("Unexpected data"); }),
293 bind([] { BOOST_FAIL("Unexpected nack"); }),
294 bind([] { BOOST_FAIL("Unexpected timeout"); }));
295 advanceClocks(10_ms);
296
297 hdl.cancel();
298 advanceClocks(10_ms);
299
300 face.receive(*makeData("/Hello/World/%21"));
301 advanceClocks(200_ms, 5);
302
303 // avoid "test case [...] did not check any assertions" message from Boost.Test
304 BOOST_CHECK(true);
305}
306
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000307BOOST_AUTO_TEST_CASE(RemoveAllPendingInterests)
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500308{
Junxiao Shib55e5d32018-07-18 13:32:00 -0600309 face.expressInterest(*makeInterest("/Hello/World/0", false, 50_ms),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800310 bind([] { BOOST_FAIL("Unexpected data"); }),
311 bind([] { BOOST_FAIL("Unexpected nack"); }),
312 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500313
Junxiao Shib55e5d32018-07-18 13:32:00 -0600314 face.expressInterest(*makeInterest("/Hello/World/1", false, 50_ms),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800315 bind([] { BOOST_FAIL("Unexpected data"); }),
316 bind([] { BOOST_FAIL("Unexpected nack"); }),
317 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500318
Davide Pesavento0f830802018-01-16 23:58:58 -0500319 advanceClocks(10_ms);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500320
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800321 face.removeAllPendingInterests();
Davide Pesavento0f830802018-01-16 23:58:58 -0500322 advanceClocks(10_ms);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500323
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800324 BOOST_CHECK_EQUAL(face.getNPendingInterests(), 0);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500325
Junxiao Shi85d90832016-08-04 03:19:46 +0000326 face.receive(*makeData("/Hello/World/0"));
327 face.receive(*makeData("/Hello/World/1"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500328 advanceClocks(200_ms, 5);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500329}
330
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000331BOOST_AUTO_TEST_CASE(DestructionWithoutCancellingPendingInterests) // Bug #2518
332{
333 {
334 DummyClientFace face2(io, m_keyChain);
Junxiao Shib55e5d32018-07-18 13:32:00 -0600335 face2.expressInterest(*makeInterest("/Hello/World", false, 50_ms),
Alexander Afanasyeve6835fe2017-01-19 20:05:01 -0800336 nullptr, nullptr, nullptr);
Davide Pesavento0f830802018-01-16 23:58:58 -0500337 advanceClocks(50_ms, 2);
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000338 }
339
Davide Pesavento0f830802018-01-16 23:58:58 -0500340 advanceClocks(50_ms, 2); // should not crash
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100341
342 // avoid "test case [...] did not check any assertions" message from Boost.Test
343 BOOST_CHECK(true);
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000344}
345
Ashlesh Gawandee2404222018-05-21 18:30:24 -0500346BOOST_AUTO_TEST_CASE(DataCallbackPutData) // Bug 4596
347{
Junxiao Shib55e5d32018-07-18 13:32:00 -0600348 face.expressInterest(*makeInterest("/localhost/notification/1"),
Ashlesh Gawandee2404222018-05-21 18:30:24 -0500349 [&] (const Interest& i, const Data& d) {
350 face.put(*makeData("/chronosync/sampleDigest/1"));
351 }, nullptr, nullptr);
352 advanceClocks(10_ms);
353 BOOST_CHECK_EQUAL(face.sentInterests.back().getName(), "/localhost/notification/1");
354
Junxiao Shib55e5d32018-07-18 13:32:00 -0600355 face.receive(*makeInterest("/chronosync/sampleDigest", true));
Ashlesh Gawandee2404222018-05-21 18:30:24 -0500356 advanceClocks(10_ms);
357
358 face.put(*makeData("/localhost/notification/1"));
359 advanceClocks(10_ms);
360 BOOST_CHECK_EQUAL(face.sentData.back().getName(), "/chronosync/sampleDigest/1");
361}
362
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000363BOOST_AUTO_TEST_SUITE_END() // Consumer
364
365BOOST_AUTO_TEST_SUITE(Producer)
366
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000367BOOST_AUTO_TEST_CASE(PutData)
368{
369 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
370
371 Data data("/4g7xxcuEow/KFvK5Kf2m");
372 signData(data);
373 face.put(data);
374
375 lp::CachePolicy cachePolicy;
376 cachePolicy.setPolicy(lp::CachePolicyType::NO_CACHE);
377 data.setTag(make_shared<lp::CachePolicyTag>(cachePolicy));
Eric Newberry4d261b62016-11-10 13:40:09 -0700378 data.setTag(make_shared<lp::CongestionMarkTag>(1));
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000379 face.put(data);
380
Davide Pesavento0f830802018-01-16 23:58:58 -0500381 advanceClocks(10_ms);
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000382 BOOST_REQUIRE_EQUAL(face.sentData.size(), 2);
383 BOOST_CHECK(face.sentData[0].getTag<lp::CachePolicyTag>() == nullptr);
Eric Newberry4d261b62016-11-10 13:40:09 -0700384 BOOST_CHECK(face.sentData[0].getTag<lp::CongestionMarkTag>() == nullptr);
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000385 BOOST_CHECK(face.sentData[1].getTag<lp::CachePolicyTag>() != nullptr);
Eric Newberry4d261b62016-11-10 13:40:09 -0700386 BOOST_CHECK(face.sentData[1].getTag<lp::CongestionMarkTag>() != nullptr);
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000387}
388
Junxiao Shib6828912017-11-20 14:06:32 +0000389BOOST_AUTO_TEST_CASE(PutDataLoopback)
390{
391 bool hasInterest1 = false, hasData = false;
392
393 // first InterestFilter allows loopback and should receive Interest
394 face.setInterestFilter("/", [&] (const InterestFilter&, const Interest& interest) {
395 hasInterest1 = true;
396 // do not respond with Data right away, so Face must send Interest to forwarder
397 });
398 // second InterestFilter disallows loopback and should not receive Interest
399 face.setInterestFilter(InterestFilter("/").allowLoopback(false),
400 bind([] { BOOST_ERROR("Unexpected Interest on second InterestFilter"); }));
401
Junxiao Shib55e5d32018-07-18 13:32:00 -0600402 face.expressInterest(*makeInterest("/A", true),
Junxiao Shib6828912017-11-20 14:06:32 +0000403 bind([&] { hasData = true; }),
404 bind([] { BOOST_FAIL("Unexpected nack"); }),
405 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500406 advanceClocks(1_ms);
Junxiao Shib6828912017-11-20 14:06:32 +0000407 BOOST_CHECK_EQUAL(hasInterest1, true); // Interest looped back
408 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1); // Interest sent to forwarder
409 BOOST_CHECK_EQUAL(hasData, false); // waiting for Data
410
411 face.put(*makeData("/A/B")); // first InterestFilter responds with Data
Davide Pesavento0f830802018-01-16 23:58:58 -0500412 advanceClocks(1_ms);
Junxiao Shib6828912017-11-20 14:06:32 +0000413 BOOST_CHECK_EQUAL(hasData, true);
414 BOOST_CHECK_EQUAL(face.sentData.size(), 0); // do not spill Data to forwarder
415}
416
Junxiao Shi859888f2017-09-12 14:29:16 +0000417BOOST_AUTO_TEST_CASE(PutMultipleData)
418{
419 bool hasInterest1 = false;
420 // register two Interest destinations
421 face.setInterestFilter("/", bind([&] {
422 hasInterest1 = true;
423 // sending Data right away from the first destination, don't care whether Interest goes to second destination
424 face.put(*makeData("/A/B"));
425 }));
426 face.setInterestFilter("/", bind([]{}));
Davide Pesavento0f830802018-01-16 23:58:58 -0500427 advanceClocks(10_ms);
Junxiao Shi859888f2017-09-12 14:29:16 +0000428
Junxiao Shib55e5d32018-07-18 13:32:00 -0600429 face.receive(*makeInterest("/A", true));
Davide Pesavento0f830802018-01-16 23:58:58 -0500430 advanceClocks(10_ms);
Junxiao Shi859888f2017-09-12 14:29:16 +0000431 BOOST_CHECK(hasInterest1);
432 BOOST_CHECK_EQUAL(face.sentData.size(), 1);
433 BOOST_CHECK_EQUAL(face.sentData.at(0).getName(), "/A/B");
434
435 face.put(*makeData("/A/C"));
436 BOOST_CHECK_EQUAL(face.sentData.size(), 1); // additional Data are ignored
437}
438
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000439BOOST_AUTO_TEST_CASE(PutNack)
440{
Junxiao Shi79a7a162017-09-09 08:33:57 +0000441 face.setInterestFilter("/", bind([]{})); // register one Interest destination so that face can accept Nacks
Davide Pesavento0f830802018-01-16 23:58:58 -0500442 advanceClocks(10_ms);
Junxiao Shi79a7a162017-09-09 08:33:57 +0000443
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000444 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0);
445
Junxiao Shib55e5d32018-07-18 13:32:00 -0600446 face.put(makeNack(*makeInterest("/unsolicited", false, DEFAULT_INTEREST_LIFETIME, 18645250),
447 lp::NackReason::NO_ROUTE));
Davide Pesavento0f830802018-01-16 23:58:58 -0500448 advanceClocks(10_ms);
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000449 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0); // unsolicited Nack would not be sent
Eric Newberry4d261b62016-11-10 13:40:09 -0700450
Junxiao Shib55e5d32018-07-18 13:32:00 -0600451 auto interest1 = makeInterest("/Hello/World", false, DEFAULT_INTEREST_LIFETIME, 14247162);
452 face.receive(*interest1);
453 auto interest2 = makeInterest("/another/prefix", false, DEFAULT_INTEREST_LIFETIME, 92203002);
454 face.receive(*interest2);
Davide Pesavento0f830802018-01-16 23:58:58 -0500455 advanceClocks(10_ms);
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000456
Junxiao Shib55e5d32018-07-18 13:32:00 -0600457 face.put(makeNack(*interest1, lp::NackReason::DUPLICATE));
Davide Pesavento0f830802018-01-16 23:58:58 -0500458 advanceClocks(10_ms);
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000459 BOOST_REQUIRE_EQUAL(face.sentNacks.size(), 1);
460 BOOST_CHECK_EQUAL(face.sentNacks[0].getReason(), lp::NackReason::DUPLICATE);
461 BOOST_CHECK(face.sentNacks[0].getTag<lp::CongestionMarkTag>() == nullptr);
462
Junxiao Shib55e5d32018-07-18 13:32:00 -0600463 auto nack = makeNack(*interest2, lp::NackReason::NO_ROUTE);
Eric Newberry4d261b62016-11-10 13:40:09 -0700464 nack.setTag(make_shared<lp::CongestionMarkTag>(1));
465 face.put(nack);
Davide Pesavento0f830802018-01-16 23:58:58 -0500466 advanceClocks(10_ms);
Eric Newberry4d261b62016-11-10 13:40:09 -0700467 BOOST_REQUIRE_EQUAL(face.sentNacks.size(), 2);
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000468 BOOST_CHECK_EQUAL(face.sentNacks[1].getReason(), lp::NackReason::NO_ROUTE);
Eric Newberry4d261b62016-11-10 13:40:09 -0700469 BOOST_CHECK(face.sentNacks[1].getTag<lp::CongestionMarkTag>() != nullptr);
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000470}
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500471
Junxiao Shi79a7a162017-09-09 08:33:57 +0000472BOOST_AUTO_TEST_CASE(PutMultipleNack)
473{
Junxiao Shi859888f2017-09-12 14:29:16 +0000474 bool hasInterest1 = false, hasInterest2 = false;
475 // register two Interest destinations
476 face.setInterestFilter("/", [&] (const InterestFilter&, const Interest& interest) {
477 hasInterest1 = true;
478 // sending Nack right away from the first destination, Interest should still go to second destination
479 face.put(makeNack(interest, lp::NackReason::CONGESTION));
480 });
481 face.setInterestFilter("/", bind([&] { hasInterest2 = true; }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500482 advanceClocks(10_ms);
Junxiao Shi79a7a162017-09-09 08:33:57 +0000483
Junxiao Shib55e5d32018-07-18 13:32:00 -0600484 auto interest = makeInterest("/A", false, DEFAULT_INTEREST_LIFETIME, 14333271);
485 face.receive(*interest);
Davide Pesavento0f830802018-01-16 23:58:58 -0500486 advanceClocks(10_ms);
Junxiao Shi859888f2017-09-12 14:29:16 +0000487 BOOST_CHECK(hasInterest1);
488 BOOST_CHECK(hasInterest2);
Junxiao Shi79a7a162017-09-09 08:33:57 +0000489
Junxiao Shi859888f2017-09-12 14:29:16 +0000490 // Nack from first destination is received, should wait for a response from the other destination
491 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0);
Junxiao Shi79a7a162017-09-09 08:33:57 +0000492
Junxiao Shib55e5d32018-07-18 13:32:00 -0600493 face.put(makeNack(*interest, lp::NackReason::NO_ROUTE)); // Nack from second destination
Davide Pesavento0f830802018-01-16 23:58:58 -0500494 advanceClocks(10_ms);
Junxiao Shi859888f2017-09-12 14:29:16 +0000495 BOOST_CHECK_EQUAL(face.sentNacks.size(), 1); // sending Nack after both destinations Nacked
Junxiao Shi79a7a162017-09-09 08:33:57 +0000496 BOOST_CHECK_EQUAL(face.sentNacks.at(0).getReason(), lp::NackReason::CONGESTION); // least severe reason
497
Junxiao Shib55e5d32018-07-18 13:32:00 -0600498 face.put(makeNack(*interest, lp::NackReason::DUPLICATE));
Junxiao Shi79a7a162017-09-09 08:33:57 +0000499 BOOST_CHECK_EQUAL(face.sentNacks.size(), 1); // additional Nacks are ignored
500}
501
Junxiao Shib6828912017-11-20 14:06:32 +0000502BOOST_AUTO_TEST_CASE(PutMultipleNackLoopback)
503{
504 bool hasInterest1 = false, hasNack = false;
505
506 // first InterestFilter allows loopback and should receive Interest
507 face.setInterestFilter("/", [&] (const InterestFilter&, const Interest& interest) {
508 hasInterest1 = true;
509 face.put(makeNack(interest, lp::NackReason::CONGESTION));
510 });
511 // second InterestFilter disallows loopback and should not receive Interest
512 face.setInterestFilter(InterestFilter("/").allowLoopback(false),
513 bind([] { BOOST_ERROR("Unexpected Interest on second InterestFilter"); }));
514
Junxiao Shib55e5d32018-07-18 13:32:00 -0600515 auto interest = makeInterest("/A", false, DEFAULT_INTEREST_LIFETIME, 28395852);
516 face.expressInterest(*interest,
Junxiao Shib6828912017-11-20 14:06:32 +0000517 bind([] { BOOST_FAIL("Unexpected data"); }),
518 [&] (const Interest&, const lp::Nack& nack) {
519 hasNack = true;
520 BOOST_CHECK_EQUAL(nack.getReason(), lp::NackReason::CONGESTION);
521 },
522 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500523 advanceClocks(1_ms);
Junxiao Shib6828912017-11-20 14:06:32 +0000524 BOOST_CHECK_EQUAL(hasInterest1, true); // Interest looped back
525 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1); // Interest sent to forwarder
526 BOOST_CHECK_EQUAL(hasNack, false); // waiting for Nack from forwarder
527
Junxiao Shib55e5d32018-07-18 13:32:00 -0600528 face.receive(makeNack(*interest, lp::NackReason::NO_ROUTE));
Davide Pesavento0f830802018-01-16 23:58:58 -0500529 advanceClocks(1_ms);
Junxiao Shib6828912017-11-20 14:06:32 +0000530 BOOST_CHECK_EQUAL(hasNack, true);
531}
532
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400533BOOST_AUTO_TEST_CASE(SetUnsetInterestFilter)
534{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800535 size_t nInterests = 0;
536 size_t nRegs = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400537 const RegisteredPrefixId* regPrefixId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800538 face.setInterestFilter("/Hello/World",
539 bind([&nInterests] { ++nInterests; }),
540 bind([&nRegs] { ++nRegs; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000541 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500542 advanceClocks(25_ms, 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800543 BOOST_CHECK_EQUAL(nRegs, 1);
544 BOOST_CHECK_EQUAL(nInterests, 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400545
Junxiao Shib55e5d32018-07-18 13:32:00 -0600546 face.receive(*makeInterest("/Hello/World/%21"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500547 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400548
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800549 BOOST_CHECK_EQUAL(nRegs, 1);
550 BOOST_CHECK_EQUAL(nInterests, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400551
Junxiao Shib55e5d32018-07-18 13:32:00 -0600552 face.receive(*makeInterest("/Bye/World/%21"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500553 advanceClocks(10000_ms, 10);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800554 BOOST_CHECK_EQUAL(nInterests, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400555
Junxiao Shib55e5d32018-07-18 13:32:00 -0600556 face.receive(*makeInterest("/Hello/World/%21/2"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500557 advanceClocks(25_ms, 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800558 BOOST_CHECK_EQUAL(nInterests, 2);
559
560 // removing filter
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800561 face.unsetInterestFilter(regPrefixId);
Davide Pesavento0f830802018-01-16 23:58:58 -0500562 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400563
Junxiao Shib55e5d32018-07-18 13:32:00 -0600564 face.receive(*makeInterest("/Hello/World/%21/3"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800565 BOOST_CHECK_EQUAL(nInterests, 2);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400566
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000567 face.unsetInterestFilter(static_cast<const RegisteredPrefixId*>(nullptr));
Davide Pesavento0f830802018-01-16 23:58:58 -0500568 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400569
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000570 face.unsetInterestFilter(static_cast<const InterestFilterId*>(nullptr));
Davide Pesavento0f830802018-01-16 23:58:58 -0500571 advanceClocks(25_ms, 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800572}
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400573
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000574BOOST_AUTO_TEST_CASE(SetInterestFilterEmptyInterestCallback)
575{
576 face.setInterestFilter("/A", nullptr);
Davide Pesavento0f830802018-01-16 23:58:58 -0500577 advanceClocks(1_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000578
579 BOOST_CHECK_NO_THROW(do {
580 face.receive(*makeInterest("/A/1"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500581 advanceClocks(1_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000582 } while (false));
583}
584
Joao Pereira0b3cac52015-07-02 14:49:49 -0400585BOOST_AUTO_TEST_CASE(SetUnsetInterestFilterWithoutSucessCallback)
586{
587 size_t nInterests = 0;
588 const RegisteredPrefixId* regPrefixId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800589 face.setInterestFilter("/Hello/World",
590 bind([&nInterests] { ++nInterests; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000591 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500592 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400593 BOOST_CHECK_EQUAL(nInterests, 0);
594
Junxiao Shib55e5d32018-07-18 13:32:00 -0600595 face.receive(*makeInterest("/Hello/World/%21"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500596 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400597
598 BOOST_CHECK_EQUAL(nInterests, 1);
599
Junxiao Shib55e5d32018-07-18 13:32:00 -0600600 face.receive(*makeInterest("/Bye/World/%21"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500601 advanceClocks(10000_ms, 10);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400602 BOOST_CHECK_EQUAL(nInterests, 1);
603
Junxiao Shib55e5d32018-07-18 13:32:00 -0600604 face.receive(*makeInterest("/Hello/World/%21/2"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500605 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400606 BOOST_CHECK_EQUAL(nInterests, 2);
607
608 // removing filter
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800609 face.unsetInterestFilter(regPrefixId);
Davide Pesavento0f830802018-01-16 23:58:58 -0500610 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400611
Junxiao Shib55e5d32018-07-18 13:32:00 -0600612 face.receive(*makeInterest("/Hello/World/%21/3"));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400613 BOOST_CHECK_EQUAL(nInterests, 2);
614
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000615 face.unsetInterestFilter(static_cast<const RegisteredPrefixId*>(nullptr));
Davide Pesavento0f830802018-01-16 23:58:58 -0500616 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400617
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000618 face.unsetInterestFilter(static_cast<const InterestFilterId*>(nullptr));
Davide Pesavento0f830802018-01-16 23:58:58 -0500619 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400620}
621
Junxiao Shiec475a72019-01-13 21:53:55 +0000622BOOST_FIXTURE_TEST_CASE(SetInterestFilterFail, FaceFixture<NoPrefixRegReply>)
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800623{
624 // don't enable registration reply
625 size_t nRegFailed = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800626 face.setInterestFilter("/Hello/World",
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000627 bind([] { BOOST_FAIL("Unexpected Interest"); }),
628 bind([] { BOOST_FAIL("Unexpected success of setInterestFilter"); }),
629 bind([&nRegFailed] { ++nRegFailed; }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800630
Davide Pesavento0f830802018-01-16 23:58:58 -0500631 advanceClocks(25_ms, 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800632 BOOST_CHECK_EQUAL(nRegFailed, 0);
633
Davide Pesavento0f830802018-01-16 23:58:58 -0500634 advanceClocks(2000_ms, 5);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800635 BOOST_CHECK_EQUAL(nRegFailed, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400636}
637
Junxiao Shiec475a72019-01-13 21:53:55 +0000638BOOST_FIXTURE_TEST_CASE(SetInterestFilterFailWithoutSuccessCallback, FaceFixture<NoPrefixRegReply>)
Joao Pereira0b3cac52015-07-02 14:49:49 -0400639{
640 // don't enable registration reply
641 size_t nRegFailed = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800642 face.setInterestFilter("/Hello/World",
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000643 bind([] { BOOST_FAIL("Unexpected Interest"); }),
644 bind([&nRegFailed] { ++nRegFailed; }));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400645
Davide Pesavento0f830802018-01-16 23:58:58 -0500646 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400647 BOOST_CHECK_EQUAL(nRegFailed, 0);
648
Davide Pesavento0f830802018-01-16 23:58:58 -0500649 advanceClocks(2000_ms, 5);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400650 BOOST_CHECK_EQUAL(nRegFailed, 1);
651}
652
Junxiao Shiec475a72019-01-13 21:53:55 +0000653BOOST_AUTO_TEST_CASE(RegisterUnregisterPrefixFunc)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400654{
Junxiao Shiec475a72019-01-13 21:53:55 +0000655 const RegisteredPrefixId* regPrefixId = nullptr;
656 BOOST_CHECK(runPrefixReg([&] (const auto& success, const auto& failure) {
657 regPrefixId = face.registerPrefix("/Hello/World", success, failure);
658 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400659
Junxiao Shiec475a72019-01-13 21:53:55 +0000660 BOOST_CHECK(runPrefixUnreg([&] (const auto& success, const auto& failure) {
661 face.unregisterPrefix(regPrefixId, success, failure);
662 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400663}
664
Junxiao Shiec475a72019-01-13 21:53:55 +0000665BOOST_FIXTURE_TEST_CASE(RegisterUnregisterPrefixFail, FaceFixture<NoPrefixRegReply>)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400666{
Junxiao Shiec475a72019-01-13 21:53:55 +0000667 BOOST_CHECK(!runPrefixReg([&] (const auto& success, const auto& failure) {
668 face.registerPrefix("/Hello/World", success, failure);
669 this->advanceClocks(5_s, 20); // wait for command timeout
670 }));
671}
Junxiao Shi60aaef02019-01-14 04:59:30 +0000672
Junxiao Shiec475a72019-01-13 21:53:55 +0000673BOOST_AUTO_TEST_CASE(RegisterUnregisterPrefixHandle)
674{
675 RegisteredPrefixHandle hdl;
676 BOOST_CHECK(!runPrefixUnreg([&] (const auto& success, const auto& failure) {
677 // despite the "undefined behavior" warning, we try not to crash, but no API guarantee for this
678 hdl.unregister(success, failure);
679 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400680
Junxiao Shiec475a72019-01-13 21:53:55 +0000681 BOOST_CHECK(runPrefixReg([&] (const auto& success, const auto& failure) {
682 hdl = face.registerPrefix("/Hello/World", success, failure);
683 }));
684
685 BOOST_CHECK(runPrefixUnreg([&] (const auto& success, const auto& failure) {
686 hdl.unregister(success, failure);
687 }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800688}
689
690BOOST_AUTO_TEST_CASE(SimilarFilters)
691{
692 size_t nInInterests1 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800693 face.setInterestFilter("/Hello/World",
694 bind([&nInInterests1] { ++nInInterests1; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000695 nullptr,
696 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800697
698 size_t nInInterests2 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800699 face.setInterestFilter("/Hello",
700 bind([&nInInterests2] { ++nInInterests2; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000701 nullptr,
702 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400703
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800704 size_t nInInterests3 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800705 face.setInterestFilter("/Los/Angeles/Lakers",
706 bind([&nInInterests3] { ++nInInterests3; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000707 nullptr,
708 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400709
Davide Pesavento0f830802018-01-16 23:58:58 -0500710 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400711
Junxiao Shib55e5d32018-07-18 13:32:00 -0600712 face.receive(*makeInterest("/Hello/World/%21"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500713 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400714
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800715 BOOST_CHECK_EQUAL(nInInterests1, 1);
716 BOOST_CHECK_EQUAL(nInInterests2, 1);
717 BOOST_CHECK_EQUAL(nInInterests3, 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400718}
719
720BOOST_AUTO_TEST_CASE(SetRegexFilterError)
721{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800722 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
723 [] (const Name&, const Interest&) {
724 BOOST_FAIL("InterestFilter::Error should have been triggered");
725 },
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000726 nullptr,
727 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400728
Davide Pesavento0f830802018-01-16 23:58:58 -0500729 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400730
Junxiao Shib55e5d32018-07-18 13:32:00 -0600731 BOOST_REQUIRE_THROW(face.receive(*makeInterest("/Hello/World/XXX/b/c")), InterestFilter::Error);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400732}
733
734BOOST_AUTO_TEST_CASE(SetRegexFilter)
735{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800736 size_t nInInterests = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800737 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
738 bind([&nInInterests] { ++nInInterests; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000739 nullptr,
740 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400741
Davide Pesavento0f830802018-01-16 23:58:58 -0500742 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400743
Junxiao Shib55e5d32018-07-18 13:32:00 -0600744 face.receive(*makeInterest("/Hello/World/a")); // shouldn't match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400745 BOOST_CHECK_EQUAL(nInInterests, 0);
746
Junxiao Shib55e5d32018-07-18 13:32:00 -0600747 face.receive(*makeInterest("/Hello/World/a/b")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400748 BOOST_CHECK_EQUAL(nInInterests, 1);
749
Junxiao Shib55e5d32018-07-18 13:32:00 -0600750 face.receive(*makeInterest("/Hello/World/a/b/c")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400751 BOOST_CHECK_EQUAL(nInInterests, 2);
752
Junxiao Shib55e5d32018-07-18 13:32:00 -0600753 face.receive(*makeInterest("/Hello/World/a/b/d")); // should not match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400754 BOOST_CHECK_EQUAL(nInInterests, 2);
755}
756
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400757BOOST_AUTO_TEST_CASE(SetRegexFilterAndRegister)
758{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800759 size_t nInInterests = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800760 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
761 bind([&nInInterests] { ++nInInterests; }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400762
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800763 size_t nRegSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800764 face.registerPrefix("/Hello/World",
765 bind([&nRegSuccesses] { ++nRegSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000766 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400767
Davide Pesavento0f830802018-01-16 23:58:58 -0500768 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400769 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
770
Junxiao Shib55e5d32018-07-18 13:32:00 -0600771 face.receive(*makeInterest("/Hello/World/a")); // shouldn't match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400772 BOOST_CHECK_EQUAL(nInInterests, 0);
773
Junxiao Shib55e5d32018-07-18 13:32:00 -0600774 face.receive(*makeInterest("/Hello/World/a/b")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400775 BOOST_CHECK_EQUAL(nInInterests, 1);
776
Junxiao Shib55e5d32018-07-18 13:32:00 -0600777 face.receive(*makeInterest("/Hello/World/a/b/c")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400778 BOOST_CHECK_EQUAL(nInInterests, 2);
779
Junxiao Shib55e5d32018-07-18 13:32:00 -0600780 face.receive(*makeInterest("/Hello/World/a/b/d")); // should not match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400781 BOOST_CHECK_EQUAL(nInInterests, 2);
782}
783
Junxiao Shiec475a72019-01-13 21:53:55 +0000784BOOST_FIXTURE_TEST_CASE(SetInterestFilterNoReg, FaceFixture<NoPrefixRegReply>) // Bug 2318
Junxiao Shia1ea5062014-12-27 22:33:39 -0700785{
786 // This behavior is specific to DummyClientFace.
787 // Regular Face won't accept incoming packets until something is sent.
788
789 int hit = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800790 face.setInterestFilter(Name("/"), bind([&hit] { ++hit; }));
791 face.processEvents(time::milliseconds(-1));
Junxiao Shia1ea5062014-12-27 22:33:39 -0700792
Junxiao Shib55e5d32018-07-18 13:32:00 -0600793 face.receive(*makeInterest("/A"));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800794 face.processEvents(time::milliseconds(-1));
Junxiao Shia1ea5062014-12-27 22:33:39 -0700795
796 BOOST_CHECK_EQUAL(hit, 1);
797}
798
Junxiao Shi60aaef02019-01-14 04:59:30 +0000799BOOST_AUTO_TEST_CASE(SetInterestFilterHandle)
800{
801 int hit = 0;
802 auto hdl = face.setInterestFilter(Name("/"), bind([&hit] { ++hit; }));
803 face.processEvents(-1_ms);
804
805 face.receive(*makeInterest("/A"));
806 face.processEvents(-1_ms);
807 BOOST_CHECK_EQUAL(hit, 1);
808
809 hdl.cancel();
810 face.processEvents(-1_ms);
811
812 face.receive(*makeInterest("/B"));
813 face.processEvents(-1_ms);
814 BOOST_CHECK_EQUAL(hit, 1);
815}
816
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000817BOOST_AUTO_TEST_SUITE_END() // Producer
818
Junxiao Shiae0b4182016-08-08 22:53:17 +0000819BOOST_AUTO_TEST_SUITE(IoRoutines)
820
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500821BOOST_AUTO_TEST_CASE(ProcessEvents)
822{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800823 face.processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500824
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800825 size_t nRegSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800826 face.registerPrefix("/Hello/World",
827 bind([&nRegSuccesses] { ++nRegSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000828 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500829
830 // io_service::poll() without reset
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800831 face.getIoService().poll();
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500832 BOOST_CHECK_EQUAL(nRegSuccesses, 0);
833
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800834 face.processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500835 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
836}
837
Junxiao Shiae0b4182016-08-08 22:53:17 +0000838BOOST_AUTO_TEST_CASE(DestroyWithoutProcessEvents) // Bug 3248
839{
840 auto face2 = make_unique<Face>(io);
841 face2.reset();
842
843 io.poll(); // should not crash
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100844
845 // avoid "test case [...] did not check any assertions" message from Boost.Test
846 BOOST_CHECK(true);
Junxiao Shiae0b4182016-08-08 22:53:17 +0000847}
848
849BOOST_AUTO_TEST_SUITE_END() // IoRoutines
850
851BOOST_AUTO_TEST_SUITE(Transport)
852
853using ndn::Transport;
854
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700855struct PibDirWithDefaultTpm
856{
857 const std::string PATH = "build/keys-with-default-tpm";
858};
859
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800860BOOST_FIXTURE_TEST_CASE(FaceTransport, IdentityManagementTimeFixture)
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800861{
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800862 BOOST_CHECK(Face().getTransport() != nullptr);
863
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800864 BOOST_CHECK(Face(shared_ptr<Transport>()).getTransport() != nullptr);
865 BOOST_CHECK(Face(shared_ptr<Transport>(), io).getTransport() != nullptr);
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800866 BOOST_CHECK(Face(shared_ptr<Transport>(), io, m_keyChain).getTransport() != nullptr);
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800867
868 auto transport = make_shared<TcpTransport>("localhost", "6363"); // no real io operations will be scheduled
869 BOOST_CHECK(Face(transport).getTransport() == transport);
870 BOOST_CHECK(Face(transport, io).getTransport() == transport);
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800871 BOOST_CHECK(Face(transport, io, m_keyChain).getTransport() == transport);
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800872}
873
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700874class WithEnv : private IdentityManagementTimeFixture
875{
876public:
877 WithEnv()
878 {
879 if (getenv("NDN_CLIENT_TRANSPORT") != nullptr) {
880 m_oldTransport = getenv("NDN_CLIENT_TRANSPORT");
881 unsetenv("NDN_CLIENT_TRANSPORT");
882 }
883 }
884
885 void
886 configure(const std::string& faceUri)
887 {
888 setenv("NDN_CLIENT_TRANSPORT", faceUri.c_str(), true);
889 }
890
891 ~WithEnv()
892 {
893 if (!m_oldTransport.empty()) {
894 setenv("NDN_CLIENT_TRANSPORT", m_oldTransport.c_str(), true);
895 }
896 else {
897 unsetenv("NDN_CLIENT_TRANSPORT");
898 }
899 }
900
901private:
902 std::string m_oldTransport;
903};
904
905class WithConfig : private TestHomeFixture<DefaultPibDir>
906{
907public:
908 void
909 configure(const std::string& faceUri)
910 {
911 createClientConf({"transport=" + faceUri});
912 }
913};
914
915class WithEnvAndConfig : public WithEnv, public WithConfig
916{
917};
918
919typedef boost::mpl::vector<WithEnv, WithConfig> ConfigOptions;
920
921BOOST_FIXTURE_TEST_CASE(NoConfig, WithEnvAndConfig) // fixture configures test HOME and PIB/TPM path
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700922{
923 shared_ptr<Face> face;
924 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
925 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700926}
927
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700928BOOST_FIXTURE_TEST_CASE_TEMPLATE(Unix, T, ConfigOptions, T)
929{
930 this->configure("unix://some/path");
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700931
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700932 shared_ptr<Face> face;
933 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
934 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
935}
936
937BOOST_FIXTURE_TEST_CASE_TEMPLATE(Tcp, T, ConfigOptions, T)
938{
939 this->configure("tcp://127.0.0.1:6000");
940
941 shared_ptr<Face> face;
942 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
943 BOOST_CHECK(dynamic_pointer_cast<TcpTransport>(face->getTransport()) != nullptr);
944}
945
946BOOST_FIXTURE_TEST_CASE_TEMPLATE(WrongTransport, T, ConfigOptions, T)
947{
948 this->configure("wrong-transport:");
949
950 BOOST_CHECK_THROW(make_shared<Face>(), ConfigFile::Error);
951}
952
953BOOST_FIXTURE_TEST_CASE_TEMPLATE(WrongUri, T, ConfigOptions, T)
954{
955 this->configure("wrong-uri");
956
957 BOOST_CHECK_THROW(make_shared<Face>(), ConfigFile::Error);
958}
959
960BOOST_FIXTURE_TEST_CASE(EnvOverride, WithEnvAndConfig)
961{
962 this->WithEnv::configure("tcp://127.0.0.1:6000");
963 this->WithConfig::configure("unix://some/path");
964
965 shared_ptr<Face> face;
966 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
967 BOOST_CHECK(dynamic_pointer_cast<TcpTransport>(face->getTransport()) != nullptr);
968}
969
970BOOST_FIXTURE_TEST_CASE(ExplicitTransport, WithEnvAndConfig)
971{
972 this->WithEnv::configure("wrong-uri");
973 this->WithConfig::configure("wrong-transport:");
974
975 auto transport = make_shared<UnixTransport>("unix://some/path");
976 shared_ptr<Face> face;
977 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>(transport));
978 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
979}
980
Junxiao Shiae0b4182016-08-08 22:53:17 +0000981BOOST_AUTO_TEST_SUITE_END() // Transport
982
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700983BOOST_AUTO_TEST_SUITE_END() // TestFace
984
985} // namespace tests
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400986} // namespace ndn