blob: 2412c66b2cf2c24820a5fc5357f945dfa12cb81b [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/*
Davide Pesavento0f830802018-01-16 23:58:58 -05003 * Copyright (c) 2013-2018 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;
Davide Pesavento0f830802018-01-16 23:58:58 -050068 face.expressInterest(Interest("/Hello/World", 50_ms),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080069 [&] (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
Davide Pesavento0f830802018-01-16 23:58:58 -050077 advanceClocks(40_ms);
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
Davide Pesavento0f830802018-01-16 23:58:58 -050082 advanceClocks(50_ms, 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;
Davide Pesavento0f830802018-01-16 23:58:58 -050089 face.expressInterest(Interest("/Hello/World/a/2", 50_ms),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080090 bind([]{}),
91 bind([]{}),
Junxiao Shi103d8ed2016-08-07 20:34:10 +000092 bind([&nTimeouts] { ++nTimeouts; }));
Davide Pesavento0f830802018-01-16 23:58:58 -050093 advanceClocks(200_ms, 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
Davide Pesavento0f830802018-01-16 23:58:58 -0500101 face.expressInterest(Interest("/Hello/World", 50_ms),
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800102 [&] (const Interest& i, const Data& d) {
103 ++nData;
104 },
105 bind([] { BOOST_FAIL("Unexpected Nack"); }),
106 bind([] { BOOST_FAIL("Unexpected timeout"); }));
107
Davide Pesavento0f830802018-01-16 23:58:58 -0500108 face.expressInterest(Interest("/Hello/World/a", 50_ms),
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800109 [&] (const Interest& i, const Data& d) {
110 ++nData;
111 },
112 bind([] { BOOST_FAIL("Unexpected Nack"); }),
113 bind([] { BOOST_FAIL("Unexpected timeout"); }));
114
Davide Pesavento0f830802018-01-16 23:58:58 -0500115 advanceClocks(40_ms);
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800116
117 face.receive(*makeData("/Hello/World/a/b"));
118
Davide Pesavento0f830802018-01-16 23:58:58 -0500119 advanceClocks(50_ms, 2);
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800120
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"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500132 advanceClocks(1_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000133
134 BOOST_CHECK_NO_THROW(do {
135 face.receive(*makeData("/Hello/World/a"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500136 advanceClocks(1_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000137 } 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;
Davide Pesavento0f830802018-01-16 23:58:58 -0500143 face.expressInterest(Interest("/Hello/World", 50_ms),
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
Davide Pesavento0f830802018-01-16 23:58:58 -0500151 advanceClocks(200_ms, 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{
Davide Pesavento0f830802018-01-16 23:58:58 -0500161 face.expressInterest(Interest("/Hello/World", 50_ms),
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000162 bind([] { BOOST_FAIL("Unexpected Data"); }),
163 bind([] { BOOST_FAIL("Unexpected Nack"); }),
164 nullptr);
Davide Pesavento0f830802018-01-16 23:58:58 -0500165 advanceClocks(40_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000166
167 BOOST_CHECK_NO_THROW(do {
Davide Pesavento0f830802018-01-16 23:58:58 -0500168 advanceClocks(6_ms, 2);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000169 } while (false));
170}
171
Eric Newberry83872fd2015-08-06 17:01:24 -0700172BOOST_AUTO_TEST_CASE(ExpressInterestNack)
173{
174 size_t nNacks = 0;
175
Davide Pesavento0f830802018-01-16 23:58:58 -0500176 Interest interest("/Hello/World", 50_ms);
Eric Newberry83872fd2015-08-06 17:01:24 -0700177
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
Davide Pesavento0f830802018-01-16 23:58:58 -0500188 advanceClocks(40_ms);
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
Davide Pesavento0f830802018-01-16 23:58:58 -0500192 advanceClocks(50_ms, 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
Davide Pesavento0f830802018-01-16 23:58:58 -0500202 Interest interest("/Hello/World", 50_ms);
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800203 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
Davide Pesavento0f830802018-01-16 23:58:58 -0500220 advanceClocks(40_ms);
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800221
222 face.receive(makeNack(face.sentInterests.at(1), lp::NackReason::DUPLICATE));
223
Davide Pesavento0f830802018-01-16 23:58:58 -0500224 advanceClocks(50_ms, 2);
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800225
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"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500236 advanceClocks(1_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000237
238 BOOST_CHECK_NO_THROW(do {
239 face.receive(makeNack(face.sentInterests.at(0), lp::NackReason::DUPLICATE));
Davide Pesavento0f830802018-01-16 23:58:58 -0500240 advanceClocks(1_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000241 } 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 =
Davide Pesavento0f830802018-01-16 23:58:58 -0500247 face.expressInterest(Interest("/Hello/World", 50_ms),
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"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500251 advanceClocks(10_ms);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400252
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800253 face.removePendingInterest(interestId);
Davide Pesavento0f830802018-01-16 23:58:58 -0500254 advanceClocks(10_ms);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400255
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000256 face.receive(*makeData("/Hello/World/%21"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500257 advanceClocks(200_ms, 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{
Davide Pesavento0f830802018-01-16 23:58:58 -0500265 face.expressInterest(Interest("/Hello/World/0", 50_ms),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800266 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
Davide Pesavento0f830802018-01-16 23:58:58 -0500270 face.expressInterest(Interest("/Hello/World/1", 50_ms),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800271 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
Davide Pesavento0f830802018-01-16 23:58:58 -0500275 advanceClocks(10_ms);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500276
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800277 face.removeAllPendingInterests();
Davide Pesavento0f830802018-01-16 23:58:58 -0500278 advanceClocks(10_ms);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500279
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"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500284 advanceClocks(200_ms, 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);
Davide Pesavento0f830802018-01-16 23:58:58 -0500291 face2.expressInterest(Interest("/Hello/World", 50_ms),
Alexander Afanasyeve6835fe2017-01-19 20:05:01 -0800292 nullptr, nullptr, nullptr);
Davide Pesavento0f830802018-01-16 23:58:58 -0500293 advanceClocks(50_ms, 2);
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000294 }
295
Davide Pesavento0f830802018-01-16 23:58:58 -0500296 advanceClocks(50_ms, 2); // should not crash
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100297
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
Ashlesh Gawandee2404222018-05-21 18:30:24 -0500302BOOST_AUTO_TEST_CASE(DataCallbackPutData) // Bug 4596
303{
304 face.expressInterest(Interest("/localhost/notification/1"),
305 [&] (const Interest& i, const Data& d) {
306 face.put(*makeData("/chronosync/sampleDigest/1"));
307 }, nullptr, nullptr);
308 advanceClocks(10_ms);
309 BOOST_CHECK_EQUAL(face.sentInterests.back().getName(), "/localhost/notification/1");
310
311 face.receive(Interest("/chronosync/sampleDigest"));
312 advanceClocks(10_ms);
313
314 face.put(*makeData("/localhost/notification/1"));
315 advanceClocks(10_ms);
316 BOOST_CHECK_EQUAL(face.sentData.back().getName(), "/chronosync/sampleDigest/1");
317}
318
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000319BOOST_AUTO_TEST_SUITE_END() // Consumer
320
321BOOST_AUTO_TEST_SUITE(Producer)
322
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000323BOOST_AUTO_TEST_CASE(PutData)
324{
325 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
326
327 Data data("/4g7xxcuEow/KFvK5Kf2m");
328 signData(data);
329 face.put(data);
330
331 lp::CachePolicy cachePolicy;
332 cachePolicy.setPolicy(lp::CachePolicyType::NO_CACHE);
333 data.setTag(make_shared<lp::CachePolicyTag>(cachePolicy));
Eric Newberry4d261b62016-11-10 13:40:09 -0700334 data.setTag(make_shared<lp::CongestionMarkTag>(1));
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000335 face.put(data);
336
Davide Pesavento0f830802018-01-16 23:58:58 -0500337 advanceClocks(10_ms);
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000338 BOOST_REQUIRE_EQUAL(face.sentData.size(), 2);
339 BOOST_CHECK(face.sentData[0].getTag<lp::CachePolicyTag>() == nullptr);
Eric Newberry4d261b62016-11-10 13:40:09 -0700340 BOOST_CHECK(face.sentData[0].getTag<lp::CongestionMarkTag>() == nullptr);
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000341 BOOST_CHECK(face.sentData[1].getTag<lp::CachePolicyTag>() != nullptr);
Eric Newberry4d261b62016-11-10 13:40:09 -0700342 BOOST_CHECK(face.sentData[1].getTag<lp::CongestionMarkTag>() != nullptr);
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000343}
344
Junxiao Shib6828912017-11-20 14:06:32 +0000345BOOST_AUTO_TEST_CASE(PutDataLoopback)
346{
347 bool hasInterest1 = false, hasData = false;
348
349 // first InterestFilter allows loopback and should receive Interest
350 face.setInterestFilter("/", [&] (const InterestFilter&, const Interest& interest) {
351 hasInterest1 = true;
352 // do not respond with Data right away, so Face must send Interest to forwarder
353 });
354 // second InterestFilter disallows loopback and should not receive Interest
355 face.setInterestFilter(InterestFilter("/").allowLoopback(false),
356 bind([] { BOOST_ERROR("Unexpected Interest on second InterestFilter"); }));
357
358 face.expressInterest(Interest("/A"),
359 bind([&] { hasData = true; }),
360 bind([] { BOOST_FAIL("Unexpected nack"); }),
361 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500362 advanceClocks(1_ms);
Junxiao Shib6828912017-11-20 14:06:32 +0000363 BOOST_CHECK_EQUAL(hasInterest1, true); // Interest looped back
364 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1); // Interest sent to forwarder
365 BOOST_CHECK_EQUAL(hasData, false); // waiting for Data
366
367 face.put(*makeData("/A/B")); // first InterestFilter responds with Data
Davide Pesavento0f830802018-01-16 23:58:58 -0500368 advanceClocks(1_ms);
Junxiao Shib6828912017-11-20 14:06:32 +0000369 BOOST_CHECK_EQUAL(hasData, true);
370 BOOST_CHECK_EQUAL(face.sentData.size(), 0); // do not spill Data to forwarder
371}
372
Junxiao Shi859888f2017-09-12 14:29:16 +0000373BOOST_AUTO_TEST_CASE(PutMultipleData)
374{
375 bool hasInterest1 = false;
376 // register two Interest destinations
377 face.setInterestFilter("/", bind([&] {
378 hasInterest1 = true;
379 // sending Data right away from the first destination, don't care whether Interest goes to second destination
380 face.put(*makeData("/A/B"));
381 }));
382 face.setInterestFilter("/", bind([]{}));
Davide Pesavento0f830802018-01-16 23:58:58 -0500383 advanceClocks(10_ms);
Junxiao Shi859888f2017-09-12 14:29:16 +0000384
385 face.receive(*makeInterest("/A"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500386 advanceClocks(10_ms);
Junxiao Shi859888f2017-09-12 14:29:16 +0000387 BOOST_CHECK(hasInterest1);
388 BOOST_CHECK_EQUAL(face.sentData.size(), 1);
389 BOOST_CHECK_EQUAL(face.sentData.at(0).getName(), "/A/B");
390
391 face.put(*makeData("/A/C"));
392 BOOST_CHECK_EQUAL(face.sentData.size(), 1); // additional Data are ignored
393}
394
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000395BOOST_AUTO_TEST_CASE(PutNack)
396{
Junxiao Shi79a7a162017-09-09 08:33:57 +0000397 face.setInterestFilter("/", bind([]{})); // register one Interest destination so that face can accept Nacks
Davide Pesavento0f830802018-01-16 23:58:58 -0500398 advanceClocks(10_ms);
Junxiao Shi79a7a162017-09-09 08:33:57 +0000399
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000400 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0);
401
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000402 face.put(makeNack("/unsolicited", 18645250, lp::NackReason::NO_ROUTE));
Davide Pesavento0f830802018-01-16 23:58:58 -0500403 advanceClocks(10_ms);
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000404 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0); // unsolicited Nack would not be sent
Eric Newberry4d261b62016-11-10 13:40:09 -0700405
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000406 face.receive(*makeInterest("/Hello/World", 14247162));
407 face.receive(*makeInterest("/another/prefix", 92203002));
Davide Pesavento0f830802018-01-16 23:58:58 -0500408 advanceClocks(10_ms);
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000409
410 face.put(makeNack("/Hello/World", 14247162, lp::NackReason::DUPLICATE));
Davide Pesavento0f830802018-01-16 23:58:58 -0500411 advanceClocks(10_ms);
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000412 BOOST_REQUIRE_EQUAL(face.sentNacks.size(), 1);
413 BOOST_CHECK_EQUAL(face.sentNacks[0].getReason(), lp::NackReason::DUPLICATE);
414 BOOST_CHECK(face.sentNacks[0].getTag<lp::CongestionMarkTag>() == nullptr);
415
416 auto nack = makeNack("/another/prefix", 92203002, lp::NackReason::NO_ROUTE);
Eric Newberry4d261b62016-11-10 13:40:09 -0700417 nack.setTag(make_shared<lp::CongestionMarkTag>(1));
418 face.put(nack);
Davide Pesavento0f830802018-01-16 23:58:58 -0500419 advanceClocks(10_ms);
Eric Newberry4d261b62016-11-10 13:40:09 -0700420 BOOST_REQUIRE_EQUAL(face.sentNacks.size(), 2);
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000421 BOOST_CHECK_EQUAL(face.sentNacks[1].getReason(), lp::NackReason::NO_ROUTE);
Eric Newberry4d261b62016-11-10 13:40:09 -0700422 BOOST_CHECK(face.sentNacks[1].getTag<lp::CongestionMarkTag>() != nullptr);
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000423}
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500424
Junxiao Shi79a7a162017-09-09 08:33:57 +0000425BOOST_AUTO_TEST_CASE(PutMultipleNack)
426{
Junxiao Shi859888f2017-09-12 14:29:16 +0000427 bool hasInterest1 = false, hasInterest2 = false;
428 // register two Interest destinations
429 face.setInterestFilter("/", [&] (const InterestFilter&, const Interest& interest) {
430 hasInterest1 = true;
431 // sending Nack right away from the first destination, Interest should still go to second destination
432 face.put(makeNack(interest, lp::NackReason::CONGESTION));
433 });
434 face.setInterestFilter("/", bind([&] { hasInterest2 = true; }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500435 advanceClocks(10_ms);
Junxiao Shi79a7a162017-09-09 08:33:57 +0000436
437 face.receive(*makeInterest("/A", 14333271));
Davide Pesavento0f830802018-01-16 23:58:58 -0500438 advanceClocks(10_ms);
Junxiao Shi859888f2017-09-12 14:29:16 +0000439 BOOST_CHECK(hasInterest1);
440 BOOST_CHECK(hasInterest2);
Junxiao Shi79a7a162017-09-09 08:33:57 +0000441
Junxiao Shi859888f2017-09-12 14:29:16 +0000442 // Nack from first destination is received, should wait for a response from the other destination
443 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0);
Junxiao Shi79a7a162017-09-09 08:33:57 +0000444
445 face.put(makeNack("/A", 14333271, lp::NackReason::NO_ROUTE)); // Nack from second destination
Davide Pesavento0f830802018-01-16 23:58:58 -0500446 advanceClocks(10_ms);
Junxiao Shi859888f2017-09-12 14:29:16 +0000447 BOOST_CHECK_EQUAL(face.sentNacks.size(), 1); // sending Nack after both destinations Nacked
Junxiao Shi79a7a162017-09-09 08:33:57 +0000448 BOOST_CHECK_EQUAL(face.sentNacks.at(0).getReason(), lp::NackReason::CONGESTION); // least severe reason
449
450 face.put(makeNack("/A", 14333271, lp::NackReason::DUPLICATE));
451 BOOST_CHECK_EQUAL(face.sentNacks.size(), 1); // additional Nacks are ignored
452}
453
Junxiao Shib6828912017-11-20 14:06:32 +0000454BOOST_AUTO_TEST_CASE(PutMultipleNackLoopback)
455{
456 bool hasInterest1 = false, hasNack = false;
457
458 // first InterestFilter allows loopback and should receive Interest
459 face.setInterestFilter("/", [&] (const InterestFilter&, const Interest& interest) {
460 hasInterest1 = true;
461 face.put(makeNack(interest, lp::NackReason::CONGESTION));
462 });
463 // second InterestFilter disallows loopback and should not receive Interest
464 face.setInterestFilter(InterestFilter("/").allowLoopback(false),
465 bind([] { BOOST_ERROR("Unexpected Interest on second InterestFilter"); }));
466
467 face.expressInterest(*makeInterest("/A", 28395852),
468 bind([] { BOOST_FAIL("Unexpected data"); }),
469 [&] (const Interest&, const lp::Nack& nack) {
470 hasNack = true;
471 BOOST_CHECK_EQUAL(nack.getReason(), lp::NackReason::CONGESTION);
472 },
473 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500474 advanceClocks(1_ms);
Junxiao Shib6828912017-11-20 14:06:32 +0000475 BOOST_CHECK_EQUAL(hasInterest1, true); // Interest looped back
476 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1); // Interest sent to forwarder
477 BOOST_CHECK_EQUAL(hasNack, false); // waiting for Nack from forwarder
478
479 face.receive(makeNack("/A", 28395852, lp::NackReason::NO_ROUTE));
Davide Pesavento0f830802018-01-16 23:58:58 -0500480 advanceClocks(1_ms);
Junxiao Shib6828912017-11-20 14:06:32 +0000481 BOOST_CHECK_EQUAL(hasNack, true);
482}
483
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400484BOOST_AUTO_TEST_CASE(SetUnsetInterestFilter)
485{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800486 size_t nInterests = 0;
487 size_t nRegs = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400488 const RegisteredPrefixId* regPrefixId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800489 face.setInterestFilter("/Hello/World",
490 bind([&nInterests] { ++nInterests; }),
491 bind([&nRegs] { ++nRegs; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000492 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500493 advanceClocks(25_ms, 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800494 BOOST_CHECK_EQUAL(nRegs, 1);
495 BOOST_CHECK_EQUAL(nInterests, 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400496
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000497 face.receive(Interest("/Hello/World/%21"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500498 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400499
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800500 BOOST_CHECK_EQUAL(nRegs, 1);
501 BOOST_CHECK_EQUAL(nInterests, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400502
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000503 face.receive(Interest("/Bye/World/%21"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500504 advanceClocks(10000_ms, 10);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800505 BOOST_CHECK_EQUAL(nInterests, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400506
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000507 face.receive(Interest("/Hello/World/%21/2"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500508 advanceClocks(25_ms, 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800509 BOOST_CHECK_EQUAL(nInterests, 2);
510
511 // removing filter
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800512 face.unsetInterestFilter(regPrefixId);
Davide Pesavento0f830802018-01-16 23:58:58 -0500513 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400514
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000515 face.receive(Interest("/Hello/World/%21/3"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800516 BOOST_CHECK_EQUAL(nInterests, 2);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400517
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000518 face.unsetInterestFilter(static_cast<const RegisteredPrefixId*>(nullptr));
Davide Pesavento0f830802018-01-16 23:58:58 -0500519 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400520
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000521 face.unsetInterestFilter(static_cast<const InterestFilterId*>(nullptr));
Davide Pesavento0f830802018-01-16 23:58:58 -0500522 advanceClocks(25_ms, 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800523}
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400524
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000525BOOST_AUTO_TEST_CASE(SetInterestFilterEmptyInterestCallback)
526{
527 face.setInterestFilter("/A", nullptr);
Davide Pesavento0f830802018-01-16 23:58:58 -0500528 advanceClocks(1_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000529
530 BOOST_CHECK_NO_THROW(do {
531 face.receive(*makeInterest("/A/1"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500532 advanceClocks(1_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000533 } while (false));
534}
535
Joao Pereira0b3cac52015-07-02 14:49:49 -0400536BOOST_AUTO_TEST_CASE(SetUnsetInterestFilterWithoutSucessCallback)
537{
538 size_t nInterests = 0;
539 const RegisteredPrefixId* regPrefixId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800540 face.setInterestFilter("/Hello/World",
541 bind([&nInterests] { ++nInterests; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000542 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500543 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400544 BOOST_CHECK_EQUAL(nInterests, 0);
545
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000546 face.receive(Interest("/Hello/World/%21"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500547 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400548
549 BOOST_CHECK_EQUAL(nInterests, 1);
550
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000551 face.receive(Interest("/Bye/World/%21"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500552 advanceClocks(10000_ms, 10);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400553 BOOST_CHECK_EQUAL(nInterests, 1);
554
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000555 face.receive(Interest("/Hello/World/%21/2"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500556 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400557 BOOST_CHECK_EQUAL(nInterests, 2);
558
559 // removing filter
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800560 face.unsetInterestFilter(regPrefixId);
Davide Pesavento0f830802018-01-16 23:58:58 -0500561 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400562
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000563 face.receive(Interest("/Hello/World/%21/3"));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400564 BOOST_CHECK_EQUAL(nInterests, 2);
565
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000566 face.unsetInterestFilter(static_cast<const RegisteredPrefixId*>(nullptr));
Davide Pesavento0f830802018-01-16 23:58:58 -0500567 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400568
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000569 face.unsetInterestFilter(static_cast<const InterestFilterId*>(nullptr));
Davide Pesavento0f830802018-01-16 23:58:58 -0500570 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400571}
572
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800573BOOST_FIXTURE_TEST_CASE(SetInterestFilterFail, FacesNoRegistrationReplyFixture)
574{
575 // don't enable registration reply
576 size_t nRegFailed = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800577 face.setInterestFilter("/Hello/World",
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000578 bind([] { BOOST_FAIL("Unexpected Interest"); }),
579 bind([] { BOOST_FAIL("Unexpected success of setInterestFilter"); }),
580 bind([&nRegFailed] { ++nRegFailed; }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800581
Davide Pesavento0f830802018-01-16 23:58:58 -0500582 advanceClocks(25_ms, 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800583 BOOST_CHECK_EQUAL(nRegFailed, 0);
584
Davide Pesavento0f830802018-01-16 23:58:58 -0500585 advanceClocks(2000_ms, 5);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800586 BOOST_CHECK_EQUAL(nRegFailed, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400587}
588
Joao Pereira0b3cac52015-07-02 14:49:49 -0400589BOOST_FIXTURE_TEST_CASE(SetInterestFilterFailWithoutSuccessCallback, FacesNoRegistrationReplyFixture)
590{
591 // don't enable registration reply
592 size_t nRegFailed = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800593 face.setInterestFilter("/Hello/World",
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000594 bind([] { BOOST_FAIL("Unexpected Interest"); }),
595 bind([&nRegFailed] { ++nRegFailed; }));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400596
Davide Pesavento0f830802018-01-16 23:58:58 -0500597 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400598 BOOST_CHECK_EQUAL(nRegFailed, 0);
599
Davide Pesavento0f830802018-01-16 23:58:58 -0500600 advanceClocks(2000_ms, 5);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400601 BOOST_CHECK_EQUAL(nRegFailed, 1);
602}
603
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400604BOOST_AUTO_TEST_CASE(RegisterUnregisterPrefix)
605{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800606 size_t nRegSuccesses = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400607 const RegisteredPrefixId* regPrefixId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800608 face.registerPrefix("/Hello/World",
609 bind([&nRegSuccesses] { ++nRegSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000610 bind([] { BOOST_FAIL("Unexpected registerPrefix failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400611
Davide Pesavento0f830802018-01-16 23:58:58 -0500612 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400613 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
614
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800615 size_t nUnregSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800616 face.unregisterPrefix(regPrefixId,
617 bind([&nUnregSuccesses] { ++nUnregSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000618 bind([] { BOOST_FAIL("Unexpected unregisterPrefix failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400619
Davide Pesavento0f830802018-01-16 23:58:58 -0500620 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400621 BOOST_CHECK_EQUAL(nUnregSuccesses, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400622}
623
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800624BOOST_FIXTURE_TEST_CASE(RegisterUnregisterPrefixFail, FacesNoRegistrationReplyFixture)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400625{
Junxiao Shib6828912017-11-20 14:06:32 +0000626 // don't enable registration reply
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800627 size_t nRegFailures = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800628 face.registerPrefix("/Hello/World",
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000629 bind([] { BOOST_FAIL("Unexpected registerPrefix success"); }),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800630 bind([&nRegFailures] { ++nRegFailures; }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400631
Davide Pesavento0f830802018-01-16 23:58:58 -0500632 advanceClocks(5000_ms, 20);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800633 BOOST_CHECK_EQUAL(nRegFailures, 1);
634}
635
636BOOST_AUTO_TEST_CASE(SimilarFilters)
637{
638 size_t nInInterests1 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800639 face.setInterestFilter("/Hello/World",
640 bind([&nInInterests1] { ++nInInterests1; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000641 nullptr,
642 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800643
644 size_t nInInterests2 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800645 face.setInterestFilter("/Hello",
646 bind([&nInInterests2] { ++nInInterests2; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000647 nullptr,
648 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400649
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800650 size_t nInInterests3 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800651 face.setInterestFilter("/Los/Angeles/Lakers",
652 bind([&nInInterests3] { ++nInInterests3; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000653 nullptr,
654 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400655
Davide Pesavento0f830802018-01-16 23:58:58 -0500656 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400657
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000658 face.receive(Interest("/Hello/World/%21"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500659 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400660
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800661 BOOST_CHECK_EQUAL(nInInterests1, 1);
662 BOOST_CHECK_EQUAL(nInInterests2, 1);
663 BOOST_CHECK_EQUAL(nInInterests3, 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400664}
665
666BOOST_AUTO_TEST_CASE(SetRegexFilterError)
667{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800668 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
669 [] (const Name&, const Interest&) {
670 BOOST_FAIL("InterestFilter::Error should have been triggered");
671 },
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000672 nullptr,
673 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400674
Davide Pesavento0f830802018-01-16 23:58:58 -0500675 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400676
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800677 BOOST_REQUIRE_THROW(face.receive(Interest("/Hello/World/XXX/b/c")), InterestFilter::Error);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400678}
679
680BOOST_AUTO_TEST_CASE(SetRegexFilter)
681{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800682 size_t nInInterests = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800683 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
684 bind([&nInInterests] { ++nInInterests; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000685 nullptr,
686 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400687
Davide Pesavento0f830802018-01-16 23:58:58 -0500688 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400689
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800690 face.receive(Interest("/Hello/World/a")); // shouldn't match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400691 BOOST_CHECK_EQUAL(nInInterests, 0);
692
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800693 face.receive(Interest("/Hello/World/a/b")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400694 BOOST_CHECK_EQUAL(nInInterests, 1);
695
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800696 face.receive(Interest("/Hello/World/a/b/c")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400697 BOOST_CHECK_EQUAL(nInInterests, 2);
698
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800699 face.receive(Interest("/Hello/World/a/b/d")); // should not match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400700 BOOST_CHECK_EQUAL(nInInterests, 2);
701}
702
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400703BOOST_AUTO_TEST_CASE(SetRegexFilterAndRegister)
704{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800705 size_t nInInterests = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800706 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
707 bind([&nInInterests] { ++nInInterests; }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400708
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800709 size_t nRegSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800710 face.registerPrefix("/Hello/World",
711 bind([&nRegSuccesses] { ++nRegSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000712 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400713
Davide Pesavento0f830802018-01-16 23:58:58 -0500714 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400715 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
716
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800717 face.receive(Interest("/Hello/World/a")); // shouldn't match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400718 BOOST_CHECK_EQUAL(nInInterests, 0);
719
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800720 face.receive(Interest("/Hello/World/a/b")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400721 BOOST_CHECK_EQUAL(nInInterests, 1);
722
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800723 face.receive(Interest("/Hello/World/a/b/c")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400724 BOOST_CHECK_EQUAL(nInInterests, 2);
725
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800726 face.receive(Interest("/Hello/World/a/b/d")); // should not match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400727 BOOST_CHECK_EQUAL(nInInterests, 2);
728}
729
Junxiao Shia1ea5062014-12-27 22:33:39 -0700730BOOST_FIXTURE_TEST_CASE(SetInterestFilterNoReg, FacesNoRegistrationReplyFixture) // Bug 2318
731{
732 // This behavior is specific to DummyClientFace.
733 // Regular Face won't accept incoming packets until something is sent.
734
735 int hit = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800736 face.setInterestFilter(Name("/"), bind([&hit] { ++hit; }));
737 face.processEvents(time::milliseconds(-1));
Junxiao Shia1ea5062014-12-27 22:33:39 -0700738
739 auto interest = make_shared<Interest>("/A");
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800740 face.receive(*interest);
741 face.processEvents(time::milliseconds(-1));
Junxiao Shia1ea5062014-12-27 22:33:39 -0700742
743 BOOST_CHECK_EQUAL(hit, 1);
744}
745
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000746BOOST_AUTO_TEST_SUITE_END() // Producer
747
Junxiao Shiae0b4182016-08-08 22:53:17 +0000748BOOST_AUTO_TEST_SUITE(IoRoutines)
749
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500750BOOST_AUTO_TEST_CASE(ProcessEvents)
751{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800752 face.processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500753
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800754 size_t nRegSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800755 face.registerPrefix("/Hello/World",
756 bind([&nRegSuccesses] { ++nRegSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000757 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500758
759 // io_service::poll() without reset
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800760 face.getIoService().poll();
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500761 BOOST_CHECK_EQUAL(nRegSuccesses, 0);
762
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800763 face.processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500764 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
765}
766
Junxiao Shiae0b4182016-08-08 22:53:17 +0000767BOOST_AUTO_TEST_CASE(DestroyWithoutProcessEvents) // Bug 3248
768{
769 auto face2 = make_unique<Face>(io);
770 face2.reset();
771
772 io.poll(); // should not crash
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100773
774 // avoid "test case [...] did not check any assertions" message from Boost.Test
775 BOOST_CHECK(true);
Junxiao Shiae0b4182016-08-08 22:53:17 +0000776}
777
778BOOST_AUTO_TEST_SUITE_END() // IoRoutines
779
780BOOST_AUTO_TEST_SUITE(Transport)
781
782using ndn::Transport;
783
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700784struct PibDirWithDefaultTpm
785{
786 const std::string PATH = "build/keys-with-default-tpm";
787};
788
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800789BOOST_FIXTURE_TEST_CASE(FaceTransport, IdentityManagementTimeFixture)
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800790{
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800791 BOOST_CHECK(Face().getTransport() != nullptr);
792
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800793 BOOST_CHECK(Face(shared_ptr<Transport>()).getTransport() != nullptr);
794 BOOST_CHECK(Face(shared_ptr<Transport>(), io).getTransport() != nullptr);
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800795 BOOST_CHECK(Face(shared_ptr<Transport>(), io, m_keyChain).getTransport() != nullptr);
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800796
797 auto transport = make_shared<TcpTransport>("localhost", "6363"); // no real io operations will be scheduled
798 BOOST_CHECK(Face(transport).getTransport() == transport);
799 BOOST_CHECK(Face(transport, io).getTransport() == transport);
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800800 BOOST_CHECK(Face(transport, io, m_keyChain).getTransport() == transport);
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800801}
802
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700803class WithEnv : private IdentityManagementTimeFixture
804{
805public:
806 WithEnv()
807 {
808 if (getenv("NDN_CLIENT_TRANSPORT") != nullptr) {
809 m_oldTransport = getenv("NDN_CLIENT_TRANSPORT");
810 unsetenv("NDN_CLIENT_TRANSPORT");
811 }
812 }
813
814 void
815 configure(const std::string& faceUri)
816 {
817 setenv("NDN_CLIENT_TRANSPORT", faceUri.c_str(), true);
818 }
819
820 ~WithEnv()
821 {
822 if (!m_oldTransport.empty()) {
823 setenv("NDN_CLIENT_TRANSPORT", m_oldTransport.c_str(), true);
824 }
825 else {
826 unsetenv("NDN_CLIENT_TRANSPORT");
827 }
828 }
829
830private:
831 std::string m_oldTransport;
832};
833
834class WithConfig : private TestHomeFixture<DefaultPibDir>
835{
836public:
837 void
838 configure(const std::string& faceUri)
839 {
840 createClientConf({"transport=" + faceUri});
841 }
842};
843
844class WithEnvAndConfig : public WithEnv, public WithConfig
845{
846};
847
848typedef boost::mpl::vector<WithEnv, WithConfig> ConfigOptions;
849
850BOOST_FIXTURE_TEST_CASE(NoConfig, WithEnvAndConfig) // fixture configures test HOME and PIB/TPM path
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700851{
852 shared_ptr<Face> face;
853 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
854 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700855}
856
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700857BOOST_FIXTURE_TEST_CASE_TEMPLATE(Unix, T, ConfigOptions, T)
858{
859 this->configure("unix://some/path");
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700860
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700861 shared_ptr<Face> face;
862 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
863 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
864}
865
866BOOST_FIXTURE_TEST_CASE_TEMPLATE(Tcp, T, ConfigOptions, T)
867{
868 this->configure("tcp://127.0.0.1:6000");
869
870 shared_ptr<Face> face;
871 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
872 BOOST_CHECK(dynamic_pointer_cast<TcpTransport>(face->getTransport()) != nullptr);
873}
874
875BOOST_FIXTURE_TEST_CASE_TEMPLATE(WrongTransport, T, ConfigOptions, T)
876{
877 this->configure("wrong-transport:");
878
879 BOOST_CHECK_THROW(make_shared<Face>(), ConfigFile::Error);
880}
881
882BOOST_FIXTURE_TEST_CASE_TEMPLATE(WrongUri, T, ConfigOptions, T)
883{
884 this->configure("wrong-uri");
885
886 BOOST_CHECK_THROW(make_shared<Face>(), ConfigFile::Error);
887}
888
889BOOST_FIXTURE_TEST_CASE(EnvOverride, WithEnvAndConfig)
890{
891 this->WithEnv::configure("tcp://127.0.0.1:6000");
892 this->WithConfig::configure("unix://some/path");
893
894 shared_ptr<Face> face;
895 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
896 BOOST_CHECK(dynamic_pointer_cast<TcpTransport>(face->getTransport()) != nullptr);
897}
898
899BOOST_FIXTURE_TEST_CASE(ExplicitTransport, WithEnvAndConfig)
900{
901 this->WithEnv::configure("wrong-uri");
902 this->WithConfig::configure("wrong-transport:");
903
904 auto transport = make_shared<UnixTransport>("unix://some/path");
905 shared_ptr<Face> face;
906 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>(transport));
907 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
908}
909
Junxiao Shiae0b4182016-08-08 22:53:17 +0000910BOOST_AUTO_TEST_SUITE_END() // Transport
911
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700912BOOST_AUTO_TEST_SUITE_END() // TestFace
913
914} // namespace tests
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400915} // namespace ndn