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 | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2020 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" |
| 26 | #include "ndn-cxx/util/dummy-client-face.hpp" |
| 27 | #include "ndn-cxx/util/scheduler.hpp" |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 28 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 29 | #include "tests/boost-test.hpp" |
| 30 | #include "tests/make-interest-data.hpp" |
| 31 | #include "tests/unit/identity-management-time-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; |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 39 | |
Junxiao Shi | ec475a7 | 2019-01-13 21:53:55 +0000 | [diff] [blame] | 40 | struct WantPrefixRegReply; |
| 41 | struct NoPrefixRegReply; |
| 42 | |
| 43 | template<typename PrefixRegReply = WantPrefixRegReply> |
Alexander Afanasyev | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame] | 44 | class FaceFixture : public IdentityManagementTimeFixture |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 45 | { |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 46 | protected: |
Junxiao Shi | ec475a7 | 2019-01-13 21:53:55 +0000 | [diff] [blame] | 47 | FaceFixture() |
| 48 | : face(io, m_keyChain, {true, !std::is_same<PrefixRegReply, NoPrefixRegReply>::value}) |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 49 | { |
Junxiao Shi | ec475a7 | 2019-01-13 21:53:55 +0000 | [diff] [blame] | 50 | static_assert(std::is_same<PrefixRegReply, WantPrefixRegReply>::value || |
| 51 | std::is_same<PrefixRegReply, NoPrefixRegReply>::value, ""); |
| 52 | } |
| 53 | |
| 54 | /** \brief Execute a prefix registration, and optionally check the name in callback. |
| 55 | * \return whether the prefix registration succeeded. |
| 56 | */ |
| 57 | bool |
| 58 | runPrefixReg(function<void(const RegisterPrefixSuccessCallback& success, |
| 59 | const RegisterPrefixFailureCallback& failure)> f) |
| 60 | { |
| 61 | boost::logic::tribool result = boost::logic::indeterminate; |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 62 | f([&] (auto) { result = true; }, |
| 63 | [&] (auto, auto) { result = false; }); |
Junxiao Shi | ec475a7 | 2019-01-13 21:53:55 +0000 | [diff] [blame] | 64 | |
| 65 | advanceClocks(1_ms); |
| 66 | BOOST_REQUIRE(!boost::logic::indeterminate(result)); |
| 67 | return static_cast<bool>(result); |
| 68 | } |
| 69 | |
| 70 | /** \brief Execute a prefix unregistration, and optionally check the name in callback. |
| 71 | * \return whether the prefix unregistration succeeded. |
| 72 | */ |
| 73 | bool |
| 74 | runPrefixUnreg(function<void(const UnregisterPrefixSuccessCallback& success, |
| 75 | const UnregisterPrefixFailureCallback& failure)> f) |
| 76 | { |
| 77 | boost::logic::tribool result = boost::logic::indeterminate; |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 78 | f([&] { result = true; }, |
| 79 | [&] (auto) { result = false; }); |
Junxiao Shi | ec475a7 | 2019-01-13 21:53:55 +0000 | [diff] [blame] | 80 | |
| 81 | advanceClocks(1_ms); |
| 82 | BOOST_REQUIRE(!boost::logic::indeterminate(result)); |
| 83 | return static_cast<bool>(result); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 84 | } |
| 85 | |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 86 | protected: |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 87 | DummyClientFace face; |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 88 | }; |
| 89 | |
Junxiao Shi | ec475a7 | 2019-01-13 21:53:55 +0000 | [diff] [blame] | 90 | BOOST_FIXTURE_TEST_SUITE(TestFace, FaceFixture<>) |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 91 | |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 92 | BOOST_AUTO_TEST_SUITE(ExpressInterest) |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 93 | |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 94 | BOOST_AUTO_TEST_CASE(ReplyData) |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 95 | { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 96 | size_t nData = 0; |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 97 | face.expressInterest(*makeInterest("/Hello/World", true, 50_ms), |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 98 | [&] (const Interest& i, const Data& d) { |
| 99 | BOOST_CHECK(i.getName().isPrefixOf(d.getName())); |
| 100 | BOOST_CHECK_EQUAL(i.getName(), "/Hello/World"); |
| 101 | ++nData; |
| 102 | }, |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 103 | bind([] { BOOST_FAIL("Unexpected Nack"); }), |
| 104 | bind([] { BOOST_FAIL("Unexpected timeout"); })); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 105 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 106 | advanceClocks(40_ms); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 107 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 108 | face.receive(*makeData("/Bye/World/a")); |
| 109 | face.receive(*makeData("/Hello/World/a")); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 110 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 111 | advanceClocks(50_ms, 2); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 112 | |
| 113 | BOOST_CHECK_EQUAL(nData, 1); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 114 | BOOST_CHECK_EQUAL(face.sentInterests.size(), 1); |
| 115 | BOOST_CHECK_EQUAL(face.sentData.size(), 0); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 116 | |
| 117 | size_t nTimeouts = 0; |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 118 | face.expressInterest(*makeInterest("/Hello/World/a/2", false, 50_ms), |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 119 | bind([]{}), |
| 120 | bind([]{}), |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 121 | bind([&nTimeouts] { ++nTimeouts; })); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 122 | advanceClocks(200_ms, 5); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 123 | BOOST_CHECK_EQUAL(nTimeouts, 1); |
| 124 | } |
| 125 | |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 126 | BOOST_AUTO_TEST_CASE(MultipleData) |
Alexander Afanasyev | 1013fd0 | 2017-01-03 13:19:03 -0800 | [diff] [blame] | 127 | { |
| 128 | size_t nData = 0; |
| 129 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 130 | face.expressInterest(*makeInterest("/Hello/World", true, 50_ms), |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 131 | [&] (const auto&, const auto&) { ++nData; }, |
Alexander Afanasyev | 1013fd0 | 2017-01-03 13:19:03 -0800 | [diff] [blame] | 132 | bind([] { BOOST_FAIL("Unexpected Nack"); }), |
| 133 | bind([] { BOOST_FAIL("Unexpected timeout"); })); |
| 134 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 135 | face.expressInterest(*makeInterest("/Hello/World/a", true, 50_ms), |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 136 | [&] (const auto&, const auto&) { ++nData; }, |
Alexander Afanasyev | 1013fd0 | 2017-01-03 13:19:03 -0800 | [diff] [blame] | 137 | bind([] { BOOST_FAIL("Unexpected Nack"); }), |
| 138 | bind([] { BOOST_FAIL("Unexpected timeout"); })); |
| 139 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 140 | advanceClocks(40_ms); |
Alexander Afanasyev | 1013fd0 | 2017-01-03 13:19:03 -0800 | [diff] [blame] | 141 | |
| 142 | face.receive(*makeData("/Hello/World/a/b")); |
| 143 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 144 | advanceClocks(50_ms, 2); |
Alexander Afanasyev | 1013fd0 | 2017-01-03 13:19:03 -0800 | [diff] [blame] | 145 | |
| 146 | BOOST_CHECK_EQUAL(nData, 2); |
| 147 | BOOST_CHECK_EQUAL(face.sentInterests.size(), 2); |
| 148 | BOOST_CHECK_EQUAL(face.sentData.size(), 0); |
| 149 | } |
| 150 | |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 151 | BOOST_AUTO_TEST_CASE(EmptyDataCallback) |
Junxiao Shi | 76e0eb2 | 2016-08-08 05:54:10 +0000 | [diff] [blame] | 152 | { |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 153 | face.expressInterest(*makeInterest("/Hello/World", true), |
Junxiao Shi | 76e0eb2 | 2016-08-08 05:54:10 +0000 | [diff] [blame] | 154 | nullptr, |
| 155 | bind([] { BOOST_FAIL("Unexpected Nack"); }), |
| 156 | bind([] { BOOST_FAIL("Unexpected timeout"); })); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 157 | advanceClocks(1_ms); |
Junxiao Shi | 76e0eb2 | 2016-08-08 05:54:10 +0000 | [diff] [blame] | 158 | |
| 159 | BOOST_CHECK_NO_THROW(do { |
| 160 | face.receive(*makeData("/Hello/World/a")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 161 | advanceClocks(1_ms); |
Junxiao Shi | 76e0eb2 | 2016-08-08 05:54:10 +0000 | [diff] [blame] | 162 | } while (false)); |
| 163 | } |
| 164 | |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 165 | BOOST_AUTO_TEST_CASE(Timeout) |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 166 | { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 167 | size_t nTimeouts = 0; |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 168 | face.expressInterest(*makeInterest("/Hello/World", false, 50_ms), |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 169 | bind([] { BOOST_FAIL("Unexpected Data"); }), |
| 170 | bind([] { BOOST_FAIL("Unexpected Nack"); }), |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 171 | [&nTimeouts] (const Interest& i) { |
| 172 | BOOST_CHECK_EQUAL(i.getName(), "/Hello/World"); |
| 173 | ++nTimeouts; |
| 174 | }); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 175 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 176 | advanceClocks(200_ms, 5); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 177 | |
| 178 | BOOST_CHECK_EQUAL(nTimeouts, 1); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 179 | BOOST_CHECK_EQUAL(face.sentInterests.size(), 1); |
| 180 | BOOST_CHECK_EQUAL(face.sentData.size(), 0); |
| 181 | BOOST_CHECK_EQUAL(face.sentNacks.size(), 0); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 184 | BOOST_AUTO_TEST_CASE(EmptyTimeoutCallback) |
Junxiao Shi | 76e0eb2 | 2016-08-08 05:54:10 +0000 | [diff] [blame] | 185 | { |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 186 | face.expressInterest(*makeInterest("/Hello/World", false, 50_ms), |
Junxiao Shi | 76e0eb2 | 2016-08-08 05:54:10 +0000 | [diff] [blame] | 187 | bind([] { BOOST_FAIL("Unexpected Data"); }), |
| 188 | bind([] { BOOST_FAIL("Unexpected Nack"); }), |
| 189 | nullptr); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 190 | advanceClocks(40_ms); |
Junxiao Shi | 76e0eb2 | 2016-08-08 05:54:10 +0000 | [diff] [blame] | 191 | |
| 192 | BOOST_CHECK_NO_THROW(do { |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 193 | advanceClocks(6_ms, 2); |
Junxiao Shi | 76e0eb2 | 2016-08-08 05:54:10 +0000 | [diff] [blame] | 194 | } while (false)); |
| 195 | } |
| 196 | |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 197 | BOOST_AUTO_TEST_CASE(ReplyNack) |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 198 | { |
| 199 | size_t nNacks = 0; |
| 200 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 201 | auto interest = makeInterest("/Hello/World", false, 50_ms); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 202 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 203 | face.expressInterest(*interest, |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 204 | bind([] { BOOST_FAIL("Unexpected Data"); }), |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 205 | [&] (const Interest& i, const lp::Nack& n) { |
| 206 | BOOST_CHECK(i.getName().isPrefixOf(n.getInterest().getName())); |
| 207 | BOOST_CHECK_EQUAL(i.getName(), "/Hello/World"); |
| 208 | BOOST_CHECK_EQUAL(n.getReason(), lp::NackReason::DUPLICATE); |
| 209 | ++nNacks; |
| 210 | }, |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 211 | bind([] { BOOST_FAIL("Unexpected timeout"); })); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 212 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 213 | advanceClocks(40_ms); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 214 | |
Junxiao Shi | f5b5ae2 | 2016-08-08 05:54:41 +0000 | [diff] [blame] | 215 | face.receive(makeNack(face.sentInterests.at(0), lp::NackReason::DUPLICATE)); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 216 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 217 | advanceClocks(50_ms, 2); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 218 | |
| 219 | BOOST_CHECK_EQUAL(nNacks, 1); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 220 | BOOST_CHECK_EQUAL(face.sentInterests.size(), 1); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 223 | BOOST_AUTO_TEST_CASE(MultipleNacks) |
Alexander Afanasyev | 1013fd0 | 2017-01-03 13:19:03 -0800 | [diff] [blame] | 224 | { |
| 225 | size_t nNacks = 0; |
| 226 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 227 | auto interest = makeInterest("/Hello/World", false, 50_ms, 1); |
| 228 | face.expressInterest(*interest, |
Alexander Afanasyev | 1013fd0 | 2017-01-03 13:19:03 -0800 | [diff] [blame] | 229 | bind([] { BOOST_FAIL("Unexpected Data"); }), |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 230 | [&] (const auto&, const auto&) { ++nNacks; }, |
Alexander Afanasyev | 1013fd0 | 2017-01-03 13:19:03 -0800 | [diff] [blame] | 231 | bind([] { BOOST_FAIL("Unexpected timeout"); })); |
| 232 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 233 | interest->setNonce(2); |
| 234 | face.expressInterest(*interest, |
Alexander Afanasyev | 1013fd0 | 2017-01-03 13:19:03 -0800 | [diff] [blame] | 235 | bind([] { BOOST_FAIL("Unexpected Data"); }), |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 236 | [&] (const auto&, const auto&) { ++nNacks; }, |
Alexander Afanasyev | 1013fd0 | 2017-01-03 13:19:03 -0800 | [diff] [blame] | 237 | bind([] { BOOST_FAIL("Unexpected timeout"); })); |
| 238 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 239 | advanceClocks(40_ms); |
Alexander Afanasyev | 1013fd0 | 2017-01-03 13:19:03 -0800 | [diff] [blame] | 240 | |
| 241 | face.receive(makeNack(face.sentInterests.at(1), lp::NackReason::DUPLICATE)); |
| 242 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 243 | advanceClocks(50_ms, 2); |
Alexander Afanasyev | 1013fd0 | 2017-01-03 13:19:03 -0800 | [diff] [blame] | 244 | |
| 245 | BOOST_CHECK_EQUAL(nNacks, 2); |
| 246 | BOOST_CHECK_EQUAL(face.sentInterests.size(), 2); |
| 247 | } |
| 248 | |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 249 | BOOST_AUTO_TEST_CASE(EmptyNackCallback) |
Junxiao Shi | 76e0eb2 | 2016-08-08 05:54:10 +0000 | [diff] [blame] | 250 | { |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 251 | face.expressInterest(*makeInterest("/Hello/World"), |
Junxiao Shi | 76e0eb2 | 2016-08-08 05:54:10 +0000 | [diff] [blame] | 252 | bind([] { BOOST_FAIL("Unexpected Data"); }), |
| 253 | nullptr, |
| 254 | bind([] { BOOST_FAIL("Unexpected timeout"); })); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 255 | advanceClocks(1_ms); |
Junxiao Shi | 76e0eb2 | 2016-08-08 05:54:10 +0000 | [diff] [blame] | 256 | |
| 257 | BOOST_CHECK_NO_THROW(do { |
| 258 | face.receive(makeNack(face.sentInterests.at(0), lp::NackReason::DUPLICATE)); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 259 | advanceClocks(1_ms); |
Junxiao Shi | 76e0eb2 | 2016-08-08 05:54:10 +0000 | [diff] [blame] | 260 | } while (false)); |
| 261 | } |
| 262 | |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 263 | BOOST_AUTO_TEST_CASE(PutDataFromDataCallback) // Bug 4596 |
| 264 | { |
| 265 | face.expressInterest(*makeInterest("/localhost/notification/1"), |
| 266 | [&] (const auto&, const auto&) { |
| 267 | face.put(*makeData("/chronosync/sampleDigest/1")); |
| 268 | }, nullptr, nullptr); |
| 269 | advanceClocks(10_ms); |
| 270 | BOOST_CHECK_EQUAL(face.sentInterests.back().getName(), "/localhost/notification/1"); |
| 271 | |
| 272 | face.receive(*makeInterest("/chronosync/sampleDigest", true)); |
| 273 | advanceClocks(10_ms); |
| 274 | |
| 275 | face.put(*makeData("/localhost/notification/1")); |
| 276 | advanceClocks(10_ms); |
| 277 | BOOST_CHECK_EQUAL(face.sentData.back().getName(), "/chronosync/sampleDigest/1"); |
| 278 | } |
| 279 | |
| 280 | BOOST_AUTO_TEST_CASE(DestroyWithPendingInterest) |
| 281 | { |
| 282 | auto face2 = make_unique<DummyClientFace>(io, m_keyChain); |
| 283 | face2->expressInterest(*makeInterest("/Hello/World", false, 50_ms), |
| 284 | nullptr, nullptr, nullptr); |
| 285 | advanceClocks(50_ms, 2); |
| 286 | face2.reset(); |
| 287 | |
| 288 | advanceClocks(50_ms, 2); // should not crash - Bug 2518 |
| 289 | |
| 290 | // avoid "test case [...] did not check any assertions" message from Boost.Test |
| 291 | BOOST_CHECK(true); |
| 292 | } |
| 293 | |
| 294 | BOOST_AUTO_TEST_CASE(Handle) |
Junxiao Shi | 80609d4 | 2019-01-29 18:15:22 +0000 | [diff] [blame] | 295 | { |
| 296 | auto hdl = face.expressInterest(*makeInterest("/Hello/World", true, 50_ms), |
| 297 | bind([] { BOOST_FAIL("Unexpected data"); }), |
| 298 | bind([] { BOOST_FAIL("Unexpected nack"); }), |
| 299 | bind([] { BOOST_FAIL("Unexpected timeout"); })); |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 300 | advanceClocks(1_ms); |
Junxiao Shi | 80609d4 | 2019-01-29 18:15:22 +0000 | [diff] [blame] | 301 | hdl.cancel(); |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 302 | advanceClocks(1_ms); |
Junxiao Shi | 80609d4 | 2019-01-29 18:15:22 +0000 | [diff] [blame] | 303 | face.receive(*makeData("/Hello/World/%21")); |
| 304 | advanceClocks(200_ms, 5); |
| 305 | |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 306 | // cancel after destructing face |
| 307 | auto face2 = make_unique<DummyClientFace>(io, m_keyChain); |
| 308 | auto hdl2 = face2->expressInterest(*makeInterest("/Hello/World", true, 50_ms), |
| 309 | bind([] { BOOST_FAIL("Unexpected data"); }), |
| 310 | bind([] { BOOST_FAIL("Unexpected nack"); }), |
| 311 | bind([] { BOOST_FAIL("Unexpected timeout"); })); |
| 312 | advanceClocks(1_ms); |
| 313 | face2.reset(); |
| 314 | advanceClocks(1_ms); |
| 315 | hdl2.cancel(); // should not crash |
| 316 | advanceClocks(1_ms); |
| 317 | |
Junxiao Shi | 80609d4 | 2019-01-29 18:15:22 +0000 | [diff] [blame] | 318 | // avoid "test case [...] did not check any assertions" message from Boost.Test |
| 319 | BOOST_CHECK(true); |
| 320 | } |
| 321 | |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 322 | BOOST_AUTO_TEST_SUITE_END() // ExpressInterest |
| 323 | |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 324 | BOOST_AUTO_TEST_CASE(RemoveAllPendingInterests) |
Ilya Moiseenko | 56b0bf8 | 2015-11-08 11:14:28 -0500 | [diff] [blame] | 325 | { |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 326 | face.expressInterest(*makeInterest("/Hello/World/0", false, 50_ms), |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 327 | bind([] { BOOST_FAIL("Unexpected data"); }), |
| 328 | bind([] { BOOST_FAIL("Unexpected nack"); }), |
| 329 | bind([] { BOOST_FAIL("Unexpected timeout"); })); |
Ilya Moiseenko | 56b0bf8 | 2015-11-08 11:14:28 -0500 | [diff] [blame] | 330 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 331 | face.expressInterest(*makeInterest("/Hello/World/1", false, 50_ms), |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 332 | bind([] { BOOST_FAIL("Unexpected data"); }), |
| 333 | bind([] { BOOST_FAIL("Unexpected nack"); }), |
| 334 | bind([] { BOOST_FAIL("Unexpected timeout"); })); |
Ilya Moiseenko | 56b0bf8 | 2015-11-08 11:14:28 -0500 | [diff] [blame] | 335 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 336 | advanceClocks(10_ms); |
Ilya Moiseenko | 56b0bf8 | 2015-11-08 11:14:28 -0500 | [diff] [blame] | 337 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 338 | face.removeAllPendingInterests(); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 339 | advanceClocks(10_ms); |
Ilya Moiseenko | 56b0bf8 | 2015-11-08 11:14:28 -0500 | [diff] [blame] | 340 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 341 | BOOST_CHECK_EQUAL(face.getNPendingInterests(), 0); |
Ilya Moiseenko | 56b0bf8 | 2015-11-08 11:14:28 -0500 | [diff] [blame] | 342 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 343 | face.receive(*makeData("/Hello/World/0")); |
| 344 | face.receive(*makeData("/Hello/World/1")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 345 | advanceClocks(200_ms, 5); |
Ilya Moiseenko | 56b0bf8 | 2015-11-08 11:14:28 -0500 | [diff] [blame] | 346 | } |
| 347 | |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 348 | BOOST_AUTO_TEST_SUITE(Producer) |
| 349 | |
Junxiao Shi | e7bb6c8 | 2016-08-08 23:16:35 +0000 | [diff] [blame] | 350 | BOOST_AUTO_TEST_CASE(PutData) |
| 351 | { |
| 352 | BOOST_CHECK_EQUAL(face.sentData.size(), 0); |
| 353 | |
| 354 | Data data("/4g7xxcuEow/KFvK5Kf2m"); |
| 355 | signData(data); |
| 356 | face.put(data); |
| 357 | |
| 358 | lp::CachePolicy cachePolicy; |
| 359 | cachePolicy.setPolicy(lp::CachePolicyType::NO_CACHE); |
| 360 | data.setTag(make_shared<lp::CachePolicyTag>(cachePolicy)); |
Eric Newberry | 4d261b6 | 2016-11-10 13:40:09 -0700 | [diff] [blame] | 361 | data.setTag(make_shared<lp::CongestionMarkTag>(1)); |
Junxiao Shi | e7bb6c8 | 2016-08-08 23:16:35 +0000 | [diff] [blame] | 362 | face.put(data); |
| 363 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 364 | advanceClocks(10_ms); |
Junxiao Shi | e7bb6c8 | 2016-08-08 23:16:35 +0000 | [diff] [blame] | 365 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 2); |
| 366 | BOOST_CHECK(face.sentData[0].getTag<lp::CachePolicyTag>() == nullptr); |
Eric Newberry | 4d261b6 | 2016-11-10 13:40:09 -0700 | [diff] [blame] | 367 | BOOST_CHECK(face.sentData[0].getTag<lp::CongestionMarkTag>() == nullptr); |
Junxiao Shi | e7bb6c8 | 2016-08-08 23:16:35 +0000 | [diff] [blame] | 368 | BOOST_CHECK(face.sentData[1].getTag<lp::CachePolicyTag>() != nullptr); |
Eric Newberry | 4d261b6 | 2016-11-10 13:40:09 -0700 | [diff] [blame] | 369 | BOOST_CHECK(face.sentData[1].getTag<lp::CongestionMarkTag>() != nullptr); |
Junxiao Shi | e7bb6c8 | 2016-08-08 23:16:35 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Junxiao Shi | b682891 | 2017-11-20 14:06:32 +0000 | [diff] [blame] | 372 | BOOST_AUTO_TEST_CASE(PutDataLoopback) |
| 373 | { |
| 374 | bool hasInterest1 = false, hasData = false; |
| 375 | |
| 376 | // first InterestFilter allows loopback and should receive Interest |
Davide Pesavento | 720e25c | 2019-07-14 01:33:52 -0400 | [diff] [blame] | 377 | face.setInterestFilter("/", [&] (const InterestFilter&, const Interest&) { |
Junxiao Shi | b682891 | 2017-11-20 14:06:32 +0000 | [diff] [blame] | 378 | hasInterest1 = true; |
| 379 | // do not respond with Data right away, so Face must send Interest to forwarder |
| 380 | }); |
| 381 | // second InterestFilter disallows loopback and should not receive Interest |
| 382 | face.setInterestFilter(InterestFilter("/").allowLoopback(false), |
| 383 | bind([] { BOOST_ERROR("Unexpected Interest on second InterestFilter"); })); |
| 384 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 385 | face.expressInterest(*makeInterest("/A", true), |
Junxiao Shi | b682891 | 2017-11-20 14:06:32 +0000 | [diff] [blame] | 386 | bind([&] { hasData = true; }), |
| 387 | bind([] { BOOST_FAIL("Unexpected nack"); }), |
| 388 | bind([] { BOOST_FAIL("Unexpected timeout"); })); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 389 | advanceClocks(1_ms); |
Junxiao Shi | b682891 | 2017-11-20 14:06:32 +0000 | [diff] [blame] | 390 | BOOST_CHECK_EQUAL(hasInterest1, true); // Interest looped back |
| 391 | BOOST_CHECK_EQUAL(face.sentInterests.size(), 1); // Interest sent to forwarder |
| 392 | BOOST_CHECK_EQUAL(hasData, false); // waiting for Data |
| 393 | |
| 394 | face.put(*makeData("/A/B")); // first InterestFilter responds with Data |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 395 | advanceClocks(1_ms); |
Junxiao Shi | b682891 | 2017-11-20 14:06:32 +0000 | [diff] [blame] | 396 | BOOST_CHECK_EQUAL(hasData, true); |
| 397 | BOOST_CHECK_EQUAL(face.sentData.size(), 0); // do not spill Data to forwarder |
| 398 | } |
| 399 | |
Junxiao Shi | 859888f | 2017-09-12 14:29:16 +0000 | [diff] [blame] | 400 | BOOST_AUTO_TEST_CASE(PutMultipleData) |
| 401 | { |
| 402 | bool hasInterest1 = false; |
| 403 | // register two Interest destinations |
| 404 | face.setInterestFilter("/", bind([&] { |
| 405 | hasInterest1 = true; |
| 406 | // sending Data right away from the first destination, don't care whether Interest goes to second destination |
| 407 | face.put(*makeData("/A/B")); |
| 408 | })); |
| 409 | face.setInterestFilter("/", bind([]{})); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 410 | advanceClocks(10_ms); |
Junxiao Shi | 859888f | 2017-09-12 14:29:16 +0000 | [diff] [blame] | 411 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 412 | face.receive(*makeInterest("/A", true)); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 413 | advanceClocks(10_ms); |
Junxiao Shi | 859888f | 2017-09-12 14:29:16 +0000 | [diff] [blame] | 414 | BOOST_CHECK(hasInterest1); |
| 415 | BOOST_CHECK_EQUAL(face.sentData.size(), 1); |
| 416 | BOOST_CHECK_EQUAL(face.sentData.at(0).getName(), "/A/B"); |
| 417 | |
| 418 | face.put(*makeData("/A/C")); |
| 419 | BOOST_CHECK_EQUAL(face.sentData.size(), 1); // additional Data are ignored |
| 420 | } |
| 421 | |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 422 | BOOST_AUTO_TEST_CASE(PutNack) |
| 423 | { |
Junxiao Shi | 79a7a16 | 2017-09-09 08:33:57 +0000 | [diff] [blame] | 424 | 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] | 425 | advanceClocks(10_ms); |
Junxiao Shi | 79a7a16 | 2017-09-09 08:33:57 +0000 | [diff] [blame] | 426 | |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 427 | BOOST_CHECK_EQUAL(face.sentNacks.size(), 0); |
| 428 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 429 | face.put(makeNack(*makeInterest("/unsolicited", false, DEFAULT_INTEREST_LIFETIME, 18645250), |
| 430 | lp::NackReason::NO_ROUTE)); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 431 | advanceClocks(10_ms); |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 432 | 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] | 433 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 434 | auto interest1 = makeInterest("/Hello/World", false, DEFAULT_INTEREST_LIFETIME, 14247162); |
| 435 | face.receive(*interest1); |
| 436 | auto interest2 = makeInterest("/another/prefix", false, DEFAULT_INTEREST_LIFETIME, 92203002); |
| 437 | face.receive(*interest2); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 438 | advanceClocks(10_ms); |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 439 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 440 | face.put(makeNack(*interest1, lp::NackReason::DUPLICATE)); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 441 | advanceClocks(10_ms); |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 442 | BOOST_REQUIRE_EQUAL(face.sentNacks.size(), 1); |
| 443 | BOOST_CHECK_EQUAL(face.sentNacks[0].getReason(), lp::NackReason::DUPLICATE); |
| 444 | BOOST_CHECK(face.sentNacks[0].getTag<lp::CongestionMarkTag>() == nullptr); |
| 445 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 446 | auto nack = makeNack(*interest2, lp::NackReason::NO_ROUTE); |
Eric Newberry | 4d261b6 | 2016-11-10 13:40:09 -0700 | [diff] [blame] | 447 | nack.setTag(make_shared<lp::CongestionMarkTag>(1)); |
| 448 | face.put(nack); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 449 | advanceClocks(10_ms); |
Eric Newberry | 4d261b6 | 2016-11-10 13:40:09 -0700 | [diff] [blame] | 450 | BOOST_REQUIRE_EQUAL(face.sentNacks.size(), 2); |
Junxiao Shi | 1ad0b4b | 2017-08-18 14:19:14 +0000 | [diff] [blame] | 451 | BOOST_CHECK_EQUAL(face.sentNacks[1].getReason(), lp::NackReason::NO_ROUTE); |
Eric Newberry | 4d261b6 | 2016-11-10 13:40:09 -0700 | [diff] [blame] | 452 | BOOST_CHECK(face.sentNacks[1].getTag<lp::CongestionMarkTag>() != nullptr); |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 453 | } |
Ilya Moiseenko | 56b0bf8 | 2015-11-08 11:14:28 -0500 | [diff] [blame] | 454 | |
Junxiao Shi | 79a7a16 | 2017-09-09 08:33:57 +0000 | [diff] [blame] | 455 | BOOST_AUTO_TEST_CASE(PutMultipleNack) |
| 456 | { |
Junxiao Shi | 859888f | 2017-09-12 14:29:16 +0000 | [diff] [blame] | 457 | bool hasInterest1 = false, hasInterest2 = false; |
| 458 | // register two Interest destinations |
| 459 | face.setInterestFilter("/", [&] (const InterestFilter&, const Interest& interest) { |
| 460 | hasInterest1 = true; |
| 461 | // sending Nack right away from the first destination, Interest should still go to second destination |
| 462 | face.put(makeNack(interest, lp::NackReason::CONGESTION)); |
| 463 | }); |
| 464 | face.setInterestFilter("/", bind([&] { hasInterest2 = true; })); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 465 | advanceClocks(10_ms); |
Junxiao Shi | 79a7a16 | 2017-09-09 08:33:57 +0000 | [diff] [blame] | 466 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 467 | auto interest = makeInterest("/A", false, DEFAULT_INTEREST_LIFETIME, 14333271); |
| 468 | face.receive(*interest); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 469 | advanceClocks(10_ms); |
Junxiao Shi | 859888f | 2017-09-12 14:29:16 +0000 | [diff] [blame] | 470 | BOOST_CHECK(hasInterest1); |
| 471 | BOOST_CHECK(hasInterest2); |
Junxiao Shi | 79a7a16 | 2017-09-09 08:33:57 +0000 | [diff] [blame] | 472 | |
Junxiao Shi | 859888f | 2017-09-12 14:29:16 +0000 | [diff] [blame] | 473 | // Nack from first destination is received, should wait for a response from the other destination |
| 474 | BOOST_CHECK_EQUAL(face.sentNacks.size(), 0); |
Junxiao Shi | 79a7a16 | 2017-09-09 08:33:57 +0000 | [diff] [blame] | 475 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 476 | face.put(makeNack(*interest, lp::NackReason::NO_ROUTE)); // Nack from second destination |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 477 | advanceClocks(10_ms); |
Junxiao Shi | 859888f | 2017-09-12 14:29:16 +0000 | [diff] [blame] | 478 | 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] | 479 | BOOST_CHECK_EQUAL(face.sentNacks.at(0).getReason(), lp::NackReason::CONGESTION); // least severe reason |
| 480 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 481 | face.put(makeNack(*interest, lp::NackReason::DUPLICATE)); |
Junxiao Shi | 79a7a16 | 2017-09-09 08:33:57 +0000 | [diff] [blame] | 482 | BOOST_CHECK_EQUAL(face.sentNacks.size(), 1); // additional Nacks are ignored |
| 483 | } |
| 484 | |
Junxiao Shi | b682891 | 2017-11-20 14:06:32 +0000 | [diff] [blame] | 485 | BOOST_AUTO_TEST_CASE(PutMultipleNackLoopback) |
| 486 | { |
| 487 | bool hasInterest1 = false, hasNack = false; |
| 488 | |
| 489 | // first InterestFilter allows loopback and should receive Interest |
| 490 | face.setInterestFilter("/", [&] (const InterestFilter&, const Interest& interest) { |
| 491 | hasInterest1 = true; |
| 492 | face.put(makeNack(interest, lp::NackReason::CONGESTION)); |
| 493 | }); |
| 494 | // second InterestFilter disallows loopback and should not receive Interest |
| 495 | face.setInterestFilter(InterestFilter("/").allowLoopback(false), |
| 496 | bind([] { BOOST_ERROR("Unexpected Interest on second InterestFilter"); })); |
| 497 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 498 | auto interest = makeInterest("/A", false, DEFAULT_INTEREST_LIFETIME, 28395852); |
| 499 | face.expressInterest(*interest, |
Junxiao Shi | b682891 | 2017-11-20 14:06:32 +0000 | [diff] [blame] | 500 | bind([] { BOOST_FAIL("Unexpected data"); }), |
| 501 | [&] (const Interest&, const lp::Nack& nack) { |
| 502 | hasNack = true; |
| 503 | BOOST_CHECK_EQUAL(nack.getReason(), lp::NackReason::CONGESTION); |
| 504 | }, |
| 505 | bind([] { BOOST_FAIL("Unexpected timeout"); })); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 506 | advanceClocks(1_ms); |
Junxiao Shi | b682891 | 2017-11-20 14:06:32 +0000 | [diff] [blame] | 507 | BOOST_CHECK_EQUAL(hasInterest1, true); // Interest looped back |
| 508 | BOOST_CHECK_EQUAL(face.sentInterests.size(), 1); // Interest sent to forwarder |
| 509 | BOOST_CHECK_EQUAL(hasNack, false); // waiting for Nack from forwarder |
| 510 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 511 | face.receive(makeNack(*interest, lp::NackReason::NO_ROUTE)); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 512 | advanceClocks(1_ms); |
Junxiao Shi | b682891 | 2017-11-20 14:06:32 +0000 | [diff] [blame] | 513 | BOOST_CHECK_EQUAL(hasNack, true); |
| 514 | } |
| 515 | |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 516 | BOOST_AUTO_TEST_SUITE_END() // Producer |
| 517 | |
| 518 | BOOST_AUTO_TEST_SUITE(RegisterPrefix) |
| 519 | |
| 520 | BOOST_FIXTURE_TEST_CASE(Failure, FaceFixture<NoPrefixRegReply>) |
| 521 | { |
| 522 | BOOST_CHECK(!runPrefixReg([&] (const auto& success, const auto& failure) { |
| 523 | face.registerPrefix("/Hello/World", success, failure); |
| 524 | this->advanceClocks(5_s, 20); // wait for command timeout |
| 525 | })); |
| 526 | } |
| 527 | |
| 528 | BOOST_AUTO_TEST_CASE(Handle) |
| 529 | { |
| 530 | RegisteredPrefixHandle hdl; |
| 531 | auto doReg = [&] { |
| 532 | return runPrefixReg([&] (const auto& success, const auto& failure) { |
| 533 | hdl = face.registerPrefix("/Hello/World", success, failure); |
| 534 | }); |
| 535 | }; |
| 536 | auto doUnreg = [&] { |
| 537 | return runPrefixUnreg([&] (const auto& success, const auto& failure) { |
| 538 | hdl.unregister(success, failure); |
| 539 | }); |
| 540 | }; |
| 541 | |
| 542 | // despite the "undefined behavior" warning, we try not to crash, but no API guarantee for this |
| 543 | BOOST_CHECK(!doUnreg()); |
| 544 | |
| 545 | // cancel after unregister |
| 546 | BOOST_CHECK(doReg()); |
| 547 | BOOST_CHECK(doUnreg()); |
| 548 | hdl.cancel(); |
| 549 | advanceClocks(1_ms); |
| 550 | |
| 551 | // unregister after cancel |
| 552 | BOOST_CHECK(doReg()); |
| 553 | hdl.cancel(); |
| 554 | advanceClocks(1_ms); |
| 555 | BOOST_CHECK(!doUnreg()); |
| 556 | |
| 557 | // cancel after destructing face |
| 558 | auto face2 = make_unique<DummyClientFace>(io, m_keyChain); |
| 559 | hdl = face2->registerPrefix("/Hello/World/2", nullptr, |
| 560 | bind([] { BOOST_FAIL("Unexpected registerPrefix failure"); })); |
| 561 | advanceClocks(1_ms); |
| 562 | face2.reset(); |
| 563 | advanceClocks(1_ms); |
| 564 | hdl.cancel(); // should not crash |
| 565 | advanceClocks(1_ms); |
| 566 | |
| 567 | // unregister after destructing face |
| 568 | auto face3 = make_unique<DummyClientFace>(io, m_keyChain); |
| 569 | hdl = face3->registerPrefix("/Hello/World/3", nullptr, |
| 570 | bind([] { BOOST_FAIL("Unexpected registerPrefix failure"); })); |
| 571 | advanceClocks(1_ms); |
| 572 | face3.reset(); |
| 573 | advanceClocks(1_ms); |
| 574 | BOOST_CHECK(!doUnreg()); |
| 575 | } |
| 576 | |
| 577 | BOOST_AUTO_TEST_SUITE_END() // RegisterPrefix |
| 578 | |
| 579 | BOOST_AUTO_TEST_SUITE(SetInterestFilter) |
| 580 | |
| 581 | BOOST_AUTO_TEST_CASE(SetAndCancel) |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 582 | { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 583 | size_t nInterests = 0; |
| 584 | size_t nRegs = 0; |
Davide Pesavento | 720e25c | 2019-07-14 01:33:52 -0400 | [diff] [blame] | 585 | auto hdl = face.setInterestFilter("/Hello/World", |
| 586 | bind([&nInterests] { ++nInterests; }), |
| 587 | bind([&nRegs] { ++nRegs; }), |
| 588 | bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); })); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 589 | advanceClocks(25_ms, 4); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 590 | BOOST_CHECK_EQUAL(nRegs, 1); |
| 591 | BOOST_CHECK_EQUAL(nInterests, 0); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 592 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 593 | face.receive(*makeInterest("/Hello/World/%21")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 594 | advanceClocks(25_ms, 4); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 595 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 596 | BOOST_CHECK_EQUAL(nRegs, 1); |
| 597 | BOOST_CHECK_EQUAL(nInterests, 1); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 598 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 599 | face.receive(*makeInterest("/Bye/World/%21")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 600 | advanceClocks(10000_ms, 10); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 601 | BOOST_CHECK_EQUAL(nInterests, 1); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 602 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 603 | face.receive(*makeInterest("/Hello/World/%21/2")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 604 | advanceClocks(25_ms, 4); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 605 | BOOST_CHECK_EQUAL(nInterests, 2); |
| 606 | |
| 607 | // removing filter |
Davide Pesavento | 720e25c | 2019-07-14 01:33:52 -0400 | [diff] [blame] | 608 | hdl.cancel(); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 609 | advanceClocks(25_ms, 4); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 610 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 611 | face.receive(*makeInterest("/Hello/World/%21/3")); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 612 | BOOST_CHECK_EQUAL(nInterests, 2); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 613 | } |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 614 | |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 615 | BOOST_AUTO_TEST_CASE(EmptyInterestCallback) |
Junxiao Shi | 76e0eb2 | 2016-08-08 05:54:10 +0000 | [diff] [blame] | 616 | { |
| 617 | face.setInterestFilter("/A", nullptr); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 618 | advanceClocks(1_ms); |
Junxiao Shi | 76e0eb2 | 2016-08-08 05:54:10 +0000 | [diff] [blame] | 619 | |
| 620 | BOOST_CHECK_NO_THROW(do { |
| 621 | face.receive(*makeInterest("/A/1")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 622 | advanceClocks(1_ms); |
Junxiao Shi | 76e0eb2 | 2016-08-08 05:54:10 +0000 | [diff] [blame] | 623 | } while (false)); |
| 624 | } |
| 625 | |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 626 | BOOST_AUTO_TEST_CASE(WithoutSuccessCallback) |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 627 | { |
| 628 | size_t nInterests = 0; |
Davide Pesavento | 720e25c | 2019-07-14 01:33:52 -0400 | [diff] [blame] | 629 | auto hdl = face.setInterestFilter("/Hello/World", |
| 630 | bind([&nInterests] { ++nInterests; }), |
| 631 | bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); })); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 632 | advanceClocks(25_ms, 4); |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 633 | BOOST_CHECK_EQUAL(nInterests, 0); |
| 634 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 635 | face.receive(*makeInterest("/Hello/World/%21")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 636 | advanceClocks(25_ms, 4); |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 637 | |
| 638 | BOOST_CHECK_EQUAL(nInterests, 1); |
| 639 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 640 | face.receive(*makeInterest("/Bye/World/%21")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 641 | advanceClocks(10000_ms, 10); |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 642 | BOOST_CHECK_EQUAL(nInterests, 1); |
| 643 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 644 | face.receive(*makeInterest("/Hello/World/%21/2")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 645 | advanceClocks(25_ms, 4); |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 646 | BOOST_CHECK_EQUAL(nInterests, 2); |
| 647 | |
| 648 | // removing filter |
Davide Pesavento | 720e25c | 2019-07-14 01:33:52 -0400 | [diff] [blame] | 649 | hdl.cancel(); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 650 | advanceClocks(25_ms, 4); |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 651 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 652 | face.receive(*makeInterest("/Hello/World/%21/3")); |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 653 | BOOST_CHECK_EQUAL(nInterests, 2); |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 654 | } |
| 655 | |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 656 | BOOST_FIXTURE_TEST_CASE(Failure, FaceFixture<NoPrefixRegReply>) |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 657 | { |
| 658 | // don't enable registration reply |
| 659 | size_t nRegFailed = 0; |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 660 | face.setInterestFilter("/Hello/World", |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 661 | bind([] { BOOST_FAIL("Unexpected Interest"); }), |
| 662 | bind([] { BOOST_FAIL("Unexpected success of setInterestFilter"); }), |
| 663 | bind([&nRegFailed] { ++nRegFailed; })); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 664 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 665 | advanceClocks(25_ms, 4); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 666 | BOOST_CHECK_EQUAL(nRegFailed, 0); |
| 667 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 668 | advanceClocks(2000_ms, 5); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 669 | BOOST_CHECK_EQUAL(nRegFailed, 1); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 670 | } |
| 671 | |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 672 | BOOST_FIXTURE_TEST_CASE(FailureWithoutSuccessCallback, FaceFixture<NoPrefixRegReply>) |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 673 | { |
| 674 | // don't enable registration reply |
| 675 | size_t nRegFailed = 0; |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 676 | face.setInterestFilter("/Hello/World", |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 677 | bind([] { BOOST_FAIL("Unexpected Interest"); }), |
| 678 | bind([&nRegFailed] { ++nRegFailed; })); |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 679 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 680 | advanceClocks(25_ms, 4); |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 681 | BOOST_CHECK_EQUAL(nRegFailed, 0); |
| 682 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 683 | advanceClocks(2000_ms, 5); |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 684 | BOOST_CHECK_EQUAL(nRegFailed, 1); |
| 685 | } |
| 686 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 687 | BOOST_AUTO_TEST_CASE(SimilarFilters) |
| 688 | { |
| 689 | size_t nInInterests1 = 0; |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 690 | face.setInterestFilter("/Hello/World", |
| 691 | bind([&nInInterests1] { ++nInInterests1; }), |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 692 | nullptr, |
| 693 | bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); })); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 694 | |
| 695 | size_t nInInterests2 = 0; |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 696 | face.setInterestFilter("/Hello", |
| 697 | bind([&nInInterests2] { ++nInInterests2; }), |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 698 | nullptr, |
| 699 | bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 700 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 701 | size_t nInInterests3 = 0; |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 702 | face.setInterestFilter("/Los/Angeles/Lakers", |
| 703 | bind([&nInInterests3] { ++nInInterests3; }), |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 704 | nullptr, |
| 705 | bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 706 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 707 | advanceClocks(25_ms, 4); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 708 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 709 | face.receive(*makeInterest("/Hello/World/%21")); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 710 | advanceClocks(25_ms, 4); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 711 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 712 | BOOST_CHECK_EQUAL(nInInterests1, 1); |
| 713 | BOOST_CHECK_EQUAL(nInInterests2, 1); |
| 714 | BOOST_CHECK_EQUAL(nInInterests3, 0); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 715 | } |
| 716 | |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 717 | BOOST_AUTO_TEST_CASE(RegexFilter) |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 718 | { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 719 | size_t nInInterests = 0; |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 720 | face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"), |
| 721 | bind([&nInInterests] { ++nInInterests; }), |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 722 | nullptr, |
| 723 | bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 724 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 725 | advanceClocks(25_ms, 4); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 726 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 727 | face.receive(*makeInterest("/Hello/World/a")); // shouldn't match |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 728 | BOOST_CHECK_EQUAL(nInInterests, 0); |
| 729 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 730 | face.receive(*makeInterest("/Hello/World/a/b")); // should match |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 731 | BOOST_CHECK_EQUAL(nInInterests, 1); |
| 732 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 733 | face.receive(*makeInterest("/Hello/World/a/b/c")); // should match |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 734 | BOOST_CHECK_EQUAL(nInInterests, 2); |
| 735 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 736 | face.receive(*makeInterest("/Hello/World/a/b/d")); // should not match |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 737 | BOOST_CHECK_EQUAL(nInInterests, 2); |
| 738 | } |
| 739 | |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 740 | BOOST_AUTO_TEST_CASE(RegexFilterError) |
| 741 | { |
| 742 | face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"), |
| 743 | [] (const Name&, const Interest&) { |
| 744 | BOOST_FAIL("InterestFilter::Error should have been triggered"); |
| 745 | }, |
| 746 | nullptr, |
| 747 | bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); })); |
| 748 | |
| 749 | advanceClocks(25_ms, 4); |
| 750 | |
| 751 | BOOST_CHECK_THROW(face.receive(*makeInterest("/Hello/World/XXX/b/c")), InterestFilter::Error); |
| 752 | } |
| 753 | |
| 754 | BOOST_AUTO_TEST_CASE(RegexFilterAndRegisterPrefix) |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 755 | { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 756 | size_t nInInterests = 0; |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 757 | face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"), |
| 758 | bind([&nInInterests] { ++nInInterests; })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 759 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 760 | size_t nRegSuccesses = 0; |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 761 | face.registerPrefix("/Hello/World", |
| 762 | bind([&nRegSuccesses] { ++nRegSuccesses; }), |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 763 | bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 764 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 765 | advanceClocks(25_ms, 4); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 766 | BOOST_CHECK_EQUAL(nRegSuccesses, 1); |
| 767 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 768 | face.receive(*makeInterest("/Hello/World/a")); // shouldn't match |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 769 | BOOST_CHECK_EQUAL(nInInterests, 0); |
| 770 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 771 | face.receive(*makeInterest("/Hello/World/a/b")); // should match |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 772 | BOOST_CHECK_EQUAL(nInInterests, 1); |
| 773 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 774 | face.receive(*makeInterest("/Hello/World/a/b/c")); // should match |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 775 | BOOST_CHECK_EQUAL(nInInterests, 2); |
| 776 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 777 | face.receive(*makeInterest("/Hello/World/a/b/d")); // should not match |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 778 | BOOST_CHECK_EQUAL(nInInterests, 2); |
| 779 | } |
| 780 | |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 781 | BOOST_FIXTURE_TEST_CASE(WithoutRegisterPrefix, FaceFixture<NoPrefixRegReply>) // Bug 2318 |
Junxiao Shi | a1ea506 | 2014-12-27 22:33:39 -0700 | [diff] [blame] | 782 | { |
| 783 | // This behavior is specific to DummyClientFace. |
| 784 | // Regular Face won't accept incoming packets until something is sent. |
| 785 | |
| 786 | int hit = 0; |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 787 | face.setInterestFilter(Name("/"), bind([&hit] { ++hit; })); |
| 788 | face.processEvents(time::milliseconds(-1)); |
Junxiao Shi | a1ea506 | 2014-12-27 22:33:39 -0700 | [diff] [blame] | 789 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 790 | face.receive(*makeInterest("/A")); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 791 | face.processEvents(time::milliseconds(-1)); |
Junxiao Shi | a1ea506 | 2014-12-27 22:33:39 -0700 | [diff] [blame] | 792 | |
| 793 | BOOST_CHECK_EQUAL(hit, 1); |
| 794 | } |
| 795 | |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 796 | BOOST_AUTO_TEST_CASE(Handle) |
Junxiao Shi | 60aaef0 | 2019-01-14 04:59:30 +0000 | [diff] [blame] | 797 | { |
| 798 | int hit = 0; |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 799 | InterestFilterHandle hdl = face.setInterestFilter(Name("/"), bind([&hit] { ++hit; })); |
Junxiao Shi | 60aaef0 | 2019-01-14 04:59:30 +0000 | [diff] [blame] | 800 | face.processEvents(-1_ms); |
| 801 | |
| 802 | face.receive(*makeInterest("/A")); |
| 803 | face.processEvents(-1_ms); |
| 804 | BOOST_CHECK_EQUAL(hit, 1); |
| 805 | |
| 806 | hdl.cancel(); |
| 807 | face.processEvents(-1_ms); |
| 808 | |
| 809 | face.receive(*makeInterest("/B")); |
| 810 | face.processEvents(-1_ms); |
| 811 | BOOST_CHECK_EQUAL(hit, 1); |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 812 | |
| 813 | // cancel after destructing face |
| 814 | auto face2 = make_unique<DummyClientFace>(io, m_keyChain); |
| 815 | InterestFilterHandle hdl2 = face2->setInterestFilter("/Hello/World/2", nullptr); |
| 816 | advanceClocks(1_ms); |
| 817 | face2.reset(); |
| 818 | advanceClocks(1_ms); |
| 819 | hdl2.cancel(); // should not crash |
| 820 | advanceClocks(1_ms); |
Junxiao Shi | 60aaef0 | 2019-01-14 04:59:30 +0000 | [diff] [blame] | 821 | } |
| 822 | |
Davide Pesavento | d247d49 | 2020-01-28 21:30:20 -0500 | [diff] [blame] | 823 | BOOST_AUTO_TEST_SUITE_END() // SetInterestFilter |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 824 | |
Alexander Afanasyev | 8e15854 | 2014-11-18 00:47:18 -0500 | [diff] [blame] | 825 | BOOST_AUTO_TEST_CASE(ProcessEvents) |
| 826 | { |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 827 | face.processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside |
Alexander Afanasyev | 8e15854 | 2014-11-18 00:47:18 -0500 | [diff] [blame] | 828 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 829 | size_t nRegSuccesses = 0; |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 830 | face.registerPrefix("/Hello/World", |
| 831 | bind([&nRegSuccesses] { ++nRegSuccesses; }), |
Junxiao Shi | 103d8ed | 2016-08-07 20:34:10 +0000 | [diff] [blame] | 832 | bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); })); |
Alexander Afanasyev | 8e15854 | 2014-11-18 00:47:18 -0500 | [diff] [blame] | 833 | |
| 834 | // io_service::poll() without reset |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 835 | face.getIoService().poll(); |
Alexander Afanasyev | 8e15854 | 2014-11-18 00:47:18 -0500 | [diff] [blame] | 836 | BOOST_CHECK_EQUAL(nRegSuccesses, 0); |
| 837 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 838 | face.processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside |
Alexander Afanasyev | 8e15854 | 2014-11-18 00:47:18 -0500 | [diff] [blame] | 839 | BOOST_CHECK_EQUAL(nRegSuccesses, 1); |
| 840 | } |
| 841 | |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 842 | BOOST_AUTO_TEST_CASE(DestroyWithoutProcessEvents) // Bug 3248 |
| 843 | { |
| 844 | auto face2 = make_unique<Face>(io); |
| 845 | face2.reset(); |
| 846 | |
| 847 | io.poll(); // should not crash |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 848 | |
| 849 | // avoid "test case [...] did not check any assertions" message from Boost.Test |
| 850 | BOOST_CHECK(true); |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 851 | } |
| 852 | |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 853 | BOOST_AUTO_TEST_SUITE(Transport) |
| 854 | |
| 855 | using ndn::Transport; |
| 856 | |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 857 | struct PibDirWithDefaultTpm |
| 858 | { |
| 859 | const std::string PATH = "build/keys-with-default-tpm"; |
| 860 | }; |
| 861 | |
Alexander Afanasyev | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame] | 862 | BOOST_FIXTURE_TEST_CASE(FaceTransport, IdentityManagementTimeFixture) |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame] | 863 | { |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame] | 864 | BOOST_CHECK(Face().getTransport() != nullptr); |
| 865 | |
Alexander Afanasyev | bb64c17 | 2015-12-29 20:32:45 -0800 | [diff] [blame] | 866 | BOOST_CHECK(Face(shared_ptr<Transport>()).getTransport() != nullptr); |
| 867 | BOOST_CHECK(Face(shared_ptr<Transport>(), io).getTransport() != nullptr); |
Alexander Afanasyev | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame] | 868 | BOOST_CHECK(Face(shared_ptr<Transport>(), io, m_keyChain).getTransport() != nullptr); |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame] | 869 | |
| 870 | auto transport = make_shared<TcpTransport>("localhost", "6363"); // no real io operations will be scheduled |
| 871 | BOOST_CHECK(Face(transport).getTransport() == transport); |
| 872 | BOOST_CHECK(Face(transport, io).getTransport() == transport); |
Alexander Afanasyev | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame] | 873 | BOOST_CHECK(Face(transport, io, m_keyChain).getTransport() == transport); |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame] | 874 | } |
| 875 | |
Alexander Afanasyev | cf49055 | 2016-06-27 22:51:36 -0700 | [diff] [blame] | 876 | class WithEnv : private IdentityManagementTimeFixture |
| 877 | { |
| 878 | public: |
| 879 | WithEnv() |
| 880 | { |
| 881 | if (getenv("NDN_CLIENT_TRANSPORT") != nullptr) { |
| 882 | m_oldTransport = getenv("NDN_CLIENT_TRANSPORT"); |
| 883 | unsetenv("NDN_CLIENT_TRANSPORT"); |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | void |
| 888 | configure(const std::string& faceUri) |
| 889 | { |
| 890 | setenv("NDN_CLIENT_TRANSPORT", faceUri.c_str(), true); |
| 891 | } |
| 892 | |
| 893 | ~WithEnv() |
| 894 | { |
| 895 | if (!m_oldTransport.empty()) { |
| 896 | setenv("NDN_CLIENT_TRANSPORT", m_oldTransport.c_str(), true); |
| 897 | } |
| 898 | else { |
| 899 | unsetenv("NDN_CLIENT_TRANSPORT"); |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | private: |
| 904 | std::string m_oldTransport; |
| 905 | }; |
| 906 | |
| 907 | class WithConfig : private TestHomeFixture<DefaultPibDir> |
| 908 | { |
| 909 | public: |
| 910 | void |
| 911 | configure(const std::string& faceUri) |
| 912 | { |
| 913 | createClientConf({"transport=" + faceUri}); |
| 914 | } |
| 915 | }; |
| 916 | |
| 917 | class WithEnvAndConfig : public WithEnv, public WithConfig |
| 918 | { |
| 919 | }; |
| 920 | |
| 921 | typedef boost::mpl::vector<WithEnv, WithConfig> ConfigOptions; |
| 922 | |
| 923 | 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] | 924 | { |
| 925 | shared_ptr<Face> face; |
| 926 | BOOST_REQUIRE_NO_THROW(face = make_shared<Face>()); |
| 927 | BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr); |
Alexander Afanasyev | 57e0036 | 2016-06-23 13:22:54 -0700 | [diff] [blame] | 928 | } |
| 929 | |
Alexander Afanasyev | cf49055 | 2016-06-27 22:51:36 -0700 | [diff] [blame] | 930 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(Unix, T, ConfigOptions, T) |
| 931 | { |
| 932 | this->configure("unix://some/path"); |
Alexander Afanasyev | 57e0036 | 2016-06-23 13:22:54 -0700 | [diff] [blame] | 933 | |
Alexander Afanasyev | cf49055 | 2016-06-27 22:51:36 -0700 | [diff] [blame] | 934 | shared_ptr<Face> face; |
| 935 | BOOST_REQUIRE_NO_THROW(face = make_shared<Face>()); |
| 936 | BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr); |
| 937 | } |
| 938 | |
| 939 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(Tcp, T, ConfigOptions, T) |
| 940 | { |
| 941 | this->configure("tcp://127.0.0.1:6000"); |
| 942 | |
| 943 | shared_ptr<Face> face; |
| 944 | BOOST_REQUIRE_NO_THROW(face = make_shared<Face>()); |
| 945 | BOOST_CHECK(dynamic_pointer_cast<TcpTransport>(face->getTransport()) != nullptr); |
| 946 | } |
| 947 | |
| 948 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(WrongTransport, T, ConfigOptions, T) |
| 949 | { |
| 950 | this->configure("wrong-transport:"); |
| 951 | |
| 952 | BOOST_CHECK_THROW(make_shared<Face>(), ConfigFile::Error); |
| 953 | } |
| 954 | |
| 955 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(WrongUri, T, ConfigOptions, T) |
| 956 | { |
| 957 | this->configure("wrong-uri"); |
| 958 | |
| 959 | BOOST_CHECK_THROW(make_shared<Face>(), ConfigFile::Error); |
| 960 | } |
| 961 | |
| 962 | BOOST_FIXTURE_TEST_CASE(EnvOverride, WithEnvAndConfig) |
| 963 | { |
| 964 | this->WithEnv::configure("tcp://127.0.0.1:6000"); |
| 965 | this->WithConfig::configure("unix://some/path"); |
| 966 | |
| 967 | shared_ptr<Face> face; |
| 968 | BOOST_REQUIRE_NO_THROW(face = make_shared<Face>()); |
| 969 | BOOST_CHECK(dynamic_pointer_cast<TcpTransport>(face->getTransport()) != nullptr); |
| 970 | } |
| 971 | |
| 972 | BOOST_FIXTURE_TEST_CASE(ExplicitTransport, WithEnvAndConfig) |
| 973 | { |
| 974 | this->WithEnv::configure("wrong-uri"); |
| 975 | this->WithConfig::configure("wrong-transport:"); |
| 976 | |
| 977 | auto transport = make_shared<UnixTransport>("unix://some/path"); |
| 978 | shared_ptr<Face> face; |
| 979 | BOOST_REQUIRE_NO_THROW(face = make_shared<Face>(transport)); |
| 980 | BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr); |
| 981 | } |
| 982 | |
Junxiao Shi | ae0b418 | 2016-08-08 22:53:17 +0000 | [diff] [blame] | 983 | BOOST_AUTO_TEST_SUITE_END() // Transport |
| 984 | |
Alexander Afanasyev | 57e0036 | 2016-06-23 13:22:54 -0700 | [diff] [blame] | 985 | BOOST_AUTO_TEST_SUITE_END() // TestFace |
| 986 | |
| 987 | } // namespace tests |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 988 | } // namespace ndn |