Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | f135f15 | 2022-01-06 20:43:06 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2022 Regents of the University of California. |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 4 | * |
| 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 Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #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 Pesavento | f135f15 | 2022-01-06 20:43:06 -0500 | [diff] [blame] | 26 | #include "ndn-cxx/util/config-file.hpp" |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 27 | #include "ndn-cxx/util/dummy-client-face.hpp" |
| 28 | #include "ndn-cxx/util/scheduler.hpp" |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 29 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 30 | #include "tests/test-common.hpp" |
| 31 | #include "tests/unit/io-key-chain-fixture.hpp" |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 32 | |
Junxiao Shi | ec475a7 | 2019-01-13 21:53:55 +0000 | [diff] [blame] | 33 | #include <boost/logic/tribool.hpp> |
| 34 | |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 35 | namespace ndn { |
| 36 | namespace tests { |
| 37 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 38 | using ndn::util::DummyClientFace; |
Davide Pesavento | 2e481fc | 2021-07-02 18:20:03 -0400 | [diff] [blame] | 39 | using std::bind; |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 40 | |
Junxiao Shi | ec475a7 | 2019-01-13 21:53:55 +0000 | [diff] [blame] | 41 | struct WantPrefixRegReply; |
| 42 | struct NoPrefixRegReply; |
| 43 | |
| 44 | template<typename PrefixRegReply = WantPrefixRegReply> |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 45 | class FaceFixture : public IoKeyChainFixture |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 46 | { |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 47 | protected: |
Junxiao Shi | ec475a7 | 2019-01-13 21:53:55 +0000 | [diff] [blame] | 48 | FaceFixture() |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 49 | : face(m_io, m_keyChain, {true, !std::is_same<PrefixRegReply, NoPrefixRegReply>::value}) |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 50 | { |
Junxiao Shi | ec475a7 | 2019-01-13 21:53:55 +0000 | [diff] [blame] | 51 | 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 Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 63 | f([&] (auto) { result = true; }, |
| 64 | [&] (auto, auto) { result = false; }); |
Junxiao Shi | ec475a7 | 2019-01-13 21:53:55 +0000 | [diff] [blame] | 65 | |
| 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 Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 79 | f([&] { result = true; }, |
| 80 | [&] (auto) { result = false; }); |
Junxiao Shi | ec475a7 | 2019-01-13 21:53:55 +0000 | [diff] [blame] | 81 | |
| 82 | advanceClocks(1_ms); |
| 83 | BOOST_REQUIRE(!boost::logic::indeterminate(result)); |
| 84 | return static_cast<bool>(result); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 85 | } |
| 86 | |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 87 | protected: |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 88 | DummyClientFace face; |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 89 | }; |
| 90 | |
Junxiao Shi | ec475a7 | 2019-01-13 21:53:55 +0000 | [diff] [blame] | 91 | BOOST_FIXTURE_TEST_SUITE(TestFace, FaceFixture<>) |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 92 | |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 93 | BOOST_AUTO_TEST_SUITE(ExpressInterest) |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 94 | |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 95 | BOOST_AUTO_TEST_CASE(ReplyData) |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 96 | { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 97 | size_t nData = 0; |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 98 | face.expressInterest(*makeInterest("/Hello/World", true, 50_ms), |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 99 | [&] (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 Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 104 | bind([] { BOOST_FAIL("Unexpected Nack"); }), |
| 105 | bind([] { BOOST_FAIL("Unexpected timeout"); })); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 106 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 107 | advanceClocks(40_ms); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 108 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 109 | face.receive(*makeData("/Bye/World/a")); |
| 110 | face.receive(*makeData("/Hello/World/a")); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 111 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 112 | advanceClocks(50_ms, 2); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 113 | |
| 114 | BOOST_CHECK_EQUAL(nData, 1); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 115 | BOOST_CHECK_EQUAL(face.sentInterests.size(), 1); |
| 116 | BOOST_CHECK_EQUAL(face.sentData.size(), 0); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 117 | |
| 118 | size_t nTimeouts = 0; |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 119 | face.expressInterest(*makeInterest("/Hello/World/a/2", false, 50_ms), |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 120 | bind([]{}), |
| 121 | bind([]{}), |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 122 | bind([&nTimeouts] { ++nTimeouts; })); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 123 | advanceClocks(200_ms, 5); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 124 | BOOST_CHECK_EQUAL(nTimeouts, 1); |
| 125 | } |
| 126 | |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 127 | BOOST_AUTO_TEST_CASE(MultipleData) |
Alexander Afanasyev | 1013fd0 | 2017-01-03 13:19:03 -0800 | [diff] [blame] | 128 | { |
| 129 | size_t nData = 0; |
| 130 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 131 | face.expressInterest(*makeInterest("/Hello/World", true, 50_ms), |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 132 | [&] (const auto&, const auto&) { ++nData; }, |
Alexander Afanasyev | 1013fd0 | 2017-01-03 13:19:03 -0800 | [diff] [blame] | 133 | bind([] { BOOST_FAIL("Unexpected Nack"); }), |
| 134 | bind([] { BOOST_FAIL("Unexpected timeout"); })); |
| 135 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 136 | face.expressInterest(*makeInterest("/Hello/World/a", true, 50_ms), |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 137 | [&] (const auto&, const auto&) { ++nData; }, |
Alexander Afanasyev | 1013fd0 | 2017-01-03 13:19:03 -0800 | [diff] [blame] | 138 | bind([] { BOOST_FAIL("Unexpected Nack"); }), |
| 139 | bind([] { BOOST_FAIL("Unexpected timeout"); })); |
| 140 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 141 | advanceClocks(40_ms); |
Alexander Afanasyev | 1013fd0 | 2017-01-03 13:19:03 -0800 | [diff] [blame] | 142 | |
| 143 | face.receive(*makeData("/Hello/World/a/b")); |
| 144 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 145 | advanceClocks(50_ms, 2); |
Alexander Afanasyev | 1013fd0 | 2017-01-03 13:19:03 -0800 | [diff] [blame] | 146 | |
| 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 Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 152 | BOOST_AUTO_TEST_CASE(EmptyDataCallback) |
Junxiao Shi | 76e0eb2 | 2016-08-08 05:54:10 +0000 | [diff] [blame] | 153 | { |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 154 | face.expressInterest(*makeInterest("/Hello/World", true), |
Junxiao Shi | 76e0eb2 | 2016-08-08 05:54:10 +0000 | [diff] [blame] | 155 | nullptr, |
| 156 | bind([] { BOOST_FAIL("Unexpected Nack"); }), |
| 157 | bind([] { BOOST_FAIL("Unexpected timeout"); })); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 158 | advanceClocks(1_ms); |
Junxiao Shi | 76e0eb2 | 2016-08-08 05:54:10 +0000 | [diff] [blame] | 159 | |
| 160 | BOOST_CHECK_NO_THROW(do { |
| 161 | face.receive(*makeData("/Hello/World/a")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 162 | advanceClocks(1_ms); |
Junxiao Shi | 76e0eb2 | 2016-08-08 05:54:10 +0000 | [diff] [blame] | 163 | } while (false)); |
| 164 | } |
| 165 | |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 166 | BOOST_AUTO_TEST_CASE(Timeout) |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 167 | { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 168 | size_t nTimeouts = 0; |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 169 | face.expressInterest(*makeInterest("/Hello/World", false, 50_ms), |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 170 | bind([] { BOOST_FAIL("Unexpected Data"); }), |
| 171 | bind([] { BOOST_FAIL("Unexpected Nack"); }), |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 172 | [&nTimeouts] (const Interest& i) { |
| 173 | BOOST_CHECK_EQUAL(i.getName(), "/Hello/World"); |
| 174 | ++nTimeouts; |
| 175 | }); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 176 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 177 | advanceClocks(200_ms, 5); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 178 | |
| 179 | BOOST_CHECK_EQUAL(nTimeouts, 1); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 180 | BOOST_CHECK_EQUAL(face.sentInterests.size(), 1); |
| 181 | BOOST_CHECK_EQUAL(face.sentData.size(), 0); |
| 182 | BOOST_CHECK_EQUAL(face.sentNacks.size(), 0); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 185 | BOOST_AUTO_TEST_CASE(EmptyTimeoutCallback) |
Junxiao Shi | 76e0eb2 | 2016-08-08 05:54:10 +0000 | [diff] [blame] | 186 | { |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 187 | face.expressInterest(*makeInterest("/Hello/World", false, 50_ms), |
Junxiao Shi | 76e0eb2 | 2016-08-08 05:54:10 +0000 | [diff] [blame] | 188 | bind([] { BOOST_FAIL("Unexpected Data"); }), |
| 189 | bind([] { BOOST_FAIL("Unexpected Nack"); }), |
| 190 | nullptr); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 191 | advanceClocks(40_ms); |
Junxiao Shi | 76e0eb2 | 2016-08-08 05:54:10 +0000 | [diff] [blame] | 192 | |
| 193 | BOOST_CHECK_NO_THROW(do { |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 194 | advanceClocks(6_ms, 2); |
Junxiao Shi | 76e0eb2 | 2016-08-08 05:54:10 +0000 | [diff] [blame] | 195 | } while (false)); |
| 196 | } |
| 197 | |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 198 | BOOST_AUTO_TEST_CASE(ReplyNack) |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 199 | { |
| 200 | size_t nNacks = 0; |
| 201 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 202 | auto interest = makeInterest("/Hello/World", false, 50_ms); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 203 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 204 | face.expressInterest(*interest, |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 205 | bind([] { BOOST_FAIL("Unexpected Data"); }), |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 206 | [&] (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 Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 212 | bind([] { BOOST_FAIL("Unexpected timeout"); })); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 213 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 214 | advanceClocks(40_ms); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 215 | |
Junxiao Shi | f5b5ae2 | 2016-08-08 05:54:41 +0000 | [diff] [blame] | 216 | face.receive(makeNack(face.sentInterests.at(0), lp::NackReason::DUPLICATE)); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 217 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 218 | advanceClocks(50_ms, 2); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 219 | |
| 220 | BOOST_CHECK_EQUAL(nNacks, 1); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 221 | BOOST_CHECK_EQUAL(face.sentInterests.size(), 1); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 224 | BOOST_AUTO_TEST_CASE(MultipleNacks) |
Alexander Afanasyev | 1013fd0 | 2017-01-03 13:19:03 -0800 | [diff] [blame] | 225 | { |
| 226 | size_t nNacks = 0; |
| 227 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 228 | auto interest = makeInterest("/Hello/World", false, 50_ms, 1); |
| 229 | face.expressInterest(*interest, |
Alexander Afanasyev | 1013fd0 | 2017-01-03 13:19:03 -0800 | [diff] [blame] | 230 | bind([] { BOOST_FAIL("Unexpected Data"); }), |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 231 | [&] (const auto&, const auto&) { ++nNacks; }, |
Alexander Afanasyev | 1013fd0 | 2017-01-03 13:19:03 -0800 | [diff] [blame] | 232 | bind([] { BOOST_FAIL("Unexpected timeout"); })); |
| 233 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 234 | interest->setNonce(2); |
| 235 | face.expressInterest(*interest, |
Alexander Afanasyev | 1013fd0 | 2017-01-03 13:19:03 -0800 | [diff] [blame] | 236 | bind([] { BOOST_FAIL("Unexpected Data"); }), |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 237 | [&] (const auto&, const auto&) { ++nNacks; }, |
Alexander Afanasyev | 1013fd0 | 2017-01-03 13:19:03 -0800 | [diff] [blame] | 238 | bind([] { BOOST_FAIL("Unexpected timeout"); })); |
| 239 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 240 | advanceClocks(40_ms); |
Alexander Afanasyev | 1013fd0 | 2017-01-03 13:19:03 -0800 | [diff] [blame] | 241 | |
| 242 | face.receive(makeNack(face.sentInterests.at(1), lp::NackReason::DUPLICATE)); |
| 243 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 244 | advanceClocks(50_ms, 2); |
Alexander Afanasyev | 1013fd0 | 2017-01-03 13:19:03 -0800 | [diff] [blame] | 245 | |
| 246 | BOOST_CHECK_EQUAL(nNacks, 2); |
| 247 | BOOST_CHECK_EQUAL(face.sentInterests.size(), 2); |
| 248 | } |
| 249 | |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 250 | BOOST_AUTO_TEST_CASE(EmptyNackCallback) |
Junxiao Shi | 76e0eb2 | 2016-08-08 05:54:10 +0000 | [diff] [blame] | 251 | { |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 252 | face.expressInterest(*makeInterest("/Hello/World"), |
Junxiao Shi | 76e0eb2 | 2016-08-08 05:54:10 +0000 | [diff] [blame] | 253 | bind([] { BOOST_FAIL("Unexpected Data"); }), |
| 254 | nullptr, |
| 255 | bind([] { BOOST_FAIL("Unexpected timeout"); })); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 256 | advanceClocks(1_ms); |
Junxiao Shi | 76e0eb2 | 2016-08-08 05:54:10 +0000 | [diff] [blame] | 257 | |
| 258 | BOOST_CHECK_NO_THROW(do { |
| 259 | face.receive(makeNack(face.sentInterests.at(0), lp::NackReason::DUPLICATE)); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 260 | advanceClocks(1_ms); |
Junxiao Shi | 76e0eb2 | 2016-08-08 05:54:10 +0000 | [diff] [blame] | 261 | } while (false)); |
| 262 | } |
| 263 | |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 264 | BOOST_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 | |
| 281 | BOOST_AUTO_TEST_CASE(DestroyWithPendingInterest) |
| 282 | { |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 283 | auto face2 = make_unique<DummyClientFace>(m_io, m_keyChain); |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 284 | 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 | |
| 295 | BOOST_AUTO_TEST_CASE(Handle) |
Junxiao Shi | 80609d4 | 2019-01-29 18:15:22 +0000 | [diff] [blame] | 296 | { |
| 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 Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 301 | advanceClocks(1_ms); |
Junxiao Shi | 80609d4 | 2019-01-29 18:15:22 +0000 | [diff] [blame] | 302 | hdl.cancel(); |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 303 | advanceClocks(1_ms); |
Junxiao Shi | 80609d4 | 2019-01-29 18:15:22 +0000 | [diff] [blame] | 304 | face.receive(*makeData("/Hello/World/%21")); |
| 305 | advanceClocks(200_ms, 5); |
| 306 | |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 307 | // cancel after destructing face |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 308 | auto face2 = make_unique<DummyClientFace>(m_io, m_keyChain); |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 309 | 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 Shi | 80609d4 | 2019-01-29 18:15:22 +0000 | [diff] [blame] | 319 | // avoid "test case [...] did not check any assertions" message from Boost.Test |
| 320 | BOOST_CHECK(true); |
| 321 | } |
| 322 | |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 323 | BOOST_AUTO_TEST_SUITE_END() // ExpressInterest |
| 324 | |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 325 | BOOST_AUTO_TEST_CASE(RemoveAllPendingInterests) |
Ilya Moiseenko | 56b0bf8 | 2015-11-08 11:14:28 -0500 | [diff] [blame] | 326 | { |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 327 | face.expressInterest(*makeInterest("/Hello/World/0", false, 50_ms), |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 328 | bind([] { BOOST_FAIL("Unexpected data"); }), |
| 329 | bind([] { BOOST_FAIL("Unexpected nack"); }), |
| 330 | bind([] { BOOST_FAIL("Unexpected timeout"); })); |
Ilya Moiseenko | 56b0bf8 | 2015-11-08 11:14:28 -0500 | [diff] [blame] | 331 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 332 | face.expressInterest(*makeInterest("/Hello/World/1", false, 50_ms), |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 333 | bind([] { BOOST_FAIL("Unexpected data"); }), |
| 334 | bind([] { BOOST_FAIL("Unexpected nack"); }), |
| 335 | bind([] { BOOST_FAIL("Unexpected timeout"); })); |
Ilya Moiseenko | 56b0bf8 | 2015-11-08 11:14:28 -0500 | [diff] [blame] | 336 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 337 | advanceClocks(10_ms); |
Ilya Moiseenko | 56b0bf8 | 2015-11-08 11:14:28 -0500 | [diff] [blame] | 338 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 339 | face.removeAllPendingInterests(); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 340 | advanceClocks(10_ms); |
Ilya Moiseenko | 56b0bf8 | 2015-11-08 11:14:28 -0500 | [diff] [blame] | 341 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 342 | BOOST_CHECK_EQUAL(face.getNPendingInterests(), 0); |
Ilya Moiseenko | 56b0bf8 | 2015-11-08 11:14:28 -0500 | [diff] [blame] | 343 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 344 | face.receive(*makeData("/Hello/World/0")); |
| 345 | face.receive(*makeData("/Hello/World/1")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 346 | advanceClocks(200_ms, 5); |
Ilya Moiseenko | 56b0bf8 | 2015-11-08 11:14:28 -0500 | [diff] [blame] | 347 | } |
| 348 | |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 349 | BOOST_AUTO_TEST_SUITE(Producer) |
| 350 | |
Junxiao Shi | e7bb6c8 | 2016-08-08 23:16:35 +0000 | [diff] [blame] | 351 | BOOST_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 Newberry | 4d261b6 | 2016-11-10 13:40:09 -0700 | [diff] [blame] | 362 | data.setTag(make_shared<lp::CongestionMarkTag>(1)); |
Junxiao Shi | e7bb6c8 | 2016-08-08 23:16:35 +0000 | [diff] [blame] | 363 | face.put(data); |
| 364 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 365 | advanceClocks(10_ms); |
Junxiao Shi | e7bb6c8 | 2016-08-08 23:16:35 +0000 | [diff] [blame] | 366 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 2); |
| 367 | BOOST_CHECK(face.sentData[0].getTag<lp::CachePolicyTag>() == nullptr); |
Eric Newberry | 4d261b6 | 2016-11-10 13:40:09 -0700 | [diff] [blame] | 368 | BOOST_CHECK(face.sentData[0].getTag<lp::CongestionMarkTag>() == nullptr); |
Junxiao Shi | e7bb6c8 | 2016-08-08 23:16:35 +0000 | [diff] [blame] | 369 | BOOST_CHECK(face.sentData[1].getTag<lp::CachePolicyTag>() != nullptr); |
Eric Newberry | 4d261b6 | 2016-11-10 13:40:09 -0700 | [diff] [blame] | 370 | BOOST_CHECK(face.sentData[1].getTag<lp::CongestionMarkTag>() != nullptr); |
Junxiao Shi | e7bb6c8 | 2016-08-08 23:16:35 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Junxiao Shi | b682891 | 2017-11-20 14:06:32 +0000 | [diff] [blame] | 373 | BOOST_AUTO_TEST_CASE(PutDataLoopback) |
| 374 | { |
| 375 | bool hasInterest1 = false, hasData = false; |
| 376 | |
| 377 | // first InterestFilter allows loopback and should receive Interest |
Davide Pesavento | 720e25c | 2019-07-14 01:33:52 -0400 | [diff] [blame] | 378 | face.setInterestFilter("/", [&] (const InterestFilter&, const Interest&) { |
Junxiao Shi | b682891 | 2017-11-20 14:06:32 +0000 | [diff] [blame] | 379 | 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 Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 386 | face.expressInterest(*makeInterest("/A", true), |
Junxiao Shi | b682891 | 2017-11-20 14:06:32 +0000 | [diff] [blame] | 387 | bind([&] { hasData = true; }), |
| 388 | bind([] { BOOST_FAIL("Unexpected nack"); }), |
| 389 | bind([] { BOOST_FAIL("Unexpected timeout"); })); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 390 | advanceClocks(1_ms); |
Junxiao Shi | b682891 | 2017-11-20 14:06:32 +0000 | [diff] [blame] | 391 | 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 Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 396 | advanceClocks(1_ms); |
Junxiao Shi | b682891 | 2017-11-20 14:06:32 +0000 | [diff] [blame] | 397 | BOOST_CHECK_EQUAL(hasData, true); |
| 398 | BOOST_CHECK_EQUAL(face.sentData.size(), 0); // do not spill Data to forwarder |
| 399 | } |
| 400 | |
Junxiao Shi | 859888f | 2017-09-12 14:29:16 +0000 | [diff] [blame] | 401 | BOOST_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 Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 411 | advanceClocks(10_ms); |
Junxiao Shi | 859888f | 2017-09-12 14:29:16 +0000 | [diff] [blame] | 412 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 413 | face.receive(*makeInterest("/A", true)); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 414 | advanceClocks(10_ms); |
Junxiao Shi | 859888f | 2017-09-12 14:29:16 +0000 | [diff] [blame] | 415 | 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 Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 423 | BOOST_AUTO_TEST_CASE(PutNack) |
| 424 | { |
Junxiao Shi | 79a7a16 | 2017-09-09 08:33:57 +0000 | [diff] [blame] | 425 | face.setInterestFilter("/", bind([]{})); // register one Interest destination so that face can accept Nacks |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 426 | advanceClocks(10_ms); |
Junxiao Shi | 79a7a16 | 2017-09-09 08:33:57 +0000 | [diff] [blame] | 427 | |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 428 | BOOST_CHECK_EQUAL(face.sentNacks.size(), 0); |
| 429 | |
Davide Pesavento | 287a7fa | 2020-10-01 22:46:43 -0400 | [diff] [blame] | 430 | face.put(makeNack(*makeInterest("/unsolicited", false, nullopt, 18645250), |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 431 | lp::NackReason::NO_ROUTE)); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 432 | advanceClocks(10_ms); |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 433 | BOOST_CHECK_EQUAL(face.sentNacks.size(), 0); // unsolicited Nack would not be sent |
Eric Newberry | 4d261b6 | 2016-11-10 13:40:09 -0700 | [diff] [blame] | 434 | |
Davide Pesavento | 287a7fa | 2020-10-01 22:46:43 -0400 | [diff] [blame] | 435 | auto interest1 = makeInterest("/Hello/World", false, nullopt, 14247162); |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 436 | face.receive(*interest1); |
Davide Pesavento | 287a7fa | 2020-10-01 22:46:43 -0400 | [diff] [blame] | 437 | auto interest2 = makeInterest("/another/prefix", false, nullopt, 92203002); |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 438 | face.receive(*interest2); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 439 | advanceClocks(10_ms); |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 440 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 441 | face.put(makeNack(*interest1, lp::NackReason::DUPLICATE)); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 442 | advanceClocks(10_ms); |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 443 | 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 Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 447 | auto nack = makeNack(*interest2, lp::NackReason::NO_ROUTE); |
Eric Newberry | 4d261b6 | 2016-11-10 13:40:09 -0700 | [diff] [blame] | 448 | nack.setTag(make_shared<lp::CongestionMarkTag>(1)); |
| 449 | face.put(nack); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 450 | advanceClocks(10_ms); |
Eric Newberry | 4d261b6 | 2016-11-10 13:40:09 -0700 | [diff] [blame] | 451 | BOOST_REQUIRE_EQUAL(face.sentNacks.size(), 2); |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 452 | BOOST_CHECK_EQUAL(face.sentNacks[1].getReason(), lp::NackReason::NO_ROUTE); |
Eric Newberry | 4d261b6 | 2016-11-10 13:40:09 -0700 | [diff] [blame] | 453 | BOOST_CHECK(face.sentNacks[1].getTag<lp::CongestionMarkTag>() != nullptr); |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 454 | } |
Ilya Moiseenko | 56b0bf8 | 2015-11-08 11:14:28 -0500 | [diff] [blame] | 455 | |
Junxiao Shi | 79a7a16 | 2017-09-09 08:33:57 +0000 | [diff] [blame] | 456 | BOOST_AUTO_TEST_CASE(PutMultipleNack) |
| 457 | { |
Junxiao Shi | 859888f | 2017-09-12 14:29:16 +0000 | [diff] [blame] | 458 | 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 Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 466 | advanceClocks(10_ms); |
Junxiao Shi | 79a7a16 | 2017-09-09 08:33:57 +0000 | [diff] [blame] | 467 | |
Davide Pesavento | 287a7fa | 2020-10-01 22:46:43 -0400 | [diff] [blame] | 468 | auto interest = makeInterest("/A", false, nullopt, 14333271); |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 469 | face.receive(*interest); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 470 | advanceClocks(10_ms); |
Junxiao Shi | 859888f | 2017-09-12 14:29:16 +0000 | [diff] [blame] | 471 | BOOST_CHECK(hasInterest1); |
| 472 | BOOST_CHECK(hasInterest2); |
Junxiao Shi | 79a7a16 | 2017-09-09 08:33:57 +0000 | [diff] [blame] | 473 | |
Junxiao Shi | 859888f | 2017-09-12 14:29:16 +0000 | [diff] [blame] | 474 | // Nack from first destination is received, should wait for a response from the other destination |
| 475 | BOOST_CHECK_EQUAL(face.sentNacks.size(), 0); |
Junxiao Shi | 79a7a16 | 2017-09-09 08:33:57 +0000 | [diff] [blame] | 476 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 477 | face.put(makeNack(*interest, lp::NackReason::NO_ROUTE)); // Nack from second destination |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 478 | advanceClocks(10_ms); |
Junxiao Shi | 859888f | 2017-09-12 14:29:16 +0000 | [diff] [blame] | 479 | BOOST_CHECK_EQUAL(face.sentNacks.size(), 1); // sending Nack after both destinations Nacked |
Junxiao Shi | 79a7a16 | 2017-09-09 08:33:57 +0000 | [diff] [blame] | 480 | BOOST_CHECK_EQUAL(face.sentNacks.at(0).getReason(), lp::NackReason::CONGESTION); // least severe reason |
| 481 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 482 | face.put(makeNack(*interest, lp::NackReason::DUPLICATE)); |
Junxiao Shi | 79a7a16 | 2017-09-09 08:33:57 +0000 | [diff] [blame] | 483 | BOOST_CHECK_EQUAL(face.sentNacks.size(), 1); // additional Nacks are ignored |
| 484 | } |
| 485 | |
Junxiao Shi | b682891 | 2017-11-20 14:06:32 +0000 | [diff] [blame] | 486 | BOOST_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 Pesavento | 287a7fa | 2020-10-01 22:46:43 -0400 | [diff] [blame] | 499 | auto interest = makeInterest("/A", false, nullopt, 28395852); |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 500 | face.expressInterest(*interest, |
Junxiao Shi | b682891 | 2017-11-20 14:06:32 +0000 | [diff] [blame] | 501 | 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 Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 507 | advanceClocks(1_ms); |
Junxiao Shi | b682891 | 2017-11-20 14:06:32 +0000 | [diff] [blame] | 508 | 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 Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 512 | face.receive(makeNack(*interest, lp::NackReason::NO_ROUTE)); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 513 | advanceClocks(1_ms); |
Junxiao Shi | b682891 | 2017-11-20 14:06:32 +0000 | [diff] [blame] | 514 | BOOST_CHECK_EQUAL(hasNack, true); |
| 515 | } |
| 516 | |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 517 | BOOST_AUTO_TEST_SUITE_END() // Producer |
| 518 | |
| 519 | BOOST_AUTO_TEST_SUITE(RegisterPrefix) |
| 520 | |
| 521 | BOOST_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 | |
| 529 | BOOST_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 Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 559 | auto face2 = make_unique<DummyClientFace>(m_io, m_keyChain); |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 560 | 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 Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 569 | auto face3 = make_unique<DummyClientFace>(m_io, m_keyChain); |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 570 | 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 | |
| 578 | BOOST_AUTO_TEST_SUITE_END() // RegisterPrefix |
| 579 | |
| 580 | BOOST_AUTO_TEST_SUITE(SetInterestFilter) |
| 581 | |
| 582 | BOOST_AUTO_TEST_CASE(SetAndCancel) |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 583 | { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 584 | size_t nInterests = 0; |
| 585 | size_t nRegs = 0; |
Davide Pesavento | 720e25c | 2019-07-14 01:33:52 -0400 | [diff] [blame] | 586 | auto hdl = face.setInterestFilter("/Hello/World", |
| 587 | bind([&nInterests] { ++nInterests; }), |
| 588 | bind([&nRegs] { ++nRegs; }), |
| 589 | bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); })); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 590 | advanceClocks(25_ms, 4); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 591 | BOOST_CHECK_EQUAL(nRegs, 1); |
| 592 | BOOST_CHECK_EQUAL(nInterests, 0); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 593 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 594 | face.receive(*makeInterest("/Hello/World/%21")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 595 | advanceClocks(25_ms, 4); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 596 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 597 | BOOST_CHECK_EQUAL(nRegs, 1); |
| 598 | BOOST_CHECK_EQUAL(nInterests, 1); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 599 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 600 | face.receive(*makeInterest("/Bye/World/%21")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 601 | advanceClocks(10000_ms, 10); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 602 | BOOST_CHECK_EQUAL(nInterests, 1); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 603 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 604 | face.receive(*makeInterest("/Hello/World/%21/2")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 605 | advanceClocks(25_ms, 4); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 606 | BOOST_CHECK_EQUAL(nInterests, 2); |
| 607 | |
| 608 | // removing filter |
Davide Pesavento | 720e25c | 2019-07-14 01:33:52 -0400 | [diff] [blame] | 609 | hdl.cancel(); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 610 | advanceClocks(25_ms, 4); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 611 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 612 | face.receive(*makeInterest("/Hello/World/%21/3")); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 613 | BOOST_CHECK_EQUAL(nInterests, 2); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 614 | } |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 615 | |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 616 | BOOST_AUTO_TEST_CASE(EmptyInterestCallback) |
Junxiao Shi | 76e0eb2 | 2016-08-08 05:54:10 +0000 | [diff] [blame] | 617 | { |
| 618 | face.setInterestFilter("/A", nullptr); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 619 | advanceClocks(1_ms); |
Junxiao Shi | 76e0eb2 | 2016-08-08 05:54:10 +0000 | [diff] [blame] | 620 | |
| 621 | BOOST_CHECK_NO_THROW(do { |
| 622 | face.receive(*makeInterest("/A/1")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 623 | advanceClocks(1_ms); |
Junxiao Shi | 76e0eb2 | 2016-08-08 05:54:10 +0000 | [diff] [blame] | 624 | } while (false)); |
| 625 | } |
| 626 | |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 627 | BOOST_AUTO_TEST_CASE(WithoutSuccessCallback) |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 628 | { |
| 629 | size_t nInterests = 0; |
Davide Pesavento | 720e25c | 2019-07-14 01:33:52 -0400 | [diff] [blame] | 630 | auto hdl = face.setInterestFilter("/Hello/World", |
| 631 | bind([&nInterests] { ++nInterests; }), |
| 632 | bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); })); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 633 | advanceClocks(25_ms, 4); |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 634 | BOOST_CHECK_EQUAL(nInterests, 0); |
| 635 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 636 | face.receive(*makeInterest("/Hello/World/%21")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 637 | advanceClocks(25_ms, 4); |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 638 | |
| 639 | BOOST_CHECK_EQUAL(nInterests, 1); |
| 640 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 641 | face.receive(*makeInterest("/Bye/World/%21")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 642 | advanceClocks(10000_ms, 10); |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 643 | BOOST_CHECK_EQUAL(nInterests, 1); |
| 644 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 645 | face.receive(*makeInterest("/Hello/World/%21/2")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 646 | advanceClocks(25_ms, 4); |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 647 | BOOST_CHECK_EQUAL(nInterests, 2); |
| 648 | |
| 649 | // removing filter |
Davide Pesavento | 720e25c | 2019-07-14 01:33:52 -0400 | [diff] [blame] | 650 | hdl.cancel(); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 651 | advanceClocks(25_ms, 4); |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 652 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 653 | face.receive(*makeInterest("/Hello/World/%21/3")); |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 654 | BOOST_CHECK_EQUAL(nInterests, 2); |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 655 | } |
| 656 | |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 657 | BOOST_FIXTURE_TEST_CASE(Failure, FaceFixture<NoPrefixRegReply>) |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 658 | { |
| 659 | // don't enable registration reply |
| 660 | size_t nRegFailed = 0; |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 661 | face.setInterestFilter("/Hello/World", |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 662 | bind([] { BOOST_FAIL("Unexpected Interest"); }), |
| 663 | bind([] { BOOST_FAIL("Unexpected success of setInterestFilter"); }), |
| 664 | bind([&nRegFailed] { ++nRegFailed; })); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 665 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 666 | advanceClocks(25_ms, 4); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 667 | BOOST_CHECK_EQUAL(nRegFailed, 0); |
| 668 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 669 | advanceClocks(2000_ms, 5); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 670 | BOOST_CHECK_EQUAL(nRegFailed, 1); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 671 | } |
| 672 | |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 673 | BOOST_FIXTURE_TEST_CASE(FailureWithoutSuccessCallback, FaceFixture<NoPrefixRegReply>) |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 674 | { |
| 675 | // don't enable registration reply |
| 676 | size_t nRegFailed = 0; |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 677 | face.setInterestFilter("/Hello/World", |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 678 | bind([] { BOOST_FAIL("Unexpected Interest"); }), |
| 679 | bind([&nRegFailed] { ++nRegFailed; })); |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 680 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 681 | advanceClocks(25_ms, 4); |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 682 | BOOST_CHECK_EQUAL(nRegFailed, 0); |
| 683 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 684 | advanceClocks(2000_ms, 5); |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 685 | BOOST_CHECK_EQUAL(nRegFailed, 1); |
| 686 | } |
| 687 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 688 | BOOST_AUTO_TEST_CASE(SimilarFilters) |
| 689 | { |
| 690 | size_t nInInterests1 = 0; |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 691 | face.setInterestFilter("/Hello/World", |
| 692 | bind([&nInInterests1] { ++nInInterests1; }), |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 693 | nullptr, |
| 694 | bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); })); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 695 | |
| 696 | size_t nInInterests2 = 0; |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 697 | face.setInterestFilter("/Hello", |
| 698 | bind([&nInInterests2] { ++nInInterests2; }), |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 699 | nullptr, |
| 700 | bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 701 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 702 | size_t nInInterests3 = 0; |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 703 | face.setInterestFilter("/Los/Angeles/Lakers", |
| 704 | bind([&nInInterests3] { ++nInInterests3; }), |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 705 | nullptr, |
| 706 | bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 707 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 708 | advanceClocks(25_ms, 4); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 709 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 710 | face.receive(*makeInterest("/Hello/World/%21")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 711 | advanceClocks(25_ms, 4); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 712 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 713 | BOOST_CHECK_EQUAL(nInInterests1, 1); |
| 714 | BOOST_CHECK_EQUAL(nInInterests2, 1); |
| 715 | BOOST_CHECK_EQUAL(nInInterests3, 0); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 716 | } |
| 717 | |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 718 | BOOST_AUTO_TEST_CASE(RegexFilter) |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 719 | { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 720 | size_t nInInterests = 0; |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 721 | face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"), |
| 722 | bind([&nInInterests] { ++nInInterests; }), |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 723 | nullptr, |
| 724 | bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 725 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 726 | advanceClocks(25_ms, 4); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 727 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 728 | face.receive(*makeInterest("/Hello/World/a")); // shouldn't match |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 729 | BOOST_CHECK_EQUAL(nInInterests, 0); |
| 730 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 731 | face.receive(*makeInterest("/Hello/World/a/b")); // should match |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 732 | BOOST_CHECK_EQUAL(nInInterests, 1); |
| 733 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 734 | face.receive(*makeInterest("/Hello/World/a/b/c")); // should match |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 735 | BOOST_CHECK_EQUAL(nInInterests, 2); |
| 736 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 737 | face.receive(*makeInterest("/Hello/World/a/b/d")); // should not match |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 738 | BOOST_CHECK_EQUAL(nInInterests, 2); |
| 739 | } |
| 740 | |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 741 | BOOST_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 | |
| 755 | BOOST_AUTO_TEST_CASE(RegexFilterAndRegisterPrefix) |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 756 | { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 757 | size_t nInInterests = 0; |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 758 | face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"), |
| 759 | bind([&nInInterests] { ++nInInterests; })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 760 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 761 | size_t nRegSuccesses = 0; |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 762 | face.registerPrefix("/Hello/World", |
| 763 | bind([&nRegSuccesses] { ++nRegSuccesses; }), |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 764 | bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 765 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 766 | advanceClocks(25_ms, 4); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 767 | BOOST_CHECK_EQUAL(nRegSuccesses, 1); |
| 768 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 769 | face.receive(*makeInterest("/Hello/World/a")); // shouldn't match |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 770 | BOOST_CHECK_EQUAL(nInInterests, 0); |
| 771 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 772 | face.receive(*makeInterest("/Hello/World/a/b")); // should match |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 773 | BOOST_CHECK_EQUAL(nInInterests, 1); |
| 774 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 775 | face.receive(*makeInterest("/Hello/World/a/b/c")); // should match |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 776 | BOOST_CHECK_EQUAL(nInInterests, 2); |
| 777 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 778 | face.receive(*makeInterest("/Hello/World/a/b/d")); // should not match |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 779 | BOOST_CHECK_EQUAL(nInInterests, 2); |
| 780 | } |
| 781 | |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 782 | BOOST_FIXTURE_TEST_CASE(WithoutRegisterPrefix, FaceFixture<NoPrefixRegReply>) // Bug 2318 |
Junxiao Shi | a1ea506 | 2014-12-27 22:33:39 -0700 | [diff] [blame] | 783 | { |
| 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 Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 788 | face.setInterestFilter(Name("/"), bind([&hit] { ++hit; })); |
| 789 | face.processEvents(time::milliseconds(-1)); |
Junxiao Shi | a1ea506 | 2014-12-27 22:33:39 -0700 | [diff] [blame] | 790 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 791 | face.receive(*makeInterest("/A")); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 792 | face.processEvents(time::milliseconds(-1)); |
Junxiao Shi | a1ea506 | 2014-12-27 22:33:39 -0700 | [diff] [blame] | 793 | |
| 794 | BOOST_CHECK_EQUAL(hit, 1); |
| 795 | } |
| 796 | |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 797 | BOOST_AUTO_TEST_CASE(Handle) |
Junxiao Shi | 60aaef0 | 2019-01-14 04:59:30 +0000 | [diff] [blame] | 798 | { |
| 799 | int hit = 0; |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 800 | InterestFilterHandle hdl = face.setInterestFilter(Name("/"), bind([&hit] { ++hit; })); |
Junxiao Shi | 60aaef0 | 2019-01-14 04:59:30 +0000 | [diff] [blame] | 801 | 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 Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 813 | |
| 814 | // cancel after destructing face |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 815 | auto face2 = make_unique<DummyClientFace>(m_io, m_keyChain); |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 816 | 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 Shi | 60aaef0 | 2019-01-14 04:59:30 +0000 | [diff] [blame] | 822 | } |
| 823 | |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 824 | BOOST_AUTO_TEST_SUITE_END() // SetInterestFilter |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 825 | |
Alexander Afanasyev | 8e15854 | 2014-11-18 00:47:18 -0500 | [diff] [blame] | 826 | BOOST_AUTO_TEST_CASE(ProcessEvents) |
| 827 | { |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 828 | face.processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside |
Alexander Afanasyev | 8e15854 | 2014-11-18 00:47:18 -0500 | [diff] [blame] | 829 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 830 | size_t nRegSuccesses = 0; |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 831 | face.registerPrefix("/Hello/World", |
| 832 | bind([&nRegSuccesses] { ++nRegSuccesses; }), |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 833 | bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); })); |
Alexander Afanasyev | 8e15854 | 2014-11-18 00:47:18 -0500 | [diff] [blame] | 834 | |
| 835 | // io_service::poll() without reset |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 836 | face.getIoService().poll(); |
Alexander Afanasyev | 8e15854 | 2014-11-18 00:47:18 -0500 | [diff] [blame] | 837 | BOOST_CHECK_EQUAL(nRegSuccesses, 0); |
| 838 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 839 | face.processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside |
Alexander Afanasyev | 8e15854 | 2014-11-18 00:47:18 -0500 | [diff] [blame] | 840 | BOOST_CHECK_EQUAL(nRegSuccesses, 1); |
| 841 | } |
| 842 | |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 843 | BOOST_AUTO_TEST_CASE(DestroyWithoutProcessEvents) // Bug 3248 |
| 844 | { |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 845 | auto face2 = make_unique<Face>(m_io); |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 846 | face2.reset(); |
| 847 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 848 | m_io.poll(); // should not crash |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 849 | |
| 850 | // avoid "test case [...] did not check any assertions" message from Boost.Test |
| 851 | BOOST_CHECK(true); |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 852 | } |
| 853 | |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 854 | BOOST_AUTO_TEST_SUITE(Transport) |
| 855 | |
| 856 | using ndn::Transport; |
| 857 | |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 858 | struct PibDirWithDefaultTpm |
| 859 | { |
| 860 | const std::string PATH = "build/keys-with-default-tpm"; |
| 861 | }; |
| 862 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 863 | BOOST_FIXTURE_TEST_CASE(FaceTransport, IoKeyChainFixture) |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame] | 864 | { |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame] | 865 | BOOST_CHECK(Face().getTransport() != nullptr); |
| 866 | |
Alexander Afanasyev | bb64c17 | 2015-12-29 20:32:45 -0800 | [diff] [blame] | 867 | BOOST_CHECK(Face(shared_ptr<Transport>()).getTransport() != nullptr); |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 868 | BOOST_CHECK(Face(shared_ptr<Transport>(), m_io).getTransport() != nullptr); |
| 869 | BOOST_CHECK(Face(shared_ptr<Transport>(), m_io, m_keyChain).getTransport() != nullptr); |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame] | 870 | |
| 871 | auto transport = make_shared<TcpTransport>("localhost", "6363"); // no real io operations will be scheduled |
| 872 | BOOST_CHECK(Face(transport).getTransport() == transport); |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 873 | BOOST_CHECK(Face(transport, m_io).getTransport() == transport); |
| 874 | BOOST_CHECK(Face(transport, m_io, m_keyChain).getTransport() == transport); |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame] | 875 | } |
| 876 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 877 | class WithEnv |
Alexander Afanasyev | cf49055 | 2016-06-27 22:51:36 -0700 | [diff] [blame] | 878 | { |
| 879 | public: |
| 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 Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 891 | setenv("NDN_CLIENT_TRANSPORT", faceUri.data(), true); |
Alexander Afanasyev | cf49055 | 2016-06-27 22:51:36 -0700 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | ~WithEnv() |
| 895 | { |
| 896 | if (!m_oldTransport.empty()) { |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 897 | setenv("NDN_CLIENT_TRANSPORT", m_oldTransport.data(), true); |
Alexander Afanasyev | cf49055 | 2016-06-27 22:51:36 -0700 | [diff] [blame] | 898 | } |
| 899 | else { |
| 900 | unsetenv("NDN_CLIENT_TRANSPORT"); |
| 901 | } |
| 902 | } |
| 903 | |
| 904 | private: |
| 905 | std::string m_oldTransport; |
| 906 | }; |
| 907 | |
| 908 | class WithConfig : private TestHomeFixture<DefaultPibDir> |
| 909 | { |
| 910 | public: |
| 911 | void |
| 912 | configure(const std::string& faceUri) |
| 913 | { |
| 914 | createClientConf({"transport=" + faceUri}); |
| 915 | } |
| 916 | }; |
| 917 | |
| 918 | class WithEnvAndConfig : public WithEnv, public WithConfig |
| 919 | { |
| 920 | }; |
| 921 | |
| 922 | typedef boost::mpl::vector<WithEnv, WithConfig> ConfigOptions; |
| 923 | |
| 924 | BOOST_FIXTURE_TEST_CASE(NoConfig, WithEnvAndConfig) // fixture configures test HOME and PIB/TPM path |
Alexander Afanasyev | 57e0036 | 2016-06-23 13:22:54 -0700 | [diff] [blame] | 925 | { |
| 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 Afanasyev | 57e0036 | 2016-06-23 13:22:54 -0700 | [diff] [blame] | 929 | } |
| 930 | |
Alexander Afanasyev | cf49055 | 2016-06-27 22:51:36 -0700 | [diff] [blame] | 931 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(Unix, T, ConfigOptions, T) |
| 932 | { |
| 933 | this->configure("unix://some/path"); |
Alexander Afanasyev | 57e0036 | 2016-06-23 13:22:54 -0700 | [diff] [blame] | 934 | |
Alexander Afanasyev | cf49055 | 2016-06-27 22:51:36 -0700 | [diff] [blame] | 935 | 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 | |
| 940 | BOOST_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 | |
| 949 | BOOST_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 | |
| 956 | BOOST_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 | |
| 963 | BOOST_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 | |
| 973 | BOOST_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 Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 984 | BOOST_AUTO_TEST_SUITE_END() // Transport |
| 985 | |
Alexander Afanasyev | 57e0036 | 2016-06-23 13:22:54 -0700 | [diff] [blame] | 986 | BOOST_AUTO_TEST_SUITE_END() // TestFace |
| 987 | |
| 988 | } // namespace tests |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 989 | } // namespace ndn |