blob: 0ee95b4c4e3c0724ed3c23c00def0b3e6de18ec8 [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 Pesaventof6b45892023-03-13 15:00:51 -04003 * Copyright (c) 2013-2023 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"
Davide Pesaventof135f152022-01-06 20:43:06 -050026#include "ndn-cxx/util/config-file.hpp"
Davide Pesavento7e780642018-11-24 15:51:34 -050027#include "ndn-cxx/util/dummy-client-face.hpp"
28#include "ndn-cxx/util/scheduler.hpp"
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040029
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050030#include "tests/test-common.hpp"
31#include "tests/unit/io-key-chain-fixture.hpp"
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040032
Junxiao Shiec475a72019-01-13 21:53:55 +000033#include <boost/logic/tribool.hpp>
34
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040035namespace ndn {
36namespace tests {
37
Junxiao Shia60d9362014-11-12 09:38:21 -070038using ndn::util::DummyClientFace;
Davide Pesavento2e481fc2021-07-02 18:20:03 -040039using std::bind;
Junxiao Shia60d9362014-11-12 09:38:21 -070040
Junxiao Shiec475a72019-01-13 21:53:55 +000041struct WantPrefixRegReply;
42struct NoPrefixRegReply;
43
44template<typename PrefixRegReply = WantPrefixRegReply>
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050045class FaceFixture : public IoKeyChainFixture
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040046{
Davide Pesaventod247d492020-01-28 21:30:20 -050047protected:
Junxiao Shiec475a72019-01-13 21:53:55 +000048 FaceFixture()
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050049 : face(m_io, m_keyChain, {true, !std::is_same<PrefixRegReply, NoPrefixRegReply>::value})
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040050 {
Junxiao Shiec475a72019-01-13 21:53:55 +000051 static_assert(std::is_same<PrefixRegReply, WantPrefixRegReply>::value ||
52 std::is_same<PrefixRegReply, NoPrefixRegReply>::value, "");
53 }
54
55 /** \brief Execute a prefix registration, and optionally check the name in callback.
56 * \return whether the prefix registration succeeded.
57 */
58 bool
59 runPrefixReg(function<void(const RegisterPrefixSuccessCallback& success,
60 const RegisterPrefixFailureCallback& failure)> f)
61 {
62 boost::logic::tribool result = boost::logic::indeterminate;
Davide Pesaventod247d492020-01-28 21:30:20 -050063 f([&] (auto) { result = true; },
64 [&] (auto, auto) { result = false; });
Junxiao Shiec475a72019-01-13 21:53:55 +000065
66 advanceClocks(1_ms);
67 BOOST_REQUIRE(!boost::logic::indeterminate(result));
68 return static_cast<bool>(result);
69 }
70
71 /** \brief Execute a prefix unregistration, and optionally check the name in callback.
72 * \return whether the prefix unregistration succeeded.
73 */
74 bool
75 runPrefixUnreg(function<void(const UnregisterPrefixSuccessCallback& success,
76 const UnregisterPrefixFailureCallback& failure)> f)
77 {
78 boost::logic::tribool result = boost::logic::indeterminate;
Davide Pesaventod247d492020-01-28 21:30:20 -050079 f([&] { result = true; },
80 [&] (auto) { result = false; });
Junxiao Shiec475a72019-01-13 21:53:55 +000081
82 advanceClocks(1_ms);
83 BOOST_REQUIRE(!boost::logic::indeterminate(result));
84 return static_cast<bool>(result);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040085 }
86
Davide Pesaventod247d492020-01-28 21:30:20 -050087protected:
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080088 DummyClientFace face;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040089};
90
Junxiao Shiec475a72019-01-13 21:53:55 +000091BOOST_FIXTURE_TEST_SUITE(TestFace, FaceFixture<>)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040092
Davide Pesaventod247d492020-01-28 21:30:20 -050093BOOST_AUTO_TEST_SUITE(ExpressInterest)
Junxiao Shi103d8ed2016-08-07 20:34:10 +000094
Davide Pesaventod247d492020-01-28 21:30:20 -050095BOOST_AUTO_TEST_CASE(ReplyData)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040096{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080097 size_t nData = 0;
Junxiao Shib55e5d32018-07-18 13:32:00 -060098 face.expressInterest(*makeInterest("/Hello/World", true, 50_ms),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080099 [&] (const Interest& i, const Data& d) {
100 BOOST_CHECK(i.getName().isPrefixOf(d.getName()));
101 BOOST_CHECK_EQUAL(i.getName(), "/Hello/World");
102 ++nData;
103 },
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000104 bind([] { BOOST_FAIL("Unexpected Nack"); }),
105 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Eric Newberry83872fd2015-08-06 17:01:24 -0700106
Davide Pesavento0f830802018-01-16 23:58:58 -0500107 advanceClocks(40_ms);
Eric Newberry83872fd2015-08-06 17:01:24 -0700108
Junxiao Shi85d90832016-08-04 03:19:46 +0000109 face.receive(*makeData("/Bye/World/a"));
110 face.receive(*makeData("/Hello/World/a"));
Eric Newberry83872fd2015-08-06 17:01:24 -0700111
Davide Pesavento0f830802018-01-16 23:58:58 -0500112 advanceClocks(50_ms, 2);
Eric Newberry83872fd2015-08-06 17:01:24 -0700113
114 BOOST_CHECK_EQUAL(nData, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800115 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
116 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Eric Newberry83872fd2015-08-06 17:01:24 -0700117
118 size_t nTimeouts = 0;
Junxiao Shib55e5d32018-07-18 13:32:00 -0600119 face.expressInterest(*makeInterest("/Hello/World/a/2", false, 50_ms),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800120 bind([]{}),
121 bind([]{}),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000122 bind([&nTimeouts] { ++nTimeouts; }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500123 advanceClocks(200_ms, 5);
Eric Newberry83872fd2015-08-06 17:01:24 -0700124 BOOST_CHECK_EQUAL(nTimeouts, 1);
125}
126
Davide Pesaventod247d492020-01-28 21:30:20 -0500127BOOST_AUTO_TEST_CASE(MultipleData)
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800128{
129 size_t nData = 0;
130
Junxiao Shib55e5d32018-07-18 13:32:00 -0600131 face.expressInterest(*makeInterest("/Hello/World", true, 50_ms),
Davide Pesaventod247d492020-01-28 21:30:20 -0500132 [&] (const auto&, const auto&) { ++nData; },
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800133 bind([] { BOOST_FAIL("Unexpected Nack"); }),
134 bind([] { BOOST_FAIL("Unexpected timeout"); }));
135
Junxiao Shib55e5d32018-07-18 13:32:00 -0600136 face.expressInterest(*makeInterest("/Hello/World/a", true, 50_ms),
Davide Pesaventod247d492020-01-28 21:30:20 -0500137 [&] (const auto&, const auto&) { ++nData; },
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800138 bind([] { BOOST_FAIL("Unexpected Nack"); }),
139 bind([] { BOOST_FAIL("Unexpected timeout"); }));
140
Davide Pesavento0f830802018-01-16 23:58:58 -0500141 advanceClocks(40_ms);
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800142
143 face.receive(*makeData("/Hello/World/a/b"));
144
Davide Pesavento0f830802018-01-16 23:58:58 -0500145 advanceClocks(50_ms, 2);
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800146
147 BOOST_CHECK_EQUAL(nData, 2);
148 BOOST_CHECK_EQUAL(face.sentInterests.size(), 2);
149 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
150}
151
Davide Pesaventod247d492020-01-28 21:30:20 -0500152BOOST_AUTO_TEST_CASE(EmptyDataCallback)
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000153{
Junxiao Shib55e5d32018-07-18 13:32:00 -0600154 face.expressInterest(*makeInterest("/Hello/World", true),
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000155 nullptr,
156 bind([] { BOOST_FAIL("Unexpected Nack"); }),
157 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500158 advanceClocks(1_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000159
160 BOOST_CHECK_NO_THROW(do {
161 face.receive(*makeData("/Hello/World/a"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500162 advanceClocks(1_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000163 } while (false));
164}
165
Davide Pesaventod247d492020-01-28 21:30:20 -0500166BOOST_AUTO_TEST_CASE(Timeout)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400167{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800168 size_t nTimeouts = 0;
Junxiao Shib55e5d32018-07-18 13:32:00 -0600169 face.expressInterest(*makeInterest("/Hello/World", false, 50_ms),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000170 bind([] { BOOST_FAIL("Unexpected Data"); }),
171 bind([] { BOOST_FAIL("Unexpected Nack"); }),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800172 [&nTimeouts] (const Interest& i) {
173 BOOST_CHECK_EQUAL(i.getName(), "/Hello/World");
174 ++nTimeouts;
175 });
Eric Newberry83872fd2015-08-06 17:01:24 -0700176
Davide Pesavento0f830802018-01-16 23:58:58 -0500177 advanceClocks(200_ms, 5);
Eric Newberry83872fd2015-08-06 17:01:24 -0700178
179 BOOST_CHECK_EQUAL(nTimeouts, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800180 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
181 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
182 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0);
Eric Newberry83872fd2015-08-06 17:01:24 -0700183}
184
Davide Pesaventod247d492020-01-28 21:30:20 -0500185BOOST_AUTO_TEST_CASE(EmptyTimeoutCallback)
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000186{
Junxiao Shib55e5d32018-07-18 13:32:00 -0600187 face.expressInterest(*makeInterest("/Hello/World", false, 50_ms),
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000188 bind([] { BOOST_FAIL("Unexpected Data"); }),
189 bind([] { BOOST_FAIL("Unexpected Nack"); }),
190 nullptr);
Davide Pesavento0f830802018-01-16 23:58:58 -0500191 advanceClocks(40_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000192
193 BOOST_CHECK_NO_THROW(do {
Davide Pesavento0f830802018-01-16 23:58:58 -0500194 advanceClocks(6_ms, 2);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000195 } while (false));
196}
197
Davide Pesaventod247d492020-01-28 21:30:20 -0500198BOOST_AUTO_TEST_CASE(ReplyNack)
Eric Newberry83872fd2015-08-06 17:01:24 -0700199{
200 size_t nNacks = 0;
201
Junxiao Shib55e5d32018-07-18 13:32:00 -0600202 auto interest = makeInterest("/Hello/World", false, 50_ms);
Eric Newberry83872fd2015-08-06 17:01:24 -0700203
Junxiao Shib55e5d32018-07-18 13:32:00 -0600204 face.expressInterest(*interest,
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000205 bind([] { BOOST_FAIL("Unexpected Data"); }),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800206 [&] (const Interest& i, const lp::Nack& n) {
207 BOOST_CHECK(i.getName().isPrefixOf(n.getInterest().getName()));
208 BOOST_CHECK_EQUAL(i.getName(), "/Hello/World");
209 BOOST_CHECK_EQUAL(n.getReason(), lp::NackReason::DUPLICATE);
210 ++nNacks;
211 },
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000212 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Eric Newberry83872fd2015-08-06 17:01:24 -0700213
Davide Pesavento0f830802018-01-16 23:58:58 -0500214 advanceClocks(40_ms);
Eric Newberry83872fd2015-08-06 17:01:24 -0700215
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000216 face.receive(makeNack(face.sentInterests.at(0), lp::NackReason::DUPLICATE));
Eric Newberry83872fd2015-08-06 17:01:24 -0700217
Davide Pesavento0f830802018-01-16 23:58:58 -0500218 advanceClocks(50_ms, 2);
Eric Newberry83872fd2015-08-06 17:01:24 -0700219
220 BOOST_CHECK_EQUAL(nNacks, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800221 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
Eric Newberry83872fd2015-08-06 17:01:24 -0700222}
223
Davide Pesaventod247d492020-01-28 21:30:20 -0500224BOOST_AUTO_TEST_CASE(MultipleNacks)
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800225{
226 size_t nNacks = 0;
227
Junxiao Shib55e5d32018-07-18 13:32:00 -0600228 auto interest = makeInterest("/Hello/World", false, 50_ms, 1);
229 face.expressInterest(*interest,
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800230 bind([] { BOOST_FAIL("Unexpected Data"); }),
Davide Pesaventod247d492020-01-28 21:30:20 -0500231 [&] (const auto&, const auto&) { ++nNacks; },
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800232 bind([] { BOOST_FAIL("Unexpected timeout"); }));
233
Junxiao Shib55e5d32018-07-18 13:32:00 -0600234 interest->setNonce(2);
235 face.expressInterest(*interest,
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800236 bind([] { BOOST_FAIL("Unexpected Data"); }),
Davide Pesaventod247d492020-01-28 21:30:20 -0500237 [&] (const auto&, const auto&) { ++nNacks; },
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800238 bind([] { BOOST_FAIL("Unexpected timeout"); }));
239
Davide Pesavento0f830802018-01-16 23:58:58 -0500240 advanceClocks(40_ms);
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800241
242 face.receive(makeNack(face.sentInterests.at(1), lp::NackReason::DUPLICATE));
243
Davide Pesavento0f830802018-01-16 23:58:58 -0500244 advanceClocks(50_ms, 2);
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800245
246 BOOST_CHECK_EQUAL(nNacks, 2);
247 BOOST_CHECK_EQUAL(face.sentInterests.size(), 2);
248}
249
Davide Pesaventod247d492020-01-28 21:30:20 -0500250BOOST_AUTO_TEST_CASE(EmptyNackCallback)
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000251{
Junxiao Shib55e5d32018-07-18 13:32:00 -0600252 face.expressInterest(*makeInterest("/Hello/World"),
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000253 bind([] { BOOST_FAIL("Unexpected Data"); }),
254 nullptr,
255 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500256 advanceClocks(1_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000257
258 BOOST_CHECK_NO_THROW(do {
259 face.receive(makeNack(face.sentInterests.at(0), lp::NackReason::DUPLICATE));
Davide Pesavento0f830802018-01-16 23:58:58 -0500260 advanceClocks(1_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000261 } while (false));
262}
263
Davide Pesaventod247d492020-01-28 21:30:20 -0500264BOOST_AUTO_TEST_CASE(PutDataFromDataCallback) // Bug 4596
265{
266 face.expressInterest(*makeInterest("/localhost/notification/1"),
267 [&] (const auto&, const auto&) {
268 face.put(*makeData("/chronosync/sampleDigest/1"));
269 }, nullptr, nullptr);
270 advanceClocks(10_ms);
271 BOOST_CHECK_EQUAL(face.sentInterests.back().getName(), "/localhost/notification/1");
272
273 face.receive(*makeInterest("/chronosync/sampleDigest", true));
274 advanceClocks(10_ms);
275
276 face.put(*makeData("/localhost/notification/1"));
277 advanceClocks(10_ms);
278 BOOST_CHECK_EQUAL(face.sentData.back().getName(), "/chronosync/sampleDigest/1");
279}
280
281BOOST_AUTO_TEST_CASE(DestroyWithPendingInterest)
282{
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500283 auto face2 = make_unique<DummyClientFace>(m_io, m_keyChain);
Davide Pesaventod247d492020-01-28 21:30:20 -0500284 face2->expressInterest(*makeInterest("/Hello/World", false, 50_ms),
285 nullptr, nullptr, nullptr);
286 advanceClocks(50_ms, 2);
287 face2.reset();
288
289 advanceClocks(50_ms, 2); // should not crash - Bug 2518
290
291 // avoid "test case [...] did not check any assertions" message from Boost.Test
292 BOOST_CHECK(true);
293}
294
295BOOST_AUTO_TEST_CASE(Handle)
Junxiao Shi80609d42019-01-29 18:15:22 +0000296{
297 auto hdl = face.expressInterest(*makeInterest("/Hello/World", true, 50_ms),
298 bind([] { BOOST_FAIL("Unexpected data"); }),
299 bind([] { BOOST_FAIL("Unexpected nack"); }),
300 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Davide Pesaventod247d492020-01-28 21:30:20 -0500301 advanceClocks(1_ms);
Junxiao Shi80609d42019-01-29 18:15:22 +0000302 hdl.cancel();
Davide Pesaventod247d492020-01-28 21:30:20 -0500303 advanceClocks(1_ms);
Junxiao Shi80609d42019-01-29 18:15:22 +0000304 face.receive(*makeData("/Hello/World/%21"));
305 advanceClocks(200_ms, 5);
306
Davide Pesaventod247d492020-01-28 21:30:20 -0500307 // cancel after destructing face
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500308 auto face2 = make_unique<DummyClientFace>(m_io, m_keyChain);
Davide Pesaventod247d492020-01-28 21:30:20 -0500309 auto hdl2 = face2->expressInterest(*makeInterest("/Hello/World", true, 50_ms),
310 bind([] { BOOST_FAIL("Unexpected data"); }),
311 bind([] { BOOST_FAIL("Unexpected nack"); }),
312 bind([] { BOOST_FAIL("Unexpected timeout"); }));
313 advanceClocks(1_ms);
314 face2.reset();
315 advanceClocks(1_ms);
316 hdl2.cancel(); // should not crash
317 advanceClocks(1_ms);
318
Junxiao Shi80609d42019-01-29 18:15:22 +0000319 // avoid "test case [...] did not check any assertions" message from Boost.Test
320 BOOST_CHECK(true);
321}
322
Davide Pesaventod247d492020-01-28 21:30:20 -0500323BOOST_AUTO_TEST_SUITE_END() // ExpressInterest
324
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000325BOOST_AUTO_TEST_CASE(RemoveAllPendingInterests)
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500326{
Junxiao Shib55e5d32018-07-18 13:32:00 -0600327 face.expressInterest(*makeInterest("/Hello/World/0", false, 50_ms),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800328 bind([] { BOOST_FAIL("Unexpected data"); }),
329 bind([] { BOOST_FAIL("Unexpected nack"); }),
330 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500331
Junxiao Shib55e5d32018-07-18 13:32:00 -0600332 face.expressInterest(*makeInterest("/Hello/World/1", false, 50_ms),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800333 bind([] { BOOST_FAIL("Unexpected data"); }),
334 bind([] { BOOST_FAIL("Unexpected nack"); }),
335 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500336
Davide Pesavento0f830802018-01-16 23:58:58 -0500337 advanceClocks(10_ms);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500338
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800339 face.removeAllPendingInterests();
Davide Pesavento0f830802018-01-16 23:58:58 -0500340 advanceClocks(10_ms);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500341
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800342 BOOST_CHECK_EQUAL(face.getNPendingInterests(), 0);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500343
Junxiao Shi85d90832016-08-04 03:19:46 +0000344 face.receive(*makeData("/Hello/World/0"));
345 face.receive(*makeData("/Hello/World/1"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500346 advanceClocks(200_ms, 5);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500347}
348
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000349BOOST_AUTO_TEST_SUITE(Producer)
350
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000351BOOST_AUTO_TEST_CASE(PutData)
352{
353 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
354
355 Data data("/4g7xxcuEow/KFvK5Kf2m");
356 signData(data);
357 face.put(data);
358
359 lp::CachePolicy cachePolicy;
360 cachePolicy.setPolicy(lp::CachePolicyType::NO_CACHE);
361 data.setTag(make_shared<lp::CachePolicyTag>(cachePolicy));
Eric Newberry4d261b62016-11-10 13:40:09 -0700362 data.setTag(make_shared<lp::CongestionMarkTag>(1));
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000363 face.put(data);
364
Davide Pesavento0f830802018-01-16 23:58:58 -0500365 advanceClocks(10_ms);
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000366 BOOST_REQUIRE_EQUAL(face.sentData.size(), 2);
367 BOOST_CHECK(face.sentData[0].getTag<lp::CachePolicyTag>() == nullptr);
Eric Newberry4d261b62016-11-10 13:40:09 -0700368 BOOST_CHECK(face.sentData[0].getTag<lp::CongestionMarkTag>() == nullptr);
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000369 BOOST_CHECK(face.sentData[1].getTag<lp::CachePolicyTag>() != nullptr);
Eric Newberry4d261b62016-11-10 13:40:09 -0700370 BOOST_CHECK(face.sentData[1].getTag<lp::CongestionMarkTag>() != nullptr);
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000371}
372
Junxiao Shib6828912017-11-20 14:06:32 +0000373BOOST_AUTO_TEST_CASE(PutDataLoopback)
374{
375 bool hasInterest1 = false, hasData = false;
376
377 // first InterestFilter allows loopback and should receive Interest
Davide Pesavento720e25c2019-07-14 01:33:52 -0400378 face.setInterestFilter("/", [&] (const InterestFilter&, const Interest&) {
Junxiao Shib6828912017-11-20 14:06:32 +0000379 hasInterest1 = true;
380 // do not respond with Data right away, so Face must send Interest to forwarder
381 });
382 // second InterestFilter disallows loopback and should not receive Interest
383 face.setInterestFilter(InterestFilter("/").allowLoopback(false),
384 bind([] { BOOST_ERROR("Unexpected Interest on second InterestFilter"); }));
385
Junxiao Shib55e5d32018-07-18 13:32:00 -0600386 face.expressInterest(*makeInterest("/A", true),
Junxiao Shib6828912017-11-20 14:06:32 +0000387 bind([&] { hasData = true; }),
388 bind([] { BOOST_FAIL("Unexpected nack"); }),
389 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500390 advanceClocks(1_ms);
Junxiao Shib6828912017-11-20 14:06:32 +0000391 BOOST_CHECK_EQUAL(hasInterest1, true); // Interest looped back
392 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1); // Interest sent to forwarder
393 BOOST_CHECK_EQUAL(hasData, false); // waiting for Data
394
395 face.put(*makeData("/A/B")); // first InterestFilter responds with Data
Davide Pesavento0f830802018-01-16 23:58:58 -0500396 advanceClocks(1_ms);
Junxiao Shib6828912017-11-20 14:06:32 +0000397 BOOST_CHECK_EQUAL(hasData, true);
398 BOOST_CHECK_EQUAL(face.sentData.size(), 0); // do not spill Data to forwarder
399}
400
Junxiao Shi859888f2017-09-12 14:29:16 +0000401BOOST_AUTO_TEST_CASE(PutMultipleData)
402{
403 bool hasInterest1 = false;
404 // register two Interest destinations
405 face.setInterestFilter("/", bind([&] {
406 hasInterest1 = true;
407 // sending Data right away from the first destination, don't care whether Interest goes to second destination
408 face.put(*makeData("/A/B"));
409 }));
410 face.setInterestFilter("/", bind([]{}));
Davide Pesavento0f830802018-01-16 23:58:58 -0500411 advanceClocks(10_ms);
Junxiao Shi859888f2017-09-12 14:29:16 +0000412
Junxiao Shib55e5d32018-07-18 13:32:00 -0600413 face.receive(*makeInterest("/A", true));
Davide Pesavento0f830802018-01-16 23:58:58 -0500414 advanceClocks(10_ms);
Junxiao Shi859888f2017-09-12 14:29:16 +0000415 BOOST_CHECK(hasInterest1);
416 BOOST_CHECK_EQUAL(face.sentData.size(), 1);
417 BOOST_CHECK_EQUAL(face.sentData.at(0).getName(), "/A/B");
418
419 face.put(*makeData("/A/C"));
420 BOOST_CHECK_EQUAL(face.sentData.size(), 1); // additional Data are ignored
421}
422
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000423BOOST_AUTO_TEST_CASE(PutNack)
424{
Junxiao Shi79a7a162017-09-09 08:33:57 +0000425 face.setInterestFilter("/", bind([]{})); // register one Interest destination so that face can accept Nacks
Davide Pesavento0f830802018-01-16 23:58:58 -0500426 advanceClocks(10_ms);
Junxiao Shi79a7a162017-09-09 08:33:57 +0000427
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000428 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0);
429
Davide Pesaventof6b45892023-03-13 15:00:51 -0400430 face.put(makeNack(*makeInterest("/unsolicited", false, std::nullopt, 18645250),
Junxiao Shib55e5d32018-07-18 13:32:00 -0600431 lp::NackReason::NO_ROUTE));
Davide Pesavento0f830802018-01-16 23:58:58 -0500432 advanceClocks(10_ms);
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000433 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0); // unsolicited Nack would not be sent
Eric Newberry4d261b62016-11-10 13:40:09 -0700434
Davide Pesaventof6b45892023-03-13 15:00:51 -0400435 auto interest1 = makeInterest("/Hello/World", false, std::nullopt, 14247162);
Junxiao Shib55e5d32018-07-18 13:32:00 -0600436 face.receive(*interest1);
Davide Pesaventof6b45892023-03-13 15:00:51 -0400437 auto interest2 = makeInterest("/another/prefix", false, std::nullopt, 92203002);
Junxiao Shib55e5d32018-07-18 13:32:00 -0600438 face.receive(*interest2);
Davide Pesavento0f830802018-01-16 23:58:58 -0500439 advanceClocks(10_ms);
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000440
Junxiao Shib55e5d32018-07-18 13:32:00 -0600441 face.put(makeNack(*interest1, lp::NackReason::DUPLICATE));
Davide Pesavento0f830802018-01-16 23:58:58 -0500442 advanceClocks(10_ms);
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000443 BOOST_REQUIRE_EQUAL(face.sentNacks.size(), 1);
444 BOOST_CHECK_EQUAL(face.sentNacks[0].getReason(), lp::NackReason::DUPLICATE);
445 BOOST_CHECK(face.sentNacks[0].getTag<lp::CongestionMarkTag>() == nullptr);
446
Junxiao Shib55e5d32018-07-18 13:32:00 -0600447 auto nack = makeNack(*interest2, lp::NackReason::NO_ROUTE);
Eric Newberry4d261b62016-11-10 13:40:09 -0700448 nack.setTag(make_shared<lp::CongestionMarkTag>(1));
449 face.put(nack);
Davide Pesavento0f830802018-01-16 23:58:58 -0500450 advanceClocks(10_ms);
Eric Newberry4d261b62016-11-10 13:40:09 -0700451 BOOST_REQUIRE_EQUAL(face.sentNacks.size(), 2);
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000452 BOOST_CHECK_EQUAL(face.sentNacks[1].getReason(), lp::NackReason::NO_ROUTE);
Eric Newberry4d261b62016-11-10 13:40:09 -0700453 BOOST_CHECK(face.sentNacks[1].getTag<lp::CongestionMarkTag>() != nullptr);
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000454}
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500455
Junxiao Shi79a7a162017-09-09 08:33:57 +0000456BOOST_AUTO_TEST_CASE(PutMultipleNack)
457{
Junxiao Shi859888f2017-09-12 14:29:16 +0000458 bool hasInterest1 = false, hasInterest2 = false;
459 // register two Interest destinations
460 face.setInterestFilter("/", [&] (const InterestFilter&, const Interest& interest) {
461 hasInterest1 = true;
462 // sending Nack right away from the first destination, Interest should still go to second destination
463 face.put(makeNack(interest, lp::NackReason::CONGESTION));
464 });
465 face.setInterestFilter("/", bind([&] { hasInterest2 = true; }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500466 advanceClocks(10_ms);
Junxiao Shi79a7a162017-09-09 08:33:57 +0000467
Davide Pesaventof6b45892023-03-13 15:00:51 -0400468 auto interest = makeInterest("/A", false, std::nullopt, 14333271);
Junxiao Shib55e5d32018-07-18 13:32:00 -0600469 face.receive(*interest);
Davide Pesavento0f830802018-01-16 23:58:58 -0500470 advanceClocks(10_ms);
Junxiao Shi859888f2017-09-12 14:29:16 +0000471 BOOST_CHECK(hasInterest1);
472 BOOST_CHECK(hasInterest2);
Junxiao Shi79a7a162017-09-09 08:33:57 +0000473
Junxiao Shi859888f2017-09-12 14:29:16 +0000474 // Nack from first destination is received, should wait for a response from the other destination
475 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0);
Junxiao Shi79a7a162017-09-09 08:33:57 +0000476
Junxiao Shib55e5d32018-07-18 13:32:00 -0600477 face.put(makeNack(*interest, lp::NackReason::NO_ROUTE)); // Nack from second destination
Davide Pesavento0f830802018-01-16 23:58:58 -0500478 advanceClocks(10_ms);
Junxiao Shi859888f2017-09-12 14:29:16 +0000479 BOOST_CHECK_EQUAL(face.sentNacks.size(), 1); // sending Nack after both destinations Nacked
Junxiao Shi79a7a162017-09-09 08:33:57 +0000480 BOOST_CHECK_EQUAL(face.sentNacks.at(0).getReason(), lp::NackReason::CONGESTION); // least severe reason
481
Junxiao Shib55e5d32018-07-18 13:32:00 -0600482 face.put(makeNack(*interest, lp::NackReason::DUPLICATE));
Junxiao Shi79a7a162017-09-09 08:33:57 +0000483 BOOST_CHECK_EQUAL(face.sentNacks.size(), 1); // additional Nacks are ignored
484}
485
Junxiao Shib6828912017-11-20 14:06:32 +0000486BOOST_AUTO_TEST_CASE(PutMultipleNackLoopback)
487{
488 bool hasInterest1 = false, hasNack = false;
489
490 // first InterestFilter allows loopback and should receive Interest
491 face.setInterestFilter("/", [&] (const InterestFilter&, const Interest& interest) {
492 hasInterest1 = true;
493 face.put(makeNack(interest, lp::NackReason::CONGESTION));
494 });
495 // second InterestFilter disallows loopback and should not receive Interest
496 face.setInterestFilter(InterestFilter("/").allowLoopback(false),
497 bind([] { BOOST_ERROR("Unexpected Interest on second InterestFilter"); }));
498
Davide Pesaventof6b45892023-03-13 15:00:51 -0400499 auto interest = makeInterest("/A", false, std::nullopt, 28395852);
Junxiao Shib55e5d32018-07-18 13:32:00 -0600500 face.expressInterest(*interest,
Junxiao Shib6828912017-11-20 14:06:32 +0000501 bind([] { BOOST_FAIL("Unexpected data"); }),
502 [&] (const Interest&, const lp::Nack& nack) {
503 hasNack = true;
504 BOOST_CHECK_EQUAL(nack.getReason(), lp::NackReason::CONGESTION);
505 },
506 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500507 advanceClocks(1_ms);
Junxiao Shib6828912017-11-20 14:06:32 +0000508 BOOST_CHECK_EQUAL(hasInterest1, true); // Interest looped back
509 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1); // Interest sent to forwarder
510 BOOST_CHECK_EQUAL(hasNack, false); // waiting for Nack from forwarder
511
Junxiao Shib55e5d32018-07-18 13:32:00 -0600512 face.receive(makeNack(*interest, lp::NackReason::NO_ROUTE));
Davide Pesavento0f830802018-01-16 23:58:58 -0500513 advanceClocks(1_ms);
Junxiao Shib6828912017-11-20 14:06:32 +0000514 BOOST_CHECK_EQUAL(hasNack, true);
515}
516
Davide Pesaventod247d492020-01-28 21:30:20 -0500517BOOST_AUTO_TEST_SUITE_END() // Producer
518
519BOOST_AUTO_TEST_SUITE(RegisterPrefix)
520
521BOOST_FIXTURE_TEST_CASE(Failure, FaceFixture<NoPrefixRegReply>)
522{
523 BOOST_CHECK(!runPrefixReg([&] (const auto& success, const auto& failure) {
524 face.registerPrefix("/Hello/World", success, failure);
525 this->advanceClocks(5_s, 20); // wait for command timeout
526 }));
527}
528
529BOOST_AUTO_TEST_CASE(Handle)
530{
531 RegisteredPrefixHandle hdl;
532 auto doReg = [&] {
533 return runPrefixReg([&] (const auto& success, const auto& failure) {
534 hdl = face.registerPrefix("/Hello/World", success, failure);
535 });
536 };
537 auto doUnreg = [&] {
538 return runPrefixUnreg([&] (const auto& success, const auto& failure) {
539 hdl.unregister(success, failure);
540 });
541 };
542
543 // despite the "undefined behavior" warning, we try not to crash, but no API guarantee for this
544 BOOST_CHECK(!doUnreg());
545
546 // cancel after unregister
547 BOOST_CHECK(doReg());
548 BOOST_CHECK(doUnreg());
549 hdl.cancel();
550 advanceClocks(1_ms);
551
552 // unregister after cancel
553 BOOST_CHECK(doReg());
554 hdl.cancel();
555 advanceClocks(1_ms);
556 BOOST_CHECK(!doUnreg());
557
558 // cancel after destructing face
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500559 auto face2 = make_unique<DummyClientFace>(m_io, m_keyChain);
Davide Pesaventod247d492020-01-28 21:30:20 -0500560 hdl = face2->registerPrefix("/Hello/World/2", nullptr,
561 bind([] { BOOST_FAIL("Unexpected registerPrefix failure"); }));
562 advanceClocks(1_ms);
563 face2.reset();
564 advanceClocks(1_ms);
565 hdl.cancel(); // should not crash
566 advanceClocks(1_ms);
567
568 // unregister after destructing face
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500569 auto face3 = make_unique<DummyClientFace>(m_io, m_keyChain);
Davide Pesaventod247d492020-01-28 21:30:20 -0500570 hdl = face3->registerPrefix("/Hello/World/3", nullptr,
571 bind([] { BOOST_FAIL("Unexpected registerPrefix failure"); }));
572 advanceClocks(1_ms);
573 face3.reset();
574 advanceClocks(1_ms);
575 BOOST_CHECK(!doUnreg());
576}
577
578BOOST_AUTO_TEST_SUITE_END() // RegisterPrefix
579
580BOOST_AUTO_TEST_SUITE(SetInterestFilter)
581
582BOOST_AUTO_TEST_CASE(SetAndCancel)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400583{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800584 size_t nInterests = 0;
585 size_t nRegs = 0;
Davide Pesavento720e25c2019-07-14 01:33:52 -0400586 auto hdl = face.setInterestFilter("/Hello/World",
587 bind([&nInterests] { ++nInterests; }),
588 bind([&nRegs] { ++nRegs; }),
589 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500590 advanceClocks(25_ms, 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800591 BOOST_CHECK_EQUAL(nRegs, 1);
592 BOOST_CHECK_EQUAL(nInterests, 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400593
Junxiao Shib55e5d32018-07-18 13:32:00 -0600594 face.receive(*makeInterest("/Hello/World/%21"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500595 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400596
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800597 BOOST_CHECK_EQUAL(nRegs, 1);
598 BOOST_CHECK_EQUAL(nInterests, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400599
Junxiao Shib55e5d32018-07-18 13:32:00 -0600600 face.receive(*makeInterest("/Bye/World/%21"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500601 advanceClocks(10000_ms, 10);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800602 BOOST_CHECK_EQUAL(nInterests, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400603
Junxiao Shib55e5d32018-07-18 13:32:00 -0600604 face.receive(*makeInterest("/Hello/World/%21/2"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500605 advanceClocks(25_ms, 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800606 BOOST_CHECK_EQUAL(nInterests, 2);
607
608 // removing filter
Davide Pesavento720e25c2019-07-14 01:33:52 -0400609 hdl.cancel();
Davide Pesavento0f830802018-01-16 23:58:58 -0500610 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400611
Junxiao Shib55e5d32018-07-18 13:32:00 -0600612 face.receive(*makeInterest("/Hello/World/%21/3"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800613 BOOST_CHECK_EQUAL(nInterests, 2);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800614}
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400615
Davide Pesaventod247d492020-01-28 21:30:20 -0500616BOOST_AUTO_TEST_CASE(EmptyInterestCallback)
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000617{
618 face.setInterestFilter("/A", nullptr);
Davide Pesavento0f830802018-01-16 23:58:58 -0500619 advanceClocks(1_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000620
621 BOOST_CHECK_NO_THROW(do {
622 face.receive(*makeInterest("/A/1"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500623 advanceClocks(1_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000624 } while (false));
625}
626
Davide Pesaventod247d492020-01-28 21:30:20 -0500627BOOST_AUTO_TEST_CASE(WithoutSuccessCallback)
Joao Pereira0b3cac52015-07-02 14:49:49 -0400628{
629 size_t nInterests = 0;
Davide Pesavento720e25c2019-07-14 01:33:52 -0400630 auto hdl = face.setInterestFilter("/Hello/World",
631 bind([&nInterests] { ++nInterests; }),
632 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500633 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400634 BOOST_CHECK_EQUAL(nInterests, 0);
635
Junxiao Shib55e5d32018-07-18 13:32:00 -0600636 face.receive(*makeInterest("/Hello/World/%21"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500637 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400638
639 BOOST_CHECK_EQUAL(nInterests, 1);
640
Junxiao Shib55e5d32018-07-18 13:32:00 -0600641 face.receive(*makeInterest("/Bye/World/%21"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500642 advanceClocks(10000_ms, 10);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400643 BOOST_CHECK_EQUAL(nInterests, 1);
644
Junxiao Shib55e5d32018-07-18 13:32:00 -0600645 face.receive(*makeInterest("/Hello/World/%21/2"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500646 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400647 BOOST_CHECK_EQUAL(nInterests, 2);
648
649 // removing filter
Davide Pesavento720e25c2019-07-14 01:33:52 -0400650 hdl.cancel();
Davide Pesavento0f830802018-01-16 23:58:58 -0500651 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400652
Junxiao Shib55e5d32018-07-18 13:32:00 -0600653 face.receive(*makeInterest("/Hello/World/%21/3"));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400654 BOOST_CHECK_EQUAL(nInterests, 2);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400655}
656
Davide Pesaventod247d492020-01-28 21:30:20 -0500657BOOST_FIXTURE_TEST_CASE(Failure, FaceFixture<NoPrefixRegReply>)
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800658{
659 // don't enable registration reply
660 size_t nRegFailed = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800661 face.setInterestFilter("/Hello/World",
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000662 bind([] { BOOST_FAIL("Unexpected Interest"); }),
663 bind([] { BOOST_FAIL("Unexpected success of setInterestFilter"); }),
664 bind([&nRegFailed] { ++nRegFailed; }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800665
Davide Pesavento0f830802018-01-16 23:58:58 -0500666 advanceClocks(25_ms, 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800667 BOOST_CHECK_EQUAL(nRegFailed, 0);
668
Davide Pesavento0f830802018-01-16 23:58:58 -0500669 advanceClocks(2000_ms, 5);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800670 BOOST_CHECK_EQUAL(nRegFailed, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400671}
672
Davide Pesaventod247d492020-01-28 21:30:20 -0500673BOOST_FIXTURE_TEST_CASE(FailureWithoutSuccessCallback, FaceFixture<NoPrefixRegReply>)
Joao Pereira0b3cac52015-07-02 14:49:49 -0400674{
675 // don't enable registration reply
676 size_t nRegFailed = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800677 face.setInterestFilter("/Hello/World",
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000678 bind([] { BOOST_FAIL("Unexpected Interest"); }),
679 bind([&nRegFailed] { ++nRegFailed; }));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400680
Davide Pesavento0f830802018-01-16 23:58:58 -0500681 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400682 BOOST_CHECK_EQUAL(nRegFailed, 0);
683
Davide Pesavento0f830802018-01-16 23:58:58 -0500684 advanceClocks(2000_ms, 5);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400685 BOOST_CHECK_EQUAL(nRegFailed, 1);
686}
687
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800688BOOST_AUTO_TEST_CASE(SimilarFilters)
689{
690 size_t nInInterests1 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800691 face.setInterestFilter("/Hello/World",
692 bind([&nInInterests1] { ++nInInterests1; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000693 nullptr,
694 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800695
696 size_t nInInterests2 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800697 face.setInterestFilter("/Hello",
698 bind([&nInInterests2] { ++nInInterests2; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000699 nullptr,
700 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400701
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800702 size_t nInInterests3 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800703 face.setInterestFilter("/Los/Angeles/Lakers",
704 bind([&nInInterests3] { ++nInInterests3; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000705 nullptr,
706 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400707
Davide Pesavento0f830802018-01-16 23:58:58 -0500708 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400709
Junxiao Shib55e5d32018-07-18 13:32:00 -0600710 face.receive(*makeInterest("/Hello/World/%21"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500711 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400712
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800713 BOOST_CHECK_EQUAL(nInInterests1, 1);
714 BOOST_CHECK_EQUAL(nInInterests2, 1);
715 BOOST_CHECK_EQUAL(nInInterests3, 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400716}
717
Davide Pesaventod247d492020-01-28 21:30:20 -0500718BOOST_AUTO_TEST_CASE(RegexFilter)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400719{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800720 size_t nInInterests = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800721 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
722 bind([&nInInterests] { ++nInInterests; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000723 nullptr,
724 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400725
Davide Pesavento0f830802018-01-16 23:58:58 -0500726 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400727
Junxiao Shib55e5d32018-07-18 13:32:00 -0600728 face.receive(*makeInterest("/Hello/World/a")); // shouldn't match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400729 BOOST_CHECK_EQUAL(nInInterests, 0);
730
Junxiao Shib55e5d32018-07-18 13:32:00 -0600731 face.receive(*makeInterest("/Hello/World/a/b")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400732 BOOST_CHECK_EQUAL(nInInterests, 1);
733
Junxiao Shib55e5d32018-07-18 13:32:00 -0600734 face.receive(*makeInterest("/Hello/World/a/b/c")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400735 BOOST_CHECK_EQUAL(nInInterests, 2);
736
Junxiao Shib55e5d32018-07-18 13:32:00 -0600737 face.receive(*makeInterest("/Hello/World/a/b/d")); // should not match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400738 BOOST_CHECK_EQUAL(nInInterests, 2);
739}
740
Davide Pesaventod247d492020-01-28 21:30:20 -0500741BOOST_AUTO_TEST_CASE(RegexFilterError)
742{
743 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
744 [] (const Name&, const Interest&) {
745 BOOST_FAIL("InterestFilter::Error should have been triggered");
746 },
747 nullptr,
748 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
749
750 advanceClocks(25_ms, 4);
751
752 BOOST_CHECK_THROW(face.receive(*makeInterest("/Hello/World/XXX/b/c")), InterestFilter::Error);
753}
754
755BOOST_AUTO_TEST_CASE(RegexFilterAndRegisterPrefix)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400756{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800757 size_t nInInterests = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800758 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
759 bind([&nInInterests] { ++nInInterests; }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400760
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800761 size_t nRegSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800762 face.registerPrefix("/Hello/World",
763 bind([&nRegSuccesses] { ++nRegSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000764 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400765
Davide Pesavento0f830802018-01-16 23:58:58 -0500766 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400767 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
768
Junxiao Shib55e5d32018-07-18 13:32:00 -0600769 face.receive(*makeInterest("/Hello/World/a")); // shouldn't match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400770 BOOST_CHECK_EQUAL(nInInterests, 0);
771
Junxiao Shib55e5d32018-07-18 13:32:00 -0600772 face.receive(*makeInterest("/Hello/World/a/b")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400773 BOOST_CHECK_EQUAL(nInInterests, 1);
774
Junxiao Shib55e5d32018-07-18 13:32:00 -0600775 face.receive(*makeInterest("/Hello/World/a/b/c")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400776 BOOST_CHECK_EQUAL(nInInterests, 2);
777
Junxiao Shib55e5d32018-07-18 13:32:00 -0600778 face.receive(*makeInterest("/Hello/World/a/b/d")); // should not match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400779 BOOST_CHECK_EQUAL(nInInterests, 2);
780}
781
Davide Pesaventod247d492020-01-28 21:30:20 -0500782BOOST_FIXTURE_TEST_CASE(WithoutRegisterPrefix, FaceFixture<NoPrefixRegReply>) // Bug 2318
Junxiao Shia1ea5062014-12-27 22:33:39 -0700783{
784 // This behavior is specific to DummyClientFace.
785 // Regular Face won't accept incoming packets until something is sent.
786
787 int hit = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800788 face.setInterestFilter(Name("/"), bind([&hit] { ++hit; }));
789 face.processEvents(time::milliseconds(-1));
Junxiao Shia1ea5062014-12-27 22:33:39 -0700790
Junxiao Shib55e5d32018-07-18 13:32:00 -0600791 face.receive(*makeInterest("/A"));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800792 face.processEvents(time::milliseconds(-1));
Junxiao Shia1ea5062014-12-27 22:33:39 -0700793
794 BOOST_CHECK_EQUAL(hit, 1);
795}
796
Davide Pesaventod247d492020-01-28 21:30:20 -0500797BOOST_AUTO_TEST_CASE(Handle)
Junxiao Shi60aaef02019-01-14 04:59:30 +0000798{
799 int hit = 0;
Davide Pesaventod247d492020-01-28 21:30:20 -0500800 InterestFilterHandle hdl = face.setInterestFilter(Name("/"), bind([&hit] { ++hit; }));
Junxiao Shi60aaef02019-01-14 04:59:30 +0000801 face.processEvents(-1_ms);
802
803 face.receive(*makeInterest("/A"));
804 face.processEvents(-1_ms);
805 BOOST_CHECK_EQUAL(hit, 1);
806
807 hdl.cancel();
808 face.processEvents(-1_ms);
809
810 face.receive(*makeInterest("/B"));
811 face.processEvents(-1_ms);
812 BOOST_CHECK_EQUAL(hit, 1);
Davide Pesaventod247d492020-01-28 21:30:20 -0500813
814 // cancel after destructing face
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500815 auto face2 = make_unique<DummyClientFace>(m_io, m_keyChain);
Davide Pesaventod247d492020-01-28 21:30:20 -0500816 InterestFilterHandle hdl2 = face2->setInterestFilter("/Hello/World/2", nullptr);
817 advanceClocks(1_ms);
818 face2.reset();
819 advanceClocks(1_ms);
820 hdl2.cancel(); // should not crash
821 advanceClocks(1_ms);
Junxiao Shi60aaef02019-01-14 04:59:30 +0000822}
823
Davide Pesaventod247d492020-01-28 21:30:20 -0500824BOOST_AUTO_TEST_SUITE_END() // SetInterestFilter
Junxiao Shiae0b4182016-08-08 22:53:17 +0000825
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500826BOOST_AUTO_TEST_CASE(ProcessEvents)
827{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800828 face.processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500829
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800830 size_t nRegSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800831 face.registerPrefix("/Hello/World",
832 bind([&nRegSuccesses] { ++nRegSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000833 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500834
835 // io_service::poll() without reset
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800836 face.getIoService().poll();
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500837 BOOST_CHECK_EQUAL(nRegSuccesses, 0);
838
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800839 face.processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500840 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
841}
842
Junxiao Shiae0b4182016-08-08 22:53:17 +0000843BOOST_AUTO_TEST_CASE(DestroyWithoutProcessEvents) // Bug 3248
844{
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500845 auto face2 = make_unique<Face>(m_io);
Junxiao Shiae0b4182016-08-08 22:53:17 +0000846 face2.reset();
847
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500848 m_io.poll(); // should not crash
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100849
850 // avoid "test case [...] did not check any assertions" message from Boost.Test
851 BOOST_CHECK(true);
Junxiao Shiae0b4182016-08-08 22:53:17 +0000852}
853
Junxiao Shiae0b4182016-08-08 22:53:17 +0000854BOOST_AUTO_TEST_SUITE(Transport)
855
856using ndn::Transport;
857
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700858struct PibDirWithDefaultTpm
859{
860 const std::string PATH = "build/keys-with-default-tpm";
861};
862
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500863BOOST_FIXTURE_TEST_CASE(FaceTransport, IoKeyChainFixture)
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800864{
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800865 BOOST_CHECK(Face().getTransport() != nullptr);
866
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800867 BOOST_CHECK(Face(shared_ptr<Transport>()).getTransport() != nullptr);
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500868 BOOST_CHECK(Face(shared_ptr<Transport>(), m_io).getTransport() != nullptr);
869 BOOST_CHECK(Face(shared_ptr<Transport>(), m_io, m_keyChain).getTransport() != nullptr);
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800870
871 auto transport = make_shared<TcpTransport>("localhost", "6363"); // no real io operations will be scheduled
872 BOOST_CHECK(Face(transport).getTransport() == transport);
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500873 BOOST_CHECK(Face(transport, m_io).getTransport() == transport);
874 BOOST_CHECK(Face(transport, m_io, m_keyChain).getTransport() == transport);
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800875}
876
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500877class WithEnv
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700878{
879public:
880 WithEnv()
881 {
882 if (getenv("NDN_CLIENT_TRANSPORT") != nullptr) {
883 m_oldTransport = getenv("NDN_CLIENT_TRANSPORT");
884 unsetenv("NDN_CLIENT_TRANSPORT");
885 }
886 }
887
888 void
889 configure(const std::string& faceUri)
890 {
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500891 setenv("NDN_CLIENT_TRANSPORT", faceUri.data(), true);
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700892 }
893
894 ~WithEnv()
895 {
896 if (!m_oldTransport.empty()) {
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500897 setenv("NDN_CLIENT_TRANSPORT", m_oldTransport.data(), true);
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700898 }
899 else {
900 unsetenv("NDN_CLIENT_TRANSPORT");
901 }
902 }
903
904private:
905 std::string m_oldTransport;
906};
907
908class WithConfig : private TestHomeFixture<DefaultPibDir>
909{
910public:
911 void
912 configure(const std::string& faceUri)
913 {
914 createClientConf({"transport=" + faceUri});
915 }
916};
917
918class WithEnvAndConfig : public WithEnv, public WithConfig
919{
920};
921
922typedef boost::mpl::vector<WithEnv, WithConfig> ConfigOptions;
923
924BOOST_FIXTURE_TEST_CASE(NoConfig, WithEnvAndConfig) // fixture configures test HOME and PIB/TPM path
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700925{
926 shared_ptr<Face> face;
927 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
928 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700929}
930
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700931BOOST_FIXTURE_TEST_CASE_TEMPLATE(Unix, T, ConfigOptions, T)
932{
933 this->configure("unix://some/path");
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700934
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700935 shared_ptr<Face> face;
936 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
937 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
938}
939
940BOOST_FIXTURE_TEST_CASE_TEMPLATE(Tcp, T, ConfigOptions, T)
941{
942 this->configure("tcp://127.0.0.1:6000");
943
944 shared_ptr<Face> face;
945 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
946 BOOST_CHECK(dynamic_pointer_cast<TcpTransport>(face->getTransport()) != nullptr);
947}
948
949BOOST_FIXTURE_TEST_CASE_TEMPLATE(WrongTransport, T, ConfigOptions, T)
950{
951 this->configure("wrong-transport:");
952
953 BOOST_CHECK_THROW(make_shared<Face>(), ConfigFile::Error);
954}
955
956BOOST_FIXTURE_TEST_CASE_TEMPLATE(WrongUri, T, ConfigOptions, T)
957{
958 this->configure("wrong-uri");
959
960 BOOST_CHECK_THROW(make_shared<Face>(), ConfigFile::Error);
961}
962
963BOOST_FIXTURE_TEST_CASE(EnvOverride, WithEnvAndConfig)
964{
965 this->WithEnv::configure("tcp://127.0.0.1:6000");
966 this->WithConfig::configure("unix://some/path");
967
968 shared_ptr<Face> face;
969 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
970 BOOST_CHECK(dynamic_pointer_cast<TcpTransport>(face->getTransport()) != nullptr);
971}
972
973BOOST_FIXTURE_TEST_CASE(ExplicitTransport, WithEnvAndConfig)
974{
975 this->WithEnv::configure("wrong-uri");
976 this->WithConfig::configure("wrong-transport:");
977
978 auto transport = make_shared<UnixTransport>("unix://some/path");
979 shared_ptr<Face> face;
980 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>(transport));
981 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
982}
983
Junxiao Shiae0b4182016-08-08 22:53:17 +0000984BOOST_AUTO_TEST_SUITE_END() // Transport
985
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700986BOOST_AUTO_TEST_SUITE_END() // TestFace
987
988} // namespace tests
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400989} // namespace ndn