blob: 438dde5d763ed25797d75f7faa0e60ec6881fefc [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()));
Eric Newberry83872fd2015-08-06 17:01:24 -070067 BOOST_CHECK_EQUAL(i.getName(), "/Hello/World");
68 ++nData;
69 },
70 bind([] {
71 BOOST_FAIL("Unexpected Nack");
72 }),
73 bind([] {
74 BOOST_FAIL("Unexpected timeout");
75 }));
76
77 advanceClocks(time::milliseconds(1), 40);
78
79 face->receive(*util::makeData("/Bye/World/a"));
80 face->receive(*util::makeData("/Hello/World/a"));
81
82 advanceClocks(time::milliseconds(1), 100);
83
84 BOOST_CHECK_EQUAL(nData, 1);
85 BOOST_CHECK_EQUAL(face->sentInterests.size(), 1);
86 BOOST_CHECK_EQUAL(face->sentDatas.size(), 0);
87
88 size_t nTimeouts = 0;
89 face->expressInterest(Interest("/Hello/World/a/2", time::milliseconds(50)),
90 bind([]{}),
91 bind([]{}),
92 bind([&nTimeouts] {
93 ++nTimeouts;
94 }));
95 advanceClocks(time::milliseconds(10), 100);
96 BOOST_CHECK_EQUAL(nTimeouts, 1);
97}
98
99// test case for deprecated expressInterest implementation
100BOOST_AUTO_TEST_CASE(DeprecatedExpressInterestData)
101{
102 size_t nData = 0;
103 face->expressInterest(Interest("/Hello/World", time::milliseconds(50)),
104 [&] (const Interest& i, const Data& d) {
105 BOOST_CHECK(i.getName().isPrefixOf(d.getName()));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800106 ++nData;
107 },
108 bind([] {
109 BOOST_FAIL("Unexpected timeout");
110 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400111
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800112 advanceClocks(time::milliseconds(1), 40);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400113
Eric Newberry83872fd2015-08-06 17:01:24 -0700114 face->receive(*util::makeData("/Bye/World/a"));
115 face->receive(*util::makeData("/Hello/World/a"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800116
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800117 advanceClocks(time::milliseconds(1), 100);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400118
119 BOOST_CHECK_EQUAL(nData, 1);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800120 BOOST_CHECK_EQUAL(face->sentInterests.size(), 1);
121 BOOST_CHECK_EQUAL(face->sentDatas.size(), 0);
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800122
Eric Newberry83872fd2015-08-06 17:01:24 -0700123 face->expressInterest(Interest("/Hello/World/a", time::milliseconds(50)),
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800124 [&] (const Interest& i, const Data& d) {
125 BOOST_CHECK(i.getName().isPrefixOf(d.getName()));
126 ++nData;
127 },
128 bind([] {
129 BOOST_FAIL("Unexpected timeout");
130 }));
131 advanceClocks(time::milliseconds(1), 40);
Eric Newberry83872fd2015-08-06 17:01:24 -0700132 face->receive(*util::makeData("/Hello/World/a/1/xxxxx"));
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800133
134 advanceClocks(time::milliseconds(1), 100);
135
136 BOOST_CHECK_EQUAL(nData, 2);
137 BOOST_CHECK_EQUAL(face->sentInterests.size(), 2);
138 BOOST_CHECK_EQUAL(face->sentDatas.size(), 0);
139
140 size_t nTimeouts = 0;
Eric Newberry83872fd2015-08-06 17:01:24 -0700141 face->expressInterest(Interest("/Hello/World/a/2", time::milliseconds(50)),
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800142 bind([]{}),
143 bind([&nTimeouts] {
144 ++nTimeouts;
145 }));
146 advanceClocks(time::milliseconds(10), 100);
147 BOOST_CHECK_EQUAL(nTimeouts, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400148}
149
150BOOST_AUTO_TEST_CASE(ExpressInterestTimeout)
151{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800152 size_t nTimeouts = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400153 face->expressInterest(Interest("/Hello/World", time::milliseconds(50)),
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800154 bind([] {
Eric Newberry83872fd2015-08-06 17:01:24 -0700155 BOOST_FAIL("Unexpected ata");
156 }),
157 bind([] {
158 BOOST_FAIL("Unexpected Nack");
159 }),
160 [&nTimeouts] (const Interest& i) {
161 BOOST_CHECK_EQUAL(i.getName(), "/Hello/World");
162 ++nTimeouts;
163 });
164
165 advanceClocks(time::milliseconds(10), 100);
166
167 BOOST_CHECK_EQUAL(nTimeouts, 1);
168 BOOST_CHECK_EQUAL(face->sentInterests.size(), 1);
169 BOOST_CHECK_EQUAL(face->sentDatas.size(), 0);
170 BOOST_CHECK_EQUAL(face->sentNacks.size(), 0);
171}
172
173// test case for deprecated expressInterest implementation
174BOOST_AUTO_TEST_CASE(DeprecatedExpressInterestTimeout)
175{
176 size_t nTimeouts = 0;
177 face->expressInterest(Interest("/Hello/World", time::milliseconds(50)),
178 bind([] {
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800179 BOOST_FAIL("Unexpected data");
180 }),
181 bind([&nTimeouts] {
182 ++nTimeouts;
183 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400184
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800185 advanceClocks(time::milliseconds(10), 100);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400186
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400187 BOOST_CHECK_EQUAL(nTimeouts, 1);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800188 BOOST_CHECK_EQUAL(face->sentInterests.size(), 1);
189 BOOST_CHECK_EQUAL(face->sentDatas.size(), 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400190}
191
Eric Newberry83872fd2015-08-06 17:01:24 -0700192BOOST_AUTO_TEST_CASE(ExpressInterestNack)
193{
194 size_t nNacks = 0;
195
196 Interest interest("/Hello/World", time::milliseconds(50));
197
198 face->expressInterest(interest,
199 bind([] {
200 BOOST_FAIL("Unexpected Data");
201 }),
202 [&] (const Interest& i, const lp::Nack& n) {
203 BOOST_CHECK(i.getName().isPrefixOf(n.getInterest().getName()));
204 BOOST_CHECK_EQUAL(i.getName(), "/Hello/World");
205 BOOST_CHECK_EQUAL(n.getReason(), lp::NackReason::DUPLICATE);
206 ++nNacks;
207 },
208 bind([] {
209 BOOST_FAIL("Unexpected timeout");
210 }));
211
212 advanceClocks(time::milliseconds(1), 40);
213
214 lp::Nack nack(face->sentInterests[0]);
215 nack.setReason(lp::NackReason::DUPLICATE);
216
217 BOOST_CHECK_EQUAL(face->sentNacks.size(), 0);
218
219 face->receive(nack);
220
221 advanceClocks(time::milliseconds(1), 100);
222
223 BOOST_CHECK_EQUAL(nNacks, 1);
224 BOOST_CHECK_EQUAL(face->sentInterests.size(), 1);
225}
226
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800227BOOST_AUTO_TEST_CASE(RemovePendingInterest)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400228{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800229 const PendingInterestId* interestId =
230 face->expressInterest(Interest("/Hello/World", time::milliseconds(50)),
231 bind([] {
232 BOOST_FAIL("Unexpected data");
233 }),
234 bind([] {
235 BOOST_FAIL("Unexpected timeout");
236 }));
237 advanceClocks(time::milliseconds(10));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400238
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800239 face->removePendingInterest(interestId);
240 advanceClocks(time::milliseconds(10));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400241
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800242 face->receive(*util::makeData("/Hello/World/!"));
243 advanceClocks(time::milliseconds(10), 100);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400244}
245
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500246BOOST_AUTO_TEST_CASE(removeAllPendingInterests)
247{
248 face->expressInterest(Interest("/Hello/World/0", time::milliseconds(50)),
249 bind([] { BOOST_FAIL("Unexpected data"); }),
250 bind([] { BOOST_FAIL("Unexpected nack"); }),
251 bind([] { BOOST_FAIL("Unexpected timeout"); }));
252
253 face->expressInterest(Interest("/Hello/World/1", time::milliseconds(50)),
254 bind([] { BOOST_FAIL("Unexpected data"); }),
255 bind([] { BOOST_FAIL("Unexpected nack"); }),
256 bind([] { BOOST_FAIL("Unexpected timeout"); }));
257
258 advanceClocks(time::milliseconds(10));
259
260 face->removeAllPendingInterests();
261 advanceClocks(time::milliseconds(10));
262
263 BOOST_CHECK_EQUAL(face->getNPendingInterests(), 0);
264
265 face->receive(*util::makeData("/Hello/World/0"));
266 face->receive(*util::makeData("/Hello/World/1"));
267 advanceClocks(time::milliseconds(10), 100);
268}
269
270
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400271BOOST_AUTO_TEST_CASE(SetUnsetInterestFilter)
272{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800273 size_t nInterests = 0;
274 size_t nRegs = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400275 const RegisteredPrefixId* regPrefixId =
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800276 face->setInterestFilter("/Hello/World",
277 bind([&nInterests] { ++nInterests; }),
278 bind([&nRegs] { ++nRegs; }),
279 bind([] {
280 BOOST_FAIL("Unexpected setInterestFilter failure");
281 }));
282 advanceClocks(time::milliseconds(10), 10);
283 BOOST_CHECK_EQUAL(nRegs, 1);
284 BOOST_CHECK_EQUAL(nInterests, 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400285
286 face->receive(Interest("/Hello/World/!"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800287 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400288
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800289 BOOST_CHECK_EQUAL(nRegs, 1);
290 BOOST_CHECK_EQUAL(nInterests, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400291
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800292 face->receive(Interest("/Bye/World/!"));
293 advanceClocks(time::milliseconds(10000), 10);
294 BOOST_CHECK_EQUAL(nInterests, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400295
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800296 face->receive(Interest("/Hello/World/!/2"));
297 advanceClocks(time::milliseconds(10), 10);
298 BOOST_CHECK_EQUAL(nInterests, 2);
299
300 // removing filter
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400301 face->unsetInterestFilter(regPrefixId);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800302 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400303
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800304 face->receive(Interest("/Hello/World/!/3"));
305 BOOST_CHECK_EQUAL(nInterests, 2);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400306
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800307 face->unsetInterestFilter(static_cast<const RegisteredPrefixId*>(0));
308 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400309
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800310 face->unsetInterestFilter(static_cast<const InterestFilterId*>(0));
311 advanceClocks(time::milliseconds(10), 10);
312}
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400313
Joao Pereira0b3cac52015-07-02 14:49:49 -0400314BOOST_AUTO_TEST_CASE(SetUnsetInterestFilterWithoutSucessCallback)
315{
316 size_t nInterests = 0;
317 const RegisteredPrefixId* regPrefixId =
318 face->setInterestFilter("/Hello/World",
319 bind([&nInterests] { ++nInterests; }),
320 bind([] {
321 BOOST_FAIL("Unexpected setInterestFilter failure");
322 }));
323 advanceClocks(time::milliseconds(10), 10);
324 BOOST_CHECK_EQUAL(nInterests, 0);
325
326 face->receive(Interest("/Hello/World/!"));
327 advanceClocks(time::milliseconds(10), 10);
328
329 BOOST_CHECK_EQUAL(nInterests, 1);
330
331 face->receive(Interest("/Bye/World/!"));
332 advanceClocks(time::milliseconds(10000), 10);
333 BOOST_CHECK_EQUAL(nInterests, 1);
334
335 face->receive(Interest("/Hello/World/!/2"));
336 advanceClocks(time::milliseconds(10), 10);
337 BOOST_CHECK_EQUAL(nInterests, 2);
338
339 // removing filter
340 face->unsetInterestFilter(regPrefixId);
341 advanceClocks(time::milliseconds(10), 10);
342
343 face->receive(Interest("/Hello/World/!/3"));
344 BOOST_CHECK_EQUAL(nInterests, 2);
345
346 face->unsetInterestFilter(static_cast<const RegisteredPrefixId*>(0));
347 advanceClocks(time::milliseconds(10), 10);
348
349 face->unsetInterestFilter(static_cast<const InterestFilterId*>(0));
350 advanceClocks(time::milliseconds(10), 10);
351}
352
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800353BOOST_FIXTURE_TEST_CASE(SetInterestFilterFail, FacesNoRegistrationReplyFixture)
354{
355 // don't enable registration reply
356 size_t nRegFailed = 0;
357 face->setInterestFilter("/Hello/World",
358 bind([] {
359 BOOST_FAIL("Unexpected Interest");
360 }),
361 bind([] {
362 BOOST_FAIL("Unexpected success of setInterestFilter");
363 }),
364 bind([&nRegFailed] {
365 ++nRegFailed;
366 }));
367
368 advanceClocks(time::milliseconds(10), 10);
369 BOOST_CHECK_EQUAL(nRegFailed, 0);
370
371 advanceClocks(time::milliseconds(1000), 10);
372 BOOST_CHECK_EQUAL(nRegFailed, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400373}
374
Joao Pereira0b3cac52015-07-02 14:49:49 -0400375BOOST_FIXTURE_TEST_CASE(SetInterestFilterFailWithoutSuccessCallback, FacesNoRegistrationReplyFixture)
376{
377 // don't enable registration reply
378 size_t nRegFailed = 0;
379 face->setInterestFilter("/Hello/World",
380 bind([] {
381 BOOST_FAIL("Unexpected Interest");
382 }),
383 bind([&nRegFailed] {
384 ++nRegFailed;
385 }));
386
387 advanceClocks(time::milliseconds(10), 10);
388 BOOST_CHECK_EQUAL(nRegFailed, 0);
389
390 advanceClocks(time::milliseconds(1000), 10);
391 BOOST_CHECK_EQUAL(nRegFailed, 1);
392}
393
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400394BOOST_AUTO_TEST_CASE(RegisterUnregisterPrefix)
395{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800396 size_t nRegSuccesses = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400397 const RegisteredPrefixId* regPrefixId =
398 face->registerPrefix("/Hello/World",
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800399 bind([&nRegSuccesses] { ++nRegSuccesses; }),
400 bind([] {
401 BOOST_FAIL("Unexpected registerPrefix failure");
402 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400403
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800404 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400405 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
406
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800407 size_t nUnregSuccesses = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400408 face->unregisterPrefix(regPrefixId,
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800409 bind([&nUnregSuccesses] { ++nUnregSuccesses; }),
410 bind([] {
411 BOOST_FAIL("Unexpected unregisterPrefix failure");
412 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400413
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800414 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400415 BOOST_CHECK_EQUAL(nUnregSuccesses, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400416}
417
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800418BOOST_FIXTURE_TEST_CASE(RegisterUnregisterPrefixFail, FacesNoRegistrationReplyFixture)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400419{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800420 size_t nRegFailures = 0;
421 face->registerPrefix("/Hello/World",
422 bind([] {
423 BOOST_FAIL("Unexpected registerPrefix success");
424 }),
425 bind([&nRegFailures] { ++nRegFailures; }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400426
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800427 advanceClocks(time::milliseconds(1000), 100);
428 BOOST_CHECK_EQUAL(nRegFailures, 1);
429}
430
431BOOST_AUTO_TEST_CASE(SimilarFilters)
432{
433 size_t nInInterests1 = 0;
434 face->setInterestFilter("/Hello/World",
435 bind([&nInInterests1] { ++nInInterests1; }),
436 RegisterPrefixSuccessCallback(),
437 bind([] {
438 BOOST_FAIL("Unexpected setInterestFilter failure");
439 }));
440
441 size_t nInInterests2 = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400442 face->setInterestFilter("/Hello",
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800443 bind([&nInInterests2] { ++nInInterests2; }),
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400444 RegisterPrefixSuccessCallback(),
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800445 bind([] {
446 BOOST_FAIL("Unexpected setInterestFilter failure");
447 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400448
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800449 size_t nInInterests3 = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400450 face->setInterestFilter("/Los/Angeles/Lakers",
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800451 bind([&nInInterests3] { ++nInInterests3; }),
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400452 RegisterPrefixSuccessCallback(),
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800453 bind([] {
454 BOOST_FAIL("Unexpected setInterestFilter failure");
455 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400456
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800457 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400458
459 face->receive(Interest("/Hello/World/!"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800460 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400461
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800462 BOOST_CHECK_EQUAL(nInInterests1, 1);
463 BOOST_CHECK_EQUAL(nInInterests2, 1);
464 BOOST_CHECK_EQUAL(nInInterests3, 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400465}
466
467BOOST_AUTO_TEST_CASE(SetRegexFilterError)
468{
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400469 face->setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800470 [] (const Name&, const Interest&) {
471 BOOST_FAIL("InterestFilter::Error should have been triggered");
472 },
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400473 RegisterPrefixSuccessCallback(),
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800474 bind([] {
475 BOOST_FAIL("Unexpected setInterestFilter failure");
476 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400477
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800478 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400479
480 BOOST_REQUIRE_THROW(face->receive(Interest("/Hello/World/XXX/b/c")), InterestFilter::Error);
481}
482
483BOOST_AUTO_TEST_CASE(SetRegexFilter)
484{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800485 size_t nInInterests = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400486 face->setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800487 bind([&nInInterests] { ++nInInterests; }),
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400488 RegisterPrefixSuccessCallback(),
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800489 bind([] {
490 BOOST_FAIL("Unexpected setInterestFilter failure");
491 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400492
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800493 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400494
495 face->receive(Interest("/Hello/World/a")); // shouldn't match
496 BOOST_CHECK_EQUAL(nInInterests, 0);
497
498 face->receive(Interest("/Hello/World/a/b")); // should match
499 BOOST_CHECK_EQUAL(nInInterests, 1);
500
501 face->receive(Interest("/Hello/World/a/b/c")); // should match
502 BOOST_CHECK_EQUAL(nInInterests, 2);
503
504 face->receive(Interest("/Hello/World/a/b/d")); // should not match
505 BOOST_CHECK_EQUAL(nInInterests, 2);
506}
507
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400508BOOST_AUTO_TEST_CASE(SetRegexFilterAndRegister)
509{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800510 size_t nInInterests = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400511 face->setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800512 bind([&nInInterests] { ++nInInterests; }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400513
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800514 size_t nRegSuccesses = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400515 face->registerPrefix("/Hello/World",
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800516 bind([&nRegSuccesses] { ++nRegSuccesses; }),
517 bind([] {
518 BOOST_FAIL("Unexpected setInterestFilter failure");
519 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400520
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800521 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400522 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
523
524 face->receive(Interest("/Hello/World/a")); // shouldn't match
525 BOOST_CHECK_EQUAL(nInInterests, 0);
526
527 face->receive(Interest("/Hello/World/a/b")); // should match
528 BOOST_CHECK_EQUAL(nInInterests, 1);
529
530 face->receive(Interest("/Hello/World/a/b/c")); // should match
531 BOOST_CHECK_EQUAL(nInInterests, 2);
532
533 face->receive(Interest("/Hello/World/a/b/d")); // should not match
534 BOOST_CHECK_EQUAL(nInInterests, 2);
535}
536
Junxiao Shia1ea5062014-12-27 22:33:39 -0700537BOOST_FIXTURE_TEST_CASE(SetInterestFilterNoReg, FacesNoRegistrationReplyFixture) // Bug 2318
538{
539 // This behavior is specific to DummyClientFace.
540 // Regular Face won't accept incoming packets until something is sent.
541
542 int hit = 0;
543 face->setInterestFilter(Name("/"), bind([&hit] { ++hit; }));
544 face->processEvents(time::milliseconds(-1));
545
546 auto interest = make_shared<Interest>("/A");
547 face->receive(*interest);
548 face->processEvents(time::milliseconds(-1));
549
550 BOOST_CHECK_EQUAL(hit, 1);
551}
552
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500553BOOST_AUTO_TEST_CASE(ProcessEvents)
554{
555 face->processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside
556
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800557 size_t nRegSuccesses = 0;
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500558 face->registerPrefix("/Hello/World",
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800559 bind([&nRegSuccesses] { ++nRegSuccesses; }),
560 bind([] {
561 BOOST_FAIL("Unexpected setInterestFilter failure");
562 }));
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500563
564 // io_service::poll() without reset
565 face->getIoService().poll();
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500566 BOOST_CHECK_EQUAL(nRegSuccesses, 0);
567
568 face->processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500569 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
570}
571
Alexander Afanasyevea719672015-02-10 20:25:23 -0800572BOOST_AUTO_TEST_CASE(ExpressInterestWithLocalControlHeader)
573{
574 Interest i("/Hello/World");
575 i.setNextHopFaceId(1000);
576 i.setIncomingFaceId(2000);
577
578 face->expressInterest(i, bind([]{}), bind([]{}));
579 advanceClocks(time::milliseconds(10));
580
581 BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 1);
582 // only NextHopFaceId is allowed to go out
583 BOOST_CHECK(face->sentInterests[0].getLocalControlHeader().hasNextHopFaceId());
584 BOOST_CHECK(!face->sentInterests[0].getLocalControlHeader().hasIncomingFaceId());
585 BOOST_CHECK_EQUAL(face->sentInterests[0].getNextHopFaceId(), 1000);
586}
587
588BOOST_AUTO_TEST_CASE(ReceiveInterestWithLocalControlHeader)
589{
590 face->setInterestFilter("/Hello/World",
591 [] (const InterestFilter&, const Interest& i) {
Alexander Afanasyevea719672015-02-10 20:25:23 -0800592 BOOST_CHECK(i.getLocalControlHeader().hasIncomingFaceId());
Alexander Afanasyevea719672015-02-10 20:25:23 -0800593 BOOST_CHECK_EQUAL(i.getIncomingFaceId(), 2000);
594 },
595 bind([]{}),
596 bind([] {
597 BOOST_FAIL("Unexpected setInterestFilter failure");
598 }));
599 advanceClocks(time::milliseconds(10));
600
601 Interest i("/Hello/World/!");
602 i.setNextHopFaceId(1000);
603 i.setIncomingFaceId(2000);
604
605 face->receive(i);
606 advanceClocks(time::milliseconds(10));
607}
608
609BOOST_AUTO_TEST_CASE(PutDataWithLocalControlHeader)
610{
611 shared_ptr<Data> d = util::makeData("/Bye/World/!");
612 d->setIncomingFaceId(2000);
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800613 d->getLocalControlHeader().setNextHopFaceId(1000); // setNextHopFaceId is intentionally
614 // not exposed directly
Alexander Afanasyevea719672015-02-10 20:25:23 -0800615
616 face->put(*d);
617 advanceClocks(time::milliseconds(10));
618
619 BOOST_REQUIRE_EQUAL(face->sentDatas.size(), 1);
620 BOOST_CHECK(!face->sentDatas[0].getLocalControlHeader().hasNextHopFaceId());
621 BOOST_CHECK(!face->sentDatas[0].getLocalControlHeader().hasIncomingFaceId());
622}
623
624BOOST_AUTO_TEST_CASE(ReceiveDataWithLocalControlHeader)
625{
626 face->expressInterest(Interest("/Hello/World", time::milliseconds(50)),
627 [&] (const Interest& i, const Data& d) {
Alexander Afanasyevea719672015-02-10 20:25:23 -0800628 BOOST_CHECK(d.getLocalControlHeader().hasIncomingFaceId());
629 BOOST_CHECK_EQUAL(d.getIncomingFaceId(), 2000);
Alexander Afanasyevea719672015-02-10 20:25:23 -0800630 },
631 bind([] {
632 BOOST_FAIL("Unexpected timeout");
633 }));
634
635 advanceClocks(time::milliseconds(10));
636
637 shared_ptr<Data> d = util::makeData("/Hello/World/!");
638 d->setIncomingFaceId(2000);
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800639 d->getLocalControlHeader().setNextHopFaceId(1000); // setNextHopFaceId is intentionally
640 // not exposed directly
Alexander Afanasyevea719672015-02-10 20:25:23 -0800641 face->receive(*d);
642
643 advanceClocks(time::milliseconds(10), 100);
644}
645
Eric Newberry83872fd2015-08-06 17:01:24 -0700646BOOST_AUTO_TEST_CASE(PutNack)
647{
648 lp::Nack nack(Interest("/Hello/World", time::milliseconds(50)));
649 nack.setReason(lp::NackReason::NO_ROUTE);
650
651 BOOST_CHECK_EQUAL(face->sentNacks.size(), 0);
652
653 face->put(nack);
654
655 advanceClocks(time::milliseconds(10));
656
657 BOOST_CHECK_EQUAL(face->sentNacks.size(), 1);
658}
659
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800660BOOST_AUTO_TEST_CASE(DestructionWithoutCancellingPendingInterests) // Bug #2518
661{
662 face->expressInterest(Interest("/Hello/World", time::milliseconds(50)),
663 bind([]{}), bind([]{}));
664 advanceClocks(time::milliseconds(10), 10);
665
666 face.reset();
667
668 advanceClocks(time::milliseconds(10), 10);
669 // should not segfault
670}
671
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400672BOOST_AUTO_TEST_SUITE_END()
673
674} // tests
675} // namespace ndn