blob: 19e673932057c8f86ef550198e0f9b590876dc91 [file] [log] [blame]
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Spyridon Mastorakis429634f2015-02-19 17:35:33 -08003 * Copyright (c) 2013-2015 Regents of the University of California.
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -04004 *
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 Afanasyevd3a55b22014-11-18 19:23:28 -080025#include "util/dummy-client-face.hpp"
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040026
27#include "boost-test.hpp"
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080028#include "unit-test-time-fixture.hpp"
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080029#include "make-interest-data.hpp"
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040030
31namespace ndn {
32namespace tests {
33
Junxiao Shia60d9362014-11-12 09:38:21 -070034using ndn::util::DummyClientFace;
35using ndn::util::makeDummyClientFace;
36
Junxiao Shia1ea5062014-12-27 22:33:39 -070037class FaceFixture : public UnitTestTimeFixture
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040038{
39public:
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080040 explicit
Junxiao Shia1ea5062014-12-27 22:33:39 -070041 FaceFixture(bool enableRegistrationReply = true)
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080042 : face(makeDummyClientFace(io, { true, enableRegistrationReply }))
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040043 {
44 }
45
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080046public:
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040047 shared_ptr<DummyClientFace> face;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040048};
49
Junxiao Shia1ea5062014-12-27 22:33:39 -070050class FacesNoRegistrationReplyFixture : public FaceFixture
Junxiao Shia60d9362014-11-12 09:38:21 -070051{
52public:
53 FacesNoRegistrationReplyFixture()
Junxiao Shia1ea5062014-12-27 22:33:39 -070054 : FaceFixture(false)
Junxiao Shia60d9362014-11-12 09:38:21 -070055 {
56 }
57};
58
Junxiao Shia1ea5062014-12-27 22:33:39 -070059BOOST_FIXTURE_TEST_SUITE(TestFace, FaceFixture)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040060
61BOOST_AUTO_TEST_CASE(ExpressInterestData)
62{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080063 size_t nData = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040064 face->expressInterest(Interest("/Hello/World", time::milliseconds(50)),
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080065 [&] (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 Afanasyev5fc795f2014-10-20 23:06:56 -040072
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080073 advanceClocks(time::milliseconds(1), 40);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040074
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080075 face->receive(*util::makeData("/Bye/World/!"));
76 face->receive(*util::makeData("/Hello/World/!"));
77
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080078 advanceClocks(time::milliseconds(1), 100);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040079
80 BOOST_CHECK_EQUAL(nData, 1);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080081 BOOST_CHECK_EQUAL(face->sentInterests.size(), 1);
82 BOOST_CHECK_EQUAL(face->sentDatas.size(), 0);
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080083
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 Afanasyev5fc795f2014-10-20 23:06:56 -0400109}
110
111BOOST_AUTO_TEST_CASE(ExpressInterestTimeout)
112{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800113 size_t nTimeouts = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400114 face->expressInterest(Interest("/Hello/World", time::milliseconds(50)),
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800115 bind([] {
116 BOOST_FAIL("Unexpected data");
117 }),
118 bind([&nTimeouts] {
119 ++nTimeouts;
120 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400121
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800122 advanceClocks(time::milliseconds(10), 100);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400123
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400124 BOOST_CHECK_EQUAL(nTimeouts, 1);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800125 BOOST_CHECK_EQUAL(face->sentInterests.size(), 1);
126 BOOST_CHECK_EQUAL(face->sentDatas.size(), 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400127}
128
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800129BOOST_AUTO_TEST_CASE(RemovePendingInterest)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400130{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800131 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 Afanasyev5fc795f2014-10-20 23:06:56 -0400140
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800141 face->removePendingInterest(interestId);
142 advanceClocks(time::milliseconds(10));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400143
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800144 face->receive(*util::makeData("/Hello/World/!"));
145 advanceClocks(time::milliseconds(10), 100);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400146}
147
148BOOST_AUTO_TEST_CASE(SetUnsetInterestFilter)
149{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800150 size_t nInterests = 0;
151 size_t nRegs = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400152 const RegisteredPrefixId* regPrefixId =
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800153 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 Afanasyev5fc795f2014-10-20 23:06:56 -0400162
163 face->receive(Interest("/Hello/World/!"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800164 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400165
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800166 BOOST_CHECK_EQUAL(nRegs, 1);
167 BOOST_CHECK_EQUAL(nInterests, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400168
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800169 face->receive(Interest("/Bye/World/!"));
170 advanceClocks(time::milliseconds(10000), 10);
171 BOOST_CHECK_EQUAL(nInterests, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400172
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800173 face->receive(Interest("/Hello/World/!/2"));
174 advanceClocks(time::milliseconds(10), 10);
175 BOOST_CHECK_EQUAL(nInterests, 2);
176
177 // removing filter
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400178 face->unsetInterestFilter(regPrefixId);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800179 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400180
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800181 face->receive(Interest("/Hello/World/!/3"));
182 BOOST_CHECK_EQUAL(nInterests, 2);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400183
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800184 face->unsetInterestFilter(static_cast<const RegisteredPrefixId*>(0));
185 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400186
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800187 face->unsetInterestFilter(static_cast<const InterestFilterId*>(0));
188 advanceClocks(time::milliseconds(10), 10);
189}
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400190
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800191BOOST_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 Afanasyev5fc795f2014-10-20 23:06:56 -0400211}
212
213BOOST_AUTO_TEST_CASE(RegisterUnregisterPrefix)
214{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800215 size_t nRegSuccesses = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400216 const RegisteredPrefixId* regPrefixId =
217 face->registerPrefix("/Hello/World",
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800218 bind([&nRegSuccesses] { ++nRegSuccesses; }),
219 bind([] {
220 BOOST_FAIL("Unexpected registerPrefix failure");
221 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400222
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800223 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400224 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
225
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800226 size_t nUnregSuccesses = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400227 face->unregisterPrefix(regPrefixId,
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800228 bind([&nUnregSuccesses] { ++nUnregSuccesses; }),
229 bind([] {
230 BOOST_FAIL("Unexpected unregisterPrefix failure");
231 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400232
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800233 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400234 BOOST_CHECK_EQUAL(nUnregSuccesses, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400235}
236
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800237BOOST_FIXTURE_TEST_CASE(RegisterUnregisterPrefixFail, FacesNoRegistrationReplyFixture)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400238{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800239 size_t nRegFailures = 0;
240 face->registerPrefix("/Hello/World",
241 bind([] {
242 BOOST_FAIL("Unexpected registerPrefix success");
243 }),
244 bind([&nRegFailures] { ++nRegFailures; }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400245
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800246 advanceClocks(time::milliseconds(1000), 100);
247 BOOST_CHECK_EQUAL(nRegFailures, 1);
248}
249
250BOOST_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 Afanasyev5fc795f2014-10-20 23:06:56 -0400261 face->setInterestFilter("/Hello",
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800262 bind([&nInInterests2] { ++nInInterests2; }),
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400263 RegisterPrefixSuccessCallback(),
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800264 bind([] {
265 BOOST_FAIL("Unexpected setInterestFilter failure");
266 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400267
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800268 size_t nInInterests3 = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400269 face->setInterestFilter("/Los/Angeles/Lakers",
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800270 bind([&nInInterests3] { ++nInInterests3; }),
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400271 RegisterPrefixSuccessCallback(),
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800272 bind([] {
273 BOOST_FAIL("Unexpected setInterestFilter failure");
274 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400275
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800276 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400277
278 face->receive(Interest("/Hello/World/!"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800279 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400280
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800281 BOOST_CHECK_EQUAL(nInInterests1, 1);
282 BOOST_CHECK_EQUAL(nInInterests2, 1);
283 BOOST_CHECK_EQUAL(nInInterests3, 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400284}
285
286BOOST_AUTO_TEST_CASE(SetRegexFilterError)
287{
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400288 face->setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800289 [] (const Name&, const Interest&) {
290 BOOST_FAIL("InterestFilter::Error should have been triggered");
291 },
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400292 RegisterPrefixSuccessCallback(),
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800293 bind([] {
294 BOOST_FAIL("Unexpected setInterestFilter failure");
295 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400296
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800297 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400298
299 BOOST_REQUIRE_THROW(face->receive(Interest("/Hello/World/XXX/b/c")), InterestFilter::Error);
300}
301
302BOOST_AUTO_TEST_CASE(SetRegexFilter)
303{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800304 size_t nInInterests = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400305 face->setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800306 bind([&nInInterests] { ++nInInterests; }),
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400307 RegisterPrefixSuccessCallback(),
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800308 bind([] {
309 BOOST_FAIL("Unexpected setInterestFilter failure");
310 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400311
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800312 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400313
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 Afanasyev5fc795f2014-10-20 23:06:56 -0400327BOOST_AUTO_TEST_CASE(SetRegexFilterAndRegister)
328{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800329 size_t nInInterests = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400330 face->setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800331 bind([&nInInterests] { ++nInInterests; }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400332
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800333 size_t nRegSuccesses = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400334 face->registerPrefix("/Hello/World",
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800335 bind([&nRegSuccesses] { ++nRegSuccesses; }),
336 bind([] {
337 BOOST_FAIL("Unexpected setInterestFilter failure");
338 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400339
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800340 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400341 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 Shia1ea5062014-12-27 22:33:39 -0700356BOOST_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 Afanasyev8e158542014-11-18 00:47:18 -0500372BOOST_AUTO_TEST_CASE(ProcessEvents)
373{
374 face->processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside
375
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800376 size_t nRegSuccesses = 0;
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500377 face->registerPrefix("/Hello/World",
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800378 bind([&nRegSuccesses] { ++nRegSuccesses; }),
379 bind([] {
380 BOOST_FAIL("Unexpected setInterestFilter failure");
381 }));
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500382
383 // io_service::poll() without reset
384 face->getIoService().poll();
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500385 BOOST_CHECK_EQUAL(nRegSuccesses, 0);
386
387 face->processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500388 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
389}
390
Alexander Afanasyevea719672015-02-10 20:25:23 -0800391BOOST_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
407BOOST_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
430BOOST_AUTO_TEST_CASE(PutDataWithLocalControlHeader)
431{
432 shared_ptr<Data> d = util::makeData("/Bye/World/!");
433 d->setIncomingFaceId(2000);
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800434 d->getLocalControlHeader().setNextHopFaceId(1000); // setNextHopFaceId is intentionally
435 // not exposed directly
Alexander Afanasyevea719672015-02-10 20:25:23 -0800436
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
445BOOST_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 Afanasyev9d158f02015-02-17 21:30:19 -0800462 d->getLocalControlHeader().setNextHopFaceId(1000); // setNextHopFaceId is intentionally
463 // not exposed directly
Alexander Afanasyevea719672015-02-10 20:25:23 -0800464 face->receive(*d);
465
466 advanceClocks(time::milliseconds(10), 100);
467}
468
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800469BOOST_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 Afanasyev5fc795f2014-10-20 23:06:56 -0400481BOOST_AUTO_TEST_SUITE_END()
482
483} // tests
484} // namespace ndn