blob: ac87d377de2ed41a818fff8e288acc653fb25991 [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 Afanasyeve4f8c3b2016-06-23 16:03:48 -070030#include "identity-management-time-fixture.hpp"
31#include "key-chain-fixture.hpp"
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080032#include "make-interest-data.hpp"
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040033
34namespace ndn {
35namespace tests {
36
Junxiao Shia60d9362014-11-12 09:38:21 -070037using ndn::util::DummyClientFace;
Junxiao Shia60d9362014-11-12 09:38:21 -070038
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070039class FaceFixture : public IdentityManagementTimeFixture
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040040{
41public:
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080042 explicit
Junxiao Shia1ea5062014-12-27 22:33:39 -070043 FaceFixture(bool enableRegistrationReply = true)
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070044 : face(io, m_keyChain, { true, enableRegistrationReply })
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040045 {
46 }
47
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080048public:
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080049 DummyClientFace face;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040050};
51
Junxiao Shia1ea5062014-12-27 22:33:39 -070052class FacesNoRegistrationReplyFixture : public FaceFixture
Junxiao Shia60d9362014-11-12 09:38:21 -070053{
54public:
55 FacesNoRegistrationReplyFixture()
Junxiao Shia1ea5062014-12-27 22:33:39 -070056 : FaceFixture(false)
Junxiao Shia60d9362014-11-12 09:38:21 -070057 {
58 }
59};
60
Junxiao Shia1ea5062014-12-27 22:33:39 -070061BOOST_FIXTURE_TEST_SUITE(TestFace, FaceFixture)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040062
63BOOST_AUTO_TEST_CASE(ExpressInterestData)
64{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080065 size_t nData = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080066 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
67 [&] (const Interest& i, const Data& d) {
68 BOOST_CHECK(i.getName().isPrefixOf(d.getName()));
69 BOOST_CHECK_EQUAL(i.getName(), "/Hello/World");
70 ++nData;
71 },
72 bind([] {
73 BOOST_FAIL("Unexpected Nack");
74 }),
75 bind([] {
76 BOOST_FAIL("Unexpected timeout");
77 }));
Eric Newberry83872fd2015-08-06 17:01:24 -070078
79 advanceClocks(time::milliseconds(1), 40);
80
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080081 face.receive(*util::makeData("/Bye/World/a"));
82 face.receive(*util::makeData("/Hello/World/a"));
Eric Newberry83872fd2015-08-06 17:01:24 -070083
84 advanceClocks(time::milliseconds(1), 100);
85
86 BOOST_CHECK_EQUAL(nData, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080087 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
88 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Eric Newberry83872fd2015-08-06 17:01:24 -070089
90 size_t nTimeouts = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080091 face.expressInterest(Interest("/Hello/World/a/2", time::milliseconds(50)),
92 bind([]{}),
93 bind([]{}),
94 bind([&nTimeouts] {
95 ++nTimeouts;
96 }));
Eric Newberry83872fd2015-08-06 17:01:24 -070097 advanceClocks(time::milliseconds(10), 100);
98 BOOST_CHECK_EQUAL(nTimeouts, 1);
99}
100
101// test case for deprecated expressInterest implementation
102BOOST_AUTO_TEST_CASE(DeprecatedExpressInterestData)
103{
104 size_t nData = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800105 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
106 [&] (const Interest& i, const Data& d) {
107 BOOST_CHECK(i.getName().isPrefixOf(d.getName()));
108 ++nData;
109 },
110 bind([] {
111 BOOST_FAIL("Unexpected timeout");
112 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400113
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800114 advanceClocks(time::milliseconds(1), 40);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400115
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800116 face.receive(*util::makeData("/Bye/World/a"));
117 face.receive(*util::makeData("/Hello/World/a"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800118
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800119 advanceClocks(time::milliseconds(1), 100);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400120
121 BOOST_CHECK_EQUAL(nData, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800122 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
123 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800124
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800125 face.expressInterest(Interest("/Hello/World/a", time::milliseconds(50)),
126 [&] (const Interest& i, const Data& d) {
127 BOOST_CHECK(i.getName().isPrefixOf(d.getName()));
128 ++nData;
129 },
130 bind([] {
131 BOOST_FAIL("Unexpected timeout");
132 }));
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800133 advanceClocks(time::milliseconds(1), 40);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800134 face.receive(*util::makeData("/Hello/World/a/1/xxxxx"));
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800135
136 advanceClocks(time::milliseconds(1), 100);
137
138 BOOST_CHECK_EQUAL(nData, 2);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800139 BOOST_CHECK_EQUAL(face.sentInterests.size(), 2);
140 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800141
142 size_t nTimeouts = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800143 face.expressInterest(Interest("/Hello/World/a/2", time::milliseconds(50)),
144 bind([]{}),
145 bind([&nTimeouts] {
146 ++nTimeouts;
147 }));
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800148 advanceClocks(time::milliseconds(10), 100);
149 BOOST_CHECK_EQUAL(nTimeouts, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400150}
151
152BOOST_AUTO_TEST_CASE(ExpressInterestTimeout)
153{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800154 size_t nTimeouts = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800155 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
156 bind([] {
157 BOOST_FAIL("Unexpected ata");
158 }),
159 bind([] {
160 BOOST_FAIL("Unexpected Nack");
161 }),
162 [&nTimeouts] (const Interest& i) {
163 BOOST_CHECK_EQUAL(i.getName(), "/Hello/World");
164 ++nTimeouts;
165 });
Eric Newberry83872fd2015-08-06 17:01:24 -0700166
167 advanceClocks(time::milliseconds(10), 100);
168
169 BOOST_CHECK_EQUAL(nTimeouts, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800170 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
171 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
172 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0);
Eric Newberry83872fd2015-08-06 17:01:24 -0700173}
174
175// test case for deprecated expressInterest implementation
176BOOST_AUTO_TEST_CASE(DeprecatedExpressInterestTimeout)
177{
178 size_t nTimeouts = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800179 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
180 bind([] {
181 BOOST_FAIL("Unexpected data");
182 }),
183 bind([&nTimeouts] {
184 ++nTimeouts;
185 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400186
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800187 advanceClocks(time::milliseconds(10), 100);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400188
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400189 BOOST_CHECK_EQUAL(nTimeouts, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800190 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
191 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400192}
193
Eric Newberry83872fd2015-08-06 17:01:24 -0700194BOOST_AUTO_TEST_CASE(ExpressInterestNack)
195{
196 size_t nNacks = 0;
197
198 Interest interest("/Hello/World", time::milliseconds(50));
199
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800200 face.expressInterest(interest,
201 bind([] {
202 BOOST_FAIL("Unexpected Data");
203 }),
204 [&] (const Interest& i, const lp::Nack& n) {
205 BOOST_CHECK(i.getName().isPrefixOf(n.getInterest().getName()));
206 BOOST_CHECK_EQUAL(i.getName(), "/Hello/World");
207 BOOST_CHECK_EQUAL(n.getReason(), lp::NackReason::DUPLICATE);
208 ++nNacks;
209 },
210 bind([] {
211 BOOST_FAIL("Unexpected timeout");
212 }));
Eric Newberry83872fd2015-08-06 17:01:24 -0700213
214 advanceClocks(time::milliseconds(1), 40);
215
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800216 lp::Nack nack(face.sentInterests[0]);
Eric Newberry83872fd2015-08-06 17:01:24 -0700217 nack.setReason(lp::NackReason::DUPLICATE);
218
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800219 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0);
Eric Newberry83872fd2015-08-06 17:01:24 -0700220
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800221 face.receive(nack);
Eric Newberry83872fd2015-08-06 17:01:24 -0700222
223 advanceClocks(time::milliseconds(1), 100);
224
225 BOOST_CHECK_EQUAL(nNacks, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800226 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
Eric Newberry83872fd2015-08-06 17:01:24 -0700227}
228
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800229BOOST_AUTO_TEST_CASE(RemovePendingInterest)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400230{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800231 const PendingInterestId* interestId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800232 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
233 bind([] {
234 BOOST_FAIL("Unexpected data");
235 }),
236 bind([] {
237 BOOST_FAIL("Unexpected timeout");
238 }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800239 advanceClocks(time::milliseconds(10));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400240
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800241 face.removePendingInterest(interestId);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800242 advanceClocks(time::milliseconds(10));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400243
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800244 face.receive(*util::makeData("/Hello/World/!"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800245 advanceClocks(time::milliseconds(10), 100);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400246}
247
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500248BOOST_AUTO_TEST_CASE(removeAllPendingInterests)
249{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800250 face.expressInterest(Interest("/Hello/World/0", time::milliseconds(50)),
251 bind([] { BOOST_FAIL("Unexpected data"); }),
252 bind([] { BOOST_FAIL("Unexpected nack"); }),
253 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500254
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800255 face.expressInterest(Interest("/Hello/World/1", time::milliseconds(50)),
256 bind([] { BOOST_FAIL("Unexpected data"); }),
257 bind([] { BOOST_FAIL("Unexpected nack"); }),
258 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500259
260 advanceClocks(time::milliseconds(10));
261
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800262 face.removeAllPendingInterests();
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500263 advanceClocks(time::milliseconds(10));
264
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800265 BOOST_CHECK_EQUAL(face.getNPendingInterests(), 0);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500266
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800267 face.receive(*util::makeData("/Hello/World/0"));
268 face.receive(*util::makeData("/Hello/World/1"));
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500269 advanceClocks(time::milliseconds(10), 100);
270}
271
272
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400273BOOST_AUTO_TEST_CASE(SetUnsetInterestFilter)
274{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800275 size_t nInterests = 0;
276 size_t nRegs = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400277 const RegisteredPrefixId* regPrefixId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800278 face.setInterestFilter("/Hello/World",
279 bind([&nInterests] { ++nInterests; }),
280 bind([&nRegs] { ++nRegs; }),
281 bind([] {
282 BOOST_FAIL("Unexpected setInterestFilter failure");
283 }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800284 advanceClocks(time::milliseconds(10), 10);
285 BOOST_CHECK_EQUAL(nRegs, 1);
286 BOOST_CHECK_EQUAL(nInterests, 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400287
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800288 face.receive(Interest("/Hello/World/!"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800289 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400290
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800291 BOOST_CHECK_EQUAL(nRegs, 1);
292 BOOST_CHECK_EQUAL(nInterests, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400293
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800294 face.receive(Interest("/Bye/World/!"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800295 advanceClocks(time::milliseconds(10000), 10);
296 BOOST_CHECK_EQUAL(nInterests, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400297
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800298 face.receive(Interest("/Hello/World/!/2"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800299 advanceClocks(time::milliseconds(10), 10);
300 BOOST_CHECK_EQUAL(nInterests, 2);
301
302 // removing filter
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800303 face.unsetInterestFilter(regPrefixId);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800304 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400305
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800306 face.receive(Interest("/Hello/World/!/3"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800307 BOOST_CHECK_EQUAL(nInterests, 2);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400308
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800309 face.unsetInterestFilter(static_cast<const RegisteredPrefixId*>(0));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800310 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400311
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800312 face.unsetInterestFilter(static_cast<const InterestFilterId*>(0));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800313 advanceClocks(time::milliseconds(10), 10);
314}
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400315
Joao Pereira0b3cac52015-07-02 14:49:49 -0400316BOOST_AUTO_TEST_CASE(SetUnsetInterestFilterWithoutSucessCallback)
317{
318 size_t nInterests = 0;
319 const RegisteredPrefixId* regPrefixId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800320 face.setInterestFilter("/Hello/World",
321 bind([&nInterests] { ++nInterests; }),
322 bind([] {
323 BOOST_FAIL("Unexpected setInterestFilter failure");
324 }));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400325 advanceClocks(time::milliseconds(10), 10);
326 BOOST_CHECK_EQUAL(nInterests, 0);
327
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800328 face.receive(Interest("/Hello/World/!"));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400329 advanceClocks(time::milliseconds(10), 10);
330
331 BOOST_CHECK_EQUAL(nInterests, 1);
332
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800333 face.receive(Interest("/Bye/World/!"));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400334 advanceClocks(time::milliseconds(10000), 10);
335 BOOST_CHECK_EQUAL(nInterests, 1);
336
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800337 face.receive(Interest("/Hello/World/!/2"));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400338 advanceClocks(time::milliseconds(10), 10);
339 BOOST_CHECK_EQUAL(nInterests, 2);
340
341 // removing filter
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800342 face.unsetInterestFilter(regPrefixId);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400343 advanceClocks(time::milliseconds(10), 10);
344
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800345 face.receive(Interest("/Hello/World/!/3"));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400346 BOOST_CHECK_EQUAL(nInterests, 2);
347
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800348 face.unsetInterestFilter(static_cast<const RegisteredPrefixId*>(0));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400349 advanceClocks(time::milliseconds(10), 10);
350
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800351 face.unsetInterestFilter(static_cast<const InterestFilterId*>(0));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400352 advanceClocks(time::milliseconds(10), 10);
353}
354
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800355BOOST_FIXTURE_TEST_CASE(SetInterestFilterFail, FacesNoRegistrationReplyFixture)
356{
357 // don't enable registration reply
358 size_t nRegFailed = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800359 face.setInterestFilter("/Hello/World",
360 bind([] {
361 BOOST_FAIL("Unexpected Interest");
362 }),
363 bind([] {
364 BOOST_FAIL("Unexpected success of setInterestFilter");
365 }),
366 bind([&nRegFailed] {
367 ++nRegFailed;
368 }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800369
370 advanceClocks(time::milliseconds(10), 10);
371 BOOST_CHECK_EQUAL(nRegFailed, 0);
372
373 advanceClocks(time::milliseconds(1000), 10);
374 BOOST_CHECK_EQUAL(nRegFailed, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400375}
376
Joao Pereira0b3cac52015-07-02 14:49:49 -0400377BOOST_FIXTURE_TEST_CASE(SetInterestFilterFailWithoutSuccessCallback, FacesNoRegistrationReplyFixture)
378{
379 // don't enable registration reply
380 size_t nRegFailed = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800381 face.setInterestFilter("/Hello/World",
382 bind([] {
383 BOOST_FAIL("Unexpected Interest");
384 }),
385 bind([&nRegFailed] {
386 ++nRegFailed;
387 }));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400388
389 advanceClocks(time::milliseconds(10), 10);
390 BOOST_CHECK_EQUAL(nRegFailed, 0);
391
392 advanceClocks(time::milliseconds(1000), 10);
393 BOOST_CHECK_EQUAL(nRegFailed, 1);
394}
395
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400396BOOST_AUTO_TEST_CASE(RegisterUnregisterPrefix)
397{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800398 size_t nRegSuccesses = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400399 const RegisteredPrefixId* regPrefixId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800400 face.registerPrefix("/Hello/World",
401 bind([&nRegSuccesses] { ++nRegSuccesses; }),
402 bind([] {
403 BOOST_FAIL("Unexpected registerPrefix failure");
404 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400405
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800406 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400407 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
408
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800409 size_t nUnregSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800410 face.unregisterPrefix(regPrefixId,
411 bind([&nUnregSuccesses] { ++nUnregSuccesses; }),
412 bind([] {
413 BOOST_FAIL("Unexpected unregisterPrefix failure");
414 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400415
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800416 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400417 BOOST_CHECK_EQUAL(nUnregSuccesses, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400418}
419
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800420BOOST_FIXTURE_TEST_CASE(RegisterUnregisterPrefixFail, FacesNoRegistrationReplyFixture)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400421{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800422 size_t nRegFailures = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800423 face.registerPrefix("/Hello/World",
424 bind([] {
425 BOOST_FAIL("Unexpected registerPrefix success");
426 }),
427 bind([&nRegFailures] { ++nRegFailures; }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400428
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800429 advanceClocks(time::milliseconds(1000), 100);
430 BOOST_CHECK_EQUAL(nRegFailures, 1);
431}
432
433BOOST_AUTO_TEST_CASE(SimilarFilters)
434{
435 size_t nInInterests1 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800436 face.setInterestFilter("/Hello/World",
437 bind([&nInInterests1] { ++nInInterests1; }),
438 RegisterPrefixSuccessCallback(),
439 bind([] {
440 BOOST_FAIL("Unexpected setInterestFilter failure");
441 }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800442
443 size_t nInInterests2 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800444 face.setInterestFilter("/Hello",
445 bind([&nInInterests2] { ++nInInterests2; }),
446 RegisterPrefixSuccessCallback(),
447 bind([] {
448 BOOST_FAIL("Unexpected setInterestFilter failure");
449 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400450
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800451 size_t nInInterests3 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800452 face.setInterestFilter("/Los/Angeles/Lakers",
453 bind([&nInInterests3] { ++nInInterests3; }),
454 RegisterPrefixSuccessCallback(),
455 bind([] {
456 BOOST_FAIL("Unexpected setInterestFilter failure");
457 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400458
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800459 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400460
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800461 face.receive(Interest("/Hello/World/!"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800462 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400463
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800464 BOOST_CHECK_EQUAL(nInInterests1, 1);
465 BOOST_CHECK_EQUAL(nInInterests2, 1);
466 BOOST_CHECK_EQUAL(nInInterests3, 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400467}
468
469BOOST_AUTO_TEST_CASE(SetRegexFilterError)
470{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800471 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
472 [] (const Name&, const Interest&) {
473 BOOST_FAIL("InterestFilter::Error should have been triggered");
474 },
475 RegisterPrefixSuccessCallback(),
476 bind([] {
477 BOOST_FAIL("Unexpected setInterestFilter failure");
478 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400479
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800480 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400481
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800482 BOOST_REQUIRE_THROW(face.receive(Interest("/Hello/World/XXX/b/c")), InterestFilter::Error);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400483}
484
485BOOST_AUTO_TEST_CASE(SetRegexFilter)
486{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800487 size_t nInInterests = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800488 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
489 bind([&nInInterests] { ++nInInterests; }),
490 RegisterPrefixSuccessCallback(),
491 bind([] {
492 BOOST_FAIL("Unexpected setInterestFilter failure");
493 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400494
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800495 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400496
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800497 face.receive(Interest("/Hello/World/a")); // shouldn't match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400498 BOOST_CHECK_EQUAL(nInInterests, 0);
499
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800500 face.receive(Interest("/Hello/World/a/b")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400501 BOOST_CHECK_EQUAL(nInInterests, 1);
502
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800503 face.receive(Interest("/Hello/World/a/b/c")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400504 BOOST_CHECK_EQUAL(nInInterests, 2);
505
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800506 face.receive(Interest("/Hello/World/a/b/d")); // should not match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400507 BOOST_CHECK_EQUAL(nInInterests, 2);
508}
509
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400510BOOST_AUTO_TEST_CASE(SetRegexFilterAndRegister)
511{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800512 size_t nInInterests = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800513 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
514 bind([&nInInterests] { ++nInInterests; }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400515
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800516 size_t nRegSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800517 face.registerPrefix("/Hello/World",
518 bind([&nRegSuccesses] { ++nRegSuccesses; }),
519 bind([] {
520 BOOST_FAIL("Unexpected setInterestFilter failure");
521 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400522
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800523 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400524 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
525
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800526 face.receive(Interest("/Hello/World/a")); // shouldn't match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400527 BOOST_CHECK_EQUAL(nInInterests, 0);
528
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800529 face.receive(Interest("/Hello/World/a/b")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400530 BOOST_CHECK_EQUAL(nInInterests, 1);
531
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800532 face.receive(Interest("/Hello/World/a/b/c")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400533 BOOST_CHECK_EQUAL(nInInterests, 2);
534
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800535 face.receive(Interest("/Hello/World/a/b/d")); // should not match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400536 BOOST_CHECK_EQUAL(nInInterests, 2);
537}
538
Junxiao Shia1ea5062014-12-27 22:33:39 -0700539BOOST_FIXTURE_TEST_CASE(SetInterestFilterNoReg, FacesNoRegistrationReplyFixture) // Bug 2318
540{
541 // This behavior is specific to DummyClientFace.
542 // Regular Face won't accept incoming packets until something is sent.
543
544 int hit = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800545 face.setInterestFilter(Name("/"), bind([&hit] { ++hit; }));
546 face.processEvents(time::milliseconds(-1));
Junxiao Shia1ea5062014-12-27 22:33:39 -0700547
548 auto interest = make_shared<Interest>("/A");
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800549 face.receive(*interest);
550 face.processEvents(time::milliseconds(-1));
Junxiao Shia1ea5062014-12-27 22:33:39 -0700551
552 BOOST_CHECK_EQUAL(hit, 1);
553}
554
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500555BOOST_AUTO_TEST_CASE(ProcessEvents)
556{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800557 face.processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500558
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800559 size_t nRegSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800560 face.registerPrefix("/Hello/World",
561 bind([&nRegSuccesses] { ++nRegSuccesses; }),
562 bind([] {
563 BOOST_FAIL("Unexpected setInterestFilter failure");
564 }));
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500565
566 // io_service::poll() without reset
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800567 face.getIoService().poll();
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500568 BOOST_CHECK_EQUAL(nRegSuccesses, 0);
569
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800570 face.processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500571 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
572}
573
Eric Newberry83872fd2015-08-06 17:01:24 -0700574BOOST_AUTO_TEST_CASE(PutNack)
575{
576 lp::Nack nack(Interest("/Hello/World", time::milliseconds(50)));
577 nack.setReason(lp::NackReason::NO_ROUTE);
578
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800579 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0);
Eric Newberry83872fd2015-08-06 17:01:24 -0700580
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800581 face.put(nack);
Eric Newberry83872fd2015-08-06 17:01:24 -0700582
583 advanceClocks(time::milliseconds(10));
584
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800585 BOOST_CHECK_EQUAL(face.sentNacks.size(), 1);
Eric Newberry83872fd2015-08-06 17:01:24 -0700586}
587
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800588BOOST_AUTO_TEST_CASE(DestructionWithoutCancellingPendingInterests) // Bug #2518
589{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800590 {
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700591 DummyClientFace face2(io, m_keyChain);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800592 face2.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
593 bind([]{}), bind([]{}));
594 advanceClocks(time::milliseconds(10), 10);
595 }
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800596
597 advanceClocks(time::milliseconds(10), 10);
598 // should not segfault
599}
600
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700601struct PibDirWithDefaultTpm
602{
603 const std::string PATH = "build/keys-with-default-tpm";
604};
605
606BOOST_FIXTURE_TEST_CASE(FaceTransport, PibDirFixture<PibDirWithDefaultTpm>)
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800607{
608 KeyChain keyChain;
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700609 boost::asio::io_service io;
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800610
611 BOOST_CHECK(Face().getTransport() != nullptr);
612
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800613 BOOST_CHECK(Face(shared_ptr<Transport>()).getTransport() != nullptr);
614 BOOST_CHECK(Face(shared_ptr<Transport>(), io).getTransport() != nullptr);
615 BOOST_CHECK(Face(shared_ptr<Transport>(), io, keyChain).getTransport() != nullptr);
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800616
617 auto transport = make_shared<TcpTransport>("localhost", "6363"); // no real io operations will be scheduled
618 BOOST_CHECK(Face(transport).getTransport() == transport);
619 BOOST_CHECK(Face(transport, io).getTransport() == transport);
620 BOOST_CHECK(Face(transport, io, keyChain).getTransport() == transport);
621}
622
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700623BOOST_AUTO_TEST_CASE(CustomizeTransportWithEnv)
624{
625 shared_ptr<Face> face;
626 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
627 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400628
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700629 setenv("NDN_CLIENT_TRANSPORT", "tcp://localhost:6363", true);
630 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
631 BOOST_CHECK(dynamic_pointer_cast<TcpTransport>(face->getTransport()) != nullptr);
632 unsetenv("NDN_CLIENT_TRANSPORT");
633
634 setenv("NDN_CLIENT_TRANSPORT", "wrong-transport:", true);
635 BOOST_CHECK_THROW(face = make_shared<Face>(), ConfigFile::Error);
636 unsetenv("NDN_CLIENT_TRANSPORT");
637}
638
639/// @todo Tests to create Face using the config file
640
641BOOST_AUTO_TEST_SUITE_END() // TestFace
642
643} // namespace tests
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400644} // namespace ndn