Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 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. |
| 20 | */ |
| 21 | |
| 22 | #include "face.hpp" |
| 23 | #include "util/scheduler.hpp" |
| 24 | #include "security/key-chain.hpp" |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 25 | #include "util/dummy-client-face.hpp" |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 26 | |
| 27 | #include "boost-test.hpp" |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 28 | #include "unit-test-time-fixture.hpp" |
| 29 | #include "test-make-interest-data.hpp" |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 30 | |
| 31 | namespace ndn { |
| 32 | namespace tests { |
| 33 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 34 | using ndn::util::DummyClientFace; |
| 35 | using ndn::util::makeDummyClientFace; |
| 36 | |
Junxiao Shi | a1ea506 | 2014-12-27 22:33:39 -0700 | [diff] [blame] | 37 | class FaceFixture : public UnitTestTimeFixture |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 38 | { |
| 39 | public: |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 40 | explicit |
Junxiao Shi | a1ea506 | 2014-12-27 22:33:39 -0700 | [diff] [blame] | 41 | FaceFixture(bool enableRegistrationReply = true) |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 42 | : face(makeDummyClientFace(io, { true, enableRegistrationReply })) |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 43 | { |
| 44 | } |
| 45 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 46 | public: |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 47 | shared_ptr<DummyClientFace> face; |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 48 | }; |
| 49 | |
Junxiao Shi | a1ea506 | 2014-12-27 22:33:39 -0700 | [diff] [blame] | 50 | class FacesNoRegistrationReplyFixture : public FaceFixture |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 51 | { |
| 52 | public: |
| 53 | FacesNoRegistrationReplyFixture() |
Junxiao Shi | a1ea506 | 2014-12-27 22:33:39 -0700 | [diff] [blame] | 54 | : FaceFixture(false) |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 55 | { |
| 56 | } |
| 57 | }; |
| 58 | |
Junxiao Shi | a1ea506 | 2014-12-27 22:33:39 -0700 | [diff] [blame] | 59 | BOOST_FIXTURE_TEST_SUITE(TestFace, FaceFixture) |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 60 | |
| 61 | BOOST_AUTO_TEST_CASE(ExpressInterestData) |
| 62 | { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 63 | size_t nData = 0; |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 64 | face->expressInterest(Interest("/Hello/World", time::milliseconds(50)), |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 65 | [&] (const Interest& i, const Data& d) { |
| 66 | BOOST_CHECK(i.getName().isPrefixOf(d.getName())); |
| 67 | ++nData; |
| 68 | }, |
| 69 | bind([] { |
| 70 | BOOST_FAIL("Unexpected timeout"); |
| 71 | })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 72 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 73 | advanceClocks(time::milliseconds(10)); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 74 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 75 | face->receive(*util::makeData("/Bye/World/!")); |
| 76 | face->receive(*util::makeData("/Hello/World/!")); |
| 77 | |
| 78 | advanceClocks(time::milliseconds(10), 100); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 79 | |
| 80 | BOOST_CHECK_EQUAL(nData, 1); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 81 | BOOST_CHECK_EQUAL(face->sentInterests.size(), 1); |
| 82 | BOOST_CHECK_EQUAL(face->sentDatas.size(), 0); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | BOOST_AUTO_TEST_CASE(ExpressInterestTimeout) |
| 86 | { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 87 | size_t nTimeouts = 0; |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 88 | face->expressInterest(Interest("/Hello/World", time::milliseconds(50)), |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 89 | bind([] { |
| 90 | BOOST_FAIL("Unexpected data"); |
| 91 | }), |
| 92 | bind([&nTimeouts] { |
| 93 | ++nTimeouts; |
| 94 | })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 95 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 96 | advanceClocks(time::milliseconds(10), 100); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 97 | |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 98 | BOOST_CHECK_EQUAL(nTimeouts, 1); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 99 | BOOST_CHECK_EQUAL(face->sentInterests.size(), 1); |
| 100 | BOOST_CHECK_EQUAL(face->sentDatas.size(), 0); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 101 | } |
| 102 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 103 | BOOST_AUTO_TEST_CASE(RemovePendingInterest) |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 104 | { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 105 | const PendingInterestId* interestId = |
| 106 | face->expressInterest(Interest("/Hello/World", time::milliseconds(50)), |
| 107 | bind([] { |
| 108 | BOOST_FAIL("Unexpected data"); |
| 109 | }), |
| 110 | bind([] { |
| 111 | BOOST_FAIL("Unexpected timeout"); |
| 112 | })); |
| 113 | advanceClocks(time::milliseconds(10)); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 114 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 115 | face->removePendingInterest(interestId); |
| 116 | advanceClocks(time::milliseconds(10)); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 117 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 118 | face->receive(*util::makeData("/Hello/World/!")); |
| 119 | advanceClocks(time::milliseconds(10), 100); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | BOOST_AUTO_TEST_CASE(SetUnsetInterestFilter) |
| 123 | { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 124 | size_t nInterests = 0; |
| 125 | size_t nRegs = 0; |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 126 | const RegisteredPrefixId* regPrefixId = |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 127 | face->setInterestFilter("/Hello/World", |
| 128 | bind([&nInterests] { ++nInterests; }), |
| 129 | bind([&nRegs] { ++nRegs; }), |
| 130 | bind([] { |
| 131 | BOOST_FAIL("Unexpected setInterestFilter failure"); |
| 132 | })); |
| 133 | advanceClocks(time::milliseconds(10), 10); |
| 134 | BOOST_CHECK_EQUAL(nRegs, 1); |
| 135 | BOOST_CHECK_EQUAL(nInterests, 0); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 136 | |
| 137 | face->receive(Interest("/Hello/World/!")); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 138 | advanceClocks(time::milliseconds(10), 10); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 139 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 140 | BOOST_CHECK_EQUAL(nRegs, 1); |
| 141 | BOOST_CHECK_EQUAL(nInterests, 1); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 142 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 143 | face->receive(Interest("/Bye/World/!")); |
| 144 | advanceClocks(time::milliseconds(10000), 10); |
| 145 | BOOST_CHECK_EQUAL(nInterests, 1); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 146 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 147 | face->receive(Interest("/Hello/World/!/2")); |
| 148 | advanceClocks(time::milliseconds(10), 10); |
| 149 | BOOST_CHECK_EQUAL(nInterests, 2); |
| 150 | |
| 151 | // removing filter |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 152 | face->unsetInterestFilter(regPrefixId); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 153 | advanceClocks(time::milliseconds(10), 10); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 154 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 155 | face->receive(Interest("/Hello/World/!/3")); |
| 156 | BOOST_CHECK_EQUAL(nInterests, 2); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 157 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 158 | face->unsetInterestFilter(static_cast<const RegisteredPrefixId*>(0)); |
| 159 | advanceClocks(time::milliseconds(10), 10); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 160 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 161 | face->unsetInterestFilter(static_cast<const InterestFilterId*>(0)); |
| 162 | advanceClocks(time::milliseconds(10), 10); |
| 163 | } |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 164 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 165 | BOOST_FIXTURE_TEST_CASE(SetInterestFilterFail, FacesNoRegistrationReplyFixture) |
| 166 | { |
| 167 | // don't enable registration reply |
| 168 | size_t nRegFailed = 0; |
| 169 | face->setInterestFilter("/Hello/World", |
| 170 | bind([] { |
| 171 | BOOST_FAIL("Unexpected Interest"); |
| 172 | }), |
| 173 | bind([] { |
| 174 | BOOST_FAIL("Unexpected success of setInterestFilter"); |
| 175 | }), |
| 176 | bind([&nRegFailed] { |
| 177 | ++nRegFailed; |
| 178 | })); |
| 179 | |
| 180 | advanceClocks(time::milliseconds(10), 10); |
| 181 | BOOST_CHECK_EQUAL(nRegFailed, 0); |
| 182 | |
| 183 | advanceClocks(time::milliseconds(1000), 10); |
| 184 | BOOST_CHECK_EQUAL(nRegFailed, 1); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | BOOST_AUTO_TEST_CASE(RegisterUnregisterPrefix) |
| 188 | { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 189 | size_t nRegSuccesses = 0; |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 190 | const RegisteredPrefixId* regPrefixId = |
| 191 | face->registerPrefix("/Hello/World", |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 192 | bind([&nRegSuccesses] { ++nRegSuccesses; }), |
| 193 | bind([] { |
| 194 | BOOST_FAIL("Unexpected registerPrefix failure"); |
| 195 | })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 196 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 197 | advanceClocks(time::milliseconds(10), 10); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 198 | BOOST_CHECK_EQUAL(nRegSuccesses, 1); |
| 199 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 200 | size_t nUnregSuccesses = 0; |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 201 | face->unregisterPrefix(regPrefixId, |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 202 | bind([&nUnregSuccesses] { ++nUnregSuccesses; }), |
| 203 | bind([] { |
| 204 | BOOST_FAIL("Unexpected unregisterPrefix failure"); |
| 205 | })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 206 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 207 | advanceClocks(time::milliseconds(10), 10); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 208 | BOOST_CHECK_EQUAL(nUnregSuccesses, 1); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 209 | } |
| 210 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 211 | BOOST_FIXTURE_TEST_CASE(RegisterUnregisterPrefixFail, FacesNoRegistrationReplyFixture) |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 212 | { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 213 | size_t nRegFailures = 0; |
| 214 | face->registerPrefix("/Hello/World", |
| 215 | bind([] { |
| 216 | BOOST_FAIL("Unexpected registerPrefix success"); |
| 217 | }), |
| 218 | bind([&nRegFailures] { ++nRegFailures; })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 219 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 220 | advanceClocks(time::milliseconds(1000), 100); |
| 221 | BOOST_CHECK_EQUAL(nRegFailures, 1); |
| 222 | } |
| 223 | |
| 224 | BOOST_AUTO_TEST_CASE(SimilarFilters) |
| 225 | { |
| 226 | size_t nInInterests1 = 0; |
| 227 | face->setInterestFilter("/Hello/World", |
| 228 | bind([&nInInterests1] { ++nInInterests1; }), |
| 229 | RegisterPrefixSuccessCallback(), |
| 230 | bind([] { |
| 231 | BOOST_FAIL("Unexpected setInterestFilter failure"); |
| 232 | })); |
| 233 | |
| 234 | size_t nInInterests2 = 0; |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 235 | face->setInterestFilter("/Hello", |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 236 | bind([&nInInterests2] { ++nInInterests2; }), |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 237 | RegisterPrefixSuccessCallback(), |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 238 | bind([] { |
| 239 | BOOST_FAIL("Unexpected setInterestFilter failure"); |
| 240 | })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 241 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 242 | size_t nInInterests3 = 0; |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 243 | face->setInterestFilter("/Los/Angeles/Lakers", |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 244 | bind([&nInInterests3] { ++nInInterests3; }), |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 245 | RegisterPrefixSuccessCallback(), |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 246 | bind([] { |
| 247 | BOOST_FAIL("Unexpected setInterestFilter failure"); |
| 248 | })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 249 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 250 | advanceClocks(time::milliseconds(10), 10); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 251 | |
| 252 | face->receive(Interest("/Hello/World/!")); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 253 | advanceClocks(time::milliseconds(10), 10); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 254 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 255 | BOOST_CHECK_EQUAL(nInInterests1, 1); |
| 256 | BOOST_CHECK_EQUAL(nInInterests2, 1); |
| 257 | BOOST_CHECK_EQUAL(nInInterests3, 0); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | BOOST_AUTO_TEST_CASE(SetRegexFilterError) |
| 261 | { |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 262 | face->setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"), |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 263 | [] (const Name&, const Interest&) { |
| 264 | BOOST_FAIL("InterestFilter::Error should have been triggered"); |
| 265 | }, |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 266 | RegisterPrefixSuccessCallback(), |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 267 | bind([] { |
| 268 | BOOST_FAIL("Unexpected setInterestFilter failure"); |
| 269 | })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 270 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 271 | advanceClocks(time::milliseconds(10), 10); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 272 | |
| 273 | BOOST_REQUIRE_THROW(face->receive(Interest("/Hello/World/XXX/b/c")), InterestFilter::Error); |
| 274 | } |
| 275 | |
| 276 | BOOST_AUTO_TEST_CASE(SetRegexFilter) |
| 277 | { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 278 | size_t nInInterests = 0; |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 279 | face->setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"), |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 280 | bind([&nInInterests] { ++nInInterests; }), |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 281 | RegisterPrefixSuccessCallback(), |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 282 | bind([] { |
| 283 | BOOST_FAIL("Unexpected setInterestFilter failure"); |
| 284 | })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 285 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 286 | advanceClocks(time::milliseconds(10), 10); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 287 | |
| 288 | face->receive(Interest("/Hello/World/a")); // shouldn't match |
| 289 | BOOST_CHECK_EQUAL(nInInterests, 0); |
| 290 | |
| 291 | face->receive(Interest("/Hello/World/a/b")); // should match |
| 292 | BOOST_CHECK_EQUAL(nInInterests, 1); |
| 293 | |
| 294 | face->receive(Interest("/Hello/World/a/b/c")); // should match |
| 295 | BOOST_CHECK_EQUAL(nInInterests, 2); |
| 296 | |
| 297 | face->receive(Interest("/Hello/World/a/b/d")); // should not match |
| 298 | BOOST_CHECK_EQUAL(nInInterests, 2); |
| 299 | } |
| 300 | |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 301 | BOOST_AUTO_TEST_CASE(SetRegexFilterAndRegister) |
| 302 | { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 303 | size_t nInInterests = 0; |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 304 | face->setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"), |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 305 | bind([&nInInterests] { ++nInInterests; })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 306 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 307 | size_t nRegSuccesses = 0; |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 308 | face->registerPrefix("/Hello/World", |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 309 | bind([&nRegSuccesses] { ++nRegSuccesses; }), |
| 310 | bind([] { |
| 311 | BOOST_FAIL("Unexpected setInterestFilter failure"); |
| 312 | })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 313 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 314 | advanceClocks(time::milliseconds(10), 10); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 315 | BOOST_CHECK_EQUAL(nRegSuccesses, 1); |
| 316 | |
| 317 | face->receive(Interest("/Hello/World/a")); // shouldn't match |
| 318 | BOOST_CHECK_EQUAL(nInInterests, 0); |
| 319 | |
| 320 | face->receive(Interest("/Hello/World/a/b")); // should match |
| 321 | BOOST_CHECK_EQUAL(nInInterests, 1); |
| 322 | |
| 323 | face->receive(Interest("/Hello/World/a/b/c")); // should match |
| 324 | BOOST_CHECK_EQUAL(nInInterests, 2); |
| 325 | |
| 326 | face->receive(Interest("/Hello/World/a/b/d")); // should not match |
| 327 | BOOST_CHECK_EQUAL(nInInterests, 2); |
| 328 | } |
| 329 | |
Junxiao Shi | a1ea506 | 2014-12-27 22:33:39 -0700 | [diff] [blame] | 330 | BOOST_FIXTURE_TEST_CASE(SetInterestFilterNoReg, FacesNoRegistrationReplyFixture) // Bug 2318 |
| 331 | { |
| 332 | // This behavior is specific to DummyClientFace. |
| 333 | // Regular Face won't accept incoming packets until something is sent. |
| 334 | |
| 335 | int hit = 0; |
| 336 | face->setInterestFilter(Name("/"), bind([&hit] { ++hit; })); |
| 337 | face->processEvents(time::milliseconds(-1)); |
| 338 | |
| 339 | auto interest = make_shared<Interest>("/A"); |
| 340 | face->receive(*interest); |
| 341 | face->processEvents(time::milliseconds(-1)); |
| 342 | |
| 343 | BOOST_CHECK_EQUAL(hit, 1); |
| 344 | } |
| 345 | |
Alexander Afanasyev | 8e15854 | 2014-11-18 00:47:18 -0500 | [diff] [blame] | 346 | BOOST_AUTO_TEST_CASE(ProcessEvents) |
| 347 | { |
| 348 | face->processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside |
| 349 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 350 | size_t nRegSuccesses = 0; |
Alexander Afanasyev | 8e15854 | 2014-11-18 00:47:18 -0500 | [diff] [blame] | 351 | face->registerPrefix("/Hello/World", |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 352 | bind([&nRegSuccesses] { ++nRegSuccesses; }), |
| 353 | bind([] { |
| 354 | BOOST_FAIL("Unexpected setInterestFilter failure"); |
| 355 | })); |
Alexander Afanasyev | 8e15854 | 2014-11-18 00:47:18 -0500 | [diff] [blame] | 356 | |
| 357 | // io_service::poll() without reset |
| 358 | face->getIoService().poll(); |
Alexander Afanasyev | 8e15854 | 2014-11-18 00:47:18 -0500 | [diff] [blame] | 359 | BOOST_CHECK_EQUAL(nRegSuccesses, 0); |
| 360 | |
| 361 | face->processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside |
Alexander Afanasyev | 8e15854 | 2014-11-18 00:47:18 -0500 | [diff] [blame] | 362 | BOOST_CHECK_EQUAL(nRegSuccesses, 1); |
| 363 | } |
| 364 | |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 365 | BOOST_AUTO_TEST_SUITE_END() |
| 366 | |
| 367 | } // tests |
| 368 | } // namespace ndn |