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