blob: 41276556ca9aeaa88ec981dc0e3aa733c61ab41f [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
246BOOST_AUTO_TEST_CASE(SetUnsetInterestFilter)
247{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800248 size_t nInterests = 0;
249 size_t nRegs = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400250 const RegisteredPrefixId* regPrefixId =
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800251 face->setInterestFilter("/Hello/World",
252 bind([&nInterests] { ++nInterests; }),
253 bind([&nRegs] { ++nRegs; }),
254 bind([] {
255 BOOST_FAIL("Unexpected setInterestFilter failure");
256 }));
257 advanceClocks(time::milliseconds(10), 10);
258 BOOST_CHECK_EQUAL(nRegs, 1);
259 BOOST_CHECK_EQUAL(nInterests, 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400260
261 face->receive(Interest("/Hello/World/!"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800262 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400263
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800264 BOOST_CHECK_EQUAL(nRegs, 1);
265 BOOST_CHECK_EQUAL(nInterests, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400266
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800267 face->receive(Interest("/Bye/World/!"));
268 advanceClocks(time::milliseconds(10000), 10);
269 BOOST_CHECK_EQUAL(nInterests, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400270
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800271 face->receive(Interest("/Hello/World/!/2"));
272 advanceClocks(time::milliseconds(10), 10);
273 BOOST_CHECK_EQUAL(nInterests, 2);
274
275 // removing filter
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400276 face->unsetInterestFilter(regPrefixId);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800277 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400278
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800279 face->receive(Interest("/Hello/World/!/3"));
280 BOOST_CHECK_EQUAL(nInterests, 2);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400281
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800282 face->unsetInterestFilter(static_cast<const RegisteredPrefixId*>(0));
283 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400284
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800285 face->unsetInterestFilter(static_cast<const InterestFilterId*>(0));
286 advanceClocks(time::milliseconds(10), 10);
287}
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400288
Joao Pereira0b3cac52015-07-02 14:49:49 -0400289BOOST_AUTO_TEST_CASE(SetUnsetInterestFilterWithoutSucessCallback)
290{
291 size_t nInterests = 0;
292 const RegisteredPrefixId* regPrefixId =
293 face->setInterestFilter("/Hello/World",
294 bind([&nInterests] { ++nInterests; }),
295 bind([] {
296 BOOST_FAIL("Unexpected setInterestFilter failure");
297 }));
298 advanceClocks(time::milliseconds(10), 10);
299 BOOST_CHECK_EQUAL(nInterests, 0);
300
301 face->receive(Interest("/Hello/World/!"));
302 advanceClocks(time::milliseconds(10), 10);
303
304 BOOST_CHECK_EQUAL(nInterests, 1);
305
306 face->receive(Interest("/Bye/World/!"));
307 advanceClocks(time::milliseconds(10000), 10);
308 BOOST_CHECK_EQUAL(nInterests, 1);
309
310 face->receive(Interest("/Hello/World/!/2"));
311 advanceClocks(time::milliseconds(10), 10);
312 BOOST_CHECK_EQUAL(nInterests, 2);
313
314 // removing filter
315 face->unsetInterestFilter(regPrefixId);
316 advanceClocks(time::milliseconds(10), 10);
317
318 face->receive(Interest("/Hello/World/!/3"));
319 BOOST_CHECK_EQUAL(nInterests, 2);
320
321 face->unsetInterestFilter(static_cast<const RegisteredPrefixId*>(0));
322 advanceClocks(time::milliseconds(10), 10);
323
324 face->unsetInterestFilter(static_cast<const InterestFilterId*>(0));
325 advanceClocks(time::milliseconds(10), 10);
326}
327
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800328BOOST_FIXTURE_TEST_CASE(SetInterestFilterFail, FacesNoRegistrationReplyFixture)
329{
330 // don't enable registration reply
331 size_t nRegFailed = 0;
332 face->setInterestFilter("/Hello/World",
333 bind([] {
334 BOOST_FAIL("Unexpected Interest");
335 }),
336 bind([] {
337 BOOST_FAIL("Unexpected success of setInterestFilter");
338 }),
339 bind([&nRegFailed] {
340 ++nRegFailed;
341 }));
342
343 advanceClocks(time::milliseconds(10), 10);
344 BOOST_CHECK_EQUAL(nRegFailed, 0);
345
346 advanceClocks(time::milliseconds(1000), 10);
347 BOOST_CHECK_EQUAL(nRegFailed, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400348}
349
Joao Pereira0b3cac52015-07-02 14:49:49 -0400350BOOST_FIXTURE_TEST_CASE(SetInterestFilterFailWithoutSuccessCallback, FacesNoRegistrationReplyFixture)
351{
352 // don't enable registration reply
353 size_t nRegFailed = 0;
354 face->setInterestFilter("/Hello/World",
355 bind([] {
356 BOOST_FAIL("Unexpected Interest");
357 }),
358 bind([&nRegFailed] {
359 ++nRegFailed;
360 }));
361
362 advanceClocks(time::milliseconds(10), 10);
363 BOOST_CHECK_EQUAL(nRegFailed, 0);
364
365 advanceClocks(time::milliseconds(1000), 10);
366 BOOST_CHECK_EQUAL(nRegFailed, 1);
367}
368
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400369BOOST_AUTO_TEST_CASE(RegisterUnregisterPrefix)
370{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800371 size_t nRegSuccesses = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400372 const RegisteredPrefixId* regPrefixId =
373 face->registerPrefix("/Hello/World",
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800374 bind([&nRegSuccesses] { ++nRegSuccesses; }),
375 bind([] {
376 BOOST_FAIL("Unexpected registerPrefix failure");
377 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400378
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800379 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400380 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
381
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800382 size_t nUnregSuccesses = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400383 face->unregisterPrefix(regPrefixId,
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800384 bind([&nUnregSuccesses] { ++nUnregSuccesses; }),
385 bind([] {
386 BOOST_FAIL("Unexpected unregisterPrefix failure");
387 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400388
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800389 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400390 BOOST_CHECK_EQUAL(nUnregSuccesses, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400391}
392
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800393BOOST_FIXTURE_TEST_CASE(RegisterUnregisterPrefixFail, FacesNoRegistrationReplyFixture)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400394{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800395 size_t nRegFailures = 0;
396 face->registerPrefix("/Hello/World",
397 bind([] {
398 BOOST_FAIL("Unexpected registerPrefix success");
399 }),
400 bind([&nRegFailures] { ++nRegFailures; }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400401
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800402 advanceClocks(time::milliseconds(1000), 100);
403 BOOST_CHECK_EQUAL(nRegFailures, 1);
404}
405
406BOOST_AUTO_TEST_CASE(SimilarFilters)
407{
408 size_t nInInterests1 = 0;
409 face->setInterestFilter("/Hello/World",
410 bind([&nInInterests1] { ++nInInterests1; }),
411 RegisterPrefixSuccessCallback(),
412 bind([] {
413 BOOST_FAIL("Unexpected setInterestFilter failure");
414 }));
415
416 size_t nInInterests2 = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400417 face->setInterestFilter("/Hello",
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800418 bind([&nInInterests2] { ++nInInterests2; }),
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400419 RegisterPrefixSuccessCallback(),
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800420 bind([] {
421 BOOST_FAIL("Unexpected setInterestFilter failure");
422 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400423
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800424 size_t nInInterests3 = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400425 face->setInterestFilter("/Los/Angeles/Lakers",
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800426 bind([&nInInterests3] { ++nInInterests3; }),
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400427 RegisterPrefixSuccessCallback(),
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800428 bind([] {
429 BOOST_FAIL("Unexpected setInterestFilter failure");
430 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400431
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800432 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400433
434 face->receive(Interest("/Hello/World/!"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800435 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400436
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800437 BOOST_CHECK_EQUAL(nInInterests1, 1);
438 BOOST_CHECK_EQUAL(nInInterests2, 1);
439 BOOST_CHECK_EQUAL(nInInterests3, 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400440}
441
442BOOST_AUTO_TEST_CASE(SetRegexFilterError)
443{
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400444 face->setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800445 [] (const Name&, const Interest&) {
446 BOOST_FAIL("InterestFilter::Error should have been triggered");
447 },
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400448 RegisterPrefixSuccessCallback(),
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800449 bind([] {
450 BOOST_FAIL("Unexpected setInterestFilter failure");
451 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400452
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800453 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400454
455 BOOST_REQUIRE_THROW(face->receive(Interest("/Hello/World/XXX/b/c")), InterestFilter::Error);
456}
457
458BOOST_AUTO_TEST_CASE(SetRegexFilter)
459{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800460 size_t nInInterests = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400461 face->setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800462 bind([&nInInterests] { ++nInInterests; }),
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400463 RegisterPrefixSuccessCallback(),
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800464 bind([] {
465 BOOST_FAIL("Unexpected setInterestFilter failure");
466 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400467
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800468 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400469
470 face->receive(Interest("/Hello/World/a")); // shouldn't match
471 BOOST_CHECK_EQUAL(nInInterests, 0);
472
473 face->receive(Interest("/Hello/World/a/b")); // should match
474 BOOST_CHECK_EQUAL(nInInterests, 1);
475
476 face->receive(Interest("/Hello/World/a/b/c")); // should match
477 BOOST_CHECK_EQUAL(nInInterests, 2);
478
479 face->receive(Interest("/Hello/World/a/b/d")); // should not match
480 BOOST_CHECK_EQUAL(nInInterests, 2);
481}
482
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400483BOOST_AUTO_TEST_CASE(SetRegexFilterAndRegister)
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
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800489 size_t nRegSuccesses = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400490 face->registerPrefix("/Hello/World",
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800491 bind([&nRegSuccesses] { ++nRegSuccesses; }),
492 bind([] {
493 BOOST_FAIL("Unexpected setInterestFilter failure");
494 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400495
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800496 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400497 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
498
499 face->receive(Interest("/Hello/World/a")); // shouldn't match
500 BOOST_CHECK_EQUAL(nInInterests, 0);
501
502 face->receive(Interest("/Hello/World/a/b")); // should match
503 BOOST_CHECK_EQUAL(nInInterests, 1);
504
505 face->receive(Interest("/Hello/World/a/b/c")); // should match
506 BOOST_CHECK_EQUAL(nInInterests, 2);
507
508 face->receive(Interest("/Hello/World/a/b/d")); // should not match
509 BOOST_CHECK_EQUAL(nInInterests, 2);
510}
511
Junxiao Shia1ea5062014-12-27 22:33:39 -0700512BOOST_FIXTURE_TEST_CASE(SetInterestFilterNoReg, FacesNoRegistrationReplyFixture) // Bug 2318
513{
514 // This behavior is specific to DummyClientFace.
515 // Regular Face won't accept incoming packets until something is sent.
516
517 int hit = 0;
518 face->setInterestFilter(Name("/"), bind([&hit] { ++hit; }));
519 face->processEvents(time::milliseconds(-1));
520
521 auto interest = make_shared<Interest>("/A");
522 face->receive(*interest);
523 face->processEvents(time::milliseconds(-1));
524
525 BOOST_CHECK_EQUAL(hit, 1);
526}
527
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500528BOOST_AUTO_TEST_CASE(ProcessEvents)
529{
530 face->processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside
531
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800532 size_t nRegSuccesses = 0;
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500533 face->registerPrefix("/Hello/World",
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800534 bind([&nRegSuccesses] { ++nRegSuccesses; }),
535 bind([] {
536 BOOST_FAIL("Unexpected setInterestFilter failure");
537 }));
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500538
539 // io_service::poll() without reset
540 face->getIoService().poll();
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500541 BOOST_CHECK_EQUAL(nRegSuccesses, 0);
542
543 face->processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500544 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
545}
546
Alexander Afanasyevea719672015-02-10 20:25:23 -0800547BOOST_AUTO_TEST_CASE(ExpressInterestWithLocalControlHeader)
548{
549 Interest i("/Hello/World");
550 i.setNextHopFaceId(1000);
551 i.setIncomingFaceId(2000);
552
553 face->expressInterest(i, bind([]{}), bind([]{}));
554 advanceClocks(time::milliseconds(10));
555
556 BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 1);
557 // only NextHopFaceId is allowed to go out
558 BOOST_CHECK(face->sentInterests[0].getLocalControlHeader().hasNextHopFaceId());
559 BOOST_CHECK(!face->sentInterests[0].getLocalControlHeader().hasIncomingFaceId());
560 BOOST_CHECK_EQUAL(face->sentInterests[0].getNextHopFaceId(), 1000);
561}
562
563BOOST_AUTO_TEST_CASE(ReceiveInterestWithLocalControlHeader)
564{
565 face->setInterestFilter("/Hello/World",
566 [] (const InterestFilter&, const Interest& i) {
Alexander Afanasyevea719672015-02-10 20:25:23 -0800567 BOOST_CHECK(i.getLocalControlHeader().hasIncomingFaceId());
Alexander Afanasyevea719672015-02-10 20:25:23 -0800568 BOOST_CHECK_EQUAL(i.getIncomingFaceId(), 2000);
569 },
570 bind([]{}),
571 bind([] {
572 BOOST_FAIL("Unexpected setInterestFilter failure");
573 }));
574 advanceClocks(time::milliseconds(10));
575
576 Interest i("/Hello/World/!");
577 i.setNextHopFaceId(1000);
578 i.setIncomingFaceId(2000);
579
580 face->receive(i);
581 advanceClocks(time::milliseconds(10));
582}
583
584BOOST_AUTO_TEST_CASE(PutDataWithLocalControlHeader)
585{
586 shared_ptr<Data> d = util::makeData("/Bye/World/!");
587 d->setIncomingFaceId(2000);
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800588 d->getLocalControlHeader().setNextHopFaceId(1000); // setNextHopFaceId is intentionally
589 // not exposed directly
Alexander Afanasyevea719672015-02-10 20:25:23 -0800590
591 face->put(*d);
592 advanceClocks(time::milliseconds(10));
593
594 BOOST_REQUIRE_EQUAL(face->sentDatas.size(), 1);
595 BOOST_CHECK(!face->sentDatas[0].getLocalControlHeader().hasNextHopFaceId());
596 BOOST_CHECK(!face->sentDatas[0].getLocalControlHeader().hasIncomingFaceId());
597}
598
599BOOST_AUTO_TEST_CASE(ReceiveDataWithLocalControlHeader)
600{
601 face->expressInterest(Interest("/Hello/World", time::milliseconds(50)),
602 [&] (const Interest& i, const Data& d) {
Alexander Afanasyevea719672015-02-10 20:25:23 -0800603 BOOST_CHECK(d.getLocalControlHeader().hasIncomingFaceId());
604 BOOST_CHECK_EQUAL(d.getIncomingFaceId(), 2000);
Alexander Afanasyevea719672015-02-10 20:25:23 -0800605 },
606 bind([] {
607 BOOST_FAIL("Unexpected timeout");
608 }));
609
610 advanceClocks(time::milliseconds(10));
611
612 shared_ptr<Data> d = util::makeData("/Hello/World/!");
613 d->setIncomingFaceId(2000);
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800614 d->getLocalControlHeader().setNextHopFaceId(1000); // setNextHopFaceId is intentionally
615 // not exposed directly
Alexander Afanasyevea719672015-02-10 20:25:23 -0800616 face->receive(*d);
617
618 advanceClocks(time::milliseconds(10), 100);
619}
620
Eric Newberry83872fd2015-08-06 17:01:24 -0700621BOOST_AUTO_TEST_CASE(PutNack)
622{
623 lp::Nack nack(Interest("/Hello/World", time::milliseconds(50)));
624 nack.setReason(lp::NackReason::NO_ROUTE);
625
626 BOOST_CHECK_EQUAL(face->sentNacks.size(), 0);
627
628 face->put(nack);
629
630 advanceClocks(time::milliseconds(10));
631
632 BOOST_CHECK_EQUAL(face->sentNacks.size(), 1);
633}
634
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800635BOOST_AUTO_TEST_CASE(DestructionWithoutCancellingPendingInterests) // Bug #2518
636{
637 face->expressInterest(Interest("/Hello/World", time::milliseconds(50)),
638 bind([]{}), bind([]{}));
639 advanceClocks(time::milliseconds(10), 10);
640
641 face.reset();
642
643 advanceClocks(time::milliseconds(10), 10);
644 // should not segfault
645}
646
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400647BOOST_AUTO_TEST_SUITE_END()
648
649} // tests
650} // namespace ndn