Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 909ffef | 2017-07-07 22:12:27 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | fd61231 | 2019-03-15 18:45:46 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2019 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 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. |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 22 | #define BOOST_TEST_MODULE ndn-cxx Integrated Tests (Face) |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 23 | #include "tests/boost-test.hpp" |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 24 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 25 | #include "ndn-cxx/face.hpp" |
| 26 | #include "ndn-cxx/transport/tcp-transport.hpp" |
| 27 | #include "ndn-cxx/transport/unix-transport.hpp" |
| 28 | #include "ndn-cxx/util/scheduler.hpp" |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 29 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 30 | #include "tests/identity-management-fixture.hpp" |
| 31 | #include "tests/make-interest-data.hpp" |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 32 | |
Junxiao Shi | cf9c6bb | 2016-07-27 02:18:19 +0000 | [diff] [blame] | 33 | #include <stdio.h> |
Alexander Afanasyev | a54d5a6 | 2017-02-11 19:01:34 -0800 | [diff] [blame] | 34 | #include <condition_variable> |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 35 | #include <mutex> |
| 36 | #include <thread> |
| 37 | |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 38 | #include <boost/mpl/vector.hpp> |
Junxiao Shi | cf9c6bb | 2016-07-27 02:18:19 +0000 | [diff] [blame] | 39 | |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 40 | namespace ndn { |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 41 | namespace tests { |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 42 | |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 43 | static Name |
| 44 | makeVeryLongName(Name prefix = Name()) |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 45 | { |
Eric Newberry | 0669061 | 2016-12-27 12:50:15 -0700 | [diff] [blame] | 46 | for (size_t i = 0; i <= MAX_NDN_PACKET_SIZE / 10; i++) { |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 47 | prefix.append("0123456789"); |
| 48 | } |
| 49 | return prefix; |
| 50 | } |
| 51 | |
| 52 | static std::string |
| 53 | executeCommand(const std::string& cmd) |
| 54 | { |
| 55 | std::string output; |
| 56 | char buf[256]; |
| 57 | FILE* pipe = popen(cmd.data(), "r"); |
| 58 | BOOST_REQUIRE_MESSAGE(pipe != nullptr, "cannot execute '" << cmd << "'"); |
| 59 | while (fgets(buf, sizeof(buf), pipe) != nullptr) { |
| 60 | output += buf; |
| 61 | } |
| 62 | pclose(pipe); |
| 63 | return output; |
| 64 | } |
| 65 | |
| 66 | template<typename TransportType> |
| 67 | class FaceFixture : public IdentityManagementFixture |
| 68 | { |
| 69 | protected: |
| 70 | FaceFixture() |
| 71 | : face(TransportType::create(""), m_keyChain) |
| 72 | , sched(face.getIoService()) |
| 73 | { |
Eric Newberry | 0669061 | 2016-12-27 12:50:15 -0700 | [diff] [blame] | 74 | } |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 75 | |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 76 | /** \brief Send an Interest from a secondary face |
| 77 | * \param delay scheduling delay before sending Interest |
| 78 | * \param interest the Interest |
| 79 | * \param[out] outcome the response, initially '?', 'D' for Data, 'N' for Nack, 'T' for timeout |
| 80 | * \return scheduled event id |
| 81 | */ |
Davide Pesavento | fd61231 | 2019-03-15 18:45:46 -0400 | [diff] [blame] | 82 | scheduler::EventId |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 83 | sendInterest(time::nanoseconds delay, const Interest& interest, char& outcome) |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 84 | { |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 85 | if (face2 == nullptr) { |
| 86 | face2 = make_unique<Face>(TransportType::create(""), face.getIoService(), m_keyChain); |
| 87 | } |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 88 | |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 89 | outcome = '?'; |
Junxiao Shi | a5f233e | 2019-03-18 09:39:22 -0600 | [diff] [blame] | 90 | return sched.schedule(delay, [this, interest, &outcome] { |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 91 | face2->expressInterest(interest, |
| 92 | [&] (const Interest&, const Data&) { outcome = 'D'; }, |
| 93 | [&] (const Interest&, const lp::Nack&) { outcome = 'N'; }, |
| 94 | [&] (const Interest&) { outcome = 'T'; }); |
| 95 | }); |
| 96 | } |
| 97 | |
Davide Pesavento | fd61231 | 2019-03-15 18:45:46 -0400 | [diff] [blame] | 98 | scheduler::EventId |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 99 | sendInterest(time::nanoseconds delay, const Interest& interest) |
| 100 | { |
| 101 | static char ignoredOutcome; |
| 102 | return sendInterest(delay, interest, ignoredOutcome); |
| 103 | } |
| 104 | |
| 105 | /** \brief Stop io_service after a delay |
| 106 | * \return scheduled event id |
| 107 | */ |
Davide Pesavento | fd61231 | 2019-03-15 18:45:46 -0400 | [diff] [blame] | 108 | scheduler::EventId |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 109 | terminateAfter(time::nanoseconds delay) |
| 110 | { |
Junxiao Shi | a5f233e | 2019-03-18 09:39:22 -0600 | [diff] [blame] | 111 | return sched.schedule(delay, [this] { face.getIoService().stop(); }); |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 112 | } |
Junxiao Shi | cf9c6bb | 2016-07-27 02:18:19 +0000 | [diff] [blame] | 113 | |
| 114 | protected: |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 115 | Face face; |
| 116 | Scheduler sched; |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 117 | unique_ptr<Face> face2; |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 118 | }; |
| 119 | |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 120 | using Transports = boost::mpl::vector<UnixTransport, TcpTransport>; |
| 121 | |
| 122 | BOOST_FIXTURE_TEST_SUITE(TestFace, FaceFixture<UnixTransport>) |
| 123 | |
| 124 | BOOST_AUTO_TEST_SUITE(Consumer) |
| 125 | |
| 126 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(ExpressInterestData, TransportType, Transports, FaceFixture<TransportType>) |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 127 | { |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 128 | int nData = 0; |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 129 | this->face.expressInterest(*makeInterest("/", true), |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 130 | [&] (const Interest&, const Data&) { ++nData; }, |
| 131 | [] (const Interest&, const lp::Nack&) { BOOST_ERROR("unexpected Nack"); }, |
| 132 | [] (const Interest&) { BOOST_ERROR("unexpected timeout"); }); |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 133 | |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 134 | this->face.processEvents(); |
| 135 | BOOST_CHECK_EQUAL(nData, 1); |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 136 | } |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 137 | |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 138 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(ExpressInterestNack, TransportType, Transports, FaceFixture<TransportType>) |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 139 | { |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 140 | int nNacks = 0; |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 141 | this->face.expressInterest(*makeInterest("/localhost/non-existent-should-nack"), |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 142 | [] (const Interest&, const Data&) { BOOST_ERROR("unexpected Data"); }, |
| 143 | [&] (const Interest&, const lp::Nack&) { ++nNacks; }, |
| 144 | [] (const Interest&) { BOOST_ERROR("unexpected timeout"); }); |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 145 | |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 146 | this->face.processEvents(); |
| 147 | BOOST_CHECK_EQUAL(nNacks, 1); |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 150 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(ExpressInterestTimeout, TransportType, Transports, FaceFixture<TransportType>) |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 151 | { |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 152 | // add route toward null face so Interest would timeout instead of getting Nacked |
| 153 | executeCommand("nfdc route add /localhost/non-existent-should-timeout null://"); |
| 154 | std::this_thread::sleep_for(std::chrono::milliseconds(200)); // wait for FIB update to take effect |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 155 | |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 156 | int nTimeouts = 0; |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 157 | this->face.expressInterest(*makeInterest("/localhost/non-existent-should-timeout", false, 1_s), |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 158 | [] (const Interest&, const Data&) { BOOST_ERROR("unexpected Data"); }, |
| 159 | [] (const Interest&, const lp::Nack&) { BOOST_ERROR("unexpected Nack"); }, |
| 160 | [&] (const Interest&) { ++nTimeouts; }); |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 161 | |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 162 | this->face.processEvents(); |
| 163 | BOOST_CHECK_EQUAL(nTimeouts, 1); |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 166 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(OversizedInterest, TransportType, Transports, FaceFixture<TransportType>) |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 167 | { |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 168 | BOOST_CHECK_THROW(do { |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 169 | this->face.expressInterest(*makeInterest(makeVeryLongName()), nullptr, nullptr, nullptr); |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 170 | this->face.processEvents(); |
| 171 | } while (false), Face::OversizedPacketError); |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 172 | } |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 173 | |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 174 | BOOST_AUTO_TEST_SUITE_END() // Consumer |
| 175 | |
| 176 | BOOST_AUTO_TEST_SUITE(Producer) |
| 177 | |
| 178 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(RegisterUnregisterPrefix, TransportType, Transports, FaceFixture<TransportType>) |
| 179 | { |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 180 | this->terminateAfter(4_s); |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 181 | |
| 182 | int nRegSuccess = 0, nUnregSuccess = 0; |
Davide Pesavento | 720e25c | 2019-07-14 01:33:52 -0400 | [diff] [blame] | 183 | auto handle = this->face.registerPrefix("/Hello/World", |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 184 | [&] (const Name&) { ++nRegSuccess; }, |
Davide Pesavento | 720e25c | 2019-07-14 01:33:52 -0400 | [diff] [blame] | 185 | [] (const Name&, const auto& msg) { BOOST_ERROR("unexpected register prefix failure: " << msg); }); |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 186 | |
Junxiao Shi | a5f233e | 2019-03-18 09:39:22 -0600 | [diff] [blame] | 187 | this->sched.schedule(1_s, [&nRegSuccess] { |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 188 | BOOST_CHECK_EQUAL(nRegSuccess, 1); |
| 189 | std::string output = executeCommand("nfdc route list | grep /Hello/World"); |
| 190 | BOOST_CHECK(!output.empty()); |
| 191 | }); |
| 192 | |
Davide Pesavento | 720e25c | 2019-07-14 01:33:52 -0400 | [diff] [blame] | 193 | this->sched.schedule(2_s, [&] { |
| 194 | handle.unregister( |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 195 | [&] { ++nUnregSuccess; }, |
Davide Pesavento | 720e25c | 2019-07-14 01:33:52 -0400 | [diff] [blame] | 196 | [] (const auto& msg) { BOOST_ERROR("unexpected unregister prefix failure: " << msg); }); |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 197 | }); |
| 198 | |
Junxiao Shi | a5f233e | 2019-03-18 09:39:22 -0600 | [diff] [blame] | 199 | this->sched.schedule(3_s, [&nUnregSuccess] { |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 200 | BOOST_CHECK_EQUAL(nUnregSuccess, 1); |
| 201 | |
| 202 | // Boost.Test would fail if a child process exits with non-zero. http://stackoverflow.com/q/5325202 |
| 203 | std::string output = executeCommand("nfdc route list | grep /Hello/World || true"); |
| 204 | BOOST_CHECK(output.empty()); |
| 205 | }); |
| 206 | |
| 207 | this->face.processEvents(); |
| 208 | } |
| 209 | |
| 210 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(RegularFilter, TransportType, Transports, FaceFixture<TransportType>) |
| 211 | { |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 212 | this->terminateAfter(2_s); |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 213 | |
| 214 | int nInterests1 = 0, nRegSuccess1 = 0, nRegSuccess2 = 0; |
| 215 | this->face.setInterestFilter("/Hello/World", |
| 216 | [&] (const InterestFilter&, const Interest&) { ++nInterests1; }, |
| 217 | [&] (const Name&) { ++nRegSuccess1; }, |
Davide Pesavento | 720e25c | 2019-07-14 01:33:52 -0400 | [diff] [blame] | 218 | [] (const Name&, const auto& msg) { BOOST_ERROR("unexpected register prefix failure: " << msg); }); |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 219 | this->face.setInterestFilter("/Los/Angeles/Lakers", |
| 220 | [&] (const InterestFilter&, const Interest&) { BOOST_ERROR("unexpected Interest"); }, |
| 221 | [&] (const Name&) { ++nRegSuccess2; }, |
Davide Pesavento | 720e25c | 2019-07-14 01:33:52 -0400 | [diff] [blame] | 222 | [] (const Name&, const auto& msg) { BOOST_ERROR("unexpected register prefix failure: " << msg); }); |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 223 | |
Junxiao Shi | a5f233e | 2019-03-18 09:39:22 -0600 | [diff] [blame] | 224 | this->sched.schedule(500_ms, [] { |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 225 | std::string output = executeCommand("nfdc route list | grep /Hello/World"); |
| 226 | BOOST_CHECK(!output.empty()); |
| 227 | }); |
| 228 | |
| 229 | char interestOutcome; |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 230 | this->sendInterest(1_s, *makeInterest("/Hello/World/regular", false, 50_ms), interestOutcome); |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 231 | |
| 232 | this->face.processEvents(); |
| 233 | BOOST_CHECK_EQUAL(interestOutcome, 'T'); |
| 234 | BOOST_CHECK_EQUAL(nInterests1, 1); |
| 235 | BOOST_CHECK_EQUAL(nRegSuccess1, 1); |
| 236 | BOOST_CHECK_EQUAL(nRegSuccess2, 1); |
| 237 | } |
| 238 | |
| 239 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(RegexFilter, TransportType, Transports, FaceFixture<TransportType>) |
| 240 | { |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 241 | this->terminateAfter(2_s); |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 242 | |
| 243 | int nRegSuccess = 0; |
| 244 | std::set<Name> receivedInterests; |
| 245 | this->face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"), |
| 246 | [&] (const InterestFilter&, const Interest& interest) { receivedInterests.insert(interest.getName()); }, |
| 247 | [&] (const Name&) { ++nRegSuccess; }, |
Davide Pesavento | 720e25c | 2019-07-14 01:33:52 -0400 | [diff] [blame] | 248 | [] (const Name&, const auto& msg) { BOOST_ERROR("unexpected register prefix failure: " << msg); }); |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 249 | |
Junxiao Shi | a5f233e | 2019-03-18 09:39:22 -0600 | [diff] [blame] | 250 | this->sched.schedule(700_ms, [] { |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 251 | std::string output = executeCommand("nfdc route list | grep /Hello/World"); |
| 252 | BOOST_CHECK(!output.empty()); |
| 253 | }); |
| 254 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 255 | this->sendInterest(200_ms, *makeInterest("/Hello/World/a", false, 50_ms)); |
| 256 | this->sendInterest(300_ms, *makeInterest("/Hello/World/a/b", false, 50_ms)); |
| 257 | this->sendInterest(400_ms, *makeInterest("/Hello/World/a/b/c", false, 50_ms)); |
| 258 | this->sendInterest(500_ms, *makeInterest("/Hello/World/a/b/d", false, 50_ms)); |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 259 | |
| 260 | this->face.processEvents(); |
| 261 | BOOST_CHECK_EQUAL(nRegSuccess, 1); |
| 262 | std::set<Name> expectedInterests{"/Hello/World/a/b", "/Hello/World/a/b/c"}; |
| 263 | BOOST_CHECK_EQUAL_COLLECTIONS(receivedInterests.begin(), receivedInterests.end(), |
| 264 | expectedInterests.begin(), expectedInterests.end()); |
| 265 | } |
| 266 | |
| 267 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(RegexFilterNoRegister, TransportType, Transports, FaceFixture<TransportType>) |
| 268 | { |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 269 | this->terminateAfter(2_s); |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 270 | |
| 271 | // no Interest shall arrive because prefix isn't registered in forwarder |
| 272 | this->face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"), |
Davide Pesavento | 720e25c | 2019-07-14 01:33:52 -0400 | [diff] [blame] | 273 | [&] (const InterestFilter&, const Interest&) { BOOST_ERROR("unexpected Interest"); }); |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 274 | |
Junxiao Shi | a5f233e | 2019-03-18 09:39:22 -0600 | [diff] [blame] | 275 | this->sched.schedule(700_ms, [] { |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 276 | // Boost.Test would fail if a child process exits with non-zero. http://stackoverflow.com/q/5325202 |
| 277 | std::string output = executeCommand("nfdc route list | grep /Hello/World || true"); |
| 278 | BOOST_CHECK(output.empty()); |
| 279 | }); |
| 280 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 281 | this->sendInterest(200_ms, *makeInterest("/Hello/World/a", false, 50_ms)); |
| 282 | this->sendInterest(300_ms, *makeInterest("/Hello/World/a/b", false, 50_ms)); |
| 283 | this->sendInterest(400_ms, *makeInterest("/Hello/World/a/b/c", false, 50_ms)); |
| 284 | this->sendInterest(500_ms, *makeInterest("/Hello/World/a/b/d", false, 50_ms)); |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 285 | |
| 286 | this->face.processEvents(); |
| 287 | } |
| 288 | |
| 289 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(PutDataNack, TransportType, Transports, FaceFixture<TransportType>) |
| 290 | { |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 291 | this->terminateAfter(2_s); |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 292 | |
| 293 | this->face.setInterestFilter("/Hello/World", |
| 294 | [&] (const InterestFilter&, const Interest& interest) { |
| 295 | if (interest.getName().at(2) == name::Component("nack")) { |
| 296 | this->face.put(makeNack(interest, lp::NackReason::NO_ROUTE)); |
| 297 | } |
| 298 | else { |
| 299 | this->face.put(*makeData(interest.getName())); |
| 300 | } |
| 301 | }, |
| 302 | nullptr, |
Davide Pesavento | 720e25c | 2019-07-14 01:33:52 -0400 | [diff] [blame] | 303 | [] (const Name&, const auto& msg) { BOOST_ERROR("unexpected register prefix failure: " << msg); }); |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 304 | |
| 305 | char outcome1, outcome2; |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 306 | this->sendInterest(700_ms, *makeInterest("/Hello/World/data", false, 50_ms), outcome1); |
| 307 | this->sendInterest(800_ms, *makeInterest("/Hello/World/nack", false, 50_ms), outcome2); |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 308 | |
| 309 | this->face.processEvents(); |
| 310 | BOOST_CHECK_EQUAL(outcome1, 'D'); |
| 311 | BOOST_CHECK_EQUAL(outcome2, 'N'); |
| 312 | } |
| 313 | |
| 314 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(OversizedData, TransportType, Transports, FaceFixture<TransportType>) |
| 315 | { |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 316 | this->terminateAfter(2_s); |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 317 | |
| 318 | this->face.setInterestFilter("/Hello/World", |
| 319 | [&] (const InterestFilter&, const Interest& interest) { |
| 320 | this->face.put(*makeData(makeVeryLongName(interest.getName()))); |
| 321 | }, |
| 322 | nullptr, |
Davide Pesavento | 720e25c | 2019-07-14 01:33:52 -0400 | [diff] [blame] | 323 | [] (const Name&, const auto& msg) { BOOST_ERROR("unexpected register prefix failure: " << msg); }); |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 324 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 325 | this->sendInterest(1_s, *makeInterest("/Hello/World/oversized", true, 50_ms)); |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 326 | |
| 327 | BOOST_CHECK_THROW(this->face.processEvents(), Face::OversizedPacketError); |
| 328 | } |
| 329 | |
| 330 | BOOST_AUTO_TEST_SUITE_END() // Producer |
| 331 | |
| 332 | BOOST_AUTO_TEST_SUITE(IoRoutine) |
| 333 | |
Alexander Afanasyev | fba1ac6 | 2015-08-26 15:19:13 -0700 | [diff] [blame] | 334 | BOOST_AUTO_TEST_CASE(ShutdownWhileSendInProgress) // Bug #3136 |
| 335 | { |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 336 | this->face.expressInterest(*makeInterest("/Hello/World"), nullptr, nullptr, nullptr); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 337 | this->face.processEvents(1_s); |
Alexander Afanasyev | fba1ac6 | 2015-08-26 15:19:13 -0700 | [diff] [blame] | 338 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 339 | this->face.expressInterest(*makeInterest("/Bye/World/1"), nullptr, nullptr, nullptr); |
| 340 | this->face.expressInterest(*makeInterest("/Bye/World/2"), nullptr, nullptr, nullptr); |
| 341 | this->face.expressInterest(*makeInterest("/Bye/World/3"), nullptr, nullptr, nullptr); |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 342 | this->face.shutdown(); |
Alexander Afanasyev | fba1ac6 | 2015-08-26 15:19:13 -0700 | [diff] [blame] | 343 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 344 | this->face.processEvents(1_s); // should not segfault |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 345 | BOOST_CHECK(true); |
Alexander Afanasyev | fba1ac6 | 2015-08-26 15:19:13 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Alexander Afanasyev | e508f14 | 2015-09-01 15:14:45 -0700 | [diff] [blame] | 348 | BOOST_AUTO_TEST_CASE(LargeDelayBetweenFaceConstructorAndProcessEvents) // Bug #2742 |
| 349 | { |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 350 | std::this_thread::sleep_for(std::chrono::seconds(5)); // simulate setup workload |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 351 | this->face.processEvents(1_s); // should not throw |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 352 | BOOST_CHECK(true); |
Alexander Afanasyev | e508f14 | 2015-09-01 15:14:45 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Alexander Afanasyev | a54d5a6 | 2017-02-11 19:01:34 -0800 | [diff] [blame] | 355 | BOOST_AUTO_TEST_CASE(ProcessEventsBlocksForeverWhenNothingScheduled) // Bug #3957 |
| 356 | { |
Alexander Afanasyev | a54d5a6 | 2017-02-11 19:01:34 -0800 | [diff] [blame] | 357 | std::mutex m; |
| 358 | std::condition_variable cv; |
| 359 | bool processEventsFinished = false; |
| 360 | |
| 361 | std::thread faceThread([&] { |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 362 | this->face.processEvents(); |
Alexander Afanasyev | a54d5a6 | 2017-02-11 19:01:34 -0800 | [diff] [blame] | 363 | |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 364 | processEventsFinished = true; |
| 365 | std::lock_guard<std::mutex> lk(m); |
| 366 | cv.notify_one(); |
| 367 | }); |
Alexander Afanasyev | a54d5a6 | 2017-02-11 19:01:34 -0800 | [diff] [blame] | 368 | |
| 369 | { |
| 370 | std::unique_lock<std::mutex> lk(m); |
| 371 | cv.wait_for(lk, std::chrono::seconds(5), [&] { return processEventsFinished; }); |
| 372 | } |
| 373 | |
| 374 | BOOST_CHECK_EQUAL(processEventsFinished, true); |
| 375 | if (!processEventsFinished) { |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 376 | this->face.shutdown(); |
Alexander Afanasyev | a54d5a6 | 2017-02-11 19:01:34 -0800 | [diff] [blame] | 377 | } |
| 378 | faceThread.join(); |
| 379 | } |
| 380 | |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 381 | BOOST_AUTO_TEST_SUITE_END() // IoRoutine |
| 382 | |
| 383 | BOOST_AUTO_TEST_SUITE_END() // TestFace |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 384 | |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 385 | } // namespace tests |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 386 | } // namespace ndn |