blob: 94d0935a7479277d10447cb098cc3148e6f22a7d [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
Joao Pereira0b3cac52015-07-02 14:49:49 -0400191BOOST_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 Afanasyevd3a55b22014-11-18 19:23:28 -0800230BOOST_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 Afanasyev5fc795f2014-10-20 23:06:56 -0400250}
251
Joao Pereira0b3cac52015-07-02 14:49:49 -0400252BOOST_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 Afanasyev5fc795f2014-10-20 23:06:56 -0400271BOOST_AUTO_TEST_CASE(RegisterUnregisterPrefix)
272{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800273 size_t nRegSuccesses = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400274 const RegisteredPrefixId* regPrefixId =
275 face->registerPrefix("/Hello/World",
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800276 bind([&nRegSuccesses] { ++nRegSuccesses; }),
277 bind([] {
278 BOOST_FAIL("Unexpected registerPrefix failure");
279 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400280
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800281 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400282 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
283
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800284 size_t nUnregSuccesses = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400285 face->unregisterPrefix(regPrefixId,
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800286 bind([&nUnregSuccesses] { ++nUnregSuccesses; }),
287 bind([] {
288 BOOST_FAIL("Unexpected unregisterPrefix failure");
289 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400290
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800291 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400292 BOOST_CHECK_EQUAL(nUnregSuccesses, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400293}
294
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800295BOOST_FIXTURE_TEST_CASE(RegisterUnregisterPrefixFail, FacesNoRegistrationReplyFixture)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400296{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800297 size_t nRegFailures = 0;
298 face->registerPrefix("/Hello/World",
299 bind([] {
300 BOOST_FAIL("Unexpected registerPrefix success");
301 }),
302 bind([&nRegFailures] { ++nRegFailures; }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400303
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800304 advanceClocks(time::milliseconds(1000), 100);
305 BOOST_CHECK_EQUAL(nRegFailures, 1);
306}
307
308BOOST_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 Afanasyev5fc795f2014-10-20 23:06:56 -0400319 face->setInterestFilter("/Hello",
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800320 bind([&nInInterests2] { ++nInInterests2; }),
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400321 RegisterPrefixSuccessCallback(),
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800322 bind([] {
323 BOOST_FAIL("Unexpected setInterestFilter failure");
324 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400325
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800326 size_t nInInterests3 = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400327 face->setInterestFilter("/Los/Angeles/Lakers",
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800328 bind([&nInInterests3] { ++nInInterests3; }),
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400329 RegisterPrefixSuccessCallback(),
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800330 bind([] {
331 BOOST_FAIL("Unexpected setInterestFilter failure");
332 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400333
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800334 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400335
336 face->receive(Interest("/Hello/World/!"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800337 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400338
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800339 BOOST_CHECK_EQUAL(nInInterests1, 1);
340 BOOST_CHECK_EQUAL(nInInterests2, 1);
341 BOOST_CHECK_EQUAL(nInInterests3, 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400342}
343
344BOOST_AUTO_TEST_CASE(SetRegexFilterError)
345{
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400346 face->setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800347 [] (const Name&, const Interest&) {
348 BOOST_FAIL("InterestFilter::Error should have been triggered");
349 },
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400350 RegisterPrefixSuccessCallback(),
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800351 bind([] {
352 BOOST_FAIL("Unexpected setInterestFilter failure");
353 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400354
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800355 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400356
357 BOOST_REQUIRE_THROW(face->receive(Interest("/Hello/World/XXX/b/c")), InterestFilter::Error);
358}
359
360BOOST_AUTO_TEST_CASE(SetRegexFilter)
361{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800362 size_t nInInterests = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400363 face->setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800364 bind([&nInInterests] { ++nInInterests; }),
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400365 RegisterPrefixSuccessCallback(),
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800366 bind([] {
367 BOOST_FAIL("Unexpected setInterestFilter failure");
368 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400369
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800370 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400371
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 Afanasyev5fc795f2014-10-20 23:06:56 -0400385BOOST_AUTO_TEST_CASE(SetRegexFilterAndRegister)
386{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800387 size_t nInInterests = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400388 face->setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800389 bind([&nInInterests] { ++nInInterests; }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400390
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800391 size_t nRegSuccesses = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400392 face->registerPrefix("/Hello/World",
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800393 bind([&nRegSuccesses] { ++nRegSuccesses; }),
394 bind([] {
395 BOOST_FAIL("Unexpected setInterestFilter failure");
396 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400397
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800398 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400399 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 Shia1ea5062014-12-27 22:33:39 -0700414BOOST_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 Afanasyev8e158542014-11-18 00:47:18 -0500430BOOST_AUTO_TEST_CASE(ProcessEvents)
431{
432 face->processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside
433
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800434 size_t nRegSuccesses = 0;
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500435 face->registerPrefix("/Hello/World",
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800436 bind([&nRegSuccesses] { ++nRegSuccesses; }),
437 bind([] {
438 BOOST_FAIL("Unexpected setInterestFilter failure");
439 }));
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500440
441 // io_service::poll() without reset
442 face->getIoService().poll();
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500443 BOOST_CHECK_EQUAL(nRegSuccesses, 0);
444
445 face->processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500446 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
447}
448
Alexander Afanasyevea719672015-02-10 20:25:23 -0800449BOOST_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
465BOOST_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
488BOOST_AUTO_TEST_CASE(PutDataWithLocalControlHeader)
489{
490 shared_ptr<Data> d = util::makeData("/Bye/World/!");
491 d->setIncomingFaceId(2000);
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800492 d->getLocalControlHeader().setNextHopFaceId(1000); // setNextHopFaceId is intentionally
493 // not exposed directly
Alexander Afanasyevea719672015-02-10 20:25:23 -0800494
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
503BOOST_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 Afanasyev9d158f02015-02-17 21:30:19 -0800520 d->getLocalControlHeader().setNextHopFaceId(1000); // setNextHopFaceId is intentionally
521 // not exposed directly
Alexander Afanasyevea719672015-02-10 20:25:23 -0800522 face->receive(*d);
523
524 advanceClocks(time::milliseconds(10), 100);
525}
526
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800527BOOST_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 Afanasyev5fc795f2014-10-20 23:06:56 -0400539BOOST_AUTO_TEST_SUITE_END()
540
541} // tests
542} // namespace ndn