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