blob: c25681b63b0c6e8a952803b8056db30710e07534 [file] [log] [blame]
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev57e00362016-06-23 13:22:54 -07003 * Copyright (c) 2013-2016 Regents of the University of California.
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -04004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#include "face.hpp"
Junxiao Shia1478db2016-09-09 04:13:15 +000023#include "lp/tags.hpp"
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040024#include "security/key-chain.hpp"
Alexander Afanasyev3a6da362015-12-29 20:31:03 -080025#include "transport/tcp-transport.hpp"
Alexander Afanasyev57e00362016-06-23 13:22:54 -070026#include "transport/unix-transport.hpp"
Junxiao Shia1478db2016-09-09 04:13:15 +000027#include "util/dummy-client-face.hpp"
28#include "util/scheduler.hpp"
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040029
30#include "boost-test.hpp"
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070031#include "identity-management-time-fixture.hpp"
32#include "key-chain-fixture.hpp"
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080033#include "make-interest-data.hpp"
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040034
35namespace ndn {
36namespace tests {
37
Junxiao Shia60d9362014-11-12 09:38:21 -070038using ndn::util::DummyClientFace;
Junxiao Shia60d9362014-11-12 09:38:21 -070039
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070040class FaceFixture : public IdentityManagementTimeFixture
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040041{
42public:
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080043 explicit
Junxiao Shia1ea5062014-12-27 22:33:39 -070044 FaceFixture(bool enableRegistrationReply = true)
Junxiao Shif5b5ae22016-08-08 05:54:41 +000045 : face(io, m_keyChain, {true, enableRegistrationReply})
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040046 {
47 }
48
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080049public:
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080050 DummyClientFace face;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040051};
52
Junxiao Shia1ea5062014-12-27 22:33:39 -070053class FacesNoRegistrationReplyFixture : public FaceFixture
Junxiao Shia60d9362014-11-12 09:38:21 -070054{
55public:
56 FacesNoRegistrationReplyFixture()
Junxiao Shia1ea5062014-12-27 22:33:39 -070057 : FaceFixture(false)
Junxiao Shia60d9362014-11-12 09:38:21 -070058 {
59 }
60};
61
Junxiao Shia1ea5062014-12-27 22:33:39 -070062BOOST_FIXTURE_TEST_SUITE(TestFace, FaceFixture)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040063
Junxiao Shi103d8ed2016-08-07 20:34:10 +000064BOOST_AUTO_TEST_SUITE(Consumer)
65
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040066BOOST_AUTO_TEST_CASE(ExpressInterestData)
67{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080068 size_t nData = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080069 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
70 [&] (const Interest& i, const Data& d) {
71 BOOST_CHECK(i.getName().isPrefixOf(d.getName()));
72 BOOST_CHECK_EQUAL(i.getName(), "/Hello/World");
73 ++nData;
74 },
Junxiao Shi103d8ed2016-08-07 20:34:10 +000075 bind([] { BOOST_FAIL("Unexpected Nack"); }),
76 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Eric Newberry83872fd2015-08-06 17:01:24 -070077
Junxiao Shif5b5ae22016-08-08 05:54:41 +000078 advanceClocks(time::milliseconds(40));
Eric Newberry83872fd2015-08-06 17:01:24 -070079
Junxiao Shi85d90832016-08-04 03:19:46 +000080 face.receive(*makeData("/Bye/World/a"));
81 face.receive(*makeData("/Hello/World/a"));
Eric Newberry83872fd2015-08-06 17:01:24 -070082
Junxiao Shif5b5ae22016-08-08 05:54:41 +000083 advanceClocks(time::milliseconds(50), 2);
Eric Newberry83872fd2015-08-06 17:01:24 -070084
85 BOOST_CHECK_EQUAL(nData, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080086 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
87 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Eric Newberry83872fd2015-08-06 17:01:24 -070088
89 size_t nTimeouts = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080090 face.expressInterest(Interest("/Hello/World/a/2", time::milliseconds(50)),
91 bind([]{}),
92 bind([]{}),
Junxiao Shi103d8ed2016-08-07 20:34:10 +000093 bind([&nTimeouts] { ++nTimeouts; }));
Junxiao Shif5b5ae22016-08-08 05:54:41 +000094 advanceClocks(time::milliseconds(200), 5);
Eric Newberry83872fd2015-08-06 17:01:24 -070095 BOOST_CHECK_EQUAL(nTimeouts, 1);
96}
97
Junxiao Shi76e0eb22016-08-08 05:54:10 +000098BOOST_AUTO_TEST_CASE(ExpressInterestEmptyDataCallback)
99{
100 face.expressInterest(Interest("/Hello/World"),
101 nullptr,
102 bind([] { BOOST_FAIL("Unexpected Nack"); }),
103 bind([] { BOOST_FAIL("Unexpected timeout"); }));
104 advanceClocks(time::milliseconds(1));
105
106 BOOST_CHECK_NO_THROW(do {
107 face.receive(*makeData("/Hello/World/a"));
108 advanceClocks(time::milliseconds(1));
109 } while (false));
110}
111
Eric Newberry83872fd2015-08-06 17:01:24 -0700112BOOST_AUTO_TEST_CASE(DeprecatedExpressInterestData)
113{
114 size_t nData = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800115 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000116 [&] (const Interest& i, Data& d) {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800117 BOOST_CHECK(i.getName().isPrefixOf(d.getName()));
118 ++nData;
119 },
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000120 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400121
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000122 advanceClocks(time::milliseconds(40));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400123
Junxiao Shi85d90832016-08-04 03:19:46 +0000124 face.receive(*makeData("/Bye/World/a"));
125 face.receive(*makeData("/Hello/World/a"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800126
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000127 advanceClocks(time::milliseconds(50), 2);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400128
129 BOOST_CHECK_EQUAL(nData, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800130 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
131 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800132
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800133 face.expressInterest(Interest("/Hello/World/a", time::milliseconds(50)),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000134 [&] (const Interest& i, Data& d) {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800135 BOOST_CHECK(i.getName().isPrefixOf(d.getName()));
136 ++nData;
137 },
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000138 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000139 advanceClocks(time::milliseconds(40));
Junxiao Shi85d90832016-08-04 03:19:46 +0000140 face.receive(*makeData("/Hello/World/a/1/xxxxx"));
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800141
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000142 advanceClocks(time::milliseconds(50), 2);
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800143
144 BOOST_CHECK_EQUAL(nData, 2);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800145 BOOST_CHECK_EQUAL(face.sentInterests.size(), 2);
146 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400147}
148
149BOOST_AUTO_TEST_CASE(ExpressInterestTimeout)
150{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800151 size_t nTimeouts = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800152 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000153 bind([] { BOOST_FAIL("Unexpected Data"); }),
154 bind([] { BOOST_FAIL("Unexpected Nack"); }),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800155 [&nTimeouts] (const Interest& i) {
156 BOOST_CHECK_EQUAL(i.getName(), "/Hello/World");
157 ++nTimeouts;
158 });
Eric Newberry83872fd2015-08-06 17:01:24 -0700159
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000160 advanceClocks(time::milliseconds(200), 5);
Eric Newberry83872fd2015-08-06 17:01:24 -0700161
162 BOOST_CHECK_EQUAL(nTimeouts, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800163 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
164 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
165 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0);
Eric Newberry83872fd2015-08-06 17:01:24 -0700166}
167
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000168BOOST_AUTO_TEST_CASE(ExpressInterestEmptyTimeoutCallback)
169{
170 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
171 bind([] { BOOST_FAIL("Unexpected Data"); }),
172 bind([] { BOOST_FAIL("Unexpected Nack"); }),
173 nullptr);
174 advanceClocks(time::milliseconds(40));
175
176 BOOST_CHECK_NO_THROW(do {
177 advanceClocks(time::milliseconds(6), 2);
178 } while (false));
179}
180
Eric Newberry83872fd2015-08-06 17:01:24 -0700181BOOST_AUTO_TEST_CASE(DeprecatedExpressInterestTimeout)
182{
183 size_t nTimeouts = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800184 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000185 bind([] { BOOST_FAIL("Unexpected data"); }),
186 bind([&nTimeouts] { ++nTimeouts; }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400187
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000188 advanceClocks(time::milliseconds(200), 5);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400189
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400190 BOOST_CHECK_EQUAL(nTimeouts, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800191 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
192 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400193}
194
Eric Newberry83872fd2015-08-06 17:01:24 -0700195BOOST_AUTO_TEST_CASE(ExpressInterestNack)
196{
197 size_t nNacks = 0;
198
199 Interest interest("/Hello/World", time::milliseconds(50));
200
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800201 face.expressInterest(interest,
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000202 bind([] { BOOST_FAIL("Unexpected Data"); }),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800203 [&] (const Interest& i, const lp::Nack& n) {
204 BOOST_CHECK(i.getName().isPrefixOf(n.getInterest().getName()));
205 BOOST_CHECK_EQUAL(i.getName(), "/Hello/World");
206 BOOST_CHECK_EQUAL(n.getReason(), lp::NackReason::DUPLICATE);
207 ++nNacks;
208 },
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000209 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Eric Newberry83872fd2015-08-06 17:01:24 -0700210
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000211 advanceClocks(time::milliseconds(40));
Eric Newberry83872fd2015-08-06 17:01:24 -0700212
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000213 face.receive(makeNack(face.sentInterests.at(0), lp::NackReason::DUPLICATE));
Eric Newberry83872fd2015-08-06 17:01:24 -0700214
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000215 advanceClocks(time::milliseconds(50), 2);
Eric Newberry83872fd2015-08-06 17:01:24 -0700216
217 BOOST_CHECK_EQUAL(nNacks, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800218 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
Eric Newberry83872fd2015-08-06 17:01:24 -0700219}
220
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000221BOOST_AUTO_TEST_CASE(ExpressInterestEmptyNackCallback)
222{
223 face.expressInterest(Interest("/Hello/World"),
224 bind([] { BOOST_FAIL("Unexpected Data"); }),
225 nullptr,
226 bind([] { BOOST_FAIL("Unexpected timeout"); }));
227 advanceClocks(time::milliseconds(1));
228
229 BOOST_CHECK_NO_THROW(do {
230 face.receive(makeNack(face.sentInterests.at(0), lp::NackReason::DUPLICATE));
231 advanceClocks(time::milliseconds(1));
232 } while (false));
233}
234
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000235BOOST_AUTO_TEST_CASE(DeprecatedExpressInterestNack)
236{
237 size_t nTimeouts = 0;
238 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
239 bind([] { BOOST_FAIL("Unexpected data"); }),
240 bind([&nTimeouts] { ++nTimeouts; }));
241 advanceClocks(time::milliseconds(1));
242
243 face.receive(makeNack(face.sentInterests.at(0), lp::NackReason::CONGESTION));
244 advanceClocks(time::milliseconds(1));
245
246 BOOST_CHECK_EQUAL(nTimeouts, 1);
247 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
248 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
249}
250
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800251BOOST_AUTO_TEST_CASE(RemovePendingInterest)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400252{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800253 const PendingInterestId* interestId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800254 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000255 bind([] { BOOST_FAIL("Unexpected data"); }),
256 bind([] { BOOST_FAIL("Unexpected nack"); }),
257 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800258 advanceClocks(time::milliseconds(10));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400259
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800260 face.removePendingInterest(interestId);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800261 advanceClocks(time::milliseconds(10));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400262
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000263 face.receive(*makeData("/Hello/World/%21"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000264 advanceClocks(time::milliseconds(200), 5);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400265}
266
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000267BOOST_AUTO_TEST_CASE(RemoveAllPendingInterests)
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500268{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800269 face.expressInterest(Interest("/Hello/World/0", time::milliseconds(50)),
270 bind([] { BOOST_FAIL("Unexpected data"); }),
271 bind([] { BOOST_FAIL("Unexpected nack"); }),
272 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500273
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800274 face.expressInterest(Interest("/Hello/World/1", time::milliseconds(50)),
275 bind([] { BOOST_FAIL("Unexpected data"); }),
276 bind([] { BOOST_FAIL("Unexpected nack"); }),
277 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500278
279 advanceClocks(time::milliseconds(10));
280
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800281 face.removeAllPendingInterests();
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500282 advanceClocks(time::milliseconds(10));
283
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800284 BOOST_CHECK_EQUAL(face.getNPendingInterests(), 0);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500285
Junxiao Shi85d90832016-08-04 03:19:46 +0000286 face.receive(*makeData("/Hello/World/0"));
287 face.receive(*makeData("/Hello/World/1"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000288 advanceClocks(time::milliseconds(200), 5);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500289}
290
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000291BOOST_AUTO_TEST_CASE(DestructionWithoutCancellingPendingInterests) // Bug #2518
292{
293 {
294 DummyClientFace face2(io, m_keyChain);
295 face2.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
296 bind([]{}), bind([]{}));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000297 advanceClocks(time::milliseconds(50), 2);
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000298 }
299
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000300 advanceClocks(time::milliseconds(50), 2);
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000301 // should not segfault
302}
303
304BOOST_AUTO_TEST_SUITE_END() // Consumer
305
306BOOST_AUTO_TEST_SUITE(Producer)
307
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000308BOOST_AUTO_TEST_CASE(PutData)
309{
310 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
311
312 Data data("/4g7xxcuEow/KFvK5Kf2m");
313 signData(data);
314 face.put(data);
315
316 lp::CachePolicy cachePolicy;
317 cachePolicy.setPolicy(lp::CachePolicyType::NO_CACHE);
318 data.setTag(make_shared<lp::CachePolicyTag>(cachePolicy));
Eric Newberry4d261b62016-11-10 13:40:09 -0700319 data.setTag(make_shared<lp::CongestionMarkTag>(1));
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000320 face.put(data);
321
322 advanceClocks(time::milliseconds(10));
323 BOOST_REQUIRE_EQUAL(face.sentData.size(), 2);
324 BOOST_CHECK(face.sentData[0].getTag<lp::CachePolicyTag>() == nullptr);
Eric Newberry4d261b62016-11-10 13:40:09 -0700325 BOOST_CHECK(face.sentData[0].getTag<lp::CongestionMarkTag>() == nullptr);
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000326 BOOST_CHECK(face.sentData[1].getTag<lp::CachePolicyTag>() != nullptr);
Eric Newberry4d261b62016-11-10 13:40:09 -0700327 BOOST_CHECK(face.sentData[1].getTag<lp::CongestionMarkTag>() != nullptr);
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000328}
329
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000330BOOST_AUTO_TEST_CASE(PutNack)
331{
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000332 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0);
333
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000334 face.put(makeNack(Interest("/Hello/World", time::milliseconds(50)), lp::NackReason::NO_ROUTE));
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000335
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000336 advanceClocks(time::milliseconds(10));
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000337 BOOST_CHECK_EQUAL(face.sentNacks.size(), 1);
Eric Newberry4d261b62016-11-10 13:40:09 -0700338
339 auto nack = makeNack(Interest("/another/prefix", time::milliseconds(50)), lp::NackReason::NO_ROUTE);
340 nack.setTag(make_shared<lp::CongestionMarkTag>(1));
341 face.put(nack);
342
343 advanceClocks(time::milliseconds(10));
344 BOOST_REQUIRE_EQUAL(face.sentNacks.size(), 2);
345 BOOST_CHECK(face.sentNacks[0].getTag<lp::CongestionMarkTag>() == nullptr);
346 BOOST_CHECK(face.sentNacks[1].getTag<lp::CongestionMarkTag>() != nullptr);
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000347}
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500348
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400349BOOST_AUTO_TEST_CASE(SetUnsetInterestFilter)
350{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800351 size_t nInterests = 0;
352 size_t nRegs = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400353 const RegisteredPrefixId* regPrefixId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800354 face.setInterestFilter("/Hello/World",
355 bind([&nInterests] { ++nInterests; }),
356 bind([&nRegs] { ++nRegs; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000357 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000358 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800359 BOOST_CHECK_EQUAL(nRegs, 1);
360 BOOST_CHECK_EQUAL(nInterests, 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400361
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000362 face.receive(Interest("/Hello/World/%21"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000363 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400364
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800365 BOOST_CHECK_EQUAL(nRegs, 1);
366 BOOST_CHECK_EQUAL(nInterests, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400367
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000368 face.receive(Interest("/Bye/World/%21"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800369 advanceClocks(time::milliseconds(10000), 10);
370 BOOST_CHECK_EQUAL(nInterests, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400371
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000372 face.receive(Interest("/Hello/World/%21/2"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000373 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800374 BOOST_CHECK_EQUAL(nInterests, 2);
375
376 // removing filter
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800377 face.unsetInterestFilter(regPrefixId);
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000378 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400379
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000380 face.receive(Interest("/Hello/World/%21/3"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800381 BOOST_CHECK_EQUAL(nInterests, 2);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400382
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000383 face.unsetInterestFilter(static_cast<const RegisteredPrefixId*>(nullptr));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000384 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400385
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000386 face.unsetInterestFilter(static_cast<const InterestFilterId*>(nullptr));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000387 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800388}
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400389
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000390BOOST_AUTO_TEST_CASE(SetInterestFilterEmptyInterestCallback)
391{
392 face.setInterestFilter("/A", nullptr);
393 advanceClocks(time::milliseconds(1));
394
395 BOOST_CHECK_NO_THROW(do {
396 face.receive(*makeInterest("/A/1"));
397 advanceClocks(time::milliseconds(1));
398 } while (false));
399}
400
Joao Pereira0b3cac52015-07-02 14:49:49 -0400401BOOST_AUTO_TEST_CASE(SetUnsetInterestFilterWithoutSucessCallback)
402{
403 size_t nInterests = 0;
404 const RegisteredPrefixId* regPrefixId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800405 face.setInterestFilter("/Hello/World",
406 bind([&nInterests] { ++nInterests; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000407 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000408 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400409 BOOST_CHECK_EQUAL(nInterests, 0);
410
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000411 face.receive(Interest("/Hello/World/%21"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000412 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400413
414 BOOST_CHECK_EQUAL(nInterests, 1);
415
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000416 face.receive(Interest("/Bye/World/%21"));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400417 advanceClocks(time::milliseconds(10000), 10);
418 BOOST_CHECK_EQUAL(nInterests, 1);
419
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000420 face.receive(Interest("/Hello/World/%21/2"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000421 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400422 BOOST_CHECK_EQUAL(nInterests, 2);
423
424 // removing filter
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800425 face.unsetInterestFilter(regPrefixId);
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000426 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400427
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000428 face.receive(Interest("/Hello/World/%21/3"));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400429 BOOST_CHECK_EQUAL(nInterests, 2);
430
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000431 face.unsetInterestFilter(static_cast<const RegisteredPrefixId*>(nullptr));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000432 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400433
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000434 face.unsetInterestFilter(static_cast<const InterestFilterId*>(nullptr));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000435 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400436}
437
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800438BOOST_FIXTURE_TEST_CASE(SetInterestFilterFail, FacesNoRegistrationReplyFixture)
439{
440 // don't enable registration reply
441 size_t nRegFailed = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800442 face.setInterestFilter("/Hello/World",
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000443 bind([] { BOOST_FAIL("Unexpected Interest"); }),
444 bind([] { BOOST_FAIL("Unexpected success of setInterestFilter"); }),
445 bind([&nRegFailed] { ++nRegFailed; }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800446
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000447 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800448 BOOST_CHECK_EQUAL(nRegFailed, 0);
449
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000450 advanceClocks(time::milliseconds(2000), 5);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800451 BOOST_CHECK_EQUAL(nRegFailed, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400452}
453
Joao Pereira0b3cac52015-07-02 14:49:49 -0400454BOOST_FIXTURE_TEST_CASE(SetInterestFilterFailWithoutSuccessCallback, FacesNoRegistrationReplyFixture)
455{
456 // don't enable registration reply
457 size_t nRegFailed = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800458 face.setInterestFilter("/Hello/World",
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000459 bind([] { BOOST_FAIL("Unexpected Interest"); }),
460 bind([&nRegFailed] { ++nRegFailed; }));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400461
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000462 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400463 BOOST_CHECK_EQUAL(nRegFailed, 0);
464
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000465 advanceClocks(time::milliseconds(2000), 5);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400466 BOOST_CHECK_EQUAL(nRegFailed, 1);
467}
468
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400469BOOST_AUTO_TEST_CASE(RegisterUnregisterPrefix)
470{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800471 size_t nRegSuccesses = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400472 const RegisteredPrefixId* regPrefixId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800473 face.registerPrefix("/Hello/World",
474 bind([&nRegSuccesses] { ++nRegSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000475 bind([] { BOOST_FAIL("Unexpected registerPrefix failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400476
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000477 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400478 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
479
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800480 size_t nUnregSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800481 face.unregisterPrefix(regPrefixId,
482 bind([&nUnregSuccesses] { ++nUnregSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000483 bind([] { BOOST_FAIL("Unexpected unregisterPrefix failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400484
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000485 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400486 BOOST_CHECK_EQUAL(nUnregSuccesses, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400487}
488
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800489BOOST_FIXTURE_TEST_CASE(RegisterUnregisterPrefixFail, FacesNoRegistrationReplyFixture)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400490{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800491 size_t nRegFailures = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800492 face.registerPrefix("/Hello/World",
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000493 bind([] { BOOST_FAIL("Unexpected registerPrefix success"); }),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800494 bind([&nRegFailures] { ++nRegFailures; }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400495
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000496 advanceClocks(time::milliseconds(5000), 20);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800497 BOOST_CHECK_EQUAL(nRegFailures, 1);
498}
499
500BOOST_AUTO_TEST_CASE(SimilarFilters)
501{
502 size_t nInInterests1 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800503 face.setInterestFilter("/Hello/World",
504 bind([&nInInterests1] { ++nInInterests1; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000505 nullptr,
506 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800507
508 size_t nInInterests2 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800509 face.setInterestFilter("/Hello",
510 bind([&nInInterests2] { ++nInInterests2; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000511 nullptr,
512 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400513
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800514 size_t nInInterests3 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800515 face.setInterestFilter("/Los/Angeles/Lakers",
516 bind([&nInInterests3] { ++nInInterests3; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000517 nullptr,
518 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400519
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000520 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400521
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000522 face.receive(Interest("/Hello/World/%21"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000523 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400524
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800525 BOOST_CHECK_EQUAL(nInInterests1, 1);
526 BOOST_CHECK_EQUAL(nInInterests2, 1);
527 BOOST_CHECK_EQUAL(nInInterests3, 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400528}
529
530BOOST_AUTO_TEST_CASE(SetRegexFilterError)
531{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800532 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
533 [] (const Name&, const Interest&) {
534 BOOST_FAIL("InterestFilter::Error should have been triggered");
535 },
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000536 nullptr,
537 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400538
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000539 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400540
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800541 BOOST_REQUIRE_THROW(face.receive(Interest("/Hello/World/XXX/b/c")), InterestFilter::Error);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400542}
543
544BOOST_AUTO_TEST_CASE(SetRegexFilter)
545{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800546 size_t nInInterests = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800547 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
548 bind([&nInInterests] { ++nInInterests; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000549 nullptr,
550 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400551
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000552 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400553
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800554 face.receive(Interest("/Hello/World/a")); // shouldn't match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400555 BOOST_CHECK_EQUAL(nInInterests, 0);
556
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800557 face.receive(Interest("/Hello/World/a/b")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400558 BOOST_CHECK_EQUAL(nInInterests, 1);
559
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800560 face.receive(Interest("/Hello/World/a/b/c")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400561 BOOST_CHECK_EQUAL(nInInterests, 2);
562
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800563 face.receive(Interest("/Hello/World/a/b/d")); // should not match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400564 BOOST_CHECK_EQUAL(nInInterests, 2);
565}
566
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400567BOOST_AUTO_TEST_CASE(SetRegexFilterAndRegister)
568{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800569 size_t nInInterests = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800570 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
571 bind([&nInInterests] { ++nInInterests; }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400572
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800573 size_t nRegSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800574 face.registerPrefix("/Hello/World",
575 bind([&nRegSuccesses] { ++nRegSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000576 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400577
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000578 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400579 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
580
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800581 face.receive(Interest("/Hello/World/a")); // shouldn't match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400582 BOOST_CHECK_EQUAL(nInInterests, 0);
583
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800584 face.receive(Interest("/Hello/World/a/b")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400585 BOOST_CHECK_EQUAL(nInInterests, 1);
586
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800587 face.receive(Interest("/Hello/World/a/b/c")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400588 BOOST_CHECK_EQUAL(nInInterests, 2);
589
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800590 face.receive(Interest("/Hello/World/a/b/d")); // should not match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400591 BOOST_CHECK_EQUAL(nInInterests, 2);
592}
593
Junxiao Shia1ea5062014-12-27 22:33:39 -0700594BOOST_FIXTURE_TEST_CASE(SetInterestFilterNoReg, FacesNoRegistrationReplyFixture) // Bug 2318
595{
596 // This behavior is specific to DummyClientFace.
597 // Regular Face won't accept incoming packets until something is sent.
598
599 int hit = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800600 face.setInterestFilter(Name("/"), bind([&hit] { ++hit; }));
601 face.processEvents(time::milliseconds(-1));
Junxiao Shia1ea5062014-12-27 22:33:39 -0700602
603 auto interest = make_shared<Interest>("/A");
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800604 face.receive(*interest);
605 face.processEvents(time::milliseconds(-1));
Junxiao Shia1ea5062014-12-27 22:33:39 -0700606
607 BOOST_CHECK_EQUAL(hit, 1);
608}
609
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000610BOOST_AUTO_TEST_SUITE_END() // Producer
611
Junxiao Shiae0b4182016-08-08 22:53:17 +0000612BOOST_AUTO_TEST_SUITE(IoRoutines)
613
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500614BOOST_AUTO_TEST_CASE(ProcessEvents)
615{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800616 face.processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500617
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800618 size_t nRegSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800619 face.registerPrefix("/Hello/World",
620 bind([&nRegSuccesses] { ++nRegSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000621 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500622
623 // io_service::poll() without reset
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800624 face.getIoService().poll();
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500625 BOOST_CHECK_EQUAL(nRegSuccesses, 0);
626
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800627 face.processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500628 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
629}
630
Junxiao Shiae0b4182016-08-08 22:53:17 +0000631BOOST_AUTO_TEST_CASE(DestroyWithoutProcessEvents) // Bug 3248
632{
633 auto face2 = make_unique<Face>(io);
634 face2.reset();
635
636 io.poll(); // should not crash
637}
638
639BOOST_AUTO_TEST_SUITE_END() // IoRoutines
640
641BOOST_AUTO_TEST_SUITE(Transport)
642
643using ndn::Transport;
644
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700645struct PibDirWithDefaultTpm
646{
647 const std::string PATH = "build/keys-with-default-tpm";
648};
649
650BOOST_FIXTURE_TEST_CASE(FaceTransport, PibDirFixture<PibDirWithDefaultTpm>)
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800651{
652 KeyChain keyChain;
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700653 boost::asio::io_service io;
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800654
655 BOOST_CHECK(Face().getTransport() != nullptr);
656
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800657 BOOST_CHECK(Face(shared_ptr<Transport>()).getTransport() != nullptr);
658 BOOST_CHECK(Face(shared_ptr<Transport>(), io).getTransport() != nullptr);
659 BOOST_CHECK(Face(shared_ptr<Transport>(), io, keyChain).getTransport() != nullptr);
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800660
661 auto transport = make_shared<TcpTransport>("localhost", "6363"); // no real io operations will be scheduled
662 BOOST_CHECK(Face(transport).getTransport() == transport);
663 BOOST_CHECK(Face(transport, io).getTransport() == transport);
664 BOOST_CHECK(Face(transport, io, keyChain).getTransport() == transport);
665}
666
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700667class WithEnv : private IdentityManagementTimeFixture
668{
669public:
670 WithEnv()
671 {
672 if (getenv("NDN_CLIENT_TRANSPORT") != nullptr) {
673 m_oldTransport = getenv("NDN_CLIENT_TRANSPORT");
674 unsetenv("NDN_CLIENT_TRANSPORT");
675 }
676 }
677
678 void
679 configure(const std::string& faceUri)
680 {
681 setenv("NDN_CLIENT_TRANSPORT", faceUri.c_str(), true);
682 }
683
684 ~WithEnv()
685 {
686 if (!m_oldTransport.empty()) {
687 setenv("NDN_CLIENT_TRANSPORT", m_oldTransport.c_str(), true);
688 }
689 else {
690 unsetenv("NDN_CLIENT_TRANSPORT");
691 }
692 }
693
694private:
695 std::string m_oldTransport;
696};
697
698class WithConfig : private TestHomeFixture<DefaultPibDir>
699{
700public:
701 void
702 configure(const std::string& faceUri)
703 {
704 createClientConf({"transport=" + faceUri});
705 }
706};
707
708class WithEnvAndConfig : public WithEnv, public WithConfig
709{
710};
711
712typedef boost::mpl::vector<WithEnv, WithConfig> ConfigOptions;
713
714BOOST_FIXTURE_TEST_CASE(NoConfig, WithEnvAndConfig) // fixture configures test HOME and PIB/TPM path
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700715{
716 shared_ptr<Face> face;
717 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
718 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700719}
720
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700721BOOST_FIXTURE_TEST_CASE_TEMPLATE(Unix, T, ConfigOptions, T)
722{
723 this->configure("unix://some/path");
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700724
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700725 shared_ptr<Face> face;
726 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
727 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
728}
729
730BOOST_FIXTURE_TEST_CASE_TEMPLATE(Tcp, T, ConfigOptions, T)
731{
732 this->configure("tcp://127.0.0.1:6000");
733
734 shared_ptr<Face> face;
735 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
736 BOOST_CHECK(dynamic_pointer_cast<TcpTransport>(face->getTransport()) != nullptr);
737}
738
739BOOST_FIXTURE_TEST_CASE_TEMPLATE(WrongTransport, T, ConfigOptions, T)
740{
741 this->configure("wrong-transport:");
742
743 BOOST_CHECK_THROW(make_shared<Face>(), ConfigFile::Error);
744}
745
746BOOST_FIXTURE_TEST_CASE_TEMPLATE(WrongUri, T, ConfigOptions, T)
747{
748 this->configure("wrong-uri");
749
750 BOOST_CHECK_THROW(make_shared<Face>(), ConfigFile::Error);
751}
752
753BOOST_FIXTURE_TEST_CASE(EnvOverride, WithEnvAndConfig)
754{
755 this->WithEnv::configure("tcp://127.0.0.1:6000");
756 this->WithConfig::configure("unix://some/path");
757
758 shared_ptr<Face> face;
759 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
760 BOOST_CHECK(dynamic_pointer_cast<TcpTransport>(face->getTransport()) != nullptr);
761}
762
763BOOST_FIXTURE_TEST_CASE(ExplicitTransport, WithEnvAndConfig)
764{
765 this->WithEnv::configure("wrong-uri");
766 this->WithConfig::configure("wrong-transport:");
767
768 auto transport = make_shared<UnixTransport>("unix://some/path");
769 shared_ptr<Face> face;
770 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>(transport));
771 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
772}
773
Junxiao Shiae0b4182016-08-08 22:53:17 +0000774BOOST_AUTO_TEST_SUITE_END() // Transport
775
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700776BOOST_AUTO_TEST_SUITE_END() // TestFace
777
778} // namespace tests
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400779} // namespace ndn