blob: 3a40320c0ea42e38b4dedd350670a8c07a385f39 [file] [log] [blame]
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev57e00362016-06-23 13:22:54 -07003 * Copyright (c) 2013-2016 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 Afanasyev57e00362016-06-23 13:22:54 -070027#include "transport/unix-transport.hpp"
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040028
29#include "boost-test.hpp"
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080030#include "unit-test-time-fixture.hpp"
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080031#include "make-interest-data.hpp"
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040032
33namespace ndn {
34namespace tests {
35
Junxiao Shia60d9362014-11-12 09:38:21 -070036using ndn::util::DummyClientFace;
Junxiao Shia60d9362014-11-12 09:38:21 -070037
Junxiao Shia1ea5062014-12-27 22:33:39 -070038class FaceFixture : public UnitTestTimeFixture
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040039{
40public:
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080041 explicit
Junxiao Shia1ea5062014-12-27 22:33:39 -070042 FaceFixture(bool enableRegistrationReply = true)
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080043 : face(io, { true, enableRegistrationReply })
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040044 {
45 }
46
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080047public:
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080048 DummyClientFace face;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040049};
50
Junxiao Shia1ea5062014-12-27 22:33:39 -070051class FacesNoRegistrationReplyFixture : public FaceFixture
Junxiao Shia60d9362014-11-12 09:38:21 -070052{
53public:
54 FacesNoRegistrationReplyFixture()
Junxiao Shia1ea5062014-12-27 22:33:39 -070055 : FaceFixture(false)
Junxiao Shia60d9362014-11-12 09:38:21 -070056 {
57 }
58};
59
Junxiao Shia1ea5062014-12-27 22:33:39 -070060BOOST_FIXTURE_TEST_SUITE(TestFace, FaceFixture)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040061
62BOOST_AUTO_TEST_CASE(ExpressInterestData)
63{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080064 size_t nData = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080065 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
66 [&] (const Interest& i, const Data& d) {
67 BOOST_CHECK(i.getName().isPrefixOf(d.getName()));
68 BOOST_CHECK_EQUAL(i.getName(), "/Hello/World");
69 ++nData;
70 },
71 bind([] {
72 BOOST_FAIL("Unexpected Nack");
73 }),
74 bind([] {
75 BOOST_FAIL("Unexpected timeout");
76 }));
Eric Newberry83872fd2015-08-06 17:01:24 -070077
78 advanceClocks(time::milliseconds(1), 40);
79
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080080 face.receive(*util::makeData("/Bye/World/a"));
81 face.receive(*util::makeData("/Hello/World/a"));
Eric Newberry83872fd2015-08-06 17:01:24 -070082
83 advanceClocks(time::milliseconds(1), 100);
84
85 BOOST_CHECK_EQUAL(nData, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080086 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
87 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Eric Newberry83872fd2015-08-06 17:01:24 -070088
89 size_t nTimeouts = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080090 face.expressInterest(Interest("/Hello/World/a/2", time::milliseconds(50)),
91 bind([]{}),
92 bind([]{}),
93 bind([&nTimeouts] {
94 ++nTimeouts;
95 }));
Eric Newberry83872fd2015-08-06 17:01:24 -070096 advanceClocks(time::milliseconds(10), 100);
97 BOOST_CHECK_EQUAL(nTimeouts, 1);
98}
99
100// test case for deprecated expressInterest implementation
101BOOST_AUTO_TEST_CASE(DeprecatedExpressInterestData)
102{
103 size_t nData = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800104 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
105 [&] (const Interest& i, const Data& d) {
106 BOOST_CHECK(i.getName().isPrefixOf(d.getName()));
107 ++nData;
108 },
109 bind([] {
110 BOOST_FAIL("Unexpected timeout");
111 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400112
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800113 advanceClocks(time::milliseconds(1), 40);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400114
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800115 face.receive(*util::makeData("/Bye/World/a"));
116 face.receive(*util::makeData("/Hello/World/a"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800117
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800118 advanceClocks(time::milliseconds(1), 100);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400119
120 BOOST_CHECK_EQUAL(nData, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800121 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
122 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800123
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800124 face.expressInterest(Interest("/Hello/World/a", time::milliseconds(50)),
125 [&] (const Interest& i, const Data& d) {
126 BOOST_CHECK(i.getName().isPrefixOf(d.getName()));
127 ++nData;
128 },
129 bind([] {
130 BOOST_FAIL("Unexpected timeout");
131 }));
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800132 advanceClocks(time::milliseconds(1), 40);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800133 face.receive(*util::makeData("/Hello/World/a/1/xxxxx"));
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800134
135 advanceClocks(time::milliseconds(1), 100);
136
137 BOOST_CHECK_EQUAL(nData, 2);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800138 BOOST_CHECK_EQUAL(face.sentInterests.size(), 2);
139 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800140
141 size_t nTimeouts = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800142 face.expressInterest(Interest("/Hello/World/a/2", time::milliseconds(50)),
143 bind([]{}),
144 bind([&nTimeouts] {
145 ++nTimeouts;
146 }));
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800147 advanceClocks(time::milliseconds(10), 100);
148 BOOST_CHECK_EQUAL(nTimeouts, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400149}
150
151BOOST_AUTO_TEST_CASE(ExpressInterestTimeout)
152{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800153 size_t nTimeouts = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800154 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
155 bind([] {
156 BOOST_FAIL("Unexpected ata");
157 }),
158 bind([] {
159 BOOST_FAIL("Unexpected Nack");
160 }),
161 [&nTimeouts] (const Interest& i) {
162 BOOST_CHECK_EQUAL(i.getName(), "/Hello/World");
163 ++nTimeouts;
164 });
Eric Newberry83872fd2015-08-06 17:01:24 -0700165
166 advanceClocks(time::milliseconds(10), 100);
167
168 BOOST_CHECK_EQUAL(nTimeouts, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800169 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
170 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
171 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0);
Eric Newberry83872fd2015-08-06 17:01:24 -0700172}
173
174// test case for deprecated expressInterest implementation
175BOOST_AUTO_TEST_CASE(DeprecatedExpressInterestTimeout)
176{
177 size_t nTimeouts = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800178 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
179 bind([] {
180 BOOST_FAIL("Unexpected data");
181 }),
182 bind([&nTimeouts] {
183 ++nTimeouts;
184 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400185
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800186 advanceClocks(time::milliseconds(10), 100);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400187
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400188 BOOST_CHECK_EQUAL(nTimeouts, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800189 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
190 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400191}
192
Eric Newberry83872fd2015-08-06 17:01:24 -0700193BOOST_AUTO_TEST_CASE(ExpressInterestNack)
194{
195 size_t nNacks = 0;
196
197 Interest interest("/Hello/World", time::milliseconds(50));
198
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800199 face.expressInterest(interest,
200 bind([] {
201 BOOST_FAIL("Unexpected Data");
202 }),
203 [&] (const Interest& i, const lp::Nack& n) {
204 BOOST_CHECK(i.getName().isPrefixOf(n.getInterest().getName()));
205 BOOST_CHECK_EQUAL(i.getName(), "/Hello/World");
206 BOOST_CHECK_EQUAL(n.getReason(), lp::NackReason::DUPLICATE);
207 ++nNacks;
208 },
209 bind([] {
210 BOOST_FAIL("Unexpected timeout");
211 }));
Eric Newberry83872fd2015-08-06 17:01:24 -0700212
213 advanceClocks(time::milliseconds(1), 40);
214
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800215 lp::Nack nack(face.sentInterests[0]);
Eric Newberry83872fd2015-08-06 17:01:24 -0700216 nack.setReason(lp::NackReason::DUPLICATE);
217
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800218 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0);
Eric Newberry83872fd2015-08-06 17:01:24 -0700219
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800220 face.receive(nack);
Eric Newberry83872fd2015-08-06 17:01:24 -0700221
222 advanceClocks(time::milliseconds(1), 100);
223
224 BOOST_CHECK_EQUAL(nNacks, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800225 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
Eric Newberry83872fd2015-08-06 17:01:24 -0700226}
227
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800228BOOST_AUTO_TEST_CASE(RemovePendingInterest)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400229{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800230 const PendingInterestId* interestId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800231 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
232 bind([] {
233 BOOST_FAIL("Unexpected data");
234 }),
235 bind([] {
236 BOOST_FAIL("Unexpected timeout");
237 }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800238 advanceClocks(time::milliseconds(10));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400239
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800240 face.removePendingInterest(interestId);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800241 advanceClocks(time::milliseconds(10));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400242
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800243 face.receive(*util::makeData("/Hello/World/!"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800244 advanceClocks(time::milliseconds(10), 100);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400245}
246
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500247BOOST_AUTO_TEST_CASE(removeAllPendingInterests)
248{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800249 face.expressInterest(Interest("/Hello/World/0", time::milliseconds(50)),
250 bind([] { BOOST_FAIL("Unexpected data"); }),
251 bind([] { BOOST_FAIL("Unexpected nack"); }),
252 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500253
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800254 face.expressInterest(Interest("/Hello/World/1", time::milliseconds(50)),
255 bind([] { BOOST_FAIL("Unexpected data"); }),
256 bind([] { BOOST_FAIL("Unexpected nack"); }),
257 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500258
259 advanceClocks(time::milliseconds(10));
260
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800261 face.removeAllPendingInterests();
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500262 advanceClocks(time::milliseconds(10));
263
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800264 BOOST_CHECK_EQUAL(face.getNPendingInterests(), 0);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500265
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800266 face.receive(*util::makeData("/Hello/World/0"));
267 face.receive(*util::makeData("/Hello/World/1"));
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500268 advanceClocks(time::milliseconds(10), 100);
269}
270
271
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400272BOOST_AUTO_TEST_CASE(SetUnsetInterestFilter)
273{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800274 size_t nInterests = 0;
275 size_t nRegs = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400276 const RegisteredPrefixId* regPrefixId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800277 face.setInterestFilter("/Hello/World",
278 bind([&nInterests] { ++nInterests; }),
279 bind([&nRegs] { ++nRegs; }),
280 bind([] {
281 BOOST_FAIL("Unexpected setInterestFilter failure");
282 }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800283 advanceClocks(time::milliseconds(10), 10);
284 BOOST_CHECK_EQUAL(nRegs, 1);
285 BOOST_CHECK_EQUAL(nInterests, 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400286
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800287 face.receive(Interest("/Hello/World/!"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800288 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400289
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800290 BOOST_CHECK_EQUAL(nRegs, 1);
291 BOOST_CHECK_EQUAL(nInterests, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400292
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800293 face.receive(Interest("/Bye/World/!"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800294 advanceClocks(time::milliseconds(10000), 10);
295 BOOST_CHECK_EQUAL(nInterests, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400296
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800297 face.receive(Interest("/Hello/World/!/2"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800298 advanceClocks(time::milliseconds(10), 10);
299 BOOST_CHECK_EQUAL(nInterests, 2);
300
301 // removing filter
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800302 face.unsetInterestFilter(regPrefixId);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800303 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400304
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800305 face.receive(Interest("/Hello/World/!/3"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800306 BOOST_CHECK_EQUAL(nInterests, 2);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400307
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800308 face.unsetInterestFilter(static_cast<const RegisteredPrefixId*>(0));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800309 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400310
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800311 face.unsetInterestFilter(static_cast<const InterestFilterId*>(0));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800312 advanceClocks(time::milliseconds(10), 10);
313}
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400314
Joao Pereira0b3cac52015-07-02 14:49:49 -0400315BOOST_AUTO_TEST_CASE(SetUnsetInterestFilterWithoutSucessCallback)
316{
317 size_t nInterests = 0;
318 const RegisteredPrefixId* regPrefixId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800319 face.setInterestFilter("/Hello/World",
320 bind([&nInterests] { ++nInterests; }),
321 bind([] {
322 BOOST_FAIL("Unexpected setInterestFilter failure");
323 }));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400324 advanceClocks(time::milliseconds(10), 10);
325 BOOST_CHECK_EQUAL(nInterests, 0);
326
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800327 face.receive(Interest("/Hello/World/!"));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400328 advanceClocks(time::milliseconds(10), 10);
329
330 BOOST_CHECK_EQUAL(nInterests, 1);
331
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800332 face.receive(Interest("/Bye/World/!"));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400333 advanceClocks(time::milliseconds(10000), 10);
334 BOOST_CHECK_EQUAL(nInterests, 1);
335
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800336 face.receive(Interest("/Hello/World/!/2"));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400337 advanceClocks(time::milliseconds(10), 10);
338 BOOST_CHECK_EQUAL(nInterests, 2);
339
340 // removing filter
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800341 face.unsetInterestFilter(regPrefixId);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400342 advanceClocks(time::milliseconds(10), 10);
343
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800344 face.receive(Interest("/Hello/World/!/3"));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400345 BOOST_CHECK_EQUAL(nInterests, 2);
346
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800347 face.unsetInterestFilter(static_cast<const RegisteredPrefixId*>(0));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400348 advanceClocks(time::milliseconds(10), 10);
349
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800350 face.unsetInterestFilter(static_cast<const InterestFilterId*>(0));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400351 advanceClocks(time::milliseconds(10), 10);
352}
353
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800354BOOST_FIXTURE_TEST_CASE(SetInterestFilterFail, FacesNoRegistrationReplyFixture)
355{
356 // don't enable registration reply
357 size_t nRegFailed = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800358 face.setInterestFilter("/Hello/World",
359 bind([] {
360 BOOST_FAIL("Unexpected Interest");
361 }),
362 bind([] {
363 BOOST_FAIL("Unexpected success of setInterestFilter");
364 }),
365 bind([&nRegFailed] {
366 ++nRegFailed;
367 }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800368
369 advanceClocks(time::milliseconds(10), 10);
370 BOOST_CHECK_EQUAL(nRegFailed, 0);
371
372 advanceClocks(time::milliseconds(1000), 10);
373 BOOST_CHECK_EQUAL(nRegFailed, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400374}
375
Joao Pereira0b3cac52015-07-02 14:49:49 -0400376BOOST_FIXTURE_TEST_CASE(SetInterestFilterFailWithoutSuccessCallback, FacesNoRegistrationReplyFixture)
377{
378 // don't enable registration reply
379 size_t nRegFailed = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800380 face.setInterestFilter("/Hello/World",
381 bind([] {
382 BOOST_FAIL("Unexpected Interest");
383 }),
384 bind([&nRegFailed] {
385 ++nRegFailed;
386 }));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400387
388 advanceClocks(time::milliseconds(10), 10);
389 BOOST_CHECK_EQUAL(nRegFailed, 0);
390
391 advanceClocks(time::milliseconds(1000), 10);
392 BOOST_CHECK_EQUAL(nRegFailed, 1);
393}
394
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400395BOOST_AUTO_TEST_CASE(RegisterUnregisterPrefix)
396{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800397 size_t nRegSuccesses = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400398 const RegisteredPrefixId* regPrefixId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800399 face.registerPrefix("/Hello/World",
400 bind([&nRegSuccesses] { ++nRegSuccesses; }),
401 bind([] {
402 BOOST_FAIL("Unexpected registerPrefix failure");
403 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400404
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800405 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400406 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
407
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800408 size_t nUnregSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800409 face.unregisterPrefix(regPrefixId,
410 bind([&nUnregSuccesses] { ++nUnregSuccesses; }),
411 bind([] {
412 BOOST_FAIL("Unexpected unregisterPrefix failure");
413 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400414
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800415 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400416 BOOST_CHECK_EQUAL(nUnregSuccesses, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400417}
418
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800419BOOST_FIXTURE_TEST_CASE(RegisterUnregisterPrefixFail, FacesNoRegistrationReplyFixture)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400420{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800421 size_t nRegFailures = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800422 face.registerPrefix("/Hello/World",
423 bind([] {
424 BOOST_FAIL("Unexpected registerPrefix success");
425 }),
426 bind([&nRegFailures] { ++nRegFailures; }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400427
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800428 advanceClocks(time::milliseconds(1000), 100);
429 BOOST_CHECK_EQUAL(nRegFailures, 1);
430}
431
432BOOST_AUTO_TEST_CASE(SimilarFilters)
433{
434 size_t nInInterests1 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800435 face.setInterestFilter("/Hello/World",
436 bind([&nInInterests1] { ++nInInterests1; }),
437 RegisterPrefixSuccessCallback(),
438 bind([] {
439 BOOST_FAIL("Unexpected setInterestFilter failure");
440 }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800441
442 size_t nInInterests2 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800443 face.setInterestFilter("/Hello",
444 bind([&nInInterests2] { ++nInInterests2; }),
445 RegisterPrefixSuccessCallback(),
446 bind([] {
447 BOOST_FAIL("Unexpected setInterestFilter failure");
448 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400449
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800450 size_t nInInterests3 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800451 face.setInterestFilter("/Los/Angeles/Lakers",
452 bind([&nInInterests3] { ++nInInterests3; }),
453 RegisterPrefixSuccessCallback(),
454 bind([] {
455 BOOST_FAIL("Unexpected setInterestFilter failure");
456 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400457
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800458 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400459
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800460 face.receive(Interest("/Hello/World/!"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800461 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400462
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800463 BOOST_CHECK_EQUAL(nInInterests1, 1);
464 BOOST_CHECK_EQUAL(nInInterests2, 1);
465 BOOST_CHECK_EQUAL(nInInterests3, 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400466}
467
468BOOST_AUTO_TEST_CASE(SetRegexFilterError)
469{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800470 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
471 [] (const Name&, const Interest&) {
472 BOOST_FAIL("InterestFilter::Error should have been triggered");
473 },
474 RegisterPrefixSuccessCallback(),
475 bind([] {
476 BOOST_FAIL("Unexpected setInterestFilter failure");
477 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400478
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800479 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400480
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800481 BOOST_REQUIRE_THROW(face.receive(Interest("/Hello/World/XXX/b/c")), InterestFilter::Error);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400482}
483
484BOOST_AUTO_TEST_CASE(SetRegexFilter)
485{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800486 size_t nInInterests = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800487 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
488 bind([&nInInterests] { ++nInInterests; }),
489 RegisterPrefixSuccessCallback(),
490 bind([] {
491 BOOST_FAIL("Unexpected setInterestFilter failure");
492 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400493
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800494 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400495
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800496 face.receive(Interest("/Hello/World/a")); // shouldn't match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400497 BOOST_CHECK_EQUAL(nInInterests, 0);
498
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800499 face.receive(Interest("/Hello/World/a/b")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400500 BOOST_CHECK_EQUAL(nInInterests, 1);
501
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800502 face.receive(Interest("/Hello/World/a/b/c")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400503 BOOST_CHECK_EQUAL(nInInterests, 2);
504
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800505 face.receive(Interest("/Hello/World/a/b/d")); // should not match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400506 BOOST_CHECK_EQUAL(nInInterests, 2);
507}
508
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400509BOOST_AUTO_TEST_CASE(SetRegexFilterAndRegister)
510{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800511 size_t nInInterests = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800512 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
513 bind([&nInInterests] { ++nInInterests; }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400514
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800515 size_t nRegSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800516 face.registerPrefix("/Hello/World",
517 bind([&nRegSuccesses] { ++nRegSuccesses; }),
518 bind([] {
519 BOOST_FAIL("Unexpected setInterestFilter failure");
520 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400521
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800522 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400523 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
524
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800525 face.receive(Interest("/Hello/World/a")); // shouldn't match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400526 BOOST_CHECK_EQUAL(nInInterests, 0);
527
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800528 face.receive(Interest("/Hello/World/a/b")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400529 BOOST_CHECK_EQUAL(nInInterests, 1);
530
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800531 face.receive(Interest("/Hello/World/a/b/c")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400532 BOOST_CHECK_EQUAL(nInInterests, 2);
533
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800534 face.receive(Interest("/Hello/World/a/b/d")); // should not match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400535 BOOST_CHECK_EQUAL(nInInterests, 2);
536}
537
Junxiao Shia1ea5062014-12-27 22:33:39 -0700538BOOST_FIXTURE_TEST_CASE(SetInterestFilterNoReg, FacesNoRegistrationReplyFixture) // Bug 2318
539{
540 // This behavior is specific to DummyClientFace.
541 // Regular Face won't accept incoming packets until something is sent.
542
543 int hit = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800544 face.setInterestFilter(Name("/"), bind([&hit] { ++hit; }));
545 face.processEvents(time::milliseconds(-1));
Junxiao Shia1ea5062014-12-27 22:33:39 -0700546
547 auto interest = make_shared<Interest>("/A");
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800548 face.receive(*interest);
549 face.processEvents(time::milliseconds(-1));
Junxiao Shia1ea5062014-12-27 22:33:39 -0700550
551 BOOST_CHECK_EQUAL(hit, 1);
552}
553
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500554BOOST_AUTO_TEST_CASE(ProcessEvents)
555{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800556 face.processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500557
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800558 size_t nRegSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800559 face.registerPrefix("/Hello/World",
560 bind([&nRegSuccesses] { ++nRegSuccesses; }),
561 bind([] {
562 BOOST_FAIL("Unexpected setInterestFilter failure");
563 }));
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500564
565 // io_service::poll() without reset
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800566 face.getIoService().poll();
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500567 BOOST_CHECK_EQUAL(nRegSuccesses, 0);
568
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800569 face.processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500570 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
571}
572
Eric Newberry83872fd2015-08-06 17:01:24 -0700573BOOST_AUTO_TEST_CASE(PutNack)
574{
575 lp::Nack nack(Interest("/Hello/World", time::milliseconds(50)));
576 nack.setReason(lp::NackReason::NO_ROUTE);
577
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800578 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0);
Eric Newberry83872fd2015-08-06 17:01:24 -0700579
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800580 face.put(nack);
Eric Newberry83872fd2015-08-06 17:01:24 -0700581
582 advanceClocks(time::milliseconds(10));
583
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800584 BOOST_CHECK_EQUAL(face.sentNacks.size(), 1);
Eric Newberry83872fd2015-08-06 17:01:24 -0700585}
586
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800587BOOST_AUTO_TEST_CASE(DestructionWithoutCancellingPendingInterests) // Bug #2518
588{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800589 {
590 DummyClientFace face2(io);
591 face2.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
592 bind([]{}), bind([]{}));
593 advanceClocks(time::milliseconds(10), 10);
594 }
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800595
596 advanceClocks(time::milliseconds(10), 10);
597 // should not segfault
598}
599
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800600BOOST_AUTO_TEST_CASE(FaceTransport)
601{
602 KeyChain keyChain;
603
604 BOOST_CHECK(Face().getTransport() != nullptr);
605
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800606 BOOST_CHECK(Face(shared_ptr<Transport>()).getTransport() != nullptr);
607 BOOST_CHECK(Face(shared_ptr<Transport>(), io).getTransport() != nullptr);
608 BOOST_CHECK(Face(shared_ptr<Transport>(), io, keyChain).getTransport() != nullptr);
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800609
610 auto transport = make_shared<TcpTransport>("localhost", "6363"); // no real io operations will be scheduled
611 BOOST_CHECK(Face(transport).getTransport() == transport);
612 BOOST_CHECK(Face(transport, io).getTransport() == transport);
613 BOOST_CHECK(Face(transport, io, keyChain).getTransport() == transport);
614}
615
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700616BOOST_AUTO_TEST_CASE(CustomizeTransportWithEnv)
617{
618 shared_ptr<Face> face;
619 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
620 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400621
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700622 setenv("NDN_CLIENT_TRANSPORT", "tcp://localhost:6363", true);
623 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
624 BOOST_CHECK(dynamic_pointer_cast<TcpTransport>(face->getTransport()) != nullptr);
625 unsetenv("NDN_CLIENT_TRANSPORT");
626
627 setenv("NDN_CLIENT_TRANSPORT", "wrong-transport:", true);
628 BOOST_CHECK_THROW(face = make_shared<Face>(), ConfigFile::Error);
629 unsetenv("NDN_CLIENT_TRANSPORT");
630}
631
632/// @todo Tests to create Face using the config file
633
634BOOST_AUTO_TEST_SUITE_END() // TestFace
635
636} // namespace tests
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400637} // namespace ndn