blob: eaab9b6de4897ea8f10272f9ab7f0b63b5d642ed [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/*
Davide Pesavento2e481fc2021-07-02 18:20:03 -04003 * Copyright (c) 2013-2021 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 Pesavento4c1ad4c2020-11-16 21:12:02 -050029#include "tests/test-common.hpp"
30#include "tests/unit/io-key-chain-fixture.hpp"
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040031
Junxiao Shiec475a72019-01-13 21:53:55 +000032#include <boost/logic/tribool.hpp>
33
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040034namespace ndn {
35namespace tests {
36
Junxiao Shia60d9362014-11-12 09:38:21 -070037using ndn::util::DummyClientFace;
Davide Pesavento2e481fc2021-07-02 18:20:03 -040038using std::bind;
Junxiao Shia60d9362014-11-12 09:38:21 -070039
Junxiao Shiec475a72019-01-13 21:53:55 +000040struct WantPrefixRegReply;
41struct NoPrefixRegReply;
42
43template<typename PrefixRegReply = WantPrefixRegReply>
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050044class FaceFixture : public IoKeyChainFixture
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040045{
Davide Pesaventod247d492020-01-28 21:30:20 -050046protected:
Junxiao Shiec475a72019-01-13 21:53:55 +000047 FaceFixture()
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050048 : face(m_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;
Davide Pesaventod247d492020-01-28 21:30:20 -050062 f([&] (auto) { result = true; },
63 [&] (auto, auto) { result = false; });
Junxiao Shiec475a72019-01-13 21:53:55 +000064
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;
Davide Pesaventod247d492020-01-28 21:30:20 -050078 f([&] { result = true; },
79 [&] (auto) { result = false; });
Junxiao Shiec475a72019-01-13 21:53:55 +000080
81 advanceClocks(1_ms);
82 BOOST_REQUIRE(!boost::logic::indeterminate(result));
83 return static_cast<bool>(result);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040084 }
85
Davide Pesaventod247d492020-01-28 21:30:20 -050086protected:
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080087 DummyClientFace face;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040088};
89
Junxiao Shiec475a72019-01-13 21:53:55 +000090BOOST_FIXTURE_TEST_SUITE(TestFace, FaceFixture<>)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040091
Davide Pesaventod247d492020-01-28 21:30:20 -050092BOOST_AUTO_TEST_SUITE(ExpressInterest)
Junxiao Shi103d8ed2016-08-07 20:34:10 +000093
Davide Pesaventod247d492020-01-28 21:30:20 -050094BOOST_AUTO_TEST_CASE(ReplyData)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040095{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080096 size_t nData = 0;
Junxiao Shib55e5d32018-07-18 13:32:00 -060097 face.expressInterest(*makeInterest("/Hello/World", true, 50_ms),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080098 [&] (const Interest& i, const Data& d) {
99 BOOST_CHECK(i.getName().isPrefixOf(d.getName()));
100 BOOST_CHECK_EQUAL(i.getName(), "/Hello/World");
101 ++nData;
102 },
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000103 bind([] { BOOST_FAIL("Unexpected Nack"); }),
104 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Eric Newberry83872fd2015-08-06 17:01:24 -0700105
Davide Pesavento0f830802018-01-16 23:58:58 -0500106 advanceClocks(40_ms);
Eric Newberry83872fd2015-08-06 17:01:24 -0700107
Junxiao Shi85d90832016-08-04 03:19:46 +0000108 face.receive(*makeData("/Bye/World/a"));
109 face.receive(*makeData("/Hello/World/a"));
Eric Newberry83872fd2015-08-06 17:01:24 -0700110
Davide Pesavento0f830802018-01-16 23:58:58 -0500111 advanceClocks(50_ms, 2);
Eric Newberry83872fd2015-08-06 17:01:24 -0700112
113 BOOST_CHECK_EQUAL(nData, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800114 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
115 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Eric Newberry83872fd2015-08-06 17:01:24 -0700116
117 size_t nTimeouts = 0;
Junxiao Shib55e5d32018-07-18 13:32:00 -0600118 face.expressInterest(*makeInterest("/Hello/World/a/2", false, 50_ms),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800119 bind([]{}),
120 bind([]{}),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000121 bind([&nTimeouts] { ++nTimeouts; }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500122 advanceClocks(200_ms, 5);
Eric Newberry83872fd2015-08-06 17:01:24 -0700123 BOOST_CHECK_EQUAL(nTimeouts, 1);
124}
125
Davide Pesaventod247d492020-01-28 21:30:20 -0500126BOOST_AUTO_TEST_CASE(MultipleData)
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800127{
128 size_t nData = 0;
129
Junxiao Shib55e5d32018-07-18 13:32:00 -0600130 face.expressInterest(*makeInterest("/Hello/World", true, 50_ms),
Davide Pesaventod247d492020-01-28 21:30:20 -0500131 [&] (const auto&, const auto&) { ++nData; },
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800132 bind([] { BOOST_FAIL("Unexpected Nack"); }),
133 bind([] { BOOST_FAIL("Unexpected timeout"); }));
134
Junxiao Shib55e5d32018-07-18 13:32:00 -0600135 face.expressInterest(*makeInterest("/Hello/World/a", true, 50_ms),
Davide Pesaventod247d492020-01-28 21:30:20 -0500136 [&] (const auto&, const auto&) { ++nData; },
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800137 bind([] { BOOST_FAIL("Unexpected Nack"); }),
138 bind([] { BOOST_FAIL("Unexpected timeout"); }));
139
Davide Pesavento0f830802018-01-16 23:58:58 -0500140 advanceClocks(40_ms);
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800141
142 face.receive(*makeData("/Hello/World/a/b"));
143
Davide Pesavento0f830802018-01-16 23:58:58 -0500144 advanceClocks(50_ms, 2);
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800145
146 BOOST_CHECK_EQUAL(nData, 2);
147 BOOST_CHECK_EQUAL(face.sentInterests.size(), 2);
148 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
149}
150
Davide Pesaventod247d492020-01-28 21:30:20 -0500151BOOST_AUTO_TEST_CASE(EmptyDataCallback)
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000152{
Junxiao Shib55e5d32018-07-18 13:32:00 -0600153 face.expressInterest(*makeInterest("/Hello/World", true),
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000154 nullptr,
155 bind([] { BOOST_FAIL("Unexpected Nack"); }),
156 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500157 advanceClocks(1_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000158
159 BOOST_CHECK_NO_THROW(do {
160 face.receive(*makeData("/Hello/World/a"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500161 advanceClocks(1_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000162 } while (false));
163}
164
Davide Pesaventod247d492020-01-28 21:30:20 -0500165BOOST_AUTO_TEST_CASE(Timeout)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400166{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800167 size_t nTimeouts = 0;
Junxiao Shib55e5d32018-07-18 13:32:00 -0600168 face.expressInterest(*makeInterest("/Hello/World", false, 50_ms),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000169 bind([] { BOOST_FAIL("Unexpected Data"); }),
170 bind([] { BOOST_FAIL("Unexpected Nack"); }),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800171 [&nTimeouts] (const Interest& i) {
172 BOOST_CHECK_EQUAL(i.getName(), "/Hello/World");
173 ++nTimeouts;
174 });
Eric Newberry83872fd2015-08-06 17:01:24 -0700175
Davide Pesavento0f830802018-01-16 23:58:58 -0500176 advanceClocks(200_ms, 5);
Eric Newberry83872fd2015-08-06 17:01:24 -0700177
178 BOOST_CHECK_EQUAL(nTimeouts, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800179 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
180 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
181 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0);
Eric Newberry83872fd2015-08-06 17:01:24 -0700182}
183
Davide Pesaventod247d492020-01-28 21:30:20 -0500184BOOST_AUTO_TEST_CASE(EmptyTimeoutCallback)
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000185{
Junxiao Shib55e5d32018-07-18 13:32:00 -0600186 face.expressInterest(*makeInterest("/Hello/World", false, 50_ms),
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000187 bind([] { BOOST_FAIL("Unexpected Data"); }),
188 bind([] { BOOST_FAIL("Unexpected Nack"); }),
189 nullptr);
Davide Pesavento0f830802018-01-16 23:58:58 -0500190 advanceClocks(40_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000191
192 BOOST_CHECK_NO_THROW(do {
Davide Pesavento0f830802018-01-16 23:58:58 -0500193 advanceClocks(6_ms, 2);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000194 } while (false));
195}
196
Davide Pesaventod247d492020-01-28 21:30:20 -0500197BOOST_AUTO_TEST_CASE(ReplyNack)
Eric Newberry83872fd2015-08-06 17:01:24 -0700198{
199 size_t nNacks = 0;
200
Junxiao Shib55e5d32018-07-18 13:32:00 -0600201 auto interest = makeInterest("/Hello/World", false, 50_ms);
Eric Newberry83872fd2015-08-06 17:01:24 -0700202
Junxiao Shib55e5d32018-07-18 13:32:00 -0600203 face.expressInterest(*interest,
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000204 bind([] { BOOST_FAIL("Unexpected Data"); }),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800205 [&] (const Interest& i, const lp::Nack& n) {
206 BOOST_CHECK(i.getName().isPrefixOf(n.getInterest().getName()));
207 BOOST_CHECK_EQUAL(i.getName(), "/Hello/World");
208 BOOST_CHECK_EQUAL(n.getReason(), lp::NackReason::DUPLICATE);
209 ++nNacks;
210 },
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000211 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Eric Newberry83872fd2015-08-06 17:01:24 -0700212
Davide Pesavento0f830802018-01-16 23:58:58 -0500213 advanceClocks(40_ms);
Eric Newberry83872fd2015-08-06 17:01:24 -0700214
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000215 face.receive(makeNack(face.sentInterests.at(0), lp::NackReason::DUPLICATE));
Eric Newberry83872fd2015-08-06 17:01:24 -0700216
Davide Pesavento0f830802018-01-16 23:58:58 -0500217 advanceClocks(50_ms, 2);
Eric Newberry83872fd2015-08-06 17:01:24 -0700218
219 BOOST_CHECK_EQUAL(nNacks, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800220 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
Eric Newberry83872fd2015-08-06 17:01:24 -0700221}
222
Davide Pesaventod247d492020-01-28 21:30:20 -0500223BOOST_AUTO_TEST_CASE(MultipleNacks)
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800224{
225 size_t nNacks = 0;
226
Junxiao Shib55e5d32018-07-18 13:32:00 -0600227 auto interest = makeInterest("/Hello/World", false, 50_ms, 1);
228 face.expressInterest(*interest,
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800229 bind([] { BOOST_FAIL("Unexpected Data"); }),
Davide Pesaventod247d492020-01-28 21:30:20 -0500230 [&] (const auto&, const auto&) { ++nNacks; },
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800231 bind([] { BOOST_FAIL("Unexpected timeout"); }));
232
Junxiao Shib55e5d32018-07-18 13:32:00 -0600233 interest->setNonce(2);
234 face.expressInterest(*interest,
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800235 bind([] { BOOST_FAIL("Unexpected Data"); }),
Davide Pesaventod247d492020-01-28 21:30:20 -0500236 [&] (const auto&, const auto&) { ++nNacks; },
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800237 bind([] { BOOST_FAIL("Unexpected timeout"); }));
238
Davide Pesavento0f830802018-01-16 23:58:58 -0500239 advanceClocks(40_ms);
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800240
241 face.receive(makeNack(face.sentInterests.at(1), lp::NackReason::DUPLICATE));
242
Davide Pesavento0f830802018-01-16 23:58:58 -0500243 advanceClocks(50_ms, 2);
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800244
245 BOOST_CHECK_EQUAL(nNacks, 2);
246 BOOST_CHECK_EQUAL(face.sentInterests.size(), 2);
247}
248
Davide Pesaventod247d492020-01-28 21:30:20 -0500249BOOST_AUTO_TEST_CASE(EmptyNackCallback)
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000250{
Junxiao Shib55e5d32018-07-18 13:32:00 -0600251 face.expressInterest(*makeInterest("/Hello/World"),
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000252 bind([] { BOOST_FAIL("Unexpected Data"); }),
253 nullptr,
254 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500255 advanceClocks(1_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000256
257 BOOST_CHECK_NO_THROW(do {
258 face.receive(makeNack(face.sentInterests.at(0), lp::NackReason::DUPLICATE));
Davide Pesavento0f830802018-01-16 23:58:58 -0500259 advanceClocks(1_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000260 } while (false));
261}
262
Davide Pesaventod247d492020-01-28 21:30:20 -0500263BOOST_AUTO_TEST_CASE(PutDataFromDataCallback) // Bug 4596
264{
265 face.expressInterest(*makeInterest("/localhost/notification/1"),
266 [&] (const auto&, const auto&) {
267 face.put(*makeData("/chronosync/sampleDigest/1"));
268 }, nullptr, nullptr);
269 advanceClocks(10_ms);
270 BOOST_CHECK_EQUAL(face.sentInterests.back().getName(), "/localhost/notification/1");
271
272 face.receive(*makeInterest("/chronosync/sampleDigest", true));
273 advanceClocks(10_ms);
274
275 face.put(*makeData("/localhost/notification/1"));
276 advanceClocks(10_ms);
277 BOOST_CHECK_EQUAL(face.sentData.back().getName(), "/chronosync/sampleDigest/1");
278}
279
280BOOST_AUTO_TEST_CASE(DestroyWithPendingInterest)
281{
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500282 auto face2 = make_unique<DummyClientFace>(m_io, m_keyChain);
Davide Pesaventod247d492020-01-28 21:30:20 -0500283 face2->expressInterest(*makeInterest("/Hello/World", false, 50_ms),
284 nullptr, nullptr, nullptr);
285 advanceClocks(50_ms, 2);
286 face2.reset();
287
288 advanceClocks(50_ms, 2); // should not crash - Bug 2518
289
290 // avoid "test case [...] did not check any assertions" message from Boost.Test
291 BOOST_CHECK(true);
292}
293
294BOOST_AUTO_TEST_CASE(Handle)
Junxiao Shi80609d42019-01-29 18:15:22 +0000295{
296 auto hdl = face.expressInterest(*makeInterest("/Hello/World", true, 50_ms),
297 bind([] { BOOST_FAIL("Unexpected data"); }),
298 bind([] { BOOST_FAIL("Unexpected nack"); }),
299 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Davide Pesaventod247d492020-01-28 21:30:20 -0500300 advanceClocks(1_ms);
Junxiao Shi80609d42019-01-29 18:15:22 +0000301 hdl.cancel();
Davide Pesaventod247d492020-01-28 21:30:20 -0500302 advanceClocks(1_ms);
Junxiao Shi80609d42019-01-29 18:15:22 +0000303 face.receive(*makeData("/Hello/World/%21"));
304 advanceClocks(200_ms, 5);
305
Davide Pesaventod247d492020-01-28 21:30:20 -0500306 // cancel after destructing face
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500307 auto face2 = make_unique<DummyClientFace>(m_io, m_keyChain);
Davide Pesaventod247d492020-01-28 21:30:20 -0500308 auto hdl2 = face2->expressInterest(*makeInterest("/Hello/World", true, 50_ms),
309 bind([] { BOOST_FAIL("Unexpected data"); }),
310 bind([] { BOOST_FAIL("Unexpected nack"); }),
311 bind([] { BOOST_FAIL("Unexpected timeout"); }));
312 advanceClocks(1_ms);
313 face2.reset();
314 advanceClocks(1_ms);
315 hdl2.cancel(); // should not crash
316 advanceClocks(1_ms);
317
Junxiao Shi80609d42019-01-29 18:15:22 +0000318 // avoid "test case [...] did not check any assertions" message from Boost.Test
319 BOOST_CHECK(true);
320}
321
Davide Pesaventod247d492020-01-28 21:30:20 -0500322BOOST_AUTO_TEST_SUITE_END() // ExpressInterest
323
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000324BOOST_AUTO_TEST_CASE(RemoveAllPendingInterests)
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500325{
Junxiao Shib55e5d32018-07-18 13:32:00 -0600326 face.expressInterest(*makeInterest("/Hello/World/0", false, 50_ms),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800327 bind([] { BOOST_FAIL("Unexpected data"); }),
328 bind([] { BOOST_FAIL("Unexpected nack"); }),
329 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500330
Junxiao Shib55e5d32018-07-18 13:32:00 -0600331 face.expressInterest(*makeInterest("/Hello/World/1", false, 50_ms),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800332 bind([] { BOOST_FAIL("Unexpected data"); }),
333 bind([] { BOOST_FAIL("Unexpected nack"); }),
334 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500335
Davide Pesavento0f830802018-01-16 23:58:58 -0500336 advanceClocks(10_ms);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500337
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800338 face.removeAllPendingInterests();
Davide Pesavento0f830802018-01-16 23:58:58 -0500339 advanceClocks(10_ms);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500340
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800341 BOOST_CHECK_EQUAL(face.getNPendingInterests(), 0);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500342
Junxiao Shi85d90832016-08-04 03:19:46 +0000343 face.receive(*makeData("/Hello/World/0"));
344 face.receive(*makeData("/Hello/World/1"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500345 advanceClocks(200_ms, 5);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500346}
347
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000348BOOST_AUTO_TEST_SUITE(Producer)
349
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000350BOOST_AUTO_TEST_CASE(PutData)
351{
352 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
353
354 Data data("/4g7xxcuEow/KFvK5Kf2m");
355 signData(data);
356 face.put(data);
357
358 lp::CachePolicy cachePolicy;
359 cachePolicy.setPolicy(lp::CachePolicyType::NO_CACHE);
360 data.setTag(make_shared<lp::CachePolicyTag>(cachePolicy));
Eric Newberry4d261b62016-11-10 13:40:09 -0700361 data.setTag(make_shared<lp::CongestionMarkTag>(1));
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000362 face.put(data);
363
Davide Pesavento0f830802018-01-16 23:58:58 -0500364 advanceClocks(10_ms);
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000365 BOOST_REQUIRE_EQUAL(face.sentData.size(), 2);
366 BOOST_CHECK(face.sentData[0].getTag<lp::CachePolicyTag>() == nullptr);
Eric Newberry4d261b62016-11-10 13:40:09 -0700367 BOOST_CHECK(face.sentData[0].getTag<lp::CongestionMarkTag>() == nullptr);
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000368 BOOST_CHECK(face.sentData[1].getTag<lp::CachePolicyTag>() != nullptr);
Eric Newberry4d261b62016-11-10 13:40:09 -0700369 BOOST_CHECK(face.sentData[1].getTag<lp::CongestionMarkTag>() != nullptr);
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000370}
371
Junxiao Shib6828912017-11-20 14:06:32 +0000372BOOST_AUTO_TEST_CASE(PutDataLoopback)
373{
374 bool hasInterest1 = false, hasData = false;
375
376 // first InterestFilter allows loopback and should receive Interest
Davide Pesavento720e25c2019-07-14 01:33:52 -0400377 face.setInterestFilter("/", [&] (const InterestFilter&, const Interest&) {
Junxiao Shib6828912017-11-20 14:06:32 +0000378 hasInterest1 = true;
379 // do not respond with Data right away, so Face must send Interest to forwarder
380 });
381 // second InterestFilter disallows loopback and should not receive Interest
382 face.setInterestFilter(InterestFilter("/").allowLoopback(false),
383 bind([] { BOOST_ERROR("Unexpected Interest on second InterestFilter"); }));
384
Junxiao Shib55e5d32018-07-18 13:32:00 -0600385 face.expressInterest(*makeInterest("/A", true),
Junxiao Shib6828912017-11-20 14:06:32 +0000386 bind([&] { hasData = true; }),
387 bind([] { BOOST_FAIL("Unexpected nack"); }),
388 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500389 advanceClocks(1_ms);
Junxiao Shib6828912017-11-20 14:06:32 +0000390 BOOST_CHECK_EQUAL(hasInterest1, true); // Interest looped back
391 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1); // Interest sent to forwarder
392 BOOST_CHECK_EQUAL(hasData, false); // waiting for Data
393
394 face.put(*makeData("/A/B")); // first InterestFilter responds with Data
Davide Pesavento0f830802018-01-16 23:58:58 -0500395 advanceClocks(1_ms);
Junxiao Shib6828912017-11-20 14:06:32 +0000396 BOOST_CHECK_EQUAL(hasData, true);
397 BOOST_CHECK_EQUAL(face.sentData.size(), 0); // do not spill Data to forwarder
398}
399
Junxiao Shi859888f2017-09-12 14:29:16 +0000400BOOST_AUTO_TEST_CASE(PutMultipleData)
401{
402 bool hasInterest1 = false;
403 // register two Interest destinations
404 face.setInterestFilter("/", bind([&] {
405 hasInterest1 = true;
406 // sending Data right away from the first destination, don't care whether Interest goes to second destination
407 face.put(*makeData("/A/B"));
408 }));
409 face.setInterestFilter("/", bind([]{}));
Davide Pesavento0f830802018-01-16 23:58:58 -0500410 advanceClocks(10_ms);
Junxiao Shi859888f2017-09-12 14:29:16 +0000411
Junxiao Shib55e5d32018-07-18 13:32:00 -0600412 face.receive(*makeInterest("/A", true));
Davide Pesavento0f830802018-01-16 23:58:58 -0500413 advanceClocks(10_ms);
Junxiao Shi859888f2017-09-12 14:29:16 +0000414 BOOST_CHECK(hasInterest1);
415 BOOST_CHECK_EQUAL(face.sentData.size(), 1);
416 BOOST_CHECK_EQUAL(face.sentData.at(0).getName(), "/A/B");
417
418 face.put(*makeData("/A/C"));
419 BOOST_CHECK_EQUAL(face.sentData.size(), 1); // additional Data are ignored
420}
421
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000422BOOST_AUTO_TEST_CASE(PutNack)
423{
Junxiao Shi79a7a162017-09-09 08:33:57 +0000424 face.setInterestFilter("/", bind([]{})); // register one Interest destination so that face can accept Nacks
Davide Pesavento0f830802018-01-16 23:58:58 -0500425 advanceClocks(10_ms);
Junxiao Shi79a7a162017-09-09 08:33:57 +0000426
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000427 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0);
428
Davide Pesavento287a7fa2020-10-01 22:46:43 -0400429 face.put(makeNack(*makeInterest("/unsolicited", false, nullopt, 18645250),
Junxiao Shib55e5d32018-07-18 13:32:00 -0600430 lp::NackReason::NO_ROUTE));
Davide Pesavento0f830802018-01-16 23:58:58 -0500431 advanceClocks(10_ms);
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000432 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0); // unsolicited Nack would not be sent
Eric Newberry4d261b62016-11-10 13:40:09 -0700433
Davide Pesavento287a7fa2020-10-01 22:46:43 -0400434 auto interest1 = makeInterest("/Hello/World", false, nullopt, 14247162);
Junxiao Shib55e5d32018-07-18 13:32:00 -0600435 face.receive(*interest1);
Davide Pesavento287a7fa2020-10-01 22:46:43 -0400436 auto interest2 = makeInterest("/another/prefix", false, nullopt, 92203002);
Junxiao Shib55e5d32018-07-18 13:32:00 -0600437 face.receive(*interest2);
Davide Pesavento0f830802018-01-16 23:58:58 -0500438 advanceClocks(10_ms);
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000439
Junxiao Shib55e5d32018-07-18 13:32:00 -0600440 face.put(makeNack(*interest1, lp::NackReason::DUPLICATE));
Davide Pesavento0f830802018-01-16 23:58:58 -0500441 advanceClocks(10_ms);
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000442 BOOST_REQUIRE_EQUAL(face.sentNacks.size(), 1);
443 BOOST_CHECK_EQUAL(face.sentNacks[0].getReason(), lp::NackReason::DUPLICATE);
444 BOOST_CHECK(face.sentNacks[0].getTag<lp::CongestionMarkTag>() == nullptr);
445
Junxiao Shib55e5d32018-07-18 13:32:00 -0600446 auto nack = makeNack(*interest2, lp::NackReason::NO_ROUTE);
Eric Newberry4d261b62016-11-10 13:40:09 -0700447 nack.setTag(make_shared<lp::CongestionMarkTag>(1));
448 face.put(nack);
Davide Pesavento0f830802018-01-16 23:58:58 -0500449 advanceClocks(10_ms);
Eric Newberry4d261b62016-11-10 13:40:09 -0700450 BOOST_REQUIRE_EQUAL(face.sentNacks.size(), 2);
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000451 BOOST_CHECK_EQUAL(face.sentNacks[1].getReason(), lp::NackReason::NO_ROUTE);
Eric Newberry4d261b62016-11-10 13:40:09 -0700452 BOOST_CHECK(face.sentNacks[1].getTag<lp::CongestionMarkTag>() != nullptr);
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000453}
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500454
Junxiao Shi79a7a162017-09-09 08:33:57 +0000455BOOST_AUTO_TEST_CASE(PutMultipleNack)
456{
Junxiao Shi859888f2017-09-12 14:29:16 +0000457 bool hasInterest1 = false, hasInterest2 = false;
458 // register two Interest destinations
459 face.setInterestFilter("/", [&] (const InterestFilter&, const Interest& interest) {
460 hasInterest1 = true;
461 // sending Nack right away from the first destination, Interest should still go to second destination
462 face.put(makeNack(interest, lp::NackReason::CONGESTION));
463 });
464 face.setInterestFilter("/", bind([&] { hasInterest2 = true; }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500465 advanceClocks(10_ms);
Junxiao Shi79a7a162017-09-09 08:33:57 +0000466
Davide Pesavento287a7fa2020-10-01 22:46:43 -0400467 auto interest = makeInterest("/A", false, nullopt, 14333271);
Junxiao Shib55e5d32018-07-18 13:32:00 -0600468 face.receive(*interest);
Davide Pesavento0f830802018-01-16 23:58:58 -0500469 advanceClocks(10_ms);
Junxiao Shi859888f2017-09-12 14:29:16 +0000470 BOOST_CHECK(hasInterest1);
471 BOOST_CHECK(hasInterest2);
Junxiao Shi79a7a162017-09-09 08:33:57 +0000472
Junxiao Shi859888f2017-09-12 14:29:16 +0000473 // Nack from first destination is received, should wait for a response from the other destination
474 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0);
Junxiao Shi79a7a162017-09-09 08:33:57 +0000475
Junxiao Shib55e5d32018-07-18 13:32:00 -0600476 face.put(makeNack(*interest, lp::NackReason::NO_ROUTE)); // Nack from second destination
Davide Pesavento0f830802018-01-16 23:58:58 -0500477 advanceClocks(10_ms);
Junxiao Shi859888f2017-09-12 14:29:16 +0000478 BOOST_CHECK_EQUAL(face.sentNacks.size(), 1); // sending Nack after both destinations Nacked
Junxiao Shi79a7a162017-09-09 08:33:57 +0000479 BOOST_CHECK_EQUAL(face.sentNacks.at(0).getReason(), lp::NackReason::CONGESTION); // least severe reason
480
Junxiao Shib55e5d32018-07-18 13:32:00 -0600481 face.put(makeNack(*interest, lp::NackReason::DUPLICATE));
Junxiao Shi79a7a162017-09-09 08:33:57 +0000482 BOOST_CHECK_EQUAL(face.sentNacks.size(), 1); // additional Nacks are ignored
483}
484
Junxiao Shib6828912017-11-20 14:06:32 +0000485BOOST_AUTO_TEST_CASE(PutMultipleNackLoopback)
486{
487 bool hasInterest1 = false, hasNack = false;
488
489 // first InterestFilter allows loopback and should receive Interest
490 face.setInterestFilter("/", [&] (const InterestFilter&, const Interest& interest) {
491 hasInterest1 = true;
492 face.put(makeNack(interest, lp::NackReason::CONGESTION));
493 });
494 // second InterestFilter disallows loopback and should not receive Interest
495 face.setInterestFilter(InterestFilter("/").allowLoopback(false),
496 bind([] { BOOST_ERROR("Unexpected Interest on second InterestFilter"); }));
497
Davide Pesavento287a7fa2020-10-01 22:46:43 -0400498 auto interest = makeInterest("/A", false, nullopt, 28395852);
Junxiao Shib55e5d32018-07-18 13:32:00 -0600499 face.expressInterest(*interest,
Junxiao Shib6828912017-11-20 14:06:32 +0000500 bind([] { BOOST_FAIL("Unexpected data"); }),
501 [&] (const Interest&, const lp::Nack& nack) {
502 hasNack = true;
503 BOOST_CHECK_EQUAL(nack.getReason(), lp::NackReason::CONGESTION);
504 },
505 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500506 advanceClocks(1_ms);
Junxiao Shib6828912017-11-20 14:06:32 +0000507 BOOST_CHECK_EQUAL(hasInterest1, true); // Interest looped back
508 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1); // Interest sent to forwarder
509 BOOST_CHECK_EQUAL(hasNack, false); // waiting for Nack from forwarder
510
Junxiao Shib55e5d32018-07-18 13:32:00 -0600511 face.receive(makeNack(*interest, lp::NackReason::NO_ROUTE));
Davide Pesavento0f830802018-01-16 23:58:58 -0500512 advanceClocks(1_ms);
Junxiao Shib6828912017-11-20 14:06:32 +0000513 BOOST_CHECK_EQUAL(hasNack, true);
514}
515
Davide Pesaventod247d492020-01-28 21:30:20 -0500516BOOST_AUTO_TEST_SUITE_END() // Producer
517
518BOOST_AUTO_TEST_SUITE(RegisterPrefix)
519
520BOOST_FIXTURE_TEST_CASE(Failure, FaceFixture<NoPrefixRegReply>)
521{
522 BOOST_CHECK(!runPrefixReg([&] (const auto& success, const auto& failure) {
523 face.registerPrefix("/Hello/World", success, failure);
524 this->advanceClocks(5_s, 20); // wait for command timeout
525 }));
526}
527
528BOOST_AUTO_TEST_CASE(Handle)
529{
530 RegisteredPrefixHandle hdl;
531 auto doReg = [&] {
532 return runPrefixReg([&] (const auto& success, const auto& failure) {
533 hdl = face.registerPrefix("/Hello/World", success, failure);
534 });
535 };
536 auto doUnreg = [&] {
537 return runPrefixUnreg([&] (const auto& success, const auto& failure) {
538 hdl.unregister(success, failure);
539 });
540 };
541
542 // despite the "undefined behavior" warning, we try not to crash, but no API guarantee for this
543 BOOST_CHECK(!doUnreg());
544
545 // cancel after unregister
546 BOOST_CHECK(doReg());
547 BOOST_CHECK(doUnreg());
548 hdl.cancel();
549 advanceClocks(1_ms);
550
551 // unregister after cancel
552 BOOST_CHECK(doReg());
553 hdl.cancel();
554 advanceClocks(1_ms);
555 BOOST_CHECK(!doUnreg());
556
557 // cancel after destructing face
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500558 auto face2 = make_unique<DummyClientFace>(m_io, m_keyChain);
Davide Pesaventod247d492020-01-28 21:30:20 -0500559 hdl = face2->registerPrefix("/Hello/World/2", nullptr,
560 bind([] { BOOST_FAIL("Unexpected registerPrefix failure"); }));
561 advanceClocks(1_ms);
562 face2.reset();
563 advanceClocks(1_ms);
564 hdl.cancel(); // should not crash
565 advanceClocks(1_ms);
566
567 // unregister after destructing face
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500568 auto face3 = make_unique<DummyClientFace>(m_io, m_keyChain);
Davide Pesaventod247d492020-01-28 21:30:20 -0500569 hdl = face3->registerPrefix("/Hello/World/3", nullptr,
570 bind([] { BOOST_FAIL("Unexpected registerPrefix failure"); }));
571 advanceClocks(1_ms);
572 face3.reset();
573 advanceClocks(1_ms);
574 BOOST_CHECK(!doUnreg());
575}
576
577BOOST_AUTO_TEST_SUITE_END() // RegisterPrefix
578
579BOOST_AUTO_TEST_SUITE(SetInterestFilter)
580
581BOOST_AUTO_TEST_CASE(SetAndCancel)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400582{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800583 size_t nInterests = 0;
584 size_t nRegs = 0;
Davide Pesavento720e25c2019-07-14 01:33:52 -0400585 auto hdl = face.setInterestFilter("/Hello/World",
586 bind([&nInterests] { ++nInterests; }),
587 bind([&nRegs] { ++nRegs; }),
588 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500589 advanceClocks(25_ms, 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800590 BOOST_CHECK_EQUAL(nRegs, 1);
591 BOOST_CHECK_EQUAL(nInterests, 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400592
Junxiao Shib55e5d32018-07-18 13:32:00 -0600593 face.receive(*makeInterest("/Hello/World/%21"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500594 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400595
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800596 BOOST_CHECK_EQUAL(nRegs, 1);
597 BOOST_CHECK_EQUAL(nInterests, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400598
Junxiao Shib55e5d32018-07-18 13:32:00 -0600599 face.receive(*makeInterest("/Bye/World/%21"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500600 advanceClocks(10000_ms, 10);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800601 BOOST_CHECK_EQUAL(nInterests, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400602
Junxiao Shib55e5d32018-07-18 13:32:00 -0600603 face.receive(*makeInterest("/Hello/World/%21/2"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500604 advanceClocks(25_ms, 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800605 BOOST_CHECK_EQUAL(nInterests, 2);
606
607 // removing filter
Davide Pesavento720e25c2019-07-14 01:33:52 -0400608 hdl.cancel();
Davide Pesavento0f830802018-01-16 23:58:58 -0500609 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400610
Junxiao Shib55e5d32018-07-18 13:32:00 -0600611 face.receive(*makeInterest("/Hello/World/%21/3"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800612 BOOST_CHECK_EQUAL(nInterests, 2);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800613}
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400614
Davide Pesaventod247d492020-01-28 21:30:20 -0500615BOOST_AUTO_TEST_CASE(EmptyInterestCallback)
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000616{
617 face.setInterestFilter("/A", nullptr);
Davide Pesavento0f830802018-01-16 23:58:58 -0500618 advanceClocks(1_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000619
620 BOOST_CHECK_NO_THROW(do {
621 face.receive(*makeInterest("/A/1"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500622 advanceClocks(1_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000623 } while (false));
624}
625
Davide Pesaventod247d492020-01-28 21:30:20 -0500626BOOST_AUTO_TEST_CASE(WithoutSuccessCallback)
Joao Pereira0b3cac52015-07-02 14:49:49 -0400627{
628 size_t nInterests = 0;
Davide Pesavento720e25c2019-07-14 01:33:52 -0400629 auto hdl = face.setInterestFilter("/Hello/World",
630 bind([&nInterests] { ++nInterests; }),
631 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500632 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400633 BOOST_CHECK_EQUAL(nInterests, 0);
634
Junxiao Shib55e5d32018-07-18 13:32:00 -0600635 face.receive(*makeInterest("/Hello/World/%21"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500636 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400637
638 BOOST_CHECK_EQUAL(nInterests, 1);
639
Junxiao Shib55e5d32018-07-18 13:32:00 -0600640 face.receive(*makeInterest("/Bye/World/%21"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500641 advanceClocks(10000_ms, 10);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400642 BOOST_CHECK_EQUAL(nInterests, 1);
643
Junxiao Shib55e5d32018-07-18 13:32:00 -0600644 face.receive(*makeInterest("/Hello/World/%21/2"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500645 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400646 BOOST_CHECK_EQUAL(nInterests, 2);
647
648 // removing filter
Davide Pesavento720e25c2019-07-14 01:33:52 -0400649 hdl.cancel();
Davide Pesavento0f830802018-01-16 23:58:58 -0500650 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400651
Junxiao Shib55e5d32018-07-18 13:32:00 -0600652 face.receive(*makeInterest("/Hello/World/%21/3"));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400653 BOOST_CHECK_EQUAL(nInterests, 2);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400654}
655
Davide Pesaventod247d492020-01-28 21:30:20 -0500656BOOST_FIXTURE_TEST_CASE(Failure, FaceFixture<NoPrefixRegReply>)
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800657{
658 // don't enable registration reply
659 size_t nRegFailed = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800660 face.setInterestFilter("/Hello/World",
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000661 bind([] { BOOST_FAIL("Unexpected Interest"); }),
662 bind([] { BOOST_FAIL("Unexpected success of setInterestFilter"); }),
663 bind([&nRegFailed] { ++nRegFailed; }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800664
Davide Pesavento0f830802018-01-16 23:58:58 -0500665 advanceClocks(25_ms, 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800666 BOOST_CHECK_EQUAL(nRegFailed, 0);
667
Davide Pesavento0f830802018-01-16 23:58:58 -0500668 advanceClocks(2000_ms, 5);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800669 BOOST_CHECK_EQUAL(nRegFailed, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400670}
671
Davide Pesaventod247d492020-01-28 21:30:20 -0500672BOOST_FIXTURE_TEST_CASE(FailureWithoutSuccessCallback, FaceFixture<NoPrefixRegReply>)
Joao Pereira0b3cac52015-07-02 14:49:49 -0400673{
674 // don't enable registration reply
675 size_t nRegFailed = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800676 face.setInterestFilter("/Hello/World",
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000677 bind([] { BOOST_FAIL("Unexpected Interest"); }),
678 bind([&nRegFailed] { ++nRegFailed; }));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400679
Davide Pesavento0f830802018-01-16 23:58:58 -0500680 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400681 BOOST_CHECK_EQUAL(nRegFailed, 0);
682
Davide Pesavento0f830802018-01-16 23:58:58 -0500683 advanceClocks(2000_ms, 5);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400684 BOOST_CHECK_EQUAL(nRegFailed, 1);
685}
686
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800687BOOST_AUTO_TEST_CASE(SimilarFilters)
688{
689 size_t nInInterests1 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800690 face.setInterestFilter("/Hello/World",
691 bind([&nInInterests1] { ++nInInterests1; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000692 nullptr,
693 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800694
695 size_t nInInterests2 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800696 face.setInterestFilter("/Hello",
697 bind([&nInInterests2] { ++nInInterests2; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000698 nullptr,
699 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400700
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800701 size_t nInInterests3 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800702 face.setInterestFilter("/Los/Angeles/Lakers",
703 bind([&nInInterests3] { ++nInInterests3; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000704 nullptr,
705 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400706
Davide Pesavento0f830802018-01-16 23:58:58 -0500707 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400708
Junxiao Shib55e5d32018-07-18 13:32:00 -0600709 face.receive(*makeInterest("/Hello/World/%21"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500710 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400711
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800712 BOOST_CHECK_EQUAL(nInInterests1, 1);
713 BOOST_CHECK_EQUAL(nInInterests2, 1);
714 BOOST_CHECK_EQUAL(nInInterests3, 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400715}
716
Davide Pesaventod247d492020-01-28 21:30:20 -0500717BOOST_AUTO_TEST_CASE(RegexFilter)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400718{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800719 size_t nInInterests = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800720 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
721 bind([&nInInterests] { ++nInInterests; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000722 nullptr,
723 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400724
Davide Pesavento0f830802018-01-16 23:58:58 -0500725 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400726
Junxiao Shib55e5d32018-07-18 13:32:00 -0600727 face.receive(*makeInterest("/Hello/World/a")); // shouldn't match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400728 BOOST_CHECK_EQUAL(nInInterests, 0);
729
Junxiao Shib55e5d32018-07-18 13:32:00 -0600730 face.receive(*makeInterest("/Hello/World/a/b")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400731 BOOST_CHECK_EQUAL(nInInterests, 1);
732
Junxiao Shib55e5d32018-07-18 13:32:00 -0600733 face.receive(*makeInterest("/Hello/World/a/b/c")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400734 BOOST_CHECK_EQUAL(nInInterests, 2);
735
Junxiao Shib55e5d32018-07-18 13:32:00 -0600736 face.receive(*makeInterest("/Hello/World/a/b/d")); // should not match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400737 BOOST_CHECK_EQUAL(nInInterests, 2);
738}
739
Davide Pesaventod247d492020-01-28 21:30:20 -0500740BOOST_AUTO_TEST_CASE(RegexFilterError)
741{
742 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
743 [] (const Name&, const Interest&) {
744 BOOST_FAIL("InterestFilter::Error should have been triggered");
745 },
746 nullptr,
747 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
748
749 advanceClocks(25_ms, 4);
750
751 BOOST_CHECK_THROW(face.receive(*makeInterest("/Hello/World/XXX/b/c")), InterestFilter::Error);
752}
753
754BOOST_AUTO_TEST_CASE(RegexFilterAndRegisterPrefix)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400755{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800756 size_t nInInterests = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800757 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
758 bind([&nInInterests] { ++nInInterests; }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400759
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800760 size_t nRegSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800761 face.registerPrefix("/Hello/World",
762 bind([&nRegSuccesses] { ++nRegSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000763 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400764
Davide Pesavento0f830802018-01-16 23:58:58 -0500765 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400766 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
767
Junxiao Shib55e5d32018-07-18 13:32:00 -0600768 face.receive(*makeInterest("/Hello/World/a")); // shouldn't match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400769 BOOST_CHECK_EQUAL(nInInterests, 0);
770
Junxiao Shib55e5d32018-07-18 13:32:00 -0600771 face.receive(*makeInterest("/Hello/World/a/b")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400772 BOOST_CHECK_EQUAL(nInInterests, 1);
773
Junxiao Shib55e5d32018-07-18 13:32:00 -0600774 face.receive(*makeInterest("/Hello/World/a/b/c")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400775 BOOST_CHECK_EQUAL(nInInterests, 2);
776
Junxiao Shib55e5d32018-07-18 13:32:00 -0600777 face.receive(*makeInterest("/Hello/World/a/b/d")); // should not match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400778 BOOST_CHECK_EQUAL(nInInterests, 2);
779}
780
Davide Pesaventod247d492020-01-28 21:30:20 -0500781BOOST_FIXTURE_TEST_CASE(WithoutRegisterPrefix, FaceFixture<NoPrefixRegReply>) // Bug 2318
Junxiao Shia1ea5062014-12-27 22:33:39 -0700782{
783 // This behavior is specific to DummyClientFace.
784 // Regular Face won't accept incoming packets until something is sent.
785
786 int hit = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800787 face.setInterestFilter(Name("/"), bind([&hit] { ++hit; }));
788 face.processEvents(time::milliseconds(-1));
Junxiao Shia1ea5062014-12-27 22:33:39 -0700789
Junxiao Shib55e5d32018-07-18 13:32:00 -0600790 face.receive(*makeInterest("/A"));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800791 face.processEvents(time::milliseconds(-1));
Junxiao Shia1ea5062014-12-27 22:33:39 -0700792
793 BOOST_CHECK_EQUAL(hit, 1);
794}
795
Davide Pesaventod247d492020-01-28 21:30:20 -0500796BOOST_AUTO_TEST_CASE(Handle)
Junxiao Shi60aaef02019-01-14 04:59:30 +0000797{
798 int hit = 0;
Davide Pesaventod247d492020-01-28 21:30:20 -0500799 InterestFilterHandle hdl = face.setInterestFilter(Name("/"), bind([&hit] { ++hit; }));
Junxiao Shi60aaef02019-01-14 04:59:30 +0000800 face.processEvents(-1_ms);
801
802 face.receive(*makeInterest("/A"));
803 face.processEvents(-1_ms);
804 BOOST_CHECK_EQUAL(hit, 1);
805
806 hdl.cancel();
807 face.processEvents(-1_ms);
808
809 face.receive(*makeInterest("/B"));
810 face.processEvents(-1_ms);
811 BOOST_CHECK_EQUAL(hit, 1);
Davide Pesaventod247d492020-01-28 21:30:20 -0500812
813 // cancel after destructing face
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500814 auto face2 = make_unique<DummyClientFace>(m_io, m_keyChain);
Davide Pesaventod247d492020-01-28 21:30:20 -0500815 InterestFilterHandle hdl2 = face2->setInterestFilter("/Hello/World/2", nullptr);
816 advanceClocks(1_ms);
817 face2.reset();
818 advanceClocks(1_ms);
819 hdl2.cancel(); // should not crash
820 advanceClocks(1_ms);
Junxiao Shi60aaef02019-01-14 04:59:30 +0000821}
822
Davide Pesaventod247d492020-01-28 21:30:20 -0500823BOOST_AUTO_TEST_SUITE_END() // SetInterestFilter
Junxiao Shiae0b4182016-08-08 22:53:17 +0000824
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500825BOOST_AUTO_TEST_CASE(ProcessEvents)
826{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800827 face.processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500828
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800829 size_t nRegSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800830 face.registerPrefix("/Hello/World",
831 bind([&nRegSuccesses] { ++nRegSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000832 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500833
834 // io_service::poll() without reset
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800835 face.getIoService().poll();
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500836 BOOST_CHECK_EQUAL(nRegSuccesses, 0);
837
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800838 face.processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500839 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
840}
841
Junxiao Shiae0b4182016-08-08 22:53:17 +0000842BOOST_AUTO_TEST_CASE(DestroyWithoutProcessEvents) // Bug 3248
843{
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500844 auto face2 = make_unique<Face>(m_io);
Junxiao Shiae0b4182016-08-08 22:53:17 +0000845 face2.reset();
846
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500847 m_io.poll(); // should not crash
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100848
849 // avoid "test case [...] did not check any assertions" message from Boost.Test
850 BOOST_CHECK(true);
Junxiao Shiae0b4182016-08-08 22:53:17 +0000851}
852
Junxiao Shiae0b4182016-08-08 22:53:17 +0000853BOOST_AUTO_TEST_SUITE(Transport)
854
855using ndn::Transport;
856
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700857struct PibDirWithDefaultTpm
858{
859 const std::string PATH = "build/keys-with-default-tpm";
860};
861
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500862BOOST_FIXTURE_TEST_CASE(FaceTransport, IoKeyChainFixture)
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800863{
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800864 BOOST_CHECK(Face().getTransport() != nullptr);
865
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800866 BOOST_CHECK(Face(shared_ptr<Transport>()).getTransport() != nullptr);
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500867 BOOST_CHECK(Face(shared_ptr<Transport>(), m_io).getTransport() != nullptr);
868 BOOST_CHECK(Face(shared_ptr<Transport>(), m_io, m_keyChain).getTransport() != nullptr);
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800869
870 auto transport = make_shared<TcpTransport>("localhost", "6363"); // no real io operations will be scheduled
871 BOOST_CHECK(Face(transport).getTransport() == transport);
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500872 BOOST_CHECK(Face(transport, m_io).getTransport() == transport);
873 BOOST_CHECK(Face(transport, m_io, m_keyChain).getTransport() == transport);
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800874}
875
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500876class WithEnv
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700877{
878public:
879 WithEnv()
880 {
881 if (getenv("NDN_CLIENT_TRANSPORT") != nullptr) {
882 m_oldTransport = getenv("NDN_CLIENT_TRANSPORT");
883 unsetenv("NDN_CLIENT_TRANSPORT");
884 }
885 }
886
887 void
888 configure(const std::string& faceUri)
889 {
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500890 setenv("NDN_CLIENT_TRANSPORT", faceUri.data(), true);
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700891 }
892
893 ~WithEnv()
894 {
895 if (!m_oldTransport.empty()) {
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500896 setenv("NDN_CLIENT_TRANSPORT", m_oldTransport.data(), true);
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700897 }
898 else {
899 unsetenv("NDN_CLIENT_TRANSPORT");
900 }
901 }
902
903private:
904 std::string m_oldTransport;
905};
906
907class WithConfig : private TestHomeFixture<DefaultPibDir>
908{
909public:
910 void
911 configure(const std::string& faceUri)
912 {
913 createClientConf({"transport=" + faceUri});
914 }
915};
916
917class WithEnvAndConfig : public WithEnv, public WithConfig
918{
919};
920
921typedef boost::mpl::vector<WithEnv, WithConfig> ConfigOptions;
922
923BOOST_FIXTURE_TEST_CASE(NoConfig, WithEnvAndConfig) // fixture configures test HOME and PIB/TPM path
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700924{
925 shared_ptr<Face> face;
926 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
927 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700928}
929
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700930BOOST_FIXTURE_TEST_CASE_TEMPLATE(Unix, T, ConfigOptions, T)
931{
932 this->configure("unix://some/path");
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700933
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700934 shared_ptr<Face> face;
935 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
936 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
937}
938
939BOOST_FIXTURE_TEST_CASE_TEMPLATE(Tcp, T, ConfigOptions, T)
940{
941 this->configure("tcp://127.0.0.1:6000");
942
943 shared_ptr<Face> face;
944 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
945 BOOST_CHECK(dynamic_pointer_cast<TcpTransport>(face->getTransport()) != nullptr);
946}
947
948BOOST_FIXTURE_TEST_CASE_TEMPLATE(WrongTransport, T, ConfigOptions, T)
949{
950 this->configure("wrong-transport:");
951
952 BOOST_CHECK_THROW(make_shared<Face>(), ConfigFile::Error);
953}
954
955BOOST_FIXTURE_TEST_CASE_TEMPLATE(WrongUri, T, ConfigOptions, T)
956{
957 this->configure("wrong-uri");
958
959 BOOST_CHECK_THROW(make_shared<Face>(), ConfigFile::Error);
960}
961
962BOOST_FIXTURE_TEST_CASE(EnvOverride, WithEnvAndConfig)
963{
964 this->WithEnv::configure("tcp://127.0.0.1:6000");
965 this->WithConfig::configure("unix://some/path");
966
967 shared_ptr<Face> face;
968 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
969 BOOST_CHECK(dynamic_pointer_cast<TcpTransport>(face->getTransport()) != nullptr);
970}
971
972BOOST_FIXTURE_TEST_CASE(ExplicitTransport, WithEnvAndConfig)
973{
974 this->WithEnv::configure("wrong-uri");
975 this->WithConfig::configure("wrong-transport:");
976
977 auto transport = make_shared<UnixTransport>("unix://some/path");
978 shared_ptr<Face> face;
979 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>(transport));
980 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
981}
982
Junxiao Shiae0b4182016-08-08 22:53:17 +0000983BOOST_AUTO_TEST_SUITE_END() // Transport
984
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700985BOOST_AUTO_TEST_SUITE_END() // TestFace
986
987} // namespace tests
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400988} // namespace ndn