blob: f7c6132fc49d1ebbde2a72c743f8afb40365a004 [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 Pesaventod247d492020-01-28 21:30:20 -05003 * Copyright (c) 2013-2020 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;
Junxiao Shia60d9362014-11-12 09:38:21 -070038
Junxiao Shiec475a72019-01-13 21:53:55 +000039struct WantPrefixRegReply;
40struct NoPrefixRegReply;
41
42template<typename PrefixRegReply = WantPrefixRegReply>
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050043class FaceFixture : public IoKeyChainFixture
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040044{
Davide Pesaventod247d492020-01-28 21:30:20 -050045protected:
Junxiao Shiec475a72019-01-13 21:53:55 +000046 FaceFixture()
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050047 : face(m_io, m_keyChain, {true, !std::is_same<PrefixRegReply, NoPrefixRegReply>::value})
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040048 {
Junxiao Shiec475a72019-01-13 21:53:55 +000049 static_assert(std::is_same<PrefixRegReply, WantPrefixRegReply>::value ||
50 std::is_same<PrefixRegReply, NoPrefixRegReply>::value, "");
51 }
52
53 /** \brief Execute a prefix registration, and optionally check the name in callback.
54 * \return whether the prefix registration succeeded.
55 */
56 bool
57 runPrefixReg(function<void(const RegisterPrefixSuccessCallback& success,
58 const RegisterPrefixFailureCallback& failure)> f)
59 {
60 boost::logic::tribool result = boost::logic::indeterminate;
Davide Pesaventod247d492020-01-28 21:30:20 -050061 f([&] (auto) { result = true; },
62 [&] (auto, auto) { result = false; });
Junxiao Shiec475a72019-01-13 21:53:55 +000063
64 advanceClocks(1_ms);
65 BOOST_REQUIRE(!boost::logic::indeterminate(result));
66 return static_cast<bool>(result);
67 }
68
69 /** \brief Execute a prefix unregistration, and optionally check the name in callback.
70 * \return whether the prefix unregistration succeeded.
71 */
72 bool
73 runPrefixUnreg(function<void(const UnregisterPrefixSuccessCallback& success,
74 const UnregisterPrefixFailureCallback& failure)> f)
75 {
76 boost::logic::tribool result = boost::logic::indeterminate;
Davide Pesaventod247d492020-01-28 21:30:20 -050077 f([&] { result = true; },
78 [&] (auto) { result = false; });
Junxiao Shiec475a72019-01-13 21:53:55 +000079
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
Davide Pesaventod247d492020-01-28 21:30:20 -050085protected:
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
Davide Pesaventod247d492020-01-28 21:30:20 -050091BOOST_AUTO_TEST_SUITE(ExpressInterest)
Junxiao Shi103d8ed2016-08-07 20:34:10 +000092
Davide Pesaventod247d492020-01-28 21:30:20 -050093BOOST_AUTO_TEST_CASE(ReplyData)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040094{
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
Davide Pesaventod247d492020-01-28 21:30:20 -0500125BOOST_AUTO_TEST_CASE(MultipleData)
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800126{
127 size_t nData = 0;
128
Junxiao Shib55e5d32018-07-18 13:32:00 -0600129 face.expressInterest(*makeInterest("/Hello/World", true, 50_ms),
Davide Pesaventod247d492020-01-28 21:30:20 -0500130 [&] (const auto&, const auto&) { ++nData; },
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800131 bind([] { BOOST_FAIL("Unexpected Nack"); }),
132 bind([] { BOOST_FAIL("Unexpected timeout"); }));
133
Junxiao Shib55e5d32018-07-18 13:32:00 -0600134 face.expressInterest(*makeInterest("/Hello/World/a", true, 50_ms),
Davide Pesaventod247d492020-01-28 21:30:20 -0500135 [&] (const auto&, const auto&) { ++nData; },
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800136 bind([] { BOOST_FAIL("Unexpected Nack"); }),
137 bind([] { BOOST_FAIL("Unexpected timeout"); }));
138
Davide Pesavento0f830802018-01-16 23:58:58 -0500139 advanceClocks(40_ms);
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800140
141 face.receive(*makeData("/Hello/World/a/b"));
142
Davide Pesavento0f830802018-01-16 23:58:58 -0500143 advanceClocks(50_ms, 2);
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800144
145 BOOST_CHECK_EQUAL(nData, 2);
146 BOOST_CHECK_EQUAL(face.sentInterests.size(), 2);
147 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
148}
149
Davide Pesaventod247d492020-01-28 21:30:20 -0500150BOOST_AUTO_TEST_CASE(EmptyDataCallback)
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000151{
Junxiao Shib55e5d32018-07-18 13:32:00 -0600152 face.expressInterest(*makeInterest("/Hello/World", true),
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000153 nullptr,
154 bind([] { BOOST_FAIL("Unexpected Nack"); }),
155 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500156 advanceClocks(1_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000157
158 BOOST_CHECK_NO_THROW(do {
159 face.receive(*makeData("/Hello/World/a"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500160 advanceClocks(1_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000161 } while (false));
162}
163
Davide Pesaventod247d492020-01-28 21:30:20 -0500164BOOST_AUTO_TEST_CASE(Timeout)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400165{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800166 size_t nTimeouts = 0;
Junxiao Shib55e5d32018-07-18 13:32:00 -0600167 face.expressInterest(*makeInterest("/Hello/World", false, 50_ms),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000168 bind([] { BOOST_FAIL("Unexpected Data"); }),
169 bind([] { BOOST_FAIL("Unexpected Nack"); }),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800170 [&nTimeouts] (const Interest& i) {
171 BOOST_CHECK_EQUAL(i.getName(), "/Hello/World");
172 ++nTimeouts;
173 });
Eric Newberry83872fd2015-08-06 17:01:24 -0700174
Davide Pesavento0f830802018-01-16 23:58:58 -0500175 advanceClocks(200_ms, 5);
Eric Newberry83872fd2015-08-06 17:01:24 -0700176
177 BOOST_CHECK_EQUAL(nTimeouts, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800178 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
179 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
180 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0);
Eric Newberry83872fd2015-08-06 17:01:24 -0700181}
182
Davide Pesaventod247d492020-01-28 21:30:20 -0500183BOOST_AUTO_TEST_CASE(EmptyTimeoutCallback)
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000184{
Junxiao Shib55e5d32018-07-18 13:32:00 -0600185 face.expressInterest(*makeInterest("/Hello/World", false, 50_ms),
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000186 bind([] { BOOST_FAIL("Unexpected Data"); }),
187 bind([] { BOOST_FAIL("Unexpected Nack"); }),
188 nullptr);
Davide Pesavento0f830802018-01-16 23:58:58 -0500189 advanceClocks(40_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000190
191 BOOST_CHECK_NO_THROW(do {
Davide Pesavento0f830802018-01-16 23:58:58 -0500192 advanceClocks(6_ms, 2);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000193 } while (false));
194}
195
Davide Pesaventod247d492020-01-28 21:30:20 -0500196BOOST_AUTO_TEST_CASE(ReplyNack)
Eric Newberry83872fd2015-08-06 17:01:24 -0700197{
198 size_t nNacks = 0;
199
Junxiao Shib55e5d32018-07-18 13:32:00 -0600200 auto interest = makeInterest("/Hello/World", false, 50_ms);
Eric Newberry83872fd2015-08-06 17:01:24 -0700201
Junxiao Shib55e5d32018-07-18 13:32:00 -0600202 face.expressInterest(*interest,
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000203 bind([] { BOOST_FAIL("Unexpected Data"); }),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800204 [&] (const Interest& i, const lp::Nack& n) {
205 BOOST_CHECK(i.getName().isPrefixOf(n.getInterest().getName()));
206 BOOST_CHECK_EQUAL(i.getName(), "/Hello/World");
207 BOOST_CHECK_EQUAL(n.getReason(), lp::NackReason::DUPLICATE);
208 ++nNacks;
209 },
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000210 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Eric Newberry83872fd2015-08-06 17:01:24 -0700211
Davide Pesavento0f830802018-01-16 23:58:58 -0500212 advanceClocks(40_ms);
Eric Newberry83872fd2015-08-06 17:01:24 -0700213
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000214 face.receive(makeNack(face.sentInterests.at(0), lp::NackReason::DUPLICATE));
Eric Newberry83872fd2015-08-06 17:01:24 -0700215
Davide Pesavento0f830802018-01-16 23:58:58 -0500216 advanceClocks(50_ms, 2);
Eric Newberry83872fd2015-08-06 17:01:24 -0700217
218 BOOST_CHECK_EQUAL(nNacks, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800219 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
Eric Newberry83872fd2015-08-06 17:01:24 -0700220}
221
Davide Pesaventod247d492020-01-28 21:30:20 -0500222BOOST_AUTO_TEST_CASE(MultipleNacks)
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800223{
224 size_t nNacks = 0;
225
Junxiao Shib55e5d32018-07-18 13:32:00 -0600226 auto interest = makeInterest("/Hello/World", false, 50_ms, 1);
227 face.expressInterest(*interest,
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800228 bind([] { BOOST_FAIL("Unexpected Data"); }),
Davide Pesaventod247d492020-01-28 21:30:20 -0500229 [&] (const auto&, const auto&) { ++nNacks; },
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800230 bind([] { BOOST_FAIL("Unexpected timeout"); }));
231
Junxiao Shib55e5d32018-07-18 13:32:00 -0600232 interest->setNonce(2);
233 face.expressInterest(*interest,
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800234 bind([] { BOOST_FAIL("Unexpected Data"); }),
Davide Pesaventod247d492020-01-28 21:30:20 -0500235 [&] (const auto&, const auto&) { ++nNacks; },
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800236 bind([] { BOOST_FAIL("Unexpected timeout"); }));
237
Davide Pesavento0f830802018-01-16 23:58:58 -0500238 advanceClocks(40_ms);
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800239
240 face.receive(makeNack(face.sentInterests.at(1), lp::NackReason::DUPLICATE));
241
Davide Pesavento0f830802018-01-16 23:58:58 -0500242 advanceClocks(50_ms, 2);
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800243
244 BOOST_CHECK_EQUAL(nNacks, 2);
245 BOOST_CHECK_EQUAL(face.sentInterests.size(), 2);
246}
247
Davide Pesaventod247d492020-01-28 21:30:20 -0500248BOOST_AUTO_TEST_CASE(EmptyNackCallback)
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000249{
Junxiao Shib55e5d32018-07-18 13:32:00 -0600250 face.expressInterest(*makeInterest("/Hello/World"),
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000251 bind([] { BOOST_FAIL("Unexpected Data"); }),
252 nullptr,
253 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500254 advanceClocks(1_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000255
256 BOOST_CHECK_NO_THROW(do {
257 face.receive(makeNack(face.sentInterests.at(0), lp::NackReason::DUPLICATE));
Davide Pesavento0f830802018-01-16 23:58:58 -0500258 advanceClocks(1_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000259 } while (false));
260}
261
Davide Pesaventod247d492020-01-28 21:30:20 -0500262BOOST_AUTO_TEST_CASE(PutDataFromDataCallback) // Bug 4596
263{
264 face.expressInterest(*makeInterest("/localhost/notification/1"),
265 [&] (const auto&, const auto&) {
266 face.put(*makeData("/chronosync/sampleDigest/1"));
267 }, nullptr, nullptr);
268 advanceClocks(10_ms);
269 BOOST_CHECK_EQUAL(face.sentInterests.back().getName(), "/localhost/notification/1");
270
271 face.receive(*makeInterest("/chronosync/sampleDigest", true));
272 advanceClocks(10_ms);
273
274 face.put(*makeData("/localhost/notification/1"));
275 advanceClocks(10_ms);
276 BOOST_CHECK_EQUAL(face.sentData.back().getName(), "/chronosync/sampleDigest/1");
277}
278
279BOOST_AUTO_TEST_CASE(DestroyWithPendingInterest)
280{
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500281 auto face2 = make_unique<DummyClientFace>(m_io, m_keyChain);
Davide Pesaventod247d492020-01-28 21:30:20 -0500282 face2->expressInterest(*makeInterest("/Hello/World", false, 50_ms),
283 nullptr, nullptr, nullptr);
284 advanceClocks(50_ms, 2);
285 face2.reset();
286
287 advanceClocks(50_ms, 2); // should not crash - Bug 2518
288
289 // avoid "test case [...] did not check any assertions" message from Boost.Test
290 BOOST_CHECK(true);
291}
292
293BOOST_AUTO_TEST_CASE(Handle)
Junxiao Shi80609d42019-01-29 18:15:22 +0000294{
295 auto hdl = face.expressInterest(*makeInterest("/Hello/World", true, 50_ms),
296 bind([] { BOOST_FAIL("Unexpected data"); }),
297 bind([] { BOOST_FAIL("Unexpected nack"); }),
298 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Davide Pesaventod247d492020-01-28 21:30:20 -0500299 advanceClocks(1_ms);
Junxiao Shi80609d42019-01-29 18:15:22 +0000300 hdl.cancel();
Davide Pesaventod247d492020-01-28 21:30:20 -0500301 advanceClocks(1_ms);
Junxiao Shi80609d42019-01-29 18:15:22 +0000302 face.receive(*makeData("/Hello/World/%21"));
303 advanceClocks(200_ms, 5);
304
Davide Pesaventod247d492020-01-28 21:30:20 -0500305 // cancel after destructing face
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500306 auto face2 = make_unique<DummyClientFace>(m_io, m_keyChain);
Davide Pesaventod247d492020-01-28 21:30:20 -0500307 auto hdl2 = face2->expressInterest(*makeInterest("/Hello/World", true, 50_ms),
308 bind([] { BOOST_FAIL("Unexpected data"); }),
309 bind([] { BOOST_FAIL("Unexpected nack"); }),
310 bind([] { BOOST_FAIL("Unexpected timeout"); }));
311 advanceClocks(1_ms);
312 face2.reset();
313 advanceClocks(1_ms);
314 hdl2.cancel(); // should not crash
315 advanceClocks(1_ms);
316
Junxiao Shi80609d42019-01-29 18:15:22 +0000317 // avoid "test case [...] did not check any assertions" message from Boost.Test
318 BOOST_CHECK(true);
319}
320
Davide Pesaventod247d492020-01-28 21:30:20 -0500321BOOST_AUTO_TEST_SUITE_END() // ExpressInterest
322
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000323BOOST_AUTO_TEST_CASE(RemoveAllPendingInterests)
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500324{
Junxiao Shib55e5d32018-07-18 13:32:00 -0600325 face.expressInterest(*makeInterest("/Hello/World/0", false, 50_ms),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800326 bind([] { BOOST_FAIL("Unexpected data"); }),
327 bind([] { BOOST_FAIL("Unexpected nack"); }),
328 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500329
Junxiao Shib55e5d32018-07-18 13:32:00 -0600330 face.expressInterest(*makeInterest("/Hello/World/1", false, 50_ms),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800331 bind([] { BOOST_FAIL("Unexpected data"); }),
332 bind([] { BOOST_FAIL("Unexpected nack"); }),
333 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500334
Davide Pesavento0f830802018-01-16 23:58:58 -0500335 advanceClocks(10_ms);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500336
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800337 face.removeAllPendingInterests();
Davide Pesavento0f830802018-01-16 23:58:58 -0500338 advanceClocks(10_ms);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500339
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800340 BOOST_CHECK_EQUAL(face.getNPendingInterests(), 0);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500341
Junxiao Shi85d90832016-08-04 03:19:46 +0000342 face.receive(*makeData("/Hello/World/0"));
343 face.receive(*makeData("/Hello/World/1"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500344 advanceClocks(200_ms, 5);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500345}
346
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000347BOOST_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
Davide Pesavento720e25c2019-07-14 01:33:52 -0400376 face.setInterestFilter("/", [&] (const InterestFilter&, const Interest&) {
Junxiao Shib6828912017-11-20 14:06:32 +0000377 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
Davide Pesavento287a7fa2020-10-01 22:46:43 -0400428 face.put(makeNack(*makeInterest("/unsolicited", false, nullopt, 18645250),
Junxiao Shib55e5d32018-07-18 13:32:00 -0600429 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
Davide Pesavento287a7fa2020-10-01 22:46:43 -0400433 auto interest1 = makeInterest("/Hello/World", false, nullopt, 14247162);
Junxiao Shib55e5d32018-07-18 13:32:00 -0600434 face.receive(*interest1);
Davide Pesavento287a7fa2020-10-01 22:46:43 -0400435 auto interest2 = makeInterest("/another/prefix", false, nullopt, 92203002);
Junxiao Shib55e5d32018-07-18 13:32:00 -0600436 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
Davide Pesavento287a7fa2020-10-01 22:46:43 -0400466 auto interest = makeInterest("/A", false, nullopt, 14333271);
Junxiao Shib55e5d32018-07-18 13:32:00 -0600467 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
Davide Pesavento287a7fa2020-10-01 22:46:43 -0400497 auto interest = makeInterest("/A", false, nullopt, 28395852);
Junxiao Shib55e5d32018-07-18 13:32:00 -0600498 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
Davide Pesaventod247d492020-01-28 21:30:20 -0500515BOOST_AUTO_TEST_SUITE_END() // Producer
516
517BOOST_AUTO_TEST_SUITE(RegisterPrefix)
518
519BOOST_FIXTURE_TEST_CASE(Failure, FaceFixture<NoPrefixRegReply>)
520{
521 BOOST_CHECK(!runPrefixReg([&] (const auto& success, const auto& failure) {
522 face.registerPrefix("/Hello/World", success, failure);
523 this->advanceClocks(5_s, 20); // wait for command timeout
524 }));
525}
526
527BOOST_AUTO_TEST_CASE(Handle)
528{
529 RegisteredPrefixHandle hdl;
530 auto doReg = [&] {
531 return runPrefixReg([&] (const auto& success, const auto& failure) {
532 hdl = face.registerPrefix("/Hello/World", success, failure);
533 });
534 };
535 auto doUnreg = [&] {
536 return runPrefixUnreg([&] (const auto& success, const auto& failure) {
537 hdl.unregister(success, failure);
538 });
539 };
540
541 // despite the "undefined behavior" warning, we try not to crash, but no API guarantee for this
542 BOOST_CHECK(!doUnreg());
543
544 // cancel after unregister
545 BOOST_CHECK(doReg());
546 BOOST_CHECK(doUnreg());
547 hdl.cancel();
548 advanceClocks(1_ms);
549
550 // unregister after cancel
551 BOOST_CHECK(doReg());
552 hdl.cancel();
553 advanceClocks(1_ms);
554 BOOST_CHECK(!doUnreg());
555
556 // cancel after destructing face
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500557 auto face2 = make_unique<DummyClientFace>(m_io, m_keyChain);
Davide Pesaventod247d492020-01-28 21:30:20 -0500558 hdl = face2->registerPrefix("/Hello/World/2", nullptr,
559 bind([] { BOOST_FAIL("Unexpected registerPrefix failure"); }));
560 advanceClocks(1_ms);
561 face2.reset();
562 advanceClocks(1_ms);
563 hdl.cancel(); // should not crash
564 advanceClocks(1_ms);
565
566 // unregister after destructing face
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500567 auto face3 = make_unique<DummyClientFace>(m_io, m_keyChain);
Davide Pesaventod247d492020-01-28 21:30:20 -0500568 hdl = face3->registerPrefix("/Hello/World/3", nullptr,
569 bind([] { BOOST_FAIL("Unexpected registerPrefix failure"); }));
570 advanceClocks(1_ms);
571 face3.reset();
572 advanceClocks(1_ms);
573 BOOST_CHECK(!doUnreg());
574}
575
576BOOST_AUTO_TEST_SUITE_END() // RegisterPrefix
577
578BOOST_AUTO_TEST_SUITE(SetInterestFilter)
579
580BOOST_AUTO_TEST_CASE(SetAndCancel)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400581{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800582 size_t nInterests = 0;
583 size_t nRegs = 0;
Davide Pesavento720e25c2019-07-14 01:33:52 -0400584 auto hdl = face.setInterestFilter("/Hello/World",
585 bind([&nInterests] { ++nInterests; }),
586 bind([&nRegs] { ++nRegs; }),
587 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500588 advanceClocks(25_ms, 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800589 BOOST_CHECK_EQUAL(nRegs, 1);
590 BOOST_CHECK_EQUAL(nInterests, 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400591
Junxiao Shib55e5d32018-07-18 13:32:00 -0600592 face.receive(*makeInterest("/Hello/World/%21"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500593 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400594
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800595 BOOST_CHECK_EQUAL(nRegs, 1);
596 BOOST_CHECK_EQUAL(nInterests, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400597
Junxiao Shib55e5d32018-07-18 13:32:00 -0600598 face.receive(*makeInterest("/Bye/World/%21"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500599 advanceClocks(10000_ms, 10);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800600 BOOST_CHECK_EQUAL(nInterests, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400601
Junxiao Shib55e5d32018-07-18 13:32:00 -0600602 face.receive(*makeInterest("/Hello/World/%21/2"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500603 advanceClocks(25_ms, 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800604 BOOST_CHECK_EQUAL(nInterests, 2);
605
606 // removing filter
Davide Pesavento720e25c2019-07-14 01:33:52 -0400607 hdl.cancel();
Davide Pesavento0f830802018-01-16 23:58:58 -0500608 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400609
Junxiao Shib55e5d32018-07-18 13:32:00 -0600610 face.receive(*makeInterest("/Hello/World/%21/3"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800611 BOOST_CHECK_EQUAL(nInterests, 2);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800612}
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400613
Davide Pesaventod247d492020-01-28 21:30:20 -0500614BOOST_AUTO_TEST_CASE(EmptyInterestCallback)
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000615{
616 face.setInterestFilter("/A", nullptr);
Davide Pesavento0f830802018-01-16 23:58:58 -0500617 advanceClocks(1_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000618
619 BOOST_CHECK_NO_THROW(do {
620 face.receive(*makeInterest("/A/1"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500621 advanceClocks(1_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000622 } while (false));
623}
624
Davide Pesaventod247d492020-01-28 21:30:20 -0500625BOOST_AUTO_TEST_CASE(WithoutSuccessCallback)
Joao Pereira0b3cac52015-07-02 14:49:49 -0400626{
627 size_t nInterests = 0;
Davide Pesavento720e25c2019-07-14 01:33:52 -0400628 auto hdl = face.setInterestFilter("/Hello/World",
629 bind([&nInterests] { ++nInterests; }),
630 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500631 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400632 BOOST_CHECK_EQUAL(nInterests, 0);
633
Junxiao Shib55e5d32018-07-18 13:32:00 -0600634 face.receive(*makeInterest("/Hello/World/%21"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500635 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400636
637 BOOST_CHECK_EQUAL(nInterests, 1);
638
Junxiao Shib55e5d32018-07-18 13:32:00 -0600639 face.receive(*makeInterest("/Bye/World/%21"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500640 advanceClocks(10000_ms, 10);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400641 BOOST_CHECK_EQUAL(nInterests, 1);
642
Junxiao Shib55e5d32018-07-18 13:32:00 -0600643 face.receive(*makeInterest("/Hello/World/%21/2"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500644 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400645 BOOST_CHECK_EQUAL(nInterests, 2);
646
647 // removing filter
Davide Pesavento720e25c2019-07-14 01:33:52 -0400648 hdl.cancel();
Davide Pesavento0f830802018-01-16 23:58:58 -0500649 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400650
Junxiao Shib55e5d32018-07-18 13:32:00 -0600651 face.receive(*makeInterest("/Hello/World/%21/3"));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400652 BOOST_CHECK_EQUAL(nInterests, 2);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400653}
654
Davide Pesaventod247d492020-01-28 21:30:20 -0500655BOOST_FIXTURE_TEST_CASE(Failure, FaceFixture<NoPrefixRegReply>)
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800656{
657 // don't enable registration reply
658 size_t nRegFailed = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800659 face.setInterestFilter("/Hello/World",
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000660 bind([] { BOOST_FAIL("Unexpected Interest"); }),
661 bind([] { BOOST_FAIL("Unexpected success of setInterestFilter"); }),
662 bind([&nRegFailed] { ++nRegFailed; }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800663
Davide Pesavento0f830802018-01-16 23:58:58 -0500664 advanceClocks(25_ms, 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800665 BOOST_CHECK_EQUAL(nRegFailed, 0);
666
Davide Pesavento0f830802018-01-16 23:58:58 -0500667 advanceClocks(2000_ms, 5);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800668 BOOST_CHECK_EQUAL(nRegFailed, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400669}
670
Davide Pesaventod247d492020-01-28 21:30:20 -0500671BOOST_FIXTURE_TEST_CASE(FailureWithoutSuccessCallback, FaceFixture<NoPrefixRegReply>)
Joao Pereira0b3cac52015-07-02 14:49:49 -0400672{
673 // don't enable registration reply
674 size_t nRegFailed = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800675 face.setInterestFilter("/Hello/World",
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000676 bind([] { BOOST_FAIL("Unexpected Interest"); }),
677 bind([&nRegFailed] { ++nRegFailed; }));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400678
Davide Pesavento0f830802018-01-16 23:58:58 -0500679 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400680 BOOST_CHECK_EQUAL(nRegFailed, 0);
681
Davide Pesavento0f830802018-01-16 23:58:58 -0500682 advanceClocks(2000_ms, 5);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400683 BOOST_CHECK_EQUAL(nRegFailed, 1);
684}
685
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800686BOOST_AUTO_TEST_CASE(SimilarFilters)
687{
688 size_t nInInterests1 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800689 face.setInterestFilter("/Hello/World",
690 bind([&nInInterests1] { ++nInInterests1; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000691 nullptr,
692 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800693
694 size_t nInInterests2 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800695 face.setInterestFilter("/Hello",
696 bind([&nInInterests2] { ++nInInterests2; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000697 nullptr,
698 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400699
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800700 size_t nInInterests3 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800701 face.setInterestFilter("/Los/Angeles/Lakers",
702 bind([&nInInterests3] { ++nInInterests3; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000703 nullptr,
704 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400705
Davide Pesavento0f830802018-01-16 23:58:58 -0500706 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400707
Junxiao Shib55e5d32018-07-18 13:32:00 -0600708 face.receive(*makeInterest("/Hello/World/%21"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500709 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400710
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800711 BOOST_CHECK_EQUAL(nInInterests1, 1);
712 BOOST_CHECK_EQUAL(nInInterests2, 1);
713 BOOST_CHECK_EQUAL(nInInterests3, 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400714}
715
Davide Pesaventod247d492020-01-28 21:30:20 -0500716BOOST_AUTO_TEST_CASE(RegexFilter)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400717{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800718 size_t nInInterests = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800719 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
720 bind([&nInInterests] { ++nInInterests; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000721 nullptr,
722 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400723
Davide Pesavento0f830802018-01-16 23:58:58 -0500724 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400725
Junxiao Shib55e5d32018-07-18 13:32:00 -0600726 face.receive(*makeInterest("/Hello/World/a")); // shouldn't match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400727 BOOST_CHECK_EQUAL(nInInterests, 0);
728
Junxiao Shib55e5d32018-07-18 13:32:00 -0600729 face.receive(*makeInterest("/Hello/World/a/b")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400730 BOOST_CHECK_EQUAL(nInInterests, 1);
731
Junxiao Shib55e5d32018-07-18 13:32:00 -0600732 face.receive(*makeInterest("/Hello/World/a/b/c")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400733 BOOST_CHECK_EQUAL(nInInterests, 2);
734
Junxiao Shib55e5d32018-07-18 13:32:00 -0600735 face.receive(*makeInterest("/Hello/World/a/b/d")); // should not match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400736 BOOST_CHECK_EQUAL(nInInterests, 2);
737}
738
Davide Pesaventod247d492020-01-28 21:30:20 -0500739BOOST_AUTO_TEST_CASE(RegexFilterError)
740{
741 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
742 [] (const Name&, const Interest&) {
743 BOOST_FAIL("InterestFilter::Error should have been triggered");
744 },
745 nullptr,
746 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
747
748 advanceClocks(25_ms, 4);
749
750 BOOST_CHECK_THROW(face.receive(*makeInterest("/Hello/World/XXX/b/c")), InterestFilter::Error);
751}
752
753BOOST_AUTO_TEST_CASE(RegexFilterAndRegisterPrefix)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400754{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800755 size_t nInInterests = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800756 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
757 bind([&nInInterests] { ++nInInterests; }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400758
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800759 size_t nRegSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800760 face.registerPrefix("/Hello/World",
761 bind([&nRegSuccesses] { ++nRegSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000762 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400763
Davide Pesavento0f830802018-01-16 23:58:58 -0500764 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400765 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
766
Junxiao Shib55e5d32018-07-18 13:32:00 -0600767 face.receive(*makeInterest("/Hello/World/a")); // shouldn't match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400768 BOOST_CHECK_EQUAL(nInInterests, 0);
769
Junxiao Shib55e5d32018-07-18 13:32:00 -0600770 face.receive(*makeInterest("/Hello/World/a/b")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400771 BOOST_CHECK_EQUAL(nInInterests, 1);
772
Junxiao Shib55e5d32018-07-18 13:32:00 -0600773 face.receive(*makeInterest("/Hello/World/a/b/c")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400774 BOOST_CHECK_EQUAL(nInInterests, 2);
775
Junxiao Shib55e5d32018-07-18 13:32:00 -0600776 face.receive(*makeInterest("/Hello/World/a/b/d")); // should not match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400777 BOOST_CHECK_EQUAL(nInInterests, 2);
778}
779
Davide Pesaventod247d492020-01-28 21:30:20 -0500780BOOST_FIXTURE_TEST_CASE(WithoutRegisterPrefix, FaceFixture<NoPrefixRegReply>) // Bug 2318
Junxiao Shia1ea5062014-12-27 22:33:39 -0700781{
782 // This behavior is specific to DummyClientFace.
783 // Regular Face won't accept incoming packets until something is sent.
784
785 int hit = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800786 face.setInterestFilter(Name("/"), bind([&hit] { ++hit; }));
787 face.processEvents(time::milliseconds(-1));
Junxiao Shia1ea5062014-12-27 22:33:39 -0700788
Junxiao Shib55e5d32018-07-18 13:32:00 -0600789 face.receive(*makeInterest("/A"));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800790 face.processEvents(time::milliseconds(-1));
Junxiao Shia1ea5062014-12-27 22:33:39 -0700791
792 BOOST_CHECK_EQUAL(hit, 1);
793}
794
Davide Pesaventod247d492020-01-28 21:30:20 -0500795BOOST_AUTO_TEST_CASE(Handle)
Junxiao Shi60aaef02019-01-14 04:59:30 +0000796{
797 int hit = 0;
Davide Pesaventod247d492020-01-28 21:30:20 -0500798 InterestFilterHandle hdl = face.setInterestFilter(Name("/"), bind([&hit] { ++hit; }));
Junxiao Shi60aaef02019-01-14 04:59:30 +0000799 face.processEvents(-1_ms);
800
801 face.receive(*makeInterest("/A"));
802 face.processEvents(-1_ms);
803 BOOST_CHECK_EQUAL(hit, 1);
804
805 hdl.cancel();
806 face.processEvents(-1_ms);
807
808 face.receive(*makeInterest("/B"));
809 face.processEvents(-1_ms);
810 BOOST_CHECK_EQUAL(hit, 1);
Davide Pesaventod247d492020-01-28 21:30:20 -0500811
812 // cancel after destructing face
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500813 auto face2 = make_unique<DummyClientFace>(m_io, m_keyChain);
Davide Pesaventod247d492020-01-28 21:30:20 -0500814 InterestFilterHandle hdl2 = face2->setInterestFilter("/Hello/World/2", nullptr);
815 advanceClocks(1_ms);
816 face2.reset();
817 advanceClocks(1_ms);
818 hdl2.cancel(); // should not crash
819 advanceClocks(1_ms);
Junxiao Shi60aaef02019-01-14 04:59:30 +0000820}
821
Davide Pesaventod247d492020-01-28 21:30:20 -0500822BOOST_AUTO_TEST_SUITE_END() // SetInterestFilter
Junxiao Shiae0b4182016-08-08 22:53:17 +0000823
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500824BOOST_AUTO_TEST_CASE(ProcessEvents)
825{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800826 face.processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500827
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800828 size_t nRegSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800829 face.registerPrefix("/Hello/World",
830 bind([&nRegSuccesses] { ++nRegSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000831 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500832
833 // io_service::poll() without reset
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800834 face.getIoService().poll();
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500835 BOOST_CHECK_EQUAL(nRegSuccesses, 0);
836
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800837 face.processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500838 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
839}
840
Junxiao Shiae0b4182016-08-08 22:53:17 +0000841BOOST_AUTO_TEST_CASE(DestroyWithoutProcessEvents) // Bug 3248
842{
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500843 auto face2 = make_unique<Face>(m_io);
Junxiao Shiae0b4182016-08-08 22:53:17 +0000844 face2.reset();
845
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500846 m_io.poll(); // should not crash
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100847
848 // avoid "test case [...] did not check any assertions" message from Boost.Test
849 BOOST_CHECK(true);
Junxiao Shiae0b4182016-08-08 22:53:17 +0000850}
851
Junxiao Shiae0b4182016-08-08 22:53:17 +0000852BOOST_AUTO_TEST_SUITE(Transport)
853
854using ndn::Transport;
855
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700856struct PibDirWithDefaultTpm
857{
858 const std::string PATH = "build/keys-with-default-tpm";
859};
860
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500861BOOST_FIXTURE_TEST_CASE(FaceTransport, IoKeyChainFixture)
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800862{
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800863 BOOST_CHECK(Face().getTransport() != nullptr);
864
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800865 BOOST_CHECK(Face(shared_ptr<Transport>()).getTransport() != nullptr);
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500866 BOOST_CHECK(Face(shared_ptr<Transport>(), m_io).getTransport() != nullptr);
867 BOOST_CHECK(Face(shared_ptr<Transport>(), m_io, m_keyChain).getTransport() != nullptr);
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800868
869 auto transport = make_shared<TcpTransport>("localhost", "6363"); // no real io operations will be scheduled
870 BOOST_CHECK(Face(transport).getTransport() == transport);
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500871 BOOST_CHECK(Face(transport, m_io).getTransport() == transport);
872 BOOST_CHECK(Face(transport, m_io, m_keyChain).getTransport() == transport);
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800873}
874
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500875class WithEnv
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700876{
877public:
878 WithEnv()
879 {
880 if (getenv("NDN_CLIENT_TRANSPORT") != nullptr) {
881 m_oldTransport = getenv("NDN_CLIENT_TRANSPORT");
882 unsetenv("NDN_CLIENT_TRANSPORT");
883 }
884 }
885
886 void
887 configure(const std::string& faceUri)
888 {
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500889 setenv("NDN_CLIENT_TRANSPORT", faceUri.data(), true);
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700890 }
891
892 ~WithEnv()
893 {
894 if (!m_oldTransport.empty()) {
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500895 setenv("NDN_CLIENT_TRANSPORT", m_oldTransport.data(), true);
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700896 }
897 else {
898 unsetenv("NDN_CLIENT_TRANSPORT");
899 }
900 }
901
902private:
903 std::string m_oldTransport;
904};
905
906class WithConfig : private TestHomeFixture<DefaultPibDir>
907{
908public:
909 void
910 configure(const std::string& faceUri)
911 {
912 createClientConf({"transport=" + faceUri});
913 }
914};
915
916class WithEnvAndConfig : public WithEnv, public WithConfig
917{
918};
919
920typedef boost::mpl::vector<WithEnv, WithConfig> ConfigOptions;
921
922BOOST_FIXTURE_TEST_CASE(NoConfig, WithEnvAndConfig) // fixture configures test HOME and PIB/TPM path
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700923{
924 shared_ptr<Face> face;
925 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
926 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700927}
928
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700929BOOST_FIXTURE_TEST_CASE_TEMPLATE(Unix, T, ConfigOptions, T)
930{
931 this->configure("unix://some/path");
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700932
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700933 shared_ptr<Face> face;
934 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
935 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
936}
937
938BOOST_FIXTURE_TEST_CASE_TEMPLATE(Tcp, T, ConfigOptions, T)
939{
940 this->configure("tcp://127.0.0.1:6000");
941
942 shared_ptr<Face> face;
943 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
944 BOOST_CHECK(dynamic_pointer_cast<TcpTransport>(face->getTransport()) != nullptr);
945}
946
947BOOST_FIXTURE_TEST_CASE_TEMPLATE(WrongTransport, T, ConfigOptions, T)
948{
949 this->configure("wrong-transport:");
950
951 BOOST_CHECK_THROW(make_shared<Face>(), ConfigFile::Error);
952}
953
954BOOST_FIXTURE_TEST_CASE_TEMPLATE(WrongUri, T, ConfigOptions, T)
955{
956 this->configure("wrong-uri");
957
958 BOOST_CHECK_THROW(make_shared<Face>(), ConfigFile::Error);
959}
960
961BOOST_FIXTURE_TEST_CASE(EnvOverride, WithEnvAndConfig)
962{
963 this->WithEnv::configure("tcp://127.0.0.1:6000");
964 this->WithConfig::configure("unix://some/path");
965
966 shared_ptr<Face> face;
967 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
968 BOOST_CHECK(dynamic_pointer_cast<TcpTransport>(face->getTransport()) != nullptr);
969}
970
971BOOST_FIXTURE_TEST_CASE(ExplicitTransport, WithEnvAndConfig)
972{
973 this->WithEnv::configure("wrong-uri");
974 this->WithConfig::configure("wrong-transport:");
975
976 auto transport = make_shared<UnixTransport>("unix://some/path");
977 shared_ptr<Face> face;
978 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>(transport));
979 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
980}
981
Junxiao Shiae0b4182016-08-08 22:53:17 +0000982BOOST_AUTO_TEST_SUITE_END() // Transport
983
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700984BOOST_AUTO_TEST_SUITE_END() // TestFace
985
986} // namespace tests
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400987} // namespace ndn