blob: 3f73f8a55ddfea1c7b7981ffea5bf0872f215f13 [file] [log] [blame]
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev1013fd02017-01-03 13:19:03 -08003 * Copyright (c) 2013-2017 Regents of the University of California.
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -04004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#include "face.hpp"
Junxiao Shia1478db2016-09-09 04:13:15 +000023#include "lp/tags.hpp"
Alexander Afanasyev3a6da362015-12-29 20:31:03 -080024#include "transport/tcp-transport.hpp"
Alexander Afanasyev57e00362016-06-23 13:22:54 -070025#include "transport/unix-transport.hpp"
Junxiao Shia1478db2016-09-09 04:13:15 +000026#include "util/dummy-client-face.hpp"
27#include "util/scheduler.hpp"
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040028
29#include "boost-test.hpp"
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070030#include "identity-management-time-fixture.hpp"
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080031#include "test-home-fixture.hpp"
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080032#include "make-interest-data.hpp"
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040033
34namespace ndn {
35namespace tests {
36
Junxiao Shia60d9362014-11-12 09:38:21 -070037using ndn::util::DummyClientFace;
Junxiao Shia60d9362014-11-12 09:38:21 -070038
Alexander Afanasyev70244f42017-01-04 12:47:12 -080039class FaceFixture : public IdentityManagementV1TimeFixture
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
Alexander Afanasyev1013fd02017-01-03 13:19:03 -080097BOOST_AUTO_TEST_CASE(ExpressMultipleInterestData)
98{
99 size_t nData = 0;
100
101 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
102 [&] (const Interest& i, const Data& d) {
103 ++nData;
104 },
105 bind([] { BOOST_FAIL("Unexpected Nack"); }),
106 bind([] { BOOST_FAIL("Unexpected timeout"); }));
107
108 face.expressInterest(Interest("/Hello/World/a", time::milliseconds(50)),
109 [&] (const Interest& i, const Data& d) {
110 ++nData;
111 },
112 bind([] { BOOST_FAIL("Unexpected Nack"); }),
113 bind([] { BOOST_FAIL("Unexpected timeout"); }));
114
115 advanceClocks(time::milliseconds(40));
116
117 face.receive(*makeData("/Hello/World/a/b"));
118
119 advanceClocks(time::milliseconds(50), 2);
120
121 BOOST_CHECK_EQUAL(nData, 2);
122 BOOST_CHECK_EQUAL(face.sentInterests.size(), 2);
123 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
124}
125
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000126BOOST_AUTO_TEST_CASE(ExpressInterestEmptyDataCallback)
127{
128 face.expressInterest(Interest("/Hello/World"),
129 nullptr,
130 bind([] { BOOST_FAIL("Unexpected Nack"); }),
131 bind([] { BOOST_FAIL("Unexpected timeout"); }));
132 advanceClocks(time::milliseconds(1));
133
134 BOOST_CHECK_NO_THROW(do {
135 face.receive(*makeData("/Hello/World/a"));
136 advanceClocks(time::milliseconds(1));
137 } 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;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800143 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
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
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000151 advanceClocks(time::milliseconds(200), 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{
161 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
162 bind([] { BOOST_FAIL("Unexpected Data"); }),
163 bind([] { BOOST_FAIL("Unexpected Nack"); }),
164 nullptr);
165 advanceClocks(time::milliseconds(40));
166
167 BOOST_CHECK_NO_THROW(do {
168 advanceClocks(time::milliseconds(6), 2);
169 } while (false));
170}
171
Eric Newberry83872fd2015-08-06 17:01:24 -0700172BOOST_AUTO_TEST_CASE(ExpressInterestNack)
173{
174 size_t nNacks = 0;
175
176 Interest interest("/Hello/World", time::milliseconds(50));
177
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800178 face.expressInterest(interest,
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000179 bind([] { BOOST_FAIL("Unexpected Data"); }),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800180 [&] (const Interest& i, const lp::Nack& n) {
181 BOOST_CHECK(i.getName().isPrefixOf(n.getInterest().getName()));
182 BOOST_CHECK_EQUAL(i.getName(), "/Hello/World");
183 BOOST_CHECK_EQUAL(n.getReason(), lp::NackReason::DUPLICATE);
184 ++nNacks;
185 },
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000186 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Eric Newberry83872fd2015-08-06 17:01:24 -0700187
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000188 advanceClocks(time::milliseconds(40));
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
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000192 advanceClocks(time::milliseconds(50), 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
202 Interest interest("/Hello/World", time::milliseconds(50));
203 interest.setNonce(1);
204
205 face.expressInterest(interest,
206 bind([] { BOOST_FAIL("Unexpected Data"); }),
207 [&] (const Interest& i, const lp::Nack& n) {
208 ++nNacks;
209 },
210 bind([] { BOOST_FAIL("Unexpected timeout"); }));
211
212 interest.setNonce(2);
213 face.expressInterest(interest,
214 bind([] { BOOST_FAIL("Unexpected Data"); }),
215 [&] (const Interest& i, const lp::Nack& n) {
216 ++nNacks;
217 },
218 bind([] { BOOST_FAIL("Unexpected timeout"); }));
219
220 advanceClocks(time::milliseconds(40));
221
222 face.receive(makeNack(face.sentInterests.at(1), lp::NackReason::DUPLICATE));
223
224 advanceClocks(time::milliseconds(50), 2);
225
226 BOOST_CHECK_EQUAL(nNacks, 2);
227 BOOST_CHECK_EQUAL(face.sentInterests.size(), 2);
228}
229
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000230BOOST_AUTO_TEST_CASE(ExpressInterestEmptyNackCallback)
231{
232 face.expressInterest(Interest("/Hello/World"),
233 bind([] { BOOST_FAIL("Unexpected Data"); }),
234 nullptr,
235 bind([] { BOOST_FAIL("Unexpected timeout"); }));
236 advanceClocks(time::milliseconds(1));
237
238 BOOST_CHECK_NO_THROW(do {
239 face.receive(makeNack(face.sentInterests.at(0), lp::NackReason::DUPLICATE));
240 advanceClocks(time::milliseconds(1));
241 } while (false));
242}
243
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800244BOOST_AUTO_TEST_CASE(RemovePendingInterest)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400245{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800246 const PendingInterestId* interestId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800247 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000248 bind([] { BOOST_FAIL("Unexpected data"); }),
249 bind([] { BOOST_FAIL("Unexpected nack"); }),
250 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800251 advanceClocks(time::milliseconds(10));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400252
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800253 face.removePendingInterest(interestId);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800254 advanceClocks(time::milliseconds(10));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400255
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000256 face.receive(*makeData("/Hello/World/%21"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000257 advanceClocks(time::milliseconds(200), 5);
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100258
259 // avoid "test case [...] did not check any assertions" message from Boost.Test
260 BOOST_CHECK(true);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400261}
262
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000263BOOST_AUTO_TEST_CASE(RemoveAllPendingInterests)
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500264{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800265 face.expressInterest(Interest("/Hello/World/0", time::milliseconds(50)),
266 bind([] { BOOST_FAIL("Unexpected data"); }),
267 bind([] { BOOST_FAIL("Unexpected nack"); }),
268 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500269
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800270 face.expressInterest(Interest("/Hello/World/1", time::milliseconds(50)),
271 bind([] { BOOST_FAIL("Unexpected data"); }),
272 bind([] { BOOST_FAIL("Unexpected nack"); }),
273 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500274
275 advanceClocks(time::milliseconds(10));
276
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800277 face.removeAllPendingInterests();
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500278 advanceClocks(time::milliseconds(10));
279
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800280 BOOST_CHECK_EQUAL(face.getNPendingInterests(), 0);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500281
Junxiao Shi85d90832016-08-04 03:19:46 +0000282 face.receive(*makeData("/Hello/World/0"));
283 face.receive(*makeData("/Hello/World/1"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000284 advanceClocks(time::milliseconds(200), 5);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500285}
286
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000287BOOST_AUTO_TEST_CASE(DestructionWithoutCancellingPendingInterests) // Bug #2518
288{
289 {
290 DummyClientFace face2(io, m_keyChain);
291 face2.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
Alexander Afanasyeve6835fe2017-01-19 20:05:01 -0800292 nullptr, nullptr, nullptr);
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000293 advanceClocks(time::milliseconds(50), 2);
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000294 }
295
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100296 advanceClocks(time::milliseconds(50), 2); // should not crash
297
298 // avoid "test case [...] did not check any assertions" message from Boost.Test
299 BOOST_CHECK(true);
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000300}
301
302BOOST_AUTO_TEST_SUITE_END() // Consumer
303
304BOOST_AUTO_TEST_SUITE(Producer)
305
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000306BOOST_AUTO_TEST_CASE(PutData)
307{
308 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
309
310 Data data("/4g7xxcuEow/KFvK5Kf2m");
311 signData(data);
312 face.put(data);
313
314 lp::CachePolicy cachePolicy;
315 cachePolicy.setPolicy(lp::CachePolicyType::NO_CACHE);
316 data.setTag(make_shared<lp::CachePolicyTag>(cachePolicy));
Eric Newberry4d261b62016-11-10 13:40:09 -0700317 data.setTag(make_shared<lp::CongestionMarkTag>(1));
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000318 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);
Eric Newberry4d261b62016-11-10 13:40:09 -0700323 BOOST_CHECK(face.sentData[0].getTag<lp::CongestionMarkTag>() == nullptr);
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000324 BOOST_CHECK(face.sentData[1].getTag<lp::CachePolicyTag>() != nullptr);
Eric Newberry4d261b62016-11-10 13:40:09 -0700325 BOOST_CHECK(face.sentData[1].getTag<lp::CongestionMarkTag>() != nullptr);
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000326}
327
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000328BOOST_AUTO_TEST_CASE(PutNack)
329{
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000330 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0);
331
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000332 face.put(makeNack(Interest("/Hello/World", time::milliseconds(50)), lp::NackReason::NO_ROUTE));
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000333
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000334 advanceClocks(time::milliseconds(10));
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000335 BOOST_CHECK_EQUAL(face.sentNacks.size(), 1);
Eric Newberry4d261b62016-11-10 13:40:09 -0700336
337 auto nack = makeNack(Interest("/another/prefix", time::milliseconds(50)), lp::NackReason::NO_ROUTE);
338 nack.setTag(make_shared<lp::CongestionMarkTag>(1));
339 face.put(nack);
340
341 advanceClocks(time::milliseconds(10));
342 BOOST_REQUIRE_EQUAL(face.sentNacks.size(), 2);
343 BOOST_CHECK(face.sentNacks[0].getTag<lp::CongestionMarkTag>() == nullptr);
344 BOOST_CHECK(face.sentNacks[1].getTag<lp::CongestionMarkTag>() != nullptr);
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000345}
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500346
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400347BOOST_AUTO_TEST_CASE(SetUnsetInterestFilter)
348{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800349 size_t nInterests = 0;
350 size_t nRegs = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400351 const RegisteredPrefixId* regPrefixId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800352 face.setInterestFilter("/Hello/World",
353 bind([&nInterests] { ++nInterests; }),
354 bind([&nRegs] { ++nRegs; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000355 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000356 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800357 BOOST_CHECK_EQUAL(nRegs, 1);
358 BOOST_CHECK_EQUAL(nInterests, 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400359
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000360 face.receive(Interest("/Hello/World/%21"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000361 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400362
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800363 BOOST_CHECK_EQUAL(nRegs, 1);
364 BOOST_CHECK_EQUAL(nInterests, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400365
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000366 face.receive(Interest("/Bye/World/%21"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800367 advanceClocks(time::milliseconds(10000), 10);
368 BOOST_CHECK_EQUAL(nInterests, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400369
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000370 face.receive(Interest("/Hello/World/%21/2"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000371 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800372 BOOST_CHECK_EQUAL(nInterests, 2);
373
374 // removing filter
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800375 face.unsetInterestFilter(regPrefixId);
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000376 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400377
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000378 face.receive(Interest("/Hello/World/%21/3"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800379 BOOST_CHECK_EQUAL(nInterests, 2);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400380
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000381 face.unsetInterestFilter(static_cast<const RegisteredPrefixId*>(nullptr));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000382 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400383
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000384 face.unsetInterestFilter(static_cast<const InterestFilterId*>(nullptr));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000385 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800386}
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400387
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000388BOOST_AUTO_TEST_CASE(SetInterestFilterEmptyInterestCallback)
389{
390 face.setInterestFilter("/A", nullptr);
391 advanceClocks(time::milliseconds(1));
392
393 BOOST_CHECK_NO_THROW(do {
394 face.receive(*makeInterest("/A/1"));
395 advanceClocks(time::milliseconds(1));
396 } while (false));
397}
398
Joao Pereira0b3cac52015-07-02 14:49:49 -0400399BOOST_AUTO_TEST_CASE(SetUnsetInterestFilterWithoutSucessCallback)
400{
401 size_t nInterests = 0;
402 const RegisteredPrefixId* regPrefixId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800403 face.setInterestFilter("/Hello/World",
404 bind([&nInterests] { ++nInterests; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000405 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000406 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400407 BOOST_CHECK_EQUAL(nInterests, 0);
408
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000409 face.receive(Interest("/Hello/World/%21"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000410 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400411
412 BOOST_CHECK_EQUAL(nInterests, 1);
413
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000414 face.receive(Interest("/Bye/World/%21"));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400415 advanceClocks(time::milliseconds(10000), 10);
416 BOOST_CHECK_EQUAL(nInterests, 1);
417
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000418 face.receive(Interest("/Hello/World/%21/2"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000419 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400420 BOOST_CHECK_EQUAL(nInterests, 2);
421
422 // removing filter
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800423 face.unsetInterestFilter(regPrefixId);
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000424 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400425
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000426 face.receive(Interest("/Hello/World/%21/3"));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400427 BOOST_CHECK_EQUAL(nInterests, 2);
428
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000429 face.unsetInterestFilter(static_cast<const RegisteredPrefixId*>(nullptr));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000430 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400431
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000432 face.unsetInterestFilter(static_cast<const InterestFilterId*>(nullptr));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000433 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400434}
435
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800436BOOST_FIXTURE_TEST_CASE(SetInterestFilterFail, FacesNoRegistrationReplyFixture)
437{
438 // don't enable registration reply
439 size_t nRegFailed = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800440 face.setInterestFilter("/Hello/World",
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000441 bind([] { BOOST_FAIL("Unexpected Interest"); }),
442 bind([] { BOOST_FAIL("Unexpected success of setInterestFilter"); }),
443 bind([&nRegFailed] { ++nRegFailed; }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800444
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000445 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800446 BOOST_CHECK_EQUAL(nRegFailed, 0);
447
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000448 advanceClocks(time::milliseconds(2000), 5);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800449 BOOST_CHECK_EQUAL(nRegFailed, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400450}
451
Joao Pereira0b3cac52015-07-02 14:49:49 -0400452BOOST_FIXTURE_TEST_CASE(SetInterestFilterFailWithoutSuccessCallback, FacesNoRegistrationReplyFixture)
453{
454 // don't enable registration reply
455 size_t nRegFailed = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800456 face.setInterestFilter("/Hello/World",
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000457 bind([] { BOOST_FAIL("Unexpected Interest"); }),
458 bind([&nRegFailed] { ++nRegFailed; }));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400459
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000460 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400461 BOOST_CHECK_EQUAL(nRegFailed, 0);
462
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000463 advanceClocks(time::milliseconds(2000), 5);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400464 BOOST_CHECK_EQUAL(nRegFailed, 1);
465}
466
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400467BOOST_AUTO_TEST_CASE(RegisterUnregisterPrefix)
468{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800469 size_t nRegSuccesses = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400470 const RegisteredPrefixId* regPrefixId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800471 face.registerPrefix("/Hello/World",
472 bind([&nRegSuccesses] { ++nRegSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000473 bind([] { BOOST_FAIL("Unexpected registerPrefix failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400474
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000475 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400476 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
477
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800478 size_t nUnregSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800479 face.unregisterPrefix(regPrefixId,
480 bind([&nUnregSuccesses] { ++nUnregSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000481 bind([] { BOOST_FAIL("Unexpected unregisterPrefix failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400482
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000483 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400484 BOOST_CHECK_EQUAL(nUnregSuccesses, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400485}
486
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800487BOOST_FIXTURE_TEST_CASE(RegisterUnregisterPrefixFail, FacesNoRegistrationReplyFixture)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400488{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800489 size_t nRegFailures = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800490 face.registerPrefix("/Hello/World",
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000491 bind([] { BOOST_FAIL("Unexpected registerPrefix success"); }),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800492 bind([&nRegFailures] { ++nRegFailures; }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400493
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000494 advanceClocks(time::milliseconds(5000), 20);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800495 BOOST_CHECK_EQUAL(nRegFailures, 1);
496}
497
498BOOST_AUTO_TEST_CASE(SimilarFilters)
499{
500 size_t nInInterests1 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800501 face.setInterestFilter("/Hello/World",
502 bind([&nInInterests1] { ++nInInterests1; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000503 nullptr,
504 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800505
506 size_t nInInterests2 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800507 face.setInterestFilter("/Hello",
508 bind([&nInInterests2] { ++nInInterests2; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000509 nullptr,
510 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400511
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800512 size_t nInInterests3 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800513 face.setInterestFilter("/Los/Angeles/Lakers",
514 bind([&nInInterests3] { ++nInInterests3; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000515 nullptr,
516 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400517
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000518 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400519
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000520 face.receive(Interest("/Hello/World/%21"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000521 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400522
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800523 BOOST_CHECK_EQUAL(nInInterests1, 1);
524 BOOST_CHECK_EQUAL(nInInterests2, 1);
525 BOOST_CHECK_EQUAL(nInInterests3, 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400526}
527
528BOOST_AUTO_TEST_CASE(SetRegexFilterError)
529{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800530 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
531 [] (const Name&, const Interest&) {
532 BOOST_FAIL("InterestFilter::Error should have been triggered");
533 },
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000534 nullptr,
535 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400536
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000537 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400538
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800539 BOOST_REQUIRE_THROW(face.receive(Interest("/Hello/World/XXX/b/c")), InterestFilter::Error);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400540}
541
542BOOST_AUTO_TEST_CASE(SetRegexFilter)
543{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800544 size_t nInInterests = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800545 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
546 bind([&nInInterests] { ++nInInterests; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000547 nullptr,
548 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400549
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000550 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400551
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800552 face.receive(Interest("/Hello/World/a")); // shouldn't match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400553 BOOST_CHECK_EQUAL(nInInterests, 0);
554
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800555 face.receive(Interest("/Hello/World/a/b")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400556 BOOST_CHECK_EQUAL(nInInterests, 1);
557
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800558 face.receive(Interest("/Hello/World/a/b/c")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400559 BOOST_CHECK_EQUAL(nInInterests, 2);
560
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800561 face.receive(Interest("/Hello/World/a/b/d")); // should not match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400562 BOOST_CHECK_EQUAL(nInInterests, 2);
563}
564
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400565BOOST_AUTO_TEST_CASE(SetRegexFilterAndRegister)
566{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800567 size_t nInInterests = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800568 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
569 bind([&nInInterests] { ++nInInterests; }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400570
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800571 size_t nRegSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800572 face.registerPrefix("/Hello/World",
573 bind([&nRegSuccesses] { ++nRegSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000574 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400575
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000576 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400577 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
578
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800579 face.receive(Interest("/Hello/World/a")); // shouldn't match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400580 BOOST_CHECK_EQUAL(nInInterests, 0);
581
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800582 face.receive(Interest("/Hello/World/a/b")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400583 BOOST_CHECK_EQUAL(nInInterests, 1);
584
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800585 face.receive(Interest("/Hello/World/a/b/c")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400586 BOOST_CHECK_EQUAL(nInInterests, 2);
587
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800588 face.receive(Interest("/Hello/World/a/b/d")); // should not match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400589 BOOST_CHECK_EQUAL(nInInterests, 2);
590}
591
Junxiao Shia1ea5062014-12-27 22:33:39 -0700592BOOST_FIXTURE_TEST_CASE(SetInterestFilterNoReg, FacesNoRegistrationReplyFixture) // Bug 2318
593{
594 // This behavior is specific to DummyClientFace.
595 // Regular Face won't accept incoming packets until something is sent.
596
597 int hit = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800598 face.setInterestFilter(Name("/"), bind([&hit] { ++hit; }));
599 face.processEvents(time::milliseconds(-1));
Junxiao Shia1ea5062014-12-27 22:33:39 -0700600
601 auto interest = make_shared<Interest>("/A");
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800602 face.receive(*interest);
603 face.processEvents(time::milliseconds(-1));
Junxiao Shia1ea5062014-12-27 22:33:39 -0700604
605 BOOST_CHECK_EQUAL(hit, 1);
606}
607
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000608BOOST_AUTO_TEST_SUITE_END() // Producer
609
Junxiao Shiae0b4182016-08-08 22:53:17 +0000610BOOST_AUTO_TEST_SUITE(IoRoutines)
611
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500612BOOST_AUTO_TEST_CASE(ProcessEvents)
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
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800616 size_t nRegSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800617 face.registerPrefix("/Hello/World",
618 bind([&nRegSuccesses] { ++nRegSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000619 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500620
621 // io_service::poll() without reset
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800622 face.getIoService().poll();
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500623 BOOST_CHECK_EQUAL(nRegSuccesses, 0);
624
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800625 face.processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500626 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
627}
628
Junxiao Shiae0b4182016-08-08 22:53:17 +0000629BOOST_AUTO_TEST_CASE(DestroyWithoutProcessEvents) // Bug 3248
630{
631 auto face2 = make_unique<Face>(io);
632 face2.reset();
633
634 io.poll(); // should not crash
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100635
636 // avoid "test case [...] did not check any assertions" message from Boost.Test
637 BOOST_CHECK(true);
Junxiao Shiae0b4182016-08-08 22:53:17 +0000638}
639
640BOOST_AUTO_TEST_SUITE_END() // IoRoutines
641
642BOOST_AUTO_TEST_SUITE(Transport)
643
644using ndn::Transport;
645
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700646struct PibDirWithDefaultTpm
647{
648 const std::string PATH = "build/keys-with-default-tpm";
649};
650
651BOOST_FIXTURE_TEST_CASE(FaceTransport, PibDirFixture<PibDirWithDefaultTpm>)
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800652{
653 KeyChain keyChain;
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700654 boost::asio::io_service io;
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800655
656 BOOST_CHECK(Face().getTransport() != nullptr);
657
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800658 BOOST_CHECK(Face(shared_ptr<Transport>()).getTransport() != nullptr);
659 BOOST_CHECK(Face(shared_ptr<Transport>(), io).getTransport() != nullptr);
660 BOOST_CHECK(Face(shared_ptr<Transport>(), io, keyChain).getTransport() != nullptr);
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800661
662 auto transport = make_shared<TcpTransport>("localhost", "6363"); // no real io operations will be scheduled
663 BOOST_CHECK(Face(transport).getTransport() == transport);
664 BOOST_CHECK(Face(transport, io).getTransport() == transport);
665 BOOST_CHECK(Face(transport, io, keyChain).getTransport() == transport);
666}
667
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700668class WithEnv : private IdentityManagementTimeFixture
669{
670public:
671 WithEnv()
672 {
673 if (getenv("NDN_CLIENT_TRANSPORT") != nullptr) {
674 m_oldTransport = getenv("NDN_CLIENT_TRANSPORT");
675 unsetenv("NDN_CLIENT_TRANSPORT");
676 }
677 }
678
679 void
680 configure(const std::string& faceUri)
681 {
682 setenv("NDN_CLIENT_TRANSPORT", faceUri.c_str(), true);
683 }
684
685 ~WithEnv()
686 {
687 if (!m_oldTransport.empty()) {
688 setenv("NDN_CLIENT_TRANSPORT", m_oldTransport.c_str(), true);
689 }
690 else {
691 unsetenv("NDN_CLIENT_TRANSPORT");
692 }
693 }
694
695private:
696 std::string m_oldTransport;
697};
698
699class WithConfig : private TestHomeFixture<DefaultPibDir>
700{
701public:
702 void
703 configure(const std::string& faceUri)
704 {
705 createClientConf({"transport=" + faceUri});
706 }
707};
708
709class WithEnvAndConfig : public WithEnv, public WithConfig
710{
711};
712
713typedef boost::mpl::vector<WithEnv, WithConfig> ConfigOptions;
714
715BOOST_FIXTURE_TEST_CASE(NoConfig, WithEnvAndConfig) // fixture configures test HOME and PIB/TPM path
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700716{
717 shared_ptr<Face> face;
718 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
719 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700720}
721
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700722BOOST_FIXTURE_TEST_CASE_TEMPLATE(Unix, T, ConfigOptions, T)
723{
724 this->configure("unix://some/path");
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700725
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700726 shared_ptr<Face> face;
727 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
728 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
729}
730
731BOOST_FIXTURE_TEST_CASE_TEMPLATE(Tcp, T, ConfigOptions, T)
732{
733 this->configure("tcp://127.0.0.1:6000");
734
735 shared_ptr<Face> face;
736 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
737 BOOST_CHECK(dynamic_pointer_cast<TcpTransport>(face->getTransport()) != nullptr);
738}
739
740BOOST_FIXTURE_TEST_CASE_TEMPLATE(WrongTransport, T, ConfigOptions, T)
741{
742 this->configure("wrong-transport:");
743
744 BOOST_CHECK_THROW(make_shared<Face>(), ConfigFile::Error);
745}
746
747BOOST_FIXTURE_TEST_CASE_TEMPLATE(WrongUri, T, ConfigOptions, T)
748{
749 this->configure("wrong-uri");
750
751 BOOST_CHECK_THROW(make_shared<Face>(), ConfigFile::Error);
752}
753
754BOOST_FIXTURE_TEST_CASE(EnvOverride, WithEnvAndConfig)
755{
756 this->WithEnv::configure("tcp://127.0.0.1:6000");
757 this->WithConfig::configure("unix://some/path");
758
759 shared_ptr<Face> face;
760 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
761 BOOST_CHECK(dynamic_pointer_cast<TcpTransport>(face->getTransport()) != nullptr);
762}
763
764BOOST_FIXTURE_TEST_CASE(ExplicitTransport, WithEnvAndConfig)
765{
766 this->WithEnv::configure("wrong-uri");
767 this->WithConfig::configure("wrong-transport:");
768
769 auto transport = make_shared<UnixTransport>("unix://some/path");
770 shared_ptr<Face> face;
771 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>(transport));
772 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
773}
774
Junxiao Shiae0b4182016-08-08 22:53:17 +0000775BOOST_AUTO_TEST_SUITE_END() // Transport
776
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700777BOOST_AUTO_TEST_SUITE_END() // TestFace
778
779} // namespace tests
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400780} // namespace ndn