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