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