blob: 6f7bb173ad277b37bf711f7a7081882d0f9d78f5 [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
Davide Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/face.hpp"
23#include "ndn-cxx/lp/tags.hpp"
24#include "ndn-cxx/transport/tcp-transport.hpp"
25#include "ndn-cxx/transport/unix-transport.hpp"
26#include "ndn-cxx/util/dummy-client-face.hpp"
27#include "ndn-cxx/util/scheduler.hpp"
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040028
Davide Pesavento7e780642018-11-24 15:51:34 -050029#include "tests/boost-test.hpp"
30#include "tests/make-interest-data.hpp"
31#include "tests/unit/identity-management-time-fixture.hpp"
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040032
33namespace ndn {
34namespace tests {
35
Junxiao Shia60d9362014-11-12 09:38:21 -070036using ndn::util::DummyClientFace;
Junxiao Shia60d9362014-11-12 09:38:21 -070037
Alexander Afanasyev80782e02017-01-04 13:16:54 -080038class FaceFixture : public IdentityManagementTimeFixture
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040039{
40public:
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080041 explicit
Junxiao Shia1ea5062014-12-27 22:33:39 -070042 FaceFixture(bool enableRegistrationReply = true)
Junxiao Shif5b5ae22016-08-08 05:54:41 +000043 : face(io, m_keyChain, {true, enableRegistrationReply})
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040044 {
45 }
46
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080047public:
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080048 DummyClientFace face;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040049};
50
Junxiao Shia1ea5062014-12-27 22:33:39 -070051class FacesNoRegistrationReplyFixture : public FaceFixture
Junxiao Shia60d9362014-11-12 09:38:21 -070052{
53public:
54 FacesNoRegistrationReplyFixture()
Junxiao Shia1ea5062014-12-27 22:33:39 -070055 : FaceFixture(false)
Junxiao Shia60d9362014-11-12 09:38:21 -070056 {
57 }
58};
59
Junxiao Shia1ea5062014-12-27 22:33:39 -070060BOOST_FIXTURE_TEST_SUITE(TestFace, FaceFixture)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040061
Junxiao Shi103d8ed2016-08-07 20:34:10 +000062BOOST_AUTO_TEST_SUITE(Consumer)
63
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040064BOOST_AUTO_TEST_CASE(ExpressInterestData)
65{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080066 size_t nData = 0;
Junxiao Shib55e5d32018-07-18 13:32:00 -060067 face.expressInterest(*makeInterest("/Hello/World", true, 50_ms),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080068 [&] (const Interest& i, const Data& d) {
69 BOOST_CHECK(i.getName().isPrefixOf(d.getName()));
70 BOOST_CHECK_EQUAL(i.getName(), "/Hello/World");
71 ++nData;
72 },
Junxiao Shi103d8ed2016-08-07 20:34:10 +000073 bind([] { BOOST_FAIL("Unexpected Nack"); }),
74 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Eric Newberry83872fd2015-08-06 17:01:24 -070075
Davide Pesavento0f830802018-01-16 23:58:58 -050076 advanceClocks(40_ms);
Eric Newberry83872fd2015-08-06 17:01:24 -070077
Junxiao Shi85d90832016-08-04 03:19:46 +000078 face.receive(*makeData("/Bye/World/a"));
79 face.receive(*makeData("/Hello/World/a"));
Eric Newberry83872fd2015-08-06 17:01:24 -070080
Davide Pesavento0f830802018-01-16 23:58:58 -050081 advanceClocks(50_ms, 2);
Eric Newberry83872fd2015-08-06 17:01:24 -070082
83 BOOST_CHECK_EQUAL(nData, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080084 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
85 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Eric Newberry83872fd2015-08-06 17:01:24 -070086
87 size_t nTimeouts = 0;
Junxiao Shib55e5d32018-07-18 13:32:00 -060088 face.expressInterest(*makeInterest("/Hello/World/a/2", false, 50_ms),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080089 bind([]{}),
90 bind([]{}),
Junxiao Shi103d8ed2016-08-07 20:34:10 +000091 bind([&nTimeouts] { ++nTimeouts; }));
Davide Pesavento0f830802018-01-16 23:58:58 -050092 advanceClocks(200_ms, 5);
Eric Newberry83872fd2015-08-06 17:01:24 -070093 BOOST_CHECK_EQUAL(nTimeouts, 1);
94}
95
Alexander Afanasyev1013fd02017-01-03 13:19:03 -080096BOOST_AUTO_TEST_CASE(ExpressMultipleInterestData)
97{
98 size_t nData = 0;
99
Junxiao Shib55e5d32018-07-18 13:32:00 -0600100 face.expressInterest(*makeInterest("/Hello/World", true, 50_ms),
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800101 [&] (const Interest& i, const Data& d) {
102 ++nData;
103 },
104 bind([] { BOOST_FAIL("Unexpected Nack"); }),
105 bind([] { BOOST_FAIL("Unexpected timeout"); }));
106
Junxiao Shib55e5d32018-07-18 13:32:00 -0600107 face.expressInterest(*makeInterest("/Hello/World/a", true, 50_ms),
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800108 [&] (const Interest& i, const Data& d) {
109 ++nData;
110 },
111 bind([] { BOOST_FAIL("Unexpected Nack"); }),
112 bind([] { BOOST_FAIL("Unexpected timeout"); }));
113
Davide Pesavento0f830802018-01-16 23:58:58 -0500114 advanceClocks(40_ms);
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800115
116 face.receive(*makeData("/Hello/World/a/b"));
117
Davide Pesavento0f830802018-01-16 23:58:58 -0500118 advanceClocks(50_ms, 2);
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800119
120 BOOST_CHECK_EQUAL(nData, 2);
121 BOOST_CHECK_EQUAL(face.sentInterests.size(), 2);
122 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
123}
124
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000125BOOST_AUTO_TEST_CASE(ExpressInterestEmptyDataCallback)
126{
Junxiao Shib55e5d32018-07-18 13:32:00 -0600127 face.expressInterest(*makeInterest("/Hello/World", true),
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000128 nullptr,
129 bind([] { BOOST_FAIL("Unexpected Nack"); }),
130 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500131 advanceClocks(1_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000132
133 BOOST_CHECK_NO_THROW(do {
134 face.receive(*makeData("/Hello/World/a"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500135 advanceClocks(1_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000136 } while (false));
137}
138
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400139BOOST_AUTO_TEST_CASE(ExpressInterestTimeout)
140{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800141 size_t nTimeouts = 0;
Junxiao Shib55e5d32018-07-18 13:32:00 -0600142 face.expressInterest(*makeInterest("/Hello/World", false, 50_ms),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000143 bind([] { BOOST_FAIL("Unexpected Data"); }),
144 bind([] { BOOST_FAIL("Unexpected Nack"); }),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800145 [&nTimeouts] (const Interest& i) {
146 BOOST_CHECK_EQUAL(i.getName(), "/Hello/World");
147 ++nTimeouts;
148 });
Eric Newberry83872fd2015-08-06 17:01:24 -0700149
Davide Pesavento0f830802018-01-16 23:58:58 -0500150 advanceClocks(200_ms, 5);
Eric Newberry83872fd2015-08-06 17:01:24 -0700151
152 BOOST_CHECK_EQUAL(nTimeouts, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800153 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
154 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
155 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0);
Eric Newberry83872fd2015-08-06 17:01:24 -0700156}
157
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000158BOOST_AUTO_TEST_CASE(ExpressInterestEmptyTimeoutCallback)
159{
Junxiao Shib55e5d32018-07-18 13:32:00 -0600160 face.expressInterest(*makeInterest("/Hello/World", false, 50_ms),
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000161 bind([] { BOOST_FAIL("Unexpected Data"); }),
162 bind([] { BOOST_FAIL("Unexpected Nack"); }),
163 nullptr);
Davide Pesavento0f830802018-01-16 23:58:58 -0500164 advanceClocks(40_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000165
166 BOOST_CHECK_NO_THROW(do {
Davide Pesavento0f830802018-01-16 23:58:58 -0500167 advanceClocks(6_ms, 2);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000168 } while (false));
169}
170
Eric Newberry83872fd2015-08-06 17:01:24 -0700171BOOST_AUTO_TEST_CASE(ExpressInterestNack)
172{
173 size_t nNacks = 0;
174
Junxiao Shib55e5d32018-07-18 13:32:00 -0600175 auto interest = makeInterest("/Hello/World", false, 50_ms);
Eric Newberry83872fd2015-08-06 17:01:24 -0700176
Junxiao Shib55e5d32018-07-18 13:32:00 -0600177 face.expressInterest(*interest,
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000178 bind([] { BOOST_FAIL("Unexpected Data"); }),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800179 [&] (const Interest& i, const lp::Nack& n) {
180 BOOST_CHECK(i.getName().isPrefixOf(n.getInterest().getName()));
181 BOOST_CHECK_EQUAL(i.getName(), "/Hello/World");
182 BOOST_CHECK_EQUAL(n.getReason(), lp::NackReason::DUPLICATE);
183 ++nNacks;
184 },
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000185 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Eric Newberry83872fd2015-08-06 17:01:24 -0700186
Davide Pesavento0f830802018-01-16 23:58:58 -0500187 advanceClocks(40_ms);
Eric Newberry83872fd2015-08-06 17:01:24 -0700188
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000189 face.receive(makeNack(face.sentInterests.at(0), lp::NackReason::DUPLICATE));
Eric Newberry83872fd2015-08-06 17:01:24 -0700190
Davide Pesavento0f830802018-01-16 23:58:58 -0500191 advanceClocks(50_ms, 2);
Eric Newberry83872fd2015-08-06 17:01:24 -0700192
193 BOOST_CHECK_EQUAL(nNacks, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800194 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
Eric Newberry83872fd2015-08-06 17:01:24 -0700195}
196
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800197BOOST_AUTO_TEST_CASE(ExpressMultipleInterestNack)
198{
199 size_t nNacks = 0;
200
Junxiao Shib55e5d32018-07-18 13:32:00 -0600201 auto interest = makeInterest("/Hello/World", false, 50_ms, 1);
202 face.expressInterest(*interest,
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800203 bind([] { BOOST_FAIL("Unexpected Data"); }),
204 [&] (const Interest& i, const lp::Nack& n) {
205 ++nNacks;
206 },
207 bind([] { BOOST_FAIL("Unexpected timeout"); }));
208
Junxiao Shib55e5d32018-07-18 13:32:00 -0600209 interest->setNonce(2);
210 face.expressInterest(*interest,
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800211 bind([] { BOOST_FAIL("Unexpected Data"); }),
212 [&] (const Interest& i, const lp::Nack& n) {
213 ++nNacks;
214 },
215 bind([] { BOOST_FAIL("Unexpected timeout"); }));
216
Davide Pesavento0f830802018-01-16 23:58:58 -0500217 advanceClocks(40_ms);
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800218
219 face.receive(makeNack(face.sentInterests.at(1), lp::NackReason::DUPLICATE));
220
Davide Pesavento0f830802018-01-16 23:58:58 -0500221 advanceClocks(50_ms, 2);
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800222
223 BOOST_CHECK_EQUAL(nNacks, 2);
224 BOOST_CHECK_EQUAL(face.sentInterests.size(), 2);
225}
226
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000227BOOST_AUTO_TEST_CASE(ExpressInterestEmptyNackCallback)
228{
Junxiao Shib55e5d32018-07-18 13:32:00 -0600229 face.expressInterest(*makeInterest("/Hello/World"),
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000230 bind([] { BOOST_FAIL("Unexpected Data"); }),
231 nullptr,
232 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500233 advanceClocks(1_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000234
235 BOOST_CHECK_NO_THROW(do {
236 face.receive(makeNack(face.sentInterests.at(0), lp::NackReason::DUPLICATE));
Davide Pesavento0f830802018-01-16 23:58:58 -0500237 advanceClocks(1_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000238 } while (false));
239}
240
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800241BOOST_AUTO_TEST_CASE(RemovePendingInterest)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400242{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800243 const PendingInterestId* interestId =
Junxiao Shib55e5d32018-07-18 13:32:00 -0600244 face.expressInterest(*makeInterest("/Hello/World", true, 50_ms),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000245 bind([] { BOOST_FAIL("Unexpected data"); }),
246 bind([] { BOOST_FAIL("Unexpected nack"); }),
247 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500248 advanceClocks(10_ms);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400249
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800250 face.removePendingInterest(interestId);
Davide Pesavento0f830802018-01-16 23:58:58 -0500251 advanceClocks(10_ms);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400252
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000253 face.receive(*makeData("/Hello/World/%21"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500254 advanceClocks(200_ms, 5);
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100255
256 // avoid "test case [...] did not check any assertions" message from Boost.Test
257 BOOST_CHECK(true);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400258}
259
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000260BOOST_AUTO_TEST_CASE(RemoveAllPendingInterests)
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500261{
Junxiao Shib55e5d32018-07-18 13:32:00 -0600262 face.expressInterest(*makeInterest("/Hello/World/0", false, 50_ms),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800263 bind([] { BOOST_FAIL("Unexpected data"); }),
264 bind([] { BOOST_FAIL("Unexpected nack"); }),
265 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500266
Junxiao Shib55e5d32018-07-18 13:32:00 -0600267 face.expressInterest(*makeInterest("/Hello/World/1", false, 50_ms),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800268 bind([] { BOOST_FAIL("Unexpected data"); }),
269 bind([] { BOOST_FAIL("Unexpected nack"); }),
270 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500271
Davide Pesavento0f830802018-01-16 23:58:58 -0500272 advanceClocks(10_ms);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500273
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800274 face.removeAllPendingInterests();
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 BOOST_CHECK_EQUAL(face.getNPendingInterests(), 0);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500278
Junxiao Shi85d90832016-08-04 03:19:46 +0000279 face.receive(*makeData("/Hello/World/0"));
280 face.receive(*makeData("/Hello/World/1"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500281 advanceClocks(200_ms, 5);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500282}
283
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000284BOOST_AUTO_TEST_CASE(DestructionWithoutCancellingPendingInterests) // Bug #2518
285{
286 {
287 DummyClientFace face2(io, m_keyChain);
Junxiao Shib55e5d32018-07-18 13:32:00 -0600288 face2.expressInterest(*makeInterest("/Hello/World", false, 50_ms),
Alexander Afanasyeve6835fe2017-01-19 20:05:01 -0800289 nullptr, nullptr, nullptr);
Davide Pesavento0f830802018-01-16 23:58:58 -0500290 advanceClocks(50_ms, 2);
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000291 }
292
Davide Pesavento0f830802018-01-16 23:58:58 -0500293 advanceClocks(50_ms, 2); // should not crash
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100294
295 // avoid "test case [...] did not check any assertions" message from Boost.Test
296 BOOST_CHECK(true);
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000297}
298
Ashlesh Gawandee2404222018-05-21 18:30:24 -0500299BOOST_AUTO_TEST_CASE(DataCallbackPutData) // Bug 4596
300{
Junxiao Shib55e5d32018-07-18 13:32:00 -0600301 face.expressInterest(*makeInterest("/localhost/notification/1"),
Ashlesh Gawandee2404222018-05-21 18:30:24 -0500302 [&] (const Interest& i, const Data& d) {
303 face.put(*makeData("/chronosync/sampleDigest/1"));
304 }, nullptr, nullptr);
305 advanceClocks(10_ms);
306 BOOST_CHECK_EQUAL(face.sentInterests.back().getName(), "/localhost/notification/1");
307
Junxiao Shib55e5d32018-07-18 13:32:00 -0600308 face.receive(*makeInterest("/chronosync/sampleDigest", true));
Ashlesh Gawandee2404222018-05-21 18:30:24 -0500309 advanceClocks(10_ms);
310
311 face.put(*makeData("/localhost/notification/1"));
312 advanceClocks(10_ms);
313 BOOST_CHECK_EQUAL(face.sentData.back().getName(), "/chronosync/sampleDigest/1");
314}
315
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000316BOOST_AUTO_TEST_SUITE_END() // Consumer
317
318BOOST_AUTO_TEST_SUITE(Producer)
319
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000320BOOST_AUTO_TEST_CASE(PutData)
321{
322 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
323
324 Data data("/4g7xxcuEow/KFvK5Kf2m");
325 signData(data);
326 face.put(data);
327
328 lp::CachePolicy cachePolicy;
329 cachePolicy.setPolicy(lp::CachePolicyType::NO_CACHE);
330 data.setTag(make_shared<lp::CachePolicyTag>(cachePolicy));
Eric Newberry4d261b62016-11-10 13:40:09 -0700331 data.setTag(make_shared<lp::CongestionMarkTag>(1));
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000332 face.put(data);
333
Davide Pesavento0f830802018-01-16 23:58:58 -0500334 advanceClocks(10_ms);
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000335 BOOST_REQUIRE_EQUAL(face.sentData.size(), 2);
336 BOOST_CHECK(face.sentData[0].getTag<lp::CachePolicyTag>() == nullptr);
Eric Newberry4d261b62016-11-10 13:40:09 -0700337 BOOST_CHECK(face.sentData[0].getTag<lp::CongestionMarkTag>() == nullptr);
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000338 BOOST_CHECK(face.sentData[1].getTag<lp::CachePolicyTag>() != nullptr);
Eric Newberry4d261b62016-11-10 13:40:09 -0700339 BOOST_CHECK(face.sentData[1].getTag<lp::CongestionMarkTag>() != nullptr);
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000340}
341
Junxiao Shib6828912017-11-20 14:06:32 +0000342BOOST_AUTO_TEST_CASE(PutDataLoopback)
343{
344 bool hasInterest1 = false, hasData = false;
345
346 // first InterestFilter allows loopback and should receive Interest
347 face.setInterestFilter("/", [&] (const InterestFilter&, const Interest& interest) {
348 hasInterest1 = true;
349 // do not respond with Data right away, so Face must send Interest to forwarder
350 });
351 // second InterestFilter disallows loopback and should not receive Interest
352 face.setInterestFilter(InterestFilter("/").allowLoopback(false),
353 bind([] { BOOST_ERROR("Unexpected Interest on second InterestFilter"); }));
354
Junxiao Shib55e5d32018-07-18 13:32:00 -0600355 face.expressInterest(*makeInterest("/A", true),
Junxiao Shib6828912017-11-20 14:06:32 +0000356 bind([&] { hasData = true; }),
357 bind([] { BOOST_FAIL("Unexpected nack"); }),
358 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500359 advanceClocks(1_ms);
Junxiao Shib6828912017-11-20 14:06:32 +0000360 BOOST_CHECK_EQUAL(hasInterest1, true); // Interest looped back
361 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1); // Interest sent to forwarder
362 BOOST_CHECK_EQUAL(hasData, false); // waiting for Data
363
364 face.put(*makeData("/A/B")); // first InterestFilter responds with Data
Davide Pesavento0f830802018-01-16 23:58:58 -0500365 advanceClocks(1_ms);
Junxiao Shib6828912017-11-20 14:06:32 +0000366 BOOST_CHECK_EQUAL(hasData, true);
367 BOOST_CHECK_EQUAL(face.sentData.size(), 0); // do not spill Data to forwarder
368}
369
Junxiao Shi859888f2017-09-12 14:29:16 +0000370BOOST_AUTO_TEST_CASE(PutMultipleData)
371{
372 bool hasInterest1 = false;
373 // register two Interest destinations
374 face.setInterestFilter("/", bind([&] {
375 hasInterest1 = true;
376 // sending Data right away from the first destination, don't care whether Interest goes to second destination
377 face.put(*makeData("/A/B"));
378 }));
379 face.setInterestFilter("/", bind([]{}));
Davide Pesavento0f830802018-01-16 23:58:58 -0500380 advanceClocks(10_ms);
Junxiao Shi859888f2017-09-12 14:29:16 +0000381
Junxiao Shib55e5d32018-07-18 13:32:00 -0600382 face.receive(*makeInterest("/A", true));
Davide Pesavento0f830802018-01-16 23:58:58 -0500383 advanceClocks(10_ms);
Junxiao Shi859888f2017-09-12 14:29:16 +0000384 BOOST_CHECK(hasInterest1);
385 BOOST_CHECK_EQUAL(face.sentData.size(), 1);
386 BOOST_CHECK_EQUAL(face.sentData.at(0).getName(), "/A/B");
387
388 face.put(*makeData("/A/C"));
389 BOOST_CHECK_EQUAL(face.sentData.size(), 1); // additional Data are ignored
390}
391
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000392BOOST_AUTO_TEST_CASE(PutNack)
393{
Junxiao Shi79a7a162017-09-09 08:33:57 +0000394 face.setInterestFilter("/", bind([]{})); // register one Interest destination so that face can accept Nacks
Davide Pesavento0f830802018-01-16 23:58:58 -0500395 advanceClocks(10_ms);
Junxiao Shi79a7a162017-09-09 08:33:57 +0000396
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000397 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0);
398
Junxiao Shib55e5d32018-07-18 13:32:00 -0600399 face.put(makeNack(*makeInterest("/unsolicited", false, DEFAULT_INTEREST_LIFETIME, 18645250),
400 lp::NackReason::NO_ROUTE));
Davide Pesavento0f830802018-01-16 23:58:58 -0500401 advanceClocks(10_ms);
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000402 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0); // unsolicited Nack would not be sent
Eric Newberry4d261b62016-11-10 13:40:09 -0700403
Junxiao Shib55e5d32018-07-18 13:32:00 -0600404 auto interest1 = makeInterest("/Hello/World", false, DEFAULT_INTEREST_LIFETIME, 14247162);
405 face.receive(*interest1);
406 auto interest2 = makeInterest("/another/prefix", false, DEFAULT_INTEREST_LIFETIME, 92203002);
407 face.receive(*interest2);
Davide Pesavento0f830802018-01-16 23:58:58 -0500408 advanceClocks(10_ms);
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000409
Junxiao Shib55e5d32018-07-18 13:32:00 -0600410 face.put(makeNack(*interest1, 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
Junxiao Shib55e5d32018-07-18 13:32:00 -0600416 auto nack = makeNack(*interest2, 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
Junxiao Shib55e5d32018-07-18 13:32:00 -0600437 auto interest = makeInterest("/A", false, DEFAULT_INTEREST_LIFETIME, 14333271);
438 face.receive(*interest);
Davide Pesavento0f830802018-01-16 23:58:58 -0500439 advanceClocks(10_ms);
Junxiao Shi859888f2017-09-12 14:29:16 +0000440 BOOST_CHECK(hasInterest1);
441 BOOST_CHECK(hasInterest2);
Junxiao Shi79a7a162017-09-09 08:33:57 +0000442
Junxiao Shi859888f2017-09-12 14:29:16 +0000443 // Nack from first destination is received, should wait for a response from the other destination
444 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0);
Junxiao Shi79a7a162017-09-09 08:33:57 +0000445
Junxiao Shib55e5d32018-07-18 13:32:00 -0600446 face.put(makeNack(*interest, lp::NackReason::NO_ROUTE)); // Nack from second destination
Davide Pesavento0f830802018-01-16 23:58:58 -0500447 advanceClocks(10_ms);
Junxiao Shi859888f2017-09-12 14:29:16 +0000448 BOOST_CHECK_EQUAL(face.sentNacks.size(), 1); // sending Nack after both destinations Nacked
Junxiao Shi79a7a162017-09-09 08:33:57 +0000449 BOOST_CHECK_EQUAL(face.sentNacks.at(0).getReason(), lp::NackReason::CONGESTION); // least severe reason
450
Junxiao Shib55e5d32018-07-18 13:32:00 -0600451 face.put(makeNack(*interest, lp::NackReason::DUPLICATE));
Junxiao Shi79a7a162017-09-09 08:33:57 +0000452 BOOST_CHECK_EQUAL(face.sentNacks.size(), 1); // additional Nacks are ignored
453}
454
Junxiao Shib6828912017-11-20 14:06:32 +0000455BOOST_AUTO_TEST_CASE(PutMultipleNackLoopback)
456{
457 bool hasInterest1 = false, hasNack = false;
458
459 // first InterestFilter allows loopback and should receive Interest
460 face.setInterestFilter("/", [&] (const InterestFilter&, const Interest& interest) {
461 hasInterest1 = true;
462 face.put(makeNack(interest, lp::NackReason::CONGESTION));
463 });
464 // second InterestFilter disallows loopback and should not receive Interest
465 face.setInterestFilter(InterestFilter("/").allowLoopback(false),
466 bind([] { BOOST_ERROR("Unexpected Interest on second InterestFilter"); }));
467
Junxiao Shib55e5d32018-07-18 13:32:00 -0600468 auto interest = makeInterest("/A", false, DEFAULT_INTEREST_LIFETIME, 28395852);
469 face.expressInterest(*interest,
Junxiao Shib6828912017-11-20 14:06:32 +0000470 bind([] { BOOST_FAIL("Unexpected data"); }),
471 [&] (const Interest&, const lp::Nack& nack) {
472 hasNack = true;
473 BOOST_CHECK_EQUAL(nack.getReason(), lp::NackReason::CONGESTION);
474 },
475 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500476 advanceClocks(1_ms);
Junxiao Shib6828912017-11-20 14:06:32 +0000477 BOOST_CHECK_EQUAL(hasInterest1, true); // Interest looped back
478 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1); // Interest sent to forwarder
479 BOOST_CHECK_EQUAL(hasNack, false); // waiting for Nack from forwarder
480
Junxiao Shib55e5d32018-07-18 13:32:00 -0600481 face.receive(makeNack(*interest, lp::NackReason::NO_ROUTE));
Davide Pesavento0f830802018-01-16 23:58:58 -0500482 advanceClocks(1_ms);
Junxiao Shib6828912017-11-20 14:06:32 +0000483 BOOST_CHECK_EQUAL(hasNack, true);
484}
485
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400486BOOST_AUTO_TEST_CASE(SetUnsetInterestFilter)
487{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800488 size_t nInterests = 0;
489 size_t nRegs = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400490 const RegisteredPrefixId* regPrefixId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800491 face.setInterestFilter("/Hello/World",
492 bind([&nInterests] { ++nInterests; }),
493 bind([&nRegs] { ++nRegs; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000494 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500495 advanceClocks(25_ms, 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800496 BOOST_CHECK_EQUAL(nRegs, 1);
497 BOOST_CHECK_EQUAL(nInterests, 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400498
Junxiao Shib55e5d32018-07-18 13:32:00 -0600499 face.receive(*makeInterest("/Hello/World/%21"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500500 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400501
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800502 BOOST_CHECK_EQUAL(nRegs, 1);
503 BOOST_CHECK_EQUAL(nInterests, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400504
Junxiao Shib55e5d32018-07-18 13:32:00 -0600505 face.receive(*makeInterest("/Bye/World/%21"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500506 advanceClocks(10000_ms, 10);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800507 BOOST_CHECK_EQUAL(nInterests, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400508
Junxiao Shib55e5d32018-07-18 13:32:00 -0600509 face.receive(*makeInterest("/Hello/World/%21/2"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500510 advanceClocks(25_ms, 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800511 BOOST_CHECK_EQUAL(nInterests, 2);
512
513 // removing filter
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800514 face.unsetInterestFilter(regPrefixId);
Davide Pesavento0f830802018-01-16 23:58:58 -0500515 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400516
Junxiao Shib55e5d32018-07-18 13:32:00 -0600517 face.receive(*makeInterest("/Hello/World/%21/3"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800518 BOOST_CHECK_EQUAL(nInterests, 2);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400519
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000520 face.unsetInterestFilter(static_cast<const RegisteredPrefixId*>(nullptr));
Davide Pesavento0f830802018-01-16 23:58:58 -0500521 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400522
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000523 face.unsetInterestFilter(static_cast<const InterestFilterId*>(nullptr));
Davide Pesavento0f830802018-01-16 23:58:58 -0500524 advanceClocks(25_ms, 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800525}
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400526
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000527BOOST_AUTO_TEST_CASE(SetInterestFilterEmptyInterestCallback)
528{
529 face.setInterestFilter("/A", nullptr);
Davide Pesavento0f830802018-01-16 23:58:58 -0500530 advanceClocks(1_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000531
532 BOOST_CHECK_NO_THROW(do {
533 face.receive(*makeInterest("/A/1"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500534 advanceClocks(1_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000535 } while (false));
536}
537
Joao Pereira0b3cac52015-07-02 14:49:49 -0400538BOOST_AUTO_TEST_CASE(SetUnsetInterestFilterWithoutSucessCallback)
539{
540 size_t nInterests = 0;
541 const RegisteredPrefixId* regPrefixId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800542 face.setInterestFilter("/Hello/World",
543 bind([&nInterests] { ++nInterests; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000544 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500545 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400546 BOOST_CHECK_EQUAL(nInterests, 0);
547
Junxiao Shib55e5d32018-07-18 13:32:00 -0600548 face.receive(*makeInterest("/Hello/World/%21"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500549 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400550
551 BOOST_CHECK_EQUAL(nInterests, 1);
552
Junxiao Shib55e5d32018-07-18 13:32:00 -0600553 face.receive(*makeInterest("/Bye/World/%21"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500554 advanceClocks(10000_ms, 10);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400555 BOOST_CHECK_EQUAL(nInterests, 1);
556
Junxiao Shib55e5d32018-07-18 13:32:00 -0600557 face.receive(*makeInterest("/Hello/World/%21/2"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500558 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400559 BOOST_CHECK_EQUAL(nInterests, 2);
560
561 // removing filter
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800562 face.unsetInterestFilter(regPrefixId);
Davide Pesavento0f830802018-01-16 23:58:58 -0500563 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400564
Junxiao Shib55e5d32018-07-18 13:32:00 -0600565 face.receive(*makeInterest("/Hello/World/%21/3"));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400566 BOOST_CHECK_EQUAL(nInterests, 2);
567
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000568 face.unsetInterestFilter(static_cast<const RegisteredPrefixId*>(nullptr));
Davide Pesavento0f830802018-01-16 23:58:58 -0500569 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400570
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000571 face.unsetInterestFilter(static_cast<const InterestFilterId*>(nullptr));
Davide Pesavento0f830802018-01-16 23:58:58 -0500572 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400573}
574
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800575BOOST_FIXTURE_TEST_CASE(SetInterestFilterFail, FacesNoRegistrationReplyFixture)
576{
577 // don't enable registration reply
578 size_t nRegFailed = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800579 face.setInterestFilter("/Hello/World",
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000580 bind([] { BOOST_FAIL("Unexpected Interest"); }),
581 bind([] { BOOST_FAIL("Unexpected success of setInterestFilter"); }),
582 bind([&nRegFailed] { ++nRegFailed; }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800583
Davide Pesavento0f830802018-01-16 23:58:58 -0500584 advanceClocks(25_ms, 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800585 BOOST_CHECK_EQUAL(nRegFailed, 0);
586
Davide Pesavento0f830802018-01-16 23:58:58 -0500587 advanceClocks(2000_ms, 5);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800588 BOOST_CHECK_EQUAL(nRegFailed, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400589}
590
Joao Pereira0b3cac52015-07-02 14:49:49 -0400591BOOST_FIXTURE_TEST_CASE(SetInterestFilterFailWithoutSuccessCallback, FacesNoRegistrationReplyFixture)
592{
593 // don't enable registration reply
594 size_t nRegFailed = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800595 face.setInterestFilter("/Hello/World",
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000596 bind([] { BOOST_FAIL("Unexpected Interest"); }),
597 bind([&nRegFailed] { ++nRegFailed; }));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400598
Davide Pesavento0f830802018-01-16 23:58:58 -0500599 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400600 BOOST_CHECK_EQUAL(nRegFailed, 0);
601
Davide Pesavento0f830802018-01-16 23:58:58 -0500602 advanceClocks(2000_ms, 5);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400603 BOOST_CHECK_EQUAL(nRegFailed, 1);
604}
605
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400606BOOST_AUTO_TEST_CASE(RegisterUnregisterPrefix)
607{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800608 size_t nRegSuccesses = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400609 const RegisteredPrefixId* regPrefixId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800610 face.registerPrefix("/Hello/World",
611 bind([&nRegSuccesses] { ++nRegSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000612 bind([] { BOOST_FAIL("Unexpected registerPrefix failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400613
Davide Pesavento0f830802018-01-16 23:58:58 -0500614 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400615 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
616
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800617 size_t nUnregSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800618 face.unregisterPrefix(regPrefixId,
619 bind([&nUnregSuccesses] { ++nUnregSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000620 bind([] { BOOST_FAIL("Unexpected unregisterPrefix failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400621
Davide Pesavento0f830802018-01-16 23:58:58 -0500622 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400623 BOOST_CHECK_EQUAL(nUnregSuccesses, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400624}
625
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800626BOOST_FIXTURE_TEST_CASE(RegisterUnregisterPrefixFail, FacesNoRegistrationReplyFixture)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400627{
Junxiao Shib6828912017-11-20 14:06:32 +0000628 // don't enable registration reply
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800629 size_t nRegFailures = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800630 face.registerPrefix("/Hello/World",
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000631 bind([] { BOOST_FAIL("Unexpected registerPrefix success"); }),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800632 bind([&nRegFailures] { ++nRegFailures; }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400633
Davide Pesavento0f830802018-01-16 23:58:58 -0500634 advanceClocks(5000_ms, 20);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800635 BOOST_CHECK_EQUAL(nRegFailures, 1);
636}
637
638BOOST_AUTO_TEST_CASE(SimilarFilters)
639{
640 size_t nInInterests1 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800641 face.setInterestFilter("/Hello/World",
642 bind([&nInInterests1] { ++nInInterests1; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000643 nullptr,
644 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800645
646 size_t nInInterests2 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800647 face.setInterestFilter("/Hello",
648 bind([&nInInterests2] { ++nInInterests2; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000649 nullptr,
650 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400651
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800652 size_t nInInterests3 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800653 face.setInterestFilter("/Los/Angeles/Lakers",
654 bind([&nInInterests3] { ++nInInterests3; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000655 nullptr,
656 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400657
Davide Pesavento0f830802018-01-16 23:58:58 -0500658 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400659
Junxiao Shib55e5d32018-07-18 13:32:00 -0600660 face.receive(*makeInterest("/Hello/World/%21"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500661 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400662
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800663 BOOST_CHECK_EQUAL(nInInterests1, 1);
664 BOOST_CHECK_EQUAL(nInInterests2, 1);
665 BOOST_CHECK_EQUAL(nInInterests3, 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400666}
667
668BOOST_AUTO_TEST_CASE(SetRegexFilterError)
669{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800670 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
671 [] (const Name&, const Interest&) {
672 BOOST_FAIL("InterestFilter::Error should have been triggered");
673 },
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000674 nullptr,
675 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400676
Davide Pesavento0f830802018-01-16 23:58:58 -0500677 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400678
Junxiao Shib55e5d32018-07-18 13:32:00 -0600679 BOOST_REQUIRE_THROW(face.receive(*makeInterest("/Hello/World/XXX/b/c")), InterestFilter::Error);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400680}
681
682BOOST_AUTO_TEST_CASE(SetRegexFilter)
683{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800684 size_t nInInterests = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800685 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
686 bind([&nInInterests] { ++nInInterests; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000687 nullptr,
688 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400689
Davide Pesavento0f830802018-01-16 23:58:58 -0500690 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400691
Junxiao Shib55e5d32018-07-18 13:32:00 -0600692 face.receive(*makeInterest("/Hello/World/a")); // shouldn't match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400693 BOOST_CHECK_EQUAL(nInInterests, 0);
694
Junxiao Shib55e5d32018-07-18 13:32:00 -0600695 face.receive(*makeInterest("/Hello/World/a/b")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400696 BOOST_CHECK_EQUAL(nInInterests, 1);
697
Junxiao Shib55e5d32018-07-18 13:32:00 -0600698 face.receive(*makeInterest("/Hello/World/a/b/c")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400699 BOOST_CHECK_EQUAL(nInInterests, 2);
700
Junxiao Shib55e5d32018-07-18 13:32:00 -0600701 face.receive(*makeInterest("/Hello/World/a/b/d")); // should not match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400702 BOOST_CHECK_EQUAL(nInInterests, 2);
703}
704
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400705BOOST_AUTO_TEST_CASE(SetRegexFilterAndRegister)
706{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800707 size_t nInInterests = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800708 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
709 bind([&nInInterests] { ++nInInterests; }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400710
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800711 size_t nRegSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800712 face.registerPrefix("/Hello/World",
713 bind([&nRegSuccesses] { ++nRegSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000714 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400715
Davide Pesavento0f830802018-01-16 23:58:58 -0500716 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400717 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
718
Junxiao Shib55e5d32018-07-18 13:32:00 -0600719 face.receive(*makeInterest("/Hello/World/a")); // shouldn't match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400720 BOOST_CHECK_EQUAL(nInInterests, 0);
721
Junxiao Shib55e5d32018-07-18 13:32:00 -0600722 face.receive(*makeInterest("/Hello/World/a/b")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400723 BOOST_CHECK_EQUAL(nInInterests, 1);
724
Junxiao Shib55e5d32018-07-18 13:32:00 -0600725 face.receive(*makeInterest("/Hello/World/a/b/c")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400726 BOOST_CHECK_EQUAL(nInInterests, 2);
727
Junxiao Shib55e5d32018-07-18 13:32:00 -0600728 face.receive(*makeInterest("/Hello/World/a/b/d")); // should not match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400729 BOOST_CHECK_EQUAL(nInInterests, 2);
730}
731
Junxiao Shia1ea5062014-12-27 22:33:39 -0700732BOOST_FIXTURE_TEST_CASE(SetInterestFilterNoReg, FacesNoRegistrationReplyFixture) // Bug 2318
733{
734 // This behavior is specific to DummyClientFace.
735 // Regular Face won't accept incoming packets until something is sent.
736
737 int hit = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800738 face.setInterestFilter(Name("/"), bind([&hit] { ++hit; }));
739 face.processEvents(time::milliseconds(-1));
Junxiao Shia1ea5062014-12-27 22:33:39 -0700740
Junxiao Shib55e5d32018-07-18 13:32:00 -0600741 face.receive(*makeInterest("/A"));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800742 face.processEvents(time::milliseconds(-1));
Junxiao Shia1ea5062014-12-27 22:33:39 -0700743
744 BOOST_CHECK_EQUAL(hit, 1);
745}
746
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000747BOOST_AUTO_TEST_SUITE_END() // Producer
748
Junxiao Shiae0b4182016-08-08 22:53:17 +0000749BOOST_AUTO_TEST_SUITE(IoRoutines)
750
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500751BOOST_AUTO_TEST_CASE(ProcessEvents)
752{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800753 face.processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500754
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800755 size_t nRegSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800756 face.registerPrefix("/Hello/World",
757 bind([&nRegSuccesses] { ++nRegSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000758 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500759
760 // io_service::poll() without reset
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800761 face.getIoService().poll();
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500762 BOOST_CHECK_EQUAL(nRegSuccesses, 0);
763
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800764 face.processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500765 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
766}
767
Junxiao Shiae0b4182016-08-08 22:53:17 +0000768BOOST_AUTO_TEST_CASE(DestroyWithoutProcessEvents) // Bug 3248
769{
770 auto face2 = make_unique<Face>(io);
771 face2.reset();
772
773 io.poll(); // should not crash
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100774
775 // avoid "test case [...] did not check any assertions" message from Boost.Test
776 BOOST_CHECK(true);
Junxiao Shiae0b4182016-08-08 22:53:17 +0000777}
778
779BOOST_AUTO_TEST_SUITE_END() // IoRoutines
780
781BOOST_AUTO_TEST_SUITE(Transport)
782
783using ndn::Transport;
784
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700785struct PibDirWithDefaultTpm
786{
787 const std::string PATH = "build/keys-with-default-tpm";
788};
789
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800790BOOST_FIXTURE_TEST_CASE(FaceTransport, IdentityManagementTimeFixture)
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800791{
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800792 BOOST_CHECK(Face().getTransport() != nullptr);
793
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800794 BOOST_CHECK(Face(shared_ptr<Transport>()).getTransport() != nullptr);
795 BOOST_CHECK(Face(shared_ptr<Transport>(), io).getTransport() != nullptr);
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800796 BOOST_CHECK(Face(shared_ptr<Transport>(), io, m_keyChain).getTransport() != nullptr);
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800797
798 auto transport = make_shared<TcpTransport>("localhost", "6363"); // no real io operations will be scheduled
799 BOOST_CHECK(Face(transport).getTransport() == transport);
800 BOOST_CHECK(Face(transport, io).getTransport() == transport);
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800801 BOOST_CHECK(Face(transport, io, m_keyChain).getTransport() == transport);
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800802}
803
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700804class WithEnv : private IdentityManagementTimeFixture
805{
806public:
807 WithEnv()
808 {
809 if (getenv("NDN_CLIENT_TRANSPORT") != nullptr) {
810 m_oldTransport = getenv("NDN_CLIENT_TRANSPORT");
811 unsetenv("NDN_CLIENT_TRANSPORT");
812 }
813 }
814
815 void
816 configure(const std::string& faceUri)
817 {
818 setenv("NDN_CLIENT_TRANSPORT", faceUri.c_str(), true);
819 }
820
821 ~WithEnv()
822 {
823 if (!m_oldTransport.empty()) {
824 setenv("NDN_CLIENT_TRANSPORT", m_oldTransport.c_str(), true);
825 }
826 else {
827 unsetenv("NDN_CLIENT_TRANSPORT");
828 }
829 }
830
831private:
832 std::string m_oldTransport;
833};
834
835class WithConfig : private TestHomeFixture<DefaultPibDir>
836{
837public:
838 void
839 configure(const std::string& faceUri)
840 {
841 createClientConf({"transport=" + faceUri});
842 }
843};
844
845class WithEnvAndConfig : public WithEnv, public WithConfig
846{
847};
848
849typedef boost::mpl::vector<WithEnv, WithConfig> ConfigOptions;
850
851BOOST_FIXTURE_TEST_CASE(NoConfig, WithEnvAndConfig) // fixture configures test HOME and PIB/TPM path
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700852{
853 shared_ptr<Face> face;
854 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
855 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700856}
857
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700858BOOST_FIXTURE_TEST_CASE_TEMPLATE(Unix, T, ConfigOptions, T)
859{
860 this->configure("unix://some/path");
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700861
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700862 shared_ptr<Face> face;
863 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
864 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
865}
866
867BOOST_FIXTURE_TEST_CASE_TEMPLATE(Tcp, T, ConfigOptions, T)
868{
869 this->configure("tcp://127.0.0.1:6000");
870
871 shared_ptr<Face> face;
872 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
873 BOOST_CHECK(dynamic_pointer_cast<TcpTransport>(face->getTransport()) != nullptr);
874}
875
876BOOST_FIXTURE_TEST_CASE_TEMPLATE(WrongTransport, T, ConfigOptions, T)
877{
878 this->configure("wrong-transport:");
879
880 BOOST_CHECK_THROW(make_shared<Face>(), ConfigFile::Error);
881}
882
883BOOST_FIXTURE_TEST_CASE_TEMPLATE(WrongUri, T, ConfigOptions, T)
884{
885 this->configure("wrong-uri");
886
887 BOOST_CHECK_THROW(make_shared<Face>(), ConfigFile::Error);
888}
889
890BOOST_FIXTURE_TEST_CASE(EnvOverride, WithEnvAndConfig)
891{
892 this->WithEnv::configure("tcp://127.0.0.1:6000");
893 this->WithConfig::configure("unix://some/path");
894
895 shared_ptr<Face> face;
896 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
897 BOOST_CHECK(dynamic_pointer_cast<TcpTransport>(face->getTransport()) != nullptr);
898}
899
900BOOST_FIXTURE_TEST_CASE(ExplicitTransport, WithEnvAndConfig)
901{
902 this->WithEnv::configure("wrong-uri");
903 this->WithConfig::configure("wrong-transport:");
904
905 auto transport = make_shared<UnixTransport>("unix://some/path");
906 shared_ptr<Face> face;
907 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>(transport));
908 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
909}
910
Junxiao Shiae0b4182016-08-08 22:53:17 +0000911BOOST_AUTO_TEST_SUITE_END() // Transport
912
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700913BOOST_AUTO_TEST_SUITE_END() // TestFace
914
915} // namespace tests
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400916} // namespace ndn