blob: 6e81644319a77a7dfafa55ea2bbbc1000a6db791 [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"
23#include "util/scheduler.hpp"
24#include "security/key-chain.hpp"
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080025#include "util/dummy-client-face.hpp"
Alexander Afanasyev3a6da362015-12-29 20:31:03 -080026#include "transport/tcp-transport.hpp"
Alexander Afanasyev57e00362016-06-23 13:22:54 -070027#include "transport/unix-transport.hpp"
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040028
29#include "boost-test.hpp"
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070030#include "identity-management-time-fixture.hpp"
31#include "key-chain-fixture.hpp"
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080032#include "make-interest-data.hpp"
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040033
34namespace ndn {
35namespace tests {
36
Junxiao Shia60d9362014-11-12 09:38:21 -070037using ndn::util::DummyClientFace;
Junxiao Shia60d9362014-11-12 09:38:21 -070038
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070039class FaceFixture : public IdentityManagementTimeFixture
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040040{
41public:
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080042 explicit
Junxiao Shia1ea5062014-12-27 22:33:39 -070043 FaceFixture(bool enableRegistrationReply = true)
Junxiao Shif5b5ae22016-08-08 05:54:41 +000044 : face(io, m_keyChain, {true, enableRegistrationReply})
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040045 {
46 }
47
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080048public:
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080049 DummyClientFace face;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040050};
51
Junxiao Shia1ea5062014-12-27 22:33:39 -070052class FacesNoRegistrationReplyFixture : public FaceFixture
Junxiao Shia60d9362014-11-12 09:38:21 -070053{
54public:
55 FacesNoRegistrationReplyFixture()
Junxiao Shia1ea5062014-12-27 22:33:39 -070056 : FaceFixture(false)
Junxiao Shia60d9362014-11-12 09:38:21 -070057 {
58 }
59};
60
Junxiao Shia1ea5062014-12-27 22:33:39 -070061BOOST_FIXTURE_TEST_SUITE(TestFace, FaceFixture)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040062
Junxiao Shi103d8ed2016-08-07 20:34:10 +000063BOOST_AUTO_TEST_SUITE(Consumer)
64
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040065BOOST_AUTO_TEST_CASE(ExpressInterestData)
66{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080067 size_t nData = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080068 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
69 [&] (const Interest& i, const Data& d) {
70 BOOST_CHECK(i.getName().isPrefixOf(d.getName()));
71 BOOST_CHECK_EQUAL(i.getName(), "/Hello/World");
72 ++nData;
73 },
Junxiao Shi103d8ed2016-08-07 20:34:10 +000074 bind([] { BOOST_FAIL("Unexpected Nack"); }),
75 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Eric Newberry83872fd2015-08-06 17:01:24 -070076
Junxiao Shif5b5ae22016-08-08 05:54:41 +000077 advanceClocks(time::milliseconds(40));
Eric Newberry83872fd2015-08-06 17:01:24 -070078
Junxiao Shi85d90832016-08-04 03:19:46 +000079 face.receive(*makeData("/Bye/World/a"));
80 face.receive(*makeData("/Hello/World/a"));
Eric Newberry83872fd2015-08-06 17:01:24 -070081
Junxiao Shif5b5ae22016-08-08 05:54:41 +000082 advanceClocks(time::milliseconds(50), 2);
Eric Newberry83872fd2015-08-06 17:01:24 -070083
84 BOOST_CHECK_EQUAL(nData, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080085 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
86 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Eric Newberry83872fd2015-08-06 17:01:24 -070087
88 size_t nTimeouts = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080089 face.expressInterest(Interest("/Hello/World/a/2", time::milliseconds(50)),
90 bind([]{}),
91 bind([]{}),
Junxiao Shi103d8ed2016-08-07 20:34:10 +000092 bind([&nTimeouts] { ++nTimeouts; }));
Junxiao Shif5b5ae22016-08-08 05:54:41 +000093 advanceClocks(time::milliseconds(200), 5);
Eric Newberry83872fd2015-08-06 17:01:24 -070094 BOOST_CHECK_EQUAL(nTimeouts, 1);
95}
96
Junxiao Shi76e0eb22016-08-08 05:54:10 +000097BOOST_AUTO_TEST_CASE(ExpressInterestEmptyDataCallback)
98{
99 face.expressInterest(Interest("/Hello/World"),
100 nullptr,
101 bind([] { BOOST_FAIL("Unexpected Nack"); }),
102 bind([] { BOOST_FAIL("Unexpected timeout"); }));
103 advanceClocks(time::milliseconds(1));
104
105 BOOST_CHECK_NO_THROW(do {
106 face.receive(*makeData("/Hello/World/a"));
107 advanceClocks(time::milliseconds(1));
108 } while (false));
109}
110
Eric Newberry83872fd2015-08-06 17:01:24 -0700111BOOST_AUTO_TEST_CASE(DeprecatedExpressInterestData)
112{
113 size_t nData = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800114 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000115 [&] (const Interest& i, Data& d) {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800116 BOOST_CHECK(i.getName().isPrefixOf(d.getName()));
117 ++nData;
118 },
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000119 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400120
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000121 advanceClocks(time::milliseconds(40));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400122
Junxiao Shi85d90832016-08-04 03:19:46 +0000123 face.receive(*makeData("/Bye/World/a"));
124 face.receive(*makeData("/Hello/World/a"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800125
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000126 advanceClocks(time::milliseconds(50), 2);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400127
128 BOOST_CHECK_EQUAL(nData, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800129 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
130 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800131
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800132 face.expressInterest(Interest("/Hello/World/a", time::milliseconds(50)),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000133 [&] (const Interest& i, Data& d) {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800134 BOOST_CHECK(i.getName().isPrefixOf(d.getName()));
135 ++nData;
136 },
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000137 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000138 advanceClocks(time::milliseconds(40));
Junxiao Shi85d90832016-08-04 03:19:46 +0000139 face.receive(*makeData("/Hello/World/a/1/xxxxx"));
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800140
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000141 advanceClocks(time::milliseconds(50), 2);
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800142
143 BOOST_CHECK_EQUAL(nData, 2);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800144 BOOST_CHECK_EQUAL(face.sentInterests.size(), 2);
145 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400146}
147
148BOOST_AUTO_TEST_CASE(ExpressInterestTimeout)
149{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800150 size_t nTimeouts = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800151 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000152 bind([] { BOOST_FAIL("Unexpected Data"); }),
153 bind([] { BOOST_FAIL("Unexpected Nack"); }),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800154 [&nTimeouts] (const Interest& i) {
155 BOOST_CHECK_EQUAL(i.getName(), "/Hello/World");
156 ++nTimeouts;
157 });
Eric Newberry83872fd2015-08-06 17:01:24 -0700158
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000159 advanceClocks(time::milliseconds(200), 5);
Eric Newberry83872fd2015-08-06 17:01:24 -0700160
161 BOOST_CHECK_EQUAL(nTimeouts, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800162 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
163 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
164 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0);
Eric Newberry83872fd2015-08-06 17:01:24 -0700165}
166
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000167BOOST_AUTO_TEST_CASE(ExpressInterestEmptyTimeoutCallback)
168{
169 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
170 bind([] { BOOST_FAIL("Unexpected Data"); }),
171 bind([] { BOOST_FAIL("Unexpected Nack"); }),
172 nullptr);
173 advanceClocks(time::milliseconds(40));
174
175 BOOST_CHECK_NO_THROW(do {
176 advanceClocks(time::milliseconds(6), 2);
177 } while (false));
178}
179
Eric Newberry83872fd2015-08-06 17:01:24 -0700180BOOST_AUTO_TEST_CASE(DeprecatedExpressInterestTimeout)
181{
182 size_t nTimeouts = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800183 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000184 bind([] { BOOST_FAIL("Unexpected data"); }),
185 bind([&nTimeouts] { ++nTimeouts; }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400186
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000187 advanceClocks(time::milliseconds(200), 5);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400188
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400189 BOOST_CHECK_EQUAL(nTimeouts, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800190 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
191 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400192}
193
Eric Newberry83872fd2015-08-06 17:01:24 -0700194BOOST_AUTO_TEST_CASE(ExpressInterestNack)
195{
196 size_t nNacks = 0;
197
198 Interest interest("/Hello/World", time::milliseconds(50));
199
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800200 face.expressInterest(interest,
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000201 bind([] { BOOST_FAIL("Unexpected Data"); }),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800202 [&] (const Interest& i, const lp::Nack& n) {
203 BOOST_CHECK(i.getName().isPrefixOf(n.getInterest().getName()));
204 BOOST_CHECK_EQUAL(i.getName(), "/Hello/World");
205 BOOST_CHECK_EQUAL(n.getReason(), lp::NackReason::DUPLICATE);
206 ++nNacks;
207 },
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000208 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Eric Newberry83872fd2015-08-06 17:01:24 -0700209
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000210 advanceClocks(time::milliseconds(40));
Eric Newberry83872fd2015-08-06 17:01:24 -0700211
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000212 face.receive(makeNack(face.sentInterests.at(0), lp::NackReason::DUPLICATE));
Eric Newberry83872fd2015-08-06 17:01:24 -0700213
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000214 advanceClocks(time::milliseconds(50), 2);
Eric Newberry83872fd2015-08-06 17:01:24 -0700215
216 BOOST_CHECK_EQUAL(nNacks, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800217 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
Eric Newberry83872fd2015-08-06 17:01:24 -0700218}
219
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000220BOOST_AUTO_TEST_CASE(ExpressInterestEmptyNackCallback)
221{
222 face.expressInterest(Interest("/Hello/World"),
223 bind([] { BOOST_FAIL("Unexpected Data"); }),
224 nullptr,
225 bind([] { BOOST_FAIL("Unexpected timeout"); }));
226 advanceClocks(time::milliseconds(1));
227
228 BOOST_CHECK_NO_THROW(do {
229 face.receive(makeNack(face.sentInterests.at(0), lp::NackReason::DUPLICATE));
230 advanceClocks(time::milliseconds(1));
231 } while (false));
232}
233
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000234BOOST_AUTO_TEST_CASE(DeprecatedExpressInterestNack)
235{
236 size_t nTimeouts = 0;
237 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
238 bind([] { BOOST_FAIL("Unexpected data"); }),
239 bind([&nTimeouts] { ++nTimeouts; }));
240 advanceClocks(time::milliseconds(1));
241
242 face.receive(makeNack(face.sentInterests.at(0), lp::NackReason::CONGESTION));
243 advanceClocks(time::milliseconds(1));
244
245 BOOST_CHECK_EQUAL(nTimeouts, 1);
246 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
247 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
248}
249
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800250BOOST_AUTO_TEST_CASE(RemovePendingInterest)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400251{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800252 const PendingInterestId* interestId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800253 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000254 bind([] { BOOST_FAIL("Unexpected data"); }),
255 bind([] { BOOST_FAIL("Unexpected nack"); }),
256 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800257 advanceClocks(time::milliseconds(10));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400258
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800259 face.removePendingInterest(interestId);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800260 advanceClocks(time::milliseconds(10));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400261
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000262 face.receive(*makeData("/Hello/World/%21"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000263 advanceClocks(time::milliseconds(200), 5);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400264}
265
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000266BOOST_AUTO_TEST_CASE(RemoveAllPendingInterests)
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500267{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800268 face.expressInterest(Interest("/Hello/World/0", time::milliseconds(50)),
269 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
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800273 face.expressInterest(Interest("/Hello/World/1", time::milliseconds(50)),
274 bind([] { BOOST_FAIL("Unexpected data"); }),
275 bind([] { BOOST_FAIL("Unexpected nack"); }),
276 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500277
278 advanceClocks(time::milliseconds(10));
279
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800280 face.removeAllPendingInterests();
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500281 advanceClocks(time::milliseconds(10));
282
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800283 BOOST_CHECK_EQUAL(face.getNPendingInterests(), 0);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500284
Junxiao Shi85d90832016-08-04 03:19:46 +0000285 face.receive(*makeData("/Hello/World/0"));
286 face.receive(*makeData("/Hello/World/1"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000287 advanceClocks(time::milliseconds(200), 5);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500288}
289
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000290BOOST_AUTO_TEST_CASE(DestructionWithoutCancellingPendingInterests) // Bug #2518
291{
292 {
293 DummyClientFace face2(io, m_keyChain);
294 face2.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
295 bind([]{}), bind([]{}));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000296 advanceClocks(time::milliseconds(50), 2);
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000297 }
298
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000299 advanceClocks(time::milliseconds(50), 2);
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000300 // should not segfault
301}
302
303BOOST_AUTO_TEST_SUITE_END() // Consumer
304
305BOOST_AUTO_TEST_SUITE(Producer)
306
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000307BOOST_AUTO_TEST_CASE(PutData)
308{
309 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
310
311 Data data("/4g7xxcuEow/KFvK5Kf2m");
312 signData(data);
313 face.put(data);
314
315 lp::CachePolicy cachePolicy;
316 cachePolicy.setPolicy(lp::CachePolicyType::NO_CACHE);
317 data.setTag(make_shared<lp::CachePolicyTag>(cachePolicy));
318 face.put(data);
319
320 advanceClocks(time::milliseconds(10));
321 BOOST_REQUIRE_EQUAL(face.sentData.size(), 2);
322 BOOST_CHECK(face.sentData[0].getTag<lp::CachePolicyTag>() == nullptr);
323 BOOST_CHECK(face.sentData[1].getTag<lp::CachePolicyTag>() != nullptr);
324}
325
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000326BOOST_AUTO_TEST_CASE(PutNack)
327{
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000328 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0);
329
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000330 face.put(makeNack(Interest("/Hello/World", time::milliseconds(50)), lp::NackReason::NO_ROUTE));
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000331
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000332 advanceClocks(time::milliseconds(10));
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000333 BOOST_CHECK_EQUAL(face.sentNacks.size(), 1);
334}
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500335
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400336BOOST_AUTO_TEST_CASE(SetUnsetInterestFilter)
337{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800338 size_t nInterests = 0;
339 size_t nRegs = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400340 const RegisteredPrefixId* regPrefixId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800341 face.setInterestFilter("/Hello/World",
342 bind([&nInterests] { ++nInterests; }),
343 bind([&nRegs] { ++nRegs; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000344 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000345 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800346 BOOST_CHECK_EQUAL(nRegs, 1);
347 BOOST_CHECK_EQUAL(nInterests, 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400348
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000349 face.receive(Interest("/Hello/World/%21"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000350 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400351
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800352 BOOST_CHECK_EQUAL(nRegs, 1);
353 BOOST_CHECK_EQUAL(nInterests, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400354
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000355 face.receive(Interest("/Bye/World/%21"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800356 advanceClocks(time::milliseconds(10000), 10);
357 BOOST_CHECK_EQUAL(nInterests, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400358
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000359 face.receive(Interest("/Hello/World/%21/2"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000360 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800361 BOOST_CHECK_EQUAL(nInterests, 2);
362
363 // removing filter
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800364 face.unsetInterestFilter(regPrefixId);
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000365 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400366
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000367 face.receive(Interest("/Hello/World/%21/3"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800368 BOOST_CHECK_EQUAL(nInterests, 2);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400369
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000370 face.unsetInterestFilter(static_cast<const RegisteredPrefixId*>(nullptr));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000371 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400372
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000373 face.unsetInterestFilter(static_cast<const InterestFilterId*>(nullptr));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000374 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800375}
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400376
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000377BOOST_AUTO_TEST_CASE(SetInterestFilterEmptyInterestCallback)
378{
379 face.setInterestFilter("/A", nullptr);
380 advanceClocks(time::milliseconds(1));
381
382 BOOST_CHECK_NO_THROW(do {
383 face.receive(*makeInterest("/A/1"));
384 advanceClocks(time::milliseconds(1));
385 } while (false));
386}
387
Joao Pereira0b3cac52015-07-02 14:49:49 -0400388BOOST_AUTO_TEST_CASE(SetUnsetInterestFilterWithoutSucessCallback)
389{
390 size_t nInterests = 0;
391 const RegisteredPrefixId* regPrefixId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800392 face.setInterestFilter("/Hello/World",
393 bind([&nInterests] { ++nInterests; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000394 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000395 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400396 BOOST_CHECK_EQUAL(nInterests, 0);
397
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000398 face.receive(Interest("/Hello/World/%21"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000399 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400400
401 BOOST_CHECK_EQUAL(nInterests, 1);
402
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000403 face.receive(Interest("/Bye/World/%21"));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400404 advanceClocks(time::milliseconds(10000), 10);
405 BOOST_CHECK_EQUAL(nInterests, 1);
406
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000407 face.receive(Interest("/Hello/World/%21/2"));
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, 2);
410
411 // removing filter
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800412 face.unsetInterestFilter(regPrefixId);
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000413 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400414
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000415 face.receive(Interest("/Hello/World/%21/3"));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400416 BOOST_CHECK_EQUAL(nInterests, 2);
417
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000418 face.unsetInterestFilter(static_cast<const RegisteredPrefixId*>(nullptr));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000419 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400420
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000421 face.unsetInterestFilter(static_cast<const InterestFilterId*>(nullptr));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000422 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400423}
424
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800425BOOST_FIXTURE_TEST_CASE(SetInterestFilterFail, FacesNoRegistrationReplyFixture)
426{
427 // don't enable registration reply
428 size_t nRegFailed = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800429 face.setInterestFilter("/Hello/World",
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000430 bind([] { BOOST_FAIL("Unexpected Interest"); }),
431 bind([] { BOOST_FAIL("Unexpected success of setInterestFilter"); }),
432 bind([&nRegFailed] { ++nRegFailed; }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800433
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000434 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800435 BOOST_CHECK_EQUAL(nRegFailed, 0);
436
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000437 advanceClocks(time::milliseconds(2000), 5);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800438 BOOST_CHECK_EQUAL(nRegFailed, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400439}
440
Joao Pereira0b3cac52015-07-02 14:49:49 -0400441BOOST_FIXTURE_TEST_CASE(SetInterestFilterFailWithoutSuccessCallback, FacesNoRegistrationReplyFixture)
442{
443 // don't enable registration reply
444 size_t nRegFailed = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800445 face.setInterestFilter("/Hello/World",
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000446 bind([] { BOOST_FAIL("Unexpected Interest"); }),
447 bind([&nRegFailed] { ++nRegFailed; }));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400448
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000449 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400450 BOOST_CHECK_EQUAL(nRegFailed, 0);
451
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000452 advanceClocks(time::milliseconds(2000), 5);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400453 BOOST_CHECK_EQUAL(nRegFailed, 1);
454}
455
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400456BOOST_AUTO_TEST_CASE(RegisterUnregisterPrefix)
457{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800458 size_t nRegSuccesses = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400459 const RegisteredPrefixId* regPrefixId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800460 face.registerPrefix("/Hello/World",
461 bind([&nRegSuccesses] { ++nRegSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000462 bind([] { BOOST_FAIL("Unexpected registerPrefix failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400463
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000464 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400465 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
466
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800467 size_t nUnregSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800468 face.unregisterPrefix(regPrefixId,
469 bind([&nUnregSuccesses] { ++nUnregSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000470 bind([] { BOOST_FAIL("Unexpected unregisterPrefix failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400471
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000472 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400473 BOOST_CHECK_EQUAL(nUnregSuccesses, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400474}
475
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800476BOOST_FIXTURE_TEST_CASE(RegisterUnregisterPrefixFail, FacesNoRegistrationReplyFixture)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400477{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800478 size_t nRegFailures = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800479 face.registerPrefix("/Hello/World",
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000480 bind([] { BOOST_FAIL("Unexpected registerPrefix success"); }),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800481 bind([&nRegFailures] { ++nRegFailures; }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400482
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000483 advanceClocks(time::milliseconds(5000), 20);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800484 BOOST_CHECK_EQUAL(nRegFailures, 1);
485}
486
487BOOST_AUTO_TEST_CASE(SimilarFilters)
488{
489 size_t nInInterests1 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800490 face.setInterestFilter("/Hello/World",
491 bind([&nInInterests1] { ++nInInterests1; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000492 nullptr,
493 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800494
495 size_t nInInterests2 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800496 face.setInterestFilter("/Hello",
497 bind([&nInInterests2] { ++nInInterests2; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000498 nullptr,
499 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400500
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800501 size_t nInInterests3 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800502 face.setInterestFilter("/Los/Angeles/Lakers",
503 bind([&nInInterests3] { ++nInInterests3; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000504 nullptr,
505 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400506
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000507 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400508
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000509 face.receive(Interest("/Hello/World/%21"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000510 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400511
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800512 BOOST_CHECK_EQUAL(nInInterests1, 1);
513 BOOST_CHECK_EQUAL(nInInterests2, 1);
514 BOOST_CHECK_EQUAL(nInInterests3, 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400515}
516
517BOOST_AUTO_TEST_CASE(SetRegexFilterError)
518{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800519 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
520 [] (const Name&, const Interest&) {
521 BOOST_FAIL("InterestFilter::Error should have been triggered");
522 },
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000523 nullptr,
524 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400525
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000526 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400527
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800528 BOOST_REQUIRE_THROW(face.receive(Interest("/Hello/World/XXX/b/c")), InterestFilter::Error);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400529}
530
531BOOST_AUTO_TEST_CASE(SetRegexFilter)
532{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800533 size_t nInInterests = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800534 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
535 bind([&nInInterests] { ++nInInterests; }),
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 face.receive(Interest("/Hello/World/a")); // shouldn't match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400542 BOOST_CHECK_EQUAL(nInInterests, 0);
543
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800544 face.receive(Interest("/Hello/World/a/b")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400545 BOOST_CHECK_EQUAL(nInInterests, 1);
546
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800547 face.receive(Interest("/Hello/World/a/b/c")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400548 BOOST_CHECK_EQUAL(nInInterests, 2);
549
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800550 face.receive(Interest("/Hello/World/a/b/d")); // should not match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400551 BOOST_CHECK_EQUAL(nInInterests, 2);
552}
553
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400554BOOST_AUTO_TEST_CASE(SetRegexFilterAndRegister)
555{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800556 size_t nInInterests = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800557 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
558 bind([&nInInterests] { ++nInInterests; }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400559
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800560 size_t nRegSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800561 face.registerPrefix("/Hello/World",
562 bind([&nRegSuccesses] { ++nRegSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000563 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400564
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000565 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400566 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
567
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800568 face.receive(Interest("/Hello/World/a")); // shouldn't match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400569 BOOST_CHECK_EQUAL(nInInterests, 0);
570
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800571 face.receive(Interest("/Hello/World/a/b")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400572 BOOST_CHECK_EQUAL(nInInterests, 1);
573
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800574 face.receive(Interest("/Hello/World/a/b/c")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400575 BOOST_CHECK_EQUAL(nInInterests, 2);
576
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800577 face.receive(Interest("/Hello/World/a/b/d")); // should not match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400578 BOOST_CHECK_EQUAL(nInInterests, 2);
579}
580
Junxiao Shia1ea5062014-12-27 22:33:39 -0700581BOOST_FIXTURE_TEST_CASE(SetInterestFilterNoReg, FacesNoRegistrationReplyFixture) // Bug 2318
582{
583 // This behavior is specific to DummyClientFace.
584 // Regular Face won't accept incoming packets until something is sent.
585
586 int hit = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800587 face.setInterestFilter(Name("/"), bind([&hit] { ++hit; }));
588 face.processEvents(time::milliseconds(-1));
Junxiao Shia1ea5062014-12-27 22:33:39 -0700589
590 auto interest = make_shared<Interest>("/A");
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800591 face.receive(*interest);
592 face.processEvents(time::milliseconds(-1));
Junxiao Shia1ea5062014-12-27 22:33:39 -0700593
594 BOOST_CHECK_EQUAL(hit, 1);
595}
596
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000597BOOST_AUTO_TEST_SUITE_END() // Producer
598
Junxiao Shiae0b4182016-08-08 22:53:17 +0000599BOOST_AUTO_TEST_SUITE(IoRoutines)
600
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500601BOOST_AUTO_TEST_CASE(ProcessEvents)
602{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800603 face.processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500604
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800605 size_t nRegSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800606 face.registerPrefix("/Hello/World",
607 bind([&nRegSuccesses] { ++nRegSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000608 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500609
610 // io_service::poll() without reset
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800611 face.getIoService().poll();
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500612 BOOST_CHECK_EQUAL(nRegSuccesses, 0);
613
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800614 face.processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500615 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
616}
617
Junxiao Shiae0b4182016-08-08 22:53:17 +0000618BOOST_AUTO_TEST_CASE(DestroyWithoutProcessEvents) // Bug 3248
619{
620 auto face2 = make_unique<Face>(io);
621 face2.reset();
622
623 io.poll(); // should not crash
624}
625
626BOOST_AUTO_TEST_SUITE_END() // IoRoutines
627
628BOOST_AUTO_TEST_SUITE(Transport)
629
630using ndn::Transport;
631
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700632struct PibDirWithDefaultTpm
633{
634 const std::string PATH = "build/keys-with-default-tpm";
635};
636
637BOOST_FIXTURE_TEST_CASE(FaceTransport, PibDirFixture<PibDirWithDefaultTpm>)
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800638{
639 KeyChain keyChain;
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700640 boost::asio::io_service io;
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800641
642 BOOST_CHECK(Face().getTransport() != nullptr);
643
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800644 BOOST_CHECK(Face(shared_ptr<Transport>()).getTransport() != nullptr);
645 BOOST_CHECK(Face(shared_ptr<Transport>(), io).getTransport() != nullptr);
646 BOOST_CHECK(Face(shared_ptr<Transport>(), io, keyChain).getTransport() != nullptr);
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800647
648 auto transport = make_shared<TcpTransport>("localhost", "6363"); // no real io operations will be scheduled
649 BOOST_CHECK(Face(transport).getTransport() == transport);
650 BOOST_CHECK(Face(transport, io).getTransport() == transport);
651 BOOST_CHECK(Face(transport, io, keyChain).getTransport() == transport);
652}
653
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700654class WithEnv : private IdentityManagementTimeFixture
655{
656public:
657 WithEnv()
658 {
659 if (getenv("NDN_CLIENT_TRANSPORT") != nullptr) {
660 m_oldTransport = getenv("NDN_CLIENT_TRANSPORT");
661 unsetenv("NDN_CLIENT_TRANSPORT");
662 }
663 }
664
665 void
666 configure(const std::string& faceUri)
667 {
668 setenv("NDN_CLIENT_TRANSPORT", faceUri.c_str(), true);
669 }
670
671 ~WithEnv()
672 {
673 if (!m_oldTransport.empty()) {
674 setenv("NDN_CLIENT_TRANSPORT", m_oldTransport.c_str(), true);
675 }
676 else {
677 unsetenv("NDN_CLIENT_TRANSPORT");
678 }
679 }
680
681private:
682 std::string m_oldTransport;
683};
684
685class WithConfig : private TestHomeFixture<DefaultPibDir>
686{
687public:
688 void
689 configure(const std::string& faceUri)
690 {
691 createClientConf({"transport=" + faceUri});
692 }
693};
694
695class WithEnvAndConfig : public WithEnv, public WithConfig
696{
697};
698
699typedef boost::mpl::vector<WithEnv, WithConfig> ConfigOptions;
700
701BOOST_FIXTURE_TEST_CASE(NoConfig, WithEnvAndConfig) // fixture configures test HOME and PIB/TPM path
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700702{
703 shared_ptr<Face> face;
704 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
705 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700706}
707
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700708BOOST_FIXTURE_TEST_CASE_TEMPLATE(Unix, T, ConfigOptions, T)
709{
710 this->configure("unix://some/path");
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700711
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700712 shared_ptr<Face> face;
713 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
714 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
715}
716
717BOOST_FIXTURE_TEST_CASE_TEMPLATE(Tcp, T, ConfigOptions, T)
718{
719 this->configure("tcp://127.0.0.1:6000");
720
721 shared_ptr<Face> face;
722 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
723 BOOST_CHECK(dynamic_pointer_cast<TcpTransport>(face->getTransport()) != nullptr);
724}
725
726BOOST_FIXTURE_TEST_CASE_TEMPLATE(WrongTransport, T, ConfigOptions, T)
727{
728 this->configure("wrong-transport:");
729
730 BOOST_CHECK_THROW(make_shared<Face>(), ConfigFile::Error);
731}
732
733BOOST_FIXTURE_TEST_CASE_TEMPLATE(WrongUri, T, ConfigOptions, T)
734{
735 this->configure("wrong-uri");
736
737 BOOST_CHECK_THROW(make_shared<Face>(), ConfigFile::Error);
738}
739
740BOOST_FIXTURE_TEST_CASE(EnvOverride, WithEnvAndConfig)
741{
742 this->WithEnv::configure("tcp://127.0.0.1:6000");
743 this->WithConfig::configure("unix://some/path");
744
745 shared_ptr<Face> face;
746 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
747 BOOST_CHECK(dynamic_pointer_cast<TcpTransport>(face->getTransport()) != nullptr);
748}
749
750BOOST_FIXTURE_TEST_CASE(ExplicitTransport, WithEnvAndConfig)
751{
752 this->WithEnv::configure("wrong-uri");
753 this->WithConfig::configure("wrong-transport:");
754
755 auto transport = make_shared<UnixTransport>("unix://some/path");
756 shared_ptr<Face> face;
757 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>(transport));
758 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
759}
760
Junxiao Shiae0b4182016-08-08 22:53:17 +0000761BOOST_AUTO_TEST_SUITE_END() // Transport
762
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700763BOOST_AUTO_TEST_SUITE_END() // TestFace
764
765} // namespace tests
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400766} // namespace ndn