blob: 5c8dda0e6d0f81692f097bfe32cda9b8a2527d6d [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 Afanasyev3a6da362015-12-29 20:31:03 -080026#include "transport/tcp-transport.hpp"
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040027
28#include "boost-test.hpp"
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080029#include "unit-test-time-fixture.hpp"
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080030#include "make-interest-data.hpp"
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040031
32namespace ndn {
33namespace tests {
34
Junxiao Shia60d9362014-11-12 09:38:21 -070035using ndn::util::DummyClientFace;
Junxiao Shia60d9362014-11-12 09:38:21 -070036
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 Afanasyev9bdbb832015-12-30 12:54:22 -080042 : face(io, { true, enableRegistrationReply })
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040043 {
44 }
45
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080046public:
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080047 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 Afanasyev9bdbb832015-12-30 12:54:22 -080064 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
65 [&] (const Interest& i, const Data& d) {
66 BOOST_CHECK(i.getName().isPrefixOf(d.getName()));
67 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 }));
Eric Newberry83872fd2015-08-06 17:01:24 -070076
77 advanceClocks(time::milliseconds(1), 40);
78
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080079 face.receive(*util::makeData("/Bye/World/a"));
80 face.receive(*util::makeData("/Hello/World/a"));
Eric Newberry83872fd2015-08-06 17:01:24 -070081
82 advanceClocks(time::milliseconds(1), 100);
83
84 BOOST_CHECK_EQUAL(nData, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080085 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
86 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Eric Newberry83872fd2015-08-06 17:01:24 -070087
88 size_t nTimeouts = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080089 face.expressInterest(Interest("/Hello/World/a/2", time::milliseconds(50)),
90 bind([]{}),
91 bind([]{}),
92 bind([&nTimeouts] {
93 ++nTimeouts;
94 }));
Eric Newberry83872fd2015-08-06 17:01:24 -070095 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;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800103 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
104 [&] (const Interest& i, const Data& d) {
105 BOOST_CHECK(i.getName().isPrefixOf(d.getName()));
106 ++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
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800114 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 Afanasyev9bdbb832015-12-30 12:54:22 -0800120 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
121 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800122
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800123 face.expressInterest(Interest("/Hello/World/a", time::milliseconds(50)),
124 [&] (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 }));
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800131 advanceClocks(time::milliseconds(1), 40);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800132 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);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800137 BOOST_CHECK_EQUAL(face.sentInterests.size(), 2);
138 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800139
140 size_t nTimeouts = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800141 face.expressInterest(Interest("/Hello/World/a/2", time::milliseconds(50)),
142 bind([]{}),
143 bind([&nTimeouts] {
144 ++nTimeouts;
145 }));
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800146 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 Afanasyev9bdbb832015-12-30 12:54:22 -0800153 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
154 bind([] {
155 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 });
Eric Newberry83872fd2015-08-06 17:01:24 -0700164
165 advanceClocks(time::milliseconds(10), 100);
166
167 BOOST_CHECK_EQUAL(nTimeouts, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800168 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
169 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
170 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0);
Eric Newberry83872fd2015-08-06 17:01:24 -0700171}
172
173// test case for deprecated expressInterest implementation
174BOOST_AUTO_TEST_CASE(DeprecatedExpressInterestTimeout)
175{
176 size_t nTimeouts = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800177 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
178 bind([] {
179 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 Afanasyev9bdbb832015-12-30 12:54:22 -0800188 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
189 BOOST_CHECK_EQUAL(face.sentData.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
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800198 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 }));
Eric Newberry83872fd2015-08-06 17:01:24 -0700211
212 advanceClocks(time::milliseconds(1), 40);
213
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800214 lp::Nack nack(face.sentInterests[0]);
Eric Newberry83872fd2015-08-06 17:01:24 -0700215 nack.setReason(lp::NackReason::DUPLICATE);
216
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800217 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0);
Eric Newberry83872fd2015-08-06 17:01:24 -0700218
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800219 face.receive(nack);
Eric Newberry83872fd2015-08-06 17:01:24 -0700220
221 advanceClocks(time::milliseconds(1), 100);
222
223 BOOST_CHECK_EQUAL(nNacks, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800224 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
Eric Newberry83872fd2015-08-06 17:01:24 -0700225}
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 =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800230 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
231 bind([] {
232 BOOST_FAIL("Unexpected data");
233 }),
234 bind([] {
235 BOOST_FAIL("Unexpected timeout");
236 }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800237 advanceClocks(time::milliseconds(10));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400238
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800239 face.removePendingInterest(interestId);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800240 advanceClocks(time::milliseconds(10));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400241
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800242 face.receive(*util::makeData("/Hello/World/!"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800243 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{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800248 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"); }));
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500252
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800253 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"); }));
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500257
258 advanceClocks(time::milliseconds(10));
259
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800260 face.removeAllPendingInterests();
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500261 advanceClocks(time::milliseconds(10));
262
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800263 BOOST_CHECK_EQUAL(face.getNPendingInterests(), 0);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500264
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800265 face.receive(*util::makeData("/Hello/World/0"));
266 face.receive(*util::makeData("/Hello/World/1"));
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500267 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 Afanasyev9bdbb832015-12-30 12:54:22 -0800276 face.setInterestFilter("/Hello/World",
277 bind([&nInterests] { ++nInterests; }),
278 bind([&nRegs] { ++nRegs; }),
279 bind([] {
280 BOOST_FAIL("Unexpected setInterestFilter failure");
281 }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800282 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
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800286 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 Afanasyev9bdbb832015-12-30 12:54:22 -0800292 face.receive(Interest("/Bye/World/!"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800293 advanceClocks(time::milliseconds(10000), 10);
294 BOOST_CHECK_EQUAL(nInterests, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400295
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800296 face.receive(Interest("/Hello/World/!/2"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800297 advanceClocks(time::milliseconds(10), 10);
298 BOOST_CHECK_EQUAL(nInterests, 2);
299
300 // removing filter
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800301 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 Afanasyev9bdbb832015-12-30 12:54:22 -0800304 face.receive(Interest("/Hello/World/!/3"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800305 BOOST_CHECK_EQUAL(nInterests, 2);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400306
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800307 face.unsetInterestFilter(static_cast<const RegisteredPrefixId*>(0));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800308 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400309
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800310 face.unsetInterestFilter(static_cast<const InterestFilterId*>(0));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800311 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 =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800318 face.setInterestFilter("/Hello/World",
319 bind([&nInterests] { ++nInterests; }),
320 bind([] {
321 BOOST_FAIL("Unexpected setInterestFilter failure");
322 }));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400323 advanceClocks(time::milliseconds(10), 10);
324 BOOST_CHECK_EQUAL(nInterests, 0);
325
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800326 face.receive(Interest("/Hello/World/!"));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400327 advanceClocks(time::milliseconds(10), 10);
328
329 BOOST_CHECK_EQUAL(nInterests, 1);
330
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800331 face.receive(Interest("/Bye/World/!"));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400332 advanceClocks(time::milliseconds(10000), 10);
333 BOOST_CHECK_EQUAL(nInterests, 1);
334
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800335 face.receive(Interest("/Hello/World/!/2"));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400336 advanceClocks(time::milliseconds(10), 10);
337 BOOST_CHECK_EQUAL(nInterests, 2);
338
339 // removing filter
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800340 face.unsetInterestFilter(regPrefixId);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400341 advanceClocks(time::milliseconds(10), 10);
342
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800343 face.receive(Interest("/Hello/World/!/3"));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400344 BOOST_CHECK_EQUAL(nInterests, 2);
345
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800346 face.unsetInterestFilter(static_cast<const RegisteredPrefixId*>(0));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400347 advanceClocks(time::milliseconds(10), 10);
348
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800349 face.unsetInterestFilter(static_cast<const InterestFilterId*>(0));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400350 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;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800357 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 }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800367
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;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800379 face.setInterestFilter("/Hello/World",
380 bind([] {
381 BOOST_FAIL("Unexpected Interest");
382 }),
383 bind([&nRegFailed] {
384 ++nRegFailed;
385 }));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400386
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 =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800398 face.registerPrefix("/Hello/World",
399 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 Afanasyev9bdbb832015-12-30 12:54:22 -0800408 face.unregisterPrefix(regPrefixId,
409 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;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800421 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;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800434 face.setInterestFilter("/Hello/World",
435 bind([&nInInterests1] { ++nInInterests1; }),
436 RegisterPrefixSuccessCallback(),
437 bind([] {
438 BOOST_FAIL("Unexpected setInterestFilter failure");
439 }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800440
441 size_t nInInterests2 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800442 face.setInterestFilter("/Hello",
443 bind([&nInInterests2] { ++nInInterests2; }),
444 RegisterPrefixSuccessCallback(),
445 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 Afanasyev9bdbb832015-12-30 12:54:22 -0800450 face.setInterestFilter("/Los/Angeles/Lakers",
451 bind([&nInInterests3] { ++nInInterests3; }),
452 RegisterPrefixSuccessCallback(),
453 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
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800459 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 Afanasyev9bdbb832015-12-30 12:54:22 -0800469 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
470 [] (const Name&, const Interest&) {
471 BOOST_FAIL("InterestFilter::Error should have been triggered");
472 },
473 RegisterPrefixSuccessCallback(),
474 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
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800480 BOOST_REQUIRE_THROW(face.receive(Interest("/Hello/World/XXX/b/c")), InterestFilter::Error);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400481}
482
483BOOST_AUTO_TEST_CASE(SetRegexFilter)
484{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800485 size_t nInInterests = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800486 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
487 bind([&nInInterests] { ++nInInterests; }),
488 RegisterPrefixSuccessCallback(),
489 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
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800495 face.receive(Interest("/Hello/World/a")); // shouldn't match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400496 BOOST_CHECK_EQUAL(nInInterests, 0);
497
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800498 face.receive(Interest("/Hello/World/a/b")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400499 BOOST_CHECK_EQUAL(nInInterests, 1);
500
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800501 face.receive(Interest("/Hello/World/a/b/c")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400502 BOOST_CHECK_EQUAL(nInInterests, 2);
503
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800504 face.receive(Interest("/Hello/World/a/b/d")); // should not match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400505 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 Afanasyev9bdbb832015-12-30 12:54:22 -0800511 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
512 bind([&nInInterests] { ++nInInterests; }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400513
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800514 size_t nRegSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800515 face.registerPrefix("/Hello/World",
516 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
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800524 face.receive(Interest("/Hello/World/a")); // shouldn't match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400525 BOOST_CHECK_EQUAL(nInInterests, 0);
526
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800527 face.receive(Interest("/Hello/World/a/b")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400528 BOOST_CHECK_EQUAL(nInInterests, 1);
529
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800530 face.receive(Interest("/Hello/World/a/b/c")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400531 BOOST_CHECK_EQUAL(nInInterests, 2);
532
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800533 face.receive(Interest("/Hello/World/a/b/d")); // should not match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400534 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;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800543 face.setInterestFilter(Name("/"), bind([&hit] { ++hit; }));
544 face.processEvents(time::milliseconds(-1));
Junxiao Shia1ea5062014-12-27 22:33:39 -0700545
546 auto interest = make_shared<Interest>("/A");
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800547 face.receive(*interest);
548 face.processEvents(time::milliseconds(-1));
Junxiao Shia1ea5062014-12-27 22:33:39 -0700549
550 BOOST_CHECK_EQUAL(hit, 1);
551}
552
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500553BOOST_AUTO_TEST_CASE(ProcessEvents)
554{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800555 face.processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500556
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800557 size_t nRegSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800558 face.registerPrefix("/Hello/World",
559 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
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800565 face.getIoService().poll();
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500566 BOOST_CHECK_EQUAL(nRegSuccesses, 0);
567
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800568 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
Eric Newberry83872fd2015-08-06 17:01:24 -0700572BOOST_AUTO_TEST_CASE(PutNack)
573{
574 lp::Nack nack(Interest("/Hello/World", time::milliseconds(50)));
575 nack.setReason(lp::NackReason::NO_ROUTE);
576
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800577 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0);
Eric Newberry83872fd2015-08-06 17:01:24 -0700578
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800579 face.put(nack);
Eric Newberry83872fd2015-08-06 17:01:24 -0700580
581 advanceClocks(time::milliseconds(10));
582
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800583 BOOST_CHECK_EQUAL(face.sentNacks.size(), 1);
Eric Newberry83872fd2015-08-06 17:01:24 -0700584}
585
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800586BOOST_AUTO_TEST_CASE(DestructionWithoutCancellingPendingInterests) // Bug #2518
587{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800588 {
589 DummyClientFace face2(io);
590 face2.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
591 bind([]{}), bind([]{}));
592 advanceClocks(time::milliseconds(10), 10);
593 }
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800594
595 advanceClocks(time::milliseconds(10), 10);
596 // should not segfault
597}
598
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800599BOOST_AUTO_TEST_CASE(FaceTransport)
600{
601 KeyChain keyChain;
602
603 BOOST_CHECK(Face().getTransport() != nullptr);
604
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800605 BOOST_CHECK(Face(shared_ptr<Transport>()).getTransport() != nullptr);
606 BOOST_CHECK(Face(shared_ptr<Transport>(), io).getTransport() != nullptr);
607 BOOST_CHECK(Face(shared_ptr<Transport>(), io, keyChain).getTransport() != nullptr);
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800608
609 auto transport = make_shared<TcpTransport>("localhost", "6363"); // no real io operations will be scheduled
610 BOOST_CHECK(Face(transport).getTransport() == transport);
611 BOOST_CHECK(Face(transport, io).getTransport() == transport);
612 BOOST_CHECK(Face(transport, io, keyChain).getTransport() == transport);
613}
614
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400615BOOST_AUTO_TEST_SUITE_END()
616
617} // tests
618} // namespace ndn