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