blob: c9b820534811624b291d69394bb076967d440c65 [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
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800270BOOST_AUTO_TEST_CASE(RemovePendingInterest)
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 Shi103d8ed2016-08-07 20:34:10 +0000289BOOST_AUTO_TEST_CASE(RemoveAllPendingInterests)
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500290{
Junxiao Shib55e5d32018-07-18 13:32:00 -0600291 face.expressInterest(*makeInterest("/Hello/World/0", false, 50_ms),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800292 bind([] { BOOST_FAIL("Unexpected data"); }),
293 bind([] { BOOST_FAIL("Unexpected nack"); }),
294 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500295
Junxiao Shib55e5d32018-07-18 13:32:00 -0600296 face.expressInterest(*makeInterest("/Hello/World/1", false, 50_ms),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800297 bind([] { BOOST_FAIL("Unexpected data"); }),
298 bind([] { BOOST_FAIL("Unexpected nack"); }),
299 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500300
Davide Pesavento0f830802018-01-16 23:58:58 -0500301 advanceClocks(10_ms);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500302
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800303 face.removeAllPendingInterests();
Davide Pesavento0f830802018-01-16 23:58:58 -0500304 advanceClocks(10_ms);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500305
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800306 BOOST_CHECK_EQUAL(face.getNPendingInterests(), 0);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500307
Junxiao Shi85d90832016-08-04 03:19:46 +0000308 face.receive(*makeData("/Hello/World/0"));
309 face.receive(*makeData("/Hello/World/1"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500310 advanceClocks(200_ms, 5);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500311}
312
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000313BOOST_AUTO_TEST_CASE(DestructionWithoutCancellingPendingInterests) // Bug #2518
314{
315 {
316 DummyClientFace face2(io, m_keyChain);
Junxiao Shib55e5d32018-07-18 13:32:00 -0600317 face2.expressInterest(*makeInterest("/Hello/World", false, 50_ms),
Alexander Afanasyeve6835fe2017-01-19 20:05:01 -0800318 nullptr, nullptr, nullptr);
Davide Pesavento0f830802018-01-16 23:58:58 -0500319 advanceClocks(50_ms, 2);
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000320 }
321
Davide Pesavento0f830802018-01-16 23:58:58 -0500322 advanceClocks(50_ms, 2); // should not crash
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100323
324 // avoid "test case [...] did not check any assertions" message from Boost.Test
325 BOOST_CHECK(true);
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000326}
327
Ashlesh Gawandee2404222018-05-21 18:30:24 -0500328BOOST_AUTO_TEST_CASE(DataCallbackPutData) // Bug 4596
329{
Junxiao Shib55e5d32018-07-18 13:32:00 -0600330 face.expressInterest(*makeInterest("/localhost/notification/1"),
Ashlesh Gawandee2404222018-05-21 18:30:24 -0500331 [&] (const Interest& i, const Data& d) {
332 face.put(*makeData("/chronosync/sampleDigest/1"));
333 }, nullptr, nullptr);
334 advanceClocks(10_ms);
335 BOOST_CHECK_EQUAL(face.sentInterests.back().getName(), "/localhost/notification/1");
336
Junxiao Shib55e5d32018-07-18 13:32:00 -0600337 face.receive(*makeInterest("/chronosync/sampleDigest", true));
Ashlesh Gawandee2404222018-05-21 18:30:24 -0500338 advanceClocks(10_ms);
339
340 face.put(*makeData("/localhost/notification/1"));
341 advanceClocks(10_ms);
342 BOOST_CHECK_EQUAL(face.sentData.back().getName(), "/chronosync/sampleDigest/1");
343}
344
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000345BOOST_AUTO_TEST_SUITE_END() // Consumer
346
347BOOST_AUTO_TEST_SUITE(Producer)
348
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000349BOOST_AUTO_TEST_CASE(PutData)
350{
351 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
352
353 Data data("/4g7xxcuEow/KFvK5Kf2m");
354 signData(data);
355 face.put(data);
356
357 lp::CachePolicy cachePolicy;
358 cachePolicy.setPolicy(lp::CachePolicyType::NO_CACHE);
359 data.setTag(make_shared<lp::CachePolicyTag>(cachePolicy));
Eric Newberry4d261b62016-11-10 13:40:09 -0700360 data.setTag(make_shared<lp::CongestionMarkTag>(1));
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000361 face.put(data);
362
Davide Pesavento0f830802018-01-16 23:58:58 -0500363 advanceClocks(10_ms);
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000364 BOOST_REQUIRE_EQUAL(face.sentData.size(), 2);
365 BOOST_CHECK(face.sentData[0].getTag<lp::CachePolicyTag>() == nullptr);
Eric Newberry4d261b62016-11-10 13:40:09 -0700366 BOOST_CHECK(face.sentData[0].getTag<lp::CongestionMarkTag>() == nullptr);
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000367 BOOST_CHECK(face.sentData[1].getTag<lp::CachePolicyTag>() != nullptr);
Eric Newberry4d261b62016-11-10 13:40:09 -0700368 BOOST_CHECK(face.sentData[1].getTag<lp::CongestionMarkTag>() != nullptr);
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000369}
370
Junxiao Shib6828912017-11-20 14:06:32 +0000371BOOST_AUTO_TEST_CASE(PutDataLoopback)
372{
373 bool hasInterest1 = false, hasData = false;
374
375 // first InterestFilter allows loopback and should receive Interest
376 face.setInterestFilter("/", [&] (const InterestFilter&, const Interest& interest) {
377 hasInterest1 = true;
378 // do not respond with Data right away, so Face must send Interest to forwarder
379 });
380 // second InterestFilter disallows loopback and should not receive Interest
381 face.setInterestFilter(InterestFilter("/").allowLoopback(false),
382 bind([] { BOOST_ERROR("Unexpected Interest on second InterestFilter"); }));
383
Junxiao Shib55e5d32018-07-18 13:32:00 -0600384 face.expressInterest(*makeInterest("/A", true),
Junxiao Shib6828912017-11-20 14:06:32 +0000385 bind([&] { hasData = true; }),
386 bind([] { BOOST_FAIL("Unexpected nack"); }),
387 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500388 advanceClocks(1_ms);
Junxiao Shib6828912017-11-20 14:06:32 +0000389 BOOST_CHECK_EQUAL(hasInterest1, true); // Interest looped back
390 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1); // Interest sent to forwarder
391 BOOST_CHECK_EQUAL(hasData, false); // waiting for Data
392
393 face.put(*makeData("/A/B")); // first InterestFilter responds with Data
Davide Pesavento0f830802018-01-16 23:58:58 -0500394 advanceClocks(1_ms);
Junxiao Shib6828912017-11-20 14:06:32 +0000395 BOOST_CHECK_EQUAL(hasData, true);
396 BOOST_CHECK_EQUAL(face.sentData.size(), 0); // do not spill Data to forwarder
397}
398
Junxiao Shi859888f2017-09-12 14:29:16 +0000399BOOST_AUTO_TEST_CASE(PutMultipleData)
400{
401 bool hasInterest1 = false;
402 // register two Interest destinations
403 face.setInterestFilter("/", bind([&] {
404 hasInterest1 = true;
405 // sending Data right away from the first destination, don't care whether Interest goes to second destination
406 face.put(*makeData("/A/B"));
407 }));
408 face.setInterestFilter("/", bind([]{}));
Davide Pesavento0f830802018-01-16 23:58:58 -0500409 advanceClocks(10_ms);
Junxiao Shi859888f2017-09-12 14:29:16 +0000410
Junxiao Shib55e5d32018-07-18 13:32:00 -0600411 face.receive(*makeInterest("/A", true));
Davide Pesavento0f830802018-01-16 23:58:58 -0500412 advanceClocks(10_ms);
Junxiao Shi859888f2017-09-12 14:29:16 +0000413 BOOST_CHECK(hasInterest1);
414 BOOST_CHECK_EQUAL(face.sentData.size(), 1);
415 BOOST_CHECK_EQUAL(face.sentData.at(0).getName(), "/A/B");
416
417 face.put(*makeData("/A/C"));
418 BOOST_CHECK_EQUAL(face.sentData.size(), 1); // additional Data are ignored
419}
420
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000421BOOST_AUTO_TEST_CASE(PutNack)
422{
Junxiao Shi79a7a162017-09-09 08:33:57 +0000423 face.setInterestFilter("/", bind([]{})); // register one Interest destination so that face can accept Nacks
Davide Pesavento0f830802018-01-16 23:58:58 -0500424 advanceClocks(10_ms);
Junxiao Shi79a7a162017-09-09 08:33:57 +0000425
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000426 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0);
427
Junxiao Shib55e5d32018-07-18 13:32:00 -0600428 face.put(makeNack(*makeInterest("/unsolicited", false, DEFAULT_INTEREST_LIFETIME, 18645250),
429 lp::NackReason::NO_ROUTE));
Davide Pesavento0f830802018-01-16 23:58:58 -0500430 advanceClocks(10_ms);
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000431 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0); // unsolicited Nack would not be sent
Eric Newberry4d261b62016-11-10 13:40:09 -0700432
Junxiao Shib55e5d32018-07-18 13:32:00 -0600433 auto interest1 = makeInterest("/Hello/World", false, DEFAULT_INTEREST_LIFETIME, 14247162);
434 face.receive(*interest1);
435 auto interest2 = makeInterest("/another/prefix", false, DEFAULT_INTEREST_LIFETIME, 92203002);
436 face.receive(*interest2);
Davide Pesavento0f830802018-01-16 23:58:58 -0500437 advanceClocks(10_ms);
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000438
Junxiao Shib55e5d32018-07-18 13:32:00 -0600439 face.put(makeNack(*interest1, lp::NackReason::DUPLICATE));
Davide Pesavento0f830802018-01-16 23:58:58 -0500440 advanceClocks(10_ms);
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000441 BOOST_REQUIRE_EQUAL(face.sentNacks.size(), 1);
442 BOOST_CHECK_EQUAL(face.sentNacks[0].getReason(), lp::NackReason::DUPLICATE);
443 BOOST_CHECK(face.sentNacks[0].getTag<lp::CongestionMarkTag>() == nullptr);
444
Junxiao Shib55e5d32018-07-18 13:32:00 -0600445 auto nack = makeNack(*interest2, lp::NackReason::NO_ROUTE);
Eric Newberry4d261b62016-11-10 13:40:09 -0700446 nack.setTag(make_shared<lp::CongestionMarkTag>(1));
447 face.put(nack);
Davide Pesavento0f830802018-01-16 23:58:58 -0500448 advanceClocks(10_ms);
Eric Newberry4d261b62016-11-10 13:40:09 -0700449 BOOST_REQUIRE_EQUAL(face.sentNacks.size(), 2);
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000450 BOOST_CHECK_EQUAL(face.sentNacks[1].getReason(), lp::NackReason::NO_ROUTE);
Eric Newberry4d261b62016-11-10 13:40:09 -0700451 BOOST_CHECK(face.sentNacks[1].getTag<lp::CongestionMarkTag>() != nullptr);
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000452}
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500453
Junxiao Shi79a7a162017-09-09 08:33:57 +0000454BOOST_AUTO_TEST_CASE(PutMultipleNack)
455{
Junxiao Shi859888f2017-09-12 14:29:16 +0000456 bool hasInterest1 = false, hasInterest2 = false;
457 // register two Interest destinations
458 face.setInterestFilter("/", [&] (const InterestFilter&, const Interest& interest) {
459 hasInterest1 = true;
460 // sending Nack right away from the first destination, Interest should still go to second destination
461 face.put(makeNack(interest, lp::NackReason::CONGESTION));
462 });
463 face.setInterestFilter("/", bind([&] { hasInterest2 = true; }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500464 advanceClocks(10_ms);
Junxiao Shi79a7a162017-09-09 08:33:57 +0000465
Junxiao Shib55e5d32018-07-18 13:32:00 -0600466 auto interest = makeInterest("/A", false, DEFAULT_INTEREST_LIFETIME, 14333271);
467 face.receive(*interest);
Davide Pesavento0f830802018-01-16 23:58:58 -0500468 advanceClocks(10_ms);
Junxiao Shi859888f2017-09-12 14:29:16 +0000469 BOOST_CHECK(hasInterest1);
470 BOOST_CHECK(hasInterest2);
Junxiao Shi79a7a162017-09-09 08:33:57 +0000471
Junxiao Shi859888f2017-09-12 14:29:16 +0000472 // Nack from first destination is received, should wait for a response from the other destination
473 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0);
Junxiao Shi79a7a162017-09-09 08:33:57 +0000474
Junxiao Shib55e5d32018-07-18 13:32:00 -0600475 face.put(makeNack(*interest, lp::NackReason::NO_ROUTE)); // Nack from second destination
Davide Pesavento0f830802018-01-16 23:58:58 -0500476 advanceClocks(10_ms);
Junxiao Shi859888f2017-09-12 14:29:16 +0000477 BOOST_CHECK_EQUAL(face.sentNacks.size(), 1); // sending Nack after both destinations Nacked
Junxiao Shi79a7a162017-09-09 08:33:57 +0000478 BOOST_CHECK_EQUAL(face.sentNacks.at(0).getReason(), lp::NackReason::CONGESTION); // least severe reason
479
Junxiao Shib55e5d32018-07-18 13:32:00 -0600480 face.put(makeNack(*interest, lp::NackReason::DUPLICATE));
Junxiao Shi79a7a162017-09-09 08:33:57 +0000481 BOOST_CHECK_EQUAL(face.sentNacks.size(), 1); // additional Nacks are ignored
482}
483
Junxiao Shib6828912017-11-20 14:06:32 +0000484BOOST_AUTO_TEST_CASE(PutMultipleNackLoopback)
485{
486 bool hasInterest1 = false, hasNack = false;
487
488 // first InterestFilter allows loopback and should receive Interest
489 face.setInterestFilter("/", [&] (const InterestFilter&, const Interest& interest) {
490 hasInterest1 = true;
491 face.put(makeNack(interest, lp::NackReason::CONGESTION));
492 });
493 // second InterestFilter disallows loopback and should not receive Interest
494 face.setInterestFilter(InterestFilter("/").allowLoopback(false),
495 bind([] { BOOST_ERROR("Unexpected Interest on second InterestFilter"); }));
496
Junxiao Shib55e5d32018-07-18 13:32:00 -0600497 auto interest = makeInterest("/A", false, DEFAULT_INTEREST_LIFETIME, 28395852);
498 face.expressInterest(*interest,
Junxiao Shib6828912017-11-20 14:06:32 +0000499 bind([] { BOOST_FAIL("Unexpected data"); }),
500 [&] (const Interest&, const lp::Nack& nack) {
501 hasNack = true;
502 BOOST_CHECK_EQUAL(nack.getReason(), lp::NackReason::CONGESTION);
503 },
504 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500505 advanceClocks(1_ms);
Junxiao Shib6828912017-11-20 14:06:32 +0000506 BOOST_CHECK_EQUAL(hasInterest1, true); // Interest looped back
507 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1); // Interest sent to forwarder
508 BOOST_CHECK_EQUAL(hasNack, false); // waiting for Nack from forwarder
509
Junxiao Shib55e5d32018-07-18 13:32:00 -0600510 face.receive(makeNack(*interest, lp::NackReason::NO_ROUTE));
Davide Pesavento0f830802018-01-16 23:58:58 -0500511 advanceClocks(1_ms);
Junxiao Shib6828912017-11-20 14:06:32 +0000512 BOOST_CHECK_EQUAL(hasNack, true);
513}
514
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400515BOOST_AUTO_TEST_CASE(SetUnsetInterestFilter)
516{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800517 size_t nInterests = 0;
518 size_t nRegs = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400519 const RegisteredPrefixId* regPrefixId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800520 face.setInterestFilter("/Hello/World",
521 bind([&nInterests] { ++nInterests; }),
522 bind([&nRegs] { ++nRegs; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000523 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500524 advanceClocks(25_ms, 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800525 BOOST_CHECK_EQUAL(nRegs, 1);
526 BOOST_CHECK_EQUAL(nInterests, 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400527
Junxiao Shib55e5d32018-07-18 13:32:00 -0600528 face.receive(*makeInterest("/Hello/World/%21"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500529 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400530
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800531 BOOST_CHECK_EQUAL(nRegs, 1);
532 BOOST_CHECK_EQUAL(nInterests, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400533
Junxiao Shib55e5d32018-07-18 13:32:00 -0600534 face.receive(*makeInterest("/Bye/World/%21"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500535 advanceClocks(10000_ms, 10);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800536 BOOST_CHECK_EQUAL(nInterests, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400537
Junxiao Shib55e5d32018-07-18 13:32:00 -0600538 face.receive(*makeInterest("/Hello/World/%21/2"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500539 advanceClocks(25_ms, 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800540 BOOST_CHECK_EQUAL(nInterests, 2);
541
542 // removing filter
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800543 face.unsetInterestFilter(regPrefixId);
Davide Pesavento0f830802018-01-16 23:58:58 -0500544 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400545
Junxiao Shib55e5d32018-07-18 13:32:00 -0600546 face.receive(*makeInterest("/Hello/World/%21/3"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800547 BOOST_CHECK_EQUAL(nInterests, 2);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400548
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000549 face.unsetInterestFilter(static_cast<const RegisteredPrefixId*>(nullptr));
Davide Pesavento0f830802018-01-16 23:58:58 -0500550 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400551
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000552 face.unsetInterestFilter(static_cast<const InterestFilterId*>(nullptr));
Davide Pesavento0f830802018-01-16 23:58:58 -0500553 advanceClocks(25_ms, 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800554}
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400555
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000556BOOST_AUTO_TEST_CASE(SetInterestFilterEmptyInterestCallback)
557{
558 face.setInterestFilter("/A", nullptr);
Davide Pesavento0f830802018-01-16 23:58:58 -0500559 advanceClocks(1_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000560
561 BOOST_CHECK_NO_THROW(do {
562 face.receive(*makeInterest("/A/1"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500563 advanceClocks(1_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000564 } while (false));
565}
566
Joao Pereira0b3cac52015-07-02 14:49:49 -0400567BOOST_AUTO_TEST_CASE(SetUnsetInterestFilterWithoutSucessCallback)
568{
569 size_t nInterests = 0;
570 const RegisteredPrefixId* regPrefixId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800571 face.setInterestFilter("/Hello/World",
572 bind([&nInterests] { ++nInterests; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000573 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500574 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400575 BOOST_CHECK_EQUAL(nInterests, 0);
576
Junxiao Shib55e5d32018-07-18 13:32:00 -0600577 face.receive(*makeInterest("/Hello/World/%21"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500578 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400579
580 BOOST_CHECK_EQUAL(nInterests, 1);
581
Junxiao Shib55e5d32018-07-18 13:32:00 -0600582 face.receive(*makeInterest("/Bye/World/%21"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500583 advanceClocks(10000_ms, 10);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400584 BOOST_CHECK_EQUAL(nInterests, 1);
585
Junxiao Shib55e5d32018-07-18 13:32:00 -0600586 face.receive(*makeInterest("/Hello/World/%21/2"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500587 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400588 BOOST_CHECK_EQUAL(nInterests, 2);
589
590 // removing filter
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800591 face.unsetInterestFilter(regPrefixId);
Davide Pesavento0f830802018-01-16 23:58:58 -0500592 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400593
Junxiao Shib55e5d32018-07-18 13:32:00 -0600594 face.receive(*makeInterest("/Hello/World/%21/3"));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400595 BOOST_CHECK_EQUAL(nInterests, 2);
596
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000597 face.unsetInterestFilter(static_cast<const RegisteredPrefixId*>(nullptr));
Davide Pesavento0f830802018-01-16 23:58:58 -0500598 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400599
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000600 face.unsetInterestFilter(static_cast<const InterestFilterId*>(nullptr));
Davide Pesavento0f830802018-01-16 23:58:58 -0500601 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400602}
603
Junxiao Shiec475a72019-01-13 21:53:55 +0000604BOOST_FIXTURE_TEST_CASE(SetInterestFilterFail, FaceFixture<NoPrefixRegReply>)
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800605{
606 // don't enable registration reply
607 size_t nRegFailed = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800608 face.setInterestFilter("/Hello/World",
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000609 bind([] { BOOST_FAIL("Unexpected Interest"); }),
610 bind([] { BOOST_FAIL("Unexpected success of setInterestFilter"); }),
611 bind([&nRegFailed] { ++nRegFailed; }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800612
Davide Pesavento0f830802018-01-16 23:58:58 -0500613 advanceClocks(25_ms, 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800614 BOOST_CHECK_EQUAL(nRegFailed, 0);
615
Davide Pesavento0f830802018-01-16 23:58:58 -0500616 advanceClocks(2000_ms, 5);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800617 BOOST_CHECK_EQUAL(nRegFailed, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400618}
619
Junxiao Shiec475a72019-01-13 21:53:55 +0000620BOOST_FIXTURE_TEST_CASE(SetInterestFilterFailWithoutSuccessCallback, FaceFixture<NoPrefixRegReply>)
Joao Pereira0b3cac52015-07-02 14:49:49 -0400621{
622 // don't enable registration reply
623 size_t nRegFailed = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800624 face.setInterestFilter("/Hello/World",
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000625 bind([] { BOOST_FAIL("Unexpected Interest"); }),
626 bind([&nRegFailed] { ++nRegFailed; }));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400627
Davide Pesavento0f830802018-01-16 23:58:58 -0500628 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400629 BOOST_CHECK_EQUAL(nRegFailed, 0);
630
Davide Pesavento0f830802018-01-16 23:58:58 -0500631 advanceClocks(2000_ms, 5);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400632 BOOST_CHECK_EQUAL(nRegFailed, 1);
633}
634
Junxiao Shiec475a72019-01-13 21:53:55 +0000635BOOST_AUTO_TEST_CASE(RegisterUnregisterPrefixFunc)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400636{
Junxiao Shiec475a72019-01-13 21:53:55 +0000637 const RegisteredPrefixId* regPrefixId = nullptr;
638 BOOST_CHECK(runPrefixReg([&] (const auto& success, const auto& failure) {
639 regPrefixId = face.registerPrefix("/Hello/World", success, failure);
640 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400641
Junxiao Shiec475a72019-01-13 21:53:55 +0000642 BOOST_CHECK(runPrefixUnreg([&] (const auto& success, const auto& failure) {
643 face.unregisterPrefix(regPrefixId, success, failure);
644 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400645}
646
Junxiao Shiec475a72019-01-13 21:53:55 +0000647BOOST_FIXTURE_TEST_CASE(RegisterUnregisterPrefixFail, FaceFixture<NoPrefixRegReply>)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400648{
Junxiao Shiec475a72019-01-13 21:53:55 +0000649 BOOST_CHECK(!runPrefixReg([&] (const auto& success, const auto& failure) {
650 face.registerPrefix("/Hello/World", success, failure);
651 this->advanceClocks(5_s, 20); // wait for command timeout
652 }));
653}
654BOOST_AUTO_TEST_CASE(RegisterUnregisterPrefixHandle)
655{
656 RegisteredPrefixHandle hdl;
657 BOOST_CHECK(!runPrefixUnreg([&] (const auto& success, const auto& failure) {
658 // despite the "undefined behavior" warning, we try not to crash, but no API guarantee for this
659 hdl.unregister(success, failure);
660 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400661
Junxiao Shiec475a72019-01-13 21:53:55 +0000662 BOOST_CHECK(runPrefixReg([&] (const auto& success, const auto& failure) {
663 hdl = face.registerPrefix("/Hello/World", success, failure);
664 }));
665
666 BOOST_CHECK(runPrefixUnreg([&] (const auto& success, const auto& failure) {
667 hdl.unregister(success, failure);
668 }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800669}
670
671BOOST_AUTO_TEST_CASE(SimilarFilters)
672{
673 size_t nInInterests1 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800674 face.setInterestFilter("/Hello/World",
675 bind([&nInInterests1] { ++nInInterests1; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000676 nullptr,
677 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800678
679 size_t nInInterests2 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800680 face.setInterestFilter("/Hello",
681 bind([&nInInterests2] { ++nInInterests2; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000682 nullptr,
683 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400684
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800685 size_t nInInterests3 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800686 face.setInterestFilter("/Los/Angeles/Lakers",
687 bind([&nInInterests3] { ++nInInterests3; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000688 nullptr,
689 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400690
Davide Pesavento0f830802018-01-16 23:58:58 -0500691 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400692
Junxiao Shib55e5d32018-07-18 13:32:00 -0600693 face.receive(*makeInterest("/Hello/World/%21"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500694 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400695
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800696 BOOST_CHECK_EQUAL(nInInterests1, 1);
697 BOOST_CHECK_EQUAL(nInInterests2, 1);
698 BOOST_CHECK_EQUAL(nInInterests3, 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400699}
700
701BOOST_AUTO_TEST_CASE(SetRegexFilterError)
702{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800703 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
704 [] (const Name&, const Interest&) {
705 BOOST_FAIL("InterestFilter::Error should have been triggered");
706 },
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 BOOST_REQUIRE_THROW(face.receive(*makeInterest("/Hello/World/XXX/b/c")), InterestFilter::Error);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400713}
714
715BOOST_AUTO_TEST_CASE(SetRegexFilter)
716{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800717 size_t nInInterests = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800718 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
719 bind([&nInInterests] { ++nInInterests; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000720 nullptr,
721 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400722
Davide Pesavento0f830802018-01-16 23:58:58 -0500723 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400724
Junxiao Shib55e5d32018-07-18 13:32:00 -0600725 face.receive(*makeInterest("/Hello/World/a")); // shouldn't match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400726 BOOST_CHECK_EQUAL(nInInterests, 0);
727
Junxiao Shib55e5d32018-07-18 13:32:00 -0600728 face.receive(*makeInterest("/Hello/World/a/b")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400729 BOOST_CHECK_EQUAL(nInInterests, 1);
730
Junxiao Shib55e5d32018-07-18 13:32:00 -0600731 face.receive(*makeInterest("/Hello/World/a/b/c")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400732 BOOST_CHECK_EQUAL(nInInterests, 2);
733
Junxiao Shib55e5d32018-07-18 13:32:00 -0600734 face.receive(*makeInterest("/Hello/World/a/b/d")); // should not match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400735 BOOST_CHECK_EQUAL(nInInterests, 2);
736}
737
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400738BOOST_AUTO_TEST_CASE(SetRegexFilterAndRegister)
739{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800740 size_t nInInterests = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800741 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
742 bind([&nInInterests] { ++nInInterests; }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400743
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800744 size_t nRegSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800745 face.registerPrefix("/Hello/World",
746 bind([&nRegSuccesses] { ++nRegSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000747 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400748
Davide Pesavento0f830802018-01-16 23:58:58 -0500749 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400750 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
751
Junxiao Shib55e5d32018-07-18 13:32:00 -0600752 face.receive(*makeInterest("/Hello/World/a")); // shouldn't match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400753 BOOST_CHECK_EQUAL(nInInterests, 0);
754
Junxiao Shib55e5d32018-07-18 13:32:00 -0600755 face.receive(*makeInterest("/Hello/World/a/b")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400756 BOOST_CHECK_EQUAL(nInInterests, 1);
757
Junxiao Shib55e5d32018-07-18 13:32:00 -0600758 face.receive(*makeInterest("/Hello/World/a/b/c")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400759 BOOST_CHECK_EQUAL(nInInterests, 2);
760
Junxiao Shib55e5d32018-07-18 13:32:00 -0600761 face.receive(*makeInterest("/Hello/World/a/b/d")); // should not match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400762 BOOST_CHECK_EQUAL(nInInterests, 2);
763}
764
Junxiao Shiec475a72019-01-13 21:53:55 +0000765BOOST_FIXTURE_TEST_CASE(SetInterestFilterNoReg, FaceFixture<NoPrefixRegReply>) // Bug 2318
Junxiao Shia1ea5062014-12-27 22:33:39 -0700766{
767 // This behavior is specific to DummyClientFace.
768 // Regular Face won't accept incoming packets until something is sent.
769
770 int hit = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800771 face.setInterestFilter(Name("/"), bind([&hit] { ++hit; }));
772 face.processEvents(time::milliseconds(-1));
Junxiao Shia1ea5062014-12-27 22:33:39 -0700773
Junxiao Shib55e5d32018-07-18 13:32:00 -0600774 face.receive(*makeInterest("/A"));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800775 face.processEvents(time::milliseconds(-1));
Junxiao Shia1ea5062014-12-27 22:33:39 -0700776
777 BOOST_CHECK_EQUAL(hit, 1);
778}
779
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000780BOOST_AUTO_TEST_SUITE_END() // Producer
781
Junxiao Shiae0b4182016-08-08 22:53:17 +0000782BOOST_AUTO_TEST_SUITE(IoRoutines)
783
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500784BOOST_AUTO_TEST_CASE(ProcessEvents)
785{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800786 face.processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500787
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800788 size_t nRegSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800789 face.registerPrefix("/Hello/World",
790 bind([&nRegSuccesses] { ++nRegSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000791 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500792
793 // io_service::poll() without reset
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800794 face.getIoService().poll();
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500795 BOOST_CHECK_EQUAL(nRegSuccesses, 0);
796
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800797 face.processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500798 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
799}
800
Junxiao Shiae0b4182016-08-08 22:53:17 +0000801BOOST_AUTO_TEST_CASE(DestroyWithoutProcessEvents) // Bug 3248
802{
803 auto face2 = make_unique<Face>(io);
804 face2.reset();
805
806 io.poll(); // should not crash
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100807
808 // avoid "test case [...] did not check any assertions" message from Boost.Test
809 BOOST_CHECK(true);
Junxiao Shiae0b4182016-08-08 22:53:17 +0000810}
811
812BOOST_AUTO_TEST_SUITE_END() // IoRoutines
813
814BOOST_AUTO_TEST_SUITE(Transport)
815
816using ndn::Transport;
817
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700818struct PibDirWithDefaultTpm
819{
820 const std::string PATH = "build/keys-with-default-tpm";
821};
822
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800823BOOST_FIXTURE_TEST_CASE(FaceTransport, IdentityManagementTimeFixture)
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800824{
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800825 BOOST_CHECK(Face().getTransport() != nullptr);
826
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800827 BOOST_CHECK(Face(shared_ptr<Transport>()).getTransport() != nullptr);
828 BOOST_CHECK(Face(shared_ptr<Transport>(), io).getTransport() != nullptr);
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800829 BOOST_CHECK(Face(shared_ptr<Transport>(), io, m_keyChain).getTransport() != nullptr);
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800830
831 auto transport = make_shared<TcpTransport>("localhost", "6363"); // no real io operations will be scheduled
832 BOOST_CHECK(Face(transport).getTransport() == transport);
833 BOOST_CHECK(Face(transport, io).getTransport() == transport);
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800834 BOOST_CHECK(Face(transport, io, m_keyChain).getTransport() == transport);
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800835}
836
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700837class WithEnv : private IdentityManagementTimeFixture
838{
839public:
840 WithEnv()
841 {
842 if (getenv("NDN_CLIENT_TRANSPORT") != nullptr) {
843 m_oldTransport = getenv("NDN_CLIENT_TRANSPORT");
844 unsetenv("NDN_CLIENT_TRANSPORT");
845 }
846 }
847
848 void
849 configure(const std::string& faceUri)
850 {
851 setenv("NDN_CLIENT_TRANSPORT", faceUri.c_str(), true);
852 }
853
854 ~WithEnv()
855 {
856 if (!m_oldTransport.empty()) {
857 setenv("NDN_CLIENT_TRANSPORT", m_oldTransport.c_str(), true);
858 }
859 else {
860 unsetenv("NDN_CLIENT_TRANSPORT");
861 }
862 }
863
864private:
865 std::string m_oldTransport;
866};
867
868class WithConfig : private TestHomeFixture<DefaultPibDir>
869{
870public:
871 void
872 configure(const std::string& faceUri)
873 {
874 createClientConf({"transport=" + faceUri});
875 }
876};
877
878class WithEnvAndConfig : public WithEnv, public WithConfig
879{
880};
881
882typedef boost::mpl::vector<WithEnv, WithConfig> ConfigOptions;
883
884BOOST_FIXTURE_TEST_CASE(NoConfig, WithEnvAndConfig) // fixture configures test HOME and PIB/TPM path
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700885{
886 shared_ptr<Face> face;
887 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
888 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700889}
890
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700891BOOST_FIXTURE_TEST_CASE_TEMPLATE(Unix, T, ConfigOptions, T)
892{
893 this->configure("unix://some/path");
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700894
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700895 shared_ptr<Face> face;
896 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
897 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
898}
899
900BOOST_FIXTURE_TEST_CASE_TEMPLATE(Tcp, T, ConfigOptions, T)
901{
902 this->configure("tcp://127.0.0.1:6000");
903
904 shared_ptr<Face> face;
905 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
906 BOOST_CHECK(dynamic_pointer_cast<TcpTransport>(face->getTransport()) != nullptr);
907}
908
909BOOST_FIXTURE_TEST_CASE_TEMPLATE(WrongTransport, T, ConfigOptions, T)
910{
911 this->configure("wrong-transport:");
912
913 BOOST_CHECK_THROW(make_shared<Face>(), ConfigFile::Error);
914}
915
916BOOST_FIXTURE_TEST_CASE_TEMPLATE(WrongUri, T, ConfigOptions, T)
917{
918 this->configure("wrong-uri");
919
920 BOOST_CHECK_THROW(make_shared<Face>(), ConfigFile::Error);
921}
922
923BOOST_FIXTURE_TEST_CASE(EnvOverride, WithEnvAndConfig)
924{
925 this->WithEnv::configure("tcp://127.0.0.1:6000");
926 this->WithConfig::configure("unix://some/path");
927
928 shared_ptr<Face> face;
929 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
930 BOOST_CHECK(dynamic_pointer_cast<TcpTransport>(face->getTransport()) != nullptr);
931}
932
933BOOST_FIXTURE_TEST_CASE(ExplicitTransport, WithEnvAndConfig)
934{
935 this->WithEnv::configure("wrong-uri");
936 this->WithConfig::configure("wrong-transport:");
937
938 auto transport = make_shared<UnixTransport>("unix://some/path");
939 shared_ptr<Face> face;
940 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>(transport));
941 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
942}
943
Junxiao Shiae0b4182016-08-08 22:53:17 +0000944BOOST_AUTO_TEST_SUITE_END() // Transport
945
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700946BOOST_AUTO_TEST_SUITE_END() // TestFace
947
948} // namespace tests
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400949} // namespace ndn