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 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 191 | BOOST_FIXTURE_TEST_CASE(SetInterestFilterFail, FacesNoRegistrationReplyFixture) |
| 192 | { |
| 193 | // don't enable registration reply |
| 194 | size_t nRegFailed = 0; |
| 195 | face->setInterestFilter("/Hello/World", |
| 196 | bind([] { |
| 197 | BOOST_FAIL("Unexpected Interest"); |
| 198 | }), |
| 199 | bind([] { |
| 200 | BOOST_FAIL("Unexpected success of setInterestFilter"); |
| 201 | }), |
| 202 | bind([&nRegFailed] { |
| 203 | ++nRegFailed; |
| 204 | })); |
| 205 | |
| 206 | advanceClocks(time::milliseconds(10), 10); |
| 207 | BOOST_CHECK_EQUAL(nRegFailed, 0); |
| 208 | |
| 209 | advanceClocks(time::milliseconds(1000), 10); |
| 210 | BOOST_CHECK_EQUAL(nRegFailed, 1); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | BOOST_AUTO_TEST_CASE(RegisterUnregisterPrefix) |
| 214 | { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 215 | size_t nRegSuccesses = 0; |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 216 | const RegisteredPrefixId* regPrefixId = |
| 217 | face->registerPrefix("/Hello/World", |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 218 | bind([&nRegSuccesses] { ++nRegSuccesses; }), |
| 219 | bind([] { |
| 220 | BOOST_FAIL("Unexpected registerPrefix failure"); |
| 221 | })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 222 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 223 | advanceClocks(time::milliseconds(10), 10); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 224 | BOOST_CHECK_EQUAL(nRegSuccesses, 1); |
| 225 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 226 | size_t nUnregSuccesses = 0; |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 227 | face->unregisterPrefix(regPrefixId, |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 228 | bind([&nUnregSuccesses] { ++nUnregSuccesses; }), |
| 229 | bind([] { |
| 230 | BOOST_FAIL("Unexpected unregisterPrefix failure"); |
| 231 | })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 232 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 233 | advanceClocks(time::milliseconds(10), 10); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 234 | BOOST_CHECK_EQUAL(nUnregSuccesses, 1); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 235 | } |
| 236 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 237 | BOOST_FIXTURE_TEST_CASE(RegisterUnregisterPrefixFail, FacesNoRegistrationReplyFixture) |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 238 | { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 239 | size_t nRegFailures = 0; |
| 240 | face->registerPrefix("/Hello/World", |
| 241 | bind([] { |
| 242 | BOOST_FAIL("Unexpected registerPrefix success"); |
| 243 | }), |
| 244 | bind([&nRegFailures] { ++nRegFailures; })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 245 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 246 | advanceClocks(time::milliseconds(1000), 100); |
| 247 | BOOST_CHECK_EQUAL(nRegFailures, 1); |
| 248 | } |
| 249 | |
| 250 | BOOST_AUTO_TEST_CASE(SimilarFilters) |
| 251 | { |
| 252 | size_t nInInterests1 = 0; |
| 253 | face->setInterestFilter("/Hello/World", |
| 254 | bind([&nInInterests1] { ++nInInterests1; }), |
| 255 | RegisterPrefixSuccessCallback(), |
| 256 | bind([] { |
| 257 | BOOST_FAIL("Unexpected setInterestFilter failure"); |
| 258 | })); |
| 259 | |
| 260 | size_t nInInterests2 = 0; |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 261 | face->setInterestFilter("/Hello", |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 262 | bind([&nInInterests2] { ++nInInterests2; }), |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 263 | RegisterPrefixSuccessCallback(), |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 264 | bind([] { |
| 265 | BOOST_FAIL("Unexpected setInterestFilter failure"); |
| 266 | })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 267 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 268 | size_t nInInterests3 = 0; |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 269 | face->setInterestFilter("/Los/Angeles/Lakers", |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 270 | bind([&nInInterests3] { ++nInInterests3; }), |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 271 | RegisterPrefixSuccessCallback(), |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 272 | bind([] { |
| 273 | BOOST_FAIL("Unexpected setInterestFilter failure"); |
| 274 | })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 275 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 276 | advanceClocks(time::milliseconds(10), 10); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 277 | |
| 278 | face->receive(Interest("/Hello/World/!")); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 279 | advanceClocks(time::milliseconds(10), 10); |
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 | BOOST_CHECK_EQUAL(nInInterests1, 1); |
| 282 | BOOST_CHECK_EQUAL(nInInterests2, 1); |
| 283 | BOOST_CHECK_EQUAL(nInInterests3, 0); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | BOOST_AUTO_TEST_CASE(SetRegexFilterError) |
| 287 | { |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 288 | face->setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"), |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 289 | [] (const Name&, const Interest&) { |
| 290 | BOOST_FAIL("InterestFilter::Error should have been triggered"); |
| 291 | }, |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 292 | RegisterPrefixSuccessCallback(), |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 293 | bind([] { |
| 294 | BOOST_FAIL("Unexpected setInterestFilter failure"); |
| 295 | })); |
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 | advanceClocks(time::milliseconds(10), 10); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 298 | |
| 299 | BOOST_REQUIRE_THROW(face->receive(Interest("/Hello/World/XXX/b/c")), InterestFilter::Error); |
| 300 | } |
| 301 | |
| 302 | BOOST_AUTO_TEST_CASE(SetRegexFilter) |
| 303 | { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 304 | size_t nInInterests = 0; |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 305 | face->setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"), |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 306 | bind([&nInInterests] { ++nInInterests; }), |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 307 | RegisterPrefixSuccessCallback(), |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 308 | bind([] { |
| 309 | BOOST_FAIL("Unexpected setInterestFilter failure"); |
| 310 | })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 311 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 312 | advanceClocks(time::milliseconds(10), 10); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 313 | |
| 314 | face->receive(Interest("/Hello/World/a")); // shouldn't match |
| 315 | BOOST_CHECK_EQUAL(nInInterests, 0); |
| 316 | |
| 317 | face->receive(Interest("/Hello/World/a/b")); // should match |
| 318 | BOOST_CHECK_EQUAL(nInInterests, 1); |
| 319 | |
| 320 | face->receive(Interest("/Hello/World/a/b/c")); // should match |
| 321 | BOOST_CHECK_EQUAL(nInInterests, 2); |
| 322 | |
| 323 | face->receive(Interest("/Hello/World/a/b/d")); // should not match |
| 324 | BOOST_CHECK_EQUAL(nInInterests, 2); |
| 325 | } |
| 326 | |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 327 | BOOST_AUTO_TEST_CASE(SetRegexFilterAndRegister) |
| 328 | { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 329 | size_t nInInterests = 0; |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 330 | face->setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"), |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 331 | bind([&nInInterests] { ++nInInterests; })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 332 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 333 | size_t nRegSuccesses = 0; |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 334 | face->registerPrefix("/Hello/World", |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 335 | bind([&nRegSuccesses] { ++nRegSuccesses; }), |
| 336 | bind([] { |
| 337 | BOOST_FAIL("Unexpected setInterestFilter failure"); |
| 338 | })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 339 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 340 | advanceClocks(time::milliseconds(10), 10); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 341 | BOOST_CHECK_EQUAL(nRegSuccesses, 1); |
| 342 | |
| 343 | face->receive(Interest("/Hello/World/a")); // shouldn't match |
| 344 | BOOST_CHECK_EQUAL(nInInterests, 0); |
| 345 | |
| 346 | face->receive(Interest("/Hello/World/a/b")); // should match |
| 347 | BOOST_CHECK_EQUAL(nInInterests, 1); |
| 348 | |
| 349 | face->receive(Interest("/Hello/World/a/b/c")); // should match |
| 350 | BOOST_CHECK_EQUAL(nInInterests, 2); |
| 351 | |
| 352 | face->receive(Interest("/Hello/World/a/b/d")); // should not match |
| 353 | BOOST_CHECK_EQUAL(nInInterests, 2); |
| 354 | } |
| 355 | |
Junxiao Shi | a1ea506 | 2014-12-27 22:33:39 -0700 | [diff] [blame] | 356 | BOOST_FIXTURE_TEST_CASE(SetInterestFilterNoReg, FacesNoRegistrationReplyFixture) // Bug 2318 |
| 357 | { |
| 358 | // This behavior is specific to DummyClientFace. |
| 359 | // Regular Face won't accept incoming packets until something is sent. |
| 360 | |
| 361 | int hit = 0; |
| 362 | face->setInterestFilter(Name("/"), bind([&hit] { ++hit; })); |
| 363 | face->processEvents(time::milliseconds(-1)); |
| 364 | |
| 365 | auto interest = make_shared<Interest>("/A"); |
| 366 | face->receive(*interest); |
| 367 | face->processEvents(time::milliseconds(-1)); |
| 368 | |
| 369 | BOOST_CHECK_EQUAL(hit, 1); |
| 370 | } |
| 371 | |
Alexander Afanasyev | 8e15854 | 2014-11-18 00:47:18 -0500 | [diff] [blame] | 372 | BOOST_AUTO_TEST_CASE(ProcessEvents) |
| 373 | { |
| 374 | face->processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside |
| 375 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 376 | size_t nRegSuccesses = 0; |
Alexander Afanasyev | 8e15854 | 2014-11-18 00:47:18 -0500 | [diff] [blame] | 377 | face->registerPrefix("/Hello/World", |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 378 | bind([&nRegSuccesses] { ++nRegSuccesses; }), |
| 379 | bind([] { |
| 380 | BOOST_FAIL("Unexpected setInterestFilter failure"); |
| 381 | })); |
Alexander Afanasyev | 8e15854 | 2014-11-18 00:47:18 -0500 | [diff] [blame] | 382 | |
| 383 | // io_service::poll() without reset |
| 384 | face->getIoService().poll(); |
Alexander Afanasyev | 8e15854 | 2014-11-18 00:47:18 -0500 | [diff] [blame] | 385 | BOOST_CHECK_EQUAL(nRegSuccesses, 0); |
| 386 | |
| 387 | face->processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside |
Alexander Afanasyev | 8e15854 | 2014-11-18 00:47:18 -0500 | [diff] [blame] | 388 | BOOST_CHECK_EQUAL(nRegSuccesses, 1); |
| 389 | } |
| 390 | |
Alexander Afanasyev | ea71967 | 2015-02-10 20:25:23 -0800 | [diff] [blame] | 391 | BOOST_AUTO_TEST_CASE(ExpressInterestWithLocalControlHeader) |
| 392 | { |
| 393 | Interest i("/Hello/World"); |
| 394 | i.setNextHopFaceId(1000); |
| 395 | i.setIncomingFaceId(2000); |
| 396 | |
| 397 | face->expressInterest(i, bind([]{}), bind([]{})); |
| 398 | advanceClocks(time::milliseconds(10)); |
| 399 | |
| 400 | BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 1); |
| 401 | // only NextHopFaceId is allowed to go out |
| 402 | BOOST_CHECK(face->sentInterests[0].getLocalControlHeader().hasNextHopFaceId()); |
| 403 | BOOST_CHECK(!face->sentInterests[0].getLocalControlHeader().hasIncomingFaceId()); |
| 404 | BOOST_CHECK_EQUAL(face->sentInterests[0].getNextHopFaceId(), 1000); |
| 405 | } |
| 406 | |
| 407 | BOOST_AUTO_TEST_CASE(ReceiveInterestWithLocalControlHeader) |
| 408 | { |
| 409 | face->setInterestFilter("/Hello/World", |
| 410 | [] (const InterestFilter&, const Interest& i) { |
| 411 | BOOST_CHECK(i.getLocalControlHeader().hasNextHopFaceId()); |
| 412 | BOOST_CHECK(i.getLocalControlHeader().hasIncomingFaceId()); |
| 413 | BOOST_CHECK_EQUAL(i.getNextHopFaceId(), 1000); |
| 414 | BOOST_CHECK_EQUAL(i.getIncomingFaceId(), 2000); |
| 415 | }, |
| 416 | bind([]{}), |
| 417 | bind([] { |
| 418 | BOOST_FAIL("Unexpected setInterestFilter failure"); |
| 419 | })); |
| 420 | advanceClocks(time::milliseconds(10)); |
| 421 | |
| 422 | Interest i("/Hello/World/!"); |
| 423 | i.setNextHopFaceId(1000); |
| 424 | i.setIncomingFaceId(2000); |
| 425 | |
| 426 | face->receive(i); |
| 427 | advanceClocks(time::milliseconds(10)); |
| 428 | } |
| 429 | |
| 430 | BOOST_AUTO_TEST_CASE(PutDataWithLocalControlHeader) |
| 431 | { |
| 432 | shared_ptr<Data> d = util::makeData("/Bye/World/!"); |
| 433 | d->setIncomingFaceId(2000); |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 434 | d->getLocalControlHeader().setNextHopFaceId(1000); // setNextHopFaceId is intentionally |
| 435 | // not exposed directly |
Alexander Afanasyev | ea71967 | 2015-02-10 20:25:23 -0800 | [diff] [blame] | 436 | |
| 437 | face->put(*d); |
| 438 | advanceClocks(time::milliseconds(10)); |
| 439 | |
| 440 | BOOST_REQUIRE_EQUAL(face->sentDatas.size(), 1); |
| 441 | BOOST_CHECK(!face->sentDatas[0].getLocalControlHeader().hasNextHopFaceId()); |
| 442 | BOOST_CHECK(!face->sentDatas[0].getLocalControlHeader().hasIncomingFaceId()); |
| 443 | } |
| 444 | |
| 445 | BOOST_AUTO_TEST_CASE(ReceiveDataWithLocalControlHeader) |
| 446 | { |
| 447 | face->expressInterest(Interest("/Hello/World", time::milliseconds(50)), |
| 448 | [&] (const Interest& i, const Data& d) { |
| 449 | BOOST_CHECK(d.getLocalControlHeader().hasNextHopFaceId()); |
| 450 | BOOST_CHECK(d.getLocalControlHeader().hasIncomingFaceId()); |
| 451 | BOOST_CHECK_EQUAL(d.getIncomingFaceId(), 2000); |
| 452 | BOOST_CHECK_EQUAL(d.getLocalControlHeader().getNextHopFaceId(), 1000); |
| 453 | }, |
| 454 | bind([] { |
| 455 | BOOST_FAIL("Unexpected timeout"); |
| 456 | })); |
| 457 | |
| 458 | advanceClocks(time::milliseconds(10)); |
| 459 | |
| 460 | shared_ptr<Data> d = util::makeData("/Hello/World/!"); |
| 461 | d->setIncomingFaceId(2000); |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 462 | d->getLocalControlHeader().setNextHopFaceId(1000); // setNextHopFaceId is intentionally |
| 463 | // not exposed directly |
Alexander Afanasyev | ea71967 | 2015-02-10 20:25:23 -0800 | [diff] [blame] | 464 | face->receive(*d); |
| 465 | |
| 466 | advanceClocks(time::milliseconds(10), 100); |
| 467 | } |
| 468 | |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 469 | BOOST_AUTO_TEST_CASE(DestructionWithoutCancellingPendingInterests) // Bug #2518 |
| 470 | { |
| 471 | face->expressInterest(Interest("/Hello/World", time::milliseconds(50)), |
| 472 | bind([]{}), bind([]{})); |
| 473 | advanceClocks(time::milliseconds(10), 10); |
| 474 | |
| 475 | face.reset(); |
| 476 | |
| 477 | advanceClocks(time::milliseconds(10), 10); |
| 478 | // should not segfault |
| 479 | } |
| 480 | |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 481 | BOOST_AUTO_TEST_SUITE_END() |
| 482 | |
| 483 | } // tests |
| 484 | } // namespace ndn |