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 | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame^] | 26 | #include "transport/tcp-transport.hpp" |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 27 | |
| 28 | #include "boost-test.hpp" |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 29 | #include "unit-test-time-fixture.hpp" |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 30 | #include "make-interest-data.hpp" |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 31 | |
| 32 | namespace ndn { |
| 33 | namespace tests { |
| 34 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 35 | using ndn::util::DummyClientFace; |
| 36 | using ndn::util::makeDummyClientFace; |
| 37 | |
Junxiao Shi | a1ea506 | 2014-12-27 22:33:39 -0700 | [diff] [blame] | 38 | class FaceFixture : public UnitTestTimeFixture |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 39 | { |
| 40 | public: |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 41 | explicit |
Junxiao Shi | a1ea506 | 2014-12-27 22:33:39 -0700 | [diff] [blame] | 42 | FaceFixture(bool enableRegistrationReply = true) |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 43 | : face(makeDummyClientFace(io, { true, enableRegistrationReply })) |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 44 | { |
| 45 | } |
| 46 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 47 | public: |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 48 | shared_ptr<DummyClientFace> face; |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 49 | }; |
| 50 | |
Junxiao Shi | a1ea506 | 2014-12-27 22:33:39 -0700 | [diff] [blame] | 51 | class FacesNoRegistrationReplyFixture : public FaceFixture |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 52 | { |
| 53 | public: |
| 54 | FacesNoRegistrationReplyFixture() |
Junxiao Shi | a1ea506 | 2014-12-27 22:33:39 -0700 | [diff] [blame] | 55 | : FaceFixture(false) |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 56 | { |
| 57 | } |
| 58 | }; |
| 59 | |
Junxiao Shi | a1ea506 | 2014-12-27 22:33:39 -0700 | [diff] [blame] | 60 | BOOST_FIXTURE_TEST_SUITE(TestFace, FaceFixture) |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 61 | |
| 62 | BOOST_AUTO_TEST_CASE(ExpressInterestData) |
| 63 | { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 64 | size_t nData = 0; |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 65 | face->expressInterest(Interest("/Hello/World", time::milliseconds(50)), |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 66 | [&] (const Interest& i, const Data& d) { |
| 67 | BOOST_CHECK(i.getName().isPrefixOf(d.getName())); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 68 | BOOST_CHECK_EQUAL(i.getName(), "/Hello/World"); |
| 69 | ++nData; |
| 70 | }, |
| 71 | bind([] { |
| 72 | BOOST_FAIL("Unexpected Nack"); |
| 73 | }), |
| 74 | bind([] { |
| 75 | BOOST_FAIL("Unexpected timeout"); |
| 76 | })); |
| 77 | |
| 78 | advanceClocks(time::milliseconds(1), 40); |
| 79 | |
| 80 | face->receive(*util::makeData("/Bye/World/a")); |
| 81 | face->receive(*util::makeData("/Hello/World/a")); |
| 82 | |
| 83 | advanceClocks(time::milliseconds(1), 100); |
| 84 | |
| 85 | BOOST_CHECK_EQUAL(nData, 1); |
| 86 | BOOST_CHECK_EQUAL(face->sentInterests.size(), 1); |
| 87 | BOOST_CHECK_EQUAL(face->sentDatas.size(), 0); |
| 88 | |
| 89 | size_t nTimeouts = 0; |
| 90 | face->expressInterest(Interest("/Hello/World/a/2", time::milliseconds(50)), |
| 91 | bind([]{}), |
| 92 | bind([]{}), |
| 93 | bind([&nTimeouts] { |
| 94 | ++nTimeouts; |
| 95 | })); |
| 96 | advanceClocks(time::milliseconds(10), 100); |
| 97 | BOOST_CHECK_EQUAL(nTimeouts, 1); |
| 98 | } |
| 99 | |
| 100 | // test case for deprecated expressInterest implementation |
| 101 | BOOST_AUTO_TEST_CASE(DeprecatedExpressInterestData) |
| 102 | { |
| 103 | size_t nData = 0; |
| 104 | face->expressInterest(Interest("/Hello/World", time::milliseconds(50)), |
| 105 | [&] (const Interest& i, const Data& d) { |
| 106 | BOOST_CHECK(i.getName().isPrefixOf(d.getName())); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 107 | ++nData; |
| 108 | }, |
| 109 | bind([] { |
| 110 | BOOST_FAIL("Unexpected timeout"); |
| 111 | })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 112 | |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 113 | advanceClocks(time::milliseconds(1), 40); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 114 | |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 115 | face->receive(*util::makeData("/Bye/World/a")); |
| 116 | face->receive(*util::makeData("/Hello/World/a")); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 117 | |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 118 | advanceClocks(time::milliseconds(1), 100); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 119 | |
| 120 | BOOST_CHECK_EQUAL(nData, 1); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 121 | BOOST_CHECK_EQUAL(face->sentInterests.size(), 1); |
| 122 | BOOST_CHECK_EQUAL(face->sentDatas.size(), 0); |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 123 | |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 124 | face->expressInterest(Interest("/Hello/World/a", time::milliseconds(50)), |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 125 | [&] (const Interest& i, const Data& d) { |
| 126 | BOOST_CHECK(i.getName().isPrefixOf(d.getName())); |
| 127 | ++nData; |
| 128 | }, |
| 129 | bind([] { |
| 130 | BOOST_FAIL("Unexpected timeout"); |
| 131 | })); |
| 132 | advanceClocks(time::milliseconds(1), 40); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 133 | face->receive(*util::makeData("/Hello/World/a/1/xxxxx")); |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 134 | |
| 135 | advanceClocks(time::milliseconds(1), 100); |
| 136 | |
| 137 | BOOST_CHECK_EQUAL(nData, 2); |
| 138 | BOOST_CHECK_EQUAL(face->sentInterests.size(), 2); |
| 139 | BOOST_CHECK_EQUAL(face->sentDatas.size(), 0); |
| 140 | |
| 141 | size_t nTimeouts = 0; |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 142 | face->expressInterest(Interest("/Hello/World/a/2", time::milliseconds(50)), |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 143 | bind([]{}), |
| 144 | bind([&nTimeouts] { |
| 145 | ++nTimeouts; |
| 146 | })); |
| 147 | advanceClocks(time::milliseconds(10), 100); |
| 148 | BOOST_CHECK_EQUAL(nTimeouts, 1); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | BOOST_AUTO_TEST_CASE(ExpressInterestTimeout) |
| 152 | { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 153 | size_t nTimeouts = 0; |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 154 | face->expressInterest(Interest("/Hello/World", time::milliseconds(50)), |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 155 | bind([] { |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 156 | BOOST_FAIL("Unexpected ata"); |
| 157 | }), |
| 158 | bind([] { |
| 159 | BOOST_FAIL("Unexpected Nack"); |
| 160 | }), |
| 161 | [&nTimeouts] (const Interest& i) { |
| 162 | BOOST_CHECK_EQUAL(i.getName(), "/Hello/World"); |
| 163 | ++nTimeouts; |
| 164 | }); |
| 165 | |
| 166 | advanceClocks(time::milliseconds(10), 100); |
| 167 | |
| 168 | BOOST_CHECK_EQUAL(nTimeouts, 1); |
| 169 | BOOST_CHECK_EQUAL(face->sentInterests.size(), 1); |
| 170 | BOOST_CHECK_EQUAL(face->sentDatas.size(), 0); |
| 171 | BOOST_CHECK_EQUAL(face->sentNacks.size(), 0); |
| 172 | } |
| 173 | |
| 174 | // test case for deprecated expressInterest implementation |
| 175 | BOOST_AUTO_TEST_CASE(DeprecatedExpressInterestTimeout) |
| 176 | { |
| 177 | size_t nTimeouts = 0; |
| 178 | face->expressInterest(Interest("/Hello/World", time::milliseconds(50)), |
| 179 | bind([] { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 180 | BOOST_FAIL("Unexpected data"); |
| 181 | }), |
| 182 | bind([&nTimeouts] { |
| 183 | ++nTimeouts; |
| 184 | })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 185 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 186 | advanceClocks(time::milliseconds(10), 100); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 187 | |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 188 | BOOST_CHECK_EQUAL(nTimeouts, 1); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 189 | BOOST_CHECK_EQUAL(face->sentInterests.size(), 1); |
| 190 | BOOST_CHECK_EQUAL(face->sentDatas.size(), 0); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 191 | } |
| 192 | |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 193 | BOOST_AUTO_TEST_CASE(ExpressInterestNack) |
| 194 | { |
| 195 | size_t nNacks = 0; |
| 196 | |
| 197 | Interest interest("/Hello/World", time::milliseconds(50)); |
| 198 | |
| 199 | face->expressInterest(interest, |
| 200 | bind([] { |
| 201 | BOOST_FAIL("Unexpected Data"); |
| 202 | }), |
| 203 | [&] (const Interest& i, const lp::Nack& n) { |
| 204 | BOOST_CHECK(i.getName().isPrefixOf(n.getInterest().getName())); |
| 205 | BOOST_CHECK_EQUAL(i.getName(), "/Hello/World"); |
| 206 | BOOST_CHECK_EQUAL(n.getReason(), lp::NackReason::DUPLICATE); |
| 207 | ++nNacks; |
| 208 | }, |
| 209 | bind([] { |
| 210 | BOOST_FAIL("Unexpected timeout"); |
| 211 | })); |
| 212 | |
| 213 | advanceClocks(time::milliseconds(1), 40); |
| 214 | |
| 215 | lp::Nack nack(face->sentInterests[0]); |
| 216 | nack.setReason(lp::NackReason::DUPLICATE); |
| 217 | |
| 218 | BOOST_CHECK_EQUAL(face->sentNacks.size(), 0); |
| 219 | |
| 220 | face->receive(nack); |
| 221 | |
| 222 | advanceClocks(time::milliseconds(1), 100); |
| 223 | |
| 224 | BOOST_CHECK_EQUAL(nNacks, 1); |
| 225 | BOOST_CHECK_EQUAL(face->sentInterests.size(), 1); |
| 226 | } |
| 227 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 228 | BOOST_AUTO_TEST_CASE(RemovePendingInterest) |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 229 | { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 230 | const PendingInterestId* interestId = |
| 231 | face->expressInterest(Interest("/Hello/World", time::milliseconds(50)), |
| 232 | bind([] { |
| 233 | BOOST_FAIL("Unexpected data"); |
| 234 | }), |
| 235 | bind([] { |
| 236 | BOOST_FAIL("Unexpected timeout"); |
| 237 | })); |
| 238 | advanceClocks(time::milliseconds(10)); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 239 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 240 | face->removePendingInterest(interestId); |
| 241 | advanceClocks(time::milliseconds(10)); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 242 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 243 | face->receive(*util::makeData("/Hello/World/!")); |
| 244 | advanceClocks(time::milliseconds(10), 100); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 245 | } |
| 246 | |
Ilya Moiseenko | 56b0bf8 | 2015-11-08 11:14:28 -0500 | [diff] [blame] | 247 | BOOST_AUTO_TEST_CASE(removeAllPendingInterests) |
| 248 | { |
| 249 | face->expressInterest(Interest("/Hello/World/0", time::milliseconds(50)), |
| 250 | bind([] { BOOST_FAIL("Unexpected data"); }), |
| 251 | bind([] { BOOST_FAIL("Unexpected nack"); }), |
| 252 | bind([] { BOOST_FAIL("Unexpected timeout"); })); |
| 253 | |
| 254 | face->expressInterest(Interest("/Hello/World/1", time::milliseconds(50)), |
| 255 | bind([] { BOOST_FAIL("Unexpected data"); }), |
| 256 | bind([] { BOOST_FAIL("Unexpected nack"); }), |
| 257 | bind([] { BOOST_FAIL("Unexpected timeout"); })); |
| 258 | |
| 259 | advanceClocks(time::milliseconds(10)); |
| 260 | |
| 261 | face->removeAllPendingInterests(); |
| 262 | advanceClocks(time::milliseconds(10)); |
| 263 | |
| 264 | BOOST_CHECK_EQUAL(face->getNPendingInterests(), 0); |
| 265 | |
| 266 | face->receive(*util::makeData("/Hello/World/0")); |
| 267 | face->receive(*util::makeData("/Hello/World/1")); |
| 268 | advanceClocks(time::milliseconds(10), 100); |
| 269 | } |
| 270 | |
| 271 | |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 272 | BOOST_AUTO_TEST_CASE(SetUnsetInterestFilter) |
| 273 | { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 274 | size_t nInterests = 0; |
| 275 | size_t nRegs = 0; |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 276 | const RegisteredPrefixId* regPrefixId = |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 277 | face->setInterestFilter("/Hello/World", |
| 278 | bind([&nInterests] { ++nInterests; }), |
| 279 | bind([&nRegs] { ++nRegs; }), |
| 280 | bind([] { |
| 281 | BOOST_FAIL("Unexpected setInterestFilter failure"); |
| 282 | })); |
| 283 | advanceClocks(time::milliseconds(10), 10); |
| 284 | BOOST_CHECK_EQUAL(nRegs, 1); |
| 285 | BOOST_CHECK_EQUAL(nInterests, 0); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 286 | |
| 287 | face->receive(Interest("/Hello/World/!")); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 288 | advanceClocks(time::milliseconds(10), 10); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 289 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 290 | BOOST_CHECK_EQUAL(nRegs, 1); |
| 291 | BOOST_CHECK_EQUAL(nInterests, 1); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 292 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 293 | face->receive(Interest("/Bye/World/!")); |
| 294 | advanceClocks(time::milliseconds(10000), 10); |
| 295 | BOOST_CHECK_EQUAL(nInterests, 1); |
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 | face->receive(Interest("/Hello/World/!/2")); |
| 298 | advanceClocks(time::milliseconds(10), 10); |
| 299 | BOOST_CHECK_EQUAL(nInterests, 2); |
| 300 | |
| 301 | // removing filter |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 302 | face->unsetInterestFilter(regPrefixId); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 303 | advanceClocks(time::milliseconds(10), 10); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 304 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 305 | face->receive(Interest("/Hello/World/!/3")); |
| 306 | BOOST_CHECK_EQUAL(nInterests, 2); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 307 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 308 | face->unsetInterestFilter(static_cast<const RegisteredPrefixId*>(0)); |
| 309 | advanceClocks(time::milliseconds(10), 10); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 310 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 311 | face->unsetInterestFilter(static_cast<const InterestFilterId*>(0)); |
| 312 | advanceClocks(time::milliseconds(10), 10); |
| 313 | } |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 314 | |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 315 | BOOST_AUTO_TEST_CASE(SetUnsetInterestFilterWithoutSucessCallback) |
| 316 | { |
| 317 | size_t nInterests = 0; |
| 318 | const RegisteredPrefixId* regPrefixId = |
| 319 | face->setInterestFilter("/Hello/World", |
| 320 | bind([&nInterests] { ++nInterests; }), |
| 321 | bind([] { |
| 322 | BOOST_FAIL("Unexpected setInterestFilter failure"); |
| 323 | })); |
| 324 | advanceClocks(time::milliseconds(10), 10); |
| 325 | BOOST_CHECK_EQUAL(nInterests, 0); |
| 326 | |
| 327 | face->receive(Interest("/Hello/World/!")); |
| 328 | advanceClocks(time::milliseconds(10), 10); |
| 329 | |
| 330 | BOOST_CHECK_EQUAL(nInterests, 1); |
| 331 | |
| 332 | face->receive(Interest("/Bye/World/!")); |
| 333 | advanceClocks(time::milliseconds(10000), 10); |
| 334 | BOOST_CHECK_EQUAL(nInterests, 1); |
| 335 | |
| 336 | face->receive(Interest("/Hello/World/!/2")); |
| 337 | advanceClocks(time::milliseconds(10), 10); |
| 338 | BOOST_CHECK_EQUAL(nInterests, 2); |
| 339 | |
| 340 | // removing filter |
| 341 | face->unsetInterestFilter(regPrefixId); |
| 342 | advanceClocks(time::milliseconds(10), 10); |
| 343 | |
| 344 | face->receive(Interest("/Hello/World/!/3")); |
| 345 | BOOST_CHECK_EQUAL(nInterests, 2); |
| 346 | |
| 347 | face->unsetInterestFilter(static_cast<const RegisteredPrefixId*>(0)); |
| 348 | advanceClocks(time::milliseconds(10), 10); |
| 349 | |
| 350 | face->unsetInterestFilter(static_cast<const InterestFilterId*>(0)); |
| 351 | advanceClocks(time::milliseconds(10), 10); |
| 352 | } |
| 353 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 354 | BOOST_FIXTURE_TEST_CASE(SetInterestFilterFail, FacesNoRegistrationReplyFixture) |
| 355 | { |
| 356 | // don't enable registration reply |
| 357 | size_t nRegFailed = 0; |
| 358 | face->setInterestFilter("/Hello/World", |
| 359 | bind([] { |
| 360 | BOOST_FAIL("Unexpected Interest"); |
| 361 | }), |
| 362 | bind([] { |
| 363 | BOOST_FAIL("Unexpected success of setInterestFilter"); |
| 364 | }), |
| 365 | bind([&nRegFailed] { |
| 366 | ++nRegFailed; |
| 367 | })); |
| 368 | |
| 369 | advanceClocks(time::milliseconds(10), 10); |
| 370 | BOOST_CHECK_EQUAL(nRegFailed, 0); |
| 371 | |
| 372 | advanceClocks(time::milliseconds(1000), 10); |
| 373 | BOOST_CHECK_EQUAL(nRegFailed, 1); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 374 | } |
| 375 | |
Joao Pereira | 0b3cac5 | 2015-07-02 14:49:49 -0400 | [diff] [blame] | 376 | BOOST_FIXTURE_TEST_CASE(SetInterestFilterFailWithoutSuccessCallback, FacesNoRegistrationReplyFixture) |
| 377 | { |
| 378 | // don't enable registration reply |
| 379 | size_t nRegFailed = 0; |
| 380 | face->setInterestFilter("/Hello/World", |
| 381 | bind([] { |
| 382 | BOOST_FAIL("Unexpected Interest"); |
| 383 | }), |
| 384 | bind([&nRegFailed] { |
| 385 | ++nRegFailed; |
| 386 | })); |
| 387 | |
| 388 | advanceClocks(time::milliseconds(10), 10); |
| 389 | BOOST_CHECK_EQUAL(nRegFailed, 0); |
| 390 | |
| 391 | advanceClocks(time::milliseconds(1000), 10); |
| 392 | BOOST_CHECK_EQUAL(nRegFailed, 1); |
| 393 | } |
| 394 | |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 395 | BOOST_AUTO_TEST_CASE(RegisterUnregisterPrefix) |
| 396 | { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 397 | size_t nRegSuccesses = 0; |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 398 | const RegisteredPrefixId* regPrefixId = |
| 399 | face->registerPrefix("/Hello/World", |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 400 | bind([&nRegSuccesses] { ++nRegSuccesses; }), |
| 401 | bind([] { |
| 402 | BOOST_FAIL("Unexpected registerPrefix failure"); |
| 403 | })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 404 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 405 | advanceClocks(time::milliseconds(10), 10); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 406 | BOOST_CHECK_EQUAL(nRegSuccesses, 1); |
| 407 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 408 | size_t nUnregSuccesses = 0; |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 409 | face->unregisterPrefix(regPrefixId, |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 410 | bind([&nUnregSuccesses] { ++nUnregSuccesses; }), |
| 411 | bind([] { |
| 412 | BOOST_FAIL("Unexpected unregisterPrefix failure"); |
| 413 | })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 414 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 415 | advanceClocks(time::milliseconds(10), 10); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 416 | BOOST_CHECK_EQUAL(nUnregSuccesses, 1); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 417 | } |
| 418 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 419 | BOOST_FIXTURE_TEST_CASE(RegisterUnregisterPrefixFail, FacesNoRegistrationReplyFixture) |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 420 | { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 421 | size_t nRegFailures = 0; |
| 422 | face->registerPrefix("/Hello/World", |
| 423 | bind([] { |
| 424 | BOOST_FAIL("Unexpected registerPrefix success"); |
| 425 | }), |
| 426 | bind([&nRegFailures] { ++nRegFailures; })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 427 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 428 | advanceClocks(time::milliseconds(1000), 100); |
| 429 | BOOST_CHECK_EQUAL(nRegFailures, 1); |
| 430 | } |
| 431 | |
| 432 | BOOST_AUTO_TEST_CASE(SimilarFilters) |
| 433 | { |
| 434 | size_t nInInterests1 = 0; |
| 435 | face->setInterestFilter("/Hello/World", |
| 436 | bind([&nInInterests1] { ++nInInterests1; }), |
| 437 | RegisterPrefixSuccessCallback(), |
| 438 | bind([] { |
| 439 | BOOST_FAIL("Unexpected setInterestFilter failure"); |
| 440 | })); |
| 441 | |
| 442 | size_t nInInterests2 = 0; |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 443 | face->setInterestFilter("/Hello", |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 444 | bind([&nInInterests2] { ++nInInterests2; }), |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 445 | RegisterPrefixSuccessCallback(), |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 446 | bind([] { |
| 447 | BOOST_FAIL("Unexpected setInterestFilter failure"); |
| 448 | })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 449 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 450 | size_t nInInterests3 = 0; |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 451 | face->setInterestFilter("/Los/Angeles/Lakers", |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 452 | bind([&nInInterests3] { ++nInInterests3; }), |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 453 | RegisterPrefixSuccessCallback(), |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 454 | bind([] { |
| 455 | BOOST_FAIL("Unexpected setInterestFilter failure"); |
| 456 | })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 457 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 458 | advanceClocks(time::milliseconds(10), 10); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 459 | |
| 460 | face->receive(Interest("/Hello/World/!")); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 461 | advanceClocks(time::milliseconds(10), 10); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 462 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 463 | BOOST_CHECK_EQUAL(nInInterests1, 1); |
| 464 | BOOST_CHECK_EQUAL(nInInterests2, 1); |
| 465 | BOOST_CHECK_EQUAL(nInInterests3, 0); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | BOOST_AUTO_TEST_CASE(SetRegexFilterError) |
| 469 | { |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 470 | face->setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"), |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 471 | [] (const Name&, const Interest&) { |
| 472 | BOOST_FAIL("InterestFilter::Error should have been triggered"); |
| 473 | }, |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 474 | RegisterPrefixSuccessCallback(), |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 475 | bind([] { |
| 476 | BOOST_FAIL("Unexpected setInterestFilter failure"); |
| 477 | })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 478 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 479 | advanceClocks(time::milliseconds(10), 10); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 480 | |
| 481 | BOOST_REQUIRE_THROW(face->receive(Interest("/Hello/World/XXX/b/c")), InterestFilter::Error); |
| 482 | } |
| 483 | |
| 484 | BOOST_AUTO_TEST_CASE(SetRegexFilter) |
| 485 | { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 486 | size_t nInInterests = 0; |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 487 | face->setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"), |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 488 | bind([&nInInterests] { ++nInInterests; }), |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 489 | RegisterPrefixSuccessCallback(), |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 490 | bind([] { |
| 491 | BOOST_FAIL("Unexpected setInterestFilter failure"); |
| 492 | })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 493 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 494 | advanceClocks(time::milliseconds(10), 10); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 495 | |
| 496 | face->receive(Interest("/Hello/World/a")); // shouldn't match |
| 497 | BOOST_CHECK_EQUAL(nInInterests, 0); |
| 498 | |
| 499 | face->receive(Interest("/Hello/World/a/b")); // should match |
| 500 | BOOST_CHECK_EQUAL(nInInterests, 1); |
| 501 | |
| 502 | face->receive(Interest("/Hello/World/a/b/c")); // should match |
| 503 | BOOST_CHECK_EQUAL(nInInterests, 2); |
| 504 | |
| 505 | face->receive(Interest("/Hello/World/a/b/d")); // should not match |
| 506 | BOOST_CHECK_EQUAL(nInInterests, 2); |
| 507 | } |
| 508 | |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 509 | BOOST_AUTO_TEST_CASE(SetRegexFilterAndRegister) |
| 510 | { |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 511 | size_t nInInterests = 0; |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 512 | face->setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"), |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 513 | bind([&nInInterests] { ++nInInterests; })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 514 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 515 | size_t nRegSuccesses = 0; |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 516 | face->registerPrefix("/Hello/World", |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 517 | bind([&nRegSuccesses] { ++nRegSuccesses; }), |
| 518 | bind([] { |
| 519 | BOOST_FAIL("Unexpected setInterestFilter failure"); |
| 520 | })); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 521 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 522 | advanceClocks(time::milliseconds(10), 10); |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 523 | BOOST_CHECK_EQUAL(nRegSuccesses, 1); |
| 524 | |
| 525 | face->receive(Interest("/Hello/World/a")); // shouldn't match |
| 526 | BOOST_CHECK_EQUAL(nInInterests, 0); |
| 527 | |
| 528 | face->receive(Interest("/Hello/World/a/b")); // should match |
| 529 | BOOST_CHECK_EQUAL(nInInterests, 1); |
| 530 | |
| 531 | face->receive(Interest("/Hello/World/a/b/c")); // should match |
| 532 | BOOST_CHECK_EQUAL(nInInterests, 2); |
| 533 | |
| 534 | face->receive(Interest("/Hello/World/a/b/d")); // should not match |
| 535 | BOOST_CHECK_EQUAL(nInInterests, 2); |
| 536 | } |
| 537 | |
Junxiao Shi | a1ea506 | 2014-12-27 22:33:39 -0700 | [diff] [blame] | 538 | BOOST_FIXTURE_TEST_CASE(SetInterestFilterNoReg, FacesNoRegistrationReplyFixture) // Bug 2318 |
| 539 | { |
| 540 | // This behavior is specific to DummyClientFace. |
| 541 | // Regular Face won't accept incoming packets until something is sent. |
| 542 | |
| 543 | int hit = 0; |
| 544 | face->setInterestFilter(Name("/"), bind([&hit] { ++hit; })); |
| 545 | face->processEvents(time::milliseconds(-1)); |
| 546 | |
| 547 | auto interest = make_shared<Interest>("/A"); |
| 548 | face->receive(*interest); |
| 549 | face->processEvents(time::milliseconds(-1)); |
| 550 | |
| 551 | BOOST_CHECK_EQUAL(hit, 1); |
| 552 | } |
| 553 | |
Alexander Afanasyev | 8e15854 | 2014-11-18 00:47:18 -0500 | [diff] [blame] | 554 | BOOST_AUTO_TEST_CASE(ProcessEvents) |
| 555 | { |
| 556 | face->processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside |
| 557 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 558 | size_t nRegSuccesses = 0; |
Alexander Afanasyev | 8e15854 | 2014-11-18 00:47:18 -0500 | [diff] [blame] | 559 | face->registerPrefix("/Hello/World", |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 560 | bind([&nRegSuccesses] { ++nRegSuccesses; }), |
| 561 | bind([] { |
| 562 | BOOST_FAIL("Unexpected setInterestFilter failure"); |
| 563 | })); |
Alexander Afanasyev | 8e15854 | 2014-11-18 00:47:18 -0500 | [diff] [blame] | 564 | |
| 565 | // io_service::poll() without reset |
| 566 | face->getIoService().poll(); |
Alexander Afanasyev | 8e15854 | 2014-11-18 00:47:18 -0500 | [diff] [blame] | 567 | BOOST_CHECK_EQUAL(nRegSuccesses, 0); |
| 568 | |
| 569 | face->processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside |
Alexander Afanasyev | 8e15854 | 2014-11-18 00:47:18 -0500 | [diff] [blame] | 570 | BOOST_CHECK_EQUAL(nRegSuccesses, 1); |
| 571 | } |
| 572 | |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 573 | BOOST_AUTO_TEST_CASE(PutNack) |
| 574 | { |
| 575 | lp::Nack nack(Interest("/Hello/World", time::milliseconds(50))); |
| 576 | nack.setReason(lp::NackReason::NO_ROUTE); |
| 577 | |
| 578 | BOOST_CHECK_EQUAL(face->sentNacks.size(), 0); |
| 579 | |
| 580 | face->put(nack); |
| 581 | |
| 582 | advanceClocks(time::milliseconds(10)); |
| 583 | |
| 584 | BOOST_CHECK_EQUAL(face->sentNacks.size(), 1); |
| 585 | } |
| 586 | |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 587 | BOOST_AUTO_TEST_CASE(DestructionWithoutCancellingPendingInterests) // Bug #2518 |
| 588 | { |
| 589 | face->expressInterest(Interest("/Hello/World", time::milliseconds(50)), |
| 590 | bind([]{}), bind([]{})); |
| 591 | advanceClocks(time::milliseconds(10), 10); |
| 592 | |
| 593 | face.reset(); |
| 594 | |
| 595 | advanceClocks(time::milliseconds(10), 10); |
| 596 | // should not segfault |
| 597 | } |
| 598 | |
Alexander Afanasyev | 3a6da36 | 2015-12-29 20:31:03 -0800 | [diff] [blame^] | 599 | BOOST_AUTO_TEST_CASE(FaceTransport) |
| 600 | { |
| 601 | KeyChain keyChain; |
| 602 | |
| 603 | BOOST_CHECK(Face().getTransport() != nullptr); |
| 604 | |
| 605 | BOOST_CHECK(Face(shared_ptr<Transport>()).getTransport() == nullptr); |
| 606 | BOOST_CHECK(Face(shared_ptr<Transport>(), io).getTransport() == nullptr); |
| 607 | BOOST_CHECK(Face(shared_ptr<Transport>(), io, keyChain).getTransport() == nullptr); |
| 608 | |
| 609 | auto transport = make_shared<TcpTransport>("localhost", "6363"); // no real io operations will be scheduled |
| 610 | BOOST_CHECK(Face(transport).getTransport() == transport); |
| 611 | BOOST_CHECK(Face(transport, io).getTransport() == transport); |
| 612 | BOOST_CHECK(Face(transport, io, keyChain).getTransport() == transport); |
| 613 | } |
| 614 | |
Alexander Afanasyev | 5fc795f | 2014-10-20 23:06:56 -0400 | [diff] [blame] | 615 | BOOST_AUTO_TEST_SUITE_END() |
| 616 | |
| 617 | } // tests |
| 618 | } // namespace ndn |