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; |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 164 | Face face2(face.getIoService()); |
| 165 | Scheduler scheduler(face.getIoService()); |
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 | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 171 | RegisterPrefixSuccessCallback(), |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 172 | bind(&FacesFixture::onRegFailed, this)); |
| 173 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 174 | scheduler.scheduleEvent(time::milliseconds(200), |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 175 | bind(&FacesFixture::expressInterest, this, |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 176 | ref(face2), Name("/Hello/World/!"))); |
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 | BOOST_REQUIRE_NO_THROW(face.processEvents()); |
| 179 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 180 | BOOST_CHECK_EQUAL(nRegFailures, 0); |
| 181 | BOOST_CHECK_EQUAL(nInInterests, 1); |
| 182 | BOOST_CHECK_EQUAL(nTimeouts, 1); |
| 183 | BOOST_CHECK_EQUAL(nData, 0); |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 184 | } |
| 185 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 186 | BOOST_AUTO_TEST_CASE(SetTwoFilters) |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 187 | { |
| 188 | Face face; |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 189 | Face face2(face.getIoService()); |
| 190 | Scheduler scheduler(face.getIoService()); |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame] | 191 | scheduler.scheduleEvent(time::seconds(1), |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 192 | bind(&FacesFixture::terminate, this, ref(face))); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 193 | |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 194 | regPrefixId = face.setInterestFilter("/Hello/World", |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 195 | bind(&FacesFixture::onInterest, this, ref(face), _1, _2), |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 196 | RegisterPrefixSuccessCallback(), |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 197 | bind(&FacesFixture::onRegFailed, this)); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 198 | |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 199 | regPrefixId2 = face.setInterestFilter("/Los/Angeles/Lakers", |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 200 | bind(&FacesFixture::onInterest2, this, ref(face), _1, _2), |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 201 | RegisterPrefixSuccessCallback(), |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 202 | bind(&FacesFixture::onRegFailed, this)); |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 203 | |
| 204 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 205 | scheduler.scheduleEvent(time::milliseconds(200), |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 206 | bind(&FacesFixture::expressInterest, this, |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 207 | ref(face2), Name("/Hello/World/!"))); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 208 | |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 209 | BOOST_REQUIRE_NO_THROW(face.processEvents()); |
| 210 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 211 | BOOST_CHECK_EQUAL(nRegFailures, 0); |
| 212 | BOOST_CHECK_EQUAL(nInInterests, 1); |
| 213 | BOOST_CHECK_EQUAL(nInInterests2, 0); |
| 214 | BOOST_CHECK_EQUAL(nTimeouts, 1); |
| 215 | BOOST_CHECK_EQUAL(nData, 0); |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 216 | } |
| 217 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 218 | BOOST_AUTO_TEST_CASE(SetRegexFilterError) |
| 219 | { |
| 220 | Face face; |
| 221 | Face face2(face.getIoService()); |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 222 | Scheduler scheduler(face.getIoService()); |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 223 | scheduler.scheduleEvent(time::seconds(1), |
| 224 | bind(&FacesFixture::terminate, this, ref(face))); |
| 225 | |
| 226 | regPrefixId = face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"), |
| 227 | bind(&FacesFixture::onInterestRegexError, this, |
| 228 | 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)); |
| 231 | |
| 232 | scheduler.scheduleEvent(time::milliseconds(300), |
| 233 | bind(&FacesFixture::expressInterest, this, |
| 234 | ref(face2), Name("/Hello/World/XXX/b/c"))); // should match |
| 235 | |
| 236 | BOOST_REQUIRE_THROW(face.processEvents(), InterestFilter::Error); |
| 237 | } |
| 238 | |
| 239 | BOOST_AUTO_TEST_CASE(SetRegexFilter) |
| 240 | { |
| 241 | Face face; |
| 242 | Face face2(face.getIoService()); |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 243 | Scheduler scheduler(face.getIoService()); |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 244 | scheduler.scheduleEvent(time::seconds(2), |
| 245 | bind(&FacesFixture::terminate, this, ref(face))); |
| 246 | |
| 247 | regPrefixId = face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"), |
| 248 | bind(&FacesFixture::onInterestRegex, this, |
| 249 | ref(face), _1, _2), |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 250 | RegisterPrefixSuccessCallback(), |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 251 | bind(&FacesFixture::onRegFailed, this)); |
| 252 | |
| 253 | scheduler.scheduleEvent(time::milliseconds(200), |
| 254 | bind(&FacesFixture::expressInterest, this, |
| 255 | ref(face2), Name("/Hello/World/a"))); // shouldn't match |
| 256 | |
| 257 | scheduler.scheduleEvent(time::milliseconds(300), |
| 258 | bind(&FacesFixture::expressInterest, this, |
| 259 | ref(face2), Name("/Hello/World/a/b"))); // should match |
| 260 | |
| 261 | scheduler.scheduleEvent(time::milliseconds(400), |
| 262 | bind(&FacesFixture::expressInterest, this, |
| 263 | ref(face2), Name("/Hello/World/a/b/c"))); // should match |
| 264 | |
| 265 | scheduler.scheduleEvent(time::milliseconds(500), |
| 266 | bind(&FacesFixture::expressInterest, this, |
| 267 | ref(face2), Name("/Hello/World/a/b/d"))); // should not match |
| 268 | |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 269 | BOOST_REQUIRE_NO_THROW(face.processEvents()); |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 270 | |
| 271 | BOOST_CHECK_EQUAL(nRegFailures, 0); |
| 272 | BOOST_CHECK_EQUAL(nInInterests, 2); |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 273 | BOOST_CHECK_EQUAL(nTimeouts, 4); |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 274 | BOOST_CHECK_EQUAL(nData, 0); |
| 275 | } |
| 276 | |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 277 | class FacesFixture2 : public FacesFixture |
| 278 | { |
| 279 | public: |
| 280 | void |
| 281 | checkPrefix(bool doesExist) |
| 282 | { |
| 283 | int result = std::system("nfd-status | grep /Hello/World >/dev/null"); |
| 284 | |
| 285 | if (doesExist) { |
| 286 | BOOST_CHECK_EQUAL(result, 0); |
| 287 | } |
| 288 | else { |
| 289 | BOOST_CHECK_NE(result, 0); |
| 290 | } |
| 291 | } |
| 292 | }; |
| 293 | |
| 294 | BOOST_FIXTURE_TEST_CASE(RegisterUnregisterPrefix, FacesFixture2) |
| 295 | { |
| 296 | Face face; |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 297 | Scheduler scheduler(face.getIoService()); |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 298 | scheduler.scheduleEvent(time::seconds(2), |
| 299 | bind(&FacesFixture::terminate, this, ref(face))); |
| 300 | |
| 301 | regPrefixId = face.setInterestFilter(InterestFilter("/Hello/World"), |
| 302 | bind(&FacesFixture::onInterest, this, |
| 303 | ref(face), _1, _2), |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 304 | RegisterPrefixSuccessCallback(), |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 305 | bind(&FacesFixture::onRegFailed, this)); |
| 306 | |
| 307 | scheduler.scheduleEvent(time::milliseconds(500), |
| 308 | bind(&FacesFixture2::checkPrefix, this, true)); |
| 309 | |
| 310 | scheduler.scheduleEvent(time::seconds(1), |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 311 | bind(static_cast<void(Face::*)(const RegisteredPrefixId*)>(&Face::unsetInterestFilter), |
| 312 | &face, |
| 313 | regPrefixId)); // shouldn't match |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 314 | |
| 315 | scheduler.scheduleEvent(time::milliseconds(1500), |
| 316 | bind(&FacesFixture2::checkPrefix, this, false)); |
| 317 | |
| 318 | BOOST_REQUIRE_NO_THROW(face.processEvents()); |
| 319 | } |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 320 | |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 321 | |
| 322 | class FacesFixture3 : public FacesFixture2 |
| 323 | { |
| 324 | public: |
| 325 | FacesFixture3() |
| 326 | : nRegSuccesses(0) |
| 327 | , nUnregSuccesses(0) |
| 328 | , nUnregFailures(0) |
| 329 | { |
| 330 | } |
| 331 | |
| 332 | void |
| 333 | onRegSucceeded() |
| 334 | { |
| 335 | ++nRegSuccesses; |
| 336 | } |
| 337 | |
| 338 | void |
| 339 | onUnregSucceeded() |
| 340 | { |
| 341 | ++nUnregSuccesses; |
| 342 | } |
| 343 | |
| 344 | void |
| 345 | onUnregFailed() |
| 346 | { |
| 347 | ++nUnregFailures; |
| 348 | } |
| 349 | |
| 350 | public: |
| 351 | uint64_t nRegSuccesses; |
| 352 | uint64_t nUnregSuccesses; |
| 353 | uint64_t nUnregFailures; |
| 354 | }; |
| 355 | |
| 356 | BOOST_FIXTURE_TEST_CASE(RegisterPrefix, FacesFixture3) |
| 357 | { |
| 358 | Face face; |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 359 | Face face2(face.getIoService()); |
| 360 | Scheduler scheduler(face.getIoService()); |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 361 | scheduler.scheduleEvent(time::seconds(2), |
| 362 | bind(&FacesFixture::terminate, this, ref(face))); |
| 363 | |
| 364 | scheduler.scheduleEvent(time::milliseconds(500), |
| 365 | bind(&FacesFixture2::checkPrefix, this, true)); |
| 366 | |
| 367 | regPrefixId = face.registerPrefix("/Hello/World", |
| 368 | bind(&FacesFixture3::onRegSucceeded, this), |
| 369 | bind(&FacesFixture3::onRegFailed, this)); |
| 370 | |
| 371 | scheduler.scheduleEvent(time::seconds(1), |
| 372 | bind(&Face::unregisterPrefix, &face, |
| 373 | regPrefixId, |
| 374 | static_cast<UnregisterPrefixSuccessCallback>(bind(&FacesFixture3::onUnregSucceeded, this)), |
| 375 | static_cast<UnregisterPrefixFailureCallback>(bind(&FacesFixture3::onUnregFailed, this)))); |
| 376 | |
| 377 | scheduler.scheduleEvent(time::milliseconds(1500), |
| 378 | bind(&FacesFixture2::checkPrefix, this, false)); |
| 379 | |
| 380 | BOOST_REQUIRE_NO_THROW(face.processEvents()); |
| 381 | |
| 382 | BOOST_CHECK_EQUAL(nRegFailures, 0); |
| 383 | BOOST_CHECK_EQUAL(nRegSuccesses, 1); |
| 384 | |
| 385 | BOOST_CHECK_EQUAL(nUnregFailures, 0); |
| 386 | BOOST_CHECK_EQUAL(nUnregSuccesses, 1); |
| 387 | } |
| 388 | |
| 389 | BOOST_AUTO_TEST_CASE(SetRegexFilterButNoRegister) |
| 390 | { |
| 391 | Face face; |
| 392 | Face face2(face.getIoService()); |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 393 | Scheduler scheduler(face.getIoService()); |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 394 | scheduler.scheduleEvent(time::seconds(2), |
| 395 | bind(&FacesFixture::terminate, this, ref(face))); |
| 396 | |
| 397 | face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"), |
| 398 | bind(&FacesFixture::onInterestRegex, this, |
| 399 | ref(face), _1, _2)); |
| 400 | |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 401 | // prefix is not registered, and also does not match regex |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 402 | scheduler.scheduleEvent(time::milliseconds(200), |
| 403 | bind(&FacesFixture::expressInterest, this, |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 404 | ref(face2), Name("/Hello/World/a"))); |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 405 | |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 406 | // matches regex, but prefix is not registered |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 407 | scheduler.scheduleEvent(time::milliseconds(300), |
| 408 | bind(&FacesFixture::expressInterest, this, |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 409 | ref(face2), Name("/Hello/World/a/b"))); |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 410 | |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 411 | // matches regex, but prefix is not registered |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 412 | scheduler.scheduleEvent(time::milliseconds(400), |
| 413 | bind(&FacesFixture::expressInterest, this, |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 414 | ref(face2), Name("/Hello/World/a/b/c"))); |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 415 | |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 416 | // prefix is not registered, and also does not match regex |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 417 | scheduler.scheduleEvent(time::milliseconds(500), |
| 418 | bind(&FacesFixture::expressInterest, this, |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 419 | ref(face2), Name("/Hello/World/a/b/d"))); |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 420 | |
| 421 | BOOST_REQUIRE_NO_THROW(face.processEvents()); |
| 422 | |
| 423 | BOOST_CHECK_EQUAL(nRegFailures, 0); |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 424 | BOOST_CHECK_EQUAL(nInInterests, 0); |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 425 | BOOST_CHECK_EQUAL(nTimeouts, 4); |
| 426 | BOOST_CHECK_EQUAL(nData, 0); |
| 427 | } |
| 428 | |
| 429 | |
| 430 | BOOST_FIXTURE_TEST_CASE(SetRegexFilterAndRegister, FacesFixture3) |
| 431 | { |
| 432 | Face face; |
| 433 | Face face2(face.getIoService()); |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 434 | Scheduler scheduler(face.getIoService()); |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 435 | scheduler.scheduleEvent(time::seconds(2), |
| 436 | bind(&FacesFixture::terminate, this, ref(face))); |
| 437 | |
| 438 | face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"), |
| 439 | bind(&FacesFixture::onInterestRegex, this, |
| 440 | ref(face), _1, _2)); |
| 441 | |
| 442 | face.registerPrefix("/Hello/World", |
| 443 | bind(&FacesFixture3::onRegSucceeded, this), |
| 444 | bind(&FacesFixture3::onRegFailed, this)); |
| 445 | |
| 446 | scheduler.scheduleEvent(time::milliseconds(200), |
| 447 | bind(&FacesFixture::expressInterest, this, |
| 448 | ref(face2), Name("/Hello/World/a"))); // shouldn't match |
| 449 | |
| 450 | scheduler.scheduleEvent(time::milliseconds(300), |
| 451 | bind(&FacesFixture::expressInterest, this, |
| 452 | ref(face2), Name("/Hello/World/a/b"))); // should match |
| 453 | |
| 454 | scheduler.scheduleEvent(time::milliseconds(400), |
| 455 | bind(&FacesFixture::expressInterest, this, |
| 456 | ref(face2), Name("/Hello/World/a/b/c"))); // should match |
| 457 | |
| 458 | scheduler.scheduleEvent(time::milliseconds(500), |
| 459 | bind(&FacesFixture::expressInterest, this, |
| 460 | ref(face2), Name("/Hello/World/a/b/d"))); // should not match |
| 461 | |
| 462 | BOOST_REQUIRE_NO_THROW(face.processEvents()); |
| 463 | |
| 464 | BOOST_CHECK_EQUAL(nRegFailures, 0); |
| 465 | BOOST_CHECK_EQUAL(nRegSuccesses, 1); |
| 466 | |
| 467 | BOOST_CHECK_EQUAL(nInInterests, 2); |
| 468 | BOOST_CHECK_EQUAL(nTimeouts, 4); |
| 469 | } |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 470 | BOOST_AUTO_TEST_SUITE_END() |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 471 | |
| 472 | } // namespace ndn |