Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2015 Regents of the University of California. |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 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" |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 29 | #include "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 | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 73 | advanceClocks(time::milliseconds(1), 40); |
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 | |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 78 | advanceClocks(time::milliseconds(1), 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 | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 83 | |
| 84 | face->expressInterest(Interest("/Hello/World/!", time::milliseconds(50)), |
| 85 | [&] (const Interest& i, const Data& d) { |
| 86 | BOOST_CHECK(i.getName().isPrefixOf(d.getName())); |
| 87 | ++nData; |
| 88 | }, |
| 89 | bind([] { |
| 90 | BOOST_FAIL("Unexpected timeout"); |
| 91 | })); |
| 92 | advanceClocks(time::milliseconds(1), 40); |
| 93 | face->receive(*util::makeData("/Hello/World/!/1/xxxxx")); |
| 94 | |
| 95 | advanceClocks(time::milliseconds(1), 100); |
| 96 | |
| 97 | BOOST_CHECK_EQUAL(nData, 2); |
| 98 | BOOST_CHECK_EQUAL(face->sentInterests.size(), 2); |
| 99 | BOOST_CHECK_EQUAL(face->sentDatas.size(), 0); |
| 100 | |
| 101 | size_t nTimeouts = 0; |
| 102 | face->expressInterest(Interest("/Hello/World/!/2", time::milliseconds(50)), |
| 103 | bind([]{}), |
| 104 | bind([&nTimeouts] { |
| 105 | ++nTimeouts; |
| 106 | })); |
| 107 | advanceClocks(time::milliseconds(10), 100); |
| 108 | BOOST_CHECK_EQUAL(nTimeouts, 1); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | BOOST_AUTO_TEST_CASE(ExpressInterestTimeout) |
| 112 | { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 113 | size_t nTimeouts = 0; |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 114 | face->expressInterest(Interest("/Hello/World", time::milliseconds(50)), |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 115 | bind([] { |
| 116 | BOOST_FAIL("Unexpected data"); |
| 117 | }), |
| 118 | bind([&nTimeouts] { |
| 119 | ++nTimeouts; |
| 120 | })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 121 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 122 | advanceClocks(time::milliseconds(10), 100); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 123 | |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 124 | BOOST_CHECK_EQUAL(nTimeouts, 1); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 125 | BOOST_CHECK_EQUAL(face->sentInterests.size(), 1); |
| 126 | BOOST_CHECK_EQUAL(face->sentDatas.size(), 0); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 127 | } |
| 128 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 129 | BOOST_AUTO_TEST_CASE(RemovePendingInterest) |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 130 | { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 131 | const PendingInterestId* interestId = |
| 132 | face->expressInterest(Interest("/Hello/World", time::milliseconds(50)), |
| 133 | bind([] { |
| 134 | BOOST_FAIL("Unexpected data"); |
| 135 | }), |
| 136 | bind([] { |
| 137 | BOOST_FAIL("Unexpected timeout"); |
| 138 | })); |
| 139 | advanceClocks(time::milliseconds(10)); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 140 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 141 | face->removePendingInterest(interestId); |
| 142 | advanceClocks(time::milliseconds(10)); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 143 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 144 | face->receive(*util::makeData("/Hello/World/!")); |
| 145 | advanceClocks(time::milliseconds(10), 100); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | BOOST_AUTO_TEST_CASE(SetUnsetInterestFilter) |
| 149 | { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 150 | size_t nInterests = 0; |
| 151 | size_t nRegs = 0; |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 152 | const RegisteredPrefixId* regPrefixId = |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 153 | face->setInterestFilter("/Hello/World", |
| 154 | bind([&nInterests] { ++nInterests; }), |
| 155 | bind([&nRegs] { ++nRegs; }), |
| 156 | bind([] { |
| 157 | BOOST_FAIL("Unexpected setInterestFilter failure"); |
| 158 | })); |
| 159 | advanceClocks(time::milliseconds(10), 10); |
| 160 | BOOST_CHECK_EQUAL(nRegs, 1); |
| 161 | BOOST_CHECK_EQUAL(nInterests, 0); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 162 | |
| 163 | face->receive(Interest("/Hello/World/!")); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 164 | advanceClocks(time::milliseconds(10), 10); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 165 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 166 | BOOST_CHECK_EQUAL(nRegs, 1); |
| 167 | BOOST_CHECK_EQUAL(nInterests, 1); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 168 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 169 | face->receive(Interest("/Bye/World/!")); |
| 170 | advanceClocks(time::milliseconds(10000), 10); |
| 171 | BOOST_CHECK_EQUAL(nInterests, 1); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 172 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 173 | face->receive(Interest("/Hello/World/!/2")); |
| 174 | advanceClocks(time::milliseconds(10), 10); |
| 175 | BOOST_CHECK_EQUAL(nInterests, 2); |
| 176 | |
| 177 | // removing filter |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 178 | face->unsetInterestFilter(regPrefixId); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 179 | advanceClocks(time::milliseconds(10), 10); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 180 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 181 | face->receive(Interest("/Hello/World/!/3")); |
| 182 | BOOST_CHECK_EQUAL(nInterests, 2); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 183 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 184 | face->unsetInterestFilter(static_cast<const RegisteredPrefixId*>(0)); |
| 185 | advanceClocks(time::milliseconds(10), 10); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 186 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 187 | face->unsetInterestFilter(static_cast<const InterestFilterId*>(0)); |
| 188 | advanceClocks(time::milliseconds(10), 10); |
| 189 | } |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 190 | |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 191 | BOOST_AUTO_TEST_CASE(SetUnsetInterestFilterWithoutSucessCallback) |
| 192 | { |
| 193 | size_t nInterests = 0; |
| 194 | const RegisteredPrefixId* regPrefixId = |
| 195 | face->setInterestFilter("/Hello/World", |
| 196 | bind([&nInterests] { ++nInterests; }), |
| 197 | bind([] { |
| 198 | BOOST_FAIL("Unexpected setInterestFilter failure"); |
| 199 | })); |
| 200 | advanceClocks(time::milliseconds(10), 10); |
| 201 | BOOST_CHECK_EQUAL(nInterests, 0); |
| 202 | |
| 203 | face->receive(Interest("/Hello/World/!")); |
| 204 | advanceClocks(time::milliseconds(10), 10); |
| 205 | |
| 206 | BOOST_CHECK_EQUAL(nInterests, 1); |
| 207 | |
| 208 | face->receive(Interest("/Bye/World/!")); |
| 209 | advanceClocks(time::milliseconds(10000), 10); |
| 210 | BOOST_CHECK_EQUAL(nInterests, 1); |
| 211 | |
| 212 | face->receive(Interest("/Hello/World/!/2")); |
| 213 | advanceClocks(time::milliseconds(10), 10); |
| 214 | BOOST_CHECK_EQUAL(nInterests, 2); |
| 215 | |
| 216 | // removing filter |
| 217 | face->unsetInterestFilter(regPrefixId); |
| 218 | advanceClocks(time::milliseconds(10), 10); |
| 219 | |
| 220 | face->receive(Interest("/Hello/World/!/3")); |
| 221 | BOOST_CHECK_EQUAL(nInterests, 2); |
| 222 | |
| 223 | face->unsetInterestFilter(static_cast<const RegisteredPrefixId*>(0)); |
| 224 | advanceClocks(time::milliseconds(10), 10); |
| 225 | |
| 226 | face->unsetInterestFilter(static_cast<const InterestFilterId*>(0)); |
| 227 | advanceClocks(time::milliseconds(10), 10); |
| 228 | } |
| 229 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 230 | BOOST_FIXTURE_TEST_CASE(SetInterestFilterFail, FacesNoRegistrationReplyFixture) |
| 231 | { |
| 232 | // don't enable registration reply |
| 233 | size_t nRegFailed = 0; |
| 234 | face->setInterestFilter("/Hello/World", |
| 235 | bind([] { |
| 236 | BOOST_FAIL("Unexpected Interest"); |
| 237 | }), |
| 238 | bind([] { |
| 239 | BOOST_FAIL("Unexpected success of setInterestFilter"); |
| 240 | }), |
| 241 | bind([&nRegFailed] { |
| 242 | ++nRegFailed; |
| 243 | })); |
| 244 | |
| 245 | advanceClocks(time::milliseconds(10), 10); |
| 246 | BOOST_CHECK_EQUAL(nRegFailed, 0); |
| 247 | |
| 248 | advanceClocks(time::milliseconds(1000), 10); |
| 249 | BOOST_CHECK_EQUAL(nRegFailed, 1); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 250 | } |
| 251 | |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 252 | BOOST_FIXTURE_TEST_CASE(SetInterestFilterFailWithoutSuccessCallback, FacesNoRegistrationReplyFixture) |
| 253 | { |
| 254 | // don't enable registration reply |
| 255 | size_t nRegFailed = 0; |
| 256 | face->setInterestFilter("/Hello/World", |
| 257 | bind([] { |
| 258 | BOOST_FAIL("Unexpected Interest"); |
| 259 | }), |
| 260 | bind([&nRegFailed] { |
| 261 | ++nRegFailed; |
| 262 | })); |
| 263 | |
| 264 | advanceClocks(time::milliseconds(10), 10); |
| 265 | BOOST_CHECK_EQUAL(nRegFailed, 0); |
| 266 | |
| 267 | advanceClocks(time::milliseconds(1000), 10); |
| 268 | BOOST_CHECK_EQUAL(nRegFailed, 1); |
| 269 | } |
| 270 | |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 271 | BOOST_AUTO_TEST_CASE(RegisterUnregisterPrefix) |
| 272 | { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 273 | size_t nRegSuccesses = 0; |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 274 | const RegisteredPrefixId* regPrefixId = |
| 275 | face->registerPrefix("/Hello/World", |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 276 | bind([&nRegSuccesses] { ++nRegSuccesses; }), |
| 277 | bind([] { |
| 278 | BOOST_FAIL("Unexpected registerPrefix failure"); |
| 279 | })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 280 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 281 | advanceClocks(time::milliseconds(10), 10); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 282 | BOOST_CHECK_EQUAL(nRegSuccesses, 1); |
| 283 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 284 | size_t nUnregSuccesses = 0; |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 285 | face->unregisterPrefix(regPrefixId, |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 286 | bind([&nUnregSuccesses] { ++nUnregSuccesses; }), |
| 287 | bind([] { |
| 288 | BOOST_FAIL("Unexpected unregisterPrefix failure"); |
| 289 | })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 290 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 291 | advanceClocks(time::milliseconds(10), 10); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 292 | BOOST_CHECK_EQUAL(nUnregSuccesses, 1); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 293 | } |
| 294 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 295 | BOOST_FIXTURE_TEST_CASE(RegisterUnregisterPrefixFail, FacesNoRegistrationReplyFixture) |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 296 | { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 297 | size_t nRegFailures = 0; |
| 298 | face->registerPrefix("/Hello/World", |
| 299 | bind([] { |
| 300 | BOOST_FAIL("Unexpected registerPrefix success"); |
| 301 | }), |
| 302 | bind([&nRegFailures] { ++nRegFailures; })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 303 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 304 | advanceClocks(time::milliseconds(1000), 100); |
| 305 | BOOST_CHECK_EQUAL(nRegFailures, 1); |
| 306 | } |
| 307 | |
| 308 | BOOST_AUTO_TEST_CASE(SimilarFilters) |
| 309 | { |
| 310 | size_t nInInterests1 = 0; |
| 311 | face->setInterestFilter("/Hello/World", |
| 312 | bind([&nInInterests1] { ++nInInterests1; }), |
| 313 | RegisterPrefixSuccessCallback(), |
| 314 | bind([] { |
| 315 | BOOST_FAIL("Unexpected setInterestFilter failure"); |
| 316 | })); |
| 317 | |
| 318 | size_t nInInterests2 = 0; |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 319 | face->setInterestFilter("/Hello", |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 320 | bind([&nInInterests2] { ++nInInterests2; }), |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 321 | RegisterPrefixSuccessCallback(), |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 322 | bind([] { |
| 323 | BOOST_FAIL("Unexpected setInterestFilter failure"); |
| 324 | })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 325 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 326 | size_t nInInterests3 = 0; |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 327 | face->setInterestFilter("/Los/Angeles/Lakers", |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 328 | bind([&nInInterests3] { ++nInInterests3; }), |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 329 | RegisterPrefixSuccessCallback(), |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 330 | bind([] { |
| 331 | BOOST_FAIL("Unexpected setInterestFilter failure"); |
| 332 | })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 333 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 334 | advanceClocks(time::milliseconds(10), 10); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 335 | |
| 336 | face->receive(Interest("/Hello/World/!")); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 337 | advanceClocks(time::milliseconds(10), 10); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 338 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 339 | BOOST_CHECK_EQUAL(nInInterests1, 1); |
| 340 | BOOST_CHECK_EQUAL(nInInterests2, 1); |
| 341 | BOOST_CHECK_EQUAL(nInInterests3, 0); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | BOOST_AUTO_TEST_CASE(SetRegexFilterError) |
| 345 | { |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 346 | face->setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"), |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 347 | [] (const Name&, const Interest&) { |
| 348 | BOOST_FAIL("InterestFilter::Error should have been triggered"); |
| 349 | }, |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 350 | RegisterPrefixSuccessCallback(), |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 351 | bind([] { |
| 352 | BOOST_FAIL("Unexpected setInterestFilter failure"); |
| 353 | })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 354 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 355 | advanceClocks(time::milliseconds(10), 10); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 356 | |
| 357 | BOOST_REQUIRE_THROW(face->receive(Interest("/Hello/World/XXX/b/c")), InterestFilter::Error); |
| 358 | } |
| 359 | |
| 360 | BOOST_AUTO_TEST_CASE(SetRegexFilter) |
| 361 | { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 362 | size_t nInInterests = 0; |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 363 | face->setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"), |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 364 | bind([&nInInterests] { ++nInInterests; }), |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 365 | RegisterPrefixSuccessCallback(), |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 366 | bind([] { |
| 367 | BOOST_FAIL("Unexpected setInterestFilter failure"); |
| 368 | })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 369 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 370 | advanceClocks(time::milliseconds(10), 10); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 371 | |
| 372 | face->receive(Interest("/Hello/World/a")); // shouldn't match |
| 373 | BOOST_CHECK_EQUAL(nInInterests, 0); |
| 374 | |
| 375 | face->receive(Interest("/Hello/World/a/b")); // should match |
| 376 | BOOST_CHECK_EQUAL(nInInterests, 1); |
| 377 | |
| 378 | face->receive(Interest("/Hello/World/a/b/c")); // should match |
| 379 | BOOST_CHECK_EQUAL(nInInterests, 2); |
| 380 | |
| 381 | face->receive(Interest("/Hello/World/a/b/d")); // should not match |
| 382 | BOOST_CHECK_EQUAL(nInInterests, 2); |
| 383 | } |
| 384 | |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 385 | BOOST_AUTO_TEST_CASE(SetRegexFilterAndRegister) |
| 386 | { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 387 | size_t nInInterests = 0; |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 388 | face->setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"), |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 389 | bind([&nInInterests] { ++nInInterests; })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 390 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 391 | size_t nRegSuccesses = 0; |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 392 | face->registerPrefix("/Hello/World", |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 393 | bind([&nRegSuccesses] { ++nRegSuccesses; }), |
| 394 | bind([] { |
| 395 | BOOST_FAIL("Unexpected setInterestFilter failure"); |
| 396 | })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 397 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 398 | advanceClocks(time::milliseconds(10), 10); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 399 | BOOST_CHECK_EQUAL(nRegSuccesses, 1); |
| 400 | |
| 401 | face->receive(Interest("/Hello/World/a")); // shouldn't match |
| 402 | BOOST_CHECK_EQUAL(nInInterests, 0); |
| 403 | |
| 404 | face->receive(Interest("/Hello/World/a/b")); // should match |
| 405 | BOOST_CHECK_EQUAL(nInInterests, 1); |
| 406 | |
| 407 | face->receive(Interest("/Hello/World/a/b/c")); // should match |
| 408 | BOOST_CHECK_EQUAL(nInInterests, 2); |
| 409 | |
| 410 | face->receive(Interest("/Hello/World/a/b/d")); // should not match |
| 411 | BOOST_CHECK_EQUAL(nInInterests, 2); |
| 412 | } |
| 413 | |
Junxiao Shi | a1ea506 | 2014-12-27 22:33:39 -0700 | [diff] [blame] | 414 | BOOST_FIXTURE_TEST_CASE(SetInterestFilterNoReg, FacesNoRegistrationReplyFixture) // Bug 2318 |
| 415 | { |
| 416 | // This behavior is specific to DummyClientFace. |
| 417 | // Regular Face won't accept incoming packets until something is sent. |
| 418 | |
| 419 | int hit = 0; |
| 420 | face->setInterestFilter(Name("/"), bind([&hit] { ++hit; })); |
| 421 | face->processEvents(time::milliseconds(-1)); |
| 422 | |
| 423 | auto interest = make_shared<Interest>("/A"); |
| 424 | face->receive(*interest); |
| 425 | face->processEvents(time::milliseconds(-1)); |
| 426 | |
| 427 | BOOST_CHECK_EQUAL(hit, 1); |
| 428 | } |
| 429 | |
Alexander Afanasyev | 8e15854 | 2014-11-18 00:47:18 -0500 | [diff] [blame] | 430 | BOOST_AUTO_TEST_CASE(ProcessEvents) |
| 431 | { |
| 432 | face->processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside |
| 433 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 434 | size_t nRegSuccesses = 0; |
Alexander Afanasyev | 8e15854 | 2014-11-18 00:47:18 -0500 | [diff] [blame] | 435 | face->registerPrefix("/Hello/World", |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 436 | bind([&nRegSuccesses] { ++nRegSuccesses; }), |
| 437 | bind([] { |
| 438 | BOOST_FAIL("Unexpected setInterestFilter failure"); |
| 439 | })); |
Alexander Afanasyev | 8e15854 | 2014-11-18 00:47:18 -0500 | [diff] [blame] | 440 | |
| 441 | // io_service::poll() without reset |
| 442 | face->getIoService().poll(); |
Alexander Afanasyev | 8e15854 | 2014-11-18 00:47:18 -0500 | [diff] [blame] | 443 | BOOST_CHECK_EQUAL(nRegSuccesses, 0); |
| 444 | |
| 445 | face->processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside |
Alexander Afanasyev | 8e15854 | 2014-11-18 00:47:18 -0500 | [diff] [blame] | 446 | BOOST_CHECK_EQUAL(nRegSuccesses, 1); |
| 447 | } |
| 448 | |
Alexander Afanasyev | ea71967 | 2015-02-10 20:25:23 -0800 | [diff] [blame] | 449 | BOOST_AUTO_TEST_CASE(ExpressInterestWithLocalControlHeader) |
| 450 | { |
| 451 | Interest i("/Hello/World"); |
| 452 | i.setNextHopFaceId(1000); |
| 453 | i.setIncomingFaceId(2000); |
| 454 | |
| 455 | face->expressInterest(i, bind([]{}), bind([]{})); |
| 456 | advanceClocks(time::milliseconds(10)); |
| 457 | |
| 458 | BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 1); |
| 459 | // only NextHopFaceId is allowed to go out |
| 460 | BOOST_CHECK(face->sentInterests[0].getLocalControlHeader().hasNextHopFaceId()); |
| 461 | BOOST_CHECK(!face->sentInterests[0].getLocalControlHeader().hasIncomingFaceId()); |
| 462 | BOOST_CHECK_EQUAL(face->sentInterests[0].getNextHopFaceId(), 1000); |
| 463 | } |
| 464 | |
| 465 | BOOST_AUTO_TEST_CASE(ReceiveInterestWithLocalControlHeader) |
| 466 | { |
| 467 | face->setInterestFilter("/Hello/World", |
| 468 | [] (const InterestFilter&, const Interest& i) { |
| 469 | BOOST_CHECK(i.getLocalControlHeader().hasNextHopFaceId()); |
| 470 | BOOST_CHECK(i.getLocalControlHeader().hasIncomingFaceId()); |
| 471 | BOOST_CHECK_EQUAL(i.getNextHopFaceId(), 1000); |
| 472 | BOOST_CHECK_EQUAL(i.getIncomingFaceId(), 2000); |
| 473 | }, |
| 474 | bind([]{}), |
| 475 | bind([] { |
| 476 | BOOST_FAIL("Unexpected setInterestFilter failure"); |
| 477 | })); |
| 478 | advanceClocks(time::milliseconds(10)); |
| 479 | |
| 480 | Interest i("/Hello/World/!"); |
| 481 | i.setNextHopFaceId(1000); |
| 482 | i.setIncomingFaceId(2000); |
| 483 | |
| 484 | face->receive(i); |
| 485 | advanceClocks(time::milliseconds(10)); |
| 486 | } |
| 487 | |
| 488 | BOOST_AUTO_TEST_CASE(PutDataWithLocalControlHeader) |
| 489 | { |
| 490 | shared_ptr<Data> d = util::makeData("/Bye/World/!"); |
| 491 | d->setIncomingFaceId(2000); |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 492 | d->getLocalControlHeader().setNextHopFaceId(1000); // setNextHopFaceId is intentionally |
| 493 | // not exposed directly |
Alexander Afanasyev | ea71967 | 2015-02-10 20:25:23 -0800 | [diff] [blame] | 494 | |
| 495 | face->put(*d); |
| 496 | advanceClocks(time::milliseconds(10)); |
| 497 | |
| 498 | BOOST_REQUIRE_EQUAL(face->sentDatas.size(), 1); |
| 499 | BOOST_CHECK(!face->sentDatas[0].getLocalControlHeader().hasNextHopFaceId()); |
| 500 | BOOST_CHECK(!face->sentDatas[0].getLocalControlHeader().hasIncomingFaceId()); |
| 501 | } |
| 502 | |
| 503 | BOOST_AUTO_TEST_CASE(ReceiveDataWithLocalControlHeader) |
| 504 | { |
| 505 | face->expressInterest(Interest("/Hello/World", time::milliseconds(50)), |
| 506 | [&] (const Interest& i, const Data& d) { |
| 507 | BOOST_CHECK(d.getLocalControlHeader().hasNextHopFaceId()); |
| 508 | BOOST_CHECK(d.getLocalControlHeader().hasIncomingFaceId()); |
| 509 | BOOST_CHECK_EQUAL(d.getIncomingFaceId(), 2000); |
| 510 | BOOST_CHECK_EQUAL(d.getLocalControlHeader().getNextHopFaceId(), 1000); |
| 511 | }, |
| 512 | bind([] { |
| 513 | BOOST_FAIL("Unexpected timeout"); |
| 514 | })); |
| 515 | |
| 516 | advanceClocks(time::milliseconds(10)); |
| 517 | |
| 518 | shared_ptr<Data> d = util::makeData("/Hello/World/!"); |
| 519 | d->setIncomingFaceId(2000); |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 520 | d->getLocalControlHeader().setNextHopFaceId(1000); // setNextHopFaceId is intentionally |
| 521 | // not exposed directly |
Alexander Afanasyev | ea71967 | 2015-02-10 20:25:23 -0800 | [diff] [blame] | 522 | face->receive(*d); |
| 523 | |
| 524 | advanceClocks(time::milliseconds(10), 100); |
| 525 | } |
| 526 | |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 527 | BOOST_AUTO_TEST_CASE(DestructionWithoutCancellingPendingInterests) // Bug #2518 |
| 528 | { |
| 529 | face->expressInterest(Interest("/Hello/World", time::milliseconds(50)), |
| 530 | bind([]{}), bind([]{})); |
| 531 | advanceClocks(time::milliseconds(10), 10); |
| 532 | |
| 533 | face.reset(); |
| 534 | |
| 535 | advanceClocks(time::milliseconds(10), 10); |
| 536 | // should not segfault |
| 537 | } |
| 538 | |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 539 | BOOST_AUTO_TEST_SUITE_END() |
| 540 | |
| 541 | } // tests |
| 542 | } // namespace ndn |