Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2016 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" |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 27 | #include "util/scheduler.hpp" |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 28 | #include "security/key-chain.hpp" |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 29 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 30 | #include "identity-management-fixture.hpp" |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 31 | #include "boost-test.hpp" |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 32 | #include "key-chain-fixture.hpp" |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 33 | |
Junxiao Shi | cf9c6bb | 2016-07-27 02:18:19 +0000 | [diff] [blame] | 34 | #include <stdio.h> |
| 35 | |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 36 | namespace ndn { |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 37 | namespace tests { |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 38 | |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 39 | struct PibDirWithDefaultTpm |
| 40 | { |
| 41 | const std::string PATH = "build/keys-with-default-tpm"; |
| 42 | }; |
| 43 | |
| 44 | class FacesFixture : public IdentityManagementFixture, |
| 45 | public PibDirFixture<PibDirWithDefaultTpm> |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 46 | { |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 47 | public: |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 48 | FacesFixture() |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 49 | : nData(0) |
| 50 | , nTimeouts(0) |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 51 | , regPrefixId(0) |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 52 | , nInInterests(0) |
| 53 | , nInInterests2(0) |
| 54 | , nRegFailures(0) |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 55 | { |
| 56 | } |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 57 | |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 58 | void |
| 59 | onData() |
| 60 | { |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 61 | ++nData; |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void |
| 65 | onTimeout() |
| 66 | { |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 67 | ++nTimeouts; |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 71 | onInterest(Face& face, |
| 72 | const Name&, const Interest&) |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 73 | { |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 74 | ++nInInterests; |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 75 | |
| 76 | face.unsetInterestFilter(regPrefixId); |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 80 | onInterest2(Face& face, |
| 81 | const Name&, const Interest&) |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 82 | { |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 83 | ++nInInterests2; |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 84 | |
| 85 | face.unsetInterestFilter(regPrefixId2); |
| 86 | } |
| 87 | |
| 88 | void |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 89 | onInterestRegex(Face& face, |
| 90 | const InterestFilter&, const Interest&) |
| 91 | { |
| 92 | ++nInInterests; |
| 93 | } |
| 94 | |
| 95 | void |
| 96 | onInterestRegexError(Face& face, |
| 97 | const Name&, const Interest&) |
| 98 | { |
| 99 | BOOST_FAIL("InterestFilter::Error should have been triggered"); |
| 100 | } |
| 101 | |
| 102 | void |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 103 | onRegFailed() |
| 104 | { |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 105 | ++nRegFailures; |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | void |
| 109 | expressInterest(Face& face, const Name& name) |
| 110 | { |
| 111 | Interest i(name); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 112 | i.setInterestLifetime(time::milliseconds(50)); |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 113 | face.expressInterest(i, |
| 114 | bind(&FacesFixture::onData, this), |
| 115 | bind(&FacesFixture::onTimeout, this)); |
| 116 | } |
| 117 | |
| 118 | void |
| 119 | terminate(Face& face) |
| 120 | { |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 121 | face.getIoService().stop(); |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 124 | uint32_t nData; |
| 125 | uint32_t nTimeouts; |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 126 | |
| 127 | const RegisteredPrefixId* regPrefixId; |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 128 | const RegisteredPrefixId* regPrefixId2; |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 129 | uint32_t nInInterests; |
| 130 | uint32_t nInInterests2; |
| 131 | uint32_t nRegFailures; |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 132 | }; |
| 133 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 134 | BOOST_FIXTURE_TEST_SUITE(TestFaces, FacesFixture) |
| 135 | |
| 136 | BOOST_AUTO_TEST_CASE(Unix) |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 137 | { |
| 138 | Face face; |
| 139 | |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 140 | face.expressInterest(Interest("/", time::milliseconds(1000)), |
| 141 | bind(&FacesFixture::onData, this), |
| 142 | bind(&FacesFixture::onTimeout, this)); |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 143 | |
| 144 | BOOST_REQUIRE_NO_THROW(face.processEvents()); |
| 145 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 146 | BOOST_CHECK_EQUAL(nData, 1); |
| 147 | BOOST_CHECK_EQUAL(nTimeouts, 0); |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 148 | |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 149 | face.expressInterest(Interest("/localhost/non-existing/data/should/not/exist/anywhere", |
| 150 | time::milliseconds(50)), |
| 151 | bind(&FacesFixture::onData, this), |
| 152 | bind(&FacesFixture::onTimeout, this)); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 153 | |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 154 | Name veryLongName; |
| 155 | for (size_t i = 0; i <= MAX_NDN_PACKET_SIZE / 10; i++) |
| 156 | { |
| 157 | veryLongName.append("0123456789"); |
| 158 | } |
| 159 | |
| 160 | BOOST_CHECK_THROW(face.expressInterest(veryLongName, OnData(), OnTimeout()), Face::Error); |
| 161 | |
| 162 | shared_ptr<Data> data = make_shared<Data>(veryLongName); |
| 163 | data->setContent(reinterpret_cast<const uint8_t*>("01234567890"), 10); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 164 | m_keyChain.sign(*data); |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 165 | BOOST_CHECK_THROW(face.put(*data), Face::Error); |
| 166 | |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 167 | BOOST_REQUIRE_NO_THROW(face.processEvents()); |
| 168 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 169 | BOOST_CHECK_EQUAL(nData, 1); |
| 170 | BOOST_CHECK_EQUAL(nTimeouts, 1); |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 171 | } |
| 172 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 173 | BOOST_AUTO_TEST_CASE(Tcp) |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 174 | { |
| 175 | Face face("localhost"); |
| 176 | |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 177 | face.expressInterest(Interest("/", time::milliseconds(1000)), |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 178 | bind(&FacesFixture::onData, this), |
| 179 | bind(&FacesFixture::onTimeout, this)); |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 180 | |
| 181 | BOOST_REQUIRE_NO_THROW(face.processEvents()); |
| 182 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 183 | BOOST_CHECK_EQUAL(nData, 1); |
| 184 | BOOST_CHECK_EQUAL(nTimeouts, 0); |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 185 | |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 186 | face.expressInterest(Interest("/localhost/non-existing/data/should/not/exist/anywhere", |
| 187 | time::milliseconds(50)), |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 188 | bind(&FacesFixture::onData, this), |
| 189 | bind(&FacesFixture::onTimeout, this)); |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 190 | |
| 191 | BOOST_REQUIRE_NO_THROW(face.processEvents()); |
| 192 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 193 | BOOST_CHECK_EQUAL(nData, 1); |
| 194 | BOOST_CHECK_EQUAL(nTimeouts, 1); |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 195 | } |
| 196 | |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 197 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 198 | BOOST_AUTO_TEST_CASE(SetFilter) |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 199 | { |
| 200 | Face face; |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 201 | Face face2(face.getIoService()); |
| 202 | Scheduler scheduler(face.getIoService()); |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 203 | scheduler.scheduleEvent(time::seconds(4), |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 204 | bind(&FacesFixture::terminate, this, ref(face))); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 205 | |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 206 | regPrefixId = face.setInterestFilter("/Hello/World", |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 207 | bind(&FacesFixture::onInterest, this, ref(face), _1, _2), |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 208 | RegisterPrefixSuccessCallback(), |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 209 | bind(&FacesFixture::onRegFailed, this)); |
| 210 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 211 | scheduler.scheduleEvent(time::milliseconds(200), |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 212 | bind(&FacesFixture::expressInterest, this, |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 213 | ref(face2), Name("/Hello/World/!"))); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 214 | |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 215 | BOOST_REQUIRE_NO_THROW(face.processEvents()); |
| 216 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 217 | BOOST_CHECK_EQUAL(nRegFailures, 0); |
| 218 | BOOST_CHECK_EQUAL(nInInterests, 1); |
| 219 | BOOST_CHECK_EQUAL(nTimeouts, 1); |
| 220 | BOOST_CHECK_EQUAL(nData, 0); |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 221 | } |
| 222 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 223 | BOOST_AUTO_TEST_CASE(SetTwoFilters) |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 224 | { |
| 225 | Face face; |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 226 | Face face2(face.getIoService()); |
| 227 | Scheduler scheduler(face.getIoService()); |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame] | 228 | scheduler.scheduleEvent(time::seconds(1), |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 229 | bind(&FacesFixture::terminate, this, ref(face))); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 230 | |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 231 | regPrefixId = face.setInterestFilter("/Hello/World", |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 232 | bind(&FacesFixture::onInterest, this, ref(face), _1, _2), |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 233 | RegisterPrefixSuccessCallback(), |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 234 | bind(&FacesFixture::onRegFailed, this)); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 235 | |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 236 | regPrefixId2 = face.setInterestFilter("/Los/Angeles/Lakers", |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 237 | bind(&FacesFixture::onInterest2, this, ref(face), _1, _2), |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 238 | RegisterPrefixSuccessCallback(), |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 239 | bind(&FacesFixture::onRegFailed, this)); |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 240 | |
| 241 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 242 | scheduler.scheduleEvent(time::milliseconds(200), |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 243 | bind(&FacesFixture::expressInterest, this, |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 244 | ref(face2), Name("/Hello/World/!"))); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 245 | |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 246 | BOOST_REQUIRE_NO_THROW(face.processEvents()); |
| 247 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 248 | BOOST_CHECK_EQUAL(nRegFailures, 0); |
| 249 | BOOST_CHECK_EQUAL(nInInterests, 1); |
| 250 | BOOST_CHECK_EQUAL(nInInterests2, 0); |
| 251 | BOOST_CHECK_EQUAL(nTimeouts, 1); |
| 252 | BOOST_CHECK_EQUAL(nData, 0); |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 253 | } |
| 254 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 255 | BOOST_AUTO_TEST_CASE(SetRegexFilterError) |
| 256 | { |
| 257 | Face face; |
| 258 | Face face2(face.getIoService()); |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 259 | Scheduler scheduler(face.getIoService()); |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 260 | scheduler.scheduleEvent(time::seconds(4), |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 261 | bind(&FacesFixture::terminate, this, ref(face))); |
| 262 | |
| 263 | regPrefixId = face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"), |
| 264 | bind(&FacesFixture::onInterestRegexError, this, |
| 265 | ref(face), _1, _2), |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 266 | RegisterPrefixSuccessCallback(), |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 267 | bind(&FacesFixture::onRegFailed, this)); |
| 268 | |
| 269 | scheduler.scheduleEvent(time::milliseconds(300), |
| 270 | bind(&FacesFixture::expressInterest, this, |
| 271 | ref(face2), Name("/Hello/World/XXX/b/c"))); // should match |
| 272 | |
| 273 | BOOST_REQUIRE_THROW(face.processEvents(), InterestFilter::Error); |
| 274 | } |
| 275 | |
| 276 | BOOST_AUTO_TEST_CASE(SetRegexFilter) |
| 277 | { |
| 278 | Face face; |
| 279 | Face face2(face.getIoService()); |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 280 | Scheduler scheduler(face.getIoService()); |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 281 | scheduler.scheduleEvent(time::seconds(4), |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 282 | bind(&FacesFixture::terminate, this, ref(face))); |
| 283 | |
| 284 | regPrefixId = face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"), |
| 285 | bind(&FacesFixture::onInterestRegex, this, |
| 286 | ref(face), _1, _2), |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 287 | RegisterPrefixSuccessCallback(), |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 288 | bind(&FacesFixture::onRegFailed, this)); |
| 289 | |
| 290 | scheduler.scheduleEvent(time::milliseconds(200), |
| 291 | bind(&FacesFixture::expressInterest, this, |
| 292 | ref(face2), Name("/Hello/World/a"))); // shouldn't match |
| 293 | |
| 294 | scheduler.scheduleEvent(time::milliseconds(300), |
| 295 | bind(&FacesFixture::expressInterest, this, |
| 296 | ref(face2), Name("/Hello/World/a/b"))); // should match |
| 297 | |
| 298 | scheduler.scheduleEvent(time::milliseconds(400), |
| 299 | bind(&FacesFixture::expressInterest, this, |
| 300 | ref(face2), Name("/Hello/World/a/b/c"))); // should match |
| 301 | |
| 302 | scheduler.scheduleEvent(time::milliseconds(500), |
| 303 | bind(&FacesFixture::expressInterest, this, |
| 304 | ref(face2), Name("/Hello/World/a/b/d"))); // should not match |
| 305 | |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 306 | BOOST_REQUIRE_NO_THROW(face.processEvents()); |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 307 | |
| 308 | BOOST_CHECK_EQUAL(nRegFailures, 0); |
| 309 | BOOST_CHECK_EQUAL(nInInterests, 2); |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 310 | BOOST_CHECK_EQUAL(nTimeouts, 4); |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 311 | BOOST_CHECK_EQUAL(nData, 0); |
| 312 | } |
| 313 | |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 314 | class FacesFixture2 : public FacesFixture |
| 315 | { |
| 316 | public: |
| 317 | void |
Junxiao Shi | cf9c6bb | 2016-07-27 02:18:19 +0000 | [diff] [blame] | 318 | checkPrefix(bool shouldExist) |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 319 | { |
Junxiao Shi | cf9c6bb | 2016-07-27 02:18:19 +0000 | [diff] [blame] | 320 | // Boost.Test fails if a child process exits with non-zero. |
| 321 | // http://stackoverflow.com/q/5325202 |
| 322 | std::string output = this->executeCommand("nfd-status -r | grep /Hello/World || true"); |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 323 | |
Junxiao Shi | cf9c6bb | 2016-07-27 02:18:19 +0000 | [diff] [blame] | 324 | if (shouldExist) { |
| 325 | BOOST_CHECK_NE(output.size(), 0); |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 326 | } |
| 327 | else { |
Junxiao Shi | cf9c6bb | 2016-07-27 02:18:19 +0000 | [diff] [blame] | 328 | BOOST_CHECK_EQUAL(output.size(), 0); |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 329 | } |
| 330 | } |
Junxiao Shi | cf9c6bb | 2016-07-27 02:18:19 +0000 | [diff] [blame] | 331 | |
| 332 | protected: |
| 333 | std::string |
| 334 | executeCommand(const std::string& cmd) |
| 335 | { |
| 336 | std::string output; |
| 337 | char buf[256]; |
| 338 | FILE* pipe = popen(cmd.c_str(), "r"); |
| 339 | BOOST_REQUIRE_MESSAGE(pipe != nullptr, "cannot execute '" << cmd << "'"); |
| 340 | while (fgets(buf, sizeof(buf), pipe) != nullptr) { |
| 341 | output += buf; |
| 342 | } |
| 343 | pclose(pipe); |
| 344 | return output; |
| 345 | } |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 346 | }; |
| 347 | |
| 348 | BOOST_FIXTURE_TEST_CASE(RegisterUnregisterPrefix, FacesFixture2) |
| 349 | { |
| 350 | Face face; |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 351 | Scheduler scheduler(face.getIoService()); |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 352 | scheduler.scheduleEvent(time::seconds(4), |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 353 | bind(&FacesFixture::terminate, this, ref(face))); |
| 354 | |
| 355 | regPrefixId = face.setInterestFilter(InterestFilter("/Hello/World"), |
| 356 | bind(&FacesFixture::onInterest, this, |
| 357 | ref(face), _1, _2), |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 358 | RegisterPrefixSuccessCallback(), |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 359 | bind(&FacesFixture::onRegFailed, this)); |
| 360 | |
| 361 | scheduler.scheduleEvent(time::milliseconds(500), |
| 362 | bind(&FacesFixture2::checkPrefix, this, true)); |
| 363 | |
| 364 | scheduler.scheduleEvent(time::seconds(1), |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 365 | bind(static_cast<void(Face::*)(const RegisteredPrefixId*)>(&Face::unsetInterestFilter), |
| 366 | &face, |
| 367 | regPrefixId)); // shouldn't match |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 368 | |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 369 | scheduler.scheduleEvent(time::milliseconds(2000), |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 370 | bind(&FacesFixture2::checkPrefix, this, false)); |
| 371 | |
| 372 | BOOST_REQUIRE_NO_THROW(face.processEvents()); |
| 373 | } |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 374 | |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 375 | |
| 376 | class FacesFixture3 : public FacesFixture2 |
| 377 | { |
| 378 | public: |
| 379 | FacesFixture3() |
| 380 | : nRegSuccesses(0) |
| 381 | , nUnregSuccesses(0) |
| 382 | , nUnregFailures(0) |
| 383 | { |
| 384 | } |
| 385 | |
| 386 | void |
| 387 | onRegSucceeded() |
| 388 | { |
| 389 | ++nRegSuccesses; |
| 390 | } |
| 391 | |
| 392 | void |
| 393 | onUnregSucceeded() |
| 394 | { |
| 395 | ++nUnregSuccesses; |
| 396 | } |
| 397 | |
| 398 | void |
| 399 | onUnregFailed() |
| 400 | { |
| 401 | ++nUnregFailures; |
| 402 | } |
| 403 | |
| 404 | public: |
| 405 | uint64_t nRegSuccesses; |
| 406 | uint64_t nUnregSuccesses; |
| 407 | uint64_t nUnregFailures; |
| 408 | }; |
| 409 | |
| 410 | BOOST_FIXTURE_TEST_CASE(RegisterPrefix, FacesFixture3) |
| 411 | { |
| 412 | Face face; |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 413 | Face face2(face.getIoService()); |
| 414 | Scheduler scheduler(face.getIoService()); |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 415 | scheduler.scheduleEvent(time::seconds(2), |
| 416 | bind(&FacesFixture::terminate, this, ref(face))); |
| 417 | |
| 418 | scheduler.scheduleEvent(time::milliseconds(500), |
| 419 | bind(&FacesFixture2::checkPrefix, this, true)); |
| 420 | |
| 421 | regPrefixId = face.registerPrefix("/Hello/World", |
| 422 | bind(&FacesFixture3::onRegSucceeded, this), |
| 423 | bind(&FacesFixture3::onRegFailed, this)); |
| 424 | |
| 425 | scheduler.scheduleEvent(time::seconds(1), |
| 426 | bind(&Face::unregisterPrefix, &face, |
| 427 | regPrefixId, |
| 428 | static_cast<UnregisterPrefixSuccessCallback>(bind(&FacesFixture3::onUnregSucceeded, this)), |
| 429 | static_cast<UnregisterPrefixFailureCallback>(bind(&FacesFixture3::onUnregFailed, this)))); |
| 430 | |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 431 | scheduler.scheduleEvent(time::milliseconds(2500), |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 432 | bind(&FacesFixture2::checkPrefix, this, false)); |
| 433 | |
| 434 | BOOST_REQUIRE_NO_THROW(face.processEvents()); |
| 435 | |
| 436 | BOOST_CHECK_EQUAL(nRegFailures, 0); |
| 437 | BOOST_CHECK_EQUAL(nRegSuccesses, 1); |
| 438 | |
| 439 | BOOST_CHECK_EQUAL(nUnregFailures, 0); |
| 440 | BOOST_CHECK_EQUAL(nUnregSuccesses, 1); |
| 441 | } |
| 442 | |
| 443 | BOOST_AUTO_TEST_CASE(SetRegexFilterButNoRegister) |
| 444 | { |
| 445 | Face face; |
| 446 | Face face2(face.getIoService()); |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 447 | Scheduler scheduler(face.getIoService()); |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 448 | scheduler.scheduleEvent(time::seconds(2), |
| 449 | bind(&FacesFixture::terminate, this, ref(face))); |
| 450 | |
| 451 | face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"), |
| 452 | bind(&FacesFixture::onInterestRegex, this, |
| 453 | ref(face), _1, _2)); |
| 454 | |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 455 | // prefix is not registered, and also does not match regex |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 456 | scheduler.scheduleEvent(time::milliseconds(200), |
| 457 | bind(&FacesFixture::expressInterest, this, |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 458 | ref(face2), Name("/Hello/World/a"))); |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 459 | |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 460 | // matches regex, but prefix is not registered |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 461 | scheduler.scheduleEvent(time::milliseconds(300), |
| 462 | bind(&FacesFixture::expressInterest, this, |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 463 | ref(face2), Name("/Hello/World/a/b"))); |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 464 | |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 465 | // matches regex, but prefix is not registered |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 466 | scheduler.scheduleEvent(time::milliseconds(400), |
| 467 | bind(&FacesFixture::expressInterest, this, |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 468 | ref(face2), Name("/Hello/World/a/b/c"))); |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 469 | |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 470 | // prefix is not registered, and also does not match regex |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 471 | scheduler.scheduleEvent(time::milliseconds(500), |
| 472 | bind(&FacesFixture::expressInterest, this, |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 473 | ref(face2), Name("/Hello/World/a/b/d"))); |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 474 | |
| 475 | BOOST_REQUIRE_NO_THROW(face.processEvents()); |
| 476 | |
| 477 | BOOST_CHECK_EQUAL(nRegFailures, 0); |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 478 | BOOST_CHECK_EQUAL(nInInterests, 0); |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 479 | BOOST_CHECK_EQUAL(nTimeouts, 4); |
| 480 | BOOST_CHECK_EQUAL(nData, 0); |
| 481 | } |
| 482 | |
| 483 | |
| 484 | BOOST_FIXTURE_TEST_CASE(SetRegexFilterAndRegister, FacesFixture3) |
| 485 | { |
| 486 | Face face; |
| 487 | Face face2(face.getIoService()); |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 488 | Scheduler scheduler(face.getIoService()); |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 489 | scheduler.scheduleEvent(time::seconds(2), |
| 490 | bind(&FacesFixture::terminate, this, ref(face))); |
| 491 | |
| 492 | face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"), |
| 493 | bind(&FacesFixture::onInterestRegex, this, |
| 494 | ref(face), _1, _2)); |
| 495 | |
| 496 | face.registerPrefix("/Hello/World", |
| 497 | bind(&FacesFixture3::onRegSucceeded, this), |
| 498 | bind(&FacesFixture3::onRegFailed, this)); |
| 499 | |
| 500 | scheduler.scheduleEvent(time::milliseconds(200), |
| 501 | bind(&FacesFixture::expressInterest, this, |
| 502 | ref(face2), Name("/Hello/World/a"))); // shouldn't match |
| 503 | |
| 504 | scheduler.scheduleEvent(time::milliseconds(300), |
| 505 | bind(&FacesFixture::expressInterest, this, |
| 506 | ref(face2), Name("/Hello/World/a/b"))); // should match |
| 507 | |
| 508 | scheduler.scheduleEvent(time::milliseconds(400), |
| 509 | bind(&FacesFixture::expressInterest, this, |
| 510 | ref(face2), Name("/Hello/World/a/b/c"))); // should match |
| 511 | |
| 512 | scheduler.scheduleEvent(time::milliseconds(500), |
| 513 | bind(&FacesFixture::expressInterest, this, |
| 514 | ref(face2), Name("/Hello/World/a/b/d"))); // should not match |
| 515 | |
| 516 | BOOST_REQUIRE_NO_THROW(face.processEvents()); |
| 517 | |
| 518 | BOOST_CHECK_EQUAL(nRegFailures, 0); |
| 519 | BOOST_CHECK_EQUAL(nRegSuccesses, 1); |
| 520 | |
| 521 | BOOST_CHECK_EQUAL(nInInterests, 2); |
| 522 | BOOST_CHECK_EQUAL(nTimeouts, 4); |
| 523 | } |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 524 | |
Alexander Afanasyev | fba1ac6 | 2015-08-26 15:19:13 -0700 | [diff] [blame] | 525 | BOOST_AUTO_TEST_CASE(ShutdownWhileSendInProgress) // Bug #3136 |
| 526 | { |
| 527 | Face face; |
| 528 | face.expressInterest(Name("/Hello/World/!"), bind([]{}), bind([]{})); |
| 529 | BOOST_REQUIRE_NO_THROW(face.processEvents(time::seconds(1))); |
| 530 | |
| 531 | face.expressInterest(Name("/Bye/World/1"), bind([]{}), bind([]{})); |
| 532 | face.expressInterest(Name("/Bye/World/2"), bind([]{}), bind([]{})); |
| 533 | face.expressInterest(Name("/Bye/World/3"), bind([]{}), bind([]{})); |
| 534 | face.shutdown(); |
| 535 | |
| 536 | BOOST_REQUIRE_NO_THROW(face.processEvents(time::seconds(1))); |
| 537 | // should not segfault |
| 538 | } |
| 539 | |
Alexander Afanasyev | e508f14 | 2015-09-01 15:14:45 -0700 | [diff] [blame] | 540 | BOOST_AUTO_TEST_CASE(LargeDelayBetweenFaceConstructorAndProcessEvents) // Bug #2742 |
| 541 | { |
| 542 | ndn::Face face; |
| 543 | |
| 544 | ::sleep(5); // simulate setup workload |
| 545 | |
| 546 | BOOST_CHECK_NO_THROW(face.processEvents(time::seconds(1))); |
| 547 | } |
| 548 | |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 549 | BOOST_AUTO_TEST_SUITE_END() |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 550 | |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 551 | } // namespace tests |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 552 | } // namespace ndn |