blob: 3f2b82a68fc589d9134e4e0fc8499dd72d730118 [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"
Junxiao Shib55e5d32018-07-18 13:32:00 -060030#include "make-interest-data.hpp"
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070031#include "identity-management-time-fixture.hpp"
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080032#include "test-home-fixture.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;
Junxiao Shib55e5d32018-07-18 13:32:00 -060068 face.expressInterest(*makeInterest("/Hello/World", true, 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;
Junxiao Shib55e5d32018-07-18 13:32:00 -060089 face.expressInterest(*makeInterest("/Hello/World/a/2", false, 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
Junxiao Shib55e5d32018-07-18 13:32:00 -0600101 face.expressInterest(*makeInterest("/Hello/World", true, 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
Junxiao Shib55e5d32018-07-18 13:32:00 -0600108 face.expressInterest(*makeInterest("/Hello/World/a", true, 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{
Junxiao Shib55e5d32018-07-18 13:32:00 -0600128 face.expressInterest(*makeInterest("/Hello/World", true),
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000129 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;
Junxiao Shib55e5d32018-07-18 13:32:00 -0600143 face.expressInterest(*makeInterest("/Hello/World", false, 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{
Junxiao Shib55e5d32018-07-18 13:32:00 -0600161 face.expressInterest(*makeInterest("/Hello/World", false, 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
Junxiao Shib55e5d32018-07-18 13:32:00 -0600176 auto interest = makeInterest("/Hello/World", false, 50_ms);
Eric Newberry83872fd2015-08-06 17:01:24 -0700177
Junxiao Shib55e5d32018-07-18 13:32:00 -0600178 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
Junxiao Shib55e5d32018-07-18 13:32:00 -0600202 auto interest = makeInterest("/Hello/World", false, 50_ms, 1);
203 face.expressInterest(*interest,
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800204 bind([] { BOOST_FAIL("Unexpected Data"); }),
205 [&] (const Interest& i, const lp::Nack& n) {
206 ++nNacks;
207 },
208 bind([] { BOOST_FAIL("Unexpected timeout"); }));
209
Junxiao Shib55e5d32018-07-18 13:32:00 -0600210 interest->setNonce(2);
211 face.expressInterest(*interest,
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800212 bind([] { BOOST_FAIL("Unexpected Data"); }),
213 [&] (const Interest& i, const lp::Nack& n) {
214 ++nNacks;
215 },
216 bind([] { BOOST_FAIL("Unexpected timeout"); }));
217
Davide Pesavento0f830802018-01-16 23:58:58 -0500218 advanceClocks(40_ms);
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800219
220 face.receive(makeNack(face.sentInterests.at(1), lp::NackReason::DUPLICATE));
221
Davide Pesavento0f830802018-01-16 23:58:58 -0500222 advanceClocks(50_ms, 2);
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800223
224 BOOST_CHECK_EQUAL(nNacks, 2);
225 BOOST_CHECK_EQUAL(face.sentInterests.size(), 2);
226}
227
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000228BOOST_AUTO_TEST_CASE(ExpressInterestEmptyNackCallback)
229{
Junxiao Shib55e5d32018-07-18 13:32:00 -0600230 face.expressInterest(*makeInterest("/Hello/World"),
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000231 bind([] { BOOST_FAIL("Unexpected Data"); }),
232 nullptr,
233 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500234 advanceClocks(1_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000235
236 BOOST_CHECK_NO_THROW(do {
237 face.receive(makeNack(face.sentInterests.at(0), lp::NackReason::DUPLICATE));
Davide Pesavento0f830802018-01-16 23:58:58 -0500238 advanceClocks(1_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000239 } while (false));
240}
241
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800242BOOST_AUTO_TEST_CASE(RemovePendingInterest)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400243{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800244 const PendingInterestId* interestId =
Junxiao Shib55e5d32018-07-18 13:32:00 -0600245 face.expressInterest(*makeInterest("/Hello/World", true, 50_ms),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000246 bind([] { BOOST_FAIL("Unexpected data"); }),
247 bind([] { BOOST_FAIL("Unexpected nack"); }),
248 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500249 advanceClocks(10_ms);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400250
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800251 face.removePendingInterest(interestId);
Davide Pesavento0f830802018-01-16 23:58:58 -0500252 advanceClocks(10_ms);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400253
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000254 face.receive(*makeData("/Hello/World/%21"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500255 advanceClocks(200_ms, 5);
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100256
257 // avoid "test case [...] did not check any assertions" message from Boost.Test
258 BOOST_CHECK(true);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400259}
260
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000261BOOST_AUTO_TEST_CASE(RemoveAllPendingInterests)
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500262{
Junxiao Shib55e5d32018-07-18 13:32:00 -0600263 face.expressInterest(*makeInterest("/Hello/World/0", false, 50_ms),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800264 bind([] { BOOST_FAIL("Unexpected data"); }),
265 bind([] { BOOST_FAIL("Unexpected nack"); }),
266 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500267
Junxiao Shib55e5d32018-07-18 13:32:00 -0600268 face.expressInterest(*makeInterest("/Hello/World/1", false, 50_ms),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800269 bind([] { BOOST_FAIL("Unexpected data"); }),
270 bind([] { BOOST_FAIL("Unexpected nack"); }),
271 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500272
Davide Pesavento0f830802018-01-16 23:58:58 -0500273 advanceClocks(10_ms);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500274
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800275 face.removeAllPendingInterests();
Davide Pesavento0f830802018-01-16 23:58:58 -0500276 advanceClocks(10_ms);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500277
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800278 BOOST_CHECK_EQUAL(face.getNPendingInterests(), 0);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500279
Junxiao Shi85d90832016-08-04 03:19:46 +0000280 face.receive(*makeData("/Hello/World/0"));
281 face.receive(*makeData("/Hello/World/1"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500282 advanceClocks(200_ms, 5);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500283}
284
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000285BOOST_AUTO_TEST_CASE(DestructionWithoutCancellingPendingInterests) // Bug #2518
286{
287 {
288 DummyClientFace face2(io, m_keyChain);
Junxiao Shib55e5d32018-07-18 13:32:00 -0600289 face2.expressInterest(*makeInterest("/Hello/World", false, 50_ms),
Alexander Afanasyeve6835fe2017-01-19 20:05:01 -0800290 nullptr, nullptr, nullptr);
Davide Pesavento0f830802018-01-16 23:58:58 -0500291 advanceClocks(50_ms, 2);
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000292 }
293
Davide Pesavento0f830802018-01-16 23:58:58 -0500294 advanceClocks(50_ms, 2); // should not crash
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100295
296 // avoid "test case [...] did not check any assertions" message from Boost.Test
297 BOOST_CHECK(true);
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000298}
299
Ashlesh Gawandee2404222018-05-21 18:30:24 -0500300BOOST_AUTO_TEST_CASE(DataCallbackPutData) // Bug 4596
301{
Junxiao Shib55e5d32018-07-18 13:32:00 -0600302 face.expressInterest(*makeInterest("/localhost/notification/1"),
Ashlesh Gawandee2404222018-05-21 18:30:24 -0500303 [&] (const Interest& i, const Data& d) {
304 face.put(*makeData("/chronosync/sampleDigest/1"));
305 }, nullptr, nullptr);
306 advanceClocks(10_ms);
307 BOOST_CHECK_EQUAL(face.sentInterests.back().getName(), "/localhost/notification/1");
308
Junxiao Shib55e5d32018-07-18 13:32:00 -0600309 face.receive(*makeInterest("/chronosync/sampleDigest", true));
Ashlesh Gawandee2404222018-05-21 18:30:24 -0500310 advanceClocks(10_ms);
311
312 face.put(*makeData("/localhost/notification/1"));
313 advanceClocks(10_ms);
314 BOOST_CHECK_EQUAL(face.sentData.back().getName(), "/chronosync/sampleDigest/1");
315}
316
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000317BOOST_AUTO_TEST_SUITE_END() // Consumer
318
319BOOST_AUTO_TEST_SUITE(Producer)
320
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000321BOOST_AUTO_TEST_CASE(PutData)
322{
323 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
324
325 Data data("/4g7xxcuEow/KFvK5Kf2m");
326 signData(data);
327 face.put(data);
328
329 lp::CachePolicy cachePolicy;
330 cachePolicy.setPolicy(lp::CachePolicyType::NO_CACHE);
331 data.setTag(make_shared<lp::CachePolicyTag>(cachePolicy));
Eric Newberry4d261b62016-11-10 13:40:09 -0700332 data.setTag(make_shared<lp::CongestionMarkTag>(1));
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000333 face.put(data);
334
Davide Pesavento0f830802018-01-16 23:58:58 -0500335 advanceClocks(10_ms);
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000336 BOOST_REQUIRE_EQUAL(face.sentData.size(), 2);
337 BOOST_CHECK(face.sentData[0].getTag<lp::CachePolicyTag>() == nullptr);
Eric Newberry4d261b62016-11-10 13:40:09 -0700338 BOOST_CHECK(face.sentData[0].getTag<lp::CongestionMarkTag>() == nullptr);
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000339 BOOST_CHECK(face.sentData[1].getTag<lp::CachePolicyTag>() != nullptr);
Eric Newberry4d261b62016-11-10 13:40:09 -0700340 BOOST_CHECK(face.sentData[1].getTag<lp::CongestionMarkTag>() != nullptr);
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000341}
342
Junxiao Shib6828912017-11-20 14:06:32 +0000343BOOST_AUTO_TEST_CASE(PutDataLoopback)
344{
345 bool hasInterest1 = false, hasData = false;
346
347 // first InterestFilter allows loopback and should receive Interest
348 face.setInterestFilter("/", [&] (const InterestFilter&, const Interest& interest) {
349 hasInterest1 = true;
350 // do not respond with Data right away, so Face must send Interest to forwarder
351 });
352 // second InterestFilter disallows loopback and should not receive Interest
353 face.setInterestFilter(InterestFilter("/").allowLoopback(false),
354 bind([] { BOOST_ERROR("Unexpected Interest on second InterestFilter"); }));
355
Junxiao Shib55e5d32018-07-18 13:32:00 -0600356 face.expressInterest(*makeInterest("/A", true),
Junxiao Shib6828912017-11-20 14:06:32 +0000357 bind([&] { hasData = true; }),
358 bind([] { BOOST_FAIL("Unexpected nack"); }),
359 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500360 advanceClocks(1_ms);
Junxiao Shib6828912017-11-20 14:06:32 +0000361 BOOST_CHECK_EQUAL(hasInterest1, true); // Interest looped back
362 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1); // Interest sent to forwarder
363 BOOST_CHECK_EQUAL(hasData, false); // waiting for Data
364
365 face.put(*makeData("/A/B")); // first InterestFilter responds with Data
Davide Pesavento0f830802018-01-16 23:58:58 -0500366 advanceClocks(1_ms);
Junxiao Shib6828912017-11-20 14:06:32 +0000367 BOOST_CHECK_EQUAL(hasData, true);
368 BOOST_CHECK_EQUAL(face.sentData.size(), 0); // do not spill Data to forwarder
369}
370
Junxiao Shi859888f2017-09-12 14:29:16 +0000371BOOST_AUTO_TEST_CASE(PutMultipleData)
372{
373 bool hasInterest1 = false;
374 // register two Interest destinations
375 face.setInterestFilter("/", bind([&] {
376 hasInterest1 = true;
377 // sending Data right away from the first destination, don't care whether Interest goes to second destination
378 face.put(*makeData("/A/B"));
379 }));
380 face.setInterestFilter("/", bind([]{}));
Davide Pesavento0f830802018-01-16 23:58:58 -0500381 advanceClocks(10_ms);
Junxiao Shi859888f2017-09-12 14:29:16 +0000382
Junxiao Shib55e5d32018-07-18 13:32:00 -0600383 face.receive(*makeInterest("/A", true));
Davide Pesavento0f830802018-01-16 23:58:58 -0500384 advanceClocks(10_ms);
Junxiao Shi859888f2017-09-12 14:29:16 +0000385 BOOST_CHECK(hasInterest1);
386 BOOST_CHECK_EQUAL(face.sentData.size(), 1);
387 BOOST_CHECK_EQUAL(face.sentData.at(0).getName(), "/A/B");
388
389 face.put(*makeData("/A/C"));
390 BOOST_CHECK_EQUAL(face.sentData.size(), 1); // additional Data are ignored
391}
392
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000393BOOST_AUTO_TEST_CASE(PutNack)
394{
Junxiao Shi79a7a162017-09-09 08:33:57 +0000395 face.setInterestFilter("/", bind([]{})); // register one Interest destination so that face can accept Nacks
Davide Pesavento0f830802018-01-16 23:58:58 -0500396 advanceClocks(10_ms);
Junxiao Shi79a7a162017-09-09 08:33:57 +0000397
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000398 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0);
399
Junxiao Shib55e5d32018-07-18 13:32:00 -0600400 face.put(makeNack(*makeInterest("/unsolicited", false, DEFAULT_INTEREST_LIFETIME, 18645250),
401 lp::NackReason::NO_ROUTE));
Davide Pesavento0f830802018-01-16 23:58:58 -0500402 advanceClocks(10_ms);
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000403 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0); // unsolicited Nack would not be sent
Eric Newberry4d261b62016-11-10 13:40:09 -0700404
Junxiao Shib55e5d32018-07-18 13:32:00 -0600405 auto interest1 = makeInterest("/Hello/World", false, DEFAULT_INTEREST_LIFETIME, 14247162);
406 face.receive(*interest1);
407 auto interest2 = makeInterest("/another/prefix", false, DEFAULT_INTEREST_LIFETIME, 92203002);
408 face.receive(*interest2);
Davide Pesavento0f830802018-01-16 23:58:58 -0500409 advanceClocks(10_ms);
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000410
Junxiao Shib55e5d32018-07-18 13:32:00 -0600411 face.put(makeNack(*interest1, lp::NackReason::DUPLICATE));
Davide Pesavento0f830802018-01-16 23:58:58 -0500412 advanceClocks(10_ms);
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000413 BOOST_REQUIRE_EQUAL(face.sentNacks.size(), 1);
414 BOOST_CHECK_EQUAL(face.sentNacks[0].getReason(), lp::NackReason::DUPLICATE);
415 BOOST_CHECK(face.sentNacks[0].getTag<lp::CongestionMarkTag>() == nullptr);
416
Junxiao Shib55e5d32018-07-18 13:32:00 -0600417 auto nack = makeNack(*interest2, lp::NackReason::NO_ROUTE);
Eric Newberry4d261b62016-11-10 13:40:09 -0700418 nack.setTag(make_shared<lp::CongestionMarkTag>(1));
419 face.put(nack);
Davide Pesavento0f830802018-01-16 23:58:58 -0500420 advanceClocks(10_ms);
Eric Newberry4d261b62016-11-10 13:40:09 -0700421 BOOST_REQUIRE_EQUAL(face.sentNacks.size(), 2);
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000422 BOOST_CHECK_EQUAL(face.sentNacks[1].getReason(), lp::NackReason::NO_ROUTE);
Eric Newberry4d261b62016-11-10 13:40:09 -0700423 BOOST_CHECK(face.sentNacks[1].getTag<lp::CongestionMarkTag>() != nullptr);
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000424}
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500425
Junxiao Shi79a7a162017-09-09 08:33:57 +0000426BOOST_AUTO_TEST_CASE(PutMultipleNack)
427{
Junxiao Shi859888f2017-09-12 14:29:16 +0000428 bool hasInterest1 = false, hasInterest2 = false;
429 // register two Interest destinations
430 face.setInterestFilter("/", [&] (const InterestFilter&, const Interest& interest) {
431 hasInterest1 = true;
432 // sending Nack right away from the first destination, Interest should still go to second destination
433 face.put(makeNack(interest, lp::NackReason::CONGESTION));
434 });
435 face.setInterestFilter("/", bind([&] { hasInterest2 = true; }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500436 advanceClocks(10_ms);
Junxiao Shi79a7a162017-09-09 08:33:57 +0000437
Junxiao Shib55e5d32018-07-18 13:32:00 -0600438 auto interest = makeInterest("/A", false, DEFAULT_INTEREST_LIFETIME, 14333271);
439 face.receive(*interest);
Davide Pesavento0f830802018-01-16 23:58:58 -0500440 advanceClocks(10_ms);
Junxiao Shi859888f2017-09-12 14:29:16 +0000441 BOOST_CHECK(hasInterest1);
442 BOOST_CHECK(hasInterest2);
Junxiao Shi79a7a162017-09-09 08:33:57 +0000443
Junxiao Shi859888f2017-09-12 14:29:16 +0000444 // Nack from first destination is received, should wait for a response from the other destination
445 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0);
Junxiao Shi79a7a162017-09-09 08:33:57 +0000446
Junxiao Shib55e5d32018-07-18 13:32:00 -0600447 face.put(makeNack(*interest, lp::NackReason::NO_ROUTE)); // Nack from second destination
Davide Pesavento0f830802018-01-16 23:58:58 -0500448 advanceClocks(10_ms);
Junxiao Shi859888f2017-09-12 14:29:16 +0000449 BOOST_CHECK_EQUAL(face.sentNacks.size(), 1); // sending Nack after both destinations Nacked
Junxiao Shi79a7a162017-09-09 08:33:57 +0000450 BOOST_CHECK_EQUAL(face.sentNacks.at(0).getReason(), lp::NackReason::CONGESTION); // least severe reason
451
Junxiao Shib55e5d32018-07-18 13:32:00 -0600452 face.put(makeNack(*interest, lp::NackReason::DUPLICATE));
Junxiao Shi79a7a162017-09-09 08:33:57 +0000453 BOOST_CHECK_EQUAL(face.sentNacks.size(), 1); // additional Nacks are ignored
454}
455
Junxiao Shib6828912017-11-20 14:06:32 +0000456BOOST_AUTO_TEST_CASE(PutMultipleNackLoopback)
457{
458 bool hasInterest1 = false, hasNack = false;
459
460 // first InterestFilter allows loopback and should receive Interest
461 face.setInterestFilter("/", [&] (const InterestFilter&, const Interest& interest) {
462 hasInterest1 = true;
463 face.put(makeNack(interest, lp::NackReason::CONGESTION));
464 });
465 // second InterestFilter disallows loopback and should not receive Interest
466 face.setInterestFilter(InterestFilter("/").allowLoopback(false),
467 bind([] { BOOST_ERROR("Unexpected Interest on second InterestFilter"); }));
468
Junxiao Shib55e5d32018-07-18 13:32:00 -0600469 auto interest = makeInterest("/A", false, DEFAULT_INTEREST_LIFETIME, 28395852);
470 face.expressInterest(*interest,
Junxiao Shib6828912017-11-20 14:06:32 +0000471 bind([] { BOOST_FAIL("Unexpected data"); }),
472 [&] (const Interest&, const lp::Nack& nack) {
473 hasNack = true;
474 BOOST_CHECK_EQUAL(nack.getReason(), lp::NackReason::CONGESTION);
475 },
476 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500477 advanceClocks(1_ms);
Junxiao Shib6828912017-11-20 14:06:32 +0000478 BOOST_CHECK_EQUAL(hasInterest1, true); // Interest looped back
479 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1); // Interest sent to forwarder
480 BOOST_CHECK_EQUAL(hasNack, false); // waiting for Nack from forwarder
481
Junxiao Shib55e5d32018-07-18 13:32:00 -0600482 face.receive(makeNack(*interest, lp::NackReason::NO_ROUTE));
Davide Pesavento0f830802018-01-16 23:58:58 -0500483 advanceClocks(1_ms);
Junxiao Shib6828912017-11-20 14:06:32 +0000484 BOOST_CHECK_EQUAL(hasNack, true);
485}
486
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400487BOOST_AUTO_TEST_CASE(SetUnsetInterestFilter)
488{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800489 size_t nInterests = 0;
490 size_t nRegs = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400491 const RegisteredPrefixId* regPrefixId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800492 face.setInterestFilter("/Hello/World",
493 bind([&nInterests] { ++nInterests; }),
494 bind([&nRegs] { ++nRegs; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000495 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500496 advanceClocks(25_ms, 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800497 BOOST_CHECK_EQUAL(nRegs, 1);
498 BOOST_CHECK_EQUAL(nInterests, 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400499
Junxiao Shib55e5d32018-07-18 13:32:00 -0600500 face.receive(*makeInterest("/Hello/World/%21"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500501 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400502
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800503 BOOST_CHECK_EQUAL(nRegs, 1);
504 BOOST_CHECK_EQUAL(nInterests, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400505
Junxiao Shib55e5d32018-07-18 13:32:00 -0600506 face.receive(*makeInterest("/Bye/World/%21"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500507 advanceClocks(10000_ms, 10);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800508 BOOST_CHECK_EQUAL(nInterests, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400509
Junxiao Shib55e5d32018-07-18 13:32:00 -0600510 face.receive(*makeInterest("/Hello/World/%21/2"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500511 advanceClocks(25_ms, 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800512 BOOST_CHECK_EQUAL(nInterests, 2);
513
514 // removing filter
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800515 face.unsetInterestFilter(regPrefixId);
Davide Pesavento0f830802018-01-16 23:58:58 -0500516 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400517
Junxiao Shib55e5d32018-07-18 13:32:00 -0600518 face.receive(*makeInterest("/Hello/World/%21/3"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800519 BOOST_CHECK_EQUAL(nInterests, 2);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400520
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000521 face.unsetInterestFilter(static_cast<const RegisteredPrefixId*>(nullptr));
Davide Pesavento0f830802018-01-16 23:58:58 -0500522 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400523
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000524 face.unsetInterestFilter(static_cast<const InterestFilterId*>(nullptr));
Davide Pesavento0f830802018-01-16 23:58:58 -0500525 advanceClocks(25_ms, 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800526}
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400527
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000528BOOST_AUTO_TEST_CASE(SetInterestFilterEmptyInterestCallback)
529{
530 face.setInterestFilter("/A", nullptr);
Davide Pesavento0f830802018-01-16 23:58:58 -0500531 advanceClocks(1_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000532
533 BOOST_CHECK_NO_THROW(do {
534 face.receive(*makeInterest("/A/1"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500535 advanceClocks(1_ms);
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000536 } while (false));
537}
538
Joao Pereira0b3cac52015-07-02 14:49:49 -0400539BOOST_AUTO_TEST_CASE(SetUnsetInterestFilterWithoutSucessCallback)
540{
541 size_t nInterests = 0;
542 const RegisteredPrefixId* regPrefixId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800543 face.setInterestFilter("/Hello/World",
544 bind([&nInterests] { ++nInterests; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000545 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Davide Pesavento0f830802018-01-16 23:58:58 -0500546 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400547 BOOST_CHECK_EQUAL(nInterests, 0);
548
Junxiao Shib55e5d32018-07-18 13:32:00 -0600549 face.receive(*makeInterest("/Hello/World/%21"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500550 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400551
552 BOOST_CHECK_EQUAL(nInterests, 1);
553
Junxiao Shib55e5d32018-07-18 13:32:00 -0600554 face.receive(*makeInterest("/Bye/World/%21"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500555 advanceClocks(10000_ms, 10);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400556 BOOST_CHECK_EQUAL(nInterests, 1);
557
Junxiao Shib55e5d32018-07-18 13:32:00 -0600558 face.receive(*makeInterest("/Hello/World/%21/2"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500559 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400560 BOOST_CHECK_EQUAL(nInterests, 2);
561
562 // removing filter
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800563 face.unsetInterestFilter(regPrefixId);
Davide Pesavento0f830802018-01-16 23:58:58 -0500564 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400565
Junxiao Shib55e5d32018-07-18 13:32:00 -0600566 face.receive(*makeInterest("/Hello/World/%21/3"));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400567 BOOST_CHECK_EQUAL(nInterests, 2);
568
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000569 face.unsetInterestFilter(static_cast<const RegisteredPrefixId*>(nullptr));
Davide Pesavento0f830802018-01-16 23:58:58 -0500570 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400571
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000572 face.unsetInterestFilter(static_cast<const InterestFilterId*>(nullptr));
Davide Pesavento0f830802018-01-16 23:58:58 -0500573 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400574}
575
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800576BOOST_FIXTURE_TEST_CASE(SetInterestFilterFail, FacesNoRegistrationReplyFixture)
577{
578 // don't enable registration reply
579 size_t nRegFailed = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800580 face.setInterestFilter("/Hello/World",
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000581 bind([] { BOOST_FAIL("Unexpected Interest"); }),
582 bind([] { BOOST_FAIL("Unexpected success of setInterestFilter"); }),
583 bind([&nRegFailed] { ++nRegFailed; }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800584
Davide Pesavento0f830802018-01-16 23:58:58 -0500585 advanceClocks(25_ms, 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800586 BOOST_CHECK_EQUAL(nRegFailed, 0);
587
Davide Pesavento0f830802018-01-16 23:58:58 -0500588 advanceClocks(2000_ms, 5);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800589 BOOST_CHECK_EQUAL(nRegFailed, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400590}
591
Joao Pereira0b3cac52015-07-02 14:49:49 -0400592BOOST_FIXTURE_TEST_CASE(SetInterestFilterFailWithoutSuccessCallback, FacesNoRegistrationReplyFixture)
593{
594 // don't enable registration reply
595 size_t nRegFailed = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800596 face.setInterestFilter("/Hello/World",
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000597 bind([] { BOOST_FAIL("Unexpected Interest"); }),
598 bind([&nRegFailed] { ++nRegFailed; }));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400599
Davide Pesavento0f830802018-01-16 23:58:58 -0500600 advanceClocks(25_ms, 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400601 BOOST_CHECK_EQUAL(nRegFailed, 0);
602
Davide Pesavento0f830802018-01-16 23:58:58 -0500603 advanceClocks(2000_ms, 5);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400604 BOOST_CHECK_EQUAL(nRegFailed, 1);
605}
606
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400607BOOST_AUTO_TEST_CASE(RegisterUnregisterPrefix)
608{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800609 size_t nRegSuccesses = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400610 const RegisteredPrefixId* regPrefixId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800611 face.registerPrefix("/Hello/World",
612 bind([&nRegSuccesses] { ++nRegSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000613 bind([] { BOOST_FAIL("Unexpected registerPrefix failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400614
Davide Pesavento0f830802018-01-16 23:58:58 -0500615 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400616 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
617
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800618 size_t nUnregSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800619 face.unregisterPrefix(regPrefixId,
620 bind([&nUnregSuccesses] { ++nUnregSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000621 bind([] { BOOST_FAIL("Unexpected unregisterPrefix failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400622
Davide Pesavento0f830802018-01-16 23:58:58 -0500623 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400624 BOOST_CHECK_EQUAL(nUnregSuccesses, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400625}
626
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800627BOOST_FIXTURE_TEST_CASE(RegisterUnregisterPrefixFail, FacesNoRegistrationReplyFixture)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400628{
Junxiao Shib6828912017-11-20 14:06:32 +0000629 // don't enable registration reply
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800630 size_t nRegFailures = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800631 face.registerPrefix("/Hello/World",
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000632 bind([] { BOOST_FAIL("Unexpected registerPrefix success"); }),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800633 bind([&nRegFailures] { ++nRegFailures; }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400634
Davide Pesavento0f830802018-01-16 23:58:58 -0500635 advanceClocks(5000_ms, 20);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800636 BOOST_CHECK_EQUAL(nRegFailures, 1);
637}
638
639BOOST_AUTO_TEST_CASE(SimilarFilters)
640{
641 size_t nInInterests1 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800642 face.setInterestFilter("/Hello/World",
643 bind([&nInInterests1] { ++nInInterests1; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000644 nullptr,
645 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800646
647 size_t nInInterests2 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800648 face.setInterestFilter("/Hello",
649 bind([&nInInterests2] { ++nInInterests2; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000650 nullptr,
651 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400652
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800653 size_t nInInterests3 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800654 face.setInterestFilter("/Los/Angeles/Lakers",
655 bind([&nInInterests3] { ++nInInterests3; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000656 nullptr,
657 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400658
Davide Pesavento0f830802018-01-16 23:58:58 -0500659 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400660
Junxiao Shib55e5d32018-07-18 13:32:00 -0600661 face.receive(*makeInterest("/Hello/World/%21"));
Davide Pesavento0f830802018-01-16 23:58:58 -0500662 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400663
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800664 BOOST_CHECK_EQUAL(nInInterests1, 1);
665 BOOST_CHECK_EQUAL(nInInterests2, 1);
666 BOOST_CHECK_EQUAL(nInInterests3, 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400667}
668
669BOOST_AUTO_TEST_CASE(SetRegexFilterError)
670{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800671 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
672 [] (const Name&, const Interest&) {
673 BOOST_FAIL("InterestFilter::Error should have been triggered");
674 },
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000675 nullptr,
676 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400677
Davide Pesavento0f830802018-01-16 23:58:58 -0500678 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400679
Junxiao Shib55e5d32018-07-18 13:32:00 -0600680 BOOST_REQUIRE_THROW(face.receive(*makeInterest("/Hello/World/XXX/b/c")), InterestFilter::Error);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400681}
682
683BOOST_AUTO_TEST_CASE(SetRegexFilter)
684{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800685 size_t nInInterests = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800686 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
687 bind([&nInInterests] { ++nInInterests; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000688 nullptr,
689 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400690
Davide Pesavento0f830802018-01-16 23:58:58 -0500691 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400692
Junxiao Shib55e5d32018-07-18 13:32:00 -0600693 face.receive(*makeInterest("/Hello/World/a")); // shouldn't match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400694 BOOST_CHECK_EQUAL(nInInterests, 0);
695
Junxiao Shib55e5d32018-07-18 13:32:00 -0600696 face.receive(*makeInterest("/Hello/World/a/b")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400697 BOOST_CHECK_EQUAL(nInInterests, 1);
698
Junxiao Shib55e5d32018-07-18 13:32:00 -0600699 face.receive(*makeInterest("/Hello/World/a/b/c")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400700 BOOST_CHECK_EQUAL(nInInterests, 2);
701
Junxiao Shib55e5d32018-07-18 13:32:00 -0600702 face.receive(*makeInterest("/Hello/World/a/b/d")); // should not match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400703 BOOST_CHECK_EQUAL(nInInterests, 2);
704}
705
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400706BOOST_AUTO_TEST_CASE(SetRegexFilterAndRegister)
707{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800708 size_t nInInterests = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800709 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
710 bind([&nInInterests] { ++nInInterests; }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400711
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800712 size_t nRegSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800713 face.registerPrefix("/Hello/World",
714 bind([&nRegSuccesses] { ++nRegSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000715 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400716
Davide Pesavento0f830802018-01-16 23:58:58 -0500717 advanceClocks(25_ms, 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400718 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
719
Junxiao Shib55e5d32018-07-18 13:32:00 -0600720 face.receive(*makeInterest("/Hello/World/a")); // shouldn't match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400721 BOOST_CHECK_EQUAL(nInInterests, 0);
722
Junxiao Shib55e5d32018-07-18 13:32:00 -0600723 face.receive(*makeInterest("/Hello/World/a/b")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400724 BOOST_CHECK_EQUAL(nInInterests, 1);
725
Junxiao Shib55e5d32018-07-18 13:32:00 -0600726 face.receive(*makeInterest("/Hello/World/a/b/c")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400727 BOOST_CHECK_EQUAL(nInInterests, 2);
728
Junxiao Shib55e5d32018-07-18 13:32:00 -0600729 face.receive(*makeInterest("/Hello/World/a/b/d")); // should not match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400730 BOOST_CHECK_EQUAL(nInInterests, 2);
731}
732
Junxiao Shia1ea5062014-12-27 22:33:39 -0700733BOOST_FIXTURE_TEST_CASE(SetInterestFilterNoReg, FacesNoRegistrationReplyFixture) // Bug 2318
734{
735 // This behavior is specific to DummyClientFace.
736 // Regular Face won't accept incoming packets until something is sent.
737
738 int hit = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800739 face.setInterestFilter(Name("/"), bind([&hit] { ++hit; }));
740 face.processEvents(time::milliseconds(-1));
Junxiao Shia1ea5062014-12-27 22:33:39 -0700741
Junxiao Shib55e5d32018-07-18 13:32:00 -0600742 face.receive(*makeInterest("/A"));
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800743 face.processEvents(time::milliseconds(-1));
Junxiao Shia1ea5062014-12-27 22:33:39 -0700744
745 BOOST_CHECK_EQUAL(hit, 1);
746}
747
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000748BOOST_AUTO_TEST_SUITE_END() // Producer
749
Junxiao Shiae0b4182016-08-08 22:53:17 +0000750BOOST_AUTO_TEST_SUITE(IoRoutines)
751
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500752BOOST_AUTO_TEST_CASE(ProcessEvents)
753{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800754 face.processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500755
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800756 size_t nRegSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800757 face.registerPrefix("/Hello/World",
758 bind([&nRegSuccesses] { ++nRegSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000759 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500760
761 // io_service::poll() without reset
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800762 face.getIoService().poll();
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500763 BOOST_CHECK_EQUAL(nRegSuccesses, 0);
764
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800765 face.processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500766 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
767}
768
Junxiao Shiae0b4182016-08-08 22:53:17 +0000769BOOST_AUTO_TEST_CASE(DestroyWithoutProcessEvents) // Bug 3248
770{
771 auto face2 = make_unique<Face>(io);
772 face2.reset();
773
774 io.poll(); // should not crash
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100775
776 // avoid "test case [...] did not check any assertions" message from Boost.Test
777 BOOST_CHECK(true);
Junxiao Shiae0b4182016-08-08 22:53:17 +0000778}
779
780BOOST_AUTO_TEST_SUITE_END() // IoRoutines
781
782BOOST_AUTO_TEST_SUITE(Transport)
783
784using ndn::Transport;
785
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700786struct PibDirWithDefaultTpm
787{
788 const std::string PATH = "build/keys-with-default-tpm";
789};
790
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800791BOOST_FIXTURE_TEST_CASE(FaceTransport, IdentityManagementTimeFixture)
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800792{
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800793 BOOST_CHECK(Face().getTransport() != nullptr);
794
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800795 BOOST_CHECK(Face(shared_ptr<Transport>()).getTransport() != nullptr);
796 BOOST_CHECK(Face(shared_ptr<Transport>(), io).getTransport() != nullptr);
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800797 BOOST_CHECK(Face(shared_ptr<Transport>(), io, m_keyChain).getTransport() != nullptr);
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800798
799 auto transport = make_shared<TcpTransport>("localhost", "6363"); // no real io operations will be scheduled
800 BOOST_CHECK(Face(transport).getTransport() == transport);
801 BOOST_CHECK(Face(transport, io).getTransport() == transport);
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800802 BOOST_CHECK(Face(transport, io, m_keyChain).getTransport() == transport);
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800803}
804
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700805class WithEnv : private IdentityManagementTimeFixture
806{
807public:
808 WithEnv()
809 {
810 if (getenv("NDN_CLIENT_TRANSPORT") != nullptr) {
811 m_oldTransport = getenv("NDN_CLIENT_TRANSPORT");
812 unsetenv("NDN_CLIENT_TRANSPORT");
813 }
814 }
815
816 void
817 configure(const std::string& faceUri)
818 {
819 setenv("NDN_CLIENT_TRANSPORT", faceUri.c_str(), true);
820 }
821
822 ~WithEnv()
823 {
824 if (!m_oldTransport.empty()) {
825 setenv("NDN_CLIENT_TRANSPORT", m_oldTransport.c_str(), true);
826 }
827 else {
828 unsetenv("NDN_CLIENT_TRANSPORT");
829 }
830 }
831
832private:
833 std::string m_oldTransport;
834};
835
836class WithConfig : private TestHomeFixture<DefaultPibDir>
837{
838public:
839 void
840 configure(const std::string& faceUri)
841 {
842 createClientConf({"transport=" + faceUri});
843 }
844};
845
846class WithEnvAndConfig : public WithEnv, public WithConfig
847{
848};
849
850typedef boost::mpl::vector<WithEnv, WithConfig> ConfigOptions;
851
852BOOST_FIXTURE_TEST_CASE(NoConfig, WithEnvAndConfig) // fixture configures test HOME and PIB/TPM path
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700853{
854 shared_ptr<Face> face;
855 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
856 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700857}
858
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700859BOOST_FIXTURE_TEST_CASE_TEMPLATE(Unix, T, ConfigOptions, T)
860{
861 this->configure("unix://some/path");
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700862
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700863 shared_ptr<Face> face;
864 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
865 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
866}
867
868BOOST_FIXTURE_TEST_CASE_TEMPLATE(Tcp, T, ConfigOptions, T)
869{
870 this->configure("tcp://127.0.0.1:6000");
871
872 shared_ptr<Face> face;
873 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
874 BOOST_CHECK(dynamic_pointer_cast<TcpTransport>(face->getTransport()) != nullptr);
875}
876
877BOOST_FIXTURE_TEST_CASE_TEMPLATE(WrongTransport, T, ConfigOptions, T)
878{
879 this->configure("wrong-transport:");
880
881 BOOST_CHECK_THROW(make_shared<Face>(), ConfigFile::Error);
882}
883
884BOOST_FIXTURE_TEST_CASE_TEMPLATE(WrongUri, T, ConfigOptions, T)
885{
886 this->configure("wrong-uri");
887
888 BOOST_CHECK_THROW(make_shared<Face>(), ConfigFile::Error);
889}
890
891BOOST_FIXTURE_TEST_CASE(EnvOverride, WithEnvAndConfig)
892{
893 this->WithEnv::configure("tcp://127.0.0.1:6000");
894 this->WithConfig::configure("unix://some/path");
895
896 shared_ptr<Face> face;
897 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
898 BOOST_CHECK(dynamic_pointer_cast<TcpTransport>(face->getTransport()) != nullptr);
899}
900
901BOOST_FIXTURE_TEST_CASE(ExplicitTransport, WithEnvAndConfig)
902{
903 this->WithEnv::configure("wrong-uri");
904 this->WithConfig::configure("wrong-transport:");
905
906 auto transport = make_shared<UnixTransport>("unix://some/path");
907 shared_ptr<Face> face;
908 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>(transport));
909 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
910}
911
Junxiao Shiae0b4182016-08-08 22:53:17 +0000912BOOST_AUTO_TEST_SUITE_END() // Transport
913
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700914BOOST_AUTO_TEST_SUITE_END() // TestFace
915
916} // namespace tests
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400917} // namespace ndn