blob: 3c903e9a0b6d71e7c97183328b9df9a15c03fff2 [file] [log] [blame]
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +00002/*
Alexander Afanasyev1013fd02017-01-03 13:19:03 -08003 * Copyright (c) 2013-2017 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"
Junxiao Shia1478db2016-09-09 04:13:15 +000023#include "lp/tags.hpp"
Alexander Afanasyev3a6da362015-12-29 20:31:03 -080024#include "transport/tcp-transport.hpp"
Alexander Afanasyev57e00362016-06-23 13:22:54 -070025#include "transport/unix-transport.hpp"
Junxiao Shia1478db2016-09-09 04:13:15 +000026#include "util/dummy-client-face.hpp"
27#include "util/scheduler.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"
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080031#include "test-home-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 Afanasyev80782e02017-01-04 13:16:54 -080039class 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)
Junxiao Shif5b5ae22016-08-08 05:54:41 +000044 : 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
Junxiao Shi103d8ed2016-08-07 20:34:10 +000063BOOST_AUTO_TEST_SUITE(Consumer)
64
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040065BOOST_AUTO_TEST_CASE(ExpressInterestData)
66{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080067 size_t nData = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080068 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
69 [&] (const Interest& i, const Data& d) {
70 BOOST_CHECK(i.getName().isPrefixOf(d.getName()));
71 BOOST_CHECK_EQUAL(i.getName(), "/Hello/World");
72 ++nData;
73 },
Junxiao Shi103d8ed2016-08-07 20:34:10 +000074 bind([] { BOOST_FAIL("Unexpected Nack"); }),
75 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Eric Newberry83872fd2015-08-06 17:01:24 -070076
Junxiao Shif5b5ae22016-08-08 05:54:41 +000077 advanceClocks(time::milliseconds(40));
Eric Newberry83872fd2015-08-06 17:01:24 -070078
Junxiao Shi85d90832016-08-04 03:19:46 +000079 face.receive(*makeData("/Bye/World/a"));
80 face.receive(*makeData("/Hello/World/a"));
Eric Newberry83872fd2015-08-06 17:01:24 -070081
Junxiao Shif5b5ae22016-08-08 05:54:41 +000082 advanceClocks(time::milliseconds(50), 2);
Eric Newberry83872fd2015-08-06 17:01:24 -070083
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([]{}),
Junxiao Shi103d8ed2016-08-07 20:34:10 +000092 bind([&nTimeouts] { ++nTimeouts; }));
Junxiao Shif5b5ae22016-08-08 05:54:41 +000093 advanceClocks(time::milliseconds(200), 5);
Eric Newberry83872fd2015-08-06 17:01:24 -070094 BOOST_CHECK_EQUAL(nTimeouts, 1);
95}
96
Alexander Afanasyev1013fd02017-01-03 13:19:03 -080097BOOST_AUTO_TEST_CASE(ExpressMultipleInterestData)
98{
99 size_t nData = 0;
100
101 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
102 [&] (const Interest& i, const Data& d) {
103 ++nData;
104 },
105 bind([] { BOOST_FAIL("Unexpected Nack"); }),
106 bind([] { BOOST_FAIL("Unexpected timeout"); }));
107
108 face.expressInterest(Interest("/Hello/World/a", time::milliseconds(50)),
109 [&] (const Interest& i, const Data& d) {
110 ++nData;
111 },
112 bind([] { BOOST_FAIL("Unexpected Nack"); }),
113 bind([] { BOOST_FAIL("Unexpected timeout"); }));
114
115 advanceClocks(time::milliseconds(40));
116
117 face.receive(*makeData("/Hello/World/a/b"));
118
119 advanceClocks(time::milliseconds(50), 2);
120
121 BOOST_CHECK_EQUAL(nData, 2);
122 BOOST_CHECK_EQUAL(face.sentInterests.size(), 2);
123 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
124}
125
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000126BOOST_AUTO_TEST_CASE(ExpressInterestEmptyDataCallback)
127{
128 face.expressInterest(Interest("/Hello/World"),
129 nullptr,
130 bind([] { BOOST_FAIL("Unexpected Nack"); }),
131 bind([] { BOOST_FAIL("Unexpected timeout"); }));
132 advanceClocks(time::milliseconds(1));
133
134 BOOST_CHECK_NO_THROW(do {
135 face.receive(*makeData("/Hello/World/a"));
136 advanceClocks(time::milliseconds(1));
137 } while (false));
138}
139
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400140BOOST_AUTO_TEST_CASE(ExpressInterestTimeout)
141{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800142 size_t nTimeouts = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800143 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000144 bind([] { BOOST_FAIL("Unexpected Data"); }),
145 bind([] { BOOST_FAIL("Unexpected Nack"); }),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800146 [&nTimeouts] (const Interest& i) {
147 BOOST_CHECK_EQUAL(i.getName(), "/Hello/World");
148 ++nTimeouts;
149 });
Eric Newberry83872fd2015-08-06 17:01:24 -0700150
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000151 advanceClocks(time::milliseconds(200), 5);
Eric Newberry83872fd2015-08-06 17:01:24 -0700152
153 BOOST_CHECK_EQUAL(nTimeouts, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800154 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
155 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
156 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0);
Eric Newberry83872fd2015-08-06 17:01:24 -0700157}
158
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000159BOOST_AUTO_TEST_CASE(ExpressInterestEmptyTimeoutCallback)
160{
161 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
162 bind([] { BOOST_FAIL("Unexpected Data"); }),
163 bind([] { BOOST_FAIL("Unexpected Nack"); }),
164 nullptr);
165 advanceClocks(time::milliseconds(40));
166
167 BOOST_CHECK_NO_THROW(do {
168 advanceClocks(time::milliseconds(6), 2);
169 } while (false));
170}
171
Eric Newberry83872fd2015-08-06 17:01:24 -0700172BOOST_AUTO_TEST_CASE(ExpressInterestNack)
173{
174 size_t nNacks = 0;
175
176 Interest interest("/Hello/World", time::milliseconds(50));
177
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800178 face.expressInterest(interest,
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000179 bind([] { BOOST_FAIL("Unexpected Data"); }),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800180 [&] (const Interest& i, const lp::Nack& n) {
181 BOOST_CHECK(i.getName().isPrefixOf(n.getInterest().getName()));
182 BOOST_CHECK_EQUAL(i.getName(), "/Hello/World");
183 BOOST_CHECK_EQUAL(n.getReason(), lp::NackReason::DUPLICATE);
184 ++nNacks;
185 },
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000186 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Eric Newberry83872fd2015-08-06 17:01:24 -0700187
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000188 advanceClocks(time::milliseconds(40));
Eric Newberry83872fd2015-08-06 17:01:24 -0700189
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000190 face.receive(makeNack(face.sentInterests.at(0), lp::NackReason::DUPLICATE));
Eric Newberry83872fd2015-08-06 17:01:24 -0700191
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000192 advanceClocks(time::milliseconds(50), 2);
Eric Newberry83872fd2015-08-06 17:01:24 -0700193
194 BOOST_CHECK_EQUAL(nNacks, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800195 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
Eric Newberry83872fd2015-08-06 17:01:24 -0700196}
197
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800198BOOST_AUTO_TEST_CASE(ExpressMultipleInterestNack)
199{
200 size_t nNacks = 0;
201
202 Interest interest("/Hello/World", time::milliseconds(50));
203 interest.setNonce(1);
204
205 face.expressInterest(interest,
206 bind([] { BOOST_FAIL("Unexpected Data"); }),
207 [&] (const Interest& i, const lp::Nack& n) {
208 ++nNacks;
209 },
210 bind([] { BOOST_FAIL("Unexpected timeout"); }));
211
212 interest.setNonce(2);
213 face.expressInterest(interest,
214 bind([] { BOOST_FAIL("Unexpected Data"); }),
215 [&] (const Interest& i, const lp::Nack& n) {
216 ++nNacks;
217 },
218 bind([] { BOOST_FAIL("Unexpected timeout"); }));
219
220 advanceClocks(time::milliseconds(40));
221
222 face.receive(makeNack(face.sentInterests.at(1), lp::NackReason::DUPLICATE));
223
224 advanceClocks(time::milliseconds(50), 2);
225
226 BOOST_CHECK_EQUAL(nNacks, 2);
227 BOOST_CHECK_EQUAL(face.sentInterests.size(), 2);
228}
229
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000230BOOST_AUTO_TEST_CASE(ExpressInterestEmptyNackCallback)
231{
232 face.expressInterest(Interest("/Hello/World"),
233 bind([] { BOOST_FAIL("Unexpected Data"); }),
234 nullptr,
235 bind([] { BOOST_FAIL("Unexpected timeout"); }));
236 advanceClocks(time::milliseconds(1));
237
238 BOOST_CHECK_NO_THROW(do {
239 face.receive(makeNack(face.sentInterests.at(0), lp::NackReason::DUPLICATE));
240 advanceClocks(time::milliseconds(1));
241 } while (false));
242}
243
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800244BOOST_AUTO_TEST_CASE(RemovePendingInterest)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400245{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800246 const PendingInterestId* interestId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800247 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000248 bind([] { BOOST_FAIL("Unexpected data"); }),
249 bind([] { BOOST_FAIL("Unexpected nack"); }),
250 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800251 advanceClocks(time::milliseconds(10));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400252
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800253 face.removePendingInterest(interestId);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800254 advanceClocks(time::milliseconds(10));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400255
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000256 face.receive(*makeData("/Hello/World/%21"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000257 advanceClocks(time::milliseconds(200), 5);
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100258
259 // avoid "test case [...] did not check any assertions" message from Boost.Test
260 BOOST_CHECK(true);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400261}
262
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000263BOOST_AUTO_TEST_CASE(RemoveAllPendingInterests)
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500264{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800265 face.expressInterest(Interest("/Hello/World/0", time::milliseconds(50)),
266 bind([] { BOOST_FAIL("Unexpected data"); }),
267 bind([] { BOOST_FAIL("Unexpected nack"); }),
268 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500269
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800270 face.expressInterest(Interest("/Hello/World/1", time::milliseconds(50)),
271 bind([] { BOOST_FAIL("Unexpected data"); }),
272 bind([] { BOOST_FAIL("Unexpected nack"); }),
273 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500274
275 advanceClocks(time::milliseconds(10));
276
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800277 face.removeAllPendingInterests();
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500278 advanceClocks(time::milliseconds(10));
279
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800280 BOOST_CHECK_EQUAL(face.getNPendingInterests(), 0);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500281
Junxiao Shi85d90832016-08-04 03:19:46 +0000282 face.receive(*makeData("/Hello/World/0"));
283 face.receive(*makeData("/Hello/World/1"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000284 advanceClocks(time::milliseconds(200), 5);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500285}
286
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000287BOOST_AUTO_TEST_CASE(DestructionWithoutCancellingPendingInterests) // Bug #2518
288{
289 {
290 DummyClientFace face2(io, m_keyChain);
291 face2.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
Alexander Afanasyeve6835fe2017-01-19 20:05:01 -0800292 nullptr, nullptr, nullptr);
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000293 advanceClocks(time::milliseconds(50), 2);
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000294 }
295
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100296 advanceClocks(time::milliseconds(50), 2); // should not crash
297
298 // avoid "test case [...] did not check any assertions" message from Boost.Test
299 BOOST_CHECK(true);
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000300}
301
302BOOST_AUTO_TEST_SUITE_END() // Consumer
303
304BOOST_AUTO_TEST_SUITE(Producer)
305
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000306BOOST_AUTO_TEST_CASE(PutData)
307{
308 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
309
310 Data data("/4g7xxcuEow/KFvK5Kf2m");
311 signData(data);
312 face.put(data);
313
314 lp::CachePolicy cachePolicy;
315 cachePolicy.setPolicy(lp::CachePolicyType::NO_CACHE);
316 data.setTag(make_shared<lp::CachePolicyTag>(cachePolicy));
Eric Newberry4d261b62016-11-10 13:40:09 -0700317 data.setTag(make_shared<lp::CongestionMarkTag>(1));
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000318 face.put(data);
319
320 advanceClocks(time::milliseconds(10));
321 BOOST_REQUIRE_EQUAL(face.sentData.size(), 2);
322 BOOST_CHECK(face.sentData[0].getTag<lp::CachePolicyTag>() == nullptr);
Eric Newberry4d261b62016-11-10 13:40:09 -0700323 BOOST_CHECK(face.sentData[0].getTag<lp::CongestionMarkTag>() == nullptr);
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000324 BOOST_CHECK(face.sentData[1].getTag<lp::CachePolicyTag>() != nullptr);
Eric Newberry4d261b62016-11-10 13:40:09 -0700325 BOOST_CHECK(face.sentData[1].getTag<lp::CongestionMarkTag>() != nullptr);
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000326}
327
Junxiao Shib6828912017-11-20 14:06:32 +0000328BOOST_AUTO_TEST_CASE(PutDataLoopback)
329{
330 bool hasInterest1 = false, hasData = false;
331
332 // first InterestFilter allows loopback and should receive Interest
333 face.setInterestFilter("/", [&] (const InterestFilter&, const Interest& interest) {
334 hasInterest1 = true;
335 // do not respond with Data right away, so Face must send Interest to forwarder
336 });
337 // second InterestFilter disallows loopback and should not receive Interest
338 face.setInterestFilter(InterestFilter("/").allowLoopback(false),
339 bind([] { BOOST_ERROR("Unexpected Interest on second InterestFilter"); }));
340
341 face.expressInterest(Interest("/A"),
342 bind([&] { hasData = true; }),
343 bind([] { BOOST_FAIL("Unexpected nack"); }),
344 bind([] { BOOST_FAIL("Unexpected timeout"); }));
345 advanceClocks(time::milliseconds(1));
346 BOOST_CHECK_EQUAL(hasInterest1, true); // Interest looped back
347 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1); // Interest sent to forwarder
348 BOOST_CHECK_EQUAL(hasData, false); // waiting for Data
349
350 face.put(*makeData("/A/B")); // first InterestFilter responds with Data
351 advanceClocks(time::milliseconds(1));
352 BOOST_CHECK_EQUAL(hasData, true);
353 BOOST_CHECK_EQUAL(face.sentData.size(), 0); // do not spill Data to forwarder
354}
355
Junxiao Shi859888f2017-09-12 14:29:16 +0000356BOOST_AUTO_TEST_CASE(PutMultipleData)
357{
358 bool hasInterest1 = false;
359 // register two Interest destinations
360 face.setInterestFilter("/", bind([&] {
361 hasInterest1 = true;
362 // sending Data right away from the first destination, don't care whether Interest goes to second destination
363 face.put(*makeData("/A/B"));
364 }));
365 face.setInterestFilter("/", bind([]{}));
366 advanceClocks(time::milliseconds(10));
367
368 face.receive(*makeInterest("/A"));
369 advanceClocks(time::milliseconds(10));
370 BOOST_CHECK(hasInterest1);
371 BOOST_CHECK_EQUAL(face.sentData.size(), 1);
372 BOOST_CHECK_EQUAL(face.sentData.at(0).getName(), "/A/B");
373
374 face.put(*makeData("/A/C"));
375 BOOST_CHECK_EQUAL(face.sentData.size(), 1); // additional Data are ignored
376}
377
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000378BOOST_AUTO_TEST_CASE(PutNack)
379{
Junxiao Shi79a7a162017-09-09 08:33:57 +0000380 face.setInterestFilter("/", bind([]{})); // register one Interest destination so that face can accept Nacks
381 advanceClocks(time::milliseconds(10));
382
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000383 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0);
384
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000385 face.put(makeNack("/unsolicited", 18645250, lp::NackReason::NO_ROUTE));
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000386 advanceClocks(time::milliseconds(10));
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000387 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0); // unsolicited Nack would not be sent
Eric Newberry4d261b62016-11-10 13:40:09 -0700388
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000389 face.receive(*makeInterest("/Hello/World", 14247162));
390 face.receive(*makeInterest("/another/prefix", 92203002));
391 advanceClocks(time::milliseconds(10));
392
393 face.put(makeNack("/Hello/World", 14247162, lp::NackReason::DUPLICATE));
394 advanceClocks(time::milliseconds(10));
395 BOOST_REQUIRE_EQUAL(face.sentNacks.size(), 1);
396 BOOST_CHECK_EQUAL(face.sentNacks[0].getReason(), lp::NackReason::DUPLICATE);
397 BOOST_CHECK(face.sentNacks[0].getTag<lp::CongestionMarkTag>() == nullptr);
398
399 auto nack = makeNack("/another/prefix", 92203002, lp::NackReason::NO_ROUTE);
Eric Newberry4d261b62016-11-10 13:40:09 -0700400 nack.setTag(make_shared<lp::CongestionMarkTag>(1));
401 face.put(nack);
Eric Newberry4d261b62016-11-10 13:40:09 -0700402 advanceClocks(time::milliseconds(10));
403 BOOST_REQUIRE_EQUAL(face.sentNacks.size(), 2);
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000404 BOOST_CHECK_EQUAL(face.sentNacks[1].getReason(), lp::NackReason::NO_ROUTE);
Eric Newberry4d261b62016-11-10 13:40:09 -0700405 BOOST_CHECK(face.sentNacks[1].getTag<lp::CongestionMarkTag>() != nullptr);
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000406}
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500407
Junxiao Shi79a7a162017-09-09 08:33:57 +0000408BOOST_AUTO_TEST_CASE(PutMultipleNack)
409{
Junxiao Shi859888f2017-09-12 14:29:16 +0000410 bool hasInterest1 = false, hasInterest2 = false;
411 // register two Interest destinations
412 face.setInterestFilter("/", [&] (const InterestFilter&, const Interest& interest) {
413 hasInterest1 = true;
414 // sending Nack right away from the first destination, Interest should still go to second destination
415 face.put(makeNack(interest, lp::NackReason::CONGESTION));
416 });
417 face.setInterestFilter("/", bind([&] { hasInterest2 = true; }));
Junxiao Shi79a7a162017-09-09 08:33:57 +0000418 advanceClocks(time::milliseconds(10));
419
420 face.receive(*makeInterest("/A", 14333271));
421 advanceClocks(time::milliseconds(10));
Junxiao Shi859888f2017-09-12 14:29:16 +0000422 BOOST_CHECK(hasInterest1);
423 BOOST_CHECK(hasInterest2);
Junxiao Shi79a7a162017-09-09 08:33:57 +0000424
Junxiao Shi859888f2017-09-12 14:29:16 +0000425 // Nack from first destination is received, should wait for a response from the other destination
426 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0);
Junxiao Shi79a7a162017-09-09 08:33:57 +0000427
428 face.put(makeNack("/A", 14333271, lp::NackReason::NO_ROUTE)); // Nack from second destination
429 advanceClocks(time::milliseconds(10));
Junxiao Shi859888f2017-09-12 14:29:16 +0000430 BOOST_CHECK_EQUAL(face.sentNacks.size(), 1); // sending Nack after both destinations Nacked
Junxiao Shi79a7a162017-09-09 08:33:57 +0000431 BOOST_CHECK_EQUAL(face.sentNacks.at(0).getReason(), lp::NackReason::CONGESTION); // least severe reason
432
433 face.put(makeNack("/A", 14333271, lp::NackReason::DUPLICATE));
434 BOOST_CHECK_EQUAL(face.sentNacks.size(), 1); // additional Nacks are ignored
435}
436
Junxiao Shib6828912017-11-20 14:06:32 +0000437BOOST_AUTO_TEST_CASE(PutMultipleNackLoopback)
438{
439 bool hasInterest1 = false, hasNack = false;
440
441 // first InterestFilter allows loopback and should receive Interest
442 face.setInterestFilter("/", [&] (const InterestFilter&, const Interest& interest) {
443 hasInterest1 = true;
444 face.put(makeNack(interest, lp::NackReason::CONGESTION));
445 });
446 // second InterestFilter disallows loopback and should not receive Interest
447 face.setInterestFilter(InterestFilter("/").allowLoopback(false),
448 bind([] { BOOST_ERROR("Unexpected Interest on second InterestFilter"); }));
449
450 face.expressInterest(*makeInterest("/A", 28395852),
451 bind([] { BOOST_FAIL("Unexpected data"); }),
452 [&] (const Interest&, const lp::Nack& nack) {
453 hasNack = true;
454 BOOST_CHECK_EQUAL(nack.getReason(), lp::NackReason::CONGESTION);
455 },
456 bind([] { BOOST_FAIL("Unexpected timeout"); }));
457 advanceClocks(time::milliseconds(1));
458 BOOST_CHECK_EQUAL(hasInterest1, true); // Interest looped back
459 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1); // Interest sent to forwarder
460 BOOST_CHECK_EQUAL(hasNack, false); // waiting for Nack from forwarder
461
462 face.receive(makeNack("/A", 28395852, lp::NackReason::NO_ROUTE));
463 advanceClocks(time::milliseconds(1));
464 BOOST_CHECK_EQUAL(hasNack, true);
465}
466
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400467BOOST_AUTO_TEST_CASE(SetUnsetInterestFilter)
468{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800469 size_t nInterests = 0;
470 size_t nRegs = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400471 const RegisteredPrefixId* regPrefixId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800472 face.setInterestFilter("/Hello/World",
473 bind([&nInterests] { ++nInterests; }),
474 bind([&nRegs] { ++nRegs; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000475 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000476 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800477 BOOST_CHECK_EQUAL(nRegs, 1);
478 BOOST_CHECK_EQUAL(nInterests, 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400479
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000480 face.receive(Interest("/Hello/World/%21"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000481 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400482
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800483 BOOST_CHECK_EQUAL(nRegs, 1);
484 BOOST_CHECK_EQUAL(nInterests, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400485
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000486 face.receive(Interest("/Bye/World/%21"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800487 advanceClocks(time::milliseconds(10000), 10);
488 BOOST_CHECK_EQUAL(nInterests, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400489
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000490 face.receive(Interest("/Hello/World/%21/2"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000491 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800492 BOOST_CHECK_EQUAL(nInterests, 2);
493
494 // removing filter
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800495 face.unsetInterestFilter(regPrefixId);
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000496 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400497
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000498 face.receive(Interest("/Hello/World/%21/3"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800499 BOOST_CHECK_EQUAL(nInterests, 2);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400500
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000501 face.unsetInterestFilter(static_cast<const RegisteredPrefixId*>(nullptr));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000502 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400503
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000504 face.unsetInterestFilter(static_cast<const InterestFilterId*>(nullptr));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000505 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800506}
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400507
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000508BOOST_AUTO_TEST_CASE(SetInterestFilterEmptyInterestCallback)
509{
510 face.setInterestFilter("/A", nullptr);
511 advanceClocks(time::milliseconds(1));
512
513 BOOST_CHECK_NO_THROW(do {
514 face.receive(*makeInterest("/A/1"));
515 advanceClocks(time::milliseconds(1));
516 } while (false));
517}
518
Joao Pereira0b3cac52015-07-02 14:49:49 -0400519BOOST_AUTO_TEST_CASE(SetUnsetInterestFilterWithoutSucessCallback)
520{
521 size_t nInterests = 0;
522 const RegisteredPrefixId* regPrefixId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800523 face.setInterestFilter("/Hello/World",
524 bind([&nInterests] { ++nInterests; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000525 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000526 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400527 BOOST_CHECK_EQUAL(nInterests, 0);
528
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000529 face.receive(Interest("/Hello/World/%21"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000530 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400531
532 BOOST_CHECK_EQUAL(nInterests, 1);
533
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000534 face.receive(Interest("/Bye/World/%21"));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400535 advanceClocks(time::milliseconds(10000), 10);
536 BOOST_CHECK_EQUAL(nInterests, 1);
537
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000538 face.receive(Interest("/Hello/World/%21/2"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000539 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400540 BOOST_CHECK_EQUAL(nInterests, 2);
541
542 // removing filter
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800543 face.unsetInterestFilter(regPrefixId);
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000544 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400545
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000546 face.receive(Interest("/Hello/World/%21/3"));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400547 BOOST_CHECK_EQUAL(nInterests, 2);
548
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000549 face.unsetInterestFilter(static_cast<const RegisteredPrefixId*>(nullptr));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000550 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400551
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000552 face.unsetInterestFilter(static_cast<const InterestFilterId*>(nullptr));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000553 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400554}
555
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800556BOOST_FIXTURE_TEST_CASE(SetInterestFilterFail, FacesNoRegistrationReplyFixture)
557{
558 // don't enable registration reply
559 size_t nRegFailed = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800560 face.setInterestFilter("/Hello/World",
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000561 bind([] { BOOST_FAIL("Unexpected Interest"); }),
562 bind([] { BOOST_FAIL("Unexpected success of setInterestFilter"); }),
563 bind([&nRegFailed] { ++nRegFailed; }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800564
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000565 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800566 BOOST_CHECK_EQUAL(nRegFailed, 0);
567
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000568 advanceClocks(time::milliseconds(2000), 5);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800569 BOOST_CHECK_EQUAL(nRegFailed, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400570}
571
Joao Pereira0b3cac52015-07-02 14:49:49 -0400572BOOST_FIXTURE_TEST_CASE(SetInterestFilterFailWithoutSuccessCallback, FacesNoRegistrationReplyFixture)
573{
574 // don't enable registration reply
575 size_t nRegFailed = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800576 face.setInterestFilter("/Hello/World",
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000577 bind([] { BOOST_FAIL("Unexpected Interest"); }),
578 bind([&nRegFailed] { ++nRegFailed; }));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400579
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000580 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400581 BOOST_CHECK_EQUAL(nRegFailed, 0);
582
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000583 advanceClocks(time::milliseconds(2000), 5);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400584 BOOST_CHECK_EQUAL(nRegFailed, 1);
585}
586
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400587BOOST_AUTO_TEST_CASE(RegisterUnregisterPrefix)
588{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800589 size_t nRegSuccesses = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400590 const RegisteredPrefixId* regPrefixId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800591 face.registerPrefix("/Hello/World",
592 bind([&nRegSuccesses] { ++nRegSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000593 bind([] { BOOST_FAIL("Unexpected registerPrefix failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400594
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000595 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400596 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
597
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800598 size_t nUnregSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800599 face.unregisterPrefix(regPrefixId,
600 bind([&nUnregSuccesses] { ++nUnregSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000601 bind([] { BOOST_FAIL("Unexpected unregisterPrefix failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400602
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000603 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400604 BOOST_CHECK_EQUAL(nUnregSuccesses, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400605}
606
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800607BOOST_FIXTURE_TEST_CASE(RegisterUnregisterPrefixFail, FacesNoRegistrationReplyFixture)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400608{
Junxiao Shib6828912017-11-20 14:06:32 +0000609 // don't enable registration reply
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800610 size_t nRegFailures = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800611 face.registerPrefix("/Hello/World",
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000612 bind([] { BOOST_FAIL("Unexpected registerPrefix success"); }),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800613 bind([&nRegFailures] { ++nRegFailures; }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400614
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000615 advanceClocks(time::milliseconds(5000), 20);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800616 BOOST_CHECK_EQUAL(nRegFailures, 1);
617}
618
619BOOST_AUTO_TEST_CASE(SimilarFilters)
620{
621 size_t nInInterests1 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800622 face.setInterestFilter("/Hello/World",
623 bind([&nInInterests1] { ++nInInterests1; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000624 nullptr,
625 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800626
627 size_t nInInterests2 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800628 face.setInterestFilter("/Hello",
629 bind([&nInInterests2] { ++nInInterests2; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000630 nullptr,
631 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400632
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800633 size_t nInInterests3 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800634 face.setInterestFilter("/Los/Angeles/Lakers",
635 bind([&nInInterests3] { ++nInInterests3; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000636 nullptr,
637 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400638
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000639 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400640
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000641 face.receive(Interest("/Hello/World/%21"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000642 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400643
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800644 BOOST_CHECK_EQUAL(nInInterests1, 1);
645 BOOST_CHECK_EQUAL(nInInterests2, 1);
646 BOOST_CHECK_EQUAL(nInInterests3, 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400647}
648
649BOOST_AUTO_TEST_CASE(SetRegexFilterError)
650{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800651 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
652 [] (const Name&, const Interest&) {
653 BOOST_FAIL("InterestFilter::Error should have been triggered");
654 },
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000655 nullptr,
656 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400657
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000658 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400659
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800660 BOOST_REQUIRE_THROW(face.receive(Interest("/Hello/World/XXX/b/c")), InterestFilter::Error);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400661}
662
663BOOST_AUTO_TEST_CASE(SetRegexFilter)
664{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800665 size_t nInInterests = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800666 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
667 bind([&nInInterests] { ++nInInterests; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000668 nullptr,
669 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400670
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000671 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400672
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800673 face.receive(Interest("/Hello/World/a")); // shouldn't match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400674 BOOST_CHECK_EQUAL(nInInterests, 0);
675
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800676 face.receive(Interest("/Hello/World/a/b")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400677 BOOST_CHECK_EQUAL(nInInterests, 1);
678
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800679 face.receive(Interest("/Hello/World/a/b/c")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400680 BOOST_CHECK_EQUAL(nInInterests, 2);
681
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800682 face.receive(Interest("/Hello/World/a/b/d")); // should not match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400683 BOOST_CHECK_EQUAL(nInInterests, 2);
684}
685
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400686BOOST_AUTO_TEST_CASE(SetRegexFilterAndRegister)
687{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800688 size_t nInInterests = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800689 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
690 bind([&nInInterests] { ++nInInterests; }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400691
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800692 size_t nRegSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800693 face.registerPrefix("/Hello/World",
694 bind([&nRegSuccesses] { ++nRegSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000695 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400696
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000697 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400698 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
699
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800700 face.receive(Interest("/Hello/World/a")); // shouldn't match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400701 BOOST_CHECK_EQUAL(nInInterests, 0);
702
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800703 face.receive(Interest("/Hello/World/a/b")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400704 BOOST_CHECK_EQUAL(nInInterests, 1);
705
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800706 face.receive(Interest("/Hello/World/a/b/c")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400707 BOOST_CHECK_EQUAL(nInInterests, 2);
708
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800709 face.receive(Interest("/Hello/World/a/b/d")); // should not match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400710 BOOST_CHECK_EQUAL(nInInterests, 2);
711}
712
Junxiao Shia1ea5062014-12-27 22:33:39 -0700713BOOST_FIXTURE_TEST_CASE(SetInterestFilterNoReg, FacesNoRegistrationReplyFixture) // Bug 2318
714{
715 // This behavior is specific to DummyClientFace.
716 // Regular Face won't accept incoming packets until something is sent.
717
718 int hit = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800719 face.setInterestFilter(Name("/"), bind([&hit] { ++hit; }));
720 face.processEvents(time::milliseconds(-1));
Junxiao Shia1ea5062014-12-27 22:33:39 -0700721
722 auto interest = make_shared<Interest>("/A");
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800723 face.receive(*interest);
724 face.processEvents(time::milliseconds(-1));
Junxiao Shia1ea5062014-12-27 22:33:39 -0700725
726 BOOST_CHECK_EQUAL(hit, 1);
727}
728
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000729BOOST_AUTO_TEST_SUITE_END() // Producer
730
Junxiao Shiae0b4182016-08-08 22:53:17 +0000731BOOST_AUTO_TEST_SUITE(IoRoutines)
732
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500733BOOST_AUTO_TEST_CASE(ProcessEvents)
734{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800735 face.processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500736
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800737 size_t nRegSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800738 face.registerPrefix("/Hello/World",
739 bind([&nRegSuccesses] { ++nRegSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000740 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500741
742 // io_service::poll() without reset
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800743 face.getIoService().poll();
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500744 BOOST_CHECK_EQUAL(nRegSuccesses, 0);
745
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800746 face.processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500747 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
748}
749
Junxiao Shiae0b4182016-08-08 22:53:17 +0000750BOOST_AUTO_TEST_CASE(DestroyWithoutProcessEvents) // Bug 3248
751{
752 auto face2 = make_unique<Face>(io);
753 face2.reset();
754
755 io.poll(); // should not crash
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100756
757 // avoid "test case [...] did not check any assertions" message from Boost.Test
758 BOOST_CHECK(true);
Junxiao Shiae0b4182016-08-08 22:53:17 +0000759}
760
761BOOST_AUTO_TEST_SUITE_END() // IoRoutines
762
763BOOST_AUTO_TEST_SUITE(Transport)
764
765using ndn::Transport;
766
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700767struct PibDirWithDefaultTpm
768{
769 const std::string PATH = "build/keys-with-default-tpm";
770};
771
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800772BOOST_FIXTURE_TEST_CASE(FaceTransport, IdentityManagementTimeFixture)
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800773{
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800774 BOOST_CHECK(Face().getTransport() != nullptr);
775
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800776 BOOST_CHECK(Face(shared_ptr<Transport>()).getTransport() != nullptr);
777 BOOST_CHECK(Face(shared_ptr<Transport>(), io).getTransport() != nullptr);
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800778 BOOST_CHECK(Face(shared_ptr<Transport>(), io, m_keyChain).getTransport() != nullptr);
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800779
780 auto transport = make_shared<TcpTransport>("localhost", "6363"); // no real io operations will be scheduled
781 BOOST_CHECK(Face(transport).getTransport() == transport);
782 BOOST_CHECK(Face(transport, io).getTransport() == transport);
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800783 BOOST_CHECK(Face(transport, io, m_keyChain).getTransport() == transport);
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800784}
785
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700786class WithEnv : private IdentityManagementTimeFixture
787{
788public:
789 WithEnv()
790 {
791 if (getenv("NDN_CLIENT_TRANSPORT") != nullptr) {
792 m_oldTransport = getenv("NDN_CLIENT_TRANSPORT");
793 unsetenv("NDN_CLIENT_TRANSPORT");
794 }
795 }
796
797 void
798 configure(const std::string& faceUri)
799 {
800 setenv("NDN_CLIENT_TRANSPORT", faceUri.c_str(), true);
801 }
802
803 ~WithEnv()
804 {
805 if (!m_oldTransport.empty()) {
806 setenv("NDN_CLIENT_TRANSPORT", m_oldTransport.c_str(), true);
807 }
808 else {
809 unsetenv("NDN_CLIENT_TRANSPORT");
810 }
811 }
812
813private:
814 std::string m_oldTransport;
815};
816
817class WithConfig : private TestHomeFixture<DefaultPibDir>
818{
819public:
820 void
821 configure(const std::string& faceUri)
822 {
823 createClientConf({"transport=" + faceUri});
824 }
825};
826
827class WithEnvAndConfig : public WithEnv, public WithConfig
828{
829};
830
831typedef boost::mpl::vector<WithEnv, WithConfig> ConfigOptions;
832
833BOOST_FIXTURE_TEST_CASE(NoConfig, WithEnvAndConfig) // fixture configures test HOME and PIB/TPM path
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700834{
835 shared_ptr<Face> face;
836 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
837 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700838}
839
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700840BOOST_FIXTURE_TEST_CASE_TEMPLATE(Unix, T, ConfigOptions, T)
841{
842 this->configure("unix://some/path");
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700843
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700844 shared_ptr<Face> face;
845 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
846 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
847}
848
849BOOST_FIXTURE_TEST_CASE_TEMPLATE(Tcp, T, ConfigOptions, T)
850{
851 this->configure("tcp://127.0.0.1:6000");
852
853 shared_ptr<Face> face;
854 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
855 BOOST_CHECK(dynamic_pointer_cast<TcpTransport>(face->getTransport()) != nullptr);
856}
857
858BOOST_FIXTURE_TEST_CASE_TEMPLATE(WrongTransport, T, ConfigOptions, T)
859{
860 this->configure("wrong-transport:");
861
862 BOOST_CHECK_THROW(make_shared<Face>(), ConfigFile::Error);
863}
864
865BOOST_FIXTURE_TEST_CASE_TEMPLATE(WrongUri, T, ConfigOptions, T)
866{
867 this->configure("wrong-uri");
868
869 BOOST_CHECK_THROW(make_shared<Face>(), ConfigFile::Error);
870}
871
872BOOST_FIXTURE_TEST_CASE(EnvOverride, WithEnvAndConfig)
873{
874 this->WithEnv::configure("tcp://127.0.0.1:6000");
875 this->WithConfig::configure("unix://some/path");
876
877 shared_ptr<Face> face;
878 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
879 BOOST_CHECK(dynamic_pointer_cast<TcpTransport>(face->getTransport()) != nullptr);
880}
881
882BOOST_FIXTURE_TEST_CASE(ExplicitTransport, WithEnvAndConfig)
883{
884 this->WithEnv::configure("wrong-uri");
885 this->WithConfig::configure("wrong-transport:");
886
887 auto transport = make_shared<UnixTransport>("unix://some/path");
888 shared_ptr<Face> face;
889 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>(transport));
890 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
891}
892
Junxiao Shiae0b4182016-08-08 22:53:17 +0000893BOOST_AUTO_TEST_SUITE_END() // Transport
894
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700895BOOST_AUTO_TEST_SUITE_END() // TestFace
896
897} // namespace tests
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400898} // namespace ndn